Skip to content

Commit 0488e9a

Browse files
galagamkevalmorabia97
authored andcommitted
[fix][5889686] AutoCast: Fix logger (#890)
## What does this PR do? **Type of change:** Bug fix **Overview:** 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 ## Before your PR is "*Ready for review*" <!-- If you haven't finished some of the above items you can still open `Draft` PR. --> - **Make sure you read and follow [Contributor guidelines](https://github.com/NVIDIA/Model-Optimizer/blob/main/CONTRIBUTING.md)** and your commits are signed. - **Is this change backward compatible?**: Yes <!--- If No, explain why. --> - **Did you write any new necessary tests?**: No - **Did you add or update any necessary documentation?**: No - **Did you update [Changelog](https://github.com/NVIDIA/Model-Optimizer/blob/main/CHANGELOG.rst)?**: No <!--- Only for new features, API changes, critical bug fixes or bw breaking changes. --> ## Additional Information <!-- E.g. related issue. --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved logging configuration to ensure consistent behavior across modules with enhanced file and console output management. * Fixed log file handling to automatically create required directories. * Enhanced logger propagation logic for more reliable output routing. * **Chores** * Refined logging initialization for better automatic configuration at startup. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Gal Hubara Agam <96368689+galagam@users.noreply.github.com>
1 parent 5c5a60f commit 0488e9a

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

modelopt/onnx/autocast/logging_config.py

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,54 @@
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

30-
def configure_logging(level=logging.INFO, log_file=None):
32+
def configure_logging(level=None, log_file=None):
3133
"""Configure logging for all AutoCast components.
3234
35+
If logging level is provided, it will be used regardless of parent logger log level.
36+
Otherwise, inherits from parent logger if exists, or fallback to default: logging.INFO.
37+
3338
Args:
34-
level: The logging level to use (default: logging.INFO).
39+
level: The logging level to use. Can be a string (e.g., "DEBUG", "INFO") or
40+
a logging constant (e.g., logging.DEBUG) default: None.
3541
log_file: Optional path to a log file. If provided, logs will be written to this file
3642
in addition to stdout (default: None).
3743
"""
38-
# Set level for the parent logger and all child loggers
44+
# Check if parent logger (modelopt.onnx) already has handlers configured
45+
parent_logger = logging.getLogger("modelopt.onnx")
46+
parent_has_handlers = len(parent_logger.handlers) > 0
47+
48+
# Determine the logging level to use
49+
if level is None:
50+
# No explicit level provided - inherit from parent or use default
51+
if parent_has_handlers:
52+
level = parent_logger.level
53+
else:
54+
level = logging.INFO
55+
# else: use the provided level as-is
56+
57+
# Set level for the autocast logger (accepts both string and int)
3958
logger.setLevel(level)
4059

60+
# If parent has handlers (standalone mode), also update parent's level
61+
# so the parent's console handler respects the autocast log level
62+
if parent_has_handlers:
63+
parent_logger.setLevel(level)
64+
4165
# Remove any existing handlers to ensure clean configuration
4266
for handler in logger.handlers[:]:
4367
logger.removeHandler(handler)
4468

69+
formatter = logging.Formatter(
70+
"%(asctime)s - %(name)s - %(levelname)s - %(filename)s - %(message)s"
71+
)
72+
4573
# Add file handler if log_file is specified
4674
if log_file:
4775
try:
@@ -50,24 +78,29 @@ def configure_logging(level=logging.INFO, log_file=None):
5078
if log_dir:
5179
os.makedirs(log_dir, exist_ok=True)
5280

53-
formatter = logging.Formatter(
54-
"%(asctime)s - %(name)s - %(levelname)s - %(filename)s - %(message)s"
55-
)
5681
file_handler = logging.FileHandler(log_file)
5782
file_handler.setFormatter(formatter)
5883
logger.addHandler(file_handler)
5984
logger.info(f"Logging configured to write to file: {log_file}")
6085
except Exception as e:
6186
logging.error(f"Failed to setup file logging to {log_file}: {e!s}")
6287

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
88+
if parent_has_handlers:
89+
# Parent logger is configured (called from quantization/other onnx modules)
90+
# Propagate to parent to use its handlers and format
91+
logger.propagate = True
92+
else:
93+
# Standalone mode (called directly via python3 -m modelopt.onnx.autocast)
94+
# Add our own console handler with autocast-specific format
95+
console_handler = logging.StreamHandler(sys.stdout)
96+
console_handler.setFormatter(formatter)
97+
logger.addHandler(console_handler)
98+
# Always propagate to support pytest's caplog fixture in tests
99+
logger.propagate = True
67100

68101
# Ensure all child loggers inherit the level setting
69102
for name in logging.root.manager.loggerDict:
70-
if name.startswith("autocast"):
103+
if name.startswith("modelopt.onnx.autocast"):
71104
logging.getLogger(name).setLevel(level)
72105

73106

modelopt/onnx/logging_config.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ def configure_logging(level=logging.INFO, log_file=None):
6464
console_handler.setFormatter(formatter)
6565
logger.addHandler(console_handler)
6666

67-
# Prevent log messages from propagating to the root logger
68-
logger.propagate = False
67+
# Allow log messages to propagate to the root logger for testing compatibility
68+
# This enables pytest's caplog fixture to capture logs while still maintaining
69+
# our custom formatting through the handlers above
70+
logger.propagate = True
6971

7072
# Ensure all child loggers inherit the level setting
7173
for name in logging.root.manager.loggerDict:

0 commit comments

Comments
 (0)