Commit Graph

28 Commits

Author SHA1 Message Date
bac62341eb PEP585 update - torch/_inductor (#145198)
See #145101 for details.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/145198
Approved by: https://github.com/bobrenjc93
2025-01-21 21:04:33 +00:00
b22749712c type _inductor/optimize_indexing.py (#137599)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/137599
Approved by: https://github.com/Skylion007, https://github.com/eellison
2024-10-09 23:29:47 +00:00
eac5e12548 [inductor] Move LoopBody to its own file (#135257)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/135257
Approved by: https://github.com/oulgen
2024-09-07 16:29:15 +00:00
b6d477fd56 [BE][Easy][16/19] enforce style for empty lines in import segments in torch/_i*/ (#129768)
See https://github.com/pytorch/pytorch/pull/129751#issue-2380881501. Most changes are auto-generated by linter.

You can review these PRs via:

```bash
git diff --ignore-all-space --ignore-blank-lines HEAD~1
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/129768
Approved by: https://github.com/jansel
2024-07-20 16:20:58 +00:00
ea614fb2b1 Flip default value for mypy disallow_untyped_defs [2/11] (#127839)
See #127836 for details.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/127839
Approved by: https://github.com/oulgen
2024-06-08 18:23:08 +00:00
8cad88e1f3 [BE]: Improve exception typing. Remove NOQAs (#125535)
Improve some exception typing

Pull Request resolved: https://github.com/pytorch/pytorch/pull/125535
Approved by: https://github.com/albanD
2024-05-08 14:07:13 +00:00
c5fafe9f48 [BE]: TRY002 - Ban raising vanilla exceptions (#124570)
Adds a ruff lint rule to ban raising raw exceptions. Most of these should at the very least be runtime exception, value errors, type errors or some other errors. There are hundreds of instance of these bad exception types already in the codebase, so I have noqa'd most of them. Hopefully this error code will get commiters to rethink what exception type they should raise when they submit a PR.

I also encourage people to gradually go and fix all the existing noqas that have been added so they can be removed overtime and our exception typing can be improved.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/124570
Approved by: https://github.com/ezyang
2024-04-21 22:26:40 +00:00
abc09b27b9 Some minor type stub improvements (#118529)
I was just playing around with improving the typing of symbolic_shapes. The PR is not "complete" but I in particular wanted to get feedback on whether or not people liked making ValueRanges Generic; it seems that distinguishing if you have an Expr ValueRange or a SympyBoolean ValueRange is a lot of trouble for downstream. Using TypeGuard, we can perform refinements on the generic parameter inside methods, although we still have to cast back to ValueRange[T] due to https://github.com/python/mypy/issues/14425#issuecomment-1914852707

Signed-off-by: Edward Z. Yang <ezyang@meta.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/118529
Approved by: https://github.com/Skylion007
2024-02-04 00:19:00 +00:00
dbba1d4bf5 Revert "Some minor type stub improvements (#118529)"
This reverts commit c978f38bd4aedeff4ee9ae693349217daea01412.

Reverted https://github.com/pytorch/pytorch/pull/118529 on behalf of https://github.com/facebook-github-bot due to Diff reverted internally ([comment](https://github.com/pytorch/pytorch/pull/118529#issuecomment-1922362331))
2024-02-01 22:18:36 +00:00
c978f38bd4 Some minor type stub improvements (#118529)
I was just playing around with improving the typing of symbolic_shapes. The PR is not "complete" but I in particular wanted to get feedback on whether or not people liked making ValueRanges Generic; it seems that distinguishing if you have an Expr ValueRange or a SympyBoolean ValueRange is a lot of trouble for downstream. Using TypeGuard, we can perform refinements on the generic parameter inside methods, although we still have to cast back to ValueRange[T] due to https://github.com/python/mypy/issues/14425#issuecomment-1914852707

Signed-off-by: Edward Z. Yang <ezyang@meta.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/118529
Approved by: https://github.com/Skylion007
2024-01-31 20:56:56 +00:00
0a9819e3e1 Prefer is_number over is_constant() (#114513)
`is_constant` tries really hard to check whether an expression is
constant. `is_number` is often enough. Note that `sympy.nan.is_number`
is true. Same for infinities

Pull Request resolved: https://github.com/pytorch/pytorch/pull/114513
Approved by: https://github.com/peterbell10
2023-12-05 16:56:15 +00:00
a1d8132210 Enable mypy check in torch/_inductor/optimize_indexing.py (#107943)
Fixes #105230

```shell
$ lintrunner init && lintrunner -a torch/_inductor/optimize_indexing.py
...
ok No lint issues.
Successfully applied all patches.
```

```shell
$ mypy torch/_inductor/optimize_indexing.py
Success: no issues found in 1 source file

Pull Request resolved: https://github.com/pytorch/pytorch/pull/107943
Approved by: https://github.com/Skylion007
2023-08-28 17:08:13 +00:00
ea3381d92c Make StarDep.index throw an error (#107092)
There was an issue where `hasattr(dep, "index")` would incorrectly be True because it was picking up `NamedTuple.index` (a method).  We were also comparing that method to a `sympy.Exper` in one place.

As far as I can tell this wasn't actually causing any bugs (the comparison actually did the right thing), but still good to fix it.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/107092
Approved by: https://github.com/eellison
2023-08-18 17:15:43 +00:00
87a3ed58cb Fix ranges for range vars (#104987)
Ranges are inclusive on both ends...

We take this chance to delete a stale comment

Pull Request resolved: https://github.com/pytorch/pytorch/pull/104987
Approved by: https://github.com/jgong5, https://github.com/eellison
2023-07-14 13:43:05 +00:00
3d07184930 Move optimize indexing to use the class Bounds (#104558)
This PR removes plenty of duplicated code. In particular, it removes the two repeated implementations of `get_expr_range`, which are superseded by the more correct `bound_sympy`.

The two duplicated `get_expr_range`s were a result of an oversight in https://github.com/pytorch/pytorch/pull/100549.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/104558
Approved by: https://github.com/eellison
2023-07-07 23:52:14 +00:00
7ae100628e Move most SymPy functions to their own file (#104556)
All these are standalone implementations of some functions and they
don't depend on anything else, so we better have them under the
`_sympy/` folder on their own

Pull Request resolved: https://github.com/pytorch/pytorch/pull/104556
Approved by: https://github.com/ezyang
2023-07-04 03:53:48 +00:00
79fe3aef2f indutor: fix issue of compute index_expr range (#103147)
For the CPU inductor side, there has an optimization to convert ```int64``` index_expr to ```int32``` for good performance(https://github.com/pytorch/pytorch/blob/main/torch/_inductor/codegen/cpp.py#L2034), but for ```ModularIndexing``` exp, we replace it as division(https://github.com/pytorch/pytorch/blob/main/torch/_inductor/optimize_indexing.py#L73, ```ModularIndexing``` doesn't have derivative) to compute derivative and then compute the expr's value range, there may meet issue which the min value may greater than the max value(```ModularIndexing(513*i2 + i3 + 262400, 512, 513), with vars_ranges is {i2: ValueRanges(lower=0, upper=256), i3: ValueRanges(lower=0, upper=513)}```).

One solution is that we don't replace ```ModularIndexing```, but it can't get the value range.
Another solution is that return ```inf``` range when the min val is great than the max val.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/103147
Approved by: https://github.com/jgong5, https://github.com/eellison
2023-06-19 04:39:02 +00:00
597e2a11a3 indexing_dtype_strength_reduction more aggressive free_symbols tests (#103470)
ValueRanges can't handle symbolic bounds. Be a bit more careful about detecting if you try to pass in expressions with free symbols, and fall back to "don't know" range if this occurs.

Signed-off-by: Edward Z. Yang <ezyang@meta.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/103470
Approved by: https://github.com/eellison
2023-06-13 16:00:41 +00:00
49cd184f89 inductor: improve the index range check for index_expr vec check (#102263)
Fix https://github.com/pytorch/pytorch/issues/102065.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/102263
Approved by: https://github.com/lezcano, https://github.com/peterbell10, https://github.com/jgong5
2023-06-01 03:07:14 +00:00
477f3f555f Simplify by using yield from (#97831)
The issues were found by SIM104 flake8-simplify in a local run.

I'll take a look on adding the check to the CI separately.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/97831
Approved by: https://github.com/Skylion007
2023-03-29 19:15:24 +00:00
50bc25baa0 Move ValueRanges into its own module (#94528)
I am going to use it in ShapeEnv shortly.

Signed-off-by: Edward Z. Yang <ezyang@meta.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/94528
Approved by: https://github.com/eellison
2023-02-11 02:54:49 +00:00
24ae50bcc7 Add config option to reduce warnings in inductor (#94413)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/94413
Approved by: https://github.com/ezyang
2023-02-10 15:44:15 +00:00
8fce9a09cd [BE]: pyupgrade Python to 3.8 - imports and object inheritance only (#94308)
Apply parts of pyupgrade to torch (starting with the safest changes).
This PR only does two things: removes the need to inherit from object and removes unused future imports.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/94308
Approved by: https://github.com/ezyang, https://github.com/albanD
2023-02-07 21:10:56 +00:00
24b501903c Minor sympy usage fix in fbcode (#93171)
Summary: To supports older versions of sympy.

Test Plan:
```
buck2 run @//mode/opt @//mode/inplace -c python.package_style=inplace -c fbcode.enable_gpu_sections=true //caffe2/benchmarks/dynamo:torchbench -- -dcuda --performance --inductor --only hf_T5
```

Differential Revision: D42812188

Pull Request resolved: https://github.com/pytorch/pytorch/pull/93171
Approved by: https://github.com/eellison
2023-01-30 23:34:22 +00:00
19c9b09449 Replace IndexingDiv with FloorDiv in Inductor (#92878)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/92878
Approved by: https://github.com/ezyang
2023-01-24 15:06:22 +00:00
353e9f883f Add name attribute to ValueRangeAnalysis (#92121)
This is expected when used within InterpreterShim.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/92121
Approved by: https://github.com/eellison
2023-01-14 12:07:52 +00:00
fd0030fe74 Fix indexing_dtype_strength_reduction (#91601)
Many of the previous inductive cases were wrong (e.g. `abs`, `sq`, `div` and `truediv`).
We rewrite it using the mathematical terms that allow to prove the relevant upper
and lower bounds.

Note that the inductive step can be seen as a not-too-difficult optimisation problem
with constraints, hence the naming of the functions.

For many of the other functions, we also simplify the formulas, which will be useful
when this code is generalised to work with symbolic shapes.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/91601
Approved by: https://github.com/jansel, https://github.com/eellison
2023-01-10 23:39:30 +00:00
8b3d31cfc5 Add A ValueRange Analysis Pass to convert int64 indexing to int32 (#91028)
Builds up sympy expressions computing the lower and upper bound of ranges, and then finds `op.to_dtype(x, torch.int64)` nodes whose dominated values can all be computed in a lower precision. I haven't gotten all the way to work with dynamic shapes but it should be a fairly small change. There's still additional work to get torchinductor to work with large tensors (see https://github.com/pytorch/torchdynamo/issues/1819) because we would need to add explicit dtype annotations to int64 which we're not doing right now.

Fix for https://github.com/pytorch/torchdynamo/issues/1293.

Performance Before OpBench aten.upsample_bilinear2d.vec float32:
(25th %, 50th %, 75th %)
Before
[0.7521964636710751, 0.8645357996607477, 2.8746003906598494]
After:
[0.9511363478204263, 1.0295566597806718, 3.2662165264101755]
Pull Request resolved: https://github.com/pytorch/pytorch/pull/91028
Approved by: https://github.com/jansel
2022-12-22 20:04:26 +00:00