Skip to content

Commit 095261a

Browse files
committed
add TensorHandleDevices unit test.
1 parent 88d0e96 commit 095261a

5 files changed

Lines changed: 107 additions & 4 deletions

File tree

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

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

45
namespace Tensorflow
56
{
@@ -196,5 +197,55 @@ public partial class c_api
196197
/// <param name="h">TFE_TensorHandle*</param>
197198
[DllImport(TensorFlowLibName)]
198199
public static extern void TFE_DeleteTensorHandle(IntPtr h);
200+
201+
/// <summary>
202+
/// Creates a new eager Executor. Nodes in one executor are guaranteed to be
203+
/// executed in sequence. Assigning nodes to different executors allows executing
204+
/// nodes in parallel.
205+
/// </summary>
206+
/// <param name="is_async"></param>
207+
/// <returns>TFE_Executor*</returns>
208+
[DllImport(TensorFlowLibName)]
209+
public static extern IntPtr TFE_NewExecutor(bool is_async);
210+
211+
/// <summary>
212+
/// Deletes the eager Executor without waiting for enqueued nodes. Please call
213+
/// TFE_ExecutorWaitForAllPendingNodes before calling this API if you want to
214+
/// make sure all nodes are finished.
215+
/// </summary>
216+
/// <param name="e">TFE_Executor*</param>
217+
[DllImport(TensorFlowLibName)]
218+
public static extern void TFE_DeleteExecutor(IntPtr executor);
219+
220+
/// <summary>
221+
/// Causes the calling thread to block till all ops dispatched in this executor
222+
/// have been executed. Note that "execution" here refers to kernel execution /
223+
/// scheduling of copies, etc. Similar to sync execution, it doesn't guarantee
224+
/// that lower level device queues (like GPU streams) have been flushed.
225+
///
226+
/// This call may not block for execution of ops enqueued concurrently with this
227+
/// call.
228+
/// </summary>
229+
/// <param name="executor">TFE_Executor*</param>
230+
/// <param name="status">TF_Status*</param>
231+
[DllImport(TensorFlowLibName)]
232+
public static extern void TFE_ExecutorWaitForAllPendingNodes(TFE_Executor executor, IntPtr status);
233+
234+
/// <summary>
235+
/// Sets a custom Executor for current thread. All nodes created by this thread
236+
/// will be added to this Executor. It will override current executor.
237+
/// </summary>
238+
/// <param name="ctx"></param>
239+
/// <param name="executor"></param>
240+
[DllImport(TensorFlowLibName)]
241+
public static extern void TFE_ContextSetExecutorForThread(IntPtr ctx, TFE_Executor executor);
242+
243+
/// <summary>
244+
/// Returns the Executor for current thread.
245+
/// </summary>
246+
/// <param name="ctx"></param>
247+
/// <returns>TFE_Executor*</returns>
248+
[DllImport(TensorFlowLibName)]
249+
public static extern TFE_Executor TFE_ContextGetExecutorForThread(IntPtr ctx);
199250
}
200251
}

‎test/TensorFlowNET.UnitTest/CApiTest.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ protected void TFE_DeleteTensorHandle(IntPtr h)
100100
protected void TFE_DeleteOp(IntPtr op)
101101
=> c_api.TFE_DeleteOp(op);
102102

103+
protected void TFE_DeleteExecutor(IntPtr executor)
104+
=> c_api.TFE_DeleteExecutor(executor);
105+
106+
protected IntPtr TFE_ContextGetExecutorForThread(IntPtr ctx)
107+
=> c_api.TFE_ContextGetExecutorForThread(ctx);
108+
109+
protected void TFE_ExecutorWaitForAllPendingNodes(IntPtr executor, IntPtr status)
110+
=> c_api.TFE_ExecutorWaitForAllPendingNodes(executor, status);
111+
103112
protected IntPtr TFE_TensorHandleResolve(IntPtr h, IntPtr status)
104113
=> c_api.TFE_TensorHandleResolve(h, status);
105114

@@ -127,6 +136,9 @@ protected void TF_DeleteDeviceList(IntPtr list)
127136
protected IntPtr TFE_TensorHandleCopyToDevice(IntPtr h, IntPtr ctx, string device_name, IntPtr status)
128137
=> c_api.TFE_TensorHandleCopyToDevice(h, ctx, device_name, status);
129138

139+
protected void TFE_OpSetDevice(IntPtr op, string device_name, IntPtr status)
140+
=> c_api.TFE_OpSetDevice(op, device_name, status);
141+
130142
protected unsafe void memcpy(void * src, IntPtr dst, ulong size)
131143
{
132144
Buffer.MemoryCopy(src, dst.ToPointer(), size, size);

‎test/TensorFlowNET.UnitTest/Eager/CApi.Eager.TensorHandleDevices.cs‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,36 @@ public unsafe void TensorHandleDevices()
3636
var hgpu = TFE_TensorHandleCopyToDevice(hcpu, ctx, gpu_device_name, status);
3737
ASSERT_TRUE(TF_GetCode(status) == TF_OK, TF_Message(status));
3838

39-
// shape_op = ShapeOp(ctx, hgpu);
39+
var shape_op = ShapeOp(ctx, hgpu);
40+
TFE_OpSetDevice(shape_op, gpu_device_name, status);
41+
ASSERT_TRUE(TF_GetCode(status) == TF_OK, TF_Message(status));
42+
var retvals = new IntPtr[1];
43+
int num_retvals = 1;
44+
c_api.TFE_Execute(shape_op, retvals, ref num_retvals, status);
45+
ASSERT_TRUE(TF_GetCode(status) == TF_OK, TF_Message(status));
46+
47+
// .device of shape is GPU since the op is executed on GPU
48+
device_name = TFE_TensorHandleDeviceName(retvals[0], status);
49+
ASSERT_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
50+
ASSERT_TRUE(device_name.Contains("GPU:0"));
51+
52+
// .backing_device of shape is CPU since the tensor is backed by CPU
53+
backing_device_name = TFE_TensorHandleBackingDeviceName(retvals[0], status);
54+
ASSERT_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
55+
ASSERT_TRUE(backing_device_name.Contains("CPU:0"));
56+
57+
TFE_DeleteOp(shape_op);
58+
TFE_DeleteTensorHandle(retvals[0]);
59+
TFE_DeleteTensorHandle(hgpu);
4060
}
61+
62+
TFE_DeleteTensorHandle(hcpu);
63+
// not export api
64+
/*var executor = TFE_ContextGetExecutorForThread(ctx);
65+
TFE_ExecutorWaitForAllPendingNodes(executor, status);
66+
ASSERT_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
67+
TFE_DeleteExecutor(executor);*/
68+
TFE_DeleteContext(ctx);
4169
}
4270
}
4371
}

‎test/TensorFlowNET.UnitTest/Eager/CApi.Eager.cs‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using System;
33
using Tensorflow;
4-
using Tensorflow.Eager;
5-
using Buffer = System.Buffer;
64

75
namespace TensorFlowNET.UnitTest.Eager
86
{
@@ -67,5 +65,19 @@ bool GetDeviceName(IntPtr ctx, ref string device_name, string device_type)
6765
TF_DeleteDeviceList(devices);
6866
return false;
6967
}
68+
69+
IntPtr ShapeOp(IntPtr ctx, IntPtr a)
70+
{
71+
var status = TF_NewStatus();
72+
73+
var op = TFE_NewOp(ctx, "Shape", status);
74+
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
75+
TFE_OpAddInput(op, a, status);
76+
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
77+
TF_DeleteStatus(status);
78+
TFE_OpSetAttrType(op, "T", TFE_TensorHandleDataType(a));
79+
80+
return op;
81+
}
7082
}
7183
}

‎test/TensorFlowNET.UnitTest/Tensorflow.UnitTest.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
3434
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
3535
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
36-
<PackageReference Include="SciSharp.TensorFlow.Redist-Windows-GPU" Version="1.15.1" />
36+
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="1.15.1" />
3737
</ItemGroup>
3838

3939
<ItemGroup>

0 commit comments

Comments
 (0)