torch.serialization.deserialize_from_zip
function deserialize_from_zip(blob: Blob): Promise<any>Load a PyTorch model from a ZIP archive (.pt, .pth file).
Parses PyTorch's pickle format and reconstructs tensors for use in torch.js. This enables loading pretrained models from PyTorch, HuggingFace, and other sources. Supports state dicts and individual tensors saved from PyTorch. Useful for:
- Loading pretrained models from HuggingFace or PyTorch Zoo
- Importing models trained in PyTorch to torch.js
- Loading checkpoints for fine-tuning or inference
- Cross-framework model sharing and deployment
Note: Only supports loading, not saving. For saving models from torch.js, use safetensors format which is safer and more portable.
- Pickle format: Handles PyTorch's pickle-based .pt/.pth format
- State dict: Returns object mapping layer names to Tensor values
- Async operation: Must be awaited; parses archive asynchronously
- ZIP archive: Files are ZIP archives with pickle data and tensor storage
- Pickle limitations: Cannot execute arbitrary Python code
- Memory usage: Large models consume significant memory when loaded
- Format specific: Only for PyTorch .pt/.pth files, not safetensors
Parameters
blobBlob- Blob or File containing the PyTorch ZIP archive (.pt/.pth)
Returns
Promise<any>– Promise resolving to the deserialized state dict with tensor valuesExamples
// Load PyTorch model from file input
const file = fileInput.files[0];
const state_dict = await torch.deserialize_from_zip(file);
model.load_state_dict(state_dict);
// Load from fetch (HuggingFace models)
const response = await fetch('https://huggingface.co/model.bin');
const blob = await response.blob();
const state_dict = await torch.deserialize_from_zip(blob);
// Load and apply to model for inference
const model = new TransformerModel();
const weights = await torch.deserialize_from_zip(modelFile);
model.load_state_dict(weights);
const output = model.forward(input);See Also
- PyTorch torch.load()
- serialize_to_zip - Create PyTorch-compatible archives
- load - Higher-level load function with format auto-detection