|
| 1 | +use crate::ArrayLayout; |
| 2 | +use std::fmt; |
| 3 | + |
| 4 | +impl<const N: usize> ArrayLayout<N> { |
| 5 | + /// 高维数组格式化。 |
| 6 | + pub fn write_array<T: fmt::Display>( |
| 7 | + &self, |
| 8 | + f: &mut fmt::Formatter, |
| 9 | + ptr: *const T, |
| 10 | + ) -> fmt::Result { |
| 11 | + match self.ndim() { |
| 12 | + 0 => { |
| 13 | + write!(f, "array<> = [{}]", unsafe { |
| 14 | + ptr.byte_offset(self.offset()).read_unaligned() |
| 15 | + }) |
| 16 | + } |
| 17 | + 1 => { |
| 18 | + let &[n] = self.shape() else { unreachable!() }; |
| 19 | + let &[s] = self.strides() else { unreachable!() }; |
| 20 | + |
| 21 | + writeln!(f, "array<{n}>[")?; |
| 22 | + let ptr = unsafe { ptr.byte_offset(self.offset()) }; |
| 23 | + for i in 0..n as isize { |
| 24 | + writeln!(f, " {}", unsafe { |
| 25 | + ptr.byte_offset(i * s).read_unaligned() |
| 26 | + })? |
| 27 | + } |
| 28 | + writeln!(f, "]")?; |
| 29 | + Ok(()) |
| 30 | + } |
| 31 | + _ => { |
| 32 | + let mut title = "array<".to_string(); |
| 33 | + for d in self.shape() { |
| 34 | + title.push_str(&format!("{d}x")) |
| 35 | + } |
| 36 | + assert_eq!(title.pop(), Some('x')); |
| 37 | + title.push('>'); |
| 38 | + |
| 39 | + let mut stack = Vec::with_capacity(self.ndim() - 2); |
| 40 | + self.write_recursive(f, ptr, &title, &mut stack) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + fn write_recursive<T: fmt::Display>( |
| 46 | + &self, |
| 47 | + f: &mut fmt::Formatter, |
| 48 | + ptr: *const T, |
| 49 | + title: &str, |
| 50 | + indices: &mut Vec<usize>, |
| 51 | + ) -> fmt::Result { |
| 52 | + match *self.shape() { |
| 53 | + [] | [_] => unreachable!(), |
| 54 | + [rows, cols] => { |
| 55 | + write!(f, "{title}[")?; |
| 56 | + for i in indices { |
| 57 | + write!(f, "{i}, ")? |
| 58 | + } |
| 59 | + writeln!(f, "..]")?; |
| 60 | + |
| 61 | + let &[rs, cs] = self.strides() else { |
| 62 | + unreachable!() |
| 63 | + }; |
| 64 | + |
| 65 | + let ptr = unsafe { ptr.byte_offset(self.offset()) }; |
| 66 | + for r in 0..rows as isize { |
| 67 | + for c in 0..cols as isize { |
| 68 | + write!(f, "{} ", unsafe { |
| 69 | + ptr.byte_offset(r * rs + c * cs).read_unaligned() |
| 70 | + })? |
| 71 | + } |
| 72 | + writeln!(f)? |
| 73 | + } |
| 74 | + } |
| 75 | + [batch, ..] => { |
| 76 | + for i in 0..batch { |
| 77 | + indices.push(i); |
| 78 | + self.index(0, i).write_recursive(f, ptr, title, indices)?; |
| 79 | + indices.pop(); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + Ok(()) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[test] |
| 88 | +fn test() { |
| 89 | + const DATA: &[u8] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; |
| 90 | + |
| 91 | + struct Tensor(ArrayLayout<4>); |
| 92 | + |
| 93 | + impl fmt::Display for Tensor { |
| 94 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 95 | + self.0.write_array(f, DATA.as_ptr()) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + let tensor = Tensor(ArrayLayout::<4>::new_contiguous( |
| 100 | + &[DATA.len()], |
| 101 | + crate::Endian::BigEndian, |
| 102 | + 1, |
| 103 | + )); |
| 104 | + println!("{}", tensor); |
| 105 | + |
| 106 | + let tensor = Tensor(tensor.0.tile_be(0, &[1, DATA.len()]).broadcast(0, 6)); |
| 107 | + println!("{}", tensor); |
| 108 | + |
| 109 | + let tensor = Tensor(tensor.0.tile_be(0, &[2, 3]).tile_be(2, &[5, 2])); |
| 110 | + println!("{}", tensor); |
| 111 | +} |
0 commit comments