Skip to content

Refactor KV Cache Manager with a new torch exportable KVCache backend - #400

Merged
shijieliu merged 31 commits into
NVIDIA:mainfrom
geoffreyQiu:aoti_kvcache
Jul 23, 2026
Merged

shijieliu merged 31 commits into
NVIDIA:mainfrom
geoffreyQiu:aoti_kvcache

Conversation

@geoffreyQiu

@geoffreyQiu geoffreyQiu commented May 20, 2026 •

Copy link
Copy Markdown
Collaborator

Description

  • Refactor KV Cache Manager with a new torch exportable KVCache backend
  • Implement KV Cache custom ops for torch export and AOTInductor.
  • Add PyTorch & C++ torch runtime example for exported aoti HSTU model inference with KVCache.
  • Add triton server deployment example for exported aoti HSTU model inference with KVCache.

Checklist

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

@geoffreyQiu
geoffreyQiu marked this pull request as ready for review June 30, 2026 14:13
@greptile-apps

greptile-apps Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR refactors the KV cache manager by introducing a KVCacheBackend ABC and a DefaultKVCacheBackend implementation, moving the original KVCacheManager logic into the backend layer. It also adds a torch-exportable C++ KV cache runtime (ExportKVCacheRuntime) with matching custom ops, fake-op shims for torch.export/AOTInductor tracing, and end-to-end inference examples using AOTI and Triton Server.

  • KVCacheManager becomes a thin facade; all logic moves to DefaultKVCacheBackend; backward-compat aliases (GPUKVCacheManager, HostKVStorageManagerBase) are preserved.
  • New C++ layer (kvcache_manager_ops.cpp, export_kvcache_runtime.cpp, kvcache_manager_context.cpp) implements lookup/allocate/onboard/offload ops as torch custom ops with both CUDA and CPU dispatch registrations.
  • fake_kvcache_manager_ops.py registers shape-only shims for every op; YAML config is loaded lazily on first invocation, fixing the previous import-time crash when the env var was absent.

Confidence Score: 5/5

Safe to merge. The previous blocking issues in ExportKVCacheBackend are confirmed deprecated and removed from the public import map; the fake-op registration no longer crashes on import; and the KVCacheBackend ABC now declares all required methods.

All previously flagged runtime-crashing defects have been resolved or are in a deprecated code path that is no longer reachable. The new backend abstraction, C++ export runtime, and fake-op shims are structurally sound. The only findings in this pass are a misleading inline comment in the offload reap path, a duplicate null guard, and the absence of a deadline in the flush loop — none of these affect correctness in the happy path.

corelib/recsys_kvcache_manager/src/runtime/export_kvcache_runtime.cpp and corelib/recsys_kvcache_manager/src/torch_binding/kvcache_manager_ops.cpp

Important Files Changed

Filename Overview
corelib/recsys_kvcache_manager/src/torch_binding/kvcache_manager_ops.cpp New file: registers all KV cache custom ops for torch export/AOTI. Both CUDA and CPU dispatch keys map to the same C++ implementations. offload_flush_impl has no timeout, risking an indefinite hang if the FlexKV server stops responding.
corelib/recsys_kvcache_manager/src/runtime/export_kvcache_runtime.cpp New file: C++ implementation of the exportable KV cache runtime. Contains a misleading continue comment inside the UNREADY branch of offload_kvcache_reap_completed and a duplicate null-guard in onboard_kvcache_wait. Core GPU page allocation and FlexKV client integration looks structurally sound.
corelib/recsys_kvcache_manager/src/torch_binding/kvcache_manager_context.cpp New file: singleton context for the KV cache runtime. kvcache_manager_ is thread_local so each new OS thread initialises its own ExportKVCacheRuntime rather than sharing one. Developer has acknowledged this is the intended design choice.
corelib/recsys_kvcache_manager/recsys_kvcache_manager/init.py Refactored to lazy imports, on-demand .so discovery, and conditional fake-op registration. The ExportKVCacheBackend entry has been removed. YAML config loading is now deferred to first fake-op invocation, fixing the previous import-time crash.
corelib/recsys_kvcache_manager/recsys_kvcache_manager/kvcache_manager.py Slimmed down to a thin facade over KVCacheBackend. Backward-compat gpu_kvcache_mgr and host_kvstorage_manager properties delegate to the backend. offload_reap_completed and offload_flush forwarding methods are now present.
corelib/recsys_kvcache_manager/recsys_kvcache_manager/kvcache_backend.py New ABC defining the full KV cache backend contract including offload_reap_completed and offload_flush. Clean, no issues found.
corelib/recsys_kvcache_manager/recsys_kvcache_manager/default_kvcache_backend.py New file: implements all KVCacheBackend abstract methods, porting the previous KVCacheManager logic faithfully.
corelib/recsys_kvcache_manager/recsys_kvcache_manager/fake_kvcache_manager_ops.py New file: shape-only fake implementations for all kvcache_manager_ops. YAML config is loaded lazily. Tensor shapes and dtypes are consistent with the C++ return values.
examples/hstu/model/export_kvcached_inference_ranking_gr.py New forward pass using torch custom ops for KV cache management. Op argument order matches the C++ schema definitions.
examples/hstu/modules/paged_hstu_infer_layer.py KV cache path routes through hstu_attn_export_impl. The sm_major_version guard now covers sm80 and sm120. The non-KV path still uses hstu_attn_varlen_func correctly.

Reviews (23): Last reviewed commit: "Fix review errors" | Re-trigger Greptile

@shijieliu

shijieliu commented Jul 16, 2026 •

Copy link
Copy Markdown
Collaborator
  1. move GUIDE_TO_RUN_AOTI_MODEL_INFERENCE_E2E.md as README.md
  2. add a benchmark folder under inference_aoti and move aoti vs. python and aoti e2e benchmark results into it.

@shijieliu

Copy link
Copy Markdown
Collaborator

about the fbgemm update, i think main branch has some update. make sure it does not reverse the changes on main.

Introduce KVCacheBackend, split the default Python backend from the public KVCacheManager facade, and add the initial export lookup path.
* Support PyTorch export and AOTI compile/package with test coverage.
* Support C++ Torch runtime validation for AOTI-compiled KV-cache graphs.
* Support Triton Inference Server validation using the PyTorch AOTI backend.
* Add required KV-cache/runtime/custom-op plumbing and export fake-op shims.
* extract and simplify HSTU KV-cache export wrapper
* remove stale debug instrumentation and export bring-up prints
* standardize macro-gated logging in KV-cache runtime/binding/client code
* rename AOTI ordering placeholder tensors consistently
* clean C++ replay demo logging and CUDA check behavior
* document end-to-end HSTU KV-cache AOTI C++ and Triton Server workflows
@geoffreyQiu

Copy link
Copy Markdown
Collaborator Author

/build fbgemm devel

@JacoCheung

JacoCheung commented Jul 17, 2026 •

Copy link
Copy Markdown
Collaborator

❔ Pipeline #58380880 -- canceling

Job Status Log
build_devel_x86 ❔ canceling view
build_devel_arm64 ❔ canceling view
build_tritonserver_devel_x86 ❔ canceling view
build_tritonserver_devel_arm64 ❌ failed view
manifest_devel 🚫 canceled view
manifest_tritonserver_devel 🚫 canceled view
pre_check 🚫 canceled view
train_build_x86 🚫 canceled view
train_build_arm64 🚫 canceled view
tritonserver_build_x86 🚫 canceled view
prepare-jet-b200-smoke 🚫 canceled view
prepare-jet-b200-inference 🚫 canceled view
prepare-jet-cw-dfw-e2e-benchmark 🚫 canceled view
build_whl 🚫 canceled view
dynamicemb_test_fwd_bwd_8gpus 🚫 canceled view
dynamicemb_test_load_dump_8gpus 🚫 canceled view
unit_test_1gpu_a100 🚫 canceled view
unit_test_1gpu_h100 🚫 canceled view
unit_test_4gpu 🚫 canceled view
unit_test_tp_4gpu 🚫 canceled view
L20_unit_test_1gpu 🚫 canceled view
inference_unit_test_1gpu 🚫 canceled view
inference_test_1gpu 🚫 canceled view

View full pipeline

@geoffreyQiu

Copy link
Copy Markdown
Collaborator Author

Final CI: pipeline
Re:

  1. Renamed and reorged the Readme file in inference_aoti.
  2. Add specific benchmark page (inference_aoti/benchmark/README.md) for inference_aoti
  3. FBGemm HSTU update:merged into tracking branch recsys-examples-dev.

@geoffreyQiu

Copy link
Copy Markdown
Collaborator Author

@shijieliu
shijieliu merged commit 43465ae into NVIDIA:main Jul 23, 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