mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3267] [Engine] LoRA adapter support with per-request on/off switch #4537
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#4537
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/3267
Author: @agrogov
Created: 10/19/2026
Status: 🔄 Open
Base:
main← Head:feat/lora-adapter-pr📝 Commits (8)
5e910f8kernels: LoRA adapter kernels for the mixer output projectionsfb9f4adcore: apply the LoRA adapter in decode, verify and the prompt path12ba3d2conversation: key cached and saved state by the LoRA switch701f29agenerate: --lora / --lora-scaled and the per-request lora=0|1 key92fa41eserve: "lora": true|false in the request body799c0c1perf(lora): overlap the up kernel with the down kernel (PDL)606da42Merge upstream/main into feat/lora-adapter-pr16600d6Update .gitignore📊 Changes
16 files changed (+994 additions, -32 deletions)
View changed files
📝
.gitignore(+1 -0)📝
CMakeLists.txt(+6 -0)📝
include/strata/core/conversation_cache.hpp(+8 -5)📝
include/strata/core/conversation_file.hpp(+2 -0)📝
include/strata/core/conversation_snapshot.hpp(+1 -0)➕
include/strata/kernels/lora.hpp(+77 -0)📝
serve/server.py(+16 -4)📝
serve/test_server.py(+6 -0)📝
src/core/conversation_file.cpp(+5 -3)📝
src/core/conversation_state.cpp(+3 -3)📝
src/core/layer.cpp(+10 -2)📝
src/core/verify.cpp(+3 -0)➕
src/kernels/cuda/lora.cu(+421 -0)➕
src/kernels/lora_parity.cpp(+237 -0)📝
src/prefill/prefill.cpp(+4 -0)📝
src/program/generate.cpp(+194 -15)📄 Description
Problem and result
Strata cannot apply a LoRA adapter, so a fine-tuned variant of the model needs a merged GGUF. This adds optional LoRA support in the CUDA engine (llama.cpp's adapter GGUF format) with a per-request on/off switch.
--lora FILE/--lora-scaled FILE:SCALE[,...]load a llama.cpp LoRA GGUF (general.type = adapter,adapter.type = lora; f32/f16/bf16). It is applied to the mixer output projections:ssm_outon GDN layers andattn_outputon QSA layers. For each adapted layer,y += s·B(A·x)withs = scale·alpha/rank; several files stack their ranks.lora=0|1, and the server takes"lora": true|falsein the request body (OpenAI and Anthropic APIs) or"sampling": {"lora": ...}in the config. The switch is a device flag the kernels read, so captured CUDA graphs serve both states with no re-capture. Turned off, the output is bit-identical to the engine without an adapter (checked, see Validation).cvec. Session files stay backward compatible (a new bit in the existing flag word; the fingerprint changes only when an adapter is loaded). While batch slots are decoding, a request for the other state is refused.Without
--loranothing changes: the feature is opt-in.Related discussion
Continues #1523, closed in the 0.2.0-beta tracker reset.
/reopenpassed its check but GitHub would not reopen it (the branch was rebased and force-pushed after it was closed), so this is the same change, not a competing attempt. No separate scope issue.Approach
Six commits: kernels (+ the
lora_paritytest); core hooks in decode, verify and the prompt path; conversation cache/session keying;generateloader, CLI flags and request key;serverequest field + unit test; the decode/verify up kernel as a programmatic dependent launch (rank > 64 and HIP use the plain launch).Rebased on
mainf57c0adc(0.2.0-beta). Conflicts were inlayer.cpp,conversation_state.cpp,conversation_cache.hppandgenerate.cpp;main's changes were kept and the LoRA lines re-added, including the LoRA switch in the #879 slot checkpoint move and the donor take-over.Validation
Revision tested: this branch's code (
799c0c17) as built in a Docker image from0e320510, which has the same source files (it differs only in docs/bench files kept out of this PR). Engine onmainf57c0adcbase. One NVIDIA RTX 5090, CUDA 13, driver 615.71. Model: Qwen3.8-Flash-Next Coder IQ1_M (--native,--spec 4,--expert-cache auto), adapterara-trial9-lora-F32.gguf: 48 layers adapted, 79.8 MiB.Passed:
lora_parity(synthetic, GPU):lora_parity: all ok. Error against double precision 3.0e-7 to 7.9e-7 for 1, 3, 8 and 300 tokens, fp32 and fp16 activations. Switched off: bitwise unchanged for 1, 8 and 300 tokens and from a graph captured while on. Switching back on re-applies through the same graph. Kernel time, rank 50, 6144 -> 2560: 3.7 µs (1 token), 7.6 µs (4), 10.5 µs (8), 31.7 µs (300); in a captured 48-layer graph 3.71 µs per layer at 1 token and 10.49 µs at 8 tokens.bench/e2e.py-style, greedy, 48 tokens, 2 prompts, with and without MTP):lora=0gives the same tokens as an engine without an adapter;lora=1repeated gives the same tokens. On these two short promptslora=1andlora=0also gave the same tokens, so this run does not show the adapter changing the output; over the API, 120-token answers did differ betweenlora: trueandlora: false./v1/messages,temperature 0): a request without the field behaves aslora: true;lora: falsediffers from it; after alora: truerequest, alora: falserequest reads 0 cached tokens (the cache is keyed by the switch) and a repeat in the same state reads the cached prefix.--layer-split 24 --split-device 1,--spec 4, with and without MTP, same two prompts, greedy, 48 tokens. The log showslayers 0-23 (CUDA0), 24-47 (CUDA1), one hand-off per windowandLoRA: 1 file, 48 layers adapted, 79.8 MiB per device. Results as on one card:lora=1repeated gives the same tokens, andlora=0gives the same tokens as an engine without an adapter. As on one card,lora=1andlora=0gave the same tokens on these short prompts, so this run shows the adapter loads on both cards and the switch is bit-neutral, not that the second card's layers change the output. Speed on the split was not measured.lora=0lora=1The MTP differences are within the run-to-run noise of a single benchmark process per setting. Prompt read is not compared: each setting reads its own texts (2,527 to 2,880 tokens), so its times are not comparable.
mainv0.1.40.4, rank 50) are in the first revision of this PR: decode -3.3% (MTP) and -5.5% (no MTP) withlora=1,lora=0-1.3% and -2.8%; they were taken before the rebase and are not repeated here.Limits
No SYCL/HIP port, no web UI toggle, no VRAM accounting in the layer-split auto planner (80 MiB at rank 50). The MTP draft layer is not adapted. Docs (
docs/DETAILS.md) and benchmark scripts/results are kept out of this PR.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.