FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

add ops._create_c_op · attackgithub/TensorFlow.NET@1291aa5 · GitHub

Repository navigation

Commit 1291aa5

Browse files
committed
add ops._create_c_op
1 parent c102715 commit 1291aa5

13 files changed

Lines changed: 199 additions & 30 deletions

File tree

‎TensorFlow.NET.sln‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.UnitTest", "t
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Core", "src\TensorFlowNET.Core\TensorFlowNET.Core.csproj", "{1B1BC950-2CB0-48E2-B4CD-8172AFF67A10}"
99
EndProject
10-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj", "{D314AE83-D586-4589-B653-1937F1138A5A}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Examples", "test\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj", "{1FE60088-157C-4140-91AB-E96B915E4BAE}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TensorFlowNET.Examples", "test\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj", "{1FE60088-157C-4140-91AB-E96B915E4BAE}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj", "{1D4158AC-0ED2-46D3-9532-E92232A10743}"
1313
EndProject
1414
Global
1515
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -25,14 +25,14 @@ Global
2525
{1B1BC950-2CB0-48E2-B4CD-8172AFF67A10}.Debug|Any CPU.Build.0 = Debug|Any CPU
2626
{1B1BC950-2CB0-48E2-B4CD-8172AFF67A10}.Release|Any CPU.ActiveCfg = Release|Any CPU
2727
{1B1BC950-2CB0-48E2-B4CD-8172AFF67A10}.Release|Any CPU.Build.0 = Release|Any CPU
28-
{D314AE83-D586-4589-B653-1937F1138A5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29-
{D314AE83-D586-4589-B653-1937F1138A5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
30-
{D314AE83-D586-4589-B653-1937F1138A5A}.Release|Any CPU.ActiveCfg = Release|Any CPU
31-
{D314AE83-D586-4589-B653-1937F1138A5A}.Release|Any CPU.Build.0 = Release|Any CPU
3228
{1FE60088-157C-4140-91AB-E96B915E4BAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
3329
{1FE60088-157C-4140-91AB-E96B915E4BAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
3430
{1FE60088-157C-4140-91AB-E96B915E4BAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
3531
{1FE60088-157C-4140-91AB-E96B915E4BAE}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{1D4158AC-0ED2-46D3-9532-E92232A10743}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{1D4158AC-0ED2-46D3-9532-E92232A10743}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{1D4158AC-0ED2-46D3-9532-E92232A10743}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{1D4158AC-0ED2-46D3-9532-E92232A10743}.Release|Any CPU.Build.0 = Release|Any CPU
3636
EndGlobalSection
3737
GlobalSection(SolutionProperties) = preSolution
3838
HideSolutionNode = FALSE

‎src/TensorFlowNET.Core/Graph.cs‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Runtime.InteropServices;
34
using System.Text;
45

56
namespace TensorFlowNET.Core
@@ -12,11 +13,21 @@ namespace TensorFlowNET.Core
1213
/// </summary>
1314
public class Graph
1415
{
15-
public IntPtr TFGraph { get; set; }
16+
public IntPtr handle;
1617

17-
public Operation create_op()
18+
public Graph(IntPtr graph)
1819
{
19-
var op = new Operation(this);
20+
this.handle = graph;
21+
}
22+
23+
public unsafe Operation create_op(object inputs, string op_type = "", string name = "")
24+
{
25+
if (String.IsNullOrEmpty(name))
26+
{
27+
op_type = name;
28+
}
29+
30+
var op = new Operation(this, inputs);
2031

2132
return op;
2233
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace TensorFlowNET.Core
6+
{
7+
public sealed class MonoPInvokeCallbackAttribute : Attribute
8+
{
9+
/// <summary>
10+
/// Use this constructor to annotate the type of the callback function that
11+
/// will be invoked from unmanaged code.
12+
/// </summary>
13+
/// <param name="t">T.</param>
14+
public MonoPInvokeCallbackAttribute(Type t) { }
15+
}
16+
}

‎src/TensorFlowNET.Core/Operation.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ namespace TensorFlowNET.Core
77
public class Operation
88
{
99
private Graph _graph;
10+
private IntPtr _c_op;
1011

11-
public Operation(Graph g)
12+
public Operation(Graph g, object inputs)
1213
{
1314
_graph = g;
15+
16+
_c_op = ops._create_c_op(g, inputs);
1417
}
1518
}
1619
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace TensorFlowNET.Core
6+
{
7+
/// <summary>
8+
/// TF_DataType holds the type for a scalar value. E.g., one slot in a tensor.
9+
/// The enum values here are identical to corresponding values in types.proto.
10+
/// </summary>
11+
public enum TF_DataType
12+
{
13+
TF_FLOAT = 1,
14+
TF_DOUBLE = 2,
15+
TF_INT32 = 3, // Int32 tensors are always in 'host' memory.
16+
TF_UINT8 = 4,
17+
TF_INT16 = 5,
18+
TF_INT8 = 6,
19+
TF_STRING = 7,
20+
TF_COMPLEX64 = 8, // Single-precision complex
21+
TF_COMPLEX = 8, // Old identifier kept for API backwards compatibility
22+
TF_INT64 = 9,
23+
TF_BOOL = 10,
24+
TF_QINT8 = 11, // Quantized int8
25+
TF_QUINT8 = 12, // Quantized uint8
26+
TF_QINT32 = 13, // Quantized int32
27+
TF_BFLOAT16 = 14, // Float32 truncated to 16 bits. Only for cast ops.
28+
TF_QINT16 = 15, // Quantized int16
29+
TF_QUINT16 = 16, // Quantized uint16
30+
TF_UINT16 = 17,
31+
TF_COMPLEX128 = 18, // Double-precision complex
32+
TF_HALF = 19,
33+
TF_RESOURCE = 20,
34+
TF_VARIANT = 21,
35+
TF_UINT32 = 22,
36+
TF_UINT64 = 23,
37+
}
38+
}

‎src/TensorFlowNET.Core/Tensor.cs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace TensorFlowNET.Core
6+
{
7+
public class Tensor
8+
{
9+
}
10+
}

‎src/TensorFlowNET.Core/Tensorflow.cs‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,35 @@
33
using System.Runtime.InteropServices;
44
using System.Text;
55

6+
7+
68
namespace TensorFlowNET.Core
79
{
810
public static class Tensorflow
911
{
10-
public const string TensorFlowLibName = "libtensorflow";
11-
12-
[DllImport(TensorFlowLibName)]
13-
public static extern unsafe IntPtr TF_Version();
12+
public delegate void Deallocator(IntPtr data, IntPtr size, IntPtr deallocatorData);
1413

15-
public static string VERSION => Marshal.PtrToStringAnsi(TF_Version());
14+
public static unsafe Tensor constant(object value)
15+
{
16+
var g = ops.get_default_graph();
17+
g.create_op(value, "Const");
1618

17-
[DllImport(TensorFlowLibName)]
18-
static extern unsafe IntPtr TF_NewOperation(IntPtr graph, string opType, string oper_name);
19+
return new Tensor();
20+
}
1921

20-
[DllImport(TensorFlowLibName)]
21-
static extern unsafe IntPtr TF_FinishOperation(IntPtr desc, IntPtr status);
22+
public static Deallocator FreeTensorDataDelegate = FreeTensorData;
2223

23-
public static IntPtr constant<T>(T value)
24+
[MonoPInvokeCallback(typeof(Deallocator))]
25+
internal static void FreeTensorData(IntPtr data, IntPtr len, IntPtr closure)
2426
{
25-
var g = Graph();
26-
return TF_NewOperation(g.TFGraph, "Const", "Const");
27+
Marshal.FreeHGlobal(data);
2728
}
2829

29-
[DllImport(TensorFlowLibName)]
30-
static extern unsafe IntPtr TF_NewGraph();
30+
public static string VERSION => Marshal.PtrToStringAnsi(c_api.TF_Version());
3131

3232
public static Graph Graph()
3333
{
34-
Graph g = new Graph();
35-
g.TFGraph = TF_NewGraph();
34+
Graph g = new Graph(c_api.TF_NewGraph());
3635
return g;
3736
}
3837
}

‎src/TensorFlowNET.Core/c_api.cs‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.InteropServices;
4+
using System.Text;
5+
6+
using size_t = System.UIntPtr;
7+
using TF_Graph = System.IntPtr;
8+
using TF_OperationDescription = System.IntPtr;
9+
using TF_Operation = System.IntPtr;
10+
using TF_Status = System.IntPtr;
11+
using TF_Tensor = System.IntPtr;
12+
13+
using static TensorFlowNET.Core.Tensorflow;
14+
15+
namespace TensorFlowNET.Core
16+
{
17+
public static class c_api
18+
{
19+
public const string TensorFlowLibName = "libtensorflow";
20+
21+
[DllImport(TensorFlowLibName)]
22+
public static unsafe extern TF_Operation TF_FinishOperation(TF_OperationDescription desc, TF_Status status);
23+
24+
[DllImport(TensorFlowLibName)]
25+
public static unsafe extern IntPtr TF_NewGraph();
26+
27+
[DllImport(TensorFlowLibName)]
28+
public static unsafe extern TF_OperationDescription TF_NewOperation(TF_Graph graph, string opType, string oper_name);
29+
30+
[DllImport(TensorFlowLibName)]
31+
public static unsafe extern TF_Status TF_NewStatus();
32+
33+
[DllImport(TensorFlowLibName)]
34+
public static extern unsafe TF_Tensor TF_NewTensor(TF_DataType dataType, IntPtr zeroDims, int num_dims, IntPtr data, size_t len, Deallocator deallocator, IntPtr deallocator_arg);
35+
36+
[DllImport(TensorFlowLibName)]
37+
public static extern unsafe void TF_SetAttrTensor(TF_OperationDescription desc, string attr_name, TF_Tensor value, TF_Status status);
38+
39+
[DllImport(TensorFlowLibName)]
40+
public static extern unsafe void TF_SetAttrType(TF_OperationDescription desc, string attr_name, TF_DataType value);
41+
42+
[DllImport(TensorFlowLibName)]
43+
public static unsafe extern IntPtr TF_Version();
44+
}
45+
}

‎src/TensorFlowNET.Core/ops.cs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.InteropServices;
4+
using System.Text;
5+
using System.Threading;
6+
using tf = TensorFlowNET.Core.Tensorflow;
7+
8+
namespace TensorFlowNET.Core
9+
{
10+
public static class ops
11+
{
12+
public static Graph get_default_graph()
13+
{
14+
return tf.Graph();
15+
}
16+
17+
public static unsafe IntPtr _create_c_op(Graph graph, object inputs)
18+
{
19+
var op_desc = c_api.TF_NewOperation(graph.handle, "Const", "Const0");
20+
var status = c_api.TF_NewStatus();
21+
22+
IntPtr tensor = IntPtr.Zero;
23+
24+
switch (inputs)
25+
{
26+
case double value:
27+
var v = (double*)Marshal.AllocHGlobal(sizeof(double));
28+
*v = value;
29+
tensor = c_api.TF_NewTensor(TF_DataType.TF_DOUBLE, IntPtr.Zero, 0, data: (IntPtr)v, len: (UIntPtr)sizeof(double), deallocator: Tensorflow.FreeTensorDataDelegate, deallocator_arg: IntPtr.Zero);
30+
c_api.TF_SetAttrType(op_desc, "dtype", TF_DataType.TF_DOUBLE);
31+
break;
32+
}
33+
34+
c_api.TF_SetAttrTensor(op_desc, "value", tensor, status);
35+
36+
var c_op = c_api.TF_FinishOperation(op_desc, status);
37+
38+
return c_op;
39+
}
40+
}
41+
}

‎test/TensorFlowNET.Examples/HelloWorld.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public void Run()
1818
1919
The value returned by the constructor represents the output
2020
of the Constant op.*/
21-
var hello = tf.constant("Hello, TensorFlow!");
21+
var hello = tf.constant(4.0);
22+
//var hello = tf.constant("Hello, TensorFlow!");
2223

2324
// Start tf session
2425
// var sess = tf.Session();

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL