Fix: Defensively close GPU device FDs in dataloader worker processes#3684
Draft
hexinw-nvidia wants to merge 2 commits intoNVIDIA:mainfrom
Draft
Fix: Defensively close GPU device FDs in dataloader worker processes#3684hexinw-nvidia wants to merge 2 commits intoNVIDIA:mainfrom
hexinw-nvidia wants to merge 2 commits intoNVIDIA:mainfrom
Conversation
This ensures workers do not keep references into NVIDIA memory space after fork.
This helps ensure GPU memory can be reclaimed even if a dataloader worker is
delayed or fails to exit.
How to Reproduce / Validate:
1. Force a long-running dataloader worker
Modify GPTDataset.__getitem__ to insert:
time.sleep(3600)
This simulates a stuck dataloader worker (e.g., blocked in I/O).
2. Start training
Launch a 1-node Megatron-LM job with:
--num-workers > 0
3. Verify dataloader workers are alive
On the GPU node:
sudo fuser -v /dev/nvidia*
You should see the dataloader worker processes listed.
With this patch, they should not retain active /dev/nvidia* file
descriptors even though they are running.
4. Trigger a rank failure
Send SIGTERM to one of the training ranks:
kill -15 <rank_pid>
5. Observe GPU memory reclaim
Run:
nvidia-smi
The corresponding rank’s GPU memory usage should return to 0
immediately (assuming no other GPU-holding child processes such as async
checkpoint workers are present in this test).
6. Baseline (without this patch)
Repeat the same steps without this change.
After killing the rank in step 4, you will observe that GPU memory
remains non-zero in nvidia-smi, because the dataloader worker still
holds /dev/nvidia* references.
Member
|
/claude review |
| if path.startswith("/dev/nvidia"): | ||
| os.close(int(fd)) | ||
| except Exception: | ||
| pass |
There was a problem hiding this comment.
The bare except Exception: pass silently swallows all errors. At minimum, unexpected exceptions (not OSError/FileNotFoundError) deserve a warning log, otherwise bugs here are invisible in production.
Suggested change
| pass | |
| except OSError: | |
| pass |
CUDA fd paths like /dev/nvidia0, /dev/nvidiactl, /dev/nvidia-uvm should reliably resolve, so only OSError (ENOENT for already-closed fds, EBADF, etc.) is expected here.
Member
|
We are changing our review process and marking all open, unlabeled PRs as draft. This change will go in effect starting once #3659 is merged. Moving forward, all PRs will be required to start as draft PRs. If you wish to get your PR merged, mark your PR as “Ready for review”. Read more about the new process at submit.md. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This ensures workers do not keep references into NVIDIA memory space after fork. This helps ensure GPU memory can be reclaimed even if a dataloader worker is delayed or fails to exit.
How to Reproduce / Validate:
Force a long-running dataloader worker
Modify GPTDataset.__getitem__ to insert:
time.sleep(3600)
This simulates a stuck dataloader worker (e.g., blocked in I/O).
Start training
Launch a 1-node Megatron-LM job with:
--num-workers > 0
Verify dataloader workers are alive
On the GPU node:
sudo fuser -v /dev/nvidia*
You should see the dataloader worker processes listed.
With this patch, they should not retain active /dev/nvidia* file
descriptors even though they are running.
Trigger a rank failure
Send SIGTERM to one of the training ranks:
kill -15 <rank_pid>
Observe GPU memory reclaim
Run:
nvidia-smi
The corresponding rank’s GPU memory usage should return to 0
immediately (assuming no other GPU-holding child processes such as async
checkpoint workers are present in this test).
Baseline (without this patch)
Repeat the same steps without this change.
After killing the rank in step 4, you will observe that GPU memory remains non-zero in nvidia-smi, because the dataloader worker still holds /dev/nvidia* references.