Browse Source

Implement SafeStatusHandle as a wrapper for TF_Status

tags/v0.20
Sam Harwell 5 years ago
parent
commit
7a30362cdb
51 changed files with 244 additions and 215 deletions
  1. +5
    -5
      src/TensorFlowNET.Core/Attributes/c_api.ops.cs
  2. +3
    -3
      src/TensorFlowNET.Core/Device/c_api.device.cs
  3. +1
    -1
      src/TensorFlowNET.Core/Eager/Context.cs
  4. +6
    -6
      src/TensorFlowNET.Core/Eager/EagerTensor.cs
  5. +18
    -18
      src/TensorFlowNET.Core/Eager/c_api.eager.cs
  6. +4
    -4
      src/TensorFlowNET.Core/Eager/wrap_tfe_src.TFE_Execute.cs
  7. +8
    -8
      src/TensorFlowNET.Core/Eager/wrap_tfe_src.TFE_FastPathExecute.cs
  8. +0
    -9
      src/TensorFlowNET.Core/Framework/Models/ScopedTFStatus.cs
  9. +1
    -1
      src/TensorFlowNET.Core/Framework/importer.cs
  10. +1
    -1
      src/TensorFlowNET.Core/Functions/c_api.function.cs
  11. +1
    -1
      src/TensorFlowNET.Core/Gradients/c_api.gradient.cs
  12. +1
    -1
      src/TensorFlowNET.Core/GraphTransformation/GraphTransformer.cs
  13. +1
    -1
      src/TensorFlowNET.Core/GraphTransformation/c_api.transform_graph.cs
  14. +1
    -1
      src/TensorFlowNET.Core/Graphs/Graph.Export.cs
  15. +2
    -2
      src/TensorFlowNET.Core/Graphs/Graph.Import.cs
  16. +2
    -2
      src/TensorFlowNET.Core/Graphs/Graph.Operation.cs
  17. +2
    -2
      src/TensorFlowNET.Core/Graphs/Graph.cs
  18. +10
    -10
      src/TensorFlowNET.Core/Graphs/c_api.graph.cs
  19. +1
    -1
      src/TensorFlowNET.Core/Operations/Operation.Input.cs
  20. +1
    -1
      src/TensorFlowNET.Core/Operations/Operation.Output.cs
  21. +4
    -4
      src/TensorFlowNET.Core/Operations/Operation.cs
  22. +1
    -1
      src/TensorFlowNET.Core/Operations/OperationDescription.cs
  23. +4
    -4
      src/TensorFlowNET.Core/Operations/c_api.ops.cs
  24. +3
    -3
      src/TensorFlowNET.Core/Sessions/BaseSession.cs
  25. +2
    -2
      src/TensorFlowNET.Core/Sessions/Session.cs
  26. +1
    -1
      src/TensorFlowNET.Core/Sessions/SessionOptions.cs
  27. +4
    -4
      src/TensorFlowNET.Core/Sessions/c_api.session.cs
  28. +39
    -0
      src/TensorFlowNET.Core/Status/SafeStatusHandle.cs
  29. +23
    -13
      src/TensorFlowNET.Core/Status/Status.cs
  30. +4
    -4
      src/TensorFlowNET.Core/Status/c_api.status.cs
  31. +1
    -1
      src/TensorFlowNET.Core/Tensors/Tensor.Conversions.cs
  32. +5
    -5
      src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs
  33. +1
    -1
      src/TensorFlowNET.Core/Tensors/Tensor.Value.cs
  34. +4
    -4
      src/TensorFlowNET.Core/Tensors/Tensor.cs
  35. +4
    -4
      src/TensorFlowNET.Core/Tensors/c_api.tensor.cs
  36. +2
    -2
      src/TensorFlowNET.Core/ops.cs
  37. +2
    -2
      test/TensorFlowNET.UnitTest/CApiAttributesTestcs.cs
  38. +1
    -1
      test/TensorFlowNET.UnitTest/CApiColocationTest.cs
  39. +2
    -2
      test/TensorFlowNET.UnitTest/CApiGradientsTest.cs
  40. +24
    -24
      test/TensorFlowNET.UnitTest/CApiTest.cs
  41. +1
    -1
      test/TensorFlowNET.UnitTest/CSession.cs
  42. +1
    -1
      test/TensorFlowNET.UnitTest/ConstantTest.cs
  43. +1
    -2
      test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Context.cs
  44. +1
    -2
      test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Execute_MatMul_CPU.cs
  45. +1
    -2
      test/TensorFlowNET.UnitTest/Eager/CApi.Eager.OpGetInputAndOutputLengths.cs
  46. +1
    -2
      test/TensorFlowNET.UnitTest/Eager/CApi.Eager.OpInferMixedTypeInputListAttrs.cs
  47. +1
    -2
      test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Variables.cs
  48. +7
    -13
      test/TensorFlowNET.UnitTest/Eager/CApi.Eager.cs
  49. +7
    -7
      test/TensorFlowNET.UnitTest/GraphTest.cs
  50. +16
    -16
      test/TensorFlowNET.UnitTest/TensorTest.cs
  51. +7
    -7
      test/TensorFlowNET.UnitTest/c_test_util.cs

+ 5
- 5
src/TensorFlowNET.Core/Attributes/c_api.ops.cs View File

@@ -32,7 +32,7 @@ namespace Tensorflow
/// <param name="status">TF_Status*</param>
/// <returns></returns>
[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);

/// <summary>
/// Fills in `value` with the value of the attribute `attr_name`. `value` must
@@ -46,7 +46,7 @@ namespace Tensorflow
/// <param name="max_length">size_t</param>
/// <param name="status">TF_Status*</param>
[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);
/// <summary>
/// Sets `output_attr_value` to the binary-serialized AttrValue proto
@@ -55,13 +55,13 @@ namespace Tensorflow
/// <param name="oper"></param>
/// <returns></returns>
[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);

/// <summary>
/// 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);


+ 3
- 3
src/TensorFlowNET.Core/Device/c_api.device.cs View File

@@ -45,7 +45,7 @@ namespace Tensorflow
/// <param name="status">TF_Status*</param>
/// <returns></returns>
[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);

/// <summary>
/// Deallocates the device list.
@@ -64,7 +64,7 @@ namespace Tensorflow
/// <param name="status">TF_Status*</param>
/// <returns>TFE_TensorHandle*</returns>
[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);

/// <summary>
/// Retrieves the full name of the device (e.g. /job:worker/replica:0/...)
@@ -76,6 +76,6 @@ namespace Tensorflow
/// <param name="index"></param>
/// <param name="status">TF_Status*</param>
[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);
}
}

+ 1
- 1
src/TensorFlowNET.Core/Eager/Context.cs View File

@@ -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);
}



+ 6
- 6
src/TensorFlowNET.Core/Eager/EagerTensor.cs View File

@@ -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()


+ 18
- 18
src/TensorFlowNET.Core/Eager/c_api.eager.cs View File

@@ -30,7 +30,7 @@ namespace Tensorflow
/// <param name="status">TF_Status*</param>
/// <returns></returns>
[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);

/// <summary>
/// Returns the length (number of tensors) of the input argument `input_name`
@@ -40,7 +40,7 @@ namespace Tensorflow
/// <param name="input_name">const char*</param>
/// <param name="status">TF_Status*</param>
[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);

/// <summary>
/// Returns the length (number of tensors) of the output argument `output_name`
@@ -51,7 +51,7 @@ namespace Tensorflow
/// <param name="status"></param>
/// <returns></returns>
[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);

/// <summary>
///
@@ -62,7 +62,7 @@ namespace Tensorflow
/// <param name="status">TF_Status*</param>
/// <returns></returns>
[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);

/// <summary>
///
@@ -71,7 +71,7 @@ namespace Tensorflow
/// <param name="status">TF_Status*</param>
/// <returns>TFE_Context*</returns>
[DllImport(TensorFlowLibName)]
public static extern TFE_Context TFE_NewContext(IntPtr opts, IntPtr status);
public static extern TFE_Context TFE_NewContext(IntPtr opts, SafeStatusHandle status);

/// <summary>
///
@@ -89,7 +89,7 @@ namespace Tensorflow
/// <param name="num_retvals">int*</param>
/// <param name="status">TF_Status*</param>
[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);

/// <summary>
///
@@ -99,7 +99,7 @@ namespace Tensorflow
/// <param name="status">TF_Status*</param>
/// <returns></returns>
[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);

/// <summary>
///
@@ -109,7 +109,7 @@ namespace Tensorflow
/// <param name="status">TF_Status*</param>
/// <param name="op_to_reset">TFE_Op*</param>
[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);

/// <summary>
///
@@ -139,7 +139,7 @@ namespace Tensorflow
/// <param name="num_dims">const int</param>
/// <param name="out_status">TF_Status*</param>
[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
/// <param name="device_name"></param>
/// <param name="status"></param>
[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);

/// <summary>
///
@@ -170,7 +170,7 @@ namespace Tensorflow
/// <param name="h">TFE_TensorHandle*</param>
/// <param name="status">TF_Status*</param>
[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);

/// <summary>
///
@@ -178,7 +178,7 @@ namespace Tensorflow
/// <param name="t">const tensorflow::Tensor&</param>
/// <returns>TFE_TensorHandle*</returns>
[DllImport(TensorFlowLibName)]
public static extern TFE_TensorHandle TFE_NewTensorHandle(IntPtr t, IntPtr status);
public static extern TFE_TensorHandle TFE_NewTensorHandle(IntPtr t, SafeStatusHandle status);

/// <summary>
/// Sets the default execution mode (sync/async). Note that this can be
@@ -206,7 +206,7 @@ namespace Tensorflow
/// <param name="status">TF_Status*</param>
/// <returns></returns>
[DllImport(TensorFlowLibName)]
public static extern TF_Tensor TFE_TensorHandleResolve(IntPtr h, IntPtr status);
public static extern TF_Tensor TFE_TensorHandleResolve(IntPtr h, SafeStatusHandle status);

/// <summary>
/// This function will block till the operation that produces `h` has completed.
@@ -215,7 +215,7 @@ namespace Tensorflow
/// <param name="status">TF_Status*</param>
/// <returns></returns>
[DllImport(TensorFlowLibName)]
public static extern int TFE_TensorHandleNumDims(IntPtr h, IntPtr status);
public static extern int TFE_TensorHandleNumDims(IntPtr h, SafeStatusHandle status);

/// <summary>
/// Returns the device of the operation that produced `h`. If `h` was produced by
@@ -228,7 +228,7 @@ namespace Tensorflow
/// <param name="status">TF_Status*</param>
/// <returns></returns>
[DllImport(TensorFlowLibName)]
public static extern IntPtr TFE_TensorHandleDeviceName(IntPtr h, IntPtr status);
public static extern IntPtr TFE_TensorHandleDeviceName(IntPtr h, SafeStatusHandle status);

/// <summary>
/// Returns the name of the device in whose memory `h` resides.
@@ -237,7 +237,7 @@ namespace Tensorflow
/// <param name="status">TF_Status*</param>
/// <returns></returns>
[DllImport(TensorFlowLibName)]
public static extern IntPtr TFE_TensorHandleBackingDeviceName(IntPtr h, IntPtr status);
public static extern IntPtr TFE_TensorHandleBackingDeviceName(IntPtr h, SafeStatusHandle status);

/// <summary>
///
@@ -246,7 +246,7 @@ namespace Tensorflow
/// <param name="status">TF_Status*</param>
/// <returns></returns>
[DllImport(TensorFlowLibName)]
public static extern IntPtr TFE_ContextListDevices(IntPtr ctx, IntPtr status);
public static extern IntPtr TFE_ContextListDevices(IntPtr ctx, SafeStatusHandle status);

/// <summary>
///
@@ -286,7 +286,7 @@ namespace Tensorflow
/// <param name="executor">TFE_Executor*</param>
/// <param name="status">TF_Status*</param>
[DllImport(TensorFlowLibName)]
public static extern void TFE_ExecutorWaitForAllPendingNodes(TFE_Executor executor, IntPtr status);
public static extern void TFE_ExecutorWaitForAllPendingNodes(TFE_Executor executor, SafeStatusHandle status);

/// <summary>
/// Sets a custom Executor for current thread. All nodes created by this thread


+ 4
- 4
src/TensorFlowNET.Core/Eager/wrap_tfe_src.TFE_Execute.cs View File

@@ -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;


+ 8
- 8
src/TensorFlowNET.Core/Eager/wrap_tfe_src.TFE_FastPathExecute.cs View File

@@ -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:


+ 0
- 9
src/TensorFlowNET.Core/Framework/Models/ScopedTFStatus.cs View File

@@ -1,9 +0,0 @@
namespace Tensorflow.Framework.Models
{
public class ScopedTFStatus : Status
{
public ScopedTFStatus() : base()
{
}
}
}

+ 1
- 1
src/TensorFlowNET.Core/Framework/importer.cs View File

@@ -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);
}



+ 1
- 1
src/TensorFlowNET.Core/Functions/c_api.function.cs View File

@@ -31,7 +31,7 @@ namespace Tensorflow
/// <param name="output_func_def"></param>
/// <param name="status"></param>
[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);


}


+ 1
- 1
src/TensorFlowNET.Core/Gradients/c_api.gradient.cs View File

@@ -38,6 +38,6 @@ namespace Tensorflow
/// <param name="dy">TF_Output*</param>
[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);
}
}

+ 1
- 1
src/TensorFlowNET.Core/GraphTransformation/GraphTransformer.cs View File

@@ -35,7 +35,7 @@ namespace Tensorflow
outputs_string,
transforms_string,
buffer,
status);
status.Handle);

status.Check(false);
var bytes = buffer.ToArray();


+ 1
- 1
src/TensorFlowNET.Core/GraphTransformation/c_api.transform_graph.cs View File

@@ -28,6 +28,6 @@ namespace Tensorflow
string outputs_string,
string transforms_string,
IntPtr output_buffer,
IntPtr status);
SafeStatusHandle status);
}
}

+ 1
- 1
src/TensorFlowNET.Core/Graphs/Graph.Export.cs View File

@@ -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;


+ 2
- 2
src/TensorFlowNET.Core/Graphs/Graph.Import.cs View File

@@ -29,7 +29,7 @@ namespace Tensorflow
int size = Marshal.SizeOf<TF_Output>();
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;
}


+ 2
- 2
src/TensorFlowNET.Core/Graphs/Graph.Operation.cs View File

@@ -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());
}
}


+ 2
- 2
src/TensorFlowNET.Core/Graphs/Graph.cs View File

@@ -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());


+ 10
- 10
src/TensorFlowNET.Core/Graphs/c_api.graph.cs View File

@@ -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);

/// <summary>
/// Returns the shape of the Tensor referenced by `output` in `graph`
@@ -60,7 +60,7 @@ namespace Tensorflow
/// <param name="num_dims"></param>
/// <param name="status"></param>
[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);

/// <summary>
/// Import the graph serialized in `graph_def` into `graph`.
@@ -78,7 +78,7 @@ namespace Tensorflow
/// <param name="num_return_outputs">int</param>
/// <param name="status">TF_Status*</param>
[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);

/// <summary>
/// Import the graph serialized in `graph_def` into `graph`. Returns nullptr and
@@ -92,7 +92,7 @@ namespace Tensorflow
/// <param name="status">TF_Status*</param>
/// <returns>TF_ImportGraphDefResults*</returns>
[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);

/// <summary>
/// Import the graph serialized in `graph_def` into `graph`.
@@ -102,7 +102,7 @@ namespace Tensorflow
/// <param name="options">TF_ImportGraphDefOptions*</param>
/// <param name="status">TF_Status*</param>
[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);
/// <summary>
/// Iterate through the operations of a graph.
@@ -128,7 +128,7 @@ namespace Tensorflow
/// the shape described by `dims` and `num_dims`.
/// </summary>
[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);

/// <summary>
/// Write out a serialized representation of `graph` (as a GraphDef protocol
@@ -138,7 +138,7 @@ namespace Tensorflow
/// <param name="output_graph_def">TF_Buffer*</param>
/// <param name="status">TF_Status*</param>
[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);
/// <summary>
/// Returns the number of dimensions of the Tensor referenced by `output`
@@ -151,7 +151,7 @@ namespace Tensorflow
/// <param name="status"></param>
/// <returns></returns>
[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);

/// <summary>
/// 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
/// <param name="status">TF_Status*</param>
[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);
}
}

+ 1
- 1
src/TensorFlowNET.Core/Operations/Operation.Input.cs View File

@@ -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;


+ 1
- 1
src/TensorFlowNET.Core/Operations/Operation.Output.cs View File

@@ -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);
}



+ 4
- 4
src/TensorFlowNET.Core/Operations/Operation.cs View File

@@ -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();
}


+ 1
- 1
src/TensorFlowNET.Core/Operations/OperationDescription.cs View File

@@ -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)


+ 4
- 4
src/TensorFlowNET.Core/Operations/c_api.ops.cs View File

@@ -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);

/// <summary>
/// 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);
}
}

+ 3
- 3
src/TensorFlowNET.Core/Sessions/BaseSession.cs View File

@@ -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);
}
}


+ 2
- 2
src/TensorFlowNET.Core/Sessions/Session.cs View File

@@ -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);
}



+ 1
- 1
src/TensorFlowNET.Core/Sessions/SessionOptions.cs View File

@@ -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);
}



+ 4
- 4
src/TensorFlowNET.Core/Sessions/c_api.session.cs View File

@@ -32,7 +32,7 @@ namespace Tensorflow
/// <param name="session">TF_Session*</param>
/// <param name="status">TF_Status*</param>
[DllImport(TensorFlowLibName)]
public static extern void TF_DeleteSession(IntPtr session, IntPtr status);
public static extern void TF_DeleteSession(IntPtr session, SafeStatusHandle status);

/// <summary>
/// Destroy an options object.
@@ -50,7 +50,7 @@ namespace Tensorflow
/// <param name="status">TF_Status*</param>
/// <returns>TF_Session*</returns>
[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);

/// <summary>
/// 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);

/// <summary>
/// Set the config in TF_SessionOptions.options.
@@ -116,7 +116,7 @@ namespace Tensorflow
/// <param name="proto_len">size_t</param>
/// <param name="status">TF_Status*</param>
[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);


+ 39
- 0
src/TensorFlowNET.Core/Status/SafeStatusHandle.cs View File

@@ -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;
}
}
}

+ 23
- 13
src/TensorFlowNET.Core/Status/Status.cs View File

@@ -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.
/// </summary>
public class Status : DisposableObject
public sealed class Status : IDisposable
{
/// <summary>
/// Error message
/// </summary>
public string Message => c_api.StringPiece(TF_Message(_handle));
public string Message
{
get
{
using (Handle.Lease())
{
return StringPiece(TF_Message(Handle));
}
}
}

/// <summary>
/// Error code
/// </summary>
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}";
}
}

+ 4
- 4
src/TensorFlowNET.Core/Status/c_api.status.cs View File

@@ -34,7 +34,7 @@ namespace Tensorflow
/// <param name="s"></param>
/// <returns></returns>
[DllImport(TensorFlowLibName)]
public static extern TF_Code TF_GetCode(IntPtr s);
public static extern TF_Code TF_GetCode(SafeStatusHandle s);

/// <summary>
/// Return a pointer to the (null-terminated) error message in *s.
@@ -44,14 +44,14 @@ namespace Tensorflow
/// <param name="s"></param>
/// <returns></returns>
[DllImport(TensorFlowLibName)]
public static extern IntPtr TF_Message(IntPtr s);
public static extern IntPtr TF_Message(SafeStatusHandle s);

/// <summary>
/// Return a new status object.
/// </summary>
/// <returns></returns>
[DllImport(TensorFlowLibName)]
public static extern IntPtr TF_NewStatus();
public static extern SafeStatusHandle TF_NewStatus();

/// <summary>
/// Record <code, msg> in *s. Any previous information is lost.
@@ -61,6 +61,6 @@ namespace Tensorflow
/// <param name="code"></param>
/// <param name="msg"></param>
[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);
}
}

+ 1
- 1
src/TensorFlowNET.Core/Tensors/Tensor.Conversions.cs View File

@@ -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);
}



+ 5
- 5
src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs View File

@@ -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;


+ 1
- 1
src/TensorFlowNET.Core/Tensors/Tensor.Value.cs View File

@@ -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);


+ 4
- 4
src/TensorFlowNET.Core/Tensors/Tensor.cs View File

@@ -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;
}


+ 4
- 4
src/TensorFlowNET.Core/Tensors/c_api.tensor.cs View File

@@ -186,10 +186,10 @@ namespace Tensorflow
/// <param name="status">TF_Status*</param>
/// <returns>On success returns the size in bytes of the encoded string.</returns>
[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);

/// <summary>
/// Decode a string encoded using TF_StringEncode.
@@ -201,10 +201,10 @@ namespace Tensorflow
/// <param name="status">TF_Status*</param>
/// <returns></returns>
[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;


+ 2
- 2
src/TensorFlowNET.Core/ops.cs View File

@@ -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);



+ 2
- 2
test/TensorFlowNET.UnitTest/CApiAttributesTestcs.cs View File

@@ -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];


+ 1
- 1
test/TensorFlowNET.UnitTest/CApiColocationTest.cs View File

@@ -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)
{


+ 2
- 2
test/TensorFlowNET.UnitTest/CApiGradientsTest.cs View File

@@ -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]);
}


+ 24
- 24
test/TensorFlowNET.UnitTest/CApiTest.cs View File

@@ -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>(T* dst, void* src, ulong size)


+ 1
- 1
test/TensorFlowNET.UnitTest/CSession.cs View File

@@ -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();



+ 1
- 1
test/TensorFlowNET.UnitTest/ConstantTest.cs View File

@@ -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));


+ 1
- 2
test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Context.cs View File

@@ -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);
}
}
}

+ 1
- 2
test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Execute_MatMul_CPU.cs View File

@@ -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);
}
}
}

+ 1
- 2
test/TensorFlowNET.UnitTest/Eager/CApi.Eager.OpGetInputAndOutputLengths.cs View File

@@ -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);


+ 1
- 2
test/TensorFlowNET.UnitTest/Eager/CApi.Eager.OpInferMixedTypeInputListAttrs.cs View File

@@ -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);


+ 1
- 2
test/TensorFlowNET.UnitTest/Eager/CApi.Eager.Variables.cs View File

@@ -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);
}
}
}

+ 7
- 13
test/TensorFlowNET.UnitTest/Eager/CApi.Eager.cs View File

@@ -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;
}
}


+ 7
- 7
test/TensorFlowNET.UnitTest/GraphTest.cs View File

@@ -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");


+ 16
- 16
test/TensorFlowNET.UnitTest/TensorTest.cs View File

@@ -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();


+ 7
- 7
test/TensorFlowNET.UnitTest/c_test_util.cs View File

@@ -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;


Loading…
Cancel
Save