TensorCache ⚡

Ultra-fast, high-fidelity block-wise INT8 feature & pixel cache engine for PyTorch.
pip install tcache → import tensorcache or import tcache — 0.3.0 on PyPI.
tensorcache eliminates two bottlenecks:
- Feature Cache Bloat:
AMO-BQasymmetric MSE-optimalG321.09B1.83xvs BF160.47%rel RMSE(sym 1.06B 0.54%) — nearG16floor0.39%. - JPEG/PNG CPU Decode: Zero-copy
mmap+GPUstream prefetch>2,000 MB/s, ring-buffer6.8MBVRAMbatch8 128x768. - Training Throughput (v0.2.2):
iter_batches27k samp/s17.7GB/sB128 446x768~8.3xvsv0.2.03.1k,Streamer21k5.1msdouble-bufferedpinned + async H2D(low_vram128MBB128),sharded 8xforH100 DDP. - Pixel Cache Tunable JPEG-XS (v0.3.0):
CDF 5/34-lvlRCTbatched [3,H,W]GPU Triton1.65ms195MB/svs 6.57ms CPUout_buffer,adaptive RDO 4bG32tunable43dB 2.85x<->29dB 5.6xpresets ultra/high/balanced/compress/ultra_compq_scale 1-8 lamb 1-50on-the-fly3.5msGPU5ms CPU.
🚀 Key Features
- AMO-BQ (Asymmetric MSE-Optimal, G32):
min-max + zp uint8 + 48×clipping search[0.95,1.10]—0.47%hetero 0.54%vssym 0.74%(-26%),per-token 1.73%(cache\dtype_comparison.json). Presetsfast/balanced/accurate/max. - Microsecond Dequant (v0.2.2 opt):
Tritonshift >>5vsdiv,autotune BLOCK 512/1024/2048stages 2/3163 GB/s5.4M(codec.py:220.19ms10M26xfor1024),FusedDequantLinear0intermediatefused_ops.py:119.NaN/Infisolated0poison1e-8scale. - Training-Optimized Dataloader:
iter_batches27k samp/s17.7GB/sB1284.5ms(8.3xvs3.1k),Streamer21.6k5.1msdouble-bufferedpinnedasync H2Doverlapdequant.low_vram=True128MBB128(53 MBB32) vs256MB. - Big Data Sharded:
Writer(num_shards=8)->feat_shard{i}_*.bin+feat_shards.json,Dataset/Streamer(rank, world_size)DDPH100 8x~5.8krandom27kcontiguous. Single shardnum_shards=1unchanged100%compat. - Minimal VRAM:
q 5.22MB + scales 0.32MB + zp 0.16MB + out 10.45MB5.4M;G64halvesscales/zp. - Cross-Platform:
CUDA/ROCmTritonelsePyTorchfallback,Windowsmmapsafeclose(). - Tunable JPEG-XS Pixel:
quantize_pixel_wavelet_adaptive(mode="balanced")lambD+lamb*RG32 4bcodebook 8CPU 5msGPU Triton 3.5ms43dB 2.85x<->29dB 5.6xvs static 5.67x 32dB. - CLI + Python one-liners:
tc.compress/tc.benchmark_tensor/tensorcache benchmark.
📦 Installation
pip install tcache # PyPI (import tensorcache or tcache)
pip install -e . # dev
pip install -e ".[fast]" # triton+zstd+blosc2 (Linux)
⚡ Quick Start
1. In-Memory (one-liners)
import torch, tensorcache as tc
x = torch.randn(16,446,768, dtype=torch.bfloat16, device="cuda")
# AMO-BQ presets: fast (16,0.95-1.05) 6.9ms 0.49%, balanced (32,0.95-1.05) 13ms 0.478% (default), accurate (48,0.95-1.10) 49ms 0.473%
q,s,zp,shape = tc.compress(x, mode="balanced") # or "fast"/"accurate"/"max"/"sym"/"adaptive"
rec = tc.decompress(q,s,shape,zp) # <0.1ms BF16
tc.benchmark_tensor(x) # rich table
tc.estimate_compression(x.shape, group_size=32) # 1.09375 B 1.83x
tc.auto_select_mode(x, target_rmse=0.5) # -> "balanced"
tc.help() # python help
# Codec object
codec = tc.BlockwiseInt8Codec(group_size=32, amo_bq=True, amo_mode="balanced")
print(codec) # G=32, amo_bq=balanced 1.0938B 1:1.83x
2. Feature Cache to Disk (mmap) - Training Optimized
import tensorcache as tc
from torch.utils.data import DataLoader
# Write (amo_bq, G32 default balanced, G64 for minimal VRAM 1.046B 0.55%)
# Single shard (default, unchanged)
writer = tc.FeatureCacheWriter("./cache/dinov3", 10000,446,768, group_size=32, amo_bq=True, amo_mode="balanced")
# Or sharded for H100 / >1M samples (8 shards ~1.25k each)
writer = tc.FeatureCacheWriter("./cache/dinov3", 10000,446,768, group_size=32, amo_bq=True, amo_mode="balanced", num_shards=8)
writer.append(x) # [seq,dim] or [B,seq,dim] (batched GPU 64/chunk, CPU per-sample)
writer.close() # single: _int8.bin ... | sharded: _shard0_int8.bin ... + _shards.json
# Load - auto-detects sharded
ds = tc.FeatureCacheDataset("./cache/dinov3") # -> (q uint8, s BF16, zp uint8) all shards
# DDP per-rank (H100 8x)
ds = tc.FeatureCacheDataset("./cache/dinov3", rank=rank, world_size=8) # only shard rank
# or explicit
ds = tc.FeatureCacheDataset("./cache/dinov3", shard_idx=0)
ds = tc.FeatureCacheDataset("./cache/dinov3", auto_dequant_device="cuda") # -> BF16 directly
# Old path still works but slower (~5k samp/s)
for q,s,zp in tc.AsyncGPUPrefetcher(DataLoader(ds,batch_size=256,pin_memory=True), device="cuda"):
batch = tc.dequantize_int8_amo_bq(q,s,zp, shape, group_size=32)
# Training fastest (27k samp/s 17.7GB/s B128, 8.3x vs v0.2.0)
for batch in ds.iter_batches(batch_size=128, shuffle=False, device="cuda"): # contiguous 27k, shuffled ~5.8k
train(batch)
# Minimal VRAM streamer (double-buffered 64MB B32 / 256MB B128, low_vram 32MB/128MB)
streamer = tc.ZeroCopyTensorStreamer("./cache/dinov3", batch_size=128, device="cuda", shuffle=True)
streamer = tc.ZeroCopyTensorStreamer("./cache/dinov3", batch_size=128, device="cuda", low_vram=True) # 128MB B128
streamer = tc.ZeroCopyTensorStreamer("./cache/dinov3", batch_size=128, device="cuda", shard_idx=0) # per-rank
for batch in streamer: # BF16 [B,seq,dim] from ring buffer out_bf16_0/1
train(batch)
streamer.close()
# Fused head (no BF16 intermediate)
from tensorcache import FusedDequantLinear
head = FusedDequantLinear(768, num_classes, group_size=32).cuda()
logits = head(q, s) # dequant+GEMM in regs
3. CLI
python -m tensorcache info
python -m tensorcache benchmark --shape 16,446,768 --device cuda
python -m tensorcache cache-info --prefix ./cache/dinov3
python -m tensorcache compress-demo --shape 4,197,768 --mode balanced
tensorcache --help
📊 Benchmark (DINOv3 ViT-Base, 5.4M randn + RSNA hetero)
| Format | B/elem | vs BF16 | rel RMSE | Outlier 0.1% | Dequant |
|---|---|---|---|---|---|
| Raw BF16 | 2.00 | 1.00x | 0.167% | 0.119% | — |
| Naive FP8 E5M2 | 1.00 | 2.00x | 5.24% | 6.81% | — |
| Naive FP8 E4M3 | 1.00 | 2.00x | 2.63% | 3.19% | — |
| MXFP8 G32 | 1.09 | 1.83x | 2.38% | — | — |
| Sym G32 | 1.06 | 1.88x | 0.540% | 0.11% | 0.036ms 435GB/s |
| AMO-BQ fast G32 | 1.093 | 1.83x | 0.490% | 0.15% | 0.06ms 337GB/s |
| AMO-BQ balanced G32 | 1.093 | 1.83x | 0.478% | 0.15% | 13ms quant |
| AMO-BQ accurate G32 | 1.093 | 1.83x | 0.473% | — | 49ms quant |
| AMO-BQ G16 | 1.187 | 1.68x | 0.395% | — | 13ms |
| Sym G64 | 1.046 | 1.91x | 0.55% | — | — |
📜 License
Apache 2.0 — see LICENSE.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
tcache-0.3.0.tar.gz
(109.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
tcache-0.3.0-py3-none-any.whl
(58.9 kB
view details)
File details
Details for the file tcache-0.3.0.tar.gz.
File metadata
- Download URL: tcache-0.3.0.tar.gz
- Upload date:
- Size: 109.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb6d0e6c0644717b1f428c55d760974cf96012cfbd09cb256c4888bd949c8a09
|
|
| MD5 |
fce636e2b8a7fbd322c897eb9cb1feb3
|
|
| BLAKE2b-256 |
1d55e620c7b5ebbfa4d21d4d2d76c2e6f7beb249023b598b03bc450991be2bc6
|
File details
Details for the file tcache-0.3.0-py3-none-any.whl.
File metadata
- Download URL: tcache-0.3.0-py3-none-any.whl
- Upload date:
- Size: 58.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a871ac80929eb036e1ce7a5f653bd2745a87923e205bce6e3b7c72d955b69b4
|
|
| MD5 |
299810e90ae28f64a7006b53657cf8e4
|
|
| BLAKE2b-256 |
69ba59b8f65aa34ac77902e476d32f19298006185b6925d417685eabf7655122
|