Skip to content

perf(qwen3-14b prefill): reduce task fragmentation with SPMD kernels#662

Merged
zhangqi-chen merged 7 commits into
hw-native-sys:mainfrom
Leaf-Salix:codex/prefill-tuning
Jul 2, 2026
Merged

perf(qwen3-14b prefill): reduce task fragmentation with SPMD kernels#662
zhangqi-chen merged 7 commits into
hw-native-sys:mainfrom
Leaf-Salix:codex/prefill-tuning

Conversation

@Leaf-Salix

@Leaf-Salix Leaf-Salix commented Jul 1, 2026

Copy link
Copy Markdown

Summary

  • Optimize Qwen3-14B prefill scheduling in models/qwen3/14b/prefill_fwd.py, reducing excessive fine-grained task dispatch while keeping the model interface and runner inputs unchanged.
  • Rework the prefill layer task layout around RoPE/KV-cache, attention, projection, RMSNorm, MLP, and residual phases so the generated orchestration uses fewer, larger, and better-balanced tasks than the original fragmented task graph.
  • Fix Q RoPE correctness in the optimized path so the optimized prefill remains numerically aligned with the original Qwen3-14B implementation.
  • Golden PASS on a2a3 for the Qwen3-14B prefill validation path, including batch=1 max_seq=128.
Scenario Metric Original / Revert Optimized Reduction Speedup / Ratio
40-layer batch=1 max_seq=128 L2 swimlane, 3 runs Mean task count 351050 31706 91.0% 11.1x fewer
40-layer batch=1 max_seq=128 L2 swimlane, 3 runs Mean Total Test Time 572.544 ms 77.385 ms 86.5% 7.40x faster
Real-weight serving replay swimlane, 5 runs Mean AICore task count 333562 36434 89.1% 9.16x fewer
Real-weight serving replay swimlane, 5 runs Mean prefill device span 579.371 ms 76.967 ms 86.7% 7.53x faster
  • A rerun with the current PR head (743ce75) in the real-weight serving replay path produced the same top token (" These") and improved the prefill L2 device span to ~77.0 ms.

Notes

  • This PR is focused on Qwen3-14B prefill task scheduling and correctness. It does not require pypto runtime changes.
  • The timing numbers are L2 swimlane/device-side measurements. End-to-end serving wall time includes additional L3/runtime/dispatch/drain overhead and should be interpreted separately from the prefill kernel device span.
  • The latest real-weight serving replay run produced the L2 records and logits successfully; the task cleanup path later reported an asc_dumper shared-library cleanup error, so only the emitted L2 device span and logits summary are used as evidence.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request refactors the Qwen3-14B prefill forward pass in prefill_fwd.py to utilize SPMD parallelization, pipelining, and micro-windowing optimizations across various stages, including RMSNorm, projections, attention, and MLP. Feedback on these changes highlights a critical correctness bug in _attention_micro_window where sequence lengths exceeding 128 tokens cause out-of-bounds indexing and bypass the necessary online softmax reduction. Additionally, a performance optimization is suggested to avoid peeling the first iteration of pl.pipeline loops, which would allow better overlapping of memory loads on CANN/Ascend hardware.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread models/qwen3/14b/prefill_fwd.py Outdated
Comment thread models/qwen3/14b/prefill_fwd.py
@Leaf-Salix Leaf-Salix marked this pull request as draft July 1, 2026 08:56
@coderabbitai

coderabbitai Bot commented Jul 1, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 79443f44-f0dc-412c-ad7b-d2af98196a60

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The Qwen3-14B prefill forward kernel is rewritten to replace the prior monolithic causal attention loop with an SPMD-tiled implementation. Q/K/V RMSNorm and projections now use chunked matmuls with FP32 accumulators, and attention is computed via micro-window scoring plus staged finalize across multiple finalize cores, feeding into the existing output/MLP/residual writeback path.

Changes

Prefill kernel rewrite

Layer / File(s) Summary
Tiling constants and micro-window helpers
models/qwen3/14b/prefill_fwd.py
New tiling/grouping constants and _attention_micro_window/_finalize_attention_micro_window helper kernels are added, plus a minor CI marker comment fix.
SPMD RMSNorm and Q/K/V projections
models/qwen3/14b/prefill_fwd.py
Stage 1 of prefill_layer switches to pl.spmd loops with chunked matmuls and FP32 accumulators for RMSNorm and Q/K/V projection, followed by per-head Q/K RMSNorm on the new tile layout.
RoPE, KV cache, and staged attention finalize
models/qwen3/14b/prefill_fwd.py
Scope 2 builds a padded Q tile after RoPE, updates the KV cache with RoPE, computes attention via repeated _attention_micro_window calls, and finalizes across multiple finalize cores into attn_tile.
Output projection, MLP, and residual writeback
models/qwen3/14b/prefill_fwd.py
Scope 3 output projection, post-attention RMSNorm, SwiGLU MLP, and down projection with residual are refactored to consume the new attention tile layout with updated valid_shape masking.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant PrefillLayer
  participant RoPECacheUpdate
  participant AttentionMicroWindow
  participant FinalizeMicroWindow

  PrefillLayer->>RoPECacheUpdate: Apply RoPE, build padded Q tile, update KV cache
  RoPECacheUpdate->>AttentionMicroWindow: Compute per-window raw scores, softmax, SV scratch
  AttentionMicroWindow->>FinalizeMicroWindow: Pass scratch tensors per finalize core
  FinalizeMicroWindow->>PrefillLayer: Return finalized attn_tile
  PrefillLayer->>PrefillLayer: Output projection, MLP, residual writeback
Loading

Possibly related PRs

  • hw-native-sys/pypto-lib#143: Ports/rewrites the same Scope 2 attention and Scope 3 output/MLP tiling patterns in the same prefill_fwd.py file.
  • hw-native-sys/pypto-lib#161: Refactors the same Scope 2 attention softmax accumulation and finalize/ctx writeback structure.
  • hw-native-sys/pypto-lib#447: Refactors Q/K/V RoPE computation from looping into SPMD/tiled execution, analogous to this PR's projection/RoPE tiling changes.

Suggested labels: enhancement

Poem

A rabbit hops through tiles so neat,
Micro-windows dance to softmax beat,
RoPE spins fast, the cache renews,
Finalize cores compute the news,
Scope by scope, the kernel's fleet! 🐇✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the main change: a performance-focused Qwen3-14B prefill rewrite using SPMD kernels to reduce task fragmentation.
Description check ✅ Passed The description matches the changes to Qwen3-14B prefill scheduling, attention tiling, and RoPE/KV-cache correctness.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
models/qwen3/14b/prefill_fwd.py (1)

87-88: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Guard the manually unrolled micro-window coverage.

The 10 hard-coded calls cover at most 10 * ATTN_TOK_GROUP relative tokens. Since FINALIZE_TOK_GROUP follows TOK_TILE, a future tile retune can silently leave tail tokens uncomputed.

Proposed guard
 FINALIZE_SPMD_BLOCKS = 48
 FINALIZE_TOK_GROUP = TOK_TILE
 ATTN_MICRO_WORK_ITEMS = ATTN_TOK_GROUP * TOTAL_Q_GROUPS
+ATTN_MICRO_WINDOWS = 10
+assert FINALIZE_TOK_GROUP <= ATTN_MICRO_WINDOWS * ATTN_TOK_GROUP

Also applies to: 620-868, 870-965

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@models/qwen3/14b/prefill_fwd.py` around lines 87 - 88, The manually unrolled
micro-window calls in prefill_fwd.py only cover a fixed range, so a future
change to FINALIZE_TOK_GROUP/TOK_TILE can leave tail tokens unprocessed. Update
the logic around the unrolled attention work in the prefill path to add an
explicit guard that checks the covered range against the total tokens needed and
falls back to a safe loop or assertion when the fixed 10-call span is
insufficient. Use the existing symbols FINALIZE_TOK_GROUP,
ATTN_MICRO_WORK_ITEMS, and the unrolled prefill/attention block to locate and
protect all affected call sites.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@models/qwen3/14b/prefill_fwd.py`:
- Around line 140-147: The attention finalize path in prefill_fwd.py is losing
sequence-block state because the scratch tensors are indexed only by (gi, token,
q_head) while the finalizer reads one row per (gi, token), so blocks alias each
other when ctx_blocks > 1. Update the online softmax flow in the affected
helpers to preserve O_i/L_i across all sb blocks, either by adding an explicit
sequence-block scratch/reduction dimension or by accumulating and reducing the
state before the final divide. Make the fix consistently in the referenced
attention finalize sections so the row layout matches the way sb is encoded in
the offsets.

---

Nitpick comments:
In `@models/qwen3/14b/prefill_fwd.py`:
- Around line 87-88: The manually unrolled micro-window calls in prefill_fwd.py
only cover a fixed range, so a future change to FINALIZE_TOK_GROUP/TOK_TILE can
leave tail tokens unprocessed. Update the logic around the unrolled attention
work in the prefill path to add an explicit guard that checks the covered range
against the total tokens needed and falls back to a safe loop or assertion when
the fixed 10-call span is insufficient. Use the existing symbols
FINALIZE_TOK_GROUP, ATTN_MICRO_WORK_ITEMS, and the unrolled prefill/attention
block to locate and protect all affected call sites.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c1b6e97a-2e0f-43bd-b577-8375d667662e

📥 Commits

Reviewing files that changed from the base of the PR and between 57772f3 and 4f31d09.

📒 Files selected for processing (1)
  • models/qwen3/14b/prefill_fwd.py

Comment thread models/qwen3/14b/prefill_fwd.py Outdated
@Leaf-Salix Leaf-Salix changed the title perf(qwen3-14b prefill): reduce task fragmentation in prefill scheduling perf(qwen3-14b prefill): reduce task fragmentation with SPMD kernels Jul 2, 2026
@Leaf-Salix Leaf-Salix force-pushed the codex/prefill-tuning branch from 813c579 to 4cb0156 Compare July 2, 2026 11:48
@Leaf-Salix Leaf-Salix marked this pull request as ready for review July 2, 2026 11:55
@zhangqi-chen zhangqi-chen merged commit 6783429 into hw-native-sys:main Jul 2, 2026
6 of 7 checks passed
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.

2 participants