fix: make replace_function / calibrate_with_adapters / enable_fake_quant exception-safe#1910
fix: make replace_function / calibrate_with_adapters / enable_fake_quant exception-safe#1910arham766 wants to merge 1 commit into
Conversation
…exception-safe These three context managers yielded without try/finally, so an exception in the body permanently leaked the patched state: the replaced function and its _<name> cache attribute stayed installed, LoRA adapters stayed disabled after a failed calibration, and fake-quant flags stayed set. Their siblings in the same module (export_torch_mode, enable_quant, disable_calib, no_requires_grad, patch_fsdp_mp_dtypes) already restore in finally; align these three. Success-path semantics unchanged. Part of the findings in NVIDIA#1902. Signed-off-by: arham766 <arhamislam766@yahoo.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThree context managers in core_utils.py (calibrate_with_adapters, replace_function, enable_fake_quant) are refactored to move restoration logic into finally blocks, ensuring state cleanup occurs even when exceptions propagate. Corresponding regression tests are added to verify this behavior. ChangesException-safe context manager restoration
Estimated code review effort: 2 (Simple) | ~10 minutes Sequence Diagram(s)Not applicable; the changes are localized exception-handling refinements without multi-component interaction flow. 🚥 Pre-merge checks | ✅ 6✅ Passed checks (6 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
What does this PR do?
Type of change: Bug fix
Bug 5 in #1902: three context managers yield without try/finally, so an exception in the body leaks patched state (replaced function + cache attribute left installed; LoRA adapters left disabled after a failed calibration; fake-quant flags left set). Siblings in the same module already restore in finally. Success-path semantics unchanged; three exception-path regression tests added.
Usage
N/A
Testing
Regression tests included (added to existing unit test files; each was verified to fail with the fix reverted). Full tests/unit/torch quantization+export+utils+opt battery passes locally with all sibling fixes applied (962 passed). Note: the test-suite PRs #1903-#1907 contain behavior-documenting NOTE tests that pin the OLD behavior fixed here — whichever lands second will be rebased to flip those assertions (happy to do so).
Before your PR is "Ready for review"
Additional Information
Issue: #1902
Summary by CodeRabbit
Bug Fixes
Tests