@@ -47,72 +47,23 @@ extension StringTensor {
4747 // utf8CString is null-terminated. TF APIs want the strings without null-terminators.
4848 let cStrings = scalars. map { $0. utf8CString. dropLast ( ) }
4949
50- // Note: `TENSORFLOW_MASTER` changes below are necessary for the new TensorFlow ABI-stable
51- // unified string tensor design.
52- #if TENSORFLOW_MASTER
53- let byteCount = scalars. count * MemoryLayout< TF_TString> . stride
54-
55- let handle = TensorHandle < String > (
56- shape: shape. dimensions,
57- byteCount: byteCount,
58- bufferInitializer: { tensorBuffer in
59- var dataAddr =
60- tensorBuffer. bindMemory ( to: TF_TString . self, capacity: scalars. count)
61- for cString in cStrings {
62- TF_TString_Init ( dataAddr)
63- cString. withUnsafeBufferPointer { buffer in
64- TF_TString_Copy ( dataAddr, buffer. baseAddress, buffer. count)
65- }
66- dataAddr = dataAddr. advanced ( by: 1 )
67- }
68- } )
69- self . init ( handle: handle)
70- #else
71- let tfEncodedSizes = cStrings. map { TF_StringEncodedSize ( $0. count) }
72-
73- // Format information copied from tensorflow/c/c_api.h:
74- // The format for TF_STRING tensors is:
75- // start_offset: array[uint64]
76- // data: byte[...]
77- //
78- // The string length (as a varint), followed by the contents of the string is encoded at
79- // data[start_offset[i]]].
80- // The size of the "start_offset" region.
81- let startOffsetsByteCount = scalars. count * MemoryLayout< UInt64> . stride
82-
83- // The size of the "data" region.
84- let dataByteCount = tfEncodedSizes. reduce ( 0 , + ) * MemoryLayout< UInt8> . stride
85-
86- let handle = TensorHandle < String > (
87- shape: shape. dimensions,
88- byteCount: startOffsetsByteCount + dataByteCount,
89- bufferInitializer: { tensorBuffer in
90- // Initialize the "start_offset" region.
91- var startOffset : UInt64 = 0
92- var startOffsetAddr =
93- tensorBuffer. bindMemory ( to: UInt64 . self, capacity: scalars. count)
94- for tfEncodedSize in tfEncodedSizes {
95- startOffsetAddr. initialize ( to: startOffset)
96- startOffsetAddr = startOffsetAddr. advanced ( by: 1 )
97- startOffset = startOffset + UInt64( tfEncodedSize)
98- }
99-
100- // Initialize the "data" region.
101- var dataAddr = tensorBuffer. advanced ( by: startOffsetsByteCount)
102- . bindMemory ( to: Int8 . self, capacity: dataByteCount)
103- let status = TF_NewStatus ( )
104- for (cString, tfEncodedSize) in zip ( cStrings, tfEncodedSizes) {
105- _ = cString. withUnsafeBufferPointer { buffer in
106- TF_StringEncode (
107- buffer. baseAddress, buffer. count, dataAddr, tfEncodedSize, status)
108- }
109- checkOk ( status)
110- dataAddr = dataAddr. advanced ( by: tfEncodedSize)
50+ let byteCount = scalars. count * MemoryLayout< TF_TString> . stride
51+
52+ let handle = TensorHandle < String > (
53+ shape: shape. dimensions,
54+ byteCount: byteCount,
55+ bufferInitializer: { tensorBuffer in
56+ var dataAddr =
57+ tensorBuffer. bindMemory ( to: TF_TString . self, capacity: scalars. count)
58+ for cString in cStrings {
59+ TF_TString_Init ( dataAddr)
60+ cString. withUnsafeBufferPointer { buffer in
61+ TF_TString_Copy ( dataAddr, buffer. baseAddress, buffer. count)
11162 }
112- TF_DeleteStatus ( status )
113- } )
114- self . init ( handle : handle )
115- #endif
63+ dataAddr = dataAddr . advanced ( by : 1 )
64+ }
65+ } )
66+ self . init ( handle : handle )
11667 }
11768
11869 /// Creates a 0-D `StringTensor` from a scalar value.
0 commit comments