8000 remove TF_DeleteTensor in test. · SciSharp/TensorFlow.NET@795c286 · GitHub
[go: up one dir, main page]

Skip to content

Commit 795c286

Browse files
committed
remove TF_DeleteTensor in test.
1 parent e73ed66 commit 795c286

File tree

13 files changed

+30
-61
lines changed

13 files changed

+30
-61
lines changed

src/TensorFlowNET.Core/NumPy/NDArray.Index.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,14 @@ NDArray GetData(IEnumerable<Slice> slices)
6262

6363
var tensor = base[slices.ToArray()];
6464
if (tensor.Handle == null)
65-
tensor = tf.defaultSession.eval(tensor);
66-
return new NDArray(tensor.Handle);
65+
{
66+
if (tf.executing_eagerly())
67+
return new NDArray(tensor);
68+
else
69+
tensor = tf.defaultSession.eval(tensor);
70+
}
71+
72+
return new NDArray(tensor);
6773
}
6874

6975
unsafe T GetAtIndex<T>(params int[] indices) where T : unmanaged

src/TensorFlowNET.Core/Tensors/Tensor.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@ public partial class Tensor : DisposableObject,
9797
/// </summary>
9898
public SafeTensorHandleHandle EagerTensorHandle => _eagerTensorHandle;
9999

100-
protected bool isReferencedByNDArray;
101-
public bool IsReferencedByNDArray => isReferencedByNDArray;
102-
103100
protected bool isCreatedInGraphMode;
104101

105102
public bool IsCreatedInGraphMode => isCreatedInGraphMode;
@@ -214,15 +211,6 @@ public TF_Output _as_tf_output()
214211

215212
return _tf_output.Value;
216213
}
217-
218-
public void SetReferencedByNDArray()
219-
{
220-
if (_handle is not null)
221-
{
222-
isReferencedByNDArray = true;
223-
_eagerTensorHandle = c_api.TFE_NewTensorHandle(_handle, tf.Status.Handle);
224-
}
225-
}
226214

227215
public Tensor MaybeMove()
228216
{

src/TensorFlowNET.Core/Tensors/tensor_util.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,6 @@ public static TensorProto make_tensor_proto(object values, TF_DataType dtype = T
141141
byte[] bytes = nd.ToByteArray();
142142
tensor_proto.TensorContent = Google.Protobuf.ByteString.CopyFrom(bytes);
143143
}
144-
else if (values is Tensor tensor && tensor.IsReferencedByNDArray)
145-
{
146-
var len = tensor.dtypesize * tensor.size;
147-
byte[] bytes = tensor.BufferToArray();
148-
tensor_proto.TensorContent = Google.Protobuf.ByteString.CopyFrom(bytes);
149-
}
150144
else if (!values.GetType().IsArray)
151145
{
152146
switch (values)

src/TensorFlowNET.Core/ops.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,6 @@ public static Tensor convert_to_tensor(object value,
149149
else
150150
return constant_op.constant(nd);
151151
}
152-
else if (value is Tensor tensor && tensor.IsReferencedByNDArray)
153-
{
154-
if (tf.executing_eagerly())
155-
return tensor;
156-
else
157-
return constant_op.constant(tensor);
158-
}
159152

160153
// graph mode
161154
Tensor ret = value switch

src/TensorFlowNet.Benchmarks/Leak/GpuLeakByCNN.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void Run()
5050
optimizer: keras.optimizers.RMSprop(),
5151
metrics: new[] { "accuracy" });
5252

53-
model.fit(new NDArray(inputImages), outLables, batch_size: 32, epochs: 200);
53+
model.fit(inputImages, outLables, batch_size: 32, epochs: 200);
5454

5555
keras.backend.clear_session();
5656
}

test/TensorFlowNET.Native.UnitTest/CApiTest.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ protected static string TF_Message(SafeStatusHandle s)
7474
protected SafeStatusHandle TF_NewStatus()
7575
=> c_api.TF_NewStatus();
7676

77-
protected void TF_DeleteTensor(SafeTensorHandle t)
78-
=> c_api.TF_DeleteTensor(t.DangerousGetHandle());
79-
8077
protected IntPtr TF_TensorData(SafeTensorHandle t)
8178
=> c_api.TF_TensorData(t);
8279

test/TensorFlowNET.Native.UnitTest/Eager/Eager.Execute_MatMul_CPU.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static SafeContextHandle NewContext(bool async, SafeStatusHandle status)
5858
EXPECT_EQ(product.Length * sizeof(float), (int)TF_TensorByteSize(t));
5959
tf.memcpy(product, TF_TensorData(t), TF_TensorByteSize(t));
6060

61-
c_api.TF_DeleteTensor(t.DangerousGetHandle());
61+
t.Dispose();
6262
EXPECT_EQ(7f, product[0]);
6363
EXPECT_EQ(10f, product[1]);
6464
EXPECT_EQ(15f, product[2]);

test/TensorFlowNET.Native.UnitTest/Eager/Eager.TensorHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public unsafe void TensorHandle()
2525
EXPECT_EQ(2.0f, data[1]);
2626
EXPECT_EQ(3.0f, data[2]);
2727
EXPECT_EQ(4.0f, data[3]);
28-
c_api.TF_DeleteTensor(t.DangerousGetHandle());
28+
t.Dispose();
2929
}
3030
}
3131
}

test/TensorFlowNET.Native.UnitTest/Eager/Eager.Variables.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static SafeContextHandle NewContext(SafeStatusHandle status)
5151
ASSERT_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
5252
ASSERT_EQ(sizeof(float), (int)TF_TensorByteSize(t));
5353
tf.memcpy(&value, TF_TensorData(t).ToPointer(), sizeof(float));
54-
c_api.TF_DeleteTensor(t.DangerousGetHandle());
54+
t.Dispose();
5555
EXPECT_EQ(12.0f, value);
5656
}
5757
finally

test/TensorFlowNET.Native.UnitTest/Eager/Eager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ SafeTensorHandleHandle TestMatrixTensorHandle()
2121
using var status = c_api.TF_NewStatus();
2222
var th = c_api.TFE_NewTensorHandle(t, status);
2323
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
24-
c_api.TF_DeleteTensor(t.DangerousGetHandle());
24+
t.Dispose();
2525
return th;
2626
}
2727

@@ -127,7 +127,7 @@ SafeTensorHandleHandle TestAxisTensorHandle()
127127
using var status = TF_NewStatus();
128128
var th = c_api.TFE_NewTensorHandle(t, status);
129129
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
130-
TF_DeleteTensor(t);
130+
t.Dispose();
131131
return th;
132132
}
133133

@@ -139,7 +139,7 @@ SafeTensorHandleHandle TestScalarTensorHandle(bool value)
139139
using var status = TF_NewStatus();
140140
var th = TFE_NewTensorHandle(t, status);
141141
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
142-
TF_DeleteTensor(t);
142+
t.Dispose();
143143
return th;
144144
}
145145

@@ -151,7 +151,7 @@ SafeTensorHandleHandle TestScalarTensorHandle(float value)
151151
using var status = TF_NewStatus();
152152
var th = TFE_NewTensorHandle(t, status);
153153
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
154-
TF_DeleteTensor(t);
154+
t.Dispose();
155155
return th;
156156
}
157157
}

test/TensorFlowNET.Native.UnitTest/Sessions/CSession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void SetOutputs(TF_Output[] outputs)
6464
foreach (var output in outputs)
6565
{
6666
outputs_.Add(output);
67-
output_values_.Add(new SafeTensorHandle(IntPtr.Zero));
67+
output_values_.Add(null);
6868
}
6969
}
7070

test/TensorFlowNET.Native.UnitTest/Tensors/TensorTest.cs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,18 @@ public unsafe void TensorFromFixed()
3939
public void TensorFromArray()
4040
{
4141
var array = new float[1000];
42-
/*using (var t = new Tensor(array, new long[] { array.Length }, tf.float32))
42+
using (var t = new Tensor(array))
4343
{
4444
Assert.IsFalse(t.IsDisposed);
4545
Assert.AreEqual(1000 * sizeof(float), (int)t.bytesize);
4646
}
4747

48-
using (var t = new Tensor(new float[] { 1 }, new long[] { 1 }, tf.float32))
48+
using (var t = new Tensor(1))
4949
{
5050
Assert.IsFalse(t.IsDisposed);
5151
Assert.AreEqual(1 * sizeof(float), (int)t.bytesize);
52+
Assert.AreEqual(t.shape, Shape.Scalar);
5253
}
53-
54-
using (var t = new Tensor(new float[] { 1 }, null, tf.float32))
55-
{
56-
Assert.IsFalse(t.IsDisposed);
57-
Assert.AreEqual(1 * sizeof(float), (int)t.bytesize);
58-
t.shape.Should().BeEmpty();
59-
}*/
6054
}
6155

6256
[TestMethod]
@@ -80,8 +74,7 @@ public void AllocateTensor()
8074
[TestMethod, Ignore]
8175
public void MaybeMove()
8276
{
83-
NDArray nd = np.array(2, 3);
84-
Tensor t = new Tensor(nd);
77+
Tensor t = new Tensor(new[] { 2, 3 });
8578
Tensor o = t.MaybeMove();
8679
ASSERT_TRUE(o.Handle.IsInvalid); // It is unsafe to move memory TF might not own.
8780
t.Dispose();
@@ -94,17 +87,15 @@ public void MaybeMove()
9487
[TestMethod]
9588
public void Tensor()
9689
{
97-
var nd = np.array(1f, 2f, 3f, 4f, 5f, 6f).reshape((2, 3));
98-
99-
var tensor = new Tensor(nd);
100-
var array = tensor.ToArray<float>();
90+
var array = new[] { 1f, 2f, 3f, 4f, 5f, 6f };
91+
var tensor = new Tensor(array, (2, 3));
10192

10293
EXPECT_EQ(tensor.dtype, TF_DataType.TF_FLOAT);
103-
EXPECT_EQ(tensor.rank, nd.ndim);
104-
EXPECT_EQ(tensor.shape[0], nd.dims[0]);
105-
EXPECT_EQ(tensor.shape[1], nd.dims[1]);
106-
EXPECT_EQ(tensor.bytesize, nd.size * sizeof(float));
107-
Assert.IsTrue(Enumerable.SequenceEqual(nd.ToArray<float>(), new float[] { 1, 2, 3, 4, 5, 6 }));
94+
EXPECT_EQ(tensor.rank, 2);
95+
EXPECT_EQ(tensor.shape[0], 2L);
96+
EXPECT_EQ(tensor.shape[1], 3L);
97+
EXPECT_EQ(tensor.bytesize, 6ul * sizeof(float));
98+
Assert.IsTrue(Enumerable.SequenceEqual(tensor.ToArray<float>(), new float[] { 1, 2, 3, 4, 5, 6 }));
10899
}
109100

110101
/// <summary>
@@ -130,7 +121,7 @@ public void StringTensor()
130121
Assert.AreEqual(TF_TString_Type.TF_TSTR_SMALL, c_api.TF_StringGetType(tensor));
131122
Assert.AreEqual(0, c_api.TF_NumDims(tensor));
132123

133-
TF_DeleteTensor(tensor);
124+
tensor.Dispose();
134125
c_api.TF_StringDealloc(tstr);
135126
}
136127

test/TensorFlowNET.UnitTest/PythonTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void assertAllClose(NDArray array1, NDArray array2, double eps = 1e-5)
141141
public void assertAllClose(double value, NDArray array2, double eps = 1e-5)
142142
{
143143
var array1 = np.ones_like(array2) * value;
144-
Assert.IsTrue(np.allclose(new NDArray(array1), array2, rtol: eps));
144+
Assert.IsTrue(np.allclose(array1, array2, rtol: eps));
145145
}
146146

147147
public void assertProtoEquals(object toProto, object o)

0 commit comments

Comments
 (0)
0