README.md 7.17 KB
Newer Older
Paul's avatar
Paul committed
1
# AMD MIGraphX
Paul's avatar
Paul committed
2

3
4
5
AMD MIGraphX is AMD's graph inference engine, which accelerates machine learning model inference.
To use MIGraphX, you can install the binaries or build from source code. Refer to the following sections
for Ubuntu installation instructions (we'll provide instructions for other Linux distributions in the future).
Shucai Xiao's avatar
Shucai Xiao committed
6

7
8
9
10
```note
You must [install ROCm](https://rocm.docs.amd.com/en/latest/deploy/linux/quick_start.html) before
installing MIGraphX.
```
Shucai Xiao's avatar
Shucai Xiao committed
11
12

## Installing from binaries
13
14
15
16

Install binaries using:

```bash
Shucai Xiao's avatar
Shucai Xiao committed
17
18
sudo apt update && sudo apt install -y migraphx
```
19
20
21

Header files and libraries are installed under `/opt/rocm-<version>`, where `<version>` is the ROCm
version.
Shucai Xiao's avatar
Shucai Xiao committed
22
23
24

## Building from source

25
You have three options for building from source:
Shucai Xiao's avatar
Shucai Xiao committed
26

27
28
29
* [ROCm build tool](#use-the-rocm-build-tool-rbuild): Uses
  [rbuild](https://github.com/RadeonOpenCompute/rbuild) to install prerequisites, then you can build
  the libraries with a single command.
Shucai Xiao's avatar
Shucai Xiao committed
30

31
32
* [CMake](#use-cmake-to-build-migraphx): Uses a script to install prerequisites, then you can use
  CMake to build the source.
Shucai Xiao's avatar
Shucai Xiao committed
33

34
35
* [Docker](#use-docker): Builds a Docker image with all prerequisites installed, then you can build the
  MIGraphX sources inside a Docker container.
Paul's avatar
Paul committed
36

37
38
39
40
41
### Build prerequisites

The following is a list of prerequisites for building MIGraphX.

* [ROCm CMake modules](https://github.com/RadeonOpenCompute/rocm-cmake) **required**
Paul's avatar
Paul committed
42
* [MIOpen](https://github.com/ROCmSoftwarePlatform/MIOpen) for running on the GPU
Shucai Xiao's avatar
Shucai Xiao committed
43
* [rocBLAS](https://github.com/ROCmSoftwarePlatform/rocBLAS) for running on the GPU
Paul's avatar
Paul committed
44
* [HIP](https://github.com/ROCm-Developer-Tools/HIP) for running on the GPU
45
46
47
48
49
50
51
* [Protobuf](https://github.com/google/protobuf) for reading [onnx](https://github.com/onnx/onnx)
  files
* [Half](http://half.sourceforge.net/), an IEEE 754-based half-precision floating point library
* [pybind11](https://pybind11.readthedocs.io/en/stable/) for python bindings
* [JSON](https://github.com/nlohmann/json) for model serialization to json string format
* [MessagePack](https://msgpack.org/index.html) for model serialization to binary format
* [SQLite3](https://www.sqlite.org/index.html) to create database of kernels' tuning information or run queries on existing database
Paul's avatar
Paul committed
52

53
### Use the ROCm build tool [rbuild](https://github.com/RadeonOpenCompute/rbuild).
Paul's avatar
Paul committed
54

55
1. Install `rocm-cmake`, `pip3`, `rocblas`, and `miopen-hip`:
Shucai Xiao's avatar
Shucai Xiao committed
56

57
58
59
    ```bash
    sudo apt install -y rocm-cmake python3-pip rocblas miopen-hip
    ```
Paul's avatar
Paul committed
60

61
2. Install [rbuild](https://github.com/RadeonOpenCompute/rbuild) (sudo may be required):
Paul's avatar
Paul committed
62

63
64
65
    ```bash
    pip3 install https://github.com/RadeonOpenCompute/rbuild/archive/master.tar.gz
    ```
Shucai Xiao's avatar
Shucai Xiao committed
66

67
3. Build MIGraphX source code:
Paul's avatar
Paul committed
68

69
    ```bash
70
    rbuild build -d depend -B build -DGPU_TARGETS=$(/opt/rocm/bin/rocminfo | grep -o -m1 'gfx.*')
71
    ```
Paul's avatar
Paul committed
72

73
74
75
76
77
78
Once completed, all prerequisites are in the `depend` folder and MIGraphX is in the `build` directory.

```note
If you get an `rbuild: command not found` error, it's because `rbuild` is installed in `$HOME/.local/bin`,
which is not in `PATH`. You can either export PATH as `export PATH=$HOME/.local/bin:$PATH` to add
the folder to `PATH`, or add the option `--prefix /usr/local` in the pip3 command when installing `rbuild`.
Shucai Xiao's avatar
Shucai Xiao committed
79
```
Paul's avatar
Paul committed
80

81
### Use CMake to build MIGraphX
Paul's avatar
Paul committed
82

83
1. Install the prerequisites:
Shucai Xiao's avatar
Shucai Xiao committed
84

85
86
87
    ```bash
    rbuild prepare -d depend
    ```
Shucai Xiao's avatar
Shucai Xiao committed
88

89
90
    This puts all the prerequisites are in `depend` the folder. They can be used in the `cmake`
    configuration as `-DCMAKE_PREFIX_PATH=depend`.
Paul's avatar
Paul committed
91

92
93
    If you have sudo access, as an alternative to the `rbuild` command, you can install the prerequisites
    in the same way as a Dockerfile, by calling `./tools/install_prereqs.sh`.
Paul's avatar
Paul committed
94

95
96
97
    By default, all prerequisites are installed at the default location (`/usr/local`) and are accessible by all
    users. For the default location, `sudo` is required to run the script. You can also specify a different
    location using `./tools/install_prereqs.sh $custom_location`.
Paul's avatar
Paul committed
98

99
2. Go to the project folder and create a `build` directory:
Paul's avatar
Paul committed
100

101
102
103
104
    ```bash
    mkdir build
    cd build
    ```
Shucai Xiao's avatar
Shucai Xiao committed
105

106
3. Configure CMake. If the prerequisites are installed at the default location `/usr/local`, use:
Shucai Xiao's avatar
Shucai Xiao committed
107

108
    ```bash
109
    CXX=/opt/rocm/llvm/bin/clang++ cmake .. -DGPU_TARGETS=$(/opt/rocm/bin/rocminfo | grep -o -m1 'gfx.*')
110
    ```
Shucai Xiao's avatar
Shucai Xiao committed
111

112
    Otherwise, you need to set `-DCMAKE_PREFIX_PATH=$your_loc` to configure CMake.
Shucai Xiao's avatar
Shucai Xiao committed
113

114
4. Build MIGraphX source code:
Shucai Xiao's avatar
Shucai Xiao committed
115

116
117
118
    ```cpp
    make -j$(nproc)
    ```
Paul's avatar
Paul committed
119

120
    You can verify this using:
Paul's avatar
Paul committed
121

122
123
124
    ```cpp
    make -j$(nproc) check
    ```
Paul's avatar
Paul committed
125

126
5. Install MIGraphX libraries:
Paul's avatar
Paul committed
127

128
129
130
    ```cpp
    make install
    ```
Paul's avatar
Paul committed
131

132
### Use Docker
Paul's avatar
Paul committed
133

134
The easiest way to set up the development environment is to use Docker.
Paul's avatar
Paul committed
135

136
1. With the Dockerfile, build a Docker image:
Paul's avatar
Paul committed
137

138
139
140
    ```bash
        docker build -t migraphx .
    ```
Paul's avatar
Paul committed
141

142
2. Enter the development environment using `docker run`:
Paul's avatar
Paul committed
143

144
145
146
    ```bash
        docker run --device='/dev/kfd' --device='/dev/dri' -v=`pwd`:/code/AMDMIGraphX -w /code/AMDMIGraphX --group-add video -it migraphx
    ```
Paul's avatar
Paul committed
147

148
149
150
3. In the Docker container, all required prerequisites are already installed, so you can go to the folder
    `/code/AMDMIGraphX` and follow the steps (starting from 2) in the
    [Use CMake to build MIGraphX](#use-cmake-to-build-migraphx).
Paul's avatar
Paul committed
151

152
## Using the MIGraphX Python module
153

154
To use MIGraphX's Python module, you can set `PYTHONPATH` or use the `.deb` package:
155

156
* Setting `PYTHONPATH`:
Shucai Xiao's avatar
Shucai Xiao committed
157

158
159
160
    ```bash
    export PYTHONPATH=/opt/rocm/lib:$PYTHONPATH
    ```
Paul's avatar
Paul committed
161

162
* Creating the `deb` package:
Paul's avatar
Paul committed
163

164
165
166
    ```bash
    make package
    ```
167

168
    This provides the path for .deb package.
169

170
    To install:
171

172
173
174
    ```bash
    dpkg -i <path_to_deb_file>
    ```
175

176
177
178
179
180
181
## Calling MIGraphX APIs

To use MIGraphX's C/C++ API in your CMake project, you must set `CMAKE_PREFIX_PATH` to the
MIGraphX installation location and run:

```bash
Shucai Xiao's avatar
Shucai Xiao committed
182
183
184
find_package(migraphx)
target_link_libraries(myApp migraphx::c)
```
185
186

Where `myApp` is the CMake target in your project.
Paul's avatar
Paul committed
187

Paul Fultz II's avatar
Paul Fultz II committed
188
189
## Building for development

190
Using `rbuild`, you can install the dependencies for development with:
Paul Fultz II's avatar
Paul Fultz II committed
191

192
```bash
Paul Fultz II's avatar
Paul Fultz II committed
193
194
195
rbuild develop
```

196
197
198
This installs development dependencies in the `deps` directory and configures `cmake` to use those
dependencies in the `build` directory. You can change these directories by passing the `--deps-dir` and
`--build-dir` flags to the `rbuild` command:
Paul Fultz II's avatar
Paul Fultz II committed
199

200
```bash
Paul Fultz II's avatar
Paul Fultz II committed
201
202
rbuild develop --build-dir build_rocm_55 --deps-dir /home/user/deps_dir
```
Paul's avatar
Paul committed
203

Paul Fultz II's avatar
Paul Fultz II committed
204
## Building the documentation
Paul's avatar
Paul committed
205
206
207
208
209

HTML and PDF documentation can be built using:

`cmake --build . --config Release --target doc` **OR** `make doc`

Chris Austen's avatar
Chris Austen committed
210
This will build a local searchable web site inside the docs/html folder.
Paul's avatar
Paul committed
211

Chris Austen's avatar
Chris Austen committed
212
Documentation is built using [Doxygen](http://www.stack.nl/~dimitri/doxygen/download.html) and [rocm-docs-core](https://github.com/RadeonOpenCompute/rocm-docs-core)
Paul's avatar
Paul committed
213

Chris Austen's avatar
Chris Austen committed
214
Run the steps below to build documentation locally.
Paul's avatar
Paul committed
215

Chris Austen's avatar
Chris Austen committed
216
217
218
219
220
221
222
```
cd docs

pip3 install -r .sphinx/requirements.txt

python3 -m sphinx -T -E -b html -d _build/doctrees -D language=en . _build/html
```
Paul's avatar
Paul committed
223
224
225

Depending on your setup `sudo` may be required for the pip install.

Paul's avatar
Paul committed
226
227
228
229
## Formatting the code

All the code is formatted using clang-format. To format a file, use:

230
```clang
bpickrel's avatar
bpickrel committed
231
clang-format-10 -style=file -i <path-to-source-file>
Paul's avatar
Paul committed
232
233
234
235
```

Also, githooks can be installed to format the code per-commit:

236
```bash
Paul's avatar
Paul committed
237
238
./.githooks/install
```