MCPcopy Create free account
hub / github.com/apache/arrow / WriteTensor

Function WriteTensor

cpp/src/arrow/ipc/writer.cc:939–969  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

937} // namespace
938
939Status WriteTensor(const Tensor& tensor, io::OutputStream* dst, int32_t* metadata_length,
940 int64_t* body_length) {
941 const int elem_size = tensor.type()->byte_width();
942
943 *body_length = tensor.size() * elem_size;
944
945 // Tensor metadata accounts for padding
946 if (tensor.is_contiguous()) {
947 RETURN_NOT_OK(WriteTensorHeader(tensor, dst, metadata_length));
948 auto data = tensor.data();
949 if (data && data->data()) {
950 RETURN_NOT_OK(dst->Write(data->data(), *body_length));
951 } else {
952 *body_length = 0;
953 }
954 } else {
955 // The tensor written is made contiguous
956 Tensor dummy(tensor.type(), nullptr, tensor.shape());
957 RETURN_NOT_OK(WriteTensorHeader(dummy, dst, metadata_length));
958
959 // TODO: Do we care enough about this temporary allocation to pass in a
960 // MemoryPool to this function?
961 ARROW_ASSIGN_OR_RAISE(auto scratch_space,
962 AllocateBuffer(tensor.shape()[tensor.ndim() - 1] * elem_size));
963
964 RETURN_NOT_OK(WriteStridedTensorData(0, 0, elem_size, tensor,
965 scratch_space->mutable_data(), dst));
966 }
967
968 return Status::OK();
969}
970
971Result<std::unique_ptr<Message>> GetTensorMessage(const Tensor& tensor,
972 MemoryPool* pool) {

Callers 4

CheckTensorRoundTripMethod · 0.85
SerializeTensorFunction · 0.85
GetTensorSizeFunction · 0.85

Calls 12

WriteTensorHeaderFunction · 0.85
WriteStridedTensorDataFunction · 0.85
is_contiguousMethod · 0.80
ndimMethod · 0.80
OKFunction · 0.50
byte_widthMethod · 0.45
typeMethod · 0.45
sizeMethod · 0.45
dataMethod · 0.45
WriteMethod · 0.45
shapeMethod · 0.45
mutable_dataMethod · 0.45

Tested by 1

CheckTensorRoundTripMethod · 0.68