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. All thresholds are adaptive — computed from the data distribution, not hardcoded.

1. Adaptive Wanda Importance Score (|w| × column_norm)
Per output neuron, each input connection's importance = weight magnitude × input activation norm. The noise floor is computed as the 5th percentile of the per-row relative score distribution (configurable via --threshold).

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
  • 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
  • 2xMAD thresholding: magnitude noise detection uses Median Absolute Deviation (robust to outliers)

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

All thresholds in weightnoise are data-adaptive, computed from the model's own weight distribution. No hardcoded values, no magical constants.

Feature weightnoise SparseGPT Wanda torch-pruning
Weight noise visualization ✅ Per-layer tables
Per-matrix SVD analysis
Adaptive thresholds ✅ (5th percentile) ❌ (fixed) ❌ (fixed) ❌ (fixed)
Dynamic hardware targets ✅ (auto-detect GPU/RAM)
Wanda importance scoring
WIT transfer diagnostics ✅ (adaptive MAD)
Per-row structured pruning ❌ (global)
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.3.0.tar.gz (22.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.3.0-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: weightnoise-0.3.0.tar.gz
  • Upload date:
  • Size: 22.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.3.0.tar.gz
Algorithm Hash digest
SHA256 a771df418f209f8c99ab449c636a70e7984e8356ebc4cdcb2612bc8fac8d98fd
MD5 a45169933c3a7dbf40e65e0421759b3c
BLAKE2b-256 a4b39971e698f1f29cdb55506c60b9cf5321a3396a09a5dd37347cd594cd1739

See more details on using hashes here.

File details

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

File metadata

  • Download URL: weightnoise-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 21.8 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 394acd7d6a61ac19c52aa8b1cb1a2f2a72fbc0f0fdbe73c2c8a42724c2c70f1b
MD5 d73489fcdc244ed152892a707a92b8e3
BLAKE2b-256 897a79a8a3a69d79def9115d466859e89239f5c171372999fa23b13e2f1c109c

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