Skip to content

Commit 10ebec4

Browse files
committed
use SetOpAttrs instead of SetOpAttrWithDefaults
1 parent 15bcc72 commit 10ebec4

5 files changed

Lines changed: 51 additions & 37 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,18 @@ public partial class c_api
333333
[DllImport(TensorFlowLibName)]
334334
public static extern TFE_Executor TFE_ContextGetExecutorForThread(IntPtr ctx);
335335

336+
/// <summary>
337+
///
338+
/// </summary>
339+
/// <param name="ctx"></param>
340+
/// <param name="device_name"></param>
341+
/// <param name="op_name"></param>
342+
/// <param name="name"></param>
343+
/// <param name="args"></param>
344+
/// <param name="input_size"></param>
345+
/// <param name="set_op_attrs"></param>
346+
/// <param name="status"></param>
347+
/// <returns>EagerTensorHandle</returns>
336348
[DllImport(TensorFlowLibName)]
337349
public static extern IntPtr TFE_FastPathExecute(IntPtr ctx,
338350
string device_name,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,15 @@ private static bool AddInputToOp(object inputs,
173173
return true;
174174
}
175175

176-
private static void SetOpAttrs(Context ctx, TFE_Op op, object[] attrs, int start_index, Status out_status)
176+
public static void SetOpAttrs(Context ctx, TFE_Op op, object[] attrs, int start_index, Status out_status)
177177
{
178178
var len = attrs.Length;
179179
for (int i = 0; i < len; i += 2)
180180
{
181181
var key = attrs[start_index + i].ToString();
182182
var value = attrs[start_index + i + 1];
183183

184-
byte is_list = 0;
184+
byte is_list = 0;
185185
var type = c_api.TFE_OpGetAttrType(op, key, ref is_list, out_status);
186186
if (!out_status.ok()) return;
187187
if (is_list != 0)
@@ -205,7 +205,7 @@ private static void SetOpAttrs(Context ctx, TFE_Op op, object[] attrs, int start
205205
/// <param name="attr_value"></param>
206206
/// <param name="attr_list_sizes"></param>
207207
/// <param name="status"></param>
208-
public static void SetOpAttrWithDefaults(Context ctx, IntPtr op, AttrDef attr,
208+
private static void SetOpAttrWithDefaults(Context ctx, IntPtr op, AttrDef attr,
209209
string attr_name, object attr_value,
210210
Dictionary<string, long> attr_list_sizes,
211211
Status status)

‎src/TensorFlowNET.Core/Operations/gen_array_ops.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public static Tensor pack(Tensor[] values, int axis = 0, string name = null)
156156
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
157157
"Pack", name,
158158
values.Select(x => (x as EagerTensor).EagerTensorHandle).ToArray(), 1,
159-
(op) => wrap_tfe_src.SetOpAttrWithDefaults(tf.context, op, null, "axis", axis, null, status),
159+
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "axis", axis }, 0 , status),
160160
status);
161161
status.Check(true);
162162
return new EagerTensor(tensor);

‎src/TensorFlowNET.Core/Operations/gen_math_ops.cs‎

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,19 @@ public static Tensor mean<T1, T2>(T1 input, T2 axis, bool keep_dims= false, stri
138138
"Mean", name,
139139
new IntPtr[]
140140
{
141-
(input as EagerTensor).EagerTensorHandle,
142-
(axis as EagerTensor).EagerTensorHandle
141+
input as EagerTensor,
142+
axis as EagerTensor
143143
}, 2,
144-
(op) => wrap_tfe_src.SetOpAttrWithDefaults(tf.context, op, null, "keep_dims", keep_dims, null, status),
144+
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "keep_dims", keep_dims }, 0, status),
145145
status);
146146
status.Check(true);
147147
return new EagerTensor(tensor);
148148
}
149149
catch (Exception)
150150
{
151+
/*tensors = c_api.TFE_Execute(tf.context, tf.context.device_name, op_name,
152+
inputs, attrs, num_outputs);*/
153+
151154
return mean_eager_fallback(input as Tensor[], axis as Tensor, keep_dims: keep_dims, name: name, ctx: tf.context);
152155
}
153156
}
@@ -220,8 +223,8 @@ public static Tensor add(Tensor x, Tensor y, string name = null)
220223
var _result = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
221224
"Add", name, new IntPtr[]
222225
{
223-
(x as EagerTensor).EagerTensorHandle,
224-
(y as EagerTensor).EagerTensorHandle
226+
x as EagerTensor,
227+
y as EagerTensor
225228
}, 2, null, status);
226229
status.Check(true);
227230
return new EagerTensor(_result);
@@ -595,12 +598,8 @@ public static Tensor cast(Tensor x, TF_DataType DstT, bool Truncate= false, stri
595598
using var status = new Status();
596599
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
597600
"Cast", name,
598-
new IntPtr[] { (x as EagerTensor).EagerTensorHandle }, 1,
599-
(op) =>
600-
{
601-
wrap_tfe_src.SetOpAttrWithDefaults(tf.context, op, null, "DstT", DstT, null, status);
602-
wrap_tfe_src.SetOpAttrWithDefaults(tf.context, op, null, "Truncate", Truncate, null, status);
603-
},
601+
new IntPtr[] { x as EagerTensor }, 1,
602+
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "DstT", DstT, "Truncate", Truncate }, 0, status),
604603
status);
605604
status.Check(true);
606605
return new EagerTensor(tensor);
@@ -649,8 +648,8 @@ public static Tensor sub(Tensor x, Tensor y, string name = null)
649648
var _result = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
650649
"Sub", name, new IntPtr[]
651650
{
652-
(x as EagerTensor).EagerTensorHandle,
653-
(y as EagerTensor).EagerTensorHandle
651+
x as EagerTensor,
652+
y as EagerTensor
654653
}, 2, null, status);
655654
status.Check(true);
656655
return new EagerTensor(_result);
@@ -743,8 +742,8 @@ public static Tensor mul(Tensor x, Tensor y, string name = null)
743742
var _result = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
744743
"Mul", name, new IntPtr[]
745744
{
746-
(x as EagerTensor).EagerTensorHandle,
747-
(y as EagerTensor).EagerTensorHandle
745+
x as EagerTensor,
746+
y as EagerTensor
748747
}, 2, null, status);
749748
status.Check(true);
750749
return new EagerTensor(_result);
@@ -785,8 +784,8 @@ public static Tensor real_div(Tensor x, Tensor y, string name = null)
785784
var _result = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
786785
"RealDiv", name, new IntPtr[]
787786
{
788-
(x as EagerTensor).EagerTensorHandle,
789-
(y as EagerTensor).EagerTensorHandle
787+
x as EagerTensor,
788+
y as EagerTensor
790789
}, 2, null, status);
791790
status.Check(true);
792791
return new EagerTensor(_result);
@@ -988,9 +987,9 @@ public static Tensor range(Tensor start, Tensor limit, Tensor delta, string name
988987
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
989988
"Range", name, new IntPtr[]
990989
{
991-
(start as EagerTensor).EagerTensorHandle,
992-
(limit as EagerTensor).EagerTensorHandle,
993-
(delta as EagerTensor).EagerTensorHandle
990+
start as EagerTensor,
991+
limit as EagerTensor,
992+
delta as EagerTensor
994993
}, 3, null, status);
995994
status.Check(true);
996995
return new EagerTensor(tensor);

‎src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ public static Operation assign_variable_op(Tensor resource, Tensor value, string
3232
using var status = new Status();
3333
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
3434
"AssignVariableOp", name,
35-
new[]
35+
new IntPtr[]
3636
{
37-
(resource as EagerTensor).EagerTensorHandle,
38-
(value as EagerTensor).EagerTensorHandle
37+
resource as EagerTensor,
38+
value as EagerTensor
3939
}, 2, null, status);
4040
status.Check(true);
4141
return tensor;
@@ -53,7 +53,7 @@ public static Tensor var_is_initialized_op(Tensor resource, string name = null)
5353
using var status = new Status();
5454
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
5555
"VarIsInitializedOp", name,
56-
new[] { (resource as EagerTensor).EagerTensorHandle },
56+
new IntPtr[] { resource as EagerTensor },
5757
1, null, status);
5858
status.Check(true);
5959
return new EagerTensor(tensor);
@@ -80,13 +80,15 @@ public static Tensor var_handle_op(TF_DataType dtype, TensorShape shape,
8080
{
8181
using var status = new Status();
8282
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
83-
"VarHandleOp", name, null, 0, op =>
84-
{
85-
wrap_tfe_src.SetOpAttrWithDefaults(tf.context, op, null, "container", container, null, status);
86-
wrap_tfe_src.SetOpAttrWithDefaults(tf.context, op, null, "shared_name", shared_name, null, status);
87-
wrap_tfe_src.SetOpAttrWithDefaults(tf.context, op, null, "dtype", dtype, null, status);
88-
wrap_tfe_src.SetOpAttrWithDefaults(tf.context, op, null, "shape", shape.dims, null, status);
89-
}, status);
83+
"VarHandleOp", name, null, 0,
84+
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[]
85+
{
86+
"container", container,
87+
"shared_name", shared_name,
88+
"dtype", dtype,
89+
"shape", shape.dims
90+
}, 0, status),
91+
status);
9092
status.Check(true);
9193
return new EagerTensor(tensor);
9294
}
@@ -114,8 +116,9 @@ public static Tensor read_variable_op(Tensor resource, TF_DataType dtype, string
114116
{
115117
using var status = new Status();
116118
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
117-
"ReadVariableOp", name, new IntPtr[] { (resource as EagerTensor).EagerTensorHandle }, 1,
118-
(op) => wrap_tfe_src.SetOpAttrWithDefaults(tf.context, op, null, "dtype", dtype, null, status),
119+
"ReadVariableOp", name,
120+
new IntPtr[] { resource as EagerTensor }, 1,
121+
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "dtype", dtype }, 0, status),
119122
status);
120123
status.Check(true);
121124
return new EagerTensor(tensor);

0 commit comments

Comments
 (0)