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

# Detailed layer view
weightnoise inspect distilgpt2 --layer 0

# Remove noise (prune 50% of low-importance weights)
weightnoise prune distilgpt2 --keep 0.5 --method wanda

# Spectral pruning (SVD truncation)
weightnoise prune distilgpt2 --keep 0.5 --method spectral

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.1.0.tar.gz (15.7 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.1.0-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for weightnoise-0.1.0.tar.gz
Algorithm Hash digest
SHA256 36f488ad0be7b26b00a11839064e8b3f02d4af55bfb4aae4ecdbf9268290a580
MD5 bf09654758d2ce71fbd3308431a0458d
BLAKE2b-256 1c1ea7d774d0995982198d35fbf4ae1c71df8ceede04e3e875c7ffb14f9522ac

See more details on using hashes here.

File details

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

File metadata

  • Download URL: weightnoise-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.6 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 53a62f67b2928e870848f0d70559b8a5fda54059adb6d0400fa59e5827e08f44
MD5 76b730a2bfa894b17a4ef8a1db728fdb
BLAKE2b-256 be5d09e30954bfe07a16d2a4878d6ee10a0042ef5498aa66aa72ea5c66f1dfe4

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