Skip to content

performance: post-compilation CLI to get/set kvcache_num_blocks - #638

Open
rebel-jongho wants to merge 5 commits into
devfrom
jongho/kvcache-num-blocks-cli
Open

performance: post-compilation CLI to get/set kvcache_num_blocks#638
rebel-jongho wants to merge 5 commits into
devfrom
jongho/kvcache-num-blocks-cli

Conversation

@rebel-jongho

@rebel-jongho rebel-jongho commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

Type of Change

  • Performance improvement

Stacked on #639 (base = jongho/memory-budget-nonsave); auto-retargets to dev once #639 merges.

Changes Overview

Post-compilation subcommands on optimum-rbln-cli operating on an already-compiled artifact directory (--model-id DIR), without recompilation:

  • --get-kvcache-num-blocks — prints kvcache_num_blocks from rbln_config.json.
  • --set-kvcache-num-blocks N — resizes the kv-cache of every *.rbln to N blocks.
  • --set-memory-budget BUDGET — resizes to the largest block count that fits BUDGET (a float fraction of the NPU available DRAM like 0.8, a "80%" string, or bytes like "10GB"). Mutually exclusive with --set-kvcache-num-blocks.

set edits in place, or with --output-dir writes a full resized copy there (copying the non-.rbln files too) and leaves the source untouched.

optimum-rbln-cli --get-kvcache-num-blocks --model-id ./my_artifact
optimum-rbln-cli --set-kvcache-num-blocks 128 --model-id ./my_artifact
optimum-rbln-cli --set-memory-budget 0.8 --model-id ./my_artifact
optimum-rbln-cli --set-memory-budget 10GB --model-id ./my_artifact --output-dir ./my_artifact_10gb

Internals:

  • RBLNDecoderOnlyFlashAttentionMixin.rescale_kvcache_num_blocks(compiled_models, rbln_config, target): saved buffers hold per_block * current bytes (current = rbln_config.kvcache_num_blocks); rescaling by the rational ratio target / current (rebel-compiler exp_rescale_buffer_size) yields exactly per_block * target. Stateless; rbln_config.json is the source of truth.
  • --set-memory-budget computes the target via estimate_num_kvcache_blocks(..., current_blocks=current). current_blocks (added to estimate_num_kvcache_blocks / _search_num_kvcache_blocks, default 1) makes the fit search correct on an already-resized artifact (buffers at current, not 1). The transient budget is not persisted to rbln_config.json (model: memory_budget config semantics (non-save, mutually exclusive with kvcache_num_blocks) #639 marks memory_budget non-save).
  • Guards: target < num_min_blocksValueError; target above the compile-time count or num_full_blocks → warning; older rebel-compiler without exp_rescale_buffer_size → clear RuntimeError.

Dependency

Requires rebel-compiler's new exp_rescale_buffer_size (rebellions-sw/rebel_compiler#12067).

Motivation and Context

Lets users tune kvcache_num_blocks on a compiled artifact without recompiling — either to an explicit count or to fit a memory budget. Split out from #629 per review; the memory_budget estimation change (#637) is already merged into dev.

How to test

On a compiled decoder-only artifact: --get prints the count; --set-kvcache-num-blocks N (N ≥ num_min_blocks) rewrites buffers to per_block * N and updates the config (verified by reloading the .rbln); --set-memory-budget BUDGET computes and sets the fitting count (smaller budget → fewer blocks); infeasible targets (< num_min_blocks) raise ValueError; --output-dir leaves the source untouched.

@rebel-jongho
rebel-jongho marked this pull request as ready for review July 21, 2026 04:03
@rebel-jongho
rebel-jongho force-pushed the jongho/kvcache-num-blocks-cli branch 2 times, most recently from 7b69f4e to df4c3f6 Compare July 22, 2026 04:06
@rebel-jongho
rebel-jongho marked this pull request as draft July 22, 2026 04:06
@rebel-jongho
rebel-jongho marked this pull request as ready for review July 22, 2026 04:09
memory_budget is a compile-time estimation input, not a persisted property (the
resolved kvcache_num_blocks is what the artifact keeps), so register it in
subclass_non_save_attributes. qwen3_vl configs override that list, so add it there
too to avoid re-exposing it. It stays readable at runtime; it is just not written
to rbln_config.json.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@rebel-jongho
rebel-jongho force-pushed the jongho/kvcache-num-blocks-cli branch from df4c3f6 to f0f6dfc Compare July 22, 2026 06:08
@rebel-jongho
rebel-jongho changed the base branch from dev to jongho/memory-budget-nonsave July 22, 2026 06:08
@rebel-jongho
rebel-jongho marked this pull request as draft July 22, 2026 06:08
rebel-jongho and others added 3 commits July 22, 2026 06:16
memory_budget only guides automatic block estimation (kvcache_num_blocks unset).
If an explicit kvcache_num_blocks is also given, estimation is skipped and
memory_budget would be silently ignored, so raise a ValueError instead.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add optimum-rbln-cli --get-kvcache-num-blocks / --set-kvcache-num-blocks N
(--model-id DIR) to inspect/resize the kv-cache of an already-compiled artifact
without recompiling, plus RBLNDecoderOnlyFlashAttentionMixin.rescale_kvcache_num_blocks.
--set edits in place or writes a resized copy with --output-dir.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add optimum-rbln-cli --set-memory-budget BUDGET, which resizes the kv-cache to
the largest block count that fits BUDGET (float fraction / "80%" / bytes),
mutually exclusive with --set-kvcache-num-blocks. Thread current_blocks through
estimate_num_kvcache_blocks / _search_num_kvcache_blocks (default 1) so the fit
search is correct on an already-resized artifact; the transient budget is not
persisted to rbln_config.json.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@rebel-jongho
rebel-jongho force-pushed the jongho/kvcache-num-blocks-cli branch from f0f6dfc to 03948ff Compare July 22, 2026 06:17
Base automatically changed from jongho/memory-budget-nonsave to dev July 22, 2026 07:31
@rebel-jongho
rebel-jongho marked this pull request as ready for review July 22, 2026 08:52
@rebel-eunji

Copy link
Copy Markdown
Contributor
  1. nit: The --get-kvcache-num-blocks and --set-kvcache-num-blocks options should be mutually exclusive.
  2. To ensure atomicity, it would be safer to generate the artifact in a temporary directory and then rename it, rather than reusing an existing destination directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants