Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/TensorFlowNET.Core/Eager/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class Context : IDisposable

public Context(ContextOptions opts, Status status)
{
Handle = c_api.TFE_NewContext(opts, status.Handle);
Handle = c_api.TFE_NewContext(opts.Handle, status.Handle);
status.Check(true);
}

Expand Down
43 changes: 25 additions & 18 deletions src/TensorFlowNET.Core/Eager/ContextOptions.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
using System;
using System.IO;
/*****************************************************************************
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/

using System;

namespace Tensorflow.Eager
{
public class ContextOptions : DisposableObject
public sealed class ContextOptions : IDisposable
{
public ContextOptions() : base(c_api.TFE_NewContextOptions())
{ }
public SafeContextOptionsHandle Handle { get; }

/// <summary>
/// Dispose any unmanaged resources related to given <paramref name="handle"/>.
/// </summary>
protected sealed override void DisposeUnmanagedResources(IntPtr handle)
=> c_api.TFE_DeleteContextOptions(_handle);
public ContextOptions()
{
Handle = c_api.TFE_NewContextOptions();
}


public static implicit operator IntPtr(ContextOptions opts)
=> opts._handle;

public static implicit operator TFE_ContextOptions(ContextOptions opts)
=> new TFE_ContextOptions(opts._handle);

public void Dispose()
=> Handle.Dispose();
}

}
40 changes: 40 additions & 0 deletions src/TensorFlowNET.Core/Eager/SafeContextOptionsHandle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*****************************************************************************
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/

using System;
using Tensorflow.Util;

namespace Tensorflow.Eager
{
public sealed class SafeContextOptionsHandle : SafeTensorflowHandle
{
public SafeContextOptionsHandle()
{
}

public SafeContextOptionsHandle(IntPtr handle)
: base(handle)
{
}

protected override bool ReleaseHandle()
{
c_api.TFE_DeleteContextOptions(handle);
SetHandle(IntPtr.Zero);
return true;
}
}
}
23 changes: 0 additions & 23 deletions src/TensorFlowNET.Core/Eager/TFE_ContextOptions.cs

This file was deleted.

6 changes: 3 additions & 3 deletions src/TensorFlowNET.Core/Eager/c_api.eager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public delegate void delete_backward_function_callback(string op_name,
/// </summary>
/// <returns>TFE_ContextOptions*</returns>
[DllImport(TensorFlowLibName)]
public static extern TFE_ContextOptions TFE_NewContextOptions();
public static extern SafeContextOptionsHandle TFE_NewContextOptions();

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

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

/// <summary>
///
Expand Down
7 changes: 2 additions & 5 deletions test/TensorFlowNET.UnitTest/NativeAPI/CApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,12 @@ protected IntPtr TFE_NewTensorHandle(IntPtr t, SafeStatusHandle status)
protected void TFE_Execute(IntPtr op, IntPtr[] retvals, ref int num_retvals, SafeStatusHandle status)
=> c_api.TFE_Execute(op, retvals, ref num_retvals, status);

protected IntPtr TFE_NewContextOptions()
protected SafeContextOptionsHandle TFE_NewContextOptions()
=> c_api.TFE_NewContextOptions();

protected SafeContextHandle TFE_NewContext(IntPtr opts, SafeStatusHandle status)
protected SafeContextHandle TFE_NewContext(SafeContextOptionsHandle opts, SafeStatusHandle status)
=> c_api.TFE_NewContext(opts, status);

protected void TFE_DeleteContextOptions(IntPtr opts)
=> c_api.TFE_DeleteContextOptions(opts);

protected int TFE_OpGetInputLength(IntPtr op, string input_name, SafeStatusHandle status)
=> c_api.TFE_OpGetInputLength(op, input_name, status);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Tensorflow;
using Tensorflow.Eager;

namespace TensorFlowNET.UnitTest.NativeAPI
{
Expand All @@ -13,13 +14,16 @@ public partial class CApiEagerTest
public void Context()
{
using var status = c_api.TF_NewStatus();
var opts = c_api.TFE_NewContextOptions();

IntPtr devices;
using (var ctx = c_api.TFE_NewContext(opts, status))
static SafeContextHandle NewContext(SafeStatusHandle status)
{
c_api.TFE_DeleteContextOptions(opts);
using var opts = c_api.TFE_NewContextOptions();
return c_api.TFE_NewContext(opts, status);
}

IntPtr devices;
using (var ctx = NewContext(status))
{
devices = c_api.TFE_ContextListDevices(ctx, status);
EXPECT_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Tensorflow;
using Tensorflow.Eager;
using static Tensorflow.Binding;

namespace TensorFlowNET.UnitTest.NativeAPI
Expand All @@ -19,14 +20,18 @@ public unsafe void Execute_MatMul_CPU()
unsafe void Execute_MatMul_CPU(bool async)
{
using var status = TF_NewStatus();
var opts = TFE_NewContextOptions();
c_api.TFE_ContextOptionsSetAsync(opts, Convert.ToByte(async));

static SafeContextHandle NewContext(bool async, SafeStatusHandle status)
{
using var opts = c_api.TFE_NewContextOptions();
c_api.TFE_ContextOptionsSetAsync(opts, Convert.ToByte(async));
return c_api.TFE_NewContext(opts, status);
}

IntPtr t;
using (var ctx = TFE_NewContext(opts, status))
using (var ctx = NewContext(async, status))
Comment thread
sharwell marked this conversation as resolved.
Resolved
{
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
TFE_DeleteContextOptions(opts);

var m = TestMatrixTensorHandle();
var matmul = MatMulOp(ctx, m, m);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System;
using Tensorflow;
using Tensorflow.Eager;
using Buffer = System.Buffer;

namespace TensorFlowNET.UnitTest.NativeAPI
{
Expand All @@ -15,10 +14,15 @@ public partial class CApiEagerTest
public unsafe void OpGetInputAndOutputLengths()
{
using var status = TF_NewStatus();
var opts = TFE_NewContextOptions();
using var ctx = TFE_NewContext(opts, status);

static SafeContextHandle NewContext(SafeStatusHandle status)
{
using var opts = c_api.TFE_NewContextOptions();
return c_api.TFE_NewContext(opts, status);
}

using var ctx = NewContext(status);
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
TFE_DeleteContextOptions(opts);

var input1 = TestMatrixTensorHandle();
var input2 = TestMatrixTensorHandle();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using Tensorflow;
using Tensorflow.Eager;
using Buffer = System.Buffer;
using System.Linq;

namespace TensorFlowNET.UnitTest.NativeAPI
{
Expand All @@ -17,10 +14,15 @@ public partial class CApiEagerTest
public unsafe void OpInferMixedTypeInputListAttrs()
{
using var status = TF_NewStatus();
var opts = TFE_NewContextOptions();
using var ctx = TFE_NewContext(opts, status);

static SafeContextHandle NewContext(SafeStatusHandle status)
{
using var opts = c_api.TFE_NewContextOptions();
return c_api.TFE_NewContext(opts, status);
}

using var ctx = NewContext(status);
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
TFE_DeleteContextOptions(opts);

var condition = TestScalarTensorHandle(true);
var t1 = TestMatrixTensorHandle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System;
using Tensorflow;
using Tensorflow.Eager;
using Buffer = System.Buffer;

namespace TensorFlowNET.UnitTest.NativeAPI
{
Expand All @@ -15,9 +14,14 @@ public partial class CApiEagerTest
public unsafe void TensorHandleDevices()
{
var status = c_api.TF_NewStatus();
var opts = TFE_NewContextOptions();
using var ctx = TFE_NewContext(opts, status);
TFE_DeleteContextOptions(opts);

static SafeContextHandle NewContext(SafeStatusHandle status)
{
using var opts = c_api.TFE_NewContextOptions();
return c_api.TFE_NewContext(opts, status);
}

using var ctx = NewContext(status);
ASSERT_EQ(TF_OK, TF_GetCode(status), TF_Message(status));

var hcpu = TestMatrixTensorHandle();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Tensorflow;
using Tensorflow.Eager;
using static Tensorflow.Binding;

namespace TensorFlowNET.UnitTest.NativeAPI
Expand All @@ -14,10 +15,15 @@ public partial class CApiEagerTest
public unsafe void Variables()
{
using var status = c_api.TF_NewStatus();
var opts = TFE_NewContextOptions();
using var ctx = TFE_NewContext(opts, status);

static SafeContextHandle NewContext(SafeStatusHandle status)
{
using var opts = c_api.TFE_NewContextOptions();
return c_api.TFE_NewContext(opts, status);
}

using var ctx = NewContext(status);
ASSERT_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
TFE_DeleteContextOptions(opts);

var var_handle = CreateVariable(ctx, 12.0f, status);
ASSERT_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
Expand Down