Skip to content

Optimize recsys-FlexKV - #418

Merged
shijieliu merged 8 commits into
NVIDIA:mainfrom
Clebrate:lyl-flexkv-optimization
Jul 10, 2026
Merged

shijieliu merged 8 commits into
NVIDIA:mainfrom
Clebrate:lyl-flexkv-optimization

Conversation

@Clebrate

@Clebrate Clebrate commented Jun 8, 2026 •

Copy link
Copy Markdown
Collaborator

Description

  • Optimize _build_slot_mappings by reducing D2H datasize.
  • Optimize as_batch
  • support SSD

Performance improvement summary

  1. slot_mapping_optimization

Before vs. after at seq_len = 2048 (L1, ms)

op Before After Δ
offload_launch 7.192 3.345 −53.5%
offload_wait 30.655 22.422 −26.9%
onboard_launch 1.380 0.696 −49.6%
onboard_wait 21.244 19.341 −9.0%
offload launch+wait 37.847 25.767 −31.9%
onboard launch+wait 22.624 20.037 −11.4%
total pipeline 67.377 54.085 −19.7%

Main gains:

  • Offload launch: _build_slot_mappings drops from 3.30 ms → 0.43 ms (−87%).
  • Offload wait: −15% ~ −37% across seq lengths.
  • Onboard launch: batch launch cuts onboard launch ~−50%.

Launch + Wait Summary (flexkv_as_batch=0 vs flexkv_as_batch=1)

path seq_len as_batch=0 (ms) as_batch=1 (ms) Δ
offload 1024 26.87 18.63 −30.7%
offload 2048 30.59 25.77 −15.7%
offload 4096 39.17 38.58 −1.5%
onboard 1024 17.51 14.65 −16.4%
onboard 2048 26.31 20.04 −23.9%
onboard 4096 40.10 30.59 −23.7%

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@Clebrate
Clebrate force-pushed the lyl-flexkv-optimization branch 10 times, most recently from 42c9818 to 9b151af Compare June 16, 2026 08:43
@Clebrate
Clebrate force-pushed the lyl-flexkv-optimization branch 4 times, most recently from b2ae455 to 188fda3 Compare June 23, 2026 01:34
@Clebrate
Clebrate force-pushed the lyl-flexkv-optimization branch from 188fda3 to d38012d Compare June 24, 2026 03:56
@Clebrate
Clebrate marked this pull request as ready for review June 24, 2026 03:58
@Clebrate

Copy link
Copy Markdown
Collaborator Author

/build

@JacoCheung

JacoCheung commented Jun 24, 2026 •

Copy link
Copy Markdown
Collaborator

❌ Pipeline #55646281 -- failed

Job Status Log
pre_check ❌ failed view
train_build_x86 ✅ success view
train_build_arm64 ✅ success view
tritonserver_build_x86 ✅ success view
prepare-jet-b200-smoke ✅ success view
prepare-jet-b200-inference ✅ success view
prepare-jet-cw-dfw-e2e-benchmark ✅ success view
build_whl ✅ success view
dynamicemb_test_fwd_bwd_8gpus ✅ success view
dynamicemb_test_load_dump_8gpus ✅ success view
unit_test_1gpu_a100 ❌ failed view
unit_test_1gpu_h100 ❌ failed view
unit_test_4gpu ✅ success view
unit_test_tp_4gpu ❌ failed view
L20_unit_test_1gpu ✅ success view
inference_unit_test_1gpu ✅ success view
inference_test_1gpu ❌ failed view

Result: 12/17 jobs passed

View full pipeline

@greptile-apps

greptile-apps Bot commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR optimizes the FlexKV KV-cache manager through two main code changes and adds SSD backend support. The optimizations deliver substantial latency reductions (up to −54% on offload launch, −50% on onboard launch) at typical sequence lengths.

  • _build_slot_mappings overhaul: replaces per-user D2H tensor copies with a single bulk transfer of kv_indices/kv_indptr, and caches the page_offsets arange as _page_offsets to avoid per-call allocation.
  • Batched launch (as_batch): new flexkv_as_batch config option enables a put_match + launch(as_batch=True) path for multi-user offload and a single batched launch call for onboard, controlled via FlexKVStorageManager.as_batch.
  • SSD support: new flexkv_config_path extra-config key lets users point to a FlexKV tier YAML (e.g., the new configs/flexkv_ssd.yml) to enable SSD-backed cache tiers.

Confidence Score: 5/5

The changes are well-scoped performance optimizations with no regressions observed in the benchmark data; the single stale comment in example.yml is cosmetic and does not affect runtime behavior.

The _build_slot_mappings refactor is straightforward and correct — a single bulk D2H copy replaces repeated per-user copies, and the cached _page_offsets avoids repeated allocation. The new as_batch offload path correctly handles edge cases (sub-page sequences via continue, empty launch slots via cancel). The slot mapping is now consistently sliced to seq_lengths[i] in the onboard path, which is more precise than the previous full-page-aligned length. No functional regressions are expected.

The new batch offload path in flex_kvcache_manager.py (the put_match + mask-slicing logic) is the most complex new code and would benefit from a dedicated test exercising the partial-match case.

Important Files Changed

Filename Overview
corelib/recsys_kvcache_manager/recsys_kvcache_manager/flex_kvcache_manager.py Core optimization: _build_slot_mappings now does a single bulk D2H copy and uses a cached _page_offsets tensor; onboard/offload slot mappings are pre-sliced to seq_lengths[i]; new as_batch path calls put_match + launch(as_batch=True) for multi-user offload. Logic is sound but the new batch offload path (put_match + mask interpretation) is non-trivial and not covered by updated tests.
corelib/recsys_kvcache_manager/recsys_kvcache_manager/kvcache_manager.py Wires up two new FlexKVStorageManager constructor params (as_batch, config_path) with proper string-to-bool parsing for flexkv_as_batch.
corelib/recsys_kvcache_manager/test/test_flexkv.py Refactors test setup to enable flexkv_as_batch=1 by default and adds SSD path support via RECSYS_FLEXKV_CONFIG_PATH; still contains a commented-out duplicate put block (lines 287-293) that was noted in a previous review.
corelib/recsys_kvcache_manager/configs/flexkv_ssd.yml New SSD tier config template with placeholder ssd_cache_dir; clean after the previous review's fix.
examples/hstu/inference/configs/example.yml New FlexKV tier config example for the HSTU inference directory; contains a stale RECSYS_FLEXKV_ENABLE_LAYERWISE=1 instruction that refers to an unimplemented env var.
corelib/recsys_kvcache_manager/README.md Adds FlexKV Backend Options section documenting flexkv_as_batch and flexkv_config_path; promotes Important Notes to a top-level heading.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[offload_kvcache_launch] --> B{as_batch AND\nnum_users > 1?}
    B -- No --> C[put_async per user\ncollect task_ids]
    B -- Yes --> D[put_match per user\ncompute return_mask]
    D --> E{valid_len >= page_size\nAND launch_slot valid?}
    E -- No --> F[cancel task_id\nskip user]
    E -- Yes --> G[collect task_id\nbatch_slot_mappings]
    G --> H{len task_ids > 1?}
    H -- Yes --> I[launch as_batch=True]
    H -- No --> J[launch as_batch=False]
    C --> K[HostKVTaskHandle]
    I --> K
    J --> K

    L[onboard_kvcache_launch] --> M[_build_slot_mappings\nbulk D2H kv_indices+kv_indptr]
    M --> N[filter users needing onboard]
    N --> O{as_batch AND\nnum_tasks > 1?}
    O -- Yes --> P[launch as_batch=True]
    O -- No --> Q[launch as_batch=False]
    P --> R[HostKVTaskHandle]
    Q --> R
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    A[offload_kvcache_launch] --> B{as_batch AND\nnum_users > 1?}
    B -- No --> C[put_async per user\ncollect task_ids]
    B -- Yes --> D[put_match per user\ncompute return_mask]
    D --> E{valid_len >= page_size\nAND launch_slot valid?}
    E -- No --> F[cancel task_id\nskip user]
    E -- Yes --> G[collect task_id\nbatch_slot_mappings]
    G --> H{len task_ids > 1?}
    H -- Yes --> I[launch as_batch=True]
    H -- No --> J[launch as_batch=False]
    C --> K[HostKVTaskHandle]
    I --> K
    J --> K

    L[onboard_kvcache_launch] --> M[_build_slot_mappings\nbulk D2H kv_indices+kv_indptr]
    M --> N[filter users needing onboard]
    N --> O{as_batch AND\nnum_tasks > 1?}
    O -- Yes --> P[launch as_batch=True]
    O -- No --> Q[launch as_batch=False]
    P --> R[HostKVTaskHandle]
    Q --> R
Loading

Reviews (9): Last reviewed commit: "Refine FLEXKV_CPU_BREAKDOWN doc" | Re-trigger Greptile

@Clebrate

Copy link
Copy Markdown
Collaborator Author

/build

@JacoCheung

JacoCheung commented Jun 25, 2026 •

Copy link
Copy Markdown
Collaborator

❌ Pipeline #55763541 -- failed

Job Status Log
pre_check ✅ success view
train_build_x86 ✅ success view
train_build_arm64 ✅ success view
tritonserver_build_x86 ✅ success view
prepare-jet-b200-smoke ✅ success view
prepare-jet-b200-inference ✅ success view
prepare-jet-cw-dfw-e2e-benchmark ✅ success view
build_whl ✅ success view
dynamicemb_test_fwd_bwd_8gpus ✅ success view
dynamicemb_test_load_dump_8gpus ✅ success view
unit_test_1gpu_a100 ❌ failed view
unit_test_1gpu_h100 ❌ failed view
unit_test_4gpu ✅ success view
unit_test_tp_4gpu ❌ failed view
L20_unit_test_1gpu ✅ success view
inference_unit_test_1gpu ✅ success view
inference_test_1gpu ❌ failed view

Result: 13/17 jobs passed

View full pipeline

@Clebrate
Clebrate force-pushed the lyl-flexkv-optimization branch from 8eb2eaf to 4ea0899 Compare July 8, 2026 09:19
@greptile-apps

greptile-apps Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Want your agent to iterate on Greptile's feedback? Try greploops.

@Clebrate
Clebrate force-pushed the lyl-flexkv-optimization branch 4 times, most recently from 3826ad5 to 6bc5757 Compare July 8, 2026 11:31
@Clebrate

Clebrate commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator Author

/build

@JacoCheung

JacoCheung commented Jul 8, 2026 •

Copy link
Copy Markdown
Collaborator

❌ Pipeline #57256931 -- failed

Job Status Log
pre_check ❌ failed view
train_build_x86 ✅ success view
train_build_arm64 ✅ success view
tritonserver_build_x86 ✅ success view
prepare-jet-b200-smoke ✅ success view
prepare-jet-b200-inference ✅ success view
prepare-jet-cw-dfw-e2e-benchmark ✅ success view
build_whl ✅ success view
dynamicemb_test_fwd_bwd_8gpus ✅ success view
dynamicemb_test_load_dump_8gpus ✅ success view
unit_test_1gpu_a100 ❌ failed view
unit_test_1gpu_h100 ❌ failed view
unit_test_4gpu ❌ failed view
unit_test_tp_4gpu ❌ failed view
L20_unit_test_1gpu ✅ success view
inference_unit_test_1gpu ✅ success view
inference_test_1gpu ❌ failed view

Result: 11/17 jobs passed

View full pipeline

@Clebrate

Copy link
Copy Markdown
Collaborator Author

/build

@JacoCheung

JacoCheung commented Jul 10, 2026 •

Copy link
Copy Markdown
Collaborator

❌ Pipeline #57502046 -- failed

Job Status Log
pre_check ❌ failed view
train_build_x86 ✅ success view
train_build_arm64 ✅ success view
tritonserver_build_x86 ✅ success view
prepare-jet-b200-smoke ✅ success view
prepare-jet-b200-inference ✅ success view
prepare-jet-cw-dfw-e2e-benchmark ✅ success view
build_whl ✅ success view
dynamicemb_test_fwd_bwd_8gpus ✅ success view
dynamicemb_test_load_dump_8gpus ✅ success view
unit_test_1gpu_a100 ❌ failed view
unit_test_1gpu_h100 ❌ failed view
unit_test_4gpu ✅ success view
unit_test_tp_4gpu ❌ failed view
L20_unit_test_1gpu ✅ success view
inference_unit_test_1gpu ✅ success view
inference_test_1gpu ❌ failed view

Result: 12/17 jobs passed

View full pipeline

Clebrate added 8 commits July 10, 2026 16:00
Replace per-seq page_ids D2H and numpy expand with one bulk kv_indices/kv_indptr
copy and torch CPU broadcast; drop loop内全量 slot D2H. Add ex1 v2 profiling
results (3-run mean) vs baseline and v1.
Remove text code-fence wrapper for the pipeline steps, drop the
denominator note, and move the slot_mapping_optimization + as_batch
perf summary out of the README (to be kept in the PR description).
@shijieliu
shijieliu force-pushed the lyl-flexkv-optimization branch from 3e216ac to 17b5a20 Compare July 10, 2026 08:00
@shijieliu
shijieliu merged commit e88284d into NVIDIA:main Jul 10, 2026
1 check passed
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.

3 participants