cubecl_reduce/instructions/
sum.rs

1use cubecl_core as cubecl;
2use cubecl_core::prelude::*;
3
4use crate::precision::ReducePrecision;
5
6use super::{ReduceCoordinate, ReduceFamily, ReduceInstruction, ReduceRequirements};
7
8#[derive(Debug, CubeType, Clone)]
9pub struct Sum {}
10
11impl ReduceFamily for Sum {
12    type Instruction<P: ReducePrecision> = Self;
13    type Config = ();
14}
15
16#[cube]
17impl<P: ReducePrecision> ReduceInstruction<P> for Sum {
18    type AccumulatorItem = Line<P::EA>;
19    type SharedAccumulator = SharedMemory<Line<P::EA>>;
20    type Config = ();
21
22    fn requirements(_this: &Self) -> ReduceRequirements {
23        ReduceRequirements { coordinates: false }
24    }
25
26    fn from_config(_config: Self::Config) -> Self {
27        Sum {}
28    }
29    fn null_input(_this: &Self, #[comptime] line_size: u32) -> Line<P::EI> {
30        Line::empty(line_size).fill(P::EI::from_int(0))
31    }
32
33    fn null_accumulator(_this: &Self, #[comptime] line_size: u32) -> Self::AccumulatorItem {
34        Line::empty(line_size).fill(P::EA::from_int(0))
35    }
36
37    fn assign_accumulator(
38        _this: &Self,
39        destination: &mut Self::AccumulatorItem,
40        source: &Self::AccumulatorItem,
41    ) {
42        *destination = *source;
43    }
44
45    fn reduce(
46        _this: &Self,
47        accumulator: &Self::AccumulatorItem,
48        item: Line<P::EI>,
49        _coordinate: ReduceCoordinate,
50        #[comptime] use_planes: bool,
51    ) -> Self::AccumulatorItem {
52        if use_planes {
53            *accumulator + plane_sum(Line::cast_from(item))
54        } else {
55            *accumulator + Line::cast_from(item)
56        }
57    }
58
59    fn fuse_accumulators(
60        _this: &Self,
61        lhs: Self::AccumulatorItem,
62        rhs: Self::AccumulatorItem,
63    ) -> Self::AccumulatorItem {
64        lhs + rhs
65    }
66
67    fn merge_line<Out: Numeric>(
68        _this: &Self,
69        accumulator: Self::AccumulatorItem,
70        _shape_axis_reduce: u32,
71    ) -> Out {
72        let mut sum = P::EA::from_int(0);
73        #[unroll]
74        for k in 0..accumulator.size() {
75            sum += accumulator[k];
76        }
77        Out::cast_from(sum)
78    }
79
80    fn to_output_perpendicular<Out: Numeric>(
81        _this: &Self,
82        accumulator: Self::AccumulatorItem,
83        _shape_axis_reduce: u32,
84    ) -> Line<Out> {
85        Line::cast_from(accumulator)
86    }
87}