Lite Transformer
This document describes how to run the Lite Transformer English-to-Chinese example on the NPU.
Refer to Model Zoo Download for the example.
Lite Transformer uses encoder and decoder sub-models.
Lite Transformer example directory structure:
$ tree ./
./
├── CMakeLists.txt
├── convert_model_decoder
├── convert_model_encoder
├── include
├── model
│ ├── bpe_order.txt
│ ├── cw_token_map_order.txt
│ ├── dict_order.txt
│ ├── lite_transformer_decoder_16_int16_a733.nb
│ ├── lite_transformer_encoder_16_int16_a733.nb
│ ├── position_embed.bin
│ └── token_embed.bin
├── src
└── README.md
Model Conversion
Enter the container development environment first. See Create and Start Container in the Model Zoo download page.
Select the Docker image that matches the NPU:
- A733: ubuntu-npu:v2.0.10.2
- T527: ubuntu-npu:v1.8.13
Download the floating-point ONNX models from the Allwinner netdisk:
- encoder: http://netstorage.allwinnertech.com:5000/sharing/48BkHmEFh
- decoder: http://netstorage.allwinnertech.com:5000/sharing/QHfBh3imH
- BPE / dictionary files: http://netstorage.allwinnertech.com:5000/sharing/ctkG9jW4t
Extract the BPE and dictionary files into the lite_transformer directory.
docker exec -it model-zoo /bin/bash
Convert encoder
cd /workspace/examples/lite_transformer/convert_model_encoder/
./convert_model_env.sh
./pegasus_import.sh lite_transformer_encoder_16
./pegasus_quantize.sh lite_transformer_encoder_16 int16 2
- A733
- T527
./pegasus_export_ovx_nbg.sh lite_transformer_encoder_16 int16 a733
./pegasus_export_ovx_nbg.sh lite_transformer_encoder_16 int16 t527
Convert decoder
cd /workspace/examples/lite_transformer/convert_model_decoder/
./convert_model_env.sh
./pegasus_import.sh lite_transformer_decoder_16
./pegasus_quantize.sh lite_transformer_decoder_16 int16 4
- A733
- T527
./pegasus_export_ovx_nbg.sh lite_transformer_decoder_16 int16 a733
./pegasus_export_ovx_nbg.sh lite_transformer_decoder_16 int16 t527
The exported models are stored in the ../model directory.
Build the Example
Then compile the example. Exit the container first, then run the commands below.
Configure the cross-compilation toolchain first.
Skip this step if you have already configured it in another example.
cd ../../../0-toolchains/
Download the toolchain from this link, put it in 0-toolchains/, then run:
tar -xvf gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu.tar.xz
cd ../examples/lite_transformer/
- A733
- T527
../build_linux.sh -t a733 -s debian11
../build_linux.sh -t t527 -s debian11
Model Deployment
After compilation, the example will be installed in the install directory. You can use scp to transfer it to the board.
Configure NPU Driver
You can skip this step if you have already configured NPU driver in other examples.
Transfer the driver library to the board's lib directory via scp.
- A733 corresponds to the common/npuruntime/lib_linux_aarch64/A733 directory
- T527 corresponds to the common/npuruntime/lib_linux_aarch64/T527 directory
Then execute the following command to export to environment variables.
echo 'export LD_LIBRARY_PATH=$HOME/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
Run Example
After configuring the driver, you can run the example.
For T527 platform, you need to first enable NPU by referring to the A5E's "Enable NPU on Board" documentation, then use the following command to grant the current user permission to use /dev/vipcore.
sudo chmod 777 /dev/vipcore
- A733
- T527
cd lite_transformer_demo_linux_a733/
chmod +x ./lite_transformer_demo_a733
./lite_transformer_demo_a733 -nb0 model/lite_transformer_encoder_16_int16_a733.nb -nb1 model/lite_transformer_decoder_16_int16_a733.nb -i "so big"
The running result is as follows:
$ ./lite_transformer_demo_a733 -nb0 model/lite_transformer_encoder_16_int16_a733.nb -nb1 model/lite_transformer_decoder_16_int16_a733.nb -i "so big"
encoder_path=model/lite_transformer_encoder_16_int16_a733.nb, decoder_path=model/lite_transformer_decoder_16_int16_a733.nb, input_strings=so big
VIPLite driver software version 2.0.3.2-AW-2024-08-30
nbg name=model/lite_transformer_encoder_16_int16_a733.nb, size: 3838272.
create network 0: 2533 us.
prepare network: 442 us.
nbg name=model/lite_transformer_decoder_16_int16_a733.nb, size: 19421952.
create network 1: 11027 us.
prepare network: 406 us.
input sentence:so big
output token: 2 83 139 676 84 2
output_strings: 如此巨大
inference time: 24.000 ms
destroy npu finished.
~NpuUint.
This performance data only calculates the time consumption of model inference. Unless otherwise specified, it does not include the time consumption of pre-processing and post-processing.
| SoC | NPU | Model | Input Resolution | Network Creation Time | Network Preparation Time | Single Frame Inference Time | Post-processing Time | Total Time | Frame Rate |
|---|---|---|---|---|---|---|---|---|---|
| Allwinner A733 | Vivante VIP9000 | lite-transformer | 16 tokens | 13.6 ms | 0.8 ms | 24.0 ms | 38.4 ms | 41.7 FPS |
cd lite_transformer_demo_linux_t527/
chmod +x ./lite_transformer_demo_t527
./lite_transformer_demo_t527 -nb0 model/lite_transformer_encoder_16_int16_t527.nb -nb1 model/lite_transformer_decoder_16_int16_t527.nb -i "so big"
The running result is as follows:
$ ./lite_transformer_demo_t527 -nb0 model/lite_transformer_encoder_16_int16_t527.nb -nb1 model/lite_transformer_decoder_16_int16_t527.nb -i "so big"
encoder_path=model/lite_transformer_encoder_16_int16_t527.nb, decoder_path=model/lite_transformer_decoder_16_int16_t527.nb, input_strings=so big
VIPLite driver software version 1.13.0.0-AW-2023-10-19
nbg name=model/lite_transformer_encoder_16_int16_t527.nb, size: 3755712.
create network 0: 5054 us.
prepare network: 836 us.
nbg name=model/lite_transformer_decoder_16_int16_t527.nb, size: 19121536.
create network 1: 23573 us.
prepare network: 2658 us.
input sentence:so big
output token: 2 83 139 676 84 2
output_strings: 如此巨大
inference time: 64.000 ms
destroy npu finished.
~NpuUint.
This performance data only calculates the time consumption of model inference. Unless otherwise specified, it does not include the time consumption of pre-processing and post-processing.
| SoC | NPU | Model | Input Resolution | Network Creation Time | Network Preparation Time | Single Frame Inference Time | Post-processing Time | Total Time | Frame Rate |
|---|---|---|---|---|---|---|---|---|---|
| Allwinner T527 | Vivante VIP9000 | lite-transformer | 16 tokens | 28.6 ms | 3.5 ms | 64.0 ms | 96.1 ms | 15.6 FPS |