build.windows.rst 7.17 KB
Newer Older
1
2
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Building on Windows
===================

To build TorchAudio on Windows, we need to enable C++ compiler and install build tools and runtime dependencies.

We use Microsoft Visual C++ for compiling C++ and Conda for managing the other build tools and runtime dependencies.

1. Install build tools
----------------------

MSVC
~~~~

Please follow the instruction at https://visualstudio.microsoft.com/downloads/, and make sure to install C++ development tools.

.. note::

   The official binary distribution are compiled with MSVC 2019.
   The following section uses path from MSVC 2019 Community Edition.

Conda
~~~~~

Please follow the instruction at https://docs.conda.io/en/latest/miniconda.html.

2. Start the dev environment
----------------------------

In the following, we need to use C++ compiler (``cl``), and Conda package manager (``conda``).
We also use Bash for the sake of similar experience to Linux/macOS.

To do so, the following three steps are required.

1. Open command prompt
2. Enable developer environment
3. [Optional] Launch bash

|

The following combination is known to work.

1. Launch Anaconda3 Command Prompt.

   |

   .. image:: https://download.pytorch.org/torchaudio/doc-assets/windows-conda.png
      :width: 360px

   |

   Please make sure that ``conda`` command is recognized.

   |

   .. image:: https://download.pytorch.org/torchaudio/doc-assets/windows-conda2.png
      :width: 360px

   |

2. Activate dev tools by running the following command.

62
63
64
65
66
   We need to use the MSVC x64 toolset for compilation.
   To enable the toolset, one can use ``vcvarsall.bat`` or ``vcvars64.bat`` file, which
   are found under Visual Studio's installation folder, under ``VC\Auxiliary\Build\``.
   More information are available at https://docs.microsoft.com/en-us/cpp/build/how-to-enable-a-64-bit-visual-cpp-toolset-on-the-command-line?view=msvc-160#use-vcvarsallbat-to-set-a-64-bit-hosted-build-architecture

67
68
69
70
71
72
73
74
75
76
77
78
79
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
   .. code-block::

      call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x64

   Please makes sure that ``cl`` command is recognized.

   .. image:: https://download.pytorch.org/torchaudio/doc-assets/windows-msvc.png
      :width: 360px

3. [Optional] Launch bash with the following command.

   If you want a similar UX as Linux/macOS, you can launch Bash. However, please note that in Bash environment, the file paths are different from native Windows style, and ``torchaudio.datasets`` module does not work.

   .. code-block::

      Miniconda3\Library\bin\bash.exe

   .. image:: https://download.pytorch.org/torchaudio/doc-assets/windows-bash.png
      :width: 360px

3. Install PyTorch
------------------

Please refer to https://pytorch.org/get-started/locally/ for the up-to-date way to install PyTorch.

The following command installs the nightly build version of PyTorch.

.. code-block::

   # CPU-only
   conda install pytorch cpuonly -c pytorch-nightly

   # With CUDA support
   conda install pytorch pytorch-cuda=11.7 -c pytorch-nightly -c nvidia

When installing CUDA-enabled version, it also install CUDA toolkit.

4. [Optional] cuDNN
-------------------

If you intend to build CUDA-related features, please install cuDNN.

Download CuDNN from https://developer.nvidia.com/cudnn, and extract files in
the same directories as CUDA toolkit.

When using conda, the directories are ``${CONDA_PREFIX}/bin``, ``${CONDA_PREFIX}/include``, ``${CONDA_PREFIX}/Lib/x64``.

5. Install external dependencies
--------------------------------

.. code-block::

   conda install cmake ninja

.. code-block::

   conda install -c conda-forge ffmpeg

6. Build TorchAudio
-------------------

Now that we have everything ready, we can build TorchAudio.

.. code-block::

   git clone https://github.com/pytorch/audio
   cd audio


.. code-block::

   # In Command Prompt
   set USE_FFMPEG=1
   python setup.py develop

.. code-block::

   # In Bash
   USE_FFMPEG=1 python setup.py develop

.. note::
   Due to the complexity of build process, TorchAudio only supports in-place build.
   To use ``pip``, please use ``--no-use-pep517`` option.

   ``USE_FFMPEG=1 pip install -v -e . --no-use-pep517``

[Optional] Building FFmpeg from source
--------------------------------------

The following section illustrates a way to build FFmpeg libraries from source.

Conda-forge's FFmpeg package comes with support for major codecs and GPU decoders, so regular users and developers do not need to build FFmpeg from source.

If you are not using Conda, then you can either find a pre-built binary distribution or build FFmpeg by yourself.

Also, in case torchaudio developer needs to update and customize the CI for FFmpeg build, this section might be helpful.

1. Install MSYS2
~~~~~~~~~~~~~~~~

moto's avatar
moto committed
167
To build FFmpeg in a way it is usable from the TorchAudio development environment, we need to build binaries native to ``MINGW64``. To do so, we need tools required by FFmpeg's build process, such as ``pkg-config`` and ``make``,  that work in ``MINGW64`` environment. For this purpose, we use MSYS2.
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

FFmpeg's official documentation touches this https://trac.ffmpeg.org/wiki/CompilationGuide/MinGW

Please follow the instruction at https://www.msys2.org/ to install MSYS2.

2. Launch MSYS2
~~~~~~~~~~~~~~~

Use the shortcut to launch MSYS2 (MINGW64).

.. image:: https://download.pytorch.org/torchaudio/doc-assets/windows-msys2.png
   :width: 360px

.. note::

   The Bash environment in MSYS2 does not play well with Conda env, so do not add Conda initialization script in ``~/.bashrc`` of MSYS2 environment. (i.e. ``C:\msys2\home\USER\.bashrc``) Instead, add it in ``C:\Users\USER\.bashrc``)

3. Install build tools
~~~~~~~~~~~~~~~~~~~~~~

.. code-block::

   $ pacman -S mingw-w64-x86_64-make
   $ pacman -S mingw-w64-x86_64-yasm

After the installation, you should have packages similar to the following;

.. code-block::

   $ pacman -Qe
   base 2020.12-1
   base-devel 2022.01-2
   filesystem 2023.01-2
   mingw-w64-x86_64-make 4.3-1
   mingw-w64-x86_64-pkgconf 1.8.0-2
   mingw-w64-x86_64-yasm 1.3.0-4
   msys2-runtime 3.4.3-5

4. Build FFmpeg
~~~~~~~~~~~~~~~

Check out FFmpeg source code.

.. code-block::

   git clone https://github.com/ffmpeg/ffmpeg
   cd ffmpeg
   git checkout <VERSION>

Build

.. code-block::

moto's avatar
moto committed
221
   ./configure --toolchain=msvc
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
   make -j

If the build succeeds, ``ffmpeg.exe`` should be found in the same directory. Make sure that you can run it.

5. Verify the build
~~~~~~~~~~~~~~~~~~~

Check that the resulting FFmpeg binary is accessible from Conda env

Now launch a new command prompt and enable the TorchAudio development environment. Make sure that you can run the ``ffmpeg.exe`` command generated in the previous step.

6. Build TorchAudio with the custom FFmpeg
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To use this FFmpeg libraries for building torchaudio, do the following;
1. Uninstall ``ffmpeg`` package installed by conda. ``conda uninstall ffmpeg``.
2. When building set ``FFMPEG_ROOT`` environment variable to the directory where the libraries like ``libavcodec`` are found.

.. code-block::

   # In Command Prompt
   set USE_FFMPEG=1
   set FFMPEG_ROOT=<FFMPEG_BUILD_DIR>
   python setup.py clean develop

.. code-block::

   # In Bash
   USE_FFMPEG=1 FFMPEG_ROOT=<FFMPEG_BUILD_DIR> python setup.py clean develop