Commit 0b8a727
committed
feat(megatron): add manual CPU offload as an alternative to torch_memory_saver
Colocate training offloads the train actor to CPU on every sleep/wake. The
only path was torch_memory_saver (TMS) VMM pause()/resume(), which is
semantically blind (backs up the whole runtime pool) and unsafe on backends
without expandable_segments (e.g. Kunlunxin P800). This adds an explicit,
application-level selective offload and lets it be selected per job.
Behavior:
- Default stays TMS.
- New --manual-offload switches to selective offload: only live train state
(param flat buffers + optimizer master/Adam state) is copied to CPU; grads
and recomputable buffers are just released, then empty_cache() returns the
freed pages to the driver.
Changes:
- weight_update/train_offload.py (new): MegatronTrainStateOffloader facade
delegating to _TmsOffloadStrategy / _ManualOffloadStrategy behind an
_OffloadStrategy Protocol. Owns TMS margin init and strategy selection
(TMS when LD_PRELOAD'ed, else manual; --manual-offload forces manual).
- backends/megatron/actor.py: sleep()/wake_up()/update_weights() delegate to
the offloader; drop the inline _torch_memory_saver_enabled block and the
torch_memory_saver / nullcontext imports.
- weight_update/common.py: _maybe_get_cpu_backup() reads from the offloader's
_relax_cpu_offload_data when a param's GPU storage was freed, and only takes
the TMS get_cpu_backup() branch when the hook is actually LD_PRELOAD'ed.
- distributed/ray/actor_group.py: skip LD_PRELOAD + TMS_INIT env vars under
--manual-offload so TMS is not the active mechanism.
- utils/arguments.py: add --manual-offload.
- scripts/training/text/run-qwen35-8xklx.sh: pass --manual-offload for P800.
No behavior change for existing TMS/CUDA users (default path untouched).1 parent f1881a9 commit 0b8a727
6 files changed
Lines changed: 353 additions & 38 deletions
File tree
- relax
- backends/megatron
- weight_update
- distributed/ray
- utils
- scripts/training/text
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
| |||
25 | 24 | | |
26 | 25 | | |
27 | 26 | | |
28 | | - | |
29 | 27 | | |
30 | 28 | | |
31 | 29 | | |
| |||
94 | 92 | | |
95 | 93 | | |
96 | 94 | | |
| 95 | + | |
97 | 96 | | |
98 | 97 | | |
99 | 98 | | |
| |||
174 | 173 | | |
175 | 174 | | |
176 | 175 | | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | 176 | | |
195 | 177 | | |
196 | 178 | | |
| |||
201 | 183 | | |
202 | 184 | | |
203 | 185 | | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
204 | 191 | | |
205 | 192 | | |
206 | 193 | | |
| |||
361 | 348 | | |
362 | 349 | | |
363 | 350 | | |
364 | | - | |
365 | | - | |
| 351 | + | |
366 | 352 | | |
367 | 353 | | |
368 | 354 | | |
| |||
371 | 357 | | |
372 | 358 | | |
373 | 359 | | |
374 | | - | |
375 | | - | |
| 360 | + | |
376 | 361 | | |
377 | 362 | | |
378 | 363 | | |
| |||
1530 | 1515 | | |
1531 | 1516 | | |
1532 | 1517 | | |
1533 | | - | |
1534 | | - | |
1535 | | - | |
1536 | | - | |
1537 | | - | |
| 1518 | + | |
1538 | 1519 | | |
1539 | 1520 | | |
1540 | 1521 | | |
| |||
2092 | 2073 | | |
2093 | 2074 | | |
2094 | 2075 | | |
| 2076 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | 114 | | |
118 | 115 | | |
119 | 116 | | |
| |||
207 | 204 | | |
208 | 205 | | |
209 | 206 | | |
210 | | - | |
211 | | - | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
212 | 214 | | |
213 | | - | |
214 | | - | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
215 | 225 | | |
216 | 226 | | |
217 | 227 | | |
| |||
330 | 340 | | |
331 | 341 | | |
332 | 342 | | |
| 343 | + | |
0 commit comments