Skip to content

Commit d7925ec

Browse files
authored
Merge pull request SciSharp#560 from sharwell/safe-context-options-handle
Implement SafeContextOptionsHandle as a wrapper for TFE_ContextOptions
2 parents 6f74c42 + 6d73b8d commit d7925ec

12 files changed

Lines changed: 121 additions & 75 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public sealed class Context : IDisposable
1616

1717
public Context(ContextOptions opts, Status status)
1818
{
19-
Handle = c_api.TFE_NewContext(opts, status.Handle);
19+
Handle = c_api.TFE_NewContext(opts.Handle, status.Handle);
2020
status.Check(true);
2121
}
2222

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
1-
using System;
2-
using System.IO;
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;
318

419
namespace Tensorflow.Eager
520
{
6-
public class ContextOptions : DisposableObject
21+
public sealed class ContextOptions : IDisposable
722
{
8-
public ContextOptions() : base(c_api.TFE_NewContextOptions())
9-
{ }
23+
public SafeContextOptionsHandle Handle { get; }
1024

11-
/// <summary>
12-
/// Dispose any unmanaged resources related to given <paramref name="handle"/>.
13-
/// </summary>
14-
protected sealed override void DisposeUnmanagedResources(IntPtr handle)
15-
=> c_api.TFE_DeleteContextOptions(_handle);
25+
public ContextOptions()
26+
{
27+
Handle = c_api.TFE_NewContextOptions();
28+
}
1629

17-
18-
public static implicit operator IntPtr(ContextOptions opts)
19-
=> opts._handle;
20-
21-
public static implicit operator TFE_ContextOptions(ContextOptions opts)
22-
=> new TFE_ContextOptions(opts._handle);
23-
30+
public void Dispose()
31+
=> Handle.Dispose();
2432
}
25-
2633
}
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 SafeContextOptionsHandle : SafeTensorflowHandle
23+
{
24+
public SafeContextOptionsHandle()
25+
{
26+
}
27+
28+
public SafeContextOptionsHandle(IntPtr handle)
29+
: base(handle)
30+
{
31+
}
32+
33+
protected override bool ReleaseHandle()
34+
{
35+
c_api.TFE_DeleteContextOptions(handle);
36+
SetHandle(IntPtr.Zero);
37+
return true;
38+
}
39+
}
40+
}

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

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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public delegate void delete_backward_function_callback(string op_name,
5252
/// </summary>
5353
/// <returns>TFE_ContextOptions*</returns>
5454
[DllImport(TensorFlowLibName)]
55-
public static extern TFE_ContextOptions TFE_NewContextOptions();
55+
public static extern SafeContextOptionsHandle TFE_NewContextOptions();
5656

5757
/// <summary>
5858
/// Destroy an options object.
@@ -114,7 +114,7 @@ public delegate void delete_backward_function_callback(string op_name,
114114
/// <param name="status">TF_Status*</param>
115115
/// <returns>TFE_Context*</returns>
116116
[DllImport(TensorFlowLibName)]
117-
public static extern SafeContextHandle TFE_NewContext(IntPtr opts, SafeStatusHandle status);
117+
public static extern SafeContextHandle TFE_NewContext(SafeContextOptionsHandle opts, SafeStatusHandle status);
118118

119119
[DllImport(TensorFlowLibName)]
120120
public static extern void TFE_ContextStartStep(SafeContextHandle ctx);
@@ -254,7 +254,7 @@ public delegate void delete_backward_function_callback(string op_name,
254254
/// <param name="opts">TFE_ContextOptions*</param>
255255
/// <param name="enable">unsigned char</param>
256256
[DllImport(TensorFlowLibName)]
257-
public static extern void TFE_ContextOptionsSetAsync(IntPtr opts, byte enable);
257+
public static extern void TFE_ContextOptionsSetAsync(SafeContextOptionsHandle opts, byte enable);
258258

259259
/// <summary>
260260
///

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,12 @@ protected IntPtr TFE_NewTensorHandle(IntPtr t, SafeStatusHandle status)
102102
protected void TFE_Execute(IntPtr op, IntPtr[] retvals, ref int num_retvals, SafeStatusHandle status)
103103
=> c_api.TFE_Execute(op, retvals, ref num_retvals, status);
104104

105-
protected IntPtr TFE_NewContextOptions()
105+
protected SafeContextOptionsHandle TFE_NewContextOptions()
106106
=> c_api.TFE_NewContextOptions();
107107

108-
protected SafeContextHandle TFE_NewContext(IntPtr opts, SafeStatusHandle status)
108+
protected SafeContextHandle TFE_NewContext(SafeContextOptionsHandle opts, SafeStatusHandle status)
109109
=> c_api.TFE_NewContext(opts, status);
110110

111-
protected void TFE_DeleteContextOptions(IntPtr opts)
112-
=> c_api.TFE_DeleteContextOptions(opts);
113-
114111
protected int TFE_OpGetInputLength(IntPtr op, string input_name, SafeStatusHandle status)
115112
=> c_api.TFE_OpGetInputLength(op, input_name, status);
116113

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

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

56
namespace TensorFlowNET.UnitTest.NativeAPI
67
{
@@ -13,13 +14,16 @@ public partial class CApiEagerTest
1314
public void Context()
1415
{
1516
using var status = c_api.TF_NewStatus();
16-
var opts = c_api.TFE_NewContextOptions();
1717

18-
IntPtr devices;
19-
using (var ctx = c_api.TFE_NewContext(opts, status))
18+
static SafeContextHandle NewContext(SafeStatusHandle status)
2019
{
21-
c_api.TFE_DeleteContextOptions(opts);
20+
using var opts = c_api.TFE_NewContextOptions();
21+
return c_api.TFE_NewContext(opts, status);
22+
}
2223

24+
IntPtr devices;
25+
using (var ctx = NewContext(status))
26+
{
2327
devices = c_api.TFE_ContextListDevices(ctx, status);
2428
EXPECT_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
2529
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using System;
33
using Tensorflow;
4+
using Tensorflow.Eager;
45
using static Tensorflow.Binding;
56

67
namespace TensorFlowNET.UnitTest.NativeAPI
@@ -19,14 +20,18 @@ public unsafe void Execute_MatMul_CPU()
1920
unsafe void Execute_MatMul_CPU(bool async)
2021
{
2122
using var status = TF_NewStatus();
22-
var opts = TFE_NewContextOptions();
23-
c_api.TFE_ContextOptionsSetAsync(opts, Convert.ToByte(async));
23+
24+
static SafeContextHandle NewContext(bool async, SafeStatusHandle status)
25+
{
26+
using var opts = c_api.TFE_NewContextOptions();
27+
c_api.TFE_ContextOptionsSetAsync(opts, Convert.ToByte(async));
28+
return c_api.TFE_NewContext(opts, status);
29+
}
2430

2531
IntPtr t;
26-
using (var ctx = TFE_NewContext(opts, status))
32+
using (var ctx = NewContext(async, status))
2733
{
2834
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
29-
TFE_DeleteContextOptions(opts);
3035

3136
var m = TestMatrixTensorHandle();
3237
var matmul = MatMulOp(ctx, m, m);

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

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

76
namespace TensorFlowNET.UnitTest.NativeAPI
87
{
@@ -15,10 +14,15 @@ public partial class CApiEagerTest
1514
public unsafe void OpGetInputAndOutputLengths()
1615
{
1716
using var status = TF_NewStatus();
18-
var opts = TFE_NewContextOptions();
19-
using var ctx = TFE_NewContext(opts, status);
17+
18+
static SafeContextHandle NewContext(SafeStatusHandle status)
19+
{
20+
using var opts = c_api.TFE_NewContextOptions();
21+
return c_api.TFE_NewContext(opts, status);
22+
}
23+
24+
using var ctx = NewContext(status);
2025
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
21-
TFE_DeleteContextOptions(opts);
2226

2327
var input1 = TestMatrixTensorHandle();
2428
var input2 = TestMatrixTensorHandle();

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using System;
3-
using System.Collections.Generic;
43
using Tensorflow;
54
using Tensorflow.Eager;
6-
using Buffer = System.Buffer;
7-
using System.Linq;
85

96
namespace TensorFlowNET.UnitTest.NativeAPI
107
{
@@ -17,10 +14,15 @@ public partial class CApiEagerTest
1714
public unsafe void OpInferMixedTypeInputListAttrs()
1815
{
1916
using var status = TF_NewStatus();
20-
var opts = TFE_NewContextOptions();
21-
using var ctx = TFE_NewContext(opts, status);
17+
18+
static SafeContextHandle NewContext(SafeStatusHandle status)
19+
{
20+
using var opts = c_api.TFE_NewContextOptions();
21+
return c_api.TFE_NewContext(opts, status);
22+
}
23+
24+
using var ctx = NewContext(status);
2225
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
23-
TFE_DeleteContextOptions(opts);
2426

2527
var condition = TestScalarTensorHandle(true);
2628
var t1 = TestMatrixTensorHandle();

0 commit comments

Comments
 (0)