Skip to content

Commit f3a5d19

Browse files
committed
fix AutoNumPy crash when in multiple thread.
1 parent 8f32e83 commit f3a5d19

File tree

6 files changed

+10
-11
lines changed

6 files changed

+10
-11
lines changed

src/TensorFlowNET.Core/Graphs/Graph.Import.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public bool Import(byte[] bytes, string prefix = "")
5252
using (var status = new Status())
5353
using (var graph_def = new Buffer(bytes))
5454
{
55-
as_default();
5655
c_api.TF_ImportGraphDefOptionsSetPrefix(opts.Handle, prefix);
5756
c_api.TF_GraphImportGraphDef(_handle, graph_def.Handle, opts.Handle, status.Handle);
5857
status.Check(true);

src/TensorFlowNET.Core/NumPy/AutoNumPyAttribute.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Diagnostics;
5+
using System.Threading;
56
using static Tensorflow.Binding;
67

78
namespace Tensorflow.NumPy
@@ -10,9 +11,12 @@ namespace Tensorflow.NumPy
1011
public sealed class AutoNumPyAttribute : OnMethodBoundaryAspect
1112
{
1213
bool _changedMode = false;
13-
14+
bool _locked = false;
15+
static object locker = new Object();
1416
public override void OnEntry(MethodExecutionArgs args)
1517
{
18+
Monitor.Enter(locker, ref _locked);
19+
1620
if (!tf.executing_eagerly())
1721
{
1822
tf.Context.eager_mode();
@@ -24,6 +28,9 @@ public override void OnExit(MethodExecutionArgs args)
2428
{
2529
if (_changedMode)
2630
tf.Context.restore_mode();
31+
32+
if (_locked)
33+
Monitor.Exit(locker);
2734
}
2835
}
2936
}

src/TensorFlowNET.Core/Numpy/NDArray.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public partial class NDArray : Tensor, IEnumerable<NDArray>
3030

3131
[AutoNumPy]
3232
public NDArray reshape(Shape newshape) => new NDArray(tf.reshape(this, newshape));
33+
[AutoNumPy]
3334
public NDArray astype(TF_DataType dtype) => new NDArray(math_ops.cast(this, dtype));
3435
public NDArray ravel() => throw new NotImplementedException("");
3536
public void shuffle(NDArray nd) => np.random.shuffle(nd);

src/TensorFlowNET.Core/Operations/math_ops.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ public static Tensor cast(Tensor x, TF_DataType dtype = TF_DataType.DtInvalid, s
126126
return tf_with(ops.name_scope(name, "Cast", new { x }), scope =>
127127
{
128128
name = scope;
129-
x = ops.convert_to_tensor(x, name: "x");
130129
if (x.dtype.as_base_dtype() != base_type)
131130
x = gen_math_ops.cast(x, base_type, name: name);
132131

src/TensorFlowNET.Core/Tensors/constant_op.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private static Tensor convert_to_eager_tensor(object value, Context ctx, TF_Data
101101
value is NDArray nd &&
102102
nd.dtype != dtype)
103103
{
104-
value = nd.astype(dtype);
104+
value = math_ops.cast(nd, dtype);
105105
}
106106

107107
// non ascii char

test/TensorFlowNET.UnitTest/Utilities/FluentExtension.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,6 @@ public AndConstraint<NDArrayAssertions> BeScalar()
197197
return new AndConstraint<NDArrayAssertions>(this);
198198
}
199199

200-
public AndConstraint<NDArrayAssertions> BeScalar(object value)
201-
{
202-
Subject.shape.IsScalar.Should().BeTrue();
203-
Subject.GetValue().Should().Be(value);
204-
return new AndConstraint<NDArrayAssertions>(this);
205-
}
206-
207200
public AndConstraint<NDArrayAssertions> BeOfType(Type typeCode)
208201
{
209202
Subject.dtype.Should().Be(typeCode);

0 commit comments

Comments
 (0)