Commit eba5d27
authored
Avoid redundant copies in MPS P2P staging (#8314)
## Summary
- make staged MPS `isend` input-only: take one CPU snapshot and retain
it through completion without copying it back
- make staged MPS `irecv` output-only: allocate an empty CPU target
without reading the destination, then publish it only after the
underlying bare `wait()` succeeds
- preserve the backend's native `None` return for non-member ranks and
never publish an unconfirmed or failed receive buffer
- keep `StagedWork.wait()` deliberately narrow instead of broadening the
Work contract
Follow-up to #8303. The directional-staging scope was approved by
@delock in [this review
comment](#8303 (comment)).
## Why
#8303 made `isend` and `irecv` genuinely asynchronous and deferred MPS
publication until completion. The generic staging path still treats
every tensor as both input and output, which adds two transfers that
cannot affect a point-to-point result:
- `isend` copies the staged CPU payload back to an unchanged MPS source
after `wait()`;
- `irecv` copies the old MPS destination to CPU before the receive
overwrites it.
P2P direction is known at the call site, so those copies can be removed
without changing the default in-out staging contract used by other
collectives.
## Correctness contract
- `isend` keeps its CPU payload reachable through the returned work
handle.
- `irecv` copies CPU to MPS only after the backend `wait()` returns
successfully.
- a failed receive leaves the caller's destination unchanged.
- a backend `None` return remains `None`, including non-member group
ranks, and never publishes the empty receive target.
- `is_completed()`, timeout-aware `wait()`, `get_future()`, and
`result()` remain outside this change, matching the maintainer-approved
scope.
## Verification
Exact local head: `5caf2b6f9aef9ee52a2c3ba58a1a24059cf35add`.
- all repository pre-commit hooks for both changed files
- four single-process real-MPS direction, failure, and `None`-return
tests: `4 passed`
- real two-rank MPS/Gloo member transfer plus non-member subgroup
behavior: `2 passed`
- real two-rank CPU/Gloo member transfer plus non-member subgroup
behavior: `2 passed`
- complete `tests/unit/comm/test_dist.py` on MPS: `16 passed, 19
skipped`
- `git diff --check`
The new four-test direction/failure matrix was first run against the
unchanged #8303 source: `3 failed, 1 passed`. It is `4 passed` with this
patch.
## Local staging evidence
An instrumented no-network microbenchmark on one Apple M5 Pro host
(Python 3.12.13, PyTorch 2.13.0) confirmed the expected
Python/ATen-visible cross-device copy matrix at 1, 16, and 64 MiB:
| Operation | Creation | Wait |
|---|---|---|
| `isend` | one MPS-to-CPU snapshot | no CPU-to-MPS copy-back |
| `irecv` | no MPS-to-CPU pre-copy | one CPU-to-MPS publication after
success |
For the eliminated local phases, median p50 reductions were 99.84-99.96%
for `isend` wait and 96.41-98.11% for `irecv` creation. These
measurements isolate wrapper staging overhead with a completed fake
Work; they are not end-to-end Gloo, pipeline, training, or multi-node
speedup claims.
Signed-off-by: Fu Xiaonan <ht3fudatou@163.com>1 parent 6e3bd08 commit eba5d27
2 files changed
Lines changed: 133 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
100 | 103 | | |
101 | 104 | | |
102 | 105 | | |
| |||
115 | 118 | | |
116 | 119 | | |
117 | 120 | | |
118 | | - | |
| 121 | + | |
119 | 122 | | |
120 | 123 | | |
121 | 124 | | |
122 | 125 | | |
123 | 126 | | |
124 | 127 | | |
125 | 128 | | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
126 | 132 | | |
127 | 133 | | |
128 | | - | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
129 | 140 | | |
130 | 141 | | |
131 | 142 | | |
132 | | - | |
| 143 | + | |
133 | 144 | | |
134 | 145 | | |
135 | 146 | | |
| |||
148 | 159 | | |
149 | 160 | | |
150 | 161 | | |
151 | | - | |
152 | | - | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
153 | 165 | | |
154 | 166 | | |
155 | 167 | | |
156 | 168 | | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
157 | 173 | | |
158 | 174 | | |
159 | 175 | | |
| |||
433 | 449 | | |
434 | 450 | | |
435 | 451 | | |
436 | | - | |
| 452 | + | |
437 | 453 | | |
438 | 454 | | |
439 | 455 | | |
440 | 456 | | |
441 | | - | |
| 457 | + | |
442 | 458 | | |
443 | 459 | | |
444 | 460 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
134 | | - | |
| 134 | + | |
135 | 135 | | |
| 136 | + | |
136 | 137 | | |
137 | 138 | | |
| 139 | + | |
| 140 | + | |
138 | 141 | | |
139 | 142 | | |
140 | 143 | | |
| |||
145 | 148 | | |
146 | 149 | | |
147 | 150 | | |
148 | | - | |
| 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 | + | |
149 | 177 | | |
150 | 178 | | |
151 | 179 | | |
152 | 180 | | |
153 | 181 | | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
154 | 185 | | |
155 | | - | |
156 | | - | |
157 | 186 | | |
158 | 187 | | |
159 | 188 | | |
160 | | - | |
161 | 189 | | |
162 | 190 | | |
| 191 | + | |
| 192 | + | |
163 | 193 | | |
164 | 194 | | |
165 | | - | |
| 195 | + | |
| 196 | + | |
166 | 197 | | |
167 | 198 | | |
168 | 199 | | |
169 | | - | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
170 | 217 | | |
171 | 218 | | |
172 | 219 | | |
173 | 220 | | |
174 | 221 | | |
| 222 | + | |
| 223 | + | |
175 | 224 | | |
176 | | - | |
177 | | - | |
178 | 225 | | |
179 | 226 | | |
180 | 227 | | |
181 | 228 | | |
182 | 229 | | |
183 | 230 | | |
| 231 | + | |
184 | 232 | | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
185 | 264 | | |
186 | 265 | | |
187 | 266 | | |
188 | 267 | | |
189 | 268 | | |
190 | 269 | | |
191 | 270 | | |
192 | | - | |
193 | | - | |
194 | | - | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
195 | 275 | | |
196 | 276 | | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
197 | 281 | | |
198 | 282 | | |
199 | 283 | | |
| |||
215 | 299 | | |
216 | 300 | | |
217 | 301 | | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
218 | 315 | | |
219 | 316 | | |
220 | 317 | | |
| |||
0 commit comments