Gradient utilities

Two primitives govern how gradients move through the training loop: maybe_no_sync controls when they get reduced, clip_grad_norm_ controls their magnitude.

maybe_no_sync

from kempnerforge.training.grad import maybe_no_sync

for micro_step in range(grad_accum_steps):
    with maybe_no_sync(model, micro_step, grad_accum_steps):
        logits = model(input_ids, doc_ids=doc_ids)
        loss = loss_fn(logits, labels)
        (loss / grad_accum_steps).backward()

Context manager from kempnerforge/training/grad.py. On all microbatches except the last, it turns gradient sync off for every FSDP2 unit of the model, and back on when the block exits:

units = [m for m in model.modules() if isinstance(m, FSDPModule)]
skipping = [u for u in units if not reduces_every_micro_step(u)]
if hasattr(model, "set_requires_gradient_sync") and skipping == units:
    switch = model.set_requires_gradient_sync
else:

    def switch(flag):
        for unit in skipping:
            unit.set_requires_gradient_sync(flag, recurse=False)

switch(False)
try: yield
finally: switch(True)

Why:

  • Each FSDP2 unit fires a reduce-scatter (under HSDP, also an all-reduce) at the end of every backward pass. Without maybe_no_sync, a step with grad_accum_steps = 8 fires them 8 times per optimizer step. With it, the first 7 accumulate locally and the 8th reduces the accumulated gradients once.

  • A model whose root is an FSDP2 module, as a text model’s is, is switched through its own set_requires_gradient_sync, which recurses into every FSDP2 submodule. A torch.compile wrapper forwards the attribute.

  • A model whose root is not an FSDP2 module and whose components are sharded on their own, such as a VLMWrapper (its blocks, its transformer, its adapter and a trainable vision encoder are each a unit), has no such method. It is switched one unit at a time: set_requires_gradient_sync(False, recurse=False) on each FSDPModule the model holds. model.modules() reaches them behind a torch.compile wrapper too.

  • A unit whose policy reduces in bf16 (train.grad_reduce_dtype or vision_encoder.grad_reduce_dtype set to "bf16"; FSDP2 reduces in the policy’s reduce_dtype, else its param_dtype) is left out and syncs on every microbatch: skipping it would hold its running sum unsharded in bf16, while syncing adds each microbatch’s reduced gradient into the sharded gradient, in the parameters’ dtype. The units that reduce in fp32 keep skipping, so a VLM with a bf16 transformer and an fp32 encoder reduces the transformer and adapter every microbatch and the encoder once. See FSDP2 § Gradient reduce dtype.

  • Only a model with no FSDP2 unit that skips, as on one GPU, makes the context a no-op, so it is safe to use unconditionally.

  • On the last microbatch (micro_step + 1 == grad_accum_steps), the context skips straight to yield so the final backward triggers a normal reduce-scatter.

Memory cost: while sync is off, each unit keeps its unsharded gradient in the reduce dtype (fp32 under the default mixed precision policy) from the first microbatch’s backward until the last microbatch reduces it. That is about 4 bytes per trainable parameter on every GPU, divided by the tensor-parallel degree, on top of the sharded parameters and optimizer state; frozen parameters keep none. It is why grad_accum > 1 runs out of memory in the 175B benchmark (about 175 GB per GPU at TP 4).

Not used under pipeline parallel — the PP schedule manages its own sync internally. See Training loop § PP step.

clip_grad_norm_

from kempnerforge.distributed import clip_grad_norm_

grad_norm = clip_grad_norm_(model, tc.grad_clip_norm)

Lives in kempnerforge/distributed/utils.py (re-exported from kempnerforge.distributed). Wraps torch.nn.utils.clip_grad_norm_ with an extra path for mixed DTensor meshes.

The problem it solves: when a model combines TP and FSDP, some parameters are DTensors on the (dp_shard, tp) mesh (TP-sharded linears) and others are on (dp_shard,) alone (norm scales, biases). PyTorch’s clip_grad_norm_ doesn’t know how to combine gradients living on different meshes — it would produce an incorrect norm.

Algorithm:

  1. Collect all non-None .grad tensors from the model.

  2. Build a mesh_key per gradient (DTensor’s _spec.mesh id, or 0 for plain tensors).

  3. If only one mesh (pure FSDP, single GPU, plain tensors) and no pipeline or expert parallelism: call torch.nn.utils.clip_grad_norm_ directly — the fast path. Under either of those, the norm is summed over the stages’ or the EP group as described below.

  4. If multiple meshes: group gradients by mesh and compute each group’s sum-of-squares from per-gradient norms taken in at least fp32 (fp16 and bf16 are promoted, fp64 stays fp64). A DTensor norm is partial over its shards, and squaring it runs the all-reduce that replicates it, so .full_tensor() only unwraps each group’s sum. The groups’ sums are added and combined with a plain sqrt. In fp16, squaring a norm above about 256 would overflow to inf and the clip would zero every gradient.

  5. Scale every gradient by clip_coef = max_norm / (total_norm + 1e-6), clamped to ≤ 1.

Returns the pre-clip total norm as a scalar tensor — the value you log as grad_norm in metrics.

Under pipeline parallelism

Each PP rank holds one stage, and a stage’s own norm is not the model’s. Pass the stages’ process group as pp_group:

grad_norm = clip_grad_norm_(stage, tc.grad_clip_norm, pp_group=device_mesh["pp"].get_group())

Each stage computes its sum of squares as in step 4, the sums are added over pp_group, and every stage scales by the coefficient of that whole-model norm. The clipped model’s norm is then at most max_norm, and every stage returns, and logs, the same pre-clip norm. Clipping each stage on its own instead leaves the model at sqrt(pp) × max_norm when every stage is above the threshold, and the different coefficients change the update’s direction, not only its length. Every stage must make the call, including one without gradients, since the sum is a collective. pipeline_step in training/loop.py passes the group.

Under expert parallelism

With ep > 1 each EP rank holds its own slice of the routed experts and a copy of every other parameter, so a rank’s own norm is not the model’s either. No argument is needed: clip_grad_norm_ reads the EP group from the model’s MoEMLP layers (expert_parallel_params in kempnerforge/distributed/expert_parallel.py) and, in one all-reduce over that group, sums the routed experts’ squared norms and averages the squared norms of the copies. Every EP rank then returns the whole model’s norm and scales by the same coefficient; the norm is the same on every EP rank even when the copies’ gradients differ in the last bits. Clipping each rank by its own norm instead would scale the copies by different coefficients. Every EP rank must make the call, since the sum is a collective.

The training loop calls clip_grad_norm_ through clip_grads, which first gives every EP rank the EP group’s mean gradient of each copy (average_non_expert_grads_). On GPU the EP ranks’ backward passes can round those gradients differently, and the mean makes them the same, so every EP rank clips and steps its copies alike and they stay bitwise equal. A loop that calls clip_grad_norm_ directly makes that call first. How the expert gradients themselves are averaged over the group is in Expert parallelism § Gradients and clipping.

foreach parameter

clip_grad_norm_(..., foreach=True) is the default — faster on CUDA via the fused foreach implementation. Only touched if your model contains some exotic parameter type that the foreach path can’t handle.

Gradient accumulation in practice

The full microbatching pattern in training/loop.py is:

for micro_step in range(tc.grad_accum_steps):
    with maybe_no_sync(model, micro_step, tc.grad_accum_steps):
        logits = model(input_ids, doc_ids=doc_ids)
        loss   = loss_fn(logits, labels)
        scaled_loss = loss / tc.grad_accum_steps
        scaled_loss.backward()
        total_loss += loss.item()

grad_norm = clip_grads(model, tc.grad_clip_norm)
optimizer.step()
optimizer.zero_grad()

Two invariants:

  • Loss scaling: scaled_loss = loss / grad_accum_steps keeps the effective LR independent of the accumulation factor. If you change grad_accum_steps=4 → 8, the optimizer step size stays the same per-token.

  • Clip after accumulate: clip_grads runs after the microbatch loop, not inside it, so the EP average and the clip see the final accumulated gradient.

Config fields

[train]
grad_accum_steps = 8                    # microbatches per optimizer step
grad_clip_norm = 1.0                    # max grad L2 norm

Both live in TrainConfig. TrainConfig.__post_init__ requires grad_clip_norm > 0 — there is no “disable clipping” escape hatch; 1.0 is a near-free safety margin and what every shipped config uses.

See also