Skip to content

TRT-LLM loading mechanism tool #3398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions dev_dep_versions.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__cuda_version__: "12.8"
__tensorrt_version__: "10.11.0"
__tensorrt_llm_version__: "0.17.0.post1"
9 changes: 9 additions & 0 deletions py/torch_tensorrt/dynamo/_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def cross_compile_for_windows(
tiling_optimization_level: str = _defaults.TILING_OPTIMIZATION_LEVEL,
l2_limit_for_tiling: int = _defaults.L2_LIMIT_FOR_TILING,
offload_module_to_cpu: bool = _defaults.OFFLOAD_MODULE_TO_CPU,
use_distributed_mode_trace: bool = _defaults.USE_DISTRIBUTED_MODE_TRACE,
**kwargs: Any,
) -> torch.fx.GraphModule:
"""Compile an ExportedProgram module using TensorRT in Linux for Inference in Windows
Expand Down Expand Up @@ -177,6 +178,7 @@ def cross_compile_for_windows(
enable_weight_streaming (bool): Enable weight streaming.
tiling_optimization_level (str): The optimization level of tiling strategies. A higher level allows TensorRT to spend more time searching for better tiling strategy. We currently support ["none", "fast", "moderate", "full"].
l2_limit_for_tiling (int): The target L2 cache usage limit (in bytes) for tiling optimization (default is -1 which means no limit).
use_distributed_mode_trace (bool): Using aot_autograd to trace the graph. This is enabled when DTensors or distributed tensors are present in distributed model
**kwargs: Any,
Returns:
torch.fx.GraphModule: Compiled FX Module, when run it will execute via TensorRT
Expand Down Expand Up @@ -339,6 +341,7 @@ def cross_compile_for_windows(
"enable_weight_streaming": enable_weight_streaming,
"tiling_optimization_level": tiling_optimization_level,
"l2_limit_for_tiling": l2_limit_for_tiling,
"use_distributed_mode_trace": use_distributed_mode_trace,
}

# disable the following settings is not supported for cross compilation for windows feature
Expand Down Expand Up @@ -439,6 +442,7 @@ def compile(
tiling_optimization_level: str = _defaults.TILING_OPTIMIZATION_LEVEL,
l2_limit_for_tiling: int = _defaults.L2_LIMIT_FOR_TILING,
offload_module_to_cpu: bool = _defaults.OFFLOAD_MODULE_TO_CPU,
use_distributed_mode_trace: bool = _defaults.USE_DISTRIBUTED_MODE_TRACE,
**kwargs: Any,
) -> torch.fx.GraphModule:
"""Compile an ExportedProgram module for NVIDIA GPUs using TensorRT
Expand Down Expand Up @@ -516,6 +520,7 @@ def compile(
tiling_optimization_level (str): The optimization level of tiling strategies. A higher level allows TensorRT to spend more time searching for better tiling strategy. We currently support ["none", "fast", "moderate", "full"].
l2_limit_for_tiling (int): The target L2 cache usage limit (in bytes) for tiling optimization (default is -1 which means no limit).
offload_module_to_cpu (bool): Offload the module to CPU. This is useful when we need to minimize GPU memory usage.
use_distributed_mode_trace (bool): Using aot_autograd to trace the graph. This is enabled when DTensors or distributed tensors are present in distributed model
**kwargs: Any,
Returns:
torch.fx.GraphModule: Compiled FX Module, when run it will execute via TensorRT
Expand Down Expand Up @@ -688,6 +693,7 @@ def compile(
"tiling_optimization_level": tiling_optimization_level,
"l2_limit_for_tiling": l2_limit_for_tiling,
"offload_module_to_cpu": offload_module_to_cpu,
"use_distributed_mode_trace": use_distributed_mode_trace,
}

settings = CompilationSettings(**compilation_options)
Expand Down Expand Up @@ -1029,6 +1035,7 @@ def convert_exported_program_to_serialized_trt_engine(
tiling_optimization_level: str = _defaults.TILING_OPTIMIZATION_LEVEL,
l2_limit_for_tiling: int = _defaults.L2_LIMIT_FOR_TILING,
offload_module_to_cpu: bool = _defaults.OFFLOAD_MODULE_TO_CPU,
use_distributed_mode_trace: bool = _defaults.USE_DISTRIBUTED_MODE_TRACE,
**kwargs: Any,
) -> bytes:
"""Convert an ExportedProgram to a serialized TensorRT engine
Expand Down Expand Up @@ -1093,6 +1100,7 @@ def convert_exported_program_to_serialized_trt_engine(
enable_weight_streaming (bool): Enable weight streaming.
tiling_optimization_level (str): The optimization level of tiling strategies. A higher level allows TensorRT to spend more time searching for better tiling strategy. We currently support ["none", "fast", "moderate", "full"].
l2_limit_for_tiling (int): The target L2 cache usage limit (in bytes) for tiling optimization (default is -1 which means no limit).
use_distributed_mode_trace: bool = _defaults.USE_DISTRIBUTED_MODE_TRACE,
Returns:
bytes: Serialized TensorRT engine, can either be saved to a file or deserialized via TensorRT APIs
"""
Expand Down Expand Up @@ -1215,6 +1223,7 @@ def convert_exported_program_to_serialized_trt_engine(
"tiling_optimization_level": tiling_optimization_level,
"l2_limit_for_tiling": l2_limit_for_tiling,
"offload_module_to_cpu": offload_module_to_cpu,
"use_distributed_mode_trace": use_distributed_mode_trace,
}

settings = CompilationSettings(**compilation_options)
Expand Down
65 changes: 0 additions & 65 deletions py/torch_tensorrt/dynamo/conversion/converter_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import collections
import ctypes
import functools
import logging
import os
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -1116,69 +1114,6 @@ def args_bounds_check(
return args[i] if len(args) > i and args[i] is not None else replacement


def load_tensorrt_llm() -> bool:
"""
Attempts to load the TensorRT-LLM plugin and initialize it.

Returns:
bool: True if the plugin was successfully loaded and initialized, False otherwise.
"""
try:
import tensorrt_llm as trt_llm # noqa: F401

_LOGGER.info("TensorRT-LLM successfully imported")
return True
except (ImportError, AssertionError) as e_import_error:
# Check for environment variable for the plugin library path
plugin_lib_path = os.environ.get("TRTLLM_PLUGINS_PATH")
if not plugin_lib_path:
_LOGGER.warning(
"TensorRT-LLM is not installed. Please install TensorRT-LLM or set TRTLLM_PLUGINS_PATH to the directory containing libnvinfer_plugin_tensorrt_llm.so to use converters for torch.distributed ops",
)
return False

_LOGGER.info(f"TensorRT-LLM Plugin lib path found: {plugin_lib_path}")
try:
# Load the shared library
handle = ctypes.CDLL(plugin_lib_path)
_LOGGER.info(f"Successfully loaded plugin library: {plugin_lib_path}")
except OSError as e_os_error:
_LOGGER.error(
f"Failed to load libnvinfer_plugin_tensorrt_llm.so from {plugin_lib_path}"
f"Ensure the path is correct and the library is compatible",
exc_info=e_os_error,
)
return False

try:
# Configure plugin initialization arguments
handle.initTrtLlmPlugins.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
handle.initTrtLlmPlugins.restype = ctypes.c_bool
except AttributeError as e_plugin_unavailable:
_LOGGER.warning(
"Unable to initialize the TensorRT-LLM plugin library",
exc_info=e_plugin_unavailable,
)
return False

try:
# Initialize the plugin
TRT_LLM_PLUGIN_NAMESPACE = "tensorrt_llm"
if handle.initTrtLlmPlugins(None, TRT_LLM_PLUGIN_NAMESPACE.encode("utf-8")):
_LOGGER.info("TensorRT-LLM plugin successfully initialized")
return True
else:
_LOGGER.warning("TensorRT-LLM plugin library failed in initialization")
return False
except Exception as e_initialization_error:
_LOGGER.warning(
"Exception occurred during TensorRT-LLM plugin library initialization",
exc_info=e_initialization_error,
)
return False
return False


def promote_trt_tensors_to_same_dtype(
ctx: ConversionContext, lhs: TRTTensor, rhs: TRTTensor, name_prefix: str
) -> tuple[TRTTensor, TRTTensor]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
from torch_tensorrt.dynamo.conversion._ConverterRegistry import (
dynamo_tensorrt_converter,
)
from torch_tensorrt.dynamo.conversion.converter_utils import load_tensorrt_llm
from torch_tensorrt.dynamo.lowering.passes.fuse_distributed_ops import (
tensorrt_fused_nccl_all_gather_op,
tensorrt_fused_nccl_reduce_scatter_op,
)
from torch_tensorrt.dynamo.utils import load_tensorrt_llm

_LOGGER: logging.Logger = logging.getLogger(__name__)

Expand Down
Loading
Loading