-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[None][feat] Add NVFP4 as a cold-page KV Cache Compression Method #18091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Hudayday
merged 39 commits into
NVIDIA:main
from
Hudayday:nvfp4-pr17512-latest-e2e-clean-20260817
Sep 1, 2026
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
5731c07
[None][feat] port NVFP4 cold-page compression to latest KVCM2
Hudayday 6b502cd
[None][chore] Clarify KVCC lifecycle and test tiers
Hudayday 8bb2c3f
[None][fix] Polish NVFP4 cold-page compression
Hudayday fa8a37c
Merge remote-tracking branch 'upstream/main' into nvfp4-pr17512-lates…
Hudayday f593902
[None][test] Fix compression compatibility test setup
Hudayday eb4418c
Merge remote-tracking branch 'upstream/main' into nvfp4-pr17512-lates…
Hudayday 2f8e955
[None][fix] Tighten KV cache compression admission
Hudayday 8bb2ed4
[None][test] Fix KV cache compression CI routing
Hudayday d9fcc3c
[None][test] Preserve existing compression test routing
Hudayday 50db0dc
[None][refactor] Move NVFP4 cold-page policy to Python
Hudayday 1971d2f
[None][refactor] Split generic cold-page codec backend
Hudayday a2e177e
[None][test] Rely on KVCM buffer validation
Hudayday ba58b11
[None][refactor] Simplify native cold-page codec
Hudayday c0d6c43
[None][refactor] Dispatch cold-page compression through Python
Hudayday af6f49d
[None][refactor] Run cold-page methods through Python policy
Hudayday af8c3bf
Merge remote-tracking branch 'refs/remotes/upstream/main' into nvfp4-…
Hudayday 9fdfeb4
Merge remote-tracking branch 'upstream/main-latest' into nvfp4-pr1809…
Hudayday cf5315c
Merge remote-tracking branch 'upstream/main-latest' into nvfp4-pr1809…
Hudayday 06246cb
Merge remote-tracking branch 'refs/remotes/upstream/main-latest' into…
Hudayday 74b788c
[None][refactor] Remove duplicate cold-page callback ABI
Hudayday 5e3ad26
Merge remote-tracking branch 'refs/remotes/upstream/main-latest' into…
Hudayday e55e829
[None][refactor] Simplify cold-page quantization dispatch
Hudayday 37509a7
[None][refactor] Dispatch cold-page methods through Python
Hudayday c7bc1c6
Merge remote-tracking branch 'refs/remotes/upstream/main-latest' into…
Hudayday 9a12ce8
[None][refactor] Keep cold-page quantization base concrete
Hudayday ff0f406
[None][build] Scope cold-page Torch op definition to its source
Hudayday 3512a2e
[None][refactor] Simplify cold-page quantization callbacks
Hudayday f651c08
[None][fix] Finalize cold-page Python callback contract
Hudayday 4f2070d
[None][refactor] Expose cold-page quantization hooks
Hudayday 0e4d4e1
[None][refactor] Separate cold-page codec state
Hudayday 9a9bf57
[None][fix] Add complete cold-page codec license headers
Hudayday 559b6bc
[None][refactor] Address cold-page review feedback on build layout an…
Hudayday 58ec834
[None][fix] Fix a sign comparison in the cold-page codec bridge
Hudayday e2c8284
[None][fix] Initialize a CUDA context in the HostMem forwarding test
Hudayday e5d6374
Merge branch 'main' of https://github.com/NVIDIA/TensorRT-LLM into nv…
Hudayday a7156ed
[None][infra] Build only the standalone KV-cache compression gtests i…
Hudayday 6822753
Merge branch 'main' of https://github.com/NVIDIA/TensorRT-LLM into nv…
Hudayday 30e2a5b
[None][refactor] Address cold-page test layout and HostMem review fee…
Hudayday ba7b21e
DCO Remediation Commit for tianruih <tianruih@nvidia.com>
Hudayday File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
cpp/tensorrt_llm/batch_manager/kv_cache_compression/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. | ||
| # All rights reserved. SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| # use this file except in compliance with the License. You may obtain a copy of | ||
| # the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| # License for the specific language governing permissions and limitations under | ||
| # the License. | ||
|
|
||
| # Sources for the KVCM cold-page compression codec bridge. These are added to | ||
| # the tensorrt_llm_batch_manager_static target by the parent CMakeLists.txt. | ||
| set(KV_CACHE_COMPRESSION_SRCS kv_cache_compression/nativeColdPageCodec.cpp) |
298 changes: 298 additions & 0 deletions
298
cpp/tensorrt_llm/batch_manager/kv_cache_compression/nativeColdPageCodec.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,298 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "tensorrt_llm/batch_manager/kv_cache_compression/nativeColdPageCodec.h" | ||
|
|
||
| #include "kv_cache_manager_v2/utils/hostMem.h" | ||
| #include "tensorrt_llm/common/logger.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <exception> | ||
| #include <set> | ||
| #include <stdexcept> | ||
| #include <utility> | ||
|
|
||
| namespace tensorrt_llm::kv_cache_compression | ||
| { | ||
| namespace | ||
| { | ||
|
|
||
| ResolvedHotLifecycle resolveLifecycle(kv::PoolGroupDesc const& gpuDesc, kv::SlotDescVariant const& variant) | ||
| { | ||
| ResolvedHotLifecycle result{variant.lifeCycleId, {}}; | ||
| for (kv::PoolIndex poolIndex{0}; poolIndex < variant.coalescedBuffers.size(); ++poolIndex) | ||
| { | ||
| auto const& coalesced = variant.coalescedBuffers.at(poolIndex); | ||
| auto const& pool = gpuDesc.pools.at(poolIndex); | ||
| std::size_t offset = 0; | ||
| for (auto const& bufferId : coalesced.bufferIds) | ||
| { | ||
| auto& layer = result.layers[bufferId.layerId]; | ||
| if (!layer | ||
| .emplace(bufferId.role, | ||
| ResolvedHotBuffer{pool.baseAddress + offset, pool.slotBytes, coalesced.singleBufferSize}) | ||
| .second) | ||
| { | ||
| throw std::invalid_argument("GPU lifecycle contains a duplicate buffer role"); | ||
| } | ||
| offset += coalesced.singleBufferSize; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| void drainAfterProviderFailure(cudaStream_t stream) noexcept | ||
| { | ||
| auto const status = cudaStreamSynchronize(stream); | ||
| if (status != cudaSuccess) | ||
| { | ||
| TLLM_LOG_ERROR("Cold-page provider rollback drain failed: %s", cudaGetErrorString(status)); | ||
| std::terminate(); | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| NativeColdPageCodec::NativeColdPageCodec(std::set<kv::LayerId> layerIds) | ||
| : mLayerIds(std::move(layerIds)) | ||
| { | ||
| } | ||
|
|
||
| bool NativeColdPageCodec::configure(kv::PoolGroupDesc const* gpuDescs, kv::PoolGroupIndex numGpuDescs) noexcept | ||
| { | ||
| try | ||
| { | ||
| auto losslessCodec = kv::createDefaultKvCacheColdPageCodec(); | ||
| if (!losslessCodec->configure(gpuDescs, numGpuDescs)) | ||
| { | ||
| throw std::invalid_argument("Default lossless codec rejected GPU layouts"); | ||
| } | ||
|
|
||
| std::map<kv::LayerGroupId, LayerGroupState> pendingGroups; | ||
| std::vector<ResolvedHotLifecycle> providerLifecycles; | ||
| std::set<kv::LayerId> consumedLayers; | ||
|
|
||
| for (kv::PoolGroupIndex poolGroupIndex{0}; poolGroupIndex < numGpuDescs; ++poolGroupIndex) | ||
| { | ||
| auto const& gpuDesc = gpuDescs[kv::toSizeT(poolGroupIndex)]; | ||
| for (auto const& variant : gpuDesc.slotDesc.variants) | ||
| { | ||
| auto resolved = resolveLifecycle(gpuDesc, variant); | ||
| auto const providerLayerCount = static_cast<std::size_t>(std::count_if(resolved.layers.begin(), | ||
| resolved.layers.end(), [this](auto const& layer) { return mLayerIds.count(layer.first) != 0U; })); | ||
|
|
||
| LayerGroupState state; | ||
| if (providerLayerCount == 0U) | ||
| { | ||
| state.coldPageBytes = losslessCodec->queryColdPageBytes(variant.lifeCycleId); | ||
| state.pageIndexLocation = losslessCodec->queryPageIndexLocation(variant.lifeCycleId); | ||
| } | ||
| else | ||
| { | ||
| if (providerLayerCount != resolved.layers.size()) | ||
| { | ||
| throw std::invalid_argument("A lifecycle cannot mix provider-owned and fallback layers"); | ||
| } | ||
| for (auto const& [layerId, buffers] : resolved.layers) | ||
| { | ||
| static_cast<void>(buffers); | ||
| if (!consumedLayers.emplace(layerId).second) | ||
| { | ||
| throw std::invalid_argument("A provider layer appears in multiple lifecycles"); | ||
| } | ||
| } | ||
| state.lifecycleIndex = providerLifecycles.size(); | ||
| providerLifecycles.push_back(std::move(resolved)); | ||
| } | ||
|
|
||
| if (!pendingGroups.emplace(variant.lifeCycleId, std::move(state)).second) | ||
| { | ||
| throw std::invalid_argument("GPU lifecycle ID appears in multiple pool groups"); | ||
| } | ||
| } | ||
| } | ||
| if (consumedLayers != mLayerIds) | ||
| { | ||
| throw std::invalid_argument("A provider layer is absent from all GPU descriptors"); | ||
| } | ||
|
|
||
| // Fail closed until KVCM replaces the batched cuMemcpyBatchAsync copies with kernels: on host | ||
| // kernels that need chunked pinned-memory registration (Linux 6.11-6.13), the embedded lossless | ||
| // codec cannot split its copies at registration boundaries when wrapped by this codec. | ||
| bool hasFallbackLifecycle = false; | ||
| for (auto const& [lifeCycleId, state] : pendingGroups) | ||
| { | ||
| static_cast<void>(lifeCycleId); | ||
| if (!state.lifecycleIndex) | ||
| { | ||
| hasFallbackLifecycle = true; | ||
| break; | ||
| } | ||
| } | ||
| if (hasFallbackLifecycle && kv::HostMem::shouldUseChunkedRegistration()) | ||
| { | ||
| throw std::invalid_argument( | ||
| "Cold-page compression is not supported for models with lossless-fallback lifecycles (SSM/GDN) on " | ||
| "this host kernel: chunked pinned-memory registration (Linux 6.11-6.13) breaks the fallback codec's " | ||
| "batched copies. Disable KV cache compression for this model or use a different host kernel."); | ||
| } | ||
|
|
||
| auto const properties = configureProvider(providerLifecycles); | ||
| if (properties.size() != providerLifecycles.size()) | ||
| { | ||
| throw std::invalid_argument("Cold-page provider returned an unexpected lifecycle count"); | ||
| } | ||
| for (std::size_t index = 0; index < properties.size(); ++index) | ||
| { | ||
| auto const& lifecycle = properties[index]; | ||
| if (lifecycle.coldPageBytes == 0U || lifecycle.pageIndexLocation == kv::PageIndexLocation::kBadLocation) | ||
| { | ||
| throw std::invalid_argument("Cold-page provider returned invalid storage properties"); | ||
| } | ||
| auto& state = pendingGroups.at(providerLifecycles[index].lifeCycleId); | ||
| state.coldPageBytes = lifecycle.coldPageBytes; | ||
| state.pageIndexLocation = lifecycle.pageIndexLocation; | ||
| } | ||
|
|
||
| mLayerGroups = std::move(pendingGroups); | ||
| mLosslessCodec = std::move(losslessCodec); | ||
| return true; | ||
| } | ||
| catch (std::exception const& error) | ||
| { | ||
| TLLM_LOG_ERROR("NativeColdPageCodec::configure rejected GPU layouts: %s", error.what()); | ||
| return false; | ||
| } | ||
| catch (...) | ||
| { | ||
| TLLM_LOG_ERROR("NativeColdPageCodec::configure rejected GPU layouts: unknown error"); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| NativeColdPageCodec::LayerGroupState const* NativeColdPageCodec::findLayerGroup( | ||
| kv::LayerGroupId layerGroupId) const noexcept | ||
| { | ||
| auto const found = mLayerGroups.find(layerGroupId); | ||
| return found == mLayerGroups.end() ? nullptr : &found->second; | ||
| } | ||
|
|
||
| std::size_t NativeColdPageCodec::queryColdPageBytes(kv::LayerGroupId layerGroupId) const noexcept | ||
| { | ||
| auto const* state = findLayerGroup(layerGroupId); | ||
| return state == nullptr ? 0U : state->coldPageBytes; | ||
| } | ||
|
|
||
| kv::LayerGroupId NativeColdPageCodec::getBatchingLayerGroupId(kv::LayerGroupId layerGroupId) const noexcept | ||
| { | ||
| return findLayerGroup(layerGroupId) == nullptr ? kv::LayerGroupId{-1} : layerGroupId; | ||
| } | ||
|
|
||
| kv::PageIndexLocation NativeColdPageCodec::queryPageIndexLocation(kv::LayerGroupId layerGroupId) const noexcept | ||
| { | ||
| auto const* state = findLayerGroup(layerGroupId); | ||
| return state == nullptr ? kv::PageIndexLocation::kBadLocation : state->pageIndexLocation; | ||
| } | ||
|
|
||
| bool NativeColdPageCodec::encode(kv::LayerGroupId layerGroupId, void* dstBasePtr, kv::PageIndexPair const* pageIndices, | ||
| std::size_t numBasePages, cudaStream_t stream) noexcept | ||
| { | ||
| bool providerStarted = false; | ||
| try | ||
| { | ||
| auto const* state = findLayerGroup(layerGroupId); | ||
| if (state == nullptr || (numBasePages != 0U && (dstBasePtr == nullptr || pageIndices == nullptr))) | ||
| { | ||
| throw std::invalid_argument("encode received an invalid lifecycle or Page batch"); | ||
| } | ||
| if (numBasePages == 0U) | ||
| { | ||
| return true; | ||
| } | ||
| if (!state->lifecycleIndex) | ||
| { | ||
| return mLosslessCodec->encode(layerGroupId, dstBasePtr, pageIndices, numBasePages, stream); | ||
| } | ||
| providerStarted = true; | ||
| encodeProvider(*state->lifecycleIndex, dstBasePtr, pageIndices, numBasePages, stream); | ||
| return true; | ||
| } | ||
| catch (std::exception const& error) | ||
| { | ||
| if (providerStarted) | ||
| { | ||
| drainAfterProviderFailure(stream); | ||
| } | ||
| TLLM_LOG_ERROR("NativeColdPageCodec::encode failed before completion fencing: %s", error.what()); | ||
| return false; | ||
| } | ||
| catch (...) | ||
| { | ||
| if (providerStarted) | ||
| { | ||
| drainAfterProviderFailure(stream); | ||
| } | ||
| TLLM_LOG_ERROR("NativeColdPageCodec::encode failed before completion fencing: unknown error"); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| bool NativeColdPageCodec::decode(kv::LayerGroupId layerGroupId, void const* srcBasePtr, | ||
| kv::PageIndexPair const* pageIndices, std::size_t numBasePages, cudaStream_t stream) noexcept | ||
| { | ||
| bool providerStarted = false; | ||
| try | ||
| { | ||
| auto const* state = findLayerGroup(layerGroupId); | ||
| if (state == nullptr || (numBasePages != 0U && (srcBasePtr == nullptr || pageIndices == nullptr))) | ||
| { | ||
| throw std::invalid_argument("decode received an invalid lifecycle or Page batch"); | ||
| } | ||
| if (numBasePages == 0U) | ||
| { | ||
| return true; | ||
| } | ||
| if (!state->lifecycleIndex) | ||
| { | ||
| return mLosslessCodec->decode(layerGroupId, srcBasePtr, pageIndices, numBasePages, stream); | ||
| } | ||
| providerStarted = true; | ||
| decodeProvider(*state->lifecycleIndex, srcBasePtr, pageIndices, numBasePages, stream); | ||
| return true; | ||
| } | ||
| catch (std::exception const& error) | ||
| { | ||
| if (providerStarted) | ||
| { | ||
| drainAfterProviderFailure(stream); | ||
| } | ||
| TLLM_LOG_ERROR("NativeColdPageCodec::decode failed before completion fencing: %s", error.what()); | ||
| return false; | ||
| } | ||
| catch (...) | ||
| { | ||
| if (providerStarted) | ||
| { | ||
| drainAfterProviderFailure(stream); | ||
| } | ||
| TLLM_LOG_ERROR("NativeColdPageCodec::decode failed before completion fencing: unknown error"); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| } // namespace tensorrt_llm::kv_cache_compression | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.