MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / EncodePyBytesArray

Function EncodePyBytesArray

tensorflow/python/lib/core/ndarray_tensor.cc:243–270  ·  view source on GitHub ↗

Encode the strings in 'array' into a contiguous buffer and return the base of the buffer. The caller takes ownership of the buffer.

Source from the content-addressed store, hash-verified

241// Encode the strings in 'array' into a contiguous buffer and return the base of
242// the buffer. The caller takes ownership of the buffer.
243Status EncodePyBytesArray(PyArrayObject* array, tensorflow::int64 nelems,
244 size_t* size, void** buffer) {
245 // Compute bytes needed for encoding.
246 *size = 0;
247 TF_RETURN_IF_ERROR(
248 PyBytesArrayMap(array, [&size](const char* ptr, Py_ssize_t len) {
249 *size += sizeof(tensorflow::uint64) +
250 tensorflow::core::VarintLength(len) + len;
251 }));
252 // Encode all strings.
253 std::unique_ptr<char[]> base_ptr(new char[*size]);
254 char* base = base_ptr.get();
255 char* data_start = base + sizeof(tensorflow::uint64) * nelems;
256 char* dst = data_start; // Where next string is encoded.
257 tensorflow::uint64* offsets = reinterpret_cast<tensorflow::uint64*>(base);
258
259 TF_RETURN_IF_ERROR(PyBytesArrayMap(
260 array, [&data_start, &dst, &offsets](const char* ptr, Py_ssize_t len) {
261 *offsets = (dst - data_start);
262 offsets++;
263 dst = tensorflow::core::EncodeVarint64(dst, len);
264 memcpy(dst, ptr, len);
265 dst += len;
266 }));
267 CHECK_EQ(dst, base + *size);
268 *buffer = base_ptr.release();
269 return Status::OK();
270}
271
272Status CopyTF_TensorStringsToPyArray(const TF_Tensor* src, uint64 nelems,
273 PyArrayObject* dst) {

Callers 1

PyArrayToTF_TensorFunction · 0.85

Calls 5

PyBytesArrayMapFunction · 0.85
VarintLengthFunction · 0.85
EncodeVarint64Function · 0.85
getMethod · 0.45
releaseMethod · 0.45

Tested by

no test coverage detected