Skip to main content

Memory-efficient weight noise injection for LLMs

Project description

Noise Injection

Memory-efficient noise injection for LLM weights. Useful for sandbagging detection, robustness testing, and interpretability research.

Documentation

Installation

pip install noise-injection

# With MLX support (Apple Silicon)
pip install "noise-injection[mlx]"

Quick Start

from noise_injection import inject_noise, with_noise, generators as G, selectors as S

# Context manager (auto-cleanup)
with with_noise(model, G.gaussian(sigma=0.01)):
    accuracy = evaluate(model)
# Noise automatically removed

# Manual control
layer = inject_noise(model, G.gaussian(sigma=0.01))
accuracy = evaluate(model)
layer.remove(model)

# Target specific parameters
with with_noise(model, G.gaussian(sigma=0.01), selector=S.by_name("layers.0.*")):
    ...

API

Noise Injection

# Inject noise and get handle for removal
layer = inject_noise(
    model,
    generator=G.gaussian(sigma=0.01, mu=0.0),
    selector=S.all_params(),      # Which parameters to affect
    seed=42,               # For reproducibility
    store_tensors=False    # False = regenerate from seed (zero memory overhead)
)

# Remove when done
layer.remove(model)

Context Manager

with with_noise(model, G.gaussian(sigma=0.01)) as layer:
    # Model has noise injected
    accuracy = evaluate(model)
# Noise automatically removed

Sigma Sweep

from noise_injection import sweep_sigma

results = sweep_sigma(
    model,
    eval_fn=lambda: evaluate(model),
    sigma_start=0.0,
    sigma_end=0.01,
    sigma_step=0.001,
    mu=0.0,
)
# Returns: [(0.0, 0.65), (0.001, 0.63), ...]

Generators

from noise_injection import generators as G

G.gaussian(sigma=0.01, mu=0.0)           # Gaussian noise
G.uniform(low=-0.01, high=0.01)          # Uniform noise
G.custom(lambda shape, gen: ...)         # Custom generator

Selectors

from noise_injection import selectors as S

S.all_params()                                  # All parameters
S.by_name("layers.0.*")                  # Regex pattern
S.by_name("attn") & S.by_name("weight")  # Composable

Examples

See examples/ for complete usage examples:

# MMLU sweep for sandbagging detection
python examples/sandbagging-detection-mmlu/mmlu_sweep.py --num-samples 100

How It Works

Snapshot-restore (default for sweeps): Clone selected parameters once, restore from the clone between iterations. Bitwise-exact weight restoration regardless of dtype, including bfloat16/float16.

1. Snapshot selected parameters
2. For each sigma:
   a. Restore from snapshot
   b. Inject noise → evaluate
3. Final restore from snapshot

Seeded subtract (for single injection, or memory-constrained sweeps): Uses seeded RNG to regenerate identical noise for add/subtract. O(max single parameter) memory overhead instead of O(total parameters).

1. Generate noise with seeded RNG
2. Add noise in-place → evaluate
3. Regenerate same noise (same seed) → subtract to restore

Module Structure

noise-injection/
├── src/noise_injection/
│   ├── __init__.py         # Main exports
│   ├── api/
│   │   ├── functional.py   # inject_noise, remove_noise
│   │   ├── context.py      # with_noise context manager
│   │   └── sweep.py        # sweep_sigma utility
│   └── core/
│       ├── generators.py   # Noise generators (gaussian, uniform, custom)
│       ├── selectors.py    # Parameter selectors
│       └── noise_layer.py  # NoiseLayer, NoiseStack
├── examples/
│   └── sandbagging-detection-mmlu/  # Replicate arXiv:2412.01784
└── pyproject.toml

Citation

If you use this tool in your research, please cite the original paper on noise injection for sandbagging detection:

@article{tice2024noise,
  title={Noise Injection Reveals Hidden Capabilities of Sandbagging Language Models},
  author={Tice, Cameron and Kreer, Philipp Alexander and others},
  journal={arXiv preprint arXiv:2412.01784},
  year={2024}
}

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

noise_injection-0.3.0.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

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

noise_injection-0.3.0-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: noise_injection-0.3.0.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for noise_injection-0.3.0.tar.gz
Algorithm Hash digest
SHA256 406b0795511cc2051aa4509bf1d1761ae403c10e1053b488d06f6abd1c315775
MD5 b31f62f5685513d1d91c21d9a13fac74
BLAKE2b-256 fc8873afa23a74057e239577268c68fbe238e035fc74935162eb664d5cc67491

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for noise_injection-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 327f916cdd7e35708edddeaffafaa746ecc92faacbdad2be08dd886ea0331479
MD5 13789e4db88377231f348cac386109d6
BLAKE2b-256 bc77f766023b52d688c1911237529b7455e7a090316b5082d134dd0aff165e98

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