Developer Tools

trunk/5d300e84867a7170de93d4153912b347eedf931c: Make it possible to load safetensors with torch.load (#170592)

Loading .safetensors files just became a one-liner in PyTorch

Deep Dive

PyTorch's latest commit (5d300e8) on trunk introduces a major quality-of-life improvement for machine learning practitioners: direct loading of safetensors files via the standard torch.load() function. Previously, loading weights in the safetensors format—a popular alternative to pickle for its security and speed benefits—required separate libraries like huggingface/safetensors or manual conversion. This PR, authored by malfet and described as "mostly Claude coded," makes torch.load("foo.safetensors") automatically detect the safetensors format and return the underlying tensordict without any additional imports or wrappers.

The change aligns with PyTorch's ongoing efforts to simplify model interchange and reduce dependency overhead. Safetensors has become the de facto standard for sharing large language models and diffusion models on platforms like Hugging Face, as it avoids pickle's security risks (arbitrary code execution) and enables zero-copy memory mapping. With this merge, users can load any safetensors file as if it were a native PyTorch file, streamlining workflows from model downloading to fine-tuning. The PR was approved by albanD and merged on May 1, 2024. This is a small but impactful integration that removes one more friction point for developers working with the broader AI ecosystem.

Key Points
  • torch.load() now accepts .safetensors files directly, returning a tensordict
  • PR #170592 was authored by malfet and described as 'mostly Claude coded'
  • Eliminates need for separate safetensors library, improving security and interoperability

Why It Matters

Simplifies model loading workflows by removing boilerplate, making the entire PyTorch ecosystem safer and easier to use.