boostr 0.2.0

ML framework built on numr - attention, quantization, model architectures
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
use super::*;
use crate::test_utils::cpu_setup;

#[test]
fn test_varlen_fwd_shape() {
    let (client, dev) = cpu_setup();

    // 2 sequences: [3, 2] tokens, 2 heads, head_dim=4
    let total_q = 5;
    let num_heads = 2;
    let head_dim = 4;

    let q_data = vec![0.1f32; total_q * num_heads * head_dim];
    let k_data = vec![0.1f32; total_q * num_heads * head_dim];
    let v_data = vec![0.2f32; total_q * num_heads * head_dim];

    let q = Tensor::<CpuRuntime>::from_slice(&q_data, &[total_q, num_heads, head_dim], &dev);
    let k = Tensor::<CpuRuntime>::from_slice(&k_data, &[total_q, num_heads, head_dim], &dev);
    let v = Tensor::<CpuRuntime>::from_slice(&v_data, &[total_q, num_heads, head_dim], &dev);

    let cu_seqlens = vec![0i32, 3, 5];
    let cu = Tensor::<CpuRuntime>::from_slice(&cu_seqlens, &[3], &dev);

    let (out, lse) = client
        .varlen_attention_fwd(
            &q, &k, &v, &cu, &cu, 2, num_heads, num_heads, 3, 3, head_dim, false,
        )
        .unwrap();

    assert_eq!(out.shape(), &[total_q, num_heads, head_dim]);
    assert_eq!(lse.shape(), &[total_q, num_heads]);
}

#[test]
fn test_varlen_fwd_causal() {
    let (client, dev) = cpu_setup();

    // Single sequence of 4 tokens, 1 head, head_dim=2
    let total_q = 4;
    let num_heads = 1;
    let head_dim = 2;

    let q_data: Vec<f32> = (0..total_q * num_heads * head_dim)
        .map(|i| (i as f32) * 0.1 + 0.1)
        .collect();
    let k_data = q_data.clone();
    let v_data: Vec<f32> = (0..total_q * num_heads * head_dim)
        .map(|i| (i as f32) * 0.05)
        .collect();

    let q = Tensor::<CpuRuntime>::from_slice(&q_data, &[total_q, num_heads, head_dim], &dev);
    let k = Tensor::<CpuRuntime>::from_slice(&k_data, &[total_q, num_heads, head_dim], &dev);
    let v = Tensor::<CpuRuntime>::from_slice(&v_data, &[total_q, num_heads, head_dim], &dev);

    let cu_seqlens = vec![0i32, 4];
    let cu = Tensor::<CpuRuntime>::from_slice(&cu_seqlens, &[2], &dev);

    let (out_causal, _) = client
        .varlen_attention_fwd(
            &q, &k, &v, &cu, &cu, 1, num_heads, num_heads, 4, 4, head_dim, true,
        )
        .unwrap();
    let (out_full, _) = client
        .varlen_attention_fwd(
            &q, &k, &v, &cu, &cu, 1, num_heads, num_heads, 4, 4, head_dim, false,
        )
        .unwrap();

    let causal_data = out_causal.to_vec::<f32>();
    let full_data = out_full.to_vec::<f32>();

    // Last token: causal sees all 4, non-causal sees all 4 → same
    let last_off = (total_q - 1) * num_heads * head_dim;
    for d in 0..head_dim {
        assert!(
            (causal_data[last_off + d] - full_data[last_off + d]).abs() < 1e-5,
            "Last token should match between causal and non-causal"
        );
    }

    // Second token (idx=1): causal sees [0,1], non-causal sees [0,1,2,3] → different
    let second_off = num_heads * head_dim;
    let differs = (0..head_dim)
        .any(|d| (causal_data[second_off + d] - full_data[second_off + d]).abs() > 1e-6);
    assert!(
        differs,
        "Middle tokens should differ between causal and non-causal"
    );
}

#[test]
fn test_varlen_bwd_shapes() {
    let (client, dev) = cpu_setup();

    let total_q = 5;
    let num_heads = 2;
    let head_dim = 4;

    let n = total_q * num_heads * head_dim;
    let q_data: Vec<f32> = (0..n).map(|i| (i as f32 * 0.3).sin()).collect();
    let k_data: Vec<f32> = (0..n).map(|i| (i as f32 * 0.7).cos()).collect();
    let v_data: Vec<f32> = (0..n).map(|i| (i as f32 * 0.5 + 1.0).sin()).collect();

    let q = Tensor::<CpuRuntime>::from_slice(&q_data, &[total_q, num_heads, head_dim], &dev);
    let k = Tensor::<CpuRuntime>::from_slice(&k_data, &[total_q, num_heads, head_dim], &dev);
    let v = Tensor::<CpuRuntime>::from_slice(&v_data, &[total_q, num_heads, head_dim], &dev);

    let cu_seqlens = vec![0i32, 3, 5];
    let cu = Tensor::<CpuRuntime>::from_slice(&cu_seqlens, &[3], &dev);

    let (out, lse) = client
        .varlen_attention_fwd(
            &q, &k, &v, &cu, &cu, 2, num_heads, num_heads, 3, 3, head_dim, false,
        )
        .unwrap();

    let do_data: Vec<f32> = (0..n).map(|i| (i as f32 * 0.2).cos() * 0.1).collect();
    let dout = Tensor::<CpuRuntime>::from_slice(&do_data, &[total_q, num_heads, head_dim], &dev);

    let (dq, dk, dv) = client
        .varlen_attention_bwd(
            &dout, &q, &k, &v, &out, &lse, &cu, &cu, 2, num_heads, num_heads, 3, 3, head_dim, false,
        )
        .unwrap();

    assert_eq!(dq.shape(), &[total_q, num_heads, head_dim]);
    assert_eq!(dk.shape(), &[total_q, num_heads, head_dim]);
    assert_eq!(dv.shape(), &[total_q, num_heads, head_dim]);

    // Gradients should be non-zero
    let dq_data = dq.to_vec::<f32>();
    let has_nonzero = dq_data.iter().any(|&x: &f32| x.abs() > 1e-10);
    assert!(has_nonzero, "dQ should have non-zero gradients");
}

/// GQA backward equivalence:
///   - Run varlen_attention_bwd with GQA (num_kv_heads=2, num_heads=8, hd=64).
///   - Run varlen_attention_bwd with K/V expanded to 8 heads (each kv head
///     repeated 4×) → MHA reference.
///   - Assert dq == dq_exp (within 1e-4).
///   - Assert dk[:,kv_h,:] == sum over 4 q-heads mapping to kv_h of dk_exp[:,q_h,:]
///     (within 1e-4).  Same for dv.
#[test]
fn test_varlen_bwd_gqa_equals_expanded_mha() {
    let (client, dev) = cpu_setup();

    let num_heads = 8usize;
    let num_kv_heads = 2usize;
    let gqa_ratio = num_heads / num_kv_heads; // 4
    let head_dim = 64usize;

    // Two sequences: lengths 3 and 5
    let total_tokens = 8usize;
    let batch_size = 2usize;

    let n_q = total_tokens * num_heads * head_dim;
    let n_kv = total_tokens * num_kv_heads * head_dim;

    // Deterministic inputs
    let q_data: Vec<f32> = (0..n_q).map(|i| ((i as f32) * 0.13).sin() * 0.3).collect();
    let k_data: Vec<f32> = (0..n_kv).map(|i| ((i as f32) * 0.07).cos() * 0.2).collect();
    let v_data: Vec<f32> = (0..n_kv)
        .map(|i| ((i as f32) * 0.17).sin() * 0.25)
        .collect();

    // Expand K and V to full num_heads
    let mut k_expanded = vec![0.0f32; total_tokens * num_heads * head_dim];
    let mut v_expanded = vec![0.0f32; total_tokens * num_heads * head_dim];
    for tok in 0..total_tokens {
        for kv_h in 0..num_kv_heads {
            for rep in 0..gqa_ratio {
                let q_h = kv_h * gqa_ratio + rep;
                let src_base = (tok * num_kv_heads + kv_h) * head_dim;
                let dst_base = (tok * num_heads + q_h) * head_dim;
                k_expanded[dst_base..dst_base + head_dim]
                    .copy_from_slice(&k_data[src_base..src_base + head_dim]);
                v_expanded[dst_base..dst_base + head_dim]
                    .copy_from_slice(&v_data[src_base..src_base + head_dim]);
            }
        }
    }

    let cu_seqlens = vec![0i32, 3, 8];
    let max_seqlen = 5usize;

    let do_data: Vec<f32> = (0..n_q).map(|i| ((i as f32) * 0.11).cos() * 0.1).collect();

    let q = Tensor::<CpuRuntime>::from_slice(&q_data, &[total_tokens, num_heads, head_dim], &dev);
    let k_gqa =
        Tensor::<CpuRuntime>::from_slice(&k_data, &[total_tokens, num_kv_heads, head_dim], &dev);
    let v_gqa =
        Tensor::<CpuRuntime>::from_slice(&v_data, &[total_tokens, num_kv_heads, head_dim], &dev);
    let k_exp =
        Tensor::<CpuRuntime>::from_slice(&k_expanded, &[total_tokens, num_heads, head_dim], &dev);
    let v_exp =
        Tensor::<CpuRuntime>::from_slice(&v_expanded, &[total_tokens, num_heads, head_dim], &dev);
    let dout =
        Tensor::<CpuRuntime>::from_slice(&do_data, &[total_tokens, num_heads, head_dim], &dev);
    let cu = Tensor::<CpuRuntime>::from_slice(&cu_seqlens, &[batch_size + 1], &dev);

    // --- GQA fwd + bwd ---
    let (out_gqa, lse_gqa) = client
        .varlen_attention_fwd(
            &q,
            &k_gqa,
            &v_gqa,
            &cu,
            &cu,
            batch_size,
            num_heads,
            num_kv_heads,
            max_seqlen,
            max_seqlen,
            head_dim,
            false,
        )
        .unwrap();
    let (dq_gqa, dk_gqa, dv_gqa) = client
        .varlen_attention_bwd(
            &dout,
            &q,
            &k_gqa,
            &v_gqa,
            &out_gqa,
            &lse_gqa,
            &cu,
            &cu,
            batch_size,
            num_heads,
            num_kv_heads,
            max_seqlen,
            max_seqlen,
            head_dim,
            false,
        )
        .unwrap();

    // --- Expanded MHA fwd + bwd ---
    let (out_exp, lse_exp) = client
        .varlen_attention_fwd(
            &q, &k_exp, &v_exp, &cu, &cu, batch_size, num_heads, num_heads, max_seqlen, max_seqlen,
            head_dim, false,
        )
        .unwrap();
    let (dq_exp, dk_exp, dv_exp) = client
        .varlen_attention_bwd(
            &dout, &q, &k_exp, &v_exp, &out_exp, &lse_exp, &cu, &cu, batch_size, num_heads,
            num_heads, max_seqlen, max_seqlen, head_dim, false,
        )
        .unwrap();

    let dq_g = dq_gqa.to_vec::<f32>();
    let dq_e = dq_exp.to_vec::<f32>();
    let dk_g = dk_gqa.to_vec::<f32>(); // [total_tokens, num_kv_heads, head_dim]
    let dk_e = dk_exp.to_vec::<f32>(); // [total_tokens, num_heads,    head_dim]
    let dv_g = dv_gqa.to_vec::<f32>();
    let dv_e = dv_exp.to_vec::<f32>();

    // 1. dQ must match exactly
    assert_eq!(dq_g.len(), dq_e.len(), "dq length mismatch");
    for (i, (&a, &b)) in dq_g.iter().zip(dq_e.iter()).enumerate() {
        assert!(
            (a - b).abs() < 1e-4,
            "dQ mismatch at index {i}: gqa={a}, exp={b}, diff={}",
            (a - b).abs()
        );
    }

    // 2. dk_gqa[:,kv_h,:] == sum over the gqa_ratio Q-heads that map to kv_h
    //    of dk_exp[:,q_h,:].
    for tok in 0..total_tokens {
        for kv_h in 0..num_kv_heads {
            for d in 0..head_dim {
                let gqa_val = dk_g[(tok * num_kv_heads + kv_h) * head_dim + d];
                let mut exp_sum = 0.0f32;
                for rep in 0..gqa_ratio {
                    let q_h = kv_h * gqa_ratio + rep;
                    exp_sum += dk_e[(tok * num_heads + q_h) * head_dim + d];
                }
                assert!(
                    (gqa_val - exp_sum).abs() < 1e-4,
                    "dK scatter mismatch tok={tok} kv_h={kv_h} d={d}: gqa={gqa_val}, exp_sum={exp_sum}"
                );
            }
        }
    }

    // 3. Same check for dV
    for tok in 0..total_tokens {
        for kv_h in 0..num_kv_heads {
            for d in 0..head_dim {
                let gqa_val = dv_g[(tok * num_kv_heads + kv_h) * head_dim + d];
                let mut exp_sum = 0.0f32;
                for rep in 0..gqa_ratio {
                    let q_h = kv_h * gqa_ratio + rep;
                    exp_sum += dv_e[(tok * num_heads + q_h) * head_dim + d];
                }
                assert!(
                    (gqa_val - exp_sum).abs() < 1e-4,
                    "dV scatter mismatch tok={tok} kv_h={kv_h} d={d}: gqa={gqa_val}, exp_sum={exp_sum}"
                );
            }
        }
    }
}

/// GQA equivalence: varlen fwd with GQA (num_kv_heads=2, num_heads=8) must
/// produce the same output as MHA with K/V expanded by repeating each kv head
/// (num_heads / num_kv_heads) = 4 times along the head axis.
#[test]
fn test_varlen_gqa_equals_expanded_mha() {
    let (client, dev) = cpu_setup();

    let num_heads = 8usize;
    let num_kv_heads = 2usize;
    let gqa_ratio = num_heads / num_kv_heads; // 4
    let head_dim = 64usize;

    // Two sequences: lengths 3 and 5
    let total_tokens = 8usize;
    let batch_size = 2usize;

    let n_q = total_tokens * num_heads * head_dim;
    let n_kv = total_tokens * num_kv_heads * head_dim;

    // Deterministic inputs
    let q_data: Vec<f32> = (0..n_q).map(|i| ((i as f32) * 0.13).sin() * 0.3).collect();
    let k_data: Vec<f32> = (0..n_kv).map(|i| ((i as f32) * 0.07).cos() * 0.2).collect();
    let v_data: Vec<f32> = (0..n_kv)
        .map(|i| ((i as f32) * 0.17).sin() * 0.25)
        .collect();

    // Expand K and V: [total_tokens, num_kv_heads, head_dim] →
    //                 [total_tokens, num_heads, head_dim]
    // Each kv head is repeated gqa_ratio times consecutively.
    let mut k_expanded = vec![0.0f32; total_tokens * num_heads * head_dim];
    let mut v_expanded = vec![0.0f32; total_tokens * num_heads * head_dim];
    for tok in 0..total_tokens {
        for kv_h in 0..num_kv_heads {
            for rep in 0..gqa_ratio {
                let q_h = kv_h * gqa_ratio + rep;
                let src_base = (tok * num_kv_heads + kv_h) * head_dim;
                let dst_base = (tok * num_heads + q_h) * head_dim;
                k_expanded[dst_base..dst_base + head_dim]
                    .copy_from_slice(&k_data[src_base..src_base + head_dim]);
                v_expanded[dst_base..dst_base + head_dim]
                    .copy_from_slice(&v_data[src_base..src_base + head_dim]);
            }
        }
    }

    let cu_seqlens = vec![0i32, 3, 8];
    let max_seqlen = 5usize;

    let q = Tensor::<CpuRuntime>::from_slice(&q_data, &[total_tokens, num_heads, head_dim], &dev);
    let k_gqa =
        Tensor::<CpuRuntime>::from_slice(&k_data, &[total_tokens, num_kv_heads, head_dim], &dev);
    let v_gqa =
        Tensor::<CpuRuntime>::from_slice(&v_data, &[total_tokens, num_kv_heads, head_dim], &dev);
    let k_exp =
        Tensor::<CpuRuntime>::from_slice(&k_expanded, &[total_tokens, num_heads, head_dim], &dev);
    let v_exp =
        Tensor::<CpuRuntime>::from_slice(&v_expanded, &[total_tokens, num_heads, head_dim], &dev);
    let cu = Tensor::<CpuRuntime>::from_slice(&cu_seqlens, &[batch_size + 1], &dev);

    // Reference: MHA with expanded K/V (num_kv_heads == num_heads)
    let (out_ref, _) = client
        .varlen_attention_fwd(
            &q, &k_exp, &v_exp, &cu, &cu, batch_size, num_heads, num_heads, max_seqlen, max_seqlen,
            head_dim, false,
        )
        .unwrap();

    // Under test: GQA with packed K/V (num_kv_heads < num_heads)
    let (out_gqa, _) = client
        .varlen_attention_fwd(
            &q,
            &k_gqa,
            &v_gqa,
            &cu,
            &cu,
            batch_size,
            num_heads,
            num_kv_heads,
            max_seqlen,
            max_seqlen,
            head_dim,
            false,
        )
        .unwrap();

    let ref_vec = out_ref.to_vec::<f32>();
    let gqa_vec = out_gqa.to_vec::<f32>();

    assert_eq!(ref_vec.len(), gqa_vec.len(), "output length mismatch");
    for (i, (&r, &g)) in ref_vec.iter().zip(gqa_vec.iter()).enumerate() {
        assert!(
            (r - g).abs() < 1e-4,
            "GQA vs expanded-MHA mismatch at index {i}: ref={r}, gqa={g}, diff={}",
            (r - g).abs()
        );
    }
}