kempnerforge.distributed.utils

Distributed training utilities.

Gradient clipping and data-parallel helpers that work correctly with FSDP2 and multi-dimensional parallelism.

Functions

agree_across_ranks(error, what, *[, group])

Barrier on group that raises on every rank if any rank's what failed.

any_rank(flag, group)

Whether flag is True on any rank of group.

any_rank_each(flags, group)

Whether each of flags is True on any rank of group, all in one all-reduce.

clip_grad_norm_(model, max_norm[, foreach, ...])

Clip gradient norm across all parameters.

data_parallel_groups(device_mesh)

The process groups whose sums, taken in turn, total a value over the DP ranks.

dp_all_reduce_(tensor, groups)

Sum tensor in place over the DP ranks that groups span, and return it.

dp_mean(value, groups, device)

Mean of value over the DP ranks that groups span; value itself when empty.

get_dp_info(device_mesh)

Get (dp_rank, dp_size) from the device mesh, accounting for PP/TP/EP.

new_control_group(timeout_sec)

A gloo group over every rank, for the decisions every rank must take together.

kempnerforge.distributed.utils.get_dp_info(device_mesh)[source]

Get (dp_rank, dp_size) from the device mesh, accounting for PP/TP/EP.

Only dp_replicate and dp_shard split the data, so ranks that differ only in pp, ep or tp share a DP rank. Under HSDP the DP rank is the row-major index of the rank’s (dp_replicate, dp_shard) coordinates and the DP size is their product; no process group is created.

Parameters:

device_mesh (DeviceMesh | None) – Full DeviceMesh, or None for single-GPU.

Returns:

Tuple of (dp_rank, dp_world_size); (0, 1) when there is no mesh or the mesh has no DP dimension.

Return type:

tuple[int, int]

kempnerforge.distributed.utils.data_parallel_groups(device_mesh)[source]

The process groups whose sums, taken in turn, total a value over the DP ranks.

One group for each of dp_shard and dp_replicate with more than one rank, taken from the mesh, so none is created. Ranks that differ only in pp, ep or tp share a DP rank, hold the same data, and are in none of them. Empty without a mesh or with one DP rank.

Parameters:

device_mesh (DeviceMesh | None) – Full DeviceMesh, or None for single-GPU.

Returns:

The groups, for dp_all_reduce_ and dp_mean.

Return type:

tuple[dist.ProcessGroup, …]

kempnerforge.distributed.utils.dp_all_reduce_(tensor, groups)[source]

Sum tensor in place over the DP ranks that groups span, and return it.

groups comes from data_parallel_groups; every rank in them must call this.

Parameters:
Return type:

torch.Tensor

kempnerforge.distributed.utils.dp_mean(value, groups, device)[source]

Mean of value over the DP ranks that groups span; value itself when empty.

The sum is taken in float64. Every rank in groups must call this.

Parameters:
Return type:

float

kempnerforge.distributed.utils.clip_grad_norm_(model, max_norm, foreach=True, pp_group=None)[source]

Clip gradient norm across all parameters.

Handles mixed DTensor meshes that arise when TP+FSDP produces parameters on different meshes (e.g., TP-sharded linears on (dp_shard, tp) vs FSDP-only norms on (dp_shard)). Groups gradients by mesh, computes per-group norms via stack, then combines across groups.

Falls back to torch.nn.utils.clip_grad_norm_ when there is only one mesh (pure FSDP or single-GPU) and neither pipeline nor expert parallelism, whose norms need a sum over their own group.

Under pipeline parallelism model holds one stage, so pass the stages’ process group as pp_group: the squared stage norms are summed over it, and every stage returns the whole model’s norm and scales by the same coefficient. Every stage must make the call, including one without gradients, since the sum is a collective.

Under expert parallelism each EP rank holds its own routed experts and a copy of every other parameter. The experts’ squared norms are summed over the EP group the MoEMLP layers record, and the copies’ are averaged over it, so every EP rank returns the whole model’s norm and scales by the same coefficient. Every EP rank must make the call.

Parameters:
  • model (torch.nn.Module) – Model whose gradients to clip.

  • max_norm (float) – Maximum gradient norm.

  • foreach (bool) – Use the faster foreach implementation.

  • pp_group (dist.ProcessGroup | None) – Process group of the pipeline stages, or None without pipeline parallelism.

Returns:

Total gradient norm (before clipping).

Return type:

torch.Tensor

kempnerforge.distributed.utils.new_control_group(timeout_sec)[source]

A gloo group over every rank, for the decisions every rank must take together.

dcp.async_save issues gloo collectives on the default group from a background thread, so a main-thread CPU collective on that group can run in a different order on different ranks. A group of its own avoids that, and still reduces when NCCL is wedged. Every rank must call this.

Parameters:

timeout_sec (float) – Timeout of the group’s collectives.

Returns:

The group, or None when torch.distributed is not initialized.

Return type:

dist.ProcessGroup | None

kempnerforge.distributed.utils.any_rank(flag, group)[source]

Whether flag is True on any rank of group.

One MAX all-reduce of a CPU tensor, so group needs a CPU backend, and every rank of it must call.

Parameters:
  • flag (bool) – This rank’s flag.

  • group (dist.ProcessGroup | None) – The group to reduce over, or None for this rank alone.

Returns:

flag reduced over group, or flag itself when group is None.

Return type:

bool

kempnerforge.distributed.utils.any_rank_each(flags, group)[source]

Whether each of flags is True on any rank of group, all in one all-reduce.

One MAX all-reduce of a CPU tensor holding every flag, so group needs a CPU backend, and every rank of it must call with as many flags, in the same order.

Parameters:
  • flags (Sequence[bool]) – This rank’s flags.

  • group (dist.ProcessGroup | None) – The group to reduce over, or None for this rank alone.

Returns:

Each flag reduced over group, or the flags themselves when group is None.

Return type:

list[bool]

kempnerforge.distributed.utils.agree_across_ranks(error, what, *, group=None)[source]

Barrier on group that raises on every rank if any rank’s what failed.

Every rank passes the error its own part of what raised, or None. A rank with an error re-raises it; when any rank has one, every other rank raises a RuntimeError naming the ranks that failed, so no rank is left waiting in a later collective for a rank that has raised. Without an initialized process group, error is raised if set.

Parameters:
Return type:

None