Skip to content

Add nccl support for multi-node tensor parallelism - #28967

Merged
gaugarg-nv merged 5 commits into
ggml-org:masterfrom
zyang-dev:rpc_nccl
Nov 3, 2026
Merged

gaugarg-nv merged 5 commits into
ggml-org:masterfrom
zyang-dev:rpc_nccl

Conversation

@zyang-dev

@zyang-dev zyang-dev commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Add a backend interface for collectives across ranks running in separate processes, implement it in the CUDA backend using NCCL, and use it in the RPC server when available.

The implementation is on top of PR #26610.

Results on DGX spark:
single node:

./build/bin/llama-bench -hf unsloth/Qwen3.8-27B-GGUF:UD-Q4_K_M -ngl 99 -d 0,8192 -r 3
| model                          |       size |     params | backend    | ngl |            test |                  t/s |
| ------------------------------ | ---------: | ---------: | ---------- | --: | --------------: | -------------------: |
| qwen35 27B Q4_K - Medium       |  15.32 GiB |    27.32 B | CUDA       |  99 |           pp512 |       846.54 ± 18.78 |
| qwen35 27B Q4_K - Medium       |  15.32 GiB |    27.32 B | CUDA       |  99 |           tg128 |         13.02 ± 0.01 |
| qwen35 27B Q4_K - Medium       |  15.32 GiB |    27.32 B | CUDA       |  99 |   pp512 @ d8192 |       801.23 ± 10.67 |
| qwen35 27B Q4_K - Medium       |  15.32 GiB |    27.32 B | CUDA       |  99 |   tg128 @ d8192 |         12.64 ± 0.01 |

Two nodes with NCCL:

./build/bin/llama-bench -hf unsloth/Qwen3.8-27B-GGUF:UD-Q4_K_M -ngl 99 -sm tensor  --rpc NODE-1-IP:50052,NODE-2-IP:50052 -dev RPC0/RPC1 -d 0,8192 -r 3
| model                          |       size |     params | backend    | ngl |     sm | dev          |            test |                  t/s |
| ------------------------------ | ---------: | ---------: | ---------- | --: | -----: | ------------ | --------------: | -------------------: |
| qwen35 27B Q4_K - Medium       |  15.32 GiB |    27.32 B | CUDA,RPC   |  99 | tensor | RPC0/RPC1    |           pp512 |      1348.29 ± 38.62 |
| qwen35 27B Q4_K - Medium       |  15.32 GiB |    27.32 B | CUDA,RPC   |  99 | tensor | RPC0/RPC1    |           tg128 |         23.51 ± 0.19 |
| qwen35 27B Q4_K - Medium       |  15.32 GiB |    27.32 B | CUDA,RPC   |  99 | tensor | RPC0/RPC1    |   pp512 @ d8192 |      1282.35 ± 33.41 |
| qwen35 27B Q4_K - Medium       |  15.32 GiB |    27.32 B | CUDA,RPC   |  99 | tensor | RPC0/RPC1    |   tg128 @ d8192 |         22.86 ± 0.10 |

Two nodes with pairwise:

./build/bin/llama-bench -hf unsloth/Qwen3.8-27B-GGUF:UD-Q4_K_M -ngl 99 -sm tensor  --rpc NODE-1-IP:50052,NODE-2-IP:50052 -dev RPC0/RPC1 -d 0,8192 -r 3
| model                          |       size |     params | backend    | ngl |     sm | dev          |            test |                  t/s |
| ------------------------------ | ---------: | ---------: | ---------- | --: | -----: | ------------ | --------------: | -------------------: |
| qwen35 27B Q4_K - Medium       |  15.32 GiB |    27.32 B | CUDA,RPC   |  99 | tensor | RPC0/RPC1    |           pp512 |       961.61 ± 17.51 |
| qwen35 27B Q4_K - Medium       |  15.32 GiB |    27.32 B | CUDA,RPC   |  99 | tensor | RPC0/RPC1    |           tg128 |         22.33 ± 0.17 |
| qwen35 27B Q4_K - Medium       |  15.32 GiB |    27.32 B | CUDA,RPC   |  99 | tensor | RPC0/RPC1    |   pp512 @ d8192 |       911.15 ± 53.39 |
| qwen35 27B Q4_K - Medium       |  15.32 GiB |    27.32 B | CUDA,RPC   |  99 | tensor | RPC0/RPC1    |   tg128 @ d8192 |         22.14 ± 0.18 |

Ratio to a single node:

┌───────────────┬──────────────────────┬───────┐
│               │ pairwise (PR #26610) │ NCCL  │
├───────────────┼──────────────────────┼───────┤
│ pp512         │                1.14x │ 1.59x │
├───────────────┼──────────────────────┼───────┤
│ tg128         │                1.72x │ 1.81x │
├───────────────┼──────────────────────┼───────┤
│ pp512 @ d8192 │                1.14x │ 1.60x │
├───────────────┼──────────────────────┼───────┤
│ tg128 @ d8192 │                1.75x │ 1.81x │
└───────────────┴──────────────────────┴───────┘

Requirements

@zyang-dev
zyang-dev requested review from a team and ggerganov as code owners September 15, 2026 20:00
@zyang-dev
zyang-dev marked this pull request as draft September 15, 2026 20:00
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Sep 15, 2026
@am17an

am17an commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Do you have any llama-bench results?

Comment thread ggml/include/ggml-backend.h Outdated
Comment on lines +212 to +220
// Same as above, but each rank is a separate process, so a process owns one backend of the
// communicator rather than all of them.
// One rank produces the unique id. Every other rank needs it, out of band, before calling
// comm_init_rank, which blocks until all ranks have joined. Free with ggml_backend_comm_free_t.
#define GGML_BACKEND_COMM_UNIQUE_ID_SIZE 128
typedef bool (*ggml_backend_comm_get_unique_id_t)(void * id_out);
typedef void * (*ggml_backend_comm_init_rank_t)(ggml_backend_t backend, const void * id, int rank, int world_size);
typedef bool (*ggml_backend_comm_allreduce_rank_t)(void * comm_ctx, struct ggml_tensor * tensor);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without having looked at any of the other code, changes to the ggml backend API like this should be avoided if at all possible.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added llama-bench results.

Removed ggml_backend_comm_allreduce_rank_t and now reuse ggml_backend_comm_allreduce_tensor for per-rank communicators.

@am17an , @JohannesGaessler, do you have any suggestions for handling ggml_backend_comm_get_unique_id_t and ggml_backend_comm_init_rank_t?

@gaugarg-nv gaugarg-nv Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JohannesGaessler we are taking the same approach for single-process multi-GPU where all comm typedefs are defined in ggml-backend.h.

An alternative could be to move all these typedefs to ggml-backend-impl.h to not expose them in the public API.

@am17an

am17an commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

what's the reason why using NCCL is so much faster for PP vs TG? Maybe there is something to optimize in the generic path too

@zyang-dev

Copy link
Copy Markdown
Contributor Author

what's the reason why using NCCL is so much faster for PP vs TG? Maybe there is something to optimize in the generic path too

@am17an,
Decode communicates far fewer tensor elements per collective than prefill. This explains why NCCL and pairwise have similar decode performance, while NCCL is significantly faster during prefill.

I tested three optimizations on the pairwise path:

  1. Duplex exchange (ggml-rpc.cpp). Previously, rank 0 sent its entire payload before receiving, leaving one direction of the full-duplex link idle during each transfer phase. Changed comm_allreduce_pairwise to exchange chunks, with both ranks sending a chunk and then receiving, allowing traffic in both directions concurrently.

  2. Deeper send queue (transport.cpp). Previously, rdma_send posted one 256 KiB work request and waited for completion before posting the next. Changed it to allow up to four outstanding sends, reducing gaps between transfers. Increasing the depth further to eight did not improve performance.

  3. Direct registered buffers. Previously, the data path was GPU → application host buffer → RDMA host buffer → network, with the reverse copies on receive. Replaced the intermediate buffers with persistent, pinned host buffers registered directly with RDMA: GPU → registered host buffer → network. This eliminated the extra host-to-host copies while retaining GPU ↔ host transfers.

Further improvement likely requires restructuring the collective into an overlapping pipeline of GPU transfers, network communication, and reduction.

┌─────────────────────────────┬─────────────────┬────────────────┐
│                             │      pp512      │ vs single node │
├─────────────────────────────┼─────────────────┼────────────────┤
│ pairwise (PR #26610)        │  961.61 ± 17.51 │          1.14x │
├─────────────────────────────┼─────────────────┼────────────────┤
│ + duplex exchange           │ 1057.67 ± 12.13 │          1.25x │
├─────────────────────────────┼─────────────────┼────────────────┤
│ + deep send queue           │  1144.75 ± 9.24 │          1.35x │
├─────────────────────────────┼─────────────────┼────────────────┤
│ + direct registered buffers │ 1213.09 ± 36.06 │          1.43x │
├─────────────────────────────┼─────────────────┼────────────────┤
│ NCCL                        │ 1348.29 ± 38.62 │          1.59x │
└─────────────────────────────┴─────────────────┴────────────────┘

@am17an

am17an commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

Ah okay nice these can be even added to the original PR I think. @ggerganov perhaps it may help in the metal speedup. Do you have a branch with these changes?

@mamama1

mamama1 commented Sep 18, 2026

Copy link
Copy Markdown

errr I'm very confused, I've recently acquired a third 5060 Ti and started using tensor parallelism with NCCL enabled. Enabling NCCL doubled decode - I think prefill also increased significantly.

I admit that I don't fully understand (or barely understand) the mechanics underneath - that's why the following question came to my mind when reading this PR:

why does the PR say "Add nccl support for tensor parrallel" if that's something that apparently is already working?

thanks

@am17an

am17an commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

This PR is for NCCL support over RDMA, not related to your setup. @zyang-dev perhaps change the title to reflect it better

@zyang-dev zyang-dev changed the title Add nccl support for tensor parrallel Add nccl support for multi-node tensor parallelism Sep 20, 2026
@zyang-dev

Copy link
Copy Markdown
Contributor Author

Ah okay nice these can be even added to the original PR I think. @ggerganov perhaps it may help in the metal speedup. Do you have a branch with these changes?

The changes are on the rpc-pairwise-perf branch: https://github.com/zyang-dev/llama.cpp/tree/rpc-pairwise-perf

@zyang-dev

Copy link
Copy Markdown
Contributor Author

This PR is for NCCL support over RDMA, not related to your setup. @zyang-dev perhaps change the title to reflect it better

Added multi-node to the title.

@ggerganov
ggerganov force-pushed the rpc_tensor branch 5 times, most recently from cf00d3b to d09d1c7 Compare September 29, 2026 05:28
@ggerganov
ggerganov force-pushed the rpc_tensor branch 3 times, most recently from 267d466 to 2ab5211 Compare October 6, 2026 06:48
@am17an
am17an force-pushed the rpc_tensor branch 2 times, most recently from 3a10a77 to f6ae027 Compare October 6, 2026 10:16
@gaugarg-nv

Copy link
Copy Markdown
Contributor

@zyang-dev can you rebase your PR on TOT master and make master the target branch as #26610 is now merged?

@github-actions github-actions Bot added documentation Improvements or additions to documentation model Model specific labels Oct 13, 2026
@0cc4m 0cc4m removed the vendor label Oct 15, 2026
@gaugarg-nv

Copy link
Copy Markdown
Contributor

The PR looks in good shape to me.

@am17an @ggerganov @JohannesGaessler can you please take a look? This provides a much faster path on Nvidia GPUs.

@ggerganov ggerganov self-assigned this Oct 17, 2026
@am17an

am17an commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

what about doing the improvements here too? #28967 (comment). These would help non-NV GPUs too or NV GPUs running vulkan

@gaugarg-nv

Copy link
Copy Markdown
Contributor

what about doing the improvements here too? #28967 (comment). These would help non-NV GPUs too or NV GPUs running vulkan

Can that be done in separate PR?

@JohannesGaessler

Copy link
Copy Markdown
Contributor

I think the current implementation is coupled too tightly to NCCL-specific structures. My understanding is that the unique NCCL ID is generated once by one process and then broadcast to the other processes. So I would suggest that we instead change ggml_backend_comm_init_t to accept a single ggml_backend_t as well as a capacity n_backends for the total number of backends. And then add some function like ggml_backend_comm_attach_t that lets another backend connect to a pre-existing communicator. To my understanding the CUDA, Hexagon, and SYCL backends would need to be transitioned to the new interface. To avoid having this PR stuck in review hell for too long we can temporarily add some function like ggml_backend_comm_init_new_t and preferentially use that until the other backends have followed suit in follow-up PRs.

@ggml-org/ggml-rpc lease tell me if my understanding is wrong or my idea of how to do this is flawed.

@zyang-dev

Copy link
Copy Markdown
Contributor Author

@JohannesGaessler,
In your proposal, how do other ranks know rank 0's information in order to connect to it? which function will send rank 0's information to other processes?

@zyang-dev

zyang-dev commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

@JohannesGaessler ,
The comm_attach interface helps to get rid of ggml_backend_comm_init_rank_t, but it introduces additional work to change the code for the one-process scenario. And it seems we still need a new interface for bootstrapping the initial information exchange between processes (ranks).

Another option is to keep two different sets of APIs for the one-process and multi-process scenarios. I'll replace the two new interfaces (ggml_backend_comm_get_unique_id_t, ggml_backend_comm_init_rank_t) with more generic ones that can handle libraries like NCCL. For example:

typedef bool   (*ggml_backend_comm_bootstrap_callback)(void * user_data, const void * send_buf, void * recv_buf, size_t size);
typedef void * (*ggml_backend_comm_init_rank_t)(ggml_backend_t backend, int rank, int world_size, ggml_backend_comm_bootstrap_callback bootstrap_callback, void * bootstrap_callback_data);

The first one is implemented by the caller and moves the bytes between ranks. The library decides what to exchange and how many rounds to run, so different libraries can do their own initial information exchange. The second one is similar to the current ggml_backend_comm_init_rank_t, with the new parameter ggml_backend_comm_bootstrap_callback.

@gaugarg-nv

Copy link
Copy Markdown
Contributor

@rgerganov @am17an could you please review RPC changes?

@ggerganov

Copy link
Copy Markdown
Member

@am17an Let me know if you can reproduce the reported numbers on your DGX Sparks. On my pair I can't reproduce (i.e. perf is same as master), but I am starting to think that the reason is because one of my Sparks is under-clocked to 1200 Hz due to power issues.

@am17an

am17an commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

I can reproduce these numbers when both sparks run at 2400mhz


static void * ggml_backend_rpc_comm_init(ggml_backend_t * backends, size_t n_backends) {
if (n_backends != 2 || std::getenv("GGML_RPC_NO_COMM") != nullptr) {
if (n_backends < 2 || std::getenv("GGML_RPC_NO_COMM") != nullptr) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will probably not work for all RDMA devices right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this check should be fine for now. ggml_backend_rpc_comm_init is only used for pairwise and nccl currently. On a config that doesn't have nccl and has more than 2 nodes, the code will fall back to pairwise. rpc_server::comm_init rejects pairwise with the check (request.world != 2). So the client gets nullptr, and the meta backend uses its generic path, the same as before the change.

@am17an

am17an commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

I did not notice that each NCCL instance takes 2GB more per RPC node. Do we know why that is?

@zyang-dev

Copy link
Copy Markdown
Contributor Author

@am17an ,
Regarding the question of NCCL memory usage, I pushed a new change to set maxCTAs in ncclConfig_t to 8 for dgx spark in ggml_backend_cuda_comm_init_rank. NCCL allocates buffers for each channel, and on DGX Spark it picks 64 channels by default. This change reduces NCCL's memory usage on dgx spark and makes NCCL run faster on dgx spark for prefill. Can you try again with the latest changes?

…ate processes, implement it in the CUDA backend using NCCL, and use it in the RPC server when available.
…s. Remove the helper function backend_comm_proc_address
…ank_t to more generic multi-process backend communicator interfaces
@gaugarg-nv
gaugarg-nv merged commit 71efc55 into ggml-org:master Nov 3, 2026
24 of 28 checks passed
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Nov 6, 2026
* Add a backend interface for collectives across ranks running in separate processes, implement it in the CUDA backend using NCCL, and use it in the RPC server when available.

* Drop ggml_backend_comm_allreduce_rank_t and reuse ggml_backend_comm_allreduce_tensor for per-rank communicators

* reuse ggml_backend_cuda_comm_allreduce_nccl for per-rank communicators. Remove the helper function backend_comm_proc_address

* Change ggml_backend_comm_get_unique_id_t and ggml_backend_comm_init_rank_t to more generic multi-process backend communicator interfaces

* set maxCTAs in ncclConfig_t to 8 for dgx spark in ggml_backend_cuda_comm_init_rank

(cherry picked from commit 71efc55)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CUDA Related to the CUDA backend devops improvements to build systems and github actions ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants