From c12b5b9d58045278af0e8943e0964fcea5a46fd8 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Thu, 1 Aug 2019 18:43:35 -0500 Subject: [PATCH] Removed shared Status instance in Operation. --- .../Operations/Operation.Input.cs | 13 +++++++++- .../Operations/Operation.Output.cs | 13 +++++++++- .../Operations/Operation.cs | 25 ++++++++++++------- .../Tensors/c_api.tensor.cs | 2 +- test/TensorFlowNET.UnitTest/TensorTest.cs | 6 ++--- 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/TensorFlowNET.Core/Operations/Operation.Input.cs b/src/TensorFlowNET.Core/Operations/Operation.Input.cs index 83e7567f..6d6403c9 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.Input.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.Input.cs @@ -26,7 +26,18 @@ namespace Tensorflow { public TF_Output Input(int index) => c_api.TF_OperationInput(new TF_Input(_handle, index)); public TF_DataType InputType(int index) => c_api.TF_OperationInputType(new TF_Input(_handle, index)); - public int InputListLength(string name) => c_api.TF_OperationInputListLength(_handle, name, status); + + public int InputListLength(string name) + { + int num = 0; + using(var status = new Status()) + { + num = c_api.TF_OperationInputListLength(_handle, name, status); + status.Check(true); + } + return num; + } + public int NumInputs => c_api.TF_OperationNumInputs(_handle); private TF_DataType[] _input_types => _inputs._inputs.Select(x => x.dtype).ToArray(); diff --git a/src/TensorFlowNET.Core/Operations/Operation.Output.cs b/src/TensorFlowNET.Core/Operations/Operation.Output.cs index 41f4a332..24348322 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.Output.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.Output.cs @@ -24,7 +24,18 @@ namespace Tensorflow { public int NumOutputs => c_api.TF_OperationNumOutputs(_handle); public TF_DataType OutputType(int index) => c_api.TF_OperationOutputType(new TF_Output(_handle, index)); - public int OutputListLength(string name) => c_api.TF_OperationOutputListLength(_handle, name, status); + + public int OutputListLength(string name) + { + int num = 0; + using (var status = new Status()) + { + num = c_api.TF_OperationOutputListLength(_handle, name, status); + status.Check(true); + } + + return num; + } private Tensor[] _outputs; public Tensor[] outputs => _outputs; diff --git a/src/TensorFlowNET.Core/Operations/Operation.cs b/src/TensorFlowNET.Core/Operations/Operation.cs index b673380b..8c4ce606 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.cs @@ -54,7 +54,6 @@ namespace Tensorflow public Operation op => this; public TF_DataType dtype => TF_DataType.DtInvalid; - private Status status = new Status(); public string name => _handle == IntPtr.Zero ? null : c_api.StringPiece(c_api.TF_OperationName(_handle)); public string OpType => c_api.StringPiece(c_api.TF_OperationOpType(_handle)); @@ -96,10 +95,14 @@ namespace Tensorflow _operDesc = c_api.TF_NewOperation(g, opType, oper_name); c_api.TF_SetAttrType(_operDesc, "dtype", TF_DataType.TF_INT32); - _handle = c_api.TF_FinishOperation(_operDesc, status); - - // Dict mapping op name to file and line information for op colocation - // context managers. + using (var status = new Status()) + { + _handle = c_api.TF_FinishOperation(_operDesc, status); + status.Check(true); + } + + // Dict mapping op name to file and line information for op colocation + // context managers. _control_flow_context = graph._get_control_flow_context(); } @@ -220,6 +223,7 @@ namespace Tensorflow { AttrValue x = null; + using (var status = new Status()) using (var buf = new Buffer()) { c_api.TF_OperationGetAttrValueProto(_handle, name, buf, status); @@ -274,12 +278,15 @@ namespace Tensorflow var output = tensor._as_tf_output(); // Reset cached inputs. - _inputs = null; + _inputs = null; // after the c_api call next time _inputs is accessed // the updated inputs are reloaded from the c_api - c_api.UpdateEdge(_graph, output, input, status); - //var updated_inputs = inputs; - status.Check(); + using (var status = new Status()) + { + c_api.UpdateEdge(_graph, output, input, status); + //var updated_inputs = inputs; + status.Check(); + } } private void _assert_same_graph(Tensor tensor) diff --git a/src/TensorFlowNET.Core/Tensors/c_api.tensor.cs b/src/TensorFlowNET.Core/Tensors/c_api.tensor.cs index fd240ee7..324de913 100644 --- a/src/TensorFlowNET.Core/Tensors/c_api.tensor.cs +++ b/src/TensorFlowNET.Core/Tensors/c_api.tensor.cs @@ -33,7 +33,7 @@ namespace Tensorflow public static extern IntPtr TF_AllocateTensor(TF_DataType dtype, IntPtr dims, int num_dims, UIntPtr len); [DllImport(TensorFlowLibName)] - public static extern IntPtr TF_AllocateTensor(TF_DataType dtype, long[] dims, int num_dims, UIntPtr len); + public static extern IntPtr TF_AllocateTensor(TF_DataType dtype, long[] dims, int num_dims, ulong len); /// /// returns the sizeof() for the underlying type corresponding to the given TF_DataType enum value. diff --git a/test/TensorFlowNET.UnitTest/TensorTest.cs b/test/TensorFlowNET.UnitTest/TensorTest.cs index 9f4fff39..6008a809 100644 --- a/test/TensorFlowNET.UnitTest/TensorTest.cs +++ b/test/TensorFlowNET.UnitTest/TensorTest.cs @@ -77,14 +77,14 @@ namespace TensorFlowNET.UnitTest [TestMethod] public void AllocateTensor() { - /*ulong num_bytes = 6 * sizeof(float); + ulong num_bytes = 6 * sizeof(float); long[] dims = { 2, 3 }; Tensor t = c_api.TF_AllocateTensor(TF_DataType.TF_FLOAT, dims, 2, num_bytes); EXPECT_EQ(TF_DataType.TF_FLOAT, t.dtype); EXPECT_EQ(2, t.NDims); - Assert.IsTrue(Enumerable.SequenceEqual(dims, t.shape)); + EXPECT_EQ((int)dims[0], t.shape[0]); EXPECT_EQ(num_bytes, t.bytesize); - t.Dispose();*/ + t.Dispose(); }