荔枝派LicheePi 4A 是基于 Lichee Module 4A 核心板的 高性能 RISC-V Linux 开发板,以 TH1520 为主控核心(4xC910@1.85G, RV64GCV,4TOPS@int8 NPU, 50GFLOP GPU),板载最大 16GB 64bit LPDDR4X,128GB eMMC,支持 HDMI+MIPI 双4K 显示输出,支持 4K 摄像头接入,双千兆网口(其中一个支持POE供电)和 4 个 USB3.0 接口,多种音频输入输出(由专用 C906 核心处理)。
荔枝派支持的AI应用较多,比如yolox、llama.cpp、llama2.c、Stable Diffusion等 ,具体见手册:
典型应用 - Sipeed Wiki
比如yolox案例
这个需要npu支持,在算能云上没有测试通过。
获取 YOLOX 模型
YOLOX 是一个类 YOLO 的目标检测模型,有相当优异的性能表现。
可以直接下载 github 上的源码和模型
git clone https://github.com/Megvii-BaseDetection/YOLOX.git
cd YOLOX/demo/ONNXRuntime
wget https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_s.onnx
修改源码
本教程将使用 HHB-onnxruntime 执行模型,因此切换到。在源码中的 onnxruntime 示例目录,修改文件 demo/ONNXRuntime/onnx_inference.py 的开头新增两行代码
#!/usr/bin/env python3
# Copyright (c) Megvii, Inc. and its affiliates.+import sys
+sys.path.insert(0, "../../")
+
import argparse
import os
也就是
import sys
sys.path.insert(0, "../../")
代码中使用 sys.path.insert 指定搜索路径,以此免去从源码中安装 YOLOX 的安装包的操作。
安装依赖包
RISC-V 体系结构的 python 生态还有欠缺,未来完善之后,YOLOX 中依赖的包可以通过 requirements.txt 文件直接安装。
本教程中的 YOLOX 示例依赖了较多的 python 包,下载预编译好的 python 包
git clone -b python3.11 https://github.com/zhangwm-pt/prebuilt_whl.git
cd prebuilt_whl
可以按照以下顺序,手工处理。
pip install numpy-1.25.0-cp311-cp311-linux_riscv64.whl
pip install opencv_python-4.5.4+4cd224d-cp311-cp311-linux_riscv64.whl
pip install kiwisolver-1.4.4-cp311-cp311-linux_riscv64.whl
pip install Pillow-9.5.0-cp311-cp311-linux_riscv64.whl
pip install matplotlib-3.7.2.dev0+gb3bd929cf0.d20230630-cp311-cp311-linux_riscv64.whl
pip install pycocotools-2.0.6-cp311-cp311-linux_riscv64.whl
pip3 install loguru-0.7.0-py3-none-any.whl
pip3 install torch-2.0.0a0+gitc263bd4-cp311-cp311-linux_riscv64.whl
pip3 install MarkupSafe-2.1.3-cp311-cp311-linux_riscv64.whl
pip3 install torchvision-0.15.1a0-cp311-cp311-linux_riscv64.whl
pip3 install psutil-5.9.5-cp311-abi3-linux_riscv64.whl
pip3 install tqdm-4.65.0-py3-none-any.whl
pip3 install tabulate-0.9.0-py3-none-any.whl
安装过程中会涉及到其他纯 python 依赖包,pip 会自动从官方源下载。
安装 HHB-onnxruntime
HHB-onnxuruntime 是移植了 SHL 后端(execution providers),让 onnxruntime 能复用到 SHL 中针对玄铁 CPU 的高性能优化代码。
wget https://github.com/zhangwm-pt/onnxruntime/releases/download/riscv_whl/onnxruntime-1.14.1-cp311-cp311-linux_riscv64.whl
pip install onnxruntime-1.14.1-cp311-cp311-linux_riscv64.whl
执行
在示例目录中执行 onnx_inference.py 示例
python3 onnx_inference.py -m yolox_s.onnx -i soccer.jpg -o outdir -s 0.3 --input_shape 640,640
python3 onnx_inference.py -m yolox_s.onnx -i soccer.jpg -o outdir -s 0.3 --input_shape640,640
llama2.c案例
这个案例可以在算能云上运行。算能云上速度还是挺快的:achieved tok/s: 20.831494
项目链接
我们可以根据项目链接文档中的步骤来运行一个 Llama 2 的小模型。
首先 clone 该项目,并切换到改项目目录下:
git clone https://github.com/karpathy/llama2.c.git
然后下载 model.bin 文件,并放到指定目录下:
wget https://karpathy.ai/llama2c/model.bin -P out
然后编译并运行:
gcc -O3 -o run run.c -lm
./run out/model.bin
可以得到如下输出:
<s>Once upon a time, there was a little girl named Lily. She loved to play with her dolls and teddy bea
rs. One day, she saw her friend Lucy playing with her favorite doll.
Lily: "Lucy, can I play with you?"
Lucy: "Sure, but can you be careful with my doll?"
Lily: "Sure, I promise I won't break her."
Lucy: "That's very original. I really love it."
Lily smiled and showed her doll to her mom. Her mom said, "That's nice, Lily. Let's put your doll on
the shelf and play with her some more."
From that day on, Lily and her doll played together every day. They were the best of friends and didn
't break anyone's possession.
<s>Once upon a time, there was a little boy named Timmy. Timmy loved to eat sandwiches. One day, Timmy'
s mom made him a sandwich for lunch. It was so yummy and tasty! But then, something unexpected happen
ed. Timmy's little sister spilled some juice on his sandwich. Tim
achieved tok/s: 30.955260
也可以修改一些编译参数来加速:
gcc -Ofast -fopenmp run.c -lm -o run
OMP_NUM_THEADS=4 ./run out/model.bin # 输出:
<s>Once upon a time, there was a boy named Tom. He liked to run fast. One day, he saw a big tree. Underthe tree, he found a hole. Tom was curious. He looked inside and saw something shiny.
Tom looked closer, and it was not a shiny thing. It was a key! He was very surprised. He took the keyand ran home. He showed his mom the key.
Tom's mom said the key was for a sneeze. The sneeze was a sneeze. Tom's mom put the key next to her p
ocket. Tom said, "Now you can have a sneeze too!"
Tom put his nose under his ears. He went home and took a nap. When he woke up, he had a small sneeze
from the key. It was not food or water. Tom was happy and laughed.
<s>Once upon a time, there was a bunny. The bunny was very restless and wanted to explore, so he hoppedoff his little house.
The bunny hopped and hopped until he saw a shiny silver carrot. He was so excited to eat it, he
achieved tok/s: 52.043098