Repository navigation
llama : adapter factors update in place, with no GGUF parse (engine #973) - #215
Merged
bong-water-water-bong merged 1 commit intoNov 6, 2026
Merged
Conversation
llama_adapter_lora_set_factors() copies new lora_a/lora_b values into the tensors the adapter already owns in the backend buffer, through ggml_backend_tensor_set(). The adapter file is not read, no tensor is allocated, and the adapter set on the context does not change, so the next decode reuses the graph. The bulk variant checks the whole batch before writing anything, so a bad entry leaves every weight as it was. n_factors() and factor_name_by_index() let a caller enumerate the weights and rewrite all of them. test-lora-inplace builds a tiny llama model in memory and a rank-2 adapter GGUF, then deletes the adapter file. It checks that the in-place update changes the logits, matches a fresh load of the same factors, restores bit-identical logits when the old values are written back, and that a refused update keeps the old values and the old logits. The factor tensors keep their addresses throughout, so nothing was reloaded. Refs engine#973. Co-authored-by: agent <agent@local>
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.
llama : adapter factors update in place, with no GGUF parse (engine ggml-org#973)
Part of engine ggml-org#973. Today the engine changes an adapter's factors by writing a new adapter GGUF and calling
llama_adapter_lora_init()again: that re-parses the file, allocates new a/b tensors, andllama_set_adapters_lora()changes the adapter set, which forces a graph rebuild. Measured cost on the reporting lane (Vulkan0, Qwen3-0.6B
Q4_K_M, rank 2, batch 16): 16-23 ms per write, 45 -> 195 ms per write as a multi-stage bank grows to rank 26.
What this adds
The adapter already holds its a/b tensors in backend buffers, allocated once by
llama_adapter_lora_init_impl()viaggml_backend_alloc_ctx_tensors_from_buft(). This PR writes new values intothose tensors through
ggml_backend_tensor_set()and touches nothing else: no file read, no allocation, no change tothe context's adapter set. Graph construction reads the tensors' pointers and shapes, and llama.cpp keys graph reuse
on the adapter set and shapes, not on tensor contents, so the next decode just reads the new values.
Constraints, documented in the header: the type and shape of a factor cannot change in place. A different rank is a
new adapter (today's path), and alpha is a struct field the graph bakes into its scale node, so a caller that changes
alpha still goes through
llama_set_adapters_lora(). A caller must not run an in-place write while a decode thatreads the same adapter is in flight.
Test
tests/test-lora-inplace.cppruns in ctest (label main) with no model files: it builds a tiny llama model in memorywith the existing
llama_model_init_from_user(), writes two rank-2 adapter GGUFs with the gguf writer, then deletesboth files before any in-place update, so a re-parse would fail. No test adapter fixture existed in the repo, so the
test creates its model and adapter itself.
What it asserts:
nmse = 3.7e-05,dmax = 7.4e-03, logits |max| 1.02).blk.0.attn_q.weighton the same context moves them again (nmse = 5.7e-05), and the resultis bit-identical to a second context that loaded the same factors from a file (
nmse = 0).ggml_tensor *addresses, the files are gone, andthe context never sees a new adapter set (readback of device tensors is also checked at load).
unknown -2) leave the logits bit-identical to before and the tensor pointers unchanged.
n_factors()/factor_name_by_index()), andwriting the original values back restores the logits bit-identically to the first application.
Build and run, CPU only, on this box (gcc 15.2.0, 16 threads):
Also run in a Debug build (assertions on): build green, test passes.
Engine side (not part of this PR)
third_party/llama.cppis at4a375bf2d; this branch's merge commit becomes the new pin(6 commits ahead of
4a375bf2d, all HRX/kv-share changes, plus this one).origin/mainthere is currentlyno
llama_adapter_lora_initcallsite (grep of the whole repo outsidethird_party), so the change is not aone-line swap in the engine: the ZO/finetune driver that writes adapter GGUFs (the lane that filed Android build fails with ld: error: undefined symbol: aligned_alloc ggml-org/llama.cpp#973 measured it
with
~/amd-oss/inferzo) must replace "write GGUF ->llama_adapter_lora_init->llama_set_adapters_lora" with"write the host factors ->
llama_adapter_lora_set_factors_multi". That also removes the intermediate GGUF write(24.5 ms of the 65 ms per ZO step on the issue).
Not verified here
-DGGML_HIP=OFF, no GPU). The write is the sameggml_backend_tensor_set()call the loader already uses to fill these buffers (src/llama-adapter.cpp), so thebackend behaviour is the existing one, but a device-side run is the missing evidence.
applies without a parse.