From 789dccecedc141014eab81f5fcd37bc832c50170 Mon Sep 17 00:00:00 2001 From: yinfan98 <32722923+yinfan98@users.noreply.github.com> Date: Wed, 31 Jan 2024 19:23:41 +0800 Subject: [PATCH] fix and change all logger using get_logger (#1064) * fix logger * Merge the main branch for logger * Fix some unchanged logging to logger --- benchmark/profile_hf_generation.py | 3 +- lmdeploy/legacy/pytorch/adapters/__init__.py | 6 ++-- lmdeploy/legacy/pytorch/adapters/base.py | 5 +-- lmdeploy/legacy/pytorch/adapters/internlm.py | 5 +-- lmdeploy/legacy/pytorch/adapters/llama2.py | 5 +-- lmdeploy/legacy/pytorch/chat.py | 26 ++++++++------- lmdeploy/legacy/pytorch/decode.py | 5 +-- lmdeploy/legacy/pytorch/model.py | 5 +-- lmdeploy/legacy/pytorch/session.py | 5 +-- lmdeploy/legacy/pytorch/utils.py | 6 ++-- .../pytorch/modeling/modeling_baichuan.py | 4 +-- .../pytorch/modeling/modeling_internlm.py | 5 +-- .../pytorch/modeling/modeling_internlm2.py | 5 +-- lmdeploy/pytorch/modeling/modeling_llama.py | 5 +-- lmdeploy/pytorch/models/falcon.py | 3 -- lmdeploy/serve/qos_engine/inner_group_schd.py | 5 +-- lmdeploy/serve/qos_engine/qos_engine.py | 4 +-- lmdeploy/serve/turbomind/chatbot.py | 21 ++++-------- lmdeploy/turbomind/turbomind.py | 24 ++++++-------- lmdeploy/turbomind/utils.py | 5 +-- lmdeploy/utils.py | 32 ++++++------------- 21 files changed, 87 insertions(+), 97 deletions(-) diff --git a/benchmark/profile_hf_generation.py b/benchmark/profile_hf_generation.py index 1c6704bc4..6e981a191 100644 --- a/benchmark/profile_hf_generation.py +++ b/benchmark/profile_hf_generation.py @@ -75,8 +75,9 @@ from transformers import AutoModelForCausalLM, GenerationConfig from lmdeploy.pytorch.accel import LoadNoInit +from lmdeploy.utils import get_logger -logger = logging.getLogger(__file__) +logger = get_logger(__file__) logger.setLevel(logging.DEBUG) info = logger.info warning = logger.warning diff --git a/lmdeploy/legacy/pytorch/adapters/__init__.py b/lmdeploy/legacy/pytorch/adapters/__init__.py index f87c0546d..f4e93b7e5 100644 --- a/lmdeploy/legacy/pytorch/adapters/__init__.py +++ b/lmdeploy/legacy/pytorch/adapters/__init__.py @@ -1,14 +1,14 @@ # Copyright (c) OpenMMLab. All rights reserved. -import logging - import torch.nn as nn +from lmdeploy.utils import get_logger + from .base import BasicAdapter, BasicAdapterFast from .internlm import InternLMAdapter from .llama2 import Llama2Adapter -logger = logging.getLogger(__name__) +logger = get_logger(__name__) def _get_default_adapter(tokenizer): diff --git a/lmdeploy/legacy/pytorch/adapters/base.py b/lmdeploy/legacy/pytorch/adapters/base.py index e958a0f37..e3eee2e9f 100644 --- a/lmdeploy/legacy/pytorch/adapters/base.py +++ b/lmdeploy/legacy/pytorch/adapters/base.py @@ -1,13 +1,14 @@ # Copyright (c) OpenMMLab. All rights reserved. """Basic adapter suitable for general HuggingFace models.""" -import logging import re from transformers import (PreTrainedTokenizer, PreTrainedTokenizerBase, PreTrainedTokenizerFast) -logger = logging.getLogger(__name__) +from lmdeploy.utils import get_logger + +logger = get_logger(__name__) class BaseAdapter: diff --git a/lmdeploy/legacy/pytorch/adapters/internlm.py b/lmdeploy/legacy/pytorch/adapters/internlm.py index 89e6f9f03..fc785c3b7 100644 --- a/lmdeploy/legacy/pytorch/adapters/internlm.py +++ b/lmdeploy/legacy/pytorch/adapters/internlm.py @@ -1,14 +1,15 @@ # Copyright (c) OpenMMLab. All rights reserved. -import logging import re import torch from transformers import (PreTrainedTokenizerFast, StoppingCriteria, StoppingCriteriaList) +from lmdeploy.utils import get_logger + from .base import BaseAdapter -logger = logging.getLogger(__name__) +logger = get_logger(__name__) class InternLMStoppingCriteria(StoppingCriteria): diff --git a/lmdeploy/legacy/pytorch/adapters/llama2.py b/lmdeploy/legacy/pytorch/adapters/llama2.py index d0dcb6d25..5e46a1d6d 100644 --- a/lmdeploy/legacy/pytorch/adapters/llama2.py +++ b/lmdeploy/legacy/pytorch/adapters/llama2.py @@ -1,12 +1,13 @@ # Copyright (c) OpenMMLab. All rights reserved. -import logging import re from transformers import PreTrainedTokenizerFast +from lmdeploy.utils import get_logger + from .base import BasicAdapterFast -logger = logging.getLogger(__name__) +logger = get_logger(__name__) B_INST, E_INST = '[INST]', '[/INST]' B_SYS, E_SYS = '<>\n', '\n<>\n\n' diff --git a/lmdeploy/legacy/pytorch/chat.py b/lmdeploy/legacy/pytorch/chat.py index 9a167cc52..12c8d4df4 100644 --- a/lmdeploy/legacy/pytorch/chat.py +++ b/lmdeploy/legacy/pytorch/chat.py @@ -54,14 +54,14 @@ import torch from transformers import GenerationConfig, PreTrainedModel +from lmdeploy.utils import get_logger + from .adapters import init_adapter from .dist import get_local_rank, get_rank, get_world_size from .model import accel_model, init_model from .session import BasicSessionManagerWithHistory from .utils import BasicStreamer, TerminalIO, control -logger = logging.getLogger(__name__) - def set_logging(log_file: str, debug: bool): torch.set_printoptions(linewidth=120) @@ -69,15 +69,19 @@ def set_logging(log_file: str, debug: bool): log_file = log_file or 'chat.log' if r := get_rank() != 0: log_file = log_file + f'.{r}' - logging.basicConfig(level=level, - format=('%(filename)s: ' - '%(levelname)s: ' - '%(funcName)s(): ' - '%(lineno)d:\t' - '%(message)s'), - filename=log_file, - filemode='w') + format = '%(filename)s: \ + %(levelname)s: \ + %(funcName)s(): \ + %(lineno)d:\t \ + %(message)s' + + logger = get_logger(__name__, + log_file=log_file, + log_level=level, + file_mode='w', + log_formatter=format) print(f'Worker {get_rank()} logging to {log_file}') + return logger def main( @@ -120,7 +124,7 @@ def main( based on `LlamaforCausalLM` class, this argument is required. Currently, only "llama1" is acceptable for llama1 models. """ # noqa: E501 - set_logging(log_file, debug) + logger = set_logging(log_file, debug) # workers should sync in sampling torch.manual_seed(seed) diff --git a/lmdeploy/legacy/pytorch/decode.py b/lmdeploy/legacy/pytorch/decode.py index 594c7807b..25e296240 100644 --- a/lmdeploy/legacy/pytorch/decode.py +++ b/lmdeploy/legacy/pytorch/decode.py @@ -1,6 +1,5 @@ # Copyright (c) OpenMMLab. All rights reserved. import argparse -import logging import queue import warnings from typing import List, Optional @@ -12,6 +11,8 @@ from transformers import (AutoTokenizer, PreTrainedModel, PreTrainedTokenizerBase) +from lmdeploy.utils import get_logger + from .model import accel_model, init_model @@ -371,7 +372,7 @@ def __del__(self): test_path = args.test_path prompts = args.prompts - logger = logging.getLogger(__name__) + logger = get_logger(__name__) # logging.basicConfig(level=logging.DEBUG) # Use test file preferentially diff --git a/lmdeploy/legacy/pytorch/model.py b/lmdeploy/legacy/pytorch/model.py index 4a0476ff5..1982adf48 100644 --- a/lmdeploy/legacy/pytorch/model.py +++ b/lmdeploy/legacy/pytorch/model.py @@ -1,5 +1,4 @@ # Copyright (c) OpenMMLab. All rights reserved. -import logging import time import warnings from typing import Optional @@ -7,9 +6,11 @@ import torch from transformers import AutoModelForCausalLM, AutoTokenizer +from lmdeploy.utils import get_logger + from .dist import get_local_rank -logger = logging.getLogger(__name__) +logger = get_logger(__name__) class LoadWoInit: diff --git a/lmdeploy/legacy/pytorch/session.py b/lmdeploy/legacy/pytorch/session.py index 4689b258b..3ccad0946 100644 --- a/lmdeploy/legacy/pytorch/session.py +++ b/lmdeploy/legacy/pytorch/session.py @@ -1,10 +1,11 @@ # Copyright (c) OpenMMLab. All rights reserved. -import logging import torch from transformers.generation.utils import ModelOutput -logger = logging.getLogger(__name__) +from lmdeploy.utils import get_logger + +logger = get_logger(__name__) class BasicSessionManager: diff --git a/lmdeploy/legacy/pytorch/utils.py b/lmdeploy/legacy/pytorch/utils.py index 8f88e3279..90a645956 100644 --- a/lmdeploy/legacy/pytorch/utils.py +++ b/lmdeploy/legacy/pytorch/utils.py @@ -1,9 +1,9 @@ # Copyright (c) OpenMMLab. All rights reserved. -import logging - from transformers.generation.streamers import BaseStreamer +from lmdeploy.utils import get_logger + from .dist import get_rank, master_only, master_only_and_broadcast_general try: @@ -11,7 +11,7 @@ except ImportError: # readline not available pass -logger = logging.getLogger(__name__) +logger = get_logger(__name__) class TerminalIO: diff --git a/lmdeploy/pytorch/modeling/modeling_baichuan.py b/lmdeploy/pytorch/modeling/modeling_baichuan.py index 0292cf41e..e5fb0360b 100644 --- a/lmdeploy/pytorch/modeling/modeling_baichuan.py +++ b/lmdeploy/pytorch/modeling/modeling_baichuan.py @@ -27,13 +27,13 @@ from transformers.activations import ACT2FN from transformers.modeling_outputs import (BaseModelOutputWithPast, CausalLMOutputWithPast) -from transformers.utils import logging from lmdeploy.pytorch.modeling.convert_to_qmodules import convert_to_qmodules +from lmdeploy.utils import get_logger from .configuration_baichuan import BaiChuanConfig -logger = logging.get_logger(__name__) +logger = get_logger(__name__) # Copied from transformers.models.bart.modeling_bart._make_causal_mask diff --git a/lmdeploy/pytorch/modeling/modeling_internlm.py b/lmdeploy/pytorch/modeling/modeling_internlm.py index 64c982bff..da7a167d2 100644 --- a/lmdeploy/pytorch/modeling/modeling_internlm.py +++ b/lmdeploy/pytorch/modeling/modeling_internlm.py @@ -33,14 +33,15 @@ SequenceClassifierOutputWithPast) from transformers.modeling_utils import PreTrainedModel from transformers.utils import (add_start_docstrings, - add_start_docstrings_to_model_forward, logging, + add_start_docstrings_to_model_forward, replace_return_docstrings) from lmdeploy.pytorch.modeling.convert_to_qmodules import convert_to_qmodules +from lmdeploy.utils import get_logger from .configuration_internlm import InternLMConfig -logger = logging.get_logger(__name__) +logger = get_logger(__name__) _CONFIG_FOR_DOC = 'InternLMConfig' diff --git a/lmdeploy/pytorch/modeling/modeling_internlm2.py b/lmdeploy/pytorch/modeling/modeling_internlm2.py index a5240a6ad..a29c37b2f 100644 --- a/lmdeploy/pytorch/modeling/modeling_internlm2.py +++ b/lmdeploy/pytorch/modeling/modeling_internlm2.py @@ -34,10 +34,11 @@ SequenceClassifierOutputWithPast) from transformers.modeling_utils import PreTrainedModel from transformers.utils import (add_start_docstrings, - add_start_docstrings_to_model_forward, logging, + add_start_docstrings_to_model_forward, replace_return_docstrings) from lmdeploy.pytorch.modeling.convert_to_qmodules import convert_to_qmodules +from lmdeploy.utils import get_logger try: from transformers.generation.streamers import BaseStreamer @@ -46,7 +47,7 @@ from .configuration_internlm import InternLMConfig as InternLM2Config -logger = logging.get_logger(__name__) +logger = get_logger(__name__) _CONFIG_FOR_DOC = 'InternLM2Config' diff --git a/lmdeploy/pytorch/modeling/modeling_llama.py b/lmdeploy/pytorch/modeling/modeling_llama.py index 68e0e5a8a..efe66123a 100644 --- a/lmdeploy/pytorch/modeling/modeling_llama.py +++ b/lmdeploy/pytorch/modeling/modeling_llama.py @@ -32,12 +32,13 @@ from transformers.modeling_utils import PreTrainedModel from transformers.models.llama.configuration_llama import LlamaConfig from transformers.utils import (add_start_docstrings, - add_start_docstrings_to_model_forward, logging, + add_start_docstrings_to_model_forward, replace_return_docstrings) from lmdeploy.pytorch.modeling.convert_to_qmodules import convert_to_qmodules +from lmdeploy.utils import get_logger -logger = logging.get_logger(__name__) +logger = get_logger(__name__) _CONFIG_FOR_DOC = 'LlamaConfig' diff --git a/lmdeploy/pytorch/models/falcon.py b/lmdeploy/pytorch/models/falcon.py index 9d18d9eaa..b708c0dbb 100644 --- a/lmdeploy/pytorch/models/falcon.py +++ b/lmdeploy/pytorch/models/falcon.py @@ -3,7 +3,6 @@ # https://huggingface.co/tiiuae/falcon-7b-instruct # https://github.com/huggingface/transformers/blob/v4.33-release/src/transformers/models/falcon/modeling_falcon.py # noqa -import logging from typing import Optional, Tuple, Union import torch @@ -20,8 +19,6 @@ from ..kernels import (alibi_paged_attention_fwd, fill_kv_cache, paged_attention_fwd) -logger = logging.getLogger() - # rotary pos emb helpers # (torch.jit.script does not seem to support staticmethod...) diff --git a/lmdeploy/serve/qos_engine/inner_group_schd.py b/lmdeploy/serve/qos_engine/inner_group_schd.py index 73a57e1ef..bc537c034 100644 --- a/lmdeploy/serve/qos_engine/inner_group_schd.py +++ b/lmdeploy/serve/qos_engine/inner_group_schd.py @@ -1,8 +1,9 @@ # Copyright (c) OpenMMLab. All rights reserved. import collections -import logging -logger = logging.getLogger(__name__) +from lmdeploy.utils import get_logger + +logger = get_logger(__name__) class UserRequestQueue: diff --git a/lmdeploy/serve/qos_engine/qos_engine.py b/lmdeploy/serve/qos_engine/qos_engine.py index ca5762de5..da2e65012 100644 --- a/lmdeploy/serve/qos_engine/qos_engine.py +++ b/lmdeploy/serve/qos_engine/qos_engine.py @@ -1,7 +1,6 @@ # Copyright (c) OpenMMLab. All rights reserved. import asyncio import json -import logging import threading import time from typing import List @@ -11,8 +10,9 @@ GenerateRequestQos) from lmdeploy.serve.qos_engine.inner_group_schd import UserRequestQueue from lmdeploy.serve.qos_engine.usage_stats import UsageStats +from lmdeploy.utils import get_logger -logger = logging.getLogger(__name__) +logger = get_logger(__name__) class QosConfig: diff --git a/lmdeploy/serve/turbomind/chatbot.py b/lmdeploy/serve/turbomind/chatbot.py index f0fc9e2e9..7d350c616 100644 --- a/lmdeploy/serve/turbomind/chatbot.py +++ b/lmdeploy/serve/turbomind/chatbot.py @@ -18,7 +18,7 @@ from lmdeploy.model import MODELS from lmdeploy.serve.turbomind.utils import (Postprocessor, Preprocessor, prepare_tensor) -from lmdeploy.utils import filter_suffix +from lmdeploy.utils import filter_suffix, get_logger @dataclass @@ -51,13 +51,6 @@ def stream_callback(que, result, error): que.put(result.get_response(as_json=True)) -def get_logger(log_file=None, log_level=logging.INFO): - """Return the logger.""" - from lmdeploy.utils import get_logger - logger = get_logger('service.ft', log_file=log_file, log_level=log_level) - return logger - - class Chatbot: """Chatbot for LLaMA series models with turbomind as inference engine. @@ -130,7 +123,7 @@ def stream_infer(self, assert isinstance(session_id, int), \ f'INT session id is required, but got {type(session_id)}' - logger = get_logger(log_level=self.log_level) + logger = get_logger('service.ft', log_level=self.log_level) logger.info(f'session {session_id}, request_id {request_id}, ' f'request_output_len {request_output_len}') @@ -180,7 +173,7 @@ def end(self, session_id: int, *args, **kwargs): assert isinstance(session_id, int), \ f'INT session id is required, but got {type(session_id)}' - logger = get_logger(log_level=self.log_level) + logger = get_logger('service.ft', log_level=self.log_level) logger.info(f'end session: {session_id}') if self._session is None: @@ -218,7 +211,7 @@ def cancel(self, session_id: int, *args, **kwargs): """ assert isinstance(session_id, int), \ f'INT session id is required, but got {type(session_id)}' - logger = get_logger(log_level=self.log_level) + logger = get_logger('service.ft', log_level=self.log_level) logger.info(f'cancel session: {session_id}') if self._session is None: @@ -267,7 +260,7 @@ def resume(self, session_id: int, *args, **kwargs): assert isinstance(session_id, int), \ f'INT session id is required, but got {type(session_id)}' - logger = get_logger(log_level=self.log_level) + logger = get_logger('service.ft', log_level=self.log_level) logger.info(f'resume session: {session_id}') if self._session is None: @@ -320,7 +313,7 @@ def infer(self, assert isinstance(session_id, int), \ f'INT session id is required, but got {type(session_id)}' - logger = get_logger(log_level=self.log_level) + logger = get_logger('service.ft', log_level=self.log_level) logger.info(f'session {session_id}, request_id {request_id}, ' f'request_output_len {request_output_len}') @@ -434,7 +427,7 @@ def _stream_infer(self, Yields: tuple: status, text, generated token number """ - logger = get_logger(log_level=self.log_level) + logger = get_logger('service.ft', log_level=self.log_level) logger.info(f'session {session.session_id}, ' f'request id {session.request_id}, ' f'request_output_len {request_output_len}, ' diff --git a/lmdeploy/turbomind/turbomind.py b/lmdeploy/turbomind/turbomind.py index c1dec981f..6af9764e2 100644 --- a/lmdeploy/turbomind/turbomind.py +++ b/lmdeploy/turbomind/turbomind.py @@ -35,7 +35,7 @@ sys.path.append(osp.join(lmdeploy_dir, 'lib')) import _turbomind as _tm # noqa: E402 -logger = logging.getLogger(__name__) +logger = get_logger(__name__) def _construct_stop_or_bad_words(words: List[int] = None): @@ -72,9 +72,8 @@ def _update_engine_config(config: TurbomindEngineConfig, **kwargs): for k, v in kwargs.items(): if v and hasattr(config, k): setattr(config, k, v) - get_logger('turbomind').warning( - f'kwargs {k} is deprecated to initialize model, ' - 'use TurbomindEngineConfig instead.') + logger.warning(f'kwargs {k} is deprecated to initialize model, ' + 'use TurbomindEngineConfig instead.') if config.model_name is not None: logger.warning('model_name is deprecated in TurbomindEngineConfig ' 'and has no effect') @@ -92,8 +91,7 @@ def _update_tm_config(dst: TurbomindModelConfig, src: TurbomindEngineConfig): def _compare_individual_gpu_memory(tp: int): - logging.basicConfig(level=logging.INFO) - + logger.setLevel(level=logging.INFO) try: total_mem = [] free_mem = [] @@ -108,12 +106,12 @@ def _compare_individual_gpu_memory(tp: int): all_free_equal = all(free == free_mem[0] for free in free_mem) if not all_total_equal or not all_free_equal: - logging.warning( + logger.warning( f'Memory discrepancy detected: Total Memory={total_mem} MB, \ Free Memory={free_mem} MB') except Exception as e: - logging.error(f'An exception occurred: {e}') + logger.error(f'An exception occurred: {e}') @contextmanager @@ -170,7 +168,7 @@ def _catch_args(**kwargs): args = _catch_args(**kwargs, model_format=model_format, tp=tp) if len(args) > 0: - get_logger('turbomind').warning( + logger.warning( f'loading from workspace, ignore args {args} ' 'please use TurbomindEngineConfig or modify config.ini') @@ -360,8 +358,7 @@ def _from_workspace(self, model_path: str, # check whether input tp is valid if cfg.tensor_para_size != 1 and \ self.gpu_count != cfg.tensor_para_size: - get_logger('turbomind').info( - f'found tp={cfg.tensor_para_size} in config.ini.') + logger.info(f'found tp={cfg.tensor_para_size} in config.ini.') self.gpu_count = cfg.tensor_para_size # update cfg @@ -537,9 +534,8 @@ def _update_generation_config(self, config: EngineGenerationConfig, config.max_new_tokens = kwargs['request_output_len'] deprecated_kwargs.append('request_output_len') for k in deprecated_kwargs: - get_logger('turbomind').warning( - f'kwargs {k} is deprecated for inference, ' - 'use GenerationConfig instead.') + logger.warning(f'kwargs {k} is deprecated for inference, ' + 'use GenerationConfig instead.') return config def end(self, session_id: int): diff --git a/lmdeploy/turbomind/utils.py b/lmdeploy/turbomind/utils.py index c8ec34cf2..7fa6513d2 100644 --- a/lmdeploy/turbomind/utils.py +++ b/lmdeploy/turbomind/utils.py @@ -1,13 +1,14 @@ # Copyright (c) OpenMMLab. All rights reserved. import dataclasses import json -import logging import os from huggingface_hub import hf_hub_download from transformers.utils import ExplicitEnum -logger = logging.getLogger(__name__) +from lmdeploy.utils import get_logger + +logger = get_logger(__name__) class ModelSource(ExplicitEnum): diff --git a/lmdeploy/utils.py b/lmdeploy/utils.py index 39e4c5062..d4a861c95 100644 --- a/lmdeploy/utils.py +++ b/lmdeploy/utils.py @@ -1,14 +1,18 @@ # Copyright (c) OpenMMLab. All rights reserved. import logging +from logging import Logger from typing import List, Optional logger_initialized = {} -def get_logger(name: str, - log_file: Optional[str] = None, - log_level: int = logging.INFO, - file_mode: str = 'w'): +def get_logger( + name: Optional[str] = None, + log_file: Optional[str] = None, + log_level: int = logging.INFO, + file_mode: str = 'w', + log_formatter: str = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' +) -> Logger: """Initialize and get a logger by name. If the logger has not been initialized, this method will initialize the @@ -22,25 +26,10 @@ def get_logger(name: str, log_level (int): The logger level. file_mode (str): The file mode used in opening log file. Defaults to 'w'. + log_formatter (str): The logger output format. Returns: logging.Logger: The expected logger. """ - # use logger in mmengine if exists. - try: - from mmengine.logging import MMLogger - if MMLogger.check_instance_created(name): - logger = MMLogger.get_instance(name) - else: - logger = MMLogger.get_instance(name, - logger_name=name, - log_file=log_file, - log_level=log_level, - file_mode=file_mode) - return logger - - except Exception: - pass - logger = logging.getLogger(name) if name in logger_initialized: return logger @@ -66,8 +55,7 @@ def get_logger(name: str, file_handler = logging.FileHandler(log_file, file_mode) handlers.append(file_handler) - formatter = logging.Formatter( - '%(asctime)s - %(name)s - %(levelname)s - %(message)s') + formatter = logging.Formatter(log_formatter) for handler in handlers: handler.setFormatter(formatter) handler.setLevel(log_level)