目的
RISC-VのSBCであるVisionFive 2でTensorFlow LiteのGPU delegate(OpenCL
backend)を実行してみる。
VisionFive 2のGPUはImagination TechnologiesのIMG BXE-4-32 MC1。
OpenCL 3.0対応でVisionFive 2にライブラリも提供されている。
なので、TensorFlow
Lite GPU delegateをやってみる。
環境
- VisionFive 2 Debian Image 202302 Released
- OpenCV 4.7.0(build from source)
- TensorFlow Lite v2.12 C++ Library(build from source)
つまづきポイント
-
TensorFlow Lite C++ LibraryをCMakeでビルドする。
バグがありビルドできないためパッチをあてて対処。 -
userだと権限によりOpenCLが利用できない。
render groupに追加する必要あり。
構築
前回、前々回のブログと同様、今回はOpenCVを使ってUSBカメラのキャプチャした画像を物体検出してみる。
OpenCL delegateなのでTensorFlow Lite C++ Libraryを使う必要がある。
C++のサンプルを自分のリポジトリに用意。
OpenCVのビルド&インストール
前々回のブログと同じ手順。
OpenCLのテスト
userをrender groupに追加、一応、再起動してみる。
$ sudo adduser $USER render
clinfoをインストール、情報を確認。
$ clinfo
Number of platforms 1
Platform Name PowerVR
Platform Vendor Imagination Technologies
Platform Version OpenCL 3.0
Platform Profile EMBEDDED_PROFILE
Platform Extensions cl_khr_icd cl_khr_fp16 cl_img_spirv cles_khr_int64 cl_img_yuv_image cl_khr_device_uuid cl_khr_depth_images cl_khr_mipmap_image cl_khr_priority_hints cl_img_generate_mipmap cl_khr_3d_image_writes cl_img_cached_allocations cl_khr_mipmap_image_writes cl_khr_create_command_queue cl_khr_suggested_local_work_size cl_img_mem_properties cl_img_mem_properties_relax_alloc_requirements cl_khr_extended_versioning cl_khr_image2d_from_buffer cl_khr_byte_addressable_store cl_khr_local_int32_base_atomics cl_khr_global_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_global_int32_extended_atomics cl_khr_spir cl_khr_il_program cl_khr_egl_image cl_arm_import_memory cl_arm_import_memory_dma_buf cl_img_protected_content cl_img_semaphore cl_img_external_semaphore cl_img_external_semaphore_sync_fd cl_khr_semaphore cl_khr_external_semaphore cl_khr_external_semaphore_sync_fd
Platform Extensions with Version cl_khr_icd 0x400000 (1.0.0)
cl_khr_fp16 0x400000 (1.0.0)
cl_img_spirv 0x400000 (1.0.0)
cles_khr_int64 0x400000 (1.0.0)
cl_img_yuv_image 0x400000 (1.0.0)
cl_khr_device_uuid 0x400000 (1.0.0)
cl_khr_depth_images 0x400000 (1.0.0)
cl_khr_mipmap_image 0x400000 (1.0.0)
cl_khr_priority_hints 0x400000 (1.0.0)
cl_img_generate_mipmap 0x400000 (1.0.0)
cl_khr_3d_image_writes 0x400000 (1.0.0)
cl_img_cached_allocations 0x400000 (1.0.0)
cl_khr_mipmap_image_writes 0x400000 (1.0.0)
cl_khr_create_command_queue 0x400000 (1.0.0)
cl_khr_suggested_local_work_size 0x400000 (1.0.0)
cl_img_mem_properties 0x400000 (1.0.0)
cl_img_mem_properties_relax_alloc_requirements 0x400000 (1.0.0)
cl_khr_extended_versioning 0x400000 (1.0.0)
cl_khr_image2d_from_buffer 0x400000 (1.0.0)
cl_khr_byte_addressable_store 0x400000 (1.0.0)
cl_khr_local_int32_base_atomics 0x400000 (1.0.0)
cl_khr_global_int32_base_atomics 0x400000 (1.0.0)
cl_khr_local_int32_extended_atomics 0x400000 (1.0.0)
cl_khr_global_int32_extended_atomics 0x400000 (1.0.0)
cl_khr_spir 0x400000 (1.0.0)
cl_khr_il_program 0x400000 (1.0.0)
cl_khr_egl_image 0x400000 (1.0.0)
cl_arm_import_memory 0x400000 (1.0.0)
cl_arm_import_memory_dma_buf 0x400000 (1.0.0)
cl_img_protected_content 0x400000 (1.0.0)
cl_img_semaphore 0x400000 (1.0.0)
cl_img_external_semaphore 0x400000 (1.0.0)
cl_img_external_semaphore_sync_fd 0x400000 (1.0.0)
cl_khr_semaphore 0x400000 (1.0.0)
cl_khr_external_semaphore 0x400000 (1.0.0)
cl_khr_external_semaphore_sync_fd 0x400000 (1.0.0)
Platform Numeric Version 0xc00000 (3.0.0)
Platform Extensions function suffix IMG
Platform Host timer resolution 0ns
Platform Name PowerVR
Number of devices 1
Device Name PowerVR B-Series BXE-4-32
Device Vendor Imagination Technologies
Device Vendor ID 0x1010
Device Version OpenCL 3.0
Device UUID 33362035-3020-3534-2031-383200000000
Driver UUID 36323130-3836-3600-0000-000000000000
Valid Device LUID No
Device LUID 0000-000000000000
Device Node Mask 0
Device Numeric Version 0xc00000 (3.0.0)
Driver Version 1.17@6210866
Device OpenCL C Version OpenCL C 1.2
Device OpenCL C all versions OpenCL C 0x400000 (1.0.0)
OpenCL C 0x401000 (1.1.0)
OpenCL C 0x402000 (1.2.0)
OpenCL C 0xc00000 (3.0.0)
Device OpenCL C features __opencl_c_int64 0x400000 (1.0.0)
__opencl_c_pipes 0xc00000 (3.0.0)
__opencl_c_images 0x400000 (1.0.0)
__opencl_c_subgroups 0xc00000 (3.0.0)
__opencl_c_3d_image_writes 0x400000 (1.0.0)
__opencl_c_read_write_images 0x400000 (1.0.0)
__opencl_c_generic_address_space 0xc00000 (3.0.0)
__opencl_c_program_scope_global_variables 0xc00000 (3.0.0)
__opencl_c_work_group_collective_functions 0xc00000 (3.0.0)
Latest comfornace test passed v2021-10-04-00
Device Type GPU
Device Profile EMBEDDED_PROFILE
Device Available Yes
Compiler Available Yes
Linker Available Yes
Max compute units 1
Max clock frequency 594MHz
Device Partition (core)
Max number of sub-devices 1
Supported partition types None
Supported affinity domains (n/a)
Max work item dimensions 3
Max work item sizes 512x512x512
Max work group size 512
Preferred work group size multiple (device) 32
Preferred work group size multiple (kernel) 32
Max sub-groups per work group 512
Preferred / native vector sizes
char 16 / 1
short 8 / 1
int 4 / 1
long 2 / 1
half 0 / 0 (cl_khr_fp16)
float 4 / 1
double 0 / 0 (n/a)
Half-precision Floating-point support (cl_khr_fp16)
Denormals No
Infinity and NANs Yes
Round to nearest Yes
Round to zero No
Round to infinity No
IEEE754-2008 fused multiply-add Yes
Support is emulated in software No
Single-precision Floating-point support (core)
Denormals No
Infinity and NANs Yes
Round to nearest Yes
Round to zero No
Round to infinity No
IEEE754-2008 fused multiply-add Yes
Support is emulated in software No
Correctly-rounded divide and sqrt operations No
Double-precision Floating-point support (n/a)
Address bits 64, Little-Endian
Global memory size 4082995200 (3.803GiB)
Error Correction support No
Max memory allocation 1020748800 (973.5MiB)
Unified memory for Host and Device Yes
Shared Virtual Memory (SVM) capabilities (core)
Coarse-grained buffer sharing Yes
Fine-grained buffer sharing No
Fine-grained system sharing No
Atomics No
Minimum alignment for any data type 128 bytes
Alignment of base address 1024 bits (128 bytes)
Preferred alignment for atomics
SVM 0 bytes
Global 0 bytes
Local 0 bytes
Atomic memory capabilities relaxed, work-group scope
Atomic fence capabilities relaxed, acquire/release, work-group scope
Max size for global variable 16384 (16KiB)
Preferred total size of global vars 0
Global Memory cache type Read/Write
Global Memory cache size 16384 (16KiB)
Global Memory cache line size 64 bytes
Image support Yes
Max number of samplers per kernel 16
Max size for 1D images from buffer 16384 pixels
Max 1D or 2D image array size 2048 images
Base address alignment for 2D image buffers 64 bytes
Pitch alignment for 2D image buffers 64 pixels
Max 2D image size 16384x16384 pixels
Max 3D image size 16384x16384x2048 pixels
Max number of read image args 8
Max number of write image args 64
Max number of read/write image args 64
Pipe support Yes
Max number of pipe args 16
Max active pipe reservations 1
Max pipe packet size 1024
Local memory type Local
Local memory size 4096 (4KiB)
Max number of constant args 256
Max constant buffer size 1020748800 (973.5MiB)
Generic address space support Yes
Max size of kernel argument 1024
Queue properties (on host)
Out-of-order execution Yes
Profiling Yes
Device enqueue capabilities (n/a)
Queue properties (on device)
Out-of-order execution No
Profiling No
Preferred size 0
Max size 0
Max queues on device 0
Max events on device 0
Prefer user sync for interop Yes
Profiling timer resolution 1000ns
Execution capabilities
Run OpenCL kernels Yes
Run native kernels Yes
Non-uniform work-groups Yes
Work-group collective functions Yes
Sub-group independent forward progress No
IL version SPIR-V_1.2
ILs with version SPIR-V 0x402000 (1.2.0)
SPIR versions 1.2
printf() buffer size 65536 (64KiB)
Built-in kernels (n/a)
Built-in kernels with version (n/a)
Device Extensions cl_khr_icd cl_khr_fp16 cl_img_spirv cles_khr_int64 cl_img_yuv_image cl_khr_device_uuid cl_khr_depth_images cl_khr_mipmap_image cl_khr_priority_hints cl_img_generate_mipmap cl_khr_3d_image_writes cl_img_cached_allocations cl_khr_mipmap_image_writes cl_khr_create_command_queue cl_khr_suggested_local_work_size cl_img_mem_properties cl_img_mem_properties_relax_alloc_requirements cl_khr_extended_versioning cl_khr_image2d_from_buffer cl_khr_byte_addressable_store cl_khr_local_int32_base_atomics cl_khr_global_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_global_int32_extended_atomics cl_khr_spir cl_khr_il_program cl_khr_egl_image cl_arm_import_memory cl_arm_import_memory_dma_buf cl_img_protected_content cl_img_semaphore cl_img_external_semaphore cl_img_external_semaphore_sync_fd cl_khr_semaphore cl_khr_external_semaphore cl_khr_external_semaphore_sync_fd
Device Extensions with Version cl_khr_icd 0x400000 (1.0.0)
cl_khr_fp16 0x400000 (1.0.0)
cl_img_spirv 0x400000 (1.0.0)
cles_khr_int64 0x400000 (1.0.0)
cl_img_yuv_image 0x400000 (1.0.0)
cl_khr_device_uuid 0x400000 (1.0.0)
cl_khr_depth_images 0x400000 (1.0.0)
cl_khr_mipmap_image 0x400000 (1.0.0)
cl_khr_priority_hints 0x400000 (1.0.0)
cl_img_generate_mipmap 0x400000 (1.0.0)
cl_khr_3d_image_writes 0x400000 (1.0.0)
cl_img_cached_allocations 0x400000 (1.0.0)
cl_khr_mipmap_image_writes 0x400000 (1.0.0)
cl_khr_create_command_queue 0x400000 (1.0.0)
cl_khr_suggested_local_work_size 0x400000 (1.0.0)
cl_img_mem_properties 0x400000 (1.0.0)
cl_img_mem_properties_relax_alloc_requirements 0x400000 (1.0.0)
cl_khr_extended_versioning 0x400000 (1.0.0)
cl_khr_image2d_from_buffer 0x400000 (1.0.0)
cl_khr_byte_addressable_store 0x400000 (1.0.0)
cl_khr_local_int32_base_atomics 0x400000 (1.0.0)
cl_khr_global_int32_base_atomics 0x400000 (1.0.0)
cl_khr_local_int32_extended_atomics 0x400000 (1.0.0)
cl_khr_global_int32_extended_atomics 0x400000 (1.0.0)
cl_khr_spir 0x400000 (1.0.0)
cl_khr_il_program 0x400000 (1.0.0)
cl_khr_egl_image 0x400000 (1.0.0)
cl_arm_import_memory 0x400000 (1.0.0)
cl_arm_import_memory_dma_buf 0x400000 (1.0.0)
cl_img_protected_content 0x400000 (1.0.0)
cl_img_semaphore 0x400000 (1.0.0)
cl_img_external_semaphore 0x400000 (1.0.0)
cl_img_external_semaphore_sync_fd 0x400000 (1.0.0)
cl_khr_semaphore 0x400000 (1.0.0)
cl_khr_external_semaphore 0x400000 (1.0.0)
cl_khr_external_semaphore_sync_fd 0x400000 (1.0.0)
NULL platform behavior
clGetPlatformInfo(NULL, CL_PLATFORM_NAME, ...) PowerVR
clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, ...) Success [IMG]
clCreateContext(NULL, ...) [default] Success [IMG]
clCreateContextFromType(NULL, CL_DEVICE_TYPE_DEFAULT) Success (1)
Platform Name PowerVR
Device Name PowerVR B-Series BXE-4-32
clCreateContextFromType(NULL, CL_DEVICE_TYPE_CPU) No devices found in platform
Platform Name PowerVR
Device Name PowerVR B-Series BXE-4-32
clCreateContextFromType(NULL, CL_DEVICE_TYPE_ACCELERATOR) No devices found in platform
clCreateContextFromType(NULL, CL_DEVICE_TYPE_CUSTOM) Invalid device type for platform
clCreateContextFromType(NULL, CL_DEVICE_TYPE_ALL) Success (1)
Platform Name PowerVR
Device Name PowerVR B-Series BXE-4-32
ICD loader properties
ICD loader Name OpenCL ICD Loader
ICD loader Vendor OCL Icd free software
ICD loader Version 2.3.1
ICD loader Profile OpenCL 3.0
最初、clinfoで"Number of platforms
0"でなぜ...となり、フォーラムまで確認してしまった...
あとで権限が足りない!と気づいたのだった...(しかも自己解決)
TensorFlow Lite C++ を使ったサンプルをビルド
リポジトリclone、ビルドする。
ビルドするためsubmoduleにtensorflowのリポジトリを登録してある。
これにyoctoのレシピであるmeta-tensorflow-liteで用意したパッチをあてる。
$ sudo apt install libboost-dev $ cd ~ $ git clone https://github.com/NobuoTsukamoto/meta-tensorflow-lite.git $ git clone https://github.com/NobuoTsukamoto/tflite-cv-example.git $ cd tflite-cv-example $ git submodule update --init $ cd tensorflow $ git apply ~/meta-tensorflow-lite/recipes-framework/tensorflow-lite/files/001-v2.12-Fix-CMAKE_Build_Error.patch $ cd ../detection/cpp/ $ mkdir build $ cd build $ cmake .. -DENABEL_GPU_DELEGATE=ON $ make -j$(nproc)
動作確認
カメラを接続して実行。
モデルはEfficientDet-Lite0のFP32。
$ ./tflite_detection \
/home/user/models/efficientdet-lite0_fp32.tflite \
--label=/home/user/tflite-cv-example/detection/models/coco_labels.txt \
--thread=3
ようやく...😭
— nb.o (@Nextremer_nb_o) March 26, 2023
VisionFive 2でTensorFlow Lite GPU delegateができました💯🎉💯🎉👍✌️👍✌️
TensorFlow v2.12、EfficentDet-Lite0をOpenCLバックエンドで動かしてみました。
これでCPU(XNNPACK)、GPU、EdgeTPUが動きましたよ。 pic.twitter.com/EhT5iNvzb3
最後に
これでVisionFive 2でTensorFlow LiteのXNNPACK、EdgeTPU、GPUが動作した。
RISC-VのSBCでここまで動くと面白くなってくる。
