888. How many tokens can my budget train?
Why it matters Founders and research leads ask this before every training run: what can this budget buy? The 6ND rule is the standard back-of-envelope answer.
Training a dense transformer costs about 6 * N * D FLOPs, where N is the parameter count and D the number of training tokens (forward plus backward pass).
Write training_budget(params, budget_usd, gpu_hourly_usd, peak_tflops, mfu, n_gpus).
- GPU hours you can buy: budget_usd / gpu_hourly_usd
- FLOPs delivered: gpu_hours * 3600 * peak_tflops * 1e12 * mfu (mfu is model FLOPs utilisation, a fraction in (0, 1])
- tokens: floor(FLOPs / (6 * params))
- wall-clock days on n_gpus GPUs: gpu_hours / n_gpus / 24
Return a dict {"gpu_hours": rounded to 1 decimal, "tokens": int, "tokens_per_param": tokens / params rounded to 1 decimal, "wall_days": rounded to 2 decimals}.
def training_budget(params, budget_usd, gpu_hourly_usd, peak_tflops, mfu, n_gpus)Example 1
Input: training_budget(1e9, 10000, 2.5, 989, 0.4, 8)
Output: {'gpu_hours': 4000.0, 'tokens': 949440000000, 'tokens_per_param': 949.4, 'wall_days': 20.83}
Example 2
Input: params = 125000000, budget_usd = 500, gpu_hourly_usd = 1.0, peak_tflops = 312, mfu = 0.5, n_gpus = 1
Output: {'gpu_hours': 500.0, 'tokens': 374400000000, 'tokens_per_param': 2995.2, 'wall_days': 20.83}
Example 3
Input: params = 7e9, budget_usd = 200000, gpu_hourly_usd = 2.0, peak_tflops = 989, mfu = 0.35, n_gpus = 64
Output: {'gpu_hours': 100000.0, 'tokens': 2967000000000, 'tokens_per_param': 423.9, 'wall_days': 65.1}
Constraints
- Python 3 standard library. Code runs in your browser.
- Your code must pass all 4 tests to be accepted.
Hint 1
GPU hours first, then FLOPs, then tokens.
Hint 2
Use math.floor for tokens.
Hint 3
Wall days divide by the number of GPUs and by 24.
Common mistakes
- Using 2ND (forward only).
- Forgetting the 1e12 for TFLOPS.
- Multiplying by
n_gpustwice.
Understand the idea
Each token costs about 2N FLOPs forward and 4N backward. Real GPUs reach only part of their peak, measured as MFU; 35 to 50 percent is typical for well-tuned dense training. With $10,000 at $2.50 an hour you get 4000 GPU hours; at 989 TFLOPS and 40 percent MFU that is about 5.7e21 FLOPs, enough for about 949 billion tokens on a 1B model.
Shapes and data flow
Scalars in, dict out.