Skip to content

Add a Runtime sample that calls llama.cpp's API directly - #30

Merged
Nick Eubanks (nieubank) merged 3 commits into
nieubank/sample-cleanupfrom
nieubank/llama-upstream-api
Nov 17, 2026
Merged

Nick Eubanks (nieubank) merged 3 commits into
nieubank/sample-cleanupfrom
nieubank/llama-upstream-api

Conversation

@nieubank

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

Copy link
Copy Markdown
Contributor

Summary

Adds Samples/Runtime/interop/llama-upstream-api, a C++ sample that calls llama.cpp's own C API (llama.h) against the llama.cpp build that Windows ML ships in Microsoft.Windows.AI.MachineLearning.LibLlama.Core. It uses no Windows ML API.

An application that already uses llama.cpp can keep its code and use the llama.cpp core that Windows ML deploys, instead of building and shipping its own. Linking llama.cpp directly is an explicit opt-in. A project sets WinMLLinkLlamaUpstreamApi to true, and the package then adds llama.cpp's headers to the include path and links the import libraries of its llama.cpp core.

  • The sample loads the ggml backends from each LlamaBackends folder with ggml_backend_load_all_from_path, with the folder on the DLL search path so that libraries placed beside a backend resolve. It then loads a GGUF model, applies the model's chat template and prints a greedy response.
  • The README explains the linking, how CMake projects use the package's config and deploy its DLLs, and how GPU backends load. ggml checks a backend's API version but doesn't read the package's backend manifest, so the README points to the package matching guidance for backends.
  • build.ps1, the Runtime solution, the sample tables and the GGUF models page list the new sample.

This PR is stacked on #33, so it targets nieubank/sample-cleanup and merges after #31 and #33, and #40 is stacked on it. It builds on the 2.9 packages that #36 pinned and doesn't depend on #31 or #33.

Related issue

None.

Affected samples

  • Samples/Runtime/interop/llama-upstream-api (new)
  • Samples/Runtime/build.ps1, Samples/Runtime/windows-ml-runtime.slnx, Samples/Runtime/README.md and Samples/README.md
  • docs/Runtime/gguf-models.md

Checklist

  • I built Samples/WindowsML-Samples.sln locally with Visual Studio 2022 or Visual Studio 2026 (the samples build per catalog now, so I built the sample from windows-ml-runtime.slnx with Visual Studio 2026 for ARM64 and x64)
  • I ran the affected sample(s) end-to-end on Windows 11 (x64 and/or ARM64) (ARM64 on an NVIDIA RTX Spark device, on the CPU and with a CUDA backend, and the x64 build on the same device under emulation)
  • C++ changes follow the repo .clang-format (ran Samples/format-code.ps1 if available) (there's no format-code.ps1, so I ran clang-format 22.1.3 with Samples/.clang-format on main.cpp)
  • I updated the relevant per-sample README.md
  • I did not check in large model artifacts, build outputs, or secrets

Testing notes

Tested with qwen2.5-0.5b-instruct-q4_k_m.gguf, the model that the GGUF samples use, first with the packages #29 pinned before, Microsoft.Windows.AI.MachineLearning 2.9.16-experimental and Microsoft.Windows.AI.MachineLearning.LibLlama.Core 0.7.0.87-dev, then on #29's move to 2.9.31-experimental and 0.7.0.102-experimental, and again on #36's move to 2.9.36-experimental and 0.7.0.107-experimental, which this branch is now rebased on. Each newer Core builds the same llama.cpp release with the same headers, and its MSBuild and CMake files are unchanged. The table, the exit codes, the imports and the two CMake builds held with all three. The CUDA backend was rebuilt for 0.7.0.87 and 0.7.0.102, and the 0.7.0.102 build loads with 0.7.0.107, which keeps the same backend API.

Build Backends loaded Result
ARM64 Release cpu Lists the CPU device, answers the prompt, exit code 0
x64 Release under emulation cpu Same answer, exit code 0
ARM64 Release cpu and cuda, built with Tools/llama/Build-LlamaBackend.ps1 for the same llama.cpp release Lists the CUDA device, loads ggml-cuda.dll, the cuBLAS libraries beside it and the NVIDIA driver, same answer, exit code 0
  • Missing or invalid arguments exit with code 2, --help exits with code 0, and a missing model file or LlamaBackends folder exits with code 1.
  • Both executables import only WinMLllama.dll, WinMLggml.dll and WinMLggml-base.dll from the llama.cpp core, plus system and C runtime DLLs.
  • I built the sample with CMake through the package's config, using the snippet that the README now shows, with Ninja for ARM64 and with the Visual Studio generator and -A x64. Both imported only the three core DLLs and gave the same answer as the MSBuild build. Without the README's copy step, the executable failed to start with 0xC0000135. An x64 build with Ninja on the ARM64 device picked the ARM64 libraries, because the package's config uses the host architecture when the generator has no platform. That's a package issue, tracked separately.

After #39 added the ONNX Runtime CG execution provider sample, this branch was rebased onto it. The Runtime README table, build.ps1 and windows-ml-runtime.slnx now list both samples, and -Sample llama-upstream-api and -Sample onnx-runtime-winmlcgep each build only their own project. build.ps1 -Sample all built all ten Runtime samples for ARM64 and x64 with no warnings, and the ARM64 sample answered the prompt.

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 fixed 256-byte token buffer incorrectly treats insufficient capacity as a conversion failure, preventing generation for valid models with longer token pieces.

1 open issue
What changed in this PR

Adds a Runtime interop sample showing how existing llama.cpp applications can use the core shipped by Windows ML without calling Windows ML APIs.

Changes:

  • Adds a C++ sample that loads ggml backends and a GGUF model, applies a chat template, and generates text with greedy sampling.
  • Integrates the sample into the Runtime build script and solution.
  • Documents opt-in linking, CMake integration, and backend package matching.
File Description
Samples/​Runtime/​windows-ml-runtime.slnx Adds the sample project to the Runtime solution.
Samples/​Runtime/​README.md Lists the new interop sample.
Samples/​Runtime/​interop/​llama-upstream-api/​README.md Documents building, running, linking, and backend loading.
Samples/​Runtime/​interop/​llama-upstream-api/​main.cpp Implements direct llama.cpp model loading and text generation.
Samples/​Runtime/​interop/​llama-upstream-api/​llama-upstream-api.vcxproj Defines the project and enables upstream API linking.
Samples/​Runtime/​build.ps1 Adds sample selection and build integration.
Samples/​README.md Expands the Runtime catalog description to include llama.cpp interop.
docs/​Runtime/​gguf-models.md Explains direct API opt-in and links to the sample.

Review effort: Balanced

@nieubank
Nick Eubanks (nieubank) marked this pull request as ready for review November 12, 2026 13:54
@nieubank
Nick Eubanks (nieubank) changed the base branch from nieubank/2.9-updates to nieubank/sample-cleanup November 13, 2026 00:51
@nieubank
Nick Eubanks (nieubank) requested a balanced review from Copilot November 13, 2026 02:26

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.

🟢 Approval recommended

No active review issues are reported for the sample demonstrating direct llama.cpp use with the Windows ML–packaged core.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@nieubank
Nick Eubanks (nieubank) force-pushed the nieubank/llama-upstream-api branch 2 times, most recently from ed1e8c1 to 5fa1b89 Compare November 13, 2026 18:42
@nieubank
Nick Eubanks (nieubank) force-pushed the nieubank/llama-upstream-api branch 2 times, most recently from ff40234 to 6e20fb8 Compare November 13, 2026 23:00
The llama-upstream-api sample in Samples/Runtime/interop calls llama.h
against the llama.cpp core that the
Microsoft.Windows.AI.MachineLearning.LibLlama.Core package deploys, with
no Windows ML API. Linking llama.cpp directly is an explicit opt-in. The
project sets WinMLLinkLlamaUpstreamApi, so the package adds its
llama.cpp headers to the include path and links the import libraries
of that core. The pinned LibLlama.Core package provides both.

The sample loads the ggml backends from each LlamaBackends folder with
ggml_backend_load_all_from_path, with the folder on the DLL search path
so that libraries placed beside a backend resolve. It then loads a GGUF
model, applies the model's chat template, and prints a greedy response.

Add the sample to build.ps1, the Runtime solution and the sample
tables, and link it from the GGUF models page.

Verified with the pinned packages on an ARM64 PC with an NVIDIA GPU.
- Release builds for ARM64 and x64 complete with no warnings or errors.
  The executable imports only WinMLllama.dll, WinMLggml.dll and
  WinMLggml-base.dll from the llama.cpp core.
- On the CPU, the ARM64 build and the x64 build, under emulation, load
  the CPU backend, answer the prompt and exit with code 0. Missing or
  invalid arguments exit with code 2.
- With a CUDA backend built for the same package in LlamaBackends\cuda,
  the ARM64 build lists the CUDA device, loads ggml-cuda.dll, the
  cuBLAS libraries beside it and the NVIDIA driver, and gives the same
  answer as on the CPU.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ca5b72cf-ae75-4384-9fc4-a6081b9fddea
llama_token_to_piece returns the negated length of a piece that doesn't
fit, so a vocabulary with pieces longer than 256 bytes, such as long
whitespace tokens, stopped generation with a conversion error. The loop
now keeps one buffer, grows it to the returned length and converts the
token again, as ApplyChatTemplate already does.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ca5b72cf-ae75-4384-9fc4-a6081b9fddea
The README told CMake projects to find the package config and link
WinML::llama, but CMake doesn't copy the package's DLLs. A project that
followed it built, then failed to start with 0xC0000135 because
WinMLllama.dll wasn't found. The README now says to add the package's
build\cmake folder to CMAKE_PREFIX_PATH, copy the files that
WINML_LLAMA_RUNTIME_FILES lists beside the executable, and copy the
WINML_LLAMA_BACKEND_DIR folder as LlamaBackends, with a short snippet. The
sample built that way with Ninja for ARM64 and with the Visual Studio
generator for x64. Both imported only WinMLllama.dll, WinMLggml.dll and
WinMLggml-base.dll and gave the same answer as the MSBuild build.

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

Labels

None yet

4 participants