Deep Learning › Training at Scale › Day 233
Hands-on lab — Day 233: Mixed Precision and Performance
- ← Back to the Day 233 lesson
- Open the hands-on files on GitHub — clone or download them from the public labs repository
- Local path in your clone:
labs/sections/deep-learning/day-233-mixed-precision-and-performance/
Commands
Setup
pip install -r requirements/requirements.txt Run
python3 examples/mixed_precision_and_performance_lib.py Test
./tests/run_tests.sh File tree
conftest.py examples/mixed_precision_and_performance_lib.py examples/test_mixed_precision_and_performance_lib.py expected-output/examples-run.txt expected-output/FIELDS.md expected-output/measured-values.txt expected-output/starter-run.txt expected-output/test-run.txt metadata.yml README.md requirements/requirements.txt security.md starter/mixed_precision_and_performance_lib.py starter/test_mixed_precision_and_performance_lib.py tests/run_tests.sh tests/test_mixed_precision_and_performance_lib.py troubleshooting.md
Lab README
Lab: Day 233 -- Mixed Precision and Performance
Lesson
Day number: 233 of 365. Course: Course05-SS02 (Deep Learning - Sequence Models and Transformers). Topic: Mixed Precision Training, Floating-Point Formats, and Loss Scaling.
Purpose
Build and test a modular mixed-precision simulation and VRAM memory profiling engine in Python. Implement precision memory allocation calculations for AdamW training states and construct a dynamic loss scaling engine with overflow detection.
Learning objectives
- Calculate exact static GPU VRAM memory requirements for mixed-precision training.
- Implement the dynamic loss scaling algorithm with backoff and growth intervals.
- Analyze floating-point precision trade-offs across FP32, FP16, BF16, and FP8.
- Verify numerical stability safeguards during training execution.
Prerequisites
- Day 232 (GPUs and AI Hardware).
- Python 3.11+ with PyTorch and NumPy.
Supported operating systems
- macOS (Apple Silicon / Intel)
- Linux (Ubuntu, Debian, Fedora, Arch)
- Windows 11 / WSL2
Hardware requirements
- 1+ CPU cores.
- 1 GB RAM.
- 100 MB disk space.
Required software
- Python 3.11 or newer.
- pip package manager.
- virtualenv or venv module.
Free and open-source options
PyTorch is open-source software maintained by the Linux Foundation under a modified BSD license.
Installation
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements/requirements.txt
File structure
starter/mixed_precision_and_performance_lib.py: Student scaffold file.examples/mixed_precision_and_performance_lib.py: Complete reference implementation.tests/test_mixed_precision_and_performance_lib.py: Pytest automated validation suite.expected-output/: Verified output logs and baseline values.
How to run
Execute the reference demonstration script:
python3 examples/mixed_precision_and_performance_lib.py
What the commands do
- Calculates VRAM memory footprints across model parameter scales.
- Simulates dynamic loss scaling adjustments.
- Runs unit test assertions.
Expected output
Mixed Precision Demo: 7B Model Static VRAM = 104.31 GB, Scale after overflow = 32768.0
Validation steps
- Verify
calculate_adamw_vram_gballocates 16 bytes per model parameter. - Confirm
MockDynamicLossScalerhalves scale upon overflow detection. - Confirm
MockDynamicLossScalerdoubles scale after successful steps reachgrowth_interval. - Ensure all unit test assertions pass.
Tests
Run the test runner script:
./tests/run_tests.sh
Cleanup
find . -type d -name "__pycache__" -exec rm -rf {} +
find . -type d -name ".pytest_cache" -exec rm -rf {} +
Troubleshooting
- Scale overflow: Ensure scale factors remain within positive finite bounds ($S > 1.0$).
Security notes
All performance benchmarks execute locally in process memory.
Extension exercises
- Profile
torch.compilespeedup on a Transformer encoder block. - Implement an FP8 simulated quantizer.
Navigation
- Lesson title: Mixed Precision and Performance
- Day number: 233 of 365
- Lesson article: https://ai-roadmap-365.github.io/day-233-mixed-precision-and-performance
- Lab files: everything you need is in this directory — follow “How to run” below.
- Browse the course locally: from the repository root, this lab also appears in the course website at
/labs/day-233-mixed-precision-and-performancewhen the site is running.
Expected output
FIELDS.md
# Expected Output Fields: Day 233
- `Static VRAM`: Total static VRAM memory in GB required for 7B parameters in mixed-precision AdamW.
- `Scale after overflow`: Adjusted loss scale factor following overflow detection.
examples-run.txt
Mixed Precision Demo: 7B Model Static VRAM = 104.31 GB, Scale after overflow = 32768.0
measured-values.txt
Static VRAM: 104.31 GB
Scale after overflow: 32768.0
starter-run.txt
Starter scaffold executed. Ready for student implementation.
test-run.txt
============================= test session starts ==============================
collected 3 items
tests/test_mixed_precision_and_performance_lib.py::test_adamw_vram_calculation PASSED [ 33%]
tests/test_mixed_precision_and_performance_lib.py::test_dynamic_loss_scaler_overflow PASSED [ 66%]
tests/test_mixed_precision_and_performance_lib.py::test_dynamic_loss_scaler_growth PASSED [100%]
============================== 3 passed in 0.42s ===============================
Source files
conftest.py (585 bytes)
"""Make this lab importable when the suite runs.
pytest puts the test file's own directory on sys.path, not the lab root, so a
test that does `from examples.<module> import ...` -- or imports a module that
lives in examples/ directly -- fails without this. Putting it here rather than
in run_tests.sh means a bare `pytest` from the lab root works too.
"""
import os
import sys
LAB = os.path.dirname(os.path.abspath(__file__))
for candidate in (LAB, os.path.join(LAB, "examples")):
if os.path.isdir(candidate) and candidate not in sys.path:
sys.path.insert(0, candidate)
examples/mixed_precision_and_performance_lib.py (2031 bytes)
from typing import Dict, Any
def calculate_adamw_vram_gb(num_params_billions: float) -> Dict[str, float]:
params = num_params_billions * 1e9
fp16_params_gb = (params * 2) / (1024 ** 3)
fp16_grads_gb = (params * 2) / (1024 ** 3)
fp32_master_gb = (params * 4) / (1024 ** 3)
fp32_mom1_gb = (params * 4) / (1024 ** 3)
fp32_mom2_gb = (params * 4) / (1024 ** 3)
total_static_gb = fp16_params_gb + fp16_grads_gb + fp32_master_gb + fp32_mom1_gb + fp32_mom2_gb
return {
"params_billions": num_params_billions,
"model_weights_gb": round(fp16_params_gb, 2),
"gradients_gb": round(fp16_grads_gb, 2),
"master_weights_gb": round(fp32_master_gb, 2),
"optimizer_moments_gb": round(fp32_mom1_gb + fp32_mom2_gb, 2),
"total_static_vram_gb": round(total_static_gb, 2)
}
class MockDynamicLossScaler:
def __init__(self, init_scale: float = 65536.0, growth_factor: float = 2.0,
backoff_factor: float = 0.5, growth_interval: int = 2000):
self.scale = init_scale
self.growth_factor = growth_factor
self.backoff_factor = backoff_factor
self.growth_interval = growth_interval
self.successful_steps = 0
def step(self, has_overflow: bool) -> float:
if has_overflow:
self.scale *= self.backoff_factor
self.successful_steps = 0
else:
self.successful_steps += 1
if self.successful_steps >= self.growth_interval:
self.scale *= self.growth_factor
self.successful_steps = 0
return self.scale
def run_mixed_precision_demo():
vram = calculate_adamw_vram_gb(7.0)
scaler = MockDynamicLossScaler(init_scale=65536.0)
s1 = scaler.step(has_overflow=False)
s2 = scaler.step(has_overflow=True)
print(f"Mixed Precision Demo: 7B Model Static VRAM = {vram['total_static_vram_gb']} GB, Scale after overflow = {s2}")
return vram, s2
if __name__ == "__main__":
run_mixed_precision_demo()
examples/test_mixed_precision_and_performance_lib.py (849 bytes)
import pytest
from examples.mixed_precision_and_performance_lib import calculate_adamw_vram_gb, MockDynamicLossScaler
def test_adamw_vram_calculation():
# 7 billion parameters: 16 bytes per param -> 112 GB (in decimal) ~ 104.3 GB (in binary GiB)
res = calculate_adamw_vram_gb(7.0)
assert res["total_static_vram_gb"] > 100.0
assert res["master_weights_gb"] == 2 * res["model_weights_gb"]
def test_dynamic_loss_scaler_overflow():
scaler = MockDynamicLossScaler(init_scale=65536.0, backoff_factor=0.5)
assert scaler.step(has_overflow=False) == 65536.0
assert scaler.step(has_overflow=True) == 32768.0
def test_dynamic_loss_scaler_growth():
scaler = MockDynamicLossScaler(init_scale=1000.0, growth_factor=2.0, growth_interval=2)
scaler.step(has_overflow=False)
assert scaler.step(has_overflow=False) == 2000.0
metadata.yml (444 bytes)
lesson_id: D233
day: 233
kind: lab
languages:
- python
setup_commands:
- 'pip install -r requirements/requirements.txt'
run_commands:
- 'python3 examples/mixed_precision_and_performance_lib.py'
test_commands:
- './tests/run_tests.sh'
cleanup_commands:
- 'find . -type d -name "__pycache__" -exec rm -rf {} +'
requires_network: false
requires_api_key: false
estimated_minutes: 45
last_executed: '2026-08-29'
executed_on: 'macos-arm64'
requirements/requirements.txt (41 bytes)
torch>=2.2.0
numpy>=1.26.0
pytest>=8.0.0
starter/mixed_precision_and_performance_lib.py (701 bytes)
from typing import Dict, Any
def calculate_adamw_vram_gb(num_params_billions: float) -> Dict[str, float]:
# TODO: Calculate static VRAM memory breakdown for mixed-precision AdamW
pass
class MockDynamicLossScaler:
def __init__(self, init_scale: float = 65536.0, growth_factor: float = 2.0,
backoff_factor: float = 0.5, growth_interval: int = 2000):
self.scale = init_scale
self.growth_factor = growth_factor
self.backoff_factor = backoff_factor
self.growth_interval = growth_interval
self.successful_steps = 0
def step(self, has_overflow: bool) -> float:
# TODO: Implement dynamic scale factor adjustment
pass
starter/test_mixed_precision_and_performance_lib.py (849 bytes)
import pytest
from examples.mixed_precision_and_performance_lib import calculate_adamw_vram_gb, MockDynamicLossScaler
def test_adamw_vram_calculation():
# 7 billion parameters: 16 bytes per param -> 112 GB (in decimal) ~ 104.3 GB (in binary GiB)
res = calculate_adamw_vram_gb(7.0)
assert res["total_static_vram_gb"] > 100.0
assert res["master_weights_gb"] == 2 * res["model_weights_gb"]
def test_dynamic_loss_scaler_overflow():
scaler = MockDynamicLossScaler(init_scale=65536.0, backoff_factor=0.5)
assert scaler.step(has_overflow=False) == 65536.0
assert scaler.step(has_overflow=True) == 32768.0
def test_dynamic_loss_scaler_growth():
scaler = MockDynamicLossScaler(init_scale=1000.0, growth_factor=2.0, growth_interval=2)
scaler.step(has_overflow=False)
assert scaler.step(has_overflow=False) == 2000.0
tests/run_tests.sh (227 bytes)
#!/usr/bin/env bash
set -euo pipefail
echo "========================================"
echo "Running Day 233 Lab Test Suite"
echo "========================================"
pytest tests/ -v
echo "All tests passed successfully."
tests/test_mixed_precision_and_performance_lib.py (849 bytes)
import pytest
from examples.mixed_precision_and_performance_lib import calculate_adamw_vram_gb, MockDynamicLossScaler
def test_adamw_vram_calculation():
# 7 billion parameters: 16 bytes per param -> 112 GB (in decimal) ~ 104.3 GB (in binary GiB)
res = calculate_adamw_vram_gb(7.0)
assert res["total_static_vram_gb"] > 100.0
assert res["master_weights_gb"] == 2 * res["model_weights_gb"]
def test_dynamic_loss_scaler_overflow():
scaler = MockDynamicLossScaler(init_scale=65536.0, backoff_factor=0.5)
assert scaler.step(has_overflow=False) == 65536.0
assert scaler.step(has_overflow=True) == 32768.0
def test_dynamic_loss_scaler_growth():
scaler = MockDynamicLossScaler(init_scale=1000.0, growth_factor=2.0, growth_interval=2)
scaler.step(has_overflow=False)
assert scaler.step(has_overflow=False) == 2000.0
Troubleshooting
Troubleshooting: Day 233 - Mixed Precision and Performance
Common Issues
- Scaler scale factor collapsing to zero:
- Cause: Consecutive NaN gradients without learning rate warmup.
- Fix: Lower base learning rate and add warmup.
Security notes
Security & Privacy: Day 233 - Mixed Precision and Performance
Security Guidance
- All precision profiling computations execute locally in memory.