evoc 0.0.1

Embedding Vector Oriented Clustering — fast clustering of high-dimensional embedding vectors (Rust port of EVōC)
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
//! NN-descent heap operations (port of `evoc.common_nndescent`).

use crate::rng::{offset_state, tau_rand};
use ndarray::parallel::prelude::*;
use ndarray::{Array2, Axis};

pub const INF: f32 = f32::MAX;

/// Raw pointer wrapper for disjoint parallel row updates.
pub(crate) struct SendMutPtr<T>(*mut T);
unsafe impl<T> Send for SendMutPtr<T> {}
unsafe impl<T> Sync for SendMutPtr<T> {}

impl<T> SendMutPtr<T> {
    #[inline]
    pub(crate) fn new(ptr: *mut T) -> Self {
        Self(ptr)
    }

    #[inline]
    pub(crate) unsafe fn as_ptr(&self) -> *mut T {
        self.0
    }
}

/// `(indices, distances, flags)` neighbor heap.
pub type GraphHeap = (Array2<i32>, Array2<f32>, Array2<u8>);

pub fn make_heap(n_points: usize, size: usize) -> GraphHeap {
    let indices = Array2::from_elem((n_points, size), -1);
    let distances = Array2::from_elem((n_points, size), INF);
    let flags = Array2::zeros((n_points, size));
    (indices, distances, flags)
}

#[inline]
fn siftdown(heap1: &mut [f32], heap2: &mut [i32], mut elt: usize) {
    let n = heap1.len();
    while elt * 2 + 1 < n {
        let left_child = elt * 2 + 1;
        let right_child = left_child + 1;
        let mut swap = elt;

        if heap1[swap] < heap1[left_child] {
            swap = left_child;
        }

        if right_child < n && heap1[swap] < heap1[right_child] {
            swap = right_child;
        }

        if swap == elt {
            break;
        }

        heap1.swap(elt, swap);
        heap2.swap(elt, swap);
        elt = swap;
    }
}

fn deheap_sort_row(indices: &mut [i32], distances: &mut [f32]) {
    let n = indices.len();
    for j in (1..n).rev() {
        indices.swap(0, j);
        distances.swap(0, j);
        siftdown(&mut distances[..j], &mut indices[..j], 0);
    }
}

/// In-place heap sort of each row (parallel over vertices).
pub fn deheap_sort(indices: &mut Array2<i32>, distances: &mut Array2<f32>) {
    indices
        .axis_iter_mut(Axis(0))
        .into_par_iter()
        .zip(distances.axis_iter_mut(Axis(0)).into_par_iter())
        .for_each(|(mut idx_row, mut dist_row)| {
            deheap_sort_row(
                idx_row.as_slice_mut().unwrap(),
                dist_row.as_slice_mut().unwrap(),
            );
        });
}

/// Max-heap push for candidate generation (priority = random key).
pub fn build_candidates_heap_push(
    priorities: &mut [f32],
    indices: &mut [i32],
    p: f32,
    n: i32,
) -> u8 {
    if p >= priorities[0] {
        return 0;
    }

    let size = priorities.len();

    for i in 0..size {
        if n == indices[i] {
            return 0;
        }
    }

    priorities[0] = p;
    indices[0] = n;

    let mut i = 0usize;
    loop {
        let ic1 = 2 * i + 1;
        let ic2 = ic1 + 1;

        let i_swap = if ic1 >= size {
            break;
        } else if ic2 >= size {
            if priorities[ic1] > p {
                ic1
            } else {
                break;
            }
        } else if priorities[ic1] >= priorities[ic2] {
            if p < priorities[ic1] {
                ic1
            } else {
                break;
            }
        } else if p < priorities[ic2] {
            ic2
        } else {
            break;
        };

        priorities[i] = priorities[i_swap];
        indices[i] = indices[i_swap];
        i = i_swap;
    }

    priorities[i] = p;
    indices[i] = n;
    1
}

/// Push into the neighbor graph heap; sets `flags[i] = 1` for the new slot.
pub fn flagged_heap_push(
    priorities: &mut [f32],
    indices: &mut [i32],
    flags: &mut [u8],
    p: f32,
    n: i32,
) -> u8 {
    if p >= priorities[0] {
        return 0;
    }

    let size = priorities.len();

    for i in 0..size {
        if n == indices[i] {
            return 0;
        }
    }

    priorities[0] = p;
    indices[0] = n;

    let mut i = 0usize;
    loop {
        let ic1 = 2 * i + 1;
        let ic2 = ic1 + 1;

        let i_swap = if ic1 >= size {
            break;
        } else if ic2 >= size {
            if priorities[ic1] > p {
                ic1
            } else {
                break;
            }
        } else if priorities[ic1] >= priorities[ic2] {
            if p < priorities[ic1] {
                ic1
            } else {
                break;
            }
        } else if p < priorities[ic2] {
            ic2
        } else {
            break;
        };

        priorities[i] = priorities[i_swap];
        indices[i] = indices[i_swap];
        flags[i] = flags[i_swap];
        i = i_swap;
    }

    priorities[i] = p;
    indices[i] = n;
    flags[i] = 1;
    1
}

/// Build candidate neighbor heaps for one NN-descent iteration.
pub fn build_candidates(
    current_graph: &mut GraphHeap,
    max_candidates: usize,
    rng_state: &[i64; 3],
    n_threads: usize,
) -> (Array2<i32>, Array2<i32>) {
    let (current_indices, _, current_flags) = current_graph;
    let n_vertices = current_indices.nrows();
    let n_neighbors = current_indices.ncols();

    let mut new_candidate_indices = Array2::from_elem((n_vertices, max_candidates), -1);
    let mut new_candidate_priority = Array2::from_elem((n_vertices, max_candidates), INF);
    let mut old_candidate_indices = Array2::from_elem((n_vertices, max_candidates), -1);
    let mut old_candidate_priority = Array2::from_elem((n_vertices, max_candidates), INF);

    let block_size = n_vertices / n_threads + 1;

    let new_pri_ptr = SendMutPtr::new(new_candidate_priority.as_mut_ptr());
    let new_idx_ptr = SendMutPtr::new(new_candidate_indices.as_mut_ptr());
    let old_pri_ptr = SendMutPtr::new(old_candidate_priority.as_mut_ptr());
    let old_idx_ptr = SendMutPtr::new(old_candidate_indices.as_mut_ptr());

    (0..n_threads).into_par_iter().for_each(|n| {
        let mut local_rng = offset_state(rng_state, n as i64);
        let block_start = n * block_size;
        let block_end = (block_start + block_size).min(n_vertices);

        for i in 0..n_vertices {
            for j in 0..n_neighbors {
                let idx = current_indices[[i, j]];
                if idx < 0 {
                    continue;
                }

                if !((i >= block_start && i < block_end)
                    || ((idx as usize) >= block_start && (idx as usize) < block_end))
                {
                    continue;
                }

                let isn = current_flags[[i, j]] != 0;
                let d = tau_rand(&mut local_rng);

                if isn {
                    if i >= block_start && i < block_end {
                        unsafe {
                            build_candidates_heap_push(
                                std::slice::from_raw_parts_mut(
                                    new_pri_ptr.as_ptr().add(i * max_candidates),
                                    max_candidates,
                                ),
                                std::slice::from_raw_parts_mut(
                                    new_idx_ptr.as_ptr().add(i * max_candidates),
                                    max_candidates,
                                ),
                                d,
                                idx,
                            );
                        }
                    }
                    if (idx as usize) >= block_start && (idx as usize) < block_end {
                        let idx_u = idx as usize;
                        unsafe {
                            build_candidates_heap_push(
                                std::slice::from_raw_parts_mut(
                                    new_pri_ptr.as_ptr().add(idx_u * max_candidates),
                                    max_candidates,
                                ),
                                std::slice::from_raw_parts_mut(
                                    new_idx_ptr.as_ptr().add(idx_u * max_candidates),
                                    max_candidates,
                                ),
                                d,
                                i as i32,
                            );
                        }
                    }
                } else {
                    if i >= block_start && i < block_end {
                        unsafe {
                            build_candidates_heap_push(
                                std::slice::from_raw_parts_mut(
                                    old_pri_ptr.as_ptr().add(i * max_candidates),
                                    max_candidates,
                                ),
                                std::slice::from_raw_parts_mut(
                                    old_idx_ptr.as_ptr().add(i * max_candidates),
                                    max_candidates,
                                ),
                                d,
                                idx,
                            );
                        }
                    }
                    if (idx as usize) >= block_start && (idx as usize) < block_end {
                        let idx_u = idx as usize;
                        unsafe {
                            build_candidates_heap_push(
                                std::slice::from_raw_parts_mut(
                                    old_pri_ptr.as_ptr().add(idx_u * max_candidates),
                                    max_candidates,
                                ),
                                std::slice::from_raw_parts_mut(
                                    old_idx_ptr.as_ptr().add(idx_u * max_candidates),
                                    max_candidates,
                                ),
                                d,
                                i as i32,
                            );
                        }
                    }
                }
            }
        }
    });

    let indices = &current_graph.0;
    let flags = &mut current_graph.2;

    flags
        .axis_iter_mut(Axis(0))
        .into_par_iter()
        .zip(indices.axis_iter(Axis(0)))
        .zip(new_candidate_indices.axis_iter(Axis(0)))
        .for_each(|((mut flag_row, idx_row), new_row)| {
            for j in 0..n_neighbors {
                let idx = idx_row[j];
                if idx < 0 {
                    continue;
                }
                for k in 0..max_candidates {
                    if new_row[k] == idx {
                        flag_row[j] = 0;
                        break;
                    }
                }
            }
        });

    (new_candidate_indices, old_candidate_indices)
}

/// Apply unsorted per-thread update batches to the graph.
pub fn apply_graph_update_array(
    current_graph: &mut GraphHeap,
    update_array: &ndarray::Array3<f32>,
    n_updates_per_thread: &[i32],
    n_threads: usize,
) -> u32 {
    let n_vertices = current_graph.1.nrows();
    let ncols = current_graph.1.ncols();
    let block_size = n_vertices / n_threads + 1;

    let indices_ptr = SendMutPtr::new(current_graph.0.as_mut_ptr());
    let distances_ptr = SendMutPtr::new(current_graph.1.as_mut_ptr());
    let flags_ptr = SendMutPtr::new(current_graph.2.as_mut_ptr());

    (0..n_threads)
        .into_par_iter()
        .map(|n| {
            let block_start = n * block_size;
            let block_end = (block_start + block_size).min(n_vertices);
            let mut local_changes = 0u32;

            for t in 0..n_threads {
                let count = n_updates_per_thread[t] as usize;
                for j in 0..count {
                    let p = update_array[[t, j, 0]] as i32;
                    if p == -1 {
                        break;
                    }
                    let q = update_array[[t, j, 1]] as i32;
                    let d = update_array[[t, j, 2]];

                    if (p as usize) >= block_start && (p as usize) < block_end {
                        unsafe {
                            local_changes += flagged_heap_push(
                                std::slice::from_raw_parts_mut(
                                    distances_ptr.as_ptr().add(p as usize * ncols),
                                    ncols,
                                ),
                                std::slice::from_raw_parts_mut(
                                    indices_ptr.as_ptr().add(p as usize * ncols),
                                    ncols,
                                ),
                                std::slice::from_raw_parts_mut(
                                    flags_ptr.as_ptr().add(p as usize * ncols),
                                    ncols,
                                ),
                                d,
                                q,
                            ) as u32;
                        }
                    }
                    if (q as usize) >= block_start && (q as usize) < block_end {
                        unsafe {
                            local_changes += flagged_heap_push(
                                std::slice::from_raw_parts_mut(
                                    distances_ptr.as_ptr().add(q as usize * ncols),
                                    ncols,
                                ),
                                std::slice::from_raw_parts_mut(
                                    indices_ptr.as_ptr().add(q as usize * ncols),
                                    ncols,
                                ),
                                std::slice::from_raw_parts_mut(
                                    flags_ptr.as_ptr().add(q as usize * ncols),
                                    ncols,
                                ),
                                d,
                                p,
                            ) as u32;
                        }
                    }
                }
            }
            local_changes
        })
        .sum()
}

/// Apply updates bucketed by target vertex block.
pub fn apply_sorted_graph_updates(
    current_graph: &mut GraphHeap,
    update_array: &ndarray::Array3<f32>,
    n_updates_per_block: &Array2<i32>,
    n_threads: usize,
) -> u32 {
    let n_vertices = current_graph.1.nrows();
    let ncols = current_graph.1.ncols();
    let vertex_block_size = n_vertices / n_threads + 1;
    let max_updates_per_thread = update_array.len_of(Axis(1)) / n_threads;

    let indices_ptr = SendMutPtr::new(current_graph.0.as_mut_ptr());
    let distances_ptr = SendMutPtr::new(current_graph.1.as_mut_ptr());
    let flags_ptr = SendMutPtr::new(current_graph.2.as_mut_ptr());

    (0..n_threads)
        .into_par_iter()
        .map(|n| {
            let block_start = n * vertex_block_size;
            let block_end = (block_start + vertex_block_size).min(n_vertices);
            let mut local_changes = 0u32;

            for t in 0..n_threads {
                let thread_start = t * max_updates_per_thread;
                let thread_count = n_updates_per_block[[n, t + 1]] as usize;

                for j in 0..thread_count {
                    let idx = thread_start + j;
                    let p = update_array[[n, idx, 0]] as i32;
                    let q = update_array[[n, idx, 1]] as i32;
                    let d = update_array[[n, idx, 2]];

                    if (p as usize) >= block_start && (p as usize) < block_end {
                        unsafe {
                            local_changes += flagged_heap_push(
                                std::slice::from_raw_parts_mut(
                                    distances_ptr.as_ptr().add(p as usize * ncols),
                                    ncols,
                                ),
                                std::slice::from_raw_parts_mut(
                                    indices_ptr.as_ptr().add(p as usize * ncols),
                                    ncols,
                                ),
                                std::slice::from_raw_parts_mut(
                                    flags_ptr.as_ptr().add(p as usize * ncols),
                                    ncols,
                                ),
                                d,
                                q,
                            ) as u32;
                        }
                    }
                    if (q as usize) >= block_start && (q as usize) < block_end {
                        unsafe {
                            local_changes += flagged_heap_push(
                                std::slice::from_raw_parts_mut(
                                    distances_ptr.as_ptr().add(q as usize * ncols),
                                    ncols,
                                ),
                                std::slice::from_raw_parts_mut(
                                    indices_ptr.as_ptr().add(q as usize * ncols),
                                    ncols,
                                ),
                                std::slice::from_raw_parts_mut(
                                    flags_ptr.as_ptr().add(q as usize * ncols),
                                    ncols,
                                ),
                                d,
                                p,
                            ) as u32;
                        }
                    }
                }
            }
            local_changes
        })
        .sum()
}