Skip to content

Commit a0cc473

Browse files
committed
rename SafeOpHandle to SafeEagerOpHandle
1 parent aee6cd6 commit a0cc473

16 files changed

Lines changed: 181 additions & 225 deletions

File tree

‎src/TensorFlowNET.Core/APIs/tf.array.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ public Tensor expand_dims(Tensor input, int axis = -1, string name = null)
116116
public Tensor fill<T>(Tensor dims, T value, string name = null)
117117
=> gen_array_ops.fill(dims, value, name: name);
118118

119+
public Tensor fill<T>(Shape dims, T value, string name = null)
120+
=> array_ops.fill(dims, value, name: name);
121+
119122
/// <summary>
120123
/// Return a tensor with the same shape and contents as input.
121124
/// </summary>

‎src/TensorFlowNET.Core/Eager/EagerRunner.RecordGradient.cs‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ BackwardFunction GetGradientFunction(string op_name,
6868
Tensor[] op_inputs,
6969
object[] attrs,
7070
Tensor[] op_outputs)
71-
=> (output_grads, unneeded_gradients) =>
71+
=> (out_grads, unneeded_gradients) =>
7272
{
7373
if (ops.gradientFunctions[op_name] == null)
7474
return new Tensor[op_inputs.Length];
7575

76-
var op = new EagerOperation
76+
var oper = new EagerOperation
7777
{
7878
Name = op_name,
7979
NumInputs = op_inputs.Length,
@@ -84,7 +84,40 @@ BackwardFunction GetGradientFunction(string op_name,
8484
Attrs = attrs
8585
};
8686

87-
return ops.gradientFunctions[op_name](op, output_grads);
87+
/*return op_name switch
88+
{
89+
"Add" => math_grad._AddGrad(oper, out_grads),
90+
"AddV2" => math_grad._AddV2Grad(oper, out_grads),
91+
"BiasAdd" => nn_grad._BiasAddGrad(oper, out_grads),
92+
"Cast" => math_grad._CastGrad(oper, out_grads),
93+
"ConcatV2" => array_grad._ConcatV2Grad(oper, out_grads),
94+
"Conv2D" => nn_grad._Conv2DGrad(oper, out_grads),
95+
"ExpandDims" => array_grad._ExpandDimsGrad(oper, out_grads),
96+
"Exp" => math_grad._ExpGrad(oper, out_grads),
97+
"FusedBatchNormV3" => nn_grad._FusedBatchNormV3Grad(oper, out_grads),
98+
"Id" => math_grad._IdGrad(oper, out_grads),
99+
"LeakyRelu" => nn_grad._LeakyReluGrad(oper, out_grads),
100+
"Log1p" => math_grad._Log1pGrad(oper, out_grads),
101+
"Maximum" => math_grad._MaximumGrad(oper, out_grads),
102+
"Mean" => math_grad._MeanGrad(oper, out_grads),
103+
"Minimum" => math_grad._MinimumGrad(oper, out_grads),
104+
"Mul" => math_grad._MulGrad(oper, out_grads),
105+
"Neg" => math_grad._NegGrad(oper, out_grads),
106+
"Pad" => array_grad._PadGrad(oper, out_grads),
107+
"Pow" => math_grad._PowGrad(oper, out_grads),
108+
"RealDiv" => math_grad._RealDivGrad(oper, out_grads),
109+
"Read" => resource_variable_grad._ReadGrad(oper, out_grads),
110+
"Reshape" => array_grad._ReshapeGrad(oper, out_grads),
111+
"ResizeNearestNeighbor" => image_grad._ResizeNearestNeighborGrad(oper, out_grads),
112+
"Select" => math_grad._SelectGrad(oper, out_grads),
113+
"Sigmoid" => math_grad._SigmoidGrad(oper, out_grads),
114+
"Sum" => math_grad._SumGrad(oper, out_grads),
115+
"Sub" => math_grad._SubGrad(oper, out_grads),
116+
"StridedSlice" => array_grad._StridedSliceGrad(oper, out_grads),
117+
_ => ops.gradientFunctions[op_name](oper, out_grads)
118+
};*/
119+
120+
return ops.gradientFunctions[op_name](oper, out_grads);
88121
};
89122

90123
bool CouldForwardprop()

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Tensorflow.Eager
1515
/// </summary>
1616
public partial class EagerRunner
1717
{
18-
UnorderedMap<string, SafeOpHandle> thread_local_eager_operation_map = new UnorderedMap<string, SafeOpHandle>();
18+
UnorderedMap<string, SafeEagerOpHandle> thread_local_eager_operation_map = new UnorderedMap<string, SafeEagerOpHandle>();
1919
public void ClearEagerOperationMap()
2020
=> thread_local_eager_operation_map.Clear();
2121

@@ -157,7 +157,7 @@ public Tensor[] TFE_FastPathExecute(FastPathOpExecInfo op_exec_info)
157157
return flat_result;
158158
}
159159

160-
SafeOpHandle GetOp(Context ctx, string op_or_function_name, Status status)
160+
SafeEagerOpHandle GetOp(Context ctx, string op_or_function_name, Status status)
161161
{
162162
if (thread_local_eager_operation_map.find(op_or_function_name, out var op))
163163
c_api.TFE_OpReset(op, op_or_function_name, ctx.DeviceName, status.Handle);
@@ -205,7 +205,7 @@ bool AddInputToOp(object inputs,
205205
ArgDef input_arg,
206206
List<object> flattened_attrs,
207207
List<Tensor> flattened_inputs,
208-
SafeOpHandle op,
208+
SafeEagerOpHandle op,
209209
Status status)
210210
{
211211
var tensor = tf.convert_to_tensor(inputs);
@@ -225,7 +225,7 @@ bool AddInputToOp(object inputs,
225225
return true;
226226
}
227227

228-
public void SetOpAttrs(SafeOpHandle op, params object[] attrs)
228+
public void SetOpAttrs(SafeEagerOpHandle op, params object[] attrs)
229229
{
230230
var status = tf.Status;
231231
var len = attrs.Length;
@@ -258,7 +258,7 @@ public void SetOpAttrs(SafeOpHandle op, params object[] attrs)
258258
/// <param name="attr_value"></param>
259259
/// <param name="attr_list_sizes"></param>
260260
/// <param name="status"></param>
261-
void SetOpAttrWithDefaults(Context ctx, SafeOpHandle op, AttrDef attr,
261+
void SetOpAttrWithDefaults(Context ctx, SafeEagerOpHandle op, AttrDef attr,
262262
string attr_name, object attr_value,
263263
Dictionary<string, long> attr_list_sizes,
264264
Status status)
@@ -280,7 +280,7 @@ void SetOpAttrWithDefaults(Context ctx, SafeOpHandle op, AttrDef attr,
280280
}
281281
}
282282

283-
bool SetOpAttrList(Context ctx, SafeOpHandle op,
283+
bool SetOpAttrList(Context ctx, SafeEagerOpHandle op,
284284
string key, object values, TF_AttrType type,
285285
Dictionary<string, long> attr_list_sizes,
286286
Status status)
@@ -326,7 +326,7 @@ bool SetOpAttrList(Context ctx, SafeOpHandle op,
326326
return true;
327327
}
328328

329-
bool SetOpAttrScalar(Context ctx, SafeOpHandle op,
329+
bool SetOpAttrScalar(Context ctx, SafeEagerOpHandle op,
330330
string key, object value, TF_AttrType type,
331331
Dictionary<string, long> attr_list_sizes,
332332
Status status)

src/TensorFlowNET.Core/Eager/SafeOpHandle.cs renamed to src/TensorFlowNET.Core/Eager/SafeEagerOpHandle.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@ limitations under the License.
1616

1717
using System;
1818
using Tensorflow.Util;
19-
using static Tensorflow.Binding;
2019

2120
namespace Tensorflow.Eager
2221
{
23-
public sealed class SafeOpHandle : SafeTensorflowHandle
22+
public sealed class SafeEagerOpHandle : SafeTensorflowHandle
2423
{
25-
private SafeOpHandle()
24+
private SafeEagerOpHandle()
2625
{
2726

2827
}
2928

30-
public SafeOpHandle(IntPtr handle)
29+
public SafeEagerOpHandle(IntPtr handle)
3130
: base(handle)
3231
{
3332

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public partial class c_api
5959
/// <param name="status">TF_Status*</param>
6060
/// <returns></returns>
6161
[DllImport(TensorFlowLibName)]
62-
public static extern TF_AttrType TFE_OpGetAttrType(SafeOpHandle op, string attr_name, ref byte is_list, SafeStatusHandle status);
62+
public static extern TF_AttrType TFE_OpGetAttrType(SafeEagerOpHandle op, string attr_name, ref byte is_list, SafeStatusHandle status);
6363

6464
[DllImport(TensorFlowLibName)]
6565
public static extern TF_AttrType TFE_OpNameGetAttrType(SafeContextHandle ctx, string op_or_function_name, string attr_name, ref byte is_list, SafeStatusHandle status);
@@ -72,7 +72,7 @@ public partial class c_api
7272
/// <param name="input_name">const char*</param>
7373
/// <param name="status">TF_Status*</param>
7474
[DllImport(TensorFlowLibName)]
75-
public static extern int TFE_OpGetInputLength(SafeOpHandle op, string input_name, SafeStatusHandle status);
75+
public static extern int TFE_OpGetInputLength(SafeEagerOpHandle op, string input_name, SafeStatusHandle status);
7676

7777
/// <summary>
7878
/// Returns the length (number of tensors) of the output argument `output_name`
@@ -83,7 +83,7 @@ public partial class c_api
8383
/// <param name="status"></param>
8484
/// <returns></returns>
8585
[DllImport(TensorFlowLibName)]
86-
public static extern int TFE_OpGetOutputLength(SafeOpHandle op, string input_name, SafeStatusHandle status);
86+
public static extern int TFE_OpGetOutputLength(SafeEagerOpHandle op, string input_name, SafeStatusHandle status);
8787

8888
/// <summary>
8989
///
@@ -94,7 +94,7 @@ public partial class c_api
9494
/// <param name="status">TF_Status*</param>
9595
/// <returns></returns>
9696
[DllImport(TensorFlowLibName)]
97-
public static extern int TFE_OpAddInputList(SafeOpHandle op, [In, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(SafeHandleArrayMarshaler))] SafeTensorHandleHandle[] inputs, int num_inputs, SafeStatusHandle status);
97+
public static extern int TFE_OpAddInputList(SafeEagerOpHandle op, [In, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(SafeHandleArrayMarshaler))] SafeTensorHandleHandle[] inputs, int num_inputs, SafeStatusHandle status);
9898

9999
/// <summary>
100100
///
@@ -161,7 +161,7 @@ public partial class c_api
161161
/// <param name="retvals"></param>
162162
/// <param name="num_retvals"></param>
163163
/// <param name="status"></param>
164-
public static void TFE_Execute(SafeOpHandle op, SafeTensorHandleHandle[] retvals, out int num_retvals, SafeStatusHandle status)
164+
public static void TFE_Execute(SafeEagerOpHandle op, SafeTensorHandleHandle[] retvals, out int num_retvals, SafeStatusHandle status)
165165
{
166166
unsafe
167167
{
@@ -187,7 +187,7 @@ public static void TFE_Execute(SafeOpHandle op, SafeTensorHandleHandle[] retvals
187187
/// <param name="num_retvals">int*</param>
188188
/// <param name="status">TF_Status*</param>
189189
[DllImport(TensorFlowLibName)]
190-
private static unsafe extern void TFE_Execute(SafeOpHandle op, IntPtr* retvals, ref int num_retvals, SafeStatusHandle status);
190+
private static unsafe extern void TFE_Execute(SafeEagerOpHandle op, IntPtr* retvals, ref int num_retvals, SafeStatusHandle status);
191191

192192
/// <summary>
193193
///
@@ -197,7 +197,7 @@ public static void TFE_Execute(SafeOpHandle op, SafeTensorHandleHandle[] retvals
197197
/// <param name="status">TF_Status*</param>
198198
/// <returns></returns>
199199
[DllImport(TensorFlowLibName)]
200-
public static extern SafeOpHandle TFE_NewOp(SafeContextHandle ctx, string op_or_function_name, SafeStatusHandle status);
200+
public static extern SafeEagerOpHandle TFE_NewOp(SafeContextHandle ctx, string op_or_function_name, SafeStatusHandle status);
201201

202202
/// <summary>
203203
/// Resets `op_to_reset` with `op_or_function_name` and `raw_device_name`. This
@@ -213,7 +213,7 @@ public static void TFE_Execute(SafeOpHandle op, SafeTensorHandleHandle[] retvals
213213
/// <param name="raw_device_name">const char*</param>
214214
/// <param name="status">TF_Status*</param>
215215
[DllImport(TensorFlowLibName)]
216-
public static extern void TFE_OpReset(SafeOpHandle op_to_reset, string op_or_function_name, string raw_device_name, SafeStatusHandle status);
216+
public static extern void TFE_OpReset(SafeEagerOpHandle op_to_reset, string op_or_function_name, string raw_device_name, SafeStatusHandle status);
217217

218218
/// <summary>
219219
///
@@ -229,13 +229,13 @@ public static void TFE_Execute(SafeOpHandle op, SafeTensorHandleHandle[] retvals
229229
/// <param name="attr_name">const char*</param>
230230
/// <param name="value">TF_DataType</param>
231231
[DllImport(TensorFlowLibName)]
232-
public static extern void TFE_OpSetAttrType(SafeOpHandle op, string attr_name, TF_DataType value);
232+
public static extern void TFE_OpSetAttrType(SafeEagerOpHandle op, string attr_name, TF_DataType value);
233233

234234
[DllImport(TensorFlowLibName)]
235-
public static extern void TFE_OpSetAttrInt(SafeOpHandle op, string attr_name, long value);
235+
public static extern void TFE_OpSetAttrInt(SafeEagerOpHandle op, string attr_name, long value);
236236

237237
[DllImport(TensorFlowLibName)]
238-
public static extern void TFE_OpSetAttrFloat(SafeOpHandle op, string attr_name, float value);
238+
public static extern void TFE_OpSetAttrFloat(SafeEagerOpHandle op, string attr_name, float value);
239239

240240
/// <summary>
241241
///
@@ -246,19 +246,19 @@ public static void TFE_Execute(SafeOpHandle op, SafeTensorHandleHandle[] retvals
246246
/// <param name="num_dims">const int</param>
247247
/// <param name="out_status">TF_Status*</param>
248248
[DllImport(TensorFlowLibName)]
249-
public static extern void TFE_OpSetAttrShape(SafeOpHandle op, string attr_name, long[] dims, int num_dims, SafeStatusHandle out_status);
249+
public static extern void TFE_OpSetAttrShape(SafeEagerOpHandle op, string attr_name, long[] dims, int num_dims, SafeStatusHandle out_status);
250250

251251
[DllImport(TensorFlowLibName)]
252-
public static extern void TFE_OpSetAttrShapeList(SafeOpHandle op, string attr_name, IntPtr[] dims, int[] num_dims, int num_values, SafeStatusHandle out_status);
252+
public static extern void TFE_OpSetAttrShapeList(SafeEagerOpHandle op, string attr_name, IntPtr[] dims, int[] num_dims, int num_values, SafeStatusHandle out_status);
253253

254254
[DllImport(TensorFlowLibName)]
255-
public static extern void TFE_OpSetAttrStringList(SafeOpHandle op, string attr_name, string[] values, ulong[] lengths, int num_values);
255+
public static extern void TFE_OpSetAttrStringList(SafeEagerOpHandle op, string attr_name, string[] values, ulong[] lengths, int num_values);
256256

257257
[DllImport(TensorFlowLibName)]
258-
public static extern void TFE_OpSetAttrBool(SafeOpHandle op, string attr_name, bool value);
258+
public static extern void TFE_OpSetAttrBool(SafeEagerOpHandle op, string attr_name, bool value);
259259

260260
[DllImport(TensorFlowLibName)]
261-
public static extern void TFE_OpSetAttrFunctionName(SafeOpHandle op, string attr_name, string data, int length);
261+
public static extern void TFE_OpSetAttrFunctionName(SafeEagerOpHandle op, string attr_name, string data, int length);
262262

263263
/// <summary>
264264
///
@@ -268,16 +268,16 @@ public static void TFE_Execute(SafeOpHandle op, SafeTensorHandleHandle[] retvals
268268
/// <param name="value">const void*</param>
269269
/// <param name="length">size_t</param>
270270
[DllImport(TensorFlowLibName)]
271-
public static extern void TFE_OpSetAttrString(SafeOpHandle op, string attr_name, string value, ulong length);
271+
public static extern void TFE_OpSetAttrString(SafeEagerOpHandle op, string attr_name, string value, ulong length);
272272

273273
[DllImport(TensorFlowLibName)]
274-
public static extern void TFE_OpSetAttrTypeList(SafeOpHandle op, string attr_name, TF_DataType[] values, int num_values);
274+
public static extern void TFE_OpSetAttrTypeList(SafeEagerOpHandle op, string attr_name, TF_DataType[] values, int num_values);
275275

276276
[DllImport(TensorFlowLibName)]
277-
public static extern void TFE_OpSetAttrIntList(SafeOpHandle op, string attr_name, long[] values, int num_values);
277+
public static extern void TFE_OpSetAttrIntList(SafeEagerOpHandle op, string attr_name, long[] values, int num_values);
278278

279279
[DllImport(TensorFlowLibName)]
280-
public static extern void TFE_OpSetAttrValueProto(SafeOpHandle op, string attr_name, IMessage[] proto, int proto_len, SafeStatusHandle status);
280+
public static extern void TFE_OpSetAttrValueProto(SafeEagerOpHandle op, string attr_name, IMessage[] proto, int proto_len, SafeStatusHandle status);
281281

282282
/// <summary>
283283
///
@@ -286,7 +286,7 @@ public static void TFE_Execute(SafeOpHandle op, SafeTensorHandleHandle[] retvals
286286
/// <param name="device_name"></param>
287287
/// <param name="status"></param>
288288
[DllImport(TensorFlowLibName)]
289-
public static extern void TFE_OpSetDevice(SafeOpHandle op, string device_name, SafeStatusHandle status);
289+
public static extern void TFE_OpSetDevice(SafeEagerOpHandle op, string device_name, SafeStatusHandle status);
290290

291291
/// <summary>
292292
///
@@ -295,7 +295,7 @@ public static void TFE_Execute(SafeOpHandle op, SafeTensorHandleHandle[] retvals
295295
/// <param name="h">TFE_TensorHandle*</param>
296296
/// <param name="status">TF_Status*</param>
297297
[DllImport(TensorFlowLibName)]
298-
public static extern void TFE_OpAddInput(SafeOpHandle op, SafeTensorHandleHandle h, SafeStatusHandle status);
298+
public static extern void TFE_OpAddInput(SafeEagerOpHandle op, SafeTensorHandleHandle h, SafeStatusHandle status);
299299

300300
/// <summary>
301301
///

‎src/TensorFlowNET.Core/Gradients/array_grad.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static Tensor[] _BroadcastToGrad(Operation op, Tensor[] grads)
5151
}
5252

5353
[RegisterGradient("ConcatV2")]
54-
public static Tensor[] _ConcatGradV2(Operation op, Tensor[] grads)
54+
public static Tensor[] _ConcatV2Grad(Operation op, Tensor[] grads)
5555
{
5656
var grad = grads[0];
5757
return _ConcatGradHelper(op, grad, start_value_index: 0, end_value_index: -1, dim_index: -1);

0 commit comments

Comments
 (0)