Commit 59e95c7
mscclpp_torchcomms: build _comms_nccl.so with bad_weak_ptr fix
Adds a separate _comms_nccl pybind11 module alongside the existing
_comms_mscclpp module. The .so wraps the upstream torchcomms NCCL
backend sources from build-torchcomm/_deps/torchcomms-src/comms/
torchcomms/nccl/, paired with a new csrc/NcclDynamicLoader.cpp that
publishes the create_dynamic_loader_nccl entry point torchcomms's
TorchCommFactory dlopen path requires.
Why this exists:
TorchCommFactory::create_generic_backend (TorchCommFactory.cpp)
wraps the raw pointer returned by loader.new_comm() in a
std::shared_ptr<TorchCommBackend>(rawBackendPtr, deleter).
std::enable_shared_from_this<Y>'s internal weak_ptr is only
initialized when the shared_ptr is constructed from a pointer
*statically typed* as the derived class. Constructing
shared_ptr<TorchCommBackend> from a pointer typed as
TorchCommBackend* skips that machinery, so when
TorchCommNCCL::createWork() later calls shared_from_this() it
throws std::bad_weak_ptr — the very first all_reduce crashes,
before any user code runs.
Verified with a 6-line minimal repro (torchcomms.new_comm("nccl",
...).all_reduce(...)) that crashes with the upstream-only build
and now succeeds with this fix.
How the fix works:
NcclDynamicLoader.cpp::new_comm_impl creates TorchCommNCCL via
std::make_shared so the weak_ptr is properly populated, then
stashes the shared_ptr in a static keep-alive map keyed by the
TorchCommBackend* it returns. destroy_comm_impl drops the
keep-alive entry. While the entry lives, shared_from_this() inside
the NCCL backend constructs a new shared_ptr that aliases our
keep-alive one — no upstream changes required.
CMakeLists.txt:
- Exclude NcclDynamicLoader.cpp from _comms_mscclpp source glob
(it's the entry point for the separate _comms_nccl target).
- Add _comms_nccl pybind11_add_module target that compiles the
upstream torchcomms NCCL backend sources + the framework set
shared with _comms_mscclpp + our loader. Links against PyTorch's
bundled libnccl.so.2, torch libs, GPU libs, and glog.
- Compile with FMT_HEADER_ONLY to sidestep the fmt v11/v12 ABI
mismatch between conda's libfmt.so.11 and PyTorch's bundled fmt
v12 headers (otherwise the .so dlopen fails with
'undefined symbol: fmt::v12::vformat...').
- Define USE_NVSHMEM to match the historical build's compile flags
(NcclApi.cpp checks the macro).
- torchcomm_nccl_copy custom target installs the .so into the
package source tree alongside _comms_mscclpp.
Build with:
./build_torchcomm.sh config
cmake --build build-torchcomm --target _comms_nccl -j
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent e729d11 commit 59e95c7
2 files changed
Lines changed: 175 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
| 33 | + | |
33 | 34 | | |
| 35 | + | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
| |||
109 | 111 | | |
110 | 112 | | |
111 | 113 | | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
0 commit comments