Skip to content

feat(llama-cpp): ship NCCL in the CUDA images for tensor split - #13523

Merged
mudler merged 1 commit into
masterfrom
feat/cuda-nccl
Nov 26, 2026
Merged

mudler merged 1 commit into
masterfrom
feat/cuda-nccl

Conversation

@mudler-agent

@mudler-agent mudler-agent commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

What changes

llama.cpp's tensor split (split_mode: tensor) adds up partial results across the GPUs after every layer. ggml does this with NCCL when it is linked, and with its own slower code otherwise. Upstream's multi-GPU guide says tensor mode is suboptimal without NCCL. The LocalAI CUDA builders do not install NCCL, so CMake never finds it and every CUDA llama-cpp image runs the fallback.

This PR:

  • Adds .docker/install-nccl.sh. Both builder stages of backend/Dockerfile.llama-cpp run it before the source copy, so the layer is cached. It installs libnccl2 and libnccl-dev for cublas on amd64 (CUDA 12 and 13) and on arm64 with CUDA 13 (SBSA). It skips the Jetson image (CUDA 12 arm64, JetPack), because NVIDIA ships no NCCL for Jetson. The version is the newest NCCL built for the exact CUDA major.minor of the image (12.8 gets 2.26.2, 13.0 gets 2.28.9), so its driver requirement matches the CUDA libraries the image already bundles. NCCL_VERSION overrides the choice.
  • Keeps NCCL out of install-base-deps.sh on purpose. The forks that share that script (ik-llama-cpp, turboquant, bonsai, ...) would find NCCL too and each grow by the same amount, and the prebuilt base-grpc-* images would need a rebuild.
  • No CMake change: GGML_CUDA_NCCL is already on by default at the pinned llama.cpp, and its FindNCCL.cmake finds the apt install.
  • backend/cpp/llama-cpp/package.sh needs no new copy step. The existing transitive sweep in package-gpu-libs.sh copies libnccl.so.2 because libggml-cuda.so now has it as NEEDED (and so do llama-cpp-grpc and llama-cpp-rpc-server, which link the shared libnccl.so that CMake finds). A new scripts/build/check-nccl-bundle.sh runs at the end of package.sh and fails the build when:
    • a packaged ELF needs libnccl.so.2 and lib/ does not have it, or
    • the builder has NCCL but libggml-cuda.so does not link it (CMake missed NCCL).
      It also copies the NCCL license notice to licenses/NCCL-LICENSE.txt.
  • Adds a path-filter rule so a change to either NCCL script rebuilds only the Dockerfile.llama-cpp images, not the full matrix through the scripts/build/ catch-all.
  • Docs: a tensor-split preset and its caveats in the GPU acceleration page, and updated split_mode / tensor_split rows in the model options pages.

Image size

Compressed (gzip) size of the added libnccl.so.2, against the current latest backend images:

Image NCCL Raw Compressed Current image (compressed) Increase
-gpu-nvidia-cuda-12-llama-cpp (amd64) 2.26.2+cuda12.8 374 MB 287 MB 2063 MB +13.9%
-gpu-nvidia-cuda-13-llama-cpp (amd64) 2.28.9+cuda13.0 190 MB 188 MB 1707 MB +11.0%
-nvidia-l4t-cuda-13-arm64-llama-cpp (arm64) 2.28.9+cuda13.0 190 MB 188 MB 1898 MB +9.9%
-nvidia-l4t-arm64-llama-cpp (Jetson) none - - - 0

The library is one shared copy per image. The three binaries link the same file. The size is NVIDIA's prebuilt NCCL, which carries kernels for every GPU generation. A smaller library is possible: build NCCL from source with only the architectures LocalAI targets, or prune libnccl_static.a with nvprune and link that. Both add build time and a custom artifact, so I left them out of this PR. Happy to follow up if the size is a concern.

Fallback behaviour

  • Single GPU, or split_mode: layer/none: NCCL is loaded with the backend but never called. ggml calls into NCCL only when the tensor-split meta backend builds its communicator. One side effect of an NCCL build is upstream's: the CUDA VMM pool grants peer access to the other visible GPUs. With one GPU that loop does nothing.
  • NCCL present but cannot start (for example CUDA virtual devices, or ncclCommInitAll fails): upstream logs a warning and falls back to its internal all-reduce, then to the generic one. GGML_CUDA_ALLREDUCE=internal|none forces those paths at run time.
  • NCCL library missing at run time: with GGML_BACKEND_DL, ggml would fail to dlopen libggml-cuda.so and run on the CPU. That is why the library is bundled with the backend, the same way the cuBLAS libraries are, and why check-nccl-bundle.sh fails the image build instead of shipping an image without it. A dlopen-based optional NCCL would need a patch we carry against llama.cpp, so I did not do that here.

Tensor-split preset

Documented in docs/content/models/gpu-acceleration.md:

flash_attention: "on"
cache_type_k: f16
cache_type_v: f16
context_size: 16384
# tensor_split: "1,1"
options:
  - split_mode:tensor
  - fit_params:false

Checked against the pinned llama.cpp:

  • Flash attention: auto turns it on in tensor mode, and off fails the load.
  • Auto-fit: common_params_fit throws for tensor split, and llama.cpp loads unfitted with a warning, so the preset turns it off and sets context_size.
  • Quantized KV: accepted since TP: quantized KV cache support ggml-org/llama.cpp#23792.
  • Architectures: some are not implemented and fail with a clear error. The docs list examples.

License

NCCL is BSD-3-Clause (the copyright file of the libnccl2 package). Binary redistribution must keep the notice, so check-nccl-bundle.sh copies it to licenses/NCCL-LICENSE.txt in the backend package. The notice also points to the NVIDIA NVTX project for files NCCL includes from it.

Tested

  • make test-ci-scripts: pass, with a new test for the path-filter rule.
  • New scripts/build/install-nccl_test.sh (which images get NCCL, version choice per CUDA release) and scripts/build/check-nccl-bundle_test.sh (stub libraries: bundled, missing, not linked, no NCCL). Both pass under make test-build-scripts.
  • In the CI builder base base-grpc-cuda-12-amd64, I ran install-nccl.sh, configured the pinned llama.cpp with this backend's flags (BUILD_SHARED_LIBS=ON GGML_BACKEND_DL=ON GGML_CPU=OFF GGML_CUDA=ON, one CUDA arch to save time) and built ggml-cuda. Then I ran package-gpu-libs.sh and check-nccl-bundle.sh on the result. The installer picked 2.26.2-1+cuda12.8 and CMake printed Found NCCL: /usr/lib/x86_64-linux-gnu/libnccl.so with GGML_CUDA_NCCL=ON. The ggml-cuda link line has libnccl.so, and the objects reference the NCCL symbols. I ran the packaging sweep against the real libnccl.so.2.26.2: it bundles the library, and the check passes and writes the license. Details are in a comment. I did not finish the full ggml-cuda compile locally; the CUDA jobs of this PR do it.

Not tested

  • No GPU run. I did not load a model, single- or multi-GPU.
  • No full image build of the three images. CI on this PR builds them.

Request: 2-GPU test

Can someone with two or more NVIDIA GPUs run a dense model (for example a Llama or Qwen3 dense GGUF) with the preset above and compare the following?

  1. split_mode:layer
  2. split_mode:tensor with GGML_CUDA_ALLREDUCE=internal (no NCCL, same image)
  3. split_mode:tensor (NCCL)
  4. split_mode:tensor with GGML_CUDA_P2P=1

Please report generation tok/s for a single request and for 4 parallel requests. Please also check that the log has no NCCL init failed line, and that a single-GPU load behaves as before.

🤖 Generated with Claude Code

llama.cpp's tensor split (split_mode: tensor) adds up partial results
across the GPUs after every layer. ggml uses NCCL for this when it is
linked, and its own slower code otherwise. The CUDA builders do not
install NCCL, so CMake never found it and every CUDA image ran the
fallback.

.docker/install-nccl.sh installs libnccl2 and libnccl-dev in both
builder stages of Dockerfile.llama-cpp, for cublas on amd64 (CUDA 12
and 13) and on arm64 with CUDA 13. The Jetson image gets none, because
NVIDIA ships no NCCL for Jetson. The script picks the newest NCCL built
for the exact CUDA major.minor of the image, so the driver requirement
stays the same. It stays out of install-base-deps.sh, so the forks that
share that script do not link NCCL and grow too.

The existing transitive sweep bundles libnccl.so.2. A new check in
package.sh fails the build when a packaged ELF needs NCCL that lib/
lacks, or when the builder has NCCL but libggml-cuda.so does not link
it. A missing library would make ggml skip the CUDA backend and run on
the CPU. The check also ships the BSD-3-Clause notice of NCCL.

The compressed images grow by about 287 MB (CUDA 12) and 188 MB
(CUDA 13). NCCL is called only by the tensor-split communicator, so
single-GPU and layer-split loads do not use it.

The docs add a tensor-split preset (flash attention on, f16 KV cache,
fit_params:false, explicit context size) and its limits. A path-filter
rule rebuilds only the llama-cpp images when the NCCL scripts change.

Assisted-by: Claude:claude-sonnet-5-5
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
@mudler-agent

Copy link
Copy Markdown
Collaborator Author

Local verification, in the CI builder base base-grpc-cuda-12-amd64:

  • install-nccl.sh picked and installed libnccl2 / libnccl-dev 2.26.2-1+cuda12.8.
  • CMake on the pinned llama.cpp, with this backend's flags (BUILD_SHARED_LIBS=ON GGML_BACKEND_DL=ON GGML_CPU=OFF GGML_CUDA=ON): -- Found NCCL: /usr/lib/x86_64-linux-gnu/libnccl.so, GGML_CUDA_NCCL:BOOL=ON.
  • The ggml-cuda link line has /usr/lib/x86_64-linux-gnu/libnccl.so, GGML_USE_NCCL is defined, and ggml-cuda.cu.o has ncclCommInitAll, ncclAllReduce, ncclCommInitRankConfig and ncclGetErrorString as undefined symbols.
  • I stopped the full ggml-cuda compile partway through. It is memory-bound on the test machine, and the CUDA image jobs of this PR build it in full. There, package.sh runs check-nccl-bundle.sh against the real binaries.
  • package-gpu-libs.sh ran on a library with NEEDED libnccl.so.2, against the real libnccl.so.2.26.2. The transitive sweep copied libnccl.so.2.26.2 plus the libnccl.so.2 symlink into lib/. check-nccl-bundle.sh then passed and wrote licenses/NCCL-LICENSE.txt.

@mudler
mudler merged commit 93f0059 into master Nov 26, 2026
24 of 55 checks passed
@mudler
mudler deleted the feat/cuda-nccl branch November 26, 2026 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants