Skip to main content

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.flock is 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 cattensors ships a .pth file into your site-packages. Python evaluates .pth files at every interpreter startup, which means import cattensors.startup runs in every Python process in that environment; before your code, before python -c, before pytest, etc. It then monkey-patches safetensors globally, and also flashpack if it is installed.

This is intentional and is what makes the library zero-config, but it has two implications you should know about:

  1. Global side effects. Any process in the env that imports safetensors gets the patched version, even if it never imports cattensors. To opt out per-process, set CATTENSORS_AUTO_PATCH=false before Python starts. To opt out entirely, uninstall the package or remove cattensors.pth from site-packages.
  2. Install-time trust. Because .pth runs 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_PATCH is evaluated once at interpreter startup (set it before Python starts).
  • Other flags are read when used, so changes affect subsequent load calls.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cattensors-0.1.0.tar.gz (139.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cattensors-0.1.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

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

Hashes for cattensors-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6bfcc924d577621c2df9fb4c8e18c5edc798a8a62d357f7d443732ccedcce2b1
MD5 70f8456d890437196596bb4e4dea2ca6
BLAKE2b-256 7287030f296de29bf492938023feaaaa2a4aeea007ac9cd691ed78ad6fd7ccbc

See more details on using hashes here.

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

Hashes for cattensors-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7c149bf3f3fb5aa37e7a5af1b348ff147d658618dcbe51d214d2124334ff5a5e
MD5 7247def171b401fcb555b6ca2322c4a2
BLAKE2b-256 b17da76d0ed99a0c564f331451eab5872dc791cdfb8d82db4ee88843e371b39d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page