Multi-node GPU training in a Lingjun cluster runs over Remote Direct Memory Access (RDMA). By default, the NVIDIA Collective Communications Library (NCCL) can pick an address that is not routable between nodes, which breaks distributed training. Point NCCL at a routable address to keep cross-node communication working.
How it works
In an RDMA over Converged Ethernet v2 (RoCE v2) environment, NCCL determines the communication address from an entry in the Global Identifier (GID) table of the RDMA network interface card (NIC). Explicitly specify which GID to use. Otherwise, cross-node communication fails.
Two methods are available. Choose either one:
Method 1: automatic selection — Set
NCCL_IB_ADDR_FAMILYtoAF_INET6. NCCL then scans the GID table and selects the correct entry. This method adds only one environment variable to the pod, but it requires a specific NCCL version.Method 2: dynamic probing — Read the GID index from sysfs in the container startup script, and then set
NCCL_IB_GID_INDEXexplicitly. This method does not depend on the NCCL version and works in all network modes. If you set both variables,NCCL_IB_GID_INDEXtakes effect.
The two network modes differ in only one respect: the NCCL version from which AF_INET6 becomes available. For the definitions of the two network modes and how to tell them apart, see Step 1: Confirm the network mode and NCCL version.
Network mode | Minimum NCCL version for | Reason and workaround for earlier versions |
hostNetwork mode | ≥ 2.21 |
|
Container network mode | ≥ 2.26.2 | Applies to both shared mode and exclusive mode. Before 2.26.2, the NCCL GID automatic selection logic has a defect in the container sysfs view. Setting |
Apart from this version threshold, the rest of the configuration is identical across network modes. Your choice of method therefore depends on two factors: whether your NCCL version meets the minimum for your network mode, and how many RDMA NICs you use.
When you use multiple RDMA NICs, the available GID index may differ between NICs, and dynamic probing can produce the result for only one NIC. Method 1 is recommended for this scenario. If your NCCL version is earlier than the minimum for your network mode, upgrade NCCL as described in Step 1 before you use Method 1.
Recommended configuration: Use NCCL 2.26.2 or later and set NCCL_IB_ADDR_FAMILY=AF_INET6. This combination applies to all network modes and requires only one environment variable.
Prerequisites
ACK Lingjun cluster: An existing Container Service for Kubernetes (ACK) Lingjun cluster whose compute network uses the IPv6 network mode. For more information, see Create an ACK Lingjun cluster.
Lingjun node specifications: Lingjun nodes equipped with an RDMA NIC that supports RoCE v2, such as the NVIDIA/Mellanox ConnectX series.
Training image: An image with the RDMA user-space libraries installed, such as libibverbs and librdmacm.
Training scenario: GPU distributed training with PyTorch and NCCL.
Pod configuration: A pod that has the permissions, shared memory, and device resources that RDMA requires, as described in Step 3.
Step 1: Confirm the network mode and NCCL version
Confirm the network mode
Check whether hostNetwork: true is set in the spec field of the pod:
hostNetwork mode:
hostNetwork: trueis set.Container network mode:
hostNetworkis not set. Based on how the RDMA NIC is allocated, this mode is further divided into:Shared mode: Multiple pods share the same physical RDMA NIC through sub-interfaces.
Exclusive mode: A pod exclusively uses an entire RDMA NIC.
Confirm the NCCL version
Use the version shown in the NCCL version X.Y.Z line of the NCCL_DEBUG=INFO log. To produce this log, set NCCL_DEBUG to INFO for the workload, as described in Step 3.
Do not use torch.cuda.nccl.version() to determine the version. The nvidia-nccl-cu12 package installed through pip dynamically overrides the NCCL library that PyTorch links at compile time. As a result, torch.cuda.nccl.version() returns the compile-time version number, which may differ from the version that actually runs.
Upgrade NCCL if your version is below the threshold
If your NCCL version is earlier than the minimum for your network mode listed in How it works, run pip install nvidia-nccl-cu12 to upgrade. NCCL 2.26.2 or later is the recommended target version for all network modes. This package dynamically overrides the NCCL library linked in the image at runtime, so you do not need to rebuild the image. Confirm the resulting version in the NCCL_DEBUG=INFO log, as described earlier in this step.
Step 2: Configure NCCL environment variables
Method 1: Automatic selection (NCCL_IB_ADDR_FAMILY)
Add one environment variable to the container, and NCCL scans the GID table and selects the correct RoCE v2 entry. Method 1 requires an NCCL version that meets the minimum for your network mode. For the minimums, see How it works.
env:
- name: NCCL_IB_ADDR_FAMILY
value: "AF_INET6"When you use Method 1, you must set this variable in all network modes, including hostNetwork mode. If you do not set it, NCCL selects GID index 0, which usually holds the link-local address configured by the driver (fe80::...). That address cannot be routed across nodes, so cross-node communication fails.
Method 2: Dynamic probing (NCCL_IB_GID_INDEX)
Read the GID table of the current pod when the container starts, probe the correct index, and export NCCL_IB_GID_INDEX. Method 2 works with any NCCL version and in all network modes. Use Method 2 when your NCCL version is earlier than the minimum for your network mode. For the minimums, see How it works.
After you explicitly set NCCL_IB_GID_INDEX, NCCL skips the automatic selection logic, so it does not trigger the container network mode defect that exists before 2.26.2.
Do not hardcode NCCL_IB_GID_INDEX. In shared mode, the sub-interfaces of multiple pods are attached to the same physical NIC, and the GID table of the physical port changes as other pods are created and destroyed. The same logical GID can map to a different index in a different pod or at a different scheduling time.
Place the following script in the container startup command, and run torchrun only after the script probes the index and exports it:
DEV=${NCCL_IB_HCA:-mlx5_bond_0}
ZERO="0000:0000:0000:0000:0000:0000:0000:0000"
IDX=""
for f in /sys/class/infiniband/$DEV/ports/1/gid_attrs/types/*; do
i=$(basename "$f")
t=$(cat "$f" 2>/dev/null)
g=$(cat "/sys/class/infiniband/$DEV/ports/1/gids/$i" 2>/dev/null)
# Requires, in order: the type is RoCE v2, the address is not all zeros, and it is not an fe80 link-local address
if [ "$t" = "RoCE v2" ] && [ "$g" != "$ZERO" ] && [ "${g#fe80}" = "$g" ]; then
IDX=$i
break
fi
done
if [ -z "$IDX" ]; then
echo "No usable RoCE v2 GID found. Check whether the device name $DEV is correct" >&2
exit 1
fi
export NCCL_IB_GID_INDEX=$IDX
echo "NCCL_IB_GID_INDEX=$IDX"Note the following points about the script:
Replace
DEVwith the name of the RDMA device in use, or pass the name through theNCCL_IB_HCAenvironment variable.Keep the check that excludes
fe80link-local addresses. This check is required in hostNetwork mode and exclusive mode, where the script may otherwise select an address that cannot be routed across nodes. In shared mode, the check does not affect the probing result, so all network modes can share the same script.Omit
NCCL_IB_ADDR_FAMILYwhen you use Method 2, because Method 2 does not require it.
Do not put the probing logic in the env field of the pod. A static env cannot read sysfs at runtime, so the logic must run in the container startup command.
Step 3: Submit a training job
A pod that uses RDMA communication requires the following basic configurations:
Security context: Add the
SYS_RESOURCEcapability, which raises the memlock limit, and theIPC_LOCKcapability, whichibv_reg_mrrequires to lock memory pages.Shared memory: Increase
/dev/shmwithsizeLimitbased on your actual needs. The default 64 MB/dev/shmof a container is usually not enough.RDMA device: Request the
rdma/hcaresource inresources.limits.
privileged: true also meets the preceding permission requirements, but its permission scope goes far beyond what RDMA needs, so it is not recommended.
The following example uses hostNetwork mode. For container network mode, including shared mode and exclusive mode, delete the hostNetwork: true and dnsPolicy lines and keep the rest of the configuration unchanged.
apiVersion: kubeflow.org/v1
kind: PyTorchJob
metadata:
name: nccl-training
spec:
pytorchReplicaSpecs:
Master:
replicas: 1
template:
spec:
hostNetwork: true # Container network mode: delete this line
dnsPolicy: ClusterFirstWithHostNet # Container network mode: delete this line
containers:
- name: training
image: <YOUR-TRAINING-IMAGE>
command: ["sh", "-c"]
args:
# Keep --nproc_per_node consistent with the number of GPUs that you request
- torchrun --nproc_per_node=8 --nnodes=$PET_NNODES --node_rank=$PET_NODE_RANK --master_addr=$PET_MASTER_ADDR --master_port=$PET_MASTER_PORT train.py
env:
- name: NCCL_IB_ADDR_FAMILY
value: "AF_INET6"
securityContext:
capabilities:
add: ["SYS_RESOURCE", "IPC_LOCK"]
resources:
limits:
nvidia.com/gpu: "8"
rdma/hca: "1"
volumeMounts:
- name: shm
mountPath: /dev/shm
volumes:
- name: shm
emptyDir:
medium: Memory
sizeLimit: 8Gi
Worker:
replicas: 1
template:
spec:
# The configuration is identical to that of Master
hostNetwork: true # Container network mode: delete this line
dnsPolicy: ClusterFirstWithHostNet # Container network mode: delete this line
containers:
- name: training
image: <YOUR-TRAINING-IMAGE>
command: ["sh", "-c"]
args:
- torchrun --nproc_per_node=8 --nnodes=$PET_NNODES --node_rank=$PET_NODE_RANK --master_addr=$PET_MASTER_ADDR --master_port=$PET_MASTER_PORT train.py
env:
- name: NCCL_IB_ADDR_FAMILY
value: "AF_INET6"
securityContext:
capabilities:
add: ["SYS_RESOURCE", "IPC_LOCK"]
resources:
limits:
nvidia.com/gpu: "8"
rdma/hca: "1"
volumeMounts:
- name: shm
mountPath: /dev/shm
volumes:
- name: shm
emptyDir:
medium: Memory
sizeLimit: 8GiThe training Operator automatically injects PET_NNODES, PET_NODE_RANK, PET_MASTER_ADDR, and PET_MASTER_PORT in the example, so you do not need to set them manually. Master and Worker can use exactly the same startup command.
The example does not set NCCL_DEBUG. Because Step 1 reads the NCCL version from the NCCL_DEBUG=INFO log and Step 4 reads the RDMA device from the same log, add NCCL_DEBUG with the value INFO to env when you need that log.
If you use Method 2, place the probing script from Step 2 before the torchrun command in args, and delete NCCL_IB_ADDR_FAMILY from env.
Step 4: Verify the configuration
Single-node verification
Run the following command in the pod to confirm that NCCL can complete an AllReduce operation:
python3 - <<'EOF'
import os
os.environ['NCCL_DEBUG'] = 'INFO'
os.environ.setdefault('MASTER_ADDR', '127.0.0.1')
os.environ.setdefault('MASTER_PORT', '29500')
import torch
import torch.distributed as dist
dist.init_process_group(backend='nccl', world_size=1, rank=0)
t = torch.randn(1000, device='cuda:0')
dist.all_reduce(t)
print('NCCL AllReduce OK')
dist.destroy_process_group()
EOFIn the NCCL_DEBUG=INFO log, confirm that an RDMA device such as mlx5_bond appears, instead of Socket-only communication.
Cross-node verification
Submit a two-node training job as described in Step 3. Confirm that the job starts normally, that the log contains no NCCL errors, and that cross-node AllReduce completes.
A successful single-node verification does not mean that cross-node communication works. An incorrect GID selection means that data packets cannot leave the local host, and single-node communication still behaves normally because traffic loops back. Always complete the cross-node verification before you start production training.
NCCL environment variables
NCCL environment variable | Value | When it is needed | Description |
|
| Required when you use Method 1, in all network modes | Lets NCCL automatically select the correct GID. The minimum NCCL version depends on the network mode. For the minimums, see How it works. |
| The index obtained by dynamic probing | Used with Method 2 | After you set this variable explicitly, NCCL skips the automatic selection logic. It takes precedence over |
| For example, | When you need to specify a NIC | Restricts NCCL to a specific RDMA device. The probing script also uses this value to build the sysfs path. |
| An IPv6 prefix, such as | Optional, and usually not needed | Used only when multiple valid GID candidates exist in hostNetwork mode and you need to filter them by subnet. It cannot be used to work around the container network mode defect that exists before 2.26.2. |
|
| When you debug | Prints NCCL initialization and communication logs, which also show the NCCL version that actually runs. |
For more information about NCCL environment variables, see NCCL documentation.