Skip to content

Commit 1035b94

Browse files
committed
TFE_QuickExecute
1 parent 10ebec4 commit 1035b94

10 files changed

Lines changed: 83 additions & 33 deletions

File tree

src/TensorFlowNET.Core/APIs/c_api.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace Tensorflow
4343
/// </summary>
4444
public partial class c_api
4545
{
46-
public const string TensorFlowLibName = @"D:\SciSharp\tensorflow-google\bazel-bin\tensorflow\tensorflow.dll";
46+
public const string TensorFlowLibName = "tensorflow";
4747

4848
public static string StringPiece(IntPtr handle)
4949
{

src/TensorFlowNET.Core/Eager/EagerTensor.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ public EagerTensor(IntPtr handle) : base(handle)
1919
_handle = c_api.TFE_TensorHandleResolve(tfe_tensor_handle, status);
2020
}
2121

22+
public EagerTensor(TFE_TensorHandle handle) : base(handle)
23+
{
24+
tfe_tensor_handle = handle;
25+
_handle = c_api.TFE_TensorHandleResolve(tfe_tensor_handle, status);
26+
EagerTensorHandle = c_api.TFE_EagerTensorFromHandle(tf.context, tfe_tensor_handle);
27+
}
28+
2229
public EagerTensor(string value, string device_name) : base(value)
2330
{
2431
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status);

src/TensorFlowNET.Core/Eager/Execute.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,30 @@ public class Execute
2727
/// <param name="ctx">The value of context.context().</param>
2828
/// <param name="name">Customized name for the operation.</param>
2929
/// <returns>List of output Tensor objects. The list is empty if there are no outputs</returns>
30-
public Tensor execute(Context ctx, string op_name, Tensor[] inputs, object[] attrs, string name = null)
30+
public Tensor execute(Context ctx, string op_name, int num_outputs,
31+
Tensor[] inputs, object[] attrs,
32+
string name = null)
3133
{
3234
ctx.ensure_initialized();
33-
using (var status = new Status())
34-
{
35-
var retVals = wrap_tfe_src.TFE_Execute(ctx, ctx.device_name, op_name, inputs, attrs, 1, status);
3635

37-
return new EagerTensor(retVals[0]);
38-
}
36+
// TFE_TensorHandle
37+
using var status = new Status();
38+
var retVals = wrap_tfe_src.TFE_Execute(ctx, ctx.device_name, op_name, inputs, attrs, num_outputs, status);
39+
40+
return new EagerTensor((TFE_TensorHandle)retVals[0]);
41+
42+
/*IntPtr[] outputs = new IntPtr[num_outputs];
43+
c_api.TFE_QuickExecute(ctx, ctx.device_name,
44+
"Sum",
45+
inputs.Select(x => (IntPtr)(TFE_TensorHandle)(x as EagerTensor)).ToArray(), inputs.Length,
46+
op => wrap_tfe_src.SetOpAttrs(ctx, op, attrs, 0, status),
47+
outputs, num_outputs,
48+
status);
49+
status.Check(true);
50+
51+
var tfe_tensor_handle = outputs[0];
52+
var eager_tensor_handle = c_api.TFE_EagerTensorFromHandle(ctx, tfe_tensor_handle);
53+
return new EagerTensor(eager_tensor_handle);*/
3954
}
4055

4156
public (TF_DataType, Tensor[]) args_to_matching_eager(Context ctx, TF_DataType default_dtype = TF_DataType.DtInvalid, object[] args = null)

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,15 @@ public static extern IntPtr TFE_FastPathExecute(IntPtr ctx,
357357
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
358358
public delegate void TFE_FastPathExecute_SetOpAttrs(IntPtr op);
359359

360+
[DllImport(TensorFlowLibName)]
361+
public static extern IntPtr TFE_QuickExecute(IntPtr ctx,
362+
string device_name,
363+
string op_name,
364+
IntPtr[] inputs, int input_size,
365+
TFE_FastPathExecute_SetOpAttrs set_op_attrs,
366+
IntPtr[] outputs, int output_size,
367+
IntPtr status);
368+
360369
[DllImport(TensorFlowLibName)]
361370
public static extern IntPtr TFE_TapeSetNew(bool persistent, bool watch_accessed_variables);
362371

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static IntPtr[] TFE_ExecuteCancelable(Context ctx,
4848
}
4949
}
5050
if (status.ok())
51-
SetOpAttrs(ctx, op, attrs, 0, status);
51+
SetOpAttrs(ctx, op, attrs, status);
5252

5353
var outputs = new IntPtr[num_outputs];
5454
if (status.ok())

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,13 +173,13 @@ private static bool AddInputToOp(object inputs,
173173
return true;
174174
}
175175

176-
public 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, Status out_status)
177177
{
178178
var len = attrs.Length;
179179
for (int i = 0; i < len; i += 2)
180180
{
181-
var key = attrs[start_index + i].ToString();
182-
var value = attrs[start_index + i + 1];
181+
var key = attrs[i].ToString();
182+
var value = attrs[i + 1];
183183

184184
byte is_list = 0;
185185
var type = c_api.TFE_OpGetAttrType(op, key, ref is_list, out_status);

src/TensorFlowNET.Core/Operations/gen_array_ops.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private static Tensor concat_v2_eager_fallback<T1, T2>(T1[] values, T2 axis, str
7979
var _inputs_flat = input.concat(axis1);
8080
var _attrs = new object[] { "N", _attr_N, "T", _attr_T, "Tidx", _attr_Tidx };
8181

82-
return _execute.execute(ctx, "ConcatV2", _inputs_flat, _attrs, name: name);
82+
return _execute.execute(ctx, "ConcatV2", 1, _inputs_flat, _attrs, name: name);
8383
}
8484

8585
public static Tensor[] concat_offset(Tensor concat_dim, Tensor[] shape, string name = null)
@@ -155,8 +155,8 @@ public static Tensor pack(Tensor[] values, int axis = 0, string name = null)
155155
using var status = new Status();
156156
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
157157
"Pack", name,
158-
values.Select(x => (x as EagerTensor).EagerTensorHandle).ToArray(), 1,
159-
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "axis", axis }, 0 , status),
158+
values.Select(x => (x as EagerTensor).EagerTensorHandle).ToArray(), values.Length,
159+
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "axis", axis } , status),
160160
status);
161161
status.Check(true);
162162
return new EagerTensor(tensor);

src/TensorFlowNET.Core/Operations/gen_math_ops.cs

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,13 @@ public static Tensor mean<T1, T2>(T1 input, T2 axis, bool keep_dims= false, stri
141141
input as EagerTensor,
142142
axis as EagerTensor
143143
}, 2,
144-
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "keep_dims", keep_dims }, 0, status),
144+
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "keep_dims", keep_dims }, 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-
154151
return mean_eager_fallback(input as Tensor[], axis as Tensor, keep_dims: keep_dims, name: name, ctx: tf.context);
155152
}
156153
}
@@ -167,7 +164,7 @@ private static Tensor mean_eager_fallback(Tensor[] inputs, Tensor axis, bool kee
167164
var _inputs_flat = input.concat(axis1);
168165
var _attrs = new object[] { "keep_dims", keep_dims, "T", _attr_T, "Tidx", _attr_Tidx };
169166

170-
return _execute.execute(ctx, "Mean", _inputs_flat, _attrs, name: name);
167+
return _execute.execute(ctx, "Mean", 1, _inputs_flat, _attrs, name: name);
171168
}
172169

173170
public static Tensor prod<T1, T2>(T1 input, T2 axis, bool keep_dims = false, string name = null)
@@ -198,7 +195,7 @@ private static Tensor prod_eager_fallback(Tensor input_t, int[] axis, bool keep_
198195
var _inputs_flat = input.concat(axis1);
199196
var _attrs = new object[] { "keep_dims", keep_dims, "T", _attr_T, "Tidx", _attr_Tidx };
200197

201-
return _execute.execute(ctx, "Prod", _inputs_flat, _attrs, name: name);
198+
return _execute.execute(ctx, "Prod", 1, _inputs_flat, _attrs, name: name);
202199
}
203200

204201
public static Tensor acos(Tensor x, string name = null)
@@ -599,7 +596,7 @@ public static Tensor cast(Tensor x, TF_DataType DstT, bool Truncate= false, stri
599596
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
600597
"Cast", name,
601598
new IntPtr[] { x as EagerTensor }, 1,
602-
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "DstT", DstT, "Truncate", Truncate }, 0, status),
599+
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "DstT", DstT, "Truncate", Truncate }, status),
603600
status);
604601
status.Check(true);
605602
return new EagerTensor(tensor);
@@ -836,10 +833,24 @@ public static Tensor mat_mul(Tensor a, Tensor b, bool transpose_a = false, bool
836833
{
837834
if (tf.context.executing_eagerly())
838835
{
839-
var _result = wrap_tfe_src.TFE_FastPathExecute(tf.context, tf.context.device_name,
840-
"MatMul", name, null,
841-
a, b, "transpose_a", transpose_a, "transpose_b", transpose_b);
842-
return _result;
836+
using var status = new Status();
837+
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
838+
"MatMul", name,
839+
new IntPtr[]
840+
{
841+
a as EagerTensor,
842+
b as EagerTensor
843+
}, 2,
844+
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[]
845+
{
846+
"transpose_a",
847+
transpose_a,
848+
"transpose_b",
849+
transpose_b
850+
}, status),
851+
status);
852+
status.Check(true);
853+
return new EagerTensor(tensor);
843854
}
844855

845856
var _op = _op_def_lib._apply_op_helper("MatMul", name, args: new { a, b, transpose_a, transpose_b });
@@ -944,10 +955,18 @@ public static Tensor _sum<Tx, Ty>(Tx input, Ty axis = default, bool keep_dims =
944955
{
945956
try
946957
{
947-
var _result = wrap_tfe_src.TFE_FastPathExecute(tf.context, tf.context.device_name,
948-
"Sum", name, null,
949-
input, axis, "keep_dims", keep_dims);
950-
return _result;
958+
using var status = new Status();
959+
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
960+
"Sum", name,
961+
new IntPtr[]
962+
{
963+
input as EagerTensor,
964+
axis as EagerTensor
965+
}, 2,
966+
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "keep_dims", keep_dims }, status),
967+
status);
968+
status.Check(true);
969+
return new EagerTensor(tensor);
951970
}
952971
catch (Exception)
953972
{
@@ -968,7 +987,7 @@ private static Tensor _sum_eager_fallback(Tensor[] inputs, Tensor axis, bool kee
968987
var _inputs_flat = input.concat(axis1);
969988
var _attrs = new object[] { "keep_dims", keep_dims, "T", _attr_T, "Tidx", _attr_Tidx };
970989

971-
return _execute.execute(ctx, "Sum", _inputs_flat, _attrs, name: name);
990+
return _execute.execute(ctx, "Sum", 1, _inputs_flat, _attrs, name: name);
972991
}
973992

974993
/// <summary>

src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static Tensor var_handle_op(TF_DataType dtype, TensorShape shape,
8787
"shared_name", shared_name,
8888
"dtype", dtype,
8989
"shape", shape.dims
90-
}, 0, status),
90+
}, status),
9191
status);
9292
status.Check(true);
9393
return new EagerTensor(tensor);
@@ -118,7 +118,7 @@ public static Tensor read_variable_op(Tensor resource, TF_DataType dtype, string
118118
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
119119
"ReadVariableOp", name,
120120
new IntPtr[] { resource as EagerTensor }, 1,
121-
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "dtype", dtype }, 0, status),
121+
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "dtype", dtype }, status),
122122
status);
123123
status.Check(true);
124124
return new EagerTensor(tensor);

src/TensorFlowNET.Core/Tensors/constant_op.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private static Tensor _eager_fill(int[] dims, Tensor value, Context ctx)
107107
var dims_t = convert_to_eager_tensor(dims, ctx, dtypes.int32);
108108
var inputs_flat = new[] { dims_t, value };
109109
var attrs = new object[] { "T", attr_t, "index_type", TF_DataType.TF_INT32 };
110-
var result = _execute.execute(ctx, "Fill", inputs_flat, attrs);
110+
var result = _execute.execute(ctx, "Fill", 1, inputs_flat, attrs);
111111
return result;
112112
}
113113

0 commit comments

Comments
 (0)