tests: add regression test for logger NameError in torchao_quantizer#13175
Open
dhruvildarji wants to merge 1 commit intohuggingface:mainfrom
Open
tests: add regression test for logger NameError in torchao_quantizer#13175dhruvildarji wants to merge 1 commit intohuggingface:mainfrom
dhruvildarji wants to merge 1 commit intohuggingface:mainfrom
Conversation
…ixes huggingface#13104) Add a test to prevent regression of the NameError that occurred when `logger` was used inside `_update_torch_safe_globals()` before being defined at module level. The fix (moving `logger` before the function) was included in PR huggingface#12901, but this test ensures it cannot regress.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #13104
Issue #13104 reports a
NameError: name 'logger' is not definedthat occurs at import time when_update_torch_safe_globals()intorchao_quantizer.pyraises anImportError(e.g. whentorchao.dtypes.uintx.uint4_layoutis missing in newer torchao versions). The exception handler on line 103 callslogger.warning(...), butloggerwas defined after the function body, so Python raises aNameErrorinstead of the intended warning.The root fix (moving
logger = logging.get_logger(__name__)to line 39, before_update_torch_safe_globals) was already included in PR #12901. This PR adds a regression test to prevent the issue from recurring.Changes
TorchAoQuantizerModuleTest.test_logger_defined_before_module_level_codeintests/quantization/torchao/test_torchao.pyastto parse the source oftorchao_quantizer.pyand asserts that theloggerassignment appears at an earlier line number than the_update_torch_safe_globalsfunction definition, ensuring the logger is always available when the exception handler runs.Test plan
python -m pytest tests/quantization/torchao/test_torchao.py::TorchAoQuantizerModuleTest::test_logger_defined_before_module_level_code -vpasses