Skip to content

Release the GIL during native CPU Adam updates - #9035

Open
Gxj230958 wants to merge 8 commits into
deepspeedai:masterfrom
Gxj230958:perf/cpu-adam-release-gil-20261108
Open

Gxj230958 wants to merge 8 commits into
deepspeedai:masterfrom
Gxj230958:perf/cpu-adam-release-gil-20261108

Conversation

@Gxj230958

@Gxj230958 Gxj230958 commented Nov 8, 2026 •

Copy link
Copy Markdown

Release the Python GIL during native CPU Adam updates so independent CPU workers
can update concurrently and the training thread can submit gradient downloads
and updated-weight uploads. The optimizer registry lookup and step-state update
retain the GIL; each kernel invocation uses its own copy of the scalar optimizer
configuration.

CPU FusedAdam shares that configuration across parameter groups, so the change
covers its caller as well as DeepSpeedCPUAdam. The added concurrent-update test
runs two independent optimizers with different hyperparameters and compares
parameters and both moments against PyTorch AdamW over four steps. The separate
ZenFlow update path retains its existing behavior.

test_cpu_adam_native_update_releases_gil independently verifies Python-thread
progress during the native call for both CPUAdam and CPU FusedAdam. The barrier
test checks numerical correctness; the native-call test checks GIL release.
Removing only gil_scoped_release fails both native-call cases while both
barrier-based numerical cases pass.

The open CPUAdam/GIL pull-request and issue searches on November 8, 2026 found
no existing change implementing this native-update GIL release.

Complete training measurements

Public pretrained Qwen3-0.6B trains with BF16 parameters and FP32 CPU AdamW
master weights and moments. A bounded four-worker pipeline uses 8 MiB buckets,
eight OpenMP threads per worker and affinity to 48 physical cores on the NPU's
local NUMA node. Both variants use the same native compiler flags, sequence
length 256, microbatch one, two-way accumulation and global clipping at 1.0.
The public document inputs are separate from calibration inputs.

Each entry is the median of six complete training steps after two warmup steps
in a fresh process. Variant order alternates across three paired seeds. Loading,
optimizer construction and compilation precede timing.

Seed Upstream step GIL-release step Speedup NPU peak, both
700 729.22 ms 636.85 ms 1.145× 3.764 GiB
701 720.79 ms 626.29 ms 1.151× 3.764 GiB
702 723.13 ms 625.30 ms 1.156× 3.764 GiB

The median paired speedup is 1.151×. Peak process CPU RSS is at most
8.577 GiB for either variant. All paired gradient norms match exactly, and the
maximum same-input loss difference across the 24 warmup/measured step pairs is
4.77e-7. These throughput measurements exercise concurrent CPUAdam through
the bucket pipeline; the ZeRO engine comparison below validates updates.

Validation

Hardware: two Intel Xeon Platinum 8468 sockets and one Ascend 910B2C, with
driver 25.0.rc1.1, CANN 8.5.1, PyTorch 2.9.0 and torch-npu
2.9.0.post1+gitdc51c2d. Submission validation uses DeepSpeed
0.19.10+02f7978, based on master revision
02f7978481cb3d3297f111c02b16d768b890b256. The paired training measurements
use revision 1a956270f42fbe7ffa485fdff45499eb77318469; the native CPU Adam
sources are unchanged between these revisions.

  • 97 focused CPU tests pass on master 5dc44e8453df8d02139c2a761aec4d2f4983b451
    plus this change, including upstream gradient scaling and BF16 parameter-copy
    coverage: strict Adam/AdamW state updates, concurrent
    CPUAdam/CPU FusedAdam updates, native-update GIL release, unsupported AMSGrad
    rejection and preservation of optimizer-state dtypes across checkpoint restoration.
  • The native-update regression checks that another Python thread progresses
    during CPUAdam and CPU FusedAdam native calls. Arguments and tensors are
    prepared before observation, and a longer interpreter switch interval excludes
    bytecode scheduling around the call. Observation uses at most 20 calls and a
    five-second deadline checked between calls, with bounded thread waits.
  • Negative control: compiling both extensions with only
    pybind11::gil_scoped_release release; removed produces two expected
    thread-progress assertion failures; both concurrent numerical tests still pass.
  • Changed-file pre-commit checks pass, including clang-format 18.1.3.
  • Ascend FP32/BF16 reference comparisons pass four consecutive steps with
    three-way gradient accumulation, global clipping, pipeline depths one and
    three, and checkpoint restore. Maximum tested FP32 weight error: 4.47e-8.
  • Actual single-rank ZeRO stage 2 with BF16 parameters and CPU optimizer offload
    passes four accumulated updates. Maximum reference weight difference:
    0.00048828125.

The focused tests can be selected in a CPU DeepSpeed installation with:

PYTHONPATH="$PWD/tests" DS_ACCELERATOR=cpu OMP_NUM_THREADS=2 MAX_JOBS=1 pytest \
  --confcutdir=tests/unit/ops/adam \
  tests/unit/ops/adam/test_cpu_adam.py \
  -k 'not TestCPUAdam and not lion and not adagrad' -q
pre-commit run --files csrc/adam/cpu_adam_impl.cpp \
  tests/unit/ops/adam/test_cpu_adam.py

The CI selector requests the complete tests/unit/v1 scope for the C++ change.
The CPU workflow also runs tests/unit, which includes the added regression.

Limitations

Device integration covers single-rank Ascend BF16 CPU offload and the FP32/BF16
optimizer reference cases above. ZeRO stages 1/3, multi-rank execution, FP16,
CUDA, XPU and other accelerators were not measured. Short training comparisons
establish update agreement and execution cost; they do not establish downstream
convergence.

Signed-off-by: Xingjian Guo <222823329+Gxj230958@users.noreply.github.com>
@Gxj230958
Gxj230958 marked this pull request as ready for review November 8, 2026 12:13
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-08T12:18:01.766390Z 790e42b Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The concurrency test still passes when gil_scoped_release is removed, leaving the central GIL-release behavior without regression coverage.

1 open issue
What changed in this PR

This PR releases the Python GIL during native CPU Adam updates, allowing independent optimizer workers and the training thread to make progress concurrently.

Changes:

  • Keeps registry lookup and step-state updates under the GIL, then copies optimizer configuration before releasing it for kernel execution.
  • Adds concurrent CPUAdam and CPU FusedAdam tests comparing parameters and both moments against PyTorch AdamW over four steps.
File Description
tests/​unit/​ops/​adam/​test_cpu_adam.py Adds concurrent optimizer correctness checks against AdamW.
csrc/​adam/​cpu_adam_impl.cpp Copies per-call optimizer state and releases the GIL during native updates.

Review effort: Balanced

Comment on lines +146 to +147
barrier.wait(timeout=30)
optimizer.step()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Gxj230958 please address this

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have fixed the conflict.

Signed-off-by: Xingjian Guo <222823329+Gxj230958@users.noreply.github.com>
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added the skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. label Nov 14, 2026
@sfc-gh-truwase

Copy link
Copy Markdown
Collaborator

@Gxj230958 please resolve conflicts. Thanks

Signed-off-by: Xingjian Guo <222823329+Gxj230958@users.noreply.github.com>
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. and removed skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. labels Nov 15, 2026
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. and removed skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. labels Nov 16, 2026
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. and removed skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. labels Nov 20, 2026
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. and removed skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. labels Nov 23, 2026
@Gxj230958

Copy link
Copy Markdown
Author

@sfc-gh-truwase @tjruwase @tohtana @loadams All conflicts resolved. Could you review and approve the CI workflows? Thanks.

@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. and removed skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. labels Nov 23, 2026
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot removed the skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. label Nov 23, 2026
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added the skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. label Nov 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants