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.1.1.tar.gz (209.5 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.1.1-py3-none-win_amd64.whl (492.0 kB view details)

Uploaded Python 3Windows x86-64

ztensor-1.1.1-py3-none-win32.whl (445.7 kB view details)

Uploaded Python 3Windows x86

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

Uploaded Python 3musllinux: musl 1.2+ x86-64

ztensor-1.1.1-py3-none-musllinux_1_2_i686.whl (1.1 MB view details)

Uploaded Python 3musllinux: musl 1.2+ i686

ztensor-1.1.1-py3-none-musllinux_1_2_armv7l.whl (1.0 MB view details)

Uploaded Python 3musllinux: musl 1.2+ ARMv7l

ztensor-1.1.1-py3-none-musllinux_1_2_aarch64.whl (975.5 kB view details)

Uploaded Python 3musllinux: musl 1.2+ ARM64

ztensor-1.1.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (819.2 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

ztensor-1.1.1-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl (804.8 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ s390x

ztensor-1.1.1-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (890.1 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ ppc64le

ztensor-1.1.1-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (747.2 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARMv7l

ztensor-1.1.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (753.5 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64

ztensor-1.1.1-py3-none-manylinux_2_5_i686.manylinux1_i686.whl (841.3 kB view details)

Uploaded Python 3manylinux: glibc 2.5+ i686

ztensor-1.1.1-py3-none-macosx_11_0_arm64.whl (619.6 kB view details)

Uploaded Python 3macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for ztensor-1.1.1.tar.gz
Algorithm Hash digest
SHA256 58f9c8876d9a2330e7b5847f23a41443e46e001b55ca2e33bc1572b809a99327
MD5 1e3b8fd29dda3b1c1b11bfd01cb1694b
BLAKE2b-256 b842f67443050e3b74cc32ad1574d506bae97f165ddbf3553525642dd25de76d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ztensor-1.1.1-py3-none-win_amd64.whl
  • Upload date:
  • Size: 492.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.1.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 ef28adf860a014b1da7aab2f94a7e99f6488a37db637c89405354ed2ff6568a7
MD5 e0dd6c1aac33782a397662be6630b1a1
BLAKE2b-256 16c49ea6f4b23bac316cb43f1921c67d309c79caadc1249c55b55e6f26711a91

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ztensor-1.1.1-py3-none-win32.whl
Algorithm Hash digest
SHA256 0be21cb15a679e3ed31fbf569f6228220758eb39bf7ee35aa66abfd43e2fd11e
MD5 ece9e3ea78ff9d91f2f9433b8d1bd620
BLAKE2b-256 3ef812429cc2354394ab6621225b4be5fb531c7e8efe93506395e25fed88162c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.1.1-py3-none-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 85b07d9bc0279b905e0a93030b9e88409adedf0a2a25b459557ccdc25f2e5237
MD5 a28e4d989260fc6d09b64e4caf5921c4
BLAKE2b-256 000317443c714fb66022e9306f34937349a89ab245c363af2e7a5f37b95557ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.1.1-py3-none-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 9d02c2d70b101f1624f8e00a5242a83b9d57d8f0b42a1de0390abe580219e10e
MD5 8e21a623b76599e6f7bfb20633e25669
BLAKE2b-256 bf73dae892f293f81afa99775ce081622ae0e00639bcacf542ee1feee87b5a90

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.1.1-py3-none-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 6ed3cb966650002ab4ff684823b07b9c2e1df950a134e9258a667afc177eb91e
MD5 a3a37b9515d8a4c60749b54d0505ffa4
BLAKE2b-256 6d790fb8ba3e3e4544f9090068bee71074ccb877b78281dcd26db7710d3add4c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.1.1-py3-none-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 ca64eb08eecaaa63122be170083c4b0bed8784f63b6ab9f088596fc49918d803
MD5 c2bf32f37a37850a33580181407a2084
BLAKE2b-256 9fefa9d458ca3c11db74203e5409bba7edcbed2cda0863960a53d4d99b3985ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.1.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c886132a2476245dd4d4eeae5353c16e535f7597aafa8463a275e8bc8d79b3d3
MD5 ef1952c682083c59368a4d756b000507
BLAKE2b-256 758d3f966343d0df231ae37aa952e819d66a5e5918e87b9294fb6434ff2dbec1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.1.1-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 9243490c11226b06afe3eea5e6079af634a301cb6d964c72b35da3b59bd5abf3
MD5 a1b9c4fb1070f20abbf937da5d7384a1
BLAKE2b-256 915a94ac54ef11ad0e229f8a24929d0f7174e055af1ded3c52c1fb38f2cbf15e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.1.1-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 58d2987b2c74ed64feb2376976643a63a07bd233221f6051a7ccb2d46dcbf0fe
MD5 10560219fbfbade6298e3daa548abcae
BLAKE2b-256 b7e3c3e94065c1b98feeeafe39871bb09a3d414c1ff5f7f761ccfa01640e122a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.1.1-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 90e758d03ee6fc16241a0947ca4584843356be02e92b668040b27a1f39381f4c
MD5 eb2c9f81074fa5582ee27b6e7446d202
BLAKE2b-256 5c056b983bd7e59da1e746bd3347e8ee7647d297f881595ea64fdac29e388f43

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.1.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 43bc97492e6227b59a03368853336a9d24c86ff5d1a0c08a754cdbc924a5aa0d
MD5 f597d800d7019eb65758c3f6d4038dd1
BLAKE2b-256 39f47227c23135aaf139c088d3659610d4249d69c3e158c1735d7b36e20433c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.1.1-py3-none-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 fc8b403a5b80e70c4ad1e201cb2100c3bda180d92cd68149bc93979e15fd54c1
MD5 81d410ed324881085706595c92f42ce0
BLAKE2b-256 59ec72915bc64a42f39a01366dc6527bc625a9921fe1c3228bdbd75f3145bf94

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ztensor-1.1.1-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d1e62bdc5b46d7bae31e5245d9f1600807fe5ec7947240bbaedcdb1bdb279e58
MD5 306e26ac842186777428e73000d55494
BLAKE2b-256 b587c59adb731e6cfa72b092615793ec2fb9161f155850a2eedde0abe8b092ff

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