Skip to content

tests: add regression test for logger NameError in torchao_quantizer#13175

Open
dhruvildarji wants to merge 1 commit intohuggingface:mainfrom
dhruvildarji:fix/torchao-logger-undefined
Open

tests: add regression test for logger NameError in torchao_quantizer#13175
dhruvildarji wants to merge 1 commit intohuggingface:mainfrom
dhruvildarji:fix/torchao-logger-undefined

Conversation

@dhruvildarji
Copy link

Summary

Closes #13104

Issue #13104 reports a NameError: name 'logger' is not defined that occurs at import time when _update_torch_safe_globals() in torchao_quantizer.py raises an ImportError (e.g. when torchao.dtypes.uintx.uint4_layout is missing in newer torchao versions). The exception handler on line 103 calls logger.warning(...), but logger was defined after the function body, so Python raises a NameError instead 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

  • Added TorchAoQuantizerModuleTest.test_logger_defined_before_module_level_code in tests/quantization/torchao/test_torchao.py
  • The test uses ast to parse the source of torchao_quantizer.py and asserts that the logger assignment appears at an earlier line number than the _update_torch_safe_globals function definition, ensuring the logger is always available when the exception handler runs.
  • Does not require GPU or torchao to be installed.

Test plan

  • python -m pytest tests/quantization/torchao/test_torchao.py::TorchAoQuantizerModuleTest::test_logger_defined_before_module_level_code -v passes

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logger is not defined

1 participant