Fast safetensors loading for distributed filesystems
Project description
cattensors
Faster safetensors loading for JuiceFS.
Note: This library was developed and benchmarked against JuiceFS, but in theory should work with any network backed distributed filesystem.
cattensors monkey-patches safetensors to pre-warm the OS page cache before tensor reads. On network backed filesystems, the first read of a large model file is slow. cattensors reads the file into page cache using parallel threads so subsequent mmap based access hits RAM instead of the network.
Compatibility
- POSIX platforms (Linux/macOS)
- Not supported on Windows (
fcntl.flockis used for cross-process coordination)
Installation
pip install git+https://github.com/badayvedat/cattensors@16ebb9ab66a59747c810530c2d4ca1b53bb41539
After installation, cattensors is auto-activated at interpreter startup via a .pth file, so no code changes are required.
To disable this behavior, set CATTENSORS_AUTO_PATCH environment variable to false
[!WARNING]
Installing
cattensorsships a.pthfile into yoursite-packages. Python evaluates.pthfiles at every interpreter startup, which meansimport cattensors.startupruns in every Python process in that environment; before your code, beforepython -c, beforepytest, etc. It then monkey-patchessafetensorsglobally, and alsoflashpackif it is installed.This is intentional and is what makes the library zero-config, but it has two implications you should know about:
- Global side effects. Any process in the env that imports
safetensorsgets the patched version, even if it never importscattensors. To opt out per-process, setCATTENSORS_AUTO_PATCH=falsebefore Python starts. To opt out entirely, uninstall the package or removecattensors.pthfromsite-packages.- Install-time trust. Because
.pthruns unconditionally, installing this package means trusting the source you installed it from to run code in every future Python invocation in that environment. Prefer pinning to a specific commit/tag, and avoid installing into shared/system Python environments.
Configuration
Environment variables:
| Variable | Default | Description |
|---|---|---|
CATTENSORS_AUTO_PATCH |
true |
Auto-patch safetensors (and flashpack, if installed) at interpreter startup |
CATTENSORS_WARM_THREADS |
8 |
Parallel reader threads per file |
CATTENSORS_PARALLEL_SHARDS |
false |
Pre-warm sibling shards in background when loading sharded models |
HF_ENABLE_PARALLEL_LOADING |
false |
If true, cattensors skips sibling-shard parallel warming to avoid overlapping I/O |
CATTENSORS_DEBUG |
false |
Enable debug logs to stderr |
Behavior notes:
CATTENSORS_AUTO_PATCHis evaluated once at interpreter startup (set it before Python starts).- Other flags are read when used, so changes affect subsequent load calls.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cattensors-0.1.0.tar.gz.
File metadata
- Download URL: cattensors-0.1.0.tar.gz
- Upload date:
- Size: 139.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6bfcc924d577621c2df9fb4c8e18c5edc798a8a62d357f7d443732ccedcce2b1
|
|
| MD5 |
70f8456d890437196596bb4e4dea2ca6
|
|
| BLAKE2b-256 |
7287030f296de29bf492938023feaaaa2a4aeea007ac9cd691ed78ad6fd7ccbc
|
File details
Details for the file cattensors-0.1.0-py3-none-any.whl.
File metadata
- Download URL: cattensors-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c149bf3f3fb5aa37e7a5af1b348ff147d658618dcbe51d214d2124334ff5a5e
|
|
| MD5 |
7247def171b401fcb555b6ca2322c4a2
|
|
| BLAKE2b-256 |
b17da76d0ed99a0c564f331451eab5872dc791cdfb8d82db4ee88843e371b39d
|