hawdb-vector-projection 0.18.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
532
533
534
535
536
537
538
539
540
541
542
543
544
// Copyright 2026 Nowledge
// SPDX-License-Identifier: Apache-2.0

use super::*;

/// Sampled local radii and the LS exponent affect construction, never queries.
/// Radii are the mean Euclidean distances to the closest sampled neighbors.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LsgBuildConfig {
    pub sample_size: usize,
    pub neighbors: usize,
    pub exponent: f64,
}

impl Default for LsgBuildConfig {
    fn default() -> Self {
        Self {
            sample_size: 256,
            neighbors: 10,
            exponent: 1.0,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DiskAnnBuildConfig {
    pub max_degree: usize,
    pub build_beam: usize,
    /// Vamana's occlusion relaxation, applied to squared construction distances.
    pub alpha: f64,
    pub lsg: Option<LsgBuildConfig>,
    pub max_working_bytes: usize,
}

impl Default for DiskAnnBuildConfig {
    fn default() -> Self {
        Self {
            max_degree: 32,
            build_beam: 128,
            alpha: 1.2,
            lsg: None,
            max_working_bytes: 64 << 20,
        }
    }
}

#[derive(Debug)]
struct Node {
    id: u64,
    vector: Vec<f32>,
    neighbors: Vec<u32>,
}

/// A private, immutable graph builder result. Database CRUD publishes successor
/// graphs; mutating a graph already visible to an old MVCC pin is forbidden.
#[derive(Debug)]
pub struct DiskAnnIndex {
    dimension: usize,
    metric: VectorDistance,
    config: DiskAnnBuildConfig,
    nodes: Vec<Node>,
    radii: Vec<f64>,
    /// A deterministic permutation supplies both insertion order and a directed
    /// connectivity backbone. Retaining one backbone edge prevents disconnection
    /// when pruning duplicate vectors or extremely separated clusters.
    order: Vec<u32>,
    successor: Vec<u32>,
}

impl DiskAnnIndex {
    /// Preflight for an exact-capacity owned source, before copying any vectors.
    pub fn build_memory_bound(
        count: usize,
        dimension: usize,
        config: DiskAnnBuildConfig,
    ) -> Result<usize> {
        if count > u32::MAX as usize {
            return Err(configuration("DiskANN source count exceeds ordinal space"));
        }
        validate_config(dimension, config)?;
        let overflow = || configuration("DiskANN build memory bound overflow");
        let per_node = dimension
            .checked_mul(size_of::<f32>())
            .and_then(|bytes| bytes.checked_add(config.max_degree.checked_mul(size_of::<u32>())?))
            .and_then(|bytes| {
                bytes.checked_add(
                    size_of::<(u64, Vec<f32>)>()
                        + size_of::<Node>()
                        + 3 * size_of::<u32>()
                        + size_of::<f64>()
                        + size_of::<Neighbor>(),
                )
            })
            .ok_or_else(overflow)?;
        let required = count
            .checked_mul(per_node)
            .and_then(|bytes| bytes.checked_add(size_of::<Self>()))
            .and_then(|bytes| {
                bytes.checked_add(scratch_bytes(count, config.build_beam.min(count), count).ok()?)
            })
            .and_then(|bytes| {
                bytes.checked_add(
                    config
                        .max_degree
                        .checked_add(1)?
                        .checked_mul(size_of::<u32>() + size_of::<Neighbor>())?,
                )
            })
            .and_then(|bytes| {
                bytes.checked_add(
                    config
                        .lsg
                        .map_or(0, |lsg| lsg.neighbors.min(count))
                        .checked_mul(size_of::<f64>())?,
                )
            })
            .ok_or_else(overflow)?;
        require_memory(required, config.max_working_bytes)?;
        Ok(required)
    }
    /// Consume an admitted private source. IDs are stable within the caller's
    /// source binding; input order cannot affect ties or the resulting graph.
    pub fn build(
        dimension: usize,
        metric: VectorDistance,
        mut entries: Vec<(u64, Vec<f32>)>,
        config: DiskAnnBuildConfig,
        task: Option<&RuntimeTaskContext>,
    ) -> Result<Self> {
        checkpoint(task)?;
        validate_config(dimension, config)?;
        if entries.len() > u32::MAX as usize {
            return Err(configuration("DiskANN node count exceeds u32"));
        }
        require_memory(build_bytes(&entries, config)?, config.max_working_bytes)?;
        for (_, vector) in &entries {
            validate_query(dimension, metric, vector, task)?;
        }
        // Heapsort is allocation-free; checkpoints also bound comparison work.
        sort_entries(&mut entries, task)?;
        for pair in entries.windows(2) {
            if pair[0].0 == pair[1].0 {
                return Err(ProjectionError::DuplicateId(pair[0].0));
            }
        }
        let count = entries.len();
        let mut index = Self {
            dimension,
            metric,
            config,
            nodes: Vec::with_capacity(count),
            radii: if config.lsg.is_some() {
                vec![1.0; count]
            } else {
                Vec::new()
            },
            order: (0..count as u32).collect(),
            successor: vec![0; count],
        };
        for (id, vector) in entries {
            checkpoint(task)?;
            index.nodes.push(Node {
                id,
                vector,
                neighbors: Vec::with_capacity(config.max_degree),
            });
        }
        // A fixed shuffle avoids a sorted-input graph while remaining replayable.
        let mut random = 0x9e37_79b9_7f4a_7c15u64;
        for position in (1..count).rev() {
            checkpoint(task)?;
            random ^= random << 13;
            random ^= random >> 7;
            random ^= random << 17;
            index
                .order
                .swap(position, (random % (position + 1) as u64) as usize);
        }
        if count < 2 {
            return Ok(index);
        }
        for position in 0..count {
            checkpoint(task)?;
            let ordinal = index.order[position];
            let next = index.order[(position + 1) % count];
            let previous = index.order[(position + count - 1) % count];
            index.successor[ordinal as usize] = next;
            index.nodes[ordinal as usize].neighbors.push(next);
            if next != previous {
                index.nodes[ordinal as usize].neighbors.push(previous);
            }
        }
        if let Some(lsg) = config.lsg {
            index.compute_radii(lsg, task)?;
        }
        let mut scratch = Scratch::new(count, config.build_beam.min(count), count)?;
        scratch.collect_visited = true;
        scratch.visited = Vec::with_capacity(count);
        // Vamana first removes redundant short edges with alpha=1, then adds
        // longer alternatives using the requested relaxation.
        for alpha in [1.0, config.alpha] {
            for position in 0..count {
                checkpoint(task)?;
                let ordinal = index.order[position];
                traverse(
                    &index,
                    &mut scratch,
                    DiskAnnSearchOptions {
                        beam_width: config.build_beam,
                        max_visited: count,
                        max_working_bytes: config.max_working_bytes,
                        task,
                    },
                    |_| Ok(true),
                    |other| index.construction_distance(ordinal, other, task),
                )?;
                let neighbors = index.prune(ordinal, &scratch.visited, alpha, task)?;
                index.nodes[ordinal as usize].neighbors = neighbors;
                // Reverse links are pruned under the same metric and degree cap.
                for position in 0..index.nodes[ordinal as usize].neighbors.len() {
                    let other = index.nodes[ordinal as usize].neighbors[position];
                    let mut candidates = Vec::with_capacity(config.max_degree + 1);
                    candidates.extend_from_slice(&index.nodes[other as usize].neighbors);
                    if candidates.contains(&ordinal) {
                        continue;
                    }
                    candidates.push(ordinal);
                    index.nodes[other as usize].neighbors =
                        index.prune(other, &candidates, alpha, task)?;
                }
            }
        }
        Ok(index)
    }

    pub fn config(&self) -> DiskAnnBuildConfig {
        self.config
    }

    pub(super) fn vector(&self, ordinal: u32) -> Result<&[f32]> {
        self.nodes
            .get(ordinal as usize)
            .map(|node| node.vector.as_slice())
            .ok_or_else(|| corrupt("DiskANN ordinal is outside its pinned graph"))
    }

    pub fn local_radius(&self, ordinal: u32) -> Option<f64> {
        self.radii.get(ordinal as usize).copied()
    }

    pub fn retained_bytes(&self) -> usize {
        size_of::<Self>()
            + self.nodes.capacity() * size_of::<Node>()
            + (self.order.capacity() + self.successor.capacity()) * size_of::<u32>()
            + self.radii.capacity() * size_of::<f64>()
            + self
                .nodes
                .iter()
                .map(|node| {
                    node.vector.capacity() * size_of::<f32>()
                        + node.neighbors.capacity() * size_of::<u32>()
                })
                .sum::<usize>()
    }

    fn construction_distance(
        &self,
        left: u32,
        right: u32,
        task: Option<&RuntimeTaskContext>,
    ) -> Result<f64> {
        let raw = self.distance(&self.nodes[left as usize].vector, right, task)?;
        let squared = match self.metric {
            VectorDistance::Euclidean => raw * raw,
            // Squared Euclidean distance after unit normalization.
            VectorDistance::Cosine => 2.0 * raw,
        };
        Ok(if let Some(lsg) = self.config.lsg {
            squared / (self.radii[left as usize] * self.radii[right as usize]).powf(lsg.exponent)
        } else {
            squared
        })
    }

    fn prune(
        &self,
        ordinal: u32,
        candidates: &[u32],
        alpha: f64,
        task: Option<&RuntimeTaskContext>,
    ) -> Result<Vec<u32>> {
        let mut scored = Vec::with_capacity(candidates.len());
        for &other in candidates {
            checkpoint(task)?;
            if other != ordinal {
                scored.push(Neighbor {
                    ordinal: other,
                    id: self.id(other)?,
                    distance: self.construction_distance(ordinal, other, task)?,
                    expanded: false,
                });
            }
        }
        // Provider ordinals are unique and traversal visits each at most once;
        // reciprocal lists also preserve uniqueness.
        scored.sort_unstable_by(Neighbor::compare);
        let mut selected = Vec::with_capacity(self.config.max_degree);
        for candidate in scored {
            checkpoint(task)?;
            if selected.len() == self.config.max_degree - 1 {
                break;
            }
            let mut occluded = false;
            for &neighbor in &selected {
                if alpha * self.construction_distance(neighbor, candidate.ordinal, task)?
                    <= candidate.distance
                {
                    occluded = true;
                    break;
                }
            }
            if !occluded {
                selected.push(candidate.ordinal);
            }
        }
        let backbone = self.successor[ordinal as usize];
        if !selected.contains(&backbone) {
            selected.push(backbone);
        }
        selected.sort_unstable();
        Ok(selected)
    }

    fn compute_radii(
        &mut self,
        lsg: LsgBuildConfig,
        task: Option<&RuntimeTaskContext>,
    ) -> Result<()> {
        let sample_size = lsg.sample_size.min(self.nodes.len());
        let mut closest = Vec::with_capacity(lsg.neighbors.min(sample_size));
        let mut largest = 0.0f64;
        for ordinal in 0..self.nodes.len() {
            closest.clear();
            for &sample in self.order.iter().take(sample_size) {
                checkpoint(task)?;
                if sample as usize == ordinal {
                    continue;
                }
                let raw = self.distance(&self.nodes[ordinal].vector, sample, task)?;
                let radius = match self.metric {
                    VectorDistance::Euclidean => raw,
                    VectorDistance::Cosine => (2.0 * raw).sqrt(),
                };
                let position = closest.partition_point(|value| *value < radius);
                if position < lsg.neighbors {
                    if closest.len() == lsg.neighbors {
                        closest.pop();
                    }
                    closest.insert(position, radius);
                }
            }
            let radius = closest.iter().sum::<f64>() / closest.len().max(1) as f64;
            self.radii[ordinal] = radius;
            largest = largest.max(radius);
        }
        // Identical points have a zero neighborhood radius. A relative floor
        // avoids NaN/Inf while preserving the scale of nondegenerate datasets.
        let floor = if largest == 0.0 { 1.0 } else { largest * 1e-12 };
        for chunk in self.radii.chunks_mut(4096) {
            checkpoint(task)?;
            for radius in chunk {
                *radius = radius.max(floor);
            }
        }
        Ok(())
    }
}

impl DiskAnnRead for DiskAnnIndex {
    fn len(&self) -> usize {
        self.nodes.len()
    }
    fn dimension(&self) -> usize {
        self.dimension
    }
    fn max_degree(&self) -> usize {
        self.config.max_degree
    }
    fn metric(&self) -> VectorDistance {
        self.metric
    }
    fn entry_point(&self) -> Option<u32> {
        self.order.first().copied()
    }
    fn ids_are_sorted(&self) -> bool {
        true
    }
    fn id(&self, ordinal: u32) -> Result<u64> {
        self.nodes
            .get(ordinal as usize)
            .map(|node| node.id)
            .ok_or_else(|| corrupt("DiskANN ordinal is outside its pinned graph"))
    }
    fn neighbors(&self, ordinal: u32) -> Result<impl Iterator<Item = u32>> {
        Ok(self
            .nodes
            .get(ordinal as usize)
            .ok_or_else(|| corrupt("DiskANN ordinal is outside its pinned graph"))?
            .neighbors
            .iter()
            .copied())
    }
    fn distance(
        &self,
        query: &[f32],
        ordinal: u32,
        task: Option<&RuntimeTaskContext>,
    ) -> Result<f64> {
        let node = self
            .nodes
            .get(ordinal as usize)
            .ok_or_else(|| corrupt("DiskANN ordinal is outside its pinned graph"))?;
        distance(self.metric, query, &node.vector, task)
    }
}

pub(super) fn validate_config(dimension: usize, config: DiskAnnBuildConfig) -> Result<()> {
    if dimension == 0 || dimension > u32::MAX as usize {
        return Err(configuration("DiskANN dimension must be a positive u32"));
    }
    if config.max_degree < 2
        || config.max_degree > u32::MAX as usize
        || config.build_beam < config.max_degree
        || config.build_beam > u32::MAX as usize
        || !config.alpha.is_finite()
        || config.alpha < 1.0
    {
        return Err(configuration(
            "DiskANN requires degree >= 2, build beam >= degree and finite alpha >= 1",
        ));
    }
    if let Some(lsg) = config.lsg
        && (lsg.sample_size < 2
            || lsg.sample_size > u32::MAX as usize
            || lsg.neighbors == 0
            || lsg.neighbors > lsg.sample_size
            || !lsg.exponent.is_finite()
            || lsg.exponent <= 0.0
            || lsg.exponent > 2.0)
    {
        return Err(configuration(
            "LSG requires 0 < neighbors <= sample size, sample size >= 2 and exponent in (0, 2]",
        ));
    }
    Ok(())
}

fn build_bytes(entries: &Vec<(u64, Vec<f32>)>, config: DiskAnnBuildConfig) -> Result<usize> {
    let overflow = || configuration("DiskANN build memory bound overflow");
    let count = entries.len();
    let mut bytes = entries
        .capacity()
        .checked_mul(size_of::<(u64, Vec<f32>)>())
        .and_then(|bytes| bytes.checked_add(size_of::<DiskAnnIndex>()))
        .ok_or_else(overflow)?;
    for (_, vector) in entries {
        bytes = bytes
            .checked_add(
                vector
                    .capacity()
                    .checked_mul(size_of::<f32>())
                    .ok_or_else(overflow)?,
            )
            .ok_or_else(overflow)?;
    }
    let per_node = config
        .max_degree
        .checked_mul(size_of::<u32>())
        .and_then(|bytes| {
            bytes.checked_add(
                size_of::<Node>() + 3 * size_of::<u32>() + size_of::<f64>() + size_of::<Neighbor>(),
            )
        })
        .ok_or_else(overflow)?;
    bytes = bytes
        .checked_add(count.checked_mul(per_node).ok_or_else(overflow)?)
        .and_then(|bytes| {
            bytes.checked_add(scratch_bytes(count, config.build_beam.min(count), count).ok()?)
        })
        .and_then(|bytes| {
            bytes.checked_add(
                config
                    .max_degree
                    .checked_add(1)?
                    .checked_mul(size_of::<u32>() + size_of::<Neighbor>())?,
            )
        })
        .ok_or_else(overflow)?;
    if let Some(lsg) = config.lsg {
        bytes = bytes
            .checked_add(
                lsg.neighbors
                    .min(count)
                    .checked_mul(size_of::<f64>())
                    .ok_or_else(overflow)?,
            )
            .ok_or_else(overflow)?;
    }
    Ok(bytes)
}

fn sort_entries(entries: &mut [(u64, Vec<f32>)], task: Option<&RuntimeTaskContext>) -> Result<()> {
    fn sift(
        entries: &mut [(u64, Vec<f32>)],
        mut root: usize,
        task: Option<&RuntimeTaskContext>,
    ) -> Result<()> {
        while root < entries.len() / 2 {
            checkpoint(task)?;
            let left = root * 2 + 1;
            let right = left + 1;
            let child = if right < entries.len() && entries[right].0 > entries[left].0 {
                right
            } else {
                left
            };
            if entries[root].0 >= entries[child].0 {
                break;
            }
            entries.swap(root, child);
            root = child;
        }
        Ok(())
    }
    for root in (0..entries.len() / 2).rev() {
        sift(entries, root, task)?;
    }
    for end in (1..entries.len()).rev() {
        checkpoint(task)?;
        entries.swap(0, end);
        sift(&mut entries[..end], 0, task)?;
    }
    Ok(())
}