cubecl_reduce/instructions/
prod.rs1use cubecl_core as cubecl;
2use cubecl_core::prelude::*;
3
4use crate::instructions::ReduceRequirements;
5
6use super::{ReduceCoordinate, ReduceFamily, ReduceInstruction};
7
8#[derive(Debug, CubeType, Clone)]
9pub struct Prod {}
10
11impl ReduceFamily for Prod {
12 type Instruction<In: Numeric> = Self;
13 type Config = ();
14}
15
16#[cube]
17impl<In: Numeric> ReduceInstruction<In> for Prod {
18 type AccumulatorItem = Line<In>;
19 type SharedAccumulator = SharedMemory<Line<In>>;
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 Prod {}
28 }
29 fn null_input(_this: &Self, #[comptime] line_size: u32) -> Line<In> {
30 Line::empty(line_size).fill(In::from_int(1))
31 }
32
33 fn null_accumulator(this: &Self, #[comptime] line_size: u32) -> Self::AccumulatorItem {
34 Self::null_input(this, line_size)
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<In>,
49 _coordinate: ReduceCoordinate,
50 #[comptime] use_planes: bool,
51 ) -> Self::AccumulatorItem {
52 if use_planes {
53 *accumulator * plane_prod(item)
54 } else {
55 *accumulator * 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 prod = In::from_int(1);
73 #[unroll]
74 for k in 0..accumulator.size() {
75 prod *= accumulator[k];
76 }
77 Out::cast_from(prod)
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}