Skip to main content

Per-weight precision + CUDA kernels for PyTorch

Project description

HyperTensor

Per-weight mixed-precision for PyTorch. Faster inference, lower VRAM, same accuracy.

HyperTensor is a precision-adaptive quantization framework that assigns the lowest safe numerical precision to each individual weight in a neural network.


Why HyperTensor?

Modern neural networks waste compute.

Most models run entirely in FP32 (or globally in FP16/BF16), even though:

  • many weights work fine in INT8 or INT4
  • some benefit from FP16/BF16
  • only a small fraction truly require FP32

HyperTensor solves this by measuring error per weight and selecting the minimum precision that preserves stability.


What it does

HyperTensor:

  • analyzes each weight independently
  • assigns the lowest safe precision
  • builds mixed-precision execution buffers
  • executes using INT4 / INT8 / FP16 / BF16 / FP32 — within the same model

Result:

  • faster inference
  • significantly lower VRAM usage
  • predictable, stable accuracy

Key Features

  • Per-weight and per-block precision selection
  • INT4 / INT8 / FP16 / BF16 / FP32 execution
  • Flexible precision planner: LOWEST, HIGHEST, ALLOW, CLOSEST, AVERAGE, BEST, PER_WEIGHT, PER_BLOCK
  • Drop-in HyperLinear layer
  • HyperAttention module for LLaMA-style architectures
  • Training + inference support
  • Inference-only mode (removes FP32 master weights)
  • Custom CUDA kernels:
    • INT4 / INT8 GEMM
    • FP16 / BF16 backward
  • Serialization of quantized models and precision plans

Design Philosophy

HyperTensor is built to be:

  • Accuracy-first — every decision is based on measured error
  • Granular — operates at the weight level, not just per tensor
  • Composable — use the planner, modules, or full stack independently
  • Training-aware — STE, backward kernels, and caching are first-class
  • Deployment-focused — inference-only mode minimizes VRAM
  • Format-minimal — INT4, INT8, FP16, BF16, FP32 only

The system is designed to be extended with new planner modes, kernels, and modules.


Benchmarks

All benchmarks were run on an NVIDIA RTX 4080 Laptop GPU (12GB VRAM), reflecting realistic consumer hardware performance.

The main benchmark suite (benchmark.py) evaluates training and inference on a 4096-dim MLP (batch size 16).


Training Benchmarks

Training Speed

Training speed remains close to FP32 (~17–18 ms/step). Most planner modes introduce minimal overhead.

Training Memory Usage

Memory usage increases due to quantization buffers and error tracking. PER_WEIGHT and PER_BLOCK are the most expensive; FP32 remains lowest.

Training Drift vs FP32

All modes maintain stable accuracy (~0.31–0.33 MAE), including aggressive configurations.


Inference Benchmarks

Inference Speed

Inference improves across most modes. AVERAGE is the fastest, but all benefit from reduced precision.

Inference Memory Usage

Memory drops significantly:

  • FP32: >500 MB
  • HyperTensor: ~210 MB typical
  • PER_BLOCK: ~147 MB (lowest)

Inference Drift vs FP32

Accuracy remains stable (~0.32 MAE) even under aggressive quantization.


TinyLLaMA Benchmark

An end-to-end LLM benchmark is included in TinyLLaMA_benchmark.py.

Results:

=== HyperTensor LLM Test ===

[1] Loading FP32 model... Running FP32 benchmark... FP32 inference: 108.732 ms

[2] Unloading FP32 model... [3] Converting to HyperTensor... Scanning weights (CPU)... Applying mixed precision (mode = best)... Moving to GPU... Running HyperTensor benchmark... HyperTensor inference: 49.795 ms

=== Summary === FP32: 108.732 ms HyperTensor: 49.795 ms Speedup: 2.18x

HyperTensor reduces TinyLLaMA inference latency by over 2× while maintaining stable accuracy.


Combined Results

Summary:

  • Training speed ≈ FP32
  • Training memory increases (expected)
  • Inference speed improves
  • Inference memory drops significantly
  • Accuracy remains stable across all modes

HyperTensor delivers consistent accuracy, strong inference performance, and substantial VRAM savings on a 12GB GPU.


Documentation

See DOCUMENTATION.md for:

  • usage examples
  • architecture details
  • advanced configuration

Installation

git clone https://github.com/CoolPuzzler/HyperTensor.git HyperTensor
cd HyperTensor
pip install -e .

Requirements

  • Python ≥ 3.12
  • PyTorch with CUDA
  • NVIDIA GPU (sm_89 target by default)
  • Optional: ninja for faster builds

CUDA extensions are compiled on first import via torch.utils.cpp_extension.load.


License

This project is licensed under the MIT License. See the LICENSE file for full details.

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

hypertensor-1.0.0.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

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

hypertensor-1.0.0-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hypertensor-1.0.0.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for hypertensor-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8ae2ef1d305b4753b3e38dee0cabfd11b88ee9add24f6d4e6436d77fb6e0d8f3
MD5 09bb1547eb3440240691d181e14bd895
BLAKE2b-256 1efbcabf150531817aa22a1b3092a9bf81db187db5cc9faa196a08d1367f361b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hypertensor-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for hypertensor-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f984037fd2c096a417c9bfba38d95eb12f90c0f9bad64adcb186c22ba180a3c8
MD5 40d6b7f2c927c33909123e63027e096a
BLAKE2b-256 d3859ba8d742392498033a1a9f3575aa76b5a55e82e368ca4a6b77e395853208

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