diff --git a/tensorrt_llm/_torch/attention_backend/star_flashinfer.py b/tensorrt_llm/_torch/attention_backend/star_flashinfer.py index 9bbb591963aa..80d31815b9cd 100644 --- a/tensorrt_llm/_torch/attention_backend/star_flashinfer.py +++ b/tensorrt_llm/_torch/attention_backend/star_flashinfer.py @@ -1,9 +1,17 @@ +from dataclasses import dataclass, field +from typing import Dict, Optional + +import flashinfer import numpy as np import torch +from tensorrt_llm.functional import AttentionMaskType +from tensorrt_llm.models.modeling_utils import QuantConfig + from ..distributed import allgather from ..modules.linear import ParallelConfig -from .flashinfer import * +from .flashinfer import FlashInferAttentionMetadata, PlanParams +from .interface import AttentionBackend, AttentionMask, PredefinedAttentionMask from .vanilla import VanillaAttention diff --git a/tensorrt_llm/_torch/attention_backend/trtllm.py b/tensorrt_llm/_torch/attention_backend/trtllm.py index dfce39336150..4fbb086b53cc 100644 --- a/tensorrt_llm/_torch/attention_backend/trtllm.py +++ b/tensorrt_llm/_torch/attention_backend/trtllm.py @@ -5,15 +5,16 @@ import torch -from tensorrt_llm._torch.attention_backend.interface import ( - AttentionBackend, AttentionMask, AttentionMetadata, MLAParams, - PositionalEmbeddingParams, PredefinedAttentionMask, RopeParams) -from tensorrt_llm._torch.attention_backend.vanilla import VanillaAttention from tensorrt_llm.functional import (AttentionMaskType, RopeEmbeddingUtils, RotaryScalingType) from tensorrt_llm.logger import logger from tensorrt_llm.models.modeling_utils import QuantConfig +from .interface import (AttentionBackend, AttentionMask, AttentionMetadata, + MLAParams, PositionalEmbeddingParams, + PredefinedAttentionMask, RopeParams) +from .vanilla import VanillaAttention + # The type of requests in qkv passed to attention # Please keep sync with AttentionInputType in cpp/tensorrt_llm/thop/attentionOp.cpp diff --git a/tensorrt_llm/_torch/auto_deploy/custom_ops/dist.py b/tensorrt_llm/_torch/auto_deploy/custom_ops/dist.py index 95faf558543a..27902f322ec8 100644 --- a/tensorrt_llm/_torch/auto_deploy/custom_ops/dist.py +++ b/tensorrt_llm/_torch/auto_deploy/custom_ops/dist.py @@ -2,8 +2,8 @@ import torch -import tensorrt_llm._torch.auto_deploy.distributed.common as dist -import tensorrt_llm._torch.auto_deploy.distributed.trtllm as trtllm_dist +from ..distributed import common as dist +from ..distributed import trtllm as trtllm_dist @torch.library.custom_op("dist::all_gather", mutates_args=(), device_types="cuda") diff --git a/tensorrt_llm/_torch/auto_deploy/custom_ops/fused_moe.py b/tensorrt_llm/_torch/auto_deploy/custom_ops/fused_moe.py index c064e69ab985..64642a36e994 100644 --- a/tensorrt_llm/_torch/auto_deploy/custom_ops/fused_moe.py +++ b/tensorrt_llm/_torch/auto_deploy/custom_ops/fused_moe.py @@ -3,7 +3,7 @@ import torch import torch.nn.functional as F -from tensorrt_llm._torch.modules.fused_moe import FusedMoE # noqa: F401 +from ...modules.fused_moe import FusedMoE # noqa: F401 @torch.library.custom_op("moe::torch_moe", mutates_args=()) diff --git a/tensorrt_llm/_torch/auto_deploy/distributed/trtllm.py b/tensorrt_llm/_torch/auto_deploy/distributed/trtllm.py index c32a013bcaa9..49d8219dae5e 100644 --- a/tensorrt_llm/_torch/auto_deploy/distributed/trtllm.py +++ b/tensorrt_llm/_torch/auto_deploy/distributed/trtllm.py @@ -4,12 +4,8 @@ # use trtllm distributed ops to improve TP performance if possible try: - from tensorrt_llm._torch.distributed import AllReduce, allgather - from tensorrt_llm._torch.modules.linear import ( - AllReduceFusionOp, - AllReduceParams, - ParallelConfig, - ) + from ...distributed import AllReduce, allgather + from ...modules.linear import AllReduceFusionOp, AllReduceParams, ParallelConfig def trtllm_allgather(tensor, dim): rank, world_size = get_rank_world_size() diff --git a/tensorrt_llm/_torch/auto_deploy/utils/quantization_utils.py b/tensorrt_llm/_torch/auto_deploy/utils/quantization_utils.py index 9fc2845cf67e..11b3c18eff1c 100644 --- a/tensorrt_llm/_torch/auto_deploy/utils/quantization_utils.py +++ b/tensorrt_llm/_torch/auto_deploy/utils/quantization_utils.py @@ -15,7 +15,7 @@ ) try: - from tensorrt_llm._torch.quantization.utils import float4_sf_dtype + from ...quantization.utils import float4_sf_dtype except ImportError: float4_sf_dtype = None diff --git a/tensorrt_llm/_torch/compilation/patterns/ar_residual_norm.py b/tensorrt_llm/_torch/compilation/patterns/ar_residual_norm.py index 4291e6041b76..ca9684520632 100644 --- a/tensorrt_llm/_torch/compilation/patterns/ar_residual_norm.py +++ b/tensorrt_llm/_torch/compilation/patterns/ar_residual_norm.py @@ -8,7 +8,8 @@ register_replacement) import tensorrt_llm -from tensorrt_llm._torch.distributed import AllReduceFusionOp + +from ...distributed import AllReduceFusionOp aten = torch.ops.aten from tensorrt_llm.mapping import Mapping diff --git a/tensorrt_llm/_torch/compilation/patterns/ub_allreduce.py b/tensorrt_llm/_torch/compilation/patterns/ub_allreduce.py index 41fb487d529a..35c6268b833a 100644 --- a/tensorrt_llm/_torch/compilation/patterns/ub_allreduce.py +++ b/tensorrt_llm/_torch/compilation/patterns/ub_allreduce.py @@ -8,7 +8,8 @@ register_replacement) import tensorrt_llm -from tensorrt_llm._torch.distributed import AllReduceFusionOp, AllReduceStrategy + +from ...distributed import AllReduceFusionOp, AllReduceStrategy aten = torch.ops.aten from tensorrt_llm.mapping import Mapping diff --git a/tensorrt_llm/_torch/models/modeling_bert.py b/tensorrt_llm/_torch/models/modeling_bert.py index 6717773d214a..19d61f6f0e2f 100644 --- a/tensorrt_llm/_torch/models/modeling_bert.py +++ b/tensorrt_llm/_torch/models/modeling_bert.py @@ -4,7 +4,6 @@ from torch import nn from transformers import BertConfig -from tensorrt_llm._torch.modules.linear import Linear from tensorrt_llm.llmapi.utils import print_colored_debug from tensorrt_llm.logger import logger @@ -14,6 +13,7 @@ from ..modules.attention import Attention from ..modules.decoder_layer import DecoderLayer from ..modules.embedding import Embedding +from ..modules.linear import Linear from .modeling_utils import register_auto_model diff --git a/tensorrt_llm/_torch/models/modeling_deepseekv3.py b/tensorrt_llm/_torch/models/modeling_deepseekv3.py index 2a0eeebfe6dc..72bd9c463830 100644 --- a/tensorrt_llm/_torch/models/modeling_deepseekv3.py +++ b/tensorrt_llm/_torch/models/modeling_deepseekv3.py @@ -9,15 +9,14 @@ from tqdm import tqdm from transformers import PretrainedConfig -from tensorrt_llm._torch.distributed import (AllReduce, AllReduceFusionOp, - AllReduceParams, DeepseekAllReduce, - ParallelConfig, allgather, - reducescatter) from tensorrt_llm.functional import PositionEmbeddingType +from tensorrt_llm.llmapi.utils import enable_llm_debug -from ...llmapi.utils import enable_llm_debug from ..attention_backend import AttentionMetadata from ..attention_backend.interface import PositionalEmbeddingParams, RopeParams +from ..distributed import (AllReduce, AllReduceFusionOp, AllReduceParams, + DeepseekAllReduce, ParallelConfig, allgather, + reducescatter) from ..model_config import ModelConfig from ..models.modeling_utils import MissingLayer, ModelConfig, support_pp from ..modules.attention import MLA @@ -263,7 +262,7 @@ def __init__(self, dtype: Optional[torch.dtype] = None, tune_max_num_tokens: int = 8192, model_config: ModelConfig = ModelConfig()): - from tensorrt_llm._torch.distributed import AllReduce + from ..distributed import AllReduce super().__init__() config = model_config.pretrained_config diff --git a/tensorrt_llm/_torch/models/modeling_mixtral.py b/tensorrt_llm/_torch/models/modeling_mixtral.py index e72bb69ee4dd..be07e6c15e3c 100644 --- a/tensorrt_llm/_torch/models/modeling_mixtral.py +++ b/tensorrt_llm/_torch/models/modeling_mixtral.py @@ -4,11 +4,11 @@ from torch import nn from transformers import PretrainedConfig -from tensorrt_llm._torch.distributed import ParallelConfig, allgather from tensorrt_llm.functional import PositionEmbeddingType from ..attention_backend import AttentionMetadata from ..attention_backend.interface import PositionalEmbeddingParams, RopeParams +from ..distributed import ParallelConfig, allgather from ..model_config import ModelConfig from ..models.modeling_utils import ModelConfig from ..modules.attention import Attention diff --git a/tensorrt_llm/_torch/modules/fused_moe.py b/tensorrt_llm/_torch/modules/fused_moe.py index a80023f8c96f..993a0a53495f 100644 --- a/tensorrt_llm/_torch/modules/fused_moe.py +++ b/tensorrt_llm/_torch/modules/fused_moe.py @@ -216,7 +216,7 @@ def __init__( tune_max_num_tokens: int = 8192, model_config: ModelConfig = ModelConfig(), ): - from tensorrt_llm._torch.distributed import AllReduce + from ..distributed import AllReduce super().__init__() self.routing_method = routing_method diff --git a/tensorrt_llm/_torch/modules/linear.py b/tensorrt_llm/_torch/modules/linear.py index cef6b00d5508..ebbd7dfa598d 100644 --- a/tensorrt_llm/_torch/modules/linear.py +++ b/tensorrt_llm/_torch/modules/linear.py @@ -140,7 +140,7 @@ def __init__(self, is_expert: bool = False, skip_create_weights: bool = False, use_custom_cublas_mm: bool = False): - from tensorrt_llm._torch.distributed import AllReduce + from ..distributed import AllReduce super().__init__() self.has_bias = bias @@ -379,7 +379,7 @@ def forward( *, all_reduce_params: Optional[AllReduceParams] = None ) -> torch.Tensor: - from tensorrt_llm._torch.distributed import allgather + from ..distributed import allgather if self.tp_mode == TensorParallelMode.ROW: bias = None if (self.tp_rank > 0) else self.bias diff --git a/tensorrt_llm/_torch/pyexecutor/_util.py b/tensorrt_llm/_torch/pyexecutor/_util.py index 5cfdb6466f0a..fe932b135cfd 100644 --- a/tensorrt_llm/_torch/pyexecutor/_util.py +++ b/tensorrt_llm/_torch/pyexecutor/_util.py @@ -3,14 +3,13 @@ import torch -import tensorrt_llm -from tensorrt_llm._torch.pyexecutor.model_engine import PyTorchModelEngine from tensorrt_llm.bindings.executor import ExecutorConfig from tensorrt_llm.logger import logger from tensorrt_llm.mapping import Mapping from ..speculative import get_spec_resource_manager -from .llm_request import LlmRequest +from .llm_request import LlmRequest, SamplingConfig +from .model_engine import PyTorchModelEngine from .resource_manager import ResourceManager from .scheduler import ScheduledRequests @@ -96,8 +95,7 @@ def _create_dummy_context(req_id: int, input_len: int, vocab_size: int): random.randint(0, vocab_size - 1) for _ in range(input_len) ], position_ids=list(range(input_len)), - sampling_config=tensorrt_llm.bindings.SamplingConfig( - sampling_params._get_sampling_config()), + sampling_config=SamplingConfig(sampling_params._get_sampling_config()), is_streaming=False, ) result.paged_kv_block_ids = [] diff --git a/tensorrt_llm/_torch/pyexecutor/cuda_graph_runner.py b/tensorrt_llm/_torch/pyexecutor/cuda_graph_runner.py index 3e6a70d7a64e..40b393669c03 100644 --- a/tensorrt_llm/_torch/pyexecutor/cuda_graph_runner.py +++ b/tensorrt_llm/_torch/pyexecutor/cuda_graph_runner.py @@ -3,11 +3,10 @@ import torch -from tensorrt_llm._torch.attention_backend.interface import AttentionMetadata -from tensorrt_llm._torch.speculative.interface import SpecMetadata -from tensorrt_llm._torch.utils import make_weak_ref - +from ..attention_backend.interface import AttentionMetadata from ..pipeline_interface import PipelineInterface +from ..speculative.interface import SpecMetadata +from ..utils import make_weak_ref _local = threading.local() diff --git a/tensorrt_llm/_torch/pyexecutor/decoder.py b/tensorrt_llm/_torch/pyexecutor/decoder.py index 844d8968fdd0..05e19ca92b5a 100644 --- a/tensorrt_llm/_torch/pyexecutor/decoder.py +++ b/tensorrt_llm/_torch/pyexecutor/decoder.py @@ -4,11 +4,23 @@ import torch -import tensorrt_llm.bindings as tllm from tensorrt_llm._utils import torch_dtype_to_binding +from tensorrt_llm.bindings import (DataType, ModelConfig, WorldConfig, + make_sampling_config) +from tensorrt_llm.bindings.executor import (DecodingConfig, DecodingMode, + ExecutorConfig, FinishReason) +from tensorrt_llm.bindings.internal.algorithms import ( + AssignReqSeqSlots, CreateNewDecoderRequests, GenerateRequestOptions, + HandleContextLogits, HandleGenerationLogits, MakeDecodingBatchInputOutput) +from tensorrt_llm.bindings.internal.batch_manager import (DecoderBuffers, + DecoderInputBuffers, + SequenceSlotManager) +from tensorrt_llm.bindings.internal.runtime import (BufferManager, CudaStream, + GptDecoderBatched, + SpeculativeDecodingMode) from tensorrt_llm.mapping import Mapping -from .llm_request import * +from .llm_request import LlmRequest, LlmRequestState from .scheduler import ScheduledRequests @@ -75,7 +87,7 @@ def decode(self, scheduled_requests: ScheduledRequests, model_outputs): for idx, request in enumerate(scheduled_requests.context_requests): request.state = LlmRequestState.GENERATION_COMPLETE #NOTE: This is a hack: set finish reason manually and set the beam 0 - request.set_finished_reason(tllm_executor.FinishReason.LENGTH, 0) + request.set_finished_reason(FinishReason.LENGTH, 0) request.context_logits = model_outputs['logits'][idx] @@ -213,20 +225,17 @@ def _handle_stop_criteria(self, request: LlmRequest, new_token: int, Returns True if generation should stop.""" if new_token == request.py_end_id: request.state = LlmRequestState.GENERATION_COMPLETE - request.set_finished_reason(tllm.executor.FinishReason.END_ID, - beam_idx) + request.set_finished_reason(FinishReason.END_ID, beam_idx) return True if self._meet_max_token_stop_criteria(request, num_tokens): request.state = LlmRequestState.GENERATION_COMPLETE - request.set_finished_reason(tllm.executor.FinishReason.LENGTH, - beam_idx) + request.set_finished_reason(FinishReason.LENGTH, beam_idx) return True if self._meet_stop_token_criteria(request): request.state = LlmRequestState.GENERATION_COMPLETE - request.set_finished_reason(tllm.executor.FinishReason.STOP_WORDS, - beam_idx) + request.set_finished_reason(FinishReason.STOP_WORDS, beam_idx) return True return False @@ -387,11 +396,11 @@ class TRTLLMDecoder(Decoder): def __init__( self, - executor_config: tllm.executor.ExecutorConfig, + executor_config: ExecutorConfig, model, model_dtype, mapping: Mapping, - decoding_mode: tllm.executor.DecodingMode, + decoding_mode: DecodingMode, ): vocab_size = model.config.vocab_size @@ -400,10 +409,10 @@ def __init__( num_heads = model.config.num_attention_heads self.model_datatype = torch_dtype_to_binding(model_dtype) - self.logits_datatype = tllm.DataType.FLOAT + self.logits_datatype = DataType.FLOAT self.decoding_mode = decoding_mode self.executor_config = executor_config - self.decoding_config = self.executor_config.decoding_config if self.executor_config.decoding_config else tllm.executor.DecodingConfig( + self.decoding_config = self.executor_config.decoding_config if self.executor_config.decoding_config else DecodingConfig( decoding_mode) max_attn_window = self.executor_config.kv_cache_config.max_attention_window self.max_attention_window = max_attn_window if max_attn_window is not None else executor_config.max_seq_len @@ -411,12 +420,11 @@ def __init__( self.max_seq_idle_microseconds = 180 * 1000 * 1000 self.max_decoding_tokens = 1 # It must be 1 when not in speculative decoding - self.world_config = tllm.WorldConfig.mpi(mapping.gpus_per_node, - mapping.tp_size, - mapping.pp_size) - self.model_config = tllm.ModelConfig(vocab_size, num_hidden_layers, - num_hidden_layers, 0, num_heads, - hidden_size, self.model_datatype) + self.world_config = WorldConfig.mpi(mapping.gpus_per_node, + mapping.tp_size, mapping.pp_size) + self.model_config = ModelConfig(vocab_size, num_hidden_layers, + num_hidden_layers, 0, num_heads, + hidden_size, self.model_datatype) self._initialize_store() self._instantiate_algorithms() @@ -424,30 +432,26 @@ def __init__( def _initialize_store(self): self.store = {} self.store["torch_stream"] = torch.cuda.Stream() - self.store["cuda_stream"] = tllm.internal.runtime.CudaStream( + self.store["cuda_stream"] = CudaStream( self.store["torch_stream"].cuda_stream) - self.store["buffer_manager"] = tllm.internal.runtime.BufferManager( + self.store["buffer_manager"] = BufferManager( stream=self.store["cuda_stream"]) - self.store[ - "seq_slot_manager"] = tllm.internal.batch_manager.SequenceSlotManager( - self.max_num_sequences, self.max_seq_idle_microseconds) - self.store[ - "decoder_buffers"] = tllm.internal.batch_manager.DecoderBuffers( - self.max_num_sequences, self.executor_config.max_beam_width, - self.max_attention_window, self.executor_config.max_seq_len, - self.max_decoding_tokens, self.store["buffer_manager"], - self.model_config, self.world_config) - self.store[ - "decoder_input_buffers"] = tllm.internal.batch_manager.DecoderInputBuffers( - self.executor_config.max_batch_size, self.max_decoding_tokens, - self.store["buffer_manager"]) + self.store["seq_slot_manager"] = SequenceSlotManager( + self.max_num_sequences, self.max_seq_idle_microseconds) + self.store["decoder_buffers"] = DecoderBuffers( + self.max_num_sequences, self.executor_config.max_beam_width, + self.max_attention_window, self.executor_config.max_seq_len, + self.max_decoding_tokens, self.store["buffer_manager"], + self.model_config, self.world_config) + self.store["decoder_input_buffers"] = DecoderInputBuffers( + self.executor_config.max_batch_size, self.max_decoding_tokens, + self.store["buffer_manager"]) def _instantiate_algorithms(self): self.algs = Algorithms() - self.algs.decoder = tllm.internal.runtime.GptDecoderBatched( + self.algs.decoder = GptDecoderBatched( stream=self.store["cuda_stream"], - speculative_decoding_mode=tllm.internal.runtime. - SpeculativeDecodingMode.NoneType(), + speculative_decoding_mode=SpeculativeDecodingMode.NoneType(), dtype=self.logits_datatype) self.algs.decoder.setup( mode=self.decoding_mode, @@ -460,19 +464,15 @@ def _instantiate_algorithms(self): dtype=self.logits_datatype, model_config=self.model_config, world_config=self.world_config) - self.algs.assign_req_seq_slots = tllm.internal.algorithms.AssignReqSeqSlots( - ) - self.algs.generate_request_options = tllm.internal.algorithms.GenerateRequestOptions( + self.algs.assign_req_seq_slots = AssignReqSeqSlots() + self.algs.generate_request_options = GenerateRequestOptions( speculative_decoding_fast_logits=False, is_leader_in_orch_mode=False, is_normalize_log_probs=False) - self.algs.create_new_decoder_requests = tllm.internal.algorithms.CreateNewDecoderRequests( - ) - self.algs.handle_context_logits = tllm.internal.algorithms.HandleContextLogits( - ) - self.algs.handle_generation_logits = tllm.internal.algorithms.HandleGenerationLogits( - ) - self.algs.make_decoding_batch_input_output = tllm.internal.algorithms.MakeDecodingBatchInputOutput( + self.algs.create_new_decoder_requests = CreateNewDecoderRequests() + self.algs.handle_context_logits = HandleContextLogits() + self.algs.handle_generation_logits = HandleGenerationLogits() + self.algs.make_decoding_batch_input_output = MakeDecodingBatchInputOutput( ) def setup_decoder(self, scheduled_requests: ScheduledRequests, @@ -506,7 +506,7 @@ def setup_decoder(self, scheduled_requests: ScheduledRequests, self.store["cuda_stream"], self.executor_config.max_seq_len) local_batch_size = len(batch_slots) - sampling_config = tllm.make_sampling_config(sampling_configs) + sampling_config = make_sampling_config(sampling_configs) self.algs.decoder.underlying_decoder().setup( sampling_config, local_batch_size, batch_slots, self.algs.decoder.joint_decoding_output, decoder_requests) @@ -579,8 +579,7 @@ def update_requests(self, scheduled_requests: ScheduledRequests): finish_reason = finish_reasons_host[seq_slot * self.beam_width + beam].item() - request.set_finished_reason( - tllm.executor.FinishReason(finish_reason), beam) + request.set_finished_reason(FinishReason(finish_reason), beam) # Set number of tokens predicted per runtime iteration. Will be > 1 for speculative decoding. request.update_num_tokens_per_iteration( diff --git a/tensorrt_llm/_torch/pyexecutor/model_engine.py b/tensorrt_llm/_torch/pyexecutor/model_engine.py index a79097c33d9a..bb86af763dad 100644 --- a/tensorrt_llm/_torch/pyexecutor/model_engine.py +++ b/tensorrt_llm/_torch/pyexecutor/model_engine.py @@ -10,36 +10,33 @@ import safetensors import torch -import tensorrt_llm._torch -import tensorrt_llm.bindings import tensorrt_llm.bindings.internal.userbuffers as ub -from tensorrt_llm._torch.attention_backend import * -from tensorrt_llm._torch.attention_backend.interface import ( - AttentionMetadata, AttentionRuntimeFeatures) -from tensorrt_llm._torch.attention_backend.utils import get_attention_backend -from tensorrt_llm._torch.autotuner import AutoTuner, autotune -from tensorrt_llm._torch.compilation.backend import Backend -from tensorrt_llm._torch.metadata import * -from tensorrt_llm._torch.models import AutoModelForCausalLM -from tensorrt_llm._torch.models.modeling_utils import MetaInitMode, timing -from tensorrt_llm._torch.pyexecutor.distributed import MPIDist -from tensorrt_llm._torch.pyexecutor.resource_manager import ( - BaseResourceManager, KVCacheManager) -from tensorrt_llm._torch.speculative import (SpecConfig, SpecMetadata, - get_spec_metadata) -from tensorrt_llm._torch.utils import set_torch_compiling +from tensorrt_llm._utils import nvtx_range +from tensorrt_llm.bindings.executor import GuidedDecodingConfig from tensorrt_llm.logger import logger from tensorrt_llm.mapping import Mapping +from tensorrt_llm.models.modeling_utils import QuantAlgo from tensorrt_llm.quantization.utils.fp4_utils import float4_e2m1x2 -from ..._utils import nvtx_range -from ...models.modeling_utils import QuantAlgo +from ..attention_backend.interface import (AttentionMetadata, + AttentionRuntimeFeatures) +from ..attention_backend.utils import get_attention_backend +from ..autotuner import AutoTuner, autotune +from ..compilation.backend import Backend +from ..metadata import KVCacheParams +from ..model_config import ModelConfig +from ..models import AutoModelForCausalLM +from ..models.modeling_utils import MetaInitMode, timing from ..pipeline_interface import PipelineInterface +from ..speculative import SpecConfig, SpecMetadata, get_spec_metadata +from ..utils import set_torch_compiling from .config import LoadFormat, PyTorchConfig from .cuda_graph_runner import DecodingCUDAGraphRunner +from .distributed import MPIDist from .guided_decoder import GuidedDecoder from .layerwise_nvtx_marker import LayerwiseNvtxMarker -from .resource_manager import ResourceManager +from .resource_manager import (BaseResourceManager, KVCacheManager, + ResourceManager) from .scheduler import ScheduledRequests MAX_UINT64 = (1 << 64) - 1 @@ -74,9 +71,8 @@ def warmup(self, resource_manager: ResourceManager) -> None: _VALID_KV_CACHE_DTYPES = ("fp8", "auto") -def validate_and_set_kv_cache_quant( - model_config: tensorrt_llm._torch.model_config.ModelConfig, - pyt_kv_cache_dtype: str) -> QuantAlgo: +def validate_and_set_kv_cache_quant(model_config: ModelConfig, + pyt_kv_cache_dtype: str) -> QuantAlgo: logger.info( f'Validating KV Cache config against kv_cache_dtype="{pyt_kv_cache_dtype}"' ) @@ -224,8 +220,7 @@ def __init__( attn_runtime_features: Optional[AttentionRuntimeFeatures] = None, dist: Optional[MPIDist] = None, spec_config: Optional[SpecConfig] = None, - guided_decoding_config: Optional[ - tensorrt_llm.bindings.executor.GuidedDecodingConfig] = None, + guided_decoding_config: Optional[GuidedDecodingConfig] = None, ): self.ub_buffers = None self.batch_size = batch_size @@ -710,8 +705,9 @@ def __del__(self) -> None: def _load_model(self, checkpoint_dir: str, load_format: LoadFormat, **kwargs): - config = tensorrt_llm._torch.model_config.ModelConfig.from_pretrained( - checkpoint_dir, trust_remote_code=True, **kwargs) + config = ModelConfig.from_pretrained(checkpoint_dir, + trust_remote_code=True, + **kwargs) config.spec_config = self.spec_config validate_and_set_kv_cache_quant( diff --git a/tensorrt_llm/_torch/pyexecutor/py_executor.py b/tensorrt_llm/_torch/pyexecutor/py_executor.py index af4691122b87..bd00e974a8bc 100644 --- a/tensorrt_llm/_torch/pyexecutor/py_executor.py +++ b/tensorrt_llm/_torch/pyexecutor/py_executor.py @@ -15,17 +15,21 @@ import numpy as np import torch -import tensorrt_llm.bindings.executor as trtllm - -from ..._utils import global_mpi_rank, nvtx_range -from ...logger import logger -from .decoder import * -from .distributed import * +from tensorrt_llm._utils import global_mpi_rank, nvtx_range +from tensorrt_llm.bindings.executor import (FinishReason, InflightBatchingStats, + IterationStats, KvCacheStats) +from tensorrt_llm.bindings.internal.batch_manager import ReqIdsSet +from tensorrt_llm.logger import logger +from tensorrt_llm.sampling_params import SamplingParams + +from .decoder import Decoder +from .distributed import Distributed from .kv_cache_transceiver import KvCacheTransceiver -from .llm_request import * -from .model_engine import * -from .resource_manager import * -from .scheduler import * +from .llm_request import (ExecutorRequest, ExecutorResponse, LlmRequest, + LlmRequestState, SamplingConfig, + executor_request_to_llm_request) +from .model_engine import ModelEngine +from .scheduler import ScheduledRequests def _is_executor_request(req_queue_item) -> bool: @@ -191,10 +195,8 @@ def __init__(self, # one handle each for metadata and serialized new_reqs buffer self.send_new_reqs_handle = [None] * 2 - self.inflight_req_ids = tensorrt_llm.bindings.internal.batch_manager.ReqIdsSet( - ) - self.canceled_req_ids = tensorrt_llm.bindings.internal.batch_manager.ReqIdsSet( - ) + self.inflight_req_ids = ReqIdsSet() + self.canceled_req_ids = ReqIdsSet() self.model_engine.warmup(self.resource_manager) @@ -219,7 +221,7 @@ def __enter__(self): def __exit__(self): self.shutdown() - def enqueue_requests(self, requests: List[trtllm.Request]): + def enqueue_requests(self, requests: List[ExecutorRequest]): """ Enqueue new requests """ @@ -241,7 +243,7 @@ def await_responses( self, id: Optional[Union[List[int], int]] = None, timeout: Optional[datetime.timedelta] = None, - ) -> Union[List[List[trtllm.Response]], List[trtllm.Response]]: + ) -> Union[List[List[ExecutorResponse]], List[ExecutorResponse]]: """ Await for ready responses Args: @@ -325,7 +327,7 @@ def wait_shutdown(self): self.shutdown_event.wait() def enqueue_request(self, - request: trtllm.Request, + request: ExecutorRequest, query: Optional[List] = None): """ Enqueue a new request, only used in `StarAttention`. @@ -385,7 +387,7 @@ def profile_step(): def _get_init_iter_stats(self, num_new_active_requests, new_active_requests_queue_latency_ms): - stats = trtllm.IterationStats() + stats = IterationStats() stats.timestamp = "" stats.num_new_active_requests = num_new_active_requests @@ -412,7 +414,7 @@ def _update_iter_stats(self, stats, iter_latency_ms, num_completed_requests, "kv_cache_manager") if kv_cache_manager is not None: kv_stats = kv_cache_manager.get_kv_cache_stats() - kv_stats_to_save = trtllm.KvCacheStats() + kv_stats_to_save = KvCacheStats() kv_stats_to_save.max_num_blocks = kv_stats.max_num_blocks kv_stats_to_save.free_num_blocks = kv_stats.free_num_blocks kv_stats_to_save.used_num_blocks = kv_stats.used_num_blocks @@ -424,7 +426,7 @@ def _update_iter_stats(self, stats, iter_latency_ms, num_completed_requests, kv_stats_to_save.cache_hit_rate = kv_stats.cache_hit_rate stats.kv_cache_stats = kv_stats_to_save - model_stats = trtllm.InflightBatchingStats() + model_stats = InflightBatchingStats() model_stats.num_scheduled_requests = len( scheduled_batch.context_requests) + len( scheduled_batch.generation_requests) @@ -1085,7 +1087,7 @@ def _add_kv_cache_events(self): # to be transferred to main thread when user needs them. kv_cache_manager.flush_iteration_events() - def _merge_tp_requests(self, new_requests: List[trtllm.Request]): + def _merge_tp_requests(self, new_requests: List[ExecutorRequest]): got_finish_signal = False for request in new_requests: # return finish signal and drop all request on shutdown @@ -1108,7 +1110,7 @@ def _merge_one_dummy_request(self): request_id=0, max_new_tokens=1, input_tokens=[1], - sampling_config=tensorrt_llm.bindings.SamplingConfig( + sampling_config=SamplingConfig( sampling_params._get_sampling_config()), is_streaming=False, ) @@ -1174,7 +1176,7 @@ def _partition_context(self, ctx_ids_list): return ctx_blocks, position_blocks, padding def _merge_star_attention_requests(self, - new_requests: List[trtllm.Request]): + new_requests: List[ExecutorRequest]): got_finish_signal = False for request in new_requests: # return finish signal and drop all request on shutdown @@ -1238,7 +1240,7 @@ def _merge_star_attention_requests(self, return got_finish_signal @nvtx_range("_merge_requests") - def _merge_requests(self, new_requests: List[trtllm.Request]): + def _merge_requests(self, new_requests: List[ExecutorRequest]): cp_config = self.dist.cp_config if 'cp_type' in cp_config: cp_type = cp_config['cp_type'] @@ -1490,7 +1492,7 @@ def _handle_cancelled_requests(self): req_id = request.py_request_id if req_id in self.canceled_req_ids: self._terminate_request(request) - request.finish_by_reason(trtllm.FinishReason.CANCELLED) + request.finish_by_reason(FinishReason.CANCELLED) cancelled_responses[req_id] = request.create_response( False, self.dist.rank) else: @@ -1502,7 +1504,7 @@ def _handle_cancelled_requests(self): self._enqueue_responses(cancelled_responses) @nvtx_range("_enqueue_responses") - def _enqueue_responses(self, responses: Dict[int, trtllm.Response]): + def _enqueue_responses(self, responses: Dict[int, ExecutorResponse]): if 0 not in self.dist.mapping.tp_group: return @@ -1571,7 +1573,7 @@ def _terminate_ctx_finished_requests(self): def _await_any_response(self, timeout: Union[float, None] = None - ) -> List[trtllm.Response]: + ) -> List[ExecutorResponse]: def any_responses_ready(): return len(self.responses) > 0 or self.is_shutdown @@ -1588,7 +1590,7 @@ def any_responses_ready(): def _await_single_response( self, id: int, - timeout: Union[float, None] = None) -> List[trtllm.Response]: + timeout: Union[float, None] = None) -> List[ExecutorResponse]: with self.response_cv: def key_has_response(): diff --git a/tensorrt_llm/_torch/pyexecutor/py_executor_creator.py b/tensorrt_llm/_torch/pyexecutor/py_executor_creator.py index e96564335e5e..8a739c27abcc 100644 --- a/tensorrt_llm/_torch/pyexecutor/py_executor_creator.py +++ b/tensorrt_llm/_torch/pyexecutor/py_executor_creator.py @@ -2,35 +2,27 @@ import tensorrt_llm import tensorrt_llm.bindings as tllm -from tensorrt_llm._torch.attention_backend.interface import \ - AttentionRuntimeFeatures -from tensorrt_llm._torch.pyexecutor.config import PyTorchConfig -from tensorrt_llm._torch.pyexecutor.decoder import (EarlyStopDecoder, - TorchDecoder, - TorchStarAttentionDecoder, - TRTLLMDecoder) -from tensorrt_llm._torch.pyexecutor.distributed import MPIDist -from tensorrt_llm._torch.pyexecutor.guided_decoder import \ - GuidedDecoderResourceManager -from tensorrt_llm._torch.pyexecutor.kv_cache_transceiver import ( - AttentionTypeCpp, create_kv_cache_transceiver) -from tensorrt_llm._torch.pyexecutor.model_engine import PyTorchModelEngine -from tensorrt_llm._torch.pyexecutor.py_executor import PyExecutor -from tensorrt_llm._torch.pyexecutor.resource_manager import (KVCacheManager, - ResourceManager) -from tensorrt_llm._torch.pyexecutor.scheduler import (BindCapacityScheduler, - BindMicroBatchScheduler, - SimpleScheduler) -from tensorrt_llm._torch.speculative import (get_num_spec_layers, - get_spec_decoder, - get_spec_resource_manager) from tensorrt_llm._utils import str_dtype_to_binding, torch_dtype_to_str from tensorrt_llm.bindings.executor import ContextChunkingPolicy, ExecutorConfig from tensorrt_llm.bindings.internal.batch_manager import ContextChunkingConfig from tensorrt_llm.logger import logger from tensorrt_llm.mapping import Mapping +from ..attention_backend.interface import AttentionRuntimeFeatures +from ..speculative import (get_num_spec_layers, get_spec_decoder, + get_spec_resource_manager) from ._util import check_flash_mla_config, estimate_max_kv_cache_tokens, is_mla +from .config import PyTorchConfig +from .decoder import (EarlyStopDecoder, TorchDecoder, TorchStarAttentionDecoder, + TRTLLMDecoder) +from .distributed import MPIDist +from .guided_decoder import GuidedDecoderResourceManager +from .kv_cache_transceiver import AttentionTypeCpp, create_kv_cache_transceiver +from .model_engine import PyTorchModelEngine +from .py_executor import PyExecutor +from .resource_manager import KVCacheManager, ResourceManager +from .scheduler import (BindCapacityScheduler, BindMicroBatchScheduler, + SimpleScheduler) def create_py_executor(executor_config: ExecutorConfig, diff --git a/tensorrt_llm/_torch/pyexecutor/resource_manager.py b/tensorrt_llm/_torch/pyexecutor/resource_manager.py index 5e16a5aa0e87..9b07376bf459 100644 --- a/tensorrt_llm/_torch/pyexecutor/resource_manager.py +++ b/tensorrt_llm/_torch/pyexecutor/resource_manager.py @@ -13,7 +13,7 @@ from ..._utils import nvtx_range from ...logger import logger from ...mapping import Mapping -from .llm_request import LlmRequest +from .llm_request import LlmRequest, LlmRequestState, SamplingConfig from .scheduler import ScheduledRequests if ENABLE_MULTI_DEVICE: @@ -303,18 +303,17 @@ def add_dummy_requests( 1 ] * token_num if self.impl.cross_kv else None # Using 1 instead of 0 prevents NaN during warmup in e.g. Deepseek - req = LlmRequest( - request_id=req_id, - max_new_tokens=1, - input_tokens=[1] * token_num, - sampling_config=tensorrt_llm.bindings.SamplingConfig( - sampling_params._get_sampling_config()), - is_streaming=False, - encoder_input_tokens=encoder_input_tokens) + req = LlmRequest(request_id=req_id, + max_new_tokens=1, + input_tokens=[1] * token_num, + sampling_config=SamplingConfig( + sampling_params._get_sampling_config()), + is_streaming=False, + encoder_input_tokens=encoder_input_tokens) req.paged_kv_block_ids = [] self.impl.add_sequence(req_id, token_num, beam_width, req) if is_gen: - req.state = tensorrt_llm.bindings.LlmRequestState.GENERATION_IN_PROGRESS + req.state = LlmRequestState.GENERATION_IN_PROGRESS req.prompt_len = token_num - 1 + max_num_draft_tokens req.py_prompt_len = req.prompt_len if max_num_draft_tokens > 0: @@ -325,7 +324,7 @@ def add_dummy_requests( def update_resources(self, scheduled_batch: ScheduledRequests): # rewind kv cache for request in scheduled_batch.generation_requests: - if request.state != tensorrt_llm.bindings.LlmRequestState.GENERATION_COMPLETE: + if request.state != LlmRequestState.GENERATION_COMPLETE: if request.py_rewind_len > 0: self.rewind_kv_cache(request, request.py_rewind_len) diff --git a/tensorrt_llm/_torch/speculative/mtp.py b/tensorrt_llm/_torch/speculative/mtp.py index de6e73dff383..364c30d479ce 100644 --- a/tensorrt_llm/_torch/speculative/mtp.py +++ b/tensorrt_llm/_torch/speculative/mtp.py @@ -1,13 +1,14 @@ from dataclasses import dataclass -from typing import Dict, Optional +from typing import Dict, List, Optional import torch from torch import nn +from tensorrt_llm.bindings.executor import FinishReason + from ..attention_backend import AttentionMetadata from ..pyexecutor.decoder import TorchDecoder -from ..pyexecutor.llm_request import * -from ..pyexecutor.llm_request import LlmRequest +from ..pyexecutor.llm_request import LlmRequest, LlmRequestState from ..pyexecutor.resource_manager import BaseResourceManager, SlotManager from ..pyexecutor.scheduler import ScheduledRequests from .interface import SpecConfig, SpecMetadata, SpeculativeDecodingMode @@ -179,8 +180,7 @@ def _draft_meet_max_token_stop_criteria(self, request: LlmRequest, if self._meet_max_token_stop_criteria(request, num_tokens + self.draft_len): request.state = LlmRequestState.GENERATION_COMPLETE - request.set_finished_reason(tllm_executor.FinishReason.LENGTH, - beam_idx) + request.set_finished_reason(FinishReason.LENGTH, beam_idx) def update_requests(self, scheduled_requests: ScheduledRequests, new_tensors_host: Dict[str, torch.Tensor],