Skip to main content

Delta compression for LLM fine-tunes - lossless or LoRA-equivalent SVD compression

Project description

∴ Sparse

Delta Compression for Fine-tuned Models and Datasets

Compress your 14GB fine-tune to 1.4GB (lossless) or 50MB (LoRA-equivalent). Reconstruct in 4 seconds.

Verified: GPT-2 compression → reconstruction → identical inference output

License: Apache 2.0 Python 3.9+ PyTorch Rust

Quick StartHow It WorksCLIPython API


What Sparse Does

Sparse compresses fine-tuned models and derivative datasets as deltas from their base versions.

📦 Model Delta Compression

Mode Size (7B) Quality Use Case
Lossless ~1.4 GB 100% When quality matters
SVD (LoRA-equiv) ~50 MB ~95-99% When size matters

Reconstruction: 4 seconds • Works on ANY existing fine-tune

Use cases:

  • Compress your existing full fine-tunes (trained without LoRA)
  • Share smaller files with collaborators
  • Save disk space storing multiple fine-tunes
  • Works with ANY training method: full fine-tune, RLHF, merges

⚡ Auto-Caching & Fast Reconstruction

Feature Benefit
Smart caching Reconstruct once, load instantly
Background prefetch Pre-load multiple deltas in parallel
HF Hub integration Uses existing HuggingFace cache
4-second reconstruction Rust-accelerated delta application

📊 Dataset Delta Compression

Metric Value
Savings 60-80% typical
Use case Derivative datasets (translations, versions, augmentations)

Quick Start

pip install sparse-llm

Compress a Fine-tune

# Lossless compression (~1.4GB for 7B model)
sparse compress meta-llama/Llama-2-7b-hf ./my-finetune -o ./my-delta

# OR: SVD compression (~50MB, LoRA-equivalent quality)
sparse svd-compress meta-llama/Llama-2-7b-hf ./my-finetune -o ./my-delta --rank 16

Reconstruct from Delta

# From lossless delta
sparse reconstruct meta-llama/Llama-2-7b-hf ./my-delta -o ./reconstructed-model

# From SVD delta
sparse svd-reconstruct meta-llama/Llama-2-7b-hf ./my-delta -o ./reconstructed-model

Dataset Delta

# Compress derivative dataset
sparse dataset-compress squad squad_v2 -o ./squad_v2_delta

# Reconstruct
sparse dataset-reconstruct ./squad_v2_delta

How It Works

Fine-tuned Model (14GB)  -  Base Model (14GB)  =  Delta
                                    ↓
                    Lossless: 1.4GB  |  SVD: 50MB
                                    ↓
                         Reconstruct: Base + Delta

Two compression modes:

Mode How It Works Size Quality
Lossless Sparse + INT8 encoding ~10% of original 100%
SVD Low-rank approximation (like LoRA) ~0.4% of original ~95-99%

CLI Reference

# Lossless compression (100% quality)
sparse compress <base> <finetune> -o <output>
sparse reconstruct <base> <delta> [-o <output>]

# SVD compression (LoRA-equivalent, ~50MB)
sparse svd-compress <base> <finetune> -o <output> [--rank 16]
sparse svd-reconstruct <base> <delta> [-o <output>]

# Adapter packaging
sparse compress-adapter <base> <adapter> -o <output>

# Dataset commands
sparse dataset-compress <base> <derivative> -o <output>
sparse dataset-reconstruct <delta_dir>
sparse dataset-estimate <base> <derivative>

# Info
sparse info <path>

Python API

from core import compress_delta, reconstruct_from_delta
from core import compress_delta_svd_full, reconstruct_from_svd_delta

# Lossless compression
manifest = compress_delta(
    base_model_id="meta-llama/Llama-2-7b-hf",
    finetune_model_id="./my-finetune",
    output_path="./my-delta"
)
print(f"Compression: {manifest.compression_ratio:.1f}x")  # ~10x

# SVD compression (LoRA-equivalent)
manifest = compress_delta_svd_full(
    base_model_id="meta-llama/Llama-2-7b-hf",
    finetune_model_id="./my-finetune",
    output_path="./my-svd-delta",
    rank=16  # Like LoRA rank
)
print(f"Compression: {manifest.compression_ratio:.1f}x")  # ~280x

# Reconstruct (lossless)
model = reconstruct_from_delta("meta-llama/Llama-2-7b-hf", "./my-delta")

# Reconstruct (SVD)
model = reconstruct_from_svd_delta("meta-llama/Llama-2-7b-hf", "./my-svd-delta")

Dataset API

from core import compress_dataset_delta, reconstruct_from_dataset_delta

# Compress
manifest = compress_dataset_delta("squad", "squad_v2", "./squad_v2_delta")
print(f"Savings: {manifest['size_stats']['savings_pct']:.1f}%")

# Reconstruct
dataset = reconstruct_from_dataset_delta("./squad_v2_delta")

Fast Reconstruction with Auto-Caching

from core.fast_reconstruct import DeltaCache, from_pretrained_with_delta

# Create cache (reconstructed models stored in ~/.cache/sparse)
cache = DeltaCache()

# Reconstruct and cache - only takes time once!
model_path = cache.get_or_reconstruct(
    base_model_id="meta-llama/Llama-2-7b-hf",
    delta_path="./my-delta",
    background=False  # Wait for completion
)

# Load model from cache
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(model_path)

# Or use drop-in replacement for from_pretrained
model = from_pretrained_with_delta(
    "./my-delta",
    base_model_id="meta-llama/Llama-2-7b-hf"
)

# Prefetch multiple deltas in background (10x faster workflow!)
cache.prefetch_deltas(
    base_model_id="meta-llama/Llama-2-7b-hf",
    delta_paths=["./delta1", "./delta2", "./delta3"]
)

Why Sparse?

Post-hoc compression for ANY fine-tune. Unlike LoRA (which requires training differently), Sparse works on models you've already trained.

LoRA/PEFT Sparse Lossless Sparse SVD
When During training After training After training
Size ~50 MB ~1.4 GB ~50 MB
Quality ~95-99% 100% ~95-99%
Works on existing models ❌ No ✅ Yes ✅ Yes

Key insight: Sparse SVD gives you LoRA-sized files from models that weren't trained with LoRA.


Requirements

  • Python 3.9+
  • PyTorch 2.0+
  • transformers
  • Rust (required, included in package)

License

Apache 2.0 - See LICENSE for details.

Free for personal and commercial use.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

sparse_llm-0.0.1-cp39-abi3-win_amd64.whl (329.2 kB view details)

Uploaded CPython 3.9+Windows x86-64

sparse_llm-0.0.1-cp39-abi3-manylinux_2_34_x86_64.whl (401.4 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.34+ x86-64

sparse_llm-0.0.1-cp39-abi3-macosx_11_0_arm64.whl (359.8 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

Details for the file sparse_llm-0.0.1-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: sparse_llm-0.0.1-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 329.2 kB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sparse_llm-0.0.1-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 83961894a1f5640b2fefad0980e77b15b846fd5ab92531b907108389d6935f74
MD5 8d80917695e9688474e5bc2f25dfd1f6
BLAKE2b-256 23aa57a0e88ea9b00fe6c76e704aaff19671dc5841a02f300afcf3d422f4b69c

See more details on using hashes here.

Provenance

The following attestation bundles were made for sparse_llm-0.0.1-cp39-abi3-win_amd64.whl:

Publisher: build-artifacts.yml on gagansuie/sparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sparse_llm-0.0.1-cp39-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for sparse_llm-0.0.1-cp39-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 4fc0cb3fe0a1dc6157beca53c646b4232b09cfc8671af57ff46108b915482342
MD5 5d57799162132e6e01f9b61426e23e19
BLAKE2b-256 4350c17923e7b714ff1ff633ec189245cda5059844e43ddf7e573163bb160d6b

See more details on using hashes here.

Provenance

The following attestation bundles were made for sparse_llm-0.0.1-cp39-abi3-manylinux_2_34_x86_64.whl:

Publisher: build-artifacts.yml on gagansuie/sparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sparse_llm-0.0.1-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sparse_llm-0.0.1-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 10dc84fee19ed63facebeebbbdc48b2a85ddd36537367ddab487ae3a0f580868
MD5 0efcb85231492f8e30183eacec5aa104
BLAKE2b-256 988ef22b0bbfaed4aae58ccb9fc270018f68c866d4be721dc29a05ad2ec3f9ba

See more details on using hashes here.

Provenance

The following attestation bundles were made for sparse_llm-0.0.1-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: build-artifacts.yml on gagansuie/sparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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