Repository navigation
Conversation
This commit introduces ReFlow, an asynchronous CPU-offloaded optimizer path for DeepSpeed ZeRO-3. ReFlow reduces CPU optimizer overhead and overlaps bucket-wise CPU optimizer execution with GPU backward computation. Highlights: - ReFlow Adam/AdamW and Lion CPU optimizers. - BF16 gradient offload with SIMD-local FP32 promotion and CPU-side gradient accumulation. - Register-resident AVX/SIMD optimizer kernels. - Ready/Persist scheduling for overlapping CPU optimizer execution without FP32 optimizer-state rollback. - ReFlow configuration, unit/training tests, CI coverage, documentation, and tutorial. Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Optimizer remapping changes supported client semantics, public optimizer steps can access unregistered native state, and failed asynchronous commits may still be checkpointed.
Review effort: Balanced
Findings: 4
Open (9)
Adam remap drops maximize semantics · New ReflowCPULion inherits incompatible optimizer step · New ReflowCPULion inherits incompatible Lion step · New Checkpoint serialization ignores pending state update failures · New Lion remap drops fp32_optimizer_states · New NUMA validation runs on the wrong host · New Documentation disagrees with bucketwise cores default · New Reference documents incorrect worker core default · New Configuration reference omits public ReflowConfig fields · New
What changed in this PR
Adds Reflow, an asynchronous CPU-offloaded Adam/Lion path for ZeRO-3.
Changes:
- Adds bucket-wise asynchronous optimization, gradient accumulation, state persistence, and NUMA scheduling.
- Adds fused AVX kernels and accelerator builder integration.
- Adds configuration, documentation, and correctness/launcher tests.
| File | Description |
|---|---|
tests/unit/v1/zero/test_reflow.py |
Tests Reflow integration and correctness. |
tests/unit/v1/ops/test_reflow_cpu_optimizers.py |
Tests native optimizer kernels. |
tests/unit/launcher/test_ds_arguments.py |
Tests numactl fallback behavior. |
op_builder/xpu/cpu_adam.py |
Adds Reflow Adam sources. |
op_builder/supa/cpu_lion.py |
Adds Reflow Lion sources. |
op_builder/supa/cpu_adam.py |
Adds Reflow Adam sources. |
op_builder/sdaa/cpu_adam.py |
Adds Reflow Adam sources. |
op_builder/npu/cpu_lion.py |
Adds Reflow Lion sources. |
op_builder/npu/cpu_adam.py |
Adds Reflow Adam sources. |
op_builder/mps/cpu_adam.py |
Adds Reflow Adam sources. |
op_builder/mlu/cpu_adam.py |
Adds Reflow Adam sources. |
op_builder/hpu/cpu_adam.py |
Adds Reflow Adam sources. |
op_builder/cpu/cpu_adam.py |
Adds CPU Reflow sources. |
op_builder/cpu_lion.py |
Adds default Lion sources. |
op_builder/cpu_adam.py |
Adds default Adam sources. |
docs/code-docs/source/zero3.rst |
Exposes Reflow configuration API. |
docs/code-docs/source/optimizers.rst |
Publishes optimizer classes. |
docs/_tutorials/reflow.md |
Adds the Reflow tutorial. |
docs/_pages/config-json.md |
Documents Reflow configuration. |
docs/_data/navigation.yml |
Links the tutorial. |
deepspeed/utils/timer.py |
Adds current-stream synchronization. |
deepspeed/utils/numa.py |
Adds numactl permission fallback. |
deepspeed/runtime/zero/utils.py |
Registers supported optimizers. |
deepspeed/runtime/zero/partitioned_param_coordinator.py |
Orders all-gathers after Reflow writes. |
deepspeed/runtime/zero/config.py |
Adds compatibility validation. |
deepspeed/runtime/reflow/reflow_utils.py |
Implements affinity and range helpers. |
deepspeed/runtime/reflow/reflow_cpu_lion.py |
Implements Reflow Lion wrapper. |
deepspeed/runtime/reflow/reflow_cpu_adam.py |
Implements Reflow Adam wrapper. |
deepspeed/runtime/reflow/reflow_config.py |
Defines Reflow settings. |
deepspeed/runtime/reflow/reflow_stage3.py |
Implements asynchronous ZeRO-3 execution. |
deepspeed/runtime/reflow/README.md |
Documents internal design. |
deepspeed/runtime/reflow/engine.py |
Integrates and remaps optimizers. |
deepspeed/runtime/reflow/__init__.py |
Defines the Reflow package. |
deepspeed/runtime/engine.py |
Selects Reflow engine components. |
deepspeed/runtime/config.py |
Rejects incompatible configurations. |
csrc/lion/reflow_cpu_lion_impl.cpp |
Implements fused Lion kernels. |
csrc/lion/reflow_cpu_lion_bindings.cpp |
Exposes Lion native APIs. |
csrc/lion/cpu_lion.cpp |
Registers Reflow Lion bindings. |
csrc/includes/reflow_simd.h |
Adds SIMD conversion/store helpers. |
csrc/includes/reflow_cpu_lion.h |
Defines Reflow Lion kernels. |
csrc/includes/reflow_cpu_affinity.h |
Applies worker affinity. |
csrc/includes/reflow_cpu_adam.h |
Defines Reflow Adam kernels. |
csrc/includes/reflow_bindings.h |
Declares native bindings. |
csrc/adam/reflow_cpu_adam_impl.cpp |
Implements fused Adam kernels. |
csrc/adam/reflow_cpu_adam_bindings.cpp |
Exposes Adam native APIs. |
csrc/adam/cpu_adam.cpp |
Registers Reflow Adam bindings. |
ci/tests_fetcher.py |
Selects Reflow tests in CI. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
|
I’ve addressed all nine review comments in separate signed-off commits, including maximize support, optimizer and precision guards, checkpoint error handling, target-host NUMA checks and fallback, and documentation fixes. |
Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
|
Hi @tjruwase, @tohtana, and @hwchen2017, Just following up on this PR. All previous review comments have been addressed, and the implementation is ready for review. Could you please take a look when you have time? I'd appreciate any feedback or guidance on the next steps. Thank you! |
|
@st-bang97 thanks for so much for your PR and even more for your patience. Apologies for the delay. Taking a look now. |
Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
pengdurice
left a comment
There was a problem hiding this comment.
DeepSpeed will skip a step if the gradient from that step is invalid (e.g. nan or inf), is_invalid_grad_norm is used to check that. Does ReFlow call it and handle invalid clearly?
Thanks for pointing this out, @pengdurice! I checked the current implementation and confirmed that ReFlow handles FP16 gradient overflow but does not use "is_invalid_grad_norm()". I'll update the validation to use "combine_grad_norm_groups()" and "is_invalid_grad_norm()", consistent with the latest ZeRO-3 path, and add regression tests for NaN/Inf handling. I'm currently attending a conference, so I plan to push the changes next week. Thanks again for the review! |
No worries, take your time and enjoy your conference. BTW, does the current implementation fuse the invalidity check into the SIMD kernel? If not, do you think it is a good idea to do so to avoid the cost of python's is_invalid_grad_norm? Is that an overkill? |
Hi Peng, thanks for the detailed review and suggestions! 1. SIMD invalidity check That's a good suggestion! The invalidity check isn't currently fused into the SIMD kernel, but it could help reduce Python overhead. Since ReFlow computes gradient norms on the GPU and checks the resulting global norm separately, I'm not sure how much additional benefit SIMD fusion would provide. I think it's worth profiling and analyzing further before deciding whether to integrate it. I'll first update the invalidity handling to match the latest ZeRO-3 implementation. 2. Potential data race across micro-steps Good catch! The current implementation waits for the GPU copy to finish before CPU accumulation, but it doesn't explicitly guarantee that the accumulation completes before the next micro-step overwrites I haven't encountered this issue in my tests, but I agree that we should guarantee correctness regardless of execution timing. I'll add synchronization to protect buffer reuse while avoiding unnecessary blocking, along with a regression test. 3. AutoTP and AutoEP compatibility ReFlow was originally designed for dense models, and we've validated single- and multi-GPU dense training, but not specifically AutoTP or AutoEP. When we initially ported ReFlow to DeepSpeed ZeRO-3, we based the implementation on the existing ZeRO-3 code. Since #8670 was merged afterward, its changes, including One related optimization I'd like to mention is ReFlow's batched gradient-norm reduction. Instead of reducing each subgroup separately, it collects their squared norms into a GPU tensor and batches collective operations for subgroups sharing the same process group. This reduces the number of collective calls, particularly when there are many small subgroups, and could potentially benefit standard ZeRO-3 offload as well. However, this optimization was originally developed without AutoTP or AutoEP. I'd like to preserve it while incorporating the recent changes, and I'd appreciate your thoughts on whether this batching approach could be extended to these parallel configurations while maintaining correctness. I currently don't have access to a multi-GPU setup to validate these specific configurations, so I'd also appreciate some help with testing. I'll review the existing optimizer offload restrictions for AutoEP+AutoTP folding as well. Thanks again for your feedback! |
|
@st-bang97 , feel free to fix the failing checks and then I can merge it. |
There was a problem hiding this comment.
🟡 Changes recommended
SIMD paths skip unscaling for valid FP16 loss scales below 1, causing incorrect Adam moments and potentially reversing Lion updates while scalar tails behave correctly.
1 open issue
9 resolved since last review
Checkpoint serialization ignores pending state update failures ReflowCPULion inherits incompatible Lion step ReflowCPULion inherits incompatible optimizer step Adam remap drops maximize semantics NUMA validation runs on the wrong host Lion remap drops fp32_optimizer_states Configuration reference omits public ReflowConfig fields Reference documents incorrect worker core default Documentation disagrees with bucketwise cores default
Review effort: Balanced
There was a problem hiding this comment.
1. Please give ReFlow its own builder instead of adding it to cpu_adam/cpu_lion.
ReFlow is opt-in, but this PR compiles it into the existing cpu_adam and cpu_lion extensions (bind_reflow_adam(m) / bind_reflow_lion(m)). ReFlow's kernels require AVX2/AVX-512, while cpu_adam must also build on CPUs without them (NEON, SVE, RVV, scalar). This false dependency means a ReFlow build problem on such CPUs would break DeepSpeedCPUAdam/DeepSpeedCPULion (and ZeRO-Offload) for users who never enable ReFlow. Please add separate ReflowCPUAdamBuilder/ReflowCPULionBuilder, with is_compatible() requiring AVX2/AVX-512, and leave cpu_adam/cpu_lion unchanged.
2. Don't add ReFlow to other accelerators' builders unless it has been verified there.
The PR changes builders under op_builder/{hpu,klxpu,mlu,mps,npu,sdaa,supa,xpu}. The validation listed in the description covers only NVIDIA GPUs with x86 CPUs, and apart from MPS no vendor workflow ran on this PR. The runtime side is also CUDA-specific in places, for example the GPU→NUMA detection via torch.cuda.get_device_properties`.
Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
|
Hi @pengdurice and @hwchen2017, I've pushed 958fdc9 to address the recent feedback. ReFlow now has separate CPU optimizer builders, with the changes to other accelerator builders reverted. I also fixed the loss scaling issue and the previous CPU CI test failures, and added regression tests. The latest CI checks are currently marked Thanks! |
|
@st-bang97 Also use |
|
Parameters that get no gradient in a step are trained on garbage (blocking). Plain ZeRO-3 now gives such parameters a zero gradient, a fix from #8981. ReFlow replaces the code that applies that fix, so it never runs. Each CPU buffer alternates between holding gradients and holding the newly computed weights. So a skipped parameter gets the previous step's weights applied as its gradient, and its GPU copy stops matching the FP32 master. In my repro the layer's FP32 master was off by 0.014 after one skipped step, and losses diverged one step later. The value the update read was bit-for-bit the layer's own weights. Gradient accumulation has the same problem when a parameter is first used after the first micro-step. MoE experts and conditional branches would hit this. @st-bang97 Please double check on this issue. |
Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
Run Ready and Persist on one CPU worker pool while preserving subgroup dependencies and worker affinity. Let gradient norm checks overlap Ready work and propagate worker failures before committing optimizer state. For unclipped, fixed-scale reentrant checkpoints, stage low-precision weights in the existing CPU buffer until recomputation and overflow checks finish. Publish disjoint shards as their workers complete and wait for the accumulated gradient before running Ready on shared weights. Cover checkpoint recomputation, local gradient edits, invalid steps and asynchronous publication against standard ZeRO-3. Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
Use one optimizer worker per rank by default and share its OpenMP team across ready ranges. Publish each transfer chunk after its writers fence their streaming stores. Keep checkpointed ranges staged until publication is safe and make the H2D chunk size configurable. Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
Gather low-precision shards within each parameter's partition group for the FP32 callback. Write the result to the CPU buffer used by Ready and Persist, refresh gradient norms and overflow, and coordinate callback failures before writing a shard. Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
Share the local squared norm, clipping scale, overflow reduction and parameter restore paths with ZeRO-3. Let NVMe swap-out take the low-precision weights produced by Ready. Simplify Ready submissions and native bindings. Reuse the AVX-512 BF16 conversion for cached and streaming stores. Exercise both parameter and optimizer NVMe offload against standard ZeRO-3. Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
Allocate ReFlow's CPU gradient slots directly and reuse idle reduction bucket storage for accumulation, norms and parameter restoration. Track copies that borrow a bucket and retain their buffers through teardown. Keep the standard ZeRO-3 allocation and copy paths intact. Exercise delayed bucket reuse and pinned master restoration in regression tests. Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
|
Hi @pengdurice, @hwchen2017, and @xylian86, I've pushed several updates addressing the review feedback and improving ReFlow's correctness, compatibility, and performance. The main changes are:
I've also added extensive regression tests covering these changes and various edge cases. For now, I've intentionally kept the tests comprehensive and conservative to catch potential correctness issues. Once the implementation stabilizes, I think we can consolidate some of the tests while preserving the important coverage. Thanks again for the detailed feedback and suggestions! |
Use the shared CPU builder list for Adam and Lion, remove their top-level re-exports, and test lookup through the accelerator. Link the ASPLOS 2027 ReFlow paper in the project and runtime READMEs. Signed-off-by: st-bang97 <st.bang@dgist.ac.kr>
|
I've updated the CUDA builder registration and removed the two redundant top-level re-export files in ef7da9c. |



Implements #8697.
This PR adds ReFlow, an asynchronous CPU-offloaded optimizer path for DeepSpeed ZeRO-3. The motivation and detailed design are described in #8697.
ReFlow reduces the cost of the CPU-offloaded optimizer path and overlaps the remaining host-side work with GPU backward computation.
The ReFlow design and evaluation have been accepted to ASPLOS 2027 as “ReFlow: Exposing Parallelism in CPU-Offloaded LLM Training via Register-Resident SIMD Fusion and Decoupled Update Scheduling.”
Once the paper is publicly available, I will update this PR and #8697 with the paper link and publish a technical blog covering ReFlow's design, usage, and evaluation.
What this PR adds
zero_optimization.reflowconfiguration.Validation
ReFlow has been tested on:
Correctness testing covers Adam/AdamW and Lion, gradient accumulation, gradient clipping, overflow handling, checkpointing, learning-rate scheduling, high-precision optimizer-state access, and single-/multi-GPU execution.
Under deterministic GPU execution, ReFlow produces bit-identical training results to the baseline.
Representative performance results:
On a single A100 at 15B, ReFlow also achieves 1.7× higher throughput than ZeRO-Infinity.
In the standard AdamW evaluation, separate from the clipping-heavy experiment, ReFlow reduces the exposed optimizer update tail by more than 96.8% across the evaluated gradient-accumulation configurations and reduces total iteration time by up to 74.3%.
Under a separate clipping OPT-30B AdamW workload, ReFlow reduces the critical-path host update time from 2440 ms to 850 ms (65.2%), while requiring no FP32 optimizer-state rollback.
ReFlow is also optimizer-agnostic: with Lion, it achieves 1.6×–4.4× higher throughput than ZeRO-Infinity-Lion across 8 to 1 GPUs.
All modified files pass the DeepSpeed pre-commit checks.