🎲 LAB-04 Playground: Inference Math, Sampling & Quantization¶
Estimated time to read: 4 minutes
Welcome to the LAB-04 Interactive Playground! In this notebook, you will interactively experiment with:
- Logit Transformations & Token Sampling: Temperature Scaling, Top-K, Top-P (Nucleus), and Min-P filtering.
- Post-Training Quantization (FP32 -> INT8 / INT4): Symmetric and Asymmetric linear quantization, scale/zero-point derivation, and SQNR signal preservation metrics.
- Inference Economics Calculator: Dynamic KV Cache memory allocation, parameter VRAM overhead, and FLOP throughput bounds.
In [1]:
Copied!
import os
import sys
import sys
from pathlib import Path
try:
import google.colab
IN_COLAB = True
except ImportError:
IN_COLAB = False
if IN_COLAB:
print("🤖 Running in Google Colab. Setting up environment...")
!pip install -q numpy scikit-learn matplotlib loguru
# Clone repository to access custom local source modules
if not os.path.exists("production-ai-systems"):
!git clone https://github.com/RogerioLS/production-ai-systems.git
sys.path.append("production-ai-systems")
print("🎉 Colab environment successfully initialized!")
# Ensure project root is in the path when running locally
if not IN_COLAB:
# Localiza dinamicamente a raiz do repositório procurando por 'pyproject.toml'
current_dir = Path.cwd()
root_dir = current_dir
for parent in [current_dir] + list(current_dir.parents):
if (parent / "pyproject.toml").exists() or (parent / "projects").exists():
root_dir = parent
break
if str(root_dir) not in sys.path:
sys.path.insert(0, str(root_dir))
print(f"📁 Project root added to sys.path: {root_dir}")
import math
import torch
import torch.nn.functional as F
from projects.a_llm_basics.src.lab_04_inference import InferenceCostCalculator, SamplingEngine, UniformQuantizer, compute_cross_entropy_loss, compute_perplexity, compute_quantization_error, compute_shannon_entropy
print("✅ LAB-04 Modules imported successfully!")
import os
import sys
import sys
from pathlib import Path
try:
import google.colab
IN_COLAB = True
except ImportError:
IN_COLAB = False
if IN_COLAB:
print("🤖 Running in Google Colab. Setting up environment...")
!pip install -q numpy scikit-learn matplotlib loguru
# Clone repository to access custom local source modules
if not os.path.exists("production-ai-systems"):
!git clone https://github.com/RogerioLS/production-ai-systems.git
sys.path.append("production-ai-systems")
print("🎉 Colab environment successfully initialized!")
# Ensure project root is in the path when running locally
if not IN_COLAB:
# Localiza dinamicamente a raiz do repositório procurando por 'pyproject.toml'
current_dir = Path.cwd()
root_dir = current_dir
for parent in [current_dir] + list(current_dir.parents):
if (parent / "pyproject.toml").exists() or (parent / "projects").exists():
root_dir = parent
break
if str(root_dir) not in sys.path:
sys.path.insert(0, str(root_dir))
print(f"📁 Project root added to sys.path: {root_dir}")
import math
import torch
import torch.nn.functional as F
from projects.a_llm_basics.src.lab_04_inference import InferenceCostCalculator, SamplingEngine, UniformQuantizer, compute_cross_entropy_loss, compute_perplexity, compute_quantization_error, compute_shannon_entropy
print("✅ LAB-04 Modules imported successfully!")
📁 Project root added to sys.path: /home/runner/work/production-ai-systems/production-ai-systems
✅ LAB-04 Modules imported successfully!
1. 🌡️ Sampling Strategies: Temperature, Top-K & Top-P¶
In [2]:
Copied!
# Simulated unnormalized logits vector for a 5-token vocabulary
logits = torch.tensor([[12.0, 8.0, 4.0, 1.0, 0.2]])
vocab = ["llama", "transformer", "attention", "token", "cat"]
print("Raw Logits:", logits.numpy()[0])
# Experiment 1: Temperature Scaling
for temp in [0.1, 0.7, 1.0, 2.0]:
scaled = logits / temp
probs = F.softmax(scaled, dim=-1)[0]
entropy = compute_shannon_entropy(probs).item()
print(f"T={temp:<4.1f} | Probs: {probs.numpy().round(3)} | Entropy: {entropy:.2f} bits")
# Simulated unnormalized logits vector for a 5-token vocabulary
logits = torch.tensor([[12.0, 8.0, 4.0, 1.0, 0.2]])
vocab = ["llama", "transformer", "attention", "token", "cat"]
print("Raw Logits:", logits.numpy()[0])
# Experiment 1: Temperature Scaling
for temp in [0.1, 0.7, 1.0, 2.0]:
scaled = logits / temp
probs = F.softmax(scaled, dim=-1)[0]
entropy = compute_shannon_entropy(probs).item()
print(f"T={temp:<4.1f} | Probs: {probs.numpy().round(3)} | Entropy: {entropy:.2f} bits")
Raw Logits: [12. 8. 4. 1. 0.2] T=0.1 | Probs: [1. 0. 0. 0. 0.] | Entropy: 0.00 bits T=0.7 | Probs: [0.997 0.003 0. 0. 0. ] | Entropy: 0.03 bits T=1.0 | Probs: [0.982 0.018 0. 0. 0. ] | Entropy: 0.13 bits T=2.0 | Probs: [0.862 0.117 0.016 0.004 0.002] | Entropy: 0.69 bits
2. ⚡ Quantization & Precision Loss Metrics (FP32 -> INT8)¶
In [3]:
Copied!
torch.manual_seed(42)
weights_fp32 = torch.randn(1000)
# Symmetric INT8 Quantization
quantizer_sym = UniformQuantizer(num_bits=8, symmetric=True)
q_sym, scale_sym, zero_sym = quantizer_sym.quantize(weights_fp32)
deq_sym = quantizer_sym.dequantize(q_sym, scale_sym, zero_sym)
metrics_sym = compute_quantization_error(weights_fp32, deq_sym)
print("=== Symmetric INT8 Quantization Metrics ===")
print(f"Scale Factor (S): {scale_sym:.6f}")
print(f"MSE: {metrics_sym['mse']:.6f}")
print(f"Cosine Similarity: {metrics_sym['cosine_similarity']:.6f}")
print(f"SQNR: {metrics_sym['sqnr_db']:.2f} dB")
torch.manual_seed(42)
weights_fp32 = torch.randn(1000)
# Symmetric INT8 Quantization
quantizer_sym = UniformQuantizer(num_bits=8, symmetric=True)
q_sym, scale_sym, zero_sym = quantizer_sym.quantize(weights_fp32)
deq_sym = quantizer_sym.dequantize(q_sym, scale_sym, zero_sym)
metrics_sym = compute_quantization_error(weights_fp32, deq_sym)
print("=== Symmetric INT8 Quantization Metrics ===")
print(f"Scale Factor (S): {scale_sym:.6f}")
print(f"MSE: {metrics_sym['mse']:.6f}")
print(f"Cosine Similarity: {metrics_sym['cosine_similarity']:.6f}")
print(f"SQNR: {metrics_sym['sqnr_db']:.2f} dB")
2026-09-30 21:12:08.575 | DEBUG | projects.a_llm_basics.src.lab_04_inference.quantization:quantize_symmetric:46 - Symmetric quantization (bits=8): scale=0.024422093639223593
=== Symmetric INT8 Quantization Metrics === Scale Factor (S): 0.024422 MSE: 0.000050 Cosine Similarity: 0.999975 SQNR: 43.07 dB
3. 🖥️ Inference Economics: VRAM & KV Cache Calculator¶
In [4]:
Copied!
# Llama-3-8B architecture specifications
calc = InferenceCostCalculator(
num_layers=32,
num_heads=32,
head_dim=128,
hidden_size=4096,
num_kv_heads=8 # Grouped-Query Attention (GQA 4:1 ratio)
)
num_params = 8_000_000_000
weight_vram_gb = calc.compute_model_param_bytes(num_params, precision_bytes=2.0) / (1024**3)
kv_vram_mb = calc.compute_kv_cache_bytes(batch_size=1, seq_len=8192, precision_bytes=2.0) / (1024**2)
max_batch = calc.compute_max_batch_size(vram_capacity_bytes=24 * (1024**3), num_params=num_params, seq_len=8192)
print("=== Llama-3-8B Inference Profile (FP16) ===")
print(f"Weights VRAM: {weight_vram_gb:.2f} GB")
print(f"KV Cache (B=1, S=8192): {kv_vram_mb:.2f} MB")
print(f"Max Batch Size on RTX 4090 (24GB VRAM): {max_batch} sequences")
# Llama-3-8B architecture specifications
calc = InferenceCostCalculator(
num_layers=32,
num_heads=32,
head_dim=128,
hidden_size=4096,
num_kv_heads=8 # Grouped-Query Attention (GQA 4:1 ratio)
)
num_params = 8_000_000_000
weight_vram_gb = calc.compute_model_param_bytes(num_params, precision_bytes=2.0) / (1024**3)
kv_vram_mb = calc.compute_kv_cache_bytes(batch_size=1, seq_len=8192, precision_bytes=2.0) / (1024**2)
max_batch = calc.compute_max_batch_size(vram_capacity_bytes=24 * (1024**3), num_params=num_params, seq_len=8192)
print("=== Llama-3-8B Inference Profile (FP16) ===")
print(f"Weights VRAM: {weight_vram_gb:.2f} GB")
print(f"KV Cache (B=1, S=8192): {kv_vram_mb:.2f} MB")
print(f"Max Batch Size on RTX 4090 (24GB VRAM): {max_batch} sequences")
2026-09-30 21:12:08.582 | INFO | projects.a_llm_basics.src.lab_04_inference.loss_metrics:__init__:138 - Initialized InferenceCostCalculator (L=32, H=32, H_kv=8, D=128, Hidden=4096)
=== Llama-3-8B Inference Profile (FP16) === Weights VRAM: 14.90 GB KV Cache (B=1, S=8192): 1024.00 MB Max Batch Size on RTX 4090 (24GB VRAM): 8 sequences