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
16 changes: 11 additions & 5 deletions cpp/include/tensorrt_llm/batch_manager/cacheTransceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ namespace tensorrt_llm::batch_manager

class ContextProgress;
class BaseCacheTransceiver;
class DataResponder;
class DataRequester;

namespace kv_cache_manager
{
class BaseKVCacheManager;
} // namespace kv_cache_manager

class CacheSender;
class CacheReceiver;

class CacheTransceiverFactory
{
Expand Down Expand Up @@ -110,9 +116,9 @@ class CacheTransceiver : public BaseCacheTransceiver

void setContextState(LlmRequest* llmRequest);

std::unique_ptr<DataResponder> mDataResponder;
std::unique_ptr<DataRequester> mDataRequester;
std::vector<std::pair<LlmRequest*, std::future<void>>> mResponderFutures;
std::unique_ptr<CacheSender> mCacheSender;
std::unique_ptr<CacheReceiver> mCacheReceiver;
std::vector<std::pair<LlmRequest*, std::future<void>>> mSenderFutures;
std::vector<std::pair<LlmRequest*, std::future<void>>> mRequesterFutures;
mpi::MpiComm const *mMpiGroupComm{nullptr}, *mMpiWorldComm{nullptr};
std::shared_ptr<mpi::MpiComm> mMpiGroupTensorParaComm, mMpiGroupPipeParaComm, mMpiGroupDataComm,
Expand Down
1 change: 0 additions & 1 deletion cpp/tensorrt_llm/batch_manager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ set(SRCS
createNewDecoderRequests.cpp
contextProgress.cpp
dataTransceiver.cpp
dataTransceiverImpl.cpp
decoderBuffers.cpp
encoderBuffers.cpp
guidedDecoder.cpp
Expand Down
5 changes: 3 additions & 2 deletions cpp/tensorrt_llm/batch_manager/cacheFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "mlaCacheFormatter.h"

#include "tensorrt_llm/batch_manager/contextProgress.h"
#include "tensorrt_llm/batch_manager/dataTransceiver.h"
#include "tensorrt_llm/batch_manager/kvCacheUtils.h"
#include "tensorrt_llm/common/assert.h"
#include "tensorrt_llm/common/cudaUtils.h"
Expand Down Expand Up @@ -154,7 +155,7 @@ std::vector<size_t> CacheFormatter::pickRecvConnections(
return ret;
}

void CacheFormatter::format(TransferSession& session)
void CacheFormatter::format(tensorrt_llm::batch_manager::TransferSession& session)
{
NVTX3_SCOPED_RANGE(CacheFormatter_format);
auto const& llmRequest = session.getLlmRequest();
Expand Down Expand Up @@ -468,7 +469,7 @@ void CacheFormatter::format(TransferSession& session)
mpi::MpiComm::world().getRank(), "End the sending of KV cache for the request ID:%ld ", llmRequest.mRequestId);
}

void CacheFormatter::unformat(TransferSession& session)
void CacheFormatter::unformat(tensorrt_llm::batch_manager::TransferSession& session)
{
NVTX3_SCOPED_RANGE(CacheFormatter_unformat);
auto const& llmRequest = session.getLlmRequest();
Expand Down
27 changes: 21 additions & 6 deletions cpp/tensorrt_llm/batch_manager/cacheFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,38 @@
#pragma once

#include "cacheTransBuffer.h"
#include "dataTransceiver.h"
#include "tensorrt_llm/batch_manager/kvCacheManager.h"
#include "tensorrt_llm/batch_manager/kvCacheUtils.h"
#include "tensorrt_llm/common/assert.h"
#include "tensorrt_llm/common/envUtils.h"
#include "tensorrt_llm/common/logger.h"
#include "tensorrt_llm/executor/cacheCommunicator.h"
#include "tensorrt_llm/executor/cache_transmission/cacheSplitConcat.h"
#include "tensorrt_llm/executor/dataTransceiverState.h"
#include "tensorrt_llm/runtime/bufferManager.h"
#include "tensorrt_llm/runtime/utils/mpiUtils.h"
#include <NvInferRuntimeBase.h>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <vector>

// Forward declare TransferSession in the correct global namespace scope
namespace tensorrt_llm::batch_manager
{
class TransferSession;
}

namespace tensorrt_llm::batch_manager::kv_cache_manager
{

using DataContext = tensorrt_llm::executor::kv_cache::DataContext;
using Connection = tensorrt_llm::executor::kv_cache::Connection;
using SizeType32 = tensorrt_llm::runtime::SizeType32;
using BaseKVCacheManager = kv_cache_manager::BaseKVCacheManager;
using CacheTransBufferManager = kv_cache_manager::CacheTransBufferManager;
using BlockRange = kv_cache_manager::BlockRange;

BlockRange getBlockRangeForSending(BaseKVCacheManager* cacheManager, LlmRequest const& llmRequest);

BlockRange getBlockRangeForReceiving(BaseKVCacheManager* cacheManager, LlmRequest const& llmRequest);
Expand All @@ -42,16 +58,15 @@ BlockRange getBlockRangeForReceiving(BaseKVCacheManager* cacheManager, LlmReques
class BaseCacheFormatter
{
public:
using SizeType32 = tensorrt_llm::runtime::SizeType32;
using CacheState = executor::kv_cache::CacheState;

/// @brief Format the cache data into bytes for sending.
/// @param session The transfer session.
virtual void format(TransferSession& session) = 0;
virtual void format(tensorrt_llm::batch_manager::TransferSession& session) = 0;

/// @brief Unformat the cache data from received bytes.
/// @param session The transfer session.
virtual void unformat(TransferSession& session) = 0;
virtual void unformat(tensorrt_llm::batch_manager::TransferSession& session) = 0;

/// @brief Determine whether the sender is applicable to the source and target.
/// @param selfConfig Source data arrangement.
Expand Down Expand Up @@ -91,9 +106,9 @@ class CacheFormatter final : public BaseCacheFormatter
TLLM_CHECK(mCacheTransBufferManager);
}

void format(TransferSession& session) override;
void format(tensorrt_llm::batch_manager::TransferSession& session) override;

void unformat(TransferSession& session) override;
void unformat(tensorrt_llm::batch_manager::TransferSession& session) override;

[[nodiscard]] bool inquireSupport(CacheState const& selfConfig, CacheState const& destConfig) const override;

Expand Down
37 changes: 17 additions & 20 deletions cpp/tensorrt_llm/batch_manager/cacheTransceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
#include "tensorrt_llm/batch_manager/cacheFormatter.h"
#include "tensorrt_llm/batch_manager/cacheTransceiver.h"
#include "tensorrt_llm/batch_manager/contextProgress.h"
#include "tensorrt_llm/batch_manager/dataTransceiverImpl.h"
#include "tensorrt_llm/batch_manager/kvCacheManager.h"
#include "tensorrt_llm/batch_manager/kvCacheType.h"
#include "tensorrt_llm/batch_manager/kvCacheUtils.h"
#include "tensorrt_llm/batch_manager/llmRequest.h"
#include "tensorrt_llm/batch_manager/mlaCacheFormatter.h"
#include "tensorrt_llm/common/envUtils.h"
Expand Down Expand Up @@ -116,7 +117,6 @@ CacheTransceiver::CacheTransceiver(kv_cache_manager::BaseKVCacheManager* cacheMa
: mMpiGroupComm(std::addressof(tensorrt_llm::mpi::MpiComm::session()))
, mCacheTransceiverConfig{cacheTransceiverConfig}
{
using tensorrt_llm::batch_manager::kv_cache_manager::CacheFormatter;
if (worldConfig.isPipelineParallel())
{
mMpiGroupPipeParaComm = std::make_shared<tensorrt_llm::mpi::MpiComm>(
Expand Down Expand Up @@ -200,14 +200,12 @@ CacheTransceiver::CacheTransceiver(kv_cache_manager::BaseKVCacheManager* cacheMa
TLLM_THROW("Unsupported cache transceiver backend type ");
}

using tensorrt_llm::batch_manager::kv_cache_manager::MLACacheFormatter;
auto makeFormatter = [cacheManager, isMLA, this]()
{ return createCacheFormatter(cacheManager, mCacheTransBufferManager.get(), isMLA); };

mDataResponder = std::make_unique<DataResponder>(
std::make_unique<DataSenderImpl>(mManager.get(), *mCacheState, worldConfig.getRank(), makeFormatter()));
mDataRequester = std::make_unique<DataRequester>(
std::make_unique<DataReceiverImpl>(mManager.get(), *mCacheState, worldConfig.getRank(), makeFormatter()));
mCacheSender = std::make_unique<CacheSender>(mManager.get(), *mCacheState, worldConfig.getRank(), makeFormatter());
mCacheReceiver
= std::make_unique<CacheReceiver>(mManager.get(), *mCacheState, worldConfig.getRank(), makeFormatter());

initializeCommState();
}
Expand All @@ -223,7 +221,7 @@ CacheTransceiver::~CacheTransceiver()

void CacheTransceiver::initializeCommState()
{
mCommState = std::addressof(mDataResponder->getCommState());
mCommState = std::addressof(mCacheSender->getCommState());
}

void CacheTransceiver::setContextState(LlmRequest* llmRequest)
Expand Down Expand Up @@ -259,8 +257,8 @@ void CacheTransceiver::respondAndSendAsync(LlmRequest* llmRequest)
return;
}
setContextState(llmRequest);
auto future = mDataResponder->respondAndSendAsync(*llmRequest);
mResponderFutures.emplace_back(llmRequest, std::move(future));
auto future = mCacheSender->sendAsync(*llmRequest);
mSenderFutures.emplace_back(llmRequest, std::move(future));
}

void CacheTransceiver::respondAndSendLayerWise(
Expand All @@ -275,16 +273,16 @@ void CacheTransceiver::respondAndSendLayerWise(

llmRequest->setState(LlmRequestState::kDISAGG_CONTEXT_INIT_AND_TRANS);
setContextState(llmRequest.get());
auto future = mDataResponder->respondAndSendAsync(*llmRequest);
mResponderFutures.emplace_back(llmRequest.get(), std::move(future));
auto future = mCacheSender->sendAsync(*llmRequest);
mSenderFutures.emplace_back(llmRequest.get(), std::move(future));
}
}

void CacheTransceiver::requestAndReceiveSync(LlmRequest* llmRequest)
{
TLLM_CHECK(llmRequest && llmRequest->isGenerationOnlyRequest());
{
auto future = mDataRequester->requestAndReceiveAsync(*llmRequest);
auto future = mCacheReceiver->receiveAsync(*llmRequest);
future.get();
}
llmRequest->setState(LlmRequestState::kDISAGG_GENERATION_TRANS_COMPLETE);
Expand All @@ -302,7 +300,7 @@ void CacheTransceiver::requestAndReceiveAsync(LlmRequest* llmRequest)
return;
}

auto future = mDataRequester->requestAndReceiveAsync(*llmRequest);
auto future = mCacheReceiver->receiveAsync(*llmRequest);
mRequesterFutures.emplace_back(llmRequest, std::move(future));
llmRequest->setState(LlmRequestState::kDISAGG_GENERATION_TRANS_IN_PROGRESS);
}
Expand Down Expand Up @@ -382,7 +380,7 @@ void CacheTransceiver::checkContextTransferStatus(std::optional<int> const& atLe
bool blockAll = !atLeastRequestNum.has_value();
auto syncComm = mCacheState->getParallelConfig().mEnableAttentionDP ? mMpiGroupTPInDPComm : mMpiGroupTensorParaComm;
std::vector<LlmRequest::RequestIdType> contextCompleteRequestIds;
for (auto&& [request, future] : mResponderFutures)
for (auto&& [request, future] : mSenderFutures)
{
if (future.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready)
{
Expand Down Expand Up @@ -422,16 +420,15 @@ void CacheTransceiver::checkContextTransferStatus(std::optional<int> const& atLe

// Make sure there are at least atLeastRequestNum requests in toCompleteIdSet.
// This will preserve the order of insertion for KVCache transfer requests.
for (auto it = mResponderFutures.begin();
atLeastRequestNum.value_or(0) > static_cast<int>(toCompleteIdSet.size()) && it != mResponderFutures.end();
++it)
for (auto it = mSenderFutures.begin();
atLeastRequestNum.value_or(0) > static_cast<int>(toCompleteIdSet.size()) && it != mSenderFutures.end(); ++it)
{
auto& [request, future] = *it;
toCompleteIdSet.insert(request->mRequestId);
}

// Complete all the requests in toCompleteIdSet
for (auto it = mResponderFutures.begin(); it != mResponderFutures.end();)
for (auto it = mSenderFutures.begin(); it != mSenderFutures.end();)
{
auto& [request, future] = *it;
if (blockAll || (toCompleteIdSet.find(request->mRequestId) != toCompleteIdSet.end()))
Expand All @@ -447,7 +444,7 @@ void CacheTransceiver::checkContextTransferStatus(std::optional<int> const& atLe
"Error occurred during context transfer for request %ld: %s", request->mRequestId, e.what());
request->setState(LlmRequestState::kDISAGG_TRANS_ERROR);
}
it = mResponderFutures.erase(it);
it = mSenderFutures.erase(it);
}
else
{
Expand Down
Loading