Skip to content

Commit 7449e2a

Browse files
committed
[fix][5889686] AutoCast: Fix logger
Previously relied on quantization logger, which caused logs to be suppressed when onnx.autocast was used directly Instead: - Inherit format and level if called from onnx.quantization - Configure independent format and level if called from onnx.autocast Signed-off-by: Gal Hubara Agam <96368689+galagam@users.noreply.github.com>
1 parent ca1f968 commit 7449e2a

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

modelopt/onnx/autocast/logging_config.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,41 @@
2222

2323
import logging
2424
import os
25+
import sys
2526

26-
# Create a parent logger for all AutoCast components
27-
logger = logging.getLogger("autocast")
27+
# Create a logger for all AutoCast components as a child of modelopt.onnx
28+
# This ensures autocast inherits log level and format when called from quantization
29+
logger = logging.getLogger("modelopt.onnx.autocast")
2830

2931

3032
def configure_logging(level=logging.INFO, log_file=None):
3133
"""Configure logging for all AutoCast components.
3234
3335
Args:
34-
level: The logging level to use (default: logging.INFO).
36+
level: The logging level to use. Can be a string (e.g., "DEBUG", "INFO") or
37+
a logging constant (e.g., logging.DEBUG). Default: logging.INFO.
3538
log_file: Optional path to a log file. If provided, logs will be written to this file
3639
in addition to stdout (default: None).
3740
"""
38-
# Set level for the parent logger and all child loggers
41+
# Check if parent logger (modelopt.onnx) already has handlers configured
42+
parent_logger = logging.getLogger("modelopt.onnx")
43+
parent_has_handlers = len(parent_logger.handlers) > 0
44+
45+
# If parent is configured and no explicit level provided, inherit parent's level
46+
if parent_has_handlers and level == logging.INFO:
47+
level = parent_logger.level
48+
49+
# Set level for the autocast logger (accepts both string and int)
3950
logger.setLevel(level)
4051

4152
# Remove any existing handlers to ensure clean configuration
4253
for handler in logger.handlers[:]:
4354
logger.removeHandler(handler)
4455

56+
formatter = logging.Formatter(
57+
"%(asctime)s - %(name)s - %(levelname)s - %(filename)s - %(message)s"
58+
)
59+
4560
# Add file handler if log_file is specified
4661
if log_file:
4762
try:
@@ -50,24 +65,28 @@ def configure_logging(level=logging.INFO, log_file=None):
5065
if log_dir:
5166
os.makedirs(log_dir, exist_ok=True)
5267

53-
formatter = logging.Formatter(
54-
"%(asctime)s - %(name)s - %(levelname)s - %(filename)s - %(message)s"
55-
)
5668
file_handler = logging.FileHandler(log_file)
5769
file_handler.setFormatter(formatter)
5870
logger.addHandler(file_handler)
5971
logger.info(f"Logging configured to write to file: {log_file}")
6072
except Exception as e:
6173
logging.error(f"Failed to setup file logging to {log_file}: {e!s}")
6274

63-
# Allow log messages to propagate to the root logger for testing compatibility
64-
# This enables pytest's caplog fixture to capture logs while still maintaining
65-
# our custom formatting through the handlers above
66-
logger.propagate = True
75+
if parent_has_handlers:
76+
# Parent logger is configured (called from quantization/other onnx modules)
77+
# Propagate to parent to use its handlers and format
78+
logger.propagate = True
79+
else:
80+
# Standalone mode (called directly via python3 -m modelopt.onnx.autocast)
81+
# Add our own console handler with autocast-specific format
82+
console_handler = logging.StreamHandler(sys.stdout)
83+
console_handler.setFormatter(formatter)
84+
logger.addHandler(console_handler)
85+
logger.propagate = False
6786

6887
# Ensure all child loggers inherit the level setting
6988
for name in logging.root.manager.loggerDict:
70-
if name.startswith("autocast"):
89+
if name.startswith("modelopt.onnx.autocast"):
7190
logging.getLogger(name).setLevel(level)
7291

7392

0 commit comments

Comments
 (0)