Repository navigation
[config] Serve unwritten config reads from the module __dict__ - #193160
anijain2305 wants to merge 23 commits into
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/193160
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 8a51c9d with merge base 7438967 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
Reading torch._inductor.config.foo costs ~1.5us. Almost none of that is the lookup itself: ConfigModule defines __getattr__ on the class, so every read first runs normal attribute lookup, which misses and builds an AttributeError, and only then reaches __getattr__, which walks the alias / env / user override / justknob / mutable-default precedence chain. Compilation reads configs hundreds of thousands of times per compile, all of it going down that path. Two changes, both aimed at the case where the answer is just the default: - _ConfigEntry.fast_default marks an entry whose read reduces to "user_override if set else default" - no alias, env value, justknob, deprecation warning, or mutable default that has to be deep-copied. __getattr__ answers those in two lookups instead of the full chain. - install_config_module parks the default of every fast_default entry in the module __dict__ (see Note [config shadow]), so plain attribute lookup finds it and __getattr__ never runs. A shadow is only correct while the value is a context-independent constant, so any write drops it and that config goes back to __getattr__ permanently. Since user_override is a ContextVar, an entry that is never written reads as its default in every thread and context. Invalidation is centralized in _ConfigEntry.set_user_override rather than left to each caller. An earlier version of this patch dropped the shadow in __setattr__ only, which silently broke _make_closure_patcher - it writes user_override directly - and that is how torch.compile(dynamic=True) clears assume_static_by_default. Reads kept returning the stale default, so the model compiled with static shapes: 30% "faster" and wrong. Any future direct write to user_override would reintroduce that, hence the comment on the field. A config read drops from 1539ns to 99ns. On a UMA cold compile (fairchem uma-s-1p2, 1000 atoms, dynamic=True), total compile time measured as the union of top-level chromium events: pair 1: baseline 49.15s changed 47.00s pair 2: baseline 49.28s changed 47.51s pair 3: baseline 48.51s changed 47.92s i.e. 48.98s -> 47.48s, -3.1%, with the change winning every pair. Test plan: ``` python test/test_utils_config_module.py python test/dynamo/test_config.py python test/inductor/test_config.py python test/dynamo/test_misc.py python test/test_dynamic_shapes.py python test/test_public_bindings.py python test/test_torch_config_hash_determinism.py ``` test_utils_config_module and dynamo/test_config pass outright; the rest have pre-existing failures on this host (aarch64 CPU codegen, missing onnxscript) and produce failure sets identical to a run with the change stashed. This PR was authored with the assistance of an AI coding agent. ghstack-source-id: 7504e3f Pull-Request: #193160
[ghstack-poisoned]
|
@claude review this PR - is it possible to still read the stale value if the user sets this config from outside |
|
Claude finished @anijain2305's task in 58m 24s —— View job Short answerYes — two narrow paths remain. Anything that goes through Full reviewPR Review: #193160SummaryAdds a Answering the question: remaining stale-read pathsThe shadow is dropped globally and permanently on first write, and never re-added. What is not covered: 1. Direct Consider making it structurally impossible rather than documented: rename the field to 2.
Since if config.fast_default and not config.hide:That costs one extra attribute load on the 3. Env vars set after import are still ignored — pre-existing, unchanged by this PR. Testing62 new lines in a module with ~900 lines of dedicated tests, and no test added. The existing
The highest-value test is a property test rather than any of the above: for every entry in Backward Compatibility
Code Quality
PerformanceNested configs ( RecommendationRequest Changes — on the missing invariant tests. The optimization is well-reasoned and the centralized invalidation point is the right design, but a mechanism whose failure mode is "silently returns a stale value" and which the author already found breaking real code once should not land with its invariants held up only by a comment. Adding the fast-path-vs-slow-path property test plus the |
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
Reading torch._inductor.config.foo costs ~1.5us. Almost none of that is the lookup itself: ConfigModule defines __getattr__ on the class, so every read first runs normal attribute lookup, which misses and builds an AttributeError, and only then reaches __getattr__, which walks the alias / env / user override / justknob / mutable-default precedence chain. Compilation reads configs hundreds of thousands of times per compile, all of it going down that path. Two changes, both aimed at the case where the answer is just the default: - _ConfigEntry.fast_default marks an entry whose read reduces to "user_override if set else default" - no alias, env value, justknob, deprecation warning, or mutable default that has to be deep-copied. __getattr__ answers those in two lookups instead of the full chain. - install_config_module parks the default of every fast_default entry in the module __dict__ (see Note [config shadow]), so plain attribute lookup finds it and __getattr__ never runs. A shadow is only correct while the value is a context-independent constant, so any write drops it and that config goes back to __getattr__ permanently. Since user_override is a ContextVar, an entry that is never written reads as its default in every thread and context. Invalidation is centralized in _ConfigEntry.set_user_override rather than left to each caller. An earlier version of this patch dropped the shadow in __setattr__ only, which silently broke _make_closure_patcher - it writes user_override directly - and that is how torch.compile(dynamic=True) clears assume_static_by_default. Reads kept returning the stale default, so the model compiled with static shapes: 30% "faster" and wrong. Any future direct write to user_override would reintroduce that, hence the comment on the field. A config read drops from 1539ns to 99ns. On one model's cold compile with dynamic=True, total compile time measured as the union of top-level chromium events: pair 1: baseline 49.15s changed 47.00s pair 2: baseline 49.28s changed 47.51s pair 3: baseline 48.51s changed 47.92s i.e. 48.98s -> 47.48s, -3.1%, with the change winning every pair. Test plan: ``` python test/test_utils_config_module.py python test/dynamo/test_config.py python test/inductor/test_config.py python test/dynamo/test_misc.py python test/test_dynamic_shapes.py python test/test_public_bindings.py python test/test_torch_config_hash_determinism.py ``` test_utils_config_module and dynamo/test_config pass outright; the rest have pre-existing failures on this host (aarch64 CPU codegen, missing onnxscript) and produce failure sets identical to a run with the change stashed. This PR was authored with the assistance of an AI coding agent. ghstack-source-id: 11c2fb6 Pull-Request: #193160
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
|
Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as |
Stack from ghstack (oldest at bottom):
Reading torch._inductor.config.foo costs ~1.5us. Almost none of that is the
lookup itself: ConfigModule defines getattr on the class, so every read
first runs normal attribute lookup, which misses and builds an AttributeError,
and only then reaches getattr, which walks the alias / env / user override
/ justknob / mutable-default precedence chain. Compilation reads configs
hundreds of thousands of times per compile, all of it going down that path.
Two changes, both aimed at the case where the answer is just the default:
_ConfigEntry.fast_default marks an entry whose read reduces to "user_override
if set else default" - no alias, env value, justknob, deprecation warning, or
mutable default that has to be deep-copied. getattr answers those in two
lookups instead of the full chain.
install_config_module parks the default of every fast_default entry in the
module dict (see Note [config shadow]), so plain attribute lookup finds
it and getattr never runs. A shadow is only correct while the value is a
context-independent constant, so any write drops it and that config goes back
to getattr permanently. Since user_override is a ContextVar, an entry
that is never written reads as its default in every thread and context.
Invalidation is centralized in _ConfigEntry.set_user_override rather than left
to each caller. An earlier version of this patch dropped the shadow in
setattr only, which silently broke _make_closure_patcher - it writes
user_override directly - and that is how torch.compile(dynamic=True) clears
assume_static_by_default. Reads kept returning the stale default, so the model
compiled with static shapes: 30% "faster" and wrong. Any future direct write to
user_override would reintroduce that, hence the comment on the field.
A config read drops from 1539ns to 99ns. On one model's cold compile with
dynamic=True, total compile time measured as the union of top-level chromium
events:
pair 1: baseline 49.15s changed 47.00s
pair 2: baseline 49.28s changed 47.51s
pair 3: baseline 48.51s changed 47.92s
i.e. 48.98s -> 47.48s, -3.1%, with the change winning every pair.
Test plan:
test_utils_config_module and dynamo/test_config pass outright; the rest have
pre-existing failures on this host (aarch64 CPU codegen, missing onnxscript)
and produce failure sets identical to a run with the change stashed.
This PR was authored with the assistance of an AI coding agent.