Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Commit

Permalink
build: bump to TensorFlow 2.4.0
Browse files Browse the repository at this point in the history
Update to build X10 against TensorFlow 2.4.0.
  • Loading branch information
compnerd committed Dec 22, 2020
1 parent 68266f9 commit 1c20c3c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 66 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 16 additions & 65 deletions Sources/TensorFlow/Core/StringTensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<TF_TString>.stride

let handle = TensorHandle<String>(
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<UInt64>.stride

// The size of the "data" region.
let dataByteCount = tfEncodedSizes.reduce(0, +) * MemoryLayout<UInt8>.stride

let handle = TensorHandle<String>(
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<TF_TString>.stride

let handle = TensorHandle<String>(
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.
Expand Down

0 comments on commit 1c20c3c

Please sign in to comment.