[Puzzletron] Fix KV-cache dimension calculation for models with custom head_dim (#2482)fix(puzzletron): respect explicit head_dim in KV-cache dimension calc… - #2484
Open
musi22 wants to merge 2 commits into
Conversation
…ulation (NVIDIA#2482) Signed-off-by: musi22 <shawrashmi7@gmail.com>
Contributor
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository: NVIDIA/Model-Optimizer/.coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
Included review availability: Your plan provides up to 12 included reviews per hour; 11 remain after this review. 📝 WalkthroughWalkthroughThe change updates KV-dimension calculation to use configured head dimensions, adds configuration fallbacks, supports descriptors without language-model configuration methods, and adds tests for KV-cache metrics. ChangesKV dimension resolution
Priority: ➖ Normal Estimated code review effort: 3 (Moderate) | ~20 minutes Change: Bug fix · Severity of issue fixed: Medium 🚥 Pre-merge checks | ✅ 6✅ Passed checks (6 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
… 100% coverage Signed-off-by: musi22 <shawrashmi7@gmail.com>
This branch has not been deployed
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.
Description
Fixes #2482
Currently,
calculate_kv_dimcomputeshead_size = n_embd // n_head. For architectures wherehead_dimis decoupled from embedding division (such as Mistral Small 24B wherehead_dim = 128vs5120 // 32 = 160, or gpt-oss 20B wherehead_dim = 64vs2880 // 64 = 45), this leads to inaccurate KV-cache memory and additive metric calculations.Changes
modelopt/torch/puzzletron/utils/misc.py:head_dim: int | None = Noneparameter tocalculate_kv_dim.head_dimis provided, it is used directly; otherwise, it falls back ton_embd // n_headto maintain complete backward compatibility.modelopt/torch/puzzletron/subblock_stats/calc_subblock_params_and_memory.py:calculate_additive_metricsandcalculate_attention_memory, resolvedeffective_head_dimfollowing precedence:subblock_config.qk_head_dim>model_config.head_dim(via_language_model_attr) > fallbackn_embd // n_head._language_model_attrhelper to safely handle configs whendescriptorlacksget_language_model_config.tests/unit/torch/puzzletron/test_kv_dim_calculation.pywith 6 unit tests validating Mistral Small 24B, gpt-oss 20B, default fallback, and precedence behavior.Testing
Summary by CodeRabbit
Bug Fixes
Tests