burn-cubecl 0.22.0-pre.4

Generic backend that can be compiled just-in-time to any shader language target
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
#[cfg(feature = "autotune")]
use super::{autotune_reduce, autotune_reduce_with_indices, autotune_sum};
use crate::ops::permute;
use crate::{
    ops::numeric::{empty_device_contiguous_dtype, fill_device_dtype, zeros_client},
    tensor::CubeTensor,
};
use burn_backend::cubecl::{dtype_to_elem_type, dtype_to_storage_type, elem_type_to_dtype};
use burn_backend::{DType, TensorMetadata};
use burn_std::{BoolDType, Metadata};
use burn_std::{Shape, Strides};
use cubecl::{AutotuneKey, client::Client, features::AtomicUsage, ir::Type, prelude::InputScalar};
use cubek::reduce::{
    ReduceDtypes, ReduceError, ReduceStrategy, ReduceWithIndicesDtypes,
    components::instructions::ReduceOperationConfig,
    launch::{RoutineStrategy, VectorizationStrategy},
    routines::{BlueprintStrategy, unit::UnitStrategy},
    shared_sum,
};
use serde::{Deserialize, Serialize};

#[derive(Hash, Eq, PartialEq, Debug, Clone, Serialize, Deserialize, AutotuneKey)]
/// Autotune key representative of sum versions
pub struct SumAutotuneKey {
    /// The type of the tensor
    dtype: burn_backend::DType,
    /// The anchored length of the tensor
    #[autotune(anchor)]
    length: usize,
}

/// The value a reduction over zero elements must produce, or `None` if there is none.
///
/// Reducing zero elements yields the folded operation's identity. The extrema have no identity in
/// a bounded numeric type (there is no integer below `i32::MIN`) and an `Arg*` would have to name
/// an element that does not exist, so they return `None`: numpy raises `ValueError` and torch
/// raises `IndexError` for all of them, as do burn's CPU backends for an empty `max`/`min`.
fn empty_reduce_identity(config: ReduceOperationConfig, dtype: DType) -> Option<f64> {
    match config {
        ReduceOperationConfig::Sum | ReduceOperationConfig::Any => Some(0.0),
        ReduceOperationConfig::Prod | ReduceOperationConfig::All => Some(1.0),
        // Only floats can carry `NaN`; an integer mean of nothing has no representable value.
        ReduceOperationConfig::Mean => dtype.is_float().then_some(f64::NAN),
        ReduceOperationConfig::Max
        | ReduceOperationConfig::Min
        | ReduceOperationConfig::MaxAbs
        | ReduceOperationConfig::TopK(_)
        | ReduceOperationConfig::ArgMax
        | ReduceOperationConfig::ArgMin
        | ReduceOperationConfig::ArgTopK(_) => None,
    }
}

/// Fill `output` with the identity of `config`, or report that `config` has none.
///
/// Filled directly rather than by a reduce kernel because cubek's `validate_shapes` rejects a
/// zero-length axis with [`ReduceError::ReduceAxisTooSmall`], so no reduction can run.
///
/// An operation with no identity is rejected even when `output` is itself empty: emptiness of the
/// output depends on the *other* axes, so allowing it would make `max` succeed for shape `[0, 0]`
/// and fail for `[3, 0]`.
fn reduce_empty_axis(
    output: CubeTensor,
    axis_length: usize,
    config: ReduceOperationConfig,
) -> Result<CubeTensor, ReduceError> {
    let identity =
        empty_reduce_identity(config, output.dtype).ok_or(ReduceError::ReduceAxisTooSmall {
            axis_length,
            k: accumulator_len(config),
        })?;

    if output.meta.num_elements() == 0 {
        return Ok(output);
    }

    let identity = InputScalar::new(identity, dtype_to_storage_type(output.dtype));

    Ok(fill_device_dtype(output, identity))
}

/// Check if the client supports atomic add for the given element type.
fn supports_atomic_add(client: &Client, dtype: DType) -> bool {
    client
        .properties()
        .atomic_type_usage(Type::atomic(dtype_to_elem_type(dtype)))
        .contains(AtomicUsage::Add)
}

/// [Sum](sum) with fallback when `client` doesn't support atomic add for the type `E`.
pub fn sum_fallback(
    tensor: CubeTensor,
    mut strategy: SumStrategy,
) -> Result<CubeTensor, ReduceError> {
    // Early check before creating output and fallback
    if matches!(strategy, SumStrategy::OneShot(_))
        && !supports_atomic_add(&tensor.client, tensor.dtype)
    {
        strategy = SumStrategy::Chained(Default::default());
    }
    sum(tensor, strategy)
}

/// Specialize reduce function to compute the sum of all elements of the `input` tensor and return
/// the value into a single-element tensor of shape `1 x 1 x 1 x ...` with the same rank as `input`.
///
/// This is expected to be faster for larger tensors than calling [reduce] with the `Sum` instruction.
///
/// Return an error if the `client` doesn't support atomic add for the type `E`.
pub fn sum(tensor: CubeTensor, strategy: SumStrategy) -> Result<CubeTensor, ReduceError> {
    let client = tensor.client.clone();
    let device = tensor.device.clone();

    // No strategy can launch a kernel over an empty input, so write the additive identity.
    if tensor.meta.num_elements() == 0 {
        return Ok(zeros_client(client, device, [1].into(), tensor.dtype));
    }

    match strategy {
        SumStrategy::OneShot(cube_count) => {
            let output = zeros_client(client.clone(), device, [1].into(), tensor.dtype);
            let dtype = tensor.dtype;

            shared_sum(
                &client,
                tensor.binding(),
                output.clone().binding(),
                cube_count,
                dtype_to_elem_type(dtype),
            )?;

            Ok(output)
        }
        SumStrategy::Chained(strategy) => {
            reduce(tensor, None, strategy, ReduceOperationConfig::Sum)
        }
        #[cfg(feature = "autotune")]
        SumStrategy::Autotune => Ok(autotune_sum(&client, tensor)),
    }
}

/// Select a strategy to perform a sum.
pub enum SumStrategy {
    /// Run a single kernel with many cubes working in parallel to sum all elements.
    /// The provided value is the number of elements summed per unit (up-to-rounding )
    OneShot(u32),
    /// Use multiple kernels
    Chained(KernelReduceStrategy),
    /// Use autotune to find the best cube count given the hardware and the input.
    #[cfg(feature = "autotune")]
    Autotune,
}

impl Default for SumStrategy {
    fn default() -> Self {
        #[cfg(feature = "autotune")]
        return Self::Autotune;

        #[cfg(not(feature = "autotune"))]
        return Self::OneShot(4);
    }
}

/// Reduce all elements of the `input` tensor using the instruction `Rd` and the given [Strategy](ReduceStrategy).
///
/// Return an error if `strategy` is `Specific(strategy)` and the specified strategy is not supported by the `client`.
///
/// If there is no error, the output is a tensor with decreasing strides
/// where the shape of reduced dim is set to 1 but all shape are similar to the input.
pub fn reduce(
    mut tensor: CubeTensor,
    output_dtype: Option<DType>,
    strategy: KernelReduceStrategy,
    config: ReduceOperationConfig,
) -> Result<CubeTensor, cubek::reduce::ReduceError> {
    // In practice, it looks like starting by the axis with the smallest shape
    // and going in increasing order lead to the fastest calculation.
    let sorted_axis = argsort(tensor.meta.shape());
    for axis in sorted_axis {
        tensor = reduce_dim(tensor, output_dtype, axis, strategy.clone(), config)?;
    }
    // reshape to scalar tensor
    *tensor.meta = Metadata::new([1], [1]);
    Ok(tensor)
}

/// Reduce several `dims` of the `input` tensor with the instruction `config`,
/// keeping each of them with length one.
///
/// Reducing one dimension at a time writes and reads back an intermediate per
/// dimension, the first of which is nearly the size of the input. Dimensions
/// that sit next to each other in memory can instead be folded into one axis by
/// a stride change alone, and reduced in a single launch — for a channels-last
/// tensor that is every non-channel dimension at once.
///
/// Only a run that memory already holds together folds, so each pass takes the
/// largest such run and reduces that, largest first because it is the pass that
/// leaves the least behind. The folded axis is presented *first* and the
/// remaining dimensions after it in logical order, which puts the reduction's
/// output back in logical order and contiguous, so the next pass starts from a
/// tensor whose memory order is its logical one. A layout holding every reduced
/// dimension together therefore takes a single launch, and one that scatters them
/// takes no more launches than reducing them one at a time would have.
pub fn reduce_dims(
    input: CubeTensor,
    output_dtype: Option<DType>,
    dims: &[usize],
    strategy: KernelReduceStrategy,
    config: ReduceOperationConfig,
) -> Result<CubeTensor, ReduceError> {
    let rank = input.meta.num_dims();
    let mut shape = input.meta.shape().clone();
    let reduced: Vec<usize> = (0..rank).filter(|dim| dims.contains(dim)).collect();

    let empty: Vec<usize> = reduced
        .iter()
        .copied()
        .filter(|dim| shape[*dim] == 0)
        .collect();

    // A dimension already of length one is reduced by being left alone, and its
    // stride is arbitrary, so keeping it among the dimensions to fold would let
    // it break a run of dimensions that do fold.
    let mut left: Vec<usize> = reduced
        .iter()
        .copied()
        .filter(|dim| shape[*dim] > 1)
        .collect();

    if empty.is_empty() {
        match (reduced.first(), left.len()) {
            (None, _) => return Ok(input),
            (Some(&dim), 0) => return reduce_dim(input, output_dtype, dim, strategy, config),
            (_, 1) => return reduce_dim(input, output_dtype, left[0], strategy, config),
            _ => {}
        }
    }

    let mut tensor = input;

    for dim in empty {
        tensor = reduce_dim(tensor, output_dtype, dim, strategy.clone(), config)?;
        shape[dim] = 1;
    }

    while !left.is_empty() {
        let run =
            largest_run_memory_holds_together(tensor.meta.shape(), tensor.meta.strides(), &left);
        let rest = (0..rank).filter(|dim| !run.contains(dim));
        let presented_dims: Vec<usize> = run.iter().copied().chain(rest).collect();

        let mut presented = permute(tensor, &presented_dims);
        fold_leading_dims(&mut presented, run.len());

        tensor = reduce_dim(presented, output_dtype, 0, strategy.clone(), config)?;

        // The reduction writes contiguously, the folded run first at length one
        // and every other dimension after it in logical order — which is the
        // logical shape again with the run's dimensions at length one.
        for dim in &run {
            shape[*dim] = 1;
        }
        *tensor.meta = Metadata::new(shape.clone(), burn_std::tensor::contiguous_strides(&shape));

        left.retain(|dim| !run.contains(dim));
    }

    Ok(tensor)
}

/// The dimensions among `left` that memory already holds together — consecutive in
/// memory order and nesting densely, so folding them into one axis is a stride
/// change — taking the run of most elements where there is more than one.
fn largest_run_memory_holds_together(
    shape: &Shape,
    strides: &Strides,
    left: &[usize],
) -> Vec<usize> {
    let mut memory_order: Vec<usize> = (0..shape.num_dims()).collect();
    memory_order.sort_by(|a, b| strides[*b].cmp(&strides[*a]).then(a.cmp(b)));

    let elements = |run: &[usize]| run.iter().map(|dim| shape[*dim]).product::<usize>();
    let mut largest: Vec<usize> = Vec::new();
    let mut run: Vec<usize> = Vec::new();

    for dim in memory_order {
        if shape[dim] == 1 {
            continue;
        }
        if !left.contains(&dim) {
            run.clear();
            continue;
        }
        // A gap under the dimension outside this one leaves the two spanning more
        // than their extents, and no stride change folds them into one axis.
        if let Some(&outside) = run.last()
            && strides[outside] != strides[dim] * shape[dim]
        {
            run.clear();
        }
        run.push(dim);

        if elements(&run) > elements(&largest) {
            largest.clone_from(&run);
        }
    }

    largest
}

/// Fold the leading `count` dimensions of `tensor` into one.
///
/// Sound only for dimensions memory holds together, which is what
/// [largest_run_memory_holds_together] returns: they span exactly their extents,
/// so the axis replacing them runs at the stride of the innermost of them.
fn fold_leading_dims(tensor: &mut CubeTensor, count: usize) {
    let shape = tensor.meta.shape();
    let strides = tensor.meta.strides();

    let mut folded_shape: Vec<usize> = vec![shape[..count].iter().product()];
    folded_shape.extend_from_slice(&shape[count..]);

    let mut folded_strides: Vec<usize> = vec![strides[count - 1]];
    folded_strides.extend_from_slice(&strides[count..]);

    *tensor.meta = Metadata::new(Shape::from(folded_shape), Strides::new(&folded_strides));
}

/// Reduce with a logical instruction ([`Any`](ReduceOperationConfig::Any) /
/// [`All`](ReduceOperationConfig::All)) and return the result as a boolean tensor.
///
/// `Any` / `All` require the output dtype (like `Arg*` index outputs): the
/// kernel writes the `0/1` flags directly into the numeric backing of the
/// boolean storage (cubek has no bool elem), so the only step left here is the
/// kernel-free relabel to `Bool`.
///
/// `dim == None` reduces the whole tensor to a scalar; `Some(dim)` reduces a
/// single axis, keeping it with length 1.
pub fn reduce_logical(
    tensor: CubeTensor,
    dim: Option<usize>,
    config: ReduceOperationConfig,
    out_dtype: BoolDType,
) -> CubeTensor {
    debug_assert!(
        matches!(
            config,
            ReduceOperationConfig::Any | ReduceOperationConfig::All
        ),
        "reduce_logical only supports Any / All, got {config:?}"
    );
    let out_bool = DType::Bool(out_dtype);
    let backing = elem_type_to_dtype(dtype_to_elem_type(out_bool));

    let mut out = match dim {
        Some(d) => reduce_dim(tensor, Some(backing), d, Default::default(), config),
        None => reduce(tensor, Some(backing), Default::default(), config),
    }
    .expect("Any/All reduce on a valid axis cannot fail");

    out.dtype = out_bool; // same storage, relabel as Bool (no kernel)
    out
}

/// Accumulator slots one reduction needs: `k` for top-k, `1` for every other operation.
///
/// Shared memory scales with it, so a routine that fits at one length can overrun the
/// device limit at another. That makes it part of the fused autotune key as well as the
/// output length along the reduced axis.
pub(crate) fn accumulator_len(config: ReduceOperationConfig) -> usize {
    match config {
        ReduceOperationConfig::TopK(k) | ReduceOperationConfig::ArgTopK(k) => k,
        _ => 1,
    }
}

fn argsort(shape: &[usize]) -> Vec<usize> {
    let mut indices = (0..shape.len()).collect::<Vec<_>>();
    indices.sort_by_key(|&i| &shape[i]);
    indices
}

/// Reduce the given `axis` of the `input` tensor using the instruction `Rd` and the given [Strategy](ReduceStrategy).
///
/// Return an error if `strategy` is `Specific(strategy)` and the specified strategy is not supported by the `client`.
/// Also returns an error if the `axis` is larger than the `input` rank or if the shape of `output` is invalid.
///
/// If there is no error, the output is a tensor with decreasing strides
/// where the shape of reduced dim is set to 1 but all shape are similar to the input.
pub fn reduce_dim(
    input: CubeTensor,
    output_dtype: Option<DType>,
    dim: usize,
    strategy: KernelReduceStrategy,
    config: ReduceOperationConfig,
) -> Result<CubeTensor, cubek::reduce::ReduceError> {
    let input = crate::kernel::untile(input);
    debug_assert!(
        !matches!(
            config,
            ReduceOperationConfig::ArgMax
                | ReduceOperationConfig::ArgMin
                | ReduceOperationConfig::ArgTopK(_)
                | ReduceOperationConfig::Any
                | ReduceOperationConfig::All
        ) || output_dtype.is_some(),
        "The `output_dtype` has to be `Some` when the `config` is `ArgMax`, `ArgMin`, `ArgTopK`, `Any` or `All`.
        "
    );

    let accumulator_len = accumulator_len(config);
    let dtypes = config.precision(
        dtype_to_elem_type(input.dtype),
        output_dtype.map(dtype_to_elem_type),
    );
    let client = input.client.clone();
    let output = init_reduce_output(&input, dim, &dtypes, accumulator_len).ok_or(
        cubek::reduce::ReduceError::InvalidAxis {
            axis: dim,
            rank: input.meta.num_dims(),
        },
    )?;

    // `output` already carries the right shape here, with `dim` set to `accumulator_len`.
    let axis_length = input.meta.shape[dim];
    if axis_length == 0 {
        return reduce_empty_axis(output, axis_length, config);
    }

    let result = match strategy {
        KernelReduceStrategy::Unspecified => cubek::reduce::reduce(
            &client,
            input.binding(),
            output.clone().binding(),
            dim,
            ReduceStrategy {
                routine: RoutineStrategy::Unit(BlueprintStrategy::Inferred(UnitStrategy)),
                vectorization: VectorizationStrategy {
                    parallel_output_vectorization: false,
                },
                autotune_level: Default::default(),
            },
            config,
            dtypes,
        ),
        KernelReduceStrategy::Specific(strategy) => cubek::reduce::reduce(
            &client,
            input.binding(),
            output.clone().binding(),
            dim,
            strategy,
            config,
            dtypes,
        ),
        #[cfg(feature = "autotune")]
        KernelReduceStrategy::Autotune => {
            autotune_reduce(&client, input, output.clone(), dim, config, dtypes);
            Ok(())
        }
    };
    result.map(|_| output)
}

/// Reduce the given `axis` of `input`, returning the values **and** their indices from a
/// single kernel launch.
///
/// Running the value reduction and its `Arg*` counterpart separately walks the input twice
/// and discards half of each result, even though one reduction already computes both. The
/// reduce kernels are memory bound, so folding the two launches into one roughly halves
/// the work.
///
/// `config` must be an operation with a meaningful index (top-k, max, min); each `Arg*`
/// config is an alias of its value counterpart here, since both halves are written either
/// way. Any other operation returns [`ReduceError::IndicesUnsupported`]. Both outputs are
/// contiguous with the reduced `dim` set to `k` for top-k and `1` otherwise.
pub fn reduce_dim_with_indices(
    input: CubeTensor,
    indices_dtype: DType,
    dim: usize,
    strategy: KernelReduceStrategy,
    config: ReduceOperationConfig,
) -> Result<(CubeTensor, CubeTensor), ReduceError> {
    let unsupported = |operation| ReduceError::IndicesUnsupported { operation };

    // Fold each `Arg*` onto its value counterpart: `precision` would otherwise demand an
    // output dtype, which here only ever applies to the indices.
    let config = match config {
        ReduceOperationConfig::ArgMax => ReduceOperationConfig::Max,
        ReduceOperationConfig::ArgMin => ReduceOperationConfig::Min,
        ReduceOperationConfig::ArgTopK(k) => ReduceOperationConfig::TopK(k),
        ReduceOperationConfig::Max
        | ReduceOperationConfig::Min
        | ReduceOperationConfig::TopK(_) => config,
        ReduceOperationConfig::Sum => return Err(unsupported("Sum")),
        ReduceOperationConfig::Prod => return Err(unsupported("Prod")),
        ReduceOperationConfig::Mean => return Err(unsupported("Mean")),
        ReduceOperationConfig::MaxAbs => return Err(unsupported("MaxAbs")),
        ReduceOperationConfig::Any => return Err(unsupported("Any")),
        ReduceOperationConfig::All => return Err(unsupported("All")),
    };

    let out_len = accumulator_len(config);

    // `precision` for these operations keeps input/values/accumulation at the input
    // dtype; the index dtype is the caller's and is converted for free in the final
    // output write.
    let value_dtypes = config.precision(dtype_to_elem_type(input.dtype), None);
    let dtypes = ReduceWithIndicesDtypes {
        input: value_dtypes.input,
        values: value_dtypes.output,
        indices: dtype_to_elem_type(indices_dtype),
        accumulation: value_dtypes.accumulation,
    };

    let invalid_axis = || ReduceError::InvalidAxis {
        axis: dim,
        rank: input.meta.num_dims(),
    };

    let values = init_reduce_output_dtype(&input, dim, elem_type_to_dtype(dtypes.values), out_len)
        .ok_or_else(invalid_axis)?;
    let indices =
        init_reduce_output_dtype(&input, dim, indices_dtype, out_len).ok_or_else(invalid_axis)?;

    // Every `config` reaching this point is an extremum, so an empty axis leaves it with no value
    // to report and no index to name. Always rejected, including when the outputs are themselves
    // empty - see `reduce_empty_axis`.
    if input.meta.shape[dim] == 0 {
        return Err(ReduceError::ReduceAxisTooSmall {
            axis_length: 0,
            k: out_len,
        });
    }

    let client = input.client.clone();

    let result = match strategy {
        KernelReduceStrategy::Unspecified => cubek::reduce::reduce_with_indices(
            &client,
            input.binding(),
            values.clone().binding(),
            indices.clone().binding(),
            dim,
            ReduceStrategy {
                routine: RoutineStrategy::Unit(BlueprintStrategy::Inferred(UnitStrategy)),
                vectorization: VectorizationStrategy {
                    parallel_output_vectorization: false,
                },
                autotune_level: Default::default(),
            },
            config,
            dtypes,
        ),
        KernelReduceStrategy::Specific(strategy) => cubek::reduce::reduce_with_indices(
            &client,
            input.binding(),
            values.clone().binding(),
            indices.clone().binding(),
            dim,
            strategy,
            config,
            dtypes,
        ),
        #[cfg(feature = "autotune")]
        KernelReduceStrategy::Autotune => {
            autotune_reduce_with_indices(
                &client,
                input,
                values.clone(),
                indices.clone(),
                dim,
                config,
                dtypes,
            );
            Ok(())
        }
    };

    result.map(|_| (values, indices))
}

/// Creates an empty output tensor with the proper shape and decreasing strides to reduce the given `axis` of `input`
/// or return `None` if `axis` is out-of-bound.
pub fn init_reduce_output(
    input: &CubeTensor,
    dim: usize,
    dtypes: &ReduceDtypes,
    accumulator_len: usize,
) -> Option<CubeTensor> {
    init_reduce_output_dtype(
        input,
        dim,
        elem_type_to_dtype(dtypes.output),
        accumulator_len,
    )
}

/// Like [`init_reduce_output`], but with the output dtype given directly rather than taken
/// from a [`ReduceDtypes`]. Needed when one reduce writes two outputs of different dtypes.
pub fn init_reduce_output_dtype(
    input: &CubeTensor,
    dim: usize,
    dtype: DType,
    accumulator_len: usize,
) -> Option<CubeTensor> {
    (dim < input.meta.num_dims()).then(|| {
        let mut shape_out = input.shape();
        shape_out[dim] = accumulator_len;
        empty_device_contiguous_dtype(input.client.clone(), input.device.clone(), shape_out, dtype)
    })
}

/// Select a strategy to perform a reduction.
#[derive(Clone, Debug)]
pub enum KernelReduceStrategy {
    /// Use a best-effort strategy based on the hardware capacity.
    /// This differs from Autotune as it doesn't try and compare many strategies to select the best.
    Unspecified,
    /// Fix the exact strategy for the reduction.
    Specific(cubek::reduce::launch::ReduceStrategy),
    /// Use autotune to find the best strategy given the hardware and the inputs.
    #[cfg(feature = "autotune")]
    Autotune,
}

impl Default for KernelReduceStrategy {
    fn default() -> Self {
        #[cfg(feature = "autotune")]
        return Self::Autotune;

        #[cfg(not(feature = "autotune"))]
        return Self::Unspecified;
    }
}