Skip to content

Commit c2f13e2

Browse files
sharwellEsther2013
authored andcommitted
Implement SafeExecutorHandle as a wrapper for TFE_Executor
1 parent e87cc04 commit c2f13e2

5 files changed

Lines changed: 47 additions & 35 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*****************************************************************************
2+
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
******************************************************************************/
16+
17+
using System;
18+
using Tensorflow.Util;
19+
20+
namespace Tensorflow.Eager
21+
{
22+
public sealed class SafeExecutorHandle : SafeTensorflowHandle
23+
{
24+
private SafeExecutorHandle()
25+
{
26+
}
27+
28+
public SafeExecutorHandle(IntPtr handle)
29+
: base(handle)
30+
{
31+
}
32+
33+
protected override bool ReleaseHandle()
34+
{
35+
c_api.TFE_DeleteExecutor(handle);
36+
SetHandle(IntPtr.Zero);
37+
return true;
38+
}
39+
}
40+
}

‎src/TensorFlowNET.Core/Eager/TFE_Executor.cs‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Runtime.InteropServices;
33
using Tensorflow.Device;
44
using Tensorflow.Eager;
5-
using TFE_Executor = System.IntPtr;
65

76
namespace Tensorflow
87
{
@@ -299,7 +298,7 @@ public partial class c_api
299298
/// <param name="is_async"></param>
300299
/// <returns>TFE_Executor*</returns>
301300
[DllImport(TensorFlowLibName)]
302-
public static extern TFE_Executor TFE_NewExecutor(bool is_async);
301+
public static extern SafeExecutorHandle TFE_NewExecutor(bool is_async);
303302

304303
/// <summary>
305304
/// Deletes the eager Executor without waiting for enqueued nodes. Please call
@@ -322,7 +321,7 @@ public partial class c_api
322321
/// <param name="executor">TFE_Executor*</param>
323322
/// <param name="status">TF_Status*</param>
324323
[DllImport(TensorFlowLibName)]
325-
public static extern void TFE_ExecutorWaitForAllPendingNodes(TFE_Executor executor, SafeStatusHandle status);
324+
public static extern void TFE_ExecutorWaitForAllPendingNodes(SafeExecutorHandle executor, SafeStatusHandle status);
326325

327326
/// <summary>
328327
/// Sets a custom Executor for current thread. All nodes created by this thread
@@ -331,15 +330,15 @@ public partial class c_api
331330
/// <param name="ctx"></param>
332331
/// <param name="executor"></param>
333332
[DllImport(TensorFlowLibName)]
334-
public static extern void TFE_ContextSetExecutorForThread(SafeContextHandle ctx, TFE_Executor executor);
333+
public static extern void TFE_ContextSetExecutorForThread(SafeContextHandle ctx, SafeExecutorHandle executor);
335334

336335
/// <summary>
337336
/// Returns the Executor for current thread.
338337
/// </summary>
339338
/// <param name="ctx"></param>
340339
/// <returns>TFE_Executor*</returns>
341340
[DllImport(TensorFlowLibName)]
342-
public static extern TFE_Executor TFE_ContextGetExecutorForThread(SafeContextHandle ctx);
341+
public static extern SafeExecutorHandle TFE_ContextGetExecutorForThread(SafeContextHandle ctx);
343342

344343
[DllImport(TensorFlowLibName)]
345344
public static extern IntPtr TFE_TapeSetNew(bool persistent, bool watch_accessed_variables);

‎test/TensorFlowNET.UnitTest/NativeAPI/CApiTest.cs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,10 @@ protected void TFE_DeleteTensorHandle(IntPtr h)
124124
protected void TFE_DeleteOp(IntPtr op)
125125
=> c_api.TFE_DeleteOp(op);
126126

127-
protected void TFE_DeleteExecutor(IntPtr executor)
128-
=> c_api.TFE_DeleteExecutor(executor);
129-
130-
protected IntPtr TFE_ContextGetExecutorForThread(SafeContextHandle ctx)
127+
protected SafeExecutorHandle TFE_ContextGetExecutorForThread(SafeContextHandle ctx)
131128
=> c_api.TFE_ContextGetExecutorForThread(ctx);
132129

133-
protected void TFE_ExecutorWaitForAllPendingNodes(IntPtr executor, SafeStatusHandle status)
130+
protected void TFE_ExecutorWaitForAllPendingNodes(SafeExecutorHandle executor, SafeStatusHandle status)
134131
=> c_api.TFE_ExecutorWaitForAllPendingNodes(executor, status);
135132

136133
protected IntPtr TFE_TensorHandleResolve(IntPtr h, SafeStatusHandle status)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,9 @@ static SafeContextHandle NewContext(SafeStatusHandle status)
6565

6666
TFE_DeleteTensorHandle(hcpu);
6767
// not export api
68-
var executor = TFE_ContextGetExecutorForThread(ctx);
68+
using var executor = TFE_ContextGetExecutorForThread(ctx);
6969
TFE_ExecutorWaitForAllPendingNodes(executor, status);
7070
ASSERT_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
71-
TFE_DeleteExecutor(executor);
7271
}
7372
}
7473
}

0 commit comments

Comments
 (0)