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

Connecting to a Notebook Instance Through PyCharm Toolkit

AI developers use PyCharm to develop algorithms or models. ModelArts provides the PyCharm Toolkit plug-in to help AI developers quickly submit locally developed code to the ModelArts training environment. With PyCharm Toolkit, developers can quickly remotely access notebook instances, upload code, submit training jobs, and obtain training logs for local display so that they can better focus on local code development.

This section introduces how to connect to a notebook instance through PyCharm Toolkit.

Constraints

  • Currently, only versions between 2019.2 and 2023.2 (including 2019.2 and 2023.2) are supported, including the community edition and professional edition.
  • PyCharm of only the professional edition can be used to access notebook instances.
  • You can use a community or professional edition of PyCharm Toolkit to submit training jobs. The latest version of PyCharm Toolkit can be used only to submit training jobs of the new version.
  • PyCharm Toolkit supports only PyCharm of the Windows version.
Table 1 Toolkit functions of the latest version

Supported Function

Description

Reference

Remote SSH connection

The notebook development environment can be accessed through remote SSH.

Connecting to a Notebook Instance Through PyCharm Toolkit

Model training

Code developed locally can be quickly submitted to ModelArts and a training job of the new version is automatically created. During the running of the training job, training logs can be obtained and displayed on a local host.

Using PyCharm Toolkit to Create and Debug a Training Job

OBS-based upload and download

Local files or folders can be uploaded to OBS and files or folders can be downloaded from OBS to a local directory.

Uploading Data to a Notebook Instance Through PyCharm

Prerequisites

PyCharm professional edition of a version between 2019.2 and 2023.2 (including 2019.2 and 2023.2) has been installed on the local PC. Remote SSH applies only to the PyCharm professional edition. Download PyCharm and install it.

Step 1 Downloading and Installing PyCharm Toolkit

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

Step2 Creating 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 3 Logging 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 1 Edit Credential

    If ModelArts > Edit Credential is not displayed on the menu bar, the PyCharm version may be too high. PyCharm Toolkit is not adapted to PyCharm versions later than 2023.2. Download the PyCharm professional edition of a version between 2019.2 and 2023.2 (including 2019.2 and 2023.2).

  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/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.
    Figure 2 Entering the region and access keys
  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 HUAWEI CLOUDcredential is valid.

Step 4 Automatically Configuring 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. Choose the target instance from the drop-down list, where all notebook instances with remote SSH enabled under the account are displayed.
    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 Accessing 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 Uploading Local Files to a 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

Step 7 Remotely Debugging the Code

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