How can I change nccl version in pytorch?

ddoron9: <p>hi I’m using cuda 11.3 and if I run multi-gpus it freezes so I thought it would be solved if I change pytorch.cuda.nccl.version…</p>
<p>also is there any way to find nccl 2.10.3 in my env? because apt search nccl didn’t show any 2.10.3 version that shows in torch.cuda.nccl.version. I wonder if I remove 2.10.3, then torch would set the default version as 2.9.9.</p>
<pre><code class="lang-auto">nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Mon_May__3_19:15:13_PDT_2021
Cuda compilation tools, release 11.3, V11.3.109
Build cuda_11.3.r11.3/compiler.29920130_0
</code></pre>
<pre><code class="lang-auto">Python 3.8.8 (default, Apr 13 2021, 19:58:26) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.10.2+cu113'
&gt;&gt;&gt; torch.cuda.nccl.version()
(2, 10, 3)
</code></pre>
<pre><code class="lang-auto">libhttpasyncclient-java/focal 4.1.4-1 all
  HTTP/1.1 compliant asynchronous HTTP agent implementation

libnccl-dev/unknown 2.11.4-1+cuda11.6 amd64 [upgradable from: 2.9.9-1+cuda11.3]
  NVIDIA Collective Communication Library (NCCL) Development Files

libnccl2/unknown 2.11.4-1+cuda11.6 amd64 [upgradable from: 2.9.9-1+cuda11.3]
  NVIDIA Collective Communication Library (NCCL) Runtime

libpuppetlabs-http-client-clojure/focal 0.9.0-1 all
  Clojure wrapper around libhttpasyncclient-java

libvncclient1/focal-updates,focal-security 0.9.12+dfsg-9ubuntu0.3 amd64
  API to write one's own VNC server - client library

python-ncclient-doc/focal 0.6.0-2.1 all
  Documentation for python-ncclient (Python library for NETCONF clients)

python3-ncclient/focal 0.6.0-2.1 all
  Python library for NETCONF clients (Python 3)
</code></pre>
<p>thanks</p>

ptrblck: <p>The binaries ship with their own CUDA runtime, cuDNN, NCCL, etc. libs, so you won’t be able to change them directly.<br>
You could build PyTorch from source and use your locally installed NCCL via e.g.:</p>
<pre><code class="lang-auto">NCCL_INCLUDE_DIR="/usr/include/" NCCL_LIB_DIR="/usr/lib/" USE_SYSTEM_NCCL=1 python setup.py install
</code></pre>

ddoron9: <p>I deleted my torch downloaded from pip and tried to download from source</p>
<pre><code class="lang-auto">conda install astunparse numpy ninja pyyaml mkl mkl-include setuptools cmake cffi typing_extensions future six requests dataclasses
conda install -c pytorch magma-cuda110 
git clone --recursive https://github.com/pytorch/pytorch
cd pytorch
# if you are updating an existing checkout
git submodule sync
git submodule update --init --recursive --jobs 0
export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}

NCCL_INCLUDE_DIR="/usr/local/cuda-11.3/targets/x86_64-linux/include“ NCCL_LIB_DIR="/usr/local/cuda-11.3/targets/x86_64-linux/“lib USE_SYSTEM_NCCL=1 python setup.py install
</code></pre>
<p>but it crashes with error</p>
<pre><code class="lang-auto">--   Private Dependencies : pthreadpool;cpuinfo;qnnpack;pytorch_qnnpack;nnpack;XNNPACK;fbgemm;fp16;/root/anaconda3/lib/libmpicxx.so;/root/anaconda3/lib/libmpi.so;gloo;tensorpipe;foxi_loader;rt;fmt::fmt-header-only;kineto;gcc_s;gcc;dl
--   USE_COREML_DELEGATE     : OFF
-- Configuring incomplete, errors occurred!
</code></pre>

ptrblck: <p>Could you check in the build logs what exactly failed?</p>

ddoron9: <p>it was nccl p2p not the nccl version in pytorch. thanks!</p>

surak: <p>Speaking of PyTorch compiled from source, we have noticed that there are problems when PyTorch is compiled with one version of NCCL and then later in deployment, we use another version, even if it’s compiled to use dynamic symbols.</p>
<p>The question is: Is PyTorch able to swap versions of NCCL or do we have to recompile it for each NCCL upgrade?</p>

surak: <p>We noticed that some stuff seem hardcoded, like this: <a href="https://github.com/pytorch/pytorch/blob/428e02461f7b1079428012cd8c885bb892298c8c/torch/csrc/cuda/nccl.cpp#L335-L336" class="inline-onebox" rel="noopener nofollow ugc">pytorch/nccl.cpp at 428e02461f7b1079428012cd8c885bb892298c8c · pytorch/pytorch · GitHub</a></p>

ptrblck: <p>I don’t think manipulating dynamic links of any library is a supported use case and you would have to use it at your own risk (it can be a great debugging tool).</p>

surak: <p>Hey, that’s not the case. The thing is that NCCL got upgraded on our supercomputers, but PyTorch wasn’t. Are you saying that we would have to recompile PyTorch all again because of dynamic libraries? (The NCCL upgrade was from 2.11.4 to 2.12.7)</p>

ptrblck: <p>Yes, I think if you are using dynamic linking and are upgrading NCCL on your clusters, the safe approach would be to rebuild PyTorch. If that’s not a desired use case, try to use static linking.</p>

surak: <p>The suggestion that one uses static linking makes absolutely no sense.</p>
<p>The idea of using dynamic libraries is exactly one when the ABI is consistent (and NCCL is), one can interchange versions of the dynamically-loaded library. That’s literally what dynamic libraries are for.</p>

ptrblck: <p>You are right that static linking would not allow you to change libraries, but I also didn’t see any explanation of your actual use case besides pointing out problems when your deployment use case uses another NCCL version (by accident? on purpose? if so, why?)</p>
<p>So to your original question:</p><aside class="quote" data-post="9" data-topic="143942">
  <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/ptrblck/48/1823_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/how-can-i-change-nccl-version-in-pytorch/143942/9">How can I change nccl version in pytorch?</a> <a class="badge-category__wrapper " href="/c/torch-package-torch-deploy/34"><span data-category-id="34" data-drop-close="true" class="badge-category " title="this category is focused on python deployment of PyTorch models and specifically the torch::deploy and torch.package APIs. More can be found at pytorch.org in the docs…"><span class="badge-category__name">torch.package / torch::deploy</span></span></a>
  </div>
  <blockquote>
    I don’t think manipulating dynamic links of any library is a supported use case and you would have to use it at your own risk (it can be a great debugging tool).
  </blockquote>
</aside>

<p>If you see improvements, contributions are more than welcome.</p>

surak: <p>I am sorry if my answer missed crucial details. I should have been clearer and I will try to improve.</p>
<p>In the past, we found NCCL bugs which were showstoppers for some runs in larger scales (more than 512 nodes and 2048 gpus, for example). In that case, a simple replacement of NCCL (or, sometimes, UCX) was enough to fix things.</p>
<p>Their API has been stable enough that we can use them in an interchangeable way (at least during minor upgrades, such as from 2.10 to 2.14 and so on).</p>
<p>Given that PyTorch calls NCCL dynamically, there is in general little problem with that - better said: none so far. The problem lies in that those lines assume a version used at compile time and give a wrong answer when probing for the NCCL version.</p>
<p>In pytorch/torch/csrc/cuda/nccl.cpp, in line 334, instead of relying on the NCCL_MINOR and NCCL_PATCH, one could use the version detection which already exists on the code.</p>

chancody: <p>So is there any other solution to change nccl version in pytorch instead of compile pytorch myself in 2023 April?</p>

ptrblck: <p>You might be able to hack around in the binary and replace NCCL libraries, which is unsupported since the pip wheels and conda binaries ship with the tagged CUDA, cuDNN, and NCCL version by design.</p>

Mona_Jalal: <p>I am told in a git issue response from NCCL that I should use nccl 2.17.1 for solving some of my issues.</p>
<p>In that case, what should I change my pytorch version to? The current version works for   nccl: 2.12.10</p>
<p>I am using a yaml for dependencies like this.</p>
<p>train-env.yaml</p>
<pre><code class="lang-auto">$schema: https://azuremlschemas.azureedge.net/latest/environment.schema.json
name: nvidia_pytorch
build:
  path: ../../../data-science/environment/
tags:
  os: ubuntu
  os_version: 20.04
  hpcx: 2.10
  mpi: openmpi
  mpi_version: 4.1.2rc4
  ucx: 1.12.0
  cuda: 11.6.2
  cudnn: 8.4.0.27
  nccl: 2.12.10
  # nccl: 2.17.1
  rdma_core: 36.0
  nsight_compute: 2022.1.1.2
  nsight_systems: "2022.2.1.31-5fe97ab"
  nccl_test: 2.11.0
  # azureml-defaults: 1.41.0
  # mlflow: 1.25.1
  azureml-defaults: 1.50.0
  mlflow: 2.3.2
  transformers: 4.18.0
</code></pre>
<p>here is my requirements.txt</p>
<pre><code class="lang-auto"> # for local testing (cpu)
torchvision==0.12.0
torch==1.11.0
transformers==4.18.0

# for metrics reporting/plotting
# mlflow==1.25.1
# azureml-mlflow==1.41.0
mlflow==2.3.2
azureml-mlflow==1.50.0
matplotlib==3.5.2
tqdm==4.64.0
psutil==5.9.0

# for unit testing
pytest==7.1.2

</code></pre>
<p>and here is my Dockerfile:</p>
<pre><code class="lang-auto"># check release notes https://docs.nvidia.com/deeplearning/frameworks/pytorch-release-notes/index.html
FROM nvcr.io/nvidia/pytorch:22.04-py3

##############################################################################
# NCCL TESTS
##############################################################################
ENV NCCL_TESTS_TAG=v2.11.0

# NOTE: adding gencodes to support K80, M60, V100, A100
RUN mkdir /tmp/nccltests &amp;&amp; \
    cd /tmp/nccltests &amp;&amp; \
    git clone -b ${NCCL_TESTS_TAG} https://github.com/NVIDIA/nccl-tests.git &amp;&amp; \
    cd nccl-tests &amp;&amp; \
    make \
    MPI=1 MPI_HOME=/opt/hpcx/ompi \
    NVCC_GENCODE="-gencode=arch=compute_35,code=sm_35 -gencode=arch=compute_50,code=sm_50 -gencode=arch=compute_60,code=sm_60 -gencode=arch=compute_61,code=sm_61 -gencode=arch=compute_70,code=sm_70 -gencode=arch=compute_80,code=sm_80" \
    CUDA_HOME=/usr/local/cuda &amp;&amp; \
    cp ./build/* /usr/local/bin &amp;&amp; \
    rm -rf /tmp/nccltests

# Install dependencies missing in this container
# NOTE: container already has matplotlib==3.5.1 tqdm==4.62.0
COPY requirements.txt ./
RUN pip install -r requirements.txt


# add ndv4-topo.xml
RUN mkdir /opt/microsoft/
ADD ./ndv4-topo.xml /opt/microsoft

# to use on A100, enable env var below in your job
# ENV NCCL_TOPO_FILE="/opt/microsoft/ndv4-topo.xml"

# adjusts the level of info from NCCL tests
ENV NCCL_DEBUG="INFO"
ENV NCCL_DEBUG_SUBSYS="GRAPH,INIT,ENV"

# Relaxed Ordering can greatly help the performance of Infiniband networks in virtualized environments.
ENV NCCL_IB_PCI_RELAXED_ORDERING="1"
ENV CUDA_DEVICE_ORDER="PCI_BUS_ID"
ENV NCCL_SOCKET_IFNAME="eth0"
# ENV NCCL_SOCKET_IFNAME='lo'
ENV NCCL_IB_DISABLE="1"

</code></pre>

youkaichao1: <p>I also came across a nccl problem, where only 2.18.3 works for me while pytorch 2.2.0 requires 2.19.3 .</p>
<p>My solution is to write a nccl binding myself, and keep my nccl version separate from pytorch nccl version to avoid any conflict.</p>

LamForest: <aside class="quote" data-post="7" data-topic="107464">
  <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/ptrblck/48/1823_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/how-to-link-a-custom-nccl-version/107464/7">How to link a custom NCCL version</a> <a class="badge-category__wrapper " href="/c/distributed/12"><span data-category-id="12" style="--category-badge-color: #0088CC; --category-badge-text-color: #FFFFFF;" data-drop-close="true" class="badge-category "><span class="badge-category__name">distributed</span></span></a>
  </div>
  <blockquote>
    You can see <a href="https://github.com/pytorch/builder/blob/43e314f7d93b7e8f15a11739d7aa80d7c528202a/conda/pytorch-nightly/build.sh#L50" rel="noopener nofollow ugc">here</a> that NCCL is statically linked to the binaries and can take a look at the repository for more information about the build process. <img width="20" height="20" src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title="wink" alt="wink" class="emoji">
  </blockquote>
</aside>
<p>
According to above answer, NCCL is statically linked to pytorch. Therefore, the only way is to compile from source.</p>
<p>Is LD_PRELOAD possible?</p>

ptrblck: <p>The linked post is ~3.5 years old and the current PyTorch binaries use the <code>nvidia-nccl-cu12</code> package from PyPI, so <code>LD_PRELOAD</code> should work (or even manually replacing the pip wheel).<br>
You can verify it via e.g. <code>LD_DEBUG=libs</code> to see which NCCL library is loaded.</p>