After you attach an Elastic Accelerator Inference Service (EAIS) instance to an Elastic Compute Service (ECS) instance, you can remotely log on to the ECS instance and use the EAIS instance for AI inference. This topic describes how to use a C++ program to infer a PyTorch model using EAIS.
Prerequisites
An EAIS instance is attached to an ECS instance. For more information, see Associate with an ECS instance.
The attached ECS instance runs Ubuntu, CentOS, or Alibaba Cloud Linux. For more information about how to change the operating system of an ECS instance, see Change the operating system (system disk).
Background information
EAIS supports PyTorch model inference in different programming languages based on your business scenario. The following two methods are available:
Use a Python script to infer a PyTorch model. For more information, see Infer a PyTorch model using EAIS (Python).
Use a C++ program to infer a PyTorch model. EAIS supports calls from C++ programs that include libtorch and CUDA, including the CUDA API and CUDA kernels.
This topic describes how to use this method for inference. If you encounter performance or feature issues when you use EAIS for inference, contact EAIS technical support for a custom optimization solution.
Compared with GPU-accelerated instances (NVIDIA T4), EAIS significantly improves inference performance. The following table compares the inference performance of an EAIS instance (eais.ei-a6.2xlarge) with a GPU-accelerated instance (NVIDIA T4) when using a C++ program that depends on libtorch.
The data in this topic is for reference only. The actual data is subject to your inference results.
You can also refer to the C++ program code provided in the eais-miniconda package to test the inference performance of a GPU-accelerated instance (NVIDIA T4) and compare it with an EAIS instance.
Inference model | eais.ei-a6.2xlarge | GPU-accelerated instance (NVIDIA T4) | Performance improvement of EAIS over GPU-accelerated instance (NVIDIA T4) |
resnet50 | 2.20 ms | 6.24 ms | 2.84 times |
bert-base | 5.38 ms | 8.32 ms | 1.55 times |
Limits
EAIS supports the following software versions:
If your ECS instance runs CentOS 7.9, the default glibc and libstdc++ software environments do not meet the requirements. You must manually upgrade the glibc and libstdc++ versions. For more information about how to view and upgrade the software versions, see Related operations.
Software | Version |
glibc version | 2.23 or greater |
libstdc++ version | 3.4.25 or later |
Procedure
Deploy an inference program using EAIS
Develop and compile the code on your development machine. Then, copy the compiled program to the ECS instance to infer the PyTorch model using EAIS.
Remotely log on to the ECS instance.
Log on to the EAIS console.
In the top-left corner of the page, select the region where the instance is located.
In the instance list, click the ID of the ECS instance that is attached to the EAIS instance to go to the ECS console.
Remotely log on to the ECS instance.
For more information, see Connect to an ECS instance.
Install the eais-tool package and view information about the EAIS instance.
For more information, see eais-tool.
Install the eais-cuda package.
For more information, see eais-cuda.
Install the eais-torch package.
For more information, see eais-torch.
Deploy the application.
Copy the compiled C++ inference application, its dependent library files, and the PyTorch model files from your development machine to the ECS instance.
Infer the PyTorch model using EAIS.
For example, if the name of your C++ program for PyTorch model inference is my_program and you run the program using the following command:
export LD_LIBRARY_PATH=/your/lib/path:$LD_LIBRARY_PATH ./my_program my_model.ptTo infer your PyTorch model using EAIS, run your C++ program using one of the following commands:
Command 1:
export LD_LIBRARY_PATH=/your/lib/path:$LD_LIBRARY_PATH LD_PRELOAD=libtorcheais.so ./my_program my_model.ptCommand 2:
export LD_LIBRARY_PATH=/your/lib/path:$LD_LIBRARY_PATH export LD_PRELOAD=libtorcheais.so ./my_program my_model.pt
Compile and run an inference program on an ECS instance that is attached to an EAIS instance
You can write, compile, and run an inference program directly on the ECS instance that is attached to the EAIS instance.
Remotely log on to the ECS instance.
Log on to the EAIS console.
In the top-left corner of the page, select the region where the instance is located.
In the instance list, click the ID of the ECS instance that is attached to the EAIS instance to go to the ECS console.
Remotely log on to the ECS instance.
For more information, see Connect to an ECS instance.
Install the CUDA 11.X.X package.
Run the following command to download and install the CUDA package.
NoteThis section uses CUDA 11.7.0 as an example. The actual command varies based on the CUDA version that you install.
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 --toolkitRun the following command to set 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-tool package and view information about the EAIS instance.
For more information, see eais-tool.
Install the eais-cuda package.
For more information, see eais-cuda.
Install the eais-torch package.
For more information, see eais-torch.
Develop and compile the model inference code.
Run the following command to download and decompress the package that contains the C++ sample code.
wget https://aiacc-inference-public.oss-cn-beijing.aliyuncs.com/eais/packages/eais-miniconda.zip unzip eais-miniconda.zipDevelop and compile the model inference code.
You can refer to the sample code in the package to develop the model inference code.
The C++ sample code is saved in the eais-miniconda/eais/cpp/src folder.
If you want to run the inference program using EAIS, you do not need to modify the code. You can proceed to the next step.
NoteThe decompressed sample code does not depend on EAIS. The model inference code is written based on libtorch and CUDA. You can use the code directly or run the sample code on your GPU-accelerated instance.
Infer the PyTorch model using EAIS.
For example, if the name of your C++ program for PyTorch model inference is my_program and you run the program using the following command:
export LD_LIBRARY_PATH=/your/lib/path:$LD_LIBRARY_PATH ./my_program my_model.ptTo infer your PyTorch model using EAIS, run your C++ program using one of the following commands:
Command 1:
export LD_LIBRARY_PATH=/your/lib/path:$LD_LIBRARY_PATH LD_PRELOAD=libtorcheais.so ./my_program my_model.ptCommand 2:
export LD_LIBRARY_PATH=/your/lib/path:$LD_LIBRARY_PATH export LD_PRELOAD=libtorcheais.so ./my_program my_model.pt
Related operations
View and upgrade the glibc version
Remotely log on to the ECS instance.
Log on to the EAIS console.
In the top-left corner of the page, select the region where the instance is located.
In the instance list, click the ID of the ECS instance that is attached to the EAIS instance to go to the ECS console.
Remotely log on to the ECS instance.
For more information, see Connect to an ECS instance.
Run the following command to view the glibc version.
strings /lib64/libc.so.6 | grep GLIBC_The following command output is returned. Check whether the glibc version is 2.23 or later.

If the glibc version is earlier than 2.23, run the following command to upgrade the glibc version.
mkdir glibc pushd glibc wget https://ftp.gnu.org/gnu/glibc/glibc-2.23.tar.gz tar -xf glibc-2.23.tar.gz cd glibc-2.23/ mkdir build pushd build ../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin make -j`nproc` sudo make DESTDIR=/tmp/stage install sudo make install popd popd sudo rm -rf glibc
View and upgrade the libstdc++ version
Remotely log on to the ECS instance.
Log on to the EAIS console.
In the top-left corner of the page, select the region where the instance is located.
In the instance list, click the ID of the ECS instance that is attached to the EAIS instance to go to the ECS console.
Remotely log on to the ECS instance.
For more information, see Connect to an ECS instance.
Run the following command to view the libstdc++ version.
strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX_The following command output is returned. Check whether the libstdc++ version is 3.4.25 or later.

If the libstdc++ version is earlier than 3.4.25, run the following command to upgrade the libstdc++ version.
mkdir libstd pushd libstd wget http://ftp.de.debian.org/debian/pool/main/g/gcc-8/libstdc++6_8.3.0-6_amd64.deb ar -x libstdc++6_8.3.0-6_amd64.deb tar xf data.tar.xz rm /usr/lib64/libstdc++.so.6 cp usr/lib/x86_64-linux-gnu/* /usr/lib64/ popd sudo rm -rf libstd