Skip to content

About

LLM pruning and sparsification techniques: magnitude pruning, 2:4 semi-structured, iterative, WANDA

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LLM Sparsity

Implementations of model pruning and sparsification techniques for LLMs.

Techniques Implemented

1. Magnitude Pruning (Unstructured)

Removes weights with smallest absolute values. Simple but effective baseline.

2. Structured Pruning

Removes entire neurons/output channels. Results in actual parameter reduction without sparse tensor support.

3. Semi-Structured 2:4 Sparsity

Keeps 2 values per 4-element group. Hardware-accelerated on NVIDIA Ampere+ GPUs via Sparse Tensor Cores.

4. Iterative/Gradual Pruning

Gradually increases sparsity over multiple steps with cubic schedule. Better accuracy retention than one-shot pruning.

5. WANDA (Pruning by Weights and Activations)

Considers activation magnitudes when pruning. Weights connected to high-activation inputs are preserved.

Installation

pip install -e .

Usage

Unstructured Magnitude Pruning

from llm_sparsity import magnitude_prune_unstructured

model = AutoModelForCausalLM.from_pretrained("gpt2")
sparse_model = magnitude_prune_unstructured(model, sparsity=0.5)

2:4 Semi-Structured Sparsity

from llm_sparsity import semi_structured_prune_model

sparse_model = semi_structured_prune_model(model)
# Achieves 50% sparsity with hardware acceleration

Iterative Pruning

from llm_sparsity import iterative_magnitude_prune

sparse_model = iterative_magnitude_prune(
    model,
    target_sparsity=0.9,
    num_steps=10,
)

Sparse Fine-Tuning

from llm_sparsity import SparseFineTuner

trainer = SparseFineTuner(
    model=sparse_model,
    optimizer=optimizer,
    mask_mode="fixed",  # Maintain sparsity during training
)

for batch in dataloader:
    loss = compute_loss(model, batch)
    trainer.step(loss)  # Applies masks after each step

Benchmark Results

Method Sparsity Size Reduction Output MSE
Magnitude (50%) 50% 2x 0.001
Magnitude (90%) 90% 10x 0.15
2:4 Structured 50% 2x 0.0008
Iterative (90%) 90% 10x 0.08

Run benchmarks:

cd benchmarks
python benchmark_pruning.py

Tests

pytest tests/ -v

References

About

LLM pruning and sparsification techniques: magnitude pruning, 2:4 semi-structured, iterative, WANDA

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages