MCPcopy Create free account
hub / github.com/antgroup/vsag / BuildSparseVectorsFromCSR

Function BuildSparseVectorsFromCSR

python_bindings/binding.cpp:68–132  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

66};
67
68SparseVectors
69BuildSparseVectorsFromCSR(py::array_t<uint32_t> index_pointers,
70 py::array_t<uint32_t> indices,
71 py::array_t<float> values) {
72 auto buf_ptr = index_pointers.request();
73 auto buf_idx = indices.request();
74 auto buf_val = values.request();
75
76 if (buf_ptr.ndim != 1 || buf_idx.ndim != 1 || buf_val.ndim != 1) {
77 throw std::invalid_argument("all inputs must be 1-dimensional");
78 }
79
80 if (buf_ptr.shape[0] < 2) {
81 throw std::invalid_argument("index_pointers length must be at least 2");
82 }
83 uint32_t num_elements = buf_ptr.shape[0] - 1;
84
85 const uint32_t* ptr_data = index_pointers.data();
86 const uint32_t* idx_data = indices.data();
87 const float* val_data = values.data();
88
89 uint32_t num_non_zeros = ptr_data[num_elements];
90
91 if (static_cast<size_t>(num_non_zeros) != buf_idx.shape[0]) {
92 throw std::invalid_argument(
93 fmt::format("Size of 'indices'({}) must equal index_pointers[last]",
94 buf_idx.shape[0],
95 num_non_zeros));
96 }
97 if (static_cast<size_t>(num_non_zeros) != buf_val.shape[0]) {
98 throw std::invalid_argument(
99 fmt::format("Size of 'values'({}) must equal index_pointers[last]({})",
100 buf_val.shape[0],
101 num_non_zeros));
102 }
103
104 if (ptr_data[0] != 0) {
105 throw std::invalid_argument("index_pointers[0] must be 0");
106 }
107 for (uint32_t i = 1; i <= num_elements; ++i) {
108 if (ptr_data[i] < ptr_data[i - 1]) {
109 throw std::invalid_argument(
110 fmt::format("index_pointers[{}]({}) > index_pointers[{}]({})",
111 i - 1,
112 ptr_data[i - 1],
113 i,
114 ptr_data[i]));
115 }
116 }
117
118 SparseVectors svs(num_elements);
119 svs.num_non_zeros = num_non_zeros;
120
121 for (uint32_t i = 0; i < num_elements; ++i) {
122 uint32_t start = ptr_data[i];
123 uint32_t end = ptr_data[i + 1];
124 uint32_t len = end - start;
125

Callers 2

SparseBuildMethod · 0.85
SparseKnnSearchMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected