Skip to content

vocab_parallel_torch

log_prob_of from softmax_torch.py for logits sharded over the class dimension across a torch.distributed group (tensor-parallel language-model heads). Chunked over rows; recomputes the softmax in backward to bound memory.

vocab_parallel_log_prob_of

vocab_parallel_log_prob_of(
    logits_shard: Tensor,
    target: Tensor,
    group: ProcessGroup | None = None,
    chunk: int = _DEFAULT_CHUNK,
) -> torch.Tensor

log_prob_of for logits split over the last (class) dimension across group.

Every rank must call it with the same target and chunk. Backward recomputes the softmax chunk by chunk and returns the gradient of this rank's shard.

Parameters:

Name Type Description Default
logits_shard Tensor

[..., C / world], holding classes [rank * C / world, (rank + 1) * C / world) (equal shards, ranks in order).

required
target Tensor

[...] global class indices, the same on every rank.

required
group ProcessGroup | None

torch.distributed process group. None (or an uninitialised torch.distributed) treats the logits as unsharded.

None
chunk int

Rows processed at a time, to bound temporary memory.

_DEFAULT_CHUNK

Returns:

Type Description
Tensor

log p(target) of shape target.shape, identical on every rank.

Source code in vocab_parallel_torch.py
def vocab_parallel_log_prob_of(logits_shard: torch.Tensor, target: torch.Tensor,
                               group: dist.ProcessGroup | None = None,
                               chunk: int = _DEFAULT_CHUNK) -> torch.Tensor:
    """``log_prob_of`` for logits split over the last (class) dimension across ``group``.

    Every rank must call it with the same ``target`` and ``chunk``. Backward recomputes the
    softmax chunk by chunk and returns the gradient of this rank's shard.

    Args:
        logits_shard: ``[..., C / world]``, holding classes ``[rank * C / world, (rank + 1) * C /
            world)`` (equal shards, ranks in order).
        target: ``[...]`` *global* class indices, the same on every rank.
        group: ``torch.distributed`` process group. ``None`` (or an uninitialised
            ``torch.distributed``) treats the logits as unsharded.
        chunk: Rows processed at a time, to bound temporary memory.

    Returns:
        ``log p(target)`` of shape ``target.shape``, identical on every rank.
    """
    vp = logits_shard.size(-1)
    out = _VocabParallelLogProb.apply(logits_shard.reshape(-1, vp), target.reshape(-1), group, chunk)
    return out.reshape(target.shape)