@@ -8,27 +8,7 @@ namespace Tensorflow
88{
99 public partial class Tensor
1010 {
11- const ulong TF_TSRING_SIZE = 24 ;
12-
13- public IntPtr StringTensor25 ( string [ ] strings , TensorShape shape )
14- {
15- var handle = c_api . TF_AllocateTensor ( TF_DataType . TF_STRING ,
16- shape . dims . Select ( x => ( long ) x ) . ToArray ( ) ,
17- shape . ndim ,
18- ( ulong ) shape . size * TF_TSRING_SIZE ) ;
19-
20- var data = c_api . TF_TensorData ( handle ) ;
21- var tstr = c_api . TF_StringInit ( handle ) ;
22- // AllocationHandle = tstr;
23- // AllocationType = AllocationType.Tensorflow;
24- for ( int i = 0 ; i < strings . Length ; i ++ )
25- {
26- c_api . TF_StringCopy ( tstr , strings [ i ] , strings [ i ] . Length ) ;
27- tstr += ( int ) TF_TSRING_SIZE ;
28- }
29- // c_api.TF_StringDealloc(tstr);
30- return handle ;
31- }
11+ const int TF_TSRING_SIZE = 24 ;
3212
3313 public IntPtr StringTensor ( string [ ] strings , TensorShape shape )
3414 {
@@ -40,69 +20,28 @@ public IntPtr StringTensor(string[] strings, TensorShape shape)
4020 return StringTensor ( buffer , shape ) ;
4121 }
4222
43- public unsafe IntPtr StringTensor ( byte [ ] [ ] buffer , TensorShape shape )
23+ public IntPtr StringTensor ( byte [ ] [ ] buffer , TensorShape shape )
4424 {
45- ulong size = 0 ;
46- foreach ( var b in buffer )
47- size += c_api . TF_StringEncodedSize ( ( ulong ) b . Length ) ;
48-
49- var src_size = size + ( ulong ) buffer . Length * sizeof ( ulong ) ;
5025 var handle = c_api . TF_AllocateTensor ( TF_DataType . TF_STRING ,
51- shape . dims . Select ( x => ( long ) x ) . ToArray ( ) ,
26+ shape . ndim == 0 ? null : shape . dims . Select ( x => ( long ) x ) . ToArray ( ) ,
5227 shape . ndim ,
53- src_size ) ;
54- AllocationType = AllocationType . Tensorflow ;
28+ ( ulong ) shape . size * TF_TSRING_SIZE ) ;
5529
56- IntPtr data_start = c_api . TF_TensorData ( handle ) ;
57- IntPtr string_start = data_start + buffer . Length * sizeof ( ulong ) ;
58- IntPtr limit = data_start + ( int ) src_size ;
59- ulong offset = 0 ;
30+ var tstr = c_api . TF_TensorData ( handle ) ;
31+ #if TRACK_TENSOR_LIFE
32+ print ( $ "New TString 0x { handle . ToString ( "x16" ) } { AllocationType } Data: 0x { tstr . ToString ( "x16" ) } " ) ;
33+ #endif
6034 for ( int i = 0 ; i < buffer . Length ; i ++ )
6135 {
62- Marshal . WriteInt64 ( data_start , i * sizeof ( ulong ) , ( long ) offset ) ;
63- if ( buffer [ i ] . Length == 0 )
64- {
65- Marshal . WriteByte ( string_start , 0 ) ;
66- break ;
67- }
68-
69- fixed ( byte * src = & buffer [ i ] [ 0 ] )
70- {
71- /*Marshal.WriteByte(string_start, Convert.ToByte(buffer[i].Length));
72- tf.memcpy((string_start + 1).ToPointer(), src, (ulong)buffer[i].Length);
73- string_start += buffer[i].Length + 1;
74- offset += buffer[i].Length + 1;*/
75-
76- var written = c_api . TF_StringEncode ( src , ( ulong ) buffer [ i ] . Length , ( byte * ) string_start , ( ulong ) ( limit . ToInt64 ( ) - string_start . ToInt64 ( ) ) , tf . Status . Handle ) ;
77- tf . Status . Check ( true ) ;
78- string_start += ( int ) written ;
79- offset += written ;
80- }
36+ c_api . TF_StringInit ( tstr ) ;
37+ c_api . TF_StringCopy ( tstr , buffer [ i ] , buffer [ i ] . Length ) ;
38+ var data = c_api . TF_StringGetDataPointer ( tstr ) ;
39+ tstr += TF_TSRING_SIZE ;
8140 }
8241
8342 return handle ;
8443 }
8544
86- public string [ ] StringData25 ( )
87- {
88- string [ ] strings = new string [ c_api . TF_Dim ( _handle , 0 ) ] ;
89- var tstrings = TensorDataPointer ;
90- for ( int i = 0 ; i < strings . Length ; i ++ )
91- {
92- var tstringData = c_api . TF_StringGetDataPointer ( tstrings ) ;
93- /*var size = c_api.TF_StringGetSize(tstrings);
94- var capacity = c_api.TF_StringGetCapacity(tstrings);
95- var type = c_api.TF_StringGetType(tstrings);*/
96- strings [ i ] = c_api . StringPiece ( tstringData ) ;
97- tstrings += ( int ) TF_TSRING_SIZE ;
98- }
99- return strings ;
100- }
101-
102- /// <summary>
103- /// Extracts string array from current Tensor.
104- /// </summary>
105- /// <exception cref="InvalidOperationException">When <see cref="dtype"/> != TF_DataType.TF_STRING</exception>
10645 public string [ ] StringData ( )
10746 {
10847 var buffer = StringBytes ( ) ;
@@ -114,7 +53,7 @@ public string[] StringData()
11453 return _str ;
11554 }
11655
117- public unsafe byte [ ] [ ] StringBytes ( )
56+ public byte [ ] [ ] StringBytes ( )
11857 {
11958 if ( dtype != TF_DataType . TF_STRING )
12059 throw new InvalidOperationException ( $ "Unable to call StringData when dtype != TF_DataType.TF_STRING (dtype is { dtype } )") ;
@@ -123,24 +62,22 @@ public unsafe byte[][] StringBytes()
12362 // TF_STRING tensors are encoded with a table of 8-byte offsets followed by TF_StringEncode-encoded bytes.
12463 // [offset1, offset2,...,offsetn, s1size, s1bytes, s2size, s2bytes,...,snsize,snbytes]
12564 //
126- long size = 1 ;
65+ int size = 1 ;
12766 foreach ( var s in TensorShape . dims )
12867 size *= s ;
12968
13069 var buffer = new byte [ size ] [ ] ;
131- var data_start = c_api . TF_TensorData ( _handle ) ;
132- data_start += ( int ) ( size * sizeof ( ulong ) ) ;
70+ var tstrings = TensorDataPointer ;
13371 for ( int i = 0 ; i < buffer . Length ; i ++ )
13472 {
135- IntPtr dst = IntPtr . Zero ;
136- ulong dstLen = 0 ;
137- var read = c_api . TF_StringDecode ( ( byte * ) data_start , bytesize , ( byte * * ) & dst , ref dstLen , tf . Status . Handle ) ;
138- tf . Status . Check ( true ) ;
139- buffer [ i ] = new byte [ ( int ) dstLen ] ;
140- Marshal . Copy ( dst , buffer [ i ] , 0 , buffer [ i ] . Length ) ;
141- data_start += ( int ) read ;
73+ var data = c_api . TF_StringGetDataPointer ( tstrings ) ;
74+ var len = c_api . TF_StringGetSize ( tstrings ) ;
75+ buffer [ i ] = new byte [ len ] ;
76+ // var capacity = c_api.TF_StringGetCapacity(tstrings );
77+ // var type = c_api.TF_StringGetType(tstrings) ;
78+ Marshal . Copy ( data , buffer [ i ] , 0 , Convert . ToInt32 ( len ) ) ;
79+ tstrings += TF_TSRING_SIZE ;
14280 }
143-
14481 return buffer ;
14582 }
14683 }
0 commit comments