1use tenferro_tensor::{Complex32, Complex64, DType, TensorScalar};
9
10mod bool_wire;
11
12#[cfg(test)]
13mod tests;
14
15pub type Tag = i32;
17
18pub const PROTOCOL_VERSION: u16 = 1;
20
21pub const MAX_WIRE_RANK: usize = 64;
23
24pub const MAX_WIRE_ELEMENTS: u64 = i32::MAX as u64;
28
29pub(crate) const HEADER_PROLOGUE: usize = 24;
30pub(crate) const SHAPE_STRIDE: usize = 8;
31pub(crate) const MAX_HEADER_LEN: usize = HEADER_PROLOGUE + SHAPE_STRIDE * MAX_WIRE_RANK;
32pub(crate) const ACK_LEN: usize = 8;
33pub(crate) const MAX_MALFORMED_FRAME: i64 = 1 << 20;
38pub(crate) const MAGIC: [u8; 4] = *b"TFMP";
39pub(crate) const KIND_HEADER: u8 = 1;
40pub(crate) const KIND_ACK: u8 = 2;
41pub(crate) const STATUS_OK: u8 = 0;
42
43pub(crate) const BYTE_ORDER_LE: u8 = 1;
45pub(crate) const BYTE_ORDER_BE: u8 = 2;
47
48pub(crate) const HOST_BYTE_ORDER: u8 = if cfg!(target_endian = "little") {
50 BYTE_ORDER_LE
51} else {
52 BYTE_ORDER_BE
53};
54
55#[derive(Clone, Copy, Debug, PartialEq, Eq)]
57pub(crate) struct Header {
58 pub(crate) version: u16,
59 pub(crate) dtype_code: u8,
60 pub(crate) status: u8,
61 pub(crate) byte_order: u8,
62 pub(crate) elements: u64,
63 shape: [usize; MAX_WIRE_RANK],
64 rank: usize,
65}
66
67impl Header {
68 pub(crate) fn new(dtype_code: u8, byte_order: u8, elements: u64, shape: &[usize]) -> Self {
69 let mut extents = [0usize; MAX_WIRE_RANK];
70 for (slot, extent) in extents.iter_mut().zip(shape) {
71 *slot = *extent;
72 }
73 Self {
74 version: PROTOCOL_VERSION,
75 dtype_code,
76 status: STATUS_OK,
77 byte_order,
78 elements,
79 shape: extents,
80 rank: shape.len(),
81 }
82 }
83
84 pub(crate) fn abort(reason: u8) -> Self {
86 Self {
87 version: PROTOCOL_VERSION,
88 dtype_code: 0,
89 status: reason,
90 byte_order: 0,
91 elements: 0,
92 shape: [0usize; MAX_WIRE_RANK],
93 rank: 0,
94 }
95 }
96
97 pub(crate) fn rank(&self) -> usize {
98 self.rank
99 }
100
101 pub(crate) fn shape(&self) -> &[usize] {
102 &self.shape[..self.rank]
103 }
104
105 pub(crate) fn encoded_len(&self) -> usize {
107 HEADER_PROLOGUE + SHAPE_STRIDE * self.rank
108 }
109
110 pub(crate) fn encode(&self, buffer: &mut [u8]) -> usize {
112 let len = self.encoded_len();
113 let out = &mut buffer[..len];
114 out[..4].copy_from_slice(&MAGIC);
115 out[4..6].copy_from_slice(&self.version.to_le_bytes());
116 out[6] = KIND_HEADER;
117 out[7] = self.dtype_code;
118 out[8] = self.status;
119 out[9] = self.byte_order;
120 out[10..12].fill(0);
121 out[12..16].copy_from_slice(&(self.rank as u32).to_le_bytes());
122 out[16..24].copy_from_slice(&self.elements.to_le_bytes());
123 for (index, extent) in self.shape().iter().enumerate() {
124 let start = HEADER_PROLOGUE + SHAPE_STRIDE * index;
125 out[start..start + SHAPE_STRIDE].copy_from_slice(&(*extent as u64).to_le_bytes());
126 }
127 len
128 }
129
130 pub(crate) fn decode(bytes: &[u8]) -> std::result::Result<Self, DecodeError> {
136 if bytes.len() < HEADER_PROLOGUE {
137 return Err(DecodeError::Length("shorter than the fixed header"));
138 }
139 if bytes[..4] != MAGIC {
140 return Err(DecodeError::Magic);
141 }
142 if bytes[6] != KIND_HEADER {
143 return Err(DecodeError::Kind(bytes[6]));
144 }
145 let version = u16::from_le_bytes([bytes[4], bytes[5]]);
146 let rank = u32::from_le_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]) as usize;
147 if rank > MAX_WIRE_RANK {
148 return Err(DecodeError::Length("declared rank exceeds MAX_WIRE_RANK"));
149 }
150 if bytes.len() != HEADER_PROLOGUE + SHAPE_STRIDE * rank {
151 return Err(DecodeError::Length(
152 "length does not match the declared rank",
153 ));
154 }
155 let mut shape = [0usize; MAX_WIRE_RANK];
156 for (index, extent) in shape.iter_mut().enumerate().take(rank) {
157 let start = HEADER_PROLOGUE + SHAPE_STRIDE * index;
158 let raw = u64::from_le_bytes(
159 bytes[start..start + SHAPE_STRIDE]
160 .try_into()
161 .map_err(|_| DecodeError::Length("truncated shape extent"))?,
162 );
163 *extent = usize::try_from(raw).map_err(|_| DecodeError::Extent)?;
164 }
165 Ok(Self {
166 version,
167 dtype_code: bytes[7],
168 status: bytes[8],
169 byte_order: bytes[9],
170 elements: u64::from_le_bytes(
171 bytes[16..24]
172 .try_into()
173 .map_err(|_| DecodeError::Length("truncated element count"))?,
174 ),
175 shape,
176 rank,
177 })
178 }
179}
180
181#[derive(Clone, Copy, Debug, PartialEq, Eq)]
183pub(crate) struct Ack {
184 pub(crate) version: u16,
185 pub(crate) verdict: u8,
186}
187
188impl Ack {
189 pub(crate) fn new(verdict: u8) -> Self {
190 Self {
191 version: PROTOCOL_VERSION,
192 verdict,
193 }
194 }
195
196 pub(crate) fn encode(&self) -> [u8; ACK_LEN] {
197 let mut out = [0u8; ACK_LEN];
198 out[..4].copy_from_slice(&MAGIC);
199 out[4..6].copy_from_slice(&self.version.to_le_bytes());
200 out[6] = KIND_ACK;
201 out[7] = self.verdict;
202 out
203 }
204
205 pub(crate) fn decode(bytes: &[u8]) -> std::result::Result<Self, DecodeError> {
206 if bytes.len() < ACK_LEN {
207 return Err(DecodeError::Length("ack frame is shorter than 8 bytes"));
208 }
209 if bytes[..4] != MAGIC {
210 return Err(DecodeError::Magic);
211 }
212 if bytes[6] != KIND_ACK {
213 return Err(DecodeError::Kind(bytes[6]));
214 }
215 Ok(Self {
216 version: u16::from_le_bytes([bytes[4], bytes[5]]),
217 verdict: bytes[7],
218 })
219 }
220}
221
222#[derive(Debug, Clone, Copy, PartialEq, Eq)]
224pub(crate) enum DecodeError {
225 Magic,
227 Length(&'static str),
229 Kind(u8),
231 Extent,
233}
234
235pub(crate) fn dtype_code(dtype: DType) -> Option<u8> {
237 match dtype {
238 DType::Bool => Some(1),
239 DType::I32 => Some(2),
240 DType::I64 => Some(3),
241 DType::F32 => Some(4),
242 DType::F64 => Some(5),
243 DType::C32 => Some(6),
244 DType::C64 => Some(7),
245 DType::External(_) => None,
246 }
247}
248
249pub(crate) fn dtype_from_code(code: u8) -> Option<DType> {
251 match code {
252 1 => Some(DType::Bool),
253 2 => Some(DType::I32),
254 3 => Some(DType::I64),
255 4 => Some(DType::F32),
256 5 => Some(DType::F64),
257 6 => Some(DType::C32),
258 7 => Some(DType::C64),
259 _ => None,
260 }
261}
262
263pub(crate) fn element_bytes(code: u8) -> Option<usize> {
265 dtype_from_code(code).map(|dtype| match dtype {
266 DType::Bool => std::mem::size_of::<bool>(),
267 DType::I32 => std::mem::size_of::<i32>(),
268 DType::I64 => std::mem::size_of::<i64>(),
269 DType::F32 => std::mem::size_of::<f32>(),
270 DType::F64 => std::mem::size_of::<f64>(),
271 DType::C32 => std::mem::size_of::<Complex32>(),
272 DType::C64 => std::mem::size_of::<Complex64>(),
273 DType::External(_) => 0,
274 })
275}
276
277pub(crate) fn checked_elements(shape: &[usize]) -> Option<u64> {
279 let mut total: u64 = 1;
280 for extent in shape {
281 total = total.checked_mul(*extent as u64)?;
282 }
283 Some(total)
284}
285
286pub(crate) fn checked_payload_bytes(elements: u64, element_bytes: usize) -> Option<i64> {
288 let bytes = elements.checked_mul(element_bytes as u64)?;
289 i64::try_from(bytes).ok()
290}
291
292pub(crate) trait WireScalar: TensorScalar + Default + WireScalarSealed {
298 const VALIDATES_PAYLOAD: bool;
304
305 fn bytes(slice: &[Self]) -> &[u8];
307 fn bytes_mut(slice: &mut [Self]) -> &mut [u8];
309 fn assign_from_bytes(out: &mut [Self], bytes: &[u8]) -> bool;
312}
313
314mod sealed {
315 pub(crate) trait WireScalarSealed {}
316}
317
318use sealed::WireScalarSealed;
319
320macro_rules! plain_wire_scalar {
323 ($($ty:ty),* $(,)?) => {$(
324 impl WireScalarSealed for $ty {}
325
326 impl WireScalar for $ty {
327 const VALIDATES_PAYLOAD: bool = false;
328
329 fn bytes(slice: &[Self]) -> &[u8] {
330 view_as_bytes(slice)
331 }
332
333 fn bytes_mut(slice: &mut [Self]) -> &mut [u8] {
334 view_as_bytes_mut(slice)
335 }
336
337 fn assign_from_bytes(_out: &mut [Self], _bytes: &[u8]) -> bool {
338 unreachable!("a plain scalar payload is received in place")
341 }
342 }
343 )*};
344}
345
346plain_wire_scalar!(f32, f64, i32, i64, Complex32, Complex64);
347
348impl WireScalarSealed for bool {}
349
350impl WireScalar for bool {
351 const VALIDATES_PAYLOAD: bool = true;
352
353 fn bytes(slice: &[Self]) -> &[u8] {
354 bool_wire::as_bytes(slice)
355 }
356
357 fn bytes_mut(slice: &mut [Self]) -> &mut [u8] {
358 bool_wire::as_bytes_mut(slice)
362 }
363
364 fn assign_from_bytes(out: &mut [Self], bytes: &[u8]) -> bool {
365 bool_wire::assign(out, bytes)
366 }
367}
368
369fn view_as_bytes<T>(slice: &[T]) -> &[u8] {
378 unsafe { std::slice::from_raw_parts(slice.as_ptr().cast::<u8>(), std::mem::size_of_val(slice)) }
380}
381
382fn view_as_bytes_mut<T>(slice: &mut [T]) -> &mut [u8] {
384 unsafe {
386 std::slice::from_raw_parts_mut(
387 slice.as_mut_ptr().cast::<u8>(),
388 std::mem::size_of_val(slice),
389 )
390 }
391}