Skip to main content

Visualize and remove noise from neural network weights. Shows exactly which weights carry signal vs noise, with interactive visualization.

Project description

weightnoise

Visualize and remove noise from neural network weights.

weightnoise analyzes every weight matrix in a transformer model to determine which weights carry signal and which are noise. Uses the Wanda importance metric (|w| × ∥x∥) from the literature, plus spectral analysis (SVD singular value distribution).

Install

pip install weightnoise

Requires PyTorch 2.0+ and 4GB RAM (for models up to ~1B parameters on CPU).

Usage

# Full noise report
weightnoise inspect distilgpt2

# Compression plan (with hardware target recommendations)
weightnoise plan Qwen/Qwen3.5-0.8B

# Compare two models (detect WIT transfer quality)
weightnoise compare Qwen/Qwen3.6-27B KiriLabs/WIT-CrossSize-Transport

# Remove noise at different levels
weightnoise prune distilgpt2 --keep 0.9 --method wanda
weightnoise prune distilgpt2 --keep 0.5 --method spectral

# Detailed layer view
weightnoise inspect distilgpt2 --layer 0

How Noise Is Measured

For each weight matrix, three independent metrics are computed:

1. Wanda Importance Score (|w| × column_norm)
Per output neuron, each input connection's importance = weight magnitude × input activation norm. Weights with scores below 1% of the row's max are classified as noise-like. This is the standard Wanda metric (Sun et al., 2024), reimplemented from scratch.

2. Spectral Analysis (SVD)
Each weight matrix is decomposed via SVD. Metrics include:

  • Effective rank: the continuous rank (Renyi entropy of singular values)
  • Concentration ratio: % of energy in top 10% of singular values
  • Noise singular values: singular values below 1% of the max
  • Rank retention: rank needed to retain 90%/95%/99% of energy

3. Distribution Analysis

  • Kurtosis: heavy-tailed distributions indicate structured features, Gaussian-like kurtosis (~3) suggests noise
  • KL divergence from Gaussian: how far the weight distribution is from random noise

Validated Results (distilgpt2, 82M params)

Noise Distribution

Layer Matrices Params Kurtosis KL(Gauss) EffRank% Top10% Noise%
0 4 7.08M 42.6 0.18 84% 39% 32%
1 4 7.08M 200.3 0.02 90% 35% 26%
2 4 7.08M 8.0 0.01 90% 35% 15%
3 4 7.08M 2.3 0.01 91% 32% 9%
4 4 7.08M 1.9 0.01 93% 28% 8%
5 4 7.08M 45.5 0.02 92% 34% 18%

Overall: 7.9% of weights are noise-like (6.4M of 81.9M params)

Key Findings

  • Earlier layers have more noise — layers 0-1 show 26-32% noise vs 8-9% in layers 3-4. This aligns with the known result that early transformer layers learn generic syntax (requiring less capacity) while later layers learn task-specific semantics (fully utilizing capacity).

  • Attention output projection (c_proj) is the most noise-like among attention matrices, consistent with the literature showing it's the most compressible via SVD.

  • MLP c_fc (first layer) shows the least noise in several layers, suggesting MLP weights are more fully utilized than attention weights.

Pruning Quality (perplexity validated on Colab CPU)

Wanda pruning sweep on distilgpt2 (82M params, 5-sample evaluation):

Keep Ratio True Sparsity Perplexity vs Baseline
100% 0% 436.3 1.000x
99% 0.9% 436.8 1.001x
95% 5.0% 435.1 0.997x
90% 9.9% 431.3 0.988x
80% 19.9% 459.0 1.052x
70% 29.9% 556.3 1.275x
50% 49.9% 2683.4 6.150x

Pruning method comparison at 50%:

Method Perplexity vs Baseline
Wanda 2683.4 6.150x
Magnitude 1313.7 3.011x
Spectral (SVD) 179,522 411x

Key finding: Wanda pruning at 5-10% sparsity improves perplexity, confirming that the weights identified as "noise" by the tool are genuinely harmful. The 7.9% noise percentage from the inspection correlates directly with the 10% pruning threshold where quality is maintained.

Note: absolute perplexity values are inflated due to distilgpt2's small size on a short eval set. The key metric is the ratio (vs baseline), which is the standard reporting convention in pruning literature.

Architecture Support

Model Family Layer Pattern Supported Tested
GPT-2 / distilgpt2 transformer.h.N
LLaMA / Mistral model.layers.N ✅ (via pattern match)
Qwen / Qwen3.5 model.layers.N Partial
BERT / RoBERTa encoder.layer.N Not yet
T5 / Flan-T5 decoder.layer.N Not yet

The auto-detection of layer naming patterns means most HuggingFace models work out of the box.

How It Compares to Existing Tools

Feature weightnoise SparseGPT Wanda torch-pruning
Weight noise visualization ✅ Per-layer tables
Per-matrix SVD analysis
Wanda importance scoring
SparseGPT Hessian pruning ❌ (planned)
Per-row structured pruning ❌ (global)
CPU-only analysis ❌ (needs GPU) ❌ (needs GPU)
CLI tool

Design Philosophy

  • Don't reinvent the wheel: The pruning core uses established metrics from the literature (Wanda for importance scoring, OBS-style heuristics for weight updates, SVD-based spectral compression). The novelty is in the visualization, the per-matrix noise report, and making everything accessible as a pip-installable CLI.

  • Honest about limitations: Current noise detection uses the Wanda metric (weight × activation norm) which is a first-order approximation. The SparseGPT Hessian-based metric is more accurate but requires GPU. This is noted in the documentation.

  • CPU-first: Analysis runs entirely on CPU (no GPU needed for inspection). Pruning quality evaluation (perplexity) benefits from GPU but the tool doesn't require it.

Roadmap

  • Hessian-based noise detection (incorporate curvature information)
  • Weight update after pruning (OBS-style compensation)
  • N:M structured sparsity patterns (2:4, 4:8)
  • Perplexity benchmark curves on TinyLlama, Qwen3.5, LLaMA
  • Integration with WIT pipeline for compressed model deployment

References

  • Wanda: Sun et al., "A Simple and Effective Pruning Approach for Large Language Models" (2024)
  • SparseGPT: Frantar & Alistarh, "Massive Language Models Can Be Accurately Pruned in One-Shot" (2023)
  • OBS: Hassibi & Stork, "Second Order Derivatives for Network Pruning" (1993)

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

weightnoise-0.2.0.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

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

weightnoise-0.2.0-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file weightnoise-0.2.0.tar.gz.

File metadata

  • Download URL: weightnoise-0.2.0.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for weightnoise-0.2.0.tar.gz
Algorithm Hash digest
SHA256 055c45b875abee9b992d203a5bf8389dca8fefb1d5c954cf98b6ac8bf7b0e1e6
MD5 05c8ba0e9bec868937cea05190a181b5
BLAKE2b-256 df7638ab9151a65bb09c3daad2e85dfb7503e2cc49ce0bbdd38fe24963c53407

See more details on using hashes here.

File details

Details for the file weightnoise-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: weightnoise-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 19.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for weightnoise-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0bf1e8c7cc0ef6056e2cb67a4802c691b4ea8f7f96ec53d62b904acde658a5bc
MD5 d095533d92e9c45c309c6d6786e8535d
BLAKE2b-256 ec675f0fdb50c4fec3b7aa07d23f500f6ed450a7016a1c96b40ef39400a23a06

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