Gradient Accumulation: Valid-token Weighting
LLM Training & Generation · 训练与生成入门 · · Easy · 15 minutes
Weight microbatch gradients by valid token counts to match one combined batch.
Implement one core function; independent tests and deterministic synthetic experiments are provided.
Functions to implement
accumulate_gradients(gradients: list[Tensor], token_counts: list[int]) -> Tensor
Gradient Accumulation: Valid-token Weighting
A standalone LLM exercise. Supplied code and synthetic experiments do not require any previously solved exercise.
Task and conventions
Nonempty equal-length lists. Each gradient is a finite float32/64 CPU tensor for the same parameter, with identical shape/dtype/device, possibly scalar or empty. Each token_count is a nonnegative integer and their total is >0. Input gradient i already represents the mean over its valid tokens. Return sum(count_i * gradient_i)/sum(count_i), ignoring zero-count batches, same shape/dtype/device. Do not average microbatches equally, divide by the number of accumulation steps, perform optimizer updates, mutate inputs or retain state. Higher-order gradients are not required. Gradients from different parameters are not being merged.
accumulate_gradients(gradients: list[Tensor], token_counts: list[int]) -> Tensor
$$ g=\frac{\sum_i n_i g_i}{\sum_i n_i} $$
Example
accumulate_gradients([torch.tensor([1., 3.]), torch.tensor([5., 7.])], [1, 3])
# tensor([4., 6.])
Before implementing
Each input is already a local token mean: weight it by its token count and divide once by the global count. Empty batches do not dilute the denominator. The experiment compares one update from unequal microbatches with a full-batch update.
Tests and experiment
T0 checks the interface; T1 compares an independent oracle; T2 checks boundaries, invariants, applicable gradients and input preservation; T3 runs your function in the supplied experiment. Charts show measured outputs, not full-model training or speed claims.
References
Related exercises
- Next-token Loss: Shifted Causal Targets
- SFT Loss Mask: Train on Response Tokens
- Multi-turn SFT: Assistant Targets and Conversation Boundaries
- Top-k: Temperature and Candidate Probabilities
- Top-p: Cumulative Probability and Nucleus Candidates
- Global Gradient Clipping: One Norm for All Parameters
- All LLM Training & Generation · 训练与生成入门 exercises