Skip to main content

Python bindings for the zTensor library.

Project description

ztensor

Crates.io Docs.rs PyPI License: MIT

Simple tensor serialization format

Key Features

  • Simple Spec — Minimalist spec for easy parsing.
  • Zero-Copy Access — Instant memory-mapping (mmap) with no RAM overhead.
  • Efficient Writes — Supports streaming and append-only operations without rewriting files.
  • Future-Proof — Decouples physical storage from logical representation for long-term compatibility.

Ecosystem

  • Rust Core — High-performance, SIMD-aligned implementation.
  • Python API — First-class bindings for NumPy and PyTorch.
  • Universal Converters — CLI tools to easily convert Pickle, SafeTensors, and GGUF files.

Comparison

Feature zTensor SafeTensors GGUF Pickle HDF5
Zero-Copy Read ⚠️
Safe (No Exec)
Streaming / Append
Sparse Tensors
Compression
Quantization
Parser Complexity 🟢 Low 🟢 Low 🟡 Med 🔴 High 🔴 High

Installation

Python

pip install ztensor

Rust

[dependencies]
ztensor = "0.1"

CLI

cargo install ztensor-cli

Quick Start: Python

Basic Usage with NumPy

import numpy as np
from ztensor import Writer, Reader

# Write tensors
with Writer("model.zt") as w:
    w.add_tensor("weights", np.random.randn(1024, 768).astype(np.float32))
    w.add_tensor("bias", np.zeros(768, dtype=np.float32))

# Read tensors (zero-copy where possible)
with Reader("model.zt") as r:
    # Returns a numpy-like view
    weights = r.read_tensor("weights")
    print(f"Weights shape: {weights.shape}, dtype: {weights.dtype}")

PyTorch Integration

import torch
from ztensor import Writer, Reader

# Write PyTorch tensors directly
t = torch.randn(10, 10)
with Writer("torch_model.zt") as w:
    w.add_tensor("embedding", t)

# Read back as PyTorch tensors
with Reader("torch_model.zt") as r:
    # 'to="torch"' returns a torch.Tensor sharing memory with the file (if mmap)
    embedding = r.read_tensor("embedding", to="torch")
    print(embedding.size())

Sparse Tensors

Supports CSR (Compressed Sparse Row) and COO (Coordinate) formats.

import scipy.sparse
from ztensor import Writer, Reader

csr = scipy.sparse.csr_matrix([[1, 0], [0, 2]], dtype=np.float32)

with Writer("sparse.zt") as w:
    # Add CSR tensor
    w.add_sparse_csr("my_csr", csr.data, csr.indices, csr.indptr, csr.shape)

with Reader("sparse.zt") as r:
    # Read back as scipy.sparse.csr_matrix
    matrix = r.read_tensor("my_csr", to="numpy")

Compression

Use Zstandard (zstd) compression to reduce file size.

with Writer("compressed.zt") as w:
    w.add_tensor("big_data", data, compress=True)

Quick Start: Rust

Basic Usage

use ztensor::{ZTensorWriter, ZTensorReader, DType, Encoding, ChecksumAlgorithm};

// Write
let mut writer = ZTensorWriter::create("model.zt")?;
writer.add_tensor("weights", vec![1024, 768], DType::Float32, 
                  Encoding::Raw, data_bytes, ChecksumAlgorithm::None)?;
writer.finalize()?;

// Read
let mut reader = ZTensorReader::open("model.zt")?;
// Read as specific type (automatically handles endianness)
let weights: Vec<f32> = reader.read_tensor_as("weights")?;

Sparse Tensors

// Write CSR
writer.add_csr_tensor(
    "sparse_data",
    vec![100, 100],      // shape
    DType::Float32,
    values_bytes,        // standard LE bytes
    indices,             // Vec<u64>
    indptr,              // Vec<u64>
    Encoding::Raw,
    ChecksumAlgorithm::None
)?;

// Read CSR
let csr = reader.read_csr_tensor::<f32>("sparse_data")?;
println!("Values: {:?}", csr.values);

Compression

// Write with compression
writer.add_tensor(
    "compressed_data",
    vec![512, 512],
    DType::Float32,
    Encoding::Zstd, // Use zstd encoding
    data_bytes,
    ChecksumAlgorithm::Crc32c // Optional checksum
)?;

// Read (auto-decompresses)
let data: Vec<f32> = reader.read_tensor_as("compressed_data")?;

CLI

The ztensor CLI tool allows you to inspect and manipulate zTensor files.

Inspect Metadata

Print tensor names, shapes, and properties.

ztensor info model.zt

Convert Other Formats

Convert SafeTensors, GGUF, or Pickle files to zTensor.

# Auto-detect format from extension
ztensor convert model.safetensors -o model.zt

# Explicit format with compression
ztensor convert -f gguf -c llama.gguf -o llama.zt

# Delete originals after conversion
ztensor convert --delete-original *.safetensors -o model.zt

Compression Tools

# Compress an existing raw file
ztensor compress raw.zt -o compressed.zt

# Decompress a file
ztensor decompress compressed.zt -o raw.zt

Merge Files

Combine multiple zTensor files into one.

ztensor merge part1.zt part2.zt -o merged.zt

Download from HuggingFace

Download safetensors from HuggingFace and convert to zTensor:

ztensor download-hf microsoft/resnet-18 -o ./models
ztensor download-hf openai-community/gpt2 -o ./models --compress

Supported Data Types

Type Description
float32, float16, bfloat16, float64 Floating point
int8, int16, int32, int64 Signed integers
uint8, uint16, uint32, uint64 Unsigned integers
bool Boolean

File Format

See SPEC.md for the complete specification.

License

MIT

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

ztensor-1.0.1.tar.gz (46.6 kB view details)

Uploaded Source

Built Distributions

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

ztensor-1.0.1-py3-none-win_amd64.whl (434.3 kB view details)

Uploaded Python 3Windows x86-64

ztensor-1.0.1-py3-none-win32.whl (392.2 kB view details)

Uploaded Python 3Windows x86

ztensor-1.0.1-py3-none-musllinux_1_2_x86_64.whl (965.5 kB view details)

Uploaded Python 3musllinux: musl 1.2+ x86-64

ztensor-1.0.1-py3-none-musllinux_1_2_i686.whl (987.6 kB view details)

Uploaded Python 3musllinux: musl 1.2+ i686

ztensor-1.0.1-py3-none-musllinux_1_2_armv7l.whl (951.6 kB view details)

Uploaded Python 3musllinux: musl 1.2+ ARMv7l

ztensor-1.0.1-py3-none-musllinux_1_2_aarch64.whl (914.8 kB view details)

Uploaded Python 3musllinux: musl 1.2+ ARM64

ztensor-1.0.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (754.1 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

ztensor-1.0.1-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl (747.1 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ s390x

ztensor-1.0.1-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (829.6 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ ppc64le

ztensor-1.0.1-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (685.7 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARMv7l

ztensor-1.0.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (693.2 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64

ztensor-1.0.1-py3-none-manylinux_2_5_i686.manylinux1_i686.whl (771.6 kB view details)

Uploaded Python 3manylinux: glibc 2.5+ i686

ztensor-1.0.1-py3-none-macosx_11_0_arm64.whl (569.8 kB view details)

Uploaded Python 3macOS 11.0+ ARM64

File details

Details for the file ztensor-1.0.1.tar.gz.

File metadata

  • Download URL: ztensor-1.0.1.tar.gz
  • Upload date:
  • Size: 46.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for ztensor-1.0.1.tar.gz
Algorithm Hash digest
SHA256 7b29d3a92cbc86c83f14bbb7325184cc086aa273f54fcbe3dd8ba2d517319645
MD5 b6305256b16c66d683eea2d14a965183
BLAKE2b-256 e9858eedb75da71a459cb9c62fed7f6ef67458df1c3870aed6529f345c3c2a2f

See more details on using hashes here.

File details

Details for the file ztensor-1.0.1-py3-none-win_amd64.whl.

File metadata

  • Download URL: ztensor-1.0.1-py3-none-win_amd64.whl
  • Upload date:
  • Size: 434.3 kB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for ztensor-1.0.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 e478a678f1473ca7ac9cf38f502fe611b5ba8c92d28052e5bfc0c880f02d57d4
MD5 c11e18d4155f782eac4ccc9b9b412bff
BLAKE2b-256 59f22435c66878360bb979f2b373d59fceee05cbf2320bc9bfa4e039ac7abde9

See more details on using hashes here.

File details

Details for the file ztensor-1.0.1-py3-none-win32.whl.

File metadata

  • Download URL: ztensor-1.0.1-py3-none-win32.whl
  • Upload date:
  • Size: 392.2 kB
  • Tags: Python 3, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for ztensor-1.0.1-py3-none-win32.whl
Algorithm Hash digest
SHA256 b57744be69e4bfdd1a8f6ab28e9efaaac3ce8db6a9316d5376c7e5740a9810e4
MD5 37ad27bfd45a08ee2421f1bb45c2200f
BLAKE2b-256 e3dcfbae825f622be63f4d664d240dd459b4308280d0b1160d2891eb84159ad8

See more details on using hashes here.

File details

Details for the file ztensor-1.0.1-py3-none-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ztensor-1.0.1-py3-none-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c02920f8b439f1558dcf40c9bd3eee8ee637f083ed6db75ef4f0b6209bc24c43
MD5 74178b7c5c59a6af3179a5f2a8fb3b93
BLAKE2b-256 a93a546906ee97bf8b2714d7a078a995f78147df612d1b1b50623f48ac65a375

See more details on using hashes here.

File details

Details for the file ztensor-1.0.1-py3-none-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for ztensor-1.0.1-py3-none-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 d96274bf0c729950504bac6ca1d1ece49bceace279afe70bf5045c8f460a945f
MD5 b830b94d46231844b68f2f725927861d
BLAKE2b-256 cb5a20ad203cf283723863df532620279656036e0965a7e7db278ead19251f4e

See more details on using hashes here.

File details

Details for the file ztensor-1.0.1-py3-none-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for ztensor-1.0.1-py3-none-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 171b18500bd301babbda16b79621e5b278fad5cd1fd2c72e6046ee3f16201c3c
MD5 6e94e0334d54f21ab4cf0259c6ad9e83
BLAKE2b-256 472f1632a766583cd937769e80c24b4cd64b351af4da92f6b197f7ad68d5f262

See more details on using hashes here.

File details

Details for the file ztensor-1.0.1-py3-none-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for ztensor-1.0.1-py3-none-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 007a6cc6183b342a623dd106b3c8a02492e07734063d5d5d8881361786970075
MD5 814dddf065b303e0f0c7be976afaefce
BLAKE2b-256 25f4c3ddb0bd72013f973b13f34e704ab65e635fb4c635f34d4fe7b696847122

See more details on using hashes here.

File details

Details for the file ztensor-1.0.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ztensor-1.0.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7ba43a607a74674e364fbba271c6fa0d9613af61cbcf6ef1aea843ed947803df
MD5 c042ec14f069f92187d7ae6b77c75df5
BLAKE2b-256 db3c1e66fc6cc1f59f8cc8e1caa303ddbcd91c8c4600e59b0adc6973e3584d64

See more details on using hashes here.

File details

Details for the file ztensor-1.0.1-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for ztensor-1.0.1-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 5798980833394f7ace13c7858b9efba949622419ed18d23a104f5f5f48f8eaaa
MD5 c23ce8d2e7b01471554f1b9c4f1825dc
BLAKE2b-256 8316d3b61ac881b00b0d4417d89be06166c1e4290203e352fa1685c198112d58

See more details on using hashes here.

File details

Details for the file ztensor-1.0.1-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for ztensor-1.0.1-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 c132acf44bab8b0d8f5bf79110ac47519411ae890f169a25bc7922cb0fe02d43
MD5 19e7659537cc50fff0d22076a26cd22e
BLAKE2b-256 10faeefa4cc37241fbabc1b8cd5c10a1f5ec33498c7741281137c021c72ca87d

See more details on using hashes here.

File details

Details for the file ztensor-1.0.1-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for ztensor-1.0.1-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 76d7860efc438c814fe44001e4b8f98a55a3a3a9fbc9dab1496252b054ac56ec
MD5 65396149b8b67cdbe9b639c72546daa9
BLAKE2b-256 4fe6ee5bd03cd9b1be5febd7ff6ee6d06f6c7ce848f1212cd9b5cd36efc47bb6

See more details on using hashes here.

File details

Details for the file ztensor-1.0.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for ztensor-1.0.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8335eaf89aa3a4fc4272db8ef3950f054db137715068b99a966a4a2469d8a7eb
MD5 695a4db62f8b4756fb0e0a86a87a7c9e
BLAKE2b-256 2ac2bff5f0eb205812426a79209a6d23693ea10d7d1fdf4177361e995874254b

See more details on using hashes here.

File details

Details for the file ztensor-1.0.1-py3-none-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for ztensor-1.0.1-py3-none-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 bc119ef38bc53d95530ca9c462ff6aa83ff915362478777638c94a67ac42bf23
MD5 652c09f82fc442ee346ef687ebcdb46f
BLAKE2b-256 b77ed6172d4a4e74a0eca9c1e7a8e992886182d5a7b9e217742c0c3b331d042c

See more details on using hashes here.

File details

Details for the file ztensor-1.0.1-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ztensor-1.0.1-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 02a181d6c72a864dfc774dcc76b6ac9d1d8a6eea269dd470c05dd809882fe019
MD5 4fcda74d7713fc276b64ee7f84152dcb
BLAKE2b-256 e79ac0ca2dd21c4ce339ba4048be0e66a4e84317a825a0f2ce6d14405b238c15

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