Fix mutable default argument in autoquant.py #3367
Open
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.
TLDR: Fix mutable default argument in autoquant.py
Fix Summary
AutoQuantizableLinearWeightand the various_autoquant_testimplementations used["relu", None]as a default parameter list. Because lists are mutable and default parameters are evaluated once at function definition time, every call shared the exact same list object. Any in-place mutation would accidentally leak between callers._DEFAULT_MODE_SENTINELand_resolve_autoquant_mode()so the public API still behaves as though the default is["relu", None], but a fresh list is created whenever a caller doesn’t supplymode._autoquant_testoverrides now default to the sentinel and immediately call_resolve_autoquant_mode(mode), ensuring explicitNonestaysNonewhile the implicit default gets rebuilt per call.Expected Outcome
mode(e.g.,mode[0] = "interpolate") affects only that invocation, preventing subtle cross-layer or cross-run contamination.modein-place; no more erratic quantization choices caused by reused mutable defaults.["relu", None]—existing call sites require no changes, just safer semantics.