[bugfix] avoid recomputing Flash-decode RoPE cache - #160
Open
taking-lying-flat wants to merge 1 commit into
Open
[bugfix] avoid recomputing Flash-decode RoPE cache#160taking-lying-flat wants to merge 1 commit into
taking-lying-flat wants to merge 1 commit into
Conversation
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
Root cause
dict.setdefault(key, default)evaluatesdefaultbefore checking whetherkeyalready exists. Therefore this code recomputed a complete cos/sin pair on every decode step, even on a cache hit:The returned value still came from the dictionary, so the cache appeared to work functionally, but the newly computed tensors were discarded after consuming compute and temporary GPU memory.
Scope
GPTModel._get_rotary_pos_emb()runs before the decoder and passes the resulting RoPE tensors into the decoder. Consequently, the redundantget_cos_sin()call happens once per model decode step, not once per decoder layer. Decoder depth increases the model/cache memory baseline, but does not multiply this particular call count.CUDA memory benchmark
The benchmark models the full text-cache topology of Qwen3.5-4B instead of measuring RoPE in isolation:
[1, 4, sequence_length, 256]head_dim=256,partial_rotary_factor=0.25)Environment: NVIDIA RTX A1000 Laptop GPU (4 GiB), PyTorch 2.13.0+cu130, Megatron-Core 0.18.2.
get_cos_sin()callsAt 32K and 64K, the old path also increased peak CUDA reserved memory by 20 MiB; the fixed path added 0 MiB. The absolute allocation reduction scales with sequence length. Its percentage of total process memory depends on model weights, parallelism, KV-cache layout, and other runtime allocations.
This is a cache-focused Transformer simulation, not an end-to-end throughput benchmark: model weights, attention kernels, and unrelated decode activations are intentionally excluded so the allocation caused by this code path remains measurable. The timing column measures only the RoPE cache path.
Validation
python -m py_compile src/mcore_bridge/model/gpt_model.pygit diff --check upstream/main...HEAD