Skip to main content

The Last-Word Safe-Tensor Stream Suite

Project description

stsw - The Last-Word Safe-Tensor Stream Suite

PyPI Python Version License CI Coverage

Perfectionist-grade Stream Writer & Stream Reader, designed once so no-one ever has to rewrite them.

Features

  • 🚀 Streaming I/O: Write and read multi-GB tensor files with <100 MB RAM
  • 🔒 Type Safe: 100% type hints, pyright strict mode
  • Zero Copy: Memory-mapped reading with no deserialization overhead
  • 🛡️ Robust: CRC32 verification, atomic writes, comprehensive error handling
  • 🔧 Simple API: import stsw → do work → close() → done
  • 🌍 Compatible: Bit-level identical to safetensors spec v1.0

Installation

pip install stsw

With optional dependencies:

pip install stsw[torch,numpy]  # For PyTorch/NumPy support
pip install stsw[all]          # Everything including dev tools

Quick Start

Writing tensors

import numpy as np
from stsw import StreamWriter, TensorMeta

# Define your tensors
data1 = np.random.rand(1000, 1000).astype(np.float32)
data2 = np.random.randint(0, 256, (500, 500, 3), dtype=np.uint8)

# Create metadata
metas = [
    TensorMeta("embeddings", "F32", data1.shape, 0, data1.nbytes),
    TensorMeta("image", "I8", data2.shape, 4000064, 4000064 + data2.nbytes),
]

# Write to file
with StreamWriter.open("model.safetensors", metas, crc32=True) as writer:
    writer.write_block("embeddings", data1.tobytes())
    writer.finalize_tensor("embeddings")
    
    writer.write_block("image", data2.tobytes())
    writer.finalize_tensor("image")

Reading tensors

from stsw import StreamReader

# Open file with memory mapping
with StreamReader("model.safetensors", verify_crc=True) as reader:
    # List available tensors
    print(reader.keys())  # ['embeddings', 'image']
    
    # Load as NumPy array
    embeddings = reader.to_numpy("embeddings")
    
    # Load as PyTorch tensor (if available)
    image = reader.to_torch("image", device="cuda")

High-level API

import torch
import stsw

# Save entire state dict
state_dict = {
    "model.weight": torch.randn(1000, 1000),
    "model.bias": torch.randn(1000),
}

stsw.dump(state_dict, "checkpoint.safetensors", crc32=True)

CLI Tools

# Inspect file contents
stsw inspect model.safetensors

# Verify checksums
stsw verify model.safetensors

# Convert PyTorch checkpoint
stsw convert model.pt model.safetensors --crc32

# Run self-test
stsw selftest

Performance

Operation Throughput Memory Usage
Write (NVMe) 1.8 GB/s <80 MB
Read (mmap) 6.2 GB/s <50 MB
CRC32 verification 2.5 GB/s <80 MB

Development

# Install development dependencies
make dev

# Run full test suite
make all

# Type checking
make type

# Run tests
make test

# Format code
make format

Documentation

Full documentation available at https://stsw-project.github.io/stsw

License

Apache-2.0. See LICENSE for details.

Citation

If you use stsw in your research, please cite:

@software{stsw,
  title = {stsw: The Last-Word Safe-Tensor Stream Suite},
  year = {2025},
  url = {https://github.com/stsw-project/stsw}
}

Your last proof to the universe: pip install stsw → you possess a tool that cannot be out-engineered for its purpose within the constraints of physics and CPython. Nothing left to streamline – only data to move.

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

stsw-1.0.0.tar.gz (28.5 kB view details)

Uploaded Source

Built Distribution

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

stsw-1.0.0-py3-none-any.whl (30.4 kB view details)

Uploaded Python 3

File details

Details for the file stsw-1.0.0.tar.gz.

File metadata

  • Download URL: stsw-1.0.0.tar.gz
  • Upload date:
  • Size: 28.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.9

File hashes

Hashes for stsw-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e25801d4212aa0883c32235bb335c7c1983d7824782a928894e499a80a13860a
MD5 83e4ddf4e1f273c6d719bd5988b022b8
BLAKE2b-256 172aa004c7f44fb855161a704f264af7d86a194bccf6949133e3b7c890997ed9

See more details on using hashes here.

File details

Details for the file stsw-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: stsw-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 30.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.9

File hashes

Hashes for stsw-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3832b79124b9b8e932aefb5bc3292ce8675acf504281201a5866bc80730d5170
MD5 e65acc3ff6389eff667ee4a20e1a69ef
BLAKE2b-256 674f5ae45ccadd95dd09bd3c318d7626264ca4ad3cbcf0dfe0791c9ad35276f9

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