Skip to content

[HIP] Fix the hardcoded die count in the HIP kernels - #6877

Open
RElbers wants to merge 5 commits into
mainfrom
relbers/gfx950_hip_xcd_count
Open

RElbers wants to merge 5 commits into
mainfrom
relbers/gfx950_hip_xcd_count

Conversation

@RElbers

@RElbers RElbers commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Takes the die and CU counts from the device in the HIP kernels that assumed a full-size gfx950, and adds the csrc helper for the die count, since none existed. On MI350P this makes Mega-MHC 2.6x faster on geomean from 1 to 1024 tokens, and leaves every other timing unchanged.

Last of four, one per backend: FlyDSL (#5504), Triton (#5505), opus (#5848), this one.

Motivation

MI350P is a gfx950 with 128 CUs on 4 dies. Several HIP paths key on the target alone and assume MI355X's 256 CUs on 8 dies:

  • MLA v1.2 metadata: num_xcd = 8 on every gfx950, so the work is dealt over dies the part does not have.
  • IQ2R GLM-5.3 MoE: the tile remap is a compile-time 8 (glm53_remap8 and the XCD template argument).
  • Mega-MHC: below 256 CUs the distributed variants are refused, and the fallback runs the generic kernel at split 1.
  • topk sampled: CU_COUNT = 256 sizes the occupancy, wide-select and phase_b batching decisions.

Technical Details

  • get_num_xcds_func reads hipDeviceAttributeNumberOfXccs, per device behind SynchronizedCache like get_warp_size_func and get_smem_size_func, and fails if the answer is not at least one die. topk_oneblock_read_device already reads the attribute the same way.
  • IQ2R: the die count stays a template argument; the launchers pick the 4- or 8-die instance from the device. glm53_remap8 becomes glm53_remap_xcd<XCD>.
  • Mega-MHC: the fallback keeps the CU-aware generic split. The tuned variants are sized for 256 CUs and none fits 128, so their guard is unchanged. The same fallback serves a CU-masked run on a 256-CU part, which also moves from split 1 to the CU-aware split.
  • topk sampled: the shape plan takes the device's CU count. topk_sampled_workspace_size gains device_id, and both Python caches are keyed by device.

Testing

On MI350P (gfx950, 128 CUs, 4 dies), at 383ab81, all pass:

Test Result
test_iq2r_glm53.py -k "not perf" 90 passed, 10 skipped
test_mega_mhc.py --check-only 30 of 30 shapes
test_topk_per_row.py --prefill_backend aiter sampled all checks and shapes pass
test_mla_persistent.py -b 32 -c 4096 50 passed
test_mla_decode_update_metadata.py 46 passed, 28 skipped, 6 xfailed

Timed against the merge base, run base, head, base on one GPU; base is the mean of the two base runs. Mega-MHC is 2.6x faster on geomean from 1 to 1024 tokens (3.0x post, 2.3x no_post) and unchanged from 2048 tokens.

Mega-MHC on MI350P (HIP, H=5120, µs)
Tokens post: base post: head post: speedup no_post: base no_post: head no_post: speedup
1 122.3 26.3 4.65x 83.0 26.2 3.17x
4 124.2 28.2 4.40x 84.8 28.0 3.03x
8 126.5 30.4 4.16x 86.8 30.4 2.86x
16 133.1 38.4 3.47x 91.9 36.5 2.52x
32 132.2 37.4 3.53x 91.1 35.1 2.60x
64 131.8 37.0 3.56x 90.6 34.5 2.62x
128 132.5 45.6 2.91x 89.4 41.2 2.17x
256 134.3 55.6 2.42x 89.7 46.5 1.93x
512 137.3 75.6 1.82x 90.4 58.2 1.55x
1024 143.2 112.5 1.27x 93.5 82.4 1.13x
2048 162.1 160.6 1.01x 105.3 105.8 1.00x
4096 187.8 187.8 1.00x 121.0 121.1 1.00x
16384 733.9 732.1 1.00x 402.1 400.1 1.00x

IQ2R, topk and MLA keep their timings: median speedup 1.00x for each, every shape within 2.5%.

A CU-masked run on MI355X (HSA_CU_MASK=0:0-127, 128 of 256 CUs visible) takes the same fallback, but sizes its split by the device's 256 CUs, not the 128 the mask leaves. Mega-MHC, timed the same way; it is up to 4.14x faster up to 1024 tokens, 14% slower at 2048 tokens (post) and unchanged above.

Mega-MHC on MI355X with half the CUs masked (HIP, H=5120, µs)
Tokens post: base post: head post: speedup no_post: base no_post: head no_post: speedup
1 99.4 24.0 4.14x 66.3 23.0 2.88x
4 105.8 26.5 3.99x 67.5 24.2 2.79x
8 113.5 29.1 3.90x 69.4 25.5 2.71x
16 129.6 40.3 3.22x 80.5 33.3 2.42x
32 129.3 39.4 3.28x 91.1 35.8 2.54x
64 129.3 39.4 3.28x 92.1 35.5 2.59x
128 129.2 41.7 3.10x 92.6 36.1 2.57x
256 129.4 55.1 2.35x 91.3 45.6 2.00x
512 131.9 71.4 1.85x 91.0 52.3 1.74x
1024 136.0 105.0 1.30x 92.5 68.9 1.34x
2048 143.5 166.0 0.86x 96.6 100.7 0.96x
4096 157.7 157.7 1.00x 109.5 109.4 1.00x
4160 195.6 195.7 1.00x 120.0 120.1 1.00x
8192 323.9 323.2 1.00x 163.1 163.3 1.00x
16384 628.8 629.5 1.00x 332.5 332.9 1.00x

At 2048 tokens the split of 2 gives 256 workgroups for 128 visible CUs, which run in two waves where split 1 ran in one. Sizing the split by the mask would mean parsing HSA_CU_MASK and ROC_GLOBAL_CU_MASK; main's generic path already sizes by the device's CUs under a mask, and this PR leaves that as is.

On MI355X, at 383ab81: test_iq2r_glm53.py -k "not perf" (90 passed), test_mla_persistent.py -b 32 -c 4096 and test_topk_per_row.py pass, and the die count reads 8. On 256-CU parts without a CU mask every kernel runs as before.

PR Self-Review

Reviewed revision

c3c1f92

Behavior and intent

On MI350P the MLA metadata and IQ2R remaps use 4 dies instead of 8, Mega-MHC falls back to a split generic kernel instead of split 1, and topk sampled plans for 128 CUs. The die count comes from HIP rather than a table of parts. MI355X and MI350X keep their current plans.

Validation and unverified scope

On MI350P every test listed under Testing passed, and Mega-MHC is 2.6x faster on geomean from 1 to 1024 tokens, up to 4.65x, with no shape slower; IQ2R, topk and MLA timings are unchanged. On MI355X the same tests pass and the die count reads 8.

A CU-masked run on MI355X is slower only at 2048 tokens (0.86x post, 0.96x no_post), since its split is sized for CUs the mask hides; below that it is faster, and above it unchanged.

Unverified: builds against ROCm older than 7.0.

PR-specific question

The MLA, IQ2R and topk changes apply to gfx950 only, and take their die or CU count from the device the kernel runs on, so one build serves both gfx950 parts. The Mega-MHC change applies only when its distributed variants are refused: below 256 CUs, or under a CU mask.

Changed op tests

op_tests/test_topk_per_row.py: passes the device to _sampled_workspace_size_cached. No test is added.

@aiter-bot

aiter-bot commented Oct 29, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision 6a519496c711 · mode shadow

Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision.

Questions to answer in the PR description

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

@RElbers
RElbers marked this pull request as ready for review October 29, 2026 22:48
@RElbers
RElbers requested a review from a team October 29, 2026 22:48
@github-actions github-actions Bot changed the title Fix the hardcoded die count in the HIP kernels [HIP] Fix the hardcoded die count in the HIP kernels Oct 29, 2026
@github-actions github-actions Bot added the HIP label Oct 29, 2026
@zufayu
zufayu requested review from amd-ruitang3 and zufayu October 30, 2026 01:29
Copilot AI balanced review requested due to automatic review settings November 2, 2026 10:13

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.

🔵 Needs a closer look

The four-XCD MLA scheduling change requires GPU validation of round-robin decode correctness and base/head latency, which this static review environment cannot provide.

No open issues 🎉

What changed in this PR

This PR completes the backend-specific die-count updates by making HIP MLA metadata use the device’s die count on gfx950, rather than assuming eight.

Changes:

  • Adds a cached per-device helper that identifies die counts from the architecture and compute-unit count.
  • Uses four dies for MI350P MLA scheduling while preserving eight for full-size gfx950 devices and one for other architectures.
File Description
csrc/​kernels/​mla/​metadata/​v1_2_device.cuh Replaces the hardcoded gfx950 die count with the device query.
csrc/​include/​aiter_hip_common.h Adds the cached die-count helper, mirroring the Python build-target table.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 2, 2026 10:28

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.

🔵 Needs a closer look

The four-XCD MLA scheduling changes still require hardware validation, particularly for round-robin correctness and base/head latency, which this review environment cannot verify because it cannot run HIP workloads.

No open issues 🎉

Review effort: Balanced

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 5, 2026 15:31

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

- Adds get_num_xcds_func to aiter_hip_common, a per-device probe behind
  SynchronizedCache, and reads it in the v1.2 MLA metadata instead of
  assuming eight dies on every gfx950 part.
- Instantiates the IQ2R gate and down kernels for four and eight dies and
  launches the one the device reports.
- Keeps the CU-aware generic split when Mega-MHC refuses its distributed
  variants, instead of dropping to split 1.
- Plans the sampled top-k for the device's CU count instead of a fixed 256,
  and keys its workspace size by device.
@RElbers
RElbers removed this pull request from stack #7002 November 5, 2026 15:55
@RElbers
RElbers changed the base branch from relbers/gfx950_opus_xcd_count to main November 5, 2026 15:55
@RElbers
RElbers force-pushed the relbers/gfx950_hip_xcd_count branch from 243f3d0 to a286f4c Compare November 5, 2026 15:55
Copilot AI balanced review requested due to automatic review settings November 5, 2026 15:55

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@RElbers

RElbers commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

I undid the PR stack, based each branch on main as one commit, and force-pushed. They don't depend on each other, so they can merge in any order.

@zufayu
zufayu requested review from amd-ruitang3 and zufayu November 6, 2026 00:54
- Drops the sentence on when the distributed variants fall back to the
  generic kernel. That fallback predates this change, which only alters the
  split the fallback uses, and the sentence restated the current tuning's
  token range.
Copilot AI balanced review requested due to automatic review settings November 6, 2026 13:39

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@vpietila-amd vpietila-amd self-assigned this Nov 11, 2026

@vpietila-amd vpietila-amd 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.

Looks mostly good. One small request to consider if we should revise get_num_xcds_func to use hipDeviceAttributeNumberOfXccs for detecting XCDs instead of a hand-written SKU table.

Comment thread csrc/include/aiter_hip_common.h Resolved
@zufayu

zufayu commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

@aiter-bot review

@zufayu
zufayu removed their request for review November 12, 2026 05:07
@aiter-bot

Copy link
Copy Markdown

aiter-bot — queued behind 2 review(s); expect to start in about 57 min. The box reviews one PR at a time.

@aiter-bot aiter-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.

ROCm/aiter PR #6877 — [HIP] Fix the hardcoded die count in the HIP kernels

Review (advisory): ⚠️ NEEDS WORK
Validation (deterministic): NOT RUN — REVIEW_AUTO_VALIDATE=0 (auto-validation disabled); the single test target op_tests/test_topk_per_row.py exists but could not be run, and this box is 8× gfx942, where the changed gfx950-only paths (topk sampled compiles to traps off-gfx950, IQ2R, MLA v1.2 num_xcd) would not be exercised anyway — an environment gap, not a PR defect
Perf (advisory): NOT RUN — no gfx950 on this box (8× gfx942) and the auto-run disabled (REVIEW_AUTO_VALIDATE=0); the changed kernels are gfx950-only, so no base-vs-head timing could be taken here — an environment gap, not a PR defect
Size: 387 lines / 11 files
Design (advisory): the die count is resolved at runtime from a hand-written (arch, CU count) → dies SKU table inside get_num_xcds_func (silent 8-die default) rather than the hipDeviceAttributeNumberOfXccs query the repo already uses in chip_info.py and topk_per_row_kernels.cu
Human: recommend vpietila-amd review whether get_num_xcds_func in this PR should query hipDeviceAttributeNumberOfXccs instead of the hand-written SKU table, and what a gfx950 CU count the table does not list should do

⚠️ [verified] The PR ships MI350P behavior that never ran on an MI350P: the author states "Unverified on MI350P: the IQ2R, Mega-MHC and topk changes, and performance", and the topk cu=128 shape plan ran on no hardware at all (IQ2R was emulated with the die count forced to 4 and Mega-MHC with the selection told 128 CUs, but nothing forces the CU count for topk), so a mis-sized plan — including shapes topk_sampled_supports may now refuse on 128 CUs at M in (128, 256], re-routing prefill to the slower paths — and the unmeasured MI350P effect would surface only post-merge. Author must run op_tests/test_topk_per_row.py, test_iq2r_glm53.py and test_mega_mhc on an MI350P part, with a base-vs-head timing, before merge.

3 finding(s) posted inline, on the lines they are about:

  • csrc/kernels/mla/metadata/v1_2_device.cuh:1408 — The changed MLA v1.2 metadata (csrc/kernels/mla/metadata/v1_2_device.cuh:1408) is the default fast_mode=True…
  • csrc/include/aiter_hip_common.h:737 — get_num_xcds_func (csrc/include/aiter_hip_common.h:737) hand-writes a (arch, CU count) → dies SKU table with…
  • aiter/ops/mega_mhc.py:316 — sizes the CU-masked fallback's split by the full device CU count, so an HSA_CU_MASK/ROC_GLOBAL_CU_MASK run on…

Comment thread csrc/include/aiter_hip_common.h Resolved
Comment thread aiter/ops/mega_mhc.py Resolved
- Asks HIP for hipDeviceAttributeNumberOfXccs in get_num_xcds_func, cached per device as before, instead of matching the architecture and CU count against a table of parts.
- Fails with the device and the returned value when the query does not give at least one die, where an unlisted part used to get eight silently.

Every supported HIP runtime provides the attribute, and the topk kernels already read it the same way.
- Breaks five calls and declarations in topk_per_row_sampled_kernels.cu that ran past 100 columns, with no change to the code.
@RElbers RElbers added the ci:all label Nov 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

No activity for 15 days, so this is now labelled stale. A push or a comment clears it; keep-open exempts it. Nothing is closed automatically.

@github-actions github-actions Bot added the stale The PR hasn't been updated in 2 weeks label Nov 29, 2026
@github-actions github-actions Bot removed the stale The PR hasn't been updated in 2 weeks label Nov 30, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants