Skip to content

Fall back to first visible GPU when local rank exceeds device count - #3843

Open
rishabhsai wants to merge 1 commit into
pytorch:masterfrom
rishabhsai:fix-slurm-ntasks-per-gpu-local-rank
Open

rishabhsai wants to merge 1 commit into
pytorch:masterfrom
rishabhsai:fix-slurm-ntasks-per-gpu-local-rank

Conversation

@rishabhsai

Copy link
Copy Markdown

Fixes #3259

Description:

Launching with srun --ntasks-per-gpu=1 --nodes=2 --gpus-per-node=4 gives each task a single visible GPU, but SLURM_LOCALID keeps counting tasks on the node, so idist.initialize("nccl") reaches torch.cuda.set_device(3) in a process that can only see device 0 and every task with a local rank above 0 dies. ignite/distributed/comp_models/native.py now uses the first visible device when the local rank is not a valid device index, which is the fallback suggested in this issue, and device() stops warning about the mismatch in that case. auto_model passes the current CUDA device to DistributedDataParallel instead of the local rank, so device_ids stays consistent with idist.device(), which is the device the model was already moved to. This was checked with a unit test that fakes the visible device count and with the existing distributed test suites on CPU, not on a real Slurm cluster.

Check list:

  • New tests are added (if a new feature is added)
  • New doc strings: description and/or example code are in RST format
  • Documentation is updated (if required)

…e device count

Running under `srun --ntasks-per-gpu=1` gives each task a single visible GPU
while SLURM_LOCALID still counts tasks on the node, so `torch.cuda.set_device`
gets an index that does not exist and every task with a local rank above 0
dies during `idist.initialize`. Use the first visible device in that case and
pass the same index to DistributedDataParallel in `auto_model` so it matches
`idist.device()`.

Fixes pytorch#3259
@github-actions github-actions Bot added the module: distributed Distributed module label Sep 14, 2026
@vfdev-5

vfdev-5 commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

@rishabhsai can you check your PR with on SLURM and first repro the issue and then check the fix?

@rishabhsai

Copy link
Copy Markdown
Author

Haven't been able to get on a multi-GPU machine yet, so I haven't reproduced the original failure end to end. The unit test covers the device-index logic (it fakes the visible device count and fails on main with set_device(3) instead of set_device(0)), but that's not the same as seeing it under a real allocation.

I'll follow up when I have access, or happy for you to take it from here if that's faster.

One question in the meantime: I can reproduce the same condition on a two-GPU box by setting the SLURM env vars ignite reads with one GPU visible per process, which hits the same set_device path without a scheduler. Would that be useful to you, or do you specifically want it verified under Slurm?

@vfdev-5

vfdev-5 commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator

The original issue is with slurm, so first we need to understand in very detail the problem with the config described in the issue and reproduce it on slurm. Next, propose a change that wont break current behavior (with and without slurm) and once approved finally implement it.
It is not the point to merge something that does not solve the original issue (even if you try to mock the slurm env vars)

@rishabhsai

Copy link
Copy Markdown
Author

The original issue is with slurm, so first we need to understand in very detail the problem with the config described in the issue and reproduce it on slurm. Next, propose a change that wont break current behavior (with and without slurm) and once approved finally implement it.

It is not the point to merge something that does not solve the original issue (even if you try to mock the slurm env vars)

that makes sense, will do!

thanks!

@pytorch pytorch deleted a comment from rishabhsai Sep 15, 2026

This branch has not been deployed

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

Labels

module: distributed Distributed module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

idist.initialize fails in Slurm when using --ntasks-per-gpu

2 participants