rusting_brain 5.0.0

A Rust deep-learning library for training and running transformer language models, dense and mixture-of-experts, on CPU or CUDA.
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
//! Device cross-attention: one token set attending to another.
//!
//! [`crate::gpu_model`] holds a whole decoder layer on the device, but the
//! graph it drives is fixed — embedding, sixteen identical blocks, logits. The
//! image-to-3D models need a different one: a flow transformer whose
//! blocks alternate self-attention over latent tokens with cross-attention to
//! a frozen image encoder's output, and a shape decoder whose queries are
//! points in space. This module is the piece that graph does not already have.
//!
//! It is built from [`Gpu`]'s primitives rather than from new kernels. The
//! only kernel change cross-attention needed was a flag: the causal softmax,
//! its backward and the log-sum-exp rebuild now take `causal`, and clearing it
//! gives exactly the unmasked row softmax two independent sequence lengths
//! want.
//!
//! # What differs from self-attention
//!
//! - Queries and keys come from **different buffers with different widths**.
//!   ViT-B tokens are 768 wide and the denoiser reading them is 640.
//! - There are **two lengths**. `q_len` queries per sequence attend to all
//!   `kv_len` keys of the same sequence.
//! - **No rotary positions and no mask.** A latent token set has no order, and
//!   every query sees every key.
//!
//! # Precision
//!
//! Under mixed precision, with a head width the fused kernels in
//! [`crate::cuda_flash`] take, every activation and weight here is BF16 and
//! attention is those kernels with the keys and values read from a second
//! buffer. Otherwise everything is FP32 and attention is three kernels around
//! a score matrix: one batched GEMM per head, the softmax, one more GEMM per
//! head. A profile of the shape VAE at batch 4 put the three-kernel path,
//! softmax and per-head GEMMs together, at about 40% of the step.

// The flow transformer block is what calls this; until that block exists the
// only caller is the parity test below, and a `cargo build` would otherwise
// report the whole module as dead.
#![allow(dead_code)]

use crate::attention::MultiHeadAttention;
use crate::cuda_training::cuda_err;
use crate::gpu_model::{Act, FlashShape, Gpu};
use crate::gpu_transformer::{
    gemm_lhs_transposed_batched, gemm_plain_batched, gemm_rhs_transposed_batched,
};
use crate::network::NetworkError;
use cudarc::driver::{CudaSlice, CudaView};

/// Where one cross-attention's queries, keys and values live, and how they are
/// grouped into sequences.
#[derive(Clone, Copy, Debug)]
pub(crate) struct CrossShape {
    /// Queries per sequence.
    pub(crate) q_len: usize,
    /// Keys and values per sequence.
    pub(crate) kv_len: usize,
    pub(crate) sequences: usize,
}

impl CrossShape {
    fn q_rows(&self) -> usize {
        self.q_len * self.sequences
    }

    fn kv_rows(&self) -> usize {
        self.kv_len * self.sequences
    }
}

/// What [`backward`] needs from [`forward`].
///
/// The probability matrix is not here: the forward pass keeps one log-sum-exp
/// per query and the backward pass rebuilds the probabilities from it, the
/// same trade [`crate::gpu_model`] makes for self-attention.
pub(crate) struct CrossCache {
    queries_in: Act,
    kv_in: Act,
    /// The query projection's output, `[q_rows, heads * head_dim]`.
    queries: Act,
    /// Key and value in one buffer, two slices of every row, because they read
    /// the same input and are one GEMM against their weights packed together.
    kv: Act,
    query_weight: std::sync::Arc<Act>,
    kv_weights: std::sync::Arc<Act>,
    output_weight: std::sync::Arc<Act>,
    merged: Act,
    log_sum_exp: CudaSlice<f32>,
    shape: CrossShape,
}

/// `attention.output(merge(softmax(Q Kᵀ / √d) V))`, with Q from `queries_in`
/// and K, V from `kv_in`.
///
/// Both inputs are FP32 and row-major: `queries_in` is `[q_rows, d_model]` and
/// `kv_in` is `[kv_rows, kv_features]`. The result is `[q_rows, d_model]`.
///
/// This is [`MultiHeadAttention::forward_train_cross`] on the device, and the
/// parity test at the bottom of this file holds the two to 1e-4.
pub(crate) fn forward(
    gpu: &Gpu<'_>,
    attention: &MultiHeadAttention,
    queries_in: &CudaView<'_, f32>,
    kv_in: &CudaView<'_, f32>,
    shape: CrossShape,
) -> Result<(CudaSlice<f32>, CrossCache), NetworkError> {
    let plan = Plan::new(attention, shape)?;
    let (q_rows, kv_rows) = (shape.q_rows(), shape.kv_rows());

    // The fused kernels read and write BF16 only, and the batched GEMMs of
    // the fallback read FP32 only, so one choice sets every buffer's width.
    let fused = gpu
        .context
        .flash
        .as_ref()
        .filter(|_| crate::cuda_flash::eligible(gpu.context.mixed_precision, plan.head_dim));
    let narrow = fused.is_some();
    let queries_in = gpu.narrowed(queries_in, q_rows * plan.d_model, narrow)?;
    let kv_in = gpu.narrowed(kv_in, kv_rows * plan.d_kv, narrow)?;

    let query_weight = gpu.pack(&[&attention.query], narrow)?;
    let kv_weights = gpu.pack(&[&attention.key, &attention.value], narrow)?;
    let output_weight = gpu.pack(&[&attention.output], narrow)?;

    let mut queries = gpu.act(q_rows * plan.query_width, narrow)?;
    gpu.linear_packed_act(
        &query_weight.all(),
        plan.query_width,
        plan.d_model,
        &queries_in.all(),
        narrow,
        &mut queries,
        q_rows,
    )?;

    // Key and value share `kv_in` and differ only in nothing at all, so one
    // GEMM at twice the width produces both.
    let mut kv = gpu.act(kv_rows * plan.kv_stride, narrow)?;
    gpu.linear_packed_act(
        &kv_weights.all(),
        plan.kv_stride,
        plan.d_kv,
        &kv_in.all(),
        narrow,
        &mut kv,
        kv_rows,
    )?;

    let mut log_sum_exp = gpu.uninit(plan.heads * q_rows)?;
    let mut merged = gpu.act(q_rows * plan.query_width, narrow)?;
    if let Some(flash) = fused {
        gpu.flash_attention(
            flash,
            &queries,
            &kv,
            &mut merged,
            &mut log_sum_exp,
            plan.flash_shape(shape),
            plan.scale,
        )?;
    } else {
        let mut probabilities = gpu.uninit(plan.heads * shape.sequences * plan.block_size)?;
        plan.scores(gpu, &queries, &kv, &mut probabilities, shape, plan.scale)?;
        gpu.softmax_lse(
            &mut probabilities,
            &mut log_sum_exp,
            plan.heads * shape.sequences * shape.q_len,
            shape.kv_len,
            false,
        )?;

        let kv_view = kv.wide();
        let mut wide_merged = merged.wide_mut();
        for head in 0..plan.heads {
            let head_probabilities =
                probabilities.slice(head * shape.sequences * plan.block_size..);
            let value = kv_view.slice(plan.value_base + plan.kv_base(head)..);
            let mut head_merged = wide_merged.slice_mut(head * plan.head_dim..);
            gemm_plain_batched(
                gpu.context,
                &head_probabilities,
                shape.kv_len,
                &value,
                plan.kv_stride,
                &mut head_merged,
                plan.query_width,
                shape.q_len,
                plan.head_dim,
                shape.kv_len,
                1.0,
                0.0,
                shape.sequences,
                plan.block_size,
                shape.kv_len * plan.kv_stride,
                shape.q_len * plan.query_width,
            )?;
        }
    }

    let mut output = gpu.uninit(q_rows * plan.d_model)?;
    gpu.linear_packed(
        &output_weight.all(),
        plan.d_model,
        plan.query_width,
        &merged.all(),
        narrow,
        &mut output,
        q_rows,
        0.0,
    )?;

    Ok((
        output,
        CrossCache {
            queries_in,
            kv_in,
            queries,
            kv,
            query_weight,
            kv_weights,
            output_weight,
            merged,
            log_sum_exp,
            shape,
        },
    ))
}

/// Accumulates the four projections' gradients and returns
/// `(dL/dqueries_in, dL/dkv_in)`.
///
/// The second is what a caller drops when the key side is a frozen encoder.
/// Computing it anyway costs one GEMM and keeps the trainable case working.
pub(crate) fn backward(
    gpu: &Gpu<'_>,
    attention: &mut MultiHeadAttention,
    cache: &CrossCache,
    grad_output: &CudaSlice<f32>,
) -> Result<(CudaSlice<f32>, CudaSlice<f32>), NetworkError> {
    let shape = cache.shape;
    let plan = Plan::new(attention, shape)?;
    let (q_rows, kv_rows) = (shape.q_rows(), shape.kv_rows());

    // The forward pass chose the path, and left its choice in every buffer.
    let narrow = cache.queries.is_narrow();
    let grad_output = gpu.narrowed(&grad_output.slice(..), q_rows * plan.d_model, narrow)?;
    let mut grad_merged = gpu.act(q_rows * plan.query_width, narrow)?;
    gpu.linear_packed_backward_input(
        &cache.output_weight.all(),
        plan.d_model,
        plan.query_width,
        &grad_output.all(),
        narrow,
        narrow,
        grad_merged.destination(),
        q_rows,
        0.0,
    )?;
    gpu.accumulate_projection_grad(
        &mut attention.output,
        &grad_output.all(),
        &cache.merged.all(),
        narrow,
        q_rows,
    )?;

    let mut grad_queries = gpu.act(q_rows * plan.query_width, narrow)?;
    let mut grad_kv = gpu.act(kv_rows * plan.kv_stride, narrow)?;
    if narrow {
        let flash = gpu
            .context
            .flash
            .as_ref()
            .expect("the forward pass ran fused");
        let mut delta = gpu.uninit(plan.heads * q_rows)?;
        // Every column of both gradients is written, not accumulated: the
        // query tiles write the queries', the key tiles the keys' and values'.
        gpu.flash_attention_backward(
            flash,
            &cache.queries,
            &cache.kv,
            &cache.merged,
            &grad_merged,
            &cache.log_sum_exp,
            &mut delta,
            &grad_queries,
            &grad_kv,
            plan.flash_shape(shape),
            plan.scale,
        )?;
    } else {
        // The forward pass kept only the log-sum-exp, so the probabilities are
        // rebuilt here and live for this backward pass alone.
        let mut probabilities = gpu.uninit(plan.heads * shape.sequences * plan.block_size)?;
        plan.scores(
            gpu,
            &cache.queries,
            &cache.kv,
            &mut probabilities,
            shape,
            plan.scale,
        )?;
        gpu.probs_from_lse(
            &mut probabilities,
            &cache.log_sum_exp,
            plan.heads * shape.sequences * shape.q_len,
            shape.kv_len,
            false,
        )?;

        let mut grad_scores = gpu.uninit(plan.heads * shape.sequences * plan.block_size)?;
        {
            // Every query head writes its own slice of `grad_queries` and of
            // `grad_scores`, but the query heads of a group all accumulate into
            // one key and value head, so that buffer starts at zero.
            let mut wide_grad_kv = grad_kv.wide_mut();
            gpu.context
                .stream
                .memset_zeros(&mut wide_grad_kv)
                .map_err(cuda_err("gradient clear"))?;

            let kv_view = cache.kv.wide();
            let queries_view = cache.queries.wide();
            let grad_merged_view = grad_merged.wide();
            let mut wide_grad_queries = grad_queries.wide_mut();

            for head in 0..plan.heads {
                let kv_base = plan.kv_base(head);
                let upstream = grad_merged_view.slice(head * plan.head_dim..);
                let value = kv_view.slice(plan.value_base + kv_base..);
                let mut scores = grad_scores.slice_mut(head * shape.sequences * plan.block_size..);
                gemm_rhs_transposed_batched(
                    gpu.context,
                    &upstream,
                    plan.query_width,
                    &value,
                    plan.kv_stride,
                    &mut scores,
                    shape.kv_len,
                    shape.q_len,
                    shape.kv_len,
                    plan.head_dim,
                    1.0,
                    0.0,
                    shape.sequences,
                    shape.q_len * plan.query_width,
                    shape.kv_len * plan.kv_stride,
                    plan.block_size,
                )?;

                let head_probabilities =
                    probabilities.slice(head * shape.sequences * plan.block_size..);
                let mut grad_value = wide_grad_kv.slice_mut(plan.value_base + kv_base..);
                gemm_lhs_transposed_batched(
                    gpu.context,
                    &head_probabilities,
                    shape.kv_len,
                    &upstream,
                    plan.query_width,
                    &mut grad_value,
                    plan.kv_stride,
                    shape.q_len,
                    shape.kv_len,
                    plan.head_dim,
                    1.0,
                    1.0,
                    shape.sequences,
                    plan.block_size,
                    shape.q_len * plan.query_width,
                    shape.kv_len * plan.kv_stride,
                )?;
            }

            gpu.softmax_backward(
                &mut grad_scores,
                &probabilities,
                plan.heads * shape.sequences * shape.q_len,
                shape.kv_len,
                false,
            )?;

            for head in 0..plan.heads {
                let kv_base = plan.kv_base(head);
                let scores = grad_scores.slice(head * shape.sequences * plan.block_size..);
                let key = kv_view.slice(plan.key_base + kv_base..);
                let mut grad_query = wide_grad_queries.slice_mut(head * plan.head_dim..);
                gemm_plain_batched(
                    gpu.context,
                    &scores,
                    shape.kv_len,
                    &key,
                    plan.kv_stride,
                    &mut grad_query,
                    plan.query_width,
                    shape.q_len,
                    plan.head_dim,
                    shape.kv_len,
                    plan.scale,
                    0.0,
                    shape.sequences,
                    plan.block_size,
                    shape.kv_len * plan.kv_stride,
                    shape.q_len * plan.query_width,
                )?;

                let query = queries_view.slice(head * plan.head_dim..);
                let mut grad_key = wide_grad_kv.slice_mut(plan.key_base + kv_base..);
                gemm_lhs_transposed_batched(
                    gpu.context,
                    &scores,
                    shape.kv_len,
                    &query,
                    plan.query_width,
                    &mut grad_key,
                    plan.kv_stride,
                    shape.q_len,
                    shape.kv_len,
                    plan.head_dim,
                    plan.scale,
                    1.0,
                    shape.sequences,
                    plan.block_size,
                    shape.q_len * plan.query_width,
                    shape.kv_len * plan.kv_stride,
                )?;
            }
        }
    }

    let mut grad_queries_in = gpu.uninit(q_rows * plan.d_model)?;
    gpu.linear_packed_backward_input(
        &cache.query_weight.all(),
        plan.query_width,
        plan.d_model,
        &grad_queries.all(),
        narrow,
        false,
        &mut grad_queries_in,
        q_rows,
        0.0,
    )?;
    gpu.accumulate_packed_projection_grad(
        &mut [&mut attention.query],
        &grad_queries.all(),
        &cache.queries_in.all(),
        narrow,
        q_rows,
    )?;

    let mut grad_kv_in = gpu.uninit(kv_rows * plan.d_kv)?;
    gpu.linear_packed_backward_input(
        &cache.kv_weights.all(),
        plan.kv_stride,
        plan.d_kv,
        &grad_kv.all(),
        narrow,
        false,
        &mut grad_kv_in,
        kv_rows,
        0.0,
    )?;
    gpu.accumulate_packed_projection_grad(
        &mut [&mut attention.key, &mut attention.value],
        &grad_kv.all(),
        &cache.kv_in.all(),
        narrow,
        kv_rows,
    )?;

    Ok((grad_queries_in, grad_kv_in))
}

/// The widths and offsets both passes derive from the layer, in one place so
/// the two cannot disagree about where a head's keys start.
struct Plan {
    d_model: usize,
    d_kv: usize,
    heads: usize,
    group: usize,
    head_dim: usize,
    query_width: usize,
    /// Key and value packed: the stride of one row of the fused `kv` buffer.
    kv_stride: usize,
    key_base: usize,
    value_base: usize,
    block_size: usize,
    scale: f32,
}

impl Plan {
    fn new(attention: &MultiHeadAttention, shape: CrossShape) -> Result<Self, NetworkError> {
        if shape.q_len == 0 || shape.kv_len == 0 || shape.sequences == 0 {
            return Err(NetworkError::InvalidConfig(format!(
                "cross-attention needs a non-empty shape, not {} x {} over {} sequences",
                shape.q_len, shape.kv_len, shape.sequences
            )));
        }
        if attention.is_rope_enabled() || attention.is_causal() {
            return Err(NetworkError::InvalidConfig(
                "device cross-attention wants a layer built with `MultiHeadAttention::cross`: \
                 rotary positions and the causal mask both have to be off"
                    .into(),
            ));
        }
        let head_dim = attention.head_dim();
        let kv_width = attention.num_kv_heads() * head_dim;
        Ok(Self {
            d_model: attention.d_model(),
            d_kv: attention.kv_features(),
            heads: attention.num_heads(),
            group: attention.num_heads() / attention.num_kv_heads(),
            head_dim,
            query_width: attention.num_heads() * head_dim,
            kv_stride: 2 * kv_width,
            key_base: 0,
            value_base: kv_width,
            block_size: shape.q_len * shape.kv_len,
            scale: (head_dim as f32).sqrt().recip(),
        })
    }

    /// Where head `head`'s key and value columns start inside their half of a
    /// fused `kv` row. Grouped-query heads share one.
    fn kv_base(&self, head: usize) -> usize {
        (head / self.group) * self.head_dim
    }

    /// Where the fused kernels find the queries in their own buffer and the
    /// keys and values in the packed one.
    fn flash_shape(&self, shape: CrossShape) -> FlashShape {
        FlashShape {
            rows: shape.q_rows(),
            seq_len: shape.q_len,
            kv_len: shape.kv_len,
            sequences: shape.sequences,
            heads: self.heads,
            group: self.group,
            qkv_width: self.query_width,
            kv_width: self.kv_stride,
            query_width: self.query_width,
            key_base: self.key_base,
            value_base: self.value_base,
            causal: false,
        }
    }

    /// `out = alpha * Q Káµ€`, one batched GEMM per query head.
    ///
    /// Both passes need the same scores, so they share this rather than
    /// repeating eight arguments that have to agree.
    fn scores(
        &self,
        gpu: &Gpu<'_>,
        queries: &Act,
        kv: &Act,
        out: &mut CudaSlice<f32>,
        shape: CrossShape,
        alpha: f32,
    ) -> Result<(), NetworkError> {
        let queries = queries.wide();
        let kv = kv.wide();
        for head in 0..self.heads {
            let query = queries.slice(head * self.head_dim..);
            let key = kv.slice(self.key_base + self.kv_base(head)..);
            let mut head_scores = out.slice_mut(head * shape.sequences * self.block_size..);
            gemm_rhs_transposed_batched(
                gpu.context,
                &query,
                self.query_width,
                &key,
                self.kv_stride,
                &mut head_scores,
                shape.kv_len,
                shape.q_len,
                shape.kv_len,
                self.head_dim,
                alpha,
                0.0,
                shape.sequences,
                shape.q_len * self.query_width,
                shape.kv_len * self.kv_stride,
                self.block_size,
            )?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cuda_training::cuda_doctor;
    use crate::gpu_transformer::GpuContext;
    use crate::matrix::Matrix;
    use crate::rope::Rope;
    use rand::SeedableRng;
    use rand::rngs::StdRng;
    use std::sync::Arc;

    /// Same contract as the other CUDA tests: no device means the parity test
    /// reports success without running, a broken device is a failure.
    fn cuda_or_skip() -> Option<Arc<GpuContext>> {
        match cuda_doctor(0, 8192) {
            Ok(_) => Some(GpuContext::new(0).expect("the CUDA doctor passed")),
            Err(NetworkError::Cuda(message))
                if message.contains("NO_DEVICE") || message.contains("no CUDA-capable device") =>
            {
                crate::cuda_training::skip_without_cuda();
                None
            }
            Err(error) => panic!("CUDA is present but the CUDA doctor failed: {error}"),
        }
    }

    fn ramp(rows: usize, cols: usize, offset: usize) -> Matrix {
        Matrix::from_vec(
            rows,
            cols,
            (0..rows * cols)
                .map(|i| (((i + offset) * 37) % 23) as f32 / 11.0 - 1.0)
                .collect(),
        )
    }

    fn assert_close(label: &str, device: &[f32], host: &[f32], tolerance: f32) {
        assert_eq!(device.len(), host.len(), "{label}: length");
        let largest = host.iter().fold(1f32, |m, v| m.max(v.abs()));
        let tolerance = tolerance * largest;
        for (index, (a, b)) in device.iter().zip(host).enumerate() {
            assert!(
                (a - b).abs() <= tolerance,
                "{label}[{index}]: {a} on the device vs {b} on the host"
            );
        }
    }

    /// The whole point of this module: the device path and
    /// [`MultiHeadAttention::forward_train_cross`] have to agree, output and
    /// gradients alike. Grouped heads, because sharing one key head between
    /// two query heads is where the batched GEMM strides are easiest to get
    /// wrong.
    #[test]
    fn cross_attention_matches_the_host_or_skips_without_device() {
        let Some(context) = cuda_or_skip() else {
            return;
        };
        parity(&context, (8, 6, 4, 2, 4), (3, 5, 2), 1e-4);
    }

    /// The fused path, which only a mixed-precision context with a 64- or
    /// 128-wide head takes. Both lengths are past one 64-row tile and neither
    /// is a multiple of it, so every kernel crosses a tile edge and masks a
    /// ragged tail, on the query side and the key side separately. BF16
    /// operands put the worst error at 0.47% of the largest value; a forward
    /// kernel that stops one key tile short is off by 0.93%.
    #[test]
    fn fused_cross_attention_matches_the_host_or_skips_without_device() {
        if cuda_or_skip().is_none() {
            return;
        }
        let context = GpuContext::with_precision(0, true).unwrap();
        // More key tiles than query tiles, then fewer, so no loop bound
        // can take the wrong length and still cover the same tiles.
        for lengths in [(70, 150, 2), (150, 70, 2)] {
            parity(&context, (48, 40, 4, 2, 64), lengths, 6e-3);
        }
    }

    /// Runs one layer both ways and holds every output and gradient to
    /// `tolerance` times the largest host value, or to `tolerance` when that
    /// is below one.
    fn parity(
        context: &Arc<GpuContext>,
        (d_model, d_kv, heads, kv_heads, head_dim): (usize, usize, usize, usize, usize),
        (q_len, kv_len, sequences): (usize, usize, usize),
        tolerance: f32,
    ) {
        let gpu = Gpu { context };
        let shape = CrossShape {
            q_len,
            kv_len,
            sequences,
        };

        let mut rng = StdRng::seed_from_u64(7);
        let rope = Rope::new(head_dim, 64, 10_000.0).unwrap();
        let mut host =
            MultiHeadAttention::cross(d_model, d_kv, heads, kv_heads, head_dim, rope, &mut rng)
                .unwrap();
        let mut device = host.clone();

        let queries_in = ramp(q_len * sequences, d_model, 0);
        let kv_in = ramp(kv_len * sequences, d_kv, 5);
        let grad_output = ramp(q_len * sequences, d_model, 11);

        let (host_output, host_cache) = host
            .forward_train_cross(&queries_in, &kv_in, q_len, kv_len)
            .unwrap();
        let (host_grad_queries, host_grad_kv) =
            host.backward_cross(&host_cache, &grad_output).unwrap();

        for param in device.params_mut() {
            param.move_to_cuda(context).unwrap();
        }
        let device_queries_in = gpu.upload(&queries_in.data).unwrap();
        let device_kv_in = gpu.upload(&kv_in.data).unwrap();
        let device_grad_output = gpu.upload(&grad_output.data).unwrap();

        let (output, cache) = forward(
            &gpu,
            &device,
            &device_queries_in.slice(..),
            &device_kv_in.slice(..),
            shape,
        )
        .expect("the device forward pass");
        assert_close(
            "cross forward",
            &gpu.download(&output).unwrap(),
            &host_output.data,
            tolerance,
        );

        let (grad_queries, grad_kv) = backward(&gpu, &mut device, &cache, &device_grad_output)
            .expect("the device backward pass");
        assert_close(
            "cross grad_queries_in",
            &gpu.download(&grad_queries).unwrap(),
            &host_grad_queries.data,
            tolerance,
        );
        assert_close(
            "cross grad_kv_in",
            &gpu.download(&grad_kv).unwrap(),
            &host_grad_kv.data,
            tolerance,
        );

        // The weight gradients live on the device until they are asked for.
        for param in device.params_mut() {
            param.move_to_cpu().unwrap();
        }
        for (name, device, host) in [
            ("query", &device.query, &host.query),
            ("key", &device.key, &host.key),
            ("value", &device.value, &host.value),
            ("output", &device.output, &host.output),
        ] {
            assert_close(
                &format!("cross grad_{name}"),
                &device.weight.grad.data,
                &host.weight.grad.data,
                tolerance,
            );
        }
    }

    /// A self-attention layer reaching this path would silently lose its mask
    /// and its rotary positions, so it is refused instead.
    #[test]
    fn a_self_attention_layer_is_refused_or_skips_without_device() {
        let Some(context) = cuda_or_skip() else {
            return;
        };
        let gpu = Gpu { context: &context };
        let mut rng = StdRng::seed_from_u64(1);
        let rope = Rope::new(4, 64, 10_000.0).unwrap();
        let layer = MultiHeadAttention::new(8, 2, 2, 4, rope, &mut rng).unwrap();

        let queries = gpu.zeros(2 * 8).unwrap();
        let keys = gpu.zeros(2 * 8).unwrap();
        let shape = CrossShape {
            q_len: 2,
            kv_len: 2,
            sequences: 1,
        };
        assert!(forward(&gpu, &layer, &queries.slice(..), &keys.slice(..), shape).is_err());
    }
}