vyre-libs 0.7.2

vyre Category A library ecosystem - pure-IR compositions over foundation IR and primitive-owned kernels
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
//! FlashAttention-2 tiling  -  sequence-tiled online-softmax attention.
//!
//! Two builders are provided:
//!
//! - [`flash_attention_2`]  -  tiled variant. Each invocation handles one
//!   query row. The KV sequence is processed in tiles of `tile_size`;
//!   scores for a whole tile are computed first, then the online-softmax
//!   state `(m, l, o_acc)` is updated once per tile.
//!
//! - [`flash_attention_2_reference`]  -  scalar per-row online-softmax
//!   without tiling. Used as a parity oracle.
//!
//! Category-A composition.

use vyre_foundation::ir::{BufferAccess, BufferDecl, DataType, Expr, Node, Program, UnOp};

use super::planner::plan_flash_attention_tiled;
use crate::region::wrap_anonymous;
use vyre_primitives::nn::attention_stability::{
    bounded_exp_arg, bounded_score, flush_tiny, positive_denominator,
};

const OP_ID: &str = "vyre-libs::nn::flash_attention_2";
const REFERENCE_OP_ID: &str = "vyre-libs::nn::flash_attention_2_reference";

/// Build a Program that computes FlashAttention-2 with explicit
/// sequence tiling.
///
/// Each invocation handles one query row.  The KV sequence is iterated
/// in tiles of `tile_size`.  For every tile all scores are computed
/// first, then the row-level online-softmax accumulator `(m, l, o_acc)`
/// is updated in one batched step.
///
/// # Parameters
///
/// * `tile_size`  -  number of keys processed per tile (e.g. 64 or 128).
///
/// # Errors
///
/// Returns a trap program when `seq_len == 0`, `head_dim == 0` or
/// `tile_size == 0`.
#[must_use]
pub fn flash_attention_2(
    q: &str,
    k: &str,
    v: &str,
    out: &str,
    seq_len: u32,
    head_dim: u32,
    tile_size: u32,
) -> Program {
    if seq_len == 0 || head_dim == 0 || tile_size == 0 {
        return crate::builder::invalid_builder_trap_program(
            OP_ID,
            out,
            DataType::F32,
            "Fix: flash_attention_2 seq_len, head_dim, and tile_size must all be > 0".to_string(),
        );
    }
    let plan = match plan_flash_attention_tiled(seq_len, head_dim, tile_size) {
        Ok(plan) => plan,
        Err(error) => {
            return crate::builder::invalid_builder_trap_program(OP_ID, out, DataType::F32, error);
        }
    };
    let scale_expr = Expr::f32(1.0f32 / (head_dim as f32).sqrt());
    let q_idx = |local: Expr, d: Expr| Expr::add(Expr::mul(local, Expr::u32(head_dim)), d);
    let score_idx = |local: Expr, j: Expr| Expr::add(Expr::mul(local, Expr::u32(tile_size)), j);
    let o_idx = |local: Expr, d: Expr| Expr::add(Expr::mul(local, Expr::u32(head_dim)), d);
    let compute_tile_scores = vec![Node::loop_for(
        "tile_j",
        Expr::u32(0),
        Expr::var("tile_len"),
        vec![
            Node::let_bind("dot_val", Expr::f32(0.0)),
            Node::loop_for(
                "score_d",
                Expr::u32(0),
                Expr::u32(head_dim),
                vec![Node::assign(
                    "dot_val",
                    Expr::add(
                        Expr::var("dot_val"),
                        Expr::mul(
                            Expr::load(
                                "q_scratch",
                                q_idx(Expr::var("local"), Expr::var("score_d")),
                            ),
                            Expr::load(
                                k,
                                Expr::add(
                                    Expr::mul(
                                        Expr::add(Expr::var("tile_start"), Expr::var("tile_j")),
                                        Expr::u32(head_dim),
                                    ),
                                    Expr::var("score_d"),
                                ),
                            ),
                        ),
                    ),
                )],
            ),
            Node::let_bind("raw_score", Expr::mul(Expr::var("dot_val"), scale_expr)),
            Node::let_bind("score", bounded_score(Expr::var("raw_score"))),
            Node::store(
                "score_tile",
                score_idx(Expr::var("local"), Expr::var("tile_j")),
                Expr::var("score"),
            ),
        ],
    )];
    let update_o_acc = vec![Node::loop_for(
        "out_d",
        Expr::u32(0),
        Expr::u32(head_dim),
        vec![
            Node::let_bind("weighted_v", Expr::f32(0.0)),
            Node::loop_for(
                "v_j",
                Expr::u32(0),
                Expr::var("tile_len"),
                vec![Node::assign(
                    "weighted_v",
                    Expr::add(
                        Expr::var("weighted_v"),
                        Expr::mul(
                            Expr::UnOp {
                                op: UnOp::Exp,
                                operand: Box::new(bounded_exp_arg(Expr::sub(
                                    Expr::load(
                                        "score_tile",
                                        score_idx(Expr::var("local"), Expr::var("v_j")),
                                    ),
                                    Expr::var("m_new"),
                                ))),
                            },
                            Expr::load(
                                v,
                                Expr::add(
                                    Expr::mul(
                                        Expr::add(Expr::var("tile_start"), Expr::var("v_j")),
                                        Expr::u32(head_dim),
                                    ),
                                    Expr::var("out_d"),
                                ),
                            ),
                        ),
                    ),
                )],
            ),
            Node::store(
                "o_acc",
                o_idx(Expr::var("local"), Expr::var("out_d")),
                Expr::add(
                    Expr::mul(
                        Expr::var("rescale"),
                        Expr::load("o_acc", o_idx(Expr::var("local"), Expr::var("out_d"))),
                    ),
                    Expr::var("weighted_v"),
                ),
            ),
        ],
    )];
    let body = super::tiled_online_softmax::tiled_online_softmax_body(
        super::tiled_online_softmax::TiledOnlineSoftmaxSpec {
            q,
            out,
            item_var: "row",
            item_count: seq_len,
            seq_len,
            head_dim,
            tile_size,
            tile_count: plan.tile_count,
        },
        compute_tile_scores,
        update_o_acc,
    );
    Program::wrapped(
        vec![
            BufferDecl::storage(q, 0, BufferAccess::ReadOnly, DataType::F32)
                .with_count(plan.logical_elements),
            BufferDecl::storage(k, 1, BufferAccess::ReadOnly, DataType::F32)
                .with_count(plan.logical_elements),
            BufferDecl::storage(v, 2, BufferAccess::ReadOnly, DataType::F32)
                .with_count(plan.logical_elements),
            BufferDecl::workgroup("q_scratch", plan.q_scratch_elements, DataType::F32),
            BufferDecl::workgroup("score_tile", plan.score_scratch_elements, DataType::F32),
            BufferDecl::workgroup("o_acc", plan.o_acc_scratch_elements, DataType::F32),
            BufferDecl::output(out, 3, DataType::F32).with_count(plan.logical_elements),
        ],
        [plan.workgroup_lanes, 1, 1],
        vec![wrap_anonymous(OP_ID, body)],
    )
}

/// Simple scalar online-softmax attention reference (no tiling).
///
/// One invocation per query row, workgroup size `[1, 1, 1]`.  This is
/// the standard flash-attention recurrence processed key-by-key.
#[must_use]
pub fn flash_attention_2_reference(
    q: &str,
    k: &str,
    v: &str,
    out: &str,
    seq_len: u32,
    head_dim: u32,
) -> Program {
    if seq_len == 0 || head_dim == 0 {
        return crate::builder::invalid_builder_trap_program(
            REFERENCE_OP_ID,
            out,
            DataType::F32,
            "Fix: flash_attention_2_reference seq_len and head_dim must be > 0".to_string(),
        );
    }

    let elements = match seq_len.checked_mul(head_dim) {
        Some(e) => e,
        None => {
            return crate::builder::invalid_builder_trap_program(
                REFERENCE_OP_ID,
                out,
                DataType::F32,
                "Fix: flash_attention_2_reference seq_len*head_dim overflows u32".to_string(),
            );
        }
    };

    let scale = 1.0f32 / (head_dim as f32).sqrt();
    let scale_expr = Expr::f32(scale);

    // Per-key online-softmax update.
    let key_body = vec![
        // score = scale * dot(Q[row,:], K[j,:])
        Node::let_bind("dot_val", Expr::f32(0.0)),
        Node::loop_for(
            "kd",
            Expr::u32(0),
            Expr::u32(head_dim),
            vec![Node::assign(
                "dot_val",
                Expr::add(
                    Expr::var("dot_val"),
                    Expr::mul(
                        Expr::load(
                            q,
                            Expr::add(
                                Expr::mul(Expr::var("row"), Expr::u32(head_dim)),
                                Expr::var("kd"),
                            ),
                        ),
                        Expr::load(
                            k,
                            Expr::add(
                                Expr::mul(Expr::var("j"), Expr::u32(head_dim)),
                                Expr::var("kd"),
                            ),
                        ),
                    ),
                ),
            )],
        ),
        Node::let_bind(
            "raw_score",
            Expr::mul(Expr::var("dot_val"), scale_expr.clone()),
        ),
        Node::let_bind("score", bounded_score(Expr::var("raw_score"))),
        // m_new = max(m, score)
        Node::let_bind(
            "m_new",
            Expr::select(
                Expr::gt(Expr::var("score"), Expr::var("m")),
                Expr::var("score"),
                Expr::var("m"),
            ),
        ),
        // rescale = exp(m - m_new)
        Node::let_bind(
            "rescale",
            Expr::UnOp {
                op: UnOp::Exp,
                operand: Box::new(bounded_exp_arg(Expr::sub(
                    Expr::var("m"),
                    Expr::var("m_new"),
                ))),
            },
        ),
        // prob = exp(score - m_new)
        Node::let_bind(
            "prob",
            Expr::UnOp {
                op: UnOp::Exp,
                operand: Box::new(bounded_exp_arg(Expr::sub(
                    Expr::var("score"),
                    Expr::var("m_new"),
                ))),
            },
        ),
        // l = rescale * l + prob
        Node::let_bind(
            "l_new",
            Expr::add(
                Expr::mul(Expr::var("rescale"), Expr::var("l")),
                Expr::var("prob"),
            ),
        ),
        // o[d] = rescale * o[d] + prob * V[j, d]
        Node::loop_for(
            "od",
            Expr::u32(0),
            Expr::u32(head_dim),
            vec![Node::store(
                "o_ref",
                Expr::add(
                    Expr::mul(Expr::var("row"), Expr::u32(head_dim)),
                    Expr::var("od"),
                ),
                Expr::add(
                    Expr::mul(
                        Expr::var("rescale"),
                        Expr::load(
                            "o_ref",
                            Expr::add(
                                Expr::mul(Expr::var("row"), Expr::u32(head_dim)),
                                Expr::var("od"),
                            ),
                        ),
                    ),
                    Expr::mul(
                        Expr::var("prob"),
                        Expr::load(
                            v,
                            Expr::add(
                                Expr::mul(Expr::var("j"), Expr::u32(head_dim)),
                                Expr::var("od"),
                            ),
                        ),
                    ),
                ),
            )],
        ),
        Node::assign("m", Expr::var("m_new")),
        Node::assign("l", Expr::var("l_new")),
    ];

    let per_row = vec![
        Node::let_bind("m", Expr::f32(f32::MIN)),
        Node::let_bind("l", Expr::f32(0.0)),
        // Zero o_ref for this row
        Node::loop_for(
            "init_d",
            Expr::u32(0),
            Expr::u32(head_dim),
            vec![Node::store(
                "o_ref",
                Expr::add(
                    Expr::mul(Expr::var("row"), Expr::u32(head_dim)),
                    Expr::var("init_d"),
                ),
                Expr::f32(0.0),
            )],
        ),
        Node::loop_for("j", Expr::u32(0), Expr::u32(seq_len), key_body),
        // Write final output
        Node::let_bind("denom", positive_denominator(Expr::var("l"))),
        Node::loop_for(
            "write_d",
            Expr::u32(0),
            Expr::u32(head_dim),
            vec![Node::store(
                out,
                Expr::add(
                    Expr::mul(Expr::var("row"), Expr::u32(head_dim)),
                    Expr::var("write_d"),
                ),
                flush_tiny(Expr::div(
                    Expr::load(
                        "o_ref",
                        Expr::add(
                            Expr::mul(Expr::var("row"), Expr::u32(head_dim)),
                            Expr::var("write_d"),
                        ),
                    ),
                    Expr::var("denom"),
                )),
            )],
        ),
    ];

    let body = vec![
        Node::let_bind("row", Expr::InvocationId { axis: 0 }),
        Node::if_then(Expr::lt(Expr::var("row"), Expr::u32(seq_len)), per_row),
    ];

    Program::wrapped(
        vec![
            BufferDecl::storage(q, 0, BufferAccess::ReadOnly, DataType::F32).with_count(elements),
            BufferDecl::storage(k, 1, BufferAccess::ReadOnly, DataType::F32).with_count(elements),
            BufferDecl::storage(v, 2, BufferAccess::ReadOnly, DataType::F32).with_count(elements),
            BufferDecl::workgroup("o_ref", elements, DataType::F32),
            BufferDecl::output(out, 3, DataType::F32).with_count(elements),
        ],
        [1, 1, 1],
        vec![wrap_anonymous(REFERENCE_OP_ID, body)],
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fixture_bytes::decode_f32;
    use crate::fixture_bytes::f32_bytes;
    use vyre_reference::value::Value;

    fn run_program(program: Program, q: &[f32], k: &[f32], v: &[f32]) -> Vec<f32> {
        let out_bytes = program
            .buffers()
            .iter()
            .find(|b| b.name() == "out")
            .map(|b| b.count() as usize * core::mem::size_of::<f32>())
            .expect("Fix: output buffer present");
        let outputs = vyre_reference::reference_eval(
            &program,
            &[
                Value::from(f32_bytes(q)),
                Value::from(f32_bytes(k)),
                Value::from(f32_bytes(v)),
                Value::from(vec![0u8; out_bytes]),
            ],
        )
        .expect("Fix: reference eval must succeed");
        decode_f32(&outputs[0].to_bytes())
    }

    /// Tiled FlashAttention-2 agrees with the scalar reference on a
    /// non-trivial random fixture.
    #[test]
    fn flash_attention_2_matches_reference() {
        let seq_len = 8_u32;
        let head_dim = 16_u32;
        let tile_size = 4_u32;
        let elements = (seq_len * head_dim) as usize;

        let q: Vec<f32> = (0..elements)
            .map(|i| ((i as f32) * 0.13).sin() - 0.5)
            .collect();
        let k: Vec<f32> = (0..elements)
            .map(|i| ((i as f32) * 0.07).cos() + 0.25)
            .collect();
        let v: Vec<f32> = (0..elements)
            .map(|i| ((i as f32) * 0.19).sin() * 2.0)
            .collect();

        let actual = run_program(
            flash_attention_2("q", "k", "v", "out", seq_len, head_dim, tile_size),
            &q,
            &k,
            &v,
        );
        let expected = run_program(
            flash_attention_2_reference("q", "k", "v", "out", seq_len, head_dim),
            &q,
            &k,
            &v,
        );

        assert_eq!(actual.len(), expected.len(), "output length must match");
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                (a - e).abs() <= 1.0e-3,
                "flash_attention_2 vs reference mismatch at index {i}: {a} != {e}"
            );
        }
    }

    /// Output shape is `[seq_len, head_dim]`.
    #[test]
    fn flash_attention_2_output_shape() {
        let seq_len = 5_u32;
        let head_dim = 7_u32;
        let tile_size = 3_u32;
        let elements = (seq_len * head_dim) as usize;

        let q = vec![1.0f32; elements];
        let k = vec![0.5f32; elements];
        let v = vec![2.0f32; elements];

        let out = run_program(
            flash_attention_2("q", "k", "v", "out", seq_len, head_dim, tile_size),
            &q,
            &k,
            &v,
        );
        assert_eq!(out.len(), elements);
    }

    /// Edge case: `seq_len == 1` degenerates to passing V through
    /// (softmax of a length-1 vector is `[1.0]`).
    #[test]
    fn flash_attention_2_seq_len_one() {
        let seq_len = 1_u32;
        let head_dim = 4_u32;
        let tile_size = 1_u32;
        let q = vec![1.0f32, 2.0, 3.0, 4.0];
        let k = vec![0.5f32, 1.5, 2.5, 3.5];
        let v = vec![10.0f32, 20.0, 30.0, 40.0];

        let actual = run_program(
            flash_attention_2("q", "k", "v", "out", seq_len, head_dim, tile_size),
            &q,
            &k,
            &v,
        );
        let expected = run_program(
            flash_attention_2_reference("q", "k", "v", "out", seq_len, head_dim),
            &q,
            &k,
            &v,
        );

        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                (a - e).abs() <= 1.0e-3,
                "seq_len=1 mismatch at {i}: {a} != {e}"
            );
        }
    }

    /// Edge case: `seq_len == tile_size`.
    #[test]
    fn flash_attention_2_seq_len_eq_tile_size() {
        let seq_len = 4_u32;
        let head_dim = 8_u32;
        let tile_size = 4_u32;
        let elements = (seq_len * head_dim) as usize;

        let q: Vec<f32> = (0..elements).map(|i| (i as f32) * 0.1).collect();
        let k: Vec<f32> = (0..elements).map(|i| (i as f32) * 0.05 + 0.2).collect();
        let v: Vec<f32> = (0..elements).map(|i| (i as f32) * 0.3 - 0.1).collect();

        let actual = run_program(
            flash_attention_2("q", "k", "v", "out", seq_len, head_dim, tile_size),
            &q,
            &k,
            &v,
        );
        let expected = run_program(
            flash_attention_2_reference("q", "k", "v", "out", seq_len, head_dim),
            &q,
            &k,
            &v,
        );

        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                (a - e).abs() <= 1.0e-3,
                "seq_len==tile_size mismatch at {i}: {a} != {e}"
            );
        }
    }

    /// Edge case: `seq_len == tile_size + 1`.
    #[test]
    fn flash_attention_2_seq_len_eq_tile_size_plus_one() {
        let seq_len = 5_u32;
        let head_dim = 8_u32;
        let tile_size = 4_u32;
        let elements = (seq_len * head_dim) as usize;

        let q: Vec<f32> = (0..elements).map(|i| (i as f32) * 0.11).collect();
        let k: Vec<f32> = (0..elements).map(|i| (i as f32) * 0.06 + 0.15).collect();
        let v: Vec<f32> = (0..elements).map(|i| (i as f32) * 0.25 - 0.05).collect();

        let actual = run_program(
            flash_attention_2("q", "k", "v", "out", seq_len, head_dim, tile_size),
            &q,
            &k,
            &v,
        );
        let expected = run_program(
            flash_attention_2_reference("q", "k", "v", "out", seq_len, head_dim),
            &q,
            &k,
            &v,
        );

        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                (a - e).abs() <= 1.0e-3,
                "seq_len==tile_size+1 mismatch at {i}: {a} != {e}"
            );
        }
    }
}