ruvector-onnx-embeddings 0.1.0

ONNX-based embedding generation for RuVector - Reimagined embedding pipeline in pure Rust
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
//! GPU Compute Shaders for RuVector Operations
//!
//! WGSL (WebGPU Shading Language) implementations for:
//! - Pooling operations
//! - Similarity computations
//! - Vector normalization
//! - Matrix operations

use std::collections::HashMap;

/// Shader registry for managing compute shaders
#[derive(Debug)]
pub struct ShaderRegistry {
    shaders: HashMap<String, ShaderModule>,
}

/// Shader module information
#[derive(Debug, Clone)]
pub struct ShaderModule {
    /// Shader name
    pub name: String,
    /// WGSL source code
    pub source: String,
    /// Entry point function
    pub entry_point: String,
    /// Default workgroup size
    pub workgroup_size: [u32; 3],
}

impl ShaderRegistry {
    /// Create new registry with built-in shaders
    pub fn new() -> Self {
        let mut shaders = HashMap::new();

        // Register all built-in shaders
        for shader in Self::builtin_shaders() {
            shaders.insert(shader.name.clone(), shader);
        }

        Self { shaders }
    }

    /// Get shader by name
    pub fn get(&self, name: &str) -> Option<&ShaderModule> {
        self.shaders.get(name)
    }

    /// Register custom shader
    pub fn register(&mut self, shader: ShaderModule) {
        self.shaders.insert(shader.name.clone(), shader);
    }

    /// List all available shaders
    pub fn list(&self) -> Vec<&str> {
        self.shaders.keys().map(|s| s.as_str()).collect()
    }

    /// Get built-in shader definitions
    fn builtin_shaders() -> Vec<ShaderModule> {
        vec![
            // Cosine Similarity
            ShaderModule {
                name: "cosine_similarity".to_string(),
                source: SHADER_COSINE_SIMILARITY.to_string(),
                entry_point: "cosine_similarity".to_string(),
                workgroup_size: [256, 1, 1],
            },
            // Batch Cosine Similarity
            ShaderModule {
                name: "batch_cosine_similarity".to_string(),
                source: SHADER_BATCH_COSINE_SIMILARITY.to_string(),
                entry_point: "batch_cosine_similarity".to_string(),
                workgroup_size: [256, 1, 1],
            },
            // Dot Product
            ShaderModule {
                name: "dot_product".to_string(),
                source: SHADER_DOT_PRODUCT.to_string(),
                entry_point: "dot_product".to_string(),
                workgroup_size: [256, 1, 1],
            },
            // Euclidean Distance
            ShaderModule {
                name: "euclidean_distance".to_string(),
                source: SHADER_EUCLIDEAN_DISTANCE.to_string(),
                entry_point: "euclidean_distance".to_string(),
                workgroup_size: [256, 1, 1],
            },
            // L2 Normalize
            ShaderModule {
                name: "l2_normalize".to_string(),
                source: SHADER_L2_NORMALIZE.to_string(),
                entry_point: "l2_normalize".to_string(),
                workgroup_size: [256, 1, 1],
            },
            // Mean Pooling
            ShaderModule {
                name: "mean_pool".to_string(),
                source: SHADER_MEAN_POOL.to_string(),
                entry_point: "mean_pool".to_string(),
                workgroup_size: [64, 1, 1],
            },
            // Max Pooling
            ShaderModule {
                name: "max_pool".to_string(),
                source: SHADER_MAX_POOL.to_string(),
                entry_point: "max_pool".to_string(),
                workgroup_size: [64, 1, 1],
            },
            // CLS Pooling
            ShaderModule {
                name: "cls_pool".to_string(),
                source: SHADER_CLS_POOL.to_string(),
                entry_point: "cls_pool".to_string(),
                workgroup_size: [64, 1, 1],
            },
            // Matrix-Vector Multiplication
            ShaderModule {
                name: "matmul".to_string(),
                source: SHADER_MATMUL.to_string(),
                entry_point: "matmul".to_string(),
                workgroup_size: [16, 16, 1],
            },
            // Vector Addition
            ShaderModule {
                name: "vector_add".to_string(),
                source: SHADER_VECTOR_ADD.to_string(),
                entry_point: "vector_add".to_string(),
                workgroup_size: [256, 1, 1],
            },
            // Vector Scale
            ShaderModule {
                name: "vector_scale".to_string(),
                source: SHADER_VECTOR_SCALE.to_string(),
                entry_point: "vector_scale".to_string(),
                workgroup_size: [256, 1, 1],
            },
        ]
    }
}

impl Default for ShaderRegistry {
    fn default() -> Self {
        Self::new()
    }
}

// ==================== Shader Source Code ====================

// Public aliases for operations.rs
pub const MEAN_POOL_SHADER: &str = SHADER_MEAN_POOL;
pub const MAX_POOL_SHADER: &str = SHADER_MAX_POOL;
pub const BATCH_COSINE_SIMILARITY_SHADER: &str = SHADER_BATCH_COSINE_SIMILARITY;
pub const DOT_PRODUCT_SHADER: &str = SHADER_DOT_PRODUCT;
pub const EUCLIDEAN_DISTANCE_SHADER: &str = SHADER_EUCLIDEAN_DISTANCE;
pub const L2_NORMALIZE_SHADER: &str = SHADER_L2_NORMALIZE;
pub const MATMUL_SHADER: &str = SHADER_MATMUL;
pub const VECTOR_ADD_SHADER: &str = SHADER_VECTOR_ADD;

/// Cosine similarity between two vectors
pub const SHADER_COSINE_SIMILARITY: &str = r#"
struct Params {
    dimension: u32,
    count: u32,
}

@group(0) @binding(0) var<storage, read> query: array<f32>;
@group(0) @binding(1) var<storage, read> candidate: array<f32>;
@group(0) @binding(2) var<storage, read_write> result: array<f32>;
@group(0) @binding(3) var<uniform> params: Params;

var<workgroup> shared_dot: array<f32, 256>;
var<workgroup> shared_norm_a: array<f32, 256>;
var<workgroup> shared_norm_b: array<f32, 256>;

@compute @workgroup_size(256)
fn cosine_similarity(@builtin(global_invocation_id) gid: vec3<u32>,
                     @builtin(local_invocation_id) lid: vec3<u32>) {
    let idx = gid.x;
    let local_idx = lid.x;

    var dot: f32 = 0.0;
    var norm_a: f32 = 0.0;
    var norm_b: f32 = 0.0;

    // Compute partial sums
    var i = local_idx;
    while (i < params.dimension) {
        let a = query[i];
        let b = candidate[i];
        dot += a * b;
        norm_a += a * a;
        norm_b += b * b;
        i += 256u;
    }

    // Store in shared memory
    shared_dot[local_idx] = dot;
    shared_norm_a[local_idx] = norm_a;
    shared_norm_b[local_idx] = norm_b;
    workgroupBarrier();

    // Reduction
    for (var stride = 128u; stride > 0u; stride >>= 1u) {
        if (local_idx < stride) {
            shared_dot[local_idx] += shared_dot[local_idx + stride];
            shared_norm_a[local_idx] += shared_norm_a[local_idx + stride];
            shared_norm_b[local_idx] += shared_norm_b[local_idx + stride];
        }
        workgroupBarrier();
    }

    // Write result
    if (local_idx == 0u) {
        let norm_product = sqrt(shared_norm_a[0] * shared_norm_b[0]);
        if (norm_product > 1e-12) {
            result[0] = shared_dot[0] / norm_product;
        } else {
            result[0] = 0.0;
        }
    }
}
"#;

/// Batch cosine similarity - one query vs many candidates
pub const SHADER_BATCH_COSINE_SIMILARITY: &str = r#"
struct Params {
    dimension: u32,
    num_candidates: u32,
}

@group(0) @binding(0) var<storage, read> query: array<f32>;
@group(0) @binding(1) var<storage, read> candidates: array<f32>;
@group(0) @binding(2) var<storage, read_write> results: array<f32>;
@group(0) @binding(3) var<uniform> params: Params;

@compute @workgroup_size(256)
fn batch_cosine_similarity(@builtin(global_invocation_id) gid: vec3<u32>) {
    let candidate_idx = gid.x;

    if (candidate_idx >= params.num_candidates) {
        return;
    }

    let base = candidate_idx * params.dimension;

    var dot: f32 = 0.0;
    var norm_a: f32 = 0.0;
    var norm_b: f32 = 0.0;

    for (var i = 0u; i < params.dimension; i++) {
        let a = query[i];
        let b = candidates[base + i];
        dot += a * b;
        norm_a += a * a;
        norm_b += b * b;
    }

    let norm_product = sqrt(norm_a * norm_b);
    if (norm_product > 1e-12) {
        results[candidate_idx] = dot / norm_product;
    } else {
        results[candidate_idx] = 0.0;
    }
}
"#;

/// Dot product computation
pub const SHADER_DOT_PRODUCT: &str = r#"
struct Params {
    dimension: u32,
    num_candidates: u32,
}

@group(0) @binding(0) var<storage, read> query: array<f32>;
@group(0) @binding(1) var<storage, read> candidates: array<f32>;
@group(0) @binding(2) var<storage, read_write> results: array<f32>;
@group(0) @binding(3) var<uniform> params: Params;

@compute @workgroup_size(256)
fn dot_product(@builtin(global_invocation_id) gid: vec3<u32>) {
    let candidate_idx = gid.x;

    if (candidate_idx >= params.num_candidates) {
        return;
    }

    let base = candidate_idx * params.dimension;

    var dot: f32 = 0.0;
    for (var i = 0u; i < params.dimension; i++) {
        dot += query[i] * candidates[base + i];
    }

    results[candidate_idx] = dot;
}
"#;

/// Euclidean distance computation
pub const SHADER_EUCLIDEAN_DISTANCE: &str = r#"
struct Params {
    dimension: u32,
    num_candidates: u32,
}

@group(0) @binding(0) var<storage, read> query: array<f32>;
@group(0) @binding(1) var<storage, read> candidates: array<f32>;
@group(0) @binding(2) var<storage, read_write> results: array<f32>;
@group(0) @binding(3) var<uniform> params: Params;

@compute @workgroup_size(256)
fn euclidean_distance(@builtin(global_invocation_id) gid: vec3<u32>) {
    let candidate_idx = gid.x;

    if (candidate_idx >= params.num_candidates) {
        return;
    }

    let base = candidate_idx * params.dimension;

    var sum_sq: f32 = 0.0;
    for (var i = 0u; i < params.dimension; i++) {
        let diff = query[i] - candidates[base + i];
        sum_sq += diff * diff;
    }

    results[candidate_idx] = sqrt(sum_sq);
}
"#;

/// L2 normalization
pub const SHADER_L2_NORMALIZE: &str = r#"
struct Params {
    dimension: u32,
    num_vectors: u32,
}

@group(0) @binding(0) var<storage, read> input_vectors: array<f32>;
@group(0) @binding(1) var<storage, read> _dummy: array<f32>;
@group(0) @binding(2) var<storage, read_write> output_vectors: array<f32>;
@group(0) @binding(3) var<uniform> params: Params;

@compute @workgroup_size(256)
fn l2_normalize(@builtin(global_invocation_id) gid: vec3<u32>) {
    let vec_idx = gid.x;

    if (vec_idx >= params.num_vectors) {
        return;
    }

    let base = vec_idx * params.dimension;

    // Compute norm
    var norm_sq: f32 = 0.0;
    for (var i = 0u; i < params.dimension; i++) {
        let val = input_vectors[base + i];
        norm_sq += val * val;
    }

    let norm = sqrt(norm_sq);

    // Normalize and write to output
    if (norm > 1e-12) {
        for (var i = 0u; i < params.dimension; i++) {
            output_vectors[base + i] = input_vectors[base + i] / norm;
        }
    } else {
        for (var i = 0u; i < params.dimension; i++) {
            output_vectors[base + i] = input_vectors[base + i];
        }
    }
}
"#;

/// Mean pooling over sequence
pub const SHADER_MEAN_POOL: &str = r#"
struct Params {
    batch_size: u32,
    seq_length: u32,
    hidden_size: u32,
}

@group(0) @binding(0) var<storage, read> tokens: array<f32>;
@group(0) @binding(1) var<storage, read> attention_mask: array<i32>;
@group(0) @binding(2) var<storage, read_write> output: array<f32>;
@group(0) @binding(3) var<uniform> params: Params;

@compute @workgroup_size(64)
fn mean_pool(@builtin(global_invocation_id) gid: vec3<u32>) {
    let batch_idx = gid.x / params.hidden_size;
    let hidden_idx = gid.x % params.hidden_size;

    if (batch_idx >= params.batch_size) {
        return;
    }

    let tokens_base = batch_idx * params.seq_length * params.hidden_size;
    let mask_base = batch_idx * params.seq_length;

    var sum: f32 = 0.0;
    var count: f32 = 0.0;

    for (var i = 0u; i < params.seq_length; i++) {
        if (attention_mask[mask_base + i] == 1) {
            sum += tokens[tokens_base + i * params.hidden_size + hidden_idx];
            count += 1.0;
        }
    }

    let out_idx = batch_idx * params.hidden_size + hidden_idx;
    if (count > 0.0) {
        output[out_idx] = sum / count;
    } else {
        output[out_idx] = 0.0;
    }
}
"#;

/// Max pooling over sequence
pub const SHADER_MAX_POOL: &str = r#"
struct Params {
    batch_size: u32,
    seq_length: u32,
    hidden_size: u32,
}

@group(0) @binding(0) var<storage, read> tokens: array<f32>;
@group(0) @binding(1) var<storage, read> attention_mask: array<i32>;
@group(0) @binding(2) var<storage, read_write> output: array<f32>;
@group(0) @binding(3) var<uniform> params: Params;

@compute @workgroup_size(64)
fn max_pool(@builtin(global_invocation_id) gid: vec3<u32>) {
    let batch_idx = gid.x / params.hidden_size;
    let hidden_idx = gid.x % params.hidden_size;

    if (batch_idx >= params.batch_size) {
        return;
    }

    let tokens_base = batch_idx * params.seq_length * params.hidden_size;
    let mask_base = batch_idx * params.seq_length;

    var max_val: f32 = -3.402823e+38; // -FLT_MAX
    var found: bool = false;

    for (var i = 0u; i < params.seq_length; i++) {
        if (attention_mask[mask_base + i] == 1) {
            let val = tokens[tokens_base + i * params.hidden_size + hidden_idx];
            if (!found || val > max_val) {
                max_val = val;
                found = true;
            }
        }
    }

    let out_idx = batch_idx * params.hidden_size + hidden_idx;
    output[out_idx] = select(0.0, max_val, found);
}
"#;

/// CLS token pooling (first token)
pub const SHADER_CLS_POOL: &str = r#"
struct Params {
    batch_size: u32,
    seq_length: u32,
    hidden_size: u32,
}

@group(0) @binding(0) var<storage, read> tokens: array<f32>;
@group(0) @binding(1) var<storage, read> _dummy: array<f32>;
@group(0) @binding(2) var<storage, read_write> output: array<f32>;
@group(0) @binding(3) var<uniform> params: Params;

@compute @workgroup_size(64)
fn cls_pool(@builtin(global_invocation_id) gid: vec3<u32>) {
    let batch_idx = gid.x / params.hidden_size;
    let hidden_idx = gid.x % params.hidden_size;

    if (batch_idx >= params.batch_size) {
        return;
    }

    // CLS is first token
    let tokens_base = batch_idx * params.seq_length * params.hidden_size;
    let out_idx = batch_idx * params.hidden_size + hidden_idx;

    output[out_idx] = tokens[tokens_base + hidden_idx];
}
"#;

/// Matrix-vector multiplication
pub const SHADER_MATMUL: &str = r#"
struct Params {
    rows: u32,
    cols: u32,
}

@group(0) @binding(0) var<storage, read> matrix: array<f32>;
@group(0) @binding(1) var<storage, read> vector: array<f32>;
@group(0) @binding(2) var<storage, read_write> result: array<f32>;
@group(0) @binding(3) var<uniform> params: Params;

@compute @workgroup_size(16, 16)
fn matmul(@builtin(global_invocation_id) gid: vec3<u32>) {
    let row = gid.x;

    if (row >= params.rows) {
        return;
    }

    var sum: f32 = 0.0;
    for (var col = 0u; col < params.cols; col++) {
        sum += matrix[row * params.cols + col] * vector[col];
    }

    result[row] = sum;
}
"#;

/// Vector addition
pub const SHADER_VECTOR_ADD: &str = r#"
struct Params {
    length: u32,
}

@group(0) @binding(0) var<storage, read> a: array<f32>;
@group(0) @binding(1) var<storage, read> b: array<f32>;
@group(0) @binding(2) var<storage, read_write> result: array<f32>;
@group(0) @binding(3) var<uniform> params: Params;

@compute @workgroup_size(256)
fn vector_add(@builtin(global_invocation_id) gid: vec3<u32>) {
    let idx = gid.x;

    if (idx >= params.length) {
        return;
    }

    result[idx] = a[idx] + b[idx];
}
"#;

/// Vector scaling
pub const SHADER_VECTOR_SCALE: &str = r#"
struct Params {
    length: u32,
    scale: f32,
}

@group(0) @binding(0) var<storage, read> input_vector: array<f32>;
@group(0) @binding(1) var<storage, read> _dummy: array<f32>;
@group(0) @binding(2) var<storage, read_write> output_vector: array<f32>;
@group(0) @binding(3) var<uniform> params: Params;

@compute @workgroup_size(256)
fn vector_scale(@builtin(global_invocation_id) gid: vec3<u32>) {
    let idx = gid.x;

    if (idx >= params.length) {
        return;
    }

    output_vector[idx] = input_vector[idx] * params.scale;
}
"#;

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

    #[test]
    fn test_shader_registry() {
        let registry = ShaderRegistry::new();

        // Check all built-in shaders are registered
        assert!(registry.get("cosine_similarity").is_some());
        assert!(registry.get("batch_cosine_similarity").is_some());
        assert!(registry.get("dot_product").is_some());
        assert!(registry.get("euclidean_distance").is_some());
        assert!(registry.get("l2_normalize").is_some());
        assert!(registry.get("mean_pool").is_some());
        assert!(registry.get("max_pool").is_some());
        assert!(registry.get("cls_pool").is_some());
        assert!(registry.get("matmul").is_some());
        assert!(registry.get("vector_add").is_some());
        assert!(registry.get("vector_scale").is_some());
    }

    #[test]
    fn test_shader_content() {
        let registry = ShaderRegistry::new();

        let cosine = registry.get("cosine_similarity").unwrap();
        assert!(cosine.source.contains("@compute"));
        assert!(cosine.source.contains("workgroup_size"));
        assert_eq!(cosine.entry_point, "cosine_similarity");
    }

    #[test]
    fn test_custom_shader() {
        let mut registry = ShaderRegistry::new();

        registry.register(ShaderModule {
            name: "custom_op".to_string(),
            source: "// custom shader".to_string(),
            entry_point: "custom".to_string(),
            workgroup_size: [128, 1, 1],
        });

        assert!(registry.get("custom_op").is_some());
    }
}