Skip to content

Commit 855eba9

Browse files
committed
add TFE_Executor, TFE_Context and TFE_Op.
1 parent afaf0c8 commit 855eba9

21 files changed

Lines changed: 422 additions & 58 deletions

src/TensorFlowNET.Core/APIs/tf.math.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,17 @@ public Tensor reduce_all(Tensor input_tensor, int[] axis = null, bool keepdims =
448448
public Tensor reduce_prod(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null)
449449
=> math_ops.reduce_prod(input_tensor, axis: axis, keepdims: keepdims, name: name);
450450

451+
/// <summary>
452+
/// Computes the sum of elements across dimensions of a tensor.
453+
/// </summary>
454+
/// <param name="input_tensors"></param>
455+
/// <param name="axis"></param>
456+
/// <param name="keepdims"></param>
457+
/// <param name="name"></param>
458+
/// <returns></returns>
459+
public Tensor reduce_sum(Tensor[] input_tensors, int? axis = null, bool keepdims = false, string name = null)
460+
=> math_ops.reduce_sum(input_tensors, axis: axis, keepdims: keepdims, name: name);
461+
451462
/// <summary>
452463
/// Computes the sum of elements across dimensions of a tensor.
453464
/// </summary>

src/TensorFlowNET.Core/Eager/Context.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,21 @@ public class Context : DisposableObject
99

1010
public int default_execution_mode;
1111
public string device_name = "";
12+
bool _initialized = false;
1213

1314
public Context(ContextOptions opts, Status status)
1415
{
1516
_handle = c_api.TFE_NewContext(opts, status);
1617
status.Check(true);
1718
}
1819

20+
public void ensure_initialized()
21+
{
22+
if (_initialized)
23+
return;
24+
_initialized = true;
25+
}
26+
1927
/// <summary>
2028
/// Dispose any unmanaged resources related to given <paramref name="handle"/>.
2129
/// </summary>
@@ -27,5 +35,8 @@ protected sealed override void DisposeUnmanagedResources(IntPtr handle)
2735

2836
public static implicit operator IntPtr(Context ctx)
2937
=> ctx._handle;
38+
39+
public static implicit operator TFE_Context(Context ctx)
40+
=> new TFE_Context(ctx._handle);
3041
}
3142
}

src/TensorFlowNET.Core/Eager/ContextOptions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ protected sealed override void DisposeUnmanagedResources(IntPtr handle)
1717

1818
public static implicit operator IntPtr(ContextOptions opts)
1919
=> opts._handle;
20+
21+
public static implicit operator TFE_ContextOptions(ContextOptions opts)
22+
=> new TFE_ContextOptions(opts._handle);
23+
2024
}
2125

2226
}
Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,67 @@
11
using System.Collections.Generic;
2+
using System;
3+
using System.Linq;
24

35
namespace Tensorflow.Eager
46
{
57
public class Execute
68
{
9+
/// <summary>
10+
/// Execute a TensorFlow operation.
11+
/// </summary>
12+
/// <param name="op_name">
13+
/// Name of the TensorFlow operation (see REGISTER_OP in C++ code) to
14+
/// execute.
15+
/// </param>
16+
/// <param name="num_outputs">
17+
/// The number of outputs of the operation to fetch.
18+
/// </param>
19+
/// <param name="inputs">
20+
/// A list of inputs to the operation. Each entry should be a Tensor, or
21+
/// a value which can be passed to the Tensor constructor to create one.
22+
/// </param>
23+
/// <param name="attrs">
24+
/// A tuple with alternating string attr names and attr values for this
25+
/// operation.
26+
/// </param>
27+
/// <param name="ctx">The value of context.context().</param>
28+
/// <param name="name">Customized name for the operation.</param>
29+
/// <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)
31+
{
32+
ctx.ensure_initialized();
33+
using (var status = new Status())
34+
{
35+
var retVals = wrap_tfe_src.TFE_Py_Execute(ctx, ctx.device_name, op_name, inputs, attrs, 1, status);
36+
37+
var t = c_api.TFE_TensorHandleResolve(retVals[0], status);
38+
status.Check(true);
39+
40+
return new EagerTensor(t);
41+
}
42+
}
43+
44+
public (TF_DataType, Tensor) args_to_matching_eager(Tensor[] l, Context ctx, TF_DataType default_dtype = TF_DataType.DtInvalid)
45+
{
46+
var dtype = default_dtype;
47+
if(dtype == TF_DataType.DtInvalid)
48+
{
49+
var tensor = ops.convert_to_tensor(l, dtype, preferred_dtype: default_dtype, ctx: ctx);
50+
51+
if (dtype == TF_DataType.DtInvalid)
52+
dtype = tensor.dtype;
53+
54+
return (dtype, tensor);
55+
}
56+
else
57+
{
58+
return (dtype, l[0]);
59+
}
60+
}
61+
762
public void record_gradient(string op_name, InputList inputs, Dictionary<string, object> attrs, Tensor[] results, string name = null)
863
{
9-
pywrap_tfe_src.RecordGradient(op_name, inputs._inputs, attrs, results, name);
64+
wrap_tfe_src.RecordGradient(op_name, inputs._inputs, attrs, results, name);
1065
}
1166
}
1267
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow.Eager
6+
{
7+
public struct TFE_Context
8+
{
9+
IntPtr _handle;
10+
11+
public TFE_Context(IntPtr handle)
12+
=> _handle = handle;
13+
14+
public static implicit operator TFE_Context(IntPtr handle)
15+
=> new TFE_Context(handle);
16+
17+
public static implicit operator IntPtr(TFE_Context tensor)
18+
=> tensor._handle;
19+
20+
public override string ToString()
21+
=> $"TFE_Context {_handle}";
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow.Eager
6+
{
7+
public struct TFE_ContextOptions
8+
{
9+
IntPtr _handle;
10+
11+
public TFE_ContextOptions(IntPtr handle)
12+
=> _handle = handle;
13+
14+
public static implicit operator TFE_ContextOptions(IntPtr handle)
15+
=> new TFE_ContextOptions(handle);
16+
17+
public static implicit operator IntPtr(TFE_ContextOptions tensor)
18+
=> tensor._handle;
19+
20+
public override string ToString()
21+
=> $"TFE_ContextOptions {_handle}";
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow.Eager
6+
{
7+
public struct TFE_Executor
8+
{
9+
IntPtr _handle;
10+
11+
public TFE_Executor(IntPtr handle)
12+
=> _handle = handle;
13+
14+
public static implicit operator TFE_Executor(IntPtr handle)
15+
=> new TFE_Executor(handle);
16+
17+
public static implicit operator IntPtr(TFE_Executor tensor)
18+
=> tensor._handle;
19+
20+
public override string ToString()
21+
=> $"TFE_Executor {_handle}";
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow.Eager
6+
{
7+
public struct TFE_Op
8+
{
9+
IntPtr _handle;
10+
11+
public TFE_Op(IntPtr handle)
12+
=> _handle = handle;
13+
14+
public static implicit operator TFE_Op(IntPtr handle)
15+
=> new TFE_Op(handle);
16+
17+
public static implicit operator IntPtr(TFE_Op tensor)
18+
=> tensor._handle;
19+
20+
public override string ToString()
21+
=> $"TFE_Op {_handle}";
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow.Eager
6+
{
7+
public struct TFE_TensorHandle
8+
{
9+
IntPtr _handle;
10+
11+
public TFE_TensorHandle(IntPtr handle)
12+
=> _handle = handle;
13+
14+
public static implicit operator TFE_TensorHandle(IntPtr handle)
15+
=> new TFE_TensorHandle(handle);
16+
17+
public static implicit operator IntPtr(TFE_TensorHandle tensor)
18+
=> tensor._handle;
19+
20+
public override string ToString()
21+
=> $"TFE_TensorHandle {_handle}";
22+
}
23+
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Runtime.InteropServices;
3+
using Tensorflow.Eager;
34
using TFE_Executor = System.IntPtr;
45

56
namespace Tensorflow
@@ -11,7 +12,7 @@ public partial class c_api
1112
/// </summary>
1213
/// <returns>TFE_ContextOptions*</returns>
1314
[DllImport(TensorFlowLibName)]
14-
public static extern IntPtr TFE_NewContextOptions();
15+
public static extern TFE_ContextOptions TFE_NewContextOptions();
1516

1617
/// <summary>
1718
/// Destroy an options object.
@@ -70,7 +71,7 @@ public partial class c_api
7071
/// <param name="status">TF_Status*</param>
7172
/// <returns>TFE_Context*</returns>
7273
[DllImport(TensorFlowLibName)]
73-
public static extern IntPtr TFE_NewContext(IntPtr opts, IntPtr status);
74+
public static extern TFE_Context TFE_NewContext(IntPtr opts, IntPtr status);
7475

7576
/// <summary>
7677
///
@@ -88,7 +89,7 @@ public partial class c_api
8889
/// <param name="num_retvals">int*</param>
8990
/// <param name="status">TF_Status*</param>
9091
[DllImport(TensorFlowLibName)]
91-
public static extern void TFE_Execute(IntPtr op, IntPtr[] retvals, ref int num_retvals, IntPtr status);
92+
public static extern void TFE_Execute(TFE_Op op, IntPtr[] retvals, ref int num_retvals, IntPtr status);
9293

9394
/// <summary>
9495
///
@@ -98,7 +99,7 @@ public partial class c_api
9899
/// <param name="status">TF_Status*</param>
99100
/// <returns></returns>
100101
[DllImport(TensorFlowLibName)]
101-
public static extern IntPtr 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, IntPtr status);
102103

103104
/// <summary>
104105
///
@@ -150,7 +151,7 @@ public partial class c_api
150151
/// <param name="device_name"></param>
151152
/// <param name="status"></param>
152153
[DllImport(TensorFlowLibName)]
153-
public static extern void TFE_OpSetDevice(IntPtr op, string device_name, IntPtr status);
154+
public static extern void TFE_OpSetDevice(TFE_Op op, string device_name, IntPtr status);
154155

155156
/// <summary>
156157
///
@@ -167,7 +168,7 @@ public partial class c_api
167168
/// <param name="t">const tensorflow::Tensor&</param>
168169
/// <returns>TFE_TensorHandle*</returns>
169170
[DllImport(TensorFlowLibName)]
170-
public static extern IntPtr TFE_NewTensorHandle(IntPtr t, IntPtr status);
171+
public static extern TFE_TensorHandle TFE_NewTensorHandle(IntPtr t, IntPtr status);
171172

172173
/// <summary>
173174
/// Sets the default execution mode (sync/async). Note that this can be
@@ -195,7 +196,7 @@ public partial class c_api
195196
/// <param name="status">TF_Status*</param>
196197
/// <returns></returns>
197198
[DllImport(TensorFlowLibName)]
198-
public static extern IntPtr TFE_TensorHandleResolve(IntPtr h, IntPtr status);
199+
public static extern TF_Tensor TFE_TensorHandleResolve(IntPtr h, IntPtr status);
199200

200201
/// <summary>
201202
/// This function will block till the operation that produces `h` has completed.
@@ -252,7 +253,7 @@ public partial class c_api
252253
/// <param name="is_async"></param>
253254
/// <returns>TFE_Executor*</returns>
254255
[DllImport(TensorFlowLibName)]
255-
public static extern IntPtr TFE_NewExecutor(bool is_async);
256+
public static extern TFE_Executor TFE_NewExecutor(bool is_async);
256257

257258
/// <summary>
258259
/// Deletes the eager Executor without waiting for enqueued nodes. Please call

0 commit comments

Comments
 (0)