Skip to content

Commit 7a30362

Browse files
committed
Implement SafeStatusHandle as a wrapper for TF_Status
1 parent 79f3d76 commit 7a30362

51 files changed

Lines changed: 244 additions & 215 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/TensorFlowNET.Core/Attributes/c_api.ops.cs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public partial class c_api
3232
/// <param name="status">TF_Status*</param>
3333
/// <returns></returns>
3434
[DllImport(TensorFlowLibName)]
35-
public static extern TF_AttrMetadata TF_OperationGetAttrMetadata(IntPtr oper, string attr_name, IntPtr status);
35+
public static extern TF_AttrMetadata TF_OperationGetAttrMetadata(IntPtr oper, string attr_name, SafeStatusHandle status);
3636

3737
/// <summary>
3838
/// Fills in `value` with the value of the attribute `attr_name`. `value` must
@@ -46,7 +46,7 @@ public partial class c_api
4646
/// <param name="max_length">size_t</param>
4747
/// <param name="status">TF_Status*</param>
4848
[DllImport(TensorFlowLibName)]
49-
public static extern void TF_OperationGetAttrString(IntPtr oper, string attr_name, IntPtr value, uint max_length, IntPtr status);
49+
public static extern void TF_OperationGetAttrString(IntPtr oper, string attr_name, IntPtr value, uint max_length, SafeStatusHandle status);
5050

5151
/// <summary>
5252
/// Sets `output_attr_value` to the binary-serialized AttrValue proto
@@ -55,13 +55,13 @@ public partial class c_api
5555
/// <param name="oper"></param>
5656
/// <returns></returns>
5757
[DllImport(TensorFlowLibName)]
58-
public static extern int TF_OperationGetAttrValueProto(IntPtr oper, string attr_name, IntPtr output_attr_value, IntPtr status);
58+
public static extern int TF_OperationGetAttrValueProto(IntPtr oper, string attr_name, IntPtr output_attr_value, SafeStatusHandle status);
5959

6060
[DllImport(TensorFlowLibName)]
6161
public static extern void TF_SetAttrBool(IntPtr desc, string attr_name, bool value);
6262

6363
[DllImport(TensorFlowLibName)]
64-
public static extern void TF_SetAttrValueProto(IntPtr desc, string attr_name, IntPtr proto, uint proto_len, IntPtr status);
64+
public static extern void TF_SetAttrValueProto(IntPtr desc, string attr_name, IntPtr proto, uint proto_len, SafeStatusHandle status);
6565

6666
/// <summary>
6767
/// Set `num_dims` to -1 to represent "unknown rank".
@@ -99,7 +99,7 @@ public partial class c_api
9999
public static extern void TF_SetAttrStringList(IntPtr desc, string attr_name, IntPtr[] values, uint[] lengths, int num_values);
100100

101101
[DllImport(TensorFlowLibName)]
102-
public static extern void TF_SetAttrTensor(IntPtr desc, string attr_name, IntPtr value, IntPtr status);
102+
public static extern void TF_SetAttrTensor(IntPtr desc, string attr_name, IntPtr value, SafeStatusHandle status);
103103

104104
[DllImport(TensorFlowLibName)]
105105
public static extern void TF_SetAttrType(IntPtr desc, string attr_name, TF_DataType value);

‎src/TensorFlowNET.Core/Device/c_api.device.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public partial class c_api
4545
/// <param name="status">TF_Status*</param>
4646
/// <returns></returns>
4747
[DllImport(TensorFlowLibName)]
48-
public static extern IntPtr TF_DeviceListType(IntPtr list, int index, IntPtr status);
48+
public static extern IntPtr TF_DeviceListType(IntPtr list, int index, SafeStatusHandle status);
4949

5050
/// <summary>
5151
/// Deallocates the device list.
@@ -64,7 +64,7 @@ public partial class c_api
6464
/// <param name="status">TF_Status*</param>
6565
/// <returns>TFE_TensorHandle*</returns>
6666
[DllImport(TensorFlowLibName)]
67-
public static extern IntPtr TFE_TensorHandleCopyToDevice(IntPtr h, IntPtr ctx, string device_name, IntPtr status);
67+
public static extern IntPtr TFE_TensorHandleCopyToDevice(IntPtr h, IntPtr ctx, string device_name, SafeStatusHandle status);
6868

6969
/// <summary>
7070
/// Retrieves the full name of the device (e.g. /job:worker/replica:0/...)
@@ -76,6 +76,6 @@ public partial class c_api
7676
/// <param name="index"></param>
7777
/// <param name="status">TF_Status*</param>
7878
[DllImport(TensorFlowLibName)]
79-
public static extern IntPtr TF_DeviceListName(IntPtr list, int index, IntPtr status);
79+
public static extern IntPtr TF_DeviceListName(IntPtr list, int index, SafeStatusHandle status);
8080
}
8181
}

‎src/TensorFlowNET.Core/Eager/Context.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Context : DisposableObject
1414

1515
public Context(ContextOptions opts, Status status)
1616
{
17-
_handle = c_api.TFE_NewContext(opts, status);
17+
_handle = c_api.TFE_NewContext(opts, status.Handle);
1818
status.Check(true);
1919
}
2020

‎src/TensorFlowNET.Core/Eager/EagerTensor.cs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,32 @@ public partial class EagerTensor : Tensor
1212
public EagerTensor(IntPtr handle) : base(handle)
1313
{
1414
tfe_tensor_handle = handle;
15-
_handle = c_api.TFE_TensorHandleResolve(handle, status);
15+
_handle = c_api.TFE_TensorHandleResolve(handle, status.Handle);
1616
}
1717

1818
public EagerTensor(string value, string device_name) : base(value)
1919
{
20-
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status);
20+
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status.Handle);
2121
}
2222

2323
public EagerTensor(int value, string device_name) : base(value)
2424
{
25-
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status);
25+
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status.Handle);
2626
}
2727

2828
public EagerTensor(float[] value, string device_name) : base(value)
2929
{
30-
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status);
30+
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status.Handle);
3131
}
3232

3333
public EagerTensor(double[] value, string device_name) : base(value)
3434
{
35-
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status);
35+
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status.Handle);
3636
}
3737

3838
public EagerTensor(NDArray value, string device_name) : base(value)
3939
{
40-
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status);
40+
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status.Handle);
4141
}
4242

4343
public override string ToString()

‎src/TensorFlowNET.Core/Eager/c_api.eager.cs‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public partial class c_api
3030
/// <param name="status">TF_Status*</param>
3131
/// <returns></returns>
3232
[DllImport(TensorFlowLibName)]
33-
public static extern TF_AttrType TFE_OpGetAttrType(IntPtr op, string attr_name, ref byte is_list, IntPtr status);
33+
public static extern TF_AttrType TFE_OpGetAttrType(IntPtr op, string attr_name, ref byte is_list, SafeStatusHandle status);
3434

3535
/// <summary>
3636
/// Returns the length (number of tensors) of the input argument `input_name`
@@ -40,7 +40,7 @@ public partial class c_api
4040
/// <param name="input_name">const char*</param>
4141
/// <param name="status">TF_Status*</param>
4242
[DllImport(TensorFlowLibName)]
43-
public static extern int TFE_OpGetInputLength(IntPtr op, string input_name, IntPtr status);
43+
public static extern int TFE_OpGetInputLength(IntPtr op, string input_name, SafeStatusHandle status);
4444

4545
/// <summary>
4646
/// Returns the length (number of tensors) of the output argument `output_name`
@@ -51,7 +51,7 @@ public partial class c_api
5151
/// <param name="status"></param>
5252
/// <returns></returns>
5353
[DllImport(TensorFlowLibName)]
54-
public static extern int TFE_OpGetOutputLength(IntPtr op, string input_name, IntPtr status);
54+
public static extern int TFE_OpGetOutputLength(IntPtr op, string input_name, SafeStatusHandle status);
5555

5656
/// <summary>
5757
///
@@ -62,7 +62,7 @@ public partial class c_api
6262
/// <param name="status">TF_Status*</param>
6363
/// <returns></returns>
6464
[DllImport(TensorFlowLibName)]
65-
public static extern int TFE_OpAddInputList(IntPtr op, IntPtr[] inputs, int num_inputs, IntPtr status);
65+
public static extern int TFE_OpAddInputList(IntPtr op, IntPtr[] inputs, int num_inputs, SafeStatusHandle status);
6666

6767
/// <summary>
6868
///
@@ -71,7 +71,7 @@ public partial class c_api
7171
/// <param name="status">TF_Status*</param>
7272
/// <returns>TFE_Context*</returns>
7373
[DllImport(TensorFlowLibName)]
74-
public static extern TFE_Context TFE_NewContext(IntPtr opts, IntPtr status);
74+
public static extern TFE_Context TFE_NewContext(IntPtr opts, SafeStatusHandle status);
7575

7676
/// <summary>
7777
///
@@ -89,7 +89,7 @@ public partial class c_api
8989
/// <param name="num_retvals">int*</param>
9090
/// <param name="status">TF_Status*</param>
9191
[DllImport(TensorFlowLibName)]
92-
public static extern void TFE_Execute(IntPtr op, IntPtr[] retvals, ref int num_retvals, IntPtr status);
92+
public static extern void TFE_Execute(IntPtr op, IntPtr[] retvals, ref int num_retvals, SafeStatusHandle status);
9393

9494
/// <summary>
9595
///
@@ -99,7 +99,7 @@ public partial class c_api
9999
/// <param name="status">TF_Status*</param>
100100
/// <returns></returns>
101101
[DllImport(TensorFlowLibName)]
102-
public static extern TFE_Op TFE_NewOp(IntPtr ctx, string op_or_function_name, IntPtr status);
102+
public static extern TFE_Op TFE_NewOp(IntPtr ctx, string op_or_function_name, SafeStatusHandle status);
103103

104104
/// <summary>
105105
///
@@ -109,7 +109,7 @@ public partial class c_api
109109
/// <param name="status">TF_Status*</param>
110110
/// <param name="op_to_reset">TFE_Op*</param>
111111
[DllImport(TensorFlowLibName)]
112-
public static extern void TFE_OpReset(IntPtr ctx, string op_or_function_name, IntPtr status, IntPtr op_to_reset);
112+
public static extern void TFE_OpReset(IntPtr ctx, string op_or_function_name, SafeStatusHandle status, IntPtr op_to_reset);
113113

114114
/// <summary>
115115
///
@@ -139,7 +139,7 @@ public partial class c_api
139139
/// <param name="num_dims">const int</param>
140140
/// <param name="out_status">TF_Status*</param>
141141
[DllImport(TensorFlowLibName)]
142-
public static extern void TFE_OpSetAttrShape(IntPtr op, string attr_name, long[] dims, int num_dims, IntPtr out_status);
142+
public static extern void TFE_OpSetAttrShape(IntPtr op, string attr_name, long[] dims, int num_dims, SafeStatusHandle out_status);
143143

144144
[DllImport(TensorFlowLibName)]
145145
public static extern void TFE_OpSetAttrBool(IntPtr op, string attr_name, bool value);
@@ -161,7 +161,7 @@ public partial class c_api
161161
/// <param name="device_name"></param>
162162
/// <param name="status"></param>
163163
[DllImport(TensorFlowLibName)]
164-
public static extern void TFE_OpSetDevice(TFE_Op op, string device_name, IntPtr status);
164+
public static extern void TFE_OpSetDevice(TFE_Op op, string device_name, SafeStatusHandle status);
165165

166166
/// <summary>
167167
///
@@ -170,15 +170,15 @@ public partial class c_api
170170
/// <param name="h">TFE_TensorHandle*</param>
171171
/// <param name="status">TF_Status*</param>
172172
[DllImport(TensorFlowLibName)]
173-
public static extern void TFE_OpAddInput(IntPtr op, IntPtr h, IntPtr status);
173+
public static extern void TFE_OpAddInput(IntPtr op, IntPtr h, SafeStatusHandle status);
174174

175175
/// <summary>
176176
///
177177
/// </summary>
178178
/// <param name="t">const tensorflow::Tensor&</param>
179179
/// <returns>TFE_TensorHandle*</returns>
180180
[DllImport(TensorFlowLibName)]
181-
public static extern TFE_TensorHandle TFE_NewTensorHandle(IntPtr t, IntPtr status);
181+
public static extern TFE_TensorHandle TFE_NewTensorHandle(IntPtr t, SafeStatusHandle status);
182182

183183
/// <summary>
184184
/// Sets the default execution mode (sync/async). Note that this can be
@@ -206,7 +206,7 @@ public partial class c_api
206206
/// <param name="status">TF_Status*</param>
207207
/// <returns></returns>
208208
[DllImport(TensorFlowLibName)]
209-
public static extern TF_Tensor TFE_TensorHandleResolve(IntPtr h, IntPtr status);
209+
public static extern TF_Tensor TFE_TensorHandleResolve(IntPtr h, SafeStatusHandle status);
210210

211211
/// <summary>
212212
/// This function will block till the operation that produces `h` has completed.
@@ -215,7 +215,7 @@ public partial class c_api
215215
/// <param name="status">TF_Status*</param>
216216
/// <returns></returns>
217217
[DllImport(TensorFlowLibName)]
218-
public static extern int TFE_TensorHandleNumDims(IntPtr h, IntPtr status);
218+
public static extern int TFE_TensorHandleNumDims(IntPtr h, SafeStatusHandle status);
219219

220220
/// <summary>
221221
/// Returns the device of the operation that produced `h`. If `h` was produced by
@@ -228,7 +228,7 @@ public partial class c_api
228228
/// <param name="status">TF_Status*</param>
229229
/// <returns></returns>
230230
[DllImport(TensorFlowLibName)]
231-
public static extern IntPtr TFE_TensorHandleDeviceName(IntPtr h, IntPtr status);
231+
public static extern IntPtr TFE_TensorHandleDeviceName(IntPtr h, SafeStatusHandle status);
232232

233233
/// <summary>
234234
/// Returns the name of the device in whose memory `h` resides.
@@ -237,7 +237,7 @@ public partial class c_api
237237
/// <param name="status">TF_Status*</param>
238238
/// <returns></returns>
239239
[DllImport(TensorFlowLibName)]
240-
public static extern IntPtr TFE_TensorHandleBackingDeviceName(IntPtr h, IntPtr status);
240+
public static extern IntPtr TFE_TensorHandleBackingDeviceName(IntPtr h, SafeStatusHandle status);
241241

242242
/// <summary>
243243
///
@@ -246,7 +246,7 @@ public partial class c_api
246246
/// <param name="status">TF_Status*</param>
247247
/// <returns></returns>
248248
[DllImport(TensorFlowLibName)]
249-
public static extern IntPtr TFE_ContextListDevices(IntPtr ctx, IntPtr status);
249+
public static extern IntPtr TFE_ContextListDevices(IntPtr ctx, SafeStatusHandle status);
250250

251251
/// <summary>
252252
///
@@ -286,7 +286,7 @@ public partial class c_api
286286
/// <param name="executor">TFE_Executor*</param>
287287
/// <param name="status">TF_Status*</param>
288288
[DllImport(TensorFlowLibName)]
289-
public static extern void TFE_ExecutorWaitForAllPendingNodes(TFE_Executor executor, IntPtr status);
289+
public static extern void TFE_ExecutorWaitForAllPendingNodes(TFE_Executor executor, SafeStatusHandle status);
290290

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

‎src/TensorFlowNET.Core/Eager/wrap_tfe_src.TFE_Execute.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static IntPtr[] TFE_ExecuteCancelable(Context ctx,
2929
{
3030
var op = GetOp(ctx, op_name, status);
3131
status.Check(true);
32-
c_api.TFE_OpSetDevice(op, device_name, status);
32+
c_api.TFE_OpSetDevice(op, device_name, status.Handle);
3333
if(status.ok())
3434
{
3535
for (int i = 0; i < inputs.Length; ++i)
@@ -41,10 +41,10 @@ public static IntPtr[] TFE_ExecuteCancelable(Context ctx,
4141
tensor_handle = (TFE_TensorHandle)et;
4242
break;
4343
default:
44-
tensor_handle = c_api.TFE_NewTensorHandle(inputs[i], status);
44+
tensor_handle = c_api.TFE_NewTensorHandle(inputs[i], status.Handle);
4545
break;
4646
}
47-
c_api.TFE_OpAddInput(op, tensor_handle, status);
47+
c_api.TFE_OpAddInput(op, tensor_handle, status.Handle);
4848
}
4949
}
5050
if (status.ok())
@@ -53,7 +53,7 @@ public static IntPtr[] TFE_ExecuteCancelable(Context ctx,
5353
var outputs = new IntPtr[num_outputs];
5454
if (status.ok())
5555
{
56-
c_api.TFE_Execute(op, outputs, ref num_outputs, status);
56+
c_api.TFE_Execute(op, outputs, ref num_outputs, status.Handle);
5757
status.Check(true);
5858
}
5959
return outputs;

‎src/TensorFlowNET.Core/Eager/wrap_tfe_src.TFE_FastPathExecute.cs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static EagerTensor TFE_FastPathExecute(Context ctx,
4444
}
4545
}
4646

47-
c_api.TFE_OpSetDevice(op, device_name, status);
47+
c_api.TFE_OpSetDevice(op, device_name, status.Handle);
4848
status.Check(true);
4949

5050
// Add inferred attrs and inputs.
@@ -98,7 +98,7 @@ public static EagerTensor TFE_FastPathExecute(Context ctx,
9898
}
9999

100100
var retVals = new IntPtr[num_retvals];
101-
c_api.TFE_Execute(op, retVals, ref num_retvals, status);
101+
c_api.TFE_Execute(op, retVals, ref num_retvals, status.Handle);
102102
status.Check(true);
103103

104104
return num_retvals == 0 ? null : new EagerTensor(retVals[0]);
@@ -110,11 +110,11 @@ private static TFE_Op GetOp(Context ctx, string op_or_function_name, Status stat
110110
var maybe_op = ReleaseThreadLocalOp();
111111
if (maybe_op != IntPtr.Zero)
112112
{
113-
c_api.TFE_OpReset(ctx, op_or_function_name, status, maybe_op);
113+
c_api.TFE_OpReset(ctx, op_or_function_name, status.Handle, maybe_op);
114114
}
115115
else
116116
{
117-
maybe_op = c_api.TFE_NewOp(ctx, op_or_function_name, status);
117+
maybe_op = c_api.TFE_NewOp(ctx, op_or_function_name, status.Handle);
118118
op = maybe_op;
119119
}
120120

@@ -165,7 +165,7 @@ private static bool AddInputToOp(object inputs,
165165
c_api.TFE_OpSetAttrType(op, input_arg.TypeAttr, dtype);
166166
}
167167

168-
c_api.TFE_OpAddInput(op, input_handle, status);
168+
c_api.TFE_OpAddInput(op, input_handle, status.Handle);
169169
status.Check(true);
170170

171171
return true;
@@ -180,7 +180,7 @@ private static void SetOpAttrs(Context ctx, TFE_Op op, object[] attrs, int start
180180
var value = attrs[start_index + i + 1];
181181

182182
byte is_list = 0;
183-
var type = c_api.TFE_OpGetAttrType(op, key, ref is_list, out_status);
183+
var type = c_api.TFE_OpGetAttrType(op, key, ref is_list, out_status.Handle);
184184
if (!out_status.ok()) return;
185185
if (is_list != 0)
186186
SetOpAttrList(ctx, op, key, value, type, null, out_status);
@@ -209,7 +209,7 @@ private static void SetOpAttrWithDefaults(Context ctx, IntPtr op, AttrDef attr,
209209
Status status)
210210
{
211211
byte is_list = 0;
212-
var type = c_api.TFE_OpGetAttrType(op, attr_name, ref is_list, status);
212+
var type = c_api.TFE_OpGetAttrType(op, attr_name, ref is_list, status.Handle);
213213
if (status.Code != TF_Code.TF_OK) return;
214214

215215
if(attr_value == null)
@@ -259,7 +259,7 @@ private static bool SetOpAttrScalar(Context ctx, IntPtr op,
259259
break;
260260
case TF_AttrType.TF_ATTR_SHAPE:
261261
var dims = (value as int[]).Select(x => (long)x).ToArray();
262-
c_api.TFE_OpSetAttrShape(op, key, dims, dims.Length, status);
262+
c_api.TFE_OpSetAttrShape(op, key, dims, dims.Length, status.Handle);
263263
status.Check(true);
264264
break;
265265
default:

‎src/TensorFlowNET.Core/Framework/Models/ScopedTFStatus.cs‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎src/TensorFlowNET.Core/Framework/importer.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static ITensorOrOperation[] import_graph_def(GraphDef graph_def,
6262
{
6363
_PopulateTFImportGraphDefOptions(scoped_options, prefix, input_map, return_elements);
6464
// need to create a class ImportGraphDefWithResults with IDisposal
65-
results = c_api.TF_GraphImportGraphDefWithResults(graph, buffer, scoped_options, status);
65+
results = c_api.TF_GraphImportGraphDefWithResults(graph, buffer, scoped_options, status.Handle);
6666
status.Check(true);
6767
}
6868

0 commit comments

Comments
 (0)