# Pickle.Defer when loading PyTorch tensors in Julia

**URL:** https://discourse.julialang.org/t/pickle-defer-when-loading-pytorch-tensors-in-julia/136313
**Category:** General Usage
**Created:** [March 20, 2026, 7:49pm UTC](https://discourse.julialang.org/t/pickle-defer-when-loading-pytorch-tensors-in-julia/136313 "2026-03-20T19:49:38Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Uneeb\_Hasan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uneeb_hasan/32/221116_2.png) [@Uneeb\_Hasan](https://discourse.julialang.org/u/Uneeb_Hasan)
#### Post date: [March 20, 2026, 7:49pm UTC](https://discourse.julialang.org/t/pickle-defer-when-loading-pytorch-tensors-in-julia/136313/1 "2026-03-20T19:49:38Z")

</div>

Hi everyone! I’m working on implementing the ZINC molecular dataset for MLDatasets.jl and ran into something I wanted to get thoughts on.  
The raw ZINC data is distributed as Python pickle files where the tensors are stored as PyTorch tensors (`torch.Tensor`). When I load them with `Pickle.jl`, instead of actual arrays I get `Pickle.Defer` objects (which makes sense since Pickle.jl has no idea what a PyTorch tensor is).

My current workaround is a one-time Python conversion script that unpacks the tensors via `.numpy()` and saves everything as flat NPZ files, which `NPZ.jl` reads perfectly. Then the Julia loader just reads the NPZ.

Is there a cleaner pure-Julia approach? Has anyone dealt with PyTorch pickle files in Julia before? Is `Pickle.Defer` something that can be handled/extended, or is the Python preprocessing step just the accepted pattern for datasets like this?

 ![Screenshot from 2026-03-21 01-03-04](https://global.discourse-cdn.com/julialang/original/3X/1/a/1a7e7d51c83f26143c72bd1048599ffc7862cef0.png)

You can reproduce this yourself without needing the ZINC dataset at all

# create\_test.py

import torch, pickle

data = [  
{“atom\_type”: torch.tensor([6, 7, 8, 6]), # atom types  
“bond\_type”: torch.zeros(4, 4).long(), # bond adjacency matrix  
“logP\_SA\_cycle\_normalized”: torch.tensor(-0.5)}  
]

with open(“test.pickle”, “wb”) as f:  
pickle.dump(data, f)

print(“test.pickle created!”)

# test\_load.jl

using Pickle

data = Pickle.load(“test.pickle”)  
mol = data[1]

@show typeof(mol[“atom\_type”]) # → Pickle.Defer (expected Array!)  
@show typeof(mol[“logP\_SA\_cycle\_normalized”]) # → Pickle.Defer

atom\_type = Int.(mol[“atom\_type”]) # MethodError: no method matching length(::Pickle.Defer)
