From 7a30362cdb77e0550c7da0304d0683ae70c310fe Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Tue, 4 Feb 2020 18:10:12 -0800 Subject: [PATCH] Implement SafeStatusHandle as a wrapper for TF_Status --- .../Attributes/c_api.ops.cs | 10 ++-- src/TensorFlowNET.Core/Device/c_api.device.cs | 6 +-- src/TensorFlowNET.Core/Eager/Context.cs | 2 +- src/TensorFlowNET.Core/Eager/EagerTensor.cs | 12 ++--- src/TensorFlowNET.Core/Eager/c_api.eager.cs | 36 +++++++------- .../Eager/wrap_tfe_src.TFE_Execute.cs | 8 ++-- .../Eager/wrap_tfe_src.TFE_FastPathExecute.cs | 16 +++---- .../Framework/Models/ScopedTFStatus.cs | 9 ---- src/TensorFlowNET.Core/Framework/importer.cs | 2 +- .../Functions/c_api.function.cs | 2 +- .../Gradients/c_api.gradient.cs | 2 +- .../GraphTransformation/GraphTransformer.cs | 2 +- .../c_api.transform_graph.cs | 2 +- src/TensorFlowNET.Core/Graphs/Graph.Export.cs | 2 +- src/TensorFlowNET.Core/Graphs/Graph.Import.cs | 4 +- .../Graphs/Graph.Operation.cs | 4 +- src/TensorFlowNET.Core/Graphs/Graph.cs | 4 +- src/TensorFlowNET.Core/Graphs/c_api.graph.cs | 20 ++++---- .../Operations/Operation.Input.cs | 2 +- .../Operations/Operation.Output.cs | 2 +- .../Operations/Operation.cs | 8 ++-- .../Operations/OperationDescription.cs | 2 +- .../Operations/c_api.ops.cs | 8 ++-- .../Sessions/BaseSession.cs | 6 +-- src/TensorFlowNET.Core/Sessions/Session.cs | 4 +- .../Sessions/SessionOptions.cs | 2 +- .../Sessions/c_api.session.cs | 8 ++-- .../Status/SafeStatusHandle.cs | 39 +++++++++++++++ src/TensorFlowNET.Core/Status/Status.cs | 36 +++++++++----- src/TensorFlowNET.Core/Status/c_api.status.cs | 8 ++-- .../Tensors/Tensor.Conversions.cs | 2 +- .../Tensors/Tensor.Creation.cs | 10 ++-- .../Tensors/Tensor.Value.cs | 2 +- src/TensorFlowNET.Core/Tensors/Tensor.cs | 8 ++-- .../Tensors/c_api.tensor.cs | 8 ++-- src/TensorFlowNET.Core/ops.cs | 4 +- .../CApiAttributesTestcs.cs | 4 +- .../CApiColocationTest.cs | 2 +- .../CApiGradientsTest.cs | 4 +- test/TensorFlowNET.UnitTest/CApiTest.cs | 48 +++++++++---------- test/TensorFlowNET.UnitTest/CSession.cs | 2 +- test/TensorFlowNET.UnitTest/ConstantTest.cs | 2 +- .../Eager/CApi.Eager.Context.cs | 3 +- .../Eager/CApi.Eager.Execute_MatMul_CPU.cs | 3 +- .../CApi.Eager.OpGetInputAndOutputLengths.cs | 3 +- ...pi.Eager.OpInferMixedTypeInputListAttrs.cs | 3 +- .../Eager/CApi.Eager.Variables.cs | 3 +- .../Eager/CApi.Eager.cs | 20 +++----- test/TensorFlowNET.UnitTest/GraphTest.cs | 14 +++--- test/TensorFlowNET.UnitTest/TensorTest.cs | 32 ++++++------- test/TensorFlowNET.UnitTest/c_test_util.cs | 14 +++--- 51 files changed, 244 insertions(+), 215 deletions(-) delete mode 100644 src/TensorFlowNET.Core/Framework/Models/ScopedTFStatus.cs create mode 100644 src/TensorFlowNET.Core/Status/SafeStatusHandle.cs diff --git a/src/TensorFlowNET.Core/Attributes/c_api.ops.cs b/src/TensorFlowNET.Core/Attributes/c_api.ops.cs index 77877a43..0bca1bba 100644 --- a/src/TensorFlowNET.Core/Attributes/c_api.ops.cs +++ b/src/TensorFlowNET.Core/Attributes/c_api.ops.cs @@ -32,7 +32,7 @@ namespace Tensorflow /// TF_Status* /// [DllImport(TensorFlowLibName)] - public static extern TF_AttrMetadata TF_OperationGetAttrMetadata(IntPtr oper, string attr_name, IntPtr status); + public static extern TF_AttrMetadata TF_OperationGetAttrMetadata(IntPtr oper, string attr_name, SafeStatusHandle status); /// /// Fills in `value` with the value of the attribute `attr_name`. `value` must @@ -46,7 +46,7 @@ namespace Tensorflow /// size_t /// TF_Status* [DllImport(TensorFlowLibName)] - public static extern void TF_OperationGetAttrString(IntPtr oper, string attr_name, IntPtr value, uint max_length, IntPtr status); + public static extern void TF_OperationGetAttrString(IntPtr oper, string attr_name, IntPtr value, uint max_length, SafeStatusHandle status); /// /// Sets `output_attr_value` to the binary-serialized AttrValue proto @@ -55,13 +55,13 @@ namespace Tensorflow /// /// [DllImport(TensorFlowLibName)] - public static extern int TF_OperationGetAttrValueProto(IntPtr oper, string attr_name, IntPtr output_attr_value, IntPtr status); + public static extern int TF_OperationGetAttrValueProto(IntPtr oper, string attr_name, IntPtr output_attr_value, SafeStatusHandle status); [DllImport(TensorFlowLibName)] public static extern void TF_SetAttrBool(IntPtr desc, string attr_name, bool value); [DllImport(TensorFlowLibName)] - public static extern void TF_SetAttrValueProto(IntPtr desc, string attr_name, IntPtr proto, uint proto_len, IntPtr status); + public static extern void TF_SetAttrValueProto(IntPtr desc, string attr_name, IntPtr proto, uint proto_len, SafeStatusHandle status); /// /// Set `num_dims` to -1 to represent "unknown rank". @@ -99,7 +99,7 @@ namespace Tensorflow public static extern void TF_SetAttrStringList(IntPtr desc, string attr_name, IntPtr[] values, uint[] lengths, int num_values); [DllImport(TensorFlowLibName)] - public static extern void TF_SetAttrTensor(IntPtr desc, string attr_name, IntPtr value, IntPtr status); + public static extern void TF_SetAttrTensor(IntPtr desc, string attr_name, IntPtr value, SafeStatusHandle status); [DllImport(TensorFlowLibName)] public static extern void TF_SetAttrType(IntPtr desc, string attr_name, TF_DataType value); diff --git a/src/TensorFlowNET.Core/Device/c_api.device.cs b/src/TensorFlowNET.Core/Device/c_api.device.cs index f2289cee..698aa227 100644 --- a/src/TensorFlowNET.Core/Device/c_api.device.cs +++ b/src/TensorFlowNET.Core/Device/c_api.device.cs @@ -45,7 +45,7 @@ namespace Tensorflow /// TF_Status* /// [DllImport(TensorFlowLibName)] - public static extern IntPtr TF_DeviceListType(IntPtr list, int index, IntPtr status); + public static extern IntPtr TF_DeviceListType(IntPtr list, int index, SafeStatusHandle status); /// /// Deallocates the device list. @@ -64,7 +64,7 @@ namespace Tensorflow /// TF_Status* /// TFE_TensorHandle* [DllImport(TensorFlowLibName)] - public static extern IntPtr TFE_TensorHandleCopyToDevice(IntPtr h, IntPtr ctx, string device_name, IntPtr status); + public static extern IntPtr TFE_TensorHandleCopyToDevice(IntPtr h, IntPtr ctx, string device_name, SafeStatusHandle status); /// /// Retrieves the full name of the device (e.g. /job:worker/replica:0/...) @@ -76,6 +76,6 @@ namespace Tensorflow /// /// TF_Status* [DllImport(TensorFlowLibName)] - public static extern IntPtr TF_DeviceListName(IntPtr list, int index, IntPtr status); + public static extern IntPtr TF_DeviceListName(IntPtr list, int index, SafeStatusHandle status); } } diff --git a/src/TensorFlowNET.Core/Eager/Context.cs b/src/TensorFlowNET.Core/Eager/Context.cs index ca01361d..8dda3ea0 100644 --- a/src/TensorFlowNET.Core/Eager/Context.cs +++ b/src/TensorFlowNET.Core/Eager/Context.cs @@ -14,7 +14,7 @@ namespace Tensorflow.Eager public Context(ContextOptions opts, Status status) { - _handle = c_api.TFE_NewContext(opts, status); + _handle = c_api.TFE_NewContext(opts, status.Handle); status.Check(true); } diff --git a/src/TensorFlowNET.Core/Eager/EagerTensor.cs b/src/TensorFlowNET.Core/Eager/EagerTensor.cs index 85cc731e..7ebcaaaf 100644 --- a/src/TensorFlowNET.Core/Eager/EagerTensor.cs +++ b/src/TensorFlowNET.Core/Eager/EagerTensor.cs @@ -12,32 +12,32 @@ namespace Tensorflow.Eager public EagerTensor(IntPtr handle) : base(handle) { tfe_tensor_handle = handle; - _handle = c_api.TFE_TensorHandleResolve(handle, status); + _handle = c_api.TFE_TensorHandleResolve(handle, status.Handle); } public EagerTensor(string value, string device_name) : base(value) { - tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status); + tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status.Handle); } public EagerTensor(int value, string device_name) : base(value) { - tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status); + tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status.Handle); } public EagerTensor(float[] value, string device_name) : base(value) { - tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status); + tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status.Handle); } public EagerTensor(double[] value, string device_name) : base(value) { - tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status); + tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status.Handle); } public EagerTensor(NDArray value, string device_name) : base(value) { - tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status); + tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status.Handle); } public override string ToString() diff --git a/src/TensorFlowNET.Core/Eager/c_api.eager.cs b/src/TensorFlowNET.Core/Eager/c_api.eager.cs index bb75f153..77746d7b 100644 --- a/src/TensorFlowNET.Core/Eager/c_api.eager.cs +++ b/src/TensorFlowNET.Core/Eager/c_api.eager.cs @@ -30,7 +30,7 @@ namespace Tensorflow /// TF_Status* /// [DllImport(TensorFlowLibName)] - public static extern TF_AttrType TFE_OpGetAttrType(IntPtr op, string attr_name, ref byte is_list, IntPtr status); + public static extern TF_AttrType TFE_OpGetAttrType(IntPtr op, string attr_name, ref byte is_list, SafeStatusHandle status); /// /// Returns the length (number of tensors) of the input argument `input_name` @@ -40,7 +40,7 @@ namespace Tensorflow /// const char* /// TF_Status* [DllImport(TensorFlowLibName)] - public static extern int TFE_OpGetInputLength(IntPtr op, string input_name, IntPtr status); + public static extern int TFE_OpGetInputLength(IntPtr op, string input_name, SafeStatusHandle status); /// /// Returns the length (number of tensors) of the output argument `output_name` @@ -51,7 +51,7 @@ namespace Tensorflow /// /// [DllImport(TensorFlowLibName)] - public static extern int TFE_OpGetOutputLength(IntPtr op, string input_name, IntPtr status); + public static extern int TFE_OpGetOutputLength(IntPtr op, string input_name, SafeStatusHandle status); /// /// @@ -62,7 +62,7 @@ namespace Tensorflow /// TF_Status* /// [DllImport(TensorFlowLibName)] - public static extern int TFE_OpAddInputList(IntPtr op, IntPtr[] inputs, int num_inputs, IntPtr status); + public static extern int TFE_OpAddInputList(IntPtr op, IntPtr[] inputs, int num_inputs, SafeStatusHandle status); /// /// @@ -71,7 +71,7 @@ namespace Tensorflow /// TF_Status* /// TFE_Context* [DllImport(TensorFlowLibName)] - public static extern TFE_Context TFE_NewContext(IntPtr opts, IntPtr status); + public static extern TFE_Context TFE_NewContext(IntPtr opts, SafeStatusHandle status); /// /// @@ -89,7 +89,7 @@ namespace Tensorflow /// int* /// TF_Status* [DllImport(TensorFlowLibName)] - public static extern void TFE_Execute(IntPtr op, IntPtr[] retvals, ref int num_retvals, IntPtr status); + public static extern void TFE_Execute(IntPtr op, IntPtr[] retvals, ref int num_retvals, SafeStatusHandle status); /// /// @@ -99,7 +99,7 @@ namespace Tensorflow /// TF_Status* /// [DllImport(TensorFlowLibName)] - public static extern TFE_Op TFE_NewOp(IntPtr ctx, string op_or_function_name, IntPtr status); + public static extern TFE_Op TFE_NewOp(IntPtr ctx, string op_or_function_name, SafeStatusHandle status); /// /// @@ -109,7 +109,7 @@ namespace Tensorflow /// TF_Status* /// TFE_Op* [DllImport(TensorFlowLibName)] - public static extern void TFE_OpReset(IntPtr ctx, string op_or_function_name, IntPtr status, IntPtr op_to_reset); + public static extern void TFE_OpReset(IntPtr ctx, string op_or_function_name, SafeStatusHandle status, IntPtr op_to_reset); /// /// @@ -139,7 +139,7 @@ namespace Tensorflow /// const int /// TF_Status* [DllImport(TensorFlowLibName)] - public static extern void TFE_OpSetAttrShape(IntPtr op, string attr_name, long[] dims, int num_dims, IntPtr out_status); + public static extern void TFE_OpSetAttrShape(IntPtr op, string attr_name, long[] dims, int num_dims, SafeStatusHandle out_status); [DllImport(TensorFlowLibName)] public static extern void TFE_OpSetAttrBool(IntPtr op, string attr_name, bool value); @@ -161,7 +161,7 @@ namespace Tensorflow /// /// [DllImport(TensorFlowLibName)] - public static extern void TFE_OpSetDevice(TFE_Op op, string device_name, IntPtr status); + public static extern void TFE_OpSetDevice(TFE_Op op, string device_name, SafeStatusHandle status); /// /// @@ -170,7 +170,7 @@ namespace Tensorflow /// TFE_TensorHandle* /// TF_Status* [DllImport(TensorFlowLibName)] - public static extern void TFE_OpAddInput(IntPtr op, IntPtr h, IntPtr status); + public static extern void TFE_OpAddInput(IntPtr op, IntPtr h, SafeStatusHandle status); /// /// @@ -178,7 +178,7 @@ namespace Tensorflow /// const tensorflow::Tensor& /// TFE_TensorHandle* [DllImport(TensorFlowLibName)] - public static extern TFE_TensorHandle TFE_NewTensorHandle(IntPtr t, IntPtr status); + public static extern TFE_TensorHandle TFE_NewTensorHandle(IntPtr t, SafeStatusHandle status); /// /// Sets the default execution mode (sync/async). Note that this can be @@ -206,7 +206,7 @@ namespace Tensorflow /// TF_Status* /// [DllImport(TensorFlowLibName)] - public static extern TF_Tensor TFE_TensorHandleResolve(IntPtr h, IntPtr status); + public static extern TF_Tensor TFE_TensorHandleResolve(IntPtr h, SafeStatusHandle status); /// /// This function will block till the operation that produces `h` has completed. @@ -215,7 +215,7 @@ namespace Tensorflow /// TF_Status* /// [DllImport(TensorFlowLibName)] - public static extern int TFE_TensorHandleNumDims(IntPtr h, IntPtr status); + public static extern int TFE_TensorHandleNumDims(IntPtr h, SafeStatusHandle status); /// /// Returns the device of the operation that produced `h`. If `h` was produced by @@ -228,7 +228,7 @@ namespace Tensorflow /// TF_Status* /// [DllImport(TensorFlowLibName)] - public static extern IntPtr TFE_TensorHandleDeviceName(IntPtr h, IntPtr status); + public static extern IntPtr TFE_TensorHandleDeviceName(IntPtr h, SafeStatusHandle status); /// /// Returns the name of the device in whose memory `h` resides. @@ -237,7 +237,7 @@ namespace Tensorflow /// TF_Status* /// [DllImport(TensorFlowLibName)] - public static extern IntPtr TFE_TensorHandleBackingDeviceName(IntPtr h, IntPtr status); + public static extern IntPtr TFE_TensorHandleBackingDeviceName(IntPtr h, SafeStatusHandle status); /// /// @@ -246,7 +246,7 @@ namespace Tensorflow /// TF_Status* /// [DllImport(TensorFlowLibName)] - public static extern IntPtr TFE_ContextListDevices(IntPtr ctx, IntPtr status); + public static extern IntPtr TFE_ContextListDevices(IntPtr ctx, SafeStatusHandle status); /// /// @@ -286,7 +286,7 @@ namespace Tensorflow /// TFE_Executor* /// TF_Status* [DllImport(TensorFlowLibName)] - public static extern void TFE_ExecutorWaitForAllPendingNodes(TFE_Executor executor, IntPtr status); + public static extern void TFE_ExecutorWaitForAllPendingNodes(TFE_Executor executor, SafeStatusHandle status); /// /// Sets a custom Executor for current thread. All nodes created by this thread diff --git a/src/TensorFlowNET.Core/Eager/wrap_tfe_src.TFE_Execute.cs b/src/TensorFlowNET.Core/Eager/wrap_tfe_src.TFE_Execute.cs index 591290d9..3e6c2895 100644 --- a/src/TensorFlowNET.Core/Eager/wrap_tfe_src.TFE_Execute.cs +++ b/src/TensorFlowNET.Core/Eager/wrap_tfe_src.TFE_Execute.cs @@ -29,7 +29,7 @@ namespace Tensorflow.Eager { var op = GetOp(ctx, op_name, status); status.Check(true); - c_api.TFE_OpSetDevice(op, device_name, status); + c_api.TFE_OpSetDevice(op, device_name, status.Handle); if(status.ok()) { for (int i = 0; i < inputs.Length; ++i) @@ -41,10 +41,10 @@ namespace Tensorflow.Eager tensor_handle = (TFE_TensorHandle)et; break; default: - tensor_handle = c_api.TFE_NewTensorHandle(inputs[i], status); + tensor_handle = c_api.TFE_NewTensorHandle(inputs[i], status.Handle); break; } - c_api.TFE_OpAddInput(op, tensor_handle, status); + c_api.TFE_OpAddInput(op, tensor_handle, status.Handle); } } if (status.ok()) @@ -53,7 +53,7 @@ namespace Tensorflow.Eager var outputs = new IntPtr[num_outputs]; if (status.ok()) { - c_api.TFE_Execute(op, outputs, ref num_outputs, status); + c_api.TFE_Execute(op, outputs, ref num_outputs, status.Handle); status.Check(true); } return outputs; diff --git a/src/TensorFlowNET.Core/Eager/wrap_tfe_src.TFE_FastPathExecute.cs b/src/TensorFlowNET.Core/Eager/wrap_tfe_src.TFE_FastPathExecute.cs index 01302805..2231f3b2 100644 --- a/src/TensorFlowNET.Core/Eager/wrap_tfe_src.TFE_FastPathExecute.cs +++ b/src/TensorFlowNET.Core/Eager/wrap_tfe_src.TFE_FastPathExecute.cs @@ -44,7 +44,7 @@ namespace Tensorflow.Eager } } - c_api.TFE_OpSetDevice(op, device_name, status); + c_api.TFE_OpSetDevice(op, device_name, status.Handle); status.Check(true); // Add inferred attrs and inputs. @@ -98,7 +98,7 @@ namespace Tensorflow.Eager } var retVals = new IntPtr[num_retvals]; - c_api.TFE_Execute(op, retVals, ref num_retvals, status); + c_api.TFE_Execute(op, retVals, ref num_retvals, status.Handle); status.Check(true); return num_retvals == 0 ? null : new EagerTensor(retVals[0]); @@ -110,11 +110,11 @@ namespace Tensorflow.Eager var maybe_op = ReleaseThreadLocalOp(); if (maybe_op != IntPtr.Zero) { - c_api.TFE_OpReset(ctx, op_or_function_name, status, maybe_op); + c_api.TFE_OpReset(ctx, op_or_function_name, status.Handle, maybe_op); } else { - maybe_op = c_api.TFE_NewOp(ctx, op_or_function_name, status); + maybe_op = c_api.TFE_NewOp(ctx, op_or_function_name, status.Handle); op = maybe_op; } @@ -165,7 +165,7 @@ namespace Tensorflow.Eager c_api.TFE_OpSetAttrType(op, input_arg.TypeAttr, dtype); } - c_api.TFE_OpAddInput(op, input_handle, status); + c_api.TFE_OpAddInput(op, input_handle, status.Handle); status.Check(true); return true; @@ -180,7 +180,7 @@ namespace Tensorflow.Eager var value = attrs[start_index + i + 1]; byte is_list = 0; - var type = c_api.TFE_OpGetAttrType(op, key, ref is_list, out_status); + var type = c_api.TFE_OpGetAttrType(op, key, ref is_list, out_status.Handle); if (!out_status.ok()) return; if (is_list != 0) SetOpAttrList(ctx, op, key, value, type, null, out_status); @@ -209,7 +209,7 @@ namespace Tensorflow.Eager Status status) { byte is_list = 0; - var type = c_api.TFE_OpGetAttrType(op, attr_name, ref is_list, status); + var type = c_api.TFE_OpGetAttrType(op, attr_name, ref is_list, status.Handle); if (status.Code != TF_Code.TF_OK) return; if(attr_value == null) @@ -259,7 +259,7 @@ namespace Tensorflow.Eager break; case TF_AttrType.TF_ATTR_SHAPE: var dims = (value as int[]).Select(x => (long)x).ToArray(); - c_api.TFE_OpSetAttrShape(op, key, dims, dims.Length, status); + c_api.TFE_OpSetAttrShape(op, key, dims, dims.Length, status.Handle); status.Check(true); break; default: diff --git a/src/TensorFlowNET.Core/Framework/Models/ScopedTFStatus.cs b/src/TensorFlowNET.Core/Framework/Models/ScopedTFStatus.cs deleted file mode 100644 index a427c994..00000000 --- a/src/TensorFlowNET.Core/Framework/Models/ScopedTFStatus.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Tensorflow.Framework.Models -{ - public class ScopedTFStatus : Status - { - public ScopedTFStatus() : base() - { - } - } -} diff --git a/src/TensorFlowNET.Core/Framework/importer.cs b/src/TensorFlowNET.Core/Framework/importer.cs index b4bf1c73..ff1ba4f5 100644 --- a/src/TensorFlowNET.Core/Framework/importer.cs +++ b/src/TensorFlowNET.Core/Framework/importer.cs @@ -62,7 +62,7 @@ namespace Tensorflow { _PopulateTFImportGraphDefOptions(scoped_options, prefix, input_map, return_elements); // need to create a class ImportGraphDefWithResults with IDisposal - results = c_api.TF_GraphImportGraphDefWithResults(graph, buffer, scoped_options, status); + results = c_api.TF_GraphImportGraphDefWithResults(graph, buffer, scoped_options, status.Handle); status.Check(true); } diff --git a/src/TensorFlowNET.Core/Functions/c_api.function.cs b/src/TensorFlowNET.Core/Functions/c_api.function.cs index 9fa12efc..11ed7bdd 100644 --- a/src/TensorFlowNET.Core/Functions/c_api.function.cs +++ b/src/TensorFlowNET.Core/Functions/c_api.function.cs @@ -31,7 +31,7 @@ namespace Tensorflow /// /// [DllImport(TensorFlowLibName)] - public static extern void TF_FunctionToFunctionDef(IntPtr func, IntPtr output_func_def, IntPtr status); + public static extern void TF_FunctionToFunctionDef(IntPtr func, IntPtr output_func_def, SafeStatusHandle status); } diff --git a/src/TensorFlowNET.Core/Gradients/c_api.gradient.cs b/src/TensorFlowNET.Core/Gradients/c_api.gradient.cs index c63783e5..70dcfd67 100644 --- a/src/TensorFlowNET.Core/Gradients/c_api.gradient.cs +++ b/src/TensorFlowNET.Core/Gradients/c_api.gradient.cs @@ -38,6 +38,6 @@ namespace Tensorflow /// TF_Output* [DllImport(TensorFlowLibName)] public static extern void TF_AddGradientsWithPrefix(IntPtr g, string prefix, TF_Output[] y, int ny, - TF_Output[] x, int nx, TF_Output[] dx, IntPtr status, IntPtr[] dy); + TF_Output[] x, int nx, TF_Output[] dx, SafeStatusHandle status, IntPtr[] dy); } } diff --git a/src/TensorFlowNET.Core/GraphTransformation/GraphTransformer.cs b/src/TensorFlowNET.Core/GraphTransformation/GraphTransformer.cs index 381ff744..e1225e63 100644 --- a/src/TensorFlowNET.Core/GraphTransformation/GraphTransformer.cs +++ b/src/TensorFlowNET.Core/GraphTransformation/GraphTransformer.cs @@ -35,7 +35,7 @@ namespace Tensorflow outputs_string, transforms_string, buffer, - status); + status.Handle); status.Check(false); var bytes = buffer.ToArray(); diff --git a/src/TensorFlowNET.Core/GraphTransformation/c_api.transform_graph.cs b/src/TensorFlowNET.Core/GraphTransformation/c_api.transform_graph.cs index 8390d74e..a08db0a8 100644 --- a/src/TensorFlowNET.Core/GraphTransformation/c_api.transform_graph.cs +++ b/src/TensorFlowNET.Core/GraphTransformation/c_api.transform_graph.cs @@ -28,6 +28,6 @@ namespace Tensorflow string outputs_string, string transforms_string, IntPtr output_buffer, - IntPtr status); + SafeStatusHandle status); } } diff --git a/src/TensorFlowNET.Core/Graphs/Graph.Export.cs b/src/TensorFlowNET.Core/Graphs/Graph.Export.cs index 2a0d939e..e85cc7be 100644 --- a/src/TensorFlowNET.Core/Graphs/Graph.Export.cs +++ b/src/TensorFlowNET.Core/Graphs/Graph.Export.cs @@ -25,7 +25,7 @@ namespace Tensorflow public Buffer ToGraphDef(Status s) { var buffer = new Buffer(); - c_api.TF_GraphToGraphDef(_handle, buffer, s); + c_api.TF_GraphToGraphDef(_handle, buffer, s.Handle); s.Check(true); return buffer; diff --git a/src/TensorFlowNET.Core/Graphs/Graph.Import.cs b/src/TensorFlowNET.Core/Graphs/Graph.Import.cs index d759e38d..4df5e1eb 100644 --- a/src/TensorFlowNET.Core/Graphs/Graph.Import.cs +++ b/src/TensorFlowNET.Core/Graphs/Graph.Import.cs @@ -29,7 +29,7 @@ namespace Tensorflow int size = Marshal.SizeOf(); var return_output_handle = Marshal.AllocHGlobal(size * num_return_outputs); - c_api.TF_GraphImportGraphDefWithReturnOutputs(_handle, graph_def, opts, return_output_handle, num_return_outputs, s); + c_api.TF_GraphImportGraphDefWithReturnOutputs(_handle, graph_def, opts, return_output_handle, num_return_outputs, s.Handle); var tf_output_ptr = (TF_Output*) return_output_handle; for (int i = 0; i < num_return_outputs; i++) @@ -54,7 +54,7 @@ namespace Tensorflow { as_default(); c_api.TF_ImportGraphDefOptionsSetPrefix(opts, prefix); - c_api.TF_GraphImportGraphDef(_handle, graph_def, opts, status); + c_api.TF_GraphImportGraphDef(_handle, graph_def, opts, status.Handle); status.Check(true); return status.Code == TF_Code.TF_OK; } diff --git a/src/TensorFlowNET.Core/Graphs/Graph.Operation.cs b/src/TensorFlowNET.Core/Graphs/Graph.Operation.cs index cd86a7b3..882f8758 100644 --- a/src/TensorFlowNET.Core/Graphs/Graph.Operation.cs +++ b/src/TensorFlowNET.Core/Graphs/Graph.Operation.cs @@ -30,7 +30,7 @@ namespace Tensorflow using (var buffer = new Buffer()) using (var status = new Status()) { - c_api.TF_GraphGetOpDef(_handle, type, buffer, status); + c_api.TF_GraphGetOpDef(_handle, type, buffer, status.Handle); return OpDef.Parser.ParseFrom(buffer.MemoryBlock.Stream()); } } @@ -41,7 +41,7 @@ namespace Tensorflow using (var buffer = new Buffer()) using (var status = new Status()) { - c_api.TF_GraphGetOpDef(handle, type, buffer, status); + c_api.TF_GraphGetOpDef(handle, type, buffer, status.Handle); return OpDef.Parser.ParseFrom(buffer.MemoryBlock.Stream()); } } diff --git a/src/TensorFlowNET.Core/Graphs/Graph.cs b/src/TensorFlowNET.Core/Graphs/Graph.cs index ff4c84fd..c4ce98ec 100644 --- a/src/TensorFlowNET.Core/Graphs/Graph.cs +++ b/src/TensorFlowNET.Core/Graphs/Graph.cs @@ -513,14 +513,14 @@ namespace Tensorflow public TensorShape GetTensorShape(TF_Output output) { var status = new Status(); - var ndim = c_api.TF_GraphGetTensorNumDims(_handle, output, status); + var ndim = c_api.TF_GraphGetTensorNumDims(_handle, output, status.Handle); status.Check(); if (ndim == -1) return new TensorShape(); var dims = new long[ndim]; - c_api.TF_GraphGetTensorShape(_handle, output, dims, dims.Length, status); + c_api.TF_GraphGetTensorShape(_handle, output, dims, dims.Length, status.Handle); status.Check(); return new TensorShape(dims.Select(x => (int)x).ToArray()); diff --git a/src/TensorFlowNET.Core/Graphs/c_api.graph.cs b/src/TensorFlowNET.Core/Graphs/c_api.graph.cs index 889949ef..888c4fea 100644 --- a/src/TensorFlowNET.Core/Graphs/c_api.graph.cs +++ b/src/TensorFlowNET.Core/Graphs/c_api.graph.cs @@ -47,7 +47,7 @@ namespace Tensorflow public static extern string TF_GraphDebugString(IntPtr graph, out int len); [DllImport(TensorFlowLibName)] - public static extern void TF_GraphGetOpDef(IntPtr graph, string op_name, IntPtr output_op_def, IntPtr status); + public static extern void TF_GraphGetOpDef(IntPtr graph, string op_name, IntPtr output_op_def, SafeStatusHandle status); /// /// Returns the shape of the Tensor referenced by `output` in `graph` @@ -60,7 +60,7 @@ namespace Tensorflow /// /// [DllImport(TensorFlowLibName)] - public static extern void TF_GraphGetTensorShape(IntPtr graph, TF_Output output, long[] dims, int num_dims, IntPtr status); + public static extern void TF_GraphGetTensorShape(IntPtr graph, TF_Output output, long[] dims, int num_dims, SafeStatusHandle status); /// /// Import the graph serialized in `graph_def` into `graph`. @@ -78,7 +78,7 @@ namespace Tensorflow /// int /// TF_Status* [DllImport(TensorFlowLibName)] - public static extern unsafe void TF_GraphImportGraphDefWithReturnOutputs(IntPtr graph, IntPtr graph_def, IntPtr options, IntPtr return_outputs, int num_return_outputs, IntPtr status); + public static extern unsafe void TF_GraphImportGraphDefWithReturnOutputs(IntPtr graph, IntPtr graph_def, IntPtr options, IntPtr return_outputs, int num_return_outputs, SafeStatusHandle status); /// /// Import the graph serialized in `graph_def` into `graph`. Returns nullptr and @@ -92,7 +92,7 @@ namespace Tensorflow /// TF_Status* /// TF_ImportGraphDefResults* [DllImport(TensorFlowLibName)] - public static extern IntPtr TF_GraphImportGraphDefWithResults(IntPtr graph, IntPtr graph_def, IntPtr options, IntPtr status); + public static extern IntPtr TF_GraphImportGraphDefWithResults(IntPtr graph, IntPtr graph_def, IntPtr options, SafeStatusHandle status); /// /// Import the graph serialized in `graph_def` into `graph`. @@ -102,7 +102,7 @@ namespace Tensorflow /// TF_ImportGraphDefOptions* /// TF_Status* [DllImport(TensorFlowLibName)] - public static extern void TF_GraphImportGraphDef(IntPtr graph, IntPtr graph_def, IntPtr options, IntPtr status); + public static extern void TF_GraphImportGraphDef(IntPtr graph, IntPtr graph_def, IntPtr options, SafeStatusHandle status); /// /// Iterate through the operations of a graph. @@ -128,7 +128,7 @@ namespace Tensorflow /// the shape described by `dims` and `num_dims`. /// [DllImport(TensorFlowLibName)] - public static extern void TF_GraphSetTensorShape(IntPtr graph, TF_Output output, long[] dims, int num_dims, IntPtr status); + public static extern void TF_GraphSetTensorShape(IntPtr graph, TF_Output output, long[] dims, int num_dims, SafeStatusHandle status); /// /// Write out a serialized representation of `graph` (as a GraphDef protocol @@ -138,7 +138,7 @@ namespace Tensorflow /// TF_Buffer* /// TF_Status* [DllImport(TensorFlowLibName)] - public static extern void TF_GraphToGraphDef(IntPtr graph, IntPtr output_graph_def, IntPtr status); + public static extern void TF_GraphToGraphDef(IntPtr graph, IntPtr output_graph_def, SafeStatusHandle status); /// /// Returns the number of dimensions of the Tensor referenced by `output` @@ -151,7 +151,7 @@ namespace Tensorflow /// /// [DllImport(TensorFlowLibName)] - public static extern int TF_GraphGetTensorNumDims(IntPtr graph, TF_Output output, IntPtr status); + public static extern int TF_GraphGetTensorNumDims(IntPtr graph, TF_Output output, SafeStatusHandle status); /// /// Cause the imported graph to have a control dependency on `oper`. `oper` @@ -289,7 +289,7 @@ namespace Tensorflow [DllImport(TensorFlowLibName)] public static extern IntPtr TF_LoadSessionFromSavedModel(IntPtr session_options, IntPtr run_options, string export_dir, string[] tags, int tags_len, - IntPtr graph, ref TF_Buffer meta_graph_def, IntPtr status); + IntPtr graph, ref TF_Buffer meta_graph_def, SafeStatusHandle status); [DllImport(TensorFlowLibName)] public static extern IntPtr TF_NewGraph(); @@ -306,6 +306,6 @@ namespace Tensorflow /// TF_Status* [DllImport(TensorFlowLibName)] - public static extern void UpdateEdge(IntPtr graph, TF_Output new_src, TF_Input dst, IntPtr status); + public static extern void UpdateEdge(IntPtr graph, TF_Output new_src, TF_Input dst, SafeStatusHandle status); } } diff --git a/src/TensorFlowNET.Core/Operations/Operation.Input.cs b/src/TensorFlowNET.Core/Operations/Operation.Input.cs index 5c992aff..3326a563 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.Input.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.Input.cs @@ -32,7 +32,7 @@ namespace Tensorflow int num = 0; using(var status = new Status()) { - num = c_api.TF_OperationInputListLength(_handle, name, status); + num = c_api.TF_OperationInputListLength(_handle, name, status.Handle); status.Check(true); } return num; diff --git a/src/TensorFlowNET.Core/Operations/Operation.Output.cs b/src/TensorFlowNET.Core/Operations/Operation.Output.cs index 18393e2f..700fec74 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.Output.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.Output.cs @@ -31,7 +31,7 @@ namespace Tensorflow int num = 0; using (var status = new Status()) { - num = c_api.TF_OperationOutputListLength(_handle, name, status); + num = c_api.TF_OperationOutputListLength(_handle, name, status.Handle); status.Check(true); } diff --git a/src/TensorFlowNET.Core/Operations/Operation.cs b/src/TensorFlowNET.Core/Operations/Operation.cs index 49ddfa6e..7c79effb 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.cs @@ -235,7 +235,7 @@ namespace Tensorflow using (var status = new Status()) using (var buf = new Buffer()) { - c_api.TF_OperationGetAttrValueProto(_handle, name, buf, status); + c_api.TF_OperationGetAttrValueProto(_handle, name, buf, status.Handle); status.Check(true); x = AttrValue.Parser.ParseFrom(buf.MemoryBlock.Stream()); @@ -259,7 +259,7 @@ namespace Tensorflow public TF_AttrMetadata GetAttributeMetadata(string attr_name, Status s) { - return c_api.TF_OperationGetAttrMetadata(_handle, attr_name, s); + return c_api.TF_OperationGetAttrMetadata(_handle, attr_name, s.Handle); } private NodeDef GetNodeDef() @@ -268,7 +268,7 @@ namespace Tensorflow using (var s = new Status()) using (var buffer = new Buffer()) { - c_api.TF_OperationToNodeDef(_handle, buffer, s); + c_api.TF_OperationToNodeDef(_handle, buffer, s.Handle); s.Check(); return NodeDef.Parser.ParseFrom(buffer.MemoryBlock.Stream()); @@ -296,7 +296,7 @@ namespace Tensorflow lock (Locks.ProcessWide) using (var status = new Status()) { - c_api.UpdateEdge(_graph, output, input, status); + c_api.UpdateEdge(_graph, output, input, status.Handle); //var updated_inputs = inputs; status.Check(); } diff --git a/src/TensorFlowNET.Core/Operations/OperationDescription.cs b/src/TensorFlowNET.Core/Operations/OperationDescription.cs index 28df548d..384f5386 100644 --- a/src/TensorFlowNET.Core/Operations/OperationDescription.cs +++ b/src/TensorFlowNET.Core/Operations/OperationDescription.cs @@ -50,7 +50,7 @@ namespace Tensorflow public Operation FinishOperation(Status status) { - return c_api.TF_FinishOperation(_handle, status); + return c_api.TF_FinishOperation(_handle, status.Handle); } public static implicit operator OperationDescription(IntPtr handle) diff --git a/src/TensorFlowNET.Core/Operations/c_api.ops.cs b/src/TensorFlowNET.Core/Operations/c_api.ops.cs index a23cd406..988bb287 100644 --- a/src/TensorFlowNET.Core/Operations/c_api.ops.cs +++ b/src/TensorFlowNET.Core/Operations/c_api.ops.cs @@ -83,7 +83,7 @@ namespace Tensorflow public static extern void TF_AddInputList(IntPtr desc, TF_Output[] inputs, int num_inputs); [DllImport(TensorFlowLibName)] - public static extern IntPtr TF_FinishOperation(IntPtr desc, IntPtr status); + public static extern IntPtr TF_FinishOperation(IntPtr desc, SafeStatusHandle status); /// /// Operation will only be added to *graph when TF_FinishOperation() is @@ -141,7 +141,7 @@ namespace Tensorflow public static extern TF_Output TF_OperationInput(TF_Input oper_in); [DllImport(TensorFlowLibName)] - public static extern int TF_OperationInputListLength(IntPtr oper, string arg_name, IntPtr status); + public static extern int TF_OperationInputListLength(IntPtr oper, string arg_name, SafeStatusHandle status); [DllImport(TensorFlowLibName)] public static extern TF_DataType TF_OperationInputType(TF_Input oper_in); @@ -204,9 +204,9 @@ namespace Tensorflow public static extern TF_DataType TF_OperationOutputType(TF_Output oper_out); [DllImport(TensorFlowLibName)] - public static extern void TF_OperationToNodeDef(IntPtr oper, IntPtr buffer, IntPtr status); + public static extern void TF_OperationToNodeDef(IntPtr oper, IntPtr buffer, SafeStatusHandle status); [DllImport(TensorFlowLibName)] - public static extern int TF_OperationOutputListLength(IntPtr oper, string arg_name, IntPtr status); + public static extern int TF_OperationOutputListLength(IntPtr oper, string arg_name, SafeStatusHandle status); } } diff --git a/src/TensorFlowNET.Core/Sessions/BaseSession.cs b/src/TensorFlowNET.Core/Sessions/BaseSession.cs index b25e77e5..5b698929 100644 --- a/src/TensorFlowNET.Core/Sessions/BaseSession.cs +++ b/src/TensorFlowNET.Core/Sessions/BaseSession.cs @@ -47,7 +47,7 @@ namespace Tensorflow lock (Locks.ProcessWide) { status = status ?? new Status(); - _handle = c_api.TF_NewSession(_graph, opts, status); + _handle = c_api.TF_NewSession(_graph, opts, status.Handle); status.Check(true); } } @@ -250,7 +250,7 @@ namespace Tensorflow target_opers: target_list.Select(f => (IntPtr) f).ToArray(), ntargets: target_list.Count, run_metadata: IntPtr.Zero, - status: status); + status: status.Handle); status.Check(true); @@ -462,7 +462,7 @@ namespace Tensorflow lock (Locks.ProcessWide) using (var status = new Status()) { - c_api.TF_DeleteSession(handle, status); + c_api.TF_DeleteSession(handle, status.Handle); status.Check(true); } } diff --git a/src/TensorFlowNET.Core/Sessions/Session.cs b/src/TensorFlowNET.Core/Sessions/Session.cs index c18df439..276f991d 100644 --- a/src/TensorFlowNET.Core/Sessions/Session.cs +++ b/src/TensorFlowNET.Core/Sessions/Session.cs @@ -62,7 +62,7 @@ namespace Tensorflow tags.Length, graph, ref buffer, - status); + status.Handle); status.Check(true); } catch (TensorflowException ex) when (ex.Message.Contains("Could not find SavedModel")) { @@ -74,7 +74,7 @@ namespace Tensorflow tags.Length, graph, ref buffer, - status); + status.Handle); status.Check(true); } diff --git a/src/TensorFlowNET.Core/Sessions/SessionOptions.cs b/src/TensorFlowNET.Core/Sessions/SessionOptions.cs index 0e64033c..56f13628 100644 --- a/src/TensorFlowNET.Core/Sessions/SessionOptions.cs +++ b/src/TensorFlowNET.Core/Sessions/SessionOptions.cs @@ -46,7 +46,7 @@ namespace Tensorflow using (var status = new Status()) { - c_api.TF_SetConfig(_handle, proto, (ulong)bytes.Length, status); + c_api.TF_SetConfig(_handle, proto, (ulong)bytes.Length, status.Handle); status.Check(false); } diff --git a/src/TensorFlowNET.Core/Sessions/c_api.session.cs b/src/TensorFlowNET.Core/Sessions/c_api.session.cs index 713d0d5f..7082c617 100644 --- a/src/TensorFlowNET.Core/Sessions/c_api.session.cs +++ b/src/TensorFlowNET.Core/Sessions/c_api.session.cs @@ -32,7 +32,7 @@ namespace Tensorflow /// TF_Session* /// TF_Status* [DllImport(TensorFlowLibName)] - public static extern void TF_DeleteSession(IntPtr session, IntPtr status); + public static extern void TF_DeleteSession(IntPtr session, SafeStatusHandle status); /// /// Destroy an options object. @@ -50,7 +50,7 @@ namespace Tensorflow /// TF_Status* /// TF_Session* [DllImport(TensorFlowLibName)] - public static extern IntPtr TF_NewSession(IntPtr graph, IntPtr opts, IntPtr status); + public static extern IntPtr TF_NewSession(IntPtr graph, IntPtr opts, SafeStatusHandle status); /// /// Return a new options object. @@ -103,7 +103,7 @@ namespace Tensorflow TF_Output[] outputs, IntPtr[] output_values, int noutputs, IntPtr[] target_opers, int ntargets, IntPtr run_metadata, - IntPtr status); + SafeStatusHandle status); /// /// Set the config in TF_SessionOptions.options. @@ -116,7 +116,7 @@ namespace Tensorflow /// size_t /// TF_Status* [DllImport(TensorFlowLibName)] - public static extern void TF_SetConfig(IntPtr options, IntPtr proto, ulong proto_len, IntPtr status); + public static extern void TF_SetConfig(IntPtr options, IntPtr proto, ulong proto_len, SafeStatusHandle status); [DllImport(TensorFlowLibName)] public static extern void TF_SetTarget(IntPtr options, string target); diff --git a/src/TensorFlowNET.Core/Status/SafeStatusHandle.cs b/src/TensorFlowNET.Core/Status/SafeStatusHandle.cs new file mode 100644 index 00000000..81fcd4fb --- /dev/null +++ b/src/TensorFlowNET.Core/Status/SafeStatusHandle.cs @@ -0,0 +1,39 @@ +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; +using Tensorflow.Util; + +namespace Tensorflow +{ + public sealed class SafeStatusHandle : SafeTensorflowHandle + { + public SafeStatusHandle() + { + } + + public SafeStatusHandle(IntPtr handle) + : base(handle) + { + } + + protected override bool ReleaseHandle() + { + c_api.TF_DeleteStatus(handle); + return true; + } + } +} diff --git a/src/TensorFlowNET.Core/Status/Status.cs b/src/TensorFlowNET.Core/Status/Status.cs index 90597195..08ccc6e6 100644 --- a/src/TensorFlowNET.Core/Status/Status.cs +++ b/src/TensorFlowNET.Core/Status/Status.cs @@ -17,6 +17,7 @@ using System; using System.Diagnostics; using System.Runtime.CompilerServices; +using Tensorflow.Util; using static Tensorflow.c_api; namespace Tensorflow @@ -25,26 +26,37 @@ namespace Tensorflow /// TF_Status holds error information. It either has an OK code, or /// else an error code with an associated error message. /// - public class Status : DisposableObject + public sealed class Status : IDisposable { /// /// Error message /// - public string Message => c_api.StringPiece(TF_Message(_handle)); + public string Message + { + get + { + using (Handle.Lease()) + { + return StringPiece(TF_Message(Handle)); + } + } + } /// /// Error code /// - public TF_Code Code => TF_GetCode(_handle); + public TF_Code Code => TF_GetCode(Handle); + + public SafeStatusHandle Handle { get; } public Status() { - _handle = TF_NewStatus(); + Handle = TF_NewStatus(); } public void SetStatus(TF_Code code, string msg) { - TF_SetStatus(_handle, code, msg); + TF_SetStatus(Handle, code, msg); } public bool ok() => Code == TF_Code.TF_OK; @@ -60,19 +72,17 @@ namespace Tensorflow { if (Code != TF_Code.TF_OK) { - Console.WriteLine(Message); + var message = Message; + Console.WriteLine(message); if (throwException) - throw new TensorflowException(Message); + throw new TensorflowException(message); } } - public static implicit operator IntPtr(Status status) - => status._handle; - - protected override void DisposeUnmanagedResources(IntPtr handle) - => TF_DeleteStatus(handle); + public void Dispose() + => Handle.Dispose(); public override string ToString() - => $"{Code} 0x{_handle.ToString("x16")}"; + => $"{Code} 0x{Handle.DangerousGetHandle():x16}"; } } \ No newline at end of file diff --git a/src/TensorFlowNET.Core/Status/c_api.status.cs b/src/TensorFlowNET.Core/Status/c_api.status.cs index ee17e447..b5da21bf 100644 --- a/src/TensorFlowNET.Core/Status/c_api.status.cs +++ b/src/TensorFlowNET.Core/Status/c_api.status.cs @@ -34,7 +34,7 @@ namespace Tensorflow /// /// [DllImport(TensorFlowLibName)] - public static extern TF_Code TF_GetCode(IntPtr s); + public static extern TF_Code TF_GetCode(SafeStatusHandle s); /// /// Return a pointer to the (null-terminated) error message in *s. @@ -44,14 +44,14 @@ namespace Tensorflow /// /// [DllImport(TensorFlowLibName)] - public static extern IntPtr TF_Message(IntPtr s); + public static extern IntPtr TF_Message(SafeStatusHandle s); /// /// Return a new status object. /// /// [DllImport(TensorFlowLibName)] - public static extern IntPtr TF_NewStatus(); + public static extern SafeStatusHandle TF_NewStatus(); /// /// Record in *s. Any previous information is lost. @@ -61,6 +61,6 @@ namespace Tensorflow /// /// [DllImport(TensorFlowLibName)] - public static extern void TF_SetStatus(IntPtr s, TF_Code code, string msg); + public static extern void TF_SetStatus(SafeStatusHandle s, TF_Code code, string msg); } } diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.Conversions.cs b/src/TensorFlowNET.Core/Tensors/Tensor.Conversions.cs index eb04814c..ee214d07 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.Conversions.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.Conversions.cs @@ -71,7 +71,7 @@ namespace Tensorflow using (var status = new Status()) { - c_api.TF_StringDecode((byte*) this.buffer + 8, (UIntPtr) (this.bytesize), (byte**) &stringStartAddress, &dstLen, status); + c_api.TF_StringDecode((byte*) this.buffer + 8, (UIntPtr) (this.bytesize), (byte**) &stringStartAddress, &dstLen, status.Handle); status.Check(true); } diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs b/src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs index ce1b0db9..42ab0d7b 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs @@ -451,7 +451,7 @@ namespace Tensorflow IntPtr tensor = c_api.TF_TensorData(handle); Marshal.WriteInt64(tensor, 0); fixed (byte* src = buffer) - c_api.TF_StringEncode(src, (UIntPtr)buffer.Length, (sbyte*)(tensor + sizeof(long)), size, status); + c_api.TF_StringEncode(src, (UIntPtr)buffer.Length, (sbyte*)(tensor + sizeof(long)), size, status.Handle); _handle = handle; status.Check(true); } @@ -475,7 +475,7 @@ namespace Tensorflow Marshal.WriteInt64(tensor, 0); var status = new Status(); - c_api.TF_StringEncode((byte*) nd.Unsafe.Address, bytesLength, (sbyte*) (tensor + sizeof(Int64)), size, status); + c_api.TF_StringEncode((byte*) nd.Unsafe.Address, bytesLength, (sbyte*) (tensor + sizeof(Int64)), size, status.Handle); status.Check(true); _handle = handle; @@ -491,7 +491,7 @@ namespace Tensorflow var status = new Status(); fixed (byte* src = buffer) - c_api.TF_StringEncode(src, (UIntPtr) buffer.Length, (sbyte*) (tensor + sizeof(Int64)), size, status); + c_api.TF_StringEncode(src, (UIntPtr) buffer.Length, (sbyte*) (tensor + sizeof(Int64)), size, status.Handle); status.Check(true); _handle = handle; @@ -552,7 +552,7 @@ namespace Tensorflow { fixed (byte* src = &buffer[i][0]) { - var written = TF_StringEncode(src, (UIntPtr) buffer[i].Length, (sbyte*) dst, (UIntPtr) (dstLimit.ToInt64() - dst.ToInt64()), status); + var written = TF_StringEncode(src, (UIntPtr) buffer[i].Length, (sbyte*) dst, (UIntPtr) (dstLimit.ToInt64() - dst.ToInt64()), status.Handle); status.Check(true); pOffset += 8; dst += (int) written; @@ -600,7 +600,7 @@ namespace Tensorflow var status = new Status(); fixed (byte* src = buffer) - c_api.TF_StringEncode(src, (UIntPtr) buffer.Length, (sbyte*) (tensor + sizeof(Int64)), size, status); + c_api.TF_StringEncode(src, (UIntPtr) buffer.Length, (sbyte*) (tensor + sizeof(Int64)), size, status.Handle); status.Check(true); return handle; diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.Value.cs b/src/TensorFlowNET.Core/Tensors/Tensor.Value.cs index 84ba7c04..49fda084 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.Value.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.Value.cs @@ -228,7 +228,7 @@ namespace Tensorflow { IntPtr dst = IntPtr.Zero; UIntPtr dstLen = UIntPtr.Zero; - var read = c_api.TF_StringDecode((byte*)src, (UIntPtr)(srcLen.ToInt64() - src.ToInt64()), (byte**)&dst, &dstLen, status); + var read = c_api.TF_StringDecode((byte*)src, (UIntPtr)(srcLen.ToInt64() - src.ToInt64()), (byte**)&dst, &dstLen, status.Handle); status.Check(true); buffer[i] = new byte[(int)dstLen]; Marshal.Copy(dst, buffer[i], 0, buffer[i].Length); diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.cs b/src/TensorFlowNET.Core/Tensors/Tensor.cs index 24af4ec9..d9a83ee8 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.cs @@ -109,7 +109,7 @@ namespace Tensorflow { using (var status = new Status()) { - c_api.TF_GraphGetTensorShape(op.graph, _as_tf_output(), dims, rank, status); + c_api.TF_GraphGetTensorShape(op.graph, _as_tf_output(), dims, rank, status.Handle); status.Check(); } } @@ -127,9 +127,9 @@ namespace Tensorflow using (var status = new Status()) { if (value == null) - c_api.TF_GraphSetTensorShape(graph, _as_tf_output(), null, -1, status); + c_api.TF_GraphSetTensorShape(graph, _as_tf_output(), null, -1, status.Handle); else - c_api.TF_GraphSetTensorShape(graph, _as_tf_output(), value.Select(Convert.ToInt64).ToArray(), value.Length, status); + c_api.TF_GraphSetTensorShape(graph, _as_tf_output(), value.Select(Convert.ToInt64).ToArray(), value.Length, status.Handle); status.Check(true); } @@ -179,7 +179,7 @@ namespace Tensorflow using (var status = new Status()) { var output = _as_tf_output(); - int ndim = c_api.TF_GraphGetTensorNumDims(op.graph, output, status); + int ndim = c_api.TF_GraphGetTensorNumDims(op.graph, output, status.Handle); status.Check(); return ndim; } diff --git a/src/TensorFlowNET.Core/Tensors/c_api.tensor.cs b/src/TensorFlowNET.Core/Tensors/c_api.tensor.cs index c56d50ae..3071e780 100644 --- a/src/TensorFlowNET.Core/Tensors/c_api.tensor.cs +++ b/src/TensorFlowNET.Core/Tensors/c_api.tensor.cs @@ -186,10 +186,10 @@ namespace Tensorflow /// TF_Status* /// On success returns the size in bytes of the encoded string. [DllImport(TensorFlowLibName)] - public static extern unsafe ulong TF_StringEncode(byte* src, UIntPtr src_len, sbyte* dst, UIntPtr dst_len, IntPtr status); + public static extern unsafe ulong TF_StringEncode(byte* src, UIntPtr src_len, sbyte* dst, UIntPtr dst_len, SafeStatusHandle status); [DllImport(TensorFlowLibName)] - public static extern unsafe ulong TF_StringEncode(IntPtr src, ulong src_len, IntPtr dst, ulong dst_len, IntPtr status); + public static extern unsafe ulong TF_StringEncode(IntPtr src, ulong src_len, IntPtr dst, ulong dst_len, SafeStatusHandle status); /// /// Decode a string encoded using TF_StringEncode. @@ -201,10 +201,10 @@ namespace Tensorflow /// TF_Status* /// [DllImport(TensorFlowLibName)] - public static extern ulong TF_StringDecode(IntPtr src, ulong src_len, IntPtr dst, ref ulong dst_len, IntPtr status); + public static extern ulong TF_StringDecode(IntPtr src, ulong src_len, IntPtr dst, ref ulong dst_len, SafeStatusHandle status); [DllImport(TensorFlowLibName)] - public static extern unsafe UIntPtr TF_StringDecode(byte* src, UIntPtr src_len, byte** dst, UIntPtr* dst_len, IntPtr status); + public static extern unsafe UIntPtr TF_StringDecode(byte* src, UIntPtr src_len, byte** dst, UIntPtr* dst_len, SafeStatusHandle status); public static c_api.Deallocator EmptyDeallocator = FreeNothingDeallocator; diff --git a/src/TensorFlowNET.Core/ops.cs b/src/TensorFlowNET.Core/ops.cs index f73278c4..31fb30c4 100644 --- a/src/TensorFlowNET.Core/ops.cs +++ b/src/TensorFlowNET.Core/ops.cs @@ -189,12 +189,12 @@ namespace Tensorflow var protoHandle = Marshal.AllocHGlobal(bytes.Length); Marshal.Copy(bytes, 0, protoHandle, bytes.Length); uint len = (uint)bytes.Length; - c_api.TF_SetAttrValueProto(op_desc, attr.Key, protoHandle, proto_len: len, status: status); + c_api.TF_SetAttrValueProto(op_desc, attr.Key, protoHandle, proto_len: len, status: status.Handle); status.Check(true); Marshal.FreeHGlobal(protoHandle); } - var c_op = c_api.TF_FinishOperation(op_desc, status); + var c_op = c_api.TF_FinishOperation(op_desc, status.Handle); status.Check(true); diff --git a/test/TensorFlowNET.UnitTest/CApiAttributesTestcs.cs b/test/TensorFlowNET.UnitTest/CApiAttributesTestcs.cs index 7662785d..9e153405 100644 --- a/test/TensorFlowNET.UnitTest/CApiAttributesTestcs.cs +++ b/test/TensorFlowNET.UnitTest/CApiAttributesTestcs.cs @@ -49,7 +49,7 @@ namespace TensorFlowNET.UnitTest private void EXPECT_TF_META(Operation oper, string attr_name, int expected_list_size, TF_AttrType expected_type, uint expected_total_size) { - var m = c_api.TF_OperationGetAttrMetadata(oper, attr_name, s_); + var m = c_api.TF_OperationGetAttrMetadata(oper, attr_name, s_.Handle); EXPECT_EQ(TF_Code.TF_OK, s_.Code); char e = expected_list_size >= 0 ? (char)1 : (char)0; /*EXPECT_EQ(e, m.is_list); @@ -64,7 +64,7 @@ namespace TensorFlowNET.UnitTest var desc = init("string"); c_api.TF_SetAttrString(desc, "v", "bunny", 5); - var oper = c_api.TF_FinishOperation(desc, s_); + var oper = c_api.TF_FinishOperation(desc, s_.Handle); //ASSERT_EQ(TF_Code.TF_OK, s_.Code); //EXPECT_TF_META(oper, "v", -1, TF_AttrType.TF_ATTR_STRING, 5); //var value = new char[5]; diff --git a/test/TensorFlowNET.UnitTest/CApiColocationTest.cs b/test/TensorFlowNET.UnitTest/CApiColocationTest.cs index 6a5b2c0a..c1e384e6 100644 --- a/test/TensorFlowNET.UnitTest/CApiColocationTest.cs +++ b/test/TensorFlowNET.UnitTest/CApiColocationTest.cs @@ -60,7 +60,7 @@ namespace TensorFlowNET.UnitTest private void VerifyCollocation(Operation op, string[] expected) { - var handle = c_api.TF_OperationGetAttrMetadata(op, "_class", s_); + var handle = c_api.TF_OperationGetAttrMetadata(op, "_class", s_.Handle); TF_AttrMetadata m = new TF_AttrMetadata(); if (expected.Length == 0) { diff --git a/test/TensorFlowNET.UnitTest/CApiGradientsTest.cs b/test/TensorFlowNET.UnitTest/CApiGradientsTest.cs index 007b5624..d12a624f 100644 --- a/test/TensorFlowNET.UnitTest/CApiGradientsTest.cs +++ b/test/TensorFlowNET.UnitTest/CApiGradientsTest.cs @@ -50,7 +50,7 @@ namespace TensorFlowNET.UnitTest { using (var buffer = new Buffer()) { - c_api.TF_GraphToGraphDef(graph, buffer, s); + c_api.TF_GraphToGraphDef(graph, buffer, s.Handle); bool ret = TF_GetCode(s) == TF_OK; EXPECT_EQ(TF_OK, TF_GetCode(s)); if (ret) @@ -113,7 +113,7 @@ namespace TensorFlowNET.UnitTest IntPtr[] handles = new IntPtr[2] { IntPtr.Zero, IntPtr.Zero }; c_api.TF_AddGradientsWithPrefix(graph_, prefix, outputs, noutputs, inputs, - ninputs, grad_inputs, s_, handles); + ninputs, grad_inputs, s_.Handle, handles); var op = new Operation(handles[0]); } diff --git a/test/TensorFlowNET.UnitTest/CApiTest.cs b/test/TensorFlowNET.UnitTest/CApiTest.cs index a8b1caea..5008a2e7 100644 --- a/test/TensorFlowNET.UnitTest/CApiTest.cs +++ b/test/TensorFlowNET.UnitTest/CApiTest.cs @@ -42,10 +42,10 @@ namespace TensorFlowNET.UnitTest => c_api.TF_AddInput(desc, input); protected Operation TF_FinishOperation(OperationDescription desc, Status s) - => c_api.TF_FinishOperation(desc, s); + => c_api.TF_FinishOperation(desc, s.Handle); protected void TF_SetAttrTensor(OperationDescription desc, string attrName, Tensor value, Status s) - => c_api.TF_SetAttrTensor(desc, attrName, value, s); + => c_api.TF_SetAttrTensor(desc, attrName, value, s.Handle); protected void TF_SetAttrType(OperationDescription desc, string attrName, TF_DataType dtype) => c_api.TF_SetAttrType(desc, attrName, dtype); @@ -56,19 +56,19 @@ namespace TensorFlowNET.UnitTest protected TF_DataType TFE_TensorHandleDataType(IntPtr h) => c_api.TFE_TensorHandleDataType(h); - protected int TFE_TensorHandleNumDims(IntPtr h, IntPtr status) + protected int TFE_TensorHandleNumDims(IntPtr h, SafeStatusHandle status) => c_api.TFE_TensorHandleNumDims(h, status); protected TF_Code TF_GetCode(Status s) => s.Code; - protected TF_Code TF_GetCode(IntPtr s) + protected TF_Code TF_GetCode(SafeStatusHandle s) => c_api.TF_GetCode(s); - protected string TF_Message(IntPtr s) + protected string TF_Message(SafeStatusHandle s) => c_api.StringPiece(c_api.TF_Message(s)); - protected IntPtr TF_NewStatus() + protected SafeStatusHandle TF_NewStatus() => c_api.TF_NewStatus(); protected void TF_DeleteStatus(IntPtr s) @@ -83,25 +83,25 @@ namespace TensorFlowNET.UnitTest protected ulong TF_TensorByteSize(IntPtr t) => c_api.TF_TensorByteSize(t); - protected void TFE_OpAddInput(IntPtr op, IntPtr h, IntPtr status) + protected void TFE_OpAddInput(IntPtr op, IntPtr h, SafeStatusHandle status) => c_api.TFE_OpAddInput(op, h, status); protected void TFE_OpSetAttrType(IntPtr op, string attr_name, TF_DataType value) => c_api.TFE_OpSetAttrType(op, attr_name, value); - protected void TFE_OpSetAttrShape(IntPtr op, string attr_name, long[] dims, int num_dims, IntPtr out_status) + protected void TFE_OpSetAttrShape(IntPtr op, string attr_name, long[] dims, int num_dims, SafeStatusHandle out_status) => c_api.TFE_OpSetAttrShape(op, attr_name, dims, num_dims, out_status); protected void TFE_OpSetAttrString(IntPtr op, string attr_name, string value, uint length) => c_api.TFE_OpSetAttrString(op, attr_name, value, length); - protected IntPtr TFE_NewOp(IntPtr ctx, string op_or_function_name, IntPtr status) + protected IntPtr TFE_NewOp(IntPtr ctx, string op_or_function_name, SafeStatusHandle status) => c_api.TFE_NewOp(ctx, op_or_function_name, status); - protected IntPtr TFE_NewTensorHandle(IntPtr t, IntPtr status) + protected IntPtr TFE_NewTensorHandle(IntPtr t, SafeStatusHandle status) => c_api.TFE_NewTensorHandle(t, status); - protected void TFE_Execute(IntPtr op, IntPtr[] retvals, ref int num_retvals, IntPtr status) + protected void TFE_Execute(IntPtr op, IntPtr[] retvals, ref int num_retvals, SafeStatusHandle status) => c_api.TFE_Execute(op, retvals, ref num_retvals, status); protected IntPtr TFE_NewContextOptions() @@ -110,19 +110,19 @@ namespace TensorFlowNET.UnitTest protected void TFE_DeleteContext(IntPtr t) => c_api.TFE_DeleteContext(t); - protected IntPtr TFE_NewContext(IntPtr opts, IntPtr status) + protected IntPtr TFE_NewContext(IntPtr opts, SafeStatusHandle status) => c_api.TFE_NewContext(opts, status); protected void TFE_DeleteContextOptions(IntPtr opts) => c_api.TFE_DeleteContextOptions(opts); - protected int TFE_OpGetInputLength(IntPtr op, string input_name, IntPtr status) + protected int TFE_OpGetInputLength(IntPtr op, string input_name, SafeStatusHandle status) => c_api.TFE_OpGetInputLength(op, input_name, status); - protected int TFE_OpAddInputList(IntPtr op, IntPtr[] inputs, int num_inputs, IntPtr status) + protected int TFE_OpAddInputList(IntPtr op, IntPtr[] inputs, int num_inputs, SafeStatusHandle status) => c_api.TFE_OpAddInputList(op, inputs, num_inputs, status); - protected int TFE_OpGetOutputLength(IntPtr op, string input_name, IntPtr status) + protected int TFE_OpGetOutputLength(IntPtr op, string input_name, SafeStatusHandle status) => c_api.TFE_OpGetOutputLength(op, input_name, status); protected void TFE_DeleteTensorHandle(IntPtr h) @@ -137,37 +137,37 @@ namespace TensorFlowNET.UnitTest protected IntPtr TFE_ContextGetExecutorForThread(IntPtr ctx) => c_api.TFE_ContextGetExecutorForThread(ctx); - protected void TFE_ExecutorWaitForAllPendingNodes(IntPtr executor, IntPtr status) + protected void TFE_ExecutorWaitForAllPendingNodes(IntPtr executor, SafeStatusHandle status) => c_api.TFE_ExecutorWaitForAllPendingNodes(executor, status); - protected IntPtr TFE_TensorHandleResolve(IntPtr h, IntPtr status) + protected IntPtr TFE_TensorHandleResolve(IntPtr h, SafeStatusHandle status) => c_api.TFE_TensorHandleResolve(h, status); - protected string TFE_TensorHandleDeviceName(IntPtr h, IntPtr status) + protected string TFE_TensorHandleDeviceName(IntPtr h, SafeStatusHandle status) => c_api.StringPiece(c_api.TFE_TensorHandleDeviceName(h, status)); - protected string TFE_TensorHandleBackingDeviceName(IntPtr h, IntPtr status) + protected string TFE_TensorHandleBackingDeviceName(IntPtr h, SafeStatusHandle status) => c_api.StringPiece(c_api.TFE_TensorHandleBackingDeviceName(h, status)); - protected IntPtr TFE_ContextListDevices(IntPtr ctx, IntPtr status) + protected IntPtr TFE_ContextListDevices(IntPtr ctx, SafeStatusHandle status) => c_api.TFE_ContextListDevices(ctx, status); protected int TF_DeviceListCount(IntPtr list) => c_api.TF_DeviceListCount(list); - protected string TF_DeviceListType(IntPtr list, int index, IntPtr status) + protected string TF_DeviceListType(IntPtr list, int index, SafeStatusHandle status) => c_api.StringPiece(c_api.TF_DeviceListType(list, index, status)); - protected string TF_DeviceListName(IntPtr list, int index, IntPtr status) + protected string TF_DeviceListName(IntPtr list, int index, SafeStatusHandle status) => c_api.StringPiece(c_api.TF_DeviceListName(list, index, status)); protected void TF_DeleteDeviceList(IntPtr list) => c_api.TF_DeleteDeviceList(list); - protected IntPtr TFE_TensorHandleCopyToDevice(IntPtr h, IntPtr ctx, string device_name, IntPtr status) + protected IntPtr TFE_TensorHandleCopyToDevice(IntPtr h, IntPtr ctx, string device_name, SafeStatusHandle status) => c_api.TFE_TensorHandleCopyToDevice(h, ctx, device_name, status); - protected void TFE_OpSetDevice(IntPtr op, string device_name, IntPtr status) + protected void TFE_OpSetDevice(IntPtr op, string device_name, SafeStatusHandle status) => c_api.TFE_OpSetDevice(op, device_name, status); protected unsafe void memcpy(T* dst, void* src, ulong size) diff --git a/test/TensorFlowNET.UnitTest/CSession.cs b/test/TensorFlowNET.UnitTest/CSession.cs index e9ed7784..1dc79b20 100644 --- a/test/TensorFlowNET.UnitTest/CSession.cs +++ b/test/TensorFlowNET.UnitTest/CSession.cs @@ -75,7 +75,7 @@ namespace TensorFlowNET.UnitTest c_api.TF_SessionRun(session_, null, inputs_ptr, input_values_ptr, inputs_ptr.Length, outputs_ptr, output_values_ptr, outputs_.Count, targets_ptr, targets_.Count, - IntPtr.Zero, s); + IntPtr.Zero, s.Handle); s.Check(); diff --git a/test/TensorFlowNET.UnitTest/ConstantTest.cs b/test/TensorFlowNET.UnitTest/ConstantTest.cs index 7742625a..9fdb5ade 100644 --- a/test/TensorFlowNET.UnitTest/ConstantTest.cs +++ b/test/TensorFlowNET.UnitTest/ConstantTest.cs @@ -165,7 +165,7 @@ namespace TensorFlowNET.UnitTest ulong dst_len = (ulong)c_api.TF_StringEncodedSize((UIntPtr)str.Length); Assert.AreEqual(dst_len, (ulong)23); IntPtr dst = Marshal.AllocHGlobal((int)dst_len); - ulong encoded_len = c_api.TF_StringEncode(handle, (ulong)str.Length, dst, dst_len, status); + ulong encoded_len = c_api.TF_StringEncode(handle, (ulong)str.Length, dst, dst_len, status.Handle); Assert.AreEqual((ulong)23, encoded_len); Assert.AreEqual(status.Code, TF_Code.TF_OK); string encoded_str = Marshal.PtrToStringUTF8(dst + sizeof(byte)); diff --git a/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Context.cs b/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Context.cs index 05d34d20..b5c07096 100644 --- a/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Context.cs +++ b/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Context.cs @@ -13,7 +13,7 @@ namespace TensorFlowNET.UnitTest.Eager [TestMethod] public void Context() { - var status = c_api.TF_NewStatus(); + using var status = c_api.TF_NewStatus(); var opts = c_api.TFE_NewContextOptions(); var ctx = c_api.TFE_NewContext(opts, status); @@ -34,7 +34,6 @@ namespace TensorFlowNET.UnitTest.Eager } c_api.TF_DeleteDeviceList(devices); - c_api.TF_DeleteStatus(status); } } } diff --git a/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Execute_MatMul_CPU.cs b/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Execute_MatMul_CPU.cs index a7274582..5d0e5c55 100644 --- a/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Execute_MatMul_CPU.cs +++ b/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Execute_MatMul_CPU.cs @@ -19,7 +19,7 @@ namespace TensorFlowNET.UnitTest.Eager unsafe void Execute_MatMul_CPU(bool async) { - var status = TF_NewStatus(); + using var status = TF_NewStatus(); var opts = TFE_NewContextOptions(); c_api.TFE_ContextOptionsSetAsync(opts, Convert.ToByte(async)); var ctx = TFE_NewContext(opts, status); @@ -50,7 +50,6 @@ namespace TensorFlowNET.UnitTest.Eager EXPECT_EQ(10f, product[1]); EXPECT_EQ(15f, product[2]); EXPECT_EQ(22f, product[3]); - TF_DeleteStatus(status); } } } diff --git a/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.OpGetInputAndOutputLengths.cs b/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.OpGetInputAndOutputLengths.cs index 789b4135..d565abcb 100644 --- a/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.OpGetInputAndOutputLengths.cs +++ b/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.OpGetInputAndOutputLengths.cs @@ -14,7 +14,7 @@ namespace TensorFlowNET.UnitTest.Eager [TestMethod] public unsafe void OpGetInputAndOutputLengths() { - var status = TF_NewStatus(); + using var status = TF_NewStatus(); var opts = TFE_NewContextOptions(); var ctx = TFE_NewContext(opts, status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); @@ -52,7 +52,6 @@ namespace TensorFlowNET.UnitTest.Eager EXPECT_EQ(2, TFE_OpGetOutputLength(identityOp, "output", status)); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); - TF_DeleteStatus(status); TFE_DeleteOp(identityOp); TFE_DeleteTensorHandle(input1); TFE_DeleteTensorHandle(input2); diff --git a/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.OpInferMixedTypeInputListAttrs.cs b/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.OpInferMixedTypeInputListAttrs.cs index 4ce86574..11c4dd34 100644 --- a/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.OpInferMixedTypeInputListAttrs.cs +++ b/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.OpInferMixedTypeInputListAttrs.cs @@ -16,7 +16,7 @@ namespace TensorFlowNET.UnitTest.Eager [TestMethod] public unsafe void OpInferMixedTypeInputListAttrs() { - var status = TF_NewStatus(); + using var status = TF_NewStatus(); var opts = TFE_NewContextOptions(); var ctx = TFE_NewContext(opts, status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); @@ -45,7 +45,6 @@ namespace TensorFlowNET.UnitTest.Eager TFE_Execute(assertOp, retvals, ref num_retvals, status); EXPECT_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); - TF_DeleteStatus(status); TFE_DeleteOp(assertOp); TFE_DeleteTensorHandle(condition); TFE_DeleteTensorHandle(t1); diff --git a/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Variables.cs b/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Variables.cs index f5300088..869fbd7e 100644 --- a/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Variables.cs +++ b/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Variables.cs @@ -14,7 +14,7 @@ namespace TensorFlowNET.UnitTest.Eager [TestMethod] public unsafe void Variables() { - var status = c_api.TF_NewStatus(); + using var status = c_api.TF_NewStatus(); var opts = TFE_NewContextOptions(); var ctx = TFE_NewContext(opts, status); ASSERT_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); @@ -50,7 +50,6 @@ namespace TensorFlowNET.UnitTest.Eager TFE_DeleteTensorHandle(value_handle[0]); TFE_DeleteContext(ctx); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); - TF_DeleteStatus(status); } } } diff --git a/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.cs b/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.cs index 9363212a..1e3e559e 100644 --- a/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.cs +++ b/test/TensorFlowNET.UnitTest/Eager/CApi.Eager.cs @@ -17,17 +17,16 @@ namespace TensorFlowNET.UnitTest.Eager var t = c_api.TF_AllocateTensor(TF_FLOAT, dims, dims.Length, (ulong)data.Length * sizeof(float)); memcpy(c_api.TF_TensorData(t), data, data.Length * sizeof(float)); - var status = c_api.TF_NewStatus(); + using var status = c_api.TF_NewStatus(); var th = c_api.TFE_NewTensorHandle(t, status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); c_api.TF_DeleteTensor(t); - c_api.TF_DeleteStatus(status); return th; } IntPtr MatMulOp(IntPtr ctx, IntPtr a, IntPtr b) { - var status = TF_NewStatus(); + using var status = TF_NewStatus(); var op = TFE_NewOp(ctx, "MatMul", status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); @@ -35,7 +34,6 @@ namespace TensorFlowNET.UnitTest.Eager CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); TFE_OpAddInput(op, b, status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); - TF_DeleteStatus(status); TFE_OpSetAttrType(op, "T", TFE_TensorHandleDataType(a)); return op; @@ -68,19 +66,18 @@ namespace TensorFlowNET.UnitTest.Eager IntPtr ShapeOp(IntPtr ctx, IntPtr a) { - var status = TF_NewStatus(); + using var status = TF_NewStatus(); var op = TFE_NewOp(ctx, "Shape", status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); TFE_OpAddInput(op, a, status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); - TF_DeleteStatus(status); TFE_OpSetAttrType(op, "T", TFE_TensorHandleDataType(a)); return op; } - unsafe IntPtr CreateVariable(IntPtr ctx, float value, IntPtr status) + unsafe IntPtr CreateVariable(IntPtr ctx, float value, SafeStatusHandle status) { var op = TFE_NewOp(ctx, "VarHandleOp", status); if (TF_GetCode(status) != TF_OK) return IntPtr.Zero; @@ -127,11 +124,10 @@ namespace TensorFlowNET.UnitTest.Eager var data = new int[] { 1 }; var t = c_api.TF_AllocateTensor(TF_DataType.TF_INT32, dims, 1, sizeof(int)); memcpy(TF_TensorData(t), data, TF_TensorByteSize(t)); - var status = TF_NewStatus(); + using var status = TF_NewStatus(); var th = c_api.TFE_NewTensorHandle(t, status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); TF_DeleteTensor(t); - TF_DeleteStatus(status); return th; } @@ -140,11 +136,10 @@ namespace TensorFlowNET.UnitTest.Eager var data = new[] { value }; var t = c_api.TF_AllocateTensor(TF_BOOL, null, 0, sizeof(bool)); memcpy(TF_TensorData(t), data, TF_TensorByteSize(t)); - var status = TF_NewStatus(); + using var status = TF_NewStatus(); var th = TFE_NewTensorHandle(t, status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); TF_DeleteTensor(t); - TF_DeleteStatus(status); return th; } @@ -153,11 +148,10 @@ namespace TensorFlowNET.UnitTest.Eager var data = new [] { value }; var t = c_api.TF_AllocateTensor(TF_FLOAT, null, 0, sizeof(float)); memcpy(TF_TensorData(t), data, TF_TensorByteSize(t)); - var status = TF_NewStatus(); + using var status = TF_NewStatus(); var th = TFE_NewTensorHandle(t, status); CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status)); TF_DeleteTensor(t); - TF_DeleteStatus(status); return th; } } diff --git a/test/TensorFlowNET.UnitTest/GraphTest.cs b/test/TensorFlowNET.UnitTest/GraphTest.cs index 80cf6088..fd5e4232 100644 --- a/test/TensorFlowNET.UnitTest/GraphTest.cs +++ b/test/TensorFlowNET.UnitTest/GraphTest.cs @@ -38,7 +38,7 @@ namespace TensorFlowNET.UnitTest EXPECT_EQ(attr_value.Type, DataType.DtInt32); // Test not found errors in TF_Operation*() query functions. - EXPECT_EQ(-1, c_api.TF_OperationOutputListLength(feed, "bogus", s)); + EXPECT_EQ(-1, c_api.TF_OperationOutputListLength(feed, "bogus", s.Handle)); EXPECT_EQ(TF_Code.TF_INVALID_ARGUMENT, s.Code); Assert.IsFalse(c_test_util.GetAttrValue(feed, "missing", ref attr_value, s)); EXPECT_EQ("Operation 'feed' has no attr named 'missing'.", s.Message); @@ -216,7 +216,7 @@ namespace TensorFlowNET.UnitTest // Export to a GraphDef. var graph_def = new Buffer(); - c_api.TF_GraphToGraphDef(graph, graph_def, s); + c_api.TF_GraphToGraphDef(graph, graph_def, s.Handle); EXPECT_EQ(TF_Code.TF_OK, s.Code); // Import it, with a prefix, in a fresh graph. @@ -224,7 +224,7 @@ namespace TensorFlowNET.UnitTest graph = new Graph().as_default(); var opts = c_api.TF_NewImportGraphDefOptions(); c_api.TF_ImportGraphDefOptionsSetPrefix(opts, "imported"); - c_api.TF_GraphImportGraphDef(graph, graph_def, opts, s); + c_api.TF_GraphImportGraphDef(graph, graph_def, opts, s.Handle); EXPECT_EQ(TF_Code.TF_OK, s.Code); Operation scalar = graph.OperationByName("imported/scalar"); @@ -267,7 +267,7 @@ namespace TensorFlowNET.UnitTest EXPECT_EQ(2, c_api.TF_ImportGraphDefOptionsNumReturnOutputs(opts)); c_api.TF_ImportGraphDefOptionsAddReturnOperation(opts, "scalar"); EXPECT_EQ(1, c_api.TF_ImportGraphDefOptionsNumReturnOperations(opts)); - var results = c_api.TF_GraphImportGraphDefWithResults(graph, graph_def, opts, s); + var results = c_api.TF_GraphImportGraphDefWithResults(graph, graph_def, opts, s.Handle); EXPECT_EQ(TF_Code.TF_OK, s.Code); Operation scalar2 = graph.OperationByName("imported2/scalar"); @@ -299,7 +299,7 @@ namespace TensorFlowNET.UnitTest c_api.TF_ImportGraphDefOptionsSetPrefix(opts, "imported3"); c_api.TF_ImportGraphDefOptionsAddControlDependency(opts, feed); c_api.TF_ImportGraphDefOptionsAddControlDependency(opts, feed2); - c_api.TF_GraphImportGraphDef(graph, graph_def, opts, s); + c_api.TF_GraphImportGraphDef(graph, graph_def, opts, s.Handle); EXPECT_EQ(TF_Code.TF_OK, s.Code); var scalar3 = graph.OperationByName("imported3/scalar"); @@ -323,7 +323,7 @@ namespace TensorFlowNET.UnitTest // Export to a graph def so we can import a graph with control dependencies graph_def = new Buffer(); - c_api.TF_GraphToGraphDef(graph, graph_def, s); + c_api.TF_GraphToGraphDef(graph, graph_def, s.Handle); EXPECT_EQ(TF_Code.TF_OK, s.Code); // Import again, with remapped control dependency, into the same graph @@ -331,7 +331,7 @@ namespace TensorFlowNET.UnitTest opts = c_api.TF_NewImportGraphDefOptions(); c_api.TF_ImportGraphDefOptionsSetPrefix(opts, "imported4"); c_api.TF_ImportGraphDefOptionsRemapControlDependency(opts, "imported/feed", feed); - c_api.TF_GraphImportGraphDef(graph, graph_def, opts, s); + c_api.TF_GraphImportGraphDef(graph, graph_def, opts, s.Handle); ASSERT_EQ(TF_Code.TF_OK, s.Code); var scalar4 = graph.OperationByName("imported4/imported3/scalar"); diff --git a/test/TensorFlowNET.UnitTest/TensorTest.cs b/test/TensorFlowNET.UnitTest/TensorTest.cs index a3a63605..e21a481f 100644 --- a/test/TensorFlowNET.UnitTest/TensorTest.cs +++ b/test/TensorFlowNET.UnitTest/TensorTest.cs @@ -124,45 +124,45 @@ namespace TensorFlowNET.UnitTest var feed_out_0 = new TF_Output(feed, 0); // Fetch the shape, it should be completely unknown. - int num_dims = c_api.TF_GraphGetTensorNumDims(graph, feed_out_0, s); + int num_dims = c_api.TF_GraphGetTensorNumDims(graph, feed_out_0, s.Handle); Assert.IsTrue(s.Code == TF_Code.TF_OK); EXPECT_EQ(-1, num_dims); // Set the shape to be unknown, expect no change. - c_api.TF_GraphSetTensorShape(graph, feed_out_0, null, -1, s); + c_api.TF_GraphSetTensorShape(graph, feed_out_0, null, -1, s.Handle); Assert.IsTrue(s.Code == TF_Code.TF_OK); - num_dims = c_api.TF_GraphGetTensorNumDims(graph, feed_out_0, s); + num_dims = c_api.TF_GraphGetTensorNumDims(graph, feed_out_0, s.Handle); EXPECT_EQ(-1, num_dims); // Set the shape to be 2 x Unknown long[] dims = {2, -1}; - c_api.TF_GraphSetTensorShape(graph, feed_out_0, dims, dims.Length, s); + c_api.TF_GraphSetTensorShape(graph, feed_out_0, dims, dims.Length, s.Handle); Assert.IsTrue(s.Code == TF_Code.TF_OK); - num_dims = c_api.TF_GraphGetTensorNumDims(graph, feed_out_0, s); + num_dims = c_api.TF_GraphGetTensorNumDims(graph, feed_out_0, s.Handle); EXPECT_EQ(2, num_dims); // Get the dimension vector appropriately. var returned_dims = new long[dims.Length]; - c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, num_dims, s); + c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, num_dims, s.Handle); Assert.IsTrue(s.Code == TF_Code.TF_OK); Assert.IsTrue(Enumerable.SequenceEqual(dims, returned_dims)); // Set to a new valid shape: [2, 3] dims[1] = 3; - c_api.TF_GraphSetTensorShape(graph, feed_out_0, dims, dims.Length, s); + c_api.TF_GraphSetTensorShape(graph, feed_out_0, dims, dims.Length, s.Handle); Assert.IsTrue(s.Code == TF_Code.TF_OK); // Fetch and see that the new value is returned. - c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, num_dims, s); + c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, num_dims, s.Handle); Assert.IsTrue(s.Code == TF_Code.TF_OK); Assert.IsTrue(Enumerable.SequenceEqual(dims, returned_dims)); // Try to set 'unknown' with unknown rank on the shape and see that // it doesn't change. - c_api.TF_GraphSetTensorShape(graph, feed_out_0, null, -1, s); + c_api.TF_GraphSetTensorShape(graph, feed_out_0, null, -1, s.Handle); Assert.IsTrue(s.Code == TF_Code.TF_OK); - c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, num_dims, s); + c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, num_dims, s.Handle); Assert.IsTrue(s.Code == TF_Code.TF_OK); EXPECT_EQ(2, num_dims); EXPECT_EQ(2, (int) returned_dims[0]); @@ -172,21 +172,21 @@ namespace TensorFlowNET.UnitTest // it doesn't change. dims[0] = -1; dims[1] = -1; - c_api.TF_GraphSetTensorShape(graph, feed_out_0, dims, 2, s); + c_api.TF_GraphSetTensorShape(graph, feed_out_0, dims, 2, s.Handle); Assert.IsTrue(s.Code == TF_Code.TF_OK); - c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, num_dims, s); + c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, num_dims, s.Handle); Assert.IsTrue(s.Code == TF_Code.TF_OK); EXPECT_EQ(2, num_dims); EXPECT_EQ(2, (int) returned_dims[0]); EXPECT_EQ(3, (int) returned_dims[1]); // Try to fetch a shape with the wrong num_dims - c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, 5, s); + c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, 5, s.Handle); Assert.IsTrue(s.Code == TF_Code.TF_INVALID_ARGUMENT); // Try to set an invalid shape (cannot change 2x3 to a 2x5). dims[1] = 5; - c_api.TF_GraphSetTensorShape(graph, feed_out_0, dims, 2, s); + c_api.TF_GraphSetTensorShape(graph, feed_out_0, dims, 2, s.Handle); Assert.IsTrue(s.Code == TF_Code.TF_INVALID_ARGUMENT); // Test for a scalar. @@ -194,10 +194,10 @@ namespace TensorFlowNET.UnitTest Assert.IsTrue(s.Code == TF_Code.TF_OK); var three_out_0 = new TF_Output(three, 0); - num_dims = c_api.TF_GraphGetTensorNumDims(graph, three_out_0, s); + num_dims = c_api.TF_GraphGetTensorNumDims(graph, three_out_0, s.Handle); Assert.IsTrue(s.Code == TF_Code.TF_OK); EXPECT_EQ(0, num_dims); - c_api.TF_GraphGetTensorShape(graph, feed_out_0, null, num_dims, s); + c_api.TF_GraphGetTensorShape(graph, feed_out_0, null, num_dims, s.Handle); //Assert.IsTrue(s.Code == TF_Code.TF_OK); // graph.Dispose(); diff --git a/test/TensorFlowNET.UnitTest/c_test_util.cs b/test/TensorFlowNET.UnitTest/c_test_util.cs index 988afa17..48c717c8 100644 --- a/test/TensorFlowNET.UnitTest/c_test_util.cs +++ b/test/TensorFlowNET.UnitTest/c_test_util.cs @@ -24,7 +24,7 @@ namespace TensorFlowNET.UnitTest c_api.TF_AddInputList(desc, inputs, inputs.Length); - var op = c_api.TF_FinishOperation(desc, s); + var op = c_api.TF_FinishOperation(desc, s.Handle); s.Check(); return op; @@ -38,7 +38,7 @@ namespace TensorFlowNET.UnitTest { using (var buffer = new Buffer()) { - c_api.TF_OperationGetAttrValueProto(oper, attr_name, buffer, s); + c_api.TF_OperationGetAttrValueProto(oper, attr_name, buffer, s.Handle); attr_value = AttrValue.Parser.ParseFrom(buffer.MemoryBlock.Stream()); } @@ -53,7 +53,7 @@ namespace TensorFlowNET.UnitTest using (var s = new Status()) using (var buffer = new Buffer()) { - c_api.TF_GraphToGraphDef(graph, buffer, s); + c_api.TF_GraphToGraphDef(graph, buffer, s.Handle); s.Check(); return GraphDef.Parser.ParseFrom(buffer.MemoryBlock.Stream()); } @@ -175,7 +175,7 @@ namespace TensorFlowNET.UnitTest OperationDescription desc = c_api.TF_NewOperation(graph, "Neg", name); var neg_input = new TF_Output(n, 0); c_api.TF_AddInput(desc, neg_input); - var op = c_api.TF_FinishOperation(desc, s); + var op = c_api.TF_FinishOperation(desc, s.Handle); s.Check(); return op; @@ -193,7 +193,7 @@ namespace TensorFlowNET.UnitTest c_api.TF_SetAttrShape(desc, "shape", dims, dims.Length); } - var op = c_api.TF_FinishOperation(desc, s); + var op = c_api.TF_FinishOperation(desc, s.Handle); s.Check(); return op; @@ -205,10 +205,10 @@ namespace TensorFlowNET.UnitTest lock (Locks.ProcessWide) { var desc = c_api.TF_NewOperation(graph, "Const", name); - c_api.TF_SetAttrTensor(desc, "value", t, s); + c_api.TF_SetAttrTensor(desc, "value", t, s.Handle); s.Check(); c_api.TF_SetAttrType(desc, "dtype", t.dtype); - var op = c_api.TF_FinishOperation(desc, s); + var op = c_api.TF_FinishOperation(desc, s.Handle); s.Check(); return op;