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

100 % CPU Load when using scipy.optimize.minimize with useLibraryCodeForTypes #1964

Closed
nullket opened this issue Oct 20, 2021 · 2 comments
Closed
Labels
fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@nullket
Copy link

nullket commented Oct 20, 2021

Environment data

  • Language Server version: v2021.10.1
  • OS and version: Ubuntu 20.04
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.10

Expected behaviour

pylance not causing a constant and never ending cpu load of 100 % when using scipy.optimize.minimize

Actual behaviour

When using scipy.optimize.minimize I encounter a never ending high cpu load due to the pylance extension. I was able to figure out, that the problem arises when specifying the function which scipy.optimize.minimize should numerically minimize.

Setting "python.analysis.useLibraryCodeForTypes": false (as mentioned here) solves the issue for the high cpu load. As I am using v2021.10.1 I thought the corresponding issue to this problem should be fixed?

I confirmed this behavior on two different machines.

Code Snippet / Additional information

See this little example snippet:

import scipy.optimize

def dummy(x):
    return x*x

scipy.optimize.minimize(fun=dummy, x0=5)  # Causes high cpu load 
scipy.optimize.minimize(x0=5)  # Code will not work but does not cause high cpu load

Logs

User belongs to experiment group 'pythonaa'
User belongs to experiment group 'pythonJediLSPcf'
User belongs to experiment group 'pythonSendEntireLineToREPL'
User belongs to experiment group 'pythonNotDisplayLinterPrompt'
User belongs to experiment group 'pythonDiscoveryModulecf'
User belongs to experiment group 'pythonTensorboardExperiment'
User belongs to experiment group 'PythonPyTorchProfiler'
> conda info --json
> /bin/python ~/.vscode/extensions/ms-python.python-2021.10.1336267007/pythonFiles/interpreterInfo.py
> /bin/python2 ~/.vscode/extensions/ms-python.python-2021.10.1336267007/pythonFiles/interpreterInfo.py
> /usr/bin/python ~/.vscode/extensions/ms-python.python-2021.10.1336267007/pythonFiles/interpreterInfo.py
> /usr/bin/python2 ~/.vscode/extensions/ms-python.python-2021.10.1336267007/pythonFiles/interpreterInfo.py
Starting Pylance language server.
Python interpreter path: /bin/python
> conda --version

@erictraut
Copy link
Contributor

I've investigated the cause of the long compute time. The scipy library is largely unannotated, so pylance needs to use return type inference to determine the return type of most functions and methods. Since input parameter types are also unannotated, pylance often needs to resort to a more expensive technique we call call-site return type inference where the input parameter types are inferred from the types of the arguments provided at the call site. Pylance currently uses this technique up to 3 levels deep (where function a calls b calls c). In the case of scipy, this was especially expensive given the complexity of the code.

I've changed the code in pylance to limit this expensive form of analysis from 3 levels to 2 levels deep. This may result in less-accurate type inference in some cases, but it greatly improves the performance in this particular example (and likely other cases in scipy).

This change will be included in the next release.

@erictraut erictraut added fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed needs investigation Could be an issue - needs investigation labels Oct 23, 2021
@bschnurr
Copy link
Member

bschnurr commented Oct 28, 2021

This issue has been fixed in version 2021.10.3, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#2021103-27-october-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

4 participants