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
13 changes: 13 additions & 0 deletions cpp/include/tensorrt_llm/batch_manager/cacheTransceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
#include "tensorrt_llm/runtime/utils/mpiUtils.h"
#include "tensorrt_llm/runtime/utils/pgUtils.h"
#include <cstddef>
#include <fstream>
#include <future>
#include <memory>
#include <mutex>
#include <optional>
#include <pybind11/pybind11.h>
#include <string>
#include <torch/csrc/jit/python/pybind_utils.h>
#include <torch/custom_class.h>
#include <torch/python.h>
Expand Down Expand Up @@ -305,6 +307,10 @@ class CacheTransceiver : public BaseCacheTransceiver

void setContextState(LlmRequest* llmRequest);

// Append one row per completed request to the gen-side transfer summary CSV. Opens the file
// lazily on first use; expects timing to already be synced across ranks by the caller.
void writeGenTransferSummary(std::vector<LlmRequest*> const& completedRequests);

std::unique_ptr<CacheSender> mCacheSender;
std::unique_ptr<CacheReceiver> mCacheReceiver;
// shared_ptr (not raw LlmRequest*) so the futures hold a strong reference for
Expand Down Expand Up @@ -340,6 +346,13 @@ class CacheTransceiver : public BaseCacheTransceiver
// TODO(shreyasm): update this to use same container as kv by using base trans buffers instead
std::unique_ptr<rnn_state_manager::RnnCacheTransBufferManager> mRnnCacheTransBufferManager{nullptr};

// Unique instance identifier for CSV file naming (avoids collisions across gen instances)
std::string mInstanceId;

// Gen-side transfer summary CSV (written after timing sync)
std::ofstream mGenTransferSummaryFile;
std::mutex mGenTransferSummaryMutex;

// library handle to the communicator related features,
// this is used to defer dependency resolution until needed.
static std::mutex mDllMutex;
Expand Down
29 changes: 20 additions & 9 deletions cpp/include/tensorrt_llm/batch_manager/llmRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ enum LlmRequestType

class ContextProgress;

// Process-global offset between the local steady clock and the global steady
// clock (rank 0's steady clock). The storage lives in a single translation unit
// (llmRequest.cpp) and is reached through this accessor so that
// libtensorrt_llm.so and the nanobind extension module share one copy across
// .so boundaries. An inline-static member would instead give each shared object
// its own copy, so an offset calibrated on one side would be invisible to the
// other.
std::optional<std::chrono::steady_clock::duration>& globalSteadyClockOffset();

template <typename TTensor, typename TStream = runtime::BufferManager::CudaStreamPtr>
class GenericLlmRequest
{
Expand Down Expand Up @@ -1857,14 +1866,18 @@ class GenericLlmRequest
mDecodingIter = iter;
}

// Callers must pass a global-steady-clock time point (getSteadyClockNow(),
// or a value merged from such time points). Normalizing again here would
// apply the global steady clock offset twice, which corrupts cross-node
// min/max merging whenever the offset is non-zero.
void setKvCacheTransferStart(TimePoint time) const
{
mPerfMetrics.timingMetrics.kvCacheTransferStart = maybeToGlobalSteadyClock(time);
mPerfMetrics.timingMetrics.kvCacheTransferStart = time;
}

void setKvCacheTransferEnd(TimePoint time) const
{
mPerfMetrics.timingMetrics.kvCacheTransferEnd = maybeToGlobalSteadyClock(time);
mPerfMetrics.timingMetrics.kvCacheTransferEnd = time;
}

TimePoint getKvCacheTransferStart() const
Expand Down Expand Up @@ -2040,8 +2053,8 @@ class GenericLlmRequest
return mUseDraftModel;
}

// If sGlobalSteadyClockOffset is set, return a global steady clock time point, otherwise return local steady clock
// time point
// If the global steady clock offset is set, return a global steady clock time point, otherwise return local steady
// clock time point
[[nodiscard]] static TimePoint getSteadyClockNow()
{
return maybeToGlobalSteadyClock(std::chrono::steady_clock::now());
Expand Down Expand Up @@ -2071,9 +2084,6 @@ class GenericLlmRequest
// current position of the prompt tuning table (only used in chunked prefill mode)
SizeType32 mPtableCurrentPosition{0};

// The offset between local steady clock and global steady clock (at rank 0)
inline static std::optional<Duration> sGlobalSteadyClockOffset{std::nullopt};

protected:
bool mIsStreaming;

Expand Down Expand Up @@ -2385,9 +2395,10 @@ class GenericLlmRequest

static TimePoint maybeToGlobalSteadyClock(TimePoint const& time_point)
{
if (sGlobalSteadyClockOffset.has_value())
auto const& offset = globalSteadyClockOffset();
if (offset.has_value())
{
return time_point + *sGlobalSteadyClockOffset;
return time_point + *offset;
}
return time_point;
}
Expand Down
Loading
Loading