| 1143 | } |
| 1144 | |
| 1145 | Status MakeSparseTensorIndexCSF(FBB& fbb, const SparseCSFIndex& sparse_index, |
| 1146 | const std::vector<BufferMetadata>& buffers, |
| 1147 | flatbuf::SparseTensorIndex* fb_sparse_index_type, |
| 1148 | Offset* fb_sparse_index, size_t* num_buffers) { |
| 1149 | *fb_sparse_index_type = |
| 1150 | flatbuf::SparseTensorIndex::SparseTensorIndex_SparseTensorIndexCSF; |
| 1151 | const int ndim = static_cast<int>(sparse_index.axis_order().size()); |
| 1152 | |
| 1153 | // We assume that the value type of indptr tensor is an integer. |
| 1154 | const auto& indptr_value_type = |
| 1155 | checked_cast<const IntegerType&>(*sparse_index.indptr()[0]->type()); |
| 1156 | auto indptr_type_offset = flatbuf::CreateInt(fbb, indptr_value_type.bit_width(), |
| 1157 | indptr_value_type.is_signed()); |
| 1158 | |
| 1159 | // We assume that the value type of indices tensor is an integer. |
| 1160 | const auto& indices_value_type = |
| 1161 | checked_cast<const IntegerType&>(*sparse_index.indices()[0]->type()); |
| 1162 | auto indices_type_offset = flatbuf::CreateInt(fbb, indices_value_type.bit_width(), |
| 1163 | indices_value_type.is_signed()); |
| 1164 | |
| 1165 | const int64_t indptr_elem_size = indptr_value_type.byte_width(); |
| 1166 | const int64_t indices_elem_size = indices_value_type.byte_width(); |
| 1167 | |
| 1168 | int64_t offset = 0; |
| 1169 | std::vector<flatbuf::Buffer> indptr, indices; |
| 1170 | |
| 1171 | for (const std::shared_ptr<arrow::Tensor>& tensor : sparse_index.indptr()) { |
| 1172 | const int64_t size = tensor->data()->size() / indptr_elem_size; |
| 1173 | const int64_t padded_size = PaddedLength(tensor->data()->size(), kArrowIpcAlignment); |
| 1174 | |
| 1175 | indptr.push_back({offset, size}); |
| 1176 | offset += padded_size; |
| 1177 | } |
| 1178 | for (const std::shared_ptr<arrow::Tensor>& tensor : sparse_index.indices()) { |
| 1179 | const int64_t size = tensor->data()->size() / indices_elem_size; |
| 1180 | const int64_t padded_size = PaddedLength(tensor->data()->size(), kArrowIpcAlignment); |
| 1181 | |
| 1182 | indices.push_back({offset, size}); |
| 1183 | offset += padded_size; |
| 1184 | } |
| 1185 | |
| 1186 | auto fb_indices = fbb.CreateVectorOfStructs(indices); |
| 1187 | auto fb_indptr = fbb.CreateVectorOfStructs(indptr); |
| 1188 | |
| 1189 | std::vector<int> axis_order; |
| 1190 | for (int i = 0; i < ndim; ++i) { |
| 1191 | axis_order.emplace_back(static_cast<int>(sparse_index.axis_order()[i])); |
| 1192 | } |
| 1193 | auto fb_axis_order = |
| 1194 | fbb.CreateVector(arrow::util::MakeNonNull(axis_order.data()), axis_order.size()); |
| 1195 | |
| 1196 | *fb_sparse_index = |
| 1197 | flatbuf::CreateSparseTensorIndexCSF(fbb, indptr_type_offset, fb_indptr, |
| 1198 | indices_type_offset, fb_indices, fb_axis_order) |
| 1199 | .Union(); |
| 1200 | *num_buffers = 2 * ndim - 1; |
| 1201 | return Status::OK(); |
| 1202 | } |
no test coverage detected