@@ -457,53 +457,15 @@ public unsafe Tensor(Complex value, TF_DataType? dType = null)
457457 /// </summary>
458458 public unsafe Tensor ( string str )
459459 {
460- var buffer = Encoding. UTF8. GetBytes( str ) ;
461- var size = c_api. TF_StringEncodedSize( ( ulong ) buffer . Length ) ;
462- var handle = TF_AllocateTensor( TF_DataType . TF_STRING , null , 0 , size + sizeof ( ulong ) ) ;
463- AllocationType = AllocationType . Tensorflow ;
464-
465- IntPtr tensor = c_api . TF_TensorData ( handle ) ;
466- Marshal . WriteInt64 ( tensor , 0 ) ;
467- fixed ( byte * src = buffer)
468- c_api. TF_StringEncode( src, ( ulong ) buffer . Length , ( byte * ) ( tensor + sizeof ( long ) ) , size , tf . Status . Handle ) ;
469- _handle = handle ;
470- tf . Status . Check ( true ) ;
460+ _handle = StringTensor( new string [ ] { str } , TensorShape . Scalar ) ;
461+ #if TRACK_TENSOR_LIFE
462+ print( $"New Tensor 0 x{ _handle. ToString( "x16") } { AllocationType} String Data: 0 x{ TensorDataPointer. ToString( "x16") } ") ;
463+ #endif
471464 }
472465
473466 public unsafe Tensor ( string [ ] strings )
474467 {
475- // convert string array to byte[][]
476- var buffer = new byte [ strings . Length ] [ ] ;
477- for ( var i = 0 ; i < strings. Length; i++ )
478- buffer[ i] = Encoding. UTF8. GetBytes( strings [ i ] ) ;
479- long [ ] shape = new long [ ] { strings . Length } ;
480-
481- ulong size = 0 ;
482- foreach ( var b in buffer)
483- size += TF_StringEncodedSize( ( ulong ) b . Length ) ;
484-
485- ulong src_size = size + ( ulong ) buffer . Length * sizeof ( ulong ) ;
486- _handle = TF_AllocateTensor( TF_DataType . TF_STRING , shape , shape . Length , src_size ) ;
487- AllocationType = AllocationType . Tensorflow ;
488-
489- // Clear offset table
490- IntPtr input = TensorDataPointer;
491- IntPtr data_start = input + buffer. Length * sizeof ( ulong ) ;
492- IntPtr limit = input + ( int ) src_size;
493- ulong offset = 0 ;
494- for ( int i = 0 ; i < buffer. Length; i++ )
495- {
496- Marshal. WriteInt64( input , i * sizeof ( ulong ) , ( long ) offset) ;
497- fixed ( byte * src = & buffer[ i] [ 0 ] )
498- {
499- var written = TF_StringEncode ( src , ( ulong ) buffer [ i ] . Length , ( byte * ) data_start , ( ulong ) ( limit . ToInt64 ( ) - data_start . ToInt64 ( ) ) , tf . Status . Handle ) ;
500- tf. Status . Check ( true ) ;
501- //input += 8;
502- data_start += ( int ) written ;
503- offset += written ;
504- }
505- }
506-
468+ _handle = StringTensor( strings , new TensorShape ( strings . Length ) ) ;
507469#if TRACK_TENSOR_LIFE
508470 print( $"New Tensor 0 x{ _handle . ToString ( "x16 ") } { AllocationType} String Data: 0 x{ TensorDataPointer. ToString( "x16") } ") ;
509471#endif
@@ -515,12 +477,12 @@ public unsafe Tensor(NDArray nd, TF_DataType? tensorDType = null)
515477 tensorDType = nd . dtype . as_dtype ( ) ;
516478
517479 // todo: handle nd of type "String" here too
518- if ( tensorDType == TF_DataType . TF_STRING && nd . typecode == NPTypeCode . Byte )
480+ /* if (tensorDType == TF_DataType.TF_STRING && nd.typecode == NPTypeCode.Byte)
519481 {
520482 if (nd.Unsafe.Storage.Shape.IsContiguous)
521483 {
522484 var bytesLength = (ulong)nd.size;
523- var size = c_api . TF_StringEncodedSize ( bytesLength ) ;
485+ var size = bytesLength + 1 ;
524486 var handle = TF_AllocateTensor(TF_DataType.TF_STRING, null, 0, size + 8);
525487 AllocationType = AllocationType.Tensorflow;
526488
@@ -534,7 +496,7 @@ public unsafe Tensor(NDArray nd, TF_DataType? tensorDType = null)
534496 else
535497 {
536498 var buffer = nd.ToArray<byte>();
537- var size = c_api . TF_StringEncodedSize ( ( ulong ) buffer . Length ) ;
499+ var size = ( ulong)buffer.Length + 1 ;
538500 var handle = TF_AllocateTensor(TF_DataType.TF_STRING, null, 0, size + 8);
539501 AllocationType = AllocationType.Tensorflow;
540502
@@ -549,9 +511,12 @@ public unsafe Tensor(NDArray nd, TF_DataType? tensorDType = null)
549511 }
550512
551513 return;
552- }
514+ }*/
553515
554516 CreateTensorFromNDArray ( nd, tensorDType ) ;
517+ #if TRACK_TENSOR_LIFE
518+ print( $"New Tensor 0 x { _handle . ToString ( "x16 ") } { AllocationType} Data: 0 x{ TensorDataPointer. ToString( "x16") } ") ;
519+ #endif
555520 }
556521
557522 private unsafe void CreateTensorFromNDArray ( NDArray nd , TF_DataType ? given_dtype )
@@ -576,10 +541,6 @@ private unsafe void CreateTensorFromNDArray(NDArray nd, TF_DataType? given_dtype
576541 }
577542 else
578543 AllocationType = AllocationType. Tensorflow;
579-
580- #if TRACK_TENSOR_LIFE
581- print ( $ "New Tensor 0x{ _handle . ToString ( "x16" ) } { AllocationType } Data: 0x{ TensorDataPointer . ToString ( "x16" ) } ") ;
582- #endif
583544 }
584545
585546 public Tensor( Operation op, int value_index , TF_DataType dtype )
@@ -608,26 +569,10 @@ public Tensor(Operation op, int value_index, TF_DataType dtype)
608569 protected IntPtr CreateTensorFromArray( TF_DataType dt , long [ ] shape , Array data , int element_size )
609570 {
610571 if ( dt == TF_DataType . TF_STRING && data is byte [ ] buffer )
611- return CreateStringTensorFromBytes ( buffer , shape ) ;
572+ return StringTensor ( new byte [ ] [ ] { buffer } , TensorShape . Scalar ) ;
612573 return CreateTensorFromArray ( dt , shape , data , 0 , data . Length , element_size ) ;
613574 }
614575
615- protected unsafe IntPtr CreateStringTensorFromBytes( byte [ ] buffer , long [ ] shape )
616- {
617- var size = c_api. TF_StringEncodedSize( ( ulong ) buffer . Length ) ;
618- var handle = TF_AllocateTensor( TF_DataType . TF_STRING , shape , 0 , size + sizeof ( long ) ) ;
619- AllocationType = AllocationType . Tensorflow ;
620-
621- IntPtr tensor = c_api . TF_TensorData ( handle ) ;
622- Marshal . WriteInt64 ( tensor , 0 ) ;
623-
624- fixed ( byte * src = buffer)
625- c_api. TF_StringEncode( src, ( ulong ) buffer . Length , ( byte * ) ( tensor + sizeof ( long ) ) , size , tf . Status . Handle ) ;
626-
627- tf . Status . Check ( true ) ;
628- return handle;
629- }
630-
631576 /// <summary>
632577 /// Creates a new tensor from a subsection of the given array without copying memory. The array is pinned down and the pointer passed on.
633578 /// </summary>
0 commit comments