Connect your remote GPU-Jupyter to your local VS Code — A great Deep Learning Solution
Visual Studio Code, also known as VS Code, is a free, open-source, cross-platform code editor developed by Microsoft that has quickly become one of the most popular tools among developers. With its intuitive interface, an extensive list of features, and a vast extension library, VS Code is a versatile and powerful tool that can enhance productivity for developers of all levels.
Many developers are unaware of a powerful feature that is not provided by most free IDEs — the ability to connect to remote servers. In this tutorial, we will explore how effortless it is to perform GPU computations remotely using Visual Studio Code.
Requirements
1. A local machine on which you prefer to develop with VS Code.
2. A remote machine with an NVIDIA GPU and a dockerized instance of GPU-Jupyter is needed. A dockerized instance of GPU-Jupyter running on a remote machine with an NVIDIA GPU offers several advantages, such as isolated instances that share common resources and can be easily scaled. With this setup, data scientists can have specific instances of GPU-Jupyter for different projects, making it a flexible and efficient solution for their needs. Check out our step-by-step guide on Medium and Github to learn how to set up this environment.
Installation of Code-server
To connect to the server using VS Code, the Code-server must be installed on the DSWS for the user according to this guide. Write your own username instead of `[username]`.
curl -fsSL https://code-server.dev/install.sh | sh
sudo systemctl start code-server@[username]
sudo systemctl enable --now code-server@[username]
Setup VS Code
Open a new window in your local VS Code and install the following Extensions:
Python
Jupyter
Remote — SSH
Then close and, click on the ><
— Symbol (“Open a Remote Window”) in the bottom left and select in the top bar “Connect Current Window to host”.
Connect to the remote machine via SSH, using your username and IP address. Note messages popping up in VS Code’s UI, you have to enter the remote OS, a keyfile, or your password.
You are now connected to the remote machine via SSH.
Now open a respective directory in which you want to develop. Therefore, open the Explorer, “Open Folder” and specify a directory on the remote machine. Then confirm with your password and that you are trusting the code.
Connect to a remote Kernel in Docker
Create a new Juypter Notebook or create a new one. Then run a cell using [Shift]+[Enter] and note a message in the top bar approaches. Select “Existing Jupyter Server” and “Enter the URL of the running Jupyter Server”.
Then you have to specify the remote kernel you want to connect with. Therefore provide the URL as you would open it in a browser, containing the hostname (or IP-address), the exposed port, and the token of the Jupyter server. The token can be extracted in a VS Code Terminal (as we are already connected per SSH) or on the remote machine with the command “docker exec -it [ service_name | UID] jupyter server list”.
Finally, select “Python 3” or whatever kernel you want and start computing. Of course, the same works for standard Python files.
We are connected both to the directory per SSH and kernel. Now, we can run code remotely:
Optional: Select a Python Interpreter for Code Inspection
While Visual Studio Code (VSCode) unfortunately does not provide support for selecting remote kernels like Jupyter for code inspection yet, you can still choose a kernel via SSH. However, this workaround deviates from the clean Docker-based setup we’ve been using. Nevertheless, to enable the Linter to detect the packages in your code for inspection, you can create a virtual environment using virtualenv
. Here, it is shown how to set up the “Pylint”-Extension for code inspection that can be installed as VSCode Extension. Follow these steps:
sudo apt install python3-virtualenv
virtualenv venv # create a virtual environment in venv
source venv/bin/activate # activate the virtual environment
(venv)$ pip install -r path/to/requirements.txt # install the requirements
(venv)$ which python # extract the path to Python in the virtual env
#/path/to/venv/bin/python
With the virtual environment ready, you can now select it as your Python interpreter in VSCode:
- Open the Command Palette with (Ctrl+Shift+P) and choose “Python: Select Interpreter”.
- Enter the extracted Python path from the virtual environment, such as
/path/to/venv/bin/python
. - Restart the linter by selecting “Pylint: Restart Server” in the Command Palette (Ctrl+Shift+P). Then reload the window or restart VSCode.
After completing these steps, the linter should detect and inspect the packages installed within the virtual environment. In case the PyLance Linter is still running and detecting false positives, you can disable or uninstall this plugin. Keep in mind that your code will still be executed within the isolated Docker container.
The whole GPU-Jupyter Series:
- Set up Your own GPU-based Jupyter easily using Docker.
- Learn how to connect VS Code to your remote GPU-Jupyter instance to incorporate the advantages of an IDE and the GPU-Jupyter.
- Use the Dockerized GPU-Jupyter for Reproducible Research for ML and AI.
Enjoy running your own code on the GPU :)