Skip to main content

ruxla_pjrt/
element_type.rs

1//! Element types of device buffers.
2
3use ruxla_ir::DType;
4
5use crate::sys::PJRT_Buffer_Type;
6
7/// The element type of a [`Buffer`](crate::Buffer).
8///
9/// This covers the types ruxla uses. Buffers of other types (for example
10/// 4-bit or 8-bit floating point) are reported as [`ElementType::Other`].
11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
12#[non_exhaustive]
13pub enum ElementType {
14    /// Boolean (`pred` in XLA), stored as one byte.
15    Pred,
16    /// Signed 8-bit integer.
17    S8,
18    /// Signed 16-bit integer.
19    S16,
20    /// Signed 32-bit integer.
21    S32,
22    /// Signed 64-bit integer.
23    S64,
24    /// Unsigned 8-bit integer.
25    U8,
26    /// Unsigned 16-bit integer.
27    U16,
28    /// Unsigned 32-bit integer.
29    U32,
30    /// Unsigned 64-bit integer.
31    U64,
32    /// IEEE 754 half precision.
33    F16,
34    /// bfloat16.
35    BF16,
36    /// IEEE 754 single precision.
37    F32,
38    /// IEEE 754 double precision.
39    F64,
40    /// Complex number made of two `F32`s.
41    C64,
42    /// Complex number made of two `F64`s.
43    C128,
44    /// A type this crate does not know, with its raw `PJRT_Buffer_Type` value.
45    Other(u32),
46}
47
48impl ElementType {
49    /// Returns the size of one element in bytes, or `None` for
50    /// [`ElementType::Other`].
51    pub fn size_in_bytes(self) -> Option<usize> {
52        Some(match self {
53            Self::Pred | Self::S8 | Self::U8 => 1,
54            Self::S16 | Self::U16 | Self::F16 | Self::BF16 => 2,
55            Self::S32 | Self::U32 | Self::F32 => 4,
56            Self::S64 | Self::U64 | Self::F64 | Self::C64 => 8,
57            Self::C128 => 16,
58            Self::Other(_) => return None,
59        })
60    }
61
62    /// Returns the element type of a ruxla dtype.
63    pub fn from_dtype(dtype: DType) -> Self {
64        match dtype {
65            DType::Bool => Self::Pred,
66            DType::I8 => Self::S8,
67            DType::I16 => Self::S16,
68            DType::I32 => Self::S32,
69            DType::I64 => Self::S64,
70            DType::U8 => Self::U8,
71            DType::U16 => Self::U16,
72            DType::U32 => Self::U32,
73            DType::U64 => Self::U64,
74            DType::F16 => Self::F16,
75            DType::BF16 => Self::BF16,
76            DType::F32 => Self::F32,
77            DType::F64 => Self::F64,
78            DType::C64 => Self::C64,
79            DType::C128 => Self::C128,
80        }
81    }
82
83    /// Returns the ruxla dtype, or `None` for types ruxla does not have.
84    pub fn to_dtype(self) -> Option<DType> {
85        DType::ALL
86            .into_iter()
87            .find(|&d| Self::from_dtype(d) == self)
88    }
89
90    pub(crate) fn to_raw(self) -> PJRT_Buffer_Type {
91        match self {
92            Self::Pred => PJRT_Buffer_Type::PJRT_Buffer_Type_PRED,
93            Self::S8 => PJRT_Buffer_Type::PJRT_Buffer_Type_S8,
94            Self::S16 => PJRT_Buffer_Type::PJRT_Buffer_Type_S16,
95            Self::S32 => PJRT_Buffer_Type::PJRT_Buffer_Type_S32,
96            Self::S64 => PJRT_Buffer_Type::PJRT_Buffer_Type_S64,
97            Self::U8 => PJRT_Buffer_Type::PJRT_Buffer_Type_U8,
98            Self::U16 => PJRT_Buffer_Type::PJRT_Buffer_Type_U16,
99            Self::U32 => PJRT_Buffer_Type::PJRT_Buffer_Type_U32,
100            Self::U64 => PJRT_Buffer_Type::PJRT_Buffer_Type_U64,
101            Self::F16 => PJRT_Buffer_Type::PJRT_Buffer_Type_F16,
102            Self::BF16 => PJRT_Buffer_Type::PJRT_Buffer_Type_BF16,
103            Self::F32 => PJRT_Buffer_Type::PJRT_Buffer_Type_F32,
104            Self::F64 => PJRT_Buffer_Type::PJRT_Buffer_Type_F64,
105            Self::C64 => PJRT_Buffer_Type::PJRT_Buffer_Type_C64,
106            Self::C128 => PJRT_Buffer_Type::PJRT_Buffer_Type_C128,
107            Self::Other(raw) => PJRT_Buffer_Type(raw),
108        }
109    }
110
111    pub(crate) fn from_raw(raw: PJRT_Buffer_Type) -> Self {
112        const ALL: [ElementType; 15] = [
113            ElementType::Pred,
114            ElementType::S8,
115            ElementType::S16,
116            ElementType::S32,
117            ElementType::S64,
118            ElementType::U8,
119            ElementType::U16,
120            ElementType::U32,
121            ElementType::U64,
122            ElementType::F16,
123            ElementType::BF16,
124            ElementType::F32,
125            ElementType::F64,
126            ElementType::C64,
127            ElementType::C128,
128        ];
129        ALL.into_iter()
130            .find(|ty| ty.to_raw() == raw)
131            .unwrap_or(Self::Other(raw.0))
132    }
133}
134
135#[cfg(test)]
136mod tests {
137    use super::*;
138
139    #[test]
140    fn dtype_round_trip() {
141        for dtype in DType::ALL {
142            let ty = ElementType::from_dtype(dtype);
143            assert_eq!(ty.to_dtype(), Some(dtype));
144            assert_eq!(ty.size_in_bytes(), Some(dtype.size_in_bytes()));
145        }
146        assert_eq!(ElementType::Other(99).to_dtype(), None);
147    }
148
149    #[test]
150    fn raw_round_trip() {
151        for ty in [ElementType::Pred, ElementType::BF16, ElementType::C128] {
152            assert_eq!(ElementType::from_raw(ty.to_raw()), ty);
153        }
154        assert_eq!(
155            ElementType::from_raw(PJRT_Buffer_Type(1000)),
156            ElementType::Other(1000)
157        );
158    }
159}