TensorRT-LLMs/tensorrt_llm/logger.py
dominicshanshan c9dca69e1b
[None][chore] Mass integration of release/1.0 - 3rd (#7519)
Signed-off-by: Nave Assaf <nassaf@nvidia.com>
Signed-off-by: Wangshanshan <30051912+dominicshanshan@users.noreply.github.com>
Signed-off-by: yechank <161688079+yechank-nvidia@users.noreply.github.com>
Signed-off-by: Balaram Buddharaju <169953907+brb-nv@users.noreply.github.com>
Signed-off-by: Iman Tabrizian <10105175+tabrizian@users.noreply.github.com>
Signed-off-by: qqiao <qqiao@nvidia.com>
Signed-off-by: Superjomn <328693+Superjomn@users.noreply.github.com>
Signed-off-by: Bo Deng <deemod@nvidia.com>
Signed-off-by: Jin Li <59594262+liji-nv@users.noreply.github.com>
Signed-off-by: Yifei Zhang <219273404+yifeizhang-c@users.noreply.github.com>
Signed-off-by: Amit Zuker <203509407+amitz-nv@users.noreply.github.com>
Signed-off-by: Erin Ho <14718778+hchings@users.noreply.github.com>
Signed-off-by: Chenfei Zhang <chenfeiz@nvidia.com>
Signed-off-by: Christina Zhang <83400082+ChristinaZ@users.noreply.github.com>
Signed-off-by: Venky Ganesh <23023424+venkywonka@users.noreply.github.com>
Signed-off-by: Pamela <179191831+pamelap-nvidia@users.noreply.github.com>
Signed-off-by: Hui Gao <huig@nvidia.com>
Signed-off-by: Alexandre Milesi <30204471+milesial@users.noreply.github.com>
Signed-off-by: Shixiaowei02 <39303645+Shixiaowei02@users.noreply.github.com>
Signed-off-by: Michal Guzek <mguzek@nvidia.com>
Signed-off-by: peaceh <103117813+peaceh-nv@users.noreply.github.com>
Signed-off-by: nv-guomingz <137257613+nv-guomingz@users.noreply.github.com>
Signed-off-by: Wanli Jiang <35160485+Wanli-Jiang@users.noreply.github.com>
Signed-off-by: Patrice Castonguay <55748270+pcastonguay@users.noreply.github.com>
Signed-off-by: ruodil <200874449+ruodil@users.noreply.github.com>
Signed-off-by: Linda-Stadter <57756729+Linda-Stadter@users.noreply.github.com>
Signed-off-by: Yuxian Qiu <142763828+yuxianq@users.noreply.github.com>
Signed-off-by: Jiagan Cheng <jiaganc@nvidia.com>
Signed-off-by: William Zhang <133824995+2ez4bz@users.noreply.github.com>
Signed-off-by: Dom Brown <3886319+DomBrown@users.noreply.github.com>
Co-authored-by: Nave Assaf <55059536+Naveassaf@users.noreply.github.com>
Co-authored-by: Yechan Kim <161688079+yechank-nvidia@users.noreply.github.com>
Co-authored-by: brb-nv <169953907+brb-nv@users.noreply.github.com>
Co-authored-by: Iman Tabrizian <10105175+Tabrizian@users.noreply.github.com>
Co-authored-by: Emma Qiao <qqiao@nvidia.com>
Co-authored-by: Yan Chunwei <328693+Superjomn@users.noreply.github.com>
Co-authored-by: Bo Deng <deemod@nvidia.com>
Co-authored-by: Jin Li <59594262+liji-nv@users.noreply.github.com>
Co-authored-by: yifeizhang-c <219273404+yifeizhang-c@users.noreply.github.com>
Co-authored-by: amitz-nv <203509407+amitz-nv@users.noreply.github.com>
Co-authored-by: Erin <14718778+hchings@users.noreply.github.com>
Co-authored-by: chenfeiz0326 <chenfeiz@nvidia.com>
Co-authored-by: ChristinaZ <83400082+ChristinaZ@users.noreply.github.com>
Co-authored-by: Venky <23023424+venkywonka@users.noreply.github.com>
Co-authored-by: Pamela Peng <179191831+pamelap-nvidia@users.noreply.github.com>
Co-authored-by: HuiGao-NV <huig@nvidia.com>
Co-authored-by: milesial <milesial@users.noreply.github.com>
Co-authored-by: Shi Xiaowei <39303645+Shixiaowei02@users.noreply.github.com>
Co-authored-by: Michal Guzek <moraxu@users.noreply.github.com>
Co-authored-by: peaceh-nv <103117813+peaceh-nv@users.noreply.github.com>
Co-authored-by: Guoming Zhang <137257613+nv-guomingz@users.noreply.github.com>
Co-authored-by: Wanli Jiang <35160485+Wanli-Jiang@users.noreply.github.com>
Co-authored-by: pcastonguay <55748270+pcastonguay@users.noreply.github.com>
Co-authored-by: ruodil <200874449+ruodil@users.noreply.github.com>
Co-authored-by: Linda <57756729+Linda-Stadter@users.noreply.github.com>
Co-authored-by: Zhanrui Sun <184402041+ZhanruiSunCh@users.noreply.github.com>
Co-authored-by: Yuxian Qiu <142763828+yuxianq@users.noreply.github.com>
Co-authored-by: Jiagan Cheng <jiaganc@nvidia.com>
Co-authored-by: William Zhang <133824995+2ez4bz@users.noreply.github.com>
Co-authored-by: Larry <197874197+LarryXFly@users.noreply.github.com>
Co-authored-by: Sharan Chetlur <116769508+schetlur-nv@users.noreply.github.com>
Co-authored-by: Dom Brown <3886319+DomBrown@users.noreply.github.com>
2025-09-08 14:03:04 +08:00

195 lines
6.1 KiB
Python

# SPDX-FileCopyrightText: Copyright (c) 2022-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# 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.
import logging
import os
import sys
from typing import Optional
import tensorrt as trt
try:
from polygraphy.logger import G_LOGGER
except ImportError:
G_LOGGER = None
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Logger(metaclass=Singleton):
ENV_VARIABLE = "TLLM_LOG_LEVEL"
PREFIX = "TRT-LLM"
DEFAULT_LEVEL = "error"
INTERNAL_ERROR = "[F]"
ERROR = "[E]"
WARNING = "[W]"
INFO = "[I]"
VERBOSE = "[V]"
DEBUG = "[D]"
def __init__(self):
environ_severity = os.environ.get(self.ENV_VARIABLE)
self._set_from_env = environ_severity is not None
self.rank: Optional[int] = None
min_severity = environ_severity.lower() if self._set_from_env else self.DEFAULT_LEVEL
invalid_severity = min_severity not in severity_map
if invalid_severity:
min_severity = self.DEFAULT_LEVEL
self._min_severity = min_severity
self._trt_logger = trt.Logger(severity_map[min_severity][0])
self._logger = logging.getLogger(self.PREFIX)
self._logger.propagate = False
handler = logging.StreamHandler(stream=sys.stdout)
handler.setFormatter(
logging.Formatter(fmt="[%(asctime)s] %(message)s", datefmt="%m/%d/%Y-%H:%M:%S")
)
self._logger.addHandler(handler)
self._logger.setLevel(severity_map[min_severity][1])
self._polygraphy_logger = G_LOGGER
if self._polygraphy_logger is not None:
self._polygraphy_logger.module_severity = severity_map[min_severity][2]
# For log_once
self._appeared_keys = set()
if invalid_severity:
self.warning(
f"Requested log level {environ_severity} is invalid. Using '{self.DEFAULT_LEVEL}' instead"
)
def set_rank(self, rank: int):
self.rank = rank
def _func_wrapper(self, severity):
if severity == self.INTERNAL_ERROR:
return self._logger.critical
elif severity == self.ERROR:
return self._logger.error
elif severity == self.WARNING:
return self._logger.warning
elif severity == self.INFO:
return self._logger.info
elif severity == self.VERBOSE or severity == self.DEBUG:
return self._logger.debug
else:
raise AttributeError(f"No such severity: {severity}")
@property
def trt_logger(self) -> trt.ILogger:
return self._trt_logger
def log(self, severity, *msg):
parts = [f"[{self.PREFIX}]"]
if self.rank is not None:
parts.append(f"[RANK {self.rank}]")
parts.append(severity)
parts.extend(map(str, msg))
self._func_wrapper(severity)(" ".join(parts))
def log_once(self, severity, *msg, key):
assert key is not None, "key is required for log_once"
if key not in self._appeared_keys:
self._appeared_keys.add(key)
self.log(severity, *msg)
def critical(self, *msg):
self.log(self.INTERNAL_ERROR, *msg)
def critical_once(self, *msg, key):
self.log_once(self.INTERNAL_ERROR, *msg, key=key)
fatal = critical
fatal_once = critical_once
def error(self, *msg):
self.log(self.ERROR, *msg)
def error_once(self, *msg, key):
self.log_once(self.ERROR, *msg, key=key)
def warning(self, *msg):
self.log(self.WARNING, *msg)
def warning_once(self, *msg, key):
self.log_once(self.WARNING, *msg, key=key)
def info(self, *msg):
self.log(self.INFO, *msg)
def info_once(self, *msg, key):
self.log_once(self.INFO, *msg, key=key)
def debug(self, *msg):
self.log(self.VERBOSE, *msg)
def debug_once(self, *msg, key):
self.log_once(self.VERBOSE, *msg, key=key)
@property
def level(self) -> str:
return self._min_severity
def set_level(self, min_severity):
if self._set_from_env:
self.warning(
f"Logger level already set from environment. Discard new verbosity: {min_severity}"
)
return
self._min_severity = min_severity
self._trt_logger.min_severity = severity_map[min_severity][0]
self._logger.setLevel(severity_map[min_severity][1])
if self._polygraphy_logger is not None:
self._polygraphy_logger.module_severity = severity_map[min_severity][2]
severity_map = {
"internal_error": [trt.Logger.INTERNAL_ERROR, logging.CRITICAL],
"error": [trt.Logger.ERROR, logging.ERROR],
"warning": [trt.Logger.WARNING, logging.WARNING],
"info": [trt.Logger.INFO, logging.INFO],
"verbose": [trt.Logger.VERBOSE, logging.DEBUG],
"debug": [trt.Logger.VERBOSE, logging.DEBUG],
"trace": [trt.Logger.VERBOSE, logging.DEBUG],
}
if G_LOGGER is not None:
g_logger_severity_map = {
"internal_error": G_LOGGER.CRITICAL,
"error": G_LOGGER.ERROR,
"warning": G_LOGGER.WARNING,
"info": G_LOGGER.INFO,
"verbose": G_LOGGER.SUPER_VERBOSE,
"debug": G_LOGGER.SUPER_VERBOSE,
"trace": G_LOGGER.SUPER_VERBOSE,
}
for key, value in g_logger_severity_map.items():
severity_map[key].append(value)
logger = Logger()
def set_level(min_severity):
logger.set_level(min_severity)