fix: enable multi-GPU DDP training in Jupyter notebooks#928
Open
mfazrinizar wants to merge 15 commits intoroboflow:developfrom
Open
fix: enable multi-GPU DDP training in Jupyter notebooks#928mfazrinizar wants to merge 15 commits intoroboflow:developfrom
mfazrinizar wants to merge 15 commits intoroboflow:developfrom
Conversation
Codecov Report❌ Patch coverage is ❌ Your patch check has failed because the patch coverage (62%) is below the target coverage (95%). You can increase the patch coverage or adjust the target coverage.
Additional details and impacted files@@ Coverage Diff @@
## develop #928 +/- ##
========================================
- Coverage 79% 57% -22%
========================================
Files 97 97
Lines 7793 7832 +39
========================================
- Hits 6148 4454 -1694
- Misses 1645 3378 +1733 🚀 New features to boost your workflow:
|
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.
What does this PR do?
Fixes multi-GPU DDP training (
strategy="ddp_notebook"andstrategy="ddp_spawn") which was completely broken in Jupyter (e.g. Kaggle) notebook environments. The fix addresses two layers of issues:CUDA early initialization:
RFDETRBase()eagerly moved the model to CUDA during__init__(), and module-leveltorch.cuda.is_available()inconfig.pycreated a CUDA driver context at import time, making multi-process training impossible.OpenMP thread pool corruption after fork: Even after fixing CUDA init, PyTorch's OpenMP thread pool (created during model construction) cannot survive
fork(). The worker threads become zombie handles, causingSIGABRT: Invalid thread pool!when the autograd engine initializes in forked children. Fixed by transparently replacing fork-based DDP with a spawn-based strategy.Related Issue(s): Fixes #923
Type of Change
Testing
Test details:
Unit tests (101 pass locally)
test_build_trainer.py: 52 tests covering precision resolution, strategy selection, ddp_notebook→spawn mapping, EMA guards, logger wiringtest_module_data.py: 49 tests includingtest_ddp_notebook_preserves_num_workersandtest_other_strategy_preserves_num_workersIntegration test (Kaggle T4 x2)
Validated on Kaggle GPU T4 x2 accelerator (Python 3.12, PyTorch 2.10.0+cu128, PTL 2.6.1):
RFDETRBase()strategy="ddp_notebook"training (3 epochs, 2×T4)strategy="ddp_spawn"training (3 epochs, 2×T4)What This Fixes
model.train(devices=2, strategy="ddp_notebook")in notebookmodel.train(devices=2, strategy="ddp_spawn")in notebookmodel.train(devices=1)model.predict(img)model.train() → model.predict(img)model.export_onnx()/model.optimize_for_inference()Checklist
Additional Context
The
ddp_notebook → spawnconversion is transparent to users: they continue passingstrategy="ddp_notebook"(orstrategy="ddp_spawn") and training just works. An INFO log message is emitted:The
find_unused_parameters=Trueflag is required because RF-DETR's architecture has parameters in the detection head that may not contribute to every loss term (e.g. encoder-only auxiliary losses).Technical Details
Two layers of CUDA initialization that had to be fixed
Module-level (
config.py):torch.cuda.is_available()creates a CUDA driver context at import time. Fixed withtorch.accelerator.current_accelerator()which queries NVML without creating a primary context.Model construction (
inference.py):nn_model.to("cuda")fully initializes the CUDA runtime. Fixed by keeping the model on CPU and deferring.to(device)to firstpredict()/export()/batch_size="auto"call via_ensure_model_on_device().Why spawn instead of fork
PyTorch creates an OpenMP thread pool (default 8 threads) during the first tensor operation (model construction).
fork()only copies the calling thread, OMP worker threads become zombie handles. When the autograd engine in forked children callsset_num_threadsduringthread_init, the OMP runtime finds an invalid pool state and aborts:This is a fundamental fork+OMP incompatibility; as far as I know, there is no library-level workaround. The fix transparently replaces fork-based
ddp_notebookwith a spawn-based_NotebookSpawnDDPStrategywhose launcher is markedis_interactive_compatible = True, allowing PTL to accept it in notebook environments.Performance impact
predict()call: ~50-200ms one-time latency from CPU→GPU model transfer. Strictly one-time,_ensure_model_on_device()checksfirst_param.device != targetand becomes a no-op once the model is on GPU. Aftertrain(), the PTL-trained model is already on CUDA (synced at line 548), so even the first post-trainingpredict()has zero transfer cost.predict()calls: Zero overhead (singlenext(parameters()).devicecomparison)RFDETRBase() → predict()without training): The one-time transfer happens on the very first call only. All subsequent calls, including batch evaluation loops, are zero-overhead.