Repository navigation
feat(llama-cpp): ship NCCL in the CUDA images for tensor split - #13523
Merged
Merged
Conversation
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>
Collaborator
Author
|
Local verification, in the CI builder base
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 CUDAllama-cppimage runs the fallback.This PR:
.docker/install-nccl.sh. Both builder stages ofbackend/Dockerfile.llama-cpprun it before the source copy, so the layer is cached. It installslibnccl2andlibnccl-devforcublason 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 CUDAmajor.minorof 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_VERSIONoverrides the choice.install-base-deps.shon 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 prebuiltbase-grpc-*images would need a rebuild.GGML_CUDA_NCCLis already on by default at the pinned llama.cpp, and itsFindNCCL.cmakefinds the apt install.backend/cpp/llama-cpp/package.shneeds no new copy step. The existing transitive sweep inpackage-gpu-libs.shcopieslibnccl.so.2becauselibggml-cuda.sonow has it asNEEDED(and so dollama-cpp-grpcandllama-cpp-rpc-server, which link the sharedlibnccl.sothat CMake finds). A newscripts/build/check-nccl-bundle.shruns at the end ofpackage.shand fails the build when:libnccl.so.2andlib/does not have it, orlibggml-cuda.sodoes not link it (CMake missed NCCL).It also copies the NCCL license notice to
licenses/NCCL-LICENSE.txt.Dockerfile.llama-cppimages, not the full matrix through thescripts/build/catch-all.split_mode/tensor_splitrows in the model options pages.Image size
Compressed (gzip) size of the added
libnccl.so.2, against the currentlatestbackend images:-gpu-nvidia-cuda-12-llama-cpp(amd64)-gpu-nvidia-cuda-13-llama-cpp(amd64)-nvidia-l4t-cuda-13-arm64-llama-cpp(arm64)-nvidia-l4t-arm64-llama-cpp(Jetson)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.awithnvpruneand 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
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.ncclCommInitAllfails): upstream logs a warning and falls back to its internal all-reduce, then to the generic one.GGML_CUDA_ALLREDUCE=internal|noneforces those paths at run time.GGML_BACKEND_DL, ggml would fail todlopenlibggml-cuda.soand run on the CPU. That is why the library is bundled with the backend, the same way the cuBLAS libraries are, and whycheck-nccl-bundle.shfails the image build instead of shipping an image without it. Adlopen-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:Checked against the pinned llama.cpp:
autoturns it on in tensor mode, andofffails the load.common_params_fitthrows for tensor split, and llama.cpp loads unfitted with a warning, so the preset turns it off and setscontext_size.License
NCCL is BSD-3-Clause (the
copyrightfile of thelibnccl2package). Binary redistribution must keep the notice, socheck-nccl-bundle.shcopies it tolicenses/NCCL-LICENSE.txtin 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.scripts/build/install-nccl_test.sh(which images get NCCL, version choice per CUDA release) andscripts/build/check-nccl-bundle_test.sh(stub libraries: bundled, missing, not linked, no NCCL). Both pass undermake test-build-scripts.base-grpc-cuda-12-amd64, I raninstall-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 builtggml-cuda. Then I ranpackage-gpu-libs.shandcheck-nccl-bundle.shon the result. The installer picked2.26.2-1+cuda12.8and CMake printedFound NCCL: /usr/lib/x86_64-linux-gnu/libnccl.sowithGGML_CUDA_NCCL=ON. Theggml-cudalink line haslibnccl.so, and the objects reference the NCCL symbols. I ran the packaging sweep against the reallibnccl.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 fullggml-cudacompile locally; the CUDA jobs of this PR do it.Not tested
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?
split_mode:layersplit_mode:tensorwithGGML_CUDA_ALLREDUCE=internal(no NCCL, same image)split_mode:tensor(NCCL)split_mode:tensorwithGGML_CUDA_P2P=1Please report generation tok/s for a single request and for 4 parallel requests. Please also check that the log has no
NCCL init failedline, and that a single-GPU load behaves as before.🤖 Generated with Claude Code