performance: allow user-adjustable kvcache_num_blocks estimation - #623
performance: allow user-adjustable kvcache_num_blocks estimation#623rebel-jongho wants to merge 1 commit into
Conversation
Add an optional `kvcache_num_blocks_adjuster` callable so users can override the auto-estimated KV cache block count at compile time. The default estimator assumes the model owns all available DRAM; the adjuster lets callers reserve memory for other uses or otherwise tune the block count to their deployment. The adjuster is carried on RBLNDecoderOnlyModelConfig as a non-serialized field and is forwarded to set_kvcache_num_blocks_after_compilation. Returned values below num_min_blocks raise; values above the estimate warn but are honored. Co-authored-by: Cursor <cursoragent@cursor.com>
|
Can this be adjusted after compilation? For example, say I've already compiled the model and the |
No. with this PR, It can't be done after compilation. This PR is just for pre-compilation. We need further refactoring to support that fuctionality. |
@rebel-jongho Are you planning to handle it in a follow-up PR, or include it here? The latter is actually what I need. |
|
Superseded by #629, which takes a different approach: instead of a compile-time |
Pull Request Description
Type of Change
Changes Overview
Add an optional
kvcache_num_blocks_adjustercallable that lets users override the auto-estimatedkvcache_num_blocksat compile time.RBLNDecoderOnlyModelConfiggains akvcache_num_blocks_adjuster: Optional[Callable[[int, RBLNModelConfig], int]]field (defaults toNone). It is registered insubclass_non_save_attributesso it is not serialized intorbln_config.json.RBLNDecoderOnlyFlashAttentionMixin.set_kvcache_num_blocks_after_compilationaccepts an optionaladjusterargument. When provided, it is called asadjuster(estimated, rbln_config)and the return value replaces the estimate.rbln_config.kvcache_num_blocks_adjusterinto that argument.RBLNQwen3VLForConditionalGenerationConfigandRBLNQwen3VLModelConfigoverridesubclass_non_save_attributes; their lists are extended with"kvcache_num_blocks_adjuster"so the inherited exclusion is not shadowed.Validation policy for the adjusted value:
intreturn value raisesValueError.num_min_blocksraises the existing "Memory is not enough"ValueError.Motivation and Context
The current
estimate_num_kvcache_blockslogic assumes the model owns all available DRAM on the NPU. In deployments where some memory must be reserved for other processes or components, the auto-estimated block count can be too high and lead to OOM at compile or runtime. Users currently have no way to influence the auto-estimated value other than fully overridingkvcache_num_blockswith a manual constant, which loses the benefit of DRAM-based estimation.The adjuster gives users a hook to scale the estimate to their actual memory budget while still relying on the compiler's DRAM-based estimate as the starting point. It is a compile-time-only knob: when loading a precompiled artifact, the estimation path does not run, so the adjuster is ignored and the persisted
kvcache_num_blocksis used.Note for reviewers / future maintainers
subclass_non_save_attributesis a plain class attribute that is shadowed (not merged) when a subclass redefines it. Any future subclass ofRBLNDecoderOnlyModelConfigthat overridessubclass_non_save_attributesmust also include"kvcache_num_blocks_adjuster", otherwise a callable passed by the user would be serialized intorbln_config.jsonand crashjson.dump.Related Issues
N/A
Usage Example
Test Plan
RBLNDecoderOnlyModelConfig(kvcache_num_blocks_adjuster=fn)and confirm the field is stored but absent from_prepare_for_serialization().kvcache_num_blocks=0and a reducing adjuster; confirm the resultingrbln_config.kvcache_num_blocksmatches the adjuster output and the model runs.num_min_blocks; confirm the expectedValueErroris raised.Made with Cursor