Skip to content

[AMD] [GLM-5.3-Flash Day 0] Enable zero-RoPE TileLang DSA on gfx950 - #38547

Merged
hnyls2002 merged 10 commits into
sgl-project:mainfrom
Raiden-Makoto:RM/glm53-rocm-tilelang-zero-rope
Sep 22, 2026
Merged

hnyls2002 merged 10 commits into
sgl-project:mainfrom
Raiden-Makoto:RM/glm53-rocm-tilelang-zero-rope

Conversation

@Raiden-Makoto

@Raiden-Makoto Raiden-Makoto commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Replacement for #37653, which GitHub closed when its deleted support base was merged into main.

GLM-5.3-Flash sparse DSA uses a 256-wide NoPE latent with a zero-width RoPE tail. The HIP TileLang BF16 partial kernel still emits zero-extent tail allocation/copy/gather/GEMM work, while the HIP FP8 kernel and scaled cache helpers are specialized for the existing DeepSeek-style 512+64 layout.

This PR enables GLM's 256+0 geometry without changing the established 512+64 behavior.

Contract Before With this PR
HIP BF16 tail zero-width tail buffers and operations are emitted tail allocation, copy, gather, and GEMM are compiled out
HIP FP8 main width fixed to four 128-wide tiles (d_v=512) supports two tiles for d_v=256 and retains four for d_v=512
HIP FP8 RoPE tail fixed 64-wide tail supports zero or 64 elements and compiles out zero-tail work
scaled FP8 cache row fixed 656 bytes (512 FP8 + 16 scale + 128 RoPE) also supports 264 bytes (256 FP8 + 8 scale + 0 RoPE)
paged dequant/gather fixed 512+64 offsets and launch geometry derives dimensions, scale count, offsets, and output width from the packed row

Scope

Four production files:

  • tilelang_kernel.py: compile out HIP BF16 zero-tail work and specialize the HIP FP8 kernel for either 256+0 or 512+64.
  • quant_k_cache.py: infer or accept the NoPE width, derive the number of 128-element scale tiles, and permit an empty RoPE tensor.
  • dequant_k_cache.py: recognize 264-byte and 656-byte packed rows and use their derived dimensions in direct, paged, and gather/dequant/requant paths.
  • memory_pool.py: document the dimension-derived scaled layout and optional RoPE payload at the existing write sites.

One MI35x test file directly covers the TileLang kernels and scaled cache helpers. No model loading, server launch, accuracy benchmark, scheduler/backend policy, dense MHA path, mHC path, MoE path, or CUDA TileLang FP8 behavior is changed.

The FP8 kernel keeps the existing 512+64 allocation, GEMM, accumulation, normalization, and store order. The 256+0 specialization emits only the first two 128-wide tiles and no tail operations.

Test plan

Combined main-based integration (RM/glm53-day0-main-integration tested at 9d0d62b744; current integration head 4e2f84ff3c; TP8/TP4 validation predates the above-cap guard, whose below-cap path is unchanged, pinned AITER d9e5ef7ce0, fresh caches): TP8 GSM8K 97.50% in 87.0s with 0.00% errors; TP4 GSM8K 96.97% in 320.2s with 0.00% errors. Decode CUDA graphs were enabled in both runs.

Recovery base: main @ 30e7a3072d.
Recovery validation on MI355X: scoped pre-commit passed; all 4 BF16/FP8 TileLang and cache-layout test groups passed.

Docker: rocm/sgl-dev:v0.5.18-rocm720-mi35x-20260830.
Hardware: one MI350X (gfx950).
Base: xinyuan/glm-5.3-flash-support @ 545bd6f839.
The same new test file was run in both arms; the four production files are the only variables.

Arm Result
Baseline FAILED (6 failures, 8 errors). All six BF16 256+0 cases hit the generated zero-tail kernel failure; the 256+0 scaled-cache case is rejected by the fixed layout; FP8 256+0 does not match the Torch oracle.
This PR BF16 256+0 matrix: passed; FP8 256+0 matrix: passed; 256+0 and 512+64 cache round-trip/paged-gather checks: passed; BF16/FP8 512+64 controls: passed.

Direct GLM geometry coverage:

  • Q [tokens, 64, 256], KV [slots, 1, 256], output BF16 [tokens, 64, 256];
  • token counts 1, 8, and 17;
  • top-k widths 2048 and 2112;
  • fully valid rows and 2051 logical entries padded with -1 to 2112;
  • explicit Torch gather/mask/score/softmax/value oracle;
  • BF16 and native gfx950 FP8 inputs;
  • finite shape/dtype checks and bit-identical repeated execution;
  • 264-byte (256+0) and 656-byte (512+64) scaled-cache round trips and repeated-page gathers;
  • BF16 and FP8 512+64 sparse-attention regression controls.
Check Detail
Registration register_amd_ci(suite="stage-b-test-1-gpu-small-amd-mi35x")
Formatting File-scoped syntax, isort, Ruff, Black, codespell, and registered-test validation pass. The whole-tree package-location hook separately reports a pre-existing file outside this PR: python/sglang/jit_kernel/tests/test_triton_store_cache_local.py.
Accuracy not measured — this is component-level Day-0 correctness; full-model serving is intentionally outside scope.
Speed not measured — no server or performance benchmark was run.

CI States

Latest PR Test (Base): ❌ Run #35688366857
Latest PR Test (Extra): ❌ Run #35688366669
Latest PR Test (AMD ROCm 10): ❌ Run #35688366761

Compile out zero-width tail work and generalize FP8 TileLang/cache layouts so GLM's 256+0 geometry is covered without changing the existing 512+64 path.
@Raiden-Makoto
Raiden-Makoto force-pushed the RM/glm53-rocm-tilelang-zero-rope branch from eab6b47 to 54624dd Compare September 8, 2026 19:42
@1am9trash 1am9trash added amd run-ci CI: run the baseline test suite on this PR labels Sep 9, 2026
@Raiden-Makoto

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@Raiden-Makoto

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

1am9trash pushed a commit that referenced this pull request Sep 17, 2026
Squashed from #38547, which carries the same change
against main and remains open there for review.

Co-Authored-By: Raiden-Makoto <[email protected]>
Co-authored-by: Cursor <[email protected]>
Comment on lines +8 to +13
def _infer_dsa_dims(dim_quant: int) -> tuple[int, int]:
if dim_quant == 264:
return 256, 0
if dim_quant == 656:
return 512, 64
raise ValueError(f"Unsupported packed DSA KV row width: {dim_quant}")

@Jacob0226 Jacob0226 Sep 18, 2026 •

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.

quantize_k_cache_separate now emits widths this cannot read

This PR widens the quant side from dim_nope != 512 -> raise to dim_nope % 128 != 0 -> raise with dim_rope in (0, 64), while dequant stays a two-entry table, so the pair no longer matches:

quant input packed width this function
256 + 0 (GLM-5.3 no-PE) 264 ✅
512 + 64 (DeepSeek MLA) 656 ✅
512 + 0 528 ❌ ValueError
256 + 64 392 ❌ ValueError

Before this PR quant accepted exactly one combination and this accepted exactly one width, so the asymmetry is new rather than pre-existing.

Not urgent: no caller can reach it (dsa_backend, deepseek_v4_backend, forward_mha and hip_flash_mla all pass a cache produced by the paired quant for one of the two real models), and it raises rather than returning wrong numbers. Worth closing anyway because it is cheap and it also un-breaks the dv / dim_nope / dim_rope parameters — _infer_dsa_dims runs unconditionally at lines 74 and 98 before they are consulted, so today they cannot rescue an unsupported width, only reject a supported one. Generalising here fixes all five call sites and leaves lines 74-81 and 98-100 untouched.

The width determines the pair uniquely, so the table can go:

Suggested change
def _infer_dsa_dims(dim_quant: int) -> tuple[int, int]:
if dim_quant == 264:
return 256, 0
if dim_quant == 656:
return 512, 64
raise ValueError(f"Unsupported packed DSA KV row width: {dim_quant}")
def _infer_dsa_dims(dim_quant: int, tile_size: int = 128) -> tuple[int, int]:
"""Recover (dim_nope, dim_rope) from a packed row width.
A packed row holds dim_nope fp8 bytes, 4 scale bytes per tile and 2 bytes
per rope element, so
dim_quant = dim_nope * (tile_size + 4) / tile_size + 2 * dim_rope.
With dim_rope constrained to 0 or 64 and dim_nope to a multiple of
tile_size at most one candidate can satisfy that: the two branches differ
by 128 * tile_size / (tile_size + 4), which is not a multiple of tile_size,
so they cannot both land on a multiple of tile_size.
"""
for dim_rope in (0, 64):
numer = (dim_quant - 2 * dim_rope) * tile_size
if numer % (tile_size + 4):
continue
dim_nope = numer // (tile_size + 4)
if dim_nope > 0 and dim_nope % tile_size == 0:
return dim_nope, dim_rope
raise ValueError(f"Unsupported packed DSA KV row width: {dim_quant}")

I checked this is equivalent rather than a behaviour change: 264 -> (256, 0) and 656 -> (512, 64) as before, every dim_nope in 128..1024 against dim_rope in (0, 64) round-trips, and no width in 1..20000 resolves to two candidates.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 4b55b94f8b: _infer_dsa_dims now derives every valid 128-aligned NoPE width with RoPE 0 or 64 instead of using a two-entry table. Added round-trip coverage for NoPE widths 128–1024 plus invalid-width rejection; the full MI35X file passes 6/6 tests.

Comment on lines 316 to 317
The paged KV cache stores 656 bytes per token:
[512 nope_fp8 | 16 scales_f32 (4 groups) | 128 rope_bf16_bytes]

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.

Docstring still describes the fixed 656/576 layout

Line 345 now resolves the widths through _infer_dsa_dims, so this function already handles the 264 -> (256, 0) case, but the docstring pins the DeepSeek shape in four places:

line says is now
316-317 stores 656 bytes per token: [512 nope_fp8 | 16 scales_f32 (4 groups) | 128 rope_bf16_bytes] one of two layouts
331 quant_k_cache: [total_num_tokens, 1, 656] dim_quant, resolved from the input
339 out: ... [num_tokens + extra_rows, 1, 576] dim_nope + dim_rope
342 output: [num_tokens + extra_rows, 1, 576] same

Line 339 is the one that matters, since out is caller-allocated: someone sizing it from the docstring while running the 264 path allocates 576 instead of 256. That is caught — assert out.numel() == total_rows * out_dim reports both numbers — so this is doc-only rather than a silent corruption, which is why I have not flagged it higher.

Describing the layout symbolically and giving both widths as examples would cover it. No suggestion block here since the wording is yours.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 4b55b94f8b: the docstring now describes the packed layout symbolically, lists both 264-byte and 656-byte examples, and sizes quant_k_cache, out, and the return value from the inferred dimensions. The MI35X regression file passes 6/6 tests.

@Jacob0226 Jacob0226 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.

LGTM

Raiden-Makoto and others added 4 commits September 20, 2026 21:00
… it to distinct failure modes

The file lived in a new test/registered/e2e/attention/ directory although it
launches no server; the registry validator's kernel layout is
test/registered/kernels/ops/<group>/. The dim-inference case recomputed the
packed width with the implementation's own formula, so it is now a literal
table of the four widths quantize_k_cache_separate accepts. The BF16/FP8
zero-RoPE matrices drop from six subcases to the two that compile distinct
inner_iter specializations. The scaled-cache round trip moves to its own
class so it also runs on the CUDA kernel lane, where the 264-byte layout is
consumed by set_mla_kv_buffer.

_dequantize_k_cache_fast has one caller, which always passes the resolved
dims, so the second _infer_dsa_dims call inside it is removed.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@hnyls2002
hnyls2002 merged commit d6cc283 into sgl-project:main Sep 22, 2026
153 of 166 checks passed
@Raiden-Makoto
Raiden-Makoto deleted the RM/glm53-rocm-tilelang-zero-rope branch September 24, 2026 23:42
hdt98 added a commit to hdt98/sglang that referenced this pull request Sep 25, 2026
Reset quark.py to upstream sgl-project#39779's per-expert fused config resolution and
keep only the block-FP8 activation-grouping check. Fused shared experts are
CUDA-only, so the removed shared-expert precision matching loses nothing on
ROCm.

Drop tests whose targets are now upstream code or duplicate upstream
coverage: k-pool cache layout and backend dispatch (sgl-project#39341), TileLang
zero-RoPE (sgl-project#38547), transform_index widths (sgl-project#39340), mHC HIP dispatch
(sgl-project#38545), the NextN name mapper, write-plan multi-pool, negative-slot
scatter, and the comment-only AITER runner. Keep one focused test per
production change this branch owns.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

amd jit-kernel memory-pool quant LLM Quantization run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants