-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathsparse_vector.hpp
More file actions
243 lines (201 loc) · 8.45 KB
/
sparse_vector.hpp
File metadata and controls
243 lines (201 loc) · 8.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#pragma once
#include <vector>
#include <cstring>
#include <stdexcept>
#include "mdbx/mdbx.h"
#include "../utils/log.hpp"
namespace ndd {
// Sparse vector payload stored in the document table. The convention in this module is that
// indices are sorted term ids and values[i] belongs to indices[i].
struct SparseVector {
std::vector<uint32_t> indices; // term IDs (sorted)
std::vector<float> values; // corresponding values
// Default constructor
SparseVector() = default;
// Constructor from packed data
SparseVector(const uint8_t* data, size_t data_size) {
if(data_size < sizeof(uint16_t)) {
throw std::runtime_error("Invalid packed data: insufficient size for nr_nonzero field");
}
const uint8_t* ptr = data;
// Packed format:
// [nr_nonzero:u16][term_ids:n * u32][values:n * f16]
uint16_t nr_nonzero;
std::memcpy(&nr_nonzero, ptr, sizeof(uint16_t));
ptr += sizeof(uint16_t);
// Validate remaining data size: nr_nonzero * (4 + 2) bytes
size_t expected_size = sizeof(uint16_t) + (nr_nonzero * (sizeof(uint32_t) + sizeof(uint16_t)));
if(data_size != expected_size) {
throw std::runtime_error("Invalid packed data: size mismatch");
}
if(nr_nonzero > 0) {
indices.resize(nr_nonzero);
values.resize(nr_nonzero);
// Read term IDs (uint32_t each)
std::memcpy(indices.data(), ptr, nr_nonzero * sizeof(uint32_t));
ptr += nr_nonzero * sizeof(uint32_t);
// Read quantized values (uint16_t each) and convert to float
std::vector<uint16_t> fp16_values(nr_nonzero);
std::memcpy(fp16_values.data(), ptr, nr_nonzero * sizeof(uint16_t));
// Convert FP16 to float
for(size_t i = 0; i < nr_nonzero; ++i) {
values[i] = fp16_to_float(fp16_values[i]);
}
} else {
LOG_WARN(2261, "Deserialized sparse vector with nr_nonzero=0");
}
}
// Convenience constructors
explicit SparseVector(const MDBX_val& mdb_val) :
SparseVector(static_cast<const uint8_t*>(mdb_val.iov_base), mdb_val.iov_len) {}
explicit SparseVector(const std::vector<uint8_t>& packed_data) :
SparseVector(packed_data.data(), packed_data.size()) {}
// Pack sparse vector into binary format: nr_nonzero(u16) + [term_ids(u32)] + [values(f16)]
std::vector<uint8_t> pack() const {
if(indices.size() != values.size()) {
throw std::runtime_error("SparseVector indices and values size mismatch");
}
uint16_t nr_nonzero = static_cast<uint16_t>(indices.size());
// Calculate total size: nr_nonzero(2) + term_ids(4*nr_nonzero) + values(2*nr_nonzero)
size_t total_size =
sizeof(uint16_t) + (nr_nonzero * sizeof(uint32_t)) + (nr_nonzero * sizeof(uint16_t));
// Serialize contiguously so the vector can be written to MDBX as one value blob.
std::vector<uint8_t> packed(total_size);
uint8_t* ptr = packed.data();
// Write nr_nonzero
std::memcpy(ptr, &nr_nonzero, sizeof(uint16_t));
ptr += sizeof(uint16_t);
if(nr_nonzero > 0) {
// Write term IDs
std::memcpy(ptr, indices.data(), nr_nonzero * sizeof(uint32_t));
ptr += nr_nonzero * sizeof(uint32_t);
// Convert float values to FP16 and write
std::vector<uint16_t> fp16_values(nr_nonzero);
for(size_t i = 0; i < nr_nonzero; ++i) {
fp16_values[i] = float_to_fp16(values[i]);
}
std::memcpy(ptr, fp16_values.data(), nr_nonzero * sizeof(uint16_t));
}
return packed;
}
#if 0
// Dot product overloads
float dot(const SparseVector& other) const {
float result = 0.0f;
size_t i = 0, j = 0;
while(i < indices.size() && j < other.indices.size()) {
if(indices[i] == other.indices[j]) {
result += values[i] * other.values[j];
++i;
++j;
} else if(indices[i] < other.indices[j]) {
++i;
} else {
++j;
}
}
return result;
}
// Dot product with packed data (zero-copy)
float dot(const uint8_t* packed_data, size_t data_size) const {
if(data_size < sizeof(uint16_t) || indices.empty()) {
return 0.0f;
}
const uint8_t* ptr = packed_data;
// Read nr_nonzero
uint16_t other_nr_nonzero;
std::memcpy(&other_nr_nonzero, ptr, sizeof(uint16_t));
ptr += sizeof(uint16_t);
if(other_nr_nonzero == 0) {
return 0.0f;
}
// Direct pointer access to packed data
const uint32_t* other_indices = reinterpret_cast<const uint32_t*>(ptr);
const uint16_t* other_fp16_values =
reinterpret_cast<const uint16_t*>(ptr + other_nr_nonzero * sizeof(uint32_t));
// Two-pointer intersection
float result = 0.0f;
size_t this_idx = 0;
for(uint16_t other_idx = 0; other_idx < other_nr_nonzero && this_idx < indices.size();) {
uint32_t this_index = indices[this_idx];
uint32_t other_index = other_indices[other_idx];
if(this_index == other_index) {
float other_value = fp16_to_float(other_fp16_values[other_idx]);
result += values[this_idx] * other_value;
++this_idx;
++other_idx;
} else if(this_index < other_index) {
++this_idx;
} else {
++other_idx;
}
}
return result;
}
// Convenience overloads
float dot(const MDBX_val& mdb_val) const {
return dot(static_cast<const uint8_t*>(mdb_val.iov_base), mdb_val.iov_len);
}
float dot(const std::vector<uint8_t>& packed_data) const {
return dot(packed_data.data(), packed_data.size());
}
#endif //if 0
// Utility methods
bool empty() const { return indices.empty(); }
size_t size() const { return indices.size(); }
void clear() {
indices.clear();
values.clear();
}
private:
// Simple FP16 conversion (you might want to use a proper library)
static uint16_t float_to_fp16(float f) {
// Simplified conversion - use proper library in production
union {
float f;
uint32_t i;
} u = {f};
uint32_t sign = (u.i >> 31) << 15;
uint32_t exp = ((u.i >> 23) & 0xff) - 127 + 15;
uint32_t frac = (u.i >> 13) & 0x3ff;
if(exp <= 0) {
return static_cast<uint16_t>(sign);
}
if(exp >= 31) {
return static_cast<uint16_t>(sign | 0x7c00);
}
return static_cast<uint16_t>(sign | (exp << 10) | frac);
}
static float fp16_to_float(uint16_t h) {
// Simplified conversion - use proper library in production
uint32_t sign = (h >> 15) << 31;
uint32_t exp = (h >> 10) & 0x1f;
uint32_t frac = h & 0x3ff;
if(exp == 0) {
if(frac == 0) {
union {
uint32_t i;
float f;
} u = {sign};
return u.f;
}
// Denormalized
exp = 127 - 15 + 1 - 10;
while((frac & 0x400) == 0) {
frac <<= 1;
exp--;
}
frac &= 0x3ff;
} else if(exp == 31) {
exp = 255;
} else {
exp = exp - 15 + 127;
}
union {
uint32_t i;
float f;
} u = {sign | (exp << 23) | (frac << 13)};
return u.f;
}
};
} // namespace ndd