Skip to content

[Stateful - M10] CUDA plugin KV cache backend - #34049

Draft
devang-ml wants to merge 3 commits into
stateful-M09-plugin-stateful-hooksfrom
stateful-M10-cuda-plugin-kv-cache
Draft

devang-ml wants to merge 3 commits into
stateful-M09-plugin-stateful-hooksfrom
stateful-M10-cuda-plugin-kv-cache

Conversation

@devang-ml

@devang-ml devang-ml commented Nov 10, 2026 •

Copy link
Copy Markdown

Description

Upstream draft stack 10/21. Original milestone: M08a.

The [Stateful - MNN] title/branch numbers identify positions in this 21-PR stack, not original milestone numbering. M16-M23 language-binding milestones remain deferred.

Historical review base: 1988019b6e23ac4def4628cddd603c1bfb504fe8, preserved as state/stateful-base-1988019b. The first PR targets that historical review anchor; each later PR targets its predecessor. These PRs have not been rebased onto current main. The anchor is for incremental review; integration/retargeting requires a later explicit decision.

Validation provenance: Recorded milestone validation below is retained. The appended reference-cleanup/merge commits change Markdown references only; executable files are byte-for-byte unchanged. No new build/test or current-main qualification is claimed. Historical commits are preserved, while current documentation and PR links use this Microsoft draft stack. Upstream CI results are separate.

M08a, stacked on #34048 (stateful-M09-plugin-stateful-hooks). This milestone implements the CUDA plugin EP cache backend only; M08b loop/chain device integration is not included or authorized yet.

  • Register six GPU cache operations: initialize, owned-output Share, Copy, arbitrary-sequence-axis Append, Reserve and Reset.
  • Support compact contiguous float32/float16/bfloat16 storage with retained immutable snapshots, compatible ORT allocators, synchronous default/user-stream completion and caller-device restoration.
  • Append a size-checked retain_value request callback instead of importing native Session types into the plugin. Public API tables and ORT_API_VERSION remain unchanged.
  • Preserve the leased owner when repeated Share results have the same Tensor pointer but a different OrtValue control block.
  • Check allocation geometry and nonempty allocation pointers before copying; report/consume CUDA errors at the callback boundary. Controlled failures require Reset and never retry on CPU.
  • Keep CPU as the default, legacy/shared CUDA native-cache capabilities absent, and GPU greedy sampling unavailable.
  • Wire native/public-only plugin test startup and V2 routing, including real public auto-selection and supported provider-option coverage.

The approved specification, milestone checklist, exact evidence and public SDK example are in SPEC.md, TASKS.md and SCRATCHPAD.md.

Validation

Linux/A100 SM80, matching CUDA 13.1/cuDNN; GPU runs expose devices 0 and 1. All reported suites have nonzero counts and zero skips/exclusions.

Configuration Exact command, from its build output directory Result
CPU ./onnxruntime_session_loop_test --gtest_output=xml:coverage/m08a/verified/native-tests.xml 63/63 passed
CPU ./onnxruntime_autoep_test --gtest_output=xml:coverage/m08a/verified/autoep-tests.xml 139/139 passed, unfiltered
CUDA plugin ./onnxruntime_session_loop_test --gtest_output=xml:coverage/m08a/complete/native-tests.xml 111/111 passed
CUDA plugin ./onnxruntime_autoep_test --gtest_output=xml:coverage/m08a/complete/autoep-tests.xml 174/174 passed, unfiltered
Legacy/shared CUDA ./onnxruntime_autoep_test --gtest_filter='AutoEpSelection.CudaEP:*Stateful*' --gtest_output=xml:coverage/m08a/legacy-stateful-tests.xml 43/43 passed; zero native-cache bits and exact required-cache ORT_NOT_IMPLEMENTED

The separate instrumented plugin target built successfully with:

cmake --compile-no-warning-as-error -S cmake -B build/state-cuda-plugin-13.1/RelWithDebInfo
cmake --build build/state-cuda-plugin-13.1/RelWithDebInfo --parallel 16 \
  --target onnxruntime_session_loop_test onnxruntime_autoep_test

Clang18 host LLVM instrumentation is combined with supported NVCC13.1/GCC13. The existing nonfatal-warning build option accommodates stock mixed-compiler/pool/Cast diagnostics; no unrelated CUDA implementation was patched to silence them. New callback/device-test sources independently pass strict warning-as-error type checking.

Actual-plugin new executable host/glue coverage: 137/137 (100%):

  • Callback/resource module: 123/123.
  • CUDA factory registration: 4/4.
  • Core retaining/owner glue: 10/10.

Fresh passing profiles were merged with llvm-profdata merge -sparse coverage/m08a/complete/*.profraw -o coverage/m08a/complete/m08a.profdata; module-specific LLVM line exports were intersected with added source locations against M07b 03d7476859. LLVM diagnostics were exclusively dormant zero-hash maps, with no new-function mismatch.

New device functions/lines: 0/0, N/A, not a claimed 100% device-coverage result. No kernels were added; the backend uses existing CUDA runtime copy APIs.

Compute Sanitizer commands, using CUDA 13.1's binary, all passed with zero errors and zero leaked bytes/allocations:

compute-sanitizer --tool memcheck --leak-check full --error-exitcode 99 \
  ./onnxruntime_autoep_test --gtest_filter='*CudaStateful*'
# 17/17 passed: all new cache cases, including two selected GPUs.

compute-sanitizer --tool memcheck --leak-check full --error-exitcode 99 \
  ./onnxruntime_session_loop_test \
  --gtest_filter='StatefulCudaProviderBoundaryTest.*:Cuda/StatefulProviderTest.*'
# 6/6 passed.

The documented C++17 CUDA SDK example also passed normal execution and memcheck through reset, backend-Session release and library unregistration. Its separate caller-owned allocator Session remains alive as required by the existing allocator API; final DLL unload while that caller resource is alive is not claimed. Genuine C11 compilation/execution, the CPU C++17 example with normal/no-exceptions client flags, formatting, 120-column limits, whitespace and all 93-requirement/24-milestone mappings passed.

Motivation and Context

Provide the first real device-resident KV implementation through M07's plugin dispatch contract, without changing stateless Session behavior or claiming GPU-native sampling. The tests prove actual GPU allocation/copy/retention rather than merely CUDA model registration, including completion before readback can hide a race and precise rejection of null-allocation/overflow failures before CUDA copies.

M08 remains incomplete until M08b is separately authorized and wires device-resident caches through loops/chains. This PR is independently reviewable and ready for human review; it does not merge its predecessors.

devang-ml and others added 2 commits October 19, 2026 03:59
Implement six synchronous device cache operations with retained storage, checked allocation geometry, explicit errors and plugin-only registration. Verify CPU and CUDA regressions, 137/137 new host lines, and sanitizer-clean standalone C/C++ device lifecycles.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Document PR #20 and completed implementation, CPU/CUDA validation, 137/137 host coverage and sanitizer gates. Keep M08b unimplemented pending user authorization.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace repository, PR and branch references with this upstream draft stack. Integrate the predecessor's documentation-only cleanup without rebasing or changing executable code. Existing implementation commits remain intact.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

1 participant