Updated on 2024-10-29 GMT+08:00

Connecting to a Notebook Instance Through VS Code Toolkit

This section describes how to use the ModelArts VS Code Toolkit plug-in to remotely connect to a notebook instance.

Prerequisites

You have downloaded and installed VS Code. For details, see Installing VS Code.

Step 1 Install the VS Code Plug-in

  1. Search for ModelArts-HuaweiCloud in the EXTENSIONS text box and click Install.
    Figure 1 Installing the VS Code plug-in
  2. Wait for about 1 to 2 minutes.
    Figure 2 Installation process
  3. After the installation is complete, check the message displayed in the lower right corner. If the ModelArts icon and remote SSH icon are displayed in the navigation pane on the left, the VS Code plug-in is installed.
    Figure 3 Installation completion message
    Figure 4 Installation completed
Network issues may cause an installation failure. If this occurs, proceed with follow-up operations. After 1 in Step 4 Access the Notebook Instance is performed, the system will automatically display a dialog box shown in the following figure. In this case, click Install and Reload.
Figure 5 Reconnecting remote SSH

Step 2 Log In to the VS Code Plug-in

  1. In the local VS Code development environment, click and User Settings, and configure the login information.
    Figure 6 Logging in to the plug-in
    Enter the login information and click Log in.
    • Name: Custom username, which is displayed only on the VS Code page and is not associated with any Huawei Cloud account.
    • AK and SK: Access key pair. To create a key pair, choose My Credentials > API Credentials > Access Keys, and click Create Access Key. For details, see How Do I Obtain an Access Key?.
    • Region: must be the same as that of the notebook instance to be remotely connected. Otherwise, the connection will fail.
  2. After the login, check the notebook instance list.

    The list displays only notebook instances in the default workspace on the ModelArts console.

    Figure 7 Login succeeded

Step 3 Create a Notebook Instance

  • Create a notebook instance with remote SSH enabled, and download the key file to either of the following directories based on your OS:

    Windows: C:\Users\{{user}}

    macOS/Linux: Users/{{user}}

  • A key pair is automatically downloaded after you create it. Securely store your key pair. If an existing key pair is lost, create a new one.

Create a notebook instance with remote SSH enabled. For details, see Creating a Notebook Instance.

Step 4 Access the Notebook Instance

  1. In the local VS Code development environment, right-click the instance name and choose Connect to Instance from the shortcut menu to start and connect to the notebook instance.

    The notebook instance can either be running or stopped. If it is stopped, the VS Code plug-in starts the instance and then connects to it.

    Figure 8 Connecting to a notebook instance
    Alternatively, click the instance name. On the instance details page, click Connect. Then, the system automatically starts and connects to the notebook instance.
    Figure 9 Viewing details about a notebook instance
  2. When you connect to a notebook instance for the first time, the system prompts you in the lower right corner to configure the key file. In this case, select the local .pem key file and click OK.
    Figure 10 Configuring the key file
  3. Wait for about 1 to 2 minutes until the notebook instance is accessed. After information similar to the following is displayed in the lower left corner of the VS Code environment, the connection is succeeded.
    Figure 11 Connection succeeded

Debugging Code Remotely

  1. On the VS Code page, upload local code to the cloud development environment.
    1. Choose File > OpenFolder, select the path to be opened, and click OK.
      Figure 12 Open Folder
      Figure 13 Selecting a file path

    1. In the displayed directory structure on the left of the IDE, drag the code and files you want to upload to the corresponding folders. Then, the code is uploaded to the cloud development environment.
    2. Open the code file to be debugged in VS Code. Before running the code, click the default Python version in the lower left part and select a version as required.
      Figure 14 Selecting a Python version

  2. Click the execution button to run the code. The code output is shown on the TERMINAL tab page.
    • If a training job takes a long time to execute, run the job at the backend through the nohup command. This prevents the disconnection of an SSH session or a network failure from affecting job execution. The following shows an example nohup command:
      nohup your_train_job.sh > output.log 2>&1  &  tail -f  output.log
    • To debug the code, perform the following operations:
      1. Choose Run > Run and Debug on the left.
      2. Select the default Python code file.
      3. Click on the left of the code to set breakpoints.
      4. Debug the code according to the debug procedure which is displayed above the code, and the debug information is displayed on the left of the page.

Related Operations

For details about uninstalling the VS Code plug-in, see Figure 15.

Figure 15 Uninstalling the VS Code plug-in