Gradient Accumulation and Global Clipping
Combine microbatch gradients by valid-token count, then clip their global norm. A free ML coding interview task on NudgeCode: write it in Python and check it with tests in your browser. Medium · Optimization, Gradient Accumulation, Clipping.
Implement token-count-weighted accumulation for a list of per-microbatch parameter-gradient lists, then clip the combined gradients by their global L2 norm. Return (clipped_grads, pre_clip_norm) without mutating inputs.