Skip to main content

ruda_tensor_device/
element.rs

1use ruda_tensor::{bf16, f16};
2use ruda_kernel::dsl::{
3    flex32,
4    prelude::{Float, Int},
5};
6use rublas::kernel_ir::definition::{MatmulPrecision, MatrixPrecision};
7use ruprim::reduce::ReducePrecision;
8
9pub use ruda_kernel::tensor::element::TensorElement;
10
11/// Element that can be used for matrix multiplication. Includes ints and floats.
12pub trait MatmulElement:
13    TensorElement + MatmulPrecision<Acc: MatrixPrecision<Global: TensorElement>>
14{
15}
16
17/// The float element type for the jit backend.
18pub trait FloatElement: MatmulElement + Float {}
19
20/// The int element type for the jit backend.
21pub trait IntElement:
22    MatmulElement + Int + ReducePrecision<EI: TensorElement, EA: TensorElement>
23{
24}
25
26/// The element type for booleans for the jit backend.
27pub trait BoolElement: TensorElement + Int {
28    /// The true value for the boolean element.
29    fn true_val() -> Self {
30        Self::from_int(1)
31    }
32
33    /// The false value for the boolean element.
34    fn false_val() -> Self {
35        Self::from_int(0)
36    }
37
38    /// New bool element from Rust bool.
39    fn new_bool(val: bool) -> Self {
40        match val {
41            true => Self::true_val(),
42            false => Self::false_val(),
43        }
44    }
45}
46
47
48impl FloatElement for f64 {}
49impl FloatElement for f32 {}
50impl FloatElement for flex32 {}
51impl FloatElement for bf16 {}
52impl FloatElement for f16 {}
53impl IntElement for i64 {}
54impl IntElement for i32 {}
55impl IntElement for i16 {}
56impl IntElement for i8 {}
57impl IntElement for u64 {}
58impl IntElement for u32 {}
59impl IntElement for u16 {}
60impl IntElement for u8 {}
61
62impl BoolElement for u8 {}
63impl BoolElement for u32 {}
64
65impl MatmulElement for f64 {}
66impl MatmulElement for f32 {}
67impl MatmulElement for flex32 {}
68impl MatmulElement for bf16 {}
69impl MatmulElement for f16 {}
70
71impl MatmulElement for i64 {}
72impl MatmulElement for i32 {}
73impl MatmulElement for i16 {}
74impl MatmulElement for i8 {}
75impl MatmulElement for u64 {}
76impl MatmulElement for u32 {}
77impl MatmulElement for u16 {}
78impl MatmulElement for u8 {}