pub struct TensorShape { /* private fields */ }Expand description
Tensor shape normalized to outermost-first order.
GGUF stores dims innermost-first (ne[0] is the contiguous row
length). Those dims are reversed here, so a GGUF [4096, 32000]
token embedding becomes [32000, 4096], which is the same order a
Safetensors header uses. Self::native_dims recovers the on-disk
order losslessly.
Implementations§
Source§impl TensorShape
impl TensorShape
Sourcepub fn outermost_first(dims: Vec<usize>) -> Self
pub fn outermost_first(dims: Vec<usize>) -> Self
Build a shape from dims that are already outermost-first.
Sourcepub fn from_innermost_first(native: &[usize]) -> Self
pub fn from_innermost_first(native: &[usize]) -> Self
Build a shape from native innermost-first dims (GGUF Tensor::dims).
Sourcepub fn dims(&self) -> &[usize]
pub fn dims(&self) -> &[usize]
Dims in outermost-first order.
Examples found in repository?
examples/checkpoint_smoke/run.rs (line 294)
289fn verify_tensor_metadata(t: &engram_parser::TensorInfo, e: &Expected) -> Result<()> {
290 require(
291 t.name == e.name
292 && t.native_dtype == e.dtype
293 && t.dtype.label() == expected_dtype_label(&e.dtype)
294 && t.shape.dims() == e.shape
295 && t.byte_len == e.bytes
296 && t.location.source == e.shard
297 && t.location.file_offset == Some(e.offset),
298 format!(
299 "metadata mismatch for {:?}: actual={t:?}, expected={e:?}",
300 e.name
301 ),
302 )
303}More examples
examples/inspect_checkpoint.rs (line 92)
66fn report(ckpt: &dyn Checkpoint, rows: usize) -> Result<()> {
67 let source = ckpt.source();
68 let tensors = ckpt.tensors();
69 let total_bytes: usize = tensors.iter().map(|t| t.byte_len).sum();
70 println!("format: {} ({})", ckpt.format(), source.kind);
71 println!("path: {}", source.path.display());
72 println!("root: {}", source.root.display());
73 if let Some(index) = &source.index_file {
74 println!("index: {index}");
75 }
76 println!("files: {}", source.files.len());
77 println!("metadata keys: {}", ckpt.metadata().len());
78 println!("tensors: {}", tensors.len());
79 println!("payload bytes: {total_bytes}");
80 println!();
81
82 println!(
83 "{:<48} {:<10} {:<10} {:<22} {:>12} location",
84 "name", "dtype", "native", "shape (outer-first)", "bytes"
85 );
86 for t in tensors.iter().take(rows) {
87 println!(
88 "{:<48} {:<10} {:<10} {:<22} {:>12} {}@{}",
89 t.name,
90 t.dtype.label(),
91 t.native_dtype,
92 format!("{:?}", t.shape.dims()),
93 t.byte_len,
94 t.location.source,
95 t.location.data_offset,
96 );
97 }
98 if tensors.len() > rows {
99 println!("… {} more (use --all)", tensors.len() - rows);
100 }
101 println!();
102
103 let mut histogram: BTreeMap<String, (usize, usize)> = BTreeMap::new();
104 for t in tensors {
105 let entry = histogram.entry(t.native_dtype.clone()).or_default();
106 entry.0 += 1;
107 entry.1 += t.byte_len;
108 }
109 println!("dtype histogram (native label: count, bytes):");
110 for (dtype, (count, bytes)) in &histogram {
111 println!(" {dtype:<12} {count:>6} {bytes:>14}");
112 }
113
114 // Probe the smallest non-empty tensor, and skip it past a size cap so
115 // owned (non-mmap) backends never allocate a huge buffer here.
116 let probe = tensors
117 .iter()
118 .filter(|t| t.byte_len > 0)
119 .min_by_key(|t| t.byte_len);
120 if let Some(big) = probe.filter(|t| t.byte_len > PROBE_CAP_BYTES) {
121 println!();
122 println!(
123 "payload probe skipped: smallest tensor '{}' is {} bytes (> {PROBE_CAP_BYTES})",
124 big.name, big.byte_len
125 );
126 } else if let Some(probe) = probe {
127 let bytes = ckpt.tensor_bytes(&probe.name)?;
128 assert_eq!(bytes.len(), probe.byte_len, "payload length mismatch");
129 println!();
130 println!(
131 "payload probe: '{}' -> {} bytes (matches byte_len)",
132 probe.name,
133 bytes.len()
134 );
135 }
136 Ok(())
137}Sourcepub fn native_order(&self) -> DimOrder
pub fn native_order(&self) -> DimOrder
Order the source format stores dims in.
Sourcepub fn native_dims(&self) -> Vec<usize>
pub fn native_dims(&self) -> Vec<usize>
Dims in the source format’s native order.
Sourcepub fn element_count(&self) -> Option<usize>
pub fn element_count(&self) -> Option<usize>
Product of dims, or None on overflow. A rank-0 shape has one element.
Trait Implementations§
Source§impl Clone for TensorShape
impl Clone for TensorShape
Source§impl Debug for TensorShape
impl Debug for TensorShape
impl Eq for TensorShape
Source§impl Hash for TensorShape
impl Hash for TensorShape
Source§impl PartialEq for TensorShape
impl PartialEq for TensorShape
impl StructuralPartialEq for TensorShape
Auto Trait Implementations§
impl Forget for TensorShape
impl Freeze for TensorShape
impl Move for TensorShape
impl RefUnwindSafe for TensorShape
impl Send for TensorShapewhere
RawVecInner: Send,
impl Sync for TensorShapewhere
RawVecInner: Sync,
impl Unpin for TensorShape
impl UnsafeUnpin for TensorShape
impl UnwindSafe for TensorShapewhere
RawVecInner: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more