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 extra RAM copying overhead.
  • Efficient Writes — Supports streaming and append-only operations without rewriting files.
  • Future-Proof — Decouples physical storage from logical representation for long-term compatibility.

Tools

  • 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

Benchmark

benchmark

See benchmark for more details.

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, Compression, ChecksumAlgorithm};

// Write
let mut writer = ZTensorWriter::create("model.zt")?;
// Zero-copy generic write with generic Compression
writer.add_object("weights", vec![1024, 768], DType::F32, 
                 Compression::Raw, &data_vec, 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_object_as("weights")?;

Sparse Tensors

// Write CSR (using generic method)
writer.add_csr_object(
    "sparse_data",
    vec![100, 100],      // shape
    DType::F32,
    &values,             // &[f32]
    &indices,            // &[u64]
    &indptr,             // &[u64]
    Compression::Raw,
    ChecksumAlgorithm::None
)?;

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

Compression

// Write with compression (level 3)
writer.add_object(
    "compressed_data",
    vec![512, 512],
    DType::F32,
    Compression::Zstd(3), // Use zstd encoding with level 3
    &data,                // &[f32]
    ChecksumAlgorithm::Crc32c // Optional checksum
)?;

// Read (auto-decompresses)
let data: Vec<f32> = reader.read_object_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 (default level 3)
ztensor convert -f gguf -c llama.gguf -o llama.zt

# Specify compression level (1-22)
ztensor convert model.safetensors -o model.zt -l 10

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

Compression Tools

# Compress an existing raw file (default level 3)
ztensor compress raw.zt -o compressed.zt

# Compress with specific level (1-22)
ztensor compress raw.zt -o highly_compressed.zt -l 19

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

File Management

# Merge multiple files into one
ztensor merge part1.zt part2.zt -o merged.zt

# Migrate legacy v0.1.0 files to v1.1.0
ztensor migrate old_model.zt -o new_model.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.2.tar.gz (206.1 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.2-py3-none-win_amd64.whl (471.0 kB view details)

Uploaded Python 3Windows x86-64

ztensor-1.0.2-py3-none-win32.whl (426.3 kB view details)

Uploaded Python 3Windows x86

ztensor-1.0.2-py3-none-musllinux_1_2_x86_64.whl (1.0 MB view details)

Uploaded Python 3musllinux: musl 1.2+ x86-64

ztensor-1.0.2-py3-none-musllinux_1_2_i686.whl (1.0 MB view details)

Uploaded Python 3musllinux: musl 1.2+ i686

ztensor-1.0.2-py3-none-musllinux_1_2_armv7l.whl (992.8 kB view details)

Uploaded Python 3musllinux: musl 1.2+ ARMv7l

ztensor-1.0.2-py3-none-musllinux_1_2_aarch64.whl (954.5 kB view details)

Uploaded Python 3musllinux: musl 1.2+ ARM64

ztensor-1.0.2-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (798.7 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

ztensor-1.0.2-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl (784.0 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ s390x

ztensor-1.0.2-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (868.5 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ ppc64le

ztensor-1.0.2-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (725.1 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARMv7l

ztensor-1.0.2-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (731.7 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64

ztensor-1.0.2-py3-none-manylinux_2_5_i686.manylinux1_i686.whl (815.7 kB view details)

Uploaded Python 3manylinux: glibc 2.5+ i686

ztensor-1.0.2-py3-none-macosx_11_0_arm64.whl (599.8 kB view details)

Uploaded Python 3macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for ztensor-1.0.2.tar.gz
Algorithm Hash digest
SHA256 fcd785f4edf24a1675f032377fe120173363fbb88bb6facf137418ae6e76406c
MD5 68cd6b92867af8f407f7281213a0b0e2
BLAKE2b-256 1b3e21a44586ad9aa0df0deebf6f07b98e6b28ff96d20523d2450ab69341bfb7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ztensor-1.0.2-py3-none-win_amd64.whl
  • Upload date:
  • Size: 471.0 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.2-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 956816472c84f8e9cabfde3824c0465c2039026eaf8f4688b400ab36ee0f3f47
MD5 37aabeb9162c0c859e5a0d7214ce2ae8
BLAKE2b-256 cb63c67637ad10be8a1373e3e19da3a5be7aa9153e168355f9f35f9f8fab3773

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ztensor-1.0.2-py3-none-win32.whl
Algorithm Hash digest
SHA256 5f6aac7d5f450eba77f35e2eac12c61d2187b47ecccb36f3b7039a20b2eca39c
MD5 8817d9b55c8213d3122a2d19330016cc
BLAKE2b-256 d07614b63649fb5ff8e699a06c237eabbc2703212bebfd93f1a530ed69a4673a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.0.2-py3-none-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 47403d14d61e1b5ababb08dd0553d78247ae4b40dfc54ab06e54739a6c2428b3
MD5 e8d497c4edcbda6fa8daeee6dec3d525
BLAKE2b-256 493a2b1e79a6b9b60e5b9aea1b5c01aacb4cc13ad036070dff22d728da492abd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.0.2-py3-none-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 c3dc74436e79a1db2db3f426ab98745aa77e950395b46cc055a689a93c6d1b38
MD5 70b1b94d7c8006c4507924778aa9ec81
BLAKE2b-256 cd65ae519f914236789473a23c1e4965494b25a737a55aeadcbe4a48317df6f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.0.2-py3-none-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 4956b40434e947dd4735fac1e30922ad6a8335b3fc32b1df6b1f9dd9169fcb74
MD5 59a0f7ba44f11ac26ff98e2548f52c64
BLAKE2b-256 cec362c4f9d963b269fbebd451d9a345c1219765aaf78ff4370f90a211c6deb0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.0.2-py3-none-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 5748c93af8a833db2384b49345db4cdf32867b68c94774dccb9e5ddde6c1df73
MD5 cd832b0d19e5c72ec9f60d604e8c4768
BLAKE2b-256 311705496f44c1ac06d35d51629d9f007d9e4837c15912fe88f102b9d12e5a7b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.0.2-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a93bf9e80f4bf9861c53c0cb8517278d27812dcbf0c9431a50a850aed7803dd7
MD5 f64763d8a3f46b8500c6c236d87489c0
BLAKE2b-256 81dd14ddd3a8c5068e4869f101b94813107c9fd2fd120d16da1f72273834d4b9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.0.2-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 d9993e016c152feeddfefdea52f5f2e53bf8f8c29a4d5df2a9cc0ec9e18e5b27
MD5 5ca96ea8b280d38797cc043cfb16c33b
BLAKE2b-256 5aea9a55d7d3e08d1621326fbedf7279811bbfa674bf2992cc023591d75b341b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.0.2-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 460ea44877f3a23abccaa4c6a65b19cc4f2dae81f2875bfa36ceae41dbba6b34
MD5 f3324813ffa3b7fffc63a4796ce06d52
BLAKE2b-256 08b0edb2e95b3e6859038faa0413083121ee618522bd2ca79a13acb9555d10d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.0.2-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 032af4f5c5cee3a2efd933ea1359be6614f5cc5d562fd7863ce0200aa77f230d
MD5 c8e9cbf8f4333b27b66b1ccfa7d3ea06
BLAKE2b-256 9fc155d8456b2aa0491ef301595939501f306bc2b6f7901abd7c64dfbb840c9d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.0.2-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 81ba44fd879307aacf3964bb2dc29b96475ae19d7c15e3a1796630e16992d111
MD5 f23b9b65eb34f7d66222161ae6462a42
BLAKE2b-256 5c54e46a36f4843278cc7f4d22cc81b702aeb03d223c4bbe92042fe6aa2e7d20

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.0.2-py3-none-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 254c0944c58eaf1a961df66e4e46943575d21a428139cf41a88915ad49fc9749
MD5 c35275c4fef383e325b721f223043734
BLAKE2b-256 d03452fdc14bfcc7ba649b4e932736b463d5de3aac08fc77d0f5ed5db2513ffe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.0.2-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a220d5d6fad275ffc78c6ef2844148f9c50fff1c409945ca2116db223c607e62
MD5 ccada7c863a4d70c66196c67e3a9de25
BLAKE2b-256 1f6253563e565514488fd62ffe2a0f0809f4dba64031897db1257bcb1c10f687

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