Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use a local Status variable #978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2023
Merged
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
29 changes: 9 additions & 20 deletions src/TensorFlowNET.Core/Sessions/BaseSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace Tensorflow
public class BaseSession : DisposableObject
{
protected Graph _graph;
protected Status _status;
public Graph graph => _graph;

public BaseSession(IntPtr handle, Graph g)
Expand All @@ -48,9 +49,9 @@ public BaseSession(string target = "", Graph g = null, ConfigProto config = null
}

using var opts = new SessionOptions(target, config);
status = status ?? tf.Status;
_handle = c_api.TF_NewSession(_graph, opts.Handle, status.Handle);
status.Check(true);
_status = status ?? tf.Status;
_handle = c_api.TF_NewSession(_graph, opts.Handle, _status.Handle);
_status.Check(true);
}

public virtual void run(Operation op, params FeedItem[] feed_dict)
Expand Down Expand Up @@ -217,8 +218,6 @@ private unsafe NDArray[] _call_tf_sessionrun(KeyValuePair<TF_Output, Tensor>[] f
// Ensure any changes to the graph are reflected in the runtime.
_extend_graph();

var status = tf.Status;

var output_values = fetch_list.Select(x => IntPtr.Zero).ToArray();

c_api.TF_SessionRun(_handle,
Expand All @@ -232,9 +231,9 @@ private unsafe NDArray[] _call_tf_sessionrun(KeyValuePair<TF_Output, Tensor>[] f
target_opers: target_list.Select(f => (IntPtr)f).ToArray(),
ntargets: target_list.Count,
run_metadata: IntPtr.Zero,
status: status.Handle);
status: _status.Handle);

status.Check(true);
_status.Check(true);

var result = new NDArray[fetch_list.Length];

Expand All @@ -246,8 +245,6 @@ private unsafe NDArray[] _call_tf_sessionrun(KeyValuePair<TF_Output, Tensor>[] f

public unsafe Tensor eval(Tensor tensor)
{
var status = tf.Status;

var output_values = new IntPtr[1];
var fetch_list = new[] { tensor._as_tf_output() };

Expand All @@ -262,9 +259,9 @@ public unsafe Tensor eval(Tensor tensor)
target_opers: new IntPtr[0],
ntargets: 0,
run_metadata: IntPtr.Zero,
status: status.Handle);
status: _status.Handle);

status.Check(true);
_status.Check(true);

return new Tensor(new SafeTensorHandle(output_values[0]));
}
Expand All @@ -291,15 +288,7 @@ private void _extend_graph()
protected override void DisposeUnmanagedResources(IntPtr handle)
{
// c_api.TF_CloseSession(handle, tf.Status.Handle);
if (tf.Status == null || tf.Status.Handle.IsInvalid)
{
using var status = new Status();
c_api.TF_DeleteSession(handle, status.Handle);
}
else
{
c_api.TF_DeleteSession(handle, tf.Status.Handle);
}
c_api.TF_DeleteSession(handle, _status.Handle);
}
}
}