build.md 11.1 KB
Newer Older
WRH's avatar
WRH committed
1
2
## Build MMCV from source

3
### Build mmcv
WRH's avatar
WRH committed
4

5
Before installing mmcv, make sure that PyTorch has been successfully installed following the [PyTorch official installation guide](https://pytorch.org/get-started/locally/#start-locally). This can be verified using the following command
WRH's avatar
WRH committed
6
7

```bash
8
python -c 'import torch;print(torch.__version__)'
WRH's avatar
WRH committed
9
10
```

11
If version information is output, then PyTorch is installed.
12

13
14
15
16
```{note}
If you would like to use `opencv-python-headless` instead of `opencv-python`,
e.g., in a minimum container environment or servers without GUI,
you can first install it before installing MMCV to skip the installation of `opencv-python`.
17
18
```

19
#### Build on Linux
WRH's avatar
WRH committed
20

21
1. Clone the repo
WRH's avatar
WRH committed
22

23
24
25
26
   ```bash
   git clone https://github.com/open-mmlab/mmcv.git
   cd mmcv
   ```
WRH's avatar
WRH committed
27

28
2. Install `ninja` and `psutil` to speed up the compilation
WRH's avatar
WRH committed
29

30
31
32
   ```bash
   pip install -r requirements/optional.txt
   ```
WRH's avatar
WRH committed
33

34
3. Check the nvcc version (requires 9.2+. Skip if no GPU available.)
WRH's avatar
WRH committed
35

36
37
38
   ```bash
   nvcc --version
   ```
WRH's avatar
WRH committed
39

40
   If the above command outputs the following message, it means that the nvcc setting is OK, otherwise you need to set CUDA_HOME.
WRH's avatar
WRH committed
41

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
   ```
   nvcc: NVIDIA (R) Cuda compiler driver
   Copyright (c) 2005-2020 NVIDIA Corporation
   Built on Mon_Nov_30_19:08:53_PST_2020
   Cuda compilation tools, release 11.2, V11.2.67
   Build cuda_11.2.r11.2/compiler.29373293_0
   ```

   :::{note}
   If you want to support ROCm, you can refer to [AMD ROCm](https://rocmdocs.amd.com/en/latest/Installation_Guide/Installation-Guide.html) to install ROCm.
   :::

4. Check the gcc version (requires 5.4+)

   ```bash
   gcc --version
   ```

5. Start building (takes 10+ min)

   ```bash
   pip install -e . -v
   ```

6. Validate the installation

   ```bash
   python .dev_scripts/check_installation.py
   ```

   If no error is reported by the above command, the installation is successful. If there is an error reported, please check [Frequently Asked Questions](../faq.md) to see if there is already a solution.

   If no solution is found, please feel free to open an [issue](https://github.com/open-mmlab/mmcv/issues).

#### Build on macOS
WRH's avatar
WRH committed
77

Zaida Zhou's avatar
Zaida Zhou committed
78
```{note}
Xin Li's avatar
Xin Li committed
79
If you are using a mac with apple silicon chip, install the PyTorch 1.13+, otherwise you will encounter the problem in [issues#2218](https://github.com/open-mmlab/mmcv/issues/2218).
Zaida Zhou's avatar
Zaida Zhou committed
80
```
81

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
1. Clone the repo

   ```bash
   git clone https://github.com/open-mmlab/mmcv.git
   cd mmcv
   ```

2. Install `ninja` and `psutil` to speed up the compilation

   ```bash
   pip install -r requirements/optional.txt
   ```

3. Start building

   ```bash
   MMCV_WITH_OPS=1 pip install -e .
   ```

4. Validate the installation

   ```bash
   python .dev_scripts/check_installation.py
   ```

   If no error is reported by the above command, the installation is successful. If there is an error reported, please check [Frequently Asked Questions](../faq.md) to see if there is already a solution.

   If no solution is found, please feel free to open an [issue](https://github.com/open-mmlab/mmcv/issues).

#### Build on Windows
WRH's avatar
WRH committed
112
113
114
115

Building MMCV on Windows is a bit more complicated than that on Linux.
The following instructions show how to get this accomplished.

116
##### Prerequisite
WRH's avatar
WRH committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130

The following software is required for building MMCV on windows.
Install them first.

- [Git](https://git-scm.com/download/win)
  - During installation, tick **add git to Path**.
- [Visual Studio Community 2019](https://visualstudio.microsoft.com)
  - A compiler for C++ and CUDA codes.
- [Miniconda](https://docs.conda.io/en/latest/miniconda.html)
  - Official distributions of Python should work too.
- [CUDA 10.2](https://developer.nvidia.com/cuda-10.2-download-archive)
  - Not required for building CPU version.
  - Customize the installation if necessary. As a recommendation, skip the driver installation if a newer version is already installed.

131
132
133
```{note}
You should know how to set up environment variables, especially `Path`, on Windows. The following instruction relies heavily on this skill.
```
WRH's avatar
WRH committed
134

135
##### Common steps
WRH's avatar
WRH committed
136
137
138

1. Launch Anaconda prompt from Windows Start menu

139
   Do not use raw `cmd.exe` s instruction is based on PowerShell syntax.
WRH's avatar
WRH committed
140

141
2. Create a new conda environment
WRH's avatar
WRH committed
142

143
144
145
   ```powershell
   (base) PS C:\Users\xxx> conda create --name mmcv python=3.7
   (base) PS C:\Users\xxx> conda activate mmcv  # make sure to activate environment before any operation
146
   ```
WRH's avatar
WRH committed
147

148
3. Install PyTorch. Choose a version based on your need.
WRH's avatar
WRH committed
149

150
151
152
153
154
   ```powershell
   # CUDA version
   (mmcv) PS C:\Users\xxx> conda install pytorch torchvision cudatoolkit=10.2 -c pytorch
   # CPU version
   (mmcv) PS C:\Users\xxx> conda install install pytorch torchvision cpuonly -c pytorch
155
   ```
WRH's avatar
WRH committed
156

157
4. Clone the repo
WRH's avatar
WRH committed
158

159
160
161
   ```powershell
   (mmcv) PS C:\Users\xxx> git clone https://github.com/open-mmlab/mmcv.git
   (mmcv) PS C:\Users\xxx\mmcv> cd mmcv
162
   ```
WRH's avatar
WRH committed
163

164
5. Install `ninja` and `psutil` to speed up the compilation
WRH's avatar
WRH committed
165

166
167
   ```powershell
   (mmcv) PS C:\Users\xxx\mmcv> pip install -r requirements/optional.txt
168
   ```
169

170
6. Set up MSVC compiler
WRH's avatar
WRH committed
171

172
   Set Environment variable, add `C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\Hostx86\x64` to `PATH`, so that `cl.exe` will be available in prompt, as shown below.
WRH's avatar
WRH committed
173

174
175
   ```powershell
   (mmcv) PS C:\Users\xxx\mmcv> cl
176
177
   Microsoft (R) C/C++ Optimizing  Compiler Version 19.27.29111 for x64
   Copyright (C) Microsoft Corporation.   All rights reserved.
WRH's avatar
WRH committed
178

179
180
   usage: cl [ option... ] filename... [ / link linkoption... ]
   ```
WRH's avatar
WRH committed
181

182
   For compatibility, we use the x86-hosted and x64-targeted compiler. note `Hostx86\x64` in the path.
WRH's avatar
WRH committed
183

184
   You may want to change the system language to English because pytorch will parse text output from `cl.exe` to check its version. However only utf-8 is recognized. Navigate to Control Panel -> Region -> Administrative -> Language for Non-Unicode programs and change it to English.
WRH's avatar
WRH committed
185

186
##### Build and install MMCV
WRH's avatar
WRH committed
187

188
mmcv can be built in two ways:
WRH's avatar
WRH committed
189

190
1. Full version (CPU ops)
WRH's avatar
WRH committed
191

192
   Module `ops` will be compiled as a pytorch extension, but only x86 code will be compiled. The compiled ops can be executed on CPU only.
193

194
2. Full version (CUDA ops)
WRH's avatar
WRH committed
195

196
   Both x86 and CUDA codes of `ops` module will be compiled. The compiled version can be run on both CPU and CUDA-enabled GPU (if implemented).
WRH's avatar
WRH committed
197

198
###### CPU version
WRH's avatar
WRH committed
199

200
Build and install
WRH's avatar
WRH committed
201

202
203
204
205
```powershell
(mmcv) PS C:\Users\xxx\mmcv> python setup.py build_ext
(mmcv) PS C:\Users\xxx\mmcv> python setup.py develop
```
WRH's avatar
WRH committed
206

207
###### GPU version
208

209
1. Make sure `CUDA_PATH` or `CUDA_HOME` is already set in `envs` via `ls env:`, desired output is shown as below:
WRH's avatar
WRH committed
210

211
212
   ```powershell
   (mmcv) PS C:\Users\xxx\mmcv> ls env:
WRH's avatar
WRH committed
213
214
215
216
217
218
219
220

   Name                           Value
   ----                           -----
   CUDA_PATH                      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2
   CUDA_PATH_V10_1                C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1
   CUDA_PATH_V10_2                C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2
   ```

221
   This should already be done by CUDA installer. If not, or you have multiple version of CUDA toolkit installed, set it with
WRH's avatar
WRH committed
222

223
224
   ```powershell
   (mmcv) PS C:\Users\xxx\mmcv> $env:CUDA_HOME = "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2"
WRH's avatar
WRH committed
225
   # OR
226
   (mmcv) PS C:\Users\xxx\mmcv> $env:CUDA_HOME = $env:CUDA_PATH_V10_2 # if CUDA_PATH_V10_2 is in envs:
WRH's avatar
WRH committed
227
228
   ```

229
2. Set CUDA target arch
WRH's avatar
WRH committed
230
231

   ```shell
232
233
   # Here you need to change to the target architecture corresponding to your GPU
   (mmcv) PS C:\Users\xxx\mmcv> $env:TORCH_CUDA_ARCH_LIST="7.5"
WRH's avatar
WRH committed
234
235
   ```

236
237
   :::{note}
   Check your the compute capability of your GPU from [here](https://developer.nvidia.com/cuda-gpus).
WRH's avatar
WRH committed
238

239
240
241
242
243
244
   ```powershell
   (mmcv) PS C:\Users\xxx\mmcv> &"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\extras\demo_suite\deviceQuery.exe"
   Device 0: "NVIDIA GeForce GTX 1660 SUPER"
   CUDA Driver Version / Runtime Version          11.7 / 11.1
   CUDA Capability Major/Minor version number:    7.5
   ```
WRH's avatar
WRH committed
245

246
247
248
249
250
251
   The 7.5 above indicates the target architecture. Note: You need to replace v10.2 with your CUDA version in the above command.
   :::

3. Build and install

   ```powershell
WRH's avatar
WRH committed
252
   # build
253
   python setup.py build_ext # if success, cl will be launched to compile ops
WRH's avatar
WRH committed
254
255
256
257
   # install
   python setup.py develop
   ```

258
259
260
   ```{note}
   If you are compiling against PyTorch 1.6.0, you might meet some errors from PyTorch as described in [this issue](https://github.com/pytorch/pytorch/issues/42467). Follow [this pull request](https://github.com/pytorch/pytorch/pull/43380/files) to modify the source code in your local PyTorch installation.
   ```
WRH's avatar
WRH committed
261

262
##### Validate installation
263

264
265
266
```powershell
(mmcv) PS C:\Users\xxx\mmcv> python .dev_scripts/check_installation.py
```
267

268
269
If no error is reported by the above command, the installation is successful. If there is an error reported, please check [Frequently Asked Questions](../faq.md) to see if there is already a solution.
If no solution is found, please feel free to open an [issue](https://github.com/open-mmlab/mmcv/issues).
270

271
### Build mmcv-lite
272

273
If you need to use PyTorch-related modules, make sure PyTorch has been successfully installed in your environment by referring to the [PyTorch official installation guide](https://github.com/pytorch/pytorch#installation).
274

275
1. Clone the repo
276

277
278
279
280
   ```bash
   git clone https://github.com/open-mmlab/mmcv.git
   cd mmcv
   ```
281

282
2. Start building
283

284
285
286
   ```bash
   MMCV_WITH_OPS=0 pip install -e . -v
   ```
287

288
289
290
291
292
3. Validate installation

   ```bash
   python -c 'import mmcv;print(mmcv.__version__)'
   ```
limm's avatar
limm committed
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349

### Build mmcv-full on Cambricon MLU Devices

#### Install torch_mlu

##### Option1: Install mmcv-full based on Cambricon docker image

Firstly, install and pull Cambricon docker image (please email service@cambricon.com for the latest release docker):

```bash
docker pull ${docker image}
```

Run and attach to the docker, [Install mmcv-full on MLU device](#install-mmcv\-full-on-cambricon-mlu-device) and [make sure you've installed mmcv-full on MLU device successfully](#test-code)

##### Option2: Install mmcv-full from compiling Cambricon PyTorch source code

Please email service@cambricon.com or contact with Cambricon engineers for a suitable version of CATCH package. After you get the suitable version of CATCH package, please follow the steps in ${CATCH-path}/CONTRIBUTING.md to install Cambricon PyTorch.

#### Install mmcv-full on Cambricon MLU device

Clone the repo

```bash
git clone https://github.com/open-mmlab/mmcv.git
```

The mlu-ops library will be downloaded to the default directory (mmcv/mlu-ops) while building MMCV. You can also set `MMCV_MLU_OPS_PATH` to an existing mlu-ops library before building as follows:

```bash
export MMCV_MLU_OPS_PATH=/xxx/xxx/mlu-ops
```

Install mmcv-full

```bash
cd mmcv
export MMCV_WITH_OPS=1
export FORCE_MLU=1
python setup.py install
```

#### Test Code

After finishing previous steps, you can run the following python code to make sure that you've installed mmcv-full on MLU device successfully

```python
import torch
import torch_mlu
from mmcv.ops import sigmoid_focal_loss
x = torch.randn(3, 10).mlu()
x.requires_grad = True
y = torch.tensor([1, 5, 3]).mlu()
w = torch.ones(10).float().mlu()
output = sigmoid_focal_loss(x, y, 2.0, 0.25, w, 'none')
print(output)
```