8000 add SafeStringTensorHandle · SciSharp/TensorFlow.NET@3052e1f · GitHub
[go: up one dir, main page]

Skip to content

Commit 3052e1f

Browse files
committed
add SafeStringTensorHandle
1 parent 795c286 commit 3052e1f

File tree

6 files changed

+54
-35
lines changed

6 files changed

+54
-35
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ NDArray GetData(IEnumerable<Slice> slices)
6464
if (tensor.Handle == null)
6565
{
6666
if (tf.executing_eagerly())
67-
return new NDArray(tensor);
68-
else
6967
tensor = tf.defaultSession.eval(tensor);
68+
else
69+
return new NDArray(tensor);
7070
}
7171

7272
return new NDArray(tensor);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow.Util;
5+
6+
namespace Tensorflow
7+
{
8+
public sealed class SafeStringTensorHandle : SafeTensorHandle
9+
{
10+
Shape _shape;
11+
SafeTensorHandle _handle;
12+
const int TF_TSRING_SIZE = 24;
13+
14+
protected SafeStringTensorHandle()
15+
{
16+
}
17+
18+
public SafeStringTensorHandle(SafeTensorHandle handle, Shape shape)
19+
: base(handle.DangerousGetHandle())
20+
{
21+
_handle = handle;
22+
_shape = shape;
23+
}
24+
25+
protected override bool ReleaseHandle()
26+
{
27+
#if TRACK_TENSOR_LIFE
28+
print($"Delete StringTensorHandle 0x{handle.ToString("x16")}");
29+
#endif
30+
31+
long size = 1;
32+
foreach (var s in _shape.dims)
33+
size *= s;
34+
var tstr = c_api.TF_TensorData(_handle);
35+
36+
for (int i = 0; i < size; i++)
37+
{
38+
c_api.TF_StringDealloc(tstr);
39+
tstr += TF_TSRING_SIZE;
40+
}
41+
42+
SetHandle(IntPtr.Zero);
43+
44+
return true;
45+
}
46+
}
47+
}

src/TensorFlowNET.Core/Tensors/SafeTensorHandle.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ limitations under the License.
2020

2121
namespace Tensorflow
2222
{
23-
public sealed class SafeTensorHandle : SafeTensorflowHandle
23+
public class SafeTensorHandle : SafeTensorflowHandle
2424
{
25-
private SafeTensorHandle()
25+
protected SafeTensorHandle()
2626
{
2727
}
2828

src/TensorFlowNET.Core/Tensors/TStringHandle.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/TensorFlowNET.Core/Tensors/Tensor.String.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public partial class Tensor
1010
{
1111
const int TF_TSRING_SIZE = 24;
1212

13-
public SafeTensorHandle StringTensor(string[] strings, Shape shape)
13+
public SafeStringTensorHandle StringTensor(string[] strings, Shape shape)
1414
{
1515
// convert string array to byte[][]
1616
var buffer = new byte[strings.Length][];
@@ -20,7 +20,7 @@ public SafeTensorHandle StringTensor(string[] strings, Shape shape)
2020
return StringTensor(buffer, shape);
2121
}
2222

23-
public SafeTensorHandle StringTensor(byte[][] buffer, Shape shape)
23+
public SafeStringTensorHandle StringTensor(byte[][] buffer, Shape shape)
2424
{
2525
var handle = c_api.TF_AllocateTensor(TF_DataType.TF_STRING,
2626
shape.ndim == 0 ? null : shape.dims,
@@ -39,7 +39,7 @@ public SafeTensorHandle StringTensor(byte[][] buffer, Shape shape)
3939
tstr += TF_TSRING_SIZE;
4040
}
4141

42-
return handle;
42+
return new SafeStringTensorHandle(handle, shape);
4343
}
4444

4545
public string[] StringData()

src/TensorFlowNET.Core/Tensors/Tensor.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -255,19 +255,7 @@ public override string ToString()
255255

256256
protected override void DisposeUnmanagedResources(IntPtr handle)
257257
{
258-
if (dtype == TF_DataType.TF_STRING)
259-
{
260-
long size = 1;
261-
foreach (var s in shape.dims)
262-
size *= s;
263-
var tstr = TensorDataPointer;
264258

265-
for (int i = 0; i < size; i++)
266-
{
267-
c_api.TF_StringDealloc(tstr);
268-
tstr += TF_TSRING_SIZE;
269-
}
270-
}
271259
}
272260

273261
public bool IsDisposed => _disposed;

0 commit comments

Comments
 (0)
0