Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
e28d094
Create Locks.cs
Nucs Aug 30, 2019
a039e64
TensorflowNET.UnitTest: Added PrivateObject
Nucs Aug 30, 2019
70a90e5
TensorflowNET.UnitTest: Added MultiThreadedUnitTestExecuter
Nucs Aug 30, 2019
8cb6470
Graph.Operation: Throw ValueError exception when `Current graph is no…
Nucs Aug 30, 2019
a7cdf6e
ops.uid: Use Interlocked.Increment instead of ++
Nucs Aug 30, 2019
44d58bc
MultiThreadedUnitTestEvaluator: Fixed when using ThreadCount==1
Nucs Aug 30, 2019
067e62b
Graph: Added tf.peak_default_graph which just returns null if no defa…
Nucs Aug 30, 2019
3578c35
TensorFlowNET.UnitTest: Upgraded to LangVersion - Latest.
Nucs Aug 30, 2019
0bcddfd
DefaultGraphStack: Minor perf-op
Nucs Aug 30, 2019
f27b0c8
Graph, Session: defaulting was changed to thread-wide using ThreadLoc…
Nucs Aug 30, 2019
70c877d
gradients_utils: minor changes
Nucs Aug 30, 2019
7690501
Merge branch 'master' into multithreading
Nucs Aug 30, 2019
e4e62dd
Tensor: Rewrite Creation to fix heap corruption.
Nucs Aug 31, 2019
a539b9f
Merge branch 'gc-deallocation' into multithreading
Nucs Aug 31, 2019
be996fa
Tensor.Creation: Revamp of CreateTensorFromArray to properly handle T…
Nucs Aug 31, 2019
5d04607
MultiThreadedUnitTestExecuter: Fixed exception throwing.
Nucs Aug 31, 2019
2259eb5
Added process-wide locks to non-threadsafe calls. Added unit tests.
Nucs Aug 31, 2019
1272a60
Changed all urls of tf.org to tensorflow.org
Nucs Aug 31, 2019
e4786b7
Tensor.Creation.CreateTensorFromNDArray: Fixed when passed ND is sliced.
Nucs Aug 31, 2019
2b9bd5f
Tensor.Creation.CreateTensorFromNDArray: Fixed AllocationReferenceHol…
Nucs Aug 31, 2019
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: 2 additions & 0 deletions TensorFlow.NET.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Tensorflow/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
9 changes: 9 additions & 0 deletions src/TensorFlowNET.Core/APIs/c_api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public static string StringPiece(IntPtr handle)

public struct DeallocatorArgs
{
internal static unsafe c_api.DeallocatorArgs* EmptyPtr;
internal static unsafe IntPtr Empty;

static unsafe DeallocatorArgs()
{
Empty = new IntPtr(EmptyPtr = (DeallocatorArgs*) Marshal.AllocHGlobal(Marshal.SizeOf<DeallocatorArgs>()));
*EmptyPtr = new DeallocatorArgs() {gc_handle = IntPtr.Zero, deallocator_called = false};
}

public bool deallocator_called;
public IntPtr gc_handle;
}
Expand Down
16 changes: 14 additions & 2 deletions src/TensorFlowNET.Core/APIs/tf.graph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ public Graph get_default_graph()
return ops.get_default_graph();
}

public Graph Graph()
/// <summary>
/// Equivalent to <see cref="get_default_graph"/> but does not create a new graph if it there is none.
/// </summary>
public Graph peak_default_graph()
{
return ops.default_graph_stack.peak_controller();
}

/// <summary>
/// Creates a new graph.
/// </summary>
///<remarks>Has no interaction with graph defaulting. Equivalent to new Graph();</remarks>
public Graph Graph()
=> new Graph();
}
}
}
6 changes: 3 additions & 3 deletions src/TensorFlowNET.Core/Gradients/gradients_util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static Tensor[] _GradientsHelper(Tensor[] ys,
string grad_scope = scope;
// Get a uid for this call to gradients that can be used to help
// cluster ops for compilation.
var gradient_uid = ops.get_default_graph().unique_name("uid");
var gradient_uid = curr_graph.unique_name("uid");
ys = ops.convert_n_to_tensor_or_indexed_slices(ys, name: "y");
xs = ops.internal_convert_n_to_tensor_or_indexed_slices(xs, name: "x", as_ref: true);
grad_ys = _DefaultGradYs(grad_ys, ys, colocate_gradients_with_ops, gradient_uid);
Expand All @@ -80,7 +80,7 @@ public static Tensor[] _GradientsHelper(Tensor[] ys,
var to_ops = ys.Select(x => x.op).ToList();
var from_ops = xs.Select(x => x.op).ToList();
var stop_gradient_ops = stop_gradients.Select(x => x.op).ToList();
(var reachable_to_ops, var pending_count, var loop_state) = _PendingCount(to_ops, from_ops, colocate_gradients_with_ops, new List<object>(), xs);
var (reachable_to_ops, pending_count, loop_state) = _PendingCount(to_ops, from_ops, colocate_gradients_with_ops, new List<object>(), xs);

foreach (var (y, grad_y) in zip(ys, grad_ys))
_SetGrad(grads, y, grad_y);
Expand Down Expand Up @@ -168,7 +168,7 @@ public static Tensor[] _GradientsHelper(Tensor[] ys,
{
if (in_grad != null)
{
if (in_grad is Tensor &&
if (!(in_grad is null) &&
in_grad.Tag == null && // maybe a IndexedSlice
t_in.dtype != TF_DataType.TF_RESOURCE)
{
Expand Down
19 changes: 16 additions & 3 deletions src/TensorFlowNET.Core/Graphs/DefaultGraphStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ limitations under the License.

namespace Tensorflow
{

/// <summary>
/// Serves as a stack for determining current default graph.
/// </summary>
public class DefaultGraphStack
public class DefaultGraphStack
{
private readonly List<StackModel> _stack = new List<StackModel>();

Expand All @@ -40,7 +39,7 @@ public void set_controller(Graph @default)

public Graph get_controller()
{
if (_stack.Count(x => x.IsDefault) == 0)
if (_stack.Count == 0 || _stack.Count(x => x.IsDefault) == 0)
_stack.Add(new StackModel {Graph = tf.Graph(), IsDefault = true});
for (var i = _stack.Count - 1; i >= 0; i--)
{
Expand All @@ -52,6 +51,20 @@ public Graph get_controller()
throw new TensorflowException("Unable to find a default graph");
}

public Graph peak_controller()
{
if (_stack.Count == 0 || _stack.Count(x => x.IsDefault) == 0)
return null;
for (var i = _stack.Count - 1; i >= 0; i--)
{
var x = _stack[i];
if (x.IsDefault)
return x.Graph;
}

return null;
}

public bool remove(Graph g)
{
if (_stack.Count == 0)
Expand Down
14 changes: 8 additions & 6 deletions src/TensorFlowNET.Core/Graphs/Graph.Operation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,21 @@ public Operation[] ReturnOperations(IntPtr results)
var handle = return_oper_handle.node + tf_op_size * i;
return_opers[i] = new Operation(*(IntPtr*)handle);
}
}
}

return return_opers;
}

public Operation OperationByName(string operName)
{
var handle = c_api.TF_GraphOperationByName(_handle, operName);
if(graph_key != tf.get_default_graph().graph_key)
{
Console.WriteLine($"Current graph is not default graph.");
// throw new ValueError($"Current graph is not default graph.");
var defaultKey = tf.get_default_graph().graph_key;
if (graph_key != defaultKey)
{
//Console.WriteLine($"Current graph is not default graph.");
throw new ValueError($"Current graph is not default graph. Default Graph Key: {defaultKey}, Current Graph Key: {graph_key}");
}

return new Operation(handle, g: this);
}

Expand Down
Loading