PyTorch 2.0 distribution that uses cuda only if available?

misberner: <p>Hey folks,</p>
<p>after upgrading to <code>torch==2.0</code> yesterday, I have found that I am no longer able to run torch programs if the system doesn’t have CUDA.</p>
<p>Here’s my observation on the various distributions:</p>
<pre><code class="lang-sh"># PyTorch 2.0 for use with CUDA, CUDA libs get installed as pip deps if unavailable on the system. Wheel size w/o deps: ~620MB
pip3 install torch==2.0
# PyTorch 2.0 with bundled CUDA 11.7. Wheel size w/o deps: 1.8GB
pip3 install torch==2.0+cu117 --extra-index-url https://download.pytorch.org/whl/cu117
# PyTorch 2.0 w/o CUDA support. Wheel size w/o deps: 195MB
pip3 install torch==2.0+cpu --extra-index-url https://download.pytorch.org/whl/cpu
</code></pre>
<p>If I now look at what got installed from the first option (<code>site-packages/torch/lib</code>), I see, among other things:</p>
<pre><code class="lang-auto">-rwxrwxr-x 1 ubuntu ubuntu 487M Apr 11 08:33 libtorch_cpu.so
-rwxrwxr-x 1 ubuntu ubuntu 627M Apr 11 08:33 libtorch_cuda.so
</code></pre>
<p>so my expectation would be that this distribution allows me to use Torch with or without CUDA support.</p>
<p>However, in reality, <code>import torch</code> fails on a non-CUDA system (w/o the CUDA pip deps installed), because <code>ldd libtorch_global_deps.so</code> shows that the global deps library – which is unconditionally loaded at package import time – is linked against a bunch of CUDA libraries (<code>libcublas.so</code>, <code>libcurand.so</code> and others), which then fails to load on a non-CUDA system.</p>
<p>This is apparently different from the behavior in <code>torch==1.11.0</code> (previous version I was using). Here, I also see</p>
<pre><code class="lang-auto">-rwxrwxr-x 1 ubuntu ubuntu 433M Apr 11 09:51 libtorch_cpu.so
-rwxrwxr-x 1 ubuntu ubuntu 994M Apr 11 09:50 libtorch_cuda.so
</code></pre>
<p>in the <code>lib</code> folder of the package, and I can indeed use CUDA on a CUDA-system, but the <code>libtorch_global_deps.so</code> does not link against any CUDA libraries:</p>
<pre><code class="lang-sh">$ ldd venv/lib/python3.9/site-packages/torch/lib/libtorch_global_deps.so | grep cuda
$
</code></pre>
<p>Does anyone have any insight into why this change was made? It makes it much much harder to use a consistent set of dependencies on various systems/architectures.</p>
<p>Now, this doesn’t matter if installing the right version of <code>torch</code> is regarded as a responsibility of the system, but in our case, we use <code>bazel</code> as our toolchain and thus require some level of hermetic homogeneity between building our production containers (using a base image with CUDA system libs) and, e.g., running basic functional tests in CI (on runners that don’t have GPU and where installing CUDA is a waste of time and space). Concretely, we don’t want to install over a GB of CUDA libraries as a Python dependency, because shipping them in a base image layer is more efficient. And we don’t want to install CUDA in the image used for our CI runner, because it will never have GPUs. But we <em>do</em> want to be able to run <code>bazel test</code>  on a GPU-enabled, CUDA-enabled Linux system, have Torch use CUDA, without <code>bazel</code> resolving dependencies differently from CI under the hood.</p>
<p>I guess we could somehow address this at the Bazel level if there’s no other way, but why do we now have to, when in torch 1.11 we didn’t, while still enjoying CUDA support?</p>

ptrblck: <aside class="quote no-group" data-username="misberner" data-post="1" data-topic="177230">
<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/misberner/48/59353_2.png" class="avatar"> misberner:</div>
<blockquote>
<p>However, in reality, <code>import torch</code> fails on a non-CUDA system (w/o the CUDA pip deps installed),</p>
</blockquote>
</aside>
<p>I don’t fully understand this claim. Are you installing the default PyTorch pip wheels with CUDA 11.7 dependencies (installed via CUDA pip  wheels) hosted on PyPI and are deleting them afterwards manually?<br>
Afterwards you are trying to <code>import torch</code> and are wondering why the import fails?</p>
<p>Without deleting libraries manually the CUDA-enabled wheels will still run:</p>
<pre data-code-wrap="python"><code class="lang-python">CUDA_VISIBLE_DEVICES="" python -c "import torch; print(torch.cuda.is_available()); print(torch.__version__)"
False
2.0.0+cu117
</code></pre>
<p>However, if you want to install CPU-only wheels you could select them from the install matrix.</p>
<aside class="quote no-group" data-username="misberner" data-post="1" data-topic="177230">
<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/misberner/48/59353_2.png" class="avatar"> misberner:</div>
<blockquote>
<p>This is apparently different from the behavior in <code>torch==1.11.0</code> (previous version I was using). Here, I also see</p>
<pre><code class="lang-auto">-rwxrwxr-x 1 ubuntu ubuntu 433M Apr 11 09:51 libtorch_cpu.so
-rwxrwxr-x 1 ubuntu ubuntu 994M Apr 11 09:50 libtorch_cuda.so
</code></pre>
<p>in the <code>lib</code> folder of the package, and I can indeed use CUDA on a CUDA-system, but the <code>libtorch_global_deps.so</code> does not link against any CUDA libraries:</p>
</blockquote>
</aside>
<p>This is caused by a change in our build process since previously we were statically linking CUDA math libs into <code>libtorch*</code> so it was not directly visible as dependent files.<br>
Besides that the same user experience was exposed: you can install the CUDA-enabled wheels on a CPU-only system and they will work (the default PyPI wheels). If you want to install the CPU-only wheels you could select them from the install matrix.</p>
<aside class="quote no-group" data-username="misberner" data-post="1" data-topic="177230">
<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/misberner/48/59353_2.png" class="avatar"> misberner:</div>
<blockquote>
<p>It makes it much much harder to use a consistent set of dependencies on various systems/architectures.</p>
</blockquote>
</aside>
<p>Could you describe what exactly is failing if you don’t manually delete dependencies?</p>

misberner: <p>Thanks for your response, you’re absolutely right. What I saw was actually a bug in the tool (based on <code>pex</code>) we use to create a Bazel lockfile out of a <code>requirements.txt</code>. It operates under the assumption that wheels for different platforms have all the same requirements, just annotated with environment markers, but that’s not the case for torch (as the requirements listed in the torch linux wheel metadata have linux/x86_64 environment markers, it wouldn’t hurt to have them in the darwin wheel as well, but they’re not there). However, I think the assumption of the tool is clearly too strict.</p>
<p>So the tool accidentally “deleted” the cuda deps, because they don’t occur in the darwin wheel. And this accidentally was the “right” thing for our scenario, as we ship our code in an image that has all required CUDA deps installed system-wide through the base image. But that right thing is clearly a hack given what the PyTorch team intended in terms of distribution flavors.</p>
<p>So I guess with one mystery solved, my questions can basically be condensed to the following:</p>
<ol>
<li>Is there a pre-compiled <code>torch</code> 2.0.0 package that neither bundles CUDA nor pulls in CUDA as Python dependencies, but instead uses CUDA libraries installed on the system?</li>
<li>Same as 1, but with additionally gracefully falling back to CPU-only mode if the required CUDA libs are not present on the system?</li>
<li>If the answer to either of the above is no, because this is a too uncommon setup, is there an option of creating such a package by compiling from (but without modifying the) source?</li>
</ol>
<blockquote>
<p>Besides that the same user experience was exposed: you can install the CUDA-enabled wheels on a CPU-only system and they will work (the default PyPI wheels). If you want to install the CPU-only wheels you could select them from the install matrix.</p>
</blockquote>
<p>It still seems that there is a gap between the two options (CUDA-enabled wheels with CUDA libs as pip dependencies vs. CPU-only wheels): while both run on a GPU-less system, the former needs an extra GB or so of dependencies installed. Not just as a formal requirement, but will indeed not run without these deps, even though on those systems the same functionality is offered as by the CPU-only package that doesn’t need this dependency.</p>
<blockquote>
<p>This is caused by a change in our build process since previously we were statically linking CUDA math libs into <code>libtorch*</code> so it was not directly visible as dependent files.</p>
</blockquote>
<p>Hmm, I see, thanks for the explanation.</p>

mapa17: <p>Hello, as a follow up to the posts above. Is it possible to get rid of the cpu related cuda library part (libtorch_cpu.so) if libtorch_cuda.so are present?<br>
thx.</p>

ptrblck: <p>Could you describe what “cpu related cuda library part” means?<br>
<code>libtorch_cpu.so</code> ships with needed symbols and you can check it via e.g. <code>readelf</code>.<br>
Some symbols would be:</p>
<pre><code class="lang-auto">...
554637: 0000000007bf5650  2432 FUNC    GLOBAL DEFAULT    8 mkl_spblas_lp64_avx512_zcoo0stluc__svout_seq
554638: 000000000ac1a850  1888 FUNC    GLOBAL DEFAULT    8 mkl_blas_avx512_xcscal
554639: 00000000052d53a0  2872 FUNC    GLOBAL DEFAULT    8 torch::jit::tensorexpr::registerize(std::shared_ptr&lt;torch::jit::tensorexpr::Stmt&gt;)
554640: 0000000002a98ee0   208 FUNC    GLOBAL DEFAULT    8 at::cpu::_upsample_nearest_exact1d_symint_out(at::Tensor&amp;, at::Tensor const&amp;, c10::ArrayRef&lt;c10::SymInt&gt;, c10::optional&lt;double&gt;)
554641: 00000000025eb780  1492 FUNC    GLOBAL DEFAULT    8 at::_ops::random_from_out::call(at::Tensor const&amp;, long, c10::optional&lt;long&gt;, c10::optional&lt;at::Generator&gt;, at::Tensor&amp;)
554642: 000000000696cd75   265 FUNC    GLOBAL DEFAULT    8 xnn_run_elu_nc_f32
554643: 0000000016c9a480    40 OBJECT  GLOBAL DEFAULT   24 at::native::_segment_reduce_offsets_backward_stub
554644: 000000000e709200    64 FUNC    GLOBAL DEFAULT    8 mkl_dft_mc3_ippsFFTInitAlloc_C_32f
554645: 0000000002c1db50    16 FUNC    GLOBAL DEFAULT    8 at::compositeexplicitautograd::indices(at::Tensor const&amp;)
554646: 0000000010a61680  1376 FUNC    GLOBAL DEFAULT    8 mkl_dft_def_dft_row_scopy_8
554647: 000000000d4307a0  1344 FUNC    GLOBAL DEFAULT    8 mkl_blas_avx2_ctrsv_unn
554648: 00000000029701e0   526 FUNC    GLOBAL DEFAULT    8 at::_ops::index_add_dimname::redispatch(c10::DispatchKeySet, at::Tensor const&amp;, at::Dimname, at::Tensor const&amp;, at::Tensor const&amp;, c10::Scalar const&amp;)
554649: 0000000002584990  1437 FUNC    GLOBAL DEFAULT    8 at::_ops::new_zeros::call(at::Tensor const&amp;, c10::ArrayRef&lt;c10::SymInt&gt;, c10::optional&lt;c10::ScalarType&gt;, c10::optional&lt;c10::Layout&gt;, c10::optional&lt;c10::Device&gt;, c10::optional&lt;bool&gt;)
554650: 0000000004ea3090 11652 FUNC    GLOBAL DEFAULT    8 torch::jit::MutationRemover::createSpecialMappedOp(torch::jit::Node*)
554651: 0000000005182980   401 FUNC    GLOBAL DEFAULT    8 torch::jit::tensorexpr::nnc_aten_quantized_mul_scalar_out(long, void**, long*, long*, long*, signed char*, long, long*)
554652: 000000000adead90    16 FUNC    GLOBAL DEFAULT    8 mkl_blas_mc3_cgemm_initialize_buffers
554653: 000000000e907c90  2064 FUNC    GLOBAL DEFAULT    8 mkl_dft_mc_ownsAdd_8u16u
554654: 0000000007fb08f0  1152 FUNC    GLOBAL DEFAULT    8 mkl_spblas_lp64_avx2_scoo1ntlnf__svout_seq
554655: 00000000053f1630    16 FUNC    WEAK   DEFAULT    8 std::_Sp_counted_ptr_inplace&lt;torch::lazy::DeviceData, std::allocator&lt;torch::lazy::DeviceData&gt;, (__gnu_cxx::_Lock_policy)2&gt;::_M_dispose()
554656: 000000000cec00b0  1456 FUNC    GLOBAL DEFAULT    8 mkl_blas_avx512_zherk_kernel_upper_b0
554657: 000000000b4f24d0  3200 FUNC    GLOBAL DEFAULT    8 mkl_vml_kernel_dExpI_Z0LAynn
554658: 00000000169b6d98    24 OBJECT  WEAK   DEFAULT   19 typeinfo for at::native::structured_index_copy_out
</code></pre>
<p>which are unrelated to CUDA as the library name suggests.</p>

mapa17: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a> thank you, I will have a look with readelf, but what I want to achieve is “easy” in the sense that I want to remove all cuda libraries (because of their size) that I dont need if I am certain I will be running pytorch on a GPU.</p>

ptrblck: <p>If you are certain you will be running PyTorch on a GPU, you won’t be able to remove any CUDA libraries. If you are not planning to use a GPU, install the CPU-only binaries.</p>

mapa17: <p>Thank you, that answers my question.</p>