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

Shucai Xiao's avatar
Shucai Xiao committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
AMD MIGraphX is AMD's graph inference engine that accelerates machine learning model inference. AMD MIGraphX can be used by
installing binaries directly or building from source code.

In the following, instructions of how to build and install MIGraphX are described with Ubuntu as the OS
(Instructions of installation on other Linux OSes will come later). Note that all the following instructions assume 
ROCm has been installed successfully. ROCm installation instructions are explained in the [ROCm installation
guide](https://rocmdocs.amd.com/en/latest/Installation_Guide/Installation-Guide.html).

## Installing from binaries
With ROCm installed correctly, MIGraphX binaries can be installed on Ubuntu with the following command:
```
sudo apt update && sudo apt install -y migraphx
```
then the header files and libs are installed under `/opt/rocm-<version>`, where `<version>` is the ROCm version.

## Building from source

There are three ways to build the MIGraphX sources. 
* [Use the ROCm build tool](#use-the-rocm-build-tool-rbuild)
    
    This approach uses [rbuild](https://github.com/RadeonOpenCompute/rbuild) to install the prerequisites and
build the libs with just one command. 

* [Use cmake](#use-cmake-to-build-migraphx)
    
    This approach uses a script to install the prerequisites, then use cmake to build the source.
      
* [Use docker](#use-docker)
    
    This approach builds a docker image with all prerequisites installed, then build the MIGraphX sources inside a docker container. 

In the following, we will first list the prerequisites required to build MIGraphX source code, then describe 
each of the three approaches.

### List of prerequisites
The following is a list of prerequisites required to build MIGraphX source. 
Paul's avatar
Paul committed
39
40
41

* [ROCm cmake modules](https://github.com/RadeonOpenCompute/rocm-cmake) **required**
* [MIOpen](https://github.com/ROCmSoftwarePlatform/MIOpen) for running on the GPU
Shucai Xiao's avatar
Shucai Xiao committed
42
* [rocBLAS](https://github.com/ROCmSoftwarePlatform/rocBLAS) for running on the GPU
Paul's avatar
Paul committed
43
* [HIP](https://github.com/ROCm-Developer-Tools/HIP) for running on the GPU
44
* [Protobuf](https://github.com/google/protobuf) for reading [onnx](https://github.com/onnx/onnx) files
Paul's avatar
Paul committed
45
* [Half](http://half.sourceforge.net/) - IEEE 754-based half-precision floating point library
Paul's avatar
Paul committed
46
* [pybind11](https://pybind11.readthedocs.io/en/stable/) - for python bindings
Shucai Xiao's avatar
Shucai Xiao committed
47
48
* [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
Umang Yadav's avatar
Umang Yadav committed
49
* [SQLite3](https://www.sqlite.org/index.html) - to create database of kernels' tuning information or execute queries on existing database
Paul's avatar
Paul committed
50

Shucai Xiao's avatar
Shucai Xiao committed
51
#### Use the ROCm build tool [rbuild](https://github.com/RadeonOpenCompute/rbuild).
Paul's avatar
Paul committed
52

Shucai Xiao's avatar
Shucai Xiao committed
53
54
55
56
In this approach, we use the [rbuild](https://github.com/RadeonOpenCompute/rbuild) build tool to
build MIGraphX. The specific steps are as follows:

1) Install rocm-cmake, pip3, rocblas, and miopen-hip with the command
Paul's avatar
Paul committed
57
58

```
Shucai Xiao's avatar
Shucai Xiao committed
59
sudo apt update && sudo apt install -y rocm-cmake python3-pip rocblas miopen-hip
Paul's avatar
Paul committed
60
61
```

Shucai Xiao's avatar
Shucai Xiao committed
62
63
2) Install [rbuild](https://github.com/RadeonOpenCompute/rbuild) (sudo may be required here.)

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

Shucai Xiao's avatar
Shucai Xiao committed
68
3) Build MIGraphX source code
Paul's avatar
Paul committed
69

Shucai Xiao's avatar
Shucai Xiao committed
70
71
72
```
rbuild build -d depend -B build --cxx=/opt/rocm/llvm/bin/clang++
```
Paul's avatar
Paul committed
73

Shucai Xiao's avatar
Shucai Xiao committed
74
then all the prerequisites are in the folder `depend`, and MIGraphX is built in the `build` directory.
Paul's avatar
Paul committed
75

Shucai Xiao's avatar
Shucai Xiao committed
76
77
Note that for ROCm3.7 and later releases, Ubuntu 18.04 or later releases are needed. 
Upgrade to Ubuntu 18.04 is available at [Upgrade Ubuntu to 18.04](https://github.com/ROCmSoftwarePlatform/AMDMIGraphX/wiki/Upgrade-to-Ubuntu-18.04-for-ROCM3.7-or-later-releases)
Paul's avatar
Paul committed
78

Shucai Xiao's avatar
Shucai Xiao committed
79
80
81
82
83
84
85
Also note that you may meet the error of `rbuild: command not found`. It is because rbuild is installed 
at `$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.

#### Use cmake to build MIGraphX

If using this approach, we need to install the prerequisites, configure the cmake, and then build the source.
Paul's avatar
Paul committed
86

Shucai Xiao's avatar
Shucai Xiao committed
87
##### Installing the prerequisites
Paul's avatar
Paul committed
88

Shucai Xiao's avatar
Shucai Xiao committed
89
For convenience, the prerequisites can be built automatically with rbuild as:
Paul's avatar
Paul committed
90
91

```
Shucai Xiao's avatar
Shucai Xiao committed
92
rbuild build -d depend --cxx=/opt/rocm/llvm/bin/clang++
Paul's avatar
Paul committed
93
94
```

Shucai Xiao's avatar
Shucai Xiao committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
then all the prerequisites are in the folder `depend`, and they can be used in the `cmake` configuration
as `-DCMAKE_PREFIX_PATH=depend`.

If you have sudo access, as an alternative to the rbuild command, you can install the prerequisites just 
like in the dockerfile by calling `./tools/install_prereqs.sh`.

(Note that this script is for Ubuntu. 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 location at which the prerequisites are installed with `./tools/install_prereqs.sh $your_loc`.)

##### Building MIGraphX source and install libs

With the above prerequisites installed, we can build source as:

1) Go to the project folder and create a `build` directory:
Paul's avatar
Paul committed
110
111
112


```
Shucai Xiao's avatar
Shucai Xiao committed
113
114
mkdir build
cd build
Paul's avatar
Paul committed
115
116
```

Shucai Xiao's avatar
Shucai Xiao committed
117
2) Configure the cmake. If the prerequisites are installed at the default location `/usr/local`, the command is:
Paul's avatar
Paul committed
118

Shucai Xiao's avatar
Shucai Xiao committed
119
120
121
122
```
CXX=/opt/rocm/llvm/bin/clang++ cmake ..
```
Otherwise, you need to set `-DCMAKE_PREFIX_PATH=$your_loc` to configure the cmake. 
Paul's avatar
Paul committed
123

Shucai Xiao's avatar
Shucai Xiao committed
124
3) Build MIGraphX source code
Paul's avatar
Paul committed
125

Shucai Xiao's avatar
Shucai Xiao committed
126
127
128
```
make -j$(nproc)
```
Paul's avatar
Paul committed
129

Shucai Xiao's avatar
Shucai Xiao committed
130
Correctness can be verified as:
Paul's avatar
Paul committed
131

Shucai Xiao's avatar
Shucai Xiao committed
132
133
134
```
make -j$(nproc) check
```
Paul's avatar
Paul committed
135

Shucai Xiao's avatar
Shucai Xiao committed
136
MIGraphX libs can be installed as:
Paul's avatar
Paul committed
137

Shucai Xiao's avatar
Shucai Xiao committed
138
139
140
```
make install
```
Paul's avatar
Paul committed
141

Shucai Xiao's avatar
Shucai Xiao committed
142
#### Use docker
Paul's avatar
Paul committed
143

Shucai Xiao's avatar
Shucai Xiao committed
144
The easiest way to setup the development environment is to use docker. With the dockerfile, you can build a docker image as:
145

Shucai Xiao's avatar
Shucai Xiao committed
146
    docker build -t migraphx .
147

Shucai Xiao's avatar
Shucai Xiao committed
148
149
150
Then to enter the developement environment use `docker run`:

    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
151

Shucai Xiao's avatar
Shucai Xiao committed
152
153
154
155
In the docker container, all the required prerequisites are already installed, so users can just go to the folder 
`/code/AMDMIGraphX` and follow the steps in the above [Build MIGraphX source and install
libs](#building-migraphx-source-and-install-libs)
section to build MIGraphX source.
Paul's avatar
Paul committed
156

157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
### Using MIGraphX Python Module
To use MIGraphX's Python module, please either set `PYTHONPATH` or use `.deb` package as explained below:

- Setting `PYTHONPATH` :
```
export PYTHONPATH=/opt/rocm/lib:$PYTHONPATH
```
- Creating and installing the package:

To create deb package:
```
make package
```
This will provide the path of .deb package.

To install:
```
dpkg -i <path_to_deb_file>
```


Shucai Xiao's avatar
Shucai Xiao committed
178
179
180
181
182
183
184
185
### Calling MIGraphX APIs
To use MIGraphX's C/C++ API in your cmake project, we need to set `CMAKE_PREFIX_PATH` to the MIGraphX
installation location and then do 
```
find_package(migraphx)
target_link_libraries(myApp migraphx::c)
```
Where `myApp` is the cmake target in your project.
Paul's avatar
Paul committed
186
187


Shucai Xiao's avatar
Shucai Xiao committed
188
### Building the documentation
Paul's avatar
Paul committed
189
190
191
192
193

HTML and PDF documentation can be built using:

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

Paul's avatar
Paul committed
194
195
196
197
198
199
200
201
202
203
This will build a local searchable web site inside the doc/html folder.

Documentation is built using [Doxygen](http://www.stack.nl/~dimitri/doxygen/download.html), [Sphinx](http://www.sphinx-doc.org/en/stable/index.html), and [Breathe](https://breathe.readthedocs.io/en/latest/)

Requirements for both Sphinx and Breathe can be installed with:

`pip install -r doc/requirements.txt`

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

Paul's avatar
Paul committed
204
205
206
207
208
## Formatting the code

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

```
bpickrel's avatar
bpickrel committed
209
clang-format-10 -style=file -i <path-to-source-file>
Paul's avatar
Paul committed
210
211
212
213
214
215
216
```

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

```
./.githooks/install
```