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
}

0 commit comments

Comments
 (0)