MCPcopy Create free account
hub / github.com/alibaba/zvec / serialize_sparse_vector

Function serialize_sparse_vector

src/binding/python/model/param/python_param.cc:128–148  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

126
127template <typename ValueType, typename ValueCastFn>
128std::pair<std::string, std::string> serialize_sparse_vector(
129 const py::dict &sparse_dict, ValueCastFn &&value_caster) {
130 const size_t n = sparse_dict.size();
131 if (n == 0) return {{}, {}};
132
133 std::string indices_buf;
134 indices_buf.resize(n * sizeof(uint32_t));
135 auto *indices_ptr = reinterpret_cast<uint32_t *>(indices_buf.data());
136
137 std::string values_buf;
138 values_buf.resize(n * sizeof(ValueType));
139 auto *values_ptr = reinterpret_cast<ValueType *>(values_buf.data());
140
141 size_t i = 0;
142 for (const auto &[py_key, py_val] : sparse_dict) {
143 indices_ptr[i] = checked_cast<uint32_t>(py_key, "Sparse indices", "UINT32");
144 values_ptr[i] = value_caster(py_val, i);
145 ++i;
146 }
147 return {std::move(indices_buf), std::move(values_buf)};
148}
149
150void ZVecPyParams::Initialize(pybind11::module_ &parent) {
151 auto m =

Callers

nothing calls this directly

Calls 3

sizeMethod · 0.45
resizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected