xref: /aosp_15_r20/external/pytorch/CONTRIBUTING.md (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1*da0073e9SAndroid Build Coastguard WorkerThank you for your interest in contributing to PyTorch!
2*da0073e9SAndroid Build Coastguard WorkerIf you're a new contributor, please first take a read through our
3*da0073e9SAndroid Build Coastguard Worker[Contributing Guide](https://github.com/pytorch/pytorch/wiki/The-Ultimate-Guide-to-PyTorch-Contributions), specifically the [Submitting a Change](https://github.com/pytorch/pytorch/wiki/The-Ultimate-Guide-to-PyTorch-Contributions#submitting-a-change) section
4*da0073e9SAndroid Build Coastguard Workerthat walks through the process of contributing a change to PyTorch.
5*da0073e9SAndroid Build Coastguard Worker
6*da0073e9SAndroid Build Coastguard WorkerThe rest of this document (CONTRIBUTING.md) covers some of the more technical
7*da0073e9SAndroid Build Coastguard Workeraspects of contributing to PyTorch.
8*da0073e9SAndroid Build Coastguard Worker
9*da0073e9SAndroid Build Coastguard Worker# Table of Contents
10*da0073e9SAndroid Build Coastguard Worker
11*da0073e9SAndroid Build Coastguard Worker<!-- toc -->
12*da0073e9SAndroid Build Coastguard Worker
13*da0073e9SAndroid Build Coastguard Worker- [Developing PyTorch](#developing-pytorch)
14*da0073e9SAndroid Build Coastguard Worker  - [Setup the development environment](#setup-the-development-environment)
15*da0073e9SAndroid Build Coastguard Worker  - [Tips and Debugging](#tips-and-debugging)
16*da0073e9SAndroid Build Coastguard Worker- [Nightly Checkout & Pull](#nightly-checkout--pull)
17*da0073e9SAndroid Build Coastguard Worker- [Codebase structure](#codebase-structure)
18*da0073e9SAndroid Build Coastguard Worker- [Unit testing](#unit-testing)
19*da0073e9SAndroid Build Coastguard Worker  - [Python Unit Testing](#python-unit-testing)
20*da0073e9SAndroid Build Coastguard Worker  - [Better local unit tests with `pytest`](#better-local-unit-tests-with-pytest)
21*da0073e9SAndroid Build Coastguard Worker  - [Local linting](#local-linting)
22*da0073e9SAndroid Build Coastguard Worker    - [Running `mypy`](#running-mypy)
23*da0073e9SAndroid Build Coastguard Worker  - [C++ Unit Testing](#c-unit-testing)
24*da0073e9SAndroid Build Coastguard Worker  - [Run Specific CI Jobs](#run-specific-ci-jobs)
25*da0073e9SAndroid Build Coastguard Worker- [Merging your Change](#merging-your-change)
26*da0073e9SAndroid Build Coastguard Worker- [Writing documentation](#writing-documentation)
27*da0073e9SAndroid Build Coastguard Worker  - [Docstring type formatting](#docstring-type-formatting)
28*da0073e9SAndroid Build Coastguard Worker  - [Building documentation](#building-documentation)
29*da0073e9SAndroid Build Coastguard Worker    - [Tips](#tips)
30*da0073e9SAndroid Build Coastguard Worker    - [Building C++ Documentation](#building-c-documentation)
31*da0073e9SAndroid Build Coastguard Worker  - [Previewing changes locally](#previewing-changes-locally)
32*da0073e9SAndroid Build Coastguard Worker  - [Previewing documentation on PRs](#previewing-documentation-on-prs)
33*da0073e9SAndroid Build Coastguard Worker  - [Adding documentation tests](#adding-documentation-tests)
34*da0073e9SAndroid Build Coastguard Worker- [Profiling with `py-spy`](#profiling-with-py-spy)
35*da0073e9SAndroid Build Coastguard Worker- [Managing multiple build trees](#managing-multiple-build-trees)
36*da0073e9SAndroid Build Coastguard Worker- [C++ development tips](#c-development-tips)
37*da0073e9SAndroid Build Coastguard Worker  - [Build only what you need](#build-only-what-you-need)
38*da0073e9SAndroid Build Coastguard Worker  - [Code completion and IDE support](#code-completion-and-ide-support)
39*da0073e9SAndroid Build Coastguard Worker  - [Make no-op build fast](#make-no-op-build-fast)
40*da0073e9SAndroid Build Coastguard Worker    - [Use Ninja](#use-ninja)
41*da0073e9SAndroid Build Coastguard Worker    - [Use CCache](#use-ccache)
42*da0073e9SAndroid Build Coastguard Worker    - [Use a faster linker](#use-a-faster-linker)
43*da0073e9SAndroid Build Coastguard Worker    - [Use pre-compiled headers](#use-pre-compiled-headers)
44*da0073e9SAndroid Build Coastguard Worker    - [Workaround for header dependency bug in nvcc](#workaround-for-header-dependency-bug-in-nvcc)
45*da0073e9SAndroid Build Coastguard Worker  - [Rebuild few files with debug information](#rebuild-few-files-with-debug-information)
46*da0073e9SAndroid Build Coastguard Worker  - [C++ frontend development tips](#c-frontend-development-tips)
47*da0073e9SAndroid Build Coastguard Worker  - [GDB integration](#gdb-integration)
48*da0073e9SAndroid Build Coastguard Worker  - [C++ stacktraces](#c-stacktraces)
49*da0073e9SAndroid Build Coastguard Worker- [CUDA development tips](#cuda-development-tips)
50*da0073e9SAndroid Build Coastguard Worker- [Windows development tips](#windows-development-tips)
51*da0073e9SAndroid Build Coastguard Worker  - [Known MSVC (and MSVC with NVCC) bugs](#known-msvc-and-msvc-with-nvcc-bugs)
52*da0073e9SAndroid Build Coastguard Worker  - [Building on legacy code and CUDA](#building-on-legacy-code-and-cuda)
53*da0073e9SAndroid Build Coastguard Worker- [Running clang-tidy](#running-clang-tidy)
54*da0073e9SAndroid Build Coastguard Worker- [Pre-commit tidy/linting hook](#pre-commit-tidylinting-hook)
55*da0073e9SAndroid Build Coastguard Worker- [Building PyTorch with ASAN](#building-pytorch-with-asan)
56*da0073e9SAndroid Build Coastguard Worker  - [Getting `ccache` to work](#getting-ccache-to-work)
57*da0073e9SAndroid Build Coastguard Worker  - [Why this stuff with `LD_PRELOAD` and `LIBASAN_RT`?](#why-this-stuff-with-ld_preload-and-libasan_rt)
58*da0073e9SAndroid Build Coastguard Worker  - [Why LD_PRELOAD in the build function?](#why-ld_preload-in-the-build-function)
59*da0073e9SAndroid Build Coastguard Worker  - [Why no leak detection?](#why-no-leak-detection)
60*da0073e9SAndroid Build Coastguard Worker- [Caffe2 notes](#caffe2-notes)
61*da0073e9SAndroid Build Coastguard Worker- [CI failure tips](#ci-failure-tips)
62*da0073e9SAndroid Build Coastguard Worker  - [Which commit is used in CI?](#which-commit-is-used-in-ci)
63*da0073e9SAndroid Build Coastguard Worker- [Dev Infra Office Hours](#dev-infra-office-hours)
64*da0073e9SAndroid Build Coastguard Worker
65*da0073e9SAndroid Build Coastguard Worker<!-- tocstop -->
66*da0073e9SAndroid Build Coastguard Worker
67*da0073e9SAndroid Build Coastguard Worker## Developing PyTorch
68*da0073e9SAndroid Build Coastguard Worker
69*da0073e9SAndroid Build Coastguard WorkerFollow the instructions for [installing PyTorch from source](https://github.com/pytorch/pytorch#from-source). If you get stuck when developing PyTorch on your machine, check out the [tips and debugging](#tips-and-debugging) section below for common solutions.
70*da0073e9SAndroid Build Coastguard Worker
71*da0073e9SAndroid Build Coastguard Worker### Setup the development environment
72*da0073e9SAndroid Build Coastguard Worker
73*da0073e9SAndroid Build Coastguard WorkerFirst, you need to [fork the PyTorch project on GitHub](https://github.com/pytorch/pytorch/fork) and follow the instructions at [Connecting to GitHub with SSH](https://docs.github.com/en/authentication/connecting-to-github-with-ssh) to setup your SSH authentication credentials.
74*da0073e9SAndroid Build Coastguard Worker
75*da0073e9SAndroid Build Coastguard WorkerThen clone the PyTorch project and setup the development environment:
76*da0073e9SAndroid Build Coastguard Worker
77*da0073e9SAndroid Build Coastguard Worker```bash
78*da0073e9SAndroid Build Coastguard Workergit clone [email protected]:<USERNAME>/pytorch.git
79*da0073e9SAndroid Build Coastguard Workercd pytorch
80*da0073e9SAndroid Build Coastguard Workergit remote add upstream [email protected]:pytorch/pytorch.git
81*da0073e9SAndroid Build Coastguard Worker
82*da0073e9SAndroid Build Coastguard Workermake setup-env  # or make setup-env-cuda for pre-built CUDA binaries
83*da0073e9SAndroid Build Coastguard Workerconda activate pytorch-deps
84*da0073e9SAndroid Build Coastguard Worker```
85*da0073e9SAndroid Build Coastguard Worker
86*da0073e9SAndroid Build Coastguard Worker### Tips and Debugging
87*da0073e9SAndroid Build Coastguard Worker
88*da0073e9SAndroid Build Coastguard Worker* If you want to have no-op incremental rebuilds (which are fast), see [Make no-op build fast](#make-no-op-build-fast) below.
89*da0073e9SAndroid Build Coastguard Worker
90*da0073e9SAndroid Build Coastguard Worker* When installing with `python setup.py develop` (in contrast to `python setup.py install`) Python runtime will use
91*da0073e9SAndroid Build Coastguard Worker  the current local source-tree when importing `torch` package. (This is done by creating [`.egg-link`](https://wiki.python.org/moin/PythonPackagingTerminology#egg-link) file in `site-packages` folder)
92*da0073e9SAndroid Build Coastguard Worker  This way you do not need to repeatedly install after modifying Python files (`.py`).
93*da0073e9SAndroid Build Coastguard Worker  However, you would need to reinstall if you modify Python interface (`.pyi`, `.pyi.in`) or
94*da0073e9SAndroid Build Coastguard Worker   non-Python files (`.cpp`, `.cc`, `.cu`, `.h`, ...).
95*da0073e9SAndroid Build Coastguard Worker
96*da0073e9SAndroid Build Coastguard Worker
97*da0073e9SAndroid Build Coastguard Worker  One way to avoid running `python setup.py develop` every time one makes a change to C++/CUDA/ObjectiveC files on Linux/Mac,
98*da0073e9SAndroid Build Coastguard Worker  is to create a symbolic link from `build` folder to `torch/lib`, for example, by issuing following:
99*da0073e9SAndroid Build Coastguard Worker  ```bash
100*da0073e9SAndroid Build Coastguard Worker   pushd torch/lib; sh -c "ln -sf ../../build/lib/libtorch_cpu.* ."; popd
101*da0073e9SAndroid Build Coastguard Worker  ```
102*da0073e9SAndroid Build Coastguard Worker   Afterwards rebuilding a library (for example to rebuild `libtorch_cpu.so` issue `ninja torch_cpu` from `build` folder),
103*da0073e9SAndroid Build Coastguard Worker   would be sufficient to make change visible in `torch` package.
104*da0073e9SAndroid Build Coastguard Worker
105*da0073e9SAndroid Build Coastguard Worker
106*da0073e9SAndroid Build Coastguard Worker  To reinstall, first uninstall all existing PyTorch installs. You may need to run `pip
107*da0073e9SAndroid Build Coastguard Worker  uninstall torch` multiple times. You'll know `torch` is fully
108*da0073e9SAndroid Build Coastguard Worker  uninstalled when you see `WARNING: Skipping torch as it is not
109*da0073e9SAndroid Build Coastguard Worker  installed`. (You should only have to `pip uninstall` a few times, but
110*da0073e9SAndroid Build Coastguard Worker  you can always `uninstall` with `timeout` or in a loop if you're feeling
111*da0073e9SAndroid Build Coastguard Worker  lazy.)
112*da0073e9SAndroid Build Coastguard Worker
113*da0073e9SAndroid Build Coastguard Worker  ```bash
114*da0073e9SAndroid Build Coastguard Worker  conda uninstall pytorch -y
115*da0073e9SAndroid Build Coastguard Worker  yes | pip uninstall torch
116*da0073e9SAndroid Build Coastguard Worker  ```
117*da0073e9SAndroid Build Coastguard Worker
118*da0073e9SAndroid Build Coastguard Worker  Next run `python setup.py clean`. After that, you can install in `develop` mode again.
119*da0073e9SAndroid Build Coastguard Worker
120*da0073e9SAndroid Build Coastguard Worker* If you run into errors when running `python setup.py develop`, here are some debugging steps:
121*da0073e9SAndroid Build Coastguard Worker  1. Run `printf '#include <stdio.h>\nint main() { printf("Hello World");}'|clang -x c -; ./a.out` to make sure
122*da0073e9SAndroid Build Coastguard Worker  your CMake works and can compile this simple Hello World program without errors.
123*da0073e9SAndroid Build Coastguard Worker  2. Nuke your `build` directory. The `setup.py` script compiles binaries into the `build` folder and caches many
124*da0073e9SAndroid Build Coastguard Worker  details along the way, which saves time the next time you build. If you're running into issues, you can always
125*da0073e9SAndroid Build Coastguard Worker  `rm -rf build` from the toplevel `pytorch` directory and start over.
126*da0073e9SAndroid Build Coastguard Worker  3. If you have made edits to the PyTorch repo, commit any change you'd like to keep and clean the repo with the
127*da0073e9SAndroid Build Coastguard Worker  following commands (note that clean _really_ removes all untracked files and changes.):
128*da0073e9SAndroid Build Coastguard Worker      ```bash
129*da0073e9SAndroid Build Coastguard Worker      git submodule deinit -f .
130*da0073e9SAndroid Build Coastguard Worker      git clean -xdf
131*da0073e9SAndroid Build Coastguard Worker      python setup.py clean
132*da0073e9SAndroid Build Coastguard Worker      git submodule update --init --recursive # very important to sync the submodules
133*da0073e9SAndroid Build Coastguard Worker      python setup.py develop                 # then try running the command again
134*da0073e9SAndroid Build Coastguard Worker      ```
135*da0073e9SAndroid Build Coastguard Worker  4. The main step within `python setup.py develop` is running `make` from the `build` directory. If you want to
136*da0073e9SAndroid Build Coastguard Worker    experiment with some environment variables, you can pass them into the command:
137*da0073e9SAndroid Build Coastguard Worker      ```bash
138*da0073e9SAndroid Build Coastguard Worker      ENV_KEY1=ENV_VAL1[, ENV_KEY2=ENV_VAL2]* python setup.py develop
139*da0073e9SAndroid Build Coastguard Worker      ```
140*da0073e9SAndroid Build Coastguard Worker
141*da0073e9SAndroid Build Coastguard Worker* If you run into issue running `git submodule update --init --recursive`. Please try the following:
142*da0073e9SAndroid Build Coastguard Worker  - If you encounter an error such as
143*da0073e9SAndroid Build Coastguard Worker    ```
144*da0073e9SAndroid Build Coastguard Worker    error: Submodule 'third_party/pybind11' could not be updated
145*da0073e9SAndroid Build Coastguard Worker    ```
146*da0073e9SAndroid Build Coastguard Worker    check whether your Git local or global config file contains any `submodule.*` settings. If yes, remove them and try again.
147*da0073e9SAndroid Build Coastguard Worker    (please reference [this doc](https://git-scm.com/docs/git-config#Documentation/git-config.txt-submoduleltnamegturl) for more info).
148*da0073e9SAndroid Build Coastguard Worker
149*da0073e9SAndroid Build Coastguard Worker  - If you encounter an error such as
150*da0073e9SAndroid Build Coastguard Worker    ```
151*da0073e9SAndroid Build Coastguard Worker    fatal: unable to access 'https://github.com/pybind11/pybind11.git': could not load PEM client certificate ...
152*da0073e9SAndroid Build Coastguard Worker    ```
153*da0073e9SAndroid Build Coastguard Worker    this is likely that you are using HTTP proxying and the certificate expired. To check if the certificate is valid, run
154*da0073e9SAndroid Build Coastguard Worker    `git config --global --list` and search for config like `http.proxysslcert=<cert_file>`. Then check certificate valid date by running
155*da0073e9SAndroid Build Coastguard Worker    ```bash
156*da0073e9SAndroid Build Coastguard Worker    openssl x509 -noout -in <cert_file> -dates
157*da0073e9SAndroid Build Coastguard Worker    ```
158*da0073e9SAndroid Build Coastguard Worker
159*da0073e9SAndroid Build Coastguard Worker  - If you encounter an error that some third_party modules are not checked out correctly, such as
160*da0073e9SAndroid Build Coastguard Worker    ```
161*da0073e9SAndroid Build Coastguard Worker    Could not find .../pytorch/third_party/pybind11/CMakeLists.txt
162*da0073e9SAndroid Build Coastguard Worker    ```
163*da0073e9SAndroid Build Coastguard Worker    remove any `submodule.*` settings in your local git config (`.git/config` of your pytorch repo) and try again.
164*da0073e9SAndroid Build Coastguard Worker* If you're a Windows contributor, please check out [Best Practices](https://github.com/pytorch/pytorch/wiki/Best-Practices-to-Edit-and-Compile-Pytorch-Source-Code-On-Windows).
165*da0073e9SAndroid Build Coastguard Worker* For help with any part of the contributing process, please don’t hesitate to utilize our Zoom office hours! See details [here](https://github.com/pytorch/pytorch/wiki/Dev-Infra-Office-Hours)
166*da0073e9SAndroid Build Coastguard Worker
167*da0073e9SAndroid Build Coastguard Worker## Nightly Checkout & Pull
168*da0073e9SAndroid Build Coastguard Worker
169*da0073e9SAndroid Build Coastguard WorkerThe `tools/nightly.py` script is provided to ease pure Python development of
170*da0073e9SAndroid Build Coastguard WorkerPyTorch. This uses `conda` and `git` to check out the nightly development
171*da0073e9SAndroid Build Coastguard Workerversion of PyTorch and installs pre-built binaries into the current repository.
172*da0073e9SAndroid Build Coastguard WorkerThis is like a development or editable install, but without needing the ability
173*da0073e9SAndroid Build Coastguard Workerto compile any C++ code.
174*da0073e9SAndroid Build Coastguard Worker
175*da0073e9SAndroid Build Coastguard WorkerYou can use this script to check out a new nightly branch with the following:
176*da0073e9SAndroid Build Coastguard Worker
177*da0073e9SAndroid Build Coastguard Worker```bash
178*da0073e9SAndroid Build Coastguard Worker./tools/nightly.py checkout -b my-nightly-branch
179*da0073e9SAndroid Build Coastguard Workerconda activate pytorch-deps
180*da0073e9SAndroid Build Coastguard Worker```
181*da0073e9SAndroid Build Coastguard Worker
182*da0073e9SAndroid Build Coastguard WorkerOr if you would like to re-use an existing conda environment, you can pass in
183*da0073e9SAndroid Build Coastguard Workerthe regular environment parameters (`--name` or `--prefix`):
184*da0073e9SAndroid Build Coastguard Worker
185*da0073e9SAndroid Build Coastguard Worker```bash
186*da0073e9SAndroid Build Coastguard Worker./tools/nightly.py checkout -b my-nightly-branch -n my-env
187*da0073e9SAndroid Build Coastguard Workerconda activate my-env
188*da0073e9SAndroid Build Coastguard Worker```
189*da0073e9SAndroid Build Coastguard Worker
190*da0073e9SAndroid Build Coastguard WorkerTo install the nightly binaries built with CUDA, you can pass in the flag `--cuda`:
191*da0073e9SAndroid Build Coastguard Worker
192*da0073e9SAndroid Build Coastguard Worker```bash
193*da0073e9SAndroid Build Coastguard Worker./tools/nightly.py checkout -b my-nightly-branch --cuda
194*da0073e9SAndroid Build Coastguard Workerconda activate pytorch-deps
195*da0073e9SAndroid Build Coastguard Worker```
196*da0073e9SAndroid Build Coastguard Worker
197*da0073e9SAndroid Build Coastguard WorkerYou can also use this tool to pull the nightly commits into the current branch:
198*da0073e9SAndroid Build Coastguard Worker
199*da0073e9SAndroid Build Coastguard Worker```bash
200*da0073e9SAndroid Build Coastguard Worker./tools/nightly.py pull -n my-env
201*da0073e9SAndroid Build Coastguard Workerconda activate my-env
202*da0073e9SAndroid Build Coastguard Worker```
203*da0073e9SAndroid Build Coastguard Worker
204*da0073e9SAndroid Build Coastguard WorkerPulling will reinstall the PyTorch dependencies as well as the nightly binaries
205*da0073e9SAndroid Build Coastguard Workerinto the repo directory.
206*da0073e9SAndroid Build Coastguard Worker
207*da0073e9SAndroid Build Coastguard Worker## Codebase structure
208*da0073e9SAndroid Build Coastguard Worker
209*da0073e9SAndroid Build Coastguard Worker* [c10](c10) - Core library files that work everywhere, both server
210*da0073e9SAndroid Build Coastguard Worker  and mobile. We are slowly moving pieces from [ATen/core](aten/src/ATen/core)
211*da0073e9SAndroid Build Coastguard Worker  here. This library is intended only to contain essential functionality,
212*da0073e9SAndroid Build Coastguard Worker  and appropriate to use in settings where binary size matters. (But
213*da0073e9SAndroid Build Coastguard Worker  you'll have a lot of missing functionality if you try to use it
214*da0073e9SAndroid Build Coastguard Worker  directly.)
215*da0073e9SAndroid Build Coastguard Worker* [aten](aten) - C++ tensor library for PyTorch (no autograd support)
216*da0073e9SAndroid Build Coastguard Worker  * [src](aten/src) - [README](aten/src/README.md)
217*da0073e9SAndroid Build Coastguard Worker    * [ATen](aten/src/ATen)
218*da0073e9SAndroid Build Coastguard Worker      * [core](aten/src/ATen/core) - Core functionality of ATen. This
219*da0073e9SAndroid Build Coastguard Worker        is migrating to top-level c10 folder.
220*da0073e9SAndroid Build Coastguard Worker      * [native](aten/src/ATen/native) - Modern implementations of
221*da0073e9SAndroid Build Coastguard Worker        operators. If you want to write a new operator, here is where
222*da0073e9SAndroid Build Coastguard Worker        it should go. Most CPU operators go in the top level directory,
223*da0073e9SAndroid Build Coastguard Worker        except for operators which need to be compiled specially; see
224*da0073e9SAndroid Build Coastguard Worker        cpu below.
225*da0073e9SAndroid Build Coastguard Worker        * [cpu](aten/src/ATen/native/cpu) - Not actually CPU
226*da0073e9SAndroid Build Coastguard Worker          implementations of operators, but specifically implementations
227*da0073e9SAndroid Build Coastguard Worker          which are compiled with processor-specific instructions, like
228*da0073e9SAndroid Build Coastguard Worker          AVX. See the [README](aten/src/ATen/native/cpu/README.md) for more
229*da0073e9SAndroid Build Coastguard Worker          details.
230*da0073e9SAndroid Build Coastguard Worker        * [cuda](aten/src/ATen/native/cuda) - CUDA implementations of
231*da0073e9SAndroid Build Coastguard Worker          operators.
232*da0073e9SAndroid Build Coastguard Worker        * [sparse](aten/src/ATen/native/sparse) - CPU and CUDA
233*da0073e9SAndroid Build Coastguard Worker          implementations of COO sparse tensor operations
234*da0073e9SAndroid Build Coastguard Worker        * [mkl](aten/src/ATen/native/mkl) [mkldnn](aten/src/ATen/native/mkldnn)
235*da0073e9SAndroid Build Coastguard Worker          [miopen](aten/src/ATen/native/miopen) [cudnn](aten/src/ATen/native/cudnn)
236*da0073e9SAndroid Build Coastguard Worker          - implementations of operators which simply bind to some
237*da0073e9SAndroid Build Coastguard Worker            backend library.
238*da0073e9SAndroid Build Coastguard Worker        * [quantized](aten/src/ATen/native/quantized/) - Quantized tensor (i.e. QTensor) operation implementations. [README](aten/src/ATen/native/quantized/README.md) contains details including how to implement native quantized operations.
239*da0073e9SAndroid Build Coastguard Worker* [torch](torch) - The actual PyTorch library. Everything that is not
240*da0073e9SAndroid Build Coastguard Worker  in [csrc](torch/csrc) is a Python module, following the PyTorch Python
241*da0073e9SAndroid Build Coastguard Worker  frontend module structure.
242*da0073e9SAndroid Build Coastguard Worker  * [csrc](torch/csrc) - C++ files composing the PyTorch library. Files
243*da0073e9SAndroid Build Coastguard Worker    in this directory tree are a mix of Python binding code, and C++
244*da0073e9SAndroid Build Coastguard Worker    heavy lifting. Consult `setup.py` for the canonical list of Python
245*da0073e9SAndroid Build Coastguard Worker    binding files; conventionally, they are often prefixed with
246*da0073e9SAndroid Build Coastguard Worker    `python_`. [README](torch/csrc/README.md)
247*da0073e9SAndroid Build Coastguard Worker    * [jit](torch/csrc/jit) - Compiler and frontend for TorchScript JIT
248*da0073e9SAndroid Build Coastguard Worker      frontend. [README](torch/csrc/jit/README.md)
249*da0073e9SAndroid Build Coastguard Worker    * [autograd](torch/csrc/autograd) - Implementation of reverse-mode automatic differentiation. [README](torch/csrc/autograd/README.md)
250*da0073e9SAndroid Build Coastguard Worker    * [api](torch/csrc/api) - The PyTorch C++ frontend.
251*da0073e9SAndroid Build Coastguard Worker    * [distributed](torch/csrc/distributed) - Distributed training
252*da0073e9SAndroid Build Coastguard Worker      support for PyTorch.
253*da0073e9SAndroid Build Coastguard Worker* [tools](tools) - Code generation scripts for the PyTorch library.
254*da0073e9SAndroid Build Coastguard Worker  See [README](tools/README.md) of this directory for more details.
255*da0073e9SAndroid Build Coastguard Worker* [test](test) - Python unit tests for PyTorch Python frontend.
256*da0073e9SAndroid Build Coastguard Worker  * [test_torch.py](test/test_torch.py) - Basic tests for PyTorch
257*da0073e9SAndroid Build Coastguard Worker    functionality.
258*da0073e9SAndroid Build Coastguard Worker  * [test_autograd.py](test/test_autograd.py) - Tests for non-NN
259*da0073e9SAndroid Build Coastguard Worker    automatic differentiation support.
260*da0073e9SAndroid Build Coastguard Worker  * [test_nn.py](test/test_nn.py) - Tests for NN operators and
261*da0073e9SAndroid Build Coastguard Worker    their automatic differentiation.
262*da0073e9SAndroid Build Coastguard Worker  * [test_jit.py](test/test_jit.py) - Tests for the JIT compiler
263*da0073e9SAndroid Build Coastguard Worker    and TorchScript.
264*da0073e9SAndroid Build Coastguard Worker  * ...
265*da0073e9SAndroid Build Coastguard Worker  * [cpp](test/cpp) - C++ unit tests for PyTorch C++ frontend.
266*da0073e9SAndroid Build Coastguard Worker    * [api](test/cpp/api) - [README](test/cpp/api/README.md)
267*da0073e9SAndroid Build Coastguard Worker    * [jit](test/cpp/jit) - [README](test/cpp/jit/README.md)
268*da0073e9SAndroid Build Coastguard Worker    * [tensorexpr](test/cpp/tensorexpr) - [README](test/cpp/tensorexpr/README.md)
269*da0073e9SAndroid Build Coastguard Worker  * [expect](test/expect) - Automatically generated "expect" files
270*da0073e9SAndroid Build Coastguard Worker    which are used to compare against expected output.
271*da0073e9SAndroid Build Coastguard Worker  * [onnx](test/onnx) - Tests for ONNX export functionality,
272*da0073e9SAndroid Build Coastguard Worker    using both PyTorch and Caffe2.
273*da0073e9SAndroid Build Coastguard Worker* [caffe2](caffe2) - The Caffe2 library.
274*da0073e9SAndroid Build Coastguard Worker  * [core](caffe2/core) - Core files of Caffe2, e.g., tensor, workspace,
275*da0073e9SAndroid Build Coastguard Worker    blobs, etc.
276*da0073e9SAndroid Build Coastguard Worker  * [operators](caffe2/operators) - Operators of Caffe2.
277*da0073e9SAndroid Build Coastguard Worker  * [python](caffe2/python) - Python bindings to Caffe2.
278*da0073e9SAndroid Build Coastguard Worker  * ...
279*da0073e9SAndroid Build Coastguard Worker* [.circleci](.circleci) - CircleCI configuration management. [README](.circleci/README.md)
280*da0073e9SAndroid Build Coastguard Worker
281*da0073e9SAndroid Build Coastguard Worker## Unit testing
282*da0073e9SAndroid Build Coastguard Worker
283*da0073e9SAndroid Build Coastguard Worker### Python Unit Testing
284*da0073e9SAndroid Build Coastguard Worker
285*da0073e9SAndroid Build Coastguard Worker**Prerequisites**:
286*da0073e9SAndroid Build Coastguard WorkerThe following packages should be installed with either `conda` or `pip`:
287*da0073e9SAndroid Build Coastguard Worker- `expecttest` and `hypothesis` - required to run tests
288*da0073e9SAndroid Build Coastguard Worker- `mypy` - recommended for linting
289*da0073e9SAndroid Build Coastguard Worker- `pytest` - recommended to run tests more selectively
290*da0073e9SAndroid Build Coastguard Worker
291*da0073e9SAndroid Build Coastguard WorkerAll PyTorch test suites are located in the `test` folder and start with
292*da0073e9SAndroid Build Coastguard Worker`test_`. Run the entire test
293*da0073e9SAndroid Build Coastguard Workersuite with
294*da0073e9SAndroid Build Coastguard Worker
295*da0073e9SAndroid Build Coastguard Worker```bash
296*da0073e9SAndroid Build Coastguard Workerpython test/run_test.py
297*da0073e9SAndroid Build Coastguard Worker```
298*da0073e9SAndroid Build Coastguard Worker
299*da0073e9SAndroid Build Coastguard Workeror run individual test suites using the command `python test/FILENAME.py`,
300*da0073e9SAndroid Build Coastguard Workerwhere `FILENAME` represents the file containing the test suite you wish
301*da0073e9SAndroid Build Coastguard Workerto run.
302*da0073e9SAndroid Build Coastguard Worker
303*da0073e9SAndroid Build Coastguard WorkerFor example, to run all the TorchScript JIT tests (located at
304*da0073e9SAndroid Build Coastguard Worker`test/test_jit.py`), you would run:
305*da0073e9SAndroid Build Coastguard Worker
306*da0073e9SAndroid Build Coastguard Worker```bash
307*da0073e9SAndroid Build Coastguard Workerpython test/test_jit.py
308*da0073e9SAndroid Build Coastguard Worker```
309*da0073e9SAndroid Build Coastguard Worker
310*da0073e9SAndroid Build Coastguard WorkerYou can narrow down what you're testing even further by specifying the
311*da0073e9SAndroid Build Coastguard Workername of an individual test with `TESTCLASSNAME.TESTNAME`. Here,
312*da0073e9SAndroid Build Coastguard Worker`TESTNAME` is the name of the test you want to run, and `TESTCLASSNAME`
313*da0073e9SAndroid Build Coastguard Workeris the name of the class in which it is defined.
314*da0073e9SAndroid Build Coastguard Worker
315*da0073e9SAndroid Build Coastguard WorkerGoing off the above example, let's say you want to run
316*da0073e9SAndroid Build Coastguard Worker`test_Sequential`, which is defined as part of the `TestJit` class
317*da0073e9SAndroid Build Coastguard Workerin `test/test_jit.py`. Your command would be:
318*da0073e9SAndroid Build Coastguard Worker
319*da0073e9SAndroid Build Coastguard Worker```bash
320*da0073e9SAndroid Build Coastguard Workerpython test/test_jit.py TestJit.test_Sequential
321*da0073e9SAndroid Build Coastguard Worker```
322*da0073e9SAndroid Build Coastguard Worker
323*da0073e9SAndroid Build Coastguard Worker**Weird note:** In our CI (Continuous Integration) jobs, we actually run the tests from the `test` folder and **not** the root of the repo, since there are various dependencies we set up for CI that expects the tests to be run from the test folder. As such, there may be some inconsistencies between local testing and CI testing--if you observe an inconsistency, please [file an issue](https://github.com/pytorch/pytorch/issues/new/choose).
324*da0073e9SAndroid Build Coastguard Worker
325*da0073e9SAndroid Build Coastguard Worker### Better local unit tests with `pytest`
326*da0073e9SAndroid Build Coastguard Worker
327*da0073e9SAndroid Build Coastguard WorkerWe don't officially support `pytest`, but it works well with our
328*da0073e9SAndroid Build Coastguard Worker`unittest` tests and offers a number of useful features for local
329*da0073e9SAndroid Build Coastguard Workerdeveloping. Install it via `pip install pytest`.
330*da0073e9SAndroid Build Coastguard Worker
331*da0073e9SAndroid Build Coastguard WorkerIf you want to just run tests that contain a specific substring, you can
332*da0073e9SAndroid Build Coastguard Workeruse the `-k` flag:
333*da0073e9SAndroid Build Coastguard Worker
334*da0073e9SAndroid Build Coastguard Worker```bash
335*da0073e9SAndroid Build Coastguard Workerpytest test/test_nn.py -k Loss -v
336*da0073e9SAndroid Build Coastguard Worker```
337*da0073e9SAndroid Build Coastguard Worker
338*da0073e9SAndroid Build Coastguard WorkerThe above is an example of testing a change to all Loss functions: this
339*da0073e9SAndroid Build Coastguard Workercommand runs tests such as `TestNN.test_BCELoss` and
340*da0073e9SAndroid Build Coastguard Worker`TestNN.test_MSELoss` and can be useful to save keystrokes.
341*da0073e9SAndroid Build Coastguard Worker
342*da0073e9SAndroid Build Coastguard Worker### Local linting
343*da0073e9SAndroid Build Coastguard Worker
344*da0073e9SAndroid Build Coastguard WorkerInstall all prerequisites by running
345*da0073e9SAndroid Build Coastguard Worker
346*da0073e9SAndroid Build Coastguard Worker```bash
347*da0073e9SAndroid Build Coastguard Workermake setup-lint
348*da0073e9SAndroid Build Coastguard Worker```
349*da0073e9SAndroid Build Coastguard Worker
350*da0073e9SAndroid Build Coastguard WorkerYou can now run the same linting steps that are used in CI locally via `make`:
351*da0073e9SAndroid Build Coastguard Worker
352*da0073e9SAndroid Build Coastguard Worker```bash
353*da0073e9SAndroid Build Coastguard Workermake lint
354*da0073e9SAndroid Build Coastguard Worker```
355*da0073e9SAndroid Build Coastguard Worker
356*da0073e9SAndroid Build Coastguard WorkerLearn more about the linter on the [lintrunner wiki page](https://github.com/pytorch/pytorch/wiki/lintrunner)
357*da0073e9SAndroid Build Coastguard Worker
358*da0073e9SAndroid Build Coastguard Worker#### Running `mypy`
359*da0073e9SAndroid Build Coastguard Worker
360*da0073e9SAndroid Build Coastguard Worker`mypy` is an optional static type checker for Python. We have multiple `mypy`
361*da0073e9SAndroid Build Coastguard Workerconfigs for the PyTorch codebase that are automatically validated against whenever the linter is run.
362*da0073e9SAndroid Build Coastguard Worker
363*da0073e9SAndroid Build Coastguard WorkerSee [Guide for adding type annotations to
364*da0073e9SAndroid Build Coastguard WorkerPyTorch](https://github.com/pytorch/pytorch/wiki/Guide-for-adding-type-annotations-to-PyTorch)
365*da0073e9SAndroid Build Coastguard Workerfor more information on how to set up `mypy` and tackle type annotation
366*da0073e9SAndroid Build Coastguard Workertasks.
367*da0073e9SAndroid Build Coastguard Worker
368*da0073e9SAndroid Build Coastguard Worker### C++ Unit Testing
369*da0073e9SAndroid Build Coastguard Worker
370*da0073e9SAndroid Build Coastguard WorkerPyTorch offers a series of tests located in the `test/cpp` folder.
371*da0073e9SAndroid Build Coastguard WorkerThese tests are written in C++ and use the Google Test testing framework.
372*da0073e9SAndroid Build Coastguard WorkerAfter compiling PyTorch from source, the test runner binaries will be
373*da0073e9SAndroid Build Coastguard Workerwritten to the `build/bin` folder. The command to run one of these tests
374*da0073e9SAndroid Build Coastguard Workeris `./build/bin/FILENAME --gtest_filter=TESTSUITE.TESTNAME`, where
375*da0073e9SAndroid Build Coastguard Worker`TESTNAME` is the name of the test you'd like to run and `TESTSUITE` is
376*da0073e9SAndroid Build Coastguard Workerthe suite that test is defined in.
377*da0073e9SAndroid Build Coastguard Worker
378*da0073e9SAndroid Build Coastguard WorkerFor example, if you wanted to run the test `MayContainAlias`, which
379*da0073e9SAndroid Build Coastguard Workeris part of the test suite `ContainerAliasingTest` in the file
380*da0073e9SAndroid Build Coastguard Worker`test/cpp/jit/test_alias_analysis.cpp`, the command would be:
381*da0073e9SAndroid Build Coastguard Worker
382*da0073e9SAndroid Build Coastguard Worker```bash
383*da0073e9SAndroid Build Coastguard Worker./build/bin/test_jit --gtest_filter=ContainerAliasingTest.MayContainAlias
384*da0073e9SAndroid Build Coastguard Worker```
385*da0073e9SAndroid Build Coastguard Worker
386*da0073e9SAndroid Build Coastguard Worker
387*da0073e9SAndroid Build Coastguard Worker### Run Specific CI Jobs
388*da0073e9SAndroid Build Coastguard Worker
389*da0073e9SAndroid Build Coastguard WorkerYou can generate a commit that limits the CI to only run a specific job by using
390*da0073e9SAndroid Build Coastguard Worker`tools/testing/explicit_ci_jobs.py` like so:
391*da0073e9SAndroid Build Coastguard Worker
392*da0073e9SAndroid Build Coastguard Worker```bash
393*da0073e9SAndroid Build Coastguard Worker# --job: specify one or more times to filter to a specific job + its dependencies
394*da0073e9SAndroid Build Coastguard Worker# --filter-gha: specify github actions workflows to keep
395*da0073e9SAndroid Build Coastguard Worker# --make-commit: commit CI changes to git with a message explaining the change
396*da0073e9SAndroid Build Coastguard Workerpython tools/testing/explicit_ci_jobs.py --job binary_linux_manywheel_3_6m_cpu_devtoolset7_nightly_test --filter-gha '*generated*gcc5.4*' --make-commit
397*da0073e9SAndroid Build Coastguard Worker
398*da0073e9SAndroid Build Coastguard Worker# Make your changes
399*da0073e9SAndroid Build Coastguard Worker
400*da0073e9SAndroid Build Coastguard Workerghstack submit
401*da0073e9SAndroid Build Coastguard Worker```
402*da0073e9SAndroid Build Coastguard Worker
403*da0073e9SAndroid Build Coastguard Worker**NB**: It is not recommended to use this workflow unless you are also using
404*da0073e9SAndroid Build Coastguard Worker[`ghstack`](https://github.com/ezyang/ghstack). It creates a large commit that is
405*da0073e9SAndroid Build Coastguard Workerof very low signal to reviewers.
406*da0073e9SAndroid Build Coastguard Worker
407*da0073e9SAndroid Build Coastguard Worker## Merging your Change
408*da0073e9SAndroid Build Coastguard WorkerIf you know the right people or team that should approve your PR (and you have the required permissions to do so), add them to the Reviewers list.
409*da0073e9SAndroid Build Coastguard Worker
410*da0073e9SAndroid Build Coastguard WorkerIf not, leave the Reviewers section empty. Our triage squad will review your PR, add a module label, and assign it to the appropriate reviewer in a couple business days.  The reviewer will then look at your PR and respond.
411*da0073e9SAndroid Build Coastguard Worker
412*da0073e9SAndroid Build Coastguard WorkerOccasionally, things might fall through the cracks (sorry!). In case your PR either doesn't get assigned to a reviewer or doesn't get any response from the reviewer for 4 business days, please leave comment on the PR (mentioning the reviewer if one has been assigned). That'll get it nudged back onto people's radar.
413*da0073e9SAndroid Build Coastguard Worker
414*da0073e9SAndroid Build Coastguard WorkerIf that still doesn't help, come see us during [our office hours](https://github.com/pytorch/pytorch/wiki/Dev-Infra-Office-Hours)
415*da0073e9SAndroid Build Coastguard Worker
416*da0073e9SAndroid Build Coastguard WorkerOnce your PR is approved, you can merge it in by entering a comment with the content `@pytorchmergebot merge` ([what's this bot?](https://github.com/pytorch/pytorch/wiki/Bot-commands))
417*da0073e9SAndroid Build Coastguard Worker
418*da0073e9SAndroid Build Coastguard Worker## Writing documentation
419*da0073e9SAndroid Build Coastguard Worker
420*da0073e9SAndroid Build Coastguard WorkerSo you want to write some documentation and don't know where to start?
421*da0073e9SAndroid Build Coastguard WorkerPyTorch has two main types of documentation:
422*da0073e9SAndroid Build Coastguard Worker- **User facing documentation**:
423*da0073e9SAndroid Build Coastguard WorkerThese are the docs that you see over at [our docs website](https://pytorch.org/docs).
424*da0073e9SAndroid Build Coastguard Worker- **Developer facing documentation**:
425*da0073e9SAndroid Build Coastguard WorkerDeveloper facing documentation is spread around our READMEs in our codebase and in
426*da0073e9SAndroid Build Coastguard Workerthe [PyTorch Developer Wiki](https://pytorch.org/wiki).
427*da0073e9SAndroid Build Coastguard WorkerIf you're interested in adding new developer docs, please read this [page on the wiki](https://github.com/pytorch/pytorch/wiki/Where-or-how-should-I-add-documentation) on our best practices for where to put it.
428*da0073e9SAndroid Build Coastguard Worker
429*da0073e9SAndroid Build Coastguard WorkerThe rest of this section is about user-facing documentation.
430*da0073e9SAndroid Build Coastguard Worker
431*da0073e9SAndroid Build Coastguard WorkerPyTorch uses [Google style](https://www.sphinx-doc.org/en/master/usage/extensions/example_google.html)
432*da0073e9SAndroid Build Coastguard Workerfor formatting docstrings. Each line inside a docstrings block must be limited to 80 characters so that it fits into Jupyter documentation popups.
433*da0073e9SAndroid Build Coastguard Worker
434*da0073e9SAndroid Build Coastguard Worker
435*da0073e9SAndroid Build Coastguard Worker### Docstring type formatting
436*da0073e9SAndroid Build Coastguard Worker
437*da0073e9SAndroid Build Coastguard WorkerIn addition to the standard Google Style docstring formatting rules, the following guidelines should be followed for docstring types (docstring types are the type information contained in the round brackets after the variable name):
438*da0073e9SAndroid Build Coastguard Worker
439*da0073e9SAndroid Build Coastguard Worker* The "`Callable`", "`Any`", "`Iterable`", "`Iterator`", "`Generator`" types should have their first letter capitalized.
440*da0073e9SAndroid Build Coastguard Worker
441*da0073e9SAndroid Build Coastguard Worker* The "`list`" and "`tuple`" types should be completely lowercase.
442*da0073e9SAndroid Build Coastguard Worker
443*da0073e9SAndroid Build Coastguard Worker* Types should not be made plural. For example: `tuple of int` should be used instead of `tuple of ints`.
444*da0073e9SAndroid Build Coastguard Worker
445*da0073e9SAndroid Build Coastguard Worker* The only acceptable delimiter words for types are `or` and `of`. No other non-type words should be used other than `optional`.
446*da0073e9SAndroid Build Coastguard Worker
447*da0073e9SAndroid Build Coastguard Worker* The word `optional` should only be used after the types, and it is only used if the user does not have to specify a value for the variable. Default values are listed after the variable description. Example:
448*da0073e9SAndroid Build Coastguard Worker
449*da0073e9SAndroid Build Coastguard Worker    ```
450*da0073e9SAndroid Build Coastguard Worker    my_var (int, optional): Variable description. Default: 1
451*da0073e9SAndroid Build Coastguard Worker    ```
452*da0073e9SAndroid Build Coastguard Worker
453*da0073e9SAndroid Build Coastguard Worker* Basic Python types should match their type name so that the [Intersphinx](https://www.sphinx-doc.org/en/master/usage/extensions/intersphinx.html) extension can correctly identify them. For example:
454*da0073e9SAndroid Build Coastguard Worker    * Use `str` instead of `string`.
455*da0073e9SAndroid Build Coastguard Worker    * Use `bool` instead of `boolean`.
456*da0073e9SAndroid Build Coastguard Worker    * Use `dict` instead of `dictionary`.
457*da0073e9SAndroid Build Coastguard Worker
458*da0073e9SAndroid Build Coastguard Worker* Square brackets should be used for the dictionary type. For example:
459*da0073e9SAndroid Build Coastguard Worker
460*da0073e9SAndroid Build Coastguard Worker    ```
461*da0073e9SAndroid Build Coastguard Worker    my_var (dict[str, int]): Variable description.
462*da0073e9SAndroid Build Coastguard Worker    ```
463*da0073e9SAndroid Build Coastguard Worker
464*da0073e9SAndroid Build Coastguard Worker* If a variable has two different possible types, then the word `or` should be used without a comma. Otherwise variables with 3 or more types should use commas to separate the types. Example:
465*da0073e9SAndroid Build Coastguard Worker
466*da0073e9SAndroid Build Coastguard Worker    ```
467*da0073e9SAndroid Build Coastguard Worker    x (type1 or type2): Variable description.
468*da0073e9SAndroid Build Coastguard Worker    y (type1, type2, or type3): Variable description.
469*da0073e9SAndroid Build Coastguard Worker    ```
470*da0073e9SAndroid Build Coastguard Worker
471*da0073e9SAndroid Build Coastguard Worker
472*da0073e9SAndroid Build Coastguard Worker### Building documentation
473*da0073e9SAndroid Build Coastguard Worker
474*da0073e9SAndroid Build Coastguard WorkerTo build the documentation:
475*da0073e9SAndroid Build Coastguard Worker
476*da0073e9SAndroid Build Coastguard Worker1. Build and install PyTorch
477*da0073e9SAndroid Build Coastguard Worker
478*da0073e9SAndroid Build Coastguard Worker2. Install the prerequisites
479*da0073e9SAndroid Build Coastguard Worker
480*da0073e9SAndroid Build Coastguard Worker```bash
481*da0073e9SAndroid Build Coastguard Workercd docs
482*da0073e9SAndroid Build Coastguard Workerpip install -r requirements.txt
483*da0073e9SAndroid Build Coastguard Worker# `katex` must also be available in your PATH.
484*da0073e9SAndroid Build Coastguard Worker# You can either install katex globally if you have properly configured npm:
485*da0073e9SAndroid Build Coastguard Worker# npm install -g katex
486*da0073e9SAndroid Build Coastguard Worker# Or if you prefer an uncontaminated global executable environment or do not want to go through the node configuration:
487*da0073e9SAndroid Build Coastguard Worker# npm install katex && export PATH="$PATH:$(pwd)/node_modules/.bin"
488*da0073e9SAndroid Build Coastguard Worker```
489*da0073e9SAndroid Build Coastguard Worker> Note: if you installed `nodejs` with a different package manager (e.g.,
490*da0073e9SAndroid Build Coastguard Worker`conda`) then `npm` will probably install a version of `katex` that is not
491*da0073e9SAndroid Build Coastguard Workercompatible with your version of `nodejs` and doc builds will fail.
492*da0073e9SAndroid Build Coastguard WorkerA combination of versions that is known to work is `[email protected]` and
493*da0073e9SAndroid Build Coastguard Worker`[email protected]`. To install the latter with `npm` you can run
494*da0073e9SAndroid Build Coastguard Worker```npm install -g [email protected]```
495*da0073e9SAndroid Build Coastguard Worker
496*da0073e9SAndroid Build Coastguard Worker
497*da0073e9SAndroid Build Coastguard Worker> Note that if you are a Facebook employee using a devserver, yarn may be more convenient to install katex:
498*da0073e9SAndroid Build Coastguard Worker
499*da0073e9SAndroid Build Coastguard Worker```bash
500*da0073e9SAndroid Build Coastguard Workeryarn global add katex
501*da0073e9SAndroid Build Coastguard Worker```
502*da0073e9SAndroid Build Coastguard Worker> If a specific version is required you can use for example `yarn global add [email protected]`.
503*da0073e9SAndroid Build Coastguard Worker
504*da0073e9SAndroid Build Coastguard Worker3. Generate the documentation HTML files. The generated files will be in `docs/build/html`.
505*da0073e9SAndroid Build Coastguard Worker
506*da0073e9SAndroid Build Coastguard Worker```bash
507*da0073e9SAndroid Build Coastguard Workermake html
508*da0073e9SAndroid Build Coastguard Worker```
509*da0073e9SAndroid Build Coastguard Worker
510*da0073e9SAndroid Build Coastguard Worker#### Tips
511*da0073e9SAndroid Build Coastguard Worker
512*da0073e9SAndroid Build Coastguard WorkerThe `.rst` source files live in [docs/source](docs/source). Some of the `.rst`
513*da0073e9SAndroid Build Coastguard Workerfiles pull in docstrings from PyTorch Python code (for example, via
514*da0073e9SAndroid Build Coastguard Workerthe `autofunction` or `autoclass` directives). To vastly shorten doc build times,
515*da0073e9SAndroid Build Coastguard Workerit is helpful to remove the files you are not working on, only keeping the base
516*da0073e9SAndroid Build Coastguard Worker`index.rst` file and the files you are editing. The Sphinx build will produce
517*da0073e9SAndroid Build Coastguard Workermissing file warnings but will still complete. For example, to work on `jit.rst`:
518*da0073e9SAndroid Build Coastguard Worker
519*da0073e9SAndroid Build Coastguard Worker```bash
520*da0073e9SAndroid Build Coastguard Workercd docs/source
521*da0073e9SAndroid Build Coastguard Workerfind . -type f | grep rst | grep -v index | grep -v jit | xargs rm
522*da0073e9SAndroid Build Coastguard Worker
523*da0073e9SAndroid Build Coastguard Worker# Make your changes, build the docs, etc.
524*da0073e9SAndroid Build Coastguard Worker
525*da0073e9SAndroid Build Coastguard Worker# Don't commit the deletions!
526*da0073e9SAndroid Build Coastguard Workergit add index.rst jit.rst
527*da0073e9SAndroid Build Coastguard Worker...
528*da0073e9SAndroid Build Coastguard Worker```
529*da0073e9SAndroid Build Coastguard Worker
530*da0073e9SAndroid Build Coastguard Worker#### Building C++ Documentation
531*da0073e9SAndroid Build Coastguard Worker
532*da0073e9SAndroid Build Coastguard WorkerFor C++ documentation (https://pytorch.org/cppdocs), we use
533*da0073e9SAndroid Build Coastguard Worker[Doxygen](http://www.doxygen.nl/) and then convert it to
534*da0073e9SAndroid Build Coastguard Worker[Sphinx](http://www.sphinx-doc.org/) via
535*da0073e9SAndroid Build Coastguard Worker[Breathe](https://github.com/michaeljones/breathe) and
536*da0073e9SAndroid Build Coastguard Worker[Exhale](https://github.com/svenevs/exhale). Check the [Doxygen
537*da0073e9SAndroid Build Coastguard Workerreference](https://www.doxygen.nl/manual/) for more
538*da0073e9SAndroid Build Coastguard Workerinformation on the documentation syntax.
539*da0073e9SAndroid Build Coastguard Worker
540*da0073e9SAndroid Build Coastguard WorkerWe run Doxygen in CI (Travis) to verify that you do not use invalid Doxygen
541*da0073e9SAndroid Build Coastguard Workercommands. To run this check locally, run `./check-doxygen.sh` from inside
542*da0073e9SAndroid Build Coastguard Worker`docs/cpp/source`.
543*da0073e9SAndroid Build Coastguard Worker
544*da0073e9SAndroid Build Coastguard WorkerTo build the documentation, follow the same steps as above, but run them from
545*da0073e9SAndroid Build Coastguard Worker`docs/cpp` instead of `docs`.
546*da0073e9SAndroid Build Coastguard Worker
547*da0073e9SAndroid Build Coastguard Worker### Previewing changes locally
548*da0073e9SAndroid Build Coastguard Worker
549*da0073e9SAndroid Build Coastguard WorkerTo view HTML files locally, you can open the files in your web browser. For example,
550*da0073e9SAndroid Build Coastguard Workernavigate to `file:///your_pytorch_folder/docs/build/html/index.html` in a web
551*da0073e9SAndroid Build Coastguard Workerbrowser.
552*da0073e9SAndroid Build Coastguard Worker
553*da0073e9SAndroid Build Coastguard WorkerIf you are developing on a remote machine, you can set up an SSH tunnel so that
554*da0073e9SAndroid Build Coastguard Workeryou can access the HTTP server on the remote machine from your local machine. To map
555*da0073e9SAndroid Build Coastguard Workerremote port 8000 to local port 8000, use either of the following commands.
556*da0073e9SAndroid Build Coastguard Worker
557*da0073e9SAndroid Build Coastguard Worker```bash
558*da0073e9SAndroid Build Coastguard Worker# For SSH
559*da0073e9SAndroid Build Coastguard Workerssh my_machine -L 8000:my_machine:8000
560*da0073e9SAndroid Build Coastguard Worker
561*da0073e9SAndroid Build Coastguard Worker# For Eternal Terminal
562*da0073e9SAndroid Build Coastguard Workeret my_machine -t="8000:8000"
563*da0073e9SAndroid Build Coastguard Worker```
564*da0073e9SAndroid Build Coastguard Worker
565*da0073e9SAndroid Build Coastguard WorkerThen navigate to `localhost:8000` in your web browser.
566*da0073e9SAndroid Build Coastguard Worker
567*da0073e9SAndroid Build Coastguard Worker**Tip:**
568*da0073e9SAndroid Build Coastguard WorkerYou can start a lightweight HTTP server on the remote machine with:
569*da0073e9SAndroid Build Coastguard Worker
570*da0073e9SAndroid Build Coastguard Worker```bash
571*da0073e9SAndroid Build Coastguard Workerpython -m http.server 8000 <path_to_html_output>
572*da0073e9SAndroid Build Coastguard Worker```
573*da0073e9SAndroid Build Coastguard Worker
574*da0073e9SAndroid Build Coastguard WorkerAlternatively, you can run `rsync` on your local machine to copy the files from
575*da0073e9SAndroid Build Coastguard Workeryour remote machine:
576*da0073e9SAndroid Build Coastguard Worker
577*da0073e9SAndroid Build Coastguard Worker```bash
578*da0073e9SAndroid Build Coastguard Workermkdir -p build cpp/build
579*da0073e9SAndroid Build Coastguard Workerrsync -az me@my_machine:/path/to/pytorch/docs/build/html build
580*da0073e9SAndroid Build Coastguard Workerrsync -az me@my_machine:/path/to/pytorch/docs/cpp/build/html cpp/build
581*da0073e9SAndroid Build Coastguard Worker```
582*da0073e9SAndroid Build Coastguard Worker
583*da0073e9SAndroid Build Coastguard Worker### Previewing documentation on PRs
584*da0073e9SAndroid Build Coastguard Worker
585*da0073e9SAndroid Build Coastguard WorkerPyTorch will host documentation previews at `https://docs-preview.pytorch.org/pytorch/pytorch/<pr number>/index.html` once the
586*da0073e9SAndroid Build Coastguard Worker`pytorch_python_doc_build` GitHub Actions job has completed on your PR. You can visit that page directly
587*da0073e9SAndroid Build Coastguard Workeror find its link in the automated Dr. CI comment on your PR.
588*da0073e9SAndroid Build Coastguard Worker
589*da0073e9SAndroid Build Coastguard Worker### Adding documentation tests
590*da0073e9SAndroid Build Coastguard Worker
591*da0073e9SAndroid Build Coastguard WorkerIt is easy for code snippets in docstrings and `.rst` files to get out of date. The docs
592*da0073e9SAndroid Build Coastguard Workerbuild includes the [Sphinx Doctest Extension](https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html),
593*da0073e9SAndroid Build Coastguard Workerwhich can run code in documentation as a unit test. To use the extension, use
594*da0073e9SAndroid Build Coastguard Workerthe `.. testcode::` directive in your `.rst` and docstrings.
595*da0073e9SAndroid Build Coastguard Worker
596*da0073e9SAndroid Build Coastguard WorkerTo manually run these tests, follow steps 1 and 2 above, then run:
597*da0073e9SAndroid Build Coastguard Worker
598*da0073e9SAndroid Build Coastguard Worker```bash
599*da0073e9SAndroid Build Coastguard Workercd docs
600*da0073e9SAndroid Build Coastguard Workermake doctest
601*da0073e9SAndroid Build Coastguard Worker```
602*da0073e9SAndroid Build Coastguard Worker
603*da0073e9SAndroid Build Coastguard Worker## Profiling with `py-spy`
604*da0073e9SAndroid Build Coastguard Worker
605*da0073e9SAndroid Build Coastguard WorkerEvaluating the performance impact of code changes in PyTorch can be complicated,
606*da0073e9SAndroid Build Coastguard Workerparticularly if code changes happen in compiled code. One simple way to profile
607*da0073e9SAndroid Build Coastguard Workerboth Python and C++ code in PyTorch is to use
608*da0073e9SAndroid Build Coastguard Worker[`py-spy`](https://github.com/benfred/py-spy), a sampling profiler for Python
609*da0073e9SAndroid Build Coastguard Workerthat has the ability to profile native code and Python code in the same session.
610*da0073e9SAndroid Build Coastguard Worker
611*da0073e9SAndroid Build Coastguard Worker`py-spy` can be installed via `pip`:
612*da0073e9SAndroid Build Coastguard Worker
613*da0073e9SAndroid Build Coastguard Worker```bash
614*da0073e9SAndroid Build Coastguard Workerpip install py-spy
615*da0073e9SAndroid Build Coastguard Worker```
616*da0073e9SAndroid Build Coastguard Worker
617*da0073e9SAndroid Build Coastguard WorkerTo use `py-spy`, first write a Python test script that exercises the
618*da0073e9SAndroid Build Coastguard Workerfunctionality you would like to profile. For example, this script profiles
619*da0073e9SAndroid Build Coastguard Worker`torch.add`:
620*da0073e9SAndroid Build Coastguard Worker
621*da0073e9SAndroid Build Coastguard Worker```python
622*da0073e9SAndroid Build Coastguard Workerimport torch
623*da0073e9SAndroid Build Coastguard Worker
624*da0073e9SAndroid Build Coastguard Workert1 = torch.tensor([[1, 1], [1, 1.]])
625*da0073e9SAndroid Build Coastguard Workert2 = torch.tensor([[0, 0], [0, 0.]])
626*da0073e9SAndroid Build Coastguard Worker
627*da0073e9SAndroid Build Coastguard Workerfor _ in range(1000000):
628*da0073e9SAndroid Build Coastguard Worker    torch.add(t1, t2)
629*da0073e9SAndroid Build Coastguard Worker```
630*da0073e9SAndroid Build Coastguard Worker
631*da0073e9SAndroid Build Coastguard WorkerSince the `torch.add` operation happens in microseconds, we repeat it a large
632*da0073e9SAndroid Build Coastguard Workernumber of times to get good statistics. The most straightforward way to use
633*da0073e9SAndroid Build Coastguard Worker`py-spy` with such a script is to generate a [flame
634*da0073e9SAndroid Build Coastguard Workergraph](http://www.brendangregg.com/flamegraphs.html):
635*da0073e9SAndroid Build Coastguard Worker
636*da0073e9SAndroid Build Coastguard Worker```bash
637*da0073e9SAndroid Build Coastguard Workerpy-spy record -o profile.svg --native -- python test_tensor_tensor_add.py
638*da0073e9SAndroid Build Coastguard Worker```
639*da0073e9SAndroid Build Coastguard Worker
640*da0073e9SAndroid Build Coastguard WorkerThis will output a file named `profile.svg` containing a flame graph you can
641*da0073e9SAndroid Build Coastguard Workerview in a web browser or SVG viewer. Individual stack frame entries in the graph
642*da0073e9SAndroid Build Coastguard Workercan be selected interactively with your mouse to zoom in on a particular part of
643*da0073e9SAndroid Build Coastguard Workerthe program execution timeline. The `--native` command-line option tells
644*da0073e9SAndroid Build Coastguard Worker`py-spy` to record stack frame entries for PyTorch C++ code. To get line numbers
645*da0073e9SAndroid Build Coastguard Workerfor C++ code it may be necessary to compile PyTorch in debug mode by prepending
646*da0073e9SAndroid Build Coastguard Workeryour `setup.py develop` call to compile PyTorch with `DEBUG=1`. Depending on
647*da0073e9SAndroid Build Coastguard Workeryour operating system it may also be necessary to run `py-spy` with root
648*da0073e9SAndroid Build Coastguard Workerprivileges.
649*da0073e9SAndroid Build Coastguard Worker
650*da0073e9SAndroid Build Coastguard Worker`py-spy` can also work in an `htop`-like "live profiling" mode and can be
651*da0073e9SAndroid Build Coastguard Workertweaked to adjust the stack sampling rate, see the `py-spy` readme for more
652*da0073e9SAndroid Build Coastguard Workerdetails.
653*da0073e9SAndroid Build Coastguard Worker
654*da0073e9SAndroid Build Coastguard Worker## Managing multiple build trees
655*da0073e9SAndroid Build Coastguard Worker
656*da0073e9SAndroid Build Coastguard WorkerOne downside to using `python setup.py develop` is that your development
657*da0073e9SAndroid Build Coastguard Workerversion of PyTorch will be installed globally on your account (e.g., if
658*da0073e9SAndroid Build Coastguard Workeryou run `import torch` anywhere else, the development version will be
659*da0073e9SAndroid Build Coastguard Workerused.
660*da0073e9SAndroid Build Coastguard Worker
661*da0073e9SAndroid Build Coastguard WorkerIf you want to manage multiple builds of PyTorch, you can make use of
662*da0073e9SAndroid Build Coastguard Worker[conda environments](https://conda.io/docs/using/envs.html) to maintain
663*da0073e9SAndroid Build Coastguard Workerseparate Python package environments, each of which can be tied to a
664*da0073e9SAndroid Build Coastguard Workerspecific build of PyTorch. To set one up:
665*da0073e9SAndroid Build Coastguard Worker
666*da0073e9SAndroid Build Coastguard Worker```bash
667*da0073e9SAndroid Build Coastguard Workerconda create -n pytorch-myfeature
668*da0073e9SAndroid Build Coastguard Workersource activate pytorch-myfeature
669*da0073e9SAndroid Build Coastguard Worker# if you run python now, torch will NOT be installed
670*da0073e9SAndroid Build Coastguard Workerpython setup.py develop
671*da0073e9SAndroid Build Coastguard Worker```
672*da0073e9SAndroid Build Coastguard Worker
673*da0073e9SAndroid Build Coastguard Worker## C++ development tips
674*da0073e9SAndroid Build Coastguard Worker
675*da0073e9SAndroid Build Coastguard WorkerIf you are working on the C++ code, there are a few important things that you
676*da0073e9SAndroid Build Coastguard Workerwill want to keep in mind:
677*da0073e9SAndroid Build Coastguard Worker
678*da0073e9SAndroid Build Coastguard Worker1. How to rebuild only the code you are working on.
679*da0073e9SAndroid Build Coastguard Worker2. How to make rebuilds in the absence of changes go faster.
680*da0073e9SAndroid Build Coastguard Worker
681*da0073e9SAndroid Build Coastguard Worker### Build only what you need
682*da0073e9SAndroid Build Coastguard Worker
683*da0073e9SAndroid Build Coastguard Worker`python setup.py build` will build everything by default, but sometimes you are
684*da0073e9SAndroid Build Coastguard Workeronly interested in a specific component.
685*da0073e9SAndroid Build Coastguard Worker
686*da0073e9SAndroid Build Coastguard Worker- Working on a test binary? Run `(cd build && ninja bin/test_binary_name)` to
687*da0073e9SAndroid Build Coastguard Worker  rebuild only that test binary (without rerunning cmake). (Replace `ninja` with
688*da0073e9SAndroid Build Coastguard Worker  `make` if you don't have ninja installed).
689*da0073e9SAndroid Build Coastguard Worker
690*da0073e9SAndroid Build Coastguard WorkerOn the initial build, you can also speed things up with the environment
691*da0073e9SAndroid Build Coastguard Workervariables `DEBUG`, `USE_DISTRIBUTED`, `USE_MKLDNN`, `USE_CUDA`, `USE_FLASH_ATTENTION`, `USE_MEM_EFF_ATTENTION`, `BUILD_TEST`, `USE_FBGEMM`, `USE_NNPACK` and `USE_QNNPACK`.
692*da0073e9SAndroid Build Coastguard Worker
693*da0073e9SAndroid Build Coastguard Worker- `DEBUG=1` will enable debug builds (-g -O0)
694*da0073e9SAndroid Build Coastguard Worker- `REL_WITH_DEB_INFO=1` will enable debug symbols with optimizations (-g -O3)
695*da0073e9SAndroid Build Coastguard Worker- `USE_DISTRIBUTED=0` will disable distributed (c10d, gloo, mpi, etc.) build.
696*da0073e9SAndroid Build Coastguard Worker- `USE_MKLDNN=0` will disable using MKL-DNN.
697*da0073e9SAndroid Build Coastguard Worker- `USE_CUDA=0` will disable compiling CUDA (in case you are developing on something not CUDA related), to save compile time.
698*da0073e9SAndroid Build Coastguard Worker- `BUILD_TEST=0` will disable building C++ test binaries.
699*da0073e9SAndroid Build Coastguard Worker- `USE_FBGEMM=0` will disable using FBGEMM (quantized 8-bit server operators).
700*da0073e9SAndroid Build Coastguard Worker- `USE_NNPACK=0` will disable compiling with NNPACK.
701*da0073e9SAndroid Build Coastguard Worker- `USE_QNNPACK=0` will disable QNNPACK build (quantized 8-bit operators).
702*da0073e9SAndroid Build Coastguard Worker- `USE_XNNPACK=0` will disable compiling with XNNPACK.
703*da0073e9SAndroid Build Coastguard Worker- `USE_FLASH_ATTENTION=0` and `USE_MEM_EFF_ATTENTION=0` will disable compiling flash attention and memory efficient kernels respectively
704*da0073e9SAndroid Build Coastguard Worker
705*da0073e9SAndroid Build Coastguard WorkerFor example:
706*da0073e9SAndroid Build Coastguard Worker
707*da0073e9SAndroid Build Coastguard Worker```bash
708*da0073e9SAndroid Build Coastguard WorkerDEBUG=1 USE_DISTRIBUTED=0 USE_MKLDNN=0 USE_CUDA=0 BUILD_TEST=0 USE_FBGEMM=0 USE_NNPACK=0 USE_QNNPACK=0 USE_XNNPACK=0 python setup.py develop
709*da0073e9SAndroid Build Coastguard Worker```
710*da0073e9SAndroid Build Coastguard Worker
711*da0073e9SAndroid Build Coastguard WorkerFor subsequent builds (i.e., when `build/CMakeCache.txt` exists), the build
712*da0073e9SAndroid Build Coastguard Workeroptions passed for the first time will persist; please run `ccmake build/`, run
713*da0073e9SAndroid Build Coastguard Worker`cmake-gui build/`, or directly edit `build/CMakeCache.txt` to adapt build
714*da0073e9SAndroid Build Coastguard Workeroptions.
715*da0073e9SAndroid Build Coastguard Worker
716*da0073e9SAndroid Build Coastguard Worker### Code completion and IDE support
717*da0073e9SAndroid Build Coastguard Worker
718*da0073e9SAndroid Build Coastguard WorkerWhen using `python setup.py develop`, PyTorch will generate
719*da0073e9SAndroid Build Coastguard Workera `compile_commands.json` file that can be used by many editors
720*da0073e9SAndroid Build Coastguard Workerto provide command completion and error highlighting for PyTorch's
721*da0073e9SAndroid Build Coastguard WorkerC++ code. You need to `pip install ninja` to generate accurate
722*da0073e9SAndroid Build Coastguard Workerinformation for the code in `torch/csrc`. More information at:
723*da0073e9SAndroid Build Coastguard Worker- https://sarcasm.github.io/notes/dev/compilation-database.html
724*da0073e9SAndroid Build Coastguard Worker
725*da0073e9SAndroid Build Coastguard Worker### Make no-op build fast
726*da0073e9SAndroid Build Coastguard Worker
727*da0073e9SAndroid Build Coastguard Worker#### Use Ninja
728*da0073e9SAndroid Build Coastguard Worker
729*da0073e9SAndroid Build Coastguard WorkerBy default, cmake will use its Makefile generator to generate your build
730*da0073e9SAndroid Build Coastguard Workersystem.  You can get faster builds if you install the ninja build system
731*da0073e9SAndroid Build Coastguard Workerwith `pip install ninja`.  If PyTorch was already built, you will need
732*da0073e9SAndroid Build Coastguard Workerto run `python setup.py clean` once after installing ninja for builds to
733*da0073e9SAndroid Build Coastguard Workersucceed.
734*da0073e9SAndroid Build Coastguard Worker
735*da0073e9SAndroid Build Coastguard WorkerNote: Make sure to use a machine with a larger number of CPU cores, this will significantly reduce your build times.
736*da0073e9SAndroid Build Coastguard Worker
737*da0073e9SAndroid Build Coastguard Worker#### Use CCache
738*da0073e9SAndroid Build Coastguard Worker
739*da0073e9SAndroid Build Coastguard WorkerEven when dependencies are tracked with file modification, there are many
740*da0073e9SAndroid Build Coastguard Workersituations where files get rebuilt when a previous compilation was exactly the
741*da0073e9SAndroid Build Coastguard Workersame. Using ccache in a situation like this is a real time-saver.
742*da0073e9SAndroid Build Coastguard Worker
743*da0073e9SAndroid Build Coastguard WorkerBefore building pytorch, install ccache from your package manager of choice:
744*da0073e9SAndroid Build Coastguard Worker
745*da0073e9SAndroid Build Coastguard Worker```bash
746*da0073e9SAndroid Build Coastguard Workerconda install ccache -c conda-forge
747*da0073e9SAndroid Build Coastguard Workersudo apt install ccache
748*da0073e9SAndroid Build Coastguard Workersudo yum install ccache
749*da0073e9SAndroid Build Coastguard Workerbrew install ccache
750*da0073e9SAndroid Build Coastguard Worker```
751*da0073e9SAndroid Build Coastguard Worker
752*da0073e9SAndroid Build Coastguard WorkerYou may also find the default cache size in ccache is too small to be useful.
753*da0073e9SAndroid Build Coastguard WorkerThe cache sizes can be increased from the command line:
754*da0073e9SAndroid Build Coastguard Worker
755*da0073e9SAndroid Build Coastguard Worker```bash
756*da0073e9SAndroid Build Coastguard Worker# config: cache dir is ~/.ccache, conf file ~/.ccache/ccache.conf
757*da0073e9SAndroid Build Coastguard Worker# max size of cache
758*da0073e9SAndroid Build Coastguard Workerccache -M 25Gi  # -M 0 for unlimited
759*da0073e9SAndroid Build Coastguard Worker# unlimited number of files
760*da0073e9SAndroid Build Coastguard Workerccache -F 0
761*da0073e9SAndroid Build Coastguard Worker```
762*da0073e9SAndroid Build Coastguard Worker
763*da0073e9SAndroid Build Coastguard WorkerTo check this is working, do two clean builds of pytorch in a row. The second
764*da0073e9SAndroid Build Coastguard Workerbuild should be substantially and noticeably faster than the first build. If
765*da0073e9SAndroid Build Coastguard Workerthis doesn't seem to be the case, check the `CMAKE_<LANG>_COMPILER_LAUNCHER`
766*da0073e9SAndroid Build Coastguard Workerrules in `build/CMakeCache.txt`, where `<LANG>` is `C`, `CXX` and `CUDA`.
767*da0073e9SAndroid Build Coastguard WorkerEach of these 3 variables should contain ccache, e.g.
768*da0073e9SAndroid Build Coastguard Worker
769*da0073e9SAndroid Build Coastguard Worker```
770*da0073e9SAndroid Build Coastguard Worker//CXX compiler launcher
771*da0073e9SAndroid Build Coastguard WorkerCMAKE_CXX_COMPILER_LAUNCHER:STRING=/usr/bin/ccache
772*da0073e9SAndroid Build Coastguard Worker```
773*da0073e9SAndroid Build Coastguard Worker
774*da0073e9SAndroid Build Coastguard WorkerIf not, you can define these variables on the command line before invoking `setup.py`.
775*da0073e9SAndroid Build Coastguard Worker
776*da0073e9SAndroid Build Coastguard Worker```bash
777*da0073e9SAndroid Build Coastguard Workerexport CMAKE_C_COMPILER_LAUNCHER=ccache
778*da0073e9SAndroid Build Coastguard Workerexport CMAKE_CXX_COMPILER_LAUNCHER=ccache
779*da0073e9SAndroid Build Coastguard Workerexport CMAKE_CUDA_COMPILER_LAUNCHER=ccache
780*da0073e9SAndroid Build Coastguard Workerpython setup.py develop
781*da0073e9SAndroid Build Coastguard Worker```
782*da0073e9SAndroid Build Coastguard Worker
783*da0073e9SAndroid Build Coastguard Worker#### Use a faster linker
784*da0073e9SAndroid Build Coastguard Worker
785*da0073e9SAndroid Build Coastguard WorkerIf you are editing a single file and rebuilding in a tight loop, the time spent
786*da0073e9SAndroid Build Coastguard Workerlinking will dominate. The system linker available in most Linux distributions
787*da0073e9SAndroid Build Coastguard Worker(GNU `ld`) is quite slow. Use a faster linker, like [lld](https://lld.llvm.org/).
788*da0073e9SAndroid Build Coastguard Worker
789*da0073e9SAndroid Build Coastguard WorkerPeople on Mac, follow [this guide](https://stackoverflow.com/questions/42730345/how-to-install-llvm-for-mac) instead.
790*da0073e9SAndroid Build Coastguard Worker
791*da0073e9SAndroid Build Coastguard WorkerThe easiest way to use `lld` this is download the
792*da0073e9SAndroid Build Coastguard Worker[latest LLVM binaries](http://releases.llvm.org/download.html#8.0.0) and run:
793*da0073e9SAndroid Build Coastguard Worker
794*da0073e9SAndroid Build Coastguard Worker```bash
795*da0073e9SAndroid Build Coastguard Workerln -s /path/to/downloaded/ld.lld /usr/local/bin/ld
796*da0073e9SAndroid Build Coastguard Worker```
797*da0073e9SAndroid Build Coastguard Worker
798*da0073e9SAndroid Build Coastguard Worker#### Use pre-compiled headers
799*da0073e9SAndroid Build Coastguard Worker
800*da0073e9SAndroid Build Coastguard WorkerSometimes there's no way of getting around rebuilding lots of files, for example
801*da0073e9SAndroid Build Coastguard Workerediting `native_functions.yaml` usually means 1000+ files being rebuilt. If
802*da0073e9SAndroid Build Coastguard Workeryou're using CMake newer than 3.16, you can enable pre-compiled headers by
803*da0073e9SAndroid Build Coastguard Workersetting `USE_PRECOMPILED_HEADERS=1` either on first setup, or in the
804*da0073e9SAndroid Build Coastguard Worker`CMakeCache.txt` file.
805*da0073e9SAndroid Build Coastguard Worker
806*da0073e9SAndroid Build Coastguard Worker```sh
807*da0073e9SAndroid Build Coastguard WorkerUSE_PRECOMPILED_HEADERS=1 python setup.py develop
808*da0073e9SAndroid Build Coastguard Worker```
809*da0073e9SAndroid Build Coastguard Worker
810*da0073e9SAndroid Build Coastguard WorkerThis adds a build step where the compiler takes `<ATen/ATen.h>` and essentially
811*da0073e9SAndroid Build Coastguard Workerdumps its internal AST to a file so the compiler can avoid repeating itself for
812*da0073e9SAndroid Build Coastguard Workerevery `.cpp` file.
813*da0073e9SAndroid Build Coastguard Worker
814*da0073e9SAndroid Build Coastguard WorkerOne caveat is that when enabled, this header gets included in every file by default.
815*da0073e9SAndroid Build Coastguard WorkerWhich may change what code is legal, for example:
816*da0073e9SAndroid Build Coastguard Worker- internal functions can never alias existing names in `<ATen/ATen.h>`
817*da0073e9SAndroid Build Coastguard Worker- names in `<ATen/ATen.h>` will work even if you don't explicitly include it.
818*da0073e9SAndroid Build Coastguard Worker
819*da0073e9SAndroid Build Coastguard Worker#### Workaround for header dependency bug in nvcc
820*da0073e9SAndroid Build Coastguard WorkerIf re-building without modifying any files results in several CUDA files being
821*da0073e9SAndroid Build Coastguard Workerre-compiled, you may be running into an `nvcc` bug where header dependencies are
822*da0073e9SAndroid Build Coastguard Workernot converted to absolute paths before reporting it to the build system. This
823*da0073e9SAndroid Build Coastguard Workermakes `ninja` think one of the header files has been deleted, so it runs the
824*da0073e9SAndroid Build Coastguard Workerbuild again.
825*da0073e9SAndroid Build Coastguard Worker
826*da0073e9SAndroid Build Coastguard WorkerA compiler-wrapper to fix this is provided in `tools/nvcc_fix_deps.py`. You can use
827*da0073e9SAndroid Build Coastguard Workerthis as a compiler launcher, similar to `ccache`
828*da0073e9SAndroid Build Coastguard Worker```bash
829*da0073e9SAndroid Build Coastguard Workerexport CMAKE_CUDA_COMPILER_LAUNCHER="python;`pwd`/tools/nvcc_fix_deps.py;ccache"
830*da0073e9SAndroid Build Coastguard Workerpython setup.py develop
831*da0073e9SAndroid Build Coastguard Worker```
832*da0073e9SAndroid Build Coastguard Worker
833*da0073e9SAndroid Build Coastguard Worker### Rebuild few files with debug information
834*da0073e9SAndroid Build Coastguard Worker
835*da0073e9SAndroid Build Coastguard WorkerWhile debugging a problem one often had to maintain a debug build in a separate folder.
836*da0073e9SAndroid Build Coastguard WorkerBut often only a few files needs to be rebuild with debug info to get a symbolicated backtrace or enable source debugging
837*da0073e9SAndroid Build Coastguard WorkerOne can easily solve this with the help of `tools/build_with_debinfo.py`
838*da0073e9SAndroid Build Coastguard Worker
839*da0073e9SAndroid Build Coastguard WorkerFor example, suppose one wants to debug what is going on while tensor index is selected, which can be achieved by setting a breakpoint at `applySelect` function:
840*da0073e9SAndroid Build Coastguard Worker```
841*da0073e9SAndroid Build Coastguard Worker% lldb -o "b applySelect" -o "process launch" -- python3 -c "import torch;print(torch.rand(5)[3])"
842*da0073e9SAndroid Build Coastguard Worker(lldb) target create "python"
843*da0073e9SAndroid Build Coastguard WorkerCurrent executable set to '/usr/bin/python3' (arm64).
844*da0073e9SAndroid Build Coastguard Worker(lldb) settings set -- target.run-args  "-c" "import torch;print(torch.rand(5)[3])"
845*da0073e9SAndroid Build Coastguard Worker(lldb) b applySelect
846*da0073e9SAndroid Build Coastguard WorkerBreakpoint 1: no locations (pending).
847*da0073e9SAndroid Build Coastguard WorkerWARNING:  Unable to resolve breakpoint to any actual locations.
848*da0073e9SAndroid Build Coastguard Worker(lldb) process launch
849*da0073e9SAndroid Build Coastguard Worker2 locations added to breakpoint 1
850*da0073e9SAndroid Build Coastguard WorkerProcess 87729 stopped
851*da0073e9SAndroid Build Coastguard Worker* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
852*da0073e9SAndroid Build Coastguard Worker    frame #0: 0x00000001023d55a8 libtorch_python.dylib`at::indexing::impl::applySelect(at::Tensor const&, long long, c10::SymInt, long long, c10::Device const&, std::__1::optional<c10::ArrayRef<c10::SymInt>> const&)
853*da0073e9SAndroid Build Coastguard Workerlibtorch_python.dylib`at::indexing::impl::applySelect:
854*da0073e9SAndroid Build Coastguard Worker->  0x1023d55a8 <+0>:  sub    sp, sp, #0xd0
855*da0073e9SAndroid Build Coastguard Worker    0x1023d55ac <+4>:  stp    x24, x23, [sp, #0x90]
856*da0073e9SAndroid Build Coastguard Worker    0x1023d55b0 <+8>:  stp    x22, x21, [sp, #0xa0]
857*da0073e9SAndroid Build Coastguard Worker    0x1023d55b4 <+12>: stp    x20, x19, [sp, #0xb0]
858*da0073e9SAndroid Build Coastguard WorkerTarget 0: (python) stopped.
859*da0073e9SAndroid Build Coastguard WorkerProcess 87729 launched: '/usr/bin/python' (arm64)
860*da0073e9SAndroid Build Coastguard Worker```
861*da0073e9SAndroid Build Coastguard WorkerWhich is not very informative, but can be easily remedied by rebuilding `python_variable_indexing.cpp` with debug information
862*da0073e9SAndroid Build Coastguard Worker```
863*da0073e9SAndroid Build Coastguard Worker% ./tools/build_with_debinfo.py torch/csrc/autograd/python_variable_indexing.cpp
864*da0073e9SAndroid Build Coastguard Worker[1 / 2] Building caffe2/torch/CMakeFiles/torch_python.dir/csrc/autograd/python_variable_indexing.cpp.o
865*da0073e9SAndroid Build Coastguard Worker[2 / 2] Building lib/libtorch_python.dylib
866*da0073e9SAndroid Build Coastguard Worker```
867*da0073e9SAndroid Build Coastguard WorkerAnd afterwards:
868*da0073e9SAndroid Build Coastguard Worker```
869*da0073e9SAndroid Build Coastguard Worker% lldb -o "b applySelect" -o "process launch" -- python3 -c "import torch;print(torch.rand(5)[3])"
870*da0073e9SAndroid Build Coastguard Worker(lldb) target create "python"
871*da0073e9SAndroid Build Coastguard WorkerCurrent executable set to '/usr/bin/python3' (arm64).
872*da0073e9SAndroid Build Coastguard Worker(lldb) settings set -- target.run-args  "-c" "import torch;print(torch.rand(5)[3])"
873*da0073e9SAndroid Build Coastguard Worker(lldb) b applySelect
874*da0073e9SAndroid Build Coastguard WorkerBreakpoint 1: no locations (pending).
875*da0073e9SAndroid Build Coastguard WorkerWARNING:  Unable to resolve breakpoint to any actual locations.
876*da0073e9SAndroid Build Coastguard Worker(lldb) process launch
877*da0073e9SAndroid Build Coastguard Worker2 locations added to breakpoint 1
878*da0073e9SAndroid Build Coastguard WorkerProcess 87741 stopped
879*da0073e9SAndroid Build Coastguard Worker* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
880*da0073e9SAndroid Build Coastguard Worker    frame #0: 0x00000001024e2628 libtorch_python.dylib`at::indexing::impl::applySelect(self=0x00000001004ee8a8, dim=0, index=(data_ = 3), real_dim=0, (null)=0x000000016fdfe535, self_sizes= Has Value=true ) at TensorIndexing.h:239:7
881*da0073e9SAndroid Build Coastguard Worker   236         const at::Device& /*self_device*/,
882*da0073e9SAndroid Build Coastguard Worker   237         const c10::optional<SymIntArrayRef>& self_sizes) {
883*da0073e9SAndroid Build Coastguard Worker   238       // See NOTE [nested tensor size for indexing]
884*da0073e9SAndroid Build Coastguard Worker-> 239       if (self_sizes.has_value()) {
885*da0073e9SAndroid Build Coastguard Worker   240         auto maybe_index = index.maybe_as_int();
886*da0073e9SAndroid Build Coastguard Worker   241         if (maybe_index.has_value()) {
887*da0073e9SAndroid Build Coastguard Worker   242           TORCH_CHECK_INDEX(
888*da0073e9SAndroid Build Coastguard WorkerTarget 0: (python) stopped.
889*da0073e9SAndroid Build Coastguard WorkerProcess 87741 launched: '/usr/bin/python3' (arm64)
890*da0073e9SAndroid Build Coastguard Worker```
891*da0073e9SAndroid Build Coastguard WorkerWhich is much more useful, isn't it?
892*da0073e9SAndroid Build Coastguard Worker
893*da0073e9SAndroid Build Coastguard Worker### C++ frontend development tips
894*da0073e9SAndroid Build Coastguard Worker
895*da0073e9SAndroid Build Coastguard WorkerWe have very extensive tests in the [test/cpp/api](test/cpp/api) folder. The
896*da0073e9SAndroid Build Coastguard Workertests are a great way to see how certain components are intended to be used.
897*da0073e9SAndroid Build Coastguard WorkerWhen compiling PyTorch from source, the test runner binary will be written to
898*da0073e9SAndroid Build Coastguard Worker`build/bin/test_api`. The tests use the [GoogleTest](https://github.com/google/googletest/blob/master/googletest)
899*da0073e9SAndroid Build Coastguard Workerframework, which you can read up about to learn how to configure the test runner. When
900*da0073e9SAndroid Build Coastguard Workersubmitting a new feature, we care very much that you write appropriate tests.
901*da0073e9SAndroid Build Coastguard WorkerPlease follow the lead of the other tests to see how to write a new test case.
902*da0073e9SAndroid Build Coastguard Worker
903*da0073e9SAndroid Build Coastguard Worker### GDB integration
904*da0073e9SAndroid Build Coastguard Worker
905*da0073e9SAndroid Build Coastguard WorkerIf you are debugging pytorch inside GDB, you might be interested in
906*da0073e9SAndroid Build Coastguard Worker[pytorch-gdb](tools/gdb/pytorch-gdb.py). This script introduces some
907*da0073e9SAndroid Build Coastguard Workerpytorch-specific commands which you can use from the GDB prompt. In
908*da0073e9SAndroid Build Coastguard Workerparticular, `torch-tensor-repr` prints a human-readable repr of an at::Tensor
909*da0073e9SAndroid Build Coastguard Workerobject. Example of usage:
910*da0073e9SAndroid Build Coastguard Worker
911*da0073e9SAndroid Build Coastguard Worker```
912*da0073e9SAndroid Build Coastguard Worker$ gdb python
913*da0073e9SAndroid Build Coastguard WorkerGNU gdb (GDB) 9.2
914*da0073e9SAndroid Build Coastguard Worker[...]
915*da0073e9SAndroid Build Coastguard Worker(gdb) # insert a breakpoint when we call .neg()
916*da0073e9SAndroid Build Coastguard Worker(gdb) break at::Tensor::neg
917*da0073e9SAndroid Build Coastguard WorkerFunction "at::Tensor::neg" not defined.
918*da0073e9SAndroid Build Coastguard WorkerMake breakpoint pending on future shared library load? (y or [n]) y
919*da0073e9SAndroid Build Coastguard WorkerBreakpoint 1 (at::Tensor::neg) pending.
920*da0073e9SAndroid Build Coastguard Worker
921*da0073e9SAndroid Build Coastguard Worker(gdb) run
922*da0073e9SAndroid Build Coastguard Worker[...]
923*da0073e9SAndroid Build Coastguard Worker>>> import torch
924*da0073e9SAndroid Build Coastguard Worker>>> t = torch.tensor([1, 2, 3, 4], dtype=torch.float64)
925*da0073e9SAndroid Build Coastguard Worker>>> t
926*da0073e9SAndroid Build Coastguard Workertensor([1., 2., 3., 4.], dtype=torch.float64)
927*da0073e9SAndroid Build Coastguard Worker>>> t.neg()
928*da0073e9SAndroid Build Coastguard Worker
929*da0073e9SAndroid Build Coastguard WorkerThread 1 "python" hit Breakpoint 1, at::Tensor::neg (this=0x7ffb118a9c88) at aten/src/ATen/core/TensorBody.h:3295
930*da0073e9SAndroid Build Coastguard Worker3295    inline at::Tensor Tensor::neg() const {
931*da0073e9SAndroid Build Coastguard Worker(gdb) # the default repr of 'this' is not very useful
932*da0073e9SAndroid Build Coastguard Worker(gdb) p this
933*da0073e9SAndroid Build Coastguard Worker$1 = (const at::Tensor * const) 0x7ffb118a9c88
934*da0073e9SAndroid Build Coastguard Worker(gdb) p *this
935*da0073e9SAndroid Build Coastguard Worker$2 = {impl_ = {target_ = 0x55629b5cd330}}
936*da0073e9SAndroid Build Coastguard Worker(gdb) torch-tensor-repr *this
937*da0073e9SAndroid Build Coastguard WorkerPython-level repr of *this:
938*da0073e9SAndroid Build Coastguard Workertensor([1., 2., 3., 4.], dtype=torch.float64)
939*da0073e9SAndroid Build Coastguard Worker```
940*da0073e9SAndroid Build Coastguard Worker
941*da0073e9SAndroid Build Coastguard WorkerGDB tries to automatically load `pytorch-gdb` thanks to the
942*da0073e9SAndroid Build Coastguard Worker[.gdbinit](.gdbinit) at the root of the pytorch repo. However, auto-loadings is disabled by default, because of security reasons:
943*da0073e9SAndroid Build Coastguard Worker
944*da0073e9SAndroid Build Coastguard Worker```bash
945*da0073e9SAndroid Build Coastguard Worker$ gdb
946*da0073e9SAndroid Build Coastguard Workerwarning: File "/path/to/pytorch/.gdbinit" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
947*da0073e9SAndroid Build Coastguard WorkerTo enable execution of this file add
948*da0073e9SAndroid Build Coastguard Worker        add-auto-load-safe-path /path/to/pytorch/.gdbinit
949*da0073e9SAndroid Build Coastguard Workerline to your configuration file "/home/YOUR-USERNAME/.gdbinit".
950*da0073e9SAndroid Build Coastguard WorkerTo completely disable this security protection add
951*da0073e9SAndroid Build Coastguard Worker        set auto-load safe-path /
952*da0073e9SAndroid Build Coastguard Workerline to your configuration file "/home/YOUR-USERNAME/.gdbinit".
953*da0073e9SAndroid Build Coastguard WorkerFor more information about this security protection see the
954*da0073e9SAndroid Build Coastguard Worker"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
955*da0073e9SAndroid Build Coastguard Worker        info "(gdb)Auto-loading safe path"
956*da0073e9SAndroid Build Coastguard Worker(gdb)
957*da0073e9SAndroid Build Coastguard Worker```
958*da0073e9SAndroid Build Coastguard Worker
959*da0073e9SAndroid Build Coastguard WorkerAs gdb itself suggests, the best way to enable auto-loading of `pytorch-gdb`
960*da0073e9SAndroid Build Coastguard Workeris to add the following line to your `~/.gdbinit` (i.e., the `.gdbinit` file
961*da0073e9SAndroid Build Coastguard Workerwhich is in your home directory, **not** `/path/to/pytorch/.gdbinit`):
962*da0073e9SAndroid Build Coastguard Worker
963*da0073e9SAndroid Build Coastguard Worker```bash
964*da0073e9SAndroid Build Coastguard Workeradd-auto-load-safe-path /path/to/pytorch/.gdbinit
965*da0073e9SAndroid Build Coastguard Worker```
966*da0073e9SAndroid Build Coastguard Worker
967*da0073e9SAndroid Build Coastguard Worker### C++ stacktraces
968*da0073e9SAndroid Build Coastguard WorkerSet `TORCH_SHOW_CPP_STACKTRACES=1` to get the C++ stacktrace when an error occurs in Python.
969*da0073e9SAndroid Build Coastguard Worker
970*da0073e9SAndroid Build Coastguard Worker## CUDA development tips
971*da0073e9SAndroid Build Coastguard Worker
972*da0073e9SAndroid Build Coastguard WorkerIf you are working on the CUDA code, here are some useful CUDA debugging tips:
973*da0073e9SAndroid Build Coastguard Worker
974*da0073e9SAndroid Build Coastguard Worker1. `CUDA_DEVICE_DEBUG=1` will enable CUDA device function debug symbols (`-g -G`).
975*da0073e9SAndroid Build Coastguard Worker    This will be particularly helpful in debugging device code. However, it will
976*da0073e9SAndroid Build Coastguard Worker    slow down the build process for about 50% (compared to only `DEBUG=1`), so use wisely.
977*da0073e9SAndroid Build Coastguard Worker2. `cuda-gdb` and `cuda-memcheck` are your best CUDA debugging friends. Unlike`gdb`,
978*da0073e9SAndroid Build Coastguard Worker   `cuda-gdb` can display actual values in a CUDA tensor (rather than all zeros).
979*da0073e9SAndroid Build Coastguard Worker3. CUDA supports a lot of C++11/14 features such as, `std::numeric_limits`, `std::nextafter`,
980*da0073e9SAndroid Build Coastguard Worker   `std::tuple` etc. in device code. Many of such features are possible because of the
981*da0073e9SAndroid Build Coastguard Worker   [--expt-relaxed-constexpr](https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#constexpr-functions)
982*da0073e9SAndroid Build Coastguard Worker   nvcc flag. There is a known [issue](https://github.com/ROCm-Developer-Tools/HIP/issues/374)
983*da0073e9SAndroid Build Coastguard Worker   that ROCm errors out on device code, which uses such stl functions.
984*da0073e9SAndroid Build Coastguard Worker4. A good performance metric for a CUDA kernel is the
985*da0073e9SAndroid Build Coastguard Worker   [Effective Memory Bandwidth](https://devblogs.nvidia.com/how-implement-performance-metrics-cuda-cc/).
986*da0073e9SAndroid Build Coastguard Worker   It is useful for you to measure this metric whenever you are writing/optimizing a CUDA
987*da0073e9SAndroid Build Coastguard Worker   kernel. Following script shows how we can measure the effective bandwidth of CUDA `uniform_`
988*da0073e9SAndroid Build Coastguard Worker   kernel.
989*da0073e9SAndroid Build Coastguard Worker   ```python
990*da0073e9SAndroid Build Coastguard Worker   import torch
991*da0073e9SAndroid Build Coastguard Worker   from torch.utils.benchmark import Timer
992*da0073e9SAndroid Build Coastguard Worker   size = 128*512
993*da0073e9SAndroid Build Coastguard Worker   nrep = 100
994*da0073e9SAndroid Build Coastguard Worker   nbytes_read_write = 4 # this is number of bytes read + written by a kernel. Change this to fit your kernel.
995*da0073e9SAndroid Build Coastguard Worker
996*da0073e9SAndroid Build Coastguard Worker   for i in range(10):
997*da0073e9SAndroid Build Coastguard Worker       a=torch.empty(size).cuda().uniform_()
998*da0073e9SAndroid Build Coastguard Worker       torch.cuda.synchronize()
999*da0073e9SAndroid Build Coastguard Worker       out = a.uniform_()
1000*da0073e9SAndroid Build Coastguard Worker       torch.cuda.synchronize()
1001*da0073e9SAndroid Build Coastguard Worker       t = Timer(stmt="a.uniform_()", globals=globals())
1002*da0073e9SAndroid Build Coastguard Worker       res = t.blocked_autorange()
1003*da0073e9SAndroid Build Coastguard Worker       timec = res.median
1004*da0073e9SAndroid Build Coastguard Worker       print("uniform, size, elements", size, "forward", timec, "bandwidth (GB/s)", size*(nbytes_read_write)*1e-9/timec)
1005*da0073e9SAndroid Build Coastguard Worker       size *=2
1006*da0073e9SAndroid Build Coastguard Worker   ```
1007*da0073e9SAndroid Build Coastguard Worker
1008*da0073e9SAndroid Build Coastguard Worker  See more cuda development tips [here](https://github.com/pytorch/pytorch/wiki/CUDA-basics)
1009*da0073e9SAndroid Build Coastguard Worker
1010*da0073e9SAndroid Build Coastguard Worker## Windows development tips
1011*da0073e9SAndroid Build Coastguard Worker
1012*da0073e9SAndroid Build Coastguard WorkerFor building from source on Windows, consult
1013*da0073e9SAndroid Build Coastguard Worker[our documentation](https://pytorch.org/docs/stable/notes/windows.html) on it.
1014*da0073e9SAndroid Build Coastguard Worker
1015*da0073e9SAndroid Build Coastguard WorkerOccasionally, you will write a patch which works on Linux, but fails CI on Windows.
1016*da0073e9SAndroid Build Coastguard WorkerThere are a few aspects in which MSVC (the Windows compiler toolchain we use) is stricter
1017*da0073e9SAndroid Build Coastguard Workerthan Linux, which are worth keeping in mind when fixing these problems.
1018*da0073e9SAndroid Build Coastguard Worker
1019*da0073e9SAndroid Build Coastguard Worker1. Symbols are NOT exported by default on Windows; instead, you have to explicitly
1020*da0073e9SAndroid Build Coastguard Worker   mark a symbol as exported/imported in a header file with `__declspec(dllexport)` /
1021*da0073e9SAndroid Build Coastguard Worker   `__declspec(dllimport)`. We have codified this pattern into a set of macros
1022*da0073e9SAndroid Build Coastguard Worker   which follow the convention `*_API`, e.g., `TORCH_API` inside Caffe2, Aten and Torch.
1023*da0073e9SAndroid Build Coastguard Worker   (Every separate shared library needs a unique macro name, because symbol visibility
1024*da0073e9SAndroid Build Coastguard Worker   is on a per shared library basis. See c10/macros/Macros.h for more details.)
1025*da0073e9SAndroid Build Coastguard Worker
1026*da0073e9SAndroid Build Coastguard Worker   The upshot is if you see an "unresolved external" error in your Windows build, this
1027*da0073e9SAndroid Build Coastguard Worker   is probably because you forgot to mark a function with `*_API`. However, there is
1028*da0073e9SAndroid Build Coastguard Worker   one important counterexample to this principle: if you want a *templated* function
1029*da0073e9SAndroid Build Coastguard Worker   to be instantiated at the call site, do NOT mark it with `*_API` (if you do mark it,
1030*da0073e9SAndroid Build Coastguard Worker   you'll have to explicitly instantiate all of the specializations used by the call
1031*da0073e9SAndroid Build Coastguard Worker   sites.)
1032*da0073e9SAndroid Build Coastguard Worker
1033*da0073e9SAndroid Build Coastguard Worker2. If you link against a library, this does not make its dependencies transitively
1034*da0073e9SAndroid Build Coastguard Worker   visible. You must explicitly specify a link dependency against every library whose
1035*da0073e9SAndroid Build Coastguard Worker   symbols you use. (This is different from Linux where in most environments,
1036*da0073e9SAndroid Build Coastguard Worker   transitive dependencies can be used to fulfill unresolved symbols.)
1037*da0073e9SAndroid Build Coastguard Worker
1038*da0073e9SAndroid Build Coastguard Worker3. If you have a Windows box (we have a few on EC2 which you can request access to) and
1039*da0073e9SAndroid Build Coastguard Worker   you want to run the build, the easiest way is to just run `.ci/pytorch/win-build.sh`.
1040*da0073e9SAndroid Build Coastguard Worker   If you need to rebuild, run `REBUILD=1 .ci/pytorch/win-build.sh` (this will avoid
1041*da0073e9SAndroid Build Coastguard Worker   blowing away your Conda environment.)
1042*da0073e9SAndroid Build Coastguard Worker
1043*da0073e9SAndroid Build Coastguard WorkerEven if you don't know anything about MSVC, you can use cmake to build simple programs on
1044*da0073e9SAndroid Build Coastguard WorkerWindows; this can be helpful if you want to learn more about some peculiar linking behavior
1045*da0073e9SAndroid Build Coastguard Workerby reproducing it on a small example. Here's a simple example cmake file that defines
1046*da0073e9SAndroid Build Coastguard Workertwo dynamic libraries, one linking with the other:
1047*da0073e9SAndroid Build Coastguard Worker
1048*da0073e9SAndroid Build Coastguard Worker```CMake
1049*da0073e9SAndroid Build Coastguard Workerproject(myproject CXX)
1050*da0073e9SAndroid Build Coastguard Workerset(CMAKE_CXX_STANDARD 14)
1051*da0073e9SAndroid Build Coastguard Workeradd_library(foo SHARED foo.cpp)
1052*da0073e9SAndroid Build Coastguard Workeradd_library(bar SHARED bar.cpp)
1053*da0073e9SAndroid Build Coastguard Worker# NB: don't forget to __declspec(dllexport) at least one symbol from foo,
1054*da0073e9SAndroid Build Coastguard Worker# otherwise foo.lib will not be created.
1055*da0073e9SAndroid Build Coastguard Workertarget_link_libraries(bar PUBLIC foo)
1056*da0073e9SAndroid Build Coastguard Worker```
1057*da0073e9SAndroid Build Coastguard Worker
1058*da0073e9SAndroid Build Coastguard WorkerYou can build it with:
1059*da0073e9SAndroid Build Coastguard Worker
1060*da0073e9SAndroid Build Coastguard Worker```bash
1061*da0073e9SAndroid Build Coastguard Workermkdir build
1062*da0073e9SAndroid Build Coastguard Workercd build
1063*da0073e9SAndroid Build Coastguard Workercmake ..
1064*da0073e9SAndroid Build Coastguard Workercmake --build .
1065*da0073e9SAndroid Build Coastguard Worker```
1066*da0073e9SAndroid Build Coastguard Worker
1067*da0073e9SAndroid Build Coastguard Worker### Known MSVC (and MSVC with NVCC) bugs
1068*da0073e9SAndroid Build Coastguard Worker
1069*da0073e9SAndroid Build Coastguard WorkerThe PyTorch codebase sometimes likes to use exciting C++ features, and
1070*da0073e9SAndroid Build Coastguard Workerthese exciting features lead to exciting bugs in Windows compilers.
1071*da0073e9SAndroid Build Coastguard WorkerTo add insult to injury, the error messages will often not tell you
1072*da0073e9SAndroid Build Coastguard Workerwhich line of code actually induced the erroring template instantiation.
1073*da0073e9SAndroid Build Coastguard Worker
1074*da0073e9SAndroid Build Coastguard WorkerWe've found the most effective way to debug these problems is to
1075*da0073e9SAndroid Build Coastguard Workercarefully read over diffs, keeping in mind known bugs in MSVC/NVCC.
1076*da0073e9SAndroid Build Coastguard WorkerHere are a few well known pitfalls and workarounds:
1077*da0073e9SAndroid Build Coastguard Worker
1078*da0073e9SAndroid Build Coastguard Worker* This is not actually a bug per se, but in general, code generated by MSVC
1079*da0073e9SAndroid Build Coastguard Worker  is more sensitive to memory errors; you may have written some code
1080*da0073e9SAndroid Build Coastguard Worker  that does a use-after-free or stack overflows; on Linux the code
1081*da0073e9SAndroid Build Coastguard Worker  might work, but on Windows your program will crash. ASAN may not
1082*da0073e9SAndroid Build Coastguard Worker  catch all of these problems: stay vigilant to the possibility that
1083*da0073e9SAndroid Build Coastguard Worker  your crash is due to a real memory problem.
1084*da0073e9SAndroid Build Coastguard Worker
1085*da0073e9SAndroid Build Coastguard Worker* (NVCC) `c10::optional` does not work when used from device code. Don't use
1086*da0073e9SAndroid Build Coastguard Worker  it from kernels. Upstream issue: https://github.com/akrzemi1/Optional/issues/58
1087*da0073e9SAndroid Build Coastguard Worker  and our local issue #10329.
1088*da0073e9SAndroid Build Coastguard Worker
1089*da0073e9SAndroid Build Coastguard Worker* `constexpr` generally works less well on MSVC.
1090*da0073e9SAndroid Build Coastguard Worker
1091*da0073e9SAndroid Build Coastguard Worker  * The idiom `static_assert(f() == f())` to test if `f` is constexpr
1092*da0073e9SAndroid Build Coastguard Worker    does not work; you'll get "error C2131: expression did not evaluate
1093*da0073e9SAndroid Build Coastguard Worker    to a constant". Don't use these asserts on Windows.
1094*da0073e9SAndroid Build Coastguard Worker    (Example: `c10/util/intrusive_ptr.h`)
1095*da0073e9SAndroid Build Coastguard Worker
1096*da0073e9SAndroid Build Coastguard Worker* (NVCC) Code you access inside a `static_assert` will eagerly be
1097*da0073e9SAndroid Build Coastguard Worker  evaluated as if it were device code, and so you might get an error
1098*da0073e9SAndroid Build Coastguard Worker  that the code is "not accessible".
1099*da0073e9SAndroid Build Coastguard Worker
1100*da0073e9SAndroid Build Coastguard Worker```cpp
1101*da0073e9SAndroid Build Coastguard Workerclass A {
1102*da0073e9SAndroid Build Coastguard Worker  static A singleton_;
1103*da0073e9SAndroid Build Coastguard Worker  static constexpr inline A* singleton() {
1104*da0073e9SAndroid Build Coastguard Worker    return &singleton_;
1105*da0073e9SAndroid Build Coastguard Worker  }
1106*da0073e9SAndroid Build Coastguard Worker};
1107*da0073e9SAndroid Build Coastguard Workerstatic_assert(std::is_same(A*, decltype(A::singleton()))::value, "hmm");
1108*da0073e9SAndroid Build Coastguard Worker```
1109*da0073e9SAndroid Build Coastguard Worker
1110*da0073e9SAndroid Build Coastguard Worker* The compiler will run out of heap space if you attempt to compile files that
1111*da0073e9SAndroid Build Coastguard Worker  are too large. Splitting such files into separate files helps.
1112*da0073e9SAndroid Build Coastguard Worker  (Example: `THTensorMath`, `THTensorMoreMath`, `THTensorEvenMoreMath`.)
1113*da0073e9SAndroid Build Coastguard Worker
1114*da0073e9SAndroid Build Coastguard Worker* MSVC's preprocessor (but not the standard compiler) has a bug
1115*da0073e9SAndroid Build Coastguard Worker  where it incorrectly tokenizes raw string literals, ending when it sees a `"`.
1116*da0073e9SAndroid Build Coastguard Worker  This causes preprocessor tokens inside the literal like an`#endif`  to be incorrectly
1117*da0073e9SAndroid Build Coastguard Worker  treated as preprocessor directives. See https://godbolt.org/z/eVTIJq as an example.
1118*da0073e9SAndroid Build Coastguard Worker
1119*da0073e9SAndroid Build Coastguard Worker* Either MSVC or the Windows headers have a PURE macro defined and will replace
1120*da0073e9SAndroid Build Coastguard Worker  any occurrences of the PURE token in code with an empty string. This is why
1121*da0073e9SAndroid Build Coastguard Worker  we have AliasAnalysisKind::PURE_FUNCTION and not AliasAnalysisKind::PURE.
1122*da0073e9SAndroid Build Coastguard Worker  The same is likely true for other identifiers that we just didn't try to use yet.
1123*da0073e9SAndroid Build Coastguard Worker
1124*da0073e9SAndroid Build Coastguard Worker### Building on legacy code and CUDA
1125*da0073e9SAndroid Build Coastguard Worker
1126*da0073e9SAndroid Build Coastguard WorkerCUDA, MSVC, and PyTorch versions are interdependent; please install matching versions from this table:
1127*da0073e9SAndroid Build Coastguard Worker| CUDA version | Newest supported VS version                             | PyTorch version |
1128*da0073e9SAndroid Build Coastguard Worker| ------------ | ------------------------------------------------------- | --------------- |
1129*da0073e9SAndroid Build Coastguard Worker| 10.1         | Visual Studio 2019 (16.X) (`_MSC_VER` < 1930)           |  1.3.0 ~ 1.7.0  |
1130*da0073e9SAndroid Build Coastguard Worker| 10.2         | Visual Studio 2019 (16.X) (`_MSC_VER` < 1930)           |  1.5.0 ~ 1.7.0  |
1131*da0073e9SAndroid Build Coastguard Worker| 11.0         | Visual Studio 2019 (16.X) (`_MSC_VER` < 1930)           |      1.7.0      |
1132*da0073e9SAndroid Build Coastguard Worker
1133*da0073e9SAndroid Build Coastguard WorkerNote: There's a [compilation issue](https://github.com/oneapi-src/oneDNN/issues/812) in several Visual Studio 2019 versions since 16.7.1, so please make sure your Visual Studio 2019 version is not in 16.7.1 ~ 16.7.5
1134*da0073e9SAndroid Build Coastguard Worker
1135*da0073e9SAndroid Build Coastguard Worker## Running clang-tidy
1136*da0073e9SAndroid Build Coastguard Worker
1137*da0073e9SAndroid Build Coastguard Worker[Clang-Tidy](https://clang.llvm.org/extra/clang-tidy/index.html) is a C++
1138*da0073e9SAndroid Build Coastguard Workerlinter and static analysis tool based on the clang compiler. We run clang-tidy
1139*da0073e9SAndroid Build Coastguard Workerin our CI to make sure that new C++ code is safe, sane and efficient. See the
1140*da0073e9SAndroid Build Coastguard Worker[`clang-tidy` job in our GitHub Workflow's
1141*da0073e9SAndroid Build Coastguard Workerlint.yml file](https://github.com/pytorch/pytorch/blob/main/.github/workflows/lint.yml)
1142*da0073e9SAndroid Build Coastguard Workerfor the simple commands we use for this.
1143*da0073e9SAndroid Build Coastguard Worker
1144*da0073e9SAndroid Build Coastguard WorkerTo run clang-tidy locally, follow these steps:
1145*da0073e9SAndroid Build Coastguard Worker
1146*da0073e9SAndroid Build Coastguard Worker1. Install clang-tidy.
1147*da0073e9SAndroid Build Coastguard WorkerWe provide custom built binaries which have additional checks enabled. You can install it by running:
1148*da0073e9SAndroid Build Coastguard Worker```bash
1149*da0073e9SAndroid Build Coastguard Workerpython3 -m tools.linter.clang_tidy.generate_build_files
1150*da0073e9SAndroid Build Coastguard Worker```
1151*da0073e9SAndroid Build Coastguard WorkerWe currently only support Linux and MacOS (x86).
1152*da0073e9SAndroid Build Coastguard Worker
1153*da0073e9SAndroid Build Coastguard Worker2. Install clang-tidy driver script dependencies
1154*da0073e9SAndroid Build Coastguard Worker```bash
1155*da0073e9SAndroid Build Coastguard Workerpip3 install -r tools/linter/clang_tidy/requirements.txt
1156*da0073e9SAndroid Build Coastguard Worker```
1157*da0073e9SAndroid Build Coastguard Worker
1158*da0073e9SAndroid Build Coastguard Worker3. Run clang-tidy
1159*da0073e9SAndroid Build Coastguard Worker```bash
1160*da0073e9SAndroid Build Coastguard Worker# Run clang-tidy on the entire codebase
1161*da0073e9SAndroid Build Coastguard Workermake clang-tidy
1162*da0073e9SAndroid Build Coastguard Worker# Run clang-tidy only on your changes
1163*da0073e9SAndroid Build Coastguard Workermake clang-tidy CHANGED_ONLY=--changed-only
1164*da0073e9SAndroid Build Coastguard Worker```
1165*da0073e9SAndroid Build Coastguard WorkerThis internally invokes our driver script and closely mimics how clang-tidy is run on CI.
1166*da0073e9SAndroid Build Coastguard Worker
1167*da0073e9SAndroid Build Coastguard Worker## Pre-commit tidy/linting hook
1168*da0073e9SAndroid Build Coastguard Worker
1169*da0073e9SAndroid Build Coastguard WorkerWe use clang-tidy to perform additional
1170*da0073e9SAndroid Build Coastguard Workerformatting and semantic checking of code. We provide a pre-commit git hook for
1171*da0073e9SAndroid Build Coastguard Workerperforming these checks, before a commit is created:
1172*da0073e9SAndroid Build Coastguard Worker
1173*da0073e9SAndroid Build Coastguard Worker  ```bash
1174*da0073e9SAndroid Build Coastguard Worker  ln -s ../../tools/git-pre-commit .git/hooks/pre-commit
1175*da0073e9SAndroid Build Coastguard Worker  ```
1176*da0073e9SAndroid Build Coastguard Worker
1177*da0073e9SAndroid Build Coastguard WorkerIf you have already committed files and
1178*da0073e9SAndroid Build Coastguard WorkerCI reports `flake8` errors, you can run the check locally in your PR branch with:
1179*da0073e9SAndroid Build Coastguard Worker
1180*da0073e9SAndroid Build Coastguard Worker  ```bash
1181*da0073e9SAndroid Build Coastguard Worker  flake8 $(git diff --name-only $(git merge-base --fork-point main))
1182*da0073e9SAndroid Build Coastguard Worker  ```
1183*da0073e9SAndroid Build Coastguard Worker
1184*da0073e9SAndroid Build Coastguard WorkerYou'll need to install an appropriately configured flake8; see
1185*da0073e9SAndroid Build Coastguard Worker[Lint as you type](https://github.com/pytorch/pytorch/wiki/Lint-as-you-type)
1186*da0073e9SAndroid Build Coastguard Workerfor documentation on how to do this.
1187*da0073e9SAndroid Build Coastguard Worker
1188*da0073e9SAndroid Build Coastguard WorkerFix the code so that no errors are reported when you re-run the above check again,
1189*da0073e9SAndroid Build Coastguard Workerand then commit the fix.
1190*da0073e9SAndroid Build Coastguard Worker
1191*da0073e9SAndroid Build Coastguard Worker## Building PyTorch with ASAN
1192*da0073e9SAndroid Build Coastguard Worker
1193*da0073e9SAndroid Build Coastguard Worker[ASAN](https://github.com/google/sanitizers/wiki/AddressSanitizer) is very
1194*da0073e9SAndroid Build Coastguard Workeruseful for debugging memory errors in C++. We run it in CI, but here's how to
1195*da0073e9SAndroid Build Coastguard Workerget the same thing to run on your local machine.
1196*da0073e9SAndroid Build Coastguard Worker
1197*da0073e9SAndroid Build Coastguard WorkerFirst, install LLVM 8. The easiest way is to get [prebuilt
1198*da0073e9SAndroid Build Coastguard Workerbinaries](http://releases.llvm.org/download.html#8.0.0) and extract them to
1199*da0073e9SAndroid Build Coastguard Workerfolder (later called `$LLVM_ROOT`).
1200*da0073e9SAndroid Build Coastguard Worker
1201*da0073e9SAndroid Build Coastguard WorkerThen set up the appropriate scripts. You can put this in your `.bashrc`:
1202*da0073e9SAndroid Build Coastguard Worker
1203*da0073e9SAndroid Build Coastguard Worker```bash
1204*da0073e9SAndroid Build Coastguard WorkerLLVM_ROOT=<wherever your llvm install is>
1205*da0073e9SAndroid Build Coastguard WorkerPYTORCH_ROOT=<wherever your pytorch checkout is>
1206*da0073e9SAndroid Build Coastguard Worker
1207*da0073e9SAndroid Build Coastguard WorkerLIBASAN_RT="$LLVM_ROOT/lib/clang/8.0.0/lib/linux/libclang_rt.asan-x86_64.so"
1208*da0073e9SAndroid Build Coastguard Workerbuild_with_asan()
1209*da0073e9SAndroid Build Coastguard Worker{
1210*da0073e9SAndroid Build Coastguard Worker  LD_PRELOAD=${LIBASAN_RT} \
1211*da0073e9SAndroid Build Coastguard Worker  CC="$LLVM_ROOT/bin/clang" \
1212*da0073e9SAndroid Build Coastguard Worker  CXX="$LLVM_ROOT/bin/clang++" \
1213*da0073e9SAndroid Build Coastguard Worker  LDSHARED="clang --shared" \
1214*da0073e9SAndroid Build Coastguard Worker  LDFLAGS="-stdlib=libstdc++" \
1215*da0073e9SAndroid Build Coastguard Worker  CFLAGS="-fsanitize=address -fno-sanitize-recover=all -shared-libasan -pthread" \
1216*da0073e9SAndroid Build Coastguard Worker  CXX_FLAGS="-pthread" \
1217*da0073e9SAndroid Build Coastguard Worker  USE_CUDA=0 USE_OPENMP=0 USE_DISTRIBUTED=0 DEBUG=1 \
1218*da0073e9SAndroid Build Coastguard Worker  python setup.py develop
1219*da0073e9SAndroid Build Coastguard Worker}
1220*da0073e9SAndroid Build Coastguard Worker
1221*da0073e9SAndroid Build Coastguard Workerrun_with_asan()
1222*da0073e9SAndroid Build Coastguard Worker{
1223*da0073e9SAndroid Build Coastguard Worker  LD_PRELOAD=${LIBASAN_RT} $@
1224*da0073e9SAndroid Build Coastguard Worker}
1225*da0073e9SAndroid Build Coastguard Worker
1226*da0073e9SAndroid Build Coastguard Worker# you can look at build-asan.sh to find the latest options the CI uses
1227*da0073e9SAndroid Build Coastguard Workerexport ASAN_OPTIONS=detect_leaks=0:symbolize=1:strict_init_order=true
1228*da0073e9SAndroid Build Coastguard Workerexport UBSAN_OPTIONS=print_stacktrace=1:suppressions=$PYTORCH_ROOT/ubsan.supp
1229*da0073e9SAndroid Build Coastguard Workerexport ASAN_SYMBOLIZER_PATH=$LLVM_ROOT/bin/llvm-symbolizer
1230*da0073e9SAndroid Build Coastguard Worker```
1231*da0073e9SAndroid Build Coastguard Worker
1232*da0073e9SAndroid Build Coastguard WorkerThen you can use the scripts like:
1233*da0073e9SAndroid Build Coastguard Worker
1234*da0073e9SAndroid Build Coastguard Worker```
1235*da0073e9SAndroid Build Coastguard Workersuo-devfair ~/pytorch ❯ build_with_asan
1236*da0073e9SAndroid Build Coastguard Workersuo-devfair ~/pytorch ❯ run_with_asan python test/test_jit.py
1237*da0073e9SAndroid Build Coastguard Worker```
1238*da0073e9SAndroid Build Coastguard Worker
1239*da0073e9SAndroid Build Coastguard Worker### Getting `ccache` to work
1240*da0073e9SAndroid Build Coastguard Worker
1241*da0073e9SAndroid Build Coastguard WorkerThe scripts above specify the `clang` and `clang++` binaries directly, which
1242*da0073e9SAndroid Build Coastguard Workerbypasses `ccache`. Here's how to get `ccache` to work:
1243*da0073e9SAndroid Build Coastguard Worker
1244*da0073e9SAndroid Build Coastguard Worker1. Make sure the ccache symlinks for `clang` and `clang++` are set up (see
1245*da0073e9SAndroid Build Coastguard Worker   CONTRIBUTING.md)
1246*da0073e9SAndroid Build Coastguard Worker2. Make sure `$LLVM_ROOT/bin` is available on your `$PATH`.
1247*da0073e9SAndroid Build Coastguard Worker3. Change the `CC` and `CXX` variables in `build_with_asan()` to point
1248*da0073e9SAndroid Build Coastguard Worker   directly to `clang` and `clang++`.
1249*da0073e9SAndroid Build Coastguard Worker
1250*da0073e9SAndroid Build Coastguard Worker### Why this stuff with `LD_PRELOAD` and `LIBASAN_RT`?
1251*da0073e9SAndroid Build Coastguard Worker
1252*da0073e9SAndroid Build Coastguard WorkerThe “standard” workflow for ASAN assumes you have a standalone binary:
1253*da0073e9SAndroid Build Coastguard Worker
1254*da0073e9SAndroid Build Coastguard Worker1. Recompile your binary with `-fsanitize=address`.
1255*da0073e9SAndroid Build Coastguard Worker2. Run the binary, and ASAN will report whatever errors it find.
1256*da0073e9SAndroid Build Coastguard Worker
1257*da0073e9SAndroid Build Coastguard WorkerUnfortunately, PyTorch is a distributed as a shared library that is loaded by
1258*da0073e9SAndroid Build Coastguard Workera third-party executable (Python). It’s too much of a hassle to recompile all
1259*da0073e9SAndroid Build Coastguard Workerof Python every time we want to use ASAN. Luckily, the ASAN folks have a
1260*da0073e9SAndroid Build Coastguard Workerworkaround for cases like this:
1261*da0073e9SAndroid Build Coastguard Worker
1262*da0073e9SAndroid Build Coastguard Worker1. Recompile your library with `-fsanitize=address -shared-libasan`. The
1263*da0073e9SAndroid Build Coastguard Worker   extra `-shared-libasan` tells the compiler to ask for the shared ASAN
1264*da0073e9SAndroid Build Coastguard Worker   runtime library.
1265*da0073e9SAndroid Build Coastguard Worker2. Use `LD_PRELOAD` to tell the dynamic linker to load the ASAN runtime
1266*da0073e9SAndroid Build Coastguard Worker   library before anything else.
1267*da0073e9SAndroid Build Coastguard Worker
1268*da0073e9SAndroid Build Coastguard WorkerMore information can be found
1269*da0073e9SAndroid Build Coastguard Worker[here](https://github.com/google/sanitizers/wiki/AddressSanitizerAsDso).
1270*da0073e9SAndroid Build Coastguard Worker
1271*da0073e9SAndroid Build Coastguard Worker### Why LD_PRELOAD in the build function?
1272*da0073e9SAndroid Build Coastguard Worker
1273*da0073e9SAndroid Build Coastguard WorkerWe need `LD_PRELOAD` because there is a cmake check that ensures that a
1274*da0073e9SAndroid Build Coastguard Workersimple program builds and runs. If we are building with ASAN as a shared
1275*da0073e9SAndroid Build Coastguard Workerlibrary, we need to `LD_PRELOAD` the runtime library, otherwise there will
1276*da0073e9SAndroid Build Coastguard Workerdynamic linker errors and the check will fail.
1277*da0073e9SAndroid Build Coastguard Worker
1278*da0073e9SAndroid Build Coastguard WorkerWe don’t actually need either of these if we fix the cmake checks.
1279*da0073e9SAndroid Build Coastguard Worker
1280*da0073e9SAndroid Build Coastguard Worker### Why no leak detection?
1281*da0073e9SAndroid Build Coastguard Worker
1282*da0073e9SAndroid Build Coastguard WorkerPython leaks a lot of memory. Possibly we could configure a suppression file,
1283*da0073e9SAndroid Build Coastguard Workerbut we haven’t gotten around to it.
1284*da0073e9SAndroid Build Coastguard Worker
1285*da0073e9SAndroid Build Coastguard Worker## Caffe2 notes
1286*da0073e9SAndroid Build Coastguard Worker
1287*da0073e9SAndroid Build Coastguard WorkerIn 2018, we merged Caffe2 into the PyTorch source repository. While the
1288*da0073e9SAndroid Build Coastguard Workersteady state aspiration is that Caffe2 and PyTorch share code freely,
1289*da0073e9SAndroid Build Coastguard Workerin the meantime there will be some separation.
1290*da0073e9SAndroid Build Coastguard Worker
1291*da0073e9SAndroid Build Coastguard WorkerThere are a few "unusual" directories which, for historical reasons,
1292*da0073e9SAndroid Build Coastguard Workerare Caffe2/PyTorch specific. Here they are:
1293*da0073e9SAndroid Build Coastguard Worker
1294*da0073e9SAndroid Build Coastguard Worker- `CMakeLists.txt`, `Makefile`, `binaries`, `cmake`, `conda`, `modules`,
1295*da0073e9SAndroid Build Coastguard Worker  `scripts` are Caffe2-specific. Don't put PyTorch code in them without
1296*da0073e9SAndroid Build Coastguard Worker  extra coordination.
1297*da0073e9SAndroid Build Coastguard Worker
1298*da0073e9SAndroid Build Coastguard Worker- `mypy*`, `requirements.txt`, `setup.py`, `test`, `tools` are
1299*da0073e9SAndroid Build Coastguard Worker  PyTorch-specific. Don't put Caffe2 code in them without extra
1300*da0073e9SAndroid Build Coastguard Worker  coordination.
1301*da0073e9SAndroid Build Coastguard Worker
1302*da0073e9SAndroid Build Coastguard Worker## CI failure tips
1303*da0073e9SAndroid Build Coastguard Worker
1304*da0073e9SAndroid Build Coastguard WorkerOnce you submit a PR or push a new commit to a branch that is in
1305*da0073e9SAndroid Build Coastguard Workeran active PR, CI jobs will be run automatically. Some of these may
1306*da0073e9SAndroid Build Coastguard Workerfail and you will need to find out why, by looking at the logs.
1307*da0073e9SAndroid Build Coastguard Worker
1308*da0073e9SAndroid Build Coastguard WorkerFairly often, a CI failure might be unrelated to your changes. You can
1309*da0073e9SAndroid Build Coastguard Workerconfirm by going to our [HUD](https://hud.pytorch.org) and seeing if the CI job
1310*da0073e9SAndroid Build Coastguard Workeris failing upstream already. In this case, you
1311*da0073e9SAndroid Build Coastguard Workercan usually ignore the failure. See [the following
1312*da0073e9SAndroid Build Coastguard Workersubsection](#which-commit-is-used-in-ci) for more details.
1313*da0073e9SAndroid Build Coastguard Worker
1314*da0073e9SAndroid Build Coastguard WorkerSome failures might be related to specific hardware or environment
1315*da0073e9SAndroid Build Coastguard Workerconfigurations. In this case, if you're a Meta employee, you can ssh into
1316*da0073e9SAndroid Build Coastguard Workerthe job's session to perform manual debugging following the instructions in
1317*da0073e9SAndroid Build Coastguard Workerour [CI wiki](https://github.com/pytorch/pytorch/wiki/Debugging-using-with-ssh-for-Github-Actions).
1318*da0073e9SAndroid Build Coastguard Worker
1319*da0073e9SAndroid Build Coastguard Worker
1320*da0073e9SAndroid Build Coastguard Worker### Which commit is used in CI?
1321*da0073e9SAndroid Build Coastguard Worker
1322*da0073e9SAndroid Build Coastguard WorkerFor CI run on `main`, this repository is checked out for a given `main`
1323*da0073e9SAndroid Build Coastguard Workercommit, and CI is run on that commit (there isn't really any other choice).
1324*da0073e9SAndroid Build Coastguard Worker
1325*da0073e9SAndroid Build Coastguard WorkerFor PRs, however, it's a bit more complicated. Consider this commit graph, where
1326*da0073e9SAndroid Build Coastguard Worker`main` is at commit `A`, and the branch for PR #42 (just a placeholder) is at
1327*da0073e9SAndroid Build Coastguard Workercommit `B`:
1328*da0073e9SAndroid Build Coastguard Worker
1329*da0073e9SAndroid Build Coastguard Worker```
1330*da0073e9SAndroid Build Coastguard Worker       o---o---B (refs/pull/42/head)
1331*da0073e9SAndroid Build Coastguard Worker      /         \
1332*da0073e9SAndroid Build Coastguard Worker     /           C (refs/pull/42/merge)
1333*da0073e9SAndroid Build Coastguard Worker    /           /
1334*da0073e9SAndroid Build Coastguard Worker---o---o---o---A (merge-destination) - usually main
1335*da0073e9SAndroid Build Coastguard Worker```
1336*da0073e9SAndroid Build Coastguard Worker
1337*da0073e9SAndroid Build Coastguard WorkerThere are two possible choices for which commit to use:
1338*da0073e9SAndroid Build Coastguard Worker
1339*da0073e9SAndroid Build Coastguard Worker1. Checkout commit `B`, the head of the PR (manually committed by the PR
1340*da0073e9SAndroid Build Coastguard Worker   author).
1341*da0073e9SAndroid Build Coastguard Worker2. Checkout commit `C`, the hypothetical result of what would happen if the PR
1342*da0073e9SAndroid Build Coastguard Worker   were merged into its destination (usually `main`).
1343*da0073e9SAndroid Build Coastguard Worker
1344*da0073e9SAndroid Build Coastguard WorkerFor all practical purposes, most people can think of the commit being used as
1345*da0073e9SAndroid Build Coastguard Workercommit `B` (choice **1**).
1346*da0073e9SAndroid Build Coastguard Worker
1347*da0073e9SAndroid Build Coastguard WorkerHowever, if workflow files (which govern CI behavior) were modified (either by your PR or since dev branch were created ) there's
1348*da0073e9SAndroid Build Coastguard Workera nuance to know about:
1349*da0073e9SAndroid Build Coastguard WorkerThe workflow files themselves get taken from checkpoint `C`, the merger of your
1350*da0073e9SAndroid Build Coastguard WorkerPR and the `main` branch. But only the workflow files get taken from that merged
1351*da0073e9SAndroid Build Coastguard Workercheckpoint. Everything else (tests, code, etc) all get taken directly from your
1352*da0073e9SAndroid Build Coastguard WorkerPR's commit (commit `B`). Please note, this scenario would never affect PRs authored by `ghstack` as they would not automatically ingest the updates from default branch.
1353*da0073e9SAndroid Build Coastguard Worker
1354*da0073e9SAndroid Build Coastguard Worker
1355*da0073e9SAndroid Build Coastguard Worker## Dev Infra Office Hours
1356*da0073e9SAndroid Build Coastguard Worker[Dev Infra Office Hours](https://github.com/pytorch/pytorch/wiki/Dev-Infra-Office-Hours) are hosted every Friday to answer any questions regarding developer experience, Green HUD, and CI.
1357