Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cpp/include/tensorrt_llm/batch_manager/kvCacheManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@

namespace kvc = tensorrt_llm::executor::kv_cache;

namespace tensorrt_llm::batch_manager::kv_cache_manager
{
class FabricMemory;
} // namespace tensorrt_llm::batch_manager::kv_cache_manager

namespace tensorrt_llm::batch_manager::eviction_policy
{
class BaseEvictionPolicy;
Expand Down Expand Up @@ -1344,6 +1349,8 @@ class WindowBlockManager

// Buffer manager
runtime::BufferManager mBufferManager;
// Fabric memory backing for primary pools (MNNVL-capable allocation)
std::vector<std::unique_ptr<kv_cache_manager::FabricMemory>> mFabricMemoryPools;

// Used to keep track of number of free blocks during scheduling
SizeType32 mSchedulingNumFreeBlocks;
Expand Down
2 changes: 1 addition & 1 deletion cpp/tensorrt_llm/batch_manager/baseTransBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ BaseTransBufferManager::BaseTransBufferManager(
mRecvBufferCount = common::getEnvRequestKVCacheConcurrent() ? common::getEnvKVCacheRecvBufferCount() : 1;
mSendBufferCount = common::getEnvKVCacheSendMaxConcurrenceNum();
mUseFabricMemory = !(common::getEnvKVCacheTransferUseSyncBuffer() || common::getEnvKVCacheTransferUseAsyncBuffer())
&& kv_cache_manager::FabricMemory::supportFbaricMemory();
&& kv_cache_manager::FabricMemory::supportFabricMemory();
if (mUseFabricMemory)
{
mTransferBufferSize = kv_cache_manager::FabricMemory::getAlignedSize(mTransferBufferSize);
Expand Down
4 changes: 2 additions & 2 deletions cpp/tensorrt_llm/batch_manager/cacheTransBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ size_t FabricMemory::getAlignedSize(size_t size)
return (size + granularity - 1) / granularity * granularity;
}

bool FabricMemory::supportFbaricMemory()
bool FabricMemory::supportFabricMemory()
{
#ifdef __aarch64__
auto support_fun = []()
Expand Down Expand Up @@ -309,7 +309,7 @@ size_t CacheTransBufferManager::preAllocBufferSize(
transferBufferSize += validTokenNum * cacheSizeBytesPerToken;
}
}
bool useFabricMemory = FabricMemory::supportFbaricMemory()
bool useFabricMemory = FabricMemory::supportFabricMemory()
&& (!(common::getEnvKVCacheTransferUseSyncBuffer() || common::getEnvKVCacheTransferUseAsyncBuffer()));
if (useFabricMemory)
{
Expand Down
2 changes: 1 addition & 1 deletion cpp/tensorrt_llm/batch_manager/cacheTransBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class FabricMemory
size_t getSize() const;

static size_t getAlignedSize(size_t size);
static bool supportFbaricMemory();
static bool supportFabricMemory();

private:
class Impl;
Expand Down
46 changes: 45 additions & 1 deletion cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

#include "tensorrt_llm/batch_manager/kvCacheManager.h"

#include "tensorrt_llm/batch_manager/cacheTransBuffer.h"
#include "tensorrt_llm/batch_manager/common.h"
#include "tensorrt_llm/batch_manager/evictionPolicy.h"
#include "tensorrt_llm/batch_manager/kvCacheTransferManager.h"
#include "tensorrt_llm/batch_manager/radixBlockTree.h"
#include "tensorrt_llm/common/assert.h"
#include "tensorrt_llm/common/cudaUtils.h"
#include "tensorrt_llm/common/envUtils.h"
#include "tensorrt_llm/common/logger.h"
#include "tensorrt_llm/common/memoryUtils.h"
#include "tensorrt_llm/executor/executor.h"
Expand Down Expand Up @@ -1092,6 +1094,23 @@ void WindowBlockManager::allocatePools(bool useUvm)
{
constexpr nvinfer1::DataType kScaleDtypeNVFP4 = nvinfer1::DataType::kFP8;

bool const requestFabricMemory = tc::getEnvKVCachePoolUseFabricMemory();
bool const fabricMemorySupported = FabricMemory::supportFabricMemory();
if (requestFabricMemory && !fabricMemorySupported)
{
TLLM_LOG_WARNING(
"[%s] TRTLLM_KVCACHE_POOL_USE_FABRIC_MEMORY=1 was set but fabric memory is not supported on this "
"platform (FabricMemory::supportFabricMemory() returned false); falling back to standard GPU "
"allocation.",
mLogPrefix.c_str());
}
bool const useFabricMemory = requestFabricMemory && fabricMemorySupported;

if (useFabricMemory)
{
TLLM_LOG_INFO("[%s] KV cache pool using fabric memory for MNNVL support", mLogPrefix.c_str());
}

// Allocate a memory pool backing the blocks for each numKvHeads
// TODO(oargov): allocate pools in a single buffer and split it, to avoid fragmentation
for (auto& pool : mPools)
Expand Down Expand Up @@ -1124,9 +1143,27 @@ void WindowBlockManager::allocatePools(bool useUvm)
cacheShape.d[2], cacheShape.d[3], pool.layerFirstLayout ? " (layer-first)" : "");

if (useUvm)
{
pool.primaryPtr = BufferManager::managed(cacheShape, poolDtype);
}
else if (useFabricMemory)
{
auto const numElements = ITensor::volume(cacheShape);
auto const elementSize = tc::getDTypeSize(poolDtype);
auto const totalBytes = static_cast<size_t>(numElements) * elementSize;

// Record ownership before exposing the raw pointer: if FabricMemory's ctor throws nothing
// is wrapped; if ITensor::wrap throws afterwards, the unique_ptr in mFabricMemoryPools
// still owns and will free the allocation.
mFabricMemoryPools.reserve(mFabricMemoryPools.size() + 1);
mFabricMemoryPools.emplace_back(std::make_unique<FabricMemory>(totalBytes));
pool.primaryPtr = ITensor::wrap(mFabricMemoryPools.back()->getPtr(), poolDtype, cacheShape, numElements);
}
else
{
pool.primaryPtr = mBufferManager.gpuSync(cacheShape, poolDtype);
}

if (mNumSecondaryBlocks > 0)
{
nvinfer1::Dims cacheShapeOffload = isRecurrentState()
Expand All @@ -1149,6 +1186,12 @@ void BlockManager::releasePools()

void WindowBlockManager::releasePools()
{
if (mTransferManager)
{
mTransferManager->syncTransfers();
}
mBufferManager.getStream().synchronize();
Comment thread
chuangz0 marked this conversation as resolved.

for (auto& pool : mPools)
{
if (pool.primaryPtr)
Expand All @@ -1160,7 +1203,8 @@ void WindowBlockManager::releasePools()
pool.secondaryPtr->release();
}
}
mBufferManager.getStream().synchronize();
// Release fabric memory backing (must happen after ITensor release).
mFabricMemoryPools.clear();
mBufferManager.memoryPoolTrimTo(0);
}

Expand Down
2 changes: 1 addition & 1 deletion cpp/tensorrt_llm/batch_manager/rnnCacheTransBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ size_t RnnCacheTransBufferManager::preAllocBufferSize(
size_t transferBufferSize
= rnnStateSizeBytes > 0 ? rnnStateSizeBytes : common::getEnvMemSizeForKVCacheTransferBuffer();

bool useFabricMemory = kv_cache_manager::FabricMemory::supportFbaricMemory()
bool useFabricMemory = kv_cache_manager::FabricMemory::supportFabricMemory()
&& (!(common::getEnvKVCacheTransferUseSyncBuffer() || common::getEnvKVCacheTransferUseAsyncBuffer()));

if (useFabricMemory)
Expand Down
6 changes: 6 additions & 0 deletions cpp/tensorrt_llm/common/envUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,12 @@ bool getEnvKVCacheTransferAllBlocksForWindow()
return allBlocksForWindow;
}

bool getEnvKVCachePoolUseFabricMemory()
{
static bool const useFabricMemory = getBoolEnv("TRTLLM_KVCACHE_POOL_USE_FABRIC_MEMORY");
return useFabricMemory;
}

uint16_t getEnvNixlPort()
{
static uint16_t const nixlPort = getUInt64Env("TRTLLM_NIXL_PORT").value_or(0);
Expand Down
2 changes: 2 additions & 0 deletions cpp/tensorrt_llm/common/envUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ size_t getEnvKVCacheSendMaxConcurrenceNum();

size_t getEnvMemSizeForKVCacheTransferBuffer();

bool getEnvKVCachePoolUseFabricMemory();

uint16_t getEnvNixlPort();

bool getEnvNixlEnableCoalesce();
Expand Down
1 change: 1 addition & 0 deletions cpp/tests/unit_tests/batch_manager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ add_gtest(capacitySchedulerTest capacitySchedulerTest.cpp)
add_gtest(contextProgressTest contextProgressTest.cu)
add_gtest(evictionPolicyTest evictionPolicyTest.cpp)
add_gtest(kvCacheManagerTest kvCacheManagerTest.cpp)
add_gtest(kvCacheManagerFabricMemoryTest kvCacheManagerFabricMemoryTest.cpp)
add_gtest(kvCacheUtilsTest kvCacheUtilsTest.cpp)
add_gtest(llmRequestTest llmRequestTest.cpp)
add_gtest(microBatchSchedulerTest microBatchSchedulerTest.cpp)
Expand Down
Loading
Loading