Wrong pytorch version when building from source

When I build pytorch from source, The generated torch/version.py file has a weird version number which I think is wrong.

I first clone pytorch like this git clone --depth 1 --branch v2.0.1 https://github.com/pytorch/pytorch

Then I follow the instructions here: GitHub - pytorch/pytorch: Tensors and Dynamic neural networks in Python with strong GPU acceleration
to build from source. The problem is the installed wheels have a generated version.py like this:

__version__ = '2.0.0a0+gite9ebda2'
debug = False
cuda = '11.8'
git_version = 'e9ebda29d87ce0916ab08c06ab26fd3766a870e5'
hip = None

This is a problem because next, I want to test it however the test application requires torchvision. So I use pip to install the latest version which is allowed to work with the latest pytorch, but it sees version 2.0.0 even though I’ve checked out tag v2.0.1, thus it overwrites my installed pytorch which I built from scratch with its own 2.0.1 from pypi.

Inspecting the CMakeLists.txt file, I see the version.py is generated with the tool tool/generate_torch_version.py. Looking at this code, there seems to be a section meant to look at git tags and grab the version from there, but debugging shows that code section isn’t working, and the regex isn’t matching anything.

I then discovered PYTORCH_BUILD_VERSION and PYTORCH_BUILD_NUMBER environment variables which are also mentioned in setup.py as used to make pytorch builds for release.

Thus my question is: Is PYTORCH_BUILD_VERSION and PYTORCH_BUILD_NUMBER actually the accepted method for making pytorch builds of a particular version? or have I done something incorrect?

You can specify the env variables, but that’s not the common approach as PyTorch should detect its version based on the commit. Did you python setup.py clean the build directory before rebuilding the v2.0.1 tag?

No. Here is my build script:

git clone --depth 1 --branch v2.0.1 https://github.com/pytorch/pytorch
cd pytorch
git submodule sync
git submodule update --init --recursive
pip install -r requirements.txt
conda install -c conda-forge cmake ninja mkl mkl-include
conda install -c pytorch magma-cuda118

export CMAKE_PREFIX_PATH=<install_path>
export TORCH_CUDA_ARCH_LIST="6.1" # Only build for my GPU for now to speed compilation
export PYTORCH_BUILD_VERSION=$(git describe --tags --exact | sed 's/^v//')
python setup.py install

Does python setup.py clean do something more than delete files? I tried running the CMakeLists.txt custom command myself filling in the arguments which cmake would normally fill out like this:

python tools/generate_torch_version.py --is_debug=False --cuda_version='11.8' --hip_version=""

And I’m getting a version.py like I mentioned above.

It’s removing all temp. files, which might have had an older version tag with this particular commit.
Note that the commit itself is not wrong as it’s the last one in v2.0.1. So, maybe you should just set the right version via the env variables.