Skip to content

PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度

Refine logger and add envs (#817)

Yi Liuc5302e16c038
diff --git a/auto_round/autoround.py b/auto_round/autoround.py
index b5e237812..9d56cb8b4 100644
--- a/auto_round/autoround.py
+++ b/auto_round/autoround.py
@@ -33,6 +33,7 @@ from auto_round.data_type import QUANT_FUNC_WITH_DTYPE
from auto_round.data_type.utils import reshape_pad_tensor_by_group_size
from auto_round.export.export_to_autoround import AutoRoundFormat
from auto_round.export.export_to_gguf.config import GGUF_CONFIG, GGUF_INNER_CONFIG, ModelType
+from auto_round.logger import logger
from auto_round.low_cpu_mem.utils import get_layers_before_block
from auto_round.schemes import QuantizationScheme, preset_name_to_scheme
from auto_round.sign_sgd import SignSGD
@@ -86,7 +87,6 @@ from auto_round.utils import (
is_static_wfp8afp8,
is_wfp8afp8,
llm_load_model,
- logger,
mv_module_from_gpu,
reset_params,
set_amax_for_all_moe_layers,
diff --git a/auto_round/data_type/gguf.py b/auto_round/data_type/gguf.py
index b906abc68..26c262ba1 100644
--- a/auto_round/data_type/gguf.py
+++ b/auto_round/data_type/gguf.py
@@ -15,7 +15,8 @@
import torch
from auto_round.data_type.register import register_dtype
-from auto_round.data_type.utils import logger, reshape_pad_tensor_by_group_size, revert_tensor_by_pad, round_ste
+from auto_round.data_type.utils import reshape_pad_tensor_by_group_size, revert_tensor_by_pad, round_ste
+from auto_round.logger import logger
from auto_round.utils import get_reciprocal
diff --git a/auto_round/data_type/nvfp.py b/auto_round/data_type/nvfp.py
index 5d7e6221f..bb56f365d 100644
--- a/auto_round/data_type/nvfp.py
+++ b/auto_round/data_type/nvfp.py
@@ -16,7 +16,8 @@ import torch
from auto_round.data_type.fp8 import float8_e4m3fn_ste
from auto_round.data_type.register import register_dtype
-from auto_round.data_type.utils import logger, reshape_pad_tensor_by_group_size, revert_tensor_by_pad
+from auto_round.data_type.utils import reshape_pad_tensor_by_group_size, revert_tensor_by_pad
+from auto_round.logger import logger
# taken from
diff --git a/auto_round/envs.py b/auto_round/envs.py
new file mode 100644
index 000000000..ae0d79f1d
--- /dev/null
+++ b/auto_round/envs.py
@@ -0,0 +1,43 @@
+# Copyright (c) 2025 Intel Corporation
+#
+# 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.
+# Note: the design of this module is inspired by vLLM's envs.py
+
+import os
+from typing import TYPE_CHECKING, Any, Callable, Optional
+
+if TYPE_CHECKING:
+ AR_LOG_LEVEL: str = "INFO"
+
+environment_variables: dict[str, Callable[[], Any]] = {
+ # this is used for configuring the default logging level
+ "AR_LOG_LEVEL": lambda: os.getenv("AR_LOG_LEVEL", "INFO").upper(),
+}
+
+
+def __getattr__(name: str):
+ # lazy evaluation of environment variables
+ if name in environment_variables:
+ return environment_variables[name]()
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
+
+
+def __dir__():
+ return list(environment_variables.keys())
+
+
+def is_set(name: str):
+ """Check if an environment variable is explicitly set."""
+ if name in environment_variables:
+ return name in os.environ
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
diff --git a/auto_round/export/export_to_autogptq/export.py b/auto_round/export/export_to_autogptq/export.py
index fc7cf452e..6c7558175 100644
--- a/auto_round/export/export_to_autogptq/export.py
+++ b/auto_round/export/export_to_autogptq/export.py
@@ -47,6 +47,7 @@ import transformers
from tqdm import tqdm
import auto_round.export.export_to_autogptq.qlinear_triton
+from auto_round.logger import logger
from auto_round.utils import (
SUPPORTED_LAYER_TYPES,
check_to_quantized,
@@ -55,7 +56,6 @@ from auto_round.utils import (
get_autogptq_packing_qlinear,
get_block_names,
get_module,
- logger,
set_module,
)
diff --git a/auto_round/export/export_to_autoround/export.py b/auto_round/export/export_to_autoround/export.py
index ffd10036c..8877f4d4b 100644
--- a/auto_round/export/export_to_autoround/export.py
+++ b/auto_round/export/export_to_autoround/export.py
@@ -27,6 +27,7 @@ import transformers
from tqdm import tqdm
from auto_round.export.export_to_autoround.utils import REQUIRED_CONFIG_KEYS, check_neq_config
+from auto_round.logger import logger
from auto_round.utils import (
SUPPORTED_FORMATS,
SUPPORTED_LAYER_TYPES,
@@ -39,7 +40,6 @@ from auto_round.utils import (
is_mx_fp,
is_nv_fp,
is_standard_fp,
- logger,
set_module,
)
diff --git a/auto_round/export/export_to_autoround/export_to_fp8.py b/auto_round/export/export_to_autoround/export_to_fp8.py
index 7e4a71d13..4d0cb5033 100644
--- a/auto_round/export/export_to_autoround/export_to_fp8.py
+++ b/auto_round/export/export_to_autoround/export_to_fp8.py
@@ -24,6 +24,7 @@ from tqdm import tqdm
from auto_round.data_type.utils import reshape_pad_tensor_by_group_size, revert_tensor_by_pad
from auto_round.export.export_to_autoround.utils import REQUIRED_CONFIG_KEYS, check_neq_config
+from auto_round.logger import logger
from auto_round.utils import (
SUPPORTED_LAYER_TYPES,
_get_packing_device,
@@ -32,7 +33,6 @@ from auto_round.utils import (
copy_python_files_from_model_cache,
filter_quantization_config,
get_module,
- logger,
set_module,
)
diff --git a/auto_round/export/export_to_autoround/export_to_nvfp_mxfp.py b/auto_round/export/export_to_autoround/export_to_nvfp_mxfp.py
index 1c2123277..bd04146e9 100644
--- a/auto_round/export/export_to_autoround/export_to_nvfp_mxfp.py
+++ b/auto_round/export/export_to_autoround/export_to_nvfp_mxfp.py
@@ -25,6 +25,7 @@ import transformers
from tqdm import tqdm
from auto_round.export.export_to_autoround.utils import REQUIRED_CONFIG_KEYS, check_neq_config
+from auto_round.logger import logger
from auto_round.utils import (
SUPPORTED_LAYER_TYPES,
_get_packing_device,
@@ -35,7 +36,6 @@ from auto_round.utils import (
get_module,
is_mx_fp,
is_nv_fp,
- logger,
set_amax_for_all_moe_layers,
set_module,
)
diff --git a/auto_round/export/export_to_awq/export.py b/auto_round/export/export_to_awq/export.py
index 17904839c..21e1e470f 100644
--- a/auto_round/export/export_to_awq/export.py
+++ b/auto_round/export/export_to_awq/export.py
@@ -31,6 +31,7 @@ import torch.nn as nn
from tqdm import tqdm
from auto_round.export.export_to_awq.utils import WQLinear_GEMM
+from auto_round.logger import logger
from auto_round.utils import (
SUPPORTED_LAYER_TYPES,
check_to_quantized,
@@ -39,7 +40,6 @@ from auto_round.utils import (
filter_quantization_config,
get_block_names,
get_module,
- logger,
set_module,
)
diff --git a/auto_round/export/export_to_gguf/export.py b/auto_round/export/export_to_gguf/export.py
index 1fda37a2a..fc784bd0b 100644
--- a/auto_round/export/export_to_gguf/export.py
+++ b/auto_round/export/export_to_gguf/export.py
@@ -21,6 +21,8 @@ from pathlib import Path
import requests
import torch
+from auto_round.logger import logger
+
# from auto_round.export.export_to_gguf.convert_hf_to_gguf import ModelBase, ModelType, get_model_architecture
from auto_round.utils import (
LazyImport,
@@ -30,7 +32,6 @@ from auto_round.utils import (
get_block_names,
get_gguf_architecture,
get_module,
- logger,
)
convert_hf_to_gguf = LazyImport("auto_round.export.export_to_gguf.convert_hf_to_gguf")
diff --git a/auto_round/export/export_to_itrex/export.py b/auto_round/export/export_to_itrex/export.py
index 6fb33b010..4276847f1 100644
--- a/auto_round/export/export_to_itrex/export.py
+++ b/auto_round/export/export_to_itrex/export.py
@@ -21,7 +21,8 @@ import torch
import transformers
from auto_round.export.register import register_format
-from auto_round.utils import check_to_quantized, detect_device, get_module, logger, set_module
+from auto_round.logger import logger
+from auto_round.utils import check_to_quantized, detect_device, get_module, set_module
from .config import QuantConfig
from .model_wrapper import WeightOnlyLinear
diff --git a/auto_round/export/export_to_llmcompressor/export.py b/auto_round/export/export_to_llmcompressor/export.py
index c051b146f..65864cb16 100644
--- a/auto_round/export/export_to_llmcompressor/export.py
+++ b/auto_round/export/export_to_llmcompressor/export.py
@@ -17,6 +17,7 @@ import os
import torch
from auto_round.export.export_to_llmcompressor.config import quantization_config
+from auto_round.logger import logger
from auto_round.utils import (
copy_python_files_from_model_cache,
detect_device,
@@ -24,7 +25,6 @@ from auto_round.utils import (
is_mx_fp,
is_nv_fp,
is_standard_fp,
- logger,
set_module,
)
from auto_round.wrapper import WrapperWALayer
diff --git a/auto_round/export/export_to_llmcompressor/export_to_fp.py b/auto_round/export/export_to_llmcompressor/export_to_fp.py
index c5e3f603d..6581f0c6f 100644
--- a/auto_round/export/export_to_llmcompressor/export_to_fp.py
+++ b/auto_round/export/export_to_llmcompressor/export_to_fp.py
@@ -25,6 +25,7 @@ import transformers
from tqdm import tqdm
from auto_round.export.export_to_autoround.qlinear_fp import QuantLinear
+from auto_round.logger import logger
from auto_round.utils import (
SUPPORTED_LAYER_TYPES,
check_start_with_block_name,
@@ -35,7 +36,6 @@ from auto_round.utils import (
get_module,
is_mx_fp,
is_nv_fp,
- logger,
set_amax_for_all_moe_layers,
set_module,
)
diff --git a/auto_round/logger.py b/auto_round/logger.py
new file mode 100644
index 000000000..73f891315
--- /dev/null
+++ b/auto_round/logger.py
@@ -0,0 +1,86 @@
+# Copyright (c) 2025 Intel Corporation
+#
+# 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
+from functools import lru_cache
+
+import auto_round.envs as envs
+
+
+@lru_cache(maxsize=None)
+def warning_once(self, msg, *args):
+ """
+ Log a warning message only once per unique message/arguments combination.
+
+ Args:
+ msg: The warning message format string
+ *args: Variable positional arguments for message formatting
+ """
+ logger.warning(msg, *args, stacklevel=2)
+
+
+# Define a new logging level TRACE
+TRACE_LEVEL = 5
+logging.addLevelName(TRACE_LEVEL, "TRACE")
+
+
+def trace(self, message, *args):
+ """
+ Log a message with the TRACE level.
+
+ Args:
+ message: The message format string
+ *args: Variable positional arguments for message formatting
+
+ """
+ if self.isEnabledFor(TRACE_LEVEL):
+ self._log(TRACE_LEVEL, message, args, stacklevel=2)
+
+
+# Add the trace method to the Logger class
+logging.Logger.trace = trace
+
+
+class AutoRoundFormatter(logging.Formatter):
+ grey = "\x1b[38;20m"
+ yellow = "\x1b[33;1m"
+ red = "\x1b[31;20m"
+ bold_red = "\x1b[31;1m"
+ reset = "\x1b[0m"
+ cyan = "\x1b[36;1m"
+ blue = "\x1b[34;1m"
+ _format = "%(asctime)s %(levelname)s %(filename)s L%(lineno)d: %(message)s"
+
+ FORMATS = {
+ logging.DEBUG: blue + _format + reset,
+ logging.INFO: grey + _format + reset,
+ logging.WARNING: yellow + _format + reset,
+ logging.ERROR: bold_red + _format + reset,
+ logging.CRITICAL: bold_red + _format + reset,
+ TRACE_LEVEL: cyan + _format + reset,
+ }
+
+ def format(self, record):
+ log_fmt = self.FORMATS.get(record.levelno)
+ formatter = logging.Formatter(log_fmt, "%Y-%m-%d %H:%M:%S")
+ return formatter.format(record)
+
+
+logging.Logger.warning_once = warning_once
+logger = logging.getLogger("autoround")
+logger.setLevel(envs.AR_LOG_LEVEL)
+logger.propagate = False
+fh = logging.StreamHandler()
+fh.setFormatter(AutoRoundFormatter())
+logger.addHandler(fh)
diff --git a/auto_round/mllm/autoround_mllm.py b/auto_round/mllm/autoround_mllm.py
index 1dc639b8d..f6be55851 100644
--- a/auto_round/mllm/autoround_mllm.py
+++ b/auto_round/mllm/autoround_mllm.py
@@ -19,6 +19,7 @@ import torch
from tqdm import tqdm
from auto_round.autoround import AutoRound
+from auto_round.logger import logger
from auto_round.low_cpu_mem.utils import get_layers_before_block
from auto_round.mllm.mllm_dataset import get_mllm_dataloader
from auto_round.mllm.template import Template, get_template
@@ -35,7 +36,6 @@ from auto_round.utils import (
extract_block_names_to_str,
find_matching_blocks,
get_block_names,
- logger,
mllm_load_model,
to_device,
to_dtype,
diff --git a/auto_round/script/mllm.py b/auto_round/script/mllm.py
index bbb2b42eb..cb3edd93b 100644
--- a/auto_round/script/mllm.py
+++ b/auto_round/script/mllm.py
@@ -16,13 +16,13 @@ import argparse
import os
import sys
+from auto_round.logger import logger
from auto_round.schemes import PRESET_SCHEMES
from auto_round.utils import (
clear_memory,
get_device_and_parallelism,
get_fp_layer_names,
is_debug_mode,
- logger,
set_cuda_visible_devices,
)
diff --git a/auto_round/utils.py b/auto_round/utils.py
index efac5bdfc..47088ef96 100644
--- a/auto_round/utils.py
+++ b/auto_round/utils.py
@@ -34,6 +34,7 @@ from packaging import version
from torch.amp import autocast
from auto_round.export.export_to_gguf.config import GGML_QUANT_SIZES, GGUF_CONFIG, GGUF_INNER_CONFIG, QK_K, ModelType
+from auto_round.logger import logger
from auto_round.schemes import QuantizationScheme
SHARED_CACHE_KEYS = ("position_ids", "cache_position", "position_embeddings")
@@ -108,50 +109,6 @@ def infer_bits_by_data_type(data_type: str):
return None
-@lru_cache(maxsize=None)
-def warning_once(self, msg, *args, **kwargs):
- """
- Log a warning message only once per unique message/arguments combination.
-
- Args:
- msg: The warning message format string
- *args: Variable positional arguments for message formatting
- **kwargs: Variable keyword arguments for message formatting and logging options
- """
- self.warning(msg, *args, **kwargs)
-
-
-class AutoRoundFormatter(logging.Formatter):
- grey = "\x1b[38;20m"
- yellow = "\x1b[33;1m"
- red = "\x1b[31;20m"
- bold_red = "\x1b[31;1m"
- reset = "\x1b[0m"
- _format = "%(asctime)s %(levelname)s %(filename)s L%(lineno)d: %(message)s"
-
- FORMATS = {
- logging.DEBUG: grey + _format + reset,
- logging.INFO: grey + _format + reset,
- logging.WARNING: yellow + _format + reset,
- logging.ERROR: bold_red + _format + reset,
- logging.CRITICAL: bold_red + _format + reset,
- }
-
- def format(self, record):
- log_fmt = self.FORMATS.get(record.levelno)
- formatter = logging.Formatter(log_fmt, "%Y-%m-%d %H:%M:%S")
- return formatter.format(record)
-
-
-logging.Logger.warning_once = warning_once
-logger = logging.getLogger("autoround")
-logger.setLevel(logging.INFO)
-logger.propagate = False
-fh = logging.StreamHandler()
-fh.setFormatter(AutoRoundFormatter())
-logger.addHandler(fh)
-
-
class LazyImport(object):
"""Lazy import python module till use."""
diff --git a/auto_round/wrapper.py b/auto_round/wrapper.py
index 4fa1ed1de..4bf816d5c 100644
--- a/auto_round/wrapper.py
+++ b/auto_round/wrapper.py
@@ -17,6 +17,7 @@ import transformers
from torch.functional import F
from auto_round.data_type import get_quant_func
+from auto_round.logger import logger
from .utils import (
SUPPORTED_LAYER_TYPES,
@@ -25,7 +26,6 @@ from .utils import (
get_scale_shape,
is_mx_fp,
is_nv_fp,
- logger,
set_module,
)
diff --git a/test/test_cpu/test_logger.py b/test/test_cpu/test_logger.py
new file mode 100644
index 000000000..dcc2d23e2
--- /dev/null
+++ b/test/test_cpu/test_logger.py
@@ -0,0 +1,57 @@
+import logging
+import os
+from io import StringIO
+
+from auto_round.logger import TRACE_LEVEL, AutoRoundFormatter, logger
+
+
+def test_logger(monkeypatch):
+ # Mock the AR_LOG_LEVEL environment variable
+ monkeypatch.setenv("AR_LOG_LEVEL", "TRACE")
+
+ # Create a StringIO to capture log output
+ log_output = StringIO()
+ stream_handler = logging.StreamHandler(log_output)
+ stream_handler.setFormatter(AutoRoundFormatter())
+
+ # Add the handler to the logger
+ logger.addHandler(stream_handler)
+ logger.setLevel(logging.getLevelName(os.getenv("AR_LOG_LEVEL", "INFO")))
+
+ # Log messages at different levels
+ logger.trace("This is a TRACE message.")
+ logger.debug("This is a DEBUG message.")
+ logger.info("This is an INFO message.")
+ logger.warning("This is a WARNING message.")
+ logger.error("This is an ERROR message.")
+ logger.critical("This is a CRITICAL message.")
+
+ # Test warning_once functionality
+ logger.warning_once("This is a WARNING_ONCE message.")
+ logger.warning_once("This is a WARNING_ONCE message.") # Should not log again
+ logger.warning_once("This is another unique WARNING_ONCE message.") # Should log
+
+ # Remove the handler after the test
+ logger.removeHandler(stream_handler)
+
+ # Get the log output
+ log_output.seek(0)
+ logs = log_output.read()
+
+ # Assertions for log levels
+ assert "TRACE" in logs
+ assert "This is a TRACE message." in logs
+ assert "DEBUG" in logs
+ assert "This is a DEBUG message." in logs
+ assert "INFO" in logs
+ assert "This is an INFO message." in logs
+ assert "WARNING" in logs
+ assert "This is a WARNING message." in logs
+ assert "ERROR" in logs
+ assert "This is an ERROR message." in logs
+ assert "CRITICAL" in logs
+ assert "This is a CRITICAL message." in logs
+
+ # Assertions for warning_once
+ assert logs.count("This is a WARNING_ONCE message.") == 1
+ assert "This is another unique WARNING_ONCE message." in logs