DeepSpeed
c102bbd1 - [Phase 2] Add NEON SIMD path for CPU Adam on AArch64 (#8453)

Commit
71 days ago
[Phase 2] Add NEON SIMD path for CPU Adam on AArch64 (#8453) ## Summary Phase 2 of Apple Silicon support (follow-up to #8293/#8300/#8335): the CPU Adam kernel — the ZeRO-Offload optimizer path — ran scalar on AArch64 machines without SVE, which includes every Apple Silicon Mac. This adds a 4-lane NEON implementation of the existing SIMD macro layer. ### Changes - **`csrc/includes/simd.h`** — a `__NEON__` branch defining the full macro set (`SIMD_LOAD/STORE/SET/ADD/MUL/FMA/SQRT/DIV/AND/ANDNOT/OR/XOR`, width 4): - fp16 via the hardware converters (`vcvt_f32_f16` / `vcvt_f16_f32`). - bf16 via the same round-to-nearest-even + NaN-quieting flow as the AVX512 `store_16_f32_as_bf16_nearest` (using `vaddhn_u32` for the add-and-take-high-half step); loads are widen+shift. - x86 `andnot(x, y) = ~x & y` maps to `vbicq(y, x)` — operand order preserved (documented in a comment). - The bf16 `simd_load`/`simd_store` guards widen from AVX512-only to AVX512-or-NEON. - **`csrc/includes/cpu_adam.h`** — `Step_AVX`'s non-AVX512 bf16 bailout is lifted for NEON (this was silently sending bf16 back to the scalar tail); the two Adam gates widen to include `__NEON__`. - **`csrc/adam/cpu_adam_impl.cpp`** — same gate widening (4 sites, including `kZenAdamAlign`). The NEON branch sits before the existing `__SVE__` alternative and they remain mutually exclusive builder-emitted defines. - **`op_builder/builder.py`** — `simd_width()` advertises `-D__NEON__` for `ARM_8` without SVE. 32-bit ARM keeps `__SCALAR__`: the `vdivq_f32`/`vsqrtq_f32` intrinsics used are A64-only. - **`op_builder/mps/cpu_adam.py`** — switches from `-D__SCALAR__` to `-D__NEON__`. - Lion/Adagrad/AIO gates are untouched and keep their current scalar behavior on ARM (candidate follow-ups). ### Measured on Apple M5 Max (macOS 26.3, Apple clang, Homebrew libomp) `DeepSpeedCPUAdam` step, 50M params, 10-step average, vs the `-D__SCALAR__` build of the same tree: | dtype | scalar | NEON | speedup | |---|---|---|---| | fp32 | 11.5 ms | 3.8 ms | 3.0× | | fp16 | 11.5 ms | 3.3 ms | 3.5× | | bf16 | 12.7 ms | 4.9 ms | 2.6× | ### Correctness - NEON and scalar builds produce **bit-identical** fp16 results on identical inputs (5 steps, 1M params). - All dtypes (fp32/fp16/bf16 params; fp32 and bf16 moments) match an fp32 `torch.optim.Adam/AdamW` oracle within storage rounding, at sizes exercising pure-SIMD, SIMD+scalar-tail (1000003), and sub-width (3) paths. - Existing suites on the M5 Max: `test_cpu_adam.py` + `test_hybrid_adam.py` + `test_adamw.py` — 122 passed, 7 skipped. The `mps-torch-latest` CI workflow JIT-builds this kernel in its offload configs, so the NEON path is exercised upstream on every touching PR. --------- Signed-off-by: PKUWZP <zhipeng.rainbowserie@gmail.com>
Author
Parents
Loading