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/Graphs/c_api.graph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public partial class c_api
/// <param name="status">TF_Status*</param>
/// <returns></returns>
[DllImport(TensorFlowLibName)]
public static extern IntPtr TF_LoadSessionFromSavedModel(IntPtr session_options, IntPtr run_options,
public static extern IntPtr TF_LoadSessionFromSavedModel(SafeSessionOptionsHandle session_options, IntPtr run_options,
string export_dir, string[] tags, int tags_len,
IntPtr graph, ref TF_Buffer meta_graph_def, SafeStatusHandle status);

Expand Down
2 changes: 1 addition & 1 deletion src/TensorFlowNET.Core/Sessions/BaseSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public BaseSession(string target = "", Graph g = null, ConfigProto config = null
lock (Locks.ProcessWide)
{
status = status ?? new Status();
_handle = c_api.TF_NewSession(_graph, opts, status.Handle);
_handle = c_api.TF_NewSession(_graph, opts.Handle, status.Handle);
status.Check(true);
}
}
Expand Down
40 changes: 40 additions & 0 deletions src/TensorFlowNET.Core/Sessions/SafeSessionOptionsHandle.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
{
public sealed class SafeSessionOptionsHandle : SafeTensorflowHandle
{
public SafeSessionOptionsHandle()
{
}

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

protected override bool ReleaseHandle()
{
c_api.TF_DeleteSessionOptions(handle);
SetHandle(IntPtr.Zero);
return true;
}
}
}
4 changes: 2 additions & 2 deletions src/TensorFlowNET.Core/Sessions/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static Session LoadFromSavedModel(string path)
IntPtr sess;
try
{
sess = c_api.TF_LoadSessionFromSavedModel(opt,
sess = c_api.TF_LoadSessionFromSavedModel(opt.Handle,
IntPtr.Zero,
path,
tags,
Expand All @@ -66,7 +66,7 @@ public static Session LoadFromSavedModel(string path)
status.Check(true);
} catch (TensorflowException ex) when (ex.Message.Contains("Could not find SavedModel"))
{
sess = c_api.TF_LoadSessionFromSavedModel(opt,
sess = c_api.TF_LoadSessionFromSavedModel(opt.Handle,
IntPtr.Zero,
Path.GetFullPath(path),
tags,
Expand Down
36 changes: 14 additions & 22 deletions src/TensorFlowNET.Core/Sessions/SessionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,36 @@ limitations under the License.

using Google.Protobuf;
using System;
using System.Runtime.InteropServices;

namespace Tensorflow
{
internal class SessionOptions : DisposableObject
internal sealed class SessionOptions : IDisposable
{
public SafeSessionOptionsHandle Handle { get; }

public SessionOptions(string target = "", ConfigProto config = null)
{
_handle = c_api.TF_NewSessionOptions();
c_api.TF_SetTarget(_handle, target);
Handle = c_api.TF_NewSessionOptions();
c_api.TF_SetTarget(Handle, target);
if (config != null)
SetConfig(config);
}

public SessionOptions(IntPtr handle)
{
_handle = handle;
}

protected override void DisposeUnmanagedResources(IntPtr handle)
=> c_api.TF_DeleteSessionOptions(handle);
public void Dispose()
=> Handle.Dispose();

private void SetConfig(ConfigProto config)
private unsafe void SetConfig(ConfigProto config)
{
var bytes = config.ToByteArray();
var proto = Marshal.AllocHGlobal(bytes.Length);
Marshal.Copy(bytes, 0, proto, bytes.Length);

using (var status = new Status())
fixed (byte* proto2 = bytes)
{
c_api.TF_SetConfig(_handle, proto, (ulong)bytes.Length, status.Handle);
status.Check(false);
using (var status = new Status())
{
c_api.TF_SetConfig(Handle, (IntPtr)proto2, (ulong)bytes.Length, status.Handle);
status.Check(false);
}
}

Marshal.FreeHGlobal(proto);
}

public static implicit operator IntPtr(SessionOptions opts) => opts._handle;
public static implicit operator SessionOptions(IntPtr handle) => new SessionOptions(handle);
}
}
8 changes: 4 additions & 4 deletions src/TensorFlowNET.Core/Sessions/c_api.session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public partial class c_api
/// <param name="status">TF_Status*</param>
/// <returns>TF_Session*</returns>
[DllImport(TensorFlowLibName)]
public static extern IntPtr TF_NewSession(IntPtr graph, IntPtr opts, SafeStatusHandle status);
public static extern IntPtr TF_NewSession(IntPtr graph, SafeSessionOptionsHandle opts, SafeStatusHandle status);

/// <summary>
/// Return a new options object.
/// </summary>
/// <returns>TF_SessionOptions*</returns>
[DllImport(TensorFlowLibName)]
public static extern unsafe IntPtr TF_NewSessionOptions();
public static extern SafeSessionOptionsHandle TF_NewSessionOptions();

/// <summary>
/// Run the graph associated with the session starting with the supplied inputs
Expand Down Expand Up @@ -116,9 +116,9 @@ public static extern unsafe void TF_SessionRun(IntPtr session, TF_Buffer* run_op
/// <param name="proto_len">size_t</param>
/// <param name="status">TF_Status*</param>
[DllImport(TensorFlowLibName)]
public static extern void TF_SetConfig(IntPtr options, IntPtr proto, ulong proto_len, SafeStatusHandle status);
public static extern void TF_SetConfig(SafeSessionOptionsHandle options, IntPtr proto, ulong proto_len, SafeStatusHandle status);

[DllImport(TensorFlowLibName)]
public static extern void TF_SetTarget(IntPtr options, string target);
public static extern void TF_SetTarget(SafeSessionOptionsHandle options, string target);
}
}