Skip to content

[CPU/MLAS] Add QMoE shared 4-bit expert layouts, AVX-512 kernels, and FileExpertStore - #33450

Open
Tianlei Wu (tianleiwu) wants to merge 5 commits into
mainfrom
tlwu/20261019/qmoe-offload-p1-cpu-shared-layout
Open

Tianlei Wu (tianleiwu) wants to merge 5 commits into
mainfrom
tlwu/20261019/qmoe-offload-p1-cpu-shared-layout

Conversation

@tianleiwu

@tianleiwu Tianlei Wu (tianleiwu) commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Description

Part 1 of 3 in the Portable QMoE Offload series adds CPU shared INT4, MXFP4 and NVFP4 expert layouts, scalar/AVX-512
expert kernels, and FileExpertStore with bounded, pin-counted host-slot caching and asynchronous prefetch.
The MLAS build owns the kernels by default, with a standalone/provider-owned compilation path when disabled.

Key changes

  • Shared layouts and reference INT4 packers preserve the prepacked expert bytes without introducing a second weight file.
  • Runtime-dispatched AVX-512 kernels support multi-row reuse, FP16/BF16 boundaries and optional bias handling.
  • File-backed banks are read into bounded cache slots; eligible small heap-deserialized banks receive owned resident backing.
  • INT4 partial ranges fall back to scalar execution when endpoints are not four-aligned.
  • SIMD scale rounding and BF16 narrowing preserve NaNs; scalar FP16 conversion preserves signed zero.
  • Standalone Clang compilation uses scoped target attributes instead of GCC-only target pragmas.
  • Generic POSIX initializer mappings retain writable private copy-on-write behavior. This PR does not change global
    initializer mapping policy; caller-created immutable read-only mappings can still be used as expert banks.

Validation

Commands below were run from the repository root unless noted:

  • .venv/bin/python onnxruntime/test/python/transformers/test_qmoe_shared_layout.py:
    passed, 12 tests passed and 4 optional tests skipped.
  • /usr/local/bin/cmake --build build/pr33505_cpu/Release --target onnxruntime_common --parallel 8:
    passed; recompiles the actual POSIX mapping implementation.
  • /usr/local/bin/cmake --build build/pr33505_cpu/Release --target onnxruntime_mlas --parallel 8:
    passed, including the separate AVX-512 translation unit.
  • Focused C++ executables built from the committed tests:
    GCC and Clang each passed all 7 tests under Intel SDE -skx; the production MLAS-linked executable also
    passed all 7 tests. ORT_QMOE_FP4_GROUP8_MASK=15 passed all 7 tests.
  • Focused platform tests run from the build output directory with
    --gtest_filter='FileIoTest.*:RandomAccessFileTest.*': passed all 14 tests, including writable COW mappings.
  • Native ASan/UBSan focused C++ run: passed, 4 tests passed and 3 SIMD tests explicitly skipped.
  • Touched-file lintrunner: passed. git diff --check: passed.

The local AMD host lacks AVX-512, so SIMD correctness was verified by actual instruction emulation, not native
performance testing. CUDA golden/artifact tests and macOS execution were not run.

Motivation and Context

Cold experts need a portable CPU execution path and bounded host caching while retaining the same quantized storage
used by accelerator execution. Keeping checked geometry, reference execution and SIMD dispatch behind a shared
layout contract allows later offload integration without making immutable bank policy apply to mutable training tensors.

The review follow-up addresses all six applicable GitHub comments, adds regressions for two additional correctness
gaps, and confirms the spin-pool comment is obsolete because that file is no longer part of this PR.

Comment thread onnxruntime/test/python/transformers/qmoe_shared_layout_ref.py Fixed
Comment thread onnxruntime/test/python/transformers/qmoe_shared_layout_ref.py Fixed
Comment thread onnxruntime/test/python/transformers/test_qmoe_shared_layout.py Fixed
@tianleiwu
Tianlei Wu (tianleiwu) force-pushed the tlwu/20261019/qmoe-offload-p1-cpu-shared-layout branch from a0d08bf to 7a92038 Compare October 19, 2026 09:00
@mirounga
mirounga self-requested a review October 19, 2026 13:41
@tianleiwu
Tianlei Wu (tianleiwu) force-pushed the tlwu/20261019/qmoe-offload-p1-cpu-shared-layout branch from 7a92038 to 7e3016c Compare October 20, 2026 09:19
@tianleiwu
Tianlei Wu (tianleiwu) force-pushed the tlwu/20261019/qmoe-offload-p1-cpu-shared-layout branch from 7e3016c to 5f1dc94 Compare October 20, 2026 10:09
@tianleiwu
Tianlei Wu (tianleiwu) added this pull request to stack #33495 October 20, 2026 18:52
@tianleiwu
Tianlei Wu (tianleiwu) force-pushed the tlwu/20261019/qmoe-offload-p1-cpu-shared-layout branch 2 times, most recently from c4535f3 to 3c007ec Compare October 21, 2026 06:32
@tianleiwu
Tianlei Wu (tianleiwu) marked this pull request as ready for review October 21, 2026 18:12
Copilot AI balanced review requested due to automatic review settings October 21, 2026 18:12

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

Memory-safety, mutable-initializer compatibility, numerical correctness, and portability defects remain unresolved.

7 open issues
What changed in this PR

Adds CPU infrastructure for the first stage of portable QMoE offloading.

Changes:

  • Adds shared INT4/FP4 layouts, scalar and AVX-512 kernels, and bounded expert caching.
  • Introduces topology-aware workers and shared read-only POSIX mappings.
  • Integrates MLAS compilation and adds Python reference packers and tests.
File Description
onnxruntime/​test/​python/​transformers/​test_qmoe_shared_layout.py Adds layout, kernel parity, and cache tests.
onnxruntime/​test/​python/​transformers/​qmoe_shared_layout_ref.py Adds INT4 reference packing and computation.
onnxruntime/​core/​platform/​posix/​env.cc Changes little-endian mappings to shared read-only storage.
onnxruntime/​core/​mlas/​lib/​platform.cpp Implements AVX-512 capability query.
onnxruntime/​core/​mlas/​inc/​mlas.h Declares the capability query.
onnxruntime/​contrib_ops/​cuda/​moe/​qmoe_spin_pool.h Adds topology-aware workers and spin controls.
onnxruntime/​contrib_ops/​cpu/​moe/​qmoe_shared_layout.h Declares layouts, stores, and kernel interfaces.
onnxruntime/​contrib_ops/​cpu/​moe/​qmoe_shared_layout.cc Implements layouts, caching, scalar kernels, and dispatch.
onnxruntime/​contrib_ops/​cpu/​moe/​qmoe_shared_layout_avx512.cc Adds vectorized expert kernels and conversions.
cmake/​onnxruntime_providers_cpu.cmake Adjusts ownership of QMoE source compilation.
cmake/​onnxruntime_mlas.cmake Adds offload option and separate AVX-512 compilation.

Review effort: Balanced

Comment thread onnxruntime/contrib_ops/cpu/moe/qmoe_shared_layout.cc Outdated
Comment thread onnxruntime/contrib_ops/cpu/moe/qmoe_shared_layout_avx512.cc Outdated
Comment thread onnxruntime/contrib_ops/cuda/moe/qmoe_spin_pool.h Outdated
Comment thread onnxruntime/core/platform/posix/env.cc Outdated
Comment thread onnxruntime/contrib_ops/cpu/moe/qmoe_shared_layout_avx512.cc Outdated
Comment thread onnxruntime/contrib_ops/cpu/moe/qmoe_shared_layout_avx512.cc
Comment thread onnxruntime/test/python/transformers/test_qmoe_shared_layout.py

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.

Comment thread onnxruntime/contrib_ops/cpu/moe/qmoe_shared_layout.cc
Comment thread onnxruntime/contrib_ops/cpu/moe/qmoe_shared_layout.cc Outdated
Comment thread onnxruntime/contrib_ops/cpu/moe/qmoe_shared_layout.cc Outdated
Comment thread onnxruntime/contrib_ops/cpu/moe/qmoe_shared_layout.cc Outdated
Comment thread onnxruntime/contrib_ops/cpu/moe/qmoe_shared_layout.cc Outdated
Comment thread onnxruntime/contrib_ops/cpu/moe/qmoe_shared_layout_avx512.cc Outdated
Comment thread onnxruntime/test/python/transformers/test_qmoe_shared_layout.py Fixed

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.

Comment thread onnxruntime/contrib_ops/cpu/moe/moe_quantization_cpu.cc
Comment on lines +414 to +416
alignas(64) static __m512 fallback_lut[256];
PopulateScaleFp4Lut2D(base_lut, global_scale, round_scale_bf16, fallback_lut);
return fallback_lut;

outs = []
for model in (m_raw, m_pk):
sess = ort.InferenceSession(model, providers=["CUDAExecutionProvider"])
… FileExpertStore

Add SharedExpertBlockV1/V2/V3 4-bit expert layouts (MXFP4/INT4/NVFP4), AVX-512/AVX-512-FP16/AVX2 CPU expert GEMV kernels, MLAS FP16 dispatch, FileExpertStore with bounded host-slot caching over MAP_SHARED/MAP_PRIVATE/heap-scale banks, and read-only MAP_SHARED external initializer mapping on little-endian POSIX.

Address review feedback:
- Fix scalar HalfToFloat subnormal exponent (127 - 14 - e) and add exhaustive scalar/SIMD FP16 bit-pattern tests.
- Track regular-file vs heap backing in FileExpertStore, retain independent resident backing for accepted heap banks, and add IsFileBackedReadOnlyMapping / EvictFileBackedHostPages helpers with heap eviction safety tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Guard partial INT4 SIMD ranges, preserve NaNs and signed zero, fix Clang target scoping, and validate inferred storage offsets. Restore writable private POSIX mappings for mutable initializers and add focused kernel/cache/mapping regressions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…oE support

- Reject null or zero-byte buffers in TensorExpertStore::Initialize before range validation.
- Exclude private mappings (r--p) from IsFileBackedReadOnlyMapping and automatic /proc/self/maps file reloading so modified COW mappings are snapshotted or rejected rather than discarded.
- Remove the 4096-byte cap in ComputePointerAlignment so alignments > 4096 work in MakeExpertBlockLease, TensorExpertStore, and FileExpertStore.
- Retain an open Win32 HANDLE (FILE_SHARE_READ|WRITE|DELETE) across FileExpertStore lifetime and read with positional OVERLAPPED ReadFile.
- Coalesce pending Prefetch requests by expert ID, refresh generations, and bound the pending queue to pre-reserved cache_slots.
- Move the 16 KiB FP4 2D LUT cache entries out of inline TLS into a small per-thread handle with lazily allocated ways.
- Gate onnxruntime_ENABLE_MLAS_QMOE_OFFLOAD off when onnxruntime_DISABLE_CONTRIB_OPS or minimal builds are enabled.
- Add canonical INT4 QMoE view/kernels (row-major and block-major scales, SM80 prepacked and raw row-major weights, FP16/BF16) and support weights_prepacked=1 INT4 QMoE in QMoECPU.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@tianleiwu
Tianlei Wu (tianleiwu) force-pushed the tlwu/20261019/qmoe-offload-p1-cpu-shared-layout branch from dd0da0f to ea94e4b Compare October 24, 2026 03:59

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants