Skip to content

Recsys KVCache Manager refactored into standalone package - #387

Merged
shijieliu merged 19 commits into
NVIDIA:mainfrom
geoffreyQiu:kvcache_release
May 15, 2026
Merged

shijieliu merged 19 commits into
NVIDIA:mainfrom
geoffreyQiu:kvcache_release

Conversation

@geoffreyQiu

Copy link
Copy Markdown
Collaborator
  • Refactored the old kvcache manager AsyncKVCacheManager into standalone Recsys KVCache Manager package.
    Contains:
    • Fully use pinned host memory in the native host kvcache for better data transfer performance.
    • Add FlexKV as 3rd host kvcache backend.
    • Support LLM-style kvcache operations APIs.
  • Modified HSTU inference examples with latest kvcache manager.

@greptile-apps

greptile-apps Bot commented May 11, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR refactors the AsyncKVCacheManager into a standalone recsys_kvcache_manager package with a cleaner lifecycle API (lookup → allocate → onboard_launch → forward → offload_launch), adds pinned-host-memory support to the native backend, and introduces FlexKV as a third host KV-storage backend. The HSTU inference examples are updated to consume the new package.

  • New corelib/recsys_kvcache_manager package: KVCacheManager, GPUKVCacheManager, NativeHostKVCacheManager, and FlexKVStorageManager with shared HostKVTaskHandle/HostKVWaitResult abstractions. Numerous correctness bugs surfaced in earlier review rounds have been addressed.
  • HSTU example migration: InferenceDenseModule drops AsyncHSTUKVCacheManager in favour of KVCacheManager.from_config; KVCacheMetadata gains kv_seqlens/kv_seqlen_offsets fields used by the attention kernel instead of mutating total_history_offsets in-place.
  • Test updates: test_paged_hstu_layer.py switched to the new metadata helpers, but two bugs were introduced in the process (missing is_layerwise=True and an undersized static buffer).

Confidence Score: 4/5

Safe to merge the core library; the test file has two bugs that will cause incorrect behaviour when batch_size > 1.

The core recsys_kvcache_manager package and updated HSTU example modules look correct after many rounds of fixes. The two issues are confined to test_paged_hstu_layer.py: HostKVTaskHandle objects created without is_layerwise=True make stream_wait_layer a no-op, silently removing the CUDA-event barrier between the side-stream data copy and the default-stream attention kernel; and a stray no-op expression leaves the static metadata buffer sized for a single sequence rather than the full batch, causing an out-of-bounds write in copy_kvcache_metadata when max_batch_size > 1.

examples/hstu/test/test_paged_hstu_layer.py — both HostKVTaskHandle constructions and the get_kvcache_metadata_buffer call for the static buffer need attention.

Important Files Changed

Filename Overview
examples/hstu/test/test_paged_hstu_layer.py Two bugs introduced: HostKVTaskHandle created without is_layerwise=True breaks stream synchronization, and a no-op alignment expression leaves the static metadata buffer undersized for batch_size > 1.
corelib/recsys_kvcache_manager/recsys_kvcache_manager/kvcache_manager.py Core KVCacheManager with lookup/allocate/onboard/offload lifecycle; previous thread bugs appear addressed in this revision.
corelib/recsys_kvcache_manager/recsys_kvcache_manager/flex_kvcache_manager.py FlexKV backend: fixed mutable-default-arg, NameError, wrong enum values, and metadata mismatches from prior review; current code looks consistent.
corelib/recsys_kvcache_manager/recsys_kvcache_manager/host_kvstorage_manager.py Abstract base classes and HostKVTaskHandle dataclass; post_init guard and stream_wait_layer look correct.
examples/hstu/modules/inference_dense_module.py Migrated from AsyncHSTUKVCacheManager to KVCacheManager; cudagraph static buffer init and forward_with_kvcache offload sequencing look correct after analysis.
examples/hstu/modules/hstu_block_inference.py CUDA graph replay updated to use stream_wait_layer; teardown block commented out rather than removed.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant KVCacheManager
    participant GPUKVCacheManager
    participant HostKVStorageManager
    participant HSTUAttention

    Caller->>KVCacheManager: lookup_kvcache(user_ids, seq_lengths)
    KVCacheManager->>GPUKVCacheManager: lookup(user_ids)
    KVCacheManager->>HostKVStorageManager: build_index_meta + lookup_kvcache
    KVCacheManager-->>Caller: (index_meta, lookup_result)

    Caller->>KVCacheManager: allocate_kvcache(index_meta, lookup_result)
    KVCacheManager->>GPUKVCacheManager: allocate(...)
    KVCacheManager-->>Caller: kvcache_metadata

    Caller->>KVCacheManager: onboard_launch(index_meta, lookup_result, kvcache_metadata)
    KVCacheManager->>HostKVStorageManager: onboard_kvcache_launch(...)
    Note over HostKVStorageManager: Async host-to-GPU transfer begins
    KVCacheManager-->>Caller: task_handle stored in kvcache_metadata.kv_onload_handle

    Note over Caller: Overlap: embedding lookup

    Caller->>HSTUAttention: forward(..., kvcache_metadata)
    loop Per layer
        HSTUAttention->>HSTUAttention: stream_wait_layer(layer_idx)
        Note over HSTUAttention: Waits for layer KV data via CUDA event
        HSTUAttention->>HSTUAttention: hstu_attn_varlen_func(kv_seqlen_offsets)
    end

    Caller->>KVCacheManager: offload_try_wait()
    Caller->>KVCacheManager: offload_launch(index_meta, kvcache_metadata)
    KVCacheManager->>HostKVStorageManager: offload_kvcache_launch(...)
    Note over HostKVStorageManager: Async GPU-to-host transfer begins
Loading

Reviews (16): Last reviewed commit: "Add FlexKV as a third_party library" | Re-trigger Greptile

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Comment thread docker/Dockerfile Resolved
@shijieliu
shijieliu merged commit 2d8a0a0 into NVIDIA:main May 15, 2026
@geoffreyQiu

Copy link
Copy Markdown
Collaborator Author

ci

@shijieliu shijieliu mentioned this pull request May 19, 2026
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