Skip to content

Commit fb83afb

Browse files
authored
Merge pull request #57 from Ar-Ray-code/feat/migraphx
add migraphx support
2 parents 0e2b5cd + b7842e4 commit fb83afb

File tree

14 files changed

+256
-21
lines changed

14 files changed

+256
-21
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515

1616
| Base | ROS2 C++ |
1717
| --------------- | -------- |
18-
| TensorRT (CUDA) ||
19-
| OpenVINO ||
20-
| ONNX Runtime ||
21-
| TFLite ||
18+
| TensorRT ||
19+
| OpenVINO ||
20+
| ONNRuntime CUDA ||
21+
| ONNRuntime MIGraphX ||
22+
| TFLite ||
2223

2324

2425
## Installation & Demo (C++)
@@ -45,7 +46,6 @@ Check [this URL](./yolox_ros_cpp/README.md).
4546

4647
<br>
4748

48-
##
4949

5050
## Reference
5151

yolox_ros_cpp/MIGraphX.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# MIGraphX + ONNX Runtime (ROCm) build notes
2+
3+
This workspace uses ONNX Runtime built with the MIGraphX execution provider.
4+
ROCm EP is not present in `/opt/onnxruntime-migraphx`, so MIGraphX is used as the GPU backend.
5+
6+
## Key findings
7+
8+
- `libyolox_cpp.so` links against `/opt/onnxruntime-migraphx/lib/libonnxruntime.so.1`.
9+
- That onnxruntime build exposes `MIGraphXExecutionProvider` (not `ROCMExecutionProvider`).
10+
- MIGraphX tries to save compiled models; if a cache path is not set, it can fail with:
11+
- `migraphx_save: ... Failure opening file: ""/....mxr`
12+
- To avoid this, ensure `ORT_MIGRAPHX_MODEL_CACHE_PATH` (and/or `ORT_MIGRAPHX_CACHE_PATH`) is set.
13+
14+
## Prerequisites (Ubuntu 24.04 + ROCm)
15+
16+
### Install Radeon driver + ROCm
17+
18+
If you already installed `amdgpu-install_7.1.x`, install the ROCm usecase:
19+
20+
```bash
21+
sudo amdgpu-install --list-usecase
22+
sudo amdgpu-install -y --usecase=graphics,rocm
23+
sudo reboot
24+
```
25+
26+
After reboot, allow GPU compute access and reboot again:
27+
28+
```bash
29+
sudo usermod -a -G render,video $USER
30+
sudo reboot
31+
```
32+
33+
Verify ROCm is visible:
34+
35+
```bash
36+
/opt/rocm/bin/rocminfo | less
37+
hipcc --version
38+
```
39+
40+
### Install MIGraphX packages
41+
42+
```bash
43+
sudo apt update
44+
sudo apt install -y migraphx migraphx-dev half
45+
dpkg -l | egrep 'migraphx|half'
46+
```
47+
48+
Check the MIGraphX install path (used as `--migraphx_home` below):
49+
50+
```bash
51+
dpkg -L migraphx-dev | head -n 50
52+
```
53+
54+
## Build and install ONNX Runtime (MIGraphX EP)
55+
56+
### Build
57+
58+
ROCm EP was removed in onnxruntime 1.23+, so use MIGraphX EP.
59+
60+
```bash
61+
git clone --recursive https://github.com/microsoft/onnxruntime
62+
cd onnxruntime
63+
./build.sh --config Release --build_shared_lib --parallel \
64+
--use_migraphx --migraphx_home /opt/rocm \
65+
--update --build --skip_tests
66+
```
67+
68+
### Install
69+
70+
```bash
71+
cd build/Linux/Release
72+
sudo cmake --install . --prefix /opt/onnxruntime-migraphx
73+
echo /opt/onnxruntime-migraphx/lib | sudo tee /etc/ld.so.conf.d/onnxruntime.conf
74+
sudo ldconfig
75+
ldconfig -p | grep onnxruntime
76+
```
77+
78+
## Build (colcon)
79+
80+
Example build command (matches local setup with `/opt/onnxruntime-migraphx`):
81+
82+
```
83+
colcon build --symlink-install \
84+
--cmake-args \
85+
-DCMAKE_BUILD_TYPE=Release \
86+
-DYOLOX_USE_ONNXRUNTIME=ON \
87+
-DONNXRUNTIME_MIGRAPHX=ON \
88+
-DCMAKE_CXX_FLAGS="-I/opt/onnxruntime-migraphx/include" \
89+
-DCMAKE_EXE_LINKER_FLAGS="-L/opt/onnxruntime-migraphx/lib -lonnxruntime" \
90+
-DCMAKE_SHARED_LINKER_FLAGS="-L/opt/onnxruntime-migraphx/lib -lonnxruntime"
91+
```
92+
93+
## Runtime cache path
94+
95+
Set a writable cache directory before running:
96+
97+
```
98+
export ORT_MIGRAPHX_MODEL_CACHE_PATH=/tmp/onnxruntime-migraphx-cache
99+
export ORT_MIGRAPHX_CACHE_PATH=/tmp/onnxruntime-migraphx-cache
100+
```
101+
102+
## Run example
103+
104+
```
105+
source install/setup.bash
106+
ros2 run yolox_ros_cpp yolox_ros_cpp_node --ros-args \
107+
-p model_path:=./src/YOLOX-ROS/weights/onnx/yolox_tiny.onnx \
108+
-p model_type:=onnxruntime \
109+
-p use_gpu:=true
110+
```

yolox_ros_cpp/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ ros2 launch yolox_ros_cpp yolox_tflite.launch.py
232232
- `class_labels_path`: ""
233233
- `num_classes`: 80
234234
- `model_version`: 0.1.1rc0
235-
- `onnxruntime_use_cuda`: true
235+
- `onnxruntime_use_gpu`: true
236236
- `onnxruntime_use_parallel`: false
237237
- `onnxruntime_device_id`: 0
238238
- `onnxruntime_inter_op_num_threads`: 1
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
version: '3.4'
2+
services:
3+
yolox_ros_onnxruntime_migraphx:
4+
container_name: yolox_onnxruntime_migraphx
5+
build:
6+
context: .
7+
args:
8+
- BASE_TAB=latest
9+
network_mode: host
10+
devices:
11+
- "/dev/kfd:/dev/kfd"
12+
- "/dev/dri:/dev/dri"
13+
# - "/dev/video0:/dev/video0"
14+
15+
environment:
16+
- DISPLAY=$DISPLAY
17+
volumes:
18+
- $HOME/ros2_ws:/root/ros2_ws
19+
- /tmp/.X11-unix:/tmp/.X11-unix
20+
working_dir: /root/ros2_ws
21+
tty: true
22+
command: bash
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
ARG BASE_TAG=latest
2+
FROM rocm/dev-ubuntu-24.04:${BASE_TAG}
3+
4+
ENV TZ=Asia/Tokyo
5+
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
6+
ENV DEBIAN_FRONTEND=noninteractive
7+
8+
RUN apt update && apt install -y locales git cmake wget curl gnupg2 lsb-release python3-numpy python3-setuptools python3-wheel && \
9+
locale-gen en_US en_US.UTF-8 && \
10+
update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
11+
ENV LANG=en_US.UTF-8
12+
13+
14+
RUN apt update && apt install -y build-essential ca-certificates python3-dev \
15+
migraphx migraphx-dev half && \
16+
rm -rf /var/lib/apt/lists/*
17+
18+
RUN git clone --recursive https://github.com/microsoft/onnxruntime /tmp/onnxruntime && \
19+
cd /tmp/onnxruntime && \
20+
./build.sh --config Release --build_shared_lib --parallel \
21+
--use_migraphx --migraphx_home /opt/rocm \
22+
--update --build --skip_tests --allow_running_as_root && \
23+
cd build/Linux/Release && \
24+
cmake --install . --prefix /opt/onnxruntime-migraphx && \
25+
echo /opt/onnxruntime-migraphx/lib > /etc/ld.so.conf.d/onnxruntime.conf && \
26+
ldconfig && \
27+
rm -rf /tmp/onnxruntime
28+
29+
RUN curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg && \
30+
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/ros2.list > /dev/null && \
31+
sed -i -e 's/ubuntu .* main/ubuntu noble main/g' /etc/apt/sources.list.d/ros2.list && \
32+
apt update && \
33+
apt install -y ros-dev-tools \
34+
ros-jazzy-cv-bridge \
35+
ros-jazzy-generate-parameter-library \
36+
ros-jazzy-parameter-traits \
37+
ros-jazzy-ros-base \
38+
ros-jazzy-rqt-image-view \
39+
ros-jazzy-usb-cam \
40+
ros-jazzy-vision-msgs && \
41+
apt -y clean && \
42+
rm -rf /var/lib/apt/lists/*
43+
44+
WORKDIR /workspace
45+
COPY ./ros_entrypoint.sh /ros_entrypoint.sh
46+
RUN echo "source /ros_entrypoint.sh" >> /root/.bashrc
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
source /opt/ros/jazzy/setup.bash

yolox_ros_cpp/docker/onnxruntime/dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ RUN git clone --depth 1 --recursive https://github.com/microsoft/onnxruntime -b
1818
cd onnxruntime && \
1919
./build.sh --cudnn_home /usr/lib/x86_64-linux-gnu/ \
2020
--cuda_home /usr/local/cuda \
21-
--use_cuda \
21+
--use_gpu \
2222
--config RelWithDebInfo \
2323
--build_shared_lib \
2424
--allow_running_as_root \

yolox_ros_cpp/yolox_cpp/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ ament_auto_find_build_dependencies()
1717
option(YOLOX_USE_OPENVINO "Use OpenVINO" OFF)
1818
option(YOLOX_USE_TENSORRT "Use TensorRT" OFF)
1919
option(YOLOX_USE_ONNXRUNTIME "Use ONNXRuntime" OFF)
20+
option(ONNXRUNTIME_CUDA "Use ONNXRuntime CUDA Execution Provider" OFF)
21+
option(ONNXRUNTIME_MIGRAPHX "Use ONNXRuntime MIGraphX Execution Provider" OFF)
2022
option(YOLOX_USE_TFLITE "Use tflite" OFF)
2123
option(JETSON "Use Jetson" OFF)
2224

@@ -33,6 +35,8 @@ endif()
3335
set(ENABLE_OPENVINO OFF)
3436
set(ENABLE_TENSORRT OFF)
3537
set(ENABLE_ONNXRUNTIME OFF)
38+
set(ENABLE_ONNXRUNTIME_CUDA OFF)
39+
set(ENABLE_ONNXRUNTIME_MIGRAPHX OFF)
3640
set(ENABLE_TFLITE OFF)
3741

3842
if(YOLOX_USE_OPENVINO)
@@ -74,7 +78,18 @@ endif()
7478
# onnxruntime
7579
if(YOLOX_USE_ONNXRUNTIME)
7680
find_library(ONNXRUNTIME NAMES onnxruntime)
81+
if(ONNXRUNTIME_CUDA AND ONNXRUNTIME_MIGRAPHX)
82+
message(FATAL_ERROR "ONNXRUNTIME_CUDA and ONNXRUNTIME_MIGRAPHX cannot both be ON")
83+
endif()
7784
set(ENABLE_ONNXRUNTIME ON)
85+
if(ONNXRUNTIME_CUDA)
86+
set(ENABLE_ONNXRUNTIME_CUDA ON)
87+
set(TARGET_DEFINITIONS ${TARGET_DEFINITIONS} ONNXRUNTIME_CUDA)
88+
endif()
89+
if(ONNXRUNTIME_MIGRAPHX)
90+
set(ENABLE_ONNXRUNTIME_MIGRAPHX ON)
91+
set(TARGET_DEFINITIONS ${TARGET_DEFINITIONS} ONNXRUNTIME_MIGRAPHX)
92+
endif()
7893
set(TARGET_SRC ${TARGET_SRC} src/yolox_onnxruntime.cpp)
7994
set(TARGET_LIBS ${TARGET_LIBS} onnxruntime)
8095
endif()
@@ -88,6 +103,7 @@ ament_auto_add_library(yolox_cpp SHARED ${TARGET_SRC})
88103
ament_target_dependencies(yolox_cpp ${TARGET_DPENDENCIES})
89104
ament_export_dependencies(${TARGET_DPENDENCIES})
90105
target_link_libraries(yolox_cpp ${TARGET_LIBS})
106+
target_compile_definitions(yolox_cpp PUBLIC ${TARGET_DEFINITIONS})
91107

92108
if (YOLOX_USE_TFLITE)
93109
target_include_directories(yolox_cpp PUBLIC ${TFLITE_INCLUDES})

yolox_ros_cpp/yolox_cpp/include/yolox_cpp/config.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#cmakedefine ENABLE_OPENVINO
55
#cmakedefine ENABLE_TENSORRT
66
#cmakedefine ENABLE_ONNXRUNTIME
7+
#cmakedefine ENABLE_ONNXRUNTIME_CUDA
8+
#cmakedefine ENABLE_ONNXRUNTIME_MIGRAPHX
79
#cmakedefine ENABLE_TFLITE
810

911
#endif // _YOLOX_CPP_CONFIG_H_

yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_onnxruntime.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#ifndef _YOLOX_CPP_YOLOX_ONNX_HPP
22
#define _YOLOX_CPP_YOLOX_ONNX_HPP
33

4+
#include <algorithm>
5+
#include <cstdlib>
6+
#include <filesystem>
7+
#include <iostream>
48
#include <iterator>
59
#include <memory>
10+
#include <opencv2/opencv.hpp>
11+
#include <stdexcept>
612
#include <string>
713
#include <vector>
8-
#include <iostream>
9-
#include <opencv2/opencv.hpp>
1014

1115
#include <onnxruntime/onnxruntime_cxx_api.h>
1216

@@ -18,15 +22,15 @@ namespace yolox_cpp{
1822
public:
1923
YoloXONNXRuntime(const file_name_t &path_to_model,
2024
int intra_op_num_threads, int inter_op_num_threads=1,
21-
bool use_cuda=true, int device_id=0, bool use_parallel=false,
25+
bool use_gpu=true, int device_id=0, bool use_parallel=false,
2226
float nms_th=0.45, float conf_th=0.3, const std::string &model_version="0.1.1rc0",
2327
int num_classes=80, bool p6=false);
2428
std::vector<Object> inference(const cv::Mat& frame) override;
2529

2630
private:
2731
int intra_op_num_threads_ = 1;
2832
int inter_op_num_threads_ = 1;
29-
bool use_cuda_ = true;
33+
bool use_gpu_ = true;
3034
int device_id_ = 0;
3135
bool use_parallel_ = false;
3236

0 commit comments

Comments
 (0)