From 28732a13b1e3d3d794f3062aeb6b17f1d12d5b90 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sun, 16 Dec 2018 06:46:39 -0600 Subject: [PATCH] TF_Input, TF_Output --- src/TensorFlowNET.Core/BaseSession.cs | 21 +++++++++++++++++++ src/TensorFlowNET.Core/TensorShape.cs | 7 +++---- .../Tensorflow/TF_Buffer.cs | 16 ++++++++++++++ src/TensorFlowNET.Core/Tensorflow/TF_Graph.cs | 13 ++++++++++++ src/TensorFlowNET.Core/Tensorflow/TF_Input.cs | 14 +++++++++++++ .../Tensorflow/TF_OperationDescription.cs | 14 +++++++++++++ .../Tensorflow/TF_Output.cs | 14 +++++++++++++ src/TensorFlowNET.Core/c_api.cs | 11 ++++++++-- src/TensorFlowNET.Core/gen_array_ops.cs | 10 +++++++++ src/TensorFlowNET.Core/ops.cs | 3 +-- .../{Tensorflow.cs => tf.cs} | 10 ++++++++- test/TensorFlowNET.Examples/HelloWorld.cs | 2 +- test/TensorFlowNET.UnitTest/GraphTest.cs | 2 +- test/TensorFlowNET.UnitTest/OperationsTest.cs | 8 ++++++- test/TensorFlowNET.UnitTest/VersionTest.cs | 1 - 15 files changed, 133 insertions(+), 13 deletions(-) create mode 100644 src/TensorFlowNET.Core/Tensorflow/TF_Buffer.cs create mode 100644 src/TensorFlowNET.Core/Tensorflow/TF_Graph.cs create mode 100644 src/TensorFlowNET.Core/Tensorflow/TF_Input.cs create mode 100644 src/TensorFlowNET.Core/Tensorflow/TF_OperationDescription.cs create mode 100644 src/TensorFlowNET.Core/Tensorflow/TF_Output.cs create mode 100644 src/TensorFlowNET.Core/gen_array_ops.cs rename src/TensorFlowNET.Core/{Tensorflow.cs => tf.cs} (89%) diff --git a/src/TensorFlowNET.Core/BaseSession.cs b/src/TensorFlowNET.Core/BaseSession.cs index 6b44b28e..2ba1fc02 100644 --- a/src/TensorFlowNET.Core/BaseSession.cs +++ b/src/TensorFlowNET.Core/BaseSession.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using Tensorflow; namespace TensorFlowNET.Core { @@ -34,6 +35,26 @@ namespace TensorFlowNET.Core public virtual byte[] run(Tensor fetches) { + return _run(fetches); + } + + private unsafe byte[] _run(Tensor fetches) + { + var status = new Status(); + + c_api.TF_SessionRun(_session, + run_options: null, + inputs: new TF_Input[] { }, + input_values: new IntPtr[] { }, + ninputs: 1, + outputs: new TF_Output[] { }, + output_values: new IntPtr[] { }, + noutputs: 1, + target_opers: new IntPtr[] { }, + ntargets: 1, + run_metadata: null, + status: status.Handle); + return null; } } diff --git a/src/TensorFlowNET.Core/TensorShape.cs b/src/TensorFlowNET.Core/TensorShape.cs index f67e5fac..7e6c7dd7 100644 --- a/src/TensorFlowNET.Core/TensorShape.cs +++ b/src/TensorFlowNET.Core/TensorShape.cs @@ -1,4 +1,5 @@ using Google.Protobuf.Collections; +using NumSharp.Core; using System; using System.Collections.Generic; using System.Text; @@ -7,11 +8,9 @@ using tensor_shape_pb2 = Tensorflow; namespace TensorFlowNET.Core { - public class TensorShape + public class TensorShape : Shape { - private int[] _dims; - - public TensorShape() + public TensorShape(params int[] shape) : base(shape) { } diff --git a/src/TensorFlowNET.Core/Tensorflow/TF_Buffer.cs b/src/TensorFlowNET.Core/Tensorflow/TF_Buffer.cs new file mode 100644 index 00000000..13e184c6 --- /dev/null +++ b/src/TensorFlowNET.Core/Tensorflow/TF_Buffer.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Text; +using size_t = System.IntPtr; + +namespace Tensorflow +{ + [StructLayout(LayoutKind.Sequential)] + public struct TF_Buffer + { + public IntPtr data; + public size_t length; + public IntPtr data_deallocator; + } +} diff --git a/src/TensorFlowNET.Core/Tensorflow/TF_Graph.cs b/src/TensorFlowNET.Core/Tensorflow/TF_Graph.cs new file mode 100644 index 00000000..bc71def4 --- /dev/null +++ b/src/TensorFlowNET.Core/Tensorflow/TF_Graph.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Text; + +namespace Tensorflow +{ + [StructLayout(LayoutKind.Sequential)] + public struct TF_Graph + { + + } +} diff --git a/src/TensorFlowNET.Core/Tensorflow/TF_Input.cs b/src/TensorFlowNET.Core/Tensorflow/TF_Input.cs new file mode 100644 index 00000000..4adcd040 --- /dev/null +++ b/src/TensorFlowNET.Core/Tensorflow/TF_Input.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Text; + +namespace Tensorflow +{ + [StructLayout(LayoutKind.Sequential)] + public struct TF_Input + { + public IntPtr oper; + public int index; + } +} diff --git a/src/TensorFlowNET.Core/Tensorflow/TF_OperationDescription.cs b/src/TensorFlowNET.Core/Tensorflow/TF_OperationDescription.cs new file mode 100644 index 00000000..9f04289b --- /dev/null +++ b/src/TensorFlowNET.Core/Tensorflow/TF_OperationDescription.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Text; + +namespace Tensorflow +{ + [StructLayout(LayoutKind.Sequential)] + public struct TF_OperationDescription + { + public IntPtr node_builder; + //public TF_Graph graph; + } +} diff --git a/src/TensorFlowNET.Core/Tensorflow/TF_Output.cs b/src/TensorFlowNET.Core/Tensorflow/TF_Output.cs new file mode 100644 index 00000000..98ec3d17 --- /dev/null +++ b/src/TensorFlowNET.Core/Tensorflow/TF_Output.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Text; + +namespace Tensorflow +{ + [StructLayout(LayoutKind.Sequential)] + public struct TF_Output + { + public IntPtr oper; + public int index; + } +} diff --git a/src/TensorFlowNET.Core/c_api.cs b/src/TensorFlowNET.Core/c_api.cs index ad052e23..f60d2007 100644 --- a/src/TensorFlowNET.Core/c_api.cs +++ b/src/TensorFlowNET.Core/c_api.cs @@ -5,7 +5,6 @@ using System.Text; using size_t = System.UIntPtr; using TF_Graph = System.IntPtr; -using TF_OperationDescription = System.IntPtr; using TF_Operation = System.IntPtr; using TF_Status = System.IntPtr; using TF_Tensor = System.IntPtr; @@ -14,7 +13,7 @@ using TF_SessionOptions = System.IntPtr; using TF_DataType = Tensorflow.DataType; using Tensorflow; -using static TensorFlowNET.Core.Tensorflow; +using static TensorFlowNET.Core.tf; namespace TensorFlowNET.Core { @@ -55,6 +54,14 @@ namespace TensorFlowNET.Core [DllImport(TensorFlowLibName)] public static extern unsafe void TF_SetAttrTensor(TF_OperationDescription desc, string attr_name, TF_Tensor value, TF_Status status); + [DllImport(TensorFlowLibName)] + public static extern unsafe void TF_SessionRun(TF_Session session, TF_Buffer* run_options, + TF_Input[] inputs, TF_Tensor[] input_values, + int ninputs, TF_Output[] outputs, + TF_Tensor[] output_values, int noutputs, + TF_Operation[] target_opers, int ntargets, + TF_Buffer* run_metadata, TF_Status status); + [DllImport(TensorFlowLibName)] public static extern unsafe void TF_SetAttrType(TF_OperationDescription desc, string attr_name, TF_DataType value); diff --git a/src/TensorFlowNET.Core/gen_array_ops.cs b/src/TensorFlowNET.Core/gen_array_ops.cs new file mode 100644 index 00000000..df131a8a --- /dev/null +++ b/src/TensorFlowNET.Core/gen_array_ops.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace TensorFlowNET.Core +{ + public static class gen_array_ops + { + } +} diff --git a/src/TensorFlowNET.Core/ops.cs b/src/TensorFlowNET.Core/ops.cs index 59a2d682..d5e5dccf 100644 --- a/src/TensorFlowNET.Core/ops.cs +++ b/src/TensorFlowNET.Core/ops.cs @@ -4,8 +4,7 @@ using System.Runtime.InteropServices; using System.Text; using System.Threading; using Tensorflow; -using tf = TensorFlowNET.Core.Tensorflow; -using TF_DataType = Tensorflow.DataType; +using tf = TensorFlowNET.Core.tf; using node_def_pb2 = Tensorflow; using Google.Protobuf; diff --git a/src/TensorFlowNET.Core/Tensorflow.cs b/src/TensorFlowNET.Core/tf.cs similarity index 89% rename from src/TensorFlowNET.Core/Tensorflow.cs rename to src/TensorFlowNET.Core/tf.cs index bfea4a4b..43fc140c 100644 --- a/src/TensorFlowNET.Core/Tensorflow.cs +++ b/src/TensorFlowNET.Core/tf.cs @@ -8,10 +8,18 @@ using Tensorflow; namespace TensorFlowNET.Core { - public static class Tensorflow + public static class tf { + public static Type float32 = typeof(float); + public delegate void Deallocator(IntPtr data, IntPtr size, IntPtr deallocatorData); + public static unsafe Tensor placeholder(Type dtype, TensorShape shape = null) + { + + return null; + } + public static unsafe Tensor constant(object value) { var g = ops.get_default_graph(); diff --git a/test/TensorFlowNET.Examples/HelloWorld.cs b/test/TensorFlowNET.Examples/HelloWorld.cs index b3991160..06d67a40 100644 --- a/test/TensorFlowNET.Examples/HelloWorld.cs +++ b/test/TensorFlowNET.Examples/HelloWorld.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using System.Text; -using tf = TensorFlowNET.Core.Tensorflow; +using tf = TensorFlowNET.Core.tf; namespace TensorFlowNET.Examples { diff --git a/test/TensorFlowNET.UnitTest/GraphTest.cs b/test/TensorFlowNET.UnitTest/GraphTest.cs index d184c21e..bfffddda 100644 --- a/test/TensorFlowNET.UnitTest/GraphTest.cs +++ b/test/TensorFlowNET.UnitTest/GraphTest.cs @@ -2,7 +2,7 @@ using System; using System.Collections.Generic; using System.Text; -using tf = TensorFlowNET.Core.Tensorflow; +using TensorFlowNET.Core; namespace TensorFlowNET.UnitTest { diff --git a/test/TensorFlowNET.UnitTest/OperationsTest.cs b/test/TensorFlowNET.UnitTest/OperationsTest.cs index 352d282b..ce58f81a 100644 --- a/test/TensorFlowNET.UnitTest/OperationsTest.cs +++ b/test/TensorFlowNET.UnitTest/OperationsTest.cs @@ -2,7 +2,7 @@ using System; using System.Collections.Generic; using System.Text; -using tf = TensorFlowNET.Core.Tensorflow; +using TensorFlowNET.Core; namespace TensorFlowNET.UnitTest { @@ -14,5 +14,11 @@ namespace TensorFlowNET.UnitTest { tf.constant(4.0); } + + [TestMethod] + public void placeholder() + { + var x = tf.placeholder(tf.float32, shape: new TensorShape(1024, 1024)); + } } } diff --git a/test/TensorFlowNET.UnitTest/VersionTest.cs b/test/TensorFlowNET.UnitTest/VersionTest.cs index a0e723d6..18d3ecec 100644 --- a/test/TensorFlowNET.UnitTest/VersionTest.cs +++ b/test/TensorFlowNET.UnitTest/VersionTest.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; using System.Text; using TensorFlowNET.Core; -using tf = TensorFlowNET.Core.Tensorflow; namespace TensorFlowNET.UnitTest {