Skip to content

Add the opt-in llama.cpp CUDA backend to the 2.9 GGUF samples - #40

Merged
Nick Eubanks (nieubank) merged 3 commits into
nieubank/llama-upstream-apifrom
nieubank/2.9-cuda
Nov 17, 2026
Merged

Nick Eubanks (nieubank) merged 3 commits into
nieubank/llama-upstream-apifrom
nieubank/2.9-cuda

Conversation

@nieubank

@nieubank Nick Eubanks (nieubank) commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds the llama.cpp CUDA backend for NVIDIA GPUs to the GGUF samples as an explicit opt-in. The Windows ML 2.9 experimental release includes the backend in the Microsoft.Windows.AI.MachineLearning.LibLlama.Cuda.win-x64 and Microsoft.Windows.AI.MachineLearning.LibLlama.Cuda.win-arm64 packages, which Microsoft.Windows.AI.MachineLearning.LibLlama brings in, and in the windowsml-llama-cuda wheel. The samples keep the CPU backend as the default, so a build on a PC without an NVIDIA GPU doesn't change.

  • build.ps1 -LlamaCuda in Samples\Runtime, Samples\Tasks and Samples\Server sets WinMLSamplesLlamaCuda, and the ten GGUF projects then also reference Microsoft.Windows.AI.MachineLearning.LibLlama. Without the switch they reference only LibLlama.Core, as before. The ten include Add a Runtime sample that calls llama.cpp's API directly #30's llama-upstream-api sample and the Task llama-text-generation sample.
  • The CUDA backend needs NVIDIA cuBLAS, which the packages don't include. The build copies it from WinMLLlamaCudaRedistDirectory, then from the CUDA Toolkit that CUDA_PATH names, then from NVIDIA's download once the developer accepts the NVIDIA CUDA Toolkit EULA by setting WinMLLlamaCudaAcceptNvidiaLicense. No project or script in this repo sets that property. Without cuBLAS the build warns with WINMLLLAMA0001, and the samples still run on the CPU.
  • docs/Runtime/gguf-models.md gets an NVIDIA CUDA section that covers the C++ opt-in, the cuBLAS sources, the license responsibility and the Python wheel. The tutorial, overview, Python guide, sample READMEs and localpackages READMEs point to it.
  • Tools/llama now describes the backend tool for other ggml families, or for a CUDA backend you build yourself.
  • verify_python_environment.py checks that installed windowsml-llama and windowsml-llama-cuda wheels match windowsml. With the CUDA wheel it also requires windowsml-llama-core and an nvidia-cublas release that Windows ML loads, the one the CUDA wheel pins or a later release with the same major version.

This PR is stacked on #30 and merges after it. -LlamaCuda restores these packages from the 2.9.36-experimental release, so it needs them on nuget.org and PyPI.

  • Microsoft.Windows.AI.MachineLearning.LibLlama.Cuda.win-x64, Microsoft.Windows.AI.MachineLearning.LibLlama.Cuda.win-arm64 and Microsoft.Windows.AI.MachineLearning.LibLlama 0.7.0.107-experimental
  • windowsml-llama-cuda and windowsml-llama 2.9.36a0

They depend on the 2.9.36-experimental Runtime and LibLlama.Core 0.7.0.107-experimental packages and the matching wheels that #36 pins. The pins in Samples/Directory.Packages.props already match, so nothing here changes once they're live. The default build and the CPU path need nothing new.

Related issue

None.

Affected samples

  • Samples/Runtime, Samples/Tasks and Samples/Server, including their build.ps1 scripts, the ten GGUF projects, READMEs and localpackages READMEs
  • Samples/Directory.Packages.props
  • Samples/Tasks/scripts/verify_python_environment.py
  • Tools/llama
  • docs/Runtime

Checklist

  • I built Samples/WindowsML-Samples.sln locally with Visual Studio 2022 or Visual Studio 2026 (the samples build per catalog, so I built the Runtime, Tasks and Server catalogs with build.ps1 and Visual Studio 2026, Release, for ARM64 and x64, with and without -LlamaCuda)
  • I ran the affected sample(s) end-to-end on Windows 11 (x64 and/or ARM64) (ARM64 on an NVIDIA RTX Spark device and x64 on a GeForce RTX 4090 PC)
  • C++ changes follow the repo .clang-format (ran Samples/format-code.ps1 if available) (no C++ source changes)
  • I updated the relevant per-sample README.md
  • I did not check in large model artifacts, build outputs, or secrets

Testing notes

Tested with the 2.9.36-experimental release packages, the unpublished ones staged in localpackages, and qwen2.5-0.5b-instruct-q4_k_m.gguf.

  • All twelve catalog builds had 0 warnings and 0 errors. Without -LlamaCuda, no output has a LlamaBackends\cuda folder. With it, all ten GGUF outputs got the package's ggml-cuda.dll and the CUDA Toolkit 13.4 cuBLAS for their platform.
  • On the ARM64 RTX Spark device, the Runtime hello-language-model, llm-chat, speech-to-language-model and llama-upstream-api samples, the Task llama-text-generation sample with and without a draft model, the in-process Server host with the C++ and C# clients, the Server executable with the Python client, and the Python hello-language-model, llm-chat and llama-text-generation samples all initialized CUDA and gave the expected answers. The CPU runs of llm-chat, Task text-generation and llama-text-generation still ran on the CPU with the CUDA backend deployed.
  • The cuBLAS sources behaved as documented. WinMLLlamaCudaRedistDirectory took precedence over CUDA_PATH, and the NVIDIA download with WinMLLlamaCudaAcceptNvidiaLicense set in the environment for one local build verified and cached the pinned archive, then reused the cache. With no source, the build warned with WINMLLLAMA0001, a GPU request failed with 0x80070032, and the CPU still worked.
  • In Python, windowsml-llama-cuda installed nvidia-cublas 13.7.0.27 on ARM64 and x64, and verify_python_environment.py --require-llama passed. Without nvidia-cublas, import windowsml.runtime warned once with the fix, a GPU request failed with the same reason, the CPU still worked, and the verify script now fails. A later cuBLAS 13 that Windows ML accepts, 13.8.0.4, passes.
  • On the GeForce RTX 4090, the x64 hello-language-model, llama-upstream-api and Task llama-text-generation builds, the in-process Server host with the C++ client, and the Python hello-language-model and llama-text-generation samples ran on the CUDA device. The x64 builds also ran on the RTX Spark device under emulation.

After review, the verifier requires windowsml-llama-core with the CUDA wheel and checks the nvidia-cublas version. In a scratch environment with the 2.9.36a0 wheels and stand-in nvidia-cublas metadata, 13.7.0.27, 13.8.0.4 and 13.10.0.20 pass, 13.5.0 and 14.0.0 fail, and removing windowsml-llama-core fails, where the previous script passed all of those. The real CUDA environment passes with --require-llama.

  • The Debug builds of all three catalogs with -LlamaCuda had 0 warnings and 0 errors on ARM64 and x64, every GGUF output got the CUDA backend and cuBLAS, and the ARM64 Debug hello-language-model initialized CUDA on the RTX Spark device and gave the expected answer.
  • On an x64 PC without an NVIDIA GPU, an Intel Arc 130V, the -LlamaCuda builds of hello-language-model and the Task llama-text-generation sample ran on the CPU and gave the expected answers. llama.cpp printed one ggml_cuda_init: failed to initialize CUDA line, and a GPU request failed with ERROR_NOT_SUPPORTED, where the Task sample suggests --device cpu.
  • After the rebase onto Add ONNX Runtime sample with the Windows ML CG Execution Provider #39, which adds a tenth Runtime sample, the Runtime catalog with -LlamaCuda built for ARM64 and x64 with no warnings. On the RTX Spark device, the CG EP sample compiled and classified the test image, and llama-upstream-api listed the CUDA device and answered the prompt.

Not covered yet is restore from nuget.org and PyPI, which waits on publishing.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The verifier can accept unsupported cuBLAS versions and a missing core wheel, allowing unusable CUDA environments to pass.

2 open issues
What changed in this PR

Adds an opt-in llama.cpp CUDA backend for NVIDIA GPUs to the Windows ML GGUF samples, while keeping CPU-only deployment as the default. It builds on the existing upstream API sample and centrally pinned 2.9 packages.

Changes:

  • Adds -LlamaCuda to three catalog build scripts and conditional package references to ten GGUF projects.
  • Extends Python environment verification to cover optional llama.cpp wheels and cuBLAS presence.
  • Documents CUDA setup, cuBLAS sources, licensing responsibilities, and custom-backend alternatives.
File Description
Tools/​llama/​README.md Distinguishes packaged CUDA from custom backends.
Tools/​llama/​Build-LlamaBackend.ps1 Updates help text for packaged CUDA availability.
Samples/​Tasks/​scripts/​verify_python_environment.py Checks optional wheel versions and cuBLAS presence.
Samples/​Tasks/​README.md Documents CUDA build and Python options.
Samples/​Tasks/​model-backed/​README.md Adds Python CUDA installation guidance.
Samples/​Tasks/​model-backed/​llama-text-generation/​task-llama-text-generation.vcxproj Conditionally references the CUDA package.
Samples/​Tasks/​model-backed/​llama-text-generation/​README.md Explains packaged CUDA GPU support.
Samples/​Tasks/​localpackages/​README.md Lists the CUDA build switch.
Samples/​Tasks/​language/​text-generation/​task-text-generation.vcxproj Conditionally references the CUDA package.
Samples/​Tasks/​language/​chat-completion/​task-chat-completion.vcxproj Conditionally references the CUDA package.
Samples/​Tasks/​composition/​speech-to-text-generation/​task-speech-to-text-generation.vcxproj Conditionally references the CUDA package.
Samples/​Tasks/​build.ps1 Adds CUDA opt-in arguments and status output.
Samples/​Server/​README.md Explains CUDA deployment and GPU execution.
Samples/​Server/​localpackages/​README.md Lists the CUDA build switch.
Samples/​Server/​host/​in-process/​server-in-process.vcxproj Conditionally references the CUDA package.
Samples/​Server/​host/​executable/​server-executable.vcxproj Conditionally references the CUDA package.
Samples/​Server/​host/​executable/​README.md Documents optional CUDA host deployment.
Samples/​Server/​build.ps1 Adds CUDA opt-in arguments and status output.
Samples/​Runtime/​localpackages/​README.md Lists local CUDA packages and wheel setup.
Samples/​Runtime/​language/​speech-to-language-model/​speech-to-language-model.vcxproj Conditionally references the CUDA package.
Samples/​Runtime/​language/​llm-chat/​README.md Points Python users to the CUDA wheel.
Samples/​Runtime/​language/​llm-chat/​llm-chat.vcxproj Conditionally references the CUDA package.
Samples/​Runtime/​language/​hello-language-model/​README.md Points Python users to the CUDA wheel.
Samples/​Runtime/​language/​hello-language-model/​hello-language-model.vcxproj Conditionally references the CUDA package.
Samples/​Runtime/​interop/​llama-upstream-api/​README.md Explains CUDA deployment and package matching.
Samples/​Runtime/​interop/​llama-upstream-api/​llama-upstream-api.vcxproj Conditionally references the CUDA package.
Samples/​Runtime/​build.ps1 Adds the CUDA switch, help, and build arguments.
Samples/​Directory.Packages.props Adds LibLlama using the existing shared version.
docs/​Runtime/​tutorials/​07-gguf-language-models.md Adds CUDA opt-in instructions.
docs/​Runtime/​python-samples.md Links NVIDIA GPU setup guidance.
docs/​Runtime/​overview.md Introduces optional CUDA packages and wheels.
docs/​Runtime/​gguf-models.md Details CUDA setup, cuBLAS sources, licensing, and cleanup.

Review effort: Balanced

@nieubank

Copy link
Copy Markdown
Contributor Author

Copilot Fix the code for all comments in this review thread.

When a review comment includes a suggested change, apply the suggestion exactly.

Do not make changes beyond what is described in the linked review thread.

Copilot AI commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Copilot Fix the code for all comments in this review thread.

When a review comment includes a suggested change, apply t...

Addressed both review comments in a6a655c; the verifier requires the core wheel with CUDA and checks cuBLAS against the CUDA wheel’s declared requirement.

build.ps1 -LlamaCuda in Samples\Runtime, Samples\Tasks and Samples\Server
sets WinMLSamplesLlamaCuda, and the ten GGUF projects then also reference
Microsoft.Windows.AI.MachineLearning.LibLlama, which brings the
LibLlama.Cuda.win-x64 and LibLlama.Cuda.win-arm64 packages of the Windows ML
2.9 experimental release. Without the switch they reference only
LibLlama.Core, as before, so the CPU backend stays the default.

The CUDA backend needs NVIDIA cuBLAS, which the packages don't include. The
docs describe the three sources the build copies it from, and nothing in the
samples sets WinMLLlamaCudaAcceptNvidiaLicense. docs\Runtime\gguf-models.md
gets an NVIDIA CUDA section for C++ and Python, and the other docs and sample
READMEs point to it. Tools\llama now describes the backend tool for other
ggml families or a CUDA backend you build yourself, and
verify_python_environment.py checks the CUDA wheels and that nvidia-cublas is
installed with them.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ca5b72cf-ae75-4384-9fc4-a6081b9fddea
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
a6a655c compared nvidia-cublas with the CUDA wheel's == pin, which rejects
the later cuBLAS 13 releases that Windows ML loads, such as 13.8.0.4. The
verifier now reads that pin and accepts it or a later release with the same
major version, without the packaging module. The core wheel check is
unchanged, and the Tasks README says what the script checks.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ca5b72cf-ae75-4384-9fc4-a6081b9fddea
@nieubank
Nick Eubanks (nieubank) merged commit defe789 into release/2.9-experimental Nov 17, 2026
1 check passed
@nieubank
Nick Eubanks (nieubank) deleted the nieubank/2.9-cuda branch November 17, 2026 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants