ztensor-compat 2.1.1

Foreign tensor formats projected into the zTensor object model
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! ONNX → zTensor object model projection.
//!
//! Extracts graph initializers (the model weights) from the protobuf
//! stream with a minimal hand-written wire-format parser, so no protobuf
//! dependency. A `raw_data` tensor is a plain range of the file, so it gets an
//! address and a borrow; the typed repeated fields (`float_data`,
//! `int32_data`, ...) have to be converted to little-endian bytes per the ONNX
//! storage rules (small types are stored one element per int32), so they exist
//! only once this reader has built them, so they are an opaque payload and
//! report as much.
//!
//! Graphs, nodes, and attributes are out of scope: this reads weights,
//! not computation. External data files are refused, not resolved.

use std::borrow::Cow;
use std::collections::BTreeMap;

use ztensor::provide::{Catalog, Decode};
use ztensor::provide::{Entry, Location, PartEntry, Payload};
use ztensor::{DType, Error, Result, Store, StoreId, Vocabulary};

use crate::project::Projection;

fn bad(detail: impl Into<String>) -> Error {
    Error::InvalidInput(format!("onnx: {}", detail.into()))
}

/// ONNX TensorProto.DataType → (storage, logical).
fn map_dtype(id: u64) -> Result<(DType, Option<&'static str>)> {
    Ok(match id {
        1 => (DType::F32, None),
        2 => (DType::U8, None),
        3 => (DType::I8, None),
        4 => (DType::U16, None),
        5 => (DType::I16, None),
        6 => (DType::I32, None),
        7 => (DType::I64, None),
        9 => (DType::U8, Some("bool")),
        10 => (DType::F16, None),
        11 => (DType::F64, None),
        12 => (DType::U32, None),
        13 => (DType::U64, None),
        16 => (DType::BF16, None),
        17 => (DType::U8, Some("f8_e4m3fn")),
        18 => (DType::U8, Some("f8_e4m3fnuz")),
        19 => (DType::U8, Some("f8_e5m2")),
        20 => (DType::U8, Some("f8_e5m2fnuz")),
        other => {
            return Err(Error::Unsupported(format!(
                "onnx data type {other} has no registered projection"
            )))
        }
    })
}

// ---- protobuf wire format ---------------------------------------------

const VARINT: u32 = 0;
const I64BIT: u32 = 1;
const LEN: u32 = 2;
const I32BIT: u32 = 5;

struct Pb<'a> {
    data: &'a [u8],
    pos: usize,
    /// Absolute offset of `data[0]` in the file, for zero-copy ranges.
    base: usize,
}

impl<'a> Pb<'a> {
    fn done(&self) -> bool {
        self.pos >= self.data.len()
    }

    fn varint(&mut self) -> Result<u64> {
        let mut v = 0u64;
        let mut shift = 0;
        loop {
            let b = *self
                .data
                .get(self.pos)
                .ok_or_else(|| bad("truncated varint"))?;
            self.pos += 1;
            v |= ((b & 0x7f) as u64) << shift;
            if b & 0x80 == 0 {
                return Ok(v);
            }
            shift += 7;
            if shift >= 64 {
                return Err(bad("varint too long"));
            }
        }
    }

    fn tag(&mut self) -> Result<(u32, u32)> {
        let v = self.varint()?;
        Ok(((v >> 3) as u32, (v & 7) as u32))
    }

    fn bytes(&mut self) -> Result<(usize, &'a [u8])> {
        let len = self.varint()? as usize;
        let end = self
            .pos
            .checked_add(len)
            .filter(|&e| e <= self.data.len())
            .ok_or_else(|| bad("length-delimited field truncated"))?;
        let abs = self.base + self.pos;
        let s = &self.data[self.pos..end];
        self.pos = end;
        Ok((abs, s))
    }

    fn skip(&mut self, wire: u32) -> Result<()> {
        match wire {
            VARINT => {
                self.varint()?;
            }
            I64BIT => {
                self.pos = self
                    .pos
                    .checked_add(8)
                    .filter(|&e| e <= self.data.len())
                    .ok_or_else(|| bad("truncated fixed64"))?;
            }
            LEN => {
                self.bytes()?;
            }
            I32BIT => {
                self.pos = self
                    .pos
                    .checked_add(4)
                    .filter(|&e| e <= self.data.len())
                    .ok_or_else(|| bad("truncated fixed32"))?;
            }
            other => return Err(bad(format!("unknown wire type {other}"))),
        }
        Ok(())
    }
}

// ---- TensorProto ------------------------------------------------------

enum TensorData {
    Raw { offset: u64, length: u64 },
    Owned(Vec<u8>),
}

struct TensorInfo {
    name: String,
    dims: Vec<u64>,
    data_type: u64,
    data: TensorData,
}

fn parse_tensor(data: &[u8], base: usize) -> Result<TensorInfo> {
    let mut pb = Pb { data, pos: 0, base };
    let mut name = String::new();
    let mut dims = Vec::new();
    let mut data_type = 0u64;
    let mut raw: Option<(u64, u64)> = None;
    // The typed repeated fields, kept as their wire integers until the
    // data type is known.
    let mut i32s: Vec<u32> = Vec::new();
    let mut i64s: Vec<u64> = Vec::new();
    let mut f32s: Vec<u8> = Vec::new();
    let mut f64s: Vec<u8> = Vec::new();
    let mut external = false;

    while !pb.done() {
        let (field, wire) = pb.tag()?;
        match (field, wire) {
            (1, LEN) => {
                let (_, b) = pb.bytes()?;
                let mut sub = Pb {
                    data: b,
                    pos: 0,
                    base: 0,
                };
                while !sub.done() {
                    dims.push(sub.varint()?);
                }
            }
            (1, VARINT) => dims.push(pb.varint()?),
            (2, VARINT) => data_type = pb.varint()?,
            (4, LEN) => f32s.extend_from_slice(pb.bytes()?.1), // packed floats: LE bytes
            (5, LEN) => {
                let (_, b) = pb.bytes()?;
                let mut sub = Pb {
                    data: b,
                    pos: 0,
                    base: 0,
                };
                while !sub.done() {
                    i32s.push(sub.varint()? as u32);
                }
            }
            (5, VARINT) => i32s.push(pb.varint()? as u32),
            (7, LEN) | (11, LEN) => {
                let (_, b) = pb.bytes()?;
                let mut sub = Pb {
                    data: b,
                    pos: 0,
                    base: 0,
                };
                while !sub.done() {
                    i64s.push(sub.varint()?);
                }
            }
            (7, VARINT) | (11, VARINT) => i64s.push(pb.varint()?),
            (8, LEN) => {
                name = String::from_utf8(pb.bytes()?.1.to_vec())
                    .map_err(|_| bad("tensor name is not UTF-8"))?;
            }
            (9, LEN) => {
                let (abs, b) = pb.bytes()?;
                raw = Some((abs as u64, b.len() as u64));
            }
            (10, LEN) => f64s.extend_from_slice(pb.bytes()?.1), // packed doubles
            (13, LEN) => {
                // external_data entries: presence alone means the payload
                // lives in another file.
                external = true;
                pb.bytes()?;
            }
            (14, VARINT) => {
                if pb.varint()? == 1 {
                    external = true;
                }
            }
            (_, w) => pb.skip(w)?,
        }
    }

    if external {
        return Err(Error::Unsupported(format!(
            "onnx: tensor {name:?} uses external data; internalize it first \
             (onnx.load_external_data_for_model)"
        )));
    }

    let (dtype, _ltype) = map_dtype(data_type)?;
    let width = dtype.width() as usize;

    // Assemble owned bytes from typed fields per ONNX storage rules:
    // int32_data carries every type of width ≤ 4 (one element per entry),
    // int64_data carries i64, uint64_data carries u32/u64, float/double
    // are packed IEEE bytes.
    if raw.is_some() && !(f32s.is_empty() && f64s.is_empty() && i32s.is_empty() && i64s.is_empty())
    {
        return Err(bad(format!(
            "tensor {name:?} carries both raw_data and a typed data field"
        )));
    }
    let data = if let Some((offset, length)) = raw {
        TensorData::Raw { offset, length }
    } else if !f32s.is_empty() {
        TensorData::Owned(f32s)
    } else if !f64s.is_empty() {
        TensorData::Owned(f64s)
    } else if !i32s.is_empty() {
        let mut out = Vec::with_capacity(i32s.len() * width);
        for v in &i32s {
            out.extend_from_slice(&v.to_le_bytes()[..width.min(4)]);
        }
        TensorData::Owned(out)
    } else if !i64s.is_empty() {
        let mut out = Vec::with_capacity(i64s.len() * width);
        for v in &i64s {
            out.extend_from_slice(&v.to_le_bytes()[..width.min(8)]);
        }
        TensorData::Owned(out)
    } else {
        TensorData::Owned(Vec::new())
    };

    Ok(TensorInfo {
        name,
        dims,
        data_type,
        data,
    })
}

// ---- projection -------------------------------------------------------

/// The typed repeated fields, converted once at open and handed out on
/// request. They have no address: nothing in the file holds these bytes in the
/// layout a consumer wants.
struct Typed {
    buffers: Vec<Vec<u8>>,
}

impl Decode for Typed {
    fn decode(&self, key: u64, decoded_len: u64) -> Result<Vec<u8>> {
        let bytes = self
            .buffers
            .get(key as usize)
            .ok_or_else(|| bad(format!("no converted tensor {key}")))?;
        if bytes.len() as u64 != decoded_len {
            return Err(bad("converted tensor changed size"));
        }
        Ok(bytes.clone())
    }
}

pub(crate) fn project(store: &Store) -> Result<Projection> {
    // ONNX keeps its weights inline in the protobuf stream and gives no index,
    // so there is no header to read on its own: an unmapped store has to read
    // the file to answer anything at all.
    let bytes: Cow<'_, [u8]> = match store.bytes() {
        Some(mapped) => Cow::Borrowed(mapped),
        None => Cow::Owned(store.read(0, store.len())?),
    };
    let buf: &[u8] = &bytes;
    if buf.is_empty() {
        return Err(bad("empty file"));
    }
    let vocab = Vocabulary::standard();

    // ModelProto.graph is field 7.
    let mut pb = Pb {
        data: buf,
        pos: 0,
        base: 0,
    };
    let mut graph: Option<(usize, usize)> = None;
    while !pb.done() {
        let (field, wire) = pb.tag()?;
        if field == 7 && wire == LEN {
            let (abs, b) = pb.bytes()?;
            graph = Some((abs, b.len()));
            break;
        }
        pb.skip(wire)?;
    }
    let (graph_base, graph_len) = graph.ok_or_else(|| bad("no graph field in ModelProto"))?;
    let graph_bytes = &buf[graph_base..graph_base + graph_len];

    // GraphProto.initializer is field 5.
    let mut catalog = Catalog::new();
    let mut converted: Vec<Vec<u8>> = Vec::new();
    let mut occupied: Vec<(u64, u64)> = Vec::new();
    let mut pb = Pb {
        data: graph_bytes,
        pos: 0,
        base: graph_base,
    };
    while !pb.done() {
        let (field, wire) = pb.tag()?;
        if field != 5 || wire != LEN {
            pb.skip(wire)?;
            continue;
        }
        let (abs, b) = pb.bytes()?;
        let info = parse_tensor(b, abs)?;
        if info.name.is_empty() {
            continue;
        }
        let (dtype, logical) = map_dtype(info.data_type)?;
        let elems = crate::safe::product("onnx shape", &info.dims)?;
        let expected = vocab
            .size_of(logical, dtype, elems)
            .ok_or_else(|| bad("size not computable"))?;
        let actual = match &info.data {
            TensorData::Raw { length, .. } => *length,
            TensorData::Owned(v) => v.len() as u64,
        };
        if actual != expected {
            return Err(bad(format!(
                "tensor {:?} holds {actual} bytes but shape implies {expected}",
                info.name
            )));
        }

        let payload = match info.data {
            TensorData::Raw { offset, length } => {
                occupied.push((offset, length));
                Payload::At(Location {
                    store: StoreId(0),
                    offset,
                    len: length,
                })
            }
            TensorData::Owned(v) => {
                converted.push(v);
                Payload::Opaque {
                    store: StoreId(0),
                    key: converted.len() as u64 - 1,
                    decoded_len: expected,
                }
            }
        };

        let mut parts = BTreeMap::new();
        parts.insert(
            "data".to_string(),
            PartEntry {
                dtype,
                logical: logical.map(str::to_string),
                payload,
                digest: None,
            },
        );
        if catalog
            .insert(
                info.name.clone(),
                Entry {
                    shape: info.dims,
                    layout: "dense".to_string(),
                    attributes: None,
                    parts,
                },
            )
            .is_some()
        {
            return Err(bad(format!("duplicate initializer {:?}", info.name)));
        }
    }

    // Weights are embedded in a protobuf stream with fields around and between
    // them, so what else shares a page is not knowable from here: occupancy
    // stays unstated and exclusivity is never claimed.
    let _ = occupied;
    let projection = Projection::new(catalog);
    Ok(if converted.is_empty() {
        projection
    } else {
        projection.with_decoder(Box::new(Typed { buffers: converted }))
    })
}