Repository navigation
[dynamo] Type source keys and region hashes as object - #196370
bobrenjc93 wants to merge 3 commits into
Conversation
Replace Any with object at opaque source-key and graph-region hashing boundaries. Keep ConstDictKeySource.index concretely typed as int, narrow consumers before indexing, validate encoded slices before unpacking, and make region sorting generic. Test Plan: ``` pyrefly check torch/_dynamo/source.py torch/_dynamo/graph_region_tracker.py python -m py_compile torch/_dynamo/source.py torch/_dynamo/graph_region_tracker.py torch/_dynamo/functional_export.py torch/_dynamo/guards.py torch/_dynamo/variables/builder.py torch/_dynamo/variables/optimizer.py lintrunner -a torch/_dynamo/source.py torch/_dynamo/graph_region_tracker.py torch/_dynamo/functional_export.py torch/_dynamo/guards.py torch/_dynamo/variables/builder.py torch/_dynamo/variables/optimizer.py ``` Targeted graph-region, source/guard, and ContextVar runtime tests passed locally through a scratch import shim for the older installed extension. Authored with assistance from an AI coding assistant.
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/196370
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (2 Unrelated Failures)As of commit ffe9741 with merge base ca66d98 ( GREEN LIGHT - PR approved to be merged without human review:
reason: CRCR (non-blocking) - The following CRCR downstream CI jobs failed but are non-blocking:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Claude finished @bobrenjc93's task in 23m 51s —— View job Review completeMostly a clean typing tightening, but three of the added runtime assertions are behavior changes disguised as type narrowing — two of them can turn currently-working compiles into hard crashes. Detailed reviewBlocking:
|
The initial typing pass introduced runtime failures while narrowing object-typed source indices. Keep the existing permissive behavior by filtering non-string builtin keys, skipping non-integer export input sources, and using the established integer invariant only as a static cast in optimizer argument mapping. Also consolidate slice validation and simplify redundant object unions. Test Plan: ``` pyrefly check torch/_dynamo/source.py torch/_dynamo/graph_region_tracker.py python -m py_compile torch/_dynamo/source.py torch/_dynamo/graph_region_tracker.py torch/_dynamo/functional_export.py torch/_dynamo/guards.py torch/_dynamo/variables/builder.py torch/_dynamo/variables/optimizer.py lintrunner -a torch/_dynamo/source.py torch/_dynamo/graph_region_tracker.py torch/_dynamo/functional_export.py torch/_dynamo/guards.py torch/_dynamo/variables/builder.py torch/_dynamo/variables/optimizer.py ``` Targeted graph-region, source/guard, and ContextVar runtime tests passed locally through a scratch import shim for the older installed extension. Authored with assistance from an AI coding assistant.
|
Addressed the review in 2fe31e3:
|
|
Claude finished @bobrenjc93's task in 11m 43s —— View job Re-review of
|
Use the imported GetItemSource name so the input-index guard stays on one line, and split the output-path casts for readability. Test Plan: ``` python -m py_compile torch/_dynamo/functional_export.py lintrunner -a torch/_dynamo/functional_export.py ``` Authored with assistance from an AI coding assistant.
|
Claude finished @bobrenjc93's task in 1m 46s —— View job Review of
|
| Site | Status |
|---|---|
guards.py:3107-3109 |
Narrowing, not a raise. used_builtin_vars is OrderedSet[str] and only membership-tested against builtins.__dict__ keys, so skipping a non-str index is exactly equivalent to the old no-op insert. |
functional_export.py:1070 |
Narrowing, not a raise. Matches the comment at 1064-1066 ("Skip inputs that come from other sources"), and is strictly safer than pre-PR — a non-int index used to land as a key in dict[int, int] and then TypeError at line 1074. |
functional_export.py:398-399, optimizer.py:230 |
typing.cast only. The earlier raise AssertionError / raise ArgMappingException and type(x) is not int checks (which also rejected bool) are gone. |
source.py:761-767 |
Single consolidated check, single message. self.index[0] is slice is exact: slice is not subclassable in CPython and __post_init__ (line 745-748) is the sole writer of index_is_slice=True, always storing slice.__reduce__(). |
graph_region_tracker.py:121,132 |
-> object / tuple[object, ...]; no redundant ` |
builder.py:461-467 |
The isinstance(cur.index, str) guard on the UnspecializedParamBufferSource collapse is the model narrowing — a non-str index now falls through instead of building an _AttrToken with a non-str name. |
source.py annotations |
GetItemSource.index, DictGetItemSource.index, DictSubclassGetItemSource.index, ContextVarGetSource.default_value as object; ConstDictKeySource.index as int (all six construction sites pass enumerate indices; dict_keys_getitem requires an integer). _esc_str(s: object) is fine — the body only calls repr/str. |
Testing caveat
No build or Python environment is available in this runner (no .venv, pyrefly/lintrunner unavailable), so this remains a static review — as does the diff's own claim, which the description notes ran through an import shim. That's acceptable here: the diff contains no new raise on a previously-permissive path, so the only runtime-visible changes are two narrowings that skip cases which used to fail downstream anyway. Standard CI should cover it.
Follow-up, not this PR
source.py:809 — NonSerializableSetGetItemSource.index is annotated int but __post_init__ validates only ConstantVariable.is_literal. Accurate at both construction sites (builder.py:1335, builder.py:2313 pass enumerate indices), but the validator and annotation disagree about what's permitted.
I'm not able to approve pull requests — that's disabled for security reasons (FAQ). Treat this as a LGTM from my side; a human reviewer will need to click approve.
Summary: ffe9741 cleanly resolves the last cosmetic nit with two no-op changes, and I have no remaining concerns — every narrowing in the PR is either a runtime-no-op cast or a condition that skips a case which previously failed downstream.
| Branch: bobren/dynamo-source-object
|
@pytorchbot drci |
|
@pytorchbot merge -i |
Merge startedYour change will be merged while ignoring the following 0 checks: Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
## Summary - replace `Any` with `object` for opaque source keys and graph-region hash inputs - retain `int` for the positional `ConstDictKeySource.index`, and narrow opaque indices at consumers before use - validate encoded slice data before unpacking and make region sorting generic These changes expose unchecked assumptions that `Any` previously hid without changing the accepted runtime values. ## Test plan - `pyrefly check torch/_dynamo/source.py torch/_dynamo/graph_region_tracker.py` (0 errors, 3 pre-existing suppressions) - `python -m py_compile torch/_dynamo/source.py torch/_dynamo/graph_region_tracker.py torch/_dynamo/functional_export.py torch/_dynamo/guards.py torch/_dynamo/variables/builder.py torch/_dynamo/variables/optimizer.py` - `lintrunner -a torch/_dynamo/source.py torch/_dynamo/graph_region_tracker.py torch/_dynamo/functional_export.py torch/_dynamo/guards.py torch/_dynamo/variables/builder.py torch/_dynamo/variables/optimizer.py` - `test/dynamo/test_graph_region_tracker.py` (13 passed, 1 hardware-gated skip) - `MiscTests.test_hash_getitem_slice`, `SourceCloneTests`, and `test/dynamo/test_contextvars.py` (32 passed) The local runtime tests loaded the patched Python modules through a scratch import shim because the available compiled extension predates this checkout. Full coverage will run in CI against a fresh build. Authored with assistance from an AI coding assistant. Pull Request resolved: pytorch#196370 Approved by: https://github.com/pytorchgreenlight
Summary
Anywithobjectfor opaque source keys and graph-region hash inputsintfor the positionalConstDictKeySource.index, and narrow opaque indices at consumers before useThese changes expose unchecked assumptions that
Anypreviously hid without changing the accepted runtime values.Test plan
pyrefly check torch/_dynamo/source.py torch/_dynamo/graph_region_tracker.py(0 errors, 3 pre-existing suppressions)python -m py_compile torch/_dynamo/source.py torch/_dynamo/graph_region_tracker.py torch/_dynamo/functional_export.py torch/_dynamo/guards.py torch/_dynamo/variables/builder.py torch/_dynamo/variables/optimizer.pylintrunner -a torch/_dynamo/source.py torch/_dynamo/graph_region_tracker.py torch/_dynamo/functional_export.py torch/_dynamo/guards.py torch/_dynamo/variables/builder.py torch/_dynamo/variables/optimizer.pytest/dynamo/test_graph_region_tracker.py(13 passed, 1 hardware-gated skip)MiscTests.test_hash_getitem_slice,SourceCloneTests, andtest/dynamo/test_contextvars.py(32 passed)The local runtime tests loaded the patched Python modules through a scratch import shim because the available compiled extension predates this checkout. Full coverage will run in CI against a fresh build.
Authored with assistance from an AI coding assistant.
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @jataylo @azahed98