Repository navigation
Add a Runtime sample that calls llama.cpp's API directly - #30
Merged
Nick Eubanks (nieubank) merged 3 commits intoNov 17, 2026
Merged
Nick Eubanks (nieubank) merged 3 commits into
Nick Eubanks (nieubank) merged 3 commits into
Conversation
There was a problem hiding this comment.
🟡 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
Nick Eubanks (nieubank)
marked this pull request as ready for review
November 12, 2026 13:54
Nick Eubanks (nieubank)
force-pushed
the
nieubank/llama-upstream-api
branch
from
November 13, 2026 00:51
c311e96 to
20b0c8b
Compare
Nick Eubanks (nieubank)
changed the base branch from
nieubank/2.9-updates
to
nieubank/sample-cleanup
November 13, 2026 00:51
Nick Eubanks (nieubank)
force-pushed
the
nieubank/llama-upstream-api
branch
from
November 13, 2026 14:28
20b0c8b to
5c6d71b
Compare
Nick Eubanks (nieubank)
force-pushed
the
nieubank/llama-upstream-api
branch
2 times, most recently
from
November 13, 2026 18:42
ed1e8c1 to
5fa1b89
Compare
Nick Eubanks (nieubank)
force-pushed
the
nieubank/llama-upstream-api
branch
2 times, most recently
from
November 13, 2026 23:00
ff40234 to
6e20fb8
Compare
5 tasks done
Nick Eubanks (nieubank)
force-pushed
the
nieubank/llama-upstream-api
branch
from
November 14, 2026 15:40
6e20fb8 to
809757e
Compare
Nick Eubanks (nieubank)
force-pushed
the
nieubank/llama-upstream-api
branch
from
November 17, 2026 02:50
809757e to
e123019
Compare
5 tasks done
Nick Eubanks (nieubank)
force-pushed
the
nieubank/llama-upstream-api
branch
from
November 17, 2026 13:41
e123019 to
97ced2f
Compare
4 of 5 tasks
Nick Eubanks (nieubank)
force-pushed
the
nieubank/llama-upstream-api
branch
from
November 17, 2026 14:55
97ced2f to
fc90dea
Compare
Nick Eubanks (nieubank)
force-pushed
the
nieubank/llama-upstream-api
branch
from
November 17, 2026 15:53
fc90dea to
5325671
Compare
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
Nick Eubanks (nieubank)
force-pushed
the
nieubank/llama-upstream-api
branch
from
November 17, 2026 17:23
5325671 to
18ce664
Compare
adrastogi
approved these changes
Nov 17, 2026
Nick Eubanks (nieubank)
merged commit Nov 17, 2026
255c1cd
into
release/2.9-experimental
1 check passed
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.

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 inMicrosoft.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
WinMLLinkLlamaUpstreamApitotrue, and the package then adds llama.cpp's headers to the include path and links the import libraries of its llama.cpp core.LlamaBackendsfolder withggml_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.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-cleanupand 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.mdandSamples/README.mddocs/Runtime/gguf-models.mdChecklist
Samples/WindowsML-Samples.slnlocally with Visual Studio 2022 or Visual Studio 2026 (the samples build per catalog now, so I built the sample fromwindows-ml-runtime.slnxwith Visual Studio 2026 for ARM64 and x64).clang-format(ranSamples/format-code.ps1if available) (there's noformat-code.ps1, so I ran clang-format 22.1.3 withSamples/.clang-formatonmain.cpp)README.mdTesting 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.MachineLearning2.9.16-experimental andMicrosoft.Windows.AI.MachineLearning.LibLlama.Core0.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.cpucpucpuandcuda, built withTools/llama/Build-LlamaBackend.ps1for the same llama.cpp releaseggml-cuda.dll, the cuBLAS libraries beside it and the NVIDIA driver, same answer, exit code 0--helpexits with code 0, and a missing model file orLlamaBackendsfolder exits with code 1.WinMLllama.dll,WinMLggml.dllandWinMLggml-base.dllfrom the llama.cpp core, plus system and C runtime DLLs.-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 with0xC0000135. 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.ps1andwindows-ml-runtime.slnxnow list both samples, and-Sample llama-upstream-apiand-Sample onnx-runtime-winmlcgepeach build only their own project.build.ps1 -Sample allbuilt all ten Runtime samples for ARM64 and x64 with no warnings, and the ARM64 sample answered the prompt.