hawdb-vector-projection 0.23.0

An embedded Rust graph database for local data-plane workloads
Documentation
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::error::{ProjectionError, Result};
use serde::{Deserialize, Serialize};

pub const PROJECTION_PROTOCOL: &str = "hawdb-rabitq-projection";
pub const PROJECTION_FORMAT_VERSION: u32 = 1;
pub const DEFAULT_PROJECTION_BIT_WIDTH: u8 = 4;
pub const PROJECTION_BIT_WIDTH: u8 = DEFAULT_PROJECTION_BIT_WIDTH;
pub const PROJECTION_ALGORITHM: &str = "rabitq";
pub const PROJECTION_TRANSFORM: &str = "signed_block_hadamard_v1";
pub const PROJECTION_QUANTIZER: &str = "rabitq_sign_then_refinement_scalar_4bit_v1";
const ONE_BIT_PROJECTION_QUANTIZER: &str = "rabitq_sign_then_refinement_scalar_1bit_v1";
pub const PROJECTION_CALIBRATION: &str = "none";
pub const DEFAULT_TRANSFORM_SEED: u64 = 0x534b_4549_4e56_5134;
pub const DEFAULT_SEGMENT_ROWS: usize = 1_024;
pub const DEFAULT_BUILD_MEMORY_BYTES: usize = 64 * 1024 * 1024;
pub(crate) const BUILD_FIXED_WORKING_BYTES: usize = 1_024;

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[repr(u8)]
pub enum RaBitQBitWidth {
    One = 1,
    #[default]
    Four = 4,
}

impl RaBitQBitWidth {
    pub const fn bits(self) -> u8 {
        self as u8
    }

    pub const fn quantizer(self) -> &'static str {
        match self {
            Self::One => ONE_BIT_PROJECTION_QUANTIZER,
            Self::Four => PROJECTION_QUANTIZER,
        }
    }

    pub(crate) const fn sign_code_bytes(self, dimension: usize) -> usize {
        let _ = self;
        dimension.div_ceil(u8::BITS as usize)
    }

    pub(crate) const fn refinement_bits(self) -> usize {
        self.bits() as usize - 1
    }

    pub(crate) const fn refinement_code_bytes(self, dimension: usize) -> usize {
        dimension
            .saturating_mul(self.refinement_bits())
            .div_ceil(u8::BITS as usize)
    }

    pub(crate) fn from_bits(bits: u8) -> Result<Self> {
        match bits {
            1 => Ok(Self::One),
            4 => Ok(Self::Four),
            _ => Err(ProjectionError::CorruptArtifact(format!(
                "unsupported RaBitQ bit width {bits}"
            ))),
        }
    }
}

pub(crate) fn encoded_vector_bytes(dimension: usize, bit_width: RaBitQBitWidth) -> usize {
    bit_width
        .sign_code_bytes(dimension)
        .saturating_add(bit_width.refinement_code_bytes(dimension))
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProjectionMetric {
    Cosine,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProjectionIdentity {
    pub generation: u64,
    pub source_epoch: Option<u64>,
    pub embedding_model: Option<String>,
    pub embedding_version: Option<String>,
}

impl ProjectionIdentity {
    pub fn new(generation: u64) -> Self {
        Self {
            generation,
            source_epoch: None,
            embedding_model: None,
            embedding_version: None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectionBuildConfig {
    pub dimension: usize,
    pub bit_width: RaBitQBitWidth,
    pub segment_rows: usize,
    pub max_working_bytes: usize,
    pub transform_seed: u64,
    pub identity: ProjectionIdentity,
}

impl ProjectionBuildConfig {
    pub fn new(dimension: usize, identity: ProjectionIdentity) -> Self {
        Self {
            dimension,
            bit_width: RaBitQBitWidth::default(),
            segment_rows: DEFAULT_SEGMENT_ROWS,
            max_working_bytes: DEFAULT_BUILD_MEMORY_BYTES,
            transform_seed: DEFAULT_TRANSFORM_SEED,
            identity,
        }
    }

    pub fn with_segment_rows(mut self, segment_rows: usize) -> Self {
        self.segment_rows = segment_rows;
        self
    }

    pub fn with_bit_width(mut self, bit_width: RaBitQBitWidth) -> Self {
        self.bit_width = bit_width;
        self
    }

    pub fn with_max_working_bytes(mut self, max_working_bytes: usize) -> Self {
        self.max_working_bytes = max_working_bytes;
        self
    }

    pub fn with_transform_seed(mut self, transform_seed: u64) -> Self {
        self.transform_seed = transform_seed;
        self
    }

    pub fn resource_admission(&self) -> Result<ProjectionBuildAdmission> {
        let validated = self.validated()?;
        Ok(ProjectionBuildAdmission {
            configured_working_bytes: validated.max_working_bytes,
            peak_working_bytes: validated.peak_working_bytes,
            requested_segment_rows: validated.requested_segment_rows,
            admitted_segment_rows: validated.admitted_segment_rows,
        })
    }

    pub(crate) fn validated(&self) -> Result<ValidatedBuildConfig> {
        if self.dimension == 0 {
            return Err(ProjectionError::InvalidConfiguration(
                "dimension must be greater than zero".to_string(),
            ));
        }
        if self.segment_rows == 0 {
            return Err(ProjectionError::InvalidConfiguration(
                "segment_rows must be greater than zero".to_string(),
            ));
        }
        let bytes_per_vector = encoded_vector_bytes(self.dimension, self.bit_width);
        let row_bytes = std::mem::size_of::<u64>()
            .saturating_add(2 * std::mem::size_of::<f32>())
            .saturating_add(bytes_per_vector);
        let scratch_bytes = self
            .dimension
            .saturating_mul(std::mem::size_of::<f32>())
            .saturating_add(bytes_per_vector)
            .saturating_add(BUILD_FIXED_WORKING_BYTES);
        let available_for_rows = self.max_working_bytes.saturating_sub(scratch_bytes);
        let admitted_rows = (available_for_rows / row_bytes).min(self.segment_rows);
        if admitted_rows == 0 {
            return Err(ProjectionError::ResourceBudgetExceeded {
                required: scratch_bytes.saturating_add(row_bytes),
                available: self.max_working_bytes,
            });
        }
        let peak_working_bytes = scratch_bytes.saturating_add(admitted_rows * row_bytes);
        Ok(ValidatedBuildConfig {
            dimension: self.dimension,
            requested_segment_rows: self.segment_rows,
            admitted_segment_rows: admitted_rows,
            max_working_bytes: self.max_working_bytes,
            peak_working_bytes,
            bytes_per_vector,
            bit_width: self.bit_width,
            transform_seed: self.transform_seed,
            identity: self.identity.clone(),
        })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ProjectionBuildAdmission {
    pub configured_working_bytes: usize,
    pub peak_working_bytes: usize,
    pub requested_segment_rows: usize,
    pub admitted_segment_rows: usize,
}

#[derive(Debug, Clone)]
pub(crate) struct ValidatedBuildConfig {
    pub dimension: usize,
    pub bit_width: RaBitQBitWidth,
    pub requested_segment_rows: usize,
    pub admitted_segment_rows: usize,
    pub max_working_bytes: usize,
    pub peak_working_bytes: usize,
    pub bytes_per_vector: usize,
    pub transform_seed: u64,
    pub identity: ProjectionIdentity,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SegmentDescriptor {
    pub index: usize,
    pub base_ordinal: usize,
    pub row_count: usize,
    pub payload_offset: u64,
    pub payload_bytes: u64,
    pub payload_checksum: u32,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProjectionManifest {
    pub protocol: String,
    pub format_version: u32,
    pub bit_width: u8,
    pub algorithm: String,
    pub metric: ProjectionMetric,
    pub transform: String,
    pub quantizer: String,
    pub calibration: String,
    pub transform_seed: u64,
    pub dimension: usize,
    pub requested_segment_rows: usize,
    pub admitted_segment_rows: usize,
    pub configured_build_working_bytes: usize,
    pub peak_build_working_bytes: usize,
    pub document_count: usize,
    pub source_digest: u64,
    pub payload_bytes: u64,
    pub payload_checksum: u32,
    pub identity: ProjectionIdentity,
    pub segments: Vec<SegmentDescriptor>,
}

impl ProjectionManifest {
    /// Capacity retained by the manifest, excluding vector payloads.
    pub fn retained_capacity_bytes(&self) -> usize {
        let strings = [
            &self.protocol,
            &self.algorithm,
            &self.transform,
            &self.quantizer,
            &self.calibration,
        ];
        strings
            .iter()
            .fold(std::mem::size_of::<Self>(), |bytes, value| {
                bytes.saturating_add(value.capacity())
            })
            .saturating_add(
                self.identity
                    .embedding_model
                    .as_ref()
                    .map_or(0, String::capacity),
            )
            .saturating_add(
                self.identity
                    .embedding_version
                    .as_ref()
                    .map_or(0, String::capacity),
            )
            .saturating_add(
                self.segments
                    .capacity()
                    .saturating_mul(std::mem::size_of::<SegmentDescriptor>()),
            )
    }

    pub(crate) fn new(
        config: &ValidatedBuildConfig,
        document_count: usize,
        source_digest: u64,
        payload_bytes: u64,
        payload_checksum: u32,
        segments: Vec<SegmentDescriptor>,
    ) -> Self {
        Self {
            protocol: PROJECTION_PROTOCOL.to_string(),
            format_version: PROJECTION_FORMAT_VERSION,
            bit_width: config.bit_width.bits(),
            algorithm: PROJECTION_ALGORITHM.to_string(),
            metric: ProjectionMetric::Cosine,
            transform: PROJECTION_TRANSFORM.to_string(),
            quantizer: config.bit_width.quantizer().to_string(),
            calibration: PROJECTION_CALIBRATION.to_string(),
            transform_seed: config.transform_seed,
            dimension: config.dimension,
            requested_segment_rows: config.requested_segment_rows,
            admitted_segment_rows: config.admitted_segment_rows,
            configured_build_working_bytes: config.max_working_bytes,
            peak_build_working_bytes: config.peak_working_bytes,
            document_count,
            source_digest,
            payload_bytes,
            payload_checksum,
            identity: config.identity.clone(),
            segments,
        }
    }

    pub(crate) fn validate(&self) -> Result<()> {
        if self.protocol != PROJECTION_PROTOCOL {
            return Err(ProjectionError::CorruptArtifact(format!(
                "unsupported protocol {}",
                self.protocol
            )));
        }
        if self.format_version != PROJECTION_FORMAT_VERSION {
            return Err(ProjectionError::CorruptArtifact(format!(
                "unsupported format version {}",
                self.format_version
            )));
        }
        let bit_width = RaBitQBitWidth::from_bits(self.bit_width)?;
        if self.algorithm != PROJECTION_ALGORITHM
            || self.metric != ProjectionMetric::Cosine
            || self.transform != PROJECTION_TRANSFORM
            || self.quantizer != bit_width.quantizer()
            || self.calibration != PROJECTION_CALIBRATION
        {
            return Err(ProjectionError::CorruptArtifact(
                "unsupported algorithm, metric, transform, quantizer, or calibration".to_string(),
            ));
        }
        if self.dimension == 0
            || self.admitted_segment_rows == 0
            || self.peak_build_working_bytes == 0
            || self.peak_build_working_bytes > self.configured_build_working_bytes
        {
            return Err(ProjectionError::CorruptArtifact(
                "dimension, segment rows, or build resource admission is invalid".to_string(),
            ));
        }

        let mut expected_offset = 0u64;
        let mut expected_ordinal = 0usize;
        for (index, segment) in self.segments.iter().enumerate() {
            if segment.index != index
                || segment.base_ordinal != expected_ordinal
                || segment.payload_offset != expected_offset
                || segment.row_count == 0
                || segment.row_count > self.admitted_segment_rows
            {
                return Err(ProjectionError::CorruptArtifact(format!(
                    "segment {} has an invalid index, ordinal, offset, or row count",
                    segment.index
                )));
            }
            let expected_bytes =
                segment_payload_bytes(self.dimension, bit_width, segment.row_count);
            if segment.payload_bytes != expected_bytes as u64 {
                return Err(ProjectionError::CorruptArtifact(format!(
                    "segment {} payload length mismatch",
                    segment.index
                )));
            }
            expected_offset = expected_offset.saturating_add(segment.payload_bytes);
            expected_ordinal = expected_ordinal.saturating_add(segment.row_count);
        }
        if expected_offset != self.payload_bytes || expected_ordinal != self.document_count {
            return Err(ProjectionError::CorruptArtifact(
                "manifest totals do not match segment descriptors".to_string(),
            ));
        }
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectionBuildReport {
    pub document_count: usize,
    pub segment_count: usize,
    pub raw_vector_bytes: u64,
    pub projection_payload_bytes: u64,
    pub configured_working_bytes: usize,
    pub peak_working_bytes: usize,
    pub requested_segment_rows: usize,
    pub admitted_segment_rows: usize,
}

#[derive(Debug)]
pub(crate) struct QuantizedSegment {
    pub base_ordinal: usize,
    pub ids: Vec<u64>,
    pub reconstruction_scales: Vec<f32>,
    pub reconstruction_offsets: Vec<f32>,
    pub codes: Vec<u8>,
}

impl QuantizedSegment {
    pub fn with_capacity(config: &ValidatedBuildConfig, base_ordinal: usize) -> Self {
        Self {
            base_ordinal,
            ids: Vec::with_capacity(config.admitted_segment_rows),
            reconstruction_scales: Vec::with_capacity(config.admitted_segment_rows),
            reconstruction_offsets: Vec::with_capacity(config.admitted_segment_rows),
            codes: Vec::with_capacity(
                config
                    .admitted_segment_rows
                    .saturating_mul(config.bytes_per_vector),
            ),
        }
    }

    pub fn row_count(&self) -> usize {
        self.ids.len()
    }

    pub fn empty(base_ordinal: usize) -> Self {
        Self {
            base_ordinal,
            ids: Vec::new(),
            reconstruction_scales: Vec::new(),
            reconstruction_offsets: Vec::new(),
            codes: Vec::new(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.ids.is_empty()
    }
}

#[derive(Debug)]
pub struct InMemoryProjection {
    pub(crate) manifest: ProjectionManifest,
    pub(crate) segments: Vec<QuantizedSegment>,
    pub(crate) build_report: ProjectionBuildReport,
}

impl InMemoryProjection {
    /// Allocated capacity, including partially filled quantized segments.
    pub fn retained_capacity_bytes(&self) -> usize {
        self.segments.iter().fold(
            self.manifest
                .retained_capacity_bytes()
                .saturating_add(std::mem::size_of::<Self>())
                .saturating_add(
                    self.segments
                        .capacity()
                        .saturating_mul(std::mem::size_of::<QuantizedSegment>()),
                ),
            |bytes, segment| {
                bytes
                    .saturating_add(
                        segment
                            .ids
                            .capacity()
                            .saturating_mul(std::mem::size_of::<u64>()),
                    )
                    .saturating_add(
                        segment
                            .reconstruction_scales
                            .capacity()
                            .saturating_mul(std::mem::size_of::<f32>()),
                    )
                    .saturating_add(
                        segment
                            .reconstruction_offsets
                            .capacity()
                            .saturating_mul(std::mem::size_of::<f32>()),
                    )
                    .saturating_add(segment.codes.capacity())
            },
        )
    }

    pub fn manifest(&self) -> &ProjectionManifest {
        &self.manifest
    }

    pub fn build_report(&self) -> &ProjectionBuildReport {
        &self.build_report
    }
}

pub(crate) fn segment_payload_bytes(
    dimension: usize,
    bit_width: RaBitQBitWidth,
    rows: usize,
) -> usize {
    rows.saturating_mul(
        std::mem::size_of::<u64>()
            .saturating_add(2 * std::mem::size_of::<f32>())
            .saturating_add(encoded_vector_bytes(dimension, bit_width)),
    )
}

pub(crate) fn ids_bytes(rows: usize) -> usize {
    rows.saturating_mul(std::mem::size_of::<u64>())
}

pub(crate) fn reconstruction_factor_bytes(rows: usize) -> usize {
    rows.saturating_mul(std::mem::size_of::<f32>())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn default_bit_width_favors_recall_over_faiss_standard_rabitq() {
        assert_eq!(DEFAULT_PROJECTION_BIT_WIDTH, 4);
        assert_eq!(RaBitQBitWidth::default(), RaBitQBitWidth::Four);
        assert_eq!(RaBitQBitWidth::default().quantizer(), PROJECTION_QUANTIZER);
    }
}