strided-basic 0.15.0

Shared typed strided CPU primitives and copy/reduction execution.
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
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
//! P6 counting-allocator matrix for the strided-basic planner and fast paths
//! (tensor4all/strided-rs#306 items 2-4, tensor4all/strided-rs#312).
//!
//! Asserts 0 library-owned allocations at rank 1..=8 for every touched entry,
//! on both sides of `SMALL_TENSOR_THRESHOLD`, with contiguous, permuted and
//! stride-0 operands, serial and inside an explicit two-worker pool. Output
//! data of array-returning APIs and allocations made inside user callbacks are
//! out of scope (P1) and are not asserted. The rank-9 view-path spill is
//! executed but not gated: above the inline rank bound the planner may spill by
//! contract. The `replay_*_raw` entries are gated at every rank: they allocate
//! nothing on a serial token, or on a pooled token whose replay is not tiled.
//!
//! The counter is process global, so the whole matrix runs in one test; the
//! setup and warm-up calls stay outside every counted window.

use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;
use std::mem::MaybeUninit;
use std::sync::atomic::{AtomicUsize, Ordering};

use strided_basic::{
    add_acc, axpy_acc, batched_outer_product_into, col_major_strides, dot,
    embed_diagonal_into_uninit, fma_acc, map_into, map_update_into, mul_acc, reduce,
    reduce_axis_into, sum, symmetrize_into, zip_map2_into, zip_map3_into, zip_map4_into,
    zip_reduce, zip_update2_into, zip_update3_into, Exec, Identity, Pool, StridedArray,
    StridedView, StridedViewMut,
};

const SMALL_TENSOR_THRESHOLD: usize = 1024;

struct CountingAllocator;

static ALLOCATIONS: AtomicUsize = AtomicUsize::new(0);

thread_local! {
    static THREAD_ALLOCATIONS: Cell<usize> = const { Cell::new(0) };
}

fn record_allocation() {
    ALLOCATIONS.fetch_add(1, Ordering::SeqCst);
    let _ = THREAD_ALLOCATIONS.try_with(|count| count.set(count.get() + 1));
}

unsafe impl GlobalAlloc for CountingAllocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        record_allocation();
        System.alloc(layout)
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        System.dealloc(ptr, layout)
    }

    unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
        record_allocation();
        System.realloc(ptr, layout, new_size)
    }
}

#[global_allocator]
static GLOBAL: CountingAllocator = CountingAllocator;

/// Steady-state allocations made by every thread while `run` executes.
///
/// The window runs three times and reports the minimum. A pool worker the
/// warm-up never scheduled pays one-time first-touch costs (thread-local
/// registration allocates on macOS) the first time it picks up a split, which
/// is not a per-call allocation; a per-call allocation shows up in every round.
/// (#321)
fn count_allocations(mut run: impl FnMut()) -> usize {
    (0..3)
        .map(|_| {
            let before = ALLOCATIONS.load(Ordering::SeqCst);
            run();
            ALLOCATIONS.load(Ordering::SeqCst) - before
        })
        .min()
        .unwrap_or(0)
}

/// Allocations made by the calling thread while `run` executes.
///
/// Serial windows use this so a concurrently idle worker pool or the global
/// global pool cannot leak background allocations into the count.
fn count_thread_allocations(run: impl FnOnce()) -> usize {
    let before = THREAD_ALLOCATIONS.with(Cell::get);
    run();
    THREAD_ALLOCATIONS.with(Cell::get) - before
}

#[derive(Clone, Copy, Debug)]
enum LayoutKind {
    Contiguous,
    Permuted,
    Broadcast,
}

/// Immutable identity view over test data.
fn sv<'a>(
    data: &'a [f64],
    dims: &[usize],
    strides: &[isize],
    offset: isize,
) -> Result<StridedView<'a, f64, Identity>, strided_basic::StridedError> {
    StridedView::new(data, dims, strides, offset)
}

fn strides_for(dims: &[usize], kind: LayoutKind) -> Vec<isize> {
    match kind {
        LayoutKind::Contiguous => col_major_strides(dims).into_vec(),
        LayoutKind::Permuted => {
            let mut strides = col_major_strides(dims).into_vec();
            strides.reverse();
            strides
        }
        LayoutKind::Broadcast => vec![0; dims.len()],
    }
}

fn kind_index(kind: LayoutKind) -> usize {
    match kind {
        LayoutKind::Contiguous => 0,
        LayoutKind::Permuted => 1,
        LayoutKind::Broadcast => 2,
    }
}

fn total(dims: &[usize]) -> usize {
    dims.iter().product()
}

/// Backing length needed to make every layout in `strides` in-bounds.
fn required_len(dims: &[usize], strides: &[isize]) -> usize {
    let mut max_offset = 0isize;
    for (&dim, &stride) in dims.iter().zip(strides) {
        if dim > 1 {
            max_offset += (dim as isize - 1) * stride.abs();
        }
    }
    (max_offset + 1) as usize
}

/// `count_allocations` runs its window three times, and a pooled token is
/// minted once per run: the pool entry and token setup stay outside the window.
fn pooled_tokens(pool: &Pool, runs: usize) -> Vec<Exec<'_>> {
    (0..runs).map(|_| Exec::new(pool, 2).unwrap()).collect()
}

/// Runs `f` on a worker of `pool`. A pooled entry called from a worker spawns
/// into the current scope without installing, so the counted windows measure
/// the entry itself, not the one-time cost of entering the pool from outside.
fn on_worker<R: Send>(pool: &Pool, f: impl FnOnce(&Pool) -> R + Send) -> R {
    let slot = std::sync::Mutex::new(Some(f));
    let out = std::sync::Mutex::new(None);
    Exec::new(pool, 2).unwrap().fork(2, |job, _child| {
        if job == 0 {
            let f = slot.lock().unwrap().take().unwrap();
            *out.lock().unwrap() = Some(f(pool));
        }
    });
    out.into_inner().unwrap().expect("job 0 ran on a worker")
}

/// Runs one entry serially and on a two-slot token of `pool`, asserting no
/// allocations. `entry` is warmed up first so one-time runtime state stays
/// outside the counted windows. Every input layout in `kinds` is exercised.
fn assert_entry<'p>(
    name: &str,
    pool: &'p Pool,
    dims: &[usize],
    kinds: &[LayoutKind],
    mut entry: impl FnMut(LayoutKind, Exec<'p>) + Send,
) {
    for &kind in kinds {
        // Warm up outside the counted windows.
        entry(kind, Exec::serial());
        let serial = count_thread_allocations(|| entry(kind, Exec::serial()));
        assert_eq!(
            serial, 0,
            "{name} {kind:?} serial at rank {dims:?} allocated {serial}"
        );
        let parallel = on_worker(pool, |_| {
            let mut tokens = pooled_tokens(pool, 4);
            entry(kind, tokens.pop().unwrap());
            count_allocations(|| entry(kind, tokens.pop().unwrap()))
        });
        assert_eq!(
            parallel, 0,
            "{name} {kind:?} two-slot at rank {dims:?} allocated {parallel}"
        );
    }
}

/// The plan-free `replay_*_raw` entries replay borrowed raw views without
/// compiling anything: no allocation on a successful warm call, serial or on
/// a pooled token, for every source layout (the callbacks allocate nothing).
fn run_replay_entries(dims: &[usize], pool: &Pool) {
    use strided_basic::{
        replay_copy_raw, replay_copy_raw_uninit, replay_map_raw, replay_map_raw_fused,
        replay_map_raw_uninit, replay_map_raw_uninit_fused, RawStridedMut, RawStridedRef,
    };
    let layouts = [
        strides_for(dims, LayoutKind::Contiguous),
        strides_for(dims, LayoutKind::Permuted),
        strides_for(dims, LayoutKind::Broadcast),
    ];
    let len = layouts
        .iter()
        .map(|strides| required_len(dims, strides))
        .max()
        .unwrap_or(0)
        .max(total(dims));
    let src: Vec<f64> = (0..len).map(|v| v as f64 + 1.0).collect();
    let mut out = vec![0.0f64; len];
    let mut uninit = vec![MaybeUninit::<f64>::uninit(); len];
    let kinds = [
        LayoutKind::Contiguous,
        LayoutKind::Permuted,
        LayoutKind::Broadcast,
    ];
    let dst_layouts = [LayoutKind::Contiguous, LayoutKind::Permuted];
    for dst_kind in dst_layouts {
        let dst_strides = strides_for(dims, dst_kind);
        assert_entry("replay_copy_raw", pool, dims, &kinds, |kind, exec| {
            let source = RawStridedRef::new(&src, dims, &layouts[kind_index(kind)], 0).unwrap();
            let mut dest = RawStridedMut::new(&mut out, dims, &dst_strides, 0).unwrap();
            replay_copy_raw(&mut dest, &source, exec).unwrap();
        });
        assert_entry("replay_map_raw", pool, dims, &kinds, |kind, exec| {
            let source = RawStridedRef::new(&src, dims, &layouts[kind_index(kind)], 0).unwrap();
            let mut dest = RawStridedMut::new(&mut out, dims, &dst_strides, 0).unwrap();
            replay_map_raw(&mut dest, &source, |d: &mut f64, s| *d += s, exec).unwrap();
        });
        assert_entry(
            "replay_copy_raw_uninit",
            pool,
            dims,
            &kinds,
            |kind, exec| {
                let source = RawStridedRef::new(&src, dims, &layouts[kind_index(kind)], 0).unwrap();
                let mut dest = RawStridedMut::new(&mut uninit, dims, &dst_strides, 0).unwrap();
                replay_copy_raw_uninit(&mut dest, &source, exec).unwrap();
            },
        );
        assert_entry("replay_map_raw_uninit", pool, dims, &kinds, |kind, exec| {
            let source = RawStridedRef::new(&src, dims, &layouts[kind_index(kind)], 0).unwrap();
            let mut dest = RawStridedMut::new(&mut uninit, dims, &dst_strides, 0).unwrap();
            replay_map_raw_uninit(&mut dest, &source, |s| 2.0 * s, exec).unwrap();
        });
        assert_entry("replay_map_raw_fused", pool, dims, &kinds, |kind, exec| {
            let source = RawStridedRef::new(&src, dims, &layouts[kind_index(kind)], 0).unwrap();
            let mut dest = RawStridedMut::new(&mut out, dims, &dst_strides, 0).unwrap();
            replay_map_raw_fused(&mut dest, &source, |d: &mut f64, s| *d += s, exec).unwrap();
        });
        assert_entry(
            "replay_map_raw_uninit_fused",
            pool,
            dims,
            &kinds,
            |kind, exec| {
                let source = RawStridedRef::new(&src, dims, &layouts[kind_index(kind)], 0).unwrap();
                let mut dest = RawStridedMut::new(&mut uninit, dims, &dst_strides, 0).unwrap();
                replay_map_raw_uninit_fused(&mut dest, &source, |s| 2.0 * s, exec).unwrap();
            },
        );
    }
}

fn run_map_entries(dims: &[usize], pool: &Pool) {
    let layouts = [
        strides_for(dims, LayoutKind::Contiguous),
        strides_for(dims, LayoutKind::Permuted),
        strides_for(dims, LayoutKind::Broadcast),
    ];
    // Every layout must index inside the same backing buffers, so size them by
    // the widest reachable offset.
    let len = layouts
        .iter()
        .map(|strides| required_len(dims, strides))
        .max()
        .unwrap_or(0)
        .max(total(dims));
    let dst_strides = strides_for(dims, LayoutKind::Contiguous);
    let src: Vec<f64> = (0..len).map(|v| v as f64 + 1.0).collect();
    let a: Vec<f64> = (0..len).map(|v| v as f64 + 2.0).collect();
    let b: Vec<f64> = (0..len).map(|v| v as f64 + 3.0).collect();
    let c: Vec<f64> = (0..len).map(|v| v as f64 + 4.0).collect();
    let mut out = vec![0.0f64; len];

    let kinds = [
        LayoutKind::Contiguous,
        LayoutKind::Permuted,
        LayoutKind::Broadcast,
    ];

    assert_entry("map_into", pool, dims, &kinds, |kind, exec| {
        let strides = &layouts[kind_index(kind)];
        let source = sv(&src, dims, strides, 0).unwrap();
        let mut dest = StridedViewMut::new(&mut out, dims, &dst_strides, 0).unwrap();
        map_into(&mut dest, &source, |x| x + 1.0, exec).unwrap();
    });

    assert_entry("zip_map2_into", pool, dims, &kinds, |kind, exec| {
        let strides = &layouts[kind_index(kind)];
        let va = sv(&a, dims, strides, 0).unwrap();
        let vb = sv(&b, dims, &dst_strides, 0).unwrap();
        let mut dest = StridedViewMut::new(&mut out, dims, &dst_strides, 0).unwrap();
        zip_map2_into(&mut dest, &va, &vb, |x, y| x + y, exec).unwrap();
    });

    assert_entry("zip_map3_into", pool, dims, &kinds, |kind, exec| {
        let strides = &layouts[kind_index(kind)];
        let va = sv(&a, dims, strides, 0).unwrap();
        let vb = sv(&b, dims, &dst_strides, 0).unwrap();
        let vc = sv(&c, dims, &dst_strides, 0).unwrap();
        let mut dest = StridedViewMut::new(&mut out, dims, &dst_strides, 0).unwrap();
        zip_map3_into(&mut dest, &va, &vb, &vc, |x, y, z| x + y + z, exec).unwrap();
    });

    assert_entry("zip_map4_into", pool, dims, &kinds, |kind, exec| {
        let strides = &layouts[kind_index(kind)];
        let va = sv(&a, dims, strides, 0).unwrap();
        let vb = sv(&b, dims, &dst_strides, 0).unwrap();
        let vc = sv(&c, dims, &dst_strides, 0).unwrap();
        let vd = sv(&src, dims, &dst_strides, 0).unwrap();
        let mut dest = StridedViewMut::new(&mut out, dims, &dst_strides, 0).unwrap();
        zip_map4_into(
            &mut dest,
            &va,
            &vb,
            &vc,
            &vd,
            |x, y, z, w| x + y + z + w,
            exec,
        )
        .unwrap();
    });

    assert_entry("add_acc", pool, dims, &kinds, |kind, exec| {
        let strides = &layouts[kind_index(kind)];
        let source = sv(&src, dims, strides, 0).unwrap();
        let mut dest = StridedViewMut::new(&mut out, dims, &dst_strides, 0).unwrap();
        add_acc(&mut dest, &source, exec).unwrap();
    });

    assert_entry("mul_acc", pool, dims, &kinds, |kind, exec| {
        let strides = &layouts[kind_index(kind)];
        let source = sv(&src, dims, strides, 0).unwrap();
        let mut dest = StridedViewMut::new(&mut out, dims, &dst_strides, 0).unwrap();
        mul_acc(&mut dest, &source, exec).unwrap();
    });

    assert_entry("axpy_acc", pool, dims, &kinds, |kind, exec| {
        let strides = &layouts[kind_index(kind)];
        let source = sv(&src, dims, strides, 0).unwrap();
        let mut dest = StridedViewMut::new(&mut out, dims, &dst_strides, 0).unwrap();
        axpy_acc(&mut dest, &source, 1.5, exec).unwrap();
    });

    assert_entry("fma_acc", pool, dims, &kinds, |kind, exec| {
        let strides = &layouts[kind_index(kind)];
        let va = sv(&a, dims, strides, 0).unwrap();
        let vb = sv(&b, dims, &dst_strides, 0).unwrap();
        let mut dest = StridedViewMut::new(&mut out, dims, &dst_strides, 0).unwrap();
        fma_acc(&mut dest, &va, &vb, exec).unwrap();
    });

    assert_entry("dot", pool, dims, &kinds, |kind, _exec| {
        let strides = &layouts[kind_index(kind)];
        let va = sv(&a, dims, strides, 0).unwrap();
        let vb = sv(&b, dims, &dst_strides, 0).unwrap();
        let _ = dot::<f64, f64, f64, Identity, Identity>(&va, &vb).unwrap();
    });

    // Single- and two-operand full reductions (#280). `dot` above is the typed
    // two-operand entry; `zip_reduce` is the mapped one, and `reduce`/`sum` are
    // the one-operand entries.
    assert_entry("reduce", pool, dims, &kinds, |kind, exec| {
        let strides = &layouts[kind_index(kind)];
        let source = sv(&src, dims, strides, 0).unwrap();
        let _ = reduce(&source, |x| x, |x, y| x + y, 0.0, exec).unwrap();
    });

    assert_entry("sum", pool, dims, &kinds, |kind, exec| {
        let strides = &layouts[kind_index(kind)];
        let source = sv(&src, dims, strides, 0).unwrap();
        let _ = sum(&source, exec).unwrap();
    });

    assert_entry("zip_reduce", pool, dims, &kinds, |kind, exec| {
        let strides = &layouts[kind_index(kind)];
        let va = sv(&a, dims, strides, 0).unwrap();
        let vb = sv(&b, dims, &dst_strides, 0).unwrap();
        let _: f64 = zip_reduce(&va, &vb, |x, y| x * y, |x, y| x + y, 0.0, exec).unwrap();
    });

    assert_entry("map_update_into", pool, dims, &kinds, |_kind, exec| {
        let mut dest = StridedViewMut::new(&mut out, dims, &dst_strides, 0).unwrap();
        map_update_into::<f64, Identity>(&mut dest, |x| x + 1.0, exec).unwrap();
    });

    assert_entry("zip_update2_into", pool, dims, &kinds, |kind, exec| {
        let strides = &layouts[kind_index(kind)];
        let va = sv(&a, dims, strides, 0).unwrap();
        let mut dest = StridedViewMut::new(&mut out, dims, &dst_strides, 0).unwrap();
        zip_update2_into::<f64, f64, Identity, Identity>(&mut dest, &va, |d, a| d + a, exec)
            .unwrap();
    });

    assert_entry("zip_update3_into", pool, dims, &kinds, |kind, exec| {
        let strides = &layouts[kind_index(kind)];
        let va = sv(&a, dims, strides, 0).unwrap();
        let vb = sv(&b, dims, &dst_strides, 0).unwrap();
        let mut dest = StridedViewMut::new(&mut out, dims, &dst_strides, 0).unwrap();
        zip_update3_into::<f64, f64, f64, Identity, Identity, Identity>(
            &mut dest,
            &va,
            &vb,
            |d, a, b| d + a + b,
            exec,
        )
        .unwrap();
    });

    // `reduce_axis_into` writes caller storage, so the output is in scope.
    if dims.len() >= 2 {
        let reduced: Vec<usize> = dims[1..].to_vec();
        let reduced_len = total(&reduced);
        let reduced_strides = col_major_strides(&reduced);
        assert_entry("reduce_axis_into", pool, dims, &kinds, |kind, exec| {
            let strides = &layouts[kind_index(kind)];
            let source = sv(&src, dims, strides, 0).unwrap();
            let mut dest =
                StridedViewMut::new(&mut out[..reduced_len], &reduced, &reduced_strides, 0)
                    .unwrap();
            reduce_axis_into(&mut dest, &source, 0, |x| x, |x, y| x + y, 0.0, exec).unwrap();
        });
    }
}

fn run_composites(pool: &Pool) {
    // symmetrize, rank 2.
    let dims = [8usize, 8];
    let len = 64;
    let src: Vec<f64> = (0..len).map(|v| v as f64 + 1.0).collect();
    let strides = col_major_strides(&dims);
    let mut out = vec![0.0f64; len];
    assert_entry(
        "symmetrize_into",
        pool,
        &dims,
        &[LayoutKind::Permuted],
        |_kind, exec| {
            let source = sv(&src, &dims, &strides, 0).unwrap();
            let mut dest = StridedViewMut::new(&mut out, &dims, &strides, 0).unwrap();
            symmetrize_into(&mut dest, &source, exec).unwrap();
        },
    );

    // batched outer product: dest [2, 3, n], lhs [2, n], rhs [3, n].
    let batch = 8usize;
    let dest_dims = [2usize, 3, batch];
    let lhs_dims = [2usize, batch];
    let rhs_dims = [3usize, batch];
    let dest_strides = col_major_strides(&dest_dims);
    let lhs_strides = col_major_strides(&lhs_dims);
    let rhs_strides = col_major_strides(&rhs_dims);
    let lhs: Vec<f64> = (0..2 * batch).map(|v| v as f64).collect();
    let rhs: Vec<f64> = (0..3 * batch).map(|v| v as f64 + 1.0).collect();
    let mut dest_data = vec![0.0f64; dest_dims.iter().product()];
    assert_entry(
        "batched_outer_product_into",
        pool,
        &dest_dims,
        &[LayoutKind::Contiguous],
        |_kind, exec| {
            let lhs_view = sv(&lhs, &lhs_dims, &lhs_strides, 0).unwrap();
            let rhs_view = sv(&rhs, &rhs_dims, &rhs_strides, 0).unwrap();
            let mut dest =
                StridedViewMut::new(&mut dest_data, &dest_dims, &dest_strides, 0).unwrap();
            batched_outer_product_into(&mut dest, &lhs_view, &rhs_view, 1, 1, exec).unwrap();
        },
    );

    // embed diagonal, rank 2 -> rank 3.
    let shape = [8usize, 8];
    let len: usize = 64;
    let input: Vec<f64> = (0..len).map(|v| v as f64).collect();
    let mut out: Vec<MaybeUninit<f64>> = vec![MaybeUninit::uninit(); len * shape[0]];
    assert_entry(
        "embed_diagonal_into_uninit",
        pool,
        &shape,
        &[LayoutKind::Contiguous],
        |_kind, exec| {
            embed_diagonal_into_uninit(&mut out, &input, &shape, 0, 0, 0.0, exec).unwrap();
        },
    );
}

#[test]
fn typed_entries_are_allocation_free_up_to_rank_limit() {
    let pool = Pool::builder().threads(2).build().unwrap();

    for rank in 1..=8usize {
        let small = vec![2usize; rank];
        let mut large = vec![2usize; rank];
        // Push the element count just past SMALL_TENSOR_THRESHOLD.
        large[0] = (SMALL_TENSOR_THRESHOLD >> (rank - 1)) + 1;
        assert!(total(&small) <= SMALL_TENSOR_THRESHOLD);
        assert!(total(&large) > SMALL_TENSOR_THRESHOLD);

        for dims in [&small, &large] {
            run_map_entries(dims, &pool);
            run_replay_entries(dims, &pool);
        }
    }

    run_composites(&pool);
    check_reduce_axis(&pool);
    check_parallel_threshold();
    check_full_reduction(&pool);
    check_plans(&pool);
    record_rank9_spill(&pool);
    check_replay_any_rank_is_allocation_free();
    check_overflowing_stride_keeps_the_exit_allocation_free();
}

/// Tensor-sized cases that must reach the parallel branches. The default
/// dispatch costs of the shared pool keep these sizes serial, so they run on a
/// pool with cheap dispatch costs, and the replay width is asserted so the
/// counting windows cannot fall back to the serial path unnoticed.
fn check_parallel_threshold() {
    use strided_basic::execution::{parallel_width, KernelClass};
    use threadteam::{DispatchCosts, DispatchKind, HostClass, WidthCosts};

    let cheap = WidthCosts::flat(std::time::Duration::from_nanos(100));
    let pool = Pool::builder()
        .threads(2)
        .dispatch_costs(
            DispatchCosts::for_class(HostClass::Generic)
                .with(DispatchKind::Team, cheap)
                .with(DispatchKind::InPool, cheap)
                .with(DispatchKind::Outside, cheap),
        )
        .build()
        .unwrap();
    let shapes: [&[usize]; 3] = [
        &[33, 33, 33],
        &[3, 3, 3, 3, 3, 3, 3, 21],
        &[5, 4, 4, 4, 4, 4, 4, 2],
    ];
    for dims in shapes {
        let width = on_worker(&pool, |pool| {
            parallel_width(&Exec::new(pool, 2).unwrap(), KernelClass::Copy, total(dims)).get()
        });
        #[cfg(feature = "parallel")]
        assert!(width >= 2, "replay case {dims:?} runs at width {width}");
        #[cfg(not(feature = "parallel"))]
        let _ = width;
        run_map_entries(dims, &pool);
        run_replay_entries(dims, &pool);
    }
    check_pooled_wide_replay(&pool);
}

/// An untiled wide replay that fans out: `[2; 17]`, column-major on both sides
/// (strictly increasing strides, so the model exits and the route is untiled),
/// on a pooled token. The window counts every thread, so a worker's range
/// cursor would show; the unit test
/// `wide_untiled_replay_fans_out_on_a_team_token` proves the dispatch.
fn check_pooled_wide_replay(pool: &Pool) {
    use strided_basic::execution::{parallel_width, KernelClass};
    use strided_basic::{replay_copy_raw, RawStridedMut, RawStridedRef};
    let dims = vec![2usize; 17];
    let strides = col_major_strides(&dims).into_vec();
    let len = total(&dims);
    let src: Vec<f64> = (0..len).map(|v| v as f64).collect();
    let mut out = vec![0.0f64; len];
    let source = RawStridedRef::new(&src, &dims, &strides, 0).unwrap();
    let mut dest = RawStridedMut::new(&mut out, &dims, &strides, 0).unwrap();
    let allocations = on_worker(pool, |pool| {
        let width = parallel_width(&Exec::new(pool, 2).unwrap(), KernelClass::Copy, len).get();
        #[cfg(feature = "parallel")]
        assert!(width >= 2, "the wide replay runs at width {width}");
        #[cfg(not(feature = "parallel"))]
        let _ = width;
        let mut tokens = pooled_tokens(pool, 4);
        replay_copy_raw(&mut dest, &source, tokens.pop().unwrap()).unwrap();
        count_allocations(|| replay_copy_raw(&mut dest, &source, tokens.pop().unwrap()).unwrap())
    });
    assert_eq!(allocations, 0, "pooled wide replay allocated {allocations}");
    assert_eq!(out, src);
}

/// The recursive full-reduction fold (`reduce`, whose parallel path combines
/// per-worker partials in a tree) must also be allocation-free on every worker
/// for a non-contiguous source above the threading threshold.
fn check_full_reduction(pool: &Pool) {
    use strided_basic::reduce;

    let dims = [257usize, 128];
    let strides = [1isize, 300];
    assert!(
        total(&dims) > 32_768,
        "full-reduction case is not tensor-sized"
    );
    let len = required_len(&dims, &strides);
    let src: Vec<f64> = (0..len).map(|v| v as f64).collect();
    // A gapped stride keeps the source non-contiguous so the recursive
    // partial-combination fold runs instead of the contiguous lane fold.
    let source = sv(&src, &dims, &strides, 0).unwrap();

    // Warm up outside the counted windows.
    let _ = reduce(&source, |x| x, |a, b| a + b, 0.0, Exec::serial()).unwrap();

    let serial = count_thread_allocations(|| {
        let _ = reduce(&source, |x| x, |a, b| a + b, 0.0, Exec::serial()).unwrap();
    });
    assert_eq!(serial, 0, "serial full reduction allocated {serial}");

    let parallel = on_worker(pool, |pool| {
        let mut tokens = pooled_tokens(pool, 4);
        let _ = reduce(&source, |x| x, |a, b| a + b, 0.0, tokens.pop().unwrap()).unwrap();
        count_allocations(|| {
            let _ = reduce(&source, |x| x, |a, b| a + b, 0.0, tokens.pop().unwrap()).unwrap();
        })
    });
    assert_eq!(parallel, 0, "two-slot full reduction allocated {parallel}");
}

/// Compiled plans must not allocate on compile or execute within the inline
/// rank/operand bounds (P4), so downstream code that compiles per call and
/// executes per accumulate does not touch the heap.
fn check_plans(_pool: &Pool) {
    use strided_basic::{
        ConcatenatePlan, ErasedConcatenatePlan, ErasedRawStridedMut, ErasedRawStridedPtr,
        ErasedRawStridedRef, ErasedRawStridedUninitMut, ErasedReducePlan, ErasedUpdatePlan,
        KernelDType, RawStridedMut, RawStridedRef, ReduceOp, UpdateOp, UpdatePlan,
    };
    for rank in 1..=8usize {
        let dims = vec![2usize; rank];
        let strides = col_major_strides(&dims).into_vec();
        let len = total(&dims);
        let src: Vec<f64> = (0..len).map(|v| v as f64 + 1.0).collect();
        let mut dst = vec![0.0f64; len];

        // UpdatePlan: compile and execute allocation-free.
        let plan = UpdatePlan::compile(&dims, &strides, &strides).unwrap();
        {
            let src_ref = RawStridedRef::new(&src, &dims, &strides, 0).unwrap();
            let mut dst_mut = RawStridedMut::new(&mut dst, &dims, &strides, 0).unwrap();
            plan.execute(&mut dst_mut, &src_ref, 2.0, 3.0, |x| x, Exec::serial())
                .unwrap();
        }
        let update_compile = count_thread_allocations(|| {
            let plan = UpdatePlan::compile(&dims, &strides, &strides).unwrap();
            std::hint::black_box(plan);
        });
        assert_eq!(
            update_compile, 0,
            "UpdatePlan::compile rank {rank} allocated {update_compile}"
        );
        let src_ref = RawStridedRef::new(&src, &dims, &strides, 0).unwrap();
        let mut dst_mut = RawStridedMut::new(&mut dst, &dims, &strides, 0).unwrap();
        let update_execute = count_thread_allocations(|| {
            plan.execute(&mut dst_mut, &src_ref, 2.0, 3.0, |x| x, Exec::serial())
                .unwrap();
        });
        assert_eq!(
            update_execute, 0,
            "UpdatePlan::execute rank {rank} allocated {update_execute}"
        );

        // Write-only uninitialized destination: same allocation-free replay.
        let mut uninit_dst = vec![MaybeUninit::<f64>::uninit(); len];
        let mut uninit_mut = RawStridedMut::new(&mut uninit_dst, &dims, &strides, 0).unwrap();
        plan.execute_uninit(&mut uninit_mut, &src_ref, 1.0, |x| x, Exec::serial())
            .unwrap();
        let update_uninit = count_thread_allocations(|| {
            plan.execute_uninit(&mut uninit_mut, &src_ref, 1.0, |x| x, Exec::serial())
                .unwrap();
        });
        assert_eq!(
            update_uninit, 0,
            "UpdatePlan::execute_uninit rank {rank} allocated {update_uninit}"
        );

        // Erased update plan: same traversal behind a dtype-erased boundary.
        let erased_update =
            ErasedUpdatePlan::compile(KernelDType::F64, &dims, &strides, &strides).unwrap();
        {
            let source = ErasedRawStridedRef::from_slice(&src, &dims, &strides, 0).unwrap();
            let mut erased_dst = vec![0.0f64; len];
            let mut dest =
                ErasedRawStridedMut::from_slice_mut(&mut erased_dst, &dims, &strides, 0).unwrap();
            erased_update
                .execute(
                    &mut dest,
                    &source,
                    2.0,
                    3.0,
                    UpdateOp::Identity,
                    Exec::serial(),
                )
                .unwrap();
        }
        let source = ErasedRawStridedRef::from_slice(&src, &dims, &strides, 0).unwrap();
        let mut erased_dst = vec![0.0f64; len];
        let mut dest =
            ErasedRawStridedMut::from_slice_mut(&mut erased_dst, &dims, &strides, 0).unwrap();
        let erased_update_execute = count_thread_allocations(|| {
            erased_update
                .execute(
                    &mut dest,
                    &source,
                    2.0,
                    3.0,
                    UpdateOp::Identity,
                    Exec::serial(),
                )
                .unwrap();
        });
        assert_eq!(
            erased_update_execute, 0,
            "ErasedUpdatePlan::execute rank {rank} allocated {erased_update_execute}"
        );

        // Erased write-only uninitialized destination.
        let mut erased_uninit = vec![MaybeUninit::<f64>::uninit(); len];
        let mut uninit_dest =
            ErasedRawStridedUninitMut::from_uninit_slice(&mut erased_uninit, &dims, &strides, 0)
                .unwrap();
        let source_ptr = ErasedRawStridedPtr::from_ref(&source);
        erased_update
            .execute_uninit(
                &mut uninit_dest,
                &source_ptr,
                1.0,
                UpdateOp::Identity,
                Exec::serial(),
            )
            .unwrap();
        let erased_update_uninit = count_thread_allocations(|| {
            erased_update
                .execute_uninit(
                    &mut uninit_dest,
                    &source_ptr,
                    1.0,
                    UpdateOp::Identity,
                    Exec::serial(),
                )
                .unwrap();
        });
        assert_eq!(
            erased_update_uninit, 0,
            "ErasedUpdatePlan::execute_uninit rank {rank} allocated {erased_update_uninit}"
        );

        // ConcatenatePlan with two inputs: compile and execute allocation-free.
        let mut dest_dims = dims.clone();
        dest_dims[0] *= 2;
        let dest_strides = col_major_strides(&dest_dims).into_vec();
        let dest_len = total(&dest_dims);
        let mut concat_dst = vec![0.0f64; dest_len];
        let concat_compile = count_thread_allocations(|| {
            let plan = ConcatenatePlan::compile(
                &[&dims, &dims],
                &[&strides, &strides],
                &dest_dims,
                &dest_strides,
                0,
            )
            .unwrap();
            std::hint::black_box(plan);
        });
        assert_eq!(
            concat_compile, 0,
            "ConcatenatePlan::compile rank {rank} allocated {concat_compile}"
        );
        let concat_plan = ConcatenatePlan::compile(
            &[&dims, &dims],
            &[&strides, &strides],
            &dest_dims,
            &dest_strides,
            0,
        )
        .unwrap();
        {
            let a = RawStridedRef::new(&src, &dims, &strides, 0).unwrap();
            let b = RawStridedRef::new(&src, &dims, &strides, 0).unwrap();
            let mut dest =
                RawStridedMut::new(&mut concat_dst, &dest_dims, &dest_strides, 0).unwrap();
            concat_plan
                .execute(&mut dest, &[a, b], Exec::serial())
                .unwrap();
        }
        let a = RawStridedRef::new(&src, &dims, &strides, 0).unwrap();
        let b = RawStridedRef::new(&src, &dims, &strides, 0).unwrap();
        let mut dest = RawStridedMut::new(&mut concat_dst, &dest_dims, &dest_strides, 0).unwrap();
        let concat_execute = count_thread_allocations(|| {
            concat_plan
                .execute(&mut dest, &[a, b], Exec::serial())
                .unwrap();
        });
        assert_eq!(
            concat_execute, 0,
            "ConcatenatePlan::execute rank {rank} allocated {concat_execute}"
        );

        // Erased concatenate execute (typed scalar, erased dtype tag).
        let erased_plan = ErasedConcatenatePlan::compile(
            KernelDType::F64,
            &[&dims, &dims],
            &[&strides, &strides],
            &dest_dims,
            &dest_strides,
            0,
        )
        .unwrap();
        let mut erased_dst = vec![0.0f64; dest_len];
        {
            let a = ErasedRawStridedRef::from_slice(&src, &dims, &strides, 0).unwrap();
            let b = ErasedRawStridedRef::from_slice(&src, &dims, &strides, 0).unwrap();
            let mut dest =
                ErasedRawStridedMut::from_slice_mut(&mut erased_dst, &dest_dims, &dest_strides, 0)
                    .unwrap();
            erased_plan
                .execute(&mut dest, &[a, b], Exec::serial())
                .unwrap();
        }
        let a = ErasedRawStridedRef::from_slice(&src, &dims, &strides, 0).unwrap();
        let b = ErasedRawStridedRef::from_slice(&src, &dims, &strides, 0).unwrap();
        let mut dest =
            ErasedRawStridedMut::from_slice_mut(&mut erased_dst, &dest_dims, &dest_strides, 0)
                .unwrap();
        let erased_concat_execute = count_thread_allocations(|| {
            erased_plan
                .execute(&mut dest, &[a, b], Exec::serial())
                .unwrap();
        });
        assert_eq!(
            erased_concat_execute, 0,
            "ErasedConcatenatePlan::execute rank {rank} allocated {erased_concat_execute}"
        );

        // Erased reduction compile (full and one axis) allocation-free.
        let full_compile = count_thread_allocations(|| {
            let plan = ErasedReducePlan::compile(KernelDType::F64, ReduceOp::Sum, &dims, &strides)
                .unwrap();
            std::hint::black_box(plan);
        });
        assert_eq!(
            full_compile, 0,
            "ErasedReducePlan::compile rank {rank} allocated {full_compile}"
        );
        let reduced_dims: Vec<usize> = dims[1..].to_vec();
        let reduced_strides = col_major_strides(&reduced_dims).into_vec();
        let axes_compile = count_thread_allocations(|| {
            let plan = ErasedReducePlan::compile_axes(
                KernelDType::F64,
                ReduceOp::Sum,
                &dims,
                &strides,
                &reduced_dims,
                &reduced_strides,
                &[0],
            )
            .unwrap();
            std::hint::black_box(plan);
        });
        assert_eq!(
            axes_compile, 0,
            "ErasedReducePlan::compile_axes rank {rank} allocated {axes_compile}"
        );
    }

    // Axis-reduction execute for each kernel strategy, allocation-free. The
    // `OutputBlocks` path exercises the per-thread reusable accumulator scratch.
    let dims = [16usize, 4];
    let strides = col_major_strides(&dims).into_vec();
    let src: Vec<f64> = (0..64).map(|v| v as f64).collect();
    for reduced_dims in [vec![4usize], vec![16usize]] {
        let reduce_axis = if reduced_dims[0] == 4 { 0 } else { 1 };
        let reduced_strides = col_major_strides(&reduced_dims).into_vec();
        let plan = ErasedReducePlan::compile_axes(
            KernelDType::F64,
            ReduceOp::Sum,
            &dims,
            &strides,
            &reduced_dims,
            &reduced_strides,
            &[reduce_axis],
        )
        .unwrap();
        let mut out = vec![0.0f64; reduced_dims.iter().product::<usize>()];
        {
            let source = ErasedRawStridedRef::from_slice(&src, &dims, &strides, 0).unwrap();
            let mut dest =
                ErasedRawStridedMut::from_slice_mut(&mut out, &reduced_dims, &reduced_strides, 0)
                    .unwrap();
            plan.execute(&mut dest, &source, Exec::serial()).unwrap();
        }
        let source = ErasedRawStridedRef::from_slice(&src, &dims, &strides, 0).unwrap();
        let mut dest =
            ErasedRawStridedMut::from_slice_mut(&mut out, &reduced_dims, &reduced_strides, 0)
                .unwrap();
        let execute = count_thread_allocations(|| {
            plan.execute(&mut dest, &source, Exec::serial()).unwrap();
        });
        assert_eq!(
            execute, 0,
            "ErasedReducePlan::execute {reduced_dims:?} allocated {execute}"
        );
    }
    let full = ErasedReducePlan::compile(KernelDType::F64, ReduceOp::Sum, &dims, &strides).unwrap();
    let mut scalar = [0.0f64; 1];
    {
        let source = ErasedRawStridedRef::from_slice(&src, &dims, &strides, 0).unwrap();
        let mut dest = ErasedRawStridedMut::from_slice_mut(&mut scalar, &[1], &[1], 0).unwrap();
        full.execute(&mut dest, &source, Exec::serial()).unwrap();
    }
    let source = ErasedRawStridedRef::from_slice(&src, &dims, &strides, 0).unwrap();
    let mut dest = ErasedRawStridedMut::from_slice_mut(&mut scalar, &[1], &[1], 0).unwrap();
    let full_execute = count_thread_allocations(|| {
        full.execute(&mut dest, &source, Exec::serial()).unwrap();
    });
    assert_eq!(
        full_execute, 0,
        "ErasedReducePlan full execute allocated {full_execute}"
    );
}

/// `reduce_axis_into` must be allocation-free; the array-returning
/// `reduce_axis` wrapper must allocate only its column-major output.
fn check_reduce_axis(_pool: &Pool) {
    use strided_basic::reduce_axis;

    let dims = [4usize, 4, 4];
    let src: Vec<f64> = (0..64).map(|v| v as f64).collect();
    let strides = col_major_strides(&dims);
    let source = sv(&src, &dims, &strides, 0).unwrap();
    let _ = reduce_axis(&source, 0, |x| x, |a, b| a + b, 0.0, Exec::serial()).unwrap();
    let wrapper = count_thread_allocations(|| {
        let out = reduce_axis(&source, 0, |x| x, |a, b| a + b, 0.0, Exec::serial()).unwrap();
        std::hint::black_box(out);
    });
    assert_eq!(
        wrapper, 1,
        "reduce_axis must allocate exactly its column-major output"
    );

    let reduced = [4usize, 4];
    let mut out = StridedArray::<f64>::col_major(&reduced);
    let mut dest = out.view_mut();
    reduce_axis_into(
        &mut dest,
        &source,
        0,
        |x| x,
        |a, b| a + b,
        0.0,
        Exec::serial(),
    )
    .unwrap();
    let into = count_thread_allocations(|| {
        reduce_axis_into(
            &mut dest,
            &source,
            0,
            |x| x,
            |a, b| a + b,
            0.0,
            Exec::serial(),
        )
        .unwrap();
    });
    assert_eq!(into, 0, "reduce_axis_into must not allocate");
}

/// Above `INLINE_RANK` the planner spills by contract. This records the spill
/// instead of gating it, so a change from inline to unconditional heap use
/// above the bound is visible without making rank 9 a failure.
fn record_rank9_spill(pool: &Pool) {
    let dims = vec![2usize; 9];
    let len = total(&dims);
    let src: Vec<f64> = (0..len).map(|v| v as f64).collect();
    let mut out = vec![0.0f64; len];
    let dest_strides = strides_for(&dims, LayoutKind::Contiguous);
    let src_strides = strides_for(&dims, LayoutKind::Permuted);
    let source = sv(&src, &dims, &src_strides, 0).unwrap();
    let mut dest = StridedViewMut::new(&mut out, &dims, &dest_strides, 0).unwrap();
    map_into(&mut dest, &source, |x| x + 1.0, Exec::serial()).unwrap();
    let serial = count_thread_allocations(|| {
        map_into(&mut dest, &source, |x| x + 1.0, Exec::serial()).unwrap();
    });
    let parallel = on_worker(pool, |pool| {
        let mut tokens = pooled_tokens(pool, 4);
        map_into(&mut dest, &source, |x| x + 1.0, tokens.pop().unwrap()).unwrap();
        count_allocations(|| {
            map_into(&mut dest, &source, |x| x + 1.0, tokens.pop().unwrap()).unwrap();
        })
    });
    eprintln!("rank-9 permuted map_into spill: serial={serial}, parallel={parallel}");
}

/// The `replay_*_raw` entries on a serial token, and `replay_map_raw_serial`,
/// at ranks above the inline one: the issue's nine-axis reversed-to-column-major copy, the same nine
/// moving axes among 191 extent-1 axes, a wide layout the model tiles, and a
/// 21-axis layout whose stride-order table exceeds the 40-entry inline scratch.
fn check_replay_any_rank_is_allocation_free() {
    // (a) The issue fixture: nine extent-2 axes, source reversed.
    let dims = vec![2usize; 9];
    let dst: Vec<isize> = (0..9).map(|k| 1isize << k).collect();
    let src: Vec<isize> = (0..9).rev().map(|k| 1isize << k).collect();
    assert_serial_replays_allocate_nothing("rank 9", &dims, &dst, &src);

    // (b) Rank 200: the nine moving axes of (a), each followed by extent-1
    // axes with arbitrary strides.
    let mut padded = (Vec::new(), Vec::new(), Vec::new());
    for axis in 0..200usize {
        if axis % 22 == 0 && axis / 22 < 9 {
            let k = axis / 22;
            padded.0.push(2usize);
            padded.1.push(dst[k]);
            padded.2.push(src[k]);
        } else {
            padded.0.push(1);
            padded.1.push(axis as isize);
            padded.2.push(-(axis as isize));
        }
    }
    assert_eq!(padded.0.iter().filter(|&&d| d == 2).count(), 9);
    assert_serial_replays_allocate_nothing("rank 200", &padded.0, &padded.1, &padded.2);

    // (c) A wide layout the model tiles: `4^9` with the source stride order
    // reversed (2 MiB per buffer).
    let dims = vec![4usize; 9];
    let dst: Vec<isize> = (0..9).map(|k| 1isize << (2 * k)).collect();
    let src: Vec<isize> = (0..9).rev().map(|k| 1isize << (2 * k)).collect();
    assert_serial_replays_allocate_nothing("wide tiled", &dims, &dst, &src);

    // (d) 21 axes, reversed source: the model builds its 42-entry stride-order
    // table (2^21 elements, 16 MiB per buffer).
    let dims = vec![2usize; 21];
    let dst: Vec<isize> = (0..21).map(|k| 1isize << k).collect();
    let src: Vec<isize> = (0..21).rev().map(|k| 1isize << k).collect();
    assert_serial_replays_allocate_nothing("rank 21", &dims, &dst, &src);
}

/// Warm, then count one serial call of each replay entry, and of the serial
/// `replay_map_raw_serial` with an `FnMut`, on the calling thread.
fn assert_serial_replays_allocate_nothing(
    name: &str,
    dims: &[usize],
    dst_strides: &[isize],
    src_strides: &[isize],
) {
    use strided_basic::{
        replay_copy_raw, replay_copy_raw_uninit, replay_map_raw, replay_map_raw_fused,
        replay_map_raw_serial, replay_map_raw_uninit, replay_map_raw_uninit_fused, RawStridedMut,
        RawStridedRef,
    };
    let src: Vec<f64> = (0..required_len(dims, src_strides))
        .map(|v| v as f64)
        .collect();
    let dst_len = required_len(dims, dst_strides);
    let mut out = vec![0.0f64; dst_len];
    let mut uninit = vec![MaybeUninit::<f64>::uninit(); dst_len];
    let source = RawStridedRef::new(&src, dims, src_strides, 0).unwrap();
    fn counted(name: &str, entry: &str, mut run: impl FnMut()) {
        run();
        let count = count_thread_allocations(&mut run);
        assert_eq!(count, 0, "{name}: serial {entry} allocated {count}");
    }
    {
        let mut dest = RawStridedMut::new(&mut out, dims, dst_strides, 0).unwrap();
        counted(name, "replay_copy_raw", || {
            replay_copy_raw(&mut dest, &source, Exec::serial()).unwrap()
        });
    }
    {
        let mut dest = RawStridedMut::new(&mut out, dims, dst_strides, 0).unwrap();
        counted(name, "replay_map_raw", || {
            replay_map_raw(&mut dest, &source, |d: &mut f64, s| *d += s, Exec::serial()).unwrap()
        });
    }
    {
        let mut dest = RawStridedMut::new(&mut uninit, dims, dst_strides, 0).unwrap();
        counted(name, "replay_copy_raw_uninit", || {
            replay_copy_raw_uninit(&mut dest, &source, Exec::serial()).unwrap()
        });
    }
    {
        let mut dest = RawStridedMut::new(&mut uninit, dims, dst_strides, 0).unwrap();
        counted(name, "replay_map_raw_uninit", || {
            replay_map_raw_uninit(&mut dest, &source, |s| 2.0 * s, Exec::serial()).unwrap()
        });
    }
    {
        let mut dest = RawStridedMut::new(&mut out, dims, dst_strides, 0).unwrap();
        counted(name, "replay_map_raw_fused", || {
            replay_map_raw_fused(&mut dest, &source, |d: &mut f64, s| *d += s, Exec::serial())
                .unwrap()
        });
    }
    {
        let mut dest = RawStridedMut::new(&mut uninit, dims, dst_strides, 0).unwrap();
        counted(name, "replay_map_raw_uninit_fused", || {
            replay_map_raw_uninit_fused(&mut dest, &source, |s| 2.0 * s, Exec::serial()).unwrap()
        });
    }
    {
        let mut dest = RawStridedMut::new(&mut out, dims, dst_strides, 0).unwrap();
        let mut calls = 0usize;
        counted(name, "replay_map_raw_serial", || {
            replay_map_raw_serial(&mut dest, &source, |d: &mut f64, s| {
                calls += 1;
                *d += s
            })
            .unwrap()
        });
        assert_eq!(calls, 2 * total(dims), "{name}: one call per element");
    }
}

/// The exits are answered from the axes, so they must also cover the case where
/// the *byte* magnitude of a stride overflows the checked `u64` region while the
/// region itself fits the budget: a zero-sized operand can carry a stride no
/// allocation could hold. The wide fallback keeps this layout on the exit path;
/// reading the overflow as "does not fit" would materialize the three model
/// slices for a decision that ends in "no tiles".
fn check_overflowing_stride_keeps_the_exit_allocation_free() {
    use strided_basic::{replay_map_raw, RawStridedMut, RawStridedRef};
    let dims = vec![2usize; 9];
    let len = total(&dims);
    let dest_strides: Vec<isize> = (0..9).map(|axis| 1isize << axis).collect();
    let mut src_strides = vec![0isize; 9];
    src_strides[0] = 1isize << 61;
    let mut out = vec![0.0f64; len];
    // A logically long zero-sized slice: the checked constructor requires every
    // reachable *element* index to be inside the slice, and `1 << 61` is not
    // inside one element. Zero-sized elements cost no storage, so this is a
    // `Vec` of `()` rather than an allocation of that many bytes.
    let unit: Vec<()> = vec![(); (1usize << 61) + 1];
    let source = RawStridedRef::new(&unit, &dims, &src_strides, 0).unwrap();
    let mut dest = RawStridedMut::new(&mut out, &dims, &dest_strides, 0).unwrap();
    let apply = |_: &mut f64, _: ()| {};
    replay_map_raw(&mut dest, &source, apply, Exec::serial()).unwrap();
    let mut call = || {
        count_thread_allocations(|| {
            replay_map_raw(&mut dest, &source, apply, Exec::serial()).unwrap()
        })
    };
    let first = call();
    let second = call();
    // The region is 4096 + 128 bytes, so the model exits before its slices
    // exist, and the wide route holds its axes and cursor on the stack.
    assert_eq!(
        first, 0,
        "a cache-fit spilled replay allocated {first} times"
    );
    assert_eq!(
        second, 0,
        "a cache-fit spilled replay allocated {second} times"
    );
}