Updated on 2024-06-12 GMT+08:00

Connecting to a Notebook Instance Through PyCharm Toolkit

ModelArts provides the PyCharm plug-in PyCharm Toolkit for you to remotely access a notebook instance through SSH, upload code, submit a training job, and obtain training logs for local display.

Prerequisites

PyCharm professional 2019.2 or later has been installed locally. Remote SSH applies only to the PyCharm professional edition. Download PyCharm and install it.

Step 1 Create a Notebook Instance

Create a notebook instance with remote SSH enabled and whitelist configured. Ensure that the instance is running. For details, see Creating a Notebook Instance.

Step 2 Download and Install PyCharm Toolkit

Installing through Marketplace

In PyCharm, choose File > Settings > Plugins, search for ModelArts in Marketplace, and click Install.
Figure 1 Installing through Marketplace

The version installed in Marketplace is the latest version.

Step 3 Log In to the Plug-in

To use the AK/SK pair for login authentication, perform the following steps:

  1. Open PyCharm with Toolkit installed. Choose ModelArts > Edit Credential from the menu bar.
    Figure 2 Edit Credential
  2. In the displayed dialog box, select the region where ModelArts is located, enter the AK and SK, and click OK. For details about how to obtain the AK and SK, see How Do I Obtain an Access Key?.
    • Region: Select a region from the drop-down list. It must be the same as the region of the ModelArts console.
    • Project: After the region is selected, the project is automatically filled.
    • Access Key ID: Enter the AK.
    • Secret Access Key: Enter the SK.
  3. View the verification result.

    In the Event Log area, if information similar to the following is displayed, the access key has been successfully added:

    16:01Validate Credential Success: The credential is valid.

Step 4 Automatically Configure PyCharm Toolkit

  1. In the local PyCharm development environment, choose ModelArts > Notebook > Remote Config... and configure PyCharm Toolkit.
    Figure 3 Remotely connecting to PyCharm Toolkit
  2. All notebook instances with remote SSH enabled under the account are displayed. Choose the target instance from the drop-down list.
    Figure 4 Notebook list
    • KeyPair: Select the locally stored key pair of the notebook instance for authentication. The key pair created during the notebook instance creation is saved in your browser's default downloads folder.
    • PathMappings: Synchronization directory for the local IDE project and notebook, which defaults to /home/ma-user/work/Project name and is adjustable.
  3. Click Apply. After the configuration is complete, restart the IDE for the configuration to take effect.

    After the restart, it takes about 20 minutes to update the Python interpreter for the first time.

Step 5 Access a Notebook Instance Through PyCharm Toolkit

Click the notebook instance name and connect it to the local IDE as prompted. The connection is kept for 4 hours by default.

Figure 5 Starting the connection

To interrupt the connection, click the notebook name and disconnect it from the local IDE as prompted.

Figure 6 Interrupting the connection

Step 6 Upload Local Files to the Notebook Instance

Code in a local file can be copied to the local IDE, which will automatically synchronize the code to the in-cloud development environment.

Initial synchronization

In the Project directory of the local IDE, right-click Deployment and choose Upload to Notebook name from the shortcut menu to upload the local project file to the specified notebook instance.

Figure 7 Synchronizing local data to a notebook instance

Follow-up synchronization

After modifying the code, press Ctrl+S to save it. The local IDE will automatically synchronize the modification to the specified notebook instance.

After PyCharm Toolkit is installed, Automatic Upload is automatically enabled in the local IDE for automatically uploading the files in the local directory to the target notebook instance. If Automatic Upload is not enabled, enable it by referring to the following figure.

Figure 8 Enabling Automatic Upload

Click Interpreter in the lower right corner of the local IDE and select a notebook Python interpreter.

Figure 9 Selecting a Python interpreter

Run the code in the notebook instance. The logs are displayed locally.

Figure 10 Runtime logs

Click Run/Debug Configurations in the upper right corner of the local IDE to set runtime parameters.

Figure 11 Setting runtime parameters (1)

Select the Python interpreter that remotely connects to the target notebook instance.

Figure 12 Setting runtime parameters (2)

To debug code, set breakpoints and run the program in debug mode.

Figure 13 Running the program in debug mode

In debug mode, the code execution is suspended in the specified line, and you can obtain variable values.

Figure 14 Viewing variable values in debug mode