diff --git a/CMakeLists.txt b/CMakeLists.txt index 7060d7424..f9380e331 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -167,7 +167,7 @@ if(NOT X10_FOUND AND NOT USE_BUNDLED_X10) GIT_REPOSITORY git://github.com/tensorflow/tensorflow GIT_TAG - r2.3 + r2.4 UPDATE_DISCONNECTED TRUE CONFIGURE_COMMAND diff --git a/Sources/TensorFlow/Core/StringTensor.swift b/Sources/TensorFlow/Core/StringTensor.swift index 6867fd1ee..b87a494fe 100644 --- a/Sources/TensorFlow/Core/StringTensor.swift +++ b/Sources/TensorFlow/Core/StringTensor.swift @@ -47,72 +47,23 @@ extension StringTensor { // utf8CString is null-terminated. TF APIs want the strings without null-terminators. let cStrings = scalars.map { $0.utf8CString.dropLast() } - // Note: `TENSORFLOW_MASTER` changes below are necessary for the new TensorFlow ABI-stable - // unified string tensor design. - #if TENSORFLOW_MASTER - let byteCount = scalars.count * MemoryLayout.stride - - let handle = TensorHandle( - shape: shape.dimensions, - byteCount: byteCount, - bufferInitializer: { tensorBuffer in - var dataAddr = - tensorBuffer.bindMemory(to: TF_TString.self, capacity: scalars.count) - for cString in cStrings { - TF_TString_Init(dataAddr) - cString.withUnsafeBufferPointer { buffer in - TF_TString_Copy(dataAddr, buffer.baseAddress, buffer.count) - } - dataAddr = dataAddr.advanced(by: 1) - } - }) - self.init(handle: handle) - #else - let tfEncodedSizes = cStrings.map { TF_StringEncodedSize($0.count) } - - // Format information copied from tensorflow/c/c_api.h: - // The format for TF_STRING tensors is: - // start_offset: array[uint64] - // data: byte[...] - // - // The string length (as a varint), followed by the contents of the string is encoded at - // data[start_offset[i]]]. - // The size of the "start_offset" region. - let startOffsetsByteCount = scalars.count * MemoryLayout.stride - - // The size of the "data" region. - let dataByteCount = tfEncodedSizes.reduce(0, +) * MemoryLayout.stride - - let handle = TensorHandle( - shape: shape.dimensions, - byteCount: startOffsetsByteCount + dataByteCount, - bufferInitializer: { tensorBuffer in - // Initialize the "start_offset" region. - var startOffset: UInt64 = 0 - var startOffsetAddr = - tensorBuffer.bindMemory(to: UInt64.self, capacity: scalars.count) - for tfEncodedSize in tfEncodedSizes { - startOffsetAddr.initialize(to: startOffset) - startOffsetAddr = startOffsetAddr.advanced(by: 1) - startOffset = startOffset + UInt64(tfEncodedSize) - } - - // Initialize the "data" region. - var dataAddr = tensorBuffer.advanced(by: startOffsetsByteCount) - .bindMemory(to: Int8.self, capacity: dataByteCount) - let status = TF_NewStatus() - for (cString, tfEncodedSize) in zip(cStrings, tfEncodedSizes) { - _ = cString.withUnsafeBufferPointer { buffer in - TF_StringEncode( - buffer.baseAddress, buffer.count, dataAddr, tfEncodedSize, status) - } - checkOk(status) - dataAddr = dataAddr.advanced(by: tfEncodedSize) + let byteCount = scalars.count * MemoryLayout.stride + + let handle = TensorHandle( + shape: shape.dimensions, + byteCount: byteCount, + bufferInitializer: { tensorBuffer in + var dataAddr = + tensorBuffer.bindMemory(to: TF_TString.self, capacity: scalars.count) + for cString in cStrings { + TF_TString_Init(dataAddr) + cString.withUnsafeBufferPointer { buffer in + TF_TString_Copy(dataAddr, buffer.baseAddress, buffer.count) } - TF_DeleteStatus(status) - }) - self.init(handle: handle) - #endif + dataAddr = dataAddr.advanced(by: 1) + } + }) + self.init(handle: handle) } /// Creates a 0-D `StringTensor` from a scalar value.