mha: size the K/V arg specs by num_KV_heads, not num_heads - #169
Open
atassis wants to merge 1 commit into
Open
Conversation
design.py declares Q/O as (heads, S_q_pad, d) but K/V as (num_KV_heads, S_kv_pad * d). get_arg_spec used num_heads for all four, so under GQA K and V are over by num_heads/num_KV_heads: 8388608 against 2097152 on the GQA param already in test.py, a factor of 4. Two things hid it. num_kv_heads=0 normalizes to heads, which makes the old formula accidentally right for plain MHA, and run_test reads only spec.direction, never spec.shape. It surfaces in an OperatorSequence, where compilation asserts the MLIR arg count equals the computed one.
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.
design.pydeclares Q and O as(heads, S_q_pad, d)but K and V as(num_KV_heads, S_kv_pad * d).get_arg_spec()sized all four fromnum_heads, sounder GQA the K/V specs are over by
num_heads / num_KV_heads. On the GQA paramalready in
test.py(seq_len=16384, d=64, heads=8, pipelines=8, kv_heads=2) that is8388608 against 2097152, a factor of 4.
Two things kept it hidden.
num_kv_heads=0normalizes toheadsindesign.py, whichmakes the old formula accidentally correct for standard MHA, and
run_testreads onlyspec.direction, taking input buffers from the supplied tensor rather than fromspec.shape. So nothing consults the wrong value today. It surfaces inside anOperatorSequence, wherecompilation/sequence.pyasserts the MLIR arg count equalsthe one computed here.
Added
iron/operators/mha/test.py:test_arg_spec_matches_design_shapes, covering GQA andstandard MHA. It asserts against the shapes
design.pydeclares rather than againstliterals, so it fails if either side drifts. Device-free, and deliberately not reusing
get_params()-- the GQA case there is markedextensive, which is half of why thiswent unnoticed.
Changed
iron/operators/mha/op.py:get_arg_spec()sizes Q/O fromnum_headsand K/V fromnum_KV_heads, applying the same0 -> num_headsnormalizationdesign.pydoes.Removed
Known limitation
_calculate_seq_paddinghardcodes the block size to 64, whiledesign.pyderivesS_q_padandS_kv_padfromB_qandB_kvseparately. That is correct today onlybecause both default to 64 and
op.pynever overrides them, and__post_init__setsself.B_q/self.B_kvwhich the helper ignores. Left alone here to keep this to onedefect.