cubecl_core/frontend/element/
float.rs

1use cubecl_ir::{ConstantScalarValue, Scope, StorageType};
2use half::{bf16, f16};
3
4use crate::{
5    ir::{ElemType, ExpandElement, FloatKind},
6    prelude::*,
7};
8
9use super::Numeric;
10
11mod fp4;
12mod fp6;
13mod fp8;
14mod relaxed;
15mod tensor_float;
16mod typemap;
17
18pub use typemap::*;
19
20/// Floating point numbers. Used as input in float kernels
21pub trait Float:
22    Numeric
23    + Exp
24    + Log
25    + Log1p
26    + Cos
27    + Sin
28    + Tanh
29    + Powf
30    + Powi<i32>
31    + Sqrt
32    + Round
33    + Floor
34    + Ceil
35    + Trunc
36    + Erf
37    + Recip
38    + Magnitude
39    + Normalize
40    + Dot
41    + IsNan
42    + IsInf
43    + Into<Self::ExpandType>
44    + core::ops::Neg<Output = Self>
45    + core::ops::Add<Output = Self>
46    + core::ops::Sub<Output = Self>
47    + core::ops::Mul<Output = Self>
48    + core::ops::Div<Output = Self>
49    + std::ops::AddAssign
50    + std::ops::SubAssign
51    + std::ops::MulAssign
52    + std::ops::DivAssign
53    + std::cmp::PartialOrd
54    + std::cmp::PartialEq
55{
56    const DIGITS: u32;
57    const EPSILON: Self;
58    const INFINITY: Self;
59    const MANTISSA_DIGITS: u32;
60    const MAX_10_EXP: i32;
61    const MAX_EXP: i32;
62    const MIN_10_EXP: i32;
63    const MIN_EXP: i32;
64    const MIN_POSITIVE: Self;
65    const NAN: Self;
66    const NEG_INFINITY: Self;
67    const RADIX: u32;
68
69    fn new(val: f32) -> Self;
70    fn __expand_new(scope: &mut Scope, val: f32) -> <Self as CubeType>::ExpandType {
71        __expand_new(scope, val)
72    }
73}
74
75macro_rules! impl_float {
76    (half $primitive:ident, $kind:ident) => {
77        impl_float!($primitive, $kind, |val| $primitive::from_f64(val));
78    };
79    ($primitive:ident, $kind:ident) => {
80        impl_float!($primitive, $kind, |val| val as $primitive);
81    };
82    ($primitive:ident, $kind:ident, $new:expr) => {
83        impl CubeType for $primitive {
84            type ExpandType = ExpandElementTyped<$primitive>;
85        }
86
87        impl CubePrimitive for $primitive {
88            /// Return the element type to use on GPU
89            fn as_type_native() -> Option<StorageType> {
90                Some(StorageType::Scalar(ElemType::Float(FloatKind::$kind)))
91            }
92
93            fn from_const_value(value: ConstantScalarValue) -> Self {
94                let ConstantScalarValue::Float(value, _) = value else {
95                    unreachable!()
96                };
97                $new(value)
98            }
99        }
100
101        impl IntoRuntime for $primitive {
102            fn __expand_runtime_method(self, scope: &mut Scope) -> ExpandElementTyped<Self> {
103                let elem: ExpandElementTyped<Self> = self.into();
104                into_runtime_expand_element(scope, elem).into()
105            }
106        }
107
108        impl Numeric for $primitive {
109            fn min_value() -> Self {
110                <Self as num_traits::Float>::min_value()
111            }
112            fn max_value() -> Self {
113                <Self as num_traits::Float>::max_value()
114            }
115        }
116
117        impl ExpandElementIntoMut for $primitive {
118            fn elem_into_mut(scope: &mut Scope, elem: ExpandElement) -> ExpandElement {
119                into_mut_expand_element(scope, elem)
120            }
121        }
122
123        impl IntoMut for $primitive {
124            fn into_mut(self, _scope: &mut Scope) -> Self {
125                self
126            }
127        }
128
129        impl Float for $primitive {
130            const DIGITS: u32 = $primitive::DIGITS;
131            const EPSILON: Self = $primitive::EPSILON;
132            const INFINITY: Self = $primitive::INFINITY;
133            const MANTISSA_DIGITS: u32 = $primitive::MANTISSA_DIGITS;
134            const MAX_10_EXP: i32 = $primitive::MAX_10_EXP;
135            const MAX_EXP: i32 = $primitive::MAX_EXP;
136            const MIN_10_EXP: i32 = $primitive::MIN_10_EXP;
137            const MIN_EXP: i32 = $primitive::MIN_EXP;
138            const MIN_POSITIVE: Self = $primitive::MIN_POSITIVE;
139            const NAN: Self = $primitive::NAN;
140            const NEG_INFINITY: Self = $primitive::NEG_INFINITY;
141            const RADIX: u32 = $primitive::RADIX;
142
143            fn new(val: f32) -> Self {
144                $new(val as f64)
145            }
146        }
147    };
148}
149
150impl_float!(half f16, F16);
151impl_float!(half bf16, BF16);
152impl_float!(f32, F32);
153impl_float!(f64, F64);
154
155impl ScalarArgSettings for f16 {
156    fn register<R: Runtime>(&self, settings: &mut KernelLauncher<R>) {
157        settings.register_f16(*self);
158    }
159}
160
161impl ScalarArgSettings for bf16 {
162    fn register<R: Runtime>(&self, settings: &mut KernelLauncher<R>) {
163        settings.register_bf16(*self);
164    }
165}
166
167impl ScalarArgSettings for f32 {
168    fn register<R: Runtime>(&self, settings: &mut KernelLauncher<R>) {
169        settings.register_f32(*self);
170    }
171}
172
173impl ScalarArgSettings for f64 {
174    fn register<R: Runtime>(&self, settings: &mut KernelLauncher<R>) {
175        settings.register_f64(*self);
176    }
177}