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

Function MakeCSFSparseTensorMessage

cpp/src/arrow/ipc/tensor_test.cc:513–555  ·  view source on GitHub ↗

Build a SparseTensor IPC message carrying a CSF index with caller-controlled buffer counts. This lets us exercise the reader's validation directly, since such inconsistent indices can only be produced by hand-crafted flatbuffers.

Source from the content-addressed store, hash-verified

511// buffer counts. This lets us exercise the reader's validation directly, since
512// such inconsistent indices can only be produced by hand-crafted flatbuffers.
513Result<std::shared_ptr<Message>> MakeCSFSparseTensorMessage(
514 const std::vector<int64_t>& shape, int num_indptr_buffers, int num_indices_buffers,
515 int axis_order_size, int64_t non_zero_length = 0) {
516 flatbuffers::FlatBufferBuilder fbb;
517
518 auto value_type = flatbuf::CreateInt(fbb, 64, /*is_signed=*/true);
519
520 std::vector<flatbuffers::Offset<flatbuf::TensorDim>> dims;
521 for (int64_t dim_size : shape) {
522 dims.push_back(flatbuf::CreateTensorDim(fbb, dim_size, /*name=*/0));
523 }
524 auto fb_shape = fbb.CreateVector(dims);
525
526 auto indptr_type = flatbuf::CreateInt(fbb, 64, /*is_signed=*/false);
527 auto indices_type = flatbuf::CreateInt(fbb, 64, /*is_signed=*/false);
528
529 std::vector<flatbuf::Buffer> indptr(num_indptr_buffers, flatbuf::Buffer(0, 0));
530 std::vector<flatbuf::Buffer> indices(num_indices_buffers, flatbuf::Buffer(0, 0));
531 auto fb_indptr = fbb.CreateVectorOfStructs(indptr);
532 auto fb_indices = fbb.CreateVectorOfStructs(indices);
533
534 std::vector<int32_t> axis_order(axis_order_size, 0);
535 auto fb_axis_order = fbb.CreateVector(axis_order);
536
537 auto csf = flatbuf::CreateSparseTensorIndexCSF(fbb, indptr_type, fb_indptr,
538 indices_type, fb_indices, fb_axis_order);
539
540 flatbuf::Buffer data(0, 0);
541 auto sparse_tensor = flatbuf::CreateSparseTensor(
542 fbb, flatbuf::Type_Int, value_type.Union(), fb_shape, non_zero_length,
543 flatbuf::SparseTensorIndex::SparseTensorIndex_SparseTensorIndexCSF, csf.Union(),
544 &data);
545
546 fbb.Finish(flatbuf::CreateMessage(fbb, internal::kCurrentMetadataVersion,
547 flatbuf::MessageHeader::MessageHeader_SparseTensor,
548 sparse_tensor.Union(),
549 /*bodyLength=*/0));
550
551 ARROW_ASSIGN_OR_RAISE(auto metadata, internal::WriteFlatbufferBuilder(fbb));
552 auto body = Buffer::FromString(std::string(8, '\0'));
553 ARROW_ASSIGN_OR_RAISE(auto message, Message::Open(metadata, body));
554 return std::shared_ptr<Message>(std::move(message));
555}
556
557IpcPayload MakeSparseTensorPayload(const std::shared_ptr<Message>& message,
558 int num_body_buffers) {

Callers 1

TESTFunction · 0.85

Calls 9

CreateIntFunction · 0.85
CreateTensorDimFunction · 0.85
CreateSparseTensorFunction · 0.85
CreateMessageFunction · 0.85
FromStringFunction · 0.85
push_backMethod · 0.80
BufferClass · 0.70
FinishMethod · 0.45

Tested by

no test coverage detected