Rob Timpe
8101fd46d4
[dynamo] Implement iter with a polyfill ( #162768 )
...
Currently most variable trackers implement `iter` via `_call_iter_tuple_list`.
This makes it difficult to customize the behavior of `iter` for different
variable types. Instead, implement `iter` via a polyfill, which will delegate
to the appropriate `__iter__` method.
While this method is more flexible, it increases the overhead of dynamo tracing.
For example, `iter(x)` will generate 9x more instructions than the current
implementation for common iterable types. Microbenchmarking shows a ~6x
slowdown for this operation. I suspect this would be much less for realistic
workloads, but more work would be needed to get specific numbers. If the
performance is a concern we could also consider adding a fast path for types
that are known to correctly implement `__iter__`.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/162768
Approved by: https://github.com/guilhermeleobas
ghstack dependencies: #166349
2025-10-29 21:54:37 +00:00
Laith Sakka
adedf26e21
Support python slicing with tensor inputs. ( #165074 )
...
when the slice is tensor, we decompose it to .item() call and pass the unbacked symbol to the slice to avoid DDE.
the diff also fix an existing bug in codegen_dynamic_slice_size in the cpp wrapper. a +1 should be -1 making it match
python codegen.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/165074
Approved by: https://github.com/Lucaskabela
2025-10-29 01:18:45 +00:00
Animesh Jain
a2b6afeac5
[dynamo][guards] CLASS_MATCH guard for readability ( #166217 )
...
We were using FUNCTION_MATCH guard for classes. This was very confusing
(although correct).
Pull Request resolved: https://github.com/pytorch/pytorch/pull/166217
Approved by: https://github.com/jansel
2025-10-26 18:35:27 +00:00
Maggie Moss
c7eee49525
Fix pyrefly ignores 1/n ( #166239 )
...
First diff adjusting the syntax for pyrefly: ignore suppressions so they only hide one class of type error.
Test:
lintrunner
pyrefly check
Pull Request resolved: https://github.com/pytorch/pytorch/pull/166239
Approved by: https://github.com/oulgen
2025-10-26 00:44:10 +00:00
Yuanyuan Chen
9d0b77f4cd
[10/N] Apply ruff UP035 rule ( #165709 )
...
This is a follow-up of #165515 . ruff `UP035` rules are applied to dynamo code to use Py 3.10+ typing.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/165709
Approved by: https://github.com/ezyang
2025-10-25 00:20:13 +00:00
can-gaa-hou
a479769488
[dynamo] Clean up assert in dynamo [2/N] ( #165745 )
...
Extend from #165430
* #165903(Clean up for graph break)
* ->#165745
* #165430
One main refractor from the previous PR:
* For assertions like checking `len(args)` or `len(kwargs)`, using `raise_args_mismatch` instead of `raise_type_error_exc`
I am also considering moving `raise_type_error_exc` into `utils.py` for consistency.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/165745
Approved by: https://github.com/Lucaskabela
2025-10-22 07:12:37 +00:00
Animesh Jain
60992d98b2
[dynamo][remaining] Replace UserFunctionVariable with VariableTracker build ( #165896 )
...
Audit: To prevent future issues with functools.partial or callable objects.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/165896
Approved by: https://github.com/Lucaskabela
2025-10-22 02:13:00 +00:00
can-gaa-hou
a88587348b
[dynamo] Clean up assert in dynamo [1/N] ( #165430 )
...
Fixes some part of #162852 and #164878 . These two issues have some relationship though.
* __->__ #165430
Pull Request resolved: https://github.com/pytorch/pytorch/pull/165430
Approved by: https://github.com/Lucaskabela , https://github.com/williamwen42
Co-authored-by: Lucas Kabela <lucasakabela@gmail.com>
2025-10-19 21:00:05 +00:00
Yuanyuan Chen
3255e7872b
Enable all flake8-logging-format rules ( #164655 )
...
These rules are enabled by removing existing suppressions.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/164655
Approved by: https://github.com/janeyx99 , https://github.com/mlazos
2025-10-19 00:59:28 +00:00
Yuanyuan Chen
e595136187
Enable PLC1802 on ruff ( #165813 )
...
This PR enables ruff check `PLC1802`, which detects len calls on sequences in a boolean test context.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/165813
Approved by: https://github.com/ezyang
2025-10-18 05:44:14 +00:00
Michael Lazos
f15c25d5c3
[user-streams] Move stream code to streams module ( #163027 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/163027
Approved by: https://github.com/StrongerXi , https://github.com/anijain2305
2025-10-14 05:43:19 +00:00
Guilherme Leobas
4e420415e8
Avoids calling builtin iter if object is a generator ( #162521 )
...
The `iter(gen)` call will return the given `gen` object. So, we just avoid this call and shaves off a few ms of tracing time
Pull Request resolved: https://github.com/pytorch/pytorch/pull/162521
Approved by: https://github.com/mlazos
2025-10-13 17:07:54 +00:00
Yuanyuan Chen
fb64da0791
[2/N] Use "is" in python type comparison ( #165142 )
...
This is follow-up of #165037 . It generally recommended to use `is/is not` to compare types. Therefore this series of changes apply this suggestion in the code base, and it aims to finally enabling related linter checks.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/165142
Approved by: https://github.com/albanD
2025-10-10 15:36:44 +00:00
Maggie Moss
c855f8632e
Pyrefly suppressions 7/n ( #164913 )
...
Adds suppressions to pyrefly will typecheck clean: https://github.com/pytorch/pytorch/issues/163283
Almost there!
Test plan:
dmypy restart && python3 scripts/lintrunner.py -a
pyrefly check
step 1: delete lines in the pyrefly.toml file from the project-excludes field
step 2: run pyrefly check
step 3: add suppressions, clean up unused suppressions
before: https://gist.github.com/maggiemoss/4b3bf2037014e116bc00706a16aef199
after:
INFO 0 errors (6,884 ignored)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/164913
Approved by: https://github.com/oulgen
2025-10-08 07:27:17 +00:00
Rob Timpe
2b1236de61
[dynamo] Fix handling of kwargs in exception constructor ( #163390 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/163390
Approved by: https://github.com/guilhermeleobas
2025-09-24 22:44:14 +00:00
FFFrog
a635505a99
[Code Clean] Remove deadcodes about Python3.9 [6/N] ( #163645 )
...
As the title stated.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/163645
Approved by: https://github.com/albanD
ghstack dependencies: #163626 , #163627 , #163629 , #163643 , #163644
2025-09-24 07:30:50 +00:00
Guilherme Leobas
c8255c67cd
redirect iter(range) to range.__iter__() ( #161803 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/161803
Approved by: https://github.com/anijain2305
ghstack dependencies: #161801 , #161802
2025-09-04 02:33:03 +00:00
Guilherme Leobas
e3718c4855
[dict] Implement dict.__ior__ and fix return type in dict.__or__ ( #155072 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/155072
Approved by: https://github.com/anijain2305
ghstack dependencies: #160156
2025-08-27 15:46:40 +00:00
Animesh Jain
3d406429b0
[dynamo][vllm] Support typing.get_type_hints ( #161362 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/161362
Approved by: https://github.com/Skylion007 , https://github.com/StrongerXi , https://github.com/jansel
2025-08-27 09:55:31 +00:00
Rob Timpe
4c36c8a994
[dynamo] Support method calls on complex ConstantVariables ( #161122 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/161122
Approved by: https://github.com/mlazos , https://github.com/guilhermeleobas
2025-08-22 21:40:03 +00:00
Rob Timpe
35e4d97e04
[dynamo] Support builtin complex with constant args ( #160799 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/160799
Approved by: https://github.com/guilhermeleobas , https://github.com/mlazos
2025-08-19 20:38:54 +00:00
Guilherme Leobas
c6333f7dae
Fixes for collections.NamedTuple ( #159367 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/159367
Approved by: https://github.com/mlazos
ghstack dependencies: #159365 , #159366 , #159368 , #159483 , #159902 , #159864 , #159865
2025-08-18 17:32:59 +00:00
Guilherme Leobas
74871d4d46
[collections.abc] Ensure that binop calls works with UserDefinedObjects ( #159865 )
...
Changes:
(1) Replace UserDefinedSetVariable by UserDefinedObjectVariable in all binop calls
Test plan:
(1) The three tests from CPython `test_collections.py` ensures that Dynamo can trace through a dunder method (e.g. __add__, __ixor__, etc) defined in a user defined class
Pull Request resolved: https://github.com/pytorch/pytorch/pull/159865
Approved by: https://github.com/mlazos
ghstack dependencies: #159365 , #159366 , #159368 , #159483 , #159902 , #159864
2025-08-16 20:44:40 +00:00
Guilherme Leobas
f019da2979
Implement list(UserDefinedObject) via force_unpack_var_sequence ( #159864 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/159864
Approved by: https://github.com/mlazos
ghstack dependencies: #159365 , #159366 , #159368 , #159483 , #159902
2025-08-16 20:44:40 +00:00
Guilherme Leobas
b78968b4d1
Support next(iterator, default) ( #159483 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/159483
Approved by: https://github.com/mlazos
ghstack dependencies: #159365 , #159366 , #159368
2025-08-15 19:08:21 +00:00
Guilherme Leobas
e5621b4d8b
Fixes for collections.Counter ( #159368 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/159368
Approved by: https://github.com/mlazos
ghstack dependencies: #159365 , #159366
2025-08-15 19:08:21 +00:00
Guilherme Leobas
2542e71f3f
Change mutation type of MutableMappingVariable to AttributeMutationNew ( #159366 )
...
Also add MutableMappingVariable to `call_or_` / `call_ior`
Pull Request resolved: https://github.com/pytorch/pytorch/pull/159366
Approved by: https://github.com/zou3519
ghstack dependencies: #159365
2025-08-15 19:08:21 +00:00
Guilherme Leobas
f9be65cea4
[iter] Wrap iter(..) call in a ObjectIteratorVariable ( #156460 )
...
This object keeps track when the iterator is exhausted (raise Stopiteration).
Pull Request resolved: https://github.com/pytorch/pytorch/pull/156460
Approved by: https://github.com/zou3519
ghstack dependencies: #156371 , #156416
2025-07-29 21:24:20 +00:00
Guilherme Leobas
4e3e3dc0a7
[iter] support iter(callable, sentinel) ( #156416 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/156416
Approved by: https://github.com/XuehaiPan , https://github.com/zou3519
ghstack dependencies: #156371
2025-07-29 21:24:20 +00:00
Guilherme Leobas
fcf59df2b6
[iter] Add support for sequence protocol in iter(..) ( #156371 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/156371
Approved by: https://github.com/zou3519
2025-07-29 21:24:20 +00:00
Lucas Kabela
efc810c7d0
[Bugfix] Fix circular import between export and dynamo from tensor fn map ( #158931 )
...
Fixes #158120
The issue was caused by populating a builtin tensor fn map at import time; if torch.export.export was called before any dynamo imports with the `meta` device, this map would not be populated, and so would populate on import time which would try to call `torch.disable`, which would not yet be initialized
Fix is to populate this map lazily
```
python test/dynamo/imports_non_circular_repro.py TestImports.test_circular_import_with_export_meta
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/158931
Approved by: https://github.com/StrongerXi , https://github.com/mlazos , https://github.com/anijain2305
2025-07-24 22:24:57 +00:00
Guilherme Leobas
0204099762
Raise exception in Dynamo if op fails in the interpreter ( #158661 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/158661
Approved by: https://github.com/williamwen42
ghstack dependencies: #158660
2025-07-23 22:31:51 +00:00
Guilherme Leobas
576253c476
[math] Trace float.fromhex ( #156976 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/156976
Approved by: https://github.com/zou3519
ghstack dependencies: #156975 , #156977
2025-07-23 16:12:08 +00:00
Guilherme Leobas
ba8d19ec02
[dict] Allow Dynamo to trace through explicit dict dunder method call ( #154794 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/154794
Approved by: https://github.com/mlazos
ghstack dependencies: #154003 , #154793
2025-07-10 22:50:39 +00:00
Guilherme Leobas
e84710d1e7
[dict] Raise TypeError in dict methods ( #154003 )
...
Raise TypeError in the following scenarios:
* #args mismatch
* arg is unhashable
Pull Request resolved: https://github.com/pytorch/pytorch/pull/154003
Approved by: https://github.com/mlazos , https://github.com/zou3519
2025-07-10 22:50:39 +00:00
Guilherme Leobas
e49acfc5c5
[list] Raise exception in invalid list method call ( #156148 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/156148
Approved by: https://github.com/zou3519
ghstack dependencies: #153969
2025-07-07 14:51:10 +00:00
Guilherme Leobas
dfcda613b6
Ensure Dynamo can trace through explicit dunder method call ( #154366 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/154366
Approved by: https://github.com/zou3519
ghstack dependencies: #153150 , #152991 , #154539 , #153553 , #154063 , #154064 , #154065 , #154066 , #154263
2025-07-04 00:46:05 +00:00
Guilherme Leobas
11c71053e0
[Dynamo] [Set] Implement some binop operators for dict/set/frozenset/dict_keys ( #154063 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/154063
Approved by: https://github.com/williamwen42 , https://github.com/zou3519
ghstack dependencies: #153150 , #152991 , #154539 , #153553
2025-07-04 00:45:34 +00:00
Guilherme Leobas
f651e28f80
[FrozenSet] Fixes for FrozenSet ( #152991 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/152991
Approved by: https://github.com/zou3519
ghstack dependencies: #153150
2025-07-04 00:45:11 +00:00
Animesh Jain
48560eef80
[dynamo] Fix bug in dict(mapping_proxy) ( #157467 )
...
Fixes https://github.com/pytorch/pytorch/issues/157284
Pull Request resolved: https://github.com/pytorch/pytorch/pull/157467
Approved by: https://github.com/jansel , https://github.com/StrongerXi
Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com>
2025-07-02 22:13:02 +00:00
Guilherme Leobas
9d175bc7e6
Fixes for CPython int/float tests ( #155978 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/155978
Approved by: https://github.com/zou3519
2025-07-02 15:04:00 +00:00
PyTorch MergeBot
c202a7329a
Revert "Fixes for CPython int/float tests ( #155978 )"
...
This reverts commit 23491519d2 .
Reverted https://github.com/pytorch/pytorch/pull/155978 on behalf of https://github.com/XuehaiPan due to sys.get_int_max_str_digits is not always available ([comment](https://github.com/pytorch/pytorch/pull/155978#issuecomment-3021990027 ))
2025-07-01 06:16:49 +00:00
Guilherme Leobas
23491519d2
Fixes for CPython int/float tests ( #155978 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/155978
Approved by: https://github.com/zou3519
2025-06-30 19:42:11 +00:00
PyTorch MergeBot
da1f337bc4
Revert "Fixes for CPython int/float tests ( #155978 )"
...
This reverts commit fab53dfdf1 .
Reverted https://github.com/pytorch/pytorch/pull/155978 on behalf of https://github.com/guilhermeleobas due to failing in trunk ([comment](https://github.com/pytorch/pytorch/pull/155978#issuecomment-3019457531 ))
2025-06-30 14:49:44 +00:00
Guilherme Leobas
fab53dfdf1
Fixes for CPython int/float tests ( #155978 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/155978
Approved by: https://github.com/zou3519
2025-06-30 14:15:47 +00:00
PyTorch MergeBot
0decd966af
Revert "Fixes for CPython int/float tests ( #155978 )"
...
This reverts commit 216bd6091e .
Reverted https://github.com/pytorch/pytorch/pull/155978 on behalf of https://github.com/huydhn due to Some tests are still failing in trunk ([comment](https://github.com/pytorch/pytorch/pull/155978#issuecomment-3014185210 ))
2025-06-27 19:39:41 +00:00
Guilherme Leobas
216bd6091e
Fixes for CPython int/float tests ( #155978 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/155978
Approved by: https://github.com/zou3519
2025-06-27 16:41:00 +00:00
Ryan Guo
d06a406656
[dynamo] Graph break on torch.Tensor.data assignment with mismatched dtype ( #156623 )
...
Fixes #152162 . Discussed with @bdhirsh and decided this is the easiest
workaround for now.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/156623
Approved by: https://github.com/bdhirsh
2025-06-25 02:03:04 +00:00
PyTorch MergeBot
1dc1eedd43
Revert "[dynamo] Graph break on torch.Tensor.data assignment with mismatched dtype ( #156623 )"
...
This reverts commit c1ad4b8e7a .
Reverted https://github.com/pytorch/pytorch/pull/156623 on behalf of https://github.com/albanD due to Breaks Dynamo tests in trunk ([comment](https://github.com/pytorch/pytorch/pull/156623#issuecomment-3001806841 ))
2025-06-24 20:44:42 +00:00
Ryan Guo
c1ad4b8e7a
[dynamo] Graph break on torch.Tensor.data assignment with mismatched dtype ( #156623 )
...
Fixes #152162 . Discussed with @bdhirsh and decided this is the easiest
workaround for now.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/156623
Approved by: https://github.com/bdhirsh
2025-06-24 19:33:11 +00:00