How can I install torchvision with my self built from source torch

username: Zhiyi_Hu
cooked: <p>Hello! I need to and have managed to build from source for torch v2.4.1 using tag v2.4.1 in the pytorch source code. However, I also need torchvision, for which I use the command pip install torchvision torchaudio --index-url <a href="https://download.pytorch.org/whl/cu118" rel="noopener nofollow ugc">https://download.pytorch.org/whl/cu118</a> provided at <a href="https://pytorch.org/get-started/locally/" class="inline-onebox" rel="noopener nofollow ugc">Start Locally | PyTorch</a>. The command will not use my self built torch and auto uninstall it:</p>
<p>Installing collected packages: torch, torchvision, torchaudio<br>
Attempting uninstall: torch<br>
Found existing installation: torch 2.4.0a0+gitee1b680<br>
Uninstalling torch-2.4.0a0+gitee1b680:<br>
Successfully uninstalled torch-2.4.0a0+gitee1b680<br>
Successfully installed torch-2.4.1+cu118 torchaudio-2.4.1+cu118 torchvision-0.19.1+cu118</p>
<p>Do you know how to solve it? Thanks a lot!</p>

username: ptrblck
cooked: <p>While the commit matches the <code>v2.4.1</code> release, the actual version tag is wrong as I believe it’s defined via an env variable.<br>
I don’t know if trying to change the PyTorch version tag after a build would work, but you could try to install <code>torchvision</code> with <code>--no-deps</code> instead.</p>

username: Zhiyi_Hu
cooked: <p>Thanks for the reply! So it means although I use <code>git checkout v2.4.1</code> and build from it, the actual version is not v2.4.1, right?</p>
<p>Also, after installing <code>torchvision</code> with <code>--no-deps</code>, I find that the installed torchvision is not compatible with the torch I built. Is it also because the built torch from v2.4.1 is not version 2.4.1</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="Zhiyi_Hu" data-post="3" data-topic="209186">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zhiyi_hu/48/72574_2.png" class="avatar"> Zhiyi_Hu:</div>
<blockquote>
<p>So it means although I use <code>git checkout v2.4.1</code> and build from it, the actual version is not v2.4.1, right?</p>
</blockquote>
</aside>
<p>No, your source build matches the <code>v2.4.1</code> release as the same commit is used. However, the version tag stored in <code>torch.__version__</code> is showing the commit (as is expected for default source builds) and you could override it via using the <code>PYTORCH_VERSION</code> env variable if I’m not mistaken. Technically nothing will change in your build as you are already building <code>v.2.4.1</code> which just corresponds to commit <code>ee1b680</code>. (<a class="mention" href="/u/malfet">@malfet</a> please correct me if the env variable is wrong)</p>
<aside class="quote no-group" data-username="Zhiyi_Hu" data-post="3" data-topic="209186">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zhiyi_hu/48/72574_2.png" class="avatar"> Zhiyi_Hu:</div>
<blockquote>
<p>after installing <code>torchvision</code> with <code>--no-deps</code>, I find that the installed torchvision is not compatible with the torch I built.</p>
</blockquote>
</aside>
<p>Which <code>torchvision</code> version was installed and what kind of issues do you see?</p>

username: ligweshh
cooked: <p>I encountered a similar issue today and hope my comment can help.</p>
<p>I was building PyTorch from source and found that the version was 2.4.0 even though I downloaded <a href="https://github.com/pytorch/pytorch/releases/download/v2.4.1/pytorch-v2.4.1.tar.gz" rel="noopener nofollow ugc">pytorch-v2.4.1.tar.gz</a> via the release page. I found this <a href="https://discuss.pytorch.org/t/how-to-specify-pytorch-version-with-the-cuda-version-when-building-from-source/134447">question</a> and after setting environment variables <code>PYTORCH_BUILD_VERSION=2.4.1</code> and <code>PYTORCH_BUILD_NUMBER=1</code> and rebuilding the project, the version changed as expected.</p>
<p>However, torchvision throwed an exception:</p>
<pre data-code-wrap="txt"><code class="lang-txt">RuntimeError: operator torchvision::nms does not exist
</code></pre>
<p>I suspected that torchvision downloaded by pip was incompatible with my CUDA toolkit’s version and also built torchvision from source. It works now!</p>

username: KirilloCirillo
cooked: <p>Hi <a class="mention" href="/u/ligweshh">@ligweshh</a>, could you explain in some more detail what exactly you did? I am also facing the same problem as you. How exactly did you install from source? Did you pick a tag? Have you set some flags?</p>
<p>Thanks!</p>

username: ligweshh
cooked: <p><a class="mention" href="/u/kirillocirillo">@KirilloCirillo</a></p>
<p>Building torchvision:</p>
<ol>
<li>Build <code>torch</code> and ensure you remain in the same environment before proceeding with the following steps.</li>
<li>Check your <code>torch</code> version and find a corresponding <code>torchvision</code> release. The <code>torch</code> package I built is v2.4.1, so the <code>torchvision</code> should be <a href="https://github.com/pytorch/vision/archive/refs/tags/v0.19.1.tar.gz" rel="noopener nofollow ugc">v0.19.1</a>. The version comparison table can be found <a href="https://github.com/pytorch/vision?tab=readme-ov-file#installation" rel="noopener nofollow ugc">here</a>.</li>
<li>Check <code>torchvision</code>’s contribution document, especially section <a href="https://github.com/pytorch/vision/blob/main/CONTRIBUTING.md#dependencies" rel="noopener nofollow ugc">Dependencies</a> and section <a href="https://github.com/pytorch/vision/blob/main/CONTRIBUTING.md#clone-and-install-torchvision" rel="noopener nofollow ugc"> Clone and install torchvision</a>.</li>
<li>I set the environment variable <code>BUILD_VERSION</code> to <code>0.19.1</code>. If you download the source from the Github releases page, there’s a file named version.txt which also controls the version of the package you build, but I believe it can be overridden by <code>BUILD_VERSION</code>.</li>
<li>I set the environment variable <code>TORCH_CUDA_ARCH_LIST</code> to <code>6.0 6.1 6.2 7.0 7.2 7.5 8.0 8.6 8.7</code>. I’m not entirely sure if <code>TORCH_CUDA_ARCH_LIST</code> has the same effect here as it does when building <code>torch</code>, so I kept it consistent with the value I used for that.</li>
<li>I ran <code>python setup.py bdist_wheel</code> instead of <code>python setup.py develop</code> to generate a <code>.whl</code> file. This allows me to install <code>torchvision</code> using <code>pip install dist/torchvision-0.19.1-cp312-cp312-linux_x86_64.whl</code>, ensuring all files are fully placed in <code>site-packages</code>. Note that the official documentation mentions that <code>torchvision</code> does not officially support building from source and suggests adding <code>--no-build-isolation</code> flag. I forgot to add that and it still works, but you may want to add it.</li>
</ol>
<p>Example:</p>
<pre data-code-wrap="bash"><code class="lang-bash">export TORCH_CUDA_ARCH_LIST="6.0 6.1 6.2 7.0 7.2 7.5 8.0 8.6 8.7"
export BUILD_VERSION=0.19.1
python setup.py bdist_wheel
# Optional: install it in current environment.
pip install dist/torchvision-0.19.1-cp312-cp312-linux_x86_64.whl
</code></pre>