strided-basic 0.6.3

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
use core::fmt::Debug;
use core::mem::MaybeUninit;

use num_complex::{Complex32, Complex64};

use super::{PadPlan, RawStridedMut, RawStridedRef};

fn assert_contiguous_pad_matches_scalar<T>(operand_data: &[T], fill: T)
where
    T: Copy + Debug + PartialEq + super::MaybeSendSync,
{
    let operand_dims = [3usize, 2];
    let operand_strides = [1isize, -3];
    let operand_offset = 3isize;
    let dest_dims = [4usize, 4];
    let dest_strides = [1isize, 4];
    let dest_offset = 2isize;
    let edge_low = [-1i64, 1];
    let edge_high = [2i64, 0];
    let interior = [0i64, 1];
    let plan = PadPlan::compile(
        &operand_dims,
        &operand_strides,
        &dest_dims,
        &dest_strides,
        &edge_low,
        &edge_high,
        &interior,
    )
    .unwrap();
    assert!(plan.contiguous_axis0_run.is_some());

    let mut scalar_plan = plan.clone();
    scalar_plan.contiguous_dest_fill = false;
    scalar_plan.contiguous_axis0_run = None;
    scalar_plan.contiguous_axis0_columns = None;
    let mut fast_dest = vec![fill; 20];
    let mut scalar_dest = fast_dest.clone();
    let operand = RawStridedRef::new(
        operand_data,
        &operand_dims,
        &operand_strides,
        operand_offset,
    )
    .unwrap();
    {
        let mut dest =
            RawStridedMut::new(&mut fast_dest, &dest_dims, &dest_strides, dest_offset).unwrap();
        plan.execute(&mut dest, &operand, fill).unwrap();
    }
    {
        let mut dest =
            RawStridedMut::new(&mut scalar_dest, &dest_dims, &dest_strides, dest_offset).unwrap();
        scalar_plan.execute(&mut dest, &operand, fill).unwrap();
    }
    assert_eq!(fast_dest, scalar_dest);
}

#[test]
fn pad_plan_selects_contiguous_axis0_run_for_dense_edge_padding() {
    let plan =
        PadPlan::compile(&[2_097_152], &[1], &[2_097_408], &[1], &[128], &[128], &[0]).unwrap();

    assert_eq!(plan.contiguous_axis0_run(), Some((0, 128, 2_097_152)));
    assert!(plan.has_contiguous_dest_fill());
}

#[test]
fn contiguous_pad_matches_scalar_for_every_erased_scalar_type() {
    assert_contiguous_pad_matches_scalar(&[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0], -1.0);
    assert_contiguous_pad_matches_scalar(&[1.0f64, 2.0, 3.0, 4.0, 5.0, 6.0], -1.0);
    assert_contiguous_pad_matches_scalar(&[1i32, 2, 3, 4, 5, 6], -1);
    assert_contiguous_pad_matches_scalar(&[1i64, 2, 3, 4, 5, 6], -1);
    assert_contiguous_pad_matches_scalar(&[true, false, true, false, true, false], false);
    assert_contiguous_pad_matches_scalar(
        &[
            Complex32::new(1.0, -1.0),
            Complex32::new(2.0, -2.0),
            Complex32::new(3.0, -3.0),
            Complex32::new(4.0, -4.0),
            Complex32::new(5.0, -5.0),
            Complex32::new(6.0, -6.0),
        ],
        Complex32::new(-1.0, 0.0),
    );
    assert_contiguous_pad_matches_scalar(
        &[
            Complex64::new(1.0, -1.0),
            Complex64::new(2.0, -2.0),
            Complex64::new(3.0, -3.0),
            Complex64::new(4.0, -4.0),
            Complex64::new(5.0, -5.0),
            Complex64::new(6.0, -6.0),
        ],
        Complex64::new(-1.0, 0.0),
    );
}

#[test]
fn pad_plan_selects_axis0_columns_only_for_dense_non_cropping_edge_padding() {
    let operand_dims = [5usize, 4];
    let operand_strides = [1isize, 5];
    let dest_dims = [9usize, 8];
    let dest_strides = [1isize, 9];

    let selected = PadPlan::compile(
        &operand_dims,
        &operand_strides,
        &dest_dims,
        &dest_strides,
        &[2, 2],
        &[2, 2],
        &[0, 0],
    )
    .unwrap();
    assert!(selected.has_contiguous_axis0_columns());

    // Interior spacing on any axis keeps the fill-then-copy fallback.
    let interior = PadPlan::compile(
        &operand_dims,
        &operand_strides,
        &[9, 11],
        &[1, 9],
        &[2, 2],
        &[2, 2],
        &[0, 1],
    )
    .unwrap();
    assert!(!interior.has_contiguous_axis0_columns());

    // A non-dense destination keeps the fallback.
    let strided = PadPlan::compile(
        &operand_dims,
        &operand_strides,
        &dest_dims,
        &[9, 1],
        &[2, 2],
        &[2, 2],
        &[0, 0],
    )
    .unwrap();
    assert!(!strided.has_contiguous_axis0_columns());

    // Cropping on either axis keeps the fallback.
    let crop_axis0 = PadPlan::compile(
        &operand_dims,
        &operand_strides,
        &[7, 8],
        &[1, 7],
        &[-1, 2],
        &[3, 2],
        &[0, 0],
    )
    .unwrap();
    assert!(!crop_axis0.has_contiguous_axis0_columns());
    let crop_axis1 = PadPlan::compile(
        &operand_dims,
        &operand_strides,
        &[9, 6],
        &[1, 9],
        &[2, -1],
        &[2, 3],
        &[0, 0],
    )
    .unwrap();
    assert!(!crop_axis1.has_contiguous_axis0_columns());

    // Unit axis-0 operand stride and dense destination with no padding at all
    // still selects the single-pass write (each element is written once).
    let exact = PadPlan::compile(
        &operand_dims,
        &operand_strides,
        &operand_dims,
        &operand_strides,
        &[0, 0],
        &[0, 0],
        &[0, 0],
    )
    .unwrap();
    assert!(exact.has_contiguous_axis0_columns());
}

/// Compare the single-pass column write against the generic fill-then-copy
/// fallback for a dense column-major destination.
fn assert_axis0_columns_match_fallback(
    operand_dims: &[usize],
    operand_strides: &[isize],
    dest_dims: &[usize],
    edge_low: &[i64],
    edge_high: &[i64],
) {
    let dest_strides = col_major_strides(dest_dims);
    let plan = PadPlan::compile(
        operand_dims,
        operand_strides,
        dest_dims,
        &dest_strides,
        edge_low,
        edge_high,
        &vec![0i64; operand_dims.len()],
    )
    .unwrap();
    assert!(
        plan.has_contiguous_axis0_columns(),
        "dense non-cropping layout should select the single-pass write"
    );
    let mut fallback = plan.clone();
    fallback.contiguous_dest_fill = false;
    fallback.contiguous_axis0_run = None;
    fallback.contiguous_axis0_columns = None;

    let operand_total: usize = operand_dims.iter().product();
    let operand: Vec<f64> = (0..operand_total).map(|value| value as f64 + 1.0).collect();
    let source = RawStridedRef::new(&operand, operand_dims, operand_strides, 0).unwrap();
    let dest_total: usize = dest_dims.iter().product();
    let fill = -1.0f64;

    let mut fast = vec![fill; dest_total];
    let mut generic = vec![fill; dest_total];
    {
        let mut dest = RawStridedMut::new(&mut fast, dest_dims, &dest_strides, 0).unwrap();
        plan.execute(&mut dest, &source, fill).unwrap();
    }
    {
        let mut dest = RawStridedMut::new(&mut generic, dest_dims, &dest_strides, 0).unwrap();
        fallback.execute(&mut dest, &source, fill).unwrap();
    }
    assert_eq!(fast, generic, "rank {:?}", operand_dims.len());
}

#[test]
fn axis0_columns_match_fallback_for_ranks_one_to_three() {
    // rank 1, low/high pad > 0, = 0
    assert_axis0_columns_match_fallback(&[7], &[1], &[7], &[0], &[0]);
    assert_axis0_columns_match_fallback(&[7], &[1], &[12], &[3], &[2]);
    assert_axis0_columns_match_fallback(&[7], &[1], &[9], &[0], &[2]);

    // rank 2, dense operand and destination
    assert_axis0_columns_match_fallback(&[5, 4], &[1, 5], &[5, 4], &[0, 0], &[0, 0]);
    assert_axis0_columns_match_fallback(&[5, 4], &[1, 5], &[9, 8], &[2, 2], &[2, 2]);
    assert_axis0_columns_match_fallback(&[5, 4], &[1, 5], &[8, 4], &[3, 0], &[0, 0]);

    // rank 3, dense operand and destination
    assert_axis0_columns_match_fallback(
        &[3, 4, 5],
        &[1, 3, 12],
        &[3, 4, 5],
        &[0, 0, 0],
        &[0, 0, 0],
    );
    assert_axis0_columns_match_fallback(
        &[3, 4, 5],
        &[1, 3, 12],
        &[6, 7, 9],
        &[1, 2, 3],
        &[2, 1, 1],
    );
}

#[test]
fn axis0_columns_uninit_initializes_every_poisoned_slot() {
    let operand_dims = [5usize, 4];
    let operand_strides = [1isize, 5];
    let dest_dims = [9usize, 8];
    let dest_strides = [1isize, 9];
    let plan = PadPlan::compile(
        &operand_dims,
        &operand_strides,
        &dest_dims,
        &dest_strides,
        &[2, 2],
        &[2, 2],
        &[0, 0],
    )
    .unwrap();
    assert!(plan.has_contiguous_axis0_columns());

    let operand: Vec<f64> = (1..=20).map(|value| value as f64).collect();
    let source = RawStridedRef::new(&operand, &operand_dims, &operand_strides, 0).unwrap();
    let mut storage = vec![MaybeUninit::new(f64::NAN); dest_dims.iter().product()];
    let mut dest = RawStridedMut::new(&mut storage, &dest_dims, &dest_strides, 0).unwrap();
    plan.execute_uninit(&mut dest, &source, -1.0).unwrap();
    // SAFETY: every reachable destination slot was written by the plan.
    let values: Vec<f64> = storage
        .iter()
        .map(|slot| unsafe { slot.assume_init() })
        .collect();
    assert!(values.iter().all(|value| !value.is_nan()));

    let mut reference = vec![-1.0f64; dest_dims.iter().product()];
    let mut dest = RawStridedMut::new(&mut reference, &dest_dims, &dest_strides, 0).unwrap();
    plan.execute(&mut dest, &source, -1.0).unwrap();
    assert_eq!(values, reference);
}

#[test]
fn pad_benchmark_recipes_use_generic_replay() {
    for (label, rank, crop, nonunit) in [
        ("compact_rank2", 2usize, false, false),
        ("compact_rank4", 4, false, false),
        ("compact_rank8", 8, false, false),
        ("rank2_negative_crop", 2, true, false),
        ("rank2_nonunit", 2, false, true),
    ] {
        let mut operand_dims = vec![2usize; rank - 1];
        operand_dims.push(8);
        let interior = std::iter::once(1i64)
            .chain(std::iter::repeat_n(0i64, rank - 1))
            .collect::<Vec<_>>();
        let mut edge_low = vec![0i64; rank];
        let mut edge_high = vec![0i64; rank];
        if crop {
            edge_low[1] = -1;
            edge_high[1] = 1;
        }
        let operand_strides = if nonunit {
            vec![2isize, 4]
        } else {
            col_major_strides(&operand_dims)
        };
        let dest_dims = operand_dims
            .iter()
            .zip(&interior)
            .zip(edge_low.iter().zip(&edge_high))
            .map(|((&dim, &inner), (&low, &high))| {
                (low + (dim as i64 - 1) * (inner + 1) + high + 1) as usize
            })
            .collect::<Vec<_>>();
        let dest_strides = if nonunit {
            vec![2isize, 6]
        } else {
            col_major_strides(&dest_dims)
        };
        let plan = PadPlan::compile(
            &operand_dims,
            &operand_strides,
            &dest_dims,
            &dest_strides,
            &edge_low,
            &edge_high,
            &interior,
        )
        .unwrap();
        assert!(
            plan.contiguous_axis0_run.is_none(),
            "{label} unexpectedly selected the axis-0 fast path"
        );
        assert!(plan.generic_copy.total > 0, "{label} has no copy domain");
    }
}

fn col_major_strides(dims: &[usize]) -> Vec<isize> {
    let mut stride = 1isize;
    dims.iter()
        .map(|&dim| {
            let current = stride;
            stride *= dim as isize;
            current
        })
        .collect()
}

#[test]
fn axis0_columns_match_fallback_for_rank_five_small_axis0() {
    // Axis-0 extent 1 and 2 keep the single-pass column write selected while
    // the outer odometer has four axes to carry, so the incremental operand
    // offset must track entering and leaving the in-bounds interval per axis.
    assert_axis0_columns_match_fallback(
        &[1, 3, 4, 2, 5],
        &[1, 1, 3, 12, 24],
        &[1, 5, 6, 5, 8],
        &[0, 1, 1, 2, 1],
        &[0, 1, 1, 1, 2],
    );
    assert_axis0_columns_match_fallback(
        &[2, 3, 4, 2, 5],
        &[1, 2, 6, 24, 48],
        &[4, 5, 6, 5, 8],
        &[1, 1, 1, 2, 1],
        &[1, 1, 1, 1, 2],
    );
}

#[test]
fn axis0_columns_uninit_writes_bool_slots() {
    // A `bool` destination with genuinely uninitialized backing storage: the
    // single-pass column write must fill pad spans through `MaybeUninit<bool>`
    // and never form a `&mut [bool]` over uninitialized memory.
    let operand_dims = [1usize, 3, 4, 2, 5];
    let operand_strides = [1isize, 1, 3, 12, 24];
    let dest_dims = [2usize, 5, 6, 5, 8];
    let dest_strides = col_major_strides(&dest_dims);
    let edge_low = [1i64, 1, 1, 2, 1];
    let edge_high = [0i64, 1, 1, 1, 2];
    let plan = PadPlan::compile(
        &operand_dims,
        &operand_strides,
        &dest_dims,
        &dest_strides,
        &edge_low,
        &edge_high,
        &[0i64; 5],
    )
    .unwrap();
    assert!(plan.has_contiguous_axis0_columns());

    let operand: Vec<bool> = (0..120).map(|index| index % 2 == 0).collect();
    let source = RawStridedRef::new(&operand, &operand_dims, &operand_strides, 0).unwrap();
    let dest_total = dest_dims.iter().product();
    let mut storage = vec![MaybeUninit::<bool>::uninit(); dest_total];
    let mut dest = RawStridedMut::new(&mut storage, &dest_dims, &dest_strides, 0).unwrap();
    plan.execute_uninit(&mut dest, &source, false).unwrap();
    // SAFETY: every reachable destination slot was written by the plan.
    let values: Vec<bool> = storage
        .iter()
        .map(|slot| unsafe { slot.assume_init() })
        .collect();

    let mut reference = vec![false; dest_total];
    let mut dest = RawStridedMut::new(&mut reference, &dest_dims, &dest_strides, 0).unwrap();
    plan.execute(&mut dest, &source, false).unwrap();
    assert_eq!(values, reference);
}