feat(ascend): Qwen-Image qk_rmsnorm & multi_axis_rope kernels (issue #386) - #411
Open
erfgss wants to merge 2 commits into
Open
feat(ascend): Qwen-Image qk_rmsnorm & multi_axis_rope kernels (issue #386)#411erfgss wants to merge 2 commits into
erfgss wants to merge 2 commits into
Conversation
…L-Align#386) Qwen-Image WS1 single-GPU kernels, NPU (Ascend C) portion: - csrc/ascend/Qwen-Image/qk_rmsnorm_ascend.asc: per-head parameter-free QK RMSNorm (head_dim=128). Host computes rstd with the exact torch reference ops; the kernel only performs order-free fp32 elementwise multiplies plus one round-to-nearest-even cast, so the fused output is bitwise identical to the PyTorch reference and batch-invariant. - csrc/ascend/Qwen-Image/multi_axis_rope_ascend.asc: multi-axis RoPE (axes [16, 56, 56], text on the grid diagonal). The axis split and diagonal placement live in the fp32 cos/sin tables built host-side; the kernel is the dtype-generic rotate-half primitive with forward (sin_sign=+1) and backward (transpose, sin_sign=-1) entry points. - npu_module.cpp: register qk_rmsnorm_ascend, multi_axis_rope_ascend_forward/backward. - PyTorch references (NativeQkRmsNormOp, NativeMultiAxisRopeOp) with qwen_image_positions / build_multi_axis_cos_sin table builders. - Ascend autograd wrappers (QkRmsNormAscendOp, MultiAxisRopeAscendOp) with clean fallback to the native references. - registry: OpBackend entries + npu priority [ASCEND, PYTORCH_NATIVE] for op types qk_rmsnorm / multi_axis_rope; native-only candidates on cpu/cuda/rocm/musa. - tests/test_qwen_image_ops.py: CPU tests (bitwise reference match, per-head independence, batch invariance, explicit-VJP vs autograd, issue shapes 1024^2 / 1328^2 / 1664x928) and NPU tests (bitwise fwd, on-device batch invariance, backward tolerance) gated on the compiled Ascend kernels. CPU: 22 passed, 15 skipped (no NPU on the dev host).
erfgss
requested review from
Flink-ddd,
KJLdefeated,
bitborne,
inaniloquentee and
maxiaosong1124
as code owners
September 13, 2026 02:52
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Contributes the
qk_rmsnormandmulti_axis_roperows of the Qwen-Image WS1 kernel table (issue #386, NPU/Ascend C + PyTorch reference + registry wiring).What
Ascend C kernels (
csrc/ascend/Qwen-Image/)qk_rmsnorm_ascend.asc— per-head parameter-free QK RMSNorm (head_dim=128). Following the establishedrmsnorm_ascendcontract, the host computesrstdwith the exact torch reference ops (fp32 mean of squares +torch.rsqrt); the kernel performs only order-free fp32 elementwise multiplies and one round-to-nearest-even cast, so the fused output is bitwise identical to the reference and batch-invariant (fixed tile, one row per block, chunk coalescing for narrow rows).multi_axis_rope_ascend.asc— multi-axis RoPE (axes[16, 56, 56], text on the grid diagonal). The axis split / per-axis frequencies / diagonal text placement live in the fp32 cos/sin tables built host-side; the kernel is the dtype-generic rotate-half primitive with forward (sin_sign=+1) and backward (transposeR^T,sin_sign=-1) entry points sharing one code path.npu_module.cpp: registersqk_rmsnorm_ascend,multi_axis_rope_ascend_forward,multi_axis_rope_ascend_backward.Python ops & registry
NativeQkRmsNormOp,NativeMultiAxisRopeOp(+qwen_image_positionsdiagonal/grid position builder andbuild_multi_axis_cos_sintable builder).QkRmsNormAscendOp,MultiAxisRopeAscendOpwith clean fallback to the native references for non-NPU / unsupported inputs.registry.py:OpBackendentriesASCEND_QK_RMS_NORM/ASCEND_MULTI_AXIS_ROPE(+ native fallbacks); npu priority[ASCEND, PYTORCH_NATIVE]for op typesqk_rmsnorm/multi_axis_rope; native-only candidates on cpu/cuda/rocm/musa (previously these op types would have resolved to the generic logp backend there).Testing (
tests/test_qwen_image_ops.py)CPU (22 passed on the dev host):
{1024², 1328², 1664×928}(patchified grids 64×64 / 83×83 / 104×58) end to endNPU (15 tests, auto-activated with
skipifwhen the kernels are compiled):Run:
python -m pytest tests/test_qwen_image_ops.py -v(NPU cases requireKERNEL_ALIGN_FORCE_ASCEND=1 pip install --no-build-isolation -e .).Issue acceptance checklist
{1024², 1328², 1664×928}