microsoft / microsoft/pyright

OrderedDict type inference slow in Pytorch

Open
#10,513 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

**Describe the bug**
Typechecking a file with with large OrderedDict variables is taking 14 seconds to analyze.

Steps:

clone https://github.com/pytorch/pytorch

run `pyright torch/ --verbose`

notice these two files are slow
```
[FG] Long operation: checking: file:///c%3A/dev/pytorch/torch/testing/_internal/common_methods_invocations.py (13595ms)
[FG] Long operation: checking: file:///c%3A/dev/pytorch/torch/utils/hipify/cuda_to_hip_mappings.py (14774ms)
```

the second file (https://github.com/pytorch/pytorch/blob/main/torch/utils/hipify/cuda_to_hip_mappings.py) contains large OrderedDict with no type annotations

```python
CUDA_TYPE_NAME_MAP = collections.OrderedDict(
[
("CUresult", ("hipError_t", CONV_TYPE, API_DRIVER)),
("cudaError_t", ("hipError_t", CONV_TYPE, API_RUNTIME)),
("cudaError", ("hipError_t", CONV_TYPE, API_RUNTIME)),
...
```

adding generic info cuts the time in half to 7 seconds
```python
CUDA_TYPE_NAME_MAP = collections.OrderedDict[str, tuple[str, int, int, int]](
```

adding annotations cuts the time in half, but 7 seconds still seems slow.
```
CUDA_TYPE_NAME_MAP: collections.OrderedDict[str, tuple[str, int, int, int]] = collections.OrderedDict(
```

I'm working on PR to update these in pytorch but thought maybe there is something pyright can improve on too.

should we warn when generic parameters are missing on collections.OrderedDict?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the slowdown with `pyright torch/ --verbose` using `torch/testing/_internal/common_methods_invocations.py` and `torch/utils/hipify/cuda_to_hip_mappings.py`. Compare the reported times for unannotated, generic, and explicitly annotated OrderedDict declarations. Done means identifying a type-inference improvement or determining whether a missing-generic-parameter warning is appropriate.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
performance, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.