Simulating Analog Hardware Thermal Drift Effects on Large Language Models
This project demonstrates how analog hardware noise (thermal drift, quantization errors, manufacturing variations) affects Large Language Model performance. By injecting controlled noise into model weights, we can observe the relationship between:
- Noise Level (Ο) β The magnitude of weight perturbation
- Perplexity β Model's "accuracy" (lower is better)
- Entropy β Model's "creativity/uncertainty" (measured in bits)
There exists a critical noise threshold where LLM output transitions from:
- Coherent text β Baseline performance
- Creative jitter β Slightly degraded but interesting variations
- Repetitive patterns β Model stuck in loops
- Complete gibberish β Total breakdown
# Clone the repository
git clone https://github.com/pranshug2704/NoisyNeuralNetwork.git
cd NoisyNeuralNetwork
# Create virtual environment (optional but recommended)
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt# Basic run with default settings (distilgpt2, Gaussian noise)
python thermal_noise_simulation.py
# Custom configuration
python thermal_noise_simulation.py \
--model gpt2 \
--noise_type gaussian \
--max_noise 0.1 \
--n_levels 10 \
--prompt "The future of artificial intelligence is"| Noise (Ο) | Perplexity | Entropy (bits) | Quality Assessment |
|---|---|---|---|
| 0.000 | 60.96 | 3.48 | β Coherent |
| 0.0125 | 164.14 | 4.63 | β‘ Creative Jitter |
| 0.025 | 5,074 | 1.24 | |
| 0.0375 | 45.5M | 0.53 | β Gibberish |
| 0.050 | 166.8M | 0.54 | β Gibberish |
Ο = 0.0 (Baseline - Coherent)
"The future of artificial intelligence is much clearer, as they are already discovering and applying new technologies and technologies. The development of these technologies has been driven by advances..."
Ο = 0.0125 (Creative Jitter)
"The future of artificial intelligence is bleak. But what drives the industry into a dangerous position is a sense of the sense that the world around us is shifting from a safe haven..."
Ο = 0.025 (Degraded - Repetitive)
"The future of artificial intelligence is coming and coming and coming and coming and coming and coming and coming..."
Ο = 0.05 (Gibberish)
"The future of artificial intelligence is optimtrehensive optimtrehensive optimtrehensive optimtrehensive..."
NoisyNeuralNetwork/
βββ thermal_noise_simulation.py # Main CLI script
βββ noise_distributions.py # Modular noise injection system
βββ evaluation.py # Perplexity & entropy utilities
βββ layer_sensitivity.py # Experiment A: Attention vs MLP
βββ optimal_jitter.py # Experiment B: Goldilocks zone
βββ noise_aware_training.py # Experiment C: QAT-style training
βββ generate_graphs.py # Comparison graph generation
βββ requirements.txt # Dependencies
βββ images/ # Generated visualizations
βββ README.md
Question: Are Attention layers or MLP layers more tolerant to noise?
python layer_sensitivity.py --max_noise 0.04 --n_levels 8Key Finding: MLP layers are slightly MORE noise-tolerant than Attention layers:
- Attention crosses PPL=100 at Ο β 0.034
- MLP crosses PPL=100 at Ο β 0.040
Implication: For hybrid analog/digital chips, consider Digital Attention + Analog MLP.
Question: Is there a noise level where the model becomes MORE creative without breaking?
python optimal_jitter.py --search_range 0.001 0.025 --n_points 12Key Finding: Goldilocks zone exists at Ο = 0.001:
- Diversity (Distinct-2): +7.8% improvement
- Perplexity: only +2.8% degradation
Question: Can we train the model to be robust to noise (like Quantization-Aware Training)?
python noise_aware_training.py --noise_level 0.01 --epochs 1 --max_samples 500This module injects noise during the forward pass, teaching the model to store information redundantly.
Question: Does the model degrade gracefully (like a tired human) or suddenly snap?
python temporal_drift.py --total_tokens 100 --end_sigma 0.02Key Finding: GRADUAL degradation - the model behaves like a tired human, not a sudden cliff!
Supports multiple drift schedules: linear, exponential, step, sine (thermal cycling).
Question: Are larger/modern models more or less noise-tolerant?
# GPT-2 comparison only (no GPU required)
python llama_scaling.py --n_levels 8 --max_noise 0.03
# Include LLaMA-3/Mistral (requires CUDA GPU)
python llama_scaling.py --include_modernKey Finding: Smaller models are MORE noise-tolerant!
- GPT-2 (82M): 2x PPL at Ο = 0.0129
- GPT-2 Medium (355M): 2x PPL at Ο = 0.0086
Implements a Strategy Pattern for modular noise injection:
from noise_distributions import inject_thermal_noise, GaussianNoise
# Inject 5% Gaussian noise into model weights
inject_thermal_noise(model, noise_level=0.05, noise_type="gaussian")Available Noise Types:
| Type | Formula | Use Case |
|---|---|---|
gaussian |
W + N(0, ΟΒ²) | Thermal noise in analog circuits |
uniform |
W + U(-a, a) | Quantization noise in DACs |
cauchy |
W + Cauchy(0, Ξ³) | Heavy-tailed outlier events |
Provides metrics for model assessment:
from evaluation import compute_perplexity, generate_text_samples, compute_output_entropy
# Compute perplexity on validation text
ppl = compute_perplexity(model, tokenizer, validation_text, device)
# Generate text samples
samples = generate_text_samples(model, tokenizer, prompt, device, n_samples=3)
# Measure output entropy
entropy = compute_output_entropy(model, tokenizer, prompt, device)- Create a new class inheriting from
NoiseDistribution:
from noise_distributions import NoiseDistribution, NOISE_REGISTRY
class LaplacianNoise(NoiseDistribution):
"""Laplacian noise for modeling double-exponential errors."""
def sample(self, shape, scale, device):
# Laplacian via inverse CDF
u = torch.rand(shape, device=device) - 0.5
return scale * torch.sign(u) * torch.log(1 - 2 * torch.abs(u))
@property
def name(self):
return "Laplacian"
# Register the new distribution
NOISE_REGISTRY["laplacian"] = LaplacianNoise- Use it in the simulation:
python thermal_noise_simulation.py --noise_type laplacian| Argument | Default | Description |
|---|---|---|
--model |
distilgpt2 |
HuggingFace model name |
--noise_type |
gaussian |
Noise distribution type |
--max_noise |
0.1 |
Maximum noise level (Ο) |
--n_levels |
10 |
Number of noise levels to test |
--prompt |
"The future of artificial intelligence is" | Generation prompt |
--device |
auto |
Compute device (cuda/mps/cpu) |
--output |
noise_vs_perplexity.png |
Output plot path |
For each Linear layer weight matrix W:
Where Ο represents the "Thermal Temperature" parameter. Higher Ο β more analog hardware degradation.
When Ο approaches the model's weight standard deviation (~0.14 for GPT-2):
- Weight perturbations exceed original weight magnitudes
- Attention patterns become corrupted
- Softmax distributions collapse to single tokens (repetition)
- Eventually outputs become random token sequences
Interestingly, entropy decreases at high noise levels because the model becomes extremely confident in wrong predictions (collapsed attention), rather than uncertain across many tokens.
- Analog AI and the Future of Computing
- GPT-2: Language Models are Unsupervised Multitask Learners
- Perplexity as a Language Model Metric
MIT License - feel free to use this for research or educational purposes!
Contributions welcome! Ideas for extensions:
- Add more noise distributions (Poisson, Student-t, Laplacian)
- Support for larger models (LLaMA, Mistral)
-
Layer-specific noise injection(Implemented:layer_sensitivity.py) -
Noise-aware fine-tuning experiments(Implemented:noise_aware_training.py) - Web UI for interactive exploration
- Per-layer noise scheduling (noise annealing)





