Accelerate PyTorch model inference with EAIS (Python)
You can attach an Elastic Acceleration Instance (EAIS) to a non-GPU ECS instance to create a new type of GPU-accelerated instance. EAIS provides the ECS instance with GPU resources. Compared with purchasing a dedicated GPU instance, this method provides flexible GPU resources and significant cost savings. If you are new to EAIS, this tutorial provides a complete walkthrough of using a Python script to accelerate PyTorch model inference on an ECS instance with EAIS, helping you get started quickly.
Background
This tutorial guides you through creating an eais.ei-a6.2xlarge EAIS instance in the China (Hangzhou) region. Using the resnet50 and bert-base models as examples, it demonstrates the complete process of using EAIS to accelerate PyTorch models for image classification and NLP inference. If you encounter performance or functional issues during EAIS inference, contact EAIS Technical Support for customized optimization solutions.
Inference methods
EAIS supports the following two methods for PyTorch model inference:
-
Use the EAIS miniconda environment
The EAIS miniconda environment, developed by Alibaba Cloud, is a ready-to-use environment based on miniconda. It includes sample scripts and model files for testing and evaluation.
This tutorial focuses on using Python scripts. For C++ instructions, see Use EAIS to accelerate PyTorch model inference (C++).
-
Install the EAIS Python package in an existing PyTorch environment
For more information, see Use EAIS to accelerate PyTorch model inference (Python) in the Operations Guide.
Inference performance
EAIS delivers significantly higher inference performance than a GPU instance (NVIDIA T4). The following table compares the inference performance of an eais.ei-a6.2xlarge EAIS instance with a GPU instance (NVIDIA T4) when using a Python script.
-
The data is for illustrative purposes only. Actual results may vary based on your specific workload.
-
You can also use the Python scripts provided in the eais-miniconda package to test the performance of a GPU instance (NVIDIA T4) and compare it with the EAIS instance.
|
Inference model |
eais.ei-a6.2xlarge |
GPU instance (NVIDIA T4) |
Performance multiplier |
|
resnet50 |
2.19 ms |
6.24 ms |
2.85x |
|
bert-base |
5.37 ms |
8.32 ms |
1.55x |
Step 1: Create and attach ECS and EAIS instances
-
Create an ECS instance.
-
Go to the ECS instance creation page.
-
Create an ECS instance in a VPC to which you can attach the EAIS instance.
For detailed instructions, see Create an instance by using the wizard.
NoteWe recommend using Ubuntu 18.04 as the operating system for the ECS instance.
-
-
Create an EAIS instance.
-
Log on to the EAIS console.
-
In the navigation pane on the left, choose Elastic Acceleration Instance EAIS > Instances.
ImportantIf your Alibaba Cloud account does not have the AliyunServiceRoleForEais role (with the AliyunServiceRoleForEais policy), you must first authorize EAIS to create a service-linked role before you can create an EAIS instance. For more information, see Service-linked roles for EAIS.
-
Click Create Elastic Acceleration Instance.
-
On the purchase page, configure the parameters for the EAIS instance, and then click Buy Now.
The following table describes the parameters.
Parameter
Example configuration
Region
China (Hangzhou)
Instance name
eais_test
Instance type
eais.ei-a6.2xlarge
VPC
eais-vpc
vSwitch
eais-vswitch
Security group
eais-securitygroup
Resource group
Default Resource Group
-
On the Confirm Order page, for the Service Agreement, select I have read and agree to the Elastic Acceleration Instance Service Agreement, and then click Activate Now.
-
-
Attach the EAIS instance to the ECS instance.
-
Click Management Console to return to the EAIS console.
-
In the EAIS instance list, find the instance named
eais_testand click Attach in the Actions column. -
In the dialog box that appears, select the ECS instance to attach.
-
Click OK.
The instance list refreshes automatically every 15 seconds. When the instance status changes from Attaching to Attached, the attachment is complete.
ImportantAn EAIS instance can be attached to only one ECS instance at a time, but an ECS instance can have one or more EAIS instances attached. For more information, see Limits.
-
Step 2: Start the EAIS instance
This step is required only for EAIS instances created through the EAIS console or by calling the CreateEaisEi operation.
-
In the EAIS instance list, find the instance named
eais_testand click Start in the Actions column. -
In the Start Instance dialog box, click OK.
The instance list refreshes automatically every 15 seconds. When the instance status changes from Attached to In Use, the instance has started.
Step 3: Log on to the ECS instance
-
Log on to the EAIS console.
-
In the EAIS instance list, click the ECS instance ID associated with
eais_testto open the instance details page. -
Remotely connect to the ECS instance.
For detailed instructions, see Connection methods. On the ECS instance details page, click Remote Connection.
Step 4: View EAIS information
-
Install the eais-tool package.
-
Run the following commands to install the eais-tool Debian package.
export VERSION=4.2.5 wget https://eais-rel-pub.oss-cn-beijing.aliyuncs.com/packages/eais-tool_${VERSION}_amd64.deb sudo dpkg -i eais-tool_${VERSION}_amd64.deb source /etc/profile -
Run the following command to verify the installation.
dpkg -l | grep eais-toolIf the output contains information about the eais-tool package, the installation was successful.
-
-
View EAIS information.
After you install the eais-tool package, run the
eais_smicommand to view information such as the EAIS instance type and GPU utilization. The following is a sample output.root@ixxx:~# eais_smi |-------------------------------+----------------------+----------------------| | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. | | | | MIG M. | |===============================+======================+======================| | 0 eais.ei-a6.2xlarge On | 00000000:00:07.0 Off | 0 | | N/A 28C P8 9W / 70W | 0MiB / 15109MiB | 0% Default | | | | N/A | +-------------------------------+----------------------+----------------------+
Step 5: Set up the runtime environment
-
Install CUDA.
-
Run the following commands to download and install the CUDA package.
wget https://developer.download.nvidia.com/compute/cuda/11.7.0/local_installers/cuda_11.7.0_515.43.04_linux.run sudo sh cuda_11.7.0_515.43.04_linux.run --silent --toolkit -
Run the following commands to set the CUDA-related environment variables.
export PATH=/usr/local/cuda/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
-
-
Install the eais-cuda package.
NoteThis topic uses an ECS instance that runs Ubuntu as an example. For instructions on how to install cGPU on CentOS or Alibaba Cloud Linux, see eais-cuda.
-
Run the following commands to install the eais-cuda Debian package.
export VERSION=4.2.5 wget https://eais-rel-pub.oss-cn-beijing.aliyuncs.com/packages/eais-cuda_${VERSION}_amd64.deb sudo dpkg -i eais-cuda_${VERSION}_amd64.deb -
Run the following command to verify the installation.
dpkg -l | grep eais-cudaIf the output contains information about the eais-cuda package, the installation was successful.
-
-
Download and activate EAIS miniconda.
-
Run the following commands to download and unzip the eais-miniconda package.
wget https://eais-rel-pub.oss-cn-beijing.aliyuncs.com/packages/eais-miniconda.zip sudo apt-get update && sudo apt-get install -y unzip unzip -q eais-miniconda.zip -
Run the following command to change to the root directory of the eais-miniconda package.
cd eais-miniconda/NoteAll subsequent operations are performed from this root directory.
-
Run the following command to activate the EAIS miniconda environment.
source bin/activate
-
Step 6: Run and accelerate PyTorch inference
-
Run the following command to change to the Python script test directory.
cd eais/python -
Run the following command to use EAIS for inference with the resnet50 model.
python pytorch_resnet50.py \ -m ../data/models/resnet50.pt \ -l ../data/densenet_labels.txt \ ../data/images/The parameters are described as follows:
-
pytorch_resnet50.py: A sample inference script from the eais-miniconda package. -
../data/models/resnet50.pt: The resnet50 model file from the eais-miniconda package. -
../data/densenet_labels.txt: A file that contains image class names, from the eais-miniconda package. -
../data/images/: The path to sample images from the eais-miniconda package.
The output shows the image classification results. The following is a sample output.
image_id: 0, image_name: ../data/images/car.jpg, max_prob: 0.7719907164573669, max_label_id: 817, class_name: SPORTS CAR image_id: 1, image_name: ../data/images/vulture.jpeg, max_prob: 0.96502685546875, max_label_id: 23, class_name: VULTURE image_id: 2, image_name: ../data/images/mug.jpg, max_prob: 0.8115575909614563, max_label_id: 504, class_name: COFFEE MUG -
-
Run the following command to test the inference performance of the resnet50 model with EAIS.
python pytorch_resnet50_perf.py \ -m ../data/models/resnet50.ptThe output shows the inference latency data for the resnet50 model. The following is a sample output.
(eais-miniconda) root@iZZzed7gollsbkwlqs1fh5Z:~/eais-miniconda/eais/python# python pytorch_resnet50_perf.py \ -m ../data/models/resnet50.pt use 2.192821741104126 ms each inference -
Run the following command to test the inference performance of the bert-base model with EAIS.
python pytorch_bert_perf.py \ -m ../data/models/bert-base-uncased.ptThe output shows the inference latency data for the bert-base model. The following is a sample output.
(eais-miniconda) root@xxx:~/eais-miniconda/eais/python# python pytorch_bert_perf.py \ -m ../data/models/bert-base-uncased.pt use 5.374878644943237 ms each inference
(Optional) Step 7: Stop the EAIS instance
Stop the EAIS instance if you do not currently need its GPU acceleration.
This operation is supported only for EAIS instances created through the EAIS console or by calling the CreateEaisEi operation.
-
Log on to the EAIS console.
-
In the EAIS instance list, find the instance named
eais_testand click Stop in the Actions column. -
In the Stop Instance dialog box, click OK.
The instance list refreshes automatically every 15 seconds. When the instance status changes from In Use to Attached, the instance has stopped.
(Optional) Step 8: Detach and release EAIS instance
Detach an EAIS instance to attach it to a different ECS instance. If you no longer need the EAIS instance, you must first detach it and then release it.
-
Log on to the EAIS console.
-
Detach the EAIS instance from the ECS instance.
-
In the EAIS instance list, find the instance named
eais_testand click Detach in the Actions column. -
In the Detach ECS Instance dialog box, click OK.
The instance list refreshes automatically every 15 seconds. When the instance status changes from Attached to Attachable, the detachment is complete.
-
-
Release the detached EAIS instance.
-
In the EAIS instance list, find the instance named
eais_testand click Release Resource in the Actions column. -
In the Release Resource dialog box, click OK.
The EAIS instance is then removed from the instance list.
-
Related documents
-
You can also use a C++ program for PyTorch model inference with EAIS. For instructions, see Use EAIS to accelerate PyTorch model inference (C++).
-
To learn how to use EAIS for TensorFlow model inference, see Use EAIS to accelerate TensorFlow model inference.
For more information about using EAIS instances, see Use EAIS instances or Tutorials.