mirror of
https://github.com/NVIDIA/TensorRT-LLM.git
synced 2026-01-14 06:27:45 +08:00
* refactor: batch slot management in decoder classes - Changed `forwardBatchSlots` from a single `TensorPtr` to a `std::vector<TensorPtr>` in `decoderBuffers.h` and updated its initialization in `decoderBuffers.cpp`. - Updated `batchSlots` in `iGptDecoderBatched.h` to a `std::vector<TensorPtr>` for better handling of batch sizes. - Modified `mBatchSlotsDecoder` in `statefulGptDecoderBatched.h` to use a `std::vector<TensorPtr>` and adjusted its initialization in `statefulGptDecoderBatched.cpp`. - Ensured proper reshaping of tensors in the setup methods to accommodate the new vector structure. These changes enhance flexibility in managing tensor buffers across different batch sizes. Signed-off-by: Robin Kobus <19427718+Funatiq@users.noreply.github.com> * refactor: Setup batch slots outside of the decoder - Refactored batch slot management to utilize `makeBatchSlots`, enhancing clarity and functionality in batch processing. - Introduced `DecoderState` to `MakeDecodingBatchInputOutput` for improved state handling during decoding. - Updated the `operator()` method to include `decoderState` as a parameter, facilitating better integration with the decoding process. - Modified related tests to accommodate changes in batch slot handling and ensure proper functionality. These updates improve the overall structure and efficiency of the decoding process in the batch manager. Signed-off-by: Robin Kobus <19427718+Funatiq@users.noreply.github.com> * refactor: Enhance decoder input structure with maxDecodingEngineTokens - Updated the `Input` class in `iGptDecoderBatched.h` to include a new parameter `maxDecodingEngineTokens` for better control over decoding limits. - Modified the `MakeDecodingBatchInputOutput` algorithm to compute the maximum number of decoding tokens based on active slots. - Adjusted the `GptDecoderBatched` class to utilize the new `maxDecodingEngineTokens` parameter, improving clarity in token management during decoding. - Updated Python bindings to reflect changes in the `Input` class constructor. - Enhanced tests to ensure proper handling of the new parameter. These changes improve the flexibility and efficiency of the decoding process in the batch manager. Signed-off-by: Robin Kobus <19427718+Funatiq@users.noreply.github.com> * refactor: Streamline decoder input creation and batch slot management - Introduced a new function `createDecoderInputs` to encapsulate the logic for creating decoder inputs, improving code organization. - Updated the `operator()` method to utilize the new `createDecoderInputs` function, simplifying the decoding input setup process. - Removed the `maxOfActiveSlots` template function to streamline the logic for determining the maximum number of active decoding engine tokens. - Introduced a direct calculation of `maxActiveDecodingEngineTokens` within the `createDecoderInputs` function, enhancing clarity and reducing complexity. These changes enhance the maintainability and readability of the decoding process in the batch manager. Signed-off-by: Robin Kobus <19427718+Funatiq@users.noreply.github.com> * refactor: Update logits handling in decoder batch - Modified the `decoder_batch::Input` to accept a vector of vectors for logits, enhancing flexibility in tensor management. - Adjusted the `createDecoderInputs` function to accommodate the new logits structure, ensuring proper batch processing. - Updated Python bindings to reflect changes in the `Input` class constructor, maintaining compatibility with existing interfaces. - Refactored the `GptDecoderBatched` and `StatefulGptDecoderBatched` classes to utilize the updated logits structure, improving clarity in tensor slicing and batch size management. - Enhanced tests to validate the new input structure and ensure correct functionality across various decoding scenarios. These changes streamline the decoding process and improve the overall maintainability of the codebase. Signed-off-by: Robin Kobus <19427718+Funatiq@users.noreply.github.com> * refactor: Rename maxDecodingEngineTokens to maxDecoderSteps - Updated the `Input` class in `iGptDecoderBatched.h` to rename `maxDecodingEngineTokens` to `maxDecoderSteps` for improved clarity. - Adjusted the `createDecoderInputs` function to reflect the new naming, ensuring consistency in the decoding process. - Modified the `GptDecoderBatched` class to utilize `maxDecoderSteps` in its logic, enhancing readability and maintainability. - Updated Python bindings to expose the renamed parameter, maintaining compatibility with existing interfaces. These changes enhance the clarity of the decoding parameters and improve the overall structure of the codebase. Signed-off-by: Robin Kobus <19427718+Funatiq@users.noreply.github.com> * refactor: remove usage of `active` vector from prepareForward Signed-off-by: Robin Kobus <19427718+Funatiq@users.noreply.github.com> * refactor: Removed the `active` vector from `decoder_batch::Input` - Removed the `active` vector from the `Input` class constructor in `iGptDecoderBatched.h`, streamlining the input handling for decoding. - Updated the `createDecoderInputs` function and related tests to reflect the changes in the `Input` class, ensuring compatibility and maintaining functionality. - Adjusted Python bindings to accommodate the new constructor signature, enhancing clarity in the interface. These changes improve the maintainability and readability of the decoding process in the batch manager. Signed-off-by: Robin Kobus <19427718+Funatiq@users.noreply.github.com> * refactor: remove usage of `active` vector from gptDecoderBatchedTest Signed-off-by: Robin Kobus <19427718+Funatiq@users.noreply.github.com> * refactor: Unify the creation of decoder batch inputs in algorithm and tests - Added a new static method `createDecoderBatchInputs` to streamline the creation of decoder batch inputs, enhancing clarity and maintainability. - Updated the implementation to utilize active slots directly, simplifying the logic for managing batch slots and logits. - Refactored the `operator()` method to leverage the new input creation function, ensuring compatibility with existing decoding processes. - Enhanced tests to validate the new input handling approach, ensuring correct functionality across various scenarios. These changes improve the overall structure and readability of the decoding process in the batch manager. Signed-off-by: Robin Kobus <19427718+Funatiq@users.noreply.github.com> * refactor: remove usage of active vector from createDecoderBatchInputs Signed-off-by: Robin Kobus <19427718+Funatiq@users.noreply.github.com> * refactor: Update maxDecoderSteps calculation - Replaced integer division with `common::ceilDiv` for calculating `maxDecoderSteps` and `numDecoderSteps`, ensuring correct handling of token counts. These changes enhance the robustness of the decoding batch input creation process. Signed-off-by: Robin Kobus <19427718+Funatiq@users.noreply.github.com> --------- Signed-off-by: Robin Kobus <19427718+Funatiq@users.noreply.github.com>
191 lines
7.9 KiB
C++
191 lines
7.9 KiB
C++
/*
|
|
* Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
|
|
*
|
|
* 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.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "tensorrt_llm/runtime/eagleBuffers.h"
|
|
#include "tensorrt_llm/runtime/explicitDraftTokensBuffers.h"
|
|
#include "tensorrt_llm/runtime/iTensor.h"
|
|
#include "tensorrt_llm/runtime/lookaheadBuffers.h"
|
|
#include "tensorrt_llm/runtime/modelConfig.h"
|
|
#include "tensorrt_llm/runtime/utils/mpiUtils.h"
|
|
#include "tensorrt_llm/runtime/worldConfig.h"
|
|
|
|
#include <optional>
|
|
#include <vector>
|
|
|
|
namespace tensorrt_llm::batch_manager
|
|
{
|
|
|
|
class DecoderInputBuffers
|
|
{
|
|
public:
|
|
using SizeType32 = runtime::SizeType32;
|
|
using TensorPtr = runtime::ITensor::SharedPtr;
|
|
|
|
explicit DecoderInputBuffers(
|
|
SizeType32 maxBatchSize, SizeType32 maxDecoderSteps, runtime::BufferManager const& manager);
|
|
|
|
// buffers for setup
|
|
TensorPtr setupBatchSlots;
|
|
TensorPtr inputsIds;
|
|
|
|
// buffers for forward
|
|
TensorPtr forwardBatchSlotsRequestOrder;
|
|
TensorPtr forwardBatchSlotsRequestOrderDevice;
|
|
TensorPtr fillValues;
|
|
TensorPtr fillValuesDevice;
|
|
std::vector<TensorPtr> forwardBatchSlots;
|
|
};
|
|
|
|
class DecoderStepAsyncSend
|
|
{
|
|
public:
|
|
using BufferPtr = runtime::IBuffer::SharedPtr;
|
|
|
|
DecoderStepAsyncSend(std::shared_ptr<mpi::MpiComm> const& commSession, BufferPtr const& newOutputTokensHost,
|
|
BufferPtr const& finished, BufferPtr const& sequenceLengthsHost, BufferPtr const& cumLogProbsHost,
|
|
BufferPtr const& logProbsHost, BufferPtr const& cacheIndirectionOutput, BufferPtr const& acceptedCumSum,
|
|
BufferPtr const& packedPaths, BufferPtr const& finishReasonsHost, int peer);
|
|
|
|
~DecoderStepAsyncSend();
|
|
|
|
static auto constexpr kMpiTagOffset = 0;
|
|
static auto constexpr kMpiTagUpperBound = kMpiTagOffset + 9;
|
|
|
|
private:
|
|
std::shared_ptr<mpi::MpiRequest> mRequest1;
|
|
std::shared_ptr<mpi::MpiRequest> mRequest2;
|
|
std::shared_ptr<mpi::MpiRequest> mRequest3;
|
|
std::shared_ptr<mpi::MpiRequest> mRequest4;
|
|
std::shared_ptr<mpi::MpiRequest> mRequest5;
|
|
std::shared_ptr<mpi::MpiRequest> mRequest6;
|
|
std::shared_ptr<mpi::MpiRequest> mRequest7;
|
|
std::shared_ptr<mpi::MpiRequest> mRequest8;
|
|
std::shared_ptr<mpi::MpiRequest> mRequest9;
|
|
};
|
|
|
|
class DecoderSlotAsyncSend
|
|
{
|
|
public:
|
|
using TensorPtr = runtime::ITensor::SharedPtr;
|
|
|
|
DecoderSlotAsyncSend(std::shared_ptr<mpi::MpiComm> const& commSession, TensorPtr const& outputIdsView,
|
|
TensorPtr const& sequenceLengthView, TensorPtr const& cumLogProbsView, TensorPtr const& logProbsView,
|
|
bool returnLogProbs, int peer);
|
|
|
|
~DecoderSlotAsyncSend();
|
|
|
|
static auto constexpr kMpiTagOffset = 9;
|
|
static auto constexpr kMpiTagUpperBound = kMpiTagOffset + 4;
|
|
static_assert(kMpiTagOffset >= DecoderStepAsyncSend::kMpiTagUpperBound);
|
|
|
|
private:
|
|
std::shared_ptr<mpi::MpiRequest> mRequest1;
|
|
std::shared_ptr<mpi::MpiRequest> mRequest2;
|
|
std::shared_ptr<mpi::MpiRequest> mRequest3;
|
|
std::shared_ptr<mpi::MpiRequest> mRequest4;
|
|
};
|
|
|
|
class DecoderBuffers
|
|
{
|
|
public:
|
|
using SizeType32 = runtime::SizeType32;
|
|
using TensorPtr = runtime::ITensor::SharedPtr;
|
|
|
|
std::vector<TensorPtr> logits;
|
|
TensorPtr slotOutputIds; // [mMaxNumRequests, beamWidth, maxSeqLen], outputIds of all batch slots
|
|
TensorPtr slotOutputIdsHost; // [beamWidth, maxSeqLen], outputIds of single batch slot
|
|
TensorPtr cacheIndirectionInput;
|
|
TensorPtr cacheIndirectionOutput;
|
|
TensorPtr sequenceLengthsHost; // [mMaxNumRequests, beamWidth], pinned host tensor
|
|
TensorPtr newOutputTokens; // [maxTokensPerStep, mMaxNumRequests, beamWidth]
|
|
TensorPtr newOutputTokensHost; // [maxTokensPerStep, mMaxNumRequests, beamWidth]
|
|
TensorPtr cumLogProbsHost; // [mMaxNumRequests, beamWidth]
|
|
TensorPtr logProbsHost; // [mMaxNumRequests, beamWidth, maxSeqLen]
|
|
TensorPtr finishedSumHost; // [mMaxNumRequests], pinned host tensor
|
|
TensorPtr finishReasonsHost; // [mMaxNumRequests, beamWidth], pinned host tensor
|
|
|
|
class DraftBuffers
|
|
{
|
|
public:
|
|
TensorPtr nextDraftTokensDevice; // [mMaxNumRequests, maxTokensPerStep-1]
|
|
TensorPtr nextDraftTokensHost; // [mMaxNumRequests, maxTokensPerStep-1]
|
|
TensorPtr prevDraftTokensLengthsDevice; // [mMaxNumRequests]
|
|
TensorPtr prevDraftTokensLengthsHost; // [mMaxNumRequests]
|
|
TensorPtr nextDraftTokensLengthsDevice; // [mMaxNumRequests]
|
|
TensorPtr nextDraftTokensLengthsHost; // [mMaxNumRequests]
|
|
TensorPtr acceptedLengthsCumSumDevice; // [mMaxNumRequests+1]
|
|
TensorPtr acceptedPackedPathsDevice; // [mMaxNumRequests * maxAcceptedTokens]
|
|
std::vector<std::vector<runtime::ITensor::SharedPtr>>
|
|
predictedDraftLogits; // [mMaxNumRequests][mMaxNumHeads][maxDraftTokens + 1, vocabSize]
|
|
|
|
void create(SizeType32 maxNumSequences, SizeType32 maxTokensPerStep, runtime::BufferManager const& manager,
|
|
runtime::ModelConfig const& modelConfig);
|
|
};
|
|
|
|
DraftBuffers draftBuffers;
|
|
runtime::ExplicitDraftTokensBuffers::Inputs explicitDraftTokensBuffers;
|
|
runtime::EagleBuffers::Inputs eagleBuffers;
|
|
std::optional<runtime::LookaheadDecodingBuffers> lookaheadBuffers;
|
|
|
|
DecoderBuffers(SizeType32 maxNumSequences, SizeType32 maxBeamWidth, SizeType32 maxAttentionWindow,
|
|
SizeType32 maxSeqLen, SizeType32 maxTokensPerStep, runtime::BufferManager const& manager,
|
|
runtime::ModelConfig const& modelConfig, runtime::WorldConfig const& worldConfig);
|
|
|
|
std::unique_ptr<DecoderStepAsyncSend> asyncSend(std::shared_ptr<mpi::MpiComm> const& commSession,
|
|
bool returnLogProbs, SizeType32 maxBeamWidth, bool useMedusa, int peer);
|
|
|
|
void recv(std::shared_ptr<mpi::MpiComm> const& commSession, bool returnLogProbs, SizeType32 maxBeamWidth,
|
|
bool useMedusa, int peer);
|
|
|
|
void bcast(std::shared_ptr<mpi::MpiComm> const& commSession, bool returnLogProbs, SizeType32 maxBeamWidth,
|
|
bool useMedusa, int root);
|
|
|
|
void enableLookaheadDecoding(SizeType32 maxNumSequences, SizeType32 maxTokensPerStep);
|
|
void disableLookaheadDecoding(SizeType32 maxNumSequences);
|
|
};
|
|
|
|
class SlotDecoderBuffers
|
|
{
|
|
public:
|
|
using SizeType32 = runtime::SizeType32;
|
|
using TensorPtr = runtime::ITensor::SharedPtr;
|
|
|
|
TensorPtr outputIds; // [beamWidth, maxSeqLen], outputIds of single batch slot
|
|
TensorPtr outputIdsHost; // [beamWidth, maxSeqLen], outputIds of single batch slot
|
|
TensorPtr sequenceLengths; // [beamWidth]
|
|
TensorPtr sequenceLengthsHost; // [beamWidth]
|
|
TensorPtr cumLogProbs; // [beamWidth]
|
|
TensorPtr cumLogProbsHost; // [beamWidth]
|
|
TensorPtr logProbs; // [beamWidth, maxSeqLen]
|
|
TensorPtr logProbsHost; // [beamWidth, maxSeqLen]
|
|
TensorPtr finishReasonsHost; // [beamWidth]
|
|
|
|
SlotDecoderBuffers(SizeType32 maxBeamWidth, SizeType32 maxSeqLen, runtime::BufferManager const& manager);
|
|
|
|
static std::unique_ptr<DecoderSlotAsyncSend> asyncSend(std::shared_ptr<mpi::MpiComm> const& commSession,
|
|
TensorPtr const& outputIdsView, TensorPtr const& sequenceLengthView, TensorPtr const& cumLogProbsView,
|
|
TensorPtr const& logProbsView, bool returnLogProbs, int peer);
|
|
|
|
std::unique_ptr<DecoderSlotAsyncSend> asyncSend(
|
|
std::shared_ptr<mpi::MpiComm> const& commSession, bool returnLogProbs, int peer) const;
|
|
|
|
void recv(std::shared_ptr<mpi::MpiComm> const& commSession, bool returnLogProbs, int peer);
|
|
};
|
|
|
|
} // namespace tensorrt_llm::batch_manager
|