Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mypy is slow when type checking torch #17919

Open
hauntsaninja opened this issue Oct 11, 2024 · 4 comments
Open

mypy is slow when type checking torch #17919

hauntsaninja opened this issue Oct 11, 2024 · 4 comments

Comments

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Oct 11, 2024

λ mypy --version          
mypy 1.11.2 (compiled: yes)

λ uv pip show torch       
Using Python 3.11.8 environment at /Users/shantanu/.virtualenvs/openai-wfht
Name: torch
Version: 2.1.0
Location: /Users/shantanu/.virtualenvs/openai-wfht/lib/python3.11/site-packages
Requires: filelock, fsspec, jinja2, networkx, sympy, typing-extensions
Required-by: ...

λ time mypy -c 'import torch' --no-incremental
Success: no issues found in 1 source file
mypy -c 'import torch' --no-incremental  33.09s user 2.73s system 98% cpu 36.391 total

λ time mypy -c 'import torch'
Success: no issues found in 1 source file
mypy -c 'import torch'  6.24s user 0.88s system 95% cpu 7.454 total

We use a lot of torch at work, performance is probably the biggest reason folks at work switch to a different type checker.

@hauntsaninja
Copy link
Collaborator Author

hauntsaninja commented Oct 11, 2024

If this is accurate, maybe the fscache exception handling is really slowing us down in the mypyc build.

mypyc:
native

interpreted:
interpreted

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 11, 2024

mypy -v produces details about processed files, and this seems important:

LOG:  Processing SCC of size 945 (torch.onnx._globals torch._inductor.exc torch._inductor.runtime.hi
nts torch.utils._traceback torch.utils._sympy.functions ... <long output snipped>

Mypy detects an import cycle with 945 modules.

Overall 1380 files were parsed, so 68% of processed files are in this one SCC. I've seen this pattern in other third-party packages as well -- the majority of the implementation is a single SCC.

A potential way to make the SCC smaller would be to process imports lazily in third-party modules (where this is possible, since errors aren't reported). It may be tricky to implement though, but I'll think about it more.

@hauntsaninja
Copy link
Collaborator Author

Yeah, lazy import resolution could be a massive perf win

@hauntsaninja
Copy link
Collaborator Author

#17924 is the issue for tracking lazy resolution

Jukka's times in #17920 (comment) are much better than mine. #17948 is the issue for tracking performance improvements in my work environment.

@hauntsaninja hauntsaninja added performance and removed bug mypy got something wrong labels Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants