From 44dc14b3c13223ddbbef44b56f56b260da600479 Mon Sep 17 00:00:00 2001 From: yiliu30 Date: Sun, 28 Jan 2024 22:46:50 +0800 Subject: [PATCH 1/7] fixed log Signed-off-by: yiliu30 --- neural_compressor/common/__init__.py | 22 +----- neural_compressor/common/base_config.py | 4 +- neural_compressor/common/base_tuning.py | 4 +- neural_compressor/common/utils/logger.py | 68 ++----------------- .../algorithms/weight_only/algo_entry.py | 4 +- .../onnxrt/quantization/config.py | 4 +- .../onnxrt/quantization/quantize.py | 4 +- neural_compressor/onnxrt/utils/onnx_model.py | 4 +- neural_compressor/onnxrt/utils/utility.py | 4 +- .../algorithms/static_quantize/keras.py | 4 +- .../tensorflow/quantization/quantize.py | 4 +- .../torch/algorithms/weight_only_algos.py | 4 +- .../torch/quantization/autotune.py | 5 +- .../torch/quantization/quantize.py | 4 +- neural_compressor/torch/utils/utility.py | 4 +- .../quantization/weight_only/test_rtn.py | 4 +- test/3x/onnxrt/test_config.py | 4 +- test/3x/tensorflow/test_config.py | 4 +- .../weight_only/test_gptq_algo.py | 4 +- .../quantization/weight_only/test_rtn.py | 4 +- test/3x/torch/test_autotune.py | 9 +-- test/3x/torch/test_config.py | 6 +- test/3x/torch/test_utils.py | 4 +- 23 files changed, 32 insertions(+), 150 deletions(-) diff --git a/neural_compressor/common/__init__.py b/neural_compressor/common/__init__.py index 5f1b4d2f866..54d0fa61d29 100644 --- a/neural_compressor/common/__init__.py +++ b/neural_compressor/common/__init__.py @@ -14,37 +14,19 @@ from neural_compressor.common.utils import ( level, - log, - info, - DEBUG, - debug, - warn, - warning, - error, - fatal, + logger, set_random_seed, set_workspace, set_resume_from, set_tensorboard, - Logger, - logger, ) from neural_compressor.common.base_config import options __all__ = [ "level", - "log", - "info", - "DEBUG", - "debug", - "warn", - "warning", - "error", - "fatal", - "options", - "Logger", "logger", + "options", "set_workspace", "set_random_seed", "set_resume_from", diff --git a/neural_compressor/common/base_config.py b/neural_compressor/common/base_config.py index dd0236f39c3..9cb752ef8d8 100644 --- a/neural_compressor/common/base_config.py +++ b/neural_compressor/common/base_config.py @@ -24,7 +24,7 @@ from itertools import product from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union -from neural_compressor.common import Logger +from neural_compressor.common import logger from neural_compressor.common.utils import ( BASE_CONFIG, COMPOSABLE_CONFIG, @@ -36,8 +36,6 @@ OP_NAME_OR_MODULE_TYPE, ) -logger = Logger().get_logger() - __all__ = [ "ConfigRegistry", "register_config", diff --git a/neural_compressor/common/base_tuning.py b/neural_compressor/common/base_tuning.py index 6145c49379b..6d3e0ed59ad 100644 --- a/neural_compressor/common/base_tuning.py +++ b/neural_compressor/common/base_tuning.py @@ -18,11 +18,9 @@ import uuid from typing import Any, Callable, Dict, Generator, List, Optional, Tuple, Union -from neural_compressor.common import Logger +from neural_compressor.common import logger from neural_compressor.common.base_config import BaseConfig, ComposableConfig -logger = Logger().get_logger() - __all__ = [ "Evaluator", "TuningConfig", diff --git a/neural_compressor/common/utils/logger.py b/neural_compressor/common/utils/logger.py index dd8cbd5388a..8761c05fcdf 100644 --- a/neural_compressor/common/utils/logger.py +++ b/neural_compressor/common/utils/logger.py @@ -19,6 +19,11 @@ import logging import os +__all__ = [ + "level", + "logger", +] + class Logger(object): """Logger class.""" @@ -70,65 +75,4 @@ def _pretty_dict(value, indent=0): level = Logger().get_logger().level DEBUG = logging.DEBUG - -def log(level, msg, *args, **kwargs): - """Output log with the level as a parameter.""" - if isinstance(msg, dict): - for _, line in enumerate(_pretty_dict(msg).split("\n")): - Logger().get_logger().log(level, line, *args, **kwargs) - else: - Logger().get_logger().log(level, msg, *args, **kwargs) - - -def debug(msg, *args, **kwargs): - """Output log with the debug level.""" - if isinstance(msg, dict): - for _, line in enumerate(_pretty_dict(msg).split("\n")): - Logger().get_logger().debug(line, *args, **kwargs) - else: - Logger().get_logger().debug(msg, *args, **kwargs) - - -def error(msg, *args, **kwargs): - """Output log with the error level.""" - if isinstance(msg, dict): - for _, line in enumerate(_pretty_dict(msg).split("\n")): - Logger().get_logger().error(line, *args, **kwargs) - else: - Logger().get_logger().error(msg, *args, **kwargs) - - -def fatal(msg, *args, **kwargs): - """Output log with the fatal level.""" - if isinstance(msg, dict): - for _, line in enumerate(_pretty_dict(msg).split("\n")): - Logger().get_logger().fatal(line, *args, **kwargs) - else: - Logger().get_logger().fatal(msg, *args, **kwargs) - - -def info(msg, *args, **kwargs): - """Output log with the info level.""" - if isinstance(msg, dict): - for _, line in enumerate(_pretty_dict(msg).split("\n")): - Logger().get_logger().info(line, *args, **kwargs) - else: - Logger().get_logger().info(msg, *args, **kwargs) - - -def warn(msg, *args, **kwargs): - """Output log with the warning level.""" - if isinstance(msg, dict): - for _, line in enumerate(_pretty_dict(msg).split("\n")): - Logger().get_logger().warning(line, *args, **kwargs) - else: - Logger().get_logger().warning(msg, *args, **kwargs) - - -def warning(msg, *args, **kwargs): - """Output log with the warning level (Alias of the method warn).""" - if isinstance(msg, dict): - for _, line in enumerate(_pretty_dict(msg).split("\n")): - Logger().get_logger().warning(line, *args, **kwargs) - else: - Logger().get_logger().warning(msg, *args, **kwargs) +logger = Logger().get_logger() diff --git a/neural_compressor/onnxrt/algorithms/weight_only/algo_entry.py b/neural_compressor/onnxrt/algorithms/weight_only/algo_entry.py index 671a90e2877..77a532cd107 100644 --- a/neural_compressor/onnxrt/algorithms/weight_only/algo_entry.py +++ b/neural_compressor/onnxrt/algorithms/weight_only/algo_entry.py @@ -18,13 +18,11 @@ import onnx -from neural_compressor.common import Logger +from neural_compressor.common import logger from neural_compressor.common.utils import RTN from neural_compressor.onnxrt.quantization.config import RTNConfig from neural_compressor.onnxrt.utils.utility import register_algo -logger = Logger().get_logger() - ###################### RTN Algo Entry ################################## @register_algo(name=RTN) diff --git a/neural_compressor/onnxrt/quantization/config.py b/neural_compressor/onnxrt/quantization/config.py index 12e97865149..7fb704976cb 100644 --- a/neural_compressor/onnxrt/quantization/config.py +++ b/neural_compressor/onnxrt/quantization/config.py @@ -23,12 +23,10 @@ import onnx -from neural_compressor.common import Logger +from neural_compressor.common import logger from neural_compressor.common.base_config import BaseConfig, register_config from neural_compressor.common.utils import DEFAULT_WHITE_LIST, OP_NAME_OR_MODULE_TYPE, RTN -logger = Logger().get_logger() - FRAMEWORK_NAME = "onnxrt" diff --git a/neural_compressor/onnxrt/quantization/quantize.py b/neural_compressor/onnxrt/quantization/quantize.py index f41a5770852..5f6a516b442 100644 --- a/neural_compressor/onnxrt/quantization/quantize.py +++ b/neural_compressor/onnxrt/quantization/quantize.py @@ -18,13 +18,11 @@ import onnx from onnxruntime.quantization import CalibrationDataReader -from neural_compressor.common import Logger +from neural_compressor.common import logger from neural_compressor.common.base_config import BaseConfig, ComposableConfig, config_registry from neural_compressor.onnxrt.quantization.config import FRAMEWORK_NAME from neural_compressor.onnxrt.utils.utility import algos_mapping -logger = Logger().get_logger() - def need_apply(quant_config: BaseConfig, algo_name): return quant_config.name == algo_name if hasattr(quant_config, "name") else False diff --git a/neural_compressor/onnxrt/utils/onnx_model.py b/neural_compressor/onnxrt/utils/onnx_model.py index 57af4da4f6e..1429dea4e40 100644 --- a/neural_compressor/onnxrt/utils/onnx_model.py +++ b/neural_compressor/onnxrt/utils/onnx_model.py @@ -22,11 +22,9 @@ import onnx -from neural_compressor.common import Logger +from neural_compressor.common import logger from neural_compressor.onnxrt.utils.utility import MAXIMUM_PROTOBUF, find_by_name -logger = Logger().get_logger() - class ONNXModel: """Build ONNX model.""" diff --git a/neural_compressor/onnxrt/utils/utility.py b/neural_compressor/onnxrt/utils/utility.py index 171bde657f2..c07ba8f5fe2 100644 --- a/neural_compressor/onnxrt/utils/utility.py +++ b/neural_compressor/onnxrt/utils/utility.py @@ -18,9 +18,7 @@ import onnx from packaging.version import Version -from neural_compressor.common import Logger - -logger = Logger().get_logger() +from neural_compressor.common import logger ONNXRT116_VERSION = Version("1.16.0") ONNXRT1161_VERSION = Version("1.16.1") diff --git a/neural_compressor/tensorflow/algorithms/static_quantize/keras.py b/neural_compressor/tensorflow/algorithms/static_quantize/keras.py index 3268f0779fb..5b8b1bcf842 100644 --- a/neural_compressor/tensorflow/algorithms/static_quantize/keras.py +++ b/neural_compressor/tensorflow/algorithms/static_quantize/keras.py @@ -26,11 +26,9 @@ import tensorflow as tf import yaml -from neural_compressor.common import Logger +from neural_compressor.common import logger from neural_compressor.tensorflow.utils import deep_get, dump_elapsed_time -logger = Logger().get_logger() - def _add_supported_quantized_objects(custom_objects): """Map all the quantized objects.""" diff --git a/neural_compressor/tensorflow/quantization/quantize.py b/neural_compressor/tensorflow/quantization/quantize.py index 0e20b5fb221..11f8ed68523 100644 --- a/neural_compressor/tensorflow/quantization/quantize.py +++ b/neural_compressor/tensorflow/quantization/quantize.py @@ -16,14 +16,12 @@ import tensorflow as tf -from neural_compressor.common import Logger +from neural_compressor.common import logger from neural_compressor.common.base_config import BaseConfig from neural_compressor.common.utils import STATIC_QUANT from neural_compressor.tensorflow.quantization.config import parse_config_from_dict from neural_compressor.tensorflow.utils import algos_mapping -logger = Logger().get_logger() - def quantize_model( model: tf.keras.Model, quant_config: BaseConfig, calib_dataloader: Callable = None, calib_iteration: int = 100 diff --git a/neural_compressor/torch/algorithms/weight_only_algos.py b/neural_compressor/torch/algorithms/weight_only_algos.py index 840973aae3d..e27f85c5292 100644 --- a/neural_compressor/torch/algorithms/weight_only_algos.py +++ b/neural_compressor/torch/algorithms/weight_only_algos.py @@ -17,13 +17,11 @@ import torch -from neural_compressor.common import Logger +from neural_compressor.common import logger from neural_compressor.common.utils import GPTQ, RTN from neural_compressor.torch.quantization.config import GPTQConfig, RTNConfig from neural_compressor.torch.utils.utility import fetch_module, register_algo, set_module -logger = Logger().get_logger() - ###################### RTN Algo Entry ################################## @register_algo(name=RTN) diff --git a/neural_compressor/torch/quantization/autotune.py b/neural_compressor/torch/quantization/autotune.py index bb48f0685c6..35e22807839 100644 --- a/neural_compressor/torch/quantization/autotune.py +++ b/neural_compressor/torch/quantization/autotune.py @@ -17,15 +17,12 @@ import torch -from neural_compressor.common import Logger +from neural_compressor.common import logger from neural_compressor.common.base_config import BaseConfig, get_all_config_set_from_config_registry from neural_compressor.common.base_tuning import TuningConfig, evaluator, init_tuning from neural_compressor.torch import quantize from neural_compressor.torch.quantization.config import FRAMEWORK_NAME -logger = Logger().get_logger() - - __all__ = [ "autotune", "get_all_config_set", diff --git a/neural_compressor/torch/quantization/quantize.py b/neural_compressor/torch/quantization/quantize.py index 297e57ab160..cc8881a4606 100644 --- a/neural_compressor/torch/quantization/quantize.py +++ b/neural_compressor/torch/quantization/quantize.py @@ -17,13 +17,11 @@ import torch -from neural_compressor.common import Logger +from neural_compressor.common import logger from neural_compressor.common.base_config import BaseConfig, ComposableConfig, config_registry from neural_compressor.torch.quantization.config import FRAMEWORK_NAME from neural_compressor.torch.utils.utility import WHITE_MODULE_LIST, algos_mapping, get_model_info -logger = Logger().get_logger() - def need_apply(configs_mapping: Dict[Tuple[str, callable], BaseConfig], algo_name): return any(config.name == algo_name for config in configs_mapping.values()) diff --git a/neural_compressor/torch/utils/utility.py b/neural_compressor/torch/utils/utility.py index 035fc70fd0e..48e94d0128e 100644 --- a/neural_compressor/torch/utils/utility.py +++ b/neural_compressor/torch/utils/utility.py @@ -15,9 +15,7 @@ from typing import Callable, Dict, List, Tuple -from neural_compressor.common import Logger - -logger = Logger().get_logger() +from neural_compressor.common import logger # Dictionary to store a mapping between algorithm names and corresponding algo implementation(function) algos_mapping: Dict[str, Callable] = {} diff --git a/test/3x/onnxrt/quantization/weight_only/test_rtn.py b/test/3x/onnxrt/quantization/weight_only/test_rtn.py index 5fbdcc240f4..13521b61e99 100644 --- a/test/3x/onnxrt/quantization/weight_only/test_rtn.py +++ b/test/3x/onnxrt/quantization/weight_only/test_rtn.py @@ -4,9 +4,7 @@ from optimum.exporters.onnx import main_export -from neural_compressor.common import Logger - -logger = Logger().get_logger() +from neural_compressor.common import logger def find_onnx_file(folder_path): diff --git a/test/3x/onnxrt/test_config.py b/test/3x/onnxrt/test_config.py index dfc8f00dea5..6f07f8092b9 100644 --- a/test/3x/onnxrt/test_config.py +++ b/test/3x/onnxrt/test_config.py @@ -7,9 +7,7 @@ import onnx from optimum.exporters.onnx import main_export -from neural_compressor.common import Logger - -logger = Logger().get_logger() +from neural_compressor.common import logger def find_onnx_file(folder_path): diff --git a/test/3x/tensorflow/test_config.py b/test/3x/tensorflow/test_config.py index 6a7bd7afeab..0d343f44ed6 100644 --- a/test/3x/tensorflow/test_config.py +++ b/test/3x/tensorflow/test_config.py @@ -25,9 +25,7 @@ import tensorflow as tf from tensorflow import keras -from neural_compressor.common import Logger - -logger = Logger().get_logger() +from neural_compressor.common import logger def build_model(): diff --git a/test/3x/torch/quantization/weight_only/test_gptq_algo.py b/test/3x/torch/quantization/weight_only/test_gptq_algo.py index 79f522ae675..4ed381c23a3 100644 --- a/test/3x/torch/quantization/weight_only/test_gptq_algo.py +++ b/test/3x/torch/quantization/weight_only/test_gptq_algo.py @@ -3,9 +3,7 @@ import torch -from neural_compressor.common import Logger - -logger = Logger().get_logger() +from neural_compressor.common import logger def get_gpt_j(): diff --git a/test/3x/torch/quantization/weight_only/test_rtn.py b/test/3x/torch/quantization/weight_only/test_rtn.py index 09321525a25..827851649ba 100644 --- a/test/3x/torch/quantization/weight_only/test_rtn.py +++ b/test/3x/torch/quantization/weight_only/test_rtn.py @@ -2,9 +2,7 @@ import torch -from neural_compressor.common import Logger - -logger = Logger().get_logger() +from neural_compressor.common import logger def get_gpt_j(): diff --git a/test/3x/torch/test_autotune.py b/test/3x/torch/test_autotune.py index e1b717e3163..4f0003b7157 100644 --- a/test/3x/torch/test_autotune.py +++ b/test/3x/torch/test_autotune.py @@ -1,13 +1,10 @@ import unittest - -import transformers - -from neural_compressor.common import Logger - -logger = Logger().get_logger() from functools import wraps import torch +import transformers + +from neural_compressor.common import logger def reset_tuning_target(test_func): diff --git a/test/3x/torch/test_config.py b/test/3x/torch/test_config.py index ff1012dc1ed..440052e7a0f 100644 --- a/test/3x/torch/test_config.py +++ b/test/3x/torch/test_config.py @@ -1,12 +1,10 @@ import copy import unittest +import torch import transformers -from neural_compressor.common import Logger - -logger = Logger().get_logger() -import torch +from neural_compressor.common import logger def build_simple_torch_model(): diff --git a/test/3x/torch/test_utils.py b/test/3x/torch/test_utils.py index 21a65bc96c7..8661fc602ae 100644 --- a/test/3x/torch/test_utils.py +++ b/test/3x/torch/test_utils.py @@ -2,9 +2,7 @@ import torch -from neural_compressor.common import Logger - -logger = Logger().get_logger() +from neural_compressor.common import logger def get_gpt_j(): From be90f9b5f7e781057d421fd5f9eb32e3cb2f55a9 Mon Sep 17 00:00:00 2001 From: yiliu30 Date: Sun, 28 Jan 2024 22:58:40 +0800 Subject: [PATCH 2/7] add Logger back Signed-off-by: yiliu30 --- neural_compressor/common/__init__.py | 1 + neural_compressor/common/utils/logger.py | 1 + test/3x/torch/test_logger.py | 12 +++++++++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/neural_compressor/common/__init__.py b/neural_compressor/common/__init__.py index 54d0fa61d29..dd25d0dbd9b 100644 --- a/neural_compressor/common/__init__.py +++ b/neural_compressor/common/__init__.py @@ -15,6 +15,7 @@ from neural_compressor.common.utils import ( level, logger, + Logger, set_random_seed, set_workspace, set_resume_from, diff --git a/neural_compressor/common/utils/logger.py b/neural_compressor/common/utils/logger.py index 8761c05fcdf..e4f835cce5e 100644 --- a/neural_compressor/common/utils/logger.py +++ b/neural_compressor/common/utils/logger.py @@ -22,6 +22,7 @@ __all__ = [ "level", "logger", + "Logger", ] diff --git a/test/3x/torch/test_logger.py b/test/3x/torch/test_logger.py index c091eba8db4..8783d8d8f91 100644 --- a/test/3x/torch/test_logger.py +++ b/test/3x/torch/test_logger.py @@ -1,7 +1,9 @@ """Tests for logging utilities.""" import unittest -from neural_compressor.common import logger +from neural_compressor.common import Logger, logger + +inc_logger = Logger().get_logger() # `inc_logger` is the same as `logger` class TestLogger(unittest.TestCase): @@ -16,8 +18,6 @@ def test_logger(self): logger.fatal({"msg": "call logger fatal function"}) logger.info("call logger info function") logger.info({"msg": "call logger info function."}) - logger.warn("call logger warn function") - logger.warn({"msg": "call logger warn function"}) logger.warning("call logger warning function") logger.warning({"msg": "call logger warning function"}) logger.warning(["call logger warning function", "done"]) @@ -29,6 +29,12 @@ def test_logger(self): logger.warning(({"msg": "call logger warning function"}, {"msg2": "done"})) logger.warning(({"msg": [{"sub_msg": "call logger"}, {"sub_msg2": "call warning function"}]}, {"msg2": "done"})) + def test_in_logger(self): + inc_logger.log(0, "call logger log function.") + inc_logger.log(1, {"msg": "call logger log function."}) + inc_logger.debug("call logger debug function.") + inc_logger.debug({"msg": "call logger debug function."}) + if __name__ == "__main__": unittest.main() From db9dca470ad2a601e5595eaf8c323bd7b6fd0b57 Mon Sep 17 00:00:00 2001 From: yiliu30 Date: Sun, 28 Jan 2024 23:07:28 +0800 Subject: [PATCH 3/7] remove the unused import Signed-off-by: yiliu30 --- neural_compressor/torch/quantization/layers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neural_compressor/torch/quantization/layers.py b/neural_compressor/torch/quantization/layers.py index b1ce99d3a59..67c96dd54cc 100644 --- a/neural_compressor/torch/quantization/layers.py +++ b/neural_compressor/torch/quantization/layers.py @@ -25,7 +25,7 @@ from torch.autograd import Function from torch.nn import functional as F -from neural_compressor.common import DEBUG, level, logger +from neural_compressor.common import logger from neural_compressor.torch.algorithms.weight_only import quant_tensor From 53c9fe66ff090f3ed7d09230bac08c99a49cd992 Mon Sep 17 00:00:00 2001 From: yiliu30 Date: Mon, 29 Jan 2024 07:38:09 +0800 Subject: [PATCH 4/7] add log func back Signed-off-by: yiliu30 --- neural_compressor/common/utils/__init__.py | 3 +- neural_compressor/common/utils/logger.py | 65 +++++++++++++++++++++- test/3x/torch/test_logger.py | 17 ++++++ 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/neural_compressor/common/utils/__init__.py b/neural_compressor/common/utils/__init__.py index 4c248c453af..645b4639f5f 100644 --- a/neural_compressor/common/utils/__init__.py +++ b/neural_compressor/common/utils/__init__.py @@ -14,4 +14,5 @@ from neural_compressor.common.utils.constants import * from neural_compressor.common.utils.utility import * -from neural_compressor.common.utils.logger import * +from neural_compressor.common.utils.logger import Logger +from neural_compressor.common.utils.logger import logger, debug, log, info, warning, error, fatal diff --git a/neural_compressor/common/utils/logger.py b/neural_compressor/common/utils/logger.py index e4f835cce5e..33192931a35 100644 --- a/neural_compressor/common/utils/logger.py +++ b/neural_compressor/common/utils/logger.py @@ -22,6 +22,16 @@ __all__ = [ "level", "logger", + "debug", + "log", + "info", + "log", + "info", + "debug", + "warning", + "error", + "fatal", + "Logger", "Logger", ] @@ -73,7 +83,60 @@ def _pretty_dict(value, indent=0): return repr(value) +def log(level, msg, *args, **kwargs): + """Output log with the level as a parameter.""" + if isinstance(msg, dict): + for _, line in enumerate(_pretty_dict(msg).split("\n")): + Logger().get_logger().log(level, line, *args, **kwargs, stacklevel=2) + else: + Logger().get_logger().log(level, msg, *args, **kwargs, stacklevel=2) + + +def debug(msg, *args, **kwargs): + """Output log with the debug level.""" + if isinstance(msg, dict): + for _, line in enumerate(_pretty_dict(msg).split("\n")): + Logger().get_logger().debug(line, *args, **kwargs, stacklevel=2) + else: + Logger().get_logger().debug(msg, *args, **kwargs, stacklevel=2) + + +def error(msg, *args, **kwargs): + """Output log with the error level.""" + if isinstance(msg, dict): + for _, line in enumerate(_pretty_dict(msg).split("\n")): + Logger().get_logger().error(line, *args, **kwargs, stacklevel=2) + else: + Logger().get_logger().error(msg, *args, **kwargs, stacklevel=2) + + +def fatal(msg, *args, **kwargs): + """Output log with the fatal level.""" + if isinstance(msg, dict): + for _, line in enumerate(_pretty_dict(msg).split("\n")): + Logger().get_logger().fatal(line, *args, **kwargs, stacklevel=2) + else: + Logger().get_logger().fatal(msg, *args, **kwargs, stacklevel=2) + + +def info(msg, *args, **kwargs): + """Output log with the info level.""" + if isinstance(msg, dict): + for _, line in enumerate(_pretty_dict(msg).split("\n")): + Logger().get_logger().info(line, *args, **kwargs, stacklevel=2) + else: + Logger().get_logger().info(msg, *args, **kwargs, stacklevel=2) + + +def warning(msg, *args, **kwargs): + """Output log with the warning level (Alias of the method warn).""" + if isinstance(msg, dict): + for _, line in enumerate(_pretty_dict(msg).split("\n")): + Logger().get_logger().warning(line, *args, **kwargs, stacklevel=2) + else: + Logger().get_logger().warning(msg, *args, **kwargs, stacklevel=2) + + level = Logger().get_logger().level -DEBUG = logging.DEBUG logger = Logger().get_logger() diff --git a/test/3x/torch/test_logger.py b/test/3x/torch/test_logger.py index 8783d8d8f91..e8605943a86 100644 --- a/test/3x/torch/test_logger.py +++ b/test/3x/torch/test_logger.py @@ -5,6 +5,15 @@ inc_logger = Logger().get_logger() # `inc_logger` is the same as `logger` +msg_lst = [ + "call logger log function.", + {"msg": "call logger warning function"}, + ["call logger warning function", "done"], + {"msg": {("bert", "embedding"): {"weight": {"dtype": ["unint8", "int8"]}}}}, + {"msg": [{"sub_msg": "call logger"}, {"sub_msg2": "call warning function"}]}, + {"msg2": "done"}, +] + class TestLogger(unittest.TestCase): def test_logger(self): @@ -35,6 +44,14 @@ def test_in_logger(self): inc_logger.debug("call logger debug function.") inc_logger.debug({"msg": "call logger debug function."}) + def test_logger_func(self): + from neural_compressor.common.utils import debug, info, log + + for msg in msg_lst: + debug(msg) + log(level=1, msg=msg) + info(msg) + if __name__ == "__main__": unittest.main() From e0185ed4c9429808aa1eb5b1cb726c53715c29ac Mon Sep 17 00:00:00 2001 From: yiliu30 Date: Mon, 29 Jan 2024 07:38:50 +0800 Subject: [PATCH 5/7] add log func back Signed-off-by: yiliu30 --- neural_compressor/common/__init__.py | 13 +++++++++++++ neural_compressor/common/utils/__init__.py | 3 +-- neural_compressor/common/utils/logger.py | 12 ++++-------- test/3x/torch/test_logger.py | 9 ++++++--- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/neural_compressor/common/__init__.py b/neural_compressor/common/__init__.py index dd25d0dbd9b..591942a7d62 100644 --- a/neural_compressor/common/__init__.py +++ b/neural_compressor/common/__init__.py @@ -13,6 +13,12 @@ # limitations under the License. from neural_compressor.common.utils import ( + log, + info, + debug, + warning, + error, + fatal, level, logger, Logger, @@ -25,8 +31,15 @@ __all__ = [ + "debug", + "error", + "fatal", + "info", "level", "logger", + "log", + "warning", + "Logger", "options", "set_workspace", "set_random_seed", diff --git a/neural_compressor/common/utils/__init__.py b/neural_compressor/common/utils/__init__.py index 645b4639f5f..4c248c453af 100644 --- a/neural_compressor/common/utils/__init__.py +++ b/neural_compressor/common/utils/__init__.py @@ -14,5 +14,4 @@ from neural_compressor.common.utils.constants import * from neural_compressor.common.utils.utility import * -from neural_compressor.common.utils.logger import Logger -from neural_compressor.common.utils.logger import logger, debug, log, info, warning, error, fatal +from neural_compressor.common.utils.logger import * diff --git a/neural_compressor/common/utils/logger.py b/neural_compressor/common/utils/logger.py index 33192931a35..f4993d566a7 100644 --- a/neural_compressor/common/utils/logger.py +++ b/neural_compressor/common/utils/logger.py @@ -20,18 +20,14 @@ import os __all__ = [ - "level", - "logger", "debug", - "log", + "error", + "fatal", "info", + "level", + "logger", "log", - "info", - "debug", "warning", - "error", - "fatal", - "Logger", "Logger", ] diff --git a/test/3x/torch/test_logger.py b/test/3x/torch/test_logger.py index e8605943a86..df3043868a0 100644 --- a/test/3x/torch/test_logger.py +++ b/test/3x/torch/test_logger.py @@ -45,12 +45,15 @@ def test_in_logger(self): inc_logger.debug({"msg": "call logger debug function."}) def test_logger_func(self): - from neural_compressor.common.utils import debug, info, log + from neural_compressor.common import debug, error, fatal, info, level, log, warning for msg in msg_lst: - debug(msg) log(level=1, msg=msg) - info(msg) + info(msg=msg) + debug(msg=msg) + warning(msg=msg) + error(msg=msg) + fatal(msg=msg) if __name__ == "__main__": From de7054545f6faf8f8488973adfab453cb16c52bb Mon Sep 17 00:00:00 2001 From: yiliu30 Date: Mon, 29 Jan 2024 08:18:51 +0800 Subject: [PATCH 6/7] add more test cases Signed-off-by: yiliu30 --- test/3x/torch/test_logger.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/3x/torch/test_logger.py b/test/3x/torch/test_logger.py index df3043868a0..980d7bb1452 100644 --- a/test/3x/torch/test_logger.py +++ b/test/3x/torch/test_logger.py @@ -9,9 +9,11 @@ "call logger log function.", {"msg": "call logger warning function"}, ["call logger warning function", "done"], + ({"msg": "call logger warning function"}, {"msg2": "done"}), {"msg": {("bert", "embedding"): {"weight": {"dtype": ["unint8", "int8"]}}}}, {"msg": [{"sub_msg": "call logger"}, {"sub_msg2": "call warning function"}]}, {"msg2": "done"}, + {("bert", "embedding"): {"op": ("a", "b")}}, ] From 853669daf112bce91f0871c31025e6515972055a Mon Sep 17 00:00:00 2001 From: chensuyue Date: Mon, 29 Jan 2024 11:15:59 +0800 Subject: [PATCH 7/7] clean up the CI log print Signed-off-by: chensuyue --- .azure-pipelines/scripts/ut/3x/collect_log_3x.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.azure-pipelines/scripts/ut/3x/collect_log_3x.sh b/.azure-pipelines/scripts/ut/3x/collect_log_3x.sh index f8313ed7e38..3cc75c1e85d 100644 --- a/.azure-pipelines/scripts/ut/3x/collect_log_3x.sh +++ b/.azure-pipelines/scripts/ut/3x/collect_log_3x.sh @@ -1,6 +1,6 @@ source /neural-compressor/.azure-pipelines/scripts/change_color.sh -set -xe +set -e pip install coverage export COVERAGE_RCFILE=/neural-compressor/.azure-pipelines/scripts/ut/3x/coverage.${1} coverage_log="/neural-compressor/log_dir/coverage_log"