Skip to content

Commit 830bb59

Browse files
committed
Merge branch 'multithreading'
2 parents 38f1a10 + 2b9bd5f commit 830bb59

34 files changed

Lines changed: 3054 additions & 663 deletions

‎TensorFlow.NET.sln.DotSettings‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<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">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Tensorflow/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

‎src/TensorFlowNET.Core/APIs/c_api.cs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ public static string StringPiece(IntPtr handle)
5454

5555
public struct DeallocatorArgs
5656
{
57+
internal static unsafe c_api.DeallocatorArgs* EmptyPtr;
58+
internal static unsafe IntPtr Empty;
59+
60+
static unsafe DeallocatorArgs()
61+
{
62+
Empty = new IntPtr(EmptyPtr = (DeallocatorArgs*) Marshal.AllocHGlobal(Marshal.SizeOf<DeallocatorArgs>()));
63+
*EmptyPtr = new DeallocatorArgs() {gc_handle = IntPtr.Zero, deallocator_called = false};
64+
}
65+
5766
public bool deallocator_called;
5867
public IntPtr gc_handle;
5968
}

‎src/TensorFlowNET.Core/APIs/tf.graph.cs‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,19 @@ public Graph get_default_graph()
2929
return ops.get_default_graph();
3030
}
3131

32-
public Graph Graph()
32+
/// <summary>
33+
/// Equivalent to <see cref="get_default_graph"/> but does not create a new graph if it there is none.
34+
/// </summary>
35+
public Graph peak_default_graph()
36+
{
37+
return ops.default_graph_stack.peak_controller();
38+
}
39+
40+
/// <summary>
41+
/// Creates a new graph.
42+
/// </summary>
43+
///<remarks>Has no interaction with graph defaulting. Equivalent to new Graph();</remarks>
44+
public Graph Graph()
3345
=> new Graph();
3446
}
35-
}
47+
}

‎src/TensorFlowNET.Core/Gradients/gradients_util.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static Tensor[] _GradientsHelper(Tensor[] ys,
6161
string grad_scope = scope;
6262
// Get a uid for this call to gradients that can be used to help
6363
// cluster ops for compilation.
64-
var gradient_uid = ops.get_default_graph().unique_name("uid");
64+
var gradient_uid = curr_graph.unique_name("uid");
6565
ys = ops.convert_n_to_tensor_or_indexed_slices(ys, name: "y");
6666
xs = ops.internal_convert_n_to_tensor_or_indexed_slices(xs, name: "x", as_ref: true);
6767
grad_ys = _DefaultGradYs(grad_ys, ys, colocate_gradients_with_ops, gradient_uid);
@@ -80,7 +80,7 @@ public static Tensor[] _GradientsHelper(Tensor[] ys,
8080
var to_ops = ys.Select(x => x.op).ToList();
8181
var from_ops = xs.Select(x => x.op).ToList();
8282
var stop_gradient_ops = stop_gradients.Select(x => x.op).ToList();
83-
(var reachable_to_ops, var pending_count, var loop_state) = _PendingCount(to_ops, from_ops, colocate_gradients_with_ops, new List<object>(), xs);
83+
var (reachable_to_ops, pending_count, loop_state) = _PendingCount(to_ops, from_ops, colocate_gradients_with_ops, new List<object>(), xs);
8484

8585
foreach (var (y, grad_y) in zip(ys, grad_ys))
8686
_SetGrad(grads, y, grad_y);
@@ -168,7 +168,7 @@ public static Tensor[] _GradientsHelper(Tensor[] ys,
168168
{
169169
if (in_grad != null)
170170
{
171-
if (in_grad is Tensor &&
171+
if (!(in_grad is null) &&
172172
in_grad.Tag == null && // maybe a IndexedSlice
173173
t_in.dtype != TF_DataType.TF_RESOURCE)
174174
{

‎src/TensorFlowNET.Core/Graphs/DefaultGraphStack.cs‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ limitations under the License.
2121

2222
namespace Tensorflow
2323
{
24-
2524
/// <summary>
2625
/// Serves as a stack for determining current default graph.
2726
/// </summary>
28-
public class DefaultGraphStack
27+
public class DefaultGraphStack
2928
{
3029
private readonly List<StackModel> _stack = new List<StackModel>();
3130

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

4140
public Graph get_controller()
4241
{
43-
if (_stack.Count(x => x.IsDefault) == 0)
42+
if (_stack.Count == 0 || _stack.Count(x => x.IsDefault) == 0)
4443
_stack.Add(new StackModel {Graph = tf.Graph(), IsDefault = true});
4544
for (var i = _stack.Count - 1; i >= 0; i--)
4645
{
@@ -52,6 +51,20 @@ public Graph get_controller()
5251
throw new TensorflowException("Unable to find a default graph");
5352
}
5453

54+
public Graph peak_controller()
55+
{
56+
if (_stack.Count == 0 || _stack.Count(x => x.IsDefault) == 0)
57+
return null;
58+
for (var i = _stack.Count - 1; i >= 0; i--)
59+
{
60+
var x = _stack[i];
61+
if (x.IsDefault)
62+
return x.Graph;
63+
}
64+
65+
return null;
66+
}
67+
5568
public bool remove(Graph g)
5669
{
5770
if (_stack.Count == 0)

‎src/TensorFlowNET.Core/Graphs/Graph.Operation.cs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,21 @@ public Operation[] ReturnOperations(IntPtr results)
5454
var handle = return_oper_handle.node + tf_op_size * i;
5555
return_opers[i] = new Operation(*(IntPtr*)handle);
5656
}
57-
}
58-
57+
}
58+
5959
return return_opers;
6060
}
6161

6262
public Operation OperationByName(string operName)
6363
{
6464
var handle = c_api.TF_GraphOperationByName(_handle, operName);
65-
if(graph_key != tf.get_default_graph().graph_key)
66-
{
67-
Console.WriteLine($"Current graph is not default graph.");
68-
// throw new ValueError($"Current graph is not default graph.");
65+
var defaultKey = tf.get_default_graph().graph_key;
66+
if (graph_key != defaultKey)
67+
{
68+
//Console.WriteLine($"Current graph is not default graph.");
69+
throw new ValueError($"Current graph is not default graph. Default Graph Key: {defaultKey}, Current Graph Key: {graph_key}");
6970
}
71+
7072
return new Operation(handle, g: this);
7173
}
7274

0 commit comments

Comments
 (0)