[Fix] Align buffer-resource num_records with logical tensor sizes#555
Closed
coderfeli wants to merge 1 commit into
Closed
[Fix] Align buffer-resource num_records with logical tensor sizes#555coderfeli wants to merge 1 commit into
coderfeli wants to merge 1 commit into
Conversation
Several MoE / GEMM kernels call ``buffer_ops.create_buffer_resource(arg, max_size=False)`` without supplying ``num_records_bytes``. With ``max_size=False`` the descriptor size is inferred from the memref *type* at trace time, which silently truncates when the kernel is reused for tensors with larger logical extents — producing out-of-bounds reads (returns 0) or dropped writes. This mirrors aiter PR ROCm/aiter#3314 and extends the same fix to two files aiter does not cover (``moe_blockscale_2stage.py`` and the ``w_rsrc``/``bias_rsrc`` sites in ``mixed_moe_gemm_2stage.py``). Compute ``num_records`` from compile-time constants (experts, model_dim, inter_dim, num_groups, …) and pass it explicitly to ``create_buffer_resource``. Also fixes pre-existing dead expressions that lacked the ``* elem_bytes`` multiplier. Files touched (18 buffer-resource sites total): - kernels/moe_gemm_2stage.py — stage1: w/sw/sorted/sorted_w; stage2: w/sw - kernels/moe_blockscale_2stage.py — stage1: w/sw/sorted/sorted_w; stage2: w/sw - kernels/mixed_moe_gemm_2stage.py — stage1: w/bias/sorted_scale; stage2: w/bias - kernels/preshuffle_gemm.py — scale_a (with fp4 path) Test plan: - tests/kernels/test_preshuffle_gemm.py (103 passed) - tests/kernels/test_moe_gemm.py (349 passed) - tests/kernels/test_moe_blockscale.py (4 passed) - bash scripts/check_python_style.sh (clean) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
Several MoE / GEMM kernels call
buffer_ops.create_buffer_resource(arg, max_size=False)without supplyingnum_records_bytes. Withmax_size=Falsethe descriptor size is inferred from the memref type at trace time, which silently truncates when the kernel is reused for tensors with larger logical extents — producing out-of-bounds reads (returns 0) or dropped writes.This mirrors aiter PR ROCm/aiter#3314 and extends the same fix to two files aiter does not cover (
moe_blockscale_2stage.pyand thew_rsrc/bias_rsrcsites inmixed_moe_gemm_2stage.py).What changes
For each affected site, compute
num_recordsfrom compile-time constants (experts,model_dim,inter_dim,num_groups, …) and pass it explicitly tocreate_buffer_resource. Pre-existing dead expressions that lacked the* elem_bytesmultiplier are also corrected.Files (18 buffer-resource sites total)
kernels/moe_gemm_2stage.pyw_rsrc,sw_rsrc,sorted_rsrc,sorted_w_rsrc; stage2:w_rsrc,sw_rsrckernels/moe_blockscale_2stage.pyw_rsrc,sw_rsrc,sorted_rsrc,sorted_w_rsrc; stage2:w_rsrc,sw_rsrckernels/mixed_moe_gemm_2stage.pyw_rsrc,bias_rsrc,sorted_scale_rsrc; stage2:w_rsrc,bias_rsrckernels/preshuffle_gemm.pyscale_a_rsrc(with fp4 path)Test plan
tests/kernels/test_preshuffle_gemm.py— 103 passedtests/kernels/test_moe_gemm.py— 349 passedtests/kernels/test_moe_blockscale.py— 4 passedbash scripts/check_python_style.sh— cleanRelated
mark_static)🤖 Generated with Claude Code