58 / 90

Day 58: Cloud Pretraining Preflight, Cost Ceilings & Offline Drills

Audit GPU environments, establish hard spending caps, verify dependencies, and run zero-cost local dry runs.

Models and Pretraining 480 active minutes (8 hours)
01 120 min

Cloud GPU Environment Auditing and Hardware Verification

Agent-Assisted Engineering Check

Prerequisites

Day 56 Resource Planning calculations and SSH public-key cryptography.

Learning Objective

Verify GPU hardware capabilities, CUDA runtime, and torch.cuda availability on remote hosts without hardcoding credentials in git.

Instructions

1. Understand secure remote connection using SSH keys (ssh -i ~/.ssh/id_rsa user@remote_ip); never embed private keys or tokens in code.
2. Run nvidia-smi --query-gpu=name,memory.total,driver_version --format=csv to verify actual GPU hardware matches rental contracts.
3. Verify PyTorch CUDA integration in a fresh Python virtualenv: torch.cuda.is_available(), torch.cuda.get_device_name(0), and torch.cuda.device_count().
4. Check NCCL communication library and bfloat16 hardware support (torch.cuda.is_bf16_supported()).

Practice & Prediction Log

Write a remote environment validator script verify_env.py that checks Python version >= 3.10, PyTorch CUDA build, GPU device name, VRAM >= 15GB, and BF16 support. Exit with status 0 on pass or 1 on failure.

Independent Mastery Criterion

Produce a validator script that certifies remote environment specs without storing credentials.

Authoritative Source Resources & Provenance

linking MIT

CS336 Assignment 1: Basics (Spring 2025 tagged release)

Author: Stanford University CS336 Staff | Original Source Link ↗

Relevant Section / Range: README setup; tests/adapters.py; tests/test_optimizer.py; tests/test_nn_utils.py; tests/test_serialization.py

Verified Version & Date: spring2025 tag; README, tests and MIT LICENSE checked 2026-11-01

Selection Rationale:

Lead assignment-driven source for pretraining component practice and tests. nanoGPT remains a compact historical code-reading companion, not the current recommended training curriculum.

Source Prerequisites:

Fluent Python, PyTorch, and linear algebra

Safe Fallback (Offline Resilience):

Local self-contained pretraining scripts run on standard local CPUs or budget GPUs without Stanford cluster dependency.

Compared Candidate Sources & Selection Analysis (2)
Karpathy nanoGPT ↗ compared_alternative [MIT]

Simpler code layout, useful complementary reference for minimal PyTorch Transformer.

Hugging Face Transformers Course ↗ compared_alternative [Apache-2.0]

High-level API focus rather than building from raw tensor fundamentals.

linking MIT

nanoGPT: train.py pretraining loop

Author: Andrej Karpathy | Original Source Link ↗

Relevant Section / Range: train.py: system settings (device, dtype bfloat16/float16 selection, compile) and DDP settings

Verified Version & Date: Git master commit 71c3905 (Verified: 2026-10-30)

Selection Rationale:

Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.

Source Prerequisites:

Python, PyTorch, and Day 46-48 Transformer model architecture.

Safe Fallback (Offline Resilience):

Runnable pretraining scripts and step-by-step tensor mechanics are fully self-contained in the curriculum lessons.

Compared Candidate Sources & Selection Analysis (2)
Hugging Face Accelerate / Trainer ↗ compared_alternative [Apache-2.0]

Heavy abstraction layer wrapping distributed training; nanoGPT exposes raw PyTorch loops without framework overhead.

Megatron-LM (NVIDIA) ↗ compared_alternative [BSD-3-Clause]

High-performance enterprise distributed framework; unsuitable for local educational first-principles comprehension.

02 120 min

Strict Spending Ceilings, Automated Cutoffs & Billing Hazards

Independent Mastery Check (no AI assistance).

Prerequisites

Day 58 Activity 1 and Day 56 cloud pricing formulas.

Learning Objective

Set a hard spending ceiling ($10.00 - $20.00 max), implement an idle shutdown watchdog, and eliminate common cloud billing traps (unattached disks, runaway instances).

Instructions

1. Calculate max safe training duration: Duration = Ceiling ($) / Hourly_Rate ($/hr). For a $15 ceiling at $1.50/hr, max runtime is 10 hours.
2. Understand billing traps: stopping an instance does NOT stop disk volume billing; unattached block storage incurs ongoing hourly charges until explicitly destroyed.
3. Implement a local watchdog script or Linux cron job: monitor GPU utilization via nvidia-smi; if GPU is idle (<5% utilization) for more than 15 minutes, execute auto-shutdown.
4. Define explicit stop conditions: stop upon reaching target step (e.g., 5,000 steps), upon validation loss plateau, or when wall-clock ceiling is reached.

Practice & Prediction Log

Write a Python budget guard script budget_watchdog.py that accepts --max-hours and --max-cost, reads elapsed time from the process start, and sends SIGTERM to the training process if runtime exceeds the budget ceiling.

Independent Mastery Criterion

Implement an automated watchdog that reliably kills training upon hitting strict spending ceilings.

Authoritative Source Resources & Provenance

linking CC-BY 4.0

Transformer Math 101

Author: Quentin Anthony, Stella Biderman, Hailey Schoelkopf | Original Source Link ↗

Relevant Section / Range: Engineering Takeaways for Compute Costs

Verified Version & Date: EleutherAI Research Blog (Verified: 2026-10-30)

Selection Rationale:

Widely cited, practical industry guide deriving 6ND compute FLOPs, activation memory, KV cache size, and VRAM estimation.

Source Prerequisites:

Transformer matrix dimensions and matrix multiplication FLOP counting.

Safe Fallback (Offline Resilience):

The 6ND derivation (2ND forward, 4ND backward) and peak VRAM estimation models are fully worked out in the curriculum.

Compared Candidate Sources & Selection Analysis (2)

Rigorous academic formulation; Transformer Math 101 provides accessible step-by-step arithmetic equations.

Kaplan et al. Section 2.1 Compute Budgeting ↗ compared_alternative [Open Access]

Original 6ND approximation; EleutherAI expands on activation memory and KV cache.

linking MIT

CS336 Assignment 1: Basics (Spring 2025 tagged release)

Author: Stanford University CS336 Staff | Original Source Link ↗

Relevant Section / Range: README setup; tests/adapters.py; tests/test_optimizer.py; tests/test_nn_utils.py; tests/test_serialization.py

Verified Version & Date: spring2025 tag; README, tests and MIT LICENSE checked 2026-11-01

Selection Rationale:

Lead assignment-driven source for pretraining component practice and tests. nanoGPT remains a compact historical code-reading companion, not the current recommended training curriculum.

Source Prerequisites:

Fluent Python, PyTorch, and linear algebra

Safe Fallback (Offline Resilience):

Local self-contained pretraining scripts run on standard local CPUs or budget GPUs without Stanford cluster dependency.

Compared Candidate Sources & Selection Analysis (2)
Karpathy nanoGPT ↗ compared_alternative [MIT]

Simpler code layout, useful complementary reference for minimal PyTorch Transformer.

Hugging Face Transformers Course ↗ compared_alternative [Apache-2.0]

High-level API focus rather than building from raw tensor fundamentals.

03 120 min

Recipe Sanity Checking and Single-Batch Offline Dry Runs

Agent-Assisted Engineering Check

Prerequisites

Day 58 Activities 1-2, PyTorch AMP autocast.

Learning Objective

Perform a zero-cost local dry run on a dummy batch (batch_size=1), verifying tensor dimensions, loss calculation, AMP GradScaler stability, and memory buffers before launching cloud runs.

Instructions

1. Construct a synthetic input tensor x of shape (1, 64) and target y of shape (1, 64).
2. Run a single forward pass under torch.cuda.amp.autocast(dtype=torch.bfloat16 if supported else torch.float16).
3. Compute cross-entropy loss and execute scaler.scale(loss).backward().
4. Verify that optimizer.step() updates weights and scaler.update() adjusts scale factor without generating NaNs.

Practice & Prediction Log

Run a 1-step dry run script dry_run.py. Confirm that loss is non-zero, gradients exist on all non-frozen parameters, and peak VRAM allocated is logged.

Independent Mastery Criterion

Verify 100% of pipeline operations in a 1-step dry run with zero NaNs and valid gradient tensors.

Authoritative Source Resources & Provenance

linking MIT

nanoGPT: train.py pretraining loop

Author: Andrej Karpathy | Original Source Link ↗

Relevant Section / Range: train.py: eval_only flag and default config values (batch_size, block_size, max_iters)

Verified Version & Date: Git master commit 71c3905 (Verified: 2026-10-30)

Selection Rationale:

Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.

Source Prerequisites:

Python, PyTorch, and Day 46-48 Transformer model architecture.

Safe Fallback (Offline Resilience):

Runnable pretraining scripts and step-by-step tensor mechanics are fully self-contained in the curriculum lessons.

Compared Candidate Sources & Selection Analysis (2)
Hugging Face Accelerate / Trainer ↗ compared_alternative [Apache-2.0]

Heavy abstraction layer wrapping distributed training; nanoGPT exposes raw PyTorch loops without framework overhead.

Megatron-LM (NVIDIA) ↗ compared_alternative [BSD-3-Clause]

High-performance enterprise distributed framework; unsuitable for local educational first-principles comprehension.

linking BSD-3-Clause

PyTorch Documentation: Automatic Mixed Precision (torch.amp)

Author: PyTorch Team | Original Source Link ↗

Relevant Section / Range: Autocasting

Verified Version & Date: PyTorch 2.x Documentation (Verified: 2026-10-30)

Selection Rationale:

Official reference on autocast and GradScaler for numerical stability in FP16/BF16 training.

Source Prerequisites:

PyTorch tensors, autograd, and GPU compute basics.

Safe Fallback (Offline Resilience):

All scaling and precision casting mechanics are demonstrated in self-contained Python examples.

Compared Candidate Sources & Selection Analysis (2)
NVIDIA Apex Documentation ↗ compared_alternative [BSD-3-Clause]

Legacy external AMP implementation; native torch.cuda.amp is the modern standard.

Framework wrapper around PyTorch AMP; we study the underlying PyTorch primitive.

04 120 min

Assessment: Preflight Verification Suite and Budget Guardrail Enforcement

Independent Mastery Check (no AI assistance).

Prerequisites

Day 58 Activities 1-3.

Learning Objective

Develop an automated, production-grade PreflightValidator that verifies all system checks, asserts spending caps, and outputs a certified GO/NO-GO preflight decision.

Instructions

1. Encapsulate verification in class PreflightValidator(config, budget_ceiling_usd).
2. Implement checks: check_hardware(), check_dependencies(), check_spending_cap(), check_dry_run().
3. Enforce strict failure: if any check fails or if budget_ceiling_usd is not in (0, 20.0], mark status='NO-GO' and refuse to proceed.
4. Output a machine-readable JSON preflight report suitable for automated audit.

Practice & Prediction Log

Run the PreflightValidator on your local test environment. Confirm that it generates a certified preflight report with status='GO' when parameters are within bounds.

Independent Mastery Criterion

Build an automated preflight validator that emits certified GO/NO-GO decisions based on strict budget guardrails.

Authoritative Source Resources & Provenance

linking MIT

CS336 Assignment 1: Basics (Spring 2025 tagged release)

Author: Stanford University CS336 Staff | Original Source Link ↗

Relevant Section / Range: README setup; tests/adapters.py; tests/test_optimizer.py; tests/test_nn_utils.py; tests/test_serialization.py

Verified Version & Date: spring2025 tag; README, tests and MIT LICENSE checked 2026-11-01

Selection Rationale:

Lead assignment-driven source for pretraining component practice and tests. nanoGPT remains a compact historical code-reading companion, not the current recommended training curriculum.

Source Prerequisites:

Fluent Python, PyTorch, and linear algebra

Safe Fallback (Offline Resilience):

Local self-contained pretraining scripts run on standard local CPUs or budget GPUs without Stanford cluster dependency.

Compared Candidate Sources & Selection Analysis (2)
Karpathy nanoGPT ↗ compared_alternative [MIT]

Simpler code layout, useful complementary reference for minimal PyTorch Transformer.

Hugging Face Transformers Course ↗ compared_alternative [Apache-2.0]

High-level API focus rather than building from raw tensor fundamentals.

linking CC-BY 4.0

Transformer Math 101

Author: Quentin Anthony, Stella Biderman, Hailey Schoelkopf | Original Source Link ↗

Relevant Section / Range: Engineering Takeaways for Compute Costs

Verified Version & Date: EleutherAI Research Blog (Verified: 2026-10-30)

Selection Rationale:

Widely cited, practical industry guide deriving 6ND compute FLOPs, activation memory, KV cache size, and VRAM estimation.

Source Prerequisites:

Transformer matrix dimensions and matrix multiplication FLOP counting.

Safe Fallback (Offline Resilience):

The 6ND derivation (2ND forward, 4ND backward) and peak VRAM estimation models are fully worked out in the curriculum.

Compared Candidate Sources & Selection Analysis (2)

Rigorous academic formulation; Transformer Math 101 provides accessible step-by-step arithmetic equations.

Kaplan et al. Section 2.1 Compute Budgeting ↗ compared_alternative [Open Access]

Original 6ND approximation; EleutherAI expands on activation memory and KV cache.