Skip to content

Commit e78c792

Browse files
[executorch][cuda] Add dynamic off-graph KV runtime manager (pytorch#23293)
This PR was created by the merge bot to help merge the original PR into the main branch. ghstack PR number: pytorch#22816 by @Gasoonjia ^ Please use this as the source of truth for the PR details, comments, and reviews ghstack PR base: https://github.com/pytorch/executorch/tree/gh/gasoonjia/186/base ghstack PR head: https://github.com/pytorch/executorch/tree/gh/gasoonjia/186/head Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/gasoonjia/185/orig Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/gasoonjia/186/orig Differential Revision: [D119571986](https://our.internmc.facebook.com/intern/diff/D119571986/) @diff-train-skip-merge --------- Co-authored-by: gasoonjia <gasoonjia@icloud.com>
1 parent 01bc772 commit e78c792

9 files changed

Lines changed: 1661 additions & 1 deletion

File tree

‎backends/cuda/CMakeLists.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@ set(_aoti_cuda_backend_sources
349349
runtime/cuda_backend.cpp runtime/cuda_mutable_state.cpp
350350
runtime/cuda_weight_cache.cpp
351351
)
352+
# The off-graph KV cache builds on the LLM extension's neutral cache.
353+
if(EXECUTORCH_BUILD_EXTENSION_LLM)
354+
list(APPEND _aoti_cuda_backend_sources runtime/cuda_kv_cache.cpp)
355+
endif()
352356
if(_cuda_is_msvc_toolchain)
353357
# MSVC links aoti_cuda_backend into portable_lib without relying on C++
354358
# symbols exported from aoti_cuda_shims.dll.
@@ -401,6 +405,9 @@ target_link_libraries(
401405
PUBLIC cuda_platform extension_tensor ${_cuda_runtime_libraries}
402406
extension_cuda ${CMAKE_DL_LIBS}
403407
)
408+
if(EXECUTORCH_BUILD_EXTENSION_LLM)
409+
target_link_libraries(aoti_cuda_backend PUBLIC extension_llm_cache)
410+
endif()
404411

405412
if(_cuda_is_msvc_toolchain)
406413
# cuda_backend.cpp uses SlimTensor CUDA utilities (e.g. getCurrentCUDAStream)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#pragma once
10+
11+
namespace executorch::backends::cuda {
12+
13+
// Shared identifiers for the CUDA backend. Included by the backend itself (to
14+
// register) and by callers/runners (to route backend options and to ask
15+
// CacheFactory for a builder), keeping the string literals in one place.
16+
17+
// Backend id under which the CUDA backend registers (see cuda_backend.cpp).
18+
inline constexpr char kCudaBackendId[] = "CudaBackend";
19+
20+
} // namespace executorch::backends::cuda

‎backends/cuda/runtime/cuda_backend.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ class ET_EXPERIMENTAL CudaBackend final
202202
AOTInductorModelContainerGetConstantOriginalFQN);
203203
LOAD_OPTIONAL_SYMBOL(
204204
get_constant_dtype, AOTInductorModelContainerGetConstantDtype);
205+
LOAD_OPTIONAL_SYMBOL(
206+
get_constant_data_size, AOTInductorModelContainerGetConstantDataSize);
205207
LOAD_OPTIONAL_SYMBOL(
206208
extract_constants_map, AOTInductorModelContainerExtractConstantsMap);
207209
LOAD_OPTIONAL_SYMBOL(

‎backends/cuda/runtime/cuda_delegate_handle.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ using AOTInductorModelContainerGetConstantDtypeFunc =
2626
aoti::AOTInductorModelContainerHandle container_handle,
2727
size_t idx,
2828
int32_t* dtype);
29+
using AOTInductorModelContainerGetConstantDataSizeFunc =
30+
aoti::AOTIRuntimeError (*)(
31+
aoti::AOTInductorModelContainerHandle container_handle,
32+
size_t idx,
33+
size_t* data_size);
2934
struct CudaWeightStorage {
3035
void* data{nullptr};
3136
size_t nbytes{0};
@@ -170,6 +175,10 @@ struct CudaGraphState {
170175
struct CudaDelegateHandle : public aoti::AOTIDelegateHandle {
171176
// Extra AOTI metadata used to validate per-FQN weights before binding.
172177
AOTInductorModelContainerGetConstantDtypeFunc get_constant_dtype{nullptr};
178+
// Bytes a constant's compiled shape spans; the off-graph KV cache checks its
179+
// storage against it before binding.
180+
AOTInductorModelContainerGetConstantDataSizeFunc get_constant_data_size{
181+
nullptr};
173182

174183
// The per-thread stream. Nothing owns it: the value is a fixed sentinel the
175184
// driver resolves to a different stream on each host thread, so releasing the

0 commit comments

Comments
 (0)