ztensor 1.2.3

Unified, zero-copy, and safe I/O for deep learning formats
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! PyTorch pickle file reader.
//!
//! Provides read-only access to PyTorch `.pt` / `.bin` / `.pkl` files through a unified API.
//! PyTorch files are ZIP archives containing a pickle file with model structure
//! and raw storage files with tensor data.
//!
//! When opened from a file path, uses memory-mapping for zero-copy reads of
//! uncompressed (STORED) ZIP entries. Falls back to buffered reads for compressed
//! entries or generic readers.
//!
//! Requires the `pickle` feature.

use std::collections::BTreeMap;
use std::fs::File;
use std::io::{self, Read, Seek};
use std::path::Path;

use memmap2::{Mmap, MmapOptions};

use crate::error::Error;
use crate::models::{DType, Manifest, Object};
use crate::pickle_vm::{parse_pytorch_pickle, PtTensorInfo};
use crate::reader::{TensorData, TensorElement, TensorReader};

/// ZIP local file header signature (PK\x03\x04).
const ZIP_LOCAL_HEADER_MAGIC: [u8; 4] = [0x50, 0x4b, 0x03, 0x04];

/// Storage backend for PyTorch files.
enum StorageBackend {
    /// Zero-copy: file is mmapped, offsets point directly into it.
    Mmap {
        mmap: Mmap,
        /// storage_key → (absolute_data_offset, data_length) in the mmap.
        storage_offsets: BTreeMap<String, (usize, usize)>,
    },
    /// Buffered: storage was eagerly read into memory.
    Buffered { data: BTreeMap<String, Vec<u8>> },
}

/// Reader for PyTorch (.pt / .bin) files.
///
/// When opened from a file path via [`PyTorchReader::open`], uses memory-mapping
/// for zero-copy reads of uncompressed ZIP entries. When opened from a generic
/// reader via [`PyTorchReader::from_reader`], loads storage data eagerly into memory.
pub struct PyTorchReader {
    pub manifest: Manifest,
    storage: StorageBackend,
    /// Maps tensor name → (storage_key, byte_offset, byte_length, dtype).
    tensor_locations: BTreeMap<String, TensorLocation>,
}

#[derive(Debug, Clone)]
struct TensorLocation {
    storage_key: String,
    byte_offset: usize,
    byte_length: usize,
    dtype: DType,
}

impl PyTorchReader {
    /// Opens a PyTorch .pt file from a path.
    ///
    /// Uses memory-mapping for zero-copy reads when all storage entries are
    /// uncompressed (STORED). Falls back to buffered reads otherwise.
    pub fn open(path: impl AsRef<Path>) -> Result<Self, Error> {
        let path = path.as_ref();
        let file = File::open(path)?;
        let mut archive = zip::ZipArchive::new(file).map_err(|e| {
            Error::InvalidFileStructure(format!("Not a valid ZIP/PyTorch file: {}", e))
        })?;

        let pickle_name = find_pickle_file(&archive)?;
        let tensor_infos = {
            let mut pkl_file = archive.by_name(&pickle_name).map_err(|e| {
                Error::InvalidFileStructure(format!("Cannot read '{}': {}", pickle_name, e))
            })?;
            let mut pkl_data = Vec::new();
            pkl_file.read_to_end(&mut pkl_data)?;
            parse_pytorch_pickle(&mut io::Cursor::new(pkl_data))?
        };

        if tensor_infos.is_empty() {
            return Err(Error::InvalidFileStructure(
                "No tensors found in PyTorch file".to_string(),
            ));
        }

        let prefix = find_data_prefix(&archive, &tensor_infos);
        let (manifest, tensor_locations) = build_manifest_and_locations(&tensor_infos);
        let storage_keys = unique_storage_keys(&tensor_infos);

        // Scan ZIP entries for storage files and record metadata
        let mut entry_headers: BTreeMap<String, (u64, u64)> = BTreeMap::new();
        let mut all_stored = true;

        for i in 0..archive.len() {
            let entry = match archive.by_index_raw(i) {
                Ok(e) => e,
                Err(_) => continue,
            };
            let entry_name = entry.name().to_string();
            let header_start = entry.header_start();
            let size = entry.size();
            let is_stored = entry.compression() == zip::CompressionMethod::Stored;
            drop(entry);

            for key in &storage_keys {
                let full_path = format!("{}{}", prefix, key);
                if entry_name == full_path || entry_name == *key {
                    if !is_stored {
                        all_stored = false;
                    }
                    entry_headers
                        .entry(key.clone())
                        .or_insert((header_start, size));
                }
            }
        }

        if all_stored && entry_headers.len() == storage_keys.len() {
            // Mmap path: all storage entries are uncompressed
            let file = File::open(path)?;
            let mmap = unsafe { MmapOptions::new().map(&file)? };
            let storage_offsets = compute_mmap_offsets(&mmap, &entry_headers)?;

            Ok(Self {
                manifest,
                storage: StorageBackend::Mmap {
                    mmap,
                    storage_offsets,
                },
                tensor_locations,
            })
        } else {
            // Buffered fallback
            let data = read_all_storage(&mut archive, &storage_keys, &prefix)?;
            Ok(Self {
                manifest,
                storage: StorageBackend::Buffered { data },
                tensor_locations,
            })
        }
    }

    /// Creates a reader from any `Read + Seek` source.
    ///
    /// All storage data is loaded eagerly into memory.
    pub fn from_reader<R: Read + Seek>(reader: R) -> Result<Self, Error> {
        let mut archive = zip::ZipArchive::new(reader).map_err(|e| {
            Error::InvalidFileStructure(format!("Not a valid ZIP/PyTorch file: {}", e))
        })?;

        let pickle_name = find_pickle_file(&archive)?;
        let tensor_infos = {
            let mut pkl_file = archive.by_name(&pickle_name).map_err(|e| {
                Error::InvalidFileStructure(format!("Cannot read '{}': {}", pickle_name, e))
            })?;
            let mut pkl_data = Vec::new();
            pkl_file.read_to_end(&mut pkl_data)?;
            parse_pytorch_pickle(&mut io::Cursor::new(pkl_data))?
        };

        if tensor_infos.is_empty() {
            return Err(Error::InvalidFileStructure(
                "No tensors found in PyTorch file".to_string(),
            ));
        }

        let prefix = find_data_prefix(&archive, &tensor_infos);
        let (manifest, tensor_locations) = build_manifest_and_locations(&tensor_infos);
        let storage_keys = unique_storage_keys(&tensor_infos);
        let data = read_all_storage(&mut archive, &storage_keys, &prefix)?;

        Ok(Self {
            manifest,
            storage: StorageBackend::Buffered { data },
            tensor_locations,
        })
    }

    /// Returns a borrowed slice into the storage for the given key.
    fn get_storage_slice(&self, key: &str) -> Result<&[u8], Error> {
        match &self.storage {
            StorageBackend::Mmap {
                mmap,
                storage_offsets,
            } => {
                let &(offset, len) = storage_offsets.get(key).ok_or_else(|| {
                    Error::InvalidFileStructure(format!(
                        "Storage '{}' not found in mmap offsets",
                        key
                    ))
                })?;
                Ok(&mmap[offset..offset + len])
            }
            StorageBackend::Buffered { data } => {
                data.get(key).map(Vec::as_slice).ok_or_else(|| {
                    Error::InvalidFileStructure(format!("Storage '{}' not found in buffer", key))
                })
            }
        }
    }

    /// Returns a borrowed slice of tensor data.
    fn get_tensor_slice(&self, name: &str) -> Result<&[u8], Error> {
        let loc = self
            .tensor_locations
            .get(name)
            .ok_or_else(|| Error::ObjectNotFound(name.to_string()))?;

        let storage = self.get_storage_slice(&loc.storage_key)?;

        let end = loc.byte_offset + loc.byte_length;
        if end > storage.len() {
            return Err(Error::InvalidFileStructure(format!(
                "Tensor '{}' data out of bounds: {}..{} > {}",
                name,
                loc.byte_offset,
                end,
                storage.len()
            )));
        }

        Ok(&storage[loc.byte_offset..end])
    }

    /// Reads raw byte data for a tensor (returns owned copy).
    pub fn read(&self, name: &str) -> Result<Vec<u8>, Error> {
        Ok(self.get_tensor_slice(name)?.to_vec())
    }

    /// Reads tensor data as a typed vector.
    pub fn read_as<T: TensorElement>(&self, name: &str) -> Result<Vec<T>, Error> {
        let loc = self
            .tensor_locations
            .get(name)
            .ok_or_else(|| Error::ObjectNotFound(name.to_string()))?;

        if T::DTYPE != loc.dtype {
            return Err(Error::TypeMismatch {
                expected: loc.dtype.as_str().to_string(),
                found: std::any::type_name::<T>().to_string(),
                context: format!("object '{}'", name),
            });
        }

        crate::reader::bytes_to_typed_vec(self.get_tensor_slice(name)?)
    }
}

impl TensorReader for PyTorchReader {
    fn manifest(&self) -> &Manifest {
        &self.manifest
    }

    fn read_data(&self, name: &str) -> Result<TensorData<'_>, Error> {
        let slice = self.get_tensor_slice(name)?;
        Ok(TensorData::Borrowed(slice))
    }
}

// =========================================================================
// Helper functions
// =========================================================================

/// Builds manifest and tensor locations from parsed tensor infos.
fn build_manifest_and_locations(
    tensor_infos: &[PtTensorInfo],
) -> (Manifest, BTreeMap<String, TensorLocation>) {
    let mut objects = BTreeMap::new();
    let mut tensor_locations = BTreeMap::new();

    for info in tensor_infos {
        let byte_length = info.numel * info.dtype.byte_size();

        let obj = Object::dense(
            info.shape.clone(),
            info.dtype,
            info.storage_offset as u64,
            byte_length as u64,
        );

        tensor_locations.insert(
            info.name.clone(),
            TensorLocation {
                storage_key: info.storage_key.clone(),
                byte_offset: info.storage_offset,
                byte_length,
                dtype: info.dtype,
            },
        );

        objects.insert(info.name.clone(), obj);
    }

    let manifest = Manifest {
        version: "pytorch".to_string(),
        attributes: None,
        objects,
    };

    (manifest, tensor_locations)
}

/// Collects unique, sorted storage keys from tensor infos.
fn unique_storage_keys(tensor_infos: &[PtTensorInfo]) -> Vec<String> {
    let mut keys: Vec<String> = tensor_infos
        .iter()
        .map(|info| info.storage_key.clone())
        .collect();
    keys.sort();
    keys.dedup();
    keys
}

/// Computes mmap data offsets by parsing ZIP local file headers.
fn compute_mmap_offsets(
    mmap: &Mmap,
    entry_headers: &BTreeMap<String, (u64, u64)>,
) -> Result<BTreeMap<String, (usize, usize)>, Error> {
    let mut storage_offsets = BTreeMap::new();

    for (key, (header_start, size)) in entry_headers {
        let hs = *header_start as usize;

        if hs + 30 > mmap.len() {
            return Err(Error::InvalidFileStructure(format!(
                "ZIP local file header for storage '{}' is truncated",
                key
            )));
        }

        // Validate local file header magic
        if mmap[hs..hs + 4] != ZIP_LOCAL_HEADER_MAGIC {
            return Err(Error::InvalidFileStructure(format!(
                "Invalid ZIP local file header for storage '{}'",
                key
            )));
        }

        let fname_len = u16::from_le_bytes([mmap[hs + 26], mmap[hs + 27]]) as usize;
        let extra_len = u16::from_le_bytes([mmap[hs + 28], mmap[hs + 29]]) as usize;
        let data_offset = hs + 30 + fname_len + extra_len;
        let data_len = *size as usize;

        if data_offset + data_len > mmap.len() {
            return Err(Error::InvalidFileStructure(format!(
                "Storage '{}' data extends beyond file",
                key
            )));
        }

        storage_offsets.insert(key.clone(), (data_offset, data_len));
    }

    Ok(storage_offsets)
}

/// Reads all storage entries from a ZIP archive into memory.
fn read_all_storage<R: Read + Seek>(
    archive: &mut zip::ZipArchive<R>,
    storage_keys: &[String],
    prefix: &str,
) -> Result<BTreeMap<String, Vec<u8>>, Error> {
    let mut data = BTreeMap::new();

    for key in storage_keys {
        let storage_path = format!("{}{}", prefix, key);
        let entry_data = read_zip_entry(archive, &storage_path)
            .or_else(|| read_zip_entry(archive, key))
            .ok_or_else(|| {
                Error::InvalidFileStructure(format!(
                    "Storage file '{}' not found in PyTorch archive",
                    key
                ))
            })?;
        data.insert(key.clone(), entry_data);
    }

    Ok(data)
}

fn read_zip_entry<R: Read + Seek>(archive: &mut zip::ZipArchive<R>, name: &str) -> Option<Vec<u8>> {
    let mut entry = archive.by_name(name).ok()?;
    let mut data = Vec::new();
    entry.read_to_end(&mut data).ok()?;
    Some(data)
}

fn find_pickle_file<R: Read + Seek>(archive: &zip::ZipArchive<R>) -> Result<String, Error> {
    let names: Vec<String> = (0..archive.len())
        .filter_map(|i| archive.name_for_index(i).map(|s| s.to_string()))
        .collect();

    // Common patterns
    for pattern in &["archive/data.pkl", "data.pkl"] {
        if names.iter().any(|n| n == *pattern) {
            return Ok(pattern.to_string());
        }
    }

    // Find any .pkl file
    for name in &names {
        if name.ends_with(".pkl") {
            return Ok(name.clone());
        }
    }

    Err(Error::InvalidFileStructure(
        "No pickle file found in PyTorch archive".to_string(),
    ))
}

fn find_data_prefix<R: Read + Seek>(
    archive: &zip::ZipArchive<R>,
    tensor_infos: &[PtTensorInfo],
) -> String {
    if tensor_infos.is_empty() {
        return String::new();
    }

    let names: Vec<String> = (0..archive.len())
        .filter_map(|i| archive.name_for_index(i).map(|s| s.to_string()))
        .collect();

    let first_key = &tensor_infos[0].storage_key;

    // Try common prefixes
    for prefix in &["archive/data/", "data/", ""] {
        let path = format!("{}{}", prefix, first_key);
        if names.iter().any(|n| n == &path) {
            return prefix.to_string();
        }
    }

    // Dynamic prefix: find any zip entry ending with /<first_key>
    let suffix = format!("/{}", first_key);
    for name in &names {
        if name.ends_with(&suffix) {
            return name[..name.len() - first_key.len()].to_string();
        }
    }

    String::new()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_pytorch_reader_structure() {
        // Create a minimal PyTorch-like ZIP file
        let mut file = NamedTempFile::new().unwrap();
        let path = file.path().to_path_buf();

        {
            let mut zip = zip::ZipWriter::new(&mut file);

            // Write a minimal pickle that creates a state dict
            // This is a hand-crafted pickle protocol 2 bytestream:
            // { "weight": _rebuild_tensor_v2(FloatStorage("0", "cpu", 6), 0, (2,3), (3,1), False, OrderedDict()) }
            let pickle_data = build_test_pickle();
            let options = zip::write::SimpleFileOptions::default()
                .compression_method(zip::CompressionMethod::Stored);
            zip.start_file("archive/data.pkl", options).unwrap();
            zip.write_all(&pickle_data).unwrap();

            // Write the raw storage data (6 f32 values)
            let data: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
            let bytes: &[u8] = bytemuck::cast_slice(&data);
            zip.start_file("archive/data/0", options).unwrap();
            zip.write_all(bytes).unwrap();

            zip.finish().unwrap();
        }

        let reader = PyTorchReader::open(&path);
        match reader {
            Ok(r) => {
                assert!(!r.tensors().is_empty());
                // Verify we found the tensor
                for (name, obj) in r.tensors() {
                    assert_eq!(obj.shape, vec![2, 3]);
                    let data: Vec<f32> = r.read_as(name).unwrap();
                    assert_eq!(data, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
                }
            }
            Err(e) => {
                // If pickle parsing fails, that's OK for this minimal test
                eprintln!("PyTorch reader test (expected in some configs): {}", e);
            }
        }
    }

    /// Build a minimal pickle protocol 2 bytestream for testing.
    fn build_test_pickle() -> Vec<u8> {
        let mut p = Vec::new();

        // PROTO 2
        p.push(0x80);
        p.push(2);

        // Create the OrderedDict result
        p.push(0x7d); // EMPTY_DICT

        // MARK for SETITEMS
        p.push(0x28);

        // Key: "weight"
        p.push(0x8c);
        p.push(6);
        p.extend_from_slice(b"weight");

        // Value: _rebuild_tensor_v2(...)
        // Push the global
        p.push(0x63);
        p.extend_from_slice(b"torch._utils\n_rebuild_tensor_v2\n");

        // Push args tuple using MARK...TUPLE
        p.push(0x28); // MARK

        // arg0: storage (via BINPERSID)
        p.push(0x28); // MARK for persistent_id tuple
        p.push(0x8c);
        p.push(7);
        p.extend_from_slice(b"storage");
        p.push(0x63);
        p.extend_from_slice(b"torch\nFloatStorage\n");
        p.push(0x8c);
        p.push(1);
        p.extend_from_slice(b"0");
        p.push(0x8c);
        p.push(3);
        p.extend_from_slice(b"cpu");
        p.push(0x4b);
        p.push(6); // numel=6
        p.push(0x74); // TUPLE
        p.push(0x51); // BINPERSID

        // arg1: storage_offset = 0
        p.push(0x4b);
        p.push(0);

        // arg2: size = (2, 3)
        p.push(0x4b);
        p.push(2);
        p.push(0x4b);
        p.push(3);
        p.push(0x86); // TUPLE2

        // arg3: stride = (3, 1)
        p.push(0x4b);
        p.push(3);
        p.push(0x4b);
        p.push(1);
        p.push(0x86); // TUPLE2

        // arg4: requires_grad = False
        p.push(0x89);

        // arg5: OrderedDict()
        p.push(0x63);
        p.extend_from_slice(b"collections\nOrderedDict\n");
        p.push(0x29); // EMPTY_TUPLE
        p.push(0x52); // REDUCE

        p.push(0x74); // TUPLE (closes the args MARK)

        // REDUCE: _rebuild_tensor_v2(args)
        p.push(0x52);

        // SETITEMS
        p.push(0x75);

        // STOP
        p.push(0x2e);

        p
    }
}