Skip to content

Distributed Torch inference binds CUDA devices using global rank #266

Description

@sylvesterkaczmarek

Summary

gpt_oss.torch.utils.init_distributed() uses global RANK as the CUDA device index:

rank = int(os.environ.get("RANK", 0))
...
torch.cuda.set_device(rank)
device = torch.device(f"cuda:{rank}")

That happens to work for single-node launches where global and local ranks are identical. Under standard multi-node torchrun, RANK is global across all workers while LOCAL_RANK identifies the GPU/process on the current node.

Reproducer

On node 2 of a two-node, four-GPU-per-node launch, a worker can have:

RANK=4
LOCAL_RANK=0
WORLD_SIZE=8

The current code attempts to bind cuda:4 on a host whose local devices are indexed 0-3.

Impact

The reference Torch generation/chat path cannot use standard multi-node distributed launches correctly and can fail with an invalid CUDA device ordinal on every node after the first.

Proposed resolution

Keep global RANK for dist.init_process_group() and rank-aware output suppression, but use LOCAL_RANK for torch.cuda.set_device() and the returned CUDA device. Fall back to RANK when LOCAL_RANK is not present to preserve existing custom/single-node launch behavior.

Add a unit regression with different global and local ranks that verifies process-group initialization receives the global rank while CUDA device selection uses the local rank.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions