hive-gpu 0.2.0

High-performance GPU acceleration for vector operations with Device Info API (Metal, CUDA, ROCm)
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
//! GPU Batch Construction Shader for HNSW
//!
//! This compute shader implements optimized batch processing for HNSW graph construction.
//! Processes multiple vectors simultaneously to maximize GPU parallelism.
//!
//! Key features:
//! - Parallel distance matrix computation (batch vs existing)
//! - Shared memory optimization for workgroup collaboration
//! - Efficient neighbor selection for entire batch
//! - Memory-efficient sparse matrix operations
//! - Support for large batches (1000-10000 vectors)

/// Batch construction parameters
struct BatchParams {
    batch_size: u32,           // Number of vectors in current batch
    batch_offset: u32,         // Offset in batch vector buffer
    total_nodes: u32,          // Total existing nodes in graph
    dimension: u32,            // Vector dimension
    m: u32,                    // Max connections per node
    m0: u32,                   // Max connections at level 0
    ef_construction: u32,      // Candidate list size
    metric_type: u32,          // 0=cosine, 1=euclidean, 2=dot_product
}

/// HNSW Node structure
struct HnswNode {
    id: u32,
    level: u32,
    connection_count: u32,
    vector_offset: u32,
}

/// Distance matrix entry (sparse storage)
struct DistanceEntry {
    batch_idx: u32,
    existing_idx: u32,
    distance: f32,
    valid: u32,
}

/// Bind group layout
@group(0) @binding(0) var<uniform> params: BatchParams;
@group(0) @binding(1) var<storage, read> batch_vectors: array<f32>;
@group(0) @binding(2) var<storage, read> existing_vectors: array<f32>;
@group(0) @binding(3) var<storage, read_write> nodes: array<u32>;
@group(0) @binding(4) var<storage, read_write> connections: array<u32>;
@group(0) @binding(5) var<storage, read_write> distance_matrix: array<f32>;
@group(0) @binding(6) var<storage, read_write> batch_candidates: array<DistanceEntry>;
@group(0) @binding(7) var<storage, read> levels: array<u32>;

// Shared memory for workgroup collaboration (Metal supports up to 32KB)
var<workgroup> shared_batch_vector: array<f32, 1024>;      // 4KB
var<workgroup> shared_existing_vector: array<f32, 1024>;   // 4KB
var<workgroup> shared_distances: array<f32, 256>;          // 1KB

// ============================================================================
// Helper Functions
// ============================================================================

fn get_node_at(index: u32) -> HnswNode {
    let base = index * 4u;
    return HnswNode(
        nodes[base + 0u],
        nodes[base + 1u],
        nodes[base + 2u],
        nodes[base + 3u]
    );
}

fn set_node_at(index: u32, node: HnswNode) {
    let base = index * 4u;
    nodes[base + 0u] = node.id;
    nodes[base + 1u] = node.level;
    nodes[base + 2u] = node.connection_count;
    nodes[base + 3u] = node.vector_offset;
}

// ============================================================================
// Distance Calculations (Optimized)
// ============================================================================

/// Cosine similarity using shared memory
fn cosine_similarity_shared(
    batch_offset: u32,
    existing_offset: u32,
    local_idx: u32,
    workgroup_size: u32
) -> f32 {
    var dot: f32 = 0.0;
    var norm_a: f32 = 0.0;
    var norm_b: f32 = 0.0;
    
    // Parallel reduction: cada thread processa dimension/workgroup_size elementos
    let items_per_thread = (params.dimension + workgroup_size - 1u) / workgroup_size;
    let start = local_idx * items_per_thread;
    let end = min(start + items_per_thread, params.dimension);
    
    for (var i = start; i < end; i++) {
        let val_a = batch_vectors[batch_offset + i];
        let val_b = existing_vectors[existing_offset + i];
        
        dot += val_a * val_b;
        norm_a += val_a * val_a;
        norm_b += val_b * val_b;
    }
    
    // Store partial results in shared memory
    shared_distances[local_idx * 3u + 0u] = dot;
    shared_distances[local_idx * 3u + 1u] = norm_a;
    shared_distances[local_idx * 3u + 2u] = norm_b;
    
    workgroupBarrier();
    
    // Reduction: combine partial results (only thread 0)
    if (local_idx == 0u) {
        var total_dot: f32 = 0.0;
        var total_norm_a: f32 = 0.0;
        var total_norm_b: f32 = 0.0;
        
        for (var i: u32 = 0u; i < workgroup_size; i++) {
            total_dot += shared_distances[i * 3u + 0u];
            total_norm_a += shared_distances[i * 3u + 1u];
            total_norm_b += shared_distances[i * 3u + 2u];
        }
        
        total_norm_a = sqrt(total_norm_a);
        total_norm_b = sqrt(total_norm_b);
        
        if (total_norm_a > 0.0 && total_norm_b > 0.0) {
            shared_distances[0] = 1.0 - (total_dot / (total_norm_a * total_norm_b));
        } else {
            shared_distances[0] = 1.0;
        }
    }
    
    workgroupBarrier();
    return shared_distances[0];
}

/// Euclidean distance using shared memory
fn euclidean_distance_shared(
    batch_offset: u32,
    existing_offset: u32,
    local_idx: u32,
    workgroup_size: u32
) -> f32 {
    var sum_squared: f32 = 0.0;
    
    let items_per_thread = (params.dimension + workgroup_size - 1u) / workgroup_size;
    let start = local_idx * items_per_thread;
    let end = min(start + items_per_thread, params.dimension);
    
    for (var i = start; i < end; i++) {
        let diff = batch_vectors[batch_offset + i] - existing_vectors[existing_offset + i];
        sum_squared += diff * diff;
    }
    
    shared_distances[local_idx] = sum_squared;
    workgroupBarrier();
    
    if (local_idx == 0u) {
        var total: f32 = 0.0;
        for (var i: u32 = 0u; i < workgroup_size; i++) {
            total += shared_distances[i];
        }
        shared_distances[0] = sqrt(total);
    }
    
    workgroupBarrier();
    return shared_distances[0];
}

/// Dot product using shared memory
fn dot_product_shared(
    batch_offset: u32,
    existing_offset: u32,
    local_idx: u32,
    workgroup_size: u32
) -> f32 {
    var dot: f32 = 0.0;
    
    let items_per_thread = (params.dimension + workgroup_size - 1u) / workgroup_size;
    let start = local_idx * items_per_thread;
    let end = min(start + items_per_thread, params.dimension);
    
    for (var i = start; i < end; i++) {
        dot += batch_vectors[batch_offset + i] * existing_vectors[existing_offset + i];
    }
    
    shared_distances[local_idx] = dot;
    workgroupBarrier();
    
    if (local_idx == 0u) {
        var total: f32 = 0.0;
        for (var i: u32 = 0u; i < workgroup_size; i++) {
            total += shared_distances[i];
        }
        shared_distances[0] = -total; // Negative for distance
    }
    
    workgroupBarrier();
    return shared_distances[0];
}

/// Calculate distance based on metric (non-shared fallback)
fn calculate_distance_direct(batch_offset: u32, existing_offset: u32) -> f32 {
    switch (params.metric_type) {
        case 0u: { // Cosine
            var dot: f32 = 0.0;
            var norm_a: f32 = 0.0;
            var norm_b: f32 = 0.0;
            
            for (var i: u32 = 0u; i < params.dimension; i++) {
                let val_a = batch_vectors[batch_offset + i];
                let val_b = existing_vectors[existing_offset + i];
                dot += val_a * val_b;
                norm_a += val_a * val_a;
                norm_b += val_b * val_b;
            }
            
            norm_a = sqrt(norm_a);
            norm_b = sqrt(norm_b);
            
            if (norm_a > 0.0 && norm_b > 0.0) {
                return 1.0 - (dot / (norm_a * norm_b));
            }
            return 1.0;
        }
        case 1u: { // Euclidean
            var sum_squared: f32 = 0.0;
            for (var i: u32 = 0u; i < params.dimension; i++) {
                let diff = batch_vectors[batch_offset + i] - existing_vectors[existing_offset + i];
                sum_squared += diff * diff;
            }
            return sqrt(sum_squared);
        }
        case 2u: { // Dot Product
            var dot: f32 = 0.0;
            for (var i: u32 = 0u; i < params.dimension; i++) {
                dot += batch_vectors[batch_offset + i] * existing_vectors[existing_offset + i];
            }
            return -dot;
        }
        default: { return 0.0; }
    }
}

// ============================================================================
// Distance Matrix Computation
// ============================================================================

/// Kernel 1: Compute distance matrix (batch vs existing)
/// Uses 2D dispatch: [batch_size, existing_nodes]
@compute @workgroup_size(16, 16)
fn compute_distance_matrix(@builtin(global_invocation_id) gid: vec3<u32>) {
    let batch_idx = gid.x;
    let existing_idx = gid.y;
    
    if (batch_idx >= params.batch_size || existing_idx >= params.total_nodes) {
        return;
    }
    
    // Calculate vector offsets
    let batch_vec_offset = (params.batch_offset + batch_idx) * params.dimension;
    let existing_vec_offset = existing_idx * params.dimension;
    
    // Compute distance
    let distance = calculate_distance_direct(batch_vec_offset, existing_vec_offset);
    
    // Store in matrix (row-major: batch_idx * total_nodes + existing_idx)
    let matrix_idx = batch_idx * params.total_nodes + existing_idx;
    distance_matrix[matrix_idx] = distance;
}

// ============================================================================
// Neighbor Selection
// ============================================================================

/// Kernel 2: Select K nearest neighbors for each batch vector
/// Uses 1D dispatch: [batch_size]
@compute @workgroup_size(256)
fn select_batch_neighbors(@builtin(global_invocation_id) gid: vec3<u32>) {
    let batch_idx = gid.x;
    
    if (batch_idx >= params.batch_size) {
        return;
    }
    
    // Find ef_construction nearest neighbors from distance matrix
    let matrix_row_start = batch_idx * params.total_nodes;
    
    // Build candidate list (using insertion sort for top-k)
    var candidates: array<DistanceEntry, 200>; // ef_construction max
    var candidate_count: u32 = 0u;
    
    // Initialize candidates
    for (var i: u32 = 0u; i < params.ef_construction; i++) {
        candidates[i] = DistanceEntry(0u, 0u, 1e10, 0u);
    }
    
    // Scan all existing nodes and keep top ef_construction
    for (var i: u32 = 0u; i < params.total_nodes; i++) {
        let distance = distance_matrix[matrix_row_start + i];
        
        // Find insertion point
        var insert_idx: u32 = params.ef_construction;
        for (var j: u32 = 0u; j < params.ef_construction; j++) {
            if (distance < candidates[j].distance) {
                insert_idx = j;
                break;
            }
        }
        
        if (insert_idx < params.ef_construction) {
            // Shift candidates
            for (var j = params.ef_construction - 1u; j > insert_idx; j--) {
                candidates[j] = candidates[j - 1u];
            }
            
            // Insert new candidate
            candidates[insert_idx] = DistanceEntry(batch_idx, i, distance, 1u);
            candidate_count = min(candidate_count + 1u, params.ef_construction);
        }
    }
    
    // Select M best using heuristic (simplified: just take M closest)
    let m = select(params.m, params.m0, true); // Assume level 0 for batch
    let final_count = min(m, candidate_count);
    
    // Write selected neighbors
    let candidate_base = batch_idx * params.ef_construction;
    for (var i: u32 = 0u; i < final_count; i++) {
        batch_candidates[candidate_base + i] = candidates[i];
    }
    
    // Mark rest as invalid
    for (var i = final_count; i < params.ef_construction; i++) {
        batch_candidates[candidate_base + i] = DistanceEntry(0u, 0u, 0.0, 0u);
    }
}

// ============================================================================
// Connection Writing
// ============================================================================

/// Kernel 3: Write connections from batch candidates to graph
@compute @workgroup_size(256)
fn write_batch_connections(@builtin(global_invocation_id) gid: vec3<u32>) {
    let batch_idx = gid.x;
    
    if (batch_idx >= params.batch_size) {
        return;
    }
    
    // Calculate actual node index in full graph
    let node_idx = params.batch_offset + batch_idx;
    
    if (node_idx >= params.total_nodes + params.batch_size) {
        return;
    }
    
    var node = get_node_at(node_idx);
    let candidate_base = batch_idx * params.ef_construction;
    
    // Count valid candidates
    var connection_count: u32 = 0u;
    for (var i: u32 = 0u; i < params.ef_construction; i++) {
        let candidate = batch_candidates[candidate_base + i];
        if (bool(candidate.valid)) {
            connection_count++;
        } else {
            break;
        }
    }
    
    // Write connections
    node.connection_count = connection_count;
    set_node_at(node_idx, node);
    
    let m0 = params.m0;
    for (var i: u32 = 0u; i < connection_count && i < m0; i++) {
        let candidate = batch_candidates[candidate_base + i];
        if (bool(candidate.valid)) {
            // Write connection (level 0)
            let connection_offset = node_idx * m0 + i;
            connections[connection_offset] = candidate.existing_idx;
        }
    }
}

// ============================================================================
// Bidirectional Connection Update
// ============================================================================

/// Kernel 4: Add reverse connections for batch
@compute @workgroup_size(256)
fn update_reverse_connections(@builtin(global_invocation_id) gid: vec3<u32>) {
    let batch_idx = gid.x;
    
    if (batch_idx >= params.batch_size) {
        return;
    }
    
    let node_idx = params.batch_offset + batch_idx;
    let candidate_base = batch_idx * params.ef_construction;
    
    // For each neighbor of this batch node
    for (var i: u32 = 0u; i < params.ef_construction; i++) {
        let candidate = batch_candidates[candidate_base + i];
        
        if (!bool(candidate.valid)) {
            break;
        }
        
        let neighbor_idx = candidate.existing_idx;
        
        if (neighbor_idx >= params.total_nodes) {
            continue;
        }
        
        var neighbor = get_node_at(neighbor_idx);
        
        // Check if reverse connection already exists
        var has_reverse = false;
        let neighbor_m0 = params.m0;
        
        for (var j: u32 = 0u; j < neighbor.connection_count && j < neighbor_m0; j++) {
            let connection_offset = neighbor_idx * neighbor_m0 + j;
            if (connections[connection_offset] == node_idx) {
                has_reverse = true;
                break;
            }
        }
        
        // Add reverse connection if space available
        if (!has_reverse && neighbor.connection_count < neighbor_m0) {
            let connection_offset = neighbor_idx * neighbor_m0 + neighbor.connection_count;
            connections[connection_offset] = node_idx;
            
            neighbor.connection_count++;
            set_node_at(neighbor_idx, neighbor);
        }
    }
}

// ============================================================================
// Utility Kernels
// ============================================================================

/// Clear distance matrix for next batch
@compute @workgroup_size(256)
fn clear_distance_matrix(@builtin(global_invocation_id) gid: vec3<u32>) {
    let idx = gid.x;
    let total_entries = params.batch_size * params.total_nodes;
    
    if (idx < total_entries) {
        distance_matrix[idx] = 0.0;
    }
}

/// Clear candidate buffer
@compute @workgroup_size(256)
fn clear_candidates(@builtin(global_invocation_id) gid: vec3<u32>) {
    let idx = gid.x;
    let total_entries = params.batch_size * params.ef_construction;
    
    if (idx < total_entries) {
        batch_candidates[idx] = DistanceEntry(0u, 0u, 0.0, 0u);
    }
}