Difference between revisions of "NPU/zh"

From FriendlyELEC WiKi
Jump to: navigation, search
(updated by API)
(updated by API)
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[NPU|English]]
 
[[NPU|English]]
  
 +
==Earlier version RKNPU2 SDK==
 +
Link to → [[NPU_SDK1.5.2/zh|v1.5.2]]
 
==How to test NPU==
 
==How to test NPU==
 
===OS===
 
===OS===
 
Tested on the following OS:
 
Tested on the following OS:
 
====Debian11 (bullseye)====
 
====Debian11 (bullseye)====
* rk3588-sd-debian-bullseye-desktop-6.1-arm64-20240116.img.gz
+
* rk3588-sd-debian-bullseye-desktop-6.1-arm64-20241011.img.gz
* rk3568-sd-debian-bullseye-desktop-6.1-arm64-20231113.img.gz
+
 
====Ubuntu20 (focal)====
 
====Ubuntu20 (focal)====
* rk3588-sd-ubuntu-focal-desktop-6.1-arm64-20240116.img.gz
+
* rk3588-sd-ubuntu-focal-desktop-6.1-arm64-20241011.img.gz
 
===install rknpu===
 
===install rknpu===
 
<syntaxhighlight lang="text">
 
<syntaxhighlight lang="text">
 +
cd ~
 
export GIT_SSL_NO_VERIFY=1
 
export GIT_SSL_NO_VERIFY=1
git clone https://github.com/rockchip-linux/rknpu2.git
+
git clone https://github.com/airockchip/rknn-toolkit2.git
cd rknpu2
+
cd rknn-toolkit2/rknpu2
git checkout tags/v1.5.2 -b v1.5.2
+
sudo cp ./runtime/Linux/librknn_api/aarch64/* /usr/lib
sudo cp ./runtime/RK3588/Linux/librknn_api/aarch64/* /usr/lib
+
sudo cp ./runtime/Linux/rknn_server/aarch64/usr/bin/* /usr/bin/
sudo cp ./runtime/RK3588/Linux/rknn_server/aarch64/usr/bin/* /usr/bin/
+
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
===check rknn version===
 
===check rknn version===
 
<syntaxhighlight lang="text">
 
<syntaxhighlight lang="text">
strings /usr/bin/rknn_server |grep 'build@'
+
$ strings /usr/bin/rknn_server |grep 'build@'
strings /usr/lib/librknnrt.so |grep 'librknnrt version:'
+
2.2.0 (3e10681 build@2024-09-06T10:26:49)
 +
rknn_server version: 2.2.0 (3e10681 build@2024-09-06T10:26:49)
 +
$ strings /usr/lib/librknnrt.so |grep 'librknnrt version:'
 +
librknnrt version: 2.2.0 (c195366594@2024-09-14T12:18:56)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 29: Line 33:
 
sudo apt-get update
 
sudo apt-get update
 
sudo apt-get install -y gcc g++ make cmake
 
sudo apt-get install -y gcc g++ make cmake
cd examples/rknn_yolov5_demo
 
./build-linux_RK3588.sh
 
  
 +
# fix broken link
 +
cd ~/rknn-toolkit2/rknpu2/examples/3rdparty/mpp/Linux/aarch64
 +
rm -f librockchip_mpp.so librockchip_mpp.so.1
 +
ln -s librockchip_mpp.so.0 librockchip_mpp.so
 +
ln -s librockchip_mpp.so.0 librockchip_mpp.so.1
 +
 +
cd ~/rknn-toolkit2/rknpu2/examples/rknn_yolov5_demo
 +
chmod +x ./build-linux.sh
 +
sudo ln -s /usr/bin/gcc /usr/bin/aarch64-gcc
 +
sudo ln -s /usr/bin/g++ /usr/bin/aarch64-g++
 +
export GCC_COMPILER=aarch64
 +
./build-linux.sh -t rk3588 -a aarch64 -b Release
 
cd install/rknn_yolov5_demo_Linux
 
cd install/rknn_yolov5_demo_Linux
 
./rknn_yolov5_demo model/RK3588/yolov5s-640-640.rknn model/bus.jpg
 
./rknn_yolov5_demo model/RK3588/yolov5s-640-640.rknn model/bus.jpg
Line 46: Line 60:
 
sudo apt-get update
 
sudo apt-get update
 
sudo apt-get install -y python3-dev python3-numpy python3-opencv python3-pip
 
sudo apt-get install -y python3-dev python3-numpy python3-opencv python3-pip
cd ~
+
cd ~/rknn-toolkit2
git clone https://github.com/rockchip-linux/rknn-toolkit2.git
+
pip3 install ./rknn-toolkit-lite2/packages/rknn_toolkit_lite2-2.2.0-cp39-cp39-linux_aarch64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple/
(cd rknn-toolkit2 && git checkout tags/v1.5.2 -b v1.5.2)
+
pip3 install ./rknn-toolkit2/rknn_toolkit_lite2/packages/rknn_toolkit_lite2-1.5.2-cp39-cp39-linux_aarch64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple/
+
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
====run python demo====
 
====run python demo====
 
<syntaxhighlight lang="text">
 
<syntaxhighlight lang="text">
$ cd rknn-toolkit2/rknn_toolkit_lite2/examples/inference_with_lite/
+
$ cd ~/rknn-toolkit2/rknn-toolkit-lite2/examples/resnet18/
 
$ python3 test.py
 
$ python3 test.py
 +
W rknn-toolkit-lite2 version: 2.2.0
 
--> Load RKNN model
 
--> Load RKNN model
 
done
 
done
 
--> Init runtime environment
 
--> Init runtime environment
I RKNN: [08:06:49.416] RKNN Runtime Information: librknnrt version: 1.5.2 (c6b7b351a@2023-08-23T15:28:22)
+
I RKNN: [06:50:39.456] RKNN Runtime Information, librknnrt version: 2.2.0 (c195366594@2024-09-14T12:18:56)
I RKNN: [08:06:49.416] RKNN Driver Information: version: 0.9.2
+
I RKNN: [06:50:39.456] RKNN Driver Information, version: 0.9.3
I RKNN: [08:06:49.416] RKNN Model Information: version: 6, toolkit version: 1.5.2-source_code(compiler version: 1.5.2 (71720f3fc@2023-08-
+
I RKNN: [06:50:39.456] RKNN Model Information, version: 6, toolkit version: 2.2.0(compiler version: 2.2.0 (c195366594@2024-09-14T12:24:14)), target: RKNPU v2, target platform: rk3588, framework name: PyTorch, framework layout: NCHW, model inference type: static_shape
21T09:35:42)), target: RKNPU v2, target platform: rk3588, framework name: PyTorch, framework layout: NCHW, model inference type: static_s
+
W RKNN: [06:50:39.474] query RKNN_QUERY_INPUT_DYNAMIC_RANGE error, rknn model is static shape type, please export rknn with dynamic_shapes
hape
+
W Query dynamic range failed. Ret code: RKNN_ERR_MODEL_INVALID. (If it is a static shape RKNN model, please ignore the above warning message.)
 
done
 
done
 
--> Running model
 
--> Running model
 
resnet18
 
resnet18
 
-----TOP 5-----
 
-----TOP 5-----
[812]: 0.9996760487556458
+
[812] score:0.999680 class:"space shuttle"
[404]: 0.00024927023332566023
+
[404] score:0.000249 class:"airliner"
[657]: 1.449744013370946e-05
+
[657] score:0.000013 class:"missile"
[466 833]: 9.023910024552606e-06
+
[466] score:0.000009 class:"bullet train, bullet"
[466 833]: 9.023910024552606e-06
+
[895] score:0.000008 class:"warplane, military plane"
  
 
done
 
done
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===install rknn_toolkit on ubuntu===
+
===install rknn_toolkit on ubuntu-focal===
 
====build python3.9 from source====
 
====build python3.9 from source====
 
<syntaxhighlight lang="text">
 
<syntaxhighlight lang="text">
Line 94: Line 107:
 
====install rknn_toolkit====
 
====install rknn_toolkit====
 
<syntaxhighlight lang="text">
 
<syntaxhighlight lang="text">
pip install --upgrade pip
+
python3.9 -m pip install --upgrade pip
pip install opencv-python
+
python3.9 -m pip install opencv-python
 +
 
 
cd ~
 
cd ~
git clone https://github.com/rockchip-linux/rknn-toolkit2.git
+
[ -d rknn-toolkit2 ] || git clone https://github.com/airockchip/rknn-toolkit2.git \
(cd rknn-toolkit2 && git checkout tags/v1.5.2 -b v1.5.2)
+
    --depth 1 -b master
/usr/local/bin/python3.9 -m pip install ./rknn-toolkit2/rknn_toolkit_lite2/packages/rknn_toolkit_lite2-1.5.2-cp39-cp39-linux_aarch64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple/
+
cd rknn-toolkit2
 +
python3.9 -m pip install \
 +
    ./rknn-toolkit-lite2/packages/rknn_toolkit_lite2-2.2.0-cp39-cp39-linux_aarch64.whl \
 +
    -i https://pypi.tuna.tsinghua.edu.cn/simple/
 
</syntaxhighlight>
 
</syntaxhighlight>
  
====run python demo====
+
====run python demo on ubuntu-focal====
 
<syntaxhighlight lang="text">
 
<syntaxhighlight lang="text">
$ cd rknn-toolkit2/rknn_toolkit_lite2/examples/inference_with_lite/
+
cd ~/rknn-toolkit2/rknn-toolkit-lite2/examples/resnet18/
$ python3 test.py
+
python3.9 test.py
 +
</syntaxhighlight>
 +
output:
 +
<syntaxhighlight lang="text">
 +
W rknn-toolkit-lite2 version: 2.2.0
 
--> Load RKNN model
 
--> Load RKNN model
 
done
 
done
 
--> Init runtime environment
 
--> Init runtime environment
I RKNN: [08:41:08.078] RKNN Runtime Information: librknnrt version: 1.5.2 (c6b7b351a@2023-08-23T15:28:22)
+
I RKNN: [09:57:36.101] RKNN Runtime Information, librknnrt version: 2.2.0 (c195366594@2024-09-14T12:18:56)
I RKNN: [08:41:08.078] RKNN Driver Information: version: 0.9.2
+
I RKNN: [09:57:36.101] RKNN Driver Information, version: 0.9.3
I RKNN: [08:41:08.080] RKNN Model Information: version: 6, toolkit version: 1.5.2-source_code(compiler version: 1.5.2 (71720f3fc@
+
I RKNN: [09:57:36.101] RKNN Model Information, version: 6, toolkit version: 2.2.0(compiler version: 2.2.0 (c195366594@2024-09-14T12:24:14)), target: RKNPU v2, target platform: rk3588, framework name: PyTorch, framework layout: NCHW, model inference type: static_shape
2023-08-21T09:35:42)), target: RKNPU v2, target platform: rk3588, framework name: PyTorch, framework layout: NCHW, model inferenc
+
W RKNN: [09:57:36.117] query RKNN_QUERY_INPUT_DYNAMIC_RANGE error, rknn model is static shape type, please export rknn with dynamic_shapes
e type: static_shape
+
W Query dynamic range failed. Ret code: RKNN_ERR_MODEL_INVALID. (If it is a static shape RKNN model, please ignore the above warning message.)
 
done
 
done
 
--> Running model
 
--> Running model
 
resnet18
 
resnet18
 
-----TOP 5-----
 
-----TOP 5-----
[812]: 0.9996760487556458
+
[812] score:0.999680 class:"space shuttle"
[404]: 0.00024927023332566023
+
[404] score:0.000249 class:"airliner"
[657]: 1.449744013370946e-05
+
[657] score:0.000013 class:"missile"
[466 833]: 9.023910024552606e-06
+
[466] score:0.000009 class:"bullet train, bullet"
[466 833]: 9.023910024552606e-06
+
[895] score:0.000008 class:"warplane, military plane"
  
 
done
 
done
Line 128: Line 149:
 
==Doc==
 
==Doc==
 
https://github.com/rockchip-linux/rknpu2/tree/master/doc
 
https://github.com/rockchip-linux/rknpu2/tree/master/doc
 +
==Other==
 +
===查看NPU占有率===
 +
<syntaxhighlight lang="text">
 +
cat /sys/kernel/debug/rknpu/load
 +
</syntaxhighlight>
 +
===设置NPU频率===
 +
<syntaxhighlight lang="text">
 +
echo userspace > /sys/class/devfreq/fdab0000.npu/governor
 +
echo 800000000 > /sys/class/devfreq/fdab0000.npu/min_freq
 +
echo 1000000000 > /sys/class/devfreq/fdab0000.npu/max_freq
 +
</syntaxhighlight>
 +
===查看NPU频率===
 +
<syntaxhighlight lang="text">
 +
cat /sys/class/devfreq/fdab0000.npu/cur_freq
 +
</syntaxhighlight>

Latest revision as of 10:00, 8 November 2024

English

1 Earlier version RKNPU2 SDK

Link to → v1.5.2

2 How to test NPU

2.1 OS

Tested on the following OS:

2.1.1 Debian11 (bullseye)

  • rk3588-sd-debian-bullseye-desktop-6.1-arm64-20241011.img.gz

2.1.2 Ubuntu20 (focal)

  • rk3588-sd-ubuntu-focal-desktop-6.1-arm64-20241011.img.gz

2.2 install rknpu

cd ~
export GIT_SSL_NO_VERIFY=1
git clone https://github.com/airockchip/rknn-toolkit2.git
cd rknn-toolkit2/rknpu2
sudo cp ./runtime/Linux/librknn_api/aarch64/* /usr/lib
sudo cp ./runtime/Linux/rknn_server/aarch64/usr/bin/* /usr/bin/

2.3 check rknn version

$ strings /usr/bin/rknn_server |grep 'build@'
2.2.0 (3e10681 build@2024-09-06T10:26:49)
rknn_server version: 2.2.0 (3e10681 build@2024-09-06T10:26:49)
$ strings /usr/lib/librknnrt.so |grep 'librknnrt version:'
librknnrt version: 2.2.0 (c195366594@2024-09-14T12:18:56)

2.4 run rknn_yolov5_demo

sudo apt-get update
sudo apt-get install -y gcc g++ make cmake
 
# fix broken link
cd ~/rknn-toolkit2/rknpu2/examples/3rdparty/mpp/Linux/aarch64
rm -f librockchip_mpp.so librockchip_mpp.so.1
ln -s librockchip_mpp.so.0 librockchip_mpp.so
ln -s librockchip_mpp.so.0 librockchip_mpp.so.1
 
cd ~/rknn-toolkit2/rknpu2/examples/rknn_yolov5_demo
chmod +x ./build-linux.sh
sudo ln -s /usr/bin/gcc /usr/bin/aarch64-gcc
sudo ln -s /usr/bin/g++ /usr/bin/aarch64-g++
export GCC_COMPILER=aarch64
./build-linux.sh -t rk3588 -a aarch64 -b Release
cd install/rknn_yolov5_demo_Linux
./rknn_yolov5_demo model/RK3588/yolov5s-640-640.rknn model/bus.jpg

Transfer the generated out.jpg to PC to view the result:

scp out.jpg xxx@YourIP:/tmp/

Rknn yolov5 demo out.jpg

2.5 install rknn_toolkit on debian11

2.5.1 install rknn_toolkit

sudo apt-get update
sudo apt-get install -y python3-dev python3-numpy python3-opencv python3-pip
cd ~/rknn-toolkit2
pip3 install ./rknn-toolkit-lite2/packages/rknn_toolkit_lite2-2.2.0-cp39-cp39-linux_aarch64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple/

2.5.2 run python demo

$ cd ~/rknn-toolkit2/rknn-toolkit-lite2/examples/resnet18/
$ python3 test.py
W rknn-toolkit-lite2 version: 2.2.0
--> Load RKNN model
done
--> Init runtime environment
I RKNN: [06:50:39.456] RKNN Runtime Information, librknnrt version: 2.2.0 (c195366594@2024-09-14T12:18:56)
I RKNN: [06:50:39.456] RKNN Driver Information, version: 0.9.3
I RKNN: [06:50:39.456] RKNN Model Information, version: 6, toolkit version: 2.2.0(compiler version: 2.2.0 (c195366594@2024-09-14T12:24:14)), target: RKNPU v2, target platform: rk3588, framework name: PyTorch, framework layout: NCHW, model inference type: static_shape
W RKNN: [06:50:39.474] query RKNN_QUERY_INPUT_DYNAMIC_RANGE error, rknn model is static shape type, please export rknn with dynamic_shapes
W Query dynamic range failed. Ret code: RKNN_ERR_MODEL_INVALID. (If it is a static shape RKNN model, please ignore the above warning message.)
done
--> Running model
resnet18
-----TOP 5-----
[812] score:0.999680 class:"space shuttle"
[404] score:0.000249 class:"airliner"
[657] score:0.000013 class:"missile"
[466] score:0.000009 class:"bullet train, bullet"
[895] score:0.000008 class:"warplane, military plane"
 
done

2.6 install rknn_toolkit on ubuntu-focal

2.6.1 build python3.9 from source

sudo apt install build-essential libssl-dev libffi-dev software-properties-common \
    libbz2-dev libncurses-dev libncursesw5-dev libgdbm-dev liblzma-dev libsqlite3-dev \
    tk-dev libgdbm-compat-dev libreadline-dev
 
wget https://www.python.org/ftp/python/3.9.16/Python-3.9.16.tar.xz
tar -xvf Python-3.9.16.tar.xz
cd Python-3.9.16/
./configure --enable-optimizations
make -j$(nproc)
sudo make install

2.6.2 install rknn_toolkit

python3.9 -m pip install --upgrade pip
python3.9 -m pip install opencv-python
 
cd ~
[ -d rknn-toolkit2 ] || git clone https://github.com/airockchip/rknn-toolkit2.git \
    --depth 1 -b master
cd rknn-toolkit2
python3.9 -m pip install \
    ./rknn-toolkit-lite2/packages/rknn_toolkit_lite2-2.2.0-cp39-cp39-linux_aarch64.whl \
    -i https://pypi.tuna.tsinghua.edu.cn/simple/

2.6.3 run python demo on ubuntu-focal

cd ~/rknn-toolkit2/rknn-toolkit-lite2/examples/resnet18/
python3.9 test.py

output:

W rknn-toolkit-lite2 version: 2.2.0
--> Load RKNN model
done
--> Init runtime environment
I RKNN: [09:57:36.101] RKNN Runtime Information, librknnrt version: 2.2.0 (c195366594@2024-09-14T12:18:56)
I RKNN: [09:57:36.101] RKNN Driver Information, version: 0.9.3
I RKNN: [09:57:36.101] RKNN Model Information, version: 6, toolkit version: 2.2.0(compiler version: 2.2.0 (c195366594@2024-09-14T12:24:14)), target: RKNPU v2, target platform: rk3588, framework name: PyTorch, framework layout: NCHW, model inference type: static_shape
W RKNN: [09:57:36.117] query RKNN_QUERY_INPUT_DYNAMIC_RANGE error, rknn model is static shape type, please export rknn with dynamic_shapes
W Query dynamic range failed. Ret code: RKNN_ERR_MODEL_INVALID. (If it is a static shape RKNN model, please ignore the above warning message.)
done
--> Running model
resnet18
-----TOP 5-----
[812] score:0.999680 class:"space shuttle"
[404] score:0.000249 class:"airliner"
[657] score:0.000013 class:"missile"
[466] score:0.000009 class:"bullet train, bullet"
[895] score:0.000008 class:"warplane, military plane"
 
done

3 Doc

https://github.com/rockchip-linux/rknpu2/tree/master/doc

4 Other

4.1 查看NPU占有率

cat /sys/kernel/debug/rknpu/load

4.2 设置NPU频率

echo userspace > /sys/class/devfreq/fdab0000.npu/governor
echo 800000000 > /sys/class/devfreq/fdab0000.npu/min_freq
echo 1000000000 > /sys/class/devfreq/fdab0000.npu/max_freq

4.3 查看NPU频率

cat /sys/class/devfreq/fdab0000.npu/cur_freq