How to get a fast debug build

Following Allow to specify specific files for debug info by albanD · Pull Request #111748 · pytorch/pytorch · GitHub being merged, there is a new compilation flag that can be used to specify debug informations ONLY for a given subset of the source files via:

# Your usual build
BUILD_CONFIG python develop

# Enable debug mode for two files
BUILD_CONFIG python clean
USE_CUSTOM_DEBINFO=torch/csrc/Module.cpp;aten/src/ATen/native/cpu/SortingKernel.cpp BUILD_CONFIG python develop

If you use ccache (as you should), this will only recompile these two files and relink the binary. You will now have full debug info for these files!

This has two main benefits: going from your usual build to enabling debug infos only recompiles the files you care about and the final binary only has bebug info for these two files. This means it won’t be gigabytes big and will not slow gdb to a crawl!

Side note, @malfet has another trick to do this if you don’t like the clean:

touch path/to/file.cpp; ninja -n >; sed '-O3/-g'; sh -f

Here is the correct command (replace sed with gsed on MacOS):

ninja -j1 -v -n torch_cpu |sed -e 's/-O[23]/-g/g' -e 's#\[[0-9]\+\/[0-9]\+\] \+##' |sh