cubek-tile 0.3.0

CubeK: Tile
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
//! A separable procedural weight contracted over three axes: the rank the abstraction carries is
//! the recipe's, not the microkernel's.
//!
//! `out[row, col] = Σ_{k0, k1, k2} (∏_f factor_f(k_f, row)) · in[k0, k1, k2, col]`. The same
//! recipe is run twice, once stating its factorization and once opaque, so the cell-major walk is
//! checked against the general one as well as against the host.
#![allow(non_snake_case)]

use super::{Form, implied};
use cubecl::{ir::ElemType, prelude::*, zspace::shape};
use cubek_test_utils::{HostData, HostDataType, TestInput};
use cubek_tile::layout::PhysicalAxisMap;
use cubek_tile::procedural::AffineCoordinate;
use cubek_tile::procedural::DivGuard;
use cubek_tile::procedural::Factors;
use cubek_tile::procedural::Procedural;
use cubek_tile::procedural::Sum;
use cubek_tile::procedural::TapSupport;
use cubek_tile::procedural::affine_along;
use cubek_tile::procedural::sum_of;
use cubek_tile::*;
use cubek_tile::{kind::Boundary, launch::BoundaryPolicy};

const ROW: Axis = Axis(0);
const COL: Axis = Axis(1);
const TAP: [Axis; 3] = [Axis(2), Axis(3), Axis(4)];

/// The software instruction every leaf here runs under: a 16-cell budget, no edge split, no unit
/// fan-out.
const REGISTER_BLOCK: RegisterBlock = RegisterBlock::new(16);

const ROWS: usize = 2;
const COLS: usize = 3;
/// Deliberately unequal, so a factor read at the wrong offset lands on the wrong tap count.
const TAPS: [usize; 3] = [2, 3, 2];

/// `offset + coefficient * tap + row_coefficient * row`, per factor. Only the first factor reads
/// the output row, which is the shape a resampling weight takes: a factor may read a free axis,
/// it may only not read another factor's contracted axis.
const OFFSET: [f32; 3] = [1.0, 2.0, 3.0];
const COEFFICIENT: [f32; 3] = [1.0, 3.0, 5.0];
const ROW_COEFFICIENT: [f32; 3] = [2.0, 0.0, 0.0];

/// Every factor is the same type, which is what a [`Factors`] holds: one filter family
/// applied along each axis, at that axis's own coordinates.
type Factor<E> = Sum<AffineCoordinate<E>, AffineCoordinate<E>>;
type Weights<E> = Factors<Factor<E>>;

fn factor_value(f: usize, tap: usize, row: usize) -> f32 {
    OFFSET[f] + COEFFICIENT[f] * tap as f32 + ROW_COEFFICIENT[f] * row as f32
}

#[cube]
fn factor<E: Float>(#[comptime] f: usize) -> Factor<E> {
    sum_of(
        affine_along(
            comptime!(TAP[f]),
            E::new(comptime!(OFFSET[f])),
            E::new(comptime!(COEFFICIENT[f])),
        ),
        affine_along(ROW, E::new(0.0_f32), E::new(comptime!(ROW_COEFFICIENT[f]))),
    )
}

#[cube]
fn weights<E: Float>() -> Weights<E> {
    let mut factors = Sequence::new();
    #[unroll]
    for f in 0..comptime!(TAP.len()) {
        factors.push(factor::<E>(f));
    }
    Factors::new(factors)
}

#[cube(launch)]
fn separable_kernel<E: Float>(
    input: &TileArg<'_, E, Const<1>>,
    output: &TileArg<'_, E, Const<1>>,
    #[comptime] separable: bool,
    space: Partitioning,
    #[comptime] level: Level,
    #[define(E)] _dtype: ElemType,
) {
    let input = input.tile(comptime!(space.clone()));
    let weight_axes = comptime!([&[ROW], TAP.as_slice()].concat());
    let weights = if comptime!(separable) {
        Procedural::<E>::separable::<Weights<E>>(
            comptime!(space.space().subspace(&weight_axes)),
            weights::<E>(),
        )
        .tile()
    } else {
        Procedural::<E>::new::<Weights<E>>(
            comptime!(space.space().subspace(&weight_axes)),
            weights::<E>(),
        )
        .tile()
    };

    let output = output.tile(comptime!(space.clone()));
    for region in space.over(&level) {
        let mut out = output
            .at(&region)
            .accumulating(REGISTER_BLOCK, Semiring::SUM_PROD);
        out.mm(&weights.at(&region), &input.at(&region));
    }
}

/// [`separable_kernel`] with the input staged into shared memory, served in `width`-wide lines
/// where one is stated (the operand is scalar, the stage pads it out to whole lines).
#[cube(launch)]
fn separable_kernel_staged<E: Float>(
    input: &TileArg<'_, E, Const<1>>,
    output: &TileArg<'_, E, Const<1>>,
    #[comptime] width: Option<usize>,
    space: Partitioning,
    #[comptime] level: Level,
    #[define(E)] _dtype: ElemType,
) {
    let input = input.tile(comptime!(space.clone()));
    let weight_axes = comptime!([&[ROW], TAP.as_slice()].concat());
    let weights = Procedural::<E>::separable::<Weights<E>>(
        comptime!(space.space().subspace(&weight_axes)),
        weights::<E>(),
    )
    .tile();

    let output = output.tile(comptime!(space.clone()));
    let walk = space.over(&level);
    let mut stages = Stages::smem_single_at(&walk, &input, StageStorage::Strided, width, 1usize);
    stages.pipelined(walk, |slot, region| {
        let mut out = output
            .at(region)
            .accumulating(REGISTER_BLOCK, Semiring::SUM_PROD);
        let weights = weights.at(region);
        slot.consume(|input| {
            out.mm(&weights, input);
        });
    });
}

/// Small integers, so the accumulation is exact in `f32`.
fn ramp(n: usize) -> Vec<f32> {
    (0..n).map(|i| ((i % 7) as f32) - 2.0).collect()
}

fn reference(input: &[f32]) -> Vec<f32> {
    let mut out = vec![0.0f32; ROWS * COLS];
    for row in 0..ROWS {
        for col in 0..COLS {
            let mut acc = 0.0f32;
            for k0 in 0..TAPS[0] {
                for k1 in 0..TAPS[1] {
                    for k2 in 0..TAPS[2] {
                        let weight = factor_value(0, k0, row)
                            * factor_value(1, k1, row)
                            * factor_value(2, k2, row);
                        let at = ((k0 * TAPS[1] + k1) * TAPS[2] + k2) * COLS + col;
                        acc += weight * input[at];
                    }
                }
            }
            out[row * COLS + col] = acc;
        }
    }
    out
}

fn run(separable: bool) -> (HostData, Vec<f32>) {
    let client = cubecl::test_device().client();
    let f32_ty = f32::elem_type_native();

    let in_shape = shape![TAPS[0], TAPS[1], TAPS[2], COLS];
    let in_data = ramp(in_shape.num_elements());
    let (in_handle, _) = TestInput::builder(client.clone(), in_shape)
        .dtype(f32_ty)
        .custom(in_data.clone())
        .generate_with_f32_host_data();
    let out_handle = TestInput::builder(client.clone(), shape![ROWS, COLS])
        .dtype(f32_ty)
        .zeros()
        .generate_without_host_data();

    let launcher = implied(
        &client,
        Partitioning::new(
            Space::new(&[
                (ROW, ROWS),
                (COL, COLS),
                (TAP[0], TAPS[0]),
                (TAP[1], TAPS[1]),
                (TAP[2], TAPS[2]),
            ]),
            Levels::leaf(&[
                (ROW, ROWS),
                (COL, COLS),
                (TAP[0], TAPS[0]),
                (TAP[1], TAPS[1]),
                (TAP[2], TAPS[2]),
            ])
            .walk_every(&[ROW, COL, TAP[0], TAP[1], TAP[2]])
            .build(),
        ),
        Form::Static,
    );

    separable_kernel::launch(
        &client,
        launcher.cube_count(),
        launcher.cube_dim(),
        TileArgLaunch::new(
            in_handle.binding().into_tensor_arg(),
            TileSpec::direct(&[TAP[0], TAP[1], TAP[2], COL]),
        ),
        TileArgLaunch::new(
            out_handle.clone().binding().into_tensor_arg(),
            TileSpec::direct(&[ROW, COL]),
        ),
        separable,
        launcher.partitioning_arg(),
        launcher.partitioning().level(0),
        f32_ty,
    );

    (
        HostData::from_tensor_handle(&client, out_handle, HostDataType::F32),
        in_data,
    )
}

fn check(separable: bool) {
    let (got, input) = run(separable);
    let want = reference(&input);
    for row in 0..ROWS {
        for col in 0..COLS {
            let have = got.get_f32(&[row, col]);
            let want = want[row * COLS + col];
            assert!(
                (have - want).abs() < 1e-4,
                "separable {separable}: at ({row}, {col}) got {have}, want {want}"
            );
        }
    }
}

/// Three factors, walked one axis at a time: `2 + 3 + 2` evaluations per cell rather than
/// `2 * 3 * 2`, and the same values.
#[test]
fn a_rank_three_separable_product_contracts_factor_by_factor() {
    check(true);
}

/// The same recipe with its factorization withheld, contracted the general way. A recipe is the
/// product of its factors, so both paths must agree.
#[test]
fn an_opaque_product_contracts_to_the_same_values() {
    check(false);
}

#[test]
fn a_separable_lhs_contracts_a_padded_staged_rhs() {
    let client = cubecl::test_device().client();
    let f32_ty = f32::elem_type_native();

    let in_shape = shape![TAPS[0], TAPS[1], TAPS[2], COLS];
    let in_data = ramp(in_shape.num_elements());
    let (in_handle, _) = TestInput::builder(client.clone(), in_shape)
        .dtype(f32_ty)
        .custom(in_data.clone())
        .generate_with_f32_host_data();
    let out_handle = TestInput::builder(client.clone(), shape![ROWS, COLS])
        .dtype(f32_ty)
        .zeros()
        .generate_without_host_data();

    let launcher = implied(
        &client,
        Partitioning::new(
            Space::new(&[
                (ROW, ROWS),
                (COL, COLS),
                (TAP[0], TAPS[0]),
                (TAP[1], TAPS[1]),
                (TAP[2], TAPS[2]),
            ]),
            Levels::leaf(&[
                (ROW, ROWS),
                (COL, COLS),
                (TAP[0], TAPS[0]),
                (TAP[1], TAPS[1]),
                (TAP[2], TAPS[2]),
            ])
            .walk_every(&[ROW, COL, TAP[0], TAP[1], TAP[2]])
            .build(),
        ),
        Form::Static,
    );

    let in_spec = TileSpec::direct(&[TAP[0], TAP[1], TAP[2], COL]);

    separable_kernel_staged::launch(
        &client,
        launcher.cube_count(),
        launcher.cube_dim(),
        TileArgLaunch::new(in_handle.binding().into_tensor_arg(), in_spec),
        TileArgLaunch::new(
            out_handle.clone().binding().into_tensor_arg(),
            TileSpec::direct(&[ROW, COL]),
        ),
        Some(4),
        launcher.partitioning_arg(),
        launcher.partitioning().level(0),
        f32_ty,
    );

    let got = HostData::from_tensor_handle(&client, out_handle, HostDataType::F32);
    let want = reference(&in_data);
    for row in 0..ROWS {
        for col in 0..COLS {
            let have = got.get_f32(&[row, col]);
            let want = want[row * COLS + col];
            assert!(
                (have - want).abs() < 1e-4,
                "separable padded staged: at ({row}, {col}) got {have}, want {want}"
            );
        }
    }
}

// ---- separable lhs against a quantized rhs ---------------------------------

// ---- separable lhs against a resampling rhs --------------------------------

/// The rhs's one gathered physical axis: `⌊(3·row + 2·tap) / 2⌋`, which is `⌊3·row / 2⌋ + tap`.
///
/// Both halves of the split the separable schedule runs on are load-bearing here. `row` stays
/// inside the floor, so it has to be anchored; `tap` has a coefficient the divisor factors out, so
/// it steps by `1` on that anchor. This pins the hand fold against folding the whole map per tap.
const ROW_NUM: usize = 3;
const RESAMPLE: usize = 2;
const RTAPS: usize = 2;
const RROWS: usize = 4;
const RCOLS: usize = 3;

fn resample_origin(row: usize) -> usize {
    (ROW_NUM * row) / RESAMPLE
}

/// One factor, over the single contracted axis: a stated factorization of rank one still takes the
/// separable schedule, since the per-row weight walk it caches is worth `nr` evaluations whatever
/// the rank.
#[cube]
fn resample_weights<E: Float>() -> Weights<E> {
    let mut factors = Sequence::new();
    factors.push(factor::<E>(0usize));
    Factors::new(factors)
}

#[cube(launch)]
fn resample_kernel<E: Float>(
    input: &TileArg<'_, E, Const<1>>,
    output: &TileArg<'_, E, Const<1>>,
    #[comptime] normalized: bool,
    space: Partitioning,
    #[comptime] level: Level,
    #[define(E)] _dtype: ElemType,
) {
    let input = input.tile(comptime!(space.clone()));
    let weights = Procedural::<E>::separable::<Weights<E>>(
        comptime!(space.space().subspace(&[ROW, TAP[0]])),
        resample_weights::<E>(),
    );
    let weights = if comptime!(normalized) {
        weights.normalized(comptime!(TapSupport::Whole), comptime!(DivGuard::default()))
    } else {
        weights
    }
    .tile();

    let output = output.tile(comptime!(space.clone()));
    for region in space.over(&level) {
        let mut out = output
            .at(&region)
            .accumulating(REGISTER_BLOCK, Semiring::SUM_PROD);
        out.mm(&weights.at(&region), &input.at(&region));
    }
}

#[test]
fn a_separable_lhs_contracts_a_resampling_rhs() {
    check_resampling(false);
}

#[test]
fn a_separable_resampling_lhs_normalizes_its_factor_run() {
    check_resampling(true);
}

fn check_resampling(normalized: bool) {
    let client = cubecl::test_device().client();
    let f32_ty = f32::elem_type_native();

    let in_rows = resample_origin(RROWS - 1) + RTAPS;
    let in_shape = shape![in_rows, RCOLS];
    let in_data = ramp(in_shape.num_elements());
    let (in_handle, _) = TestInput::builder(client.clone(), in_shape)
        .dtype(f32_ty)
        .custom(in_data.clone())
        .generate_with_f32_host_data();
    let out_handle = TestInput::builder(client.clone(), shape![RROWS, RCOLS])
        .dtype(f32_ty)
        .zeros()
        .generate_without_host_data();

    let launcher = implied(
        &client,
        Partitioning::new(
            Space::new(&[(ROW, RROWS), (COL, RCOLS), (TAP[0], RTAPS)]),
            Levels::leaf(&[(ROW, RROWS), (COL, RCOLS), (TAP[0], RTAPS)])
                .walk_every(&[ROW, COL, TAP[0]])
                .build(),
        ),
        Form::Static,
    );

    let in_spec = TileSpec::new(Projection::new(
        &[ROW, TAP[0], COL],
        &[
            PhysicalAxisMap::affine(&[(ROW, ROW_NUM), (TAP[0], RESAMPLE)]).over(RESAMPLE),
            PhysicalAxisMap::of(COL),
        ],
    ));

    resample_kernel::launch(
        &client,
        launcher.cube_count(),
        launcher.cube_dim(),
        TileArgLaunch::new(in_handle.binding().into_tensor_arg(), in_spec),
        TileArgLaunch::new(
            out_handle.clone().binding().into_tensor_arg(),
            TileSpec::direct(&[ROW, COL]),
        ),
        normalized,
        launcher.partitioning_arg(),
        launcher.partitioning().level(0),
        f32_ty,
    );

    let got = HostData::from_tensor_handle(&client, out_handle, HostDataType::F32);
    for row in 0..RROWS {
        for col in 0..RCOLS {
            let mut want = 0.0f32;
            for tap in 0..RTAPS {
                let at = (resample_origin(row) + tap) * RCOLS + col;
                want += factor_value(0, tap, row) * in_data[at];
            }
            if normalized {
                want /= (0..RTAPS).map(|tap| factor_value(0, tap, row)).sum::<f32>();
            }
            let have = got.get_f32(&[row, col]);
            assert!(
                (have - want).abs() < 1e-4,
                "separable resample (normalized={normalized}): at ({row}, {col}) got {have}, \
                 want {want}"
            );
        }
    }
}

// ---- masked normalization against a procedural trailing tile ----------------

/// Normalize one deliberately child-local factor run at a time. This shape makes the rhs's
/// second procedural window contain one real tap and one padded tap, so `TapSupport::InBounds` must
/// distinguish the checked-read zero from an in-bounds sample without relying on backing memory.
#[cube(launch)]
fn procedural_mask_kernel<E: Float>(
    output: &TileArg<'_, E, Const<1>>,
    space: Partitioning,
    #[comptime] level: Level,
    #[define(E)] _dtype: ElemType,
) {
    let rhs = Procedural::<E>::new::<AffineCoordinate<E>>(
        comptime!(space.space().subspace(&[TAP[0], COL])),
        affine_along(TAP[0], E::new(1.0_f32), E::new(1.0_f32)),
    )
    .tile();
    let mut output = output.tile(comptime!(space.clone()));
    output.zero();

    for region in rhs.over(&level) {
        let rhs = rhs.at(&region);
        let child = comptime!(level.clone().child(&space.space().clone()));
        let mut factors = Sequence::new();
        factors.push(affine_along(TAP[0], E::new(1.0_f32), E::new(0.0_f32)));
        let weights = Procedural::<E>::separable::<Factors<AffineCoordinate<E>>>(
            comptime!(child.subspace(&[ROW, TAP[0]])),
            Factors::new(factors),
        )
        .normalized(
            comptime!(TapSupport::InBounds),
            comptime!(DivGuard::default()),
        )
        .tile();
        output
            .at(&region)
            .accumulating(REGISTER_BLOCK, Semiring::SUM_PROD)
            .mma(&weights, &rhs);
    }
}

#[test]
fn masked_normalization_excludes_a_procedural_overhang() {
    let client = cubecl::test_device().client();
    let dtype = f32::elem_type_native();
    let output = TestInput::builder(client.clone(), shape![1, 1])
        .dtype(dtype)
        .zeros()
        .generate_without_host_data();
    let launcher = implied(
        &client,
        Partitioning::new(
            Space::new(&[(ROW, 1), (COL, 1), (TAP[0], 3)]),
            Levels::leaf(&[(ROW, 1), (COL, 1), (TAP[0], 2)])
                .walk_every(&[ROW, COL, TAP[0]])
                .build(),
        ),
        Form::Static,
    );

    procedural_mask_kernel::launch(
        &client,
        launcher.cube_count(),
        launcher.cube_dim(),
        TileArgLaunch::new(
            output.clone().binding().into_tensor_arg(),
            TileSpec::direct(&[ROW, COL]),
        ),
        launcher.partitioning_arg(),
        launcher.partitioning().level(0),
        dtype,
    );

    let got = HostData::from_tensor_handle(&client, output, HostDataType::F32).get_f32(&[0, 0]);
    // Mechanism assertion: tests that `TapSupport::InBounds` excludes the overhang tap across
    // chunks. First chunk: (1 + 2) / 2. Trailing chunk: 3 / 1 (with its padded fourth tap
    // excluded). Sum across chunks yields 1.5 + 3.0 = 4.5.
    assert!((got - 4.5).abs() < 1.0e-6, "got {got}, want 4.5");
}

// ---- masked normalization against Gmem Boundary::Zero input -----------------

#[cube(launch)]
fn resample_kernel_masked<E: Float>(
    input: &TileArg<'_, E, Const<1>>,
    output: &TileArg<'_, E, Const<1>>,
    space: Partitioning,
    #[comptime] level: Level,
    #[define(E)] _dtype: ElemType,
) {
    let input = input.tile(comptime!(space.clone()));
    let weights = Procedural::<E>::separable::<Weights<E>>(
        comptime!(space.space().subspace(&[ROW, TAP[0]])),
        resample_weights::<E>(),
    )
    .normalized(
        comptime!(TapSupport::InBounds),
        comptime!(DivGuard::default()),
    )
    .tile();

    let output = output.tile(comptime!(space.clone()));
    for region in space.over(&level) {
        let mut out = output
            .at(&region)
            .accumulating(REGISTER_BLOCK, Semiring::SUM_PROD);
        out.mm(&weights.at(&region), &input.at(&region));
    }
}

/// [`resample_kernel_masked`] with the input staged: the stage records the window it was filled
/// from, and the mask is put to that rectangle.
#[cube(launch)]
fn resample_kernel_masked_staged<E: Float>(
    input: &TileArg<'_, E, Const<1>>,
    output: &TileArg<'_, E, Const<1>>,
    space: Partitioning,
    #[comptime] level: Level,
    #[define(E)] _dtype: ElemType,
) {
    let input = input.tile(comptime!(space.clone()));
    let weights = Procedural::<E>::separable::<Weights<E>>(
        comptime!(space.space().subspace(&[ROW, TAP[0]])),
        resample_weights::<E>(),
    )
    .normalized(
        comptime!(TapSupport::InBounds),
        comptime!(DivGuard::default()),
    )
    .tile();

    let output = output.tile(comptime!(space.clone()));
    let walk = space.over(&level);
    let mut stages = Stages::smem_single(&walk, &input, StageStorage::Strided, 1usize);
    stages.pipelined(walk, |slot, region| {
        let mut out = output
            .at(region)
            .accumulating(REGISTER_BLOCK, Semiring::SUM_PROD);
        let weights = weights.at(region);
        slot.consume(|input| {
            out.mm(&weights, input);
        });
    });
}

#[test]
fn masked_normalization_dedarkens_a_boundary_zero_gmem_input() {
    let client = cubecl::test_device().client();
    let f32_ty = f32::elem_type_native();

    // Deliberately clip input rows so the last output row's tap window overhangs the edge.
    let in_rows = resample_origin(RROWS - 1) + 1;
    let in_shape = shape![in_rows, RCOLS];
    let in_data = ramp(in_shape.num_elements());
    let (in_handle, _) = TestInput::builder(client.clone(), in_shape)
        .dtype(f32_ty)
        .custom(in_data.clone())
        .generate_with_f32_host_data();
    let out_handle = TestInput::builder(client.clone(), shape![RROWS, RCOLS])
        .dtype(f32_ty)
        .zeros()
        .generate_without_host_data();

    let launcher = implied(
        &client,
        Partitioning::new(
            Space::new(&[(ROW, RROWS), (COL, RCOLS), (TAP[0], RTAPS)]),
            Levels::leaf(&[(ROW, RROWS), (COL, RCOLS), (TAP[0], RTAPS)])
                .walk_every(&[ROW, COL, TAP[0]])
                .build(),
        ),
        Form::Static,
    );

    let in_spec = TileSpec::new(Projection::new(
        &[ROW, TAP[0], COL],
        &[
            PhysicalAxisMap::affine(&[(ROW, ROW_NUM), (TAP[0], RESAMPLE)]).over(RESAMPLE),
            PhysicalAxisMap::of(COL),
        ],
    ))
    .boundary(BoundaryPolicy::Every(Boundary::Zero));

    resample_kernel_masked::launch(
        &client,
        launcher.cube_count(),
        launcher.cube_dim(),
        TileArgLaunch::new(in_handle.binding().into_tensor_arg(), in_spec),
        TileArgLaunch::new(
            out_handle.clone().binding().into_tensor_arg(),
            TileSpec::direct(&[ROW, COL]),
        ),
        launcher.partitioning_arg(),
        launcher.partitioning().level(0),
        f32_ty,
    );

    let got = HostData::from_tensor_handle(&client, out_handle, HostDataType::F32);
    for row in 0..RROWS {
        for col in 0..RCOLS {
            let mut num = 0.0f32;
            let mut den = 0.0f32;
            for tap in 0..RTAPS {
                let in_r = resample_origin(row) + tap;
                if in_r < in_rows {
                    let w = factor_value(0, tap, row);
                    num += w * in_data[in_r * RCOLS + col];
                    den += w;
                }
            }
            let want = if den != 0.0 { num / den } else { 0.0 };
            let have = got.get_f32(&[row, col]);
            assert!(
                (have - want).abs() < 1e-4,
                "masked gmem resample: at ({row}, {col}) got {have}, want {want}"
            );
        }
    }
}

/// The staged twin of [`masked_normalization_dedarkens_a_boundary_zero_gmem_input`].
///
/// `TapSupport::InBounds` has to drop the taps that overhang the input, but the fill has already
/// replaced those with zeros by the time the leaf reads them: a staged window cannot tell a padded
/// zero from a real sample, so the stage records its source window and masks to that rectangle.
///
/// The expected values are the gmem test's, because staging is a placement decision and must
/// not move a number.
#[test]
fn masked_normalization_dedarkens_a_boundary_zero_smem_input() {
    let client = cubecl::test_device().client();
    let f32_ty = f32::elem_type_native();

    // Same clipped input as the gmem twin, so the last output row's taps overhang the edge.
    let in_rows = resample_origin(RROWS - 1) + 1;
    let in_shape = shape![in_rows, RCOLS];
    let in_data = ramp(in_shape.num_elements());
    let (in_handle, _) = TestInput::builder(client.clone(), in_shape)
        .dtype(f32_ty)
        .custom(in_data.clone())
        .generate_with_f32_host_data();
    let out_handle = TestInput::builder(client.clone(), shape![RROWS, RCOLS])
        .dtype(f32_ty)
        .zeros()
        .generate_without_host_data();

    let launcher = implied(
        &client,
        Partitioning::new(
            Space::new(&[(ROW, RROWS), (COL, RCOLS), (TAP[0], RTAPS)]),
            Levels::leaf(&[(ROW, RROWS), (COL, RCOLS), (TAP[0], RTAPS)])
                .walk_every(&[ROW, COL, TAP[0]])
                .build(),
        ),
        Form::Static,
    );

    let in_spec = TileSpec::new(Projection::new(
        &[ROW, TAP[0], COL],
        &[
            PhysicalAxisMap::affine(&[(ROW, ROW_NUM), (TAP[0], RESAMPLE)]).over(RESAMPLE),
            PhysicalAxisMap::of(COL),
        ],
    ))
    .boundary(BoundaryPolicy::Every(Boundary::Zero));

    resample_kernel_masked_staged::launch(
        &client,
        launcher.cube_count(),
        launcher.cube_dim(),
        TileArgLaunch::new(in_handle.binding().into_tensor_arg(), in_spec),
        TileArgLaunch::new(
            out_handle.clone().binding().into_tensor_arg(),
            TileSpec::direct(&[ROW, COL]),
        ),
        launcher.partitioning_arg(),
        launcher.partitioning().level(0),
        f32_ty,
    );

    let got = HostData::from_tensor_handle(&client, out_handle, HostDataType::F32);
    let mut overhanging = 0usize;
    for row in 0..RROWS {
        for col in 0..RCOLS {
            let mut num = 0.0f32;
            let mut den = 0.0f32;
            for tap in 0..RTAPS {
                let in_r = resample_origin(row) + tap;
                if in_r < in_rows {
                    let w = factor_value(0, tap, row);
                    num += w * in_data[in_r * RCOLS + col];
                    den += w;
                } else if col == 0 {
                    overhanging += 1;
                }
            }
            let want = if den != 0.0 { num / den } else { 0.0 };
            let have = got.get_f32(&[row, col]);
            assert!(
                (have - want).abs() < 1e-4,
                "masked smem resample: at ({row}, {col}) got {have}, want {want}"
            );
        }
    }
    // Without an overhanging tap the mask is a no-op and the test would pass on a stage that
    // dropped the boundary entirely, which is the bug this defends.
    assert!(
        overhanging > 0,
        "masked smem resample: no tap overhangs the input, so the mask was never exercised"
    );
}

// ---- column-spanning normalized separable contraction -----------------------

#[cube(launch)]
fn column_spanning_resample_kernel<E: Float>(
    input: &TileArg<'_, E, Const<1>>,
    output: &TileArg<'_, E, Const<1>>,
    space: Partitioning,
    #[comptime] level: Level,
    #[define(E)] _dtype: ElemType,
) {
    let input = input.tile(comptime!(space.clone()));
    let weights = Procedural::<E>::separable::<Weights<E>>(
        comptime!(space.space().subspace(&[ROW, COL, TAP[0]])),
        resample_weights::<E>(),
    )
    .normalized(comptime!(TapSupport::Whole), comptime!(DivGuard::default()))
    .tile();

    let output = output.tile(comptime!(space.clone()));
    for region in space.over(&level) {
        let mut out = output
            .at(&region)
            .accumulating(REGISTER_BLOCK, Semiring::SUM_PROD);
        out.mm(&weights.at(&region), &input.at(&region));
    }
}

#[test]
fn a_column_spanning_separable_lhs_normalizes_its_factor_run() {
    let client = cubecl::test_device().client();
    let f32_ty = f32::elem_type_native();

    // `Weights` only reads `TAP[0]` and `ROW`, so adding `COL` to the LHS nest isolates the
    // column-spanning coordinate schedule without changing the mathematical values.
    let in_rows = resample_origin(RROWS - 1) + RTAPS;
    let in_shape = shape![in_rows, RCOLS];
    let in_data = ramp(in_shape.num_elements());
    let (in_handle, _) = TestInput::builder(client.clone(), in_shape)
        .dtype(f32_ty)
        .custom(in_data.clone())
        .generate_with_f32_host_data();
    let out_handle = TestInput::builder(client.clone(), shape![RROWS, RCOLS])
        .dtype(f32_ty)
        .zeros()
        .generate_without_host_data();

    let launcher = implied(
        &client,
        Partitioning::new(
            Space::new(&[(ROW, RROWS), (COL, RCOLS), (TAP[0], RTAPS)]),
            Levels::leaf(&[(ROW, RROWS), (COL, RCOLS), (TAP[0], RTAPS)])
                .walk_every(&[ROW, COL, TAP[0]])
                .build(),
        ),
        Form::Static,
    );

    let in_spec = TileSpec::new(Projection::new(
        &[ROW, TAP[0], COL],
        &[
            PhysicalAxisMap::affine(&[(ROW, ROW_NUM), (TAP[0], RESAMPLE)]).over(RESAMPLE),
            PhysicalAxisMap::of(COL),
        ],
    ));

    column_spanning_resample_kernel::launch(
        &client,
        launcher.cube_count(),
        launcher.cube_dim(),
        TileArgLaunch::new(in_handle.binding().into_tensor_arg(), in_spec),
        TileArgLaunch::new(
            out_handle.clone().binding().into_tensor_arg(),
            TileSpec::direct(&[ROW, COL]),
        ),
        launcher.partitioning_arg(),
        launcher.partitioning().level(0),
        f32_ty,
    );

    let got = HostData::from_tensor_handle(&client, out_handle, HostDataType::F32);
    for row in 0..RROWS {
        for col in 0..RCOLS {
            let mut want = 0.0f32;
            for tap in 0..RTAPS {
                let at = (resample_origin(row) + tap) * RCOLS + col;
                want += factor_value(0, tap, row) * in_data[at];
            }
            want /= (0..RTAPS).map(|tap| factor_value(0, tap, row)).sum::<f32>();
            let have = got.get_f32(&[row, col]);
            assert!(
                (have - want).abs() < 1e-4,
                "column spanning separable resample: at ({row}, {col}) got {have}, want {want}"
            );
        }
    }
}

#[cube(launch)]
fn column_spanning_resample_kernel_masked<E: Float>(
    input: &TileArg<'_, E, Const<1>>,
    output: &TileArg<'_, E, Const<1>>,
    space: Partitioning,
    #[comptime] level: Level,
    #[define(E)] _dtype: ElemType,
) {
    let input = input.tile(comptime!(space.clone()));
    let weights = Procedural::<E>::separable::<Weights<E>>(
        comptime!(space.space().subspace(&[ROW, COL, TAP[0]])),
        resample_weights::<E>(),
    )
    .normalized(
        comptime!(TapSupport::InBounds),
        comptime!(DivGuard::default()),
    )
    .tile();

    let output = output.tile(comptime!(space.clone()));
    for region in space.over(&level) {
        let mut out = output
            .at(&region)
            .accumulating(REGISTER_BLOCK, Semiring::SUM_PROD);
        out.mm(&weights.at(&region), &input.at(&region));
    }
}

#[test]
fn a_column_spanning_separable_lhs_masks_and_dedarkens_boundary_zero_gmem_input() {
    let client = cubecl::test_device().client();
    let f32_ty = f32::elem_type_native();

    // Deliberately clip input rows so trailing output rows overhang the Boundary::Zero edge.
    let in_rows = resample_origin(RROWS - 1) + 1;
    let in_shape = shape![in_rows, RCOLS];
    let in_data = ramp(in_shape.num_elements());
    let (in_handle, _) = TestInput::builder(client.clone(), in_shape)
        .dtype(f32_ty)
        .custom(in_data.clone())
        .generate_with_f32_host_data();
    let out_handle = TestInput::builder(client.clone(), shape![RROWS, RCOLS])
        .dtype(f32_ty)
        .zeros()
        .generate_without_host_data();

    let launcher = implied(
        &client,
        Partitioning::new(
            Space::new(&[(ROW, RROWS), (COL, RCOLS), (TAP[0], RTAPS)]),
            Levels::leaf(&[(ROW, RROWS), (COL, RCOLS), (TAP[0], RTAPS)])
                .walk_every(&[ROW, COL, TAP[0]])
                .build(),
        ),
        Form::Static,
    );

    let in_spec = TileSpec::new(Projection::new(
        &[ROW, TAP[0], COL],
        &[
            PhysicalAxisMap::affine(&[(ROW, ROW_NUM), (TAP[0], RESAMPLE)]).over(RESAMPLE),
            PhysicalAxisMap::of(COL),
        ],
    ))
    .boundary(BoundaryPolicy::Every(Boundary::Zero));

    column_spanning_resample_kernel_masked::launch(
        &client,
        launcher.cube_count(),
        launcher.cube_dim(),
        TileArgLaunch::new(in_handle.binding().into_tensor_arg(), in_spec),
        TileArgLaunch::new(
            out_handle.clone().binding().into_tensor_arg(),
            TileSpec::direct(&[ROW, COL]),
        ),
        launcher.partitioning_arg(),
        launcher.partitioning().level(0),
        f32_ty,
    );

    let got = HostData::from_tensor_handle(&client, out_handle, HostDataType::F32);
    for row in 0..RROWS {
        for col in 0..RCOLS {
            let mut num = 0.0f32;
            let mut den = 0.0f32;
            for tap in 0..RTAPS {
                let in_r = resample_origin(row) + tap;
                if in_r < in_rows {
                    let w = factor_value(0, tap, row);
                    num += w * in_data[in_r * RCOLS + col];
                    den += w;
                }
            }
            let want = if den != 0.0 { num / den } else { 0.0 };
            let have = got.get_f32(&[row, col]);
            assert!(
                (have - want).abs() < 1e-4,
                "column spanning masked gmem resample: at ({row}, {col}) got {have}, want {want}"
            );
        }
    }
}

// ---- zero factor sum taking fallback without poisoning siblings -------------

#[cube(launch)]
fn zero_sum_fallback_kernel<E: Float>(
    input: &TileArg<'_, E, Const<1>>,
    output: &TileArg<'_, E, Const<1>>,
    space: Partitioning,
    #[comptime] level: Level,
    #[define(E)] _dtype: ElemType,
) {
    let input = input.tile(comptime!(space.clone()));
    let mut factors = Sequence::new();
    // Factor 0: taps at k=0 (1.0) and k=1 (-1.0), sum = 0.0
    factors.push(affine_along(TAP[0], E::new(1.0_f32), E::new(-2.0_f32)));
    // Factor 1: taps at k=0 (2.0) and k=1 (2.0), sum = 4.0
    factors.push(affine_along(TAP[1], E::new(2.0_f32), E::new(0.0_f32)));
    let weights = Procedural::<E>::separable::<Factors<AffineCoordinate<E>>>(
        comptime!(space.space().subspace(&[ROW, TAP[0], TAP[1]])),
        Factors::new(factors),
    )
    .normalized(
        comptime!(TapSupport::Whole),
        comptime!(DivGuard {
            epsilon: 1.0e-7,
            fallback: 3.0,
        }),
    )
    .tile();

    let output = output.tile(comptime!(space.clone()));
    for region in space.over(&level) {
        let mut out = output
            .at(&region)
            .accumulating(REGISTER_BLOCK, Semiring::SUM_PROD);
        out.mm(&weights.at(&region), &input.at(&region));
    }
}

#[test]
fn a_zero_factor_sum_takes_fallback_without_poisoning_siblings() {
    let client = cubecl::test_device().client();
    let f32_ty = f32::elem_type_native();

    // Varies along TAP[0] so factor 0's antisymmetric taps do not cancel: the result then pins
    // the fallback value itself, not just the absence of a NaN.
    let in_handle = TestInput::builder(client.clone(), shape![2, 2, 1])
        .dtype(f32_ty)
        .custom(vec![1.0f32, 1.0, 2.0, 2.0])
        .generate_without_host_data();
    let out_handle = TestInput::builder(client.clone(), shape![1, 1])
        .dtype(f32_ty)
        .zeros()
        .generate_without_host_data();

    let launcher = implied(
        &client,
        Partitioning::new(
            Space::new(&[(ROW, 1), (COL, 1), (TAP[0], 2), (TAP[1], 2)]),
            Levels::leaf(&[(ROW, 1), (COL, 1), (TAP[0], 2), (TAP[1], 2)])
                .walk_every(&[ROW, COL, TAP[0], TAP[1]])
                .build(),
        ),
        Form::Static,
    );

    zero_sum_fallback_kernel::launch(
        &client,
        launcher.cube_count(),
        launcher.cube_dim(),
        TileArgLaunch::new(
            in_handle.binding().into_tensor_arg(),
            TileSpec::direct(&[TAP[0], TAP[1], COL]),
        ),
        TileArgLaunch::new(
            out_handle.clone().binding().into_tensor_arg(),
            TileSpec::direct(&[ROW, COL]),
        ),
        launcher.partitioning_arg(),
        launcher.partitioning().level(0),
        f32_ty,
    );

    let got = HostData::from_tensor_handle(&client, out_handle, HostDataType::F32).get_f32(&[0, 0]);
    // Factor 0 has sum 0.0 -> fallback recip 3.0 -> taps become [3.0, -3.0].
    // Factor 1 has sum 4.0 -> recip 0.25 -> taps become [0.5, 0.5].
    // Product sum = (3.0 * 1.0 - 3.0 * 2.0) * (0.5 + 0.5) = -3.0.
    assert!((got + 3.0).abs() < 1.0e-6, "got {got}, want -3.0");
}

/// [`separable_kernel`] against a packed rhs scaled per tensor, decoded into a stage first:
/// `weights · stage`, the stage filled from `input ⊗ scale`. A separable contraction reads its
/// operands cell by cell (the N-D nest), which takes plain values, so the rhs decodes where the
/// kernel stages it.
#[cube(launch)]
fn separable_scaled_kernel<E: Float, V: Size>(
    input: &TileArg<'_, u32, Const<1>>,
    scale: &TileArg<'_, f32, Const<1>>,
    output: &TileArg<'_, E, V>,
    space: Partitioning,
    #[comptime] level: Level,
    #[define(E)] _dtype: ElemType,
) {
    let input = input.tile_as::<E>(comptime!(space.clone()));
    let scale = scale.tile(comptime!(space.clone()));
    let weight_axes = comptime!([&[ROW], TAP.as_slice()].concat());
    let weights = Procedural::<E>::separable::<Weights<E>>(
        comptime!(space.space().subspace(&weight_axes)),
        weights::<E>(),
    )
    .tile();

    let output = output.tile(comptime!(space.clone()));
    let walk = space.over(&level);
    let decoded = input.mul(&scale);
    let mut stages = Stages::smem_single(&walk, &decoded, StageStorage::Strided, 1usize);
    stages.pipelined(walk, |slot, region| {
        let mut out = output
            .at(region)
            .accumulating(REGISTER_BLOCK, Semiring::SUM_PROD);
        let weights = weights.at(region);
        slot.consume(|input| {
            out.mm(&weights, input);
        });
    });
}

/// A separable lhs against a packed, per-tensor scaled rhs, decoded into the stage the separable
/// contraction reads.
#[test]
fn a_separable_lhs_contracts_a_packed_scaled_rhs() {
    use cubek_quant::scheme::{QuantScheme, QuantStore, QuantValue, ScaleDtype};
    use cubek_test_utils::{TestOutcome, TileInput, ValidationResult};

    let client = cubecl::test_device().client();
    let scheme = QuantScheme::default()
        .per_tensor(ScaleDtype::F32)
        .with_store(QuantStore::PackedU32(0))
        .with_value(QuantValue::Q8S);
    let pack = scheme.num_quants();
    let max_width = client.properties().hardware.max_vector_size;
    if pack > max_width {
        TestOutcome::Validated(ValidationResult::Skipped(format!(
            "device vectors cap at {max_width}, below the packing factor ({pack})"
        )))
        .enforce();
        return;
    }

    let launcher = implied(
        &client,
        Partitioning::new(
            Space::new(&[
                (ROW, ROWS),
                (COL, pack),
                (TAP[0], TAPS[0]),
                (TAP[1], TAPS[1]),
                (TAP[2], TAPS[2]),
            ]),
            Levels::leaf(&[
                (ROW, ROWS),
                (COL, pack),
                (TAP[0], TAPS[0]),
                (TAP[1], TAPS[1]),
                (TAP[2], TAPS[2]),
            ])
            .walk_every(&[ROW, COL, TAP[0], TAP[1], TAP[2]])
            .build(),
        ),
        Form::Static,
    );
    let input_axes = [TAP[0], TAP[1], TAP[2], COL];
    let input = TileInput::builder(&client, launcher.space().subspace(&input_axes))
        .untiled()
        .packed(&scheme)
        .arange();

    let f32_ty = f32::elem_type_native();
    let out_handle = TestInput::builder(client.clone(), shape![ROWS, pack])
        .dtype(f32_ty)
        .zeros()
        .generate_without_host_data();

    // One scale for the whole rhs: a scale tile over none of its axes.
    let per_tensor = Projection::new(&input_axes, &[PhysicalAxisMap::broadcast()]);
    separable_scaled_kernel::launch(
        &client,
        launcher.cube_count(),
        launcher.cube_dim(),
        pack,
        input.values_arg(TileSpec::direct(&input_axes)),
        input.scales_arg(TileSpec::new(per_tensor)),
        TileArgLaunch::new(
            out_handle.clone().binding().into_tensor_arg(),
            TileSpec::direct(&[ROW, COL]),
        ),
        launcher.partitioning_arg(),
        launcher.partitioning().level(0),
        f32_ty,
    );

    let got = HostData::from_tensor_handle(&client, out_handle, HostDataType::F32);
    let scale = input.scale_values[0];
    for row in 0..ROWS {
        for col in 0..pack {
            let mut want = 0.0f32;
            for k0 in 0..TAPS[0] {
                for k1 in 0..TAPS[1] {
                    for k2 in 0..TAPS[2] {
                        let weight = factor_value(0, k0, row)
                            * factor_value(1, k1, row)
                            * factor_value(2, k2, row);
                        let at = ((k0 * TAPS[1] + k1) * TAPS[2] + k2) * pack + col;
                        want += weight * input.q[at] as f32 * scale;
                    }
                }
            }
            let have = got.get_f32(&[row, col]);
            assert!(
                (have - want).abs() < 1e-3,
                "separable scaled: at ({row}, {col}) got {have}, want {want}"
            );
        }
    }
}