mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
add setup pages
6
Contributor-Guide/Submit-PR.md
Normal file
6
Contributor-Guide/Submit-PR.md
Normal file
@ -0,0 +1,6 @@
|
||||
|
||||
### Tip: Skip CI
|
||||
* If a commit is simple and doesn't affect any code (keep in mind that some docstrings contain code
|
||||
that is used in tests), you can add `[skip ci]` (case sensitive) somewhere in your commit message to
|
||||
[skip all build / test steps](https://github.blog/changelog/2021-02-08-github-actions-skip-pull-request-and-push-workflows-with-skip-ci/).
|
||||
Note that changing the pull request body or title on GitHub itself has no effect.
|
59
Contributor-Guide/setup/Checkout-Code.md
Normal file
59
Contributor-Guide/setup/Checkout-Code.md
Normal file
@ -0,0 +1,59 @@
|
||||
## Fork & Clone PyTorch
|
||||
|
||||
Fork the Pytorch repository to your Github account and create a local clone.
|
||||
|
||||
1. Go to https://github.com/pytorch/pytorch
|
||||
1. Press Fork on the top right.
|
||||
1. When asked where to fork the repository, choose to fork it to your username.
|
||||
1. Your fork will be created at https://github.com/<username>/pytorch.
|
||||
1. Clone your GitHub fork (replace <username> with your username):
|
||||
```
|
||||
git clone --recursive https://github.com/<username>/pytorch.git
|
||||
cd pytorch
|
||||
# if you are updating an existing checkout
|
||||
git submodule sync
|
||||
git submodule update --init --recursive
|
||||
```
|
||||
1. Add an `upstream` remote to stay in sync with the main repo. Always push changes to `origin` (your fork)!
|
||||
```
|
||||
$ git remote add upstream https://github.com/pytorch/pytorch
|
||||
$ git remote -v
|
||||
> origin https://github.com/<username>/pytorch.git (fetch)
|
||||
> origin https://github.com/<username>/pytorch.git (push)
|
||||
> upstream https://github.com/pytorch/pytorch.git (fetch)
|
||||
> upstream https://github.com/pytorch/pytorch.git (push)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Nightly Checkout & Pull for Python-only development
|
||||
The `tools/nightly.py` script is provided to allow Python-only development of
|
||||
PyTorch. Choose this if you aren't changing or compiling C++ code.
|
||||
This uses `conda` and `git` to check out the nightly development
|
||||
version of PyTorch and installs pre-built binaries into the current repository.
|
||||
|
||||
You can use this script to check out a new nightly branch with the following:
|
||||
|
||||
```bash
|
||||
cd pytorch
|
||||
./tools/nightly.py checkout -b my-nightly-branch
|
||||
conda activate pytorch-deps
|
||||
```
|
||||
|
||||
Or if you would like to re-use an existing conda environment, you can pass in
|
||||
the regular environment parameters (`--name` or `--prefix`):
|
||||
|
||||
```bash
|
||||
./tools/nightly.py checkout -b my-nightly-branch -n my-env
|
||||
conda activate my-env
|
||||
```
|
||||
|
||||
You can also use this tool to pull the nightly commits into the current branch:
|
||||
|
||||
```bash
|
||||
./tools/nightly.py pull -n my-env
|
||||
conda activate my-env
|
||||
```
|
||||
|
||||
Pulling will reinstall the PyTorch dependencies as well as the nightly binaries
|
||||
into the repo directory.
|
218
Contributor-Guide/setup/Debugging.md
Normal file
218
Contributor-Guide/setup/Debugging.md
Normal file
@ -0,0 +1,218 @@
|
||||
# Tips & Debugging
|
||||
|
||||
If you are working on the C++ code, there are a few important things that you will want to keep in mind:
|
||||
1. How to rebuild only the code you are working on.
|
||||
2. How to make rebuilds in the absence of changes go faster.
|
||||
|
||||
|
||||
## Selective Rebuild
|
||||
|
||||
### Use build options
|
||||
See [Install-Build.md#build-only-what-you-need] for building specific components.
|
||||
|
||||
### Reduce reinstalls
|
||||
* One way to avoid running `python setup.py develop` every time one makes a change to C++/CUDA/ObjectiveC files on Linux/Mac, is to create a symbolic link from `build` folder to `torch/lib`, for example, by issuing following:
|
||||
```bash
|
||||
pushd torch/lib; sh -c "ln -sf ../../build/lib/libtorch_cpu.* ."; popd
|
||||
```
|
||||
Afterwards rebuilding a library (for example to rebuild `libtorch_cpu.so` issue `ninja torch_cpu` from `build` folder), would be sufficient to make change visible in `torch` package.
|
||||
|
||||
### Clean reinstall
|
||||
* To reinstall, first uninstall all existing PyTorch installs. You may need to run `pip uninstall torch` multiple times. You'll know `torch` is fully
|
||||
uninstalled when you see `WARNING: Skipping torch as it is not installed`. (You should only have to `pip uninstall` a few times, but you can always `uninstall` with `timeout` or in a loop if you're feeling lazy.)
|
||||
|
||||
```bash
|
||||
conda uninstall pytorch -y
|
||||
yes | pip uninstall torch
|
||||
```
|
||||
|
||||
Next run `python setup.py clean`. After that, you can install in `develop` mode again.
|
||||
|
||||
## Faster Rebuild
|
||||
|
||||
### Make no-op build fast
|
||||
|
||||
#### Use Ninja
|
||||
|
||||
By default, cmake will use its Makefile generator to generate your build
|
||||
system. You can get faster builds if you install the ninja build system
|
||||
with `pip install ninja`. If PyTorch was already built, you will need
|
||||
to run `python setup.py clean` once after installing ninja for builds to
|
||||
succeed.
|
||||
|
||||
#### Use CCache
|
||||
|
||||
Even when dependencies are tracked with file modification, there are many
|
||||
situations where files get rebuilt when a previous compilation was exactly the
|
||||
same. Using ccache in a situation like this is a real time-saver.
|
||||
|
||||
Before building pytorch, install ccache from your package manager of choice:
|
||||
|
||||
```bash
|
||||
conda install ccache -c conda-forge
|
||||
sudo apt install ccache
|
||||
sudo yum install ccache
|
||||
brew install ccache
|
||||
```
|
||||
|
||||
You may also find the default cache size in ccache is too small to be useful.
|
||||
The cache sizes can be increased from the command line:
|
||||
|
||||
```bash
|
||||
# config: cache dir is ~/.ccache, conf file ~/.ccache/ccache.conf
|
||||
# max size of cache
|
||||
ccache -M 25Gi # -M 0 for unlimited
|
||||
# unlimited number of files
|
||||
ccache -F 0
|
||||
```
|
||||
|
||||
To check this is working, do two clean builds of pytorch in a row. The second
|
||||
build should be substantially and noticeably faster than the first build. If
|
||||
this doesn't seem to be the case, check the `CMAKE_<LANG>_COMPILER_LAUNCHER`
|
||||
rules in `build/CMakeCache.txt`, where `<LANG>` is `C`, `CXX` and `CUDA`.
|
||||
Each of these 3 variables should contain ccache, e.g.
|
||||
|
||||
```
|
||||
//CXX compiler launcher
|
||||
CMAKE_CXX_COMPILER_LAUNCHER:STRING=/usr/bin/ccache
|
||||
```
|
||||
|
||||
If not, you can define these variables on the command line before invoking `setup.py`.
|
||||
|
||||
```bash
|
||||
export CMAKE_C_COMPILER_LAUNCHER=ccache
|
||||
export CMAKE_CXX_COMPILER_LAUNCHER=ccache
|
||||
export CMAKE_CUDA_COMPILER_LAUNCHER=ccache
|
||||
python setup.py develop
|
||||
```
|
||||
|
||||
#### Use a faster linker
|
||||
|
||||
If you are editing a single file and rebuilding in a tight loop, the time spent
|
||||
linking will dominate. The system linker available in most Linux distributions
|
||||
(GNU `ld`) is quite slow. Use a faster linker, like [lld](https://lld.llvm.org/).
|
||||
|
||||
The easiest way to use `lld` this is download the
|
||||
[latest LLVM binaries](http://releases.llvm.org/download.html#8.0.0) and run:
|
||||
|
||||
```bash
|
||||
ln -s /path/to/downloaded/ld.lld /usr/local/bin/ld
|
||||
```
|
||||
|
||||
Mac users should follow [this guide](https://stackoverflow.com/questions/42730345/how-to-install-llvm-for-mac) instead.
|
||||
|
||||
#### Use pre-compiled headers
|
||||
|
||||
Sometimes there's no way of getting around rebuilding lots of files, for example
|
||||
editing `native_functions.yaml` usually means 1000+ files being rebuilt. If
|
||||
you're using CMake newer than 3.16, you can enable pre-compiled headers by
|
||||
setting `USE_PRECOMPILED_HEADERS=1` either on first setup, or in the
|
||||
`CMakeCache.txt` file.
|
||||
|
||||
```sh
|
||||
USE_PRECOMPILED_HEADERS=1 python setup.py develop
|
||||
```
|
||||
|
||||
This adds a build step where the compiler takes `<ATen/ATen.h>` and essentially
|
||||
dumps it's internal AST to a file so the compiler can avoid repeating itself for
|
||||
every `.cpp` file.
|
||||
|
||||
One caveat is that when enabled, this header gets included in every file by default.
|
||||
Which may change what code is legal, for example:
|
||||
- internal functions can never alias existing names in `<ATen/ATen.h>`
|
||||
- names in `<ATen/ATen.h>` will work even if you don't explicitly include it.
|
||||
|
||||
#### Workaround for header dependency bug in nvcc
|
||||
If re-building without modifying any files results in several CUDA files being
|
||||
re-compiled, you may be running into an `nvcc` bug where header dependencies are
|
||||
not converted to absolute paths before reporting it to the build system. This
|
||||
makes `ninja` think one of the header files has been deleted, so it runs the
|
||||
build again.
|
||||
|
||||
A compiler-wrapper to fix this is provided in `tools/nvcc_fix_deps.py`. You can use
|
||||
this as a compiler launcher, similar to `ccache`
|
||||
```bash
|
||||
export CMAKE_CUDA_COMPILER_LAUNCHER="python;`pwd`/tools/nvcc_fix_deps.py;ccache"
|
||||
python setup.py develop
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## General Debugging
|
||||
|
||||
* If you run into errors when running `python setup.py develop`, here are some debugging steps:
|
||||
1. Run `printf '#include <stdio.h>\nint main() { printf("Hello World");}'|clang -x c -; ./a.out` to make sure
|
||||
your CMake works and can compile this simple Hello World program without errors.
|
||||
2. Nuke your `build` directory. The `setup.py` script compiles binaries into the `build` folder and caches many
|
||||
details along the way, which saves time the next time you build. If you're running into issues, you can always
|
||||
`rm -rf build` from the toplevel `pytorch` directory and start over.
|
||||
3. If you have made edits to the PyTorch repo, commit any change you'd like to keep and clean the repo with the
|
||||
following commands (note that clean _really_ removes all untracked files and changes.):
|
||||
```bash
|
||||
git submodule deinit -f .
|
||||
git clean -xdf
|
||||
python setup.py clean
|
||||
git submodule update --init --recursive # very important to sync the submodules
|
||||
python setup.py develop # then try running the command again
|
||||
```
|
||||
4. The main step within `python setup.py develop` is running `make` from the `build` directory. If you want to
|
||||
experiment with some environment variables, you can pass them into the command:
|
||||
```bash
|
||||
ENV_KEY1=ENV_VAL1[, ENV_KEY2=ENV_VAL2]* python setup.py develop
|
||||
```
|
||||
|
||||
* If you run into issue running `git submodule update --init --recursive`. Please try the following:
|
||||
- If you encounter an error such as
|
||||
```
|
||||
error: Submodule 'third_party/pybind11' could not be updated
|
||||
```
|
||||
check whether your Git local or global config file contains any `submodule.*` settings. If yes, remove them and try again.
|
||||
(please reference [this doc](https://git-scm.com/docs/git-config#Documentation/git-config.txt-submoduleltnamegturl) for more info).
|
||||
|
||||
- If you encounter an error such as
|
||||
```
|
||||
fatal: unable to access 'https://github.com/pybind11/pybind11.git': could not load PEM client certificate ...
|
||||
```
|
||||
this is likely that you are using HTTP proxying and the certificate expired. To check if the certificate is valid, run
|
||||
`git config --global --list` and search for config like `http.proxysslcert=<cert_file>`. Then check certificate valid date by running
|
||||
```bash
|
||||
openssl x509 -noout -in <cert_file> -dates
|
||||
```
|
||||
|
||||
- If you encounter an error that some third_party modules are not checked out correctly, such as
|
||||
```
|
||||
Could not find .../pytorch/third_party/pybind11/CMakeLists.txt
|
||||
```
|
||||
remove any `submodule.*` settings in your local git config (`.git/config` of your pytorch repo) and try again.
|
||||
* 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).
|
||||
* 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)
|
||||
|
||||
|
||||
## Uncategorized 1
|
||||
|
||||
You can adjust the configuration of cmake variables optionally (without building first), by doing
|
||||
the following. For example, adjusting the pre-detected directories for CuDNN or BLAS can be done
|
||||
with such a step.
|
||||
|
||||
On Linux
|
||||
```bash
|
||||
export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
|
||||
python setup.py build --cmake-only
|
||||
ccmake build # or cmake-gui build
|
||||
```
|
||||
|
||||
On macOS
|
||||
```bash
|
||||
export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
|
||||
MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ python setup.py build --cmake-only
|
||||
ccmake build # or cmake-gui build
|
||||
```
|
||||
|
||||
## Uncategorized 2
|
||||
When installing with `python setup.py develop` (in contrast to `python setup.py install`) Python runtime will use 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). This way you do not need to repeatedly install after modifying Python files (`.py`). However, you would need to reinstall if you modify Python interface (`.pyi`, `.pyi.in`) or non-Python files (`.cpp`, `.cc`, `.cu`, `.h`, ...).
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
250
Contributor-Guide/setup/Install-Build.md
Normal file
250
Contributor-Guide/setup/Install-Build.md
Normal file
@ -0,0 +1,250 @@
|
||||
|
||||
# Install & Build
|
||||
|
||||
Source: https://github.com/pytorch/pytorch#from-source
|
||||
|
||||
Before you build, take a look at some tips that make building faster. You will need to do them before you build PyTorch.
|
||||
|
||||
## Install from Source
|
||||
Now that you have the source code, you can build PyTorch on your development machine. Note that the steps might be different depending on your machine's platform.
|
||||
|
||||
## ** !!! when should I use build/develop/install !!! **
|
||||
|
||||
### MacOS
|
||||
|
||||
```bash
|
||||
python3 setup.py develop
|
||||
```
|
||||
|
||||
### Linux
|
||||
|
||||
If you would like to compile PyTorch with [new C++ ABI](https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html) enabled, then first run this command:
|
||||
```bash
|
||||
export _GLIBCXX_USE_CXX11_ABI=1
|
||||
```
|
||||
|
||||
If you're compiling for AMD ROCm then first run this command:
|
||||
```bash
|
||||
# Only run this if you're compiling for ROCm
|
||||
python tools/amd_build/build_amd.py
|
||||
```
|
||||
|
||||
Install PyTorch
|
||||
```bash
|
||||
export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
|
||||
python setup.py develop
|
||||
```
|
||||
|
||||
> _Aside:_ If you are using [Anaconda](https://www.anaconda.com/distribution/#download-section), you may experience an error caused by the linker:
|
||||
>
|
||||
> ```plaintext
|
||||
> build/temp.linux-x86_64-3.7/torch/csrc/stub.o: file not recognized: file format not recognized
|
||||
> collect2: error: ld returned 1 exit status
|
||||
> error: command 'g++' failed with exit status 1
|
||||
> ```
|
||||
>
|
||||
> This is caused by `ld` from the Conda environment shadowing the system `ld`. You should use a newer version of Python that fixes this issue. The recommended Python version is 3.8.1+.
|
||||
|
||||
|
||||
### Windows
|
||||
|
||||
Choose Correct Visual Studio Version.
|
||||
|
||||
PyTorch CI uses Visual C++ BuildTools, which come with Visual Studio Enterprise,
|
||||
Professional, or Community Editions. You can also install the build tools from
|
||||
https://visualstudio.microsoft.com/visual-cpp-build-tools/. The build tools *do not*
|
||||
come with Visual Studio Code by default.
|
||||
|
||||
If you want to build legacy python code, please refer to [Building on legacy code and CUDA](https://github.com/pytorch/pytorch/blob/main/CONTRIBUTING.md#building-on-legacy-code-and-cuda)
|
||||
|
||||
|
||||
#### CPU-only builds
|
||||
|
||||
In this mode PyTorch computations will run on your CPU, not your GPU
|
||||
|
||||
```cmd
|
||||
conda activate
|
||||
python setup.py develop
|
||||
```
|
||||
|
||||
Note on OpenMP: The desired OpenMP implementation is Intel OpenMP (iomp). In order to link against iomp, you'll need to manually download the library and set up the building environment by tweaking `CMAKE_INCLUDE_PATH` and `LIB`. The instruction [here](https://github.com/pytorch/pytorch/blob/main/docs/source/notes/windows.rst#building-from-source) is an example for setting up both MKL and Intel OpenMP. Without these configurations for CMake, Microsoft Visual C OpenMP runtime (vcomp) will be used.
|
||||
|
||||
|
||||
#### CUDA based build
|
||||
|
||||
In this mode PyTorch computations will leverage your GPU via CUDA for faster number crunching
|
||||
|
||||
[NVTX](https://docs.nvidia.com/gameworks/content/gameworkslibrary/nvtx/nvidia_tools_extension_library_nvtx.htm) is needed to build Pytorch with CUDA.
|
||||
NVTX is a part of CUDA distributive, where it is called "Nsight Compute". To install it onto an already installed CUDA run CUDA installation once again and check the corresponding checkbox.
|
||||
Make sure that CUDA with Nsight Compute is installed after Visual Studio.
|
||||
|
||||
Currently, VS 2017 / 2019, and Ninja are supported as the generator of CMake. If `ninja.exe` is detected in `PATH`, then Ninja will be used as the default generator, otherwise, it will use VS 2017 / 2019.
|
||||
<br/> If Ninja is selected as the generator, the latest MSVC will get selected as the underlying toolchain.
|
||||
|
||||
Additional libraries such as
|
||||
[Magma](https://developer.nvidia.com/magma), [oneDNN, a.k.a. MKLDNN or DNNL](https://github.com/oneapi-src/oneDNN), and [Sccache](https://github.com/mozilla/sccache) are often needed. Please refer to the [installation-helper](https://github.com/pytorch/pytorch/tree/main/.ci/pytorch/win-test-helpers/installation-helpers) to install them.
|
||||
|
||||
You can refer to the [build_pytorch.bat](https://github.com/pytorch/pytorch/blob/main/.ci/pytorch/win-test-helpers/build_pytorch.bat) script for some other environment variables configurations
|
||||
|
||||
|
||||
|
||||
```cmd
|
||||
cmd
|
||||
|
||||
:: Set the environment variables after you have downloaded and unzipped the mkl package,
|
||||
:: else CMake would throw an error as `Could NOT find OpenMP`.
|
||||
set CMAKE_INCLUDE_PATH={Your directory}\mkl\include
|
||||
set LIB={Your directory}\mkl\lib;%LIB%
|
||||
|
||||
:: Read the content in the previous section carefully before you proceed.
|
||||
:: [Optional] If you want to override the underlying toolset used by Ninja and Visual Studio with CUDA, please run the following script block.
|
||||
:: "Visual Studio 2019 Developer Command Prompt" will be run automatically.
|
||||
:: Make sure you have CMake >= 3.12 before you do this when you use the Visual Studio generator.
|
||||
set CMAKE_GENERATOR_TOOLSET_VERSION=14.27
|
||||
set DISTUTILS_USE_SDK=1
|
||||
for /f "usebackq tokens=*" %i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -version [15^,17^) -products * -latest -property installationPath`) do call "%i\VC\Auxiliary\Build\vcvarsall.bat" x64 -vcvars_ver=%CMAKE_GENERATOR_TOOLSET_VERSION%
|
||||
|
||||
:: [Optional] If you want to override the CUDA host compiler
|
||||
set CUDAHOSTCXX=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\cl.exe
|
||||
|
||||
python setup.py develop
|
||||
|
||||
```
|
||||
|
||||
If you are building for NVIDIA's Jetson platforms (Jetson Nano, TX1, TX2, AGX Xavier), Instructions to install PyTorch for Jetson Nano are [available here](https://devtalk.nvidia.com/default/topic/1049071/jetson-nano/pytorch-for-jetson-nano/)
|
||||
|
||||
---
|
||||
|
||||
## Build Options
|
||||
|
||||
### Build only what you need
|
||||
|
||||
`python setup.py build` will build everything by default, but sometimes you are
|
||||
only interested in a specific component.
|
||||
|
||||
- Working on a test binary? Run `(cd build && ninja bin/test_binary_name)` to
|
||||
rebuild only that test binary (without rerunning cmake). (Replace `ninja` with
|
||||
`make` if you don't have ninja installed).
|
||||
- Don't need Caffe2? Pass `BUILD_CAFFE2=0` to disable Caffe2 build.
|
||||
|
||||
On the initial build, you can also speed things up with the environment
|
||||
variables `DEBUG`, `USE_DISTRIBUTED`, `USE_MKLDNN`, `USE_CUDA`, `BUILD_TEST`, `USE_FBGEMM`, `USE_NNPACK` and `USE_QNNPACK`.
|
||||
|
||||
- `DEBUG=1` will enable debug builds (-g -O0)
|
||||
- `REL_WITH_DEB_INFO=1` will enable debug symbols with optimizations (-g -O3)
|
||||
- `USE_DISTRIBUTED=0` will disable distributed (c10d, gloo, mpi, etc.) build.
|
||||
- `USE_MKLDNN=0` will disable using MKL-DNN.
|
||||
- `USE_CUDA=0` will disable compiling CUDA (in case you are developing on something not CUDA related), to save compile time.
|
||||
- `USE_ROCM=0` will disable compiling ROCm.
|
||||
- `BUILD_TEST=0` will disable building C++ test binaries.
|
||||
- `USE_FBGEMM=0` will disable using FBGEMM (quantized 8-bit server operators).
|
||||
- `USE_NNPACK=0` will disable compiling with NNPACK.
|
||||
- `USE_QNNPACK=0` will disable QNNPACK build (quantized 8-bit operators).
|
||||
- `USE_XNNPACK=0` will disable compiling with XNNPACK.
|
||||
|
||||
For example:
|
||||
|
||||
```bash
|
||||
DEBUG=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
|
||||
```
|
||||
|
||||
For subsequent builds (i.e., when `build/CMakeCache.txt` exists), the build
|
||||
options passed for the first time will persist; please run `ccmake build/`, run
|
||||
`cmake-gui build/`, or directly edit `build/CMakeCache.txt` to adapt build
|
||||
options.
|
||||
|
||||
---
|
||||
|
||||
### Building PyTorch with ASAN
|
||||
|
||||
[ASAN](https://github.com/google/sanitizers/wiki/AddressSanitizer) is very
|
||||
useful for debugging memory errors in C++. We run it in CI, but here's how to
|
||||
get the same thing to run on your local machine.
|
||||
|
||||
First, install LLVM 8. The easiest way is to get [prebuilt
|
||||
binaries](http://releases.llvm.org/download.html#8.0.0) and extract them to
|
||||
folder (later called `$LLVM_ROOT`).
|
||||
|
||||
Then set up the appropriate scripts. You can put this in your `.bashrc`:
|
||||
|
||||
```bash
|
||||
LLVM_ROOT=<wherever your llvm install is>
|
||||
PYTORCH_ROOT=<wherever your pytorch checkout is>
|
||||
|
||||
LIBASAN_RT="$LLVM_ROOT/lib/clang/8.0.0/lib/linux/libclang_rt.asan-x86_64.so"
|
||||
build_with_asan()
|
||||
{
|
||||
LD_PRELOAD=${LIBASAN_RT} \
|
||||
CC="$LLVM_ROOT/bin/clang" \
|
||||
CXX="$LLVM_ROOT/bin/clang++" \
|
||||
LDSHARED="clang --shared" \
|
||||
LDFLAGS="-stdlib=libstdc++" \
|
||||
CFLAGS="-fsanitize=address -fno-sanitize-recover=all -shared-libasan -pthread" \
|
||||
CXX_FLAGS="-pthread" \
|
||||
USE_CUDA=0 USE_OPENMP=0 BUILD_CAFFE2_OPS=0 USE_DISTRIBUTED=0 DEBUG=1 \
|
||||
python setup.py develop
|
||||
}
|
||||
|
||||
run_with_asan()
|
||||
{
|
||||
LD_PRELOAD=${LIBASAN_RT} $@
|
||||
}
|
||||
|
||||
# you can look at build-asan.sh to find the latest options the CI uses
|
||||
export ASAN_OPTIONS=detect_leaks=0:symbolize=1:strict_init_order=true
|
||||
export UBSAN_OPTIONS=print_stacktrace=1:suppressions=$PYTORCH_ROOT/ubsan.supp
|
||||
export ASAN_SYMBOLIZER_PATH=$LLVM_ROOT/bin/llvm-symbolizer
|
||||
```
|
||||
|
||||
Then you can use the scripts like:
|
||||
|
||||
```
|
||||
suo-devfair ~/pytorch ❯ build_with_asan
|
||||
suo-devfair ~/pytorch ❯ run_with_asan python test/test_jit.py
|
||||
```
|
||||
|
||||
### Getting `ccache` to work
|
||||
|
||||
The scripts above specify the `clang` and `clang++` binaries directly, which
|
||||
bypasses `ccache`. Here's how to get `ccache` to work:
|
||||
|
||||
1. Make sure the ccache symlinks for `clang` and `clang++` are set up (see
|
||||
CONTRIBUTING.md)
|
||||
2. Make sure `$LLVM_ROOT/bin` is available on your `$PATH`.
|
||||
3. Change the `CC` and `CXX` variables in `build_with_asan()` to point
|
||||
directly to `clang` and `clang++`.
|
||||
|
||||
### Why this stuff with `LD_PRELOAD` and `LIBASAN_RT`?
|
||||
|
||||
The “standard” workflow for ASAN assumes you have a standalone binary:
|
||||
|
||||
1. Recompile your binary with `-fsanitize=address`.
|
||||
2. Run the binary, and ASAN will report whatever errors it find.
|
||||
|
||||
Unfortunately, PyTorch is a distributed as a shared library that is loaded by
|
||||
a third-party executable (Python). It’s too much of a hassle to recompile all
|
||||
of Python every time we want to use ASAN. Luckily, the ASAN folks have a
|
||||
workaround for cases like this:
|
||||
|
||||
1. Recompile your library with `-fsanitize=address -shared-libasan`. The
|
||||
extra `-shared-libasan` tells the compiler to ask for the shared ASAN
|
||||
runtime library.
|
||||
2. Use `LD_PRELOAD` to tell the dynamic linker to load the ASAN runtime
|
||||
library before anything else.
|
||||
|
||||
More information can be found
|
||||
[here](https://github.com/google/sanitizers/wiki/AddressSanitizerAsDso).
|
||||
|
||||
### Why LD_PRELOAD in the build function?
|
||||
|
||||
We need `LD_PRELOAD` because there is a cmake check that ensures that a
|
||||
simple program builds and runs. If we are building with ASAN as a shared
|
||||
library, we need to `LD_PRELOAD` the runtime library, otherwise there will
|
||||
dynamic linker errors and the check will fail.
|
||||
|
||||
We don’t actually need either of these if we fix the cmake checks.
|
||||
|
||||
### Why no leak detection?
|
||||
|
||||
Python leaks a lot of memory. Possibly we could configure a suppression file,
|
||||
but we haven’t gotten around to it.
|
65
Contributor-Guide/setup/Prerequisites.md
Normal file
65
Contributor-Guide/setup/Prerequisites.md
Normal file
@ -0,0 +1,65 @@
|
||||
## Prerequisites
|
||||
|
||||
Source: https://github.com/pytorch/pytorch#prerequisites
|
||||
|
||||
<!-- If you are installing from source, you will need: -->
|
||||
To develop PyTorch you will need:
|
||||
- Python 3.8 or later (for Linux, Python 3.8.1+ is needed)
|
||||
- A C++17 compatible compiler, such as clang
|
||||
|
||||
We highly recommend installing an [Anaconda](https://www.anaconda.com/distribution/#download-section) environment. You will get a high-quality BLAS library (MKL) and you get controlled dependency versions regardless of your Linux distro.
|
||||
|
||||
### CUDA Support
|
||||
If you want to compile with CUDA support, install the following (note that CUDA is not supported on macOS)
|
||||
- [NVIDIA CUDA](https://developer.nvidia.com/cuda-downloads) 11.0 or above
|
||||
- [NVIDIA cuDNN](https://developer.nvidia.com/cudnn) v7 or above
|
||||
- [Compiler](https://gist.github.com/ax3l/9489132) compatible with CUDA
|
||||
|
||||
Note: You could refer to the [cuDNN Support Matrix](https://docs.nvidia.com/deeplearning/cudnn/pdf/cuDNN-Support-Matrix.pdf) for cuDNN versions with the various supported CUDA, CUDA driver and NVIDIA hardware
|
||||
|
||||
### ROCm Support
|
||||
If you want to compile with ROCm support, install
|
||||
- [AMD ROCm](https://rocmdocs.amd.com/en/latest/Installation_Guide/Installation-Guide.html) 4.0 and above installation
|
||||
- ROCm is currently supported only for Linux systems.
|
||||
|
||||
---
|
||||
|
||||
## Install Dependencies
|
||||
|
||||
**Common**
|
||||
|
||||
```bash
|
||||
conda install cmake ninja
|
||||
# Run this command from the PyTorch directory after cloning the source code using the “Get the PyTorch Source“ section below
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
**On Linux**
|
||||
|
||||
```bash
|
||||
conda install mkl mkl-include
|
||||
# CUDA only: Add LAPACK support for the GPU if needed
|
||||
conda install -c pytorch magma-cuda110 # or the magma-cuda* that matches your CUDA version from https://anaconda.org/pytorch/repo
|
||||
|
||||
# (optional) If using torch.compile with inductor/triton, install the matching version of triton
|
||||
# Run from the pytorch directory after cloning
|
||||
make triton
|
||||
```
|
||||
|
||||
**On MacOS**
|
||||
|
||||
```bash
|
||||
# Add this package on intel x86 processor machines only
|
||||
conda install mkl mkl-include
|
||||
# Add these packages if torch.distributed is needed
|
||||
conda install pkg-config libuv
|
||||
```
|
||||
|
||||
**On Windows**
|
||||
|
||||
```bash
|
||||
conda install mkl mkl-include
|
||||
# Add these packages if torch.distributed is needed.
|
||||
# Distributed package support on Windows is a prototype feature and is subject to changes.
|
||||
conda install -c conda-forge libuv=1.39
|
||||
```
|
Reference in New Issue
Block a user