Skip to content

Fix CUDA resume and optimize training throughput - #1

Merged
ferdinandobons merged 3 commits into
mainfrom
agent/fix-cuda-rng-resume
Jul 21, 2026
Merged

Fix CUDA resume and optimize training throughput#1
ferdinandobons merged 3 commits into
mainfrom
agent/fix-cuda-rng-resume

Conversation

@ferdinandobons

@ferdinandobons ferdinandobons commented Jul 21, 2026

Copy link
Copy Markdown
Owner

What changed

  • Fix CUDA checkpoint resume by moving mapped RNG states back to CPU ByteTensors before restoration.
  • Add an opt-in fused multi-head attention path with one combined QKV projection and one batched SDPA call per Transformer block.
  • Vectorize memmapped batch extraction and use pinned, non-blocking CUDA transfers.
  • Add lightweight --log-interval progress output independent of validation and checkpoint cadence.
  • Preserve legacy checkpoint behavior by defaulting missing fused-attention configuration to the original separate-head implementation.
  • Keep the Lesson 42 snapshot synchronized with the final project.
  • Add CUDA_TRAINING_OPTIMIZATIONS.md with concepts, measurements, tradeoffs, and production commands.

Why

CUDA resume failed because torch.load(..., map_location="cuda") moved RNG-state tensors to CUDA while torch.cuda.set_rng_state_all requires CPU ByteTensors.

The educational attention implementation also launched Q, K, V, and SDPA separately for every head. On the measured 12-block, 12-head model this caused 432 QKV projections and 144 attention calls per micro-batch. Fusing heads preserves the model computation while substantially reducing Python and CUDA launch overhead.

Separating logging from evaluation gives frequent progress visibility without running validation, context diagnostics, and multi-gigabyte checkpoint writes every 100 steps.

Impact

  • Existing checkpoints continue to load with the original attention layout.
  • New runs can opt into --fused-attention.
  • CUDA resume preserves model, optimizer, GradScaler, and RNG state.
  • On the local RTX 4060 benchmark, fused batch 8 reached 20,312 tokens/s versus 3,740 tokens/s for the best separate-head batch 8 run.
  • The effective batch remains 8,192 token positions per optimizer step and model parameter count is unchanged.

Validation

  • python -B tools/validate_learngpt.py
  • python -B -m unittest discover -s tests -v
  • 37 tests passed.
  • Fused and separate attention output parity verified after weight mapping.
  • Equal parameter counts verified.
  • Fresh CUDA and resume smoke runs completed with zero AMP overflows.
  • Batch 8 and batch 16 were benchmarked; batch 8 was faster and retained safer VRAM headroom.

@ferdinandobons
ferdinandobons force-pushed the agent/fix-cuda-rng-resume branch from 678075b to f1c876e Compare July 21, 2026 19:06
@ferdinandobons ferdinandobons changed the title Fix CUDA RNG checkpoint resume Fix CUDA resume and optimize training throughput Jul 21, 2026
@ferdinandobons
ferdinandobons marked this pull request as ready for review July 21, 2026 19:30
@ferdinandobons
ferdinandobons merged commit d58065c into main Jul 21, 2026
2 checks passed
@ferdinandobons
ferdinandobons deleted the agent/fix-cuda-rng-resume branch July 21, 2026 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant