ruxla_pjrt/
element_type.rs1use ruxla_ir::DType;
4
5use crate::sys::PJRT_Buffer_Type;
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
12#[non_exhaustive]
13pub enum ElementType {
14 Pred,
16 S8,
18 S16,
20 S32,
22 S64,
24 U8,
26 U16,
28 U32,
30 U64,
32 F16,
34 BF16,
36 F32,
38 F64,
40 C64,
42 C128,
44 Other(u32),
46}
47
48impl ElementType {
49 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 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 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}