I am running some codes that use the JIT feature of Pytorch using remote Python interpreters via SSH on Pycharm. I got the error Ninja is required to load C++ extensions
from the function verify_ninja_availability()
. This is because is_ninja_available()
returns False
.
The minimal codes to produce my problem is:
from torch.utils.cpp_extension import verify_ninja_availability
verify_ninja_availability()
I created a virtual conda environment remotely, installed PyTorch, and configured remote Python interpreters via SSH.
My analysis of the cause
verify_ninja_availability()
simply calls is_ninja_available()
, the code of which is below:
def is_ninja_available():
r'''
Returns ``True`` if the `ninja <https://ninja-build.org/>`_ build system is
available on the system, ``False`` otherwise.
'''
try:
subprocess.check_output('ninja --version'.split())
except Exception:
return False
else:
return True
It seems that it is the subprocess
that causes the problem. According to this answer,
This means that
Popen
looks at the value ofPATH
as it was when Python launched (the Python that runs thePopen
instantiation)
and running via ssh is executing /PATH/TO/CONDA/ENV/bin/python /PATH/TO/SCRIPT/main.py
. The $PATH
when python launched does not contain ninja
, which is located at /PATH/TO/CONDA/ENV/bin/ninja
.
What I have tried
I followed this link to create a script as a wrapper of Python and use the wrapper as the interpreter, but not work either.
I want to somehow execute source ~/.bashrc
remotely before executing, like How to source additional environment in pycharm?. I think "Run/Debug Configurations"->"Before launch"->"+"->"Run Remote External Tool" might be useful, but I do not know how to configure it.
My Pycharm configuration
File->Setting->Project->Python Interpreter->Add Interpreter->On SSH->...->Virtualenv Environment->Existing->select the python of my Conda env.
Possible workaround I found
Pycharm support provides this link, it seems that conda does not support SSH itself. (I guess this is why during my configuration, there is only Virtualenv Environment and System Interpreter and no "Conda Environment"). The link also provide a workaround: select the "System Interpreter" and set the path to the Conda Python.
I also noticed Setting up a PyCharm remote conda interpreter, which provides a link to another Pycharm issue. On the issue page, it shows a different wrapper from https://stackoverflow.com/a/48645242/9758790 that I have tried before. I will have a try later.