You can bind an Elastic Acceleration Instance (EAIS) to a non-GPU Elastic Compute Service (ECS) instance to create a new type of GPU-accelerated instance. Compared to purchasing a dedicated GPU instance, this approach provides GPU resources with greater flexibility and cost-effectiveness. If you are new to EAIS, this tutorial shows you how to accelerate PyTorch model inference on an ECS instance by using a C++ program.
Background information
This tutorial guides you through creating an EAIS instance of the eais.ei-a6.2xlarge instance type in the China (Hangzhou) region. Using the public resnet50 and bert-base models as examples, you will learn how to use an EAIS instance to run PyTorch model inference for image classification and NLP scenarios and accelerate performance. If you encounter performance or feature issues while using an EAIS instance, contact EAIS technical support for customized optimization solutions.
This tutorial focuses on running PyTorch model inference with a C++ program. To learn how to run PyTorch model inference by using a Python script, see Accelerate PyTorch model inference by using an EAIS instance (Python).
Compared to using a GPU instance (NVIDIA T4), using an EAIS instance significantly improves inference performance. The following table compares the inference performance of a C++ program (dependent on libtorch) running on an eais.ei-a6.2xlarge EAIS instance with that on a GPU instance (NVIDIA T4).
-
The data in this topic is for reference only. Your actual performance may vary.
-
You can also use the C++ code provided in the eais-miniconda package to test the inference performance on a GPU instance (NVIDIA T4) and compare the performance with that of an EAIS instance.
|
Inference model |
eais.ei-a6.2xlarge |
GPU instance (NVIDIA T4) |
EAIS performance gain |
|
resnet50 |
2.20 ms |
6.24 ms |
2.84x |
|
bert-base |
5.38 ms |
8.32 ms |
1.55x |
Step 1: Create and associate an ECS instance and an EAIS instance
Create an ECS instance.
Log on to the ECS instance creation page.
Create a virtual private cloud (VPC) ECS instance to associate with the EAIS instance.
For more information, see Create an instance.
NoteUse Ubuntu 18.04 as the operating system for the ECS instance.
Create an EAIS instance.
Log on to the EAIS console.
In the left navigation pane, choose Elastic Accelerated Computing Instances EAIS > Elastic Accelerated Computing Instances List.
ImportantIf your Alibaba Cloud account has not been granted the AliyunServiceRoleForEais role, which uses the AliyunServiceRoleForEais access 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 Role.
Click Create Elastic Accelerated Computing Instance.
On the EAIS instance purchase page, configure the instance parameters and click Buy Now.
The following table describes the configuration items:Configuration item
Example configuration
Region
China (Hangzhou)
Instance name
eais_test
Instance type
eais.ei-a6.2xlarge
Virtual Private Cloud
eais-vpc
vSwitch
eais-vswitch
Security group
eais-securitygroup
Resource group
Default Resource Group
On the Confirm Order page, agree to the Service Agreement by selecting the I have read and agree to the Elastic Accelerated Computing Instances Service Agreement checkbox, and then click Activate Now.

Associate the EAIS instance with the ECS instance.
Click Management Console to navigate to the EAIS console.
In the list of EAIS instances, find the
eais_testinstance and click Associate in the Operation column.
In the dialog box, select the ECS instance that you want to associate.
Click OK.
The EAIS instance list auto-refreshes every 15 seconds. The EAIS instance is successfully associated when the instance status changes from Associating to Associated.
ImportantAn EAIS instance can be associated with only one ECS instance. An ECS instance can be associated with one or more EAIS instances. For more information, see Limits.
Step 2: Start the EAIS instance
This operation is required only for EAIS instances created using the EAIS console or by calling the CreateEaisEi operation.
In the EAIS instance list, find the
eais_testinstance and click Start in the Actions column.
In the Start Instance dialog box, click OK.
The EAIS instance list auto-refreshes every 15 seconds. When the instance status changes from Associated to In Use, the EAIS instance starts.
Step 3: Log on to the ECS instance
Log on to the EAIS console.
In the EAIS instance list, click the ID of the ECS instance that is associated with the
eais_testinstance to go to the details page of the ECS instance.
Remotely log on to the ECS instance.
For more information, see Select a method for remote connection to an ECS instance.

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/profileRun the following command to verify the installation of the eais-tool Debian package.
dpkg -l | grep eais-toolIf the output contains information about the eais-tool package, the eais-tool Debian package is installed successfully.
View EAIS information.
After you install the eais-tool package, run the
eais_smicommand to view information about the EAIS instance, such as the instance type and GPU utilization. The following output is an example.
Step 5: Set up the runtime environment
In this tutorial, you set up the C++ runtime environment using an EAIS Debian package as an example. This package is compiled with CXX11 ABI and depends on libtorch 1.13.1.
-
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 configure environment variables for CUDA.
export PATH=/usr/local/cuda/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
-
-
Install the eais-cuda package.
-
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 check whether the eais-cuda Debian package is installed.
dpkg -l | grep eais-cudaIf the command output contains the eais-cuda package information, the eais-cuda Debian package is installed.
-
-
Install the eais-torch package.
-
Run the following commands to install the eais-torch package.
export VERSION=4.2.5 wget https://eais-rel-pub.oss-cn-beijing.aliyuncs.com/packages/eais-torch1.13.1-cxx11-abi_${VERSION}_amd64.deb sudo dpkg -i eais-torch1.13.1-cxx11-abi_${VERSION}_amd64.deb source /etc/profile -
Run the following command to check whether the eais-torch Debian package is installed.
dpkg -l | grep eais-torchIf the command output contains the eais-torch package information, the eais-torch Debian package is installed.
-
Step 6: Run and accelerate PyTorch model inference
-
Download the EAIS C++ sample.
-
Run the following commands to download and decompress the eais-miniconda package that contains the EAIS C++ sample.
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 go to the root directory of the eais-miniconda package.
cd eais-miniconda/NoteAll subsequent commands in this tutorial should be run from this directory.
-
-
Use the C++ package to run and accelerate PyTorch model inference.
-
Run the following command to go to the test directory of the C++ package.
cd eais/cpp -
Compile the C++ sample script.
-
Run the following command to install the packages that are required to compile and run the sample script.
sudo apt-get install -y build-essential cmake libopencv-dev -
Run the following commands to compile the executable sample program.
mkdir build cd build cmake .. make -j8 cd .. -
Run the following command to view the generated sample program.
ls build/srcThe following output indicates that the sample program is generated.
root@...SZ:~/eais-miniconda/eais/cpp# ls build/src/ CMakeFiles cmake_install.cmake libpre_post_proprocess.a Makefile pytorch_bert_perf pytorch_resnet50 pytorch_resnet50_cuda pytorch_resnet50_perf
-
-
-
Run the C++ sample program.
-
Run the following command to configure the path of the dynamic library on which the sample program depends.
export LD_LIBRARY_PATH=`pwd`/third_party/libtorch/lib:$LD_LIBRARY_PATH -
Run inference for the resnet50 model.
-
Run the following command to run inference for the resnet50 model. The CUDA call information is not included.
NoteIn this example, data preprocessing and post-processing are implemented on the CPU by using
opencvandtorchinterfaces. The code is in thesrcdirectory.LD_PRELOAD=libtorcheais.so \ build/src/pytorch_resnet50 \ -m ../data/models/resnet50.pt \ -l ../data/densenet_labels.txt \ ../data/imagesThe parameters are described as follows:
-
LD_PRELOAD: preloads the installedlibtorcheais.sodynamic library. -
build/src/pytorch_resnet50: the executable sample programpytorch_resnet50that is generated by compilation. -
../data/models/resnet50.pt: the resnet50 model file that is provided in the eais-miniconda package. -
../data/densenet_labels.txt: the file that stores image category names and is provided in the eais-miniconda package. -
../data/images/: the path of the sample images that are provided in the eais-miniconda package.
The execution result provides the image classification information. The following code shows a sample output.
image_id: 0, image_name: ../data/images/car.jpg, max_prob: 0.700754, max_label_id: 817, class_name: SPORTS CAR image_id: 1, image_name: ../data/images/mug.jpg, max_prob: 0.849474, max_label_id: 504, class_name: COFFEE MUG image_id: 2, image_name: ../data/images/vulture.jpeg, max_prob: 0.933375, max_label_id: 23, class_name: VULTURE -
-
Run the following command to run inference for the resnet50 model. The CUDA call information is included.
NoteIn this example, data preprocessing and post-processing are implemented by using a
cuda kernel. The code is in thesrcdirectory.LD_PRELOAD=libtorcheais.so \ build/src/pytorch_resnet50_cuda \ -m ../data/models/resnet50.pt \ -l ../data/densenet_labels.txt \ ../data/imagesThe parameters are described as follows:
-
LD_PRELOAD: preloads the installedlibtorcheais.sodynamic library. -
build/src/pytorch_resnet50_cuda: the executable sample programbuild/src/pytorch_resnet50_cudathat is generated by compilation. -
../data/models/resnet50.pt: the resnet50 model file that is provided in the eais-miniconda package. -
../data/densenet_labels.txt: the file that stores image category names and is provided in the eais-miniconda package. -
../data/images/: the path of the sample images that are provided in the eais-miniconda package.
-
-
-
Test the inference performance of the resnet50 and bert-base models.
-
Run the following command to test the inference performance of the resnet50 model on an EAIS instance.
LD_PRELOAD=libtorcheais.so \ build/src/pytorch_resnet50_perf \ -m ../data/models/resnet50.pt \ -d gpuThe execution result provides the inference latency data for the resnet50 model. The following code shows a sample output.
5Z:~/eais-miniconda/eais/cpp# LD_PRELOAD=libtorcheais.so \ > build/src/pytorch_resnet50_perf \ > -m ../data/models/resnet50.pt \ > -d gpu test device: gpu average forward time: 2.20047 ms -
Run the following command to test the inference performance of the bert-base model on an EAIS instance.
LD_PRELOAD=libtorcheais.so \ build/src/pytorch_bert_perf \ -m ../data/models/bert-base-uncased.pt \ -d gpuThe execution result provides the inference latency data for the bert-base model. A successful command returns the average forward time. The value
5.37935 msrepresents the inference time after EAIS acceleration.average forward time: 5.37935 ms
-
-
(Optional) Step 7: Stop the EAIS instance
You can stop the EAIS instance if you do not temporarily need its GPU capabilities.
This operation is supported only for EAIS instances created using the EAIS console or by calling the CreateEaisEi operation.
Log on to the EAIS console.
In the EAIS instance list, find the
eais_testinstance, and in its Operation column, click Stop.In the Stop Instance dialog box, click OK.
The EAIS instance list auto-refreshes every 15 seconds. A change in the instance status from In Use to Associated indicates that the EAIS instance has stopped.
(Optional) Step 8: Disassociate and release the EAIS instance
After inference is complete, you can disassociate the EAIS instance from the current ECS instance and associate it with another one. If you no longer need the EAIS instance, you must first disassociate it before you can release it. To disassociate and release an EAIS instance, perform the following steps:
Log on to the EAIS console.
Disassociate the EAIS instance from the ECS instance.
In the EAIS instance list, find the
eais_testinstance and click Disassociate in the Operation column.In the Disassociate from ECS Instance dialog box, click OK.
The EAIS instance list auto-refreshes every 15 seconds. When the instance status changes from Associated to Available, the EAIS instance is disassociated.
Release the disassociated EAIS instance.
In the list of EAIS instances, find the
eais_testinstance and click Release Resource in the Operation column.In the Release Resource dialog box, click OK.
The EAIS instance is deleted from the EAIS instance list.
References
-
You can also use a Python script to run PyTorch model inference with an EAIS instance. For more information, see Accelerate PyTorch model inference by using an EAIS instance (Python).
-
To get started quickly with EAIS, you can also run TensorFlow model inference on an EAIS instance. For more information, see Accelerate TensorFlow model inference by using an EAIS instance.
For more information about how to use an EAIS instance, see EAIS instances or Tutorials.

