Wrong pytorch version when building from source

username: krafczyk
cooked: <p>When I build pytorch from source, The generated torch/version.py file has a weird version number which I think is wrong.</p>
<p>I first clone pytorch like this <code>git clone --depth 1 --branch v2.0.1 https://github.com/pytorch/pytorch</code></p>
<p>Then I follow the instructions here: <a href="https://github.com/pytorch/pytorch#from-source" class="inline-onebox" rel="noopener nofollow ugc">GitHub - pytorch/pytorch: Tensors and Dynamic neural networks in Python with strong GPU acceleration</a><br>
to build from source. The problem is the installed wheels have a generated version.py like this:</p>
<pre><code class="lang-auto">__version__ = '2.0.0a0+gite9ebda2'
debug = False
cuda = '11.8'
git_version = 'e9ebda29d87ce0916ab08c06ab26fd3766a870e5'
hip = None
</code></pre>
<p>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.</p>
<p>Inspecting the <code>CMakeLists.txt</code> file, I see the <code>version.py</code> is generated with the tool <code>tool/generate_torch_version.py</code>. 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.</p>
<p>I then discovered <code>PYTORCH_BUILD_VERSION</code> and <code>PYTORCH_BUILD_NUMBER</code> environment variables which are also mentioned in <code>setup.py</code> as used to make pytorch builds for release.</p>
<p>Thus my question is: Is <code>PYTORCH_BUILD_VERSION</code> and <code>PYTORCH_BUILD_NUMBER</code> actually the accepted method for making pytorch builds of a particular version? or have I done something incorrect?</p>

username: ptrblck
cooked: <p>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 <code>python setup.py clean</code> the build directory before rebuilding the <code>v2.0.1</code> tag?</p>

username: krafczyk
cooked: <p>No. Here is my build script:</p>
<pre><code class="lang-auto">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=&lt;install_path&gt;
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//')
export PYTORCH_BUILD_NUMBER=0
python setup.py install
</code></pre>
<p>Does <code>python setup.py clean</code> do something more than delete files? I tried running the <code>CMakeLists.txt</code> custom command myself filling in the arguments which cmake would normally fill out like this:</p>
<pre><code class="lang-auto">python tools/generate_torch_version.py --is_debug=False --cuda_version='11.8' --hip_version=""
</code></pre>
<p>And I’m getting a <code>version.py</code> like I mentioned above.</p>

username: ptrblck
cooked: <p>It’s removing all temp. files, which might have had an older version tag with this particular commit.<br>
Note that the commit itself is not wrong as it’s <a href="https://github.com/pytorch/pytorch/commits/v2.0.1">the last one in <code>v2.0.1</code></a>. So, maybe you should just set the right version via the env variables.</p>

username: cooprm
cooked: <p>Is there a solution to this? I’m having this same issue and although I was able to adjust the version number in torch’s version.py and in the torch metadata, I now get this error when trying to import xformers:<br>
“xFormers was built for: PyTorch 2.0.1+cu118 with CUDA 1108 (you have 2.0.1+cu118)”</p>
<p>Where is xformers pulling this data from?</p>