[PR #1523] [CLOSED] [Engine] LoRA adapter support with per-request on/off switch #3507

Closed
opened 2026-11-07 14:41:57 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/1523
Author: @agrogov
Created: 10/8/2026
Status: ❌ Closed

Base: main ← Head: feat/lora-adapter-pr


📝 Commits (10+)

  • 1d4743a kernels: LoRA adapter kernels for the mixer output projections
  • 2a6d701 core: apply the LoRA adapter in decode, verify and the prompt path
  • 80625eb conversation: key cached and saved state by the LoRA switch
  • 1df9ce9 generate: --lora / --lora-scaled and the per-request lora=0|1 key
  • bae032d serve: "lora": true|false in the request body
  • b9099da perf(lora): overlap the up kernel with the down kernel (PDL)
  • 3385e61 Merge branch 'main' into feat/lora-adapter-pr
  • f271f74 Merge branch 'main' into feat/lora-adapter-pr
  • 51d3ca8 Merge tag 'v0.1.45' into feat/lora-adapter-pr
  • 9ebbf70 generate.cpp: close load_lora_adapters (the brace was lost in an earlier merge)

📊 Changes

15 files changed (+989 additions, -30 deletions)

View changed files

📝 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 (+3 -0)
📝 src/program/generate.cpp (+191 -13)

📄 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_out on GDN layers and attn_output on QSA layers. For each adapted layer, y += s·B(A·x) with s = scale·alpha/rank; several files stack their ranks.
  • Per request: the engine key is lora=0|1, and the server takes "lora": true|false in 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.
  • State: the conversation cache, batch slots and session files are keyed by the switch, next to 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.
  • Not adapted: the MTP draft layer (it only drafts; the verified tokens are the adapted model's).

Without --lora nothing changes: the feature is opt-in.

none

Approach

Commits: kernels (+ lora_parity test); core hooks in decode, verify and prompt path; conversation cache/session keying; generate loader, CLI flags and request key; serve request field + unit test; PDL overlap of the decode/verify up kernel (rank > 64 and HIP use the plain launch). Rebased onto main at f57c0adc (0.2.0-beta line); conflicts were in layer.cpp, conversation_state.cpp, conversation_cache.hpp and generate.cpp and were resolved by keeping main's changes and re-adding the LoRA lines (including the LoRA switch in the #879 slot checkpoint move).

Validation

Passed (measured on the earlier revision, NOT on the rebased branch):
Tested on main v0.1.40.4 (6674a00) with this branch applied, and before that on v0.1.40.2 with the same results. One NVIDIA B200, CUDA 12.8, sm_100. Model: Qwen4Exp IQ2_XS. Adapter: rank 50 on all 48 layers, 80 MiB VRAM.

  • lora_parity: all checks pass.
    • Error against double precision ≤ 8e-7 for 1–300 tokens, fp32 and fp16 activations.
    • Off is bitwise unchanged, eager and from a graph captured while on.
    • Switching back on re-applies through the same graph.
  • End to end: one engine answers lora=1, lora=0, lora=1; a second engine without an adapter answers the same prompts. Greedy, with and without MTP:
    • lora=0 gives the same tokens as no adapter.
    • Both lora=1 runs give the same tokens.
    • lora=1 differs from lora=0 where the adapter changes behaviour.
  • Speed (256 generated tokens, prompts of about 2,700 tokens, 3 runs each; no MTP is the mean of 3 such benchmarks):
decode, MTP decode, no MTP prompt read / 1K tokens
no adapter 213.2 tok/s 118.2 tok/s 216 ms
loaded, lora=0 210.4 (−1.3%) 114.9 (−2.8%) 209 ms
loaded, lora=1 206.1 (−3.3%) 111.7 (−5.5%) 227 ms (+5%)

Before commit 6, lora=1 ran at 202.2 tok/s with MTP and 110.1 without. Each setting reads different prompt texts, so prompt read moves by a few percent between settings even where the prompt path is unchanged.

  • Latency (derived from the same runs: decode as ms per generated token, prompt read as the mean wall time for prompts of about 2,700 tokens, normalized to 1K tokens):
per token, MTP per token, no MTP prompt read / 1K tokens, MTP prompt read / 1K tokens, no MTP
no adapter 4.69 ms 8.46 ms 216 ms 216 ms
loaded, lora=0 4.75 ms (+0.06) 8.70 ms (+0.24) 209 ms 209 ms
loaded, lora=1 4.85 ms (+0.16) 8.95 ms (+0.49) 229 ms 227 ms
  • Kernel latency per adapted layer (lora_parity, rank 50, 6144 → 2560; there are 48 adapted layers per forward pass). Decode replays its layers from CUDA graphs, so the in-graph figure is the one decode pays:
tokens per call path one call in a 48-layer graph
1 decode 6.2 µs (was 10.2) 4.74 µs (was 6.06)
4 verify window 10.2 µs (was 12.3)
8 verify window 13.3 µs (was 16.4) 11.48 µs (was 13.05)
300 prompt chunk 47.1 µs
  • serve/test_server.py: the new test_lora_key passes. The 3 errors that remain also occur on the untouched base: a socket timeout and Python 3.9 staticmethod.

Failed:
none observed (on the earlier revision). On the rebased branch the build has not been attempted yet.

Not run (on the rebased branch, 799c0c17, on top of f57c0adc):

  • CUDA build, lora_parity, the end-to-end lora=0|1 checks and the speed table: not re-run after the rebase (the rebase was done on a Mac without CUDA). The numbers above are from the earlier revision.
  • HIP and SYCL builds: not run; the feature is CUDA only, but layer.cpp, verify.cpp, prefill.cpp and generate.cpp are shared files.
  • serve/test_server.py test_lora_key: not re-run after the rebase.

Limits

No SYCL/HIP port, no web UI toggle, no VRAM accounting in the layer-split auto planner (80 MiB at rank 50). Docs (docs/DETAILS.md) and benchmark scripts/results are kept out of this PR; they live on the fork's feat/lora-adapter branch.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/1523 **Author:** [@agrogov](https://github.com/agrogov) **Created:** 10/8/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/lora-adapter-pr` --- ### 📝 Commits (10+) - [`1d4743a`](https://github.com/Niko1221/Strata/commit/1d4743a3b582b211856acdd8779006067a221081) kernels: LoRA adapter kernels for the mixer output projections - [`2a6d701`](https://github.com/Niko1221/Strata/commit/2a6d7019df09d35a7eaf2e4ad9f6f5965b05bc6b) core: apply the LoRA adapter in decode, verify and the prompt path - [`80625eb`](https://github.com/Niko1221/Strata/commit/80625eb79940399ed735d7769a292dea160972bb) conversation: key cached and saved state by the LoRA switch - [`1df9ce9`](https://github.com/Niko1221/Strata/commit/1df9ce97acca809f161e755aaa9e6a6e6b691d26) generate: --lora / --lora-scaled and the per-request lora=0|1 key - [`bae032d`](https://github.com/Niko1221/Strata/commit/bae032df2f79715cc08632c5cd233245a0529a1d) serve: "lora": true|false in the request body - [`b9099da`](https://github.com/Niko1221/Strata/commit/b9099daf6959b773f72e591e4e0e55aea22fd544) perf(lora): overlap the up kernel with the down kernel (PDL) - [`3385e61`](https://github.com/Niko1221/Strata/commit/3385e61adb7743ea596669f29036e8d3d3ed2812) Merge branch 'main' into feat/lora-adapter-pr - [`f271f74`](https://github.com/Niko1221/Strata/commit/f271f74b5f2d93d467bc4f1c3b0fb44427cf3277) Merge branch 'main' into feat/lora-adapter-pr - [`51d3ca8`](https://github.com/Niko1221/Strata/commit/51d3ca8cc5a93c70cde79360443bd4c1cd745d23) Merge tag 'v0.1.45' into feat/lora-adapter-pr - [`9ebbf70`](https://github.com/Niko1221/Strata/commit/9ebbf7097bd5872ced11c6e2098e51f096fb161b) generate.cpp: close load_lora_adapters (the brace was lost in an earlier merge) ### 📊 Changes **15 files changed** (+989 additions, -30 deletions) <details> <summary>View changed files</summary> 📝 `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` (+3 -0) 📝 `src/program/generate.cpp` (+191 -13) </details> ### 📄 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_out` on GDN layers and `attn_output` on QSA layers. For each adapted layer, `y += s·B(A·x)` with `s = scale·alpha/rank`; several files stack their ranks. - **Per request:** the engine key is `lora=0|1`, and the server takes `"lora": true|false` in 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. - **State:** the conversation cache, batch slots and session files are keyed by the switch, next to `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. - **Not adapted:** the MTP draft layer (it only drafts; the verified tokens are the adapted model's). Without `--lora` nothing changes: the feature is opt-in. ## Related discussion none ## Approach Commits: kernels (+ `lora_parity` test); core hooks in decode, verify and prompt path; conversation cache/session keying; `generate` loader, CLI flags and request key; `serve` request field + unit test; PDL overlap of the decode/verify up kernel (rank > 64 and HIP use the plain launch). Rebased onto `main` at `f57c0adc` (0.2.0-beta line); conflicts were in `layer.cpp`, `conversation_state.cpp`, `conversation_cache.hpp` and `generate.cpp` and were resolved by keeping `main`'s changes and re-adding the LoRA lines (including the LoRA switch in the #879 slot checkpoint move). ## Validation **Passed** (measured on the earlier revision, NOT on the rebased branch): Tested on `main` v0.1.40.4 (`6674a00`) with this branch applied, and before that on v0.1.40.2 with the same results. One NVIDIA B200, CUDA 12.8, sm_100. Model: Qwen4Exp IQ2_XS. Adapter: rank 50 on all 48 layers, 80 MiB VRAM. - **`lora_parity`:** all checks pass. - Error against double precision ≤ 8e-7 for 1–300 tokens, fp32 and fp16 activations. - Off is bitwise unchanged, eager and from a graph captured while on. - Switching back on re-applies through the same graph. - **End to end:** one engine answers `lora=1`, `lora=0`, `lora=1`; a second engine without an adapter answers the same prompts. Greedy, with and without MTP: - `lora=0` gives the same tokens as no adapter. - Both `lora=1` runs give the same tokens. - `lora=1` differs from `lora=0` where the adapter changes behaviour. - **Speed** (256 generated tokens, prompts of about 2,700 tokens, 3 runs each; no MTP is the mean of 3 such benchmarks): | | decode, MTP | decode, no MTP | prompt read / 1K tokens | |---|---|---|---| | no adapter | 213.2 tok/s | 118.2 tok/s | 216 ms | | loaded, `lora=0` | 210.4 (−1.3%) | 114.9 (−2.8%) | 209 ms | | loaded, `lora=1` | 206.1 (−3.3%) | 111.7 (−5.5%) | 227 ms (+5%) | Before commit 6, `lora=1` ran at 202.2 tok/s with MTP and 110.1 without. Each setting reads different prompt texts, so prompt read moves by a few percent between settings even where the prompt path is unchanged. - **Latency** (derived from the same runs: decode as ms per generated token, prompt read as the mean wall time for prompts of about 2,700 tokens, normalized to 1K tokens): | | per token, MTP | per token, no MTP | prompt read / 1K tokens, MTP | prompt read / 1K tokens, no MTP | |---|---|---|---|---| | no adapter | 4.69 ms | 8.46 ms | 216 ms | 216 ms | | loaded, `lora=0` | 4.75 ms (+0.06) | 8.70 ms (+0.24) | 209 ms | 209 ms | | loaded, `lora=1` | 4.85 ms (+0.16) | 8.95 ms (+0.49) | 229 ms | 227 ms | - **Kernel latency per adapted layer** (`lora_parity`, rank 50, 6144 → 2560; there are 48 adapted layers per forward pass). Decode replays its layers from CUDA graphs, so the in-graph figure is the one decode pays: | tokens per call | path | one call | in a 48-layer graph | |---|---|---|---| | 1 | decode | 6.2 µs (was 10.2) | 4.74 µs (was 6.06) | | 4 | verify window | 10.2 µs (was 12.3) | | | 8 | verify window | 13.3 µs (was 16.4) | 11.48 µs (was 13.05) | | 300 | prompt chunk | 47.1 µs | | - **`serve/test_server.py`:** the new `test_lora_key` passes. The 3 errors that remain also occur on the untouched base: a socket timeout and Python 3.9 `staticmethod`. **Failed:** none observed (on the earlier revision). On the rebased branch the build has not been attempted yet. **Not run** (on the rebased branch, `799c0c17`, on top of `f57c0adc`): - CUDA build, `lora_parity`, the end-to-end `lora=0|1` checks and the speed table: not re-run after the rebase (the rebase was done on a Mac without CUDA). The numbers above are from the earlier revision. - HIP and SYCL builds: not run; the feature is CUDA only, but `layer.cpp`, `verify.cpp`, `prefill.cpp` and `generate.cpp` are shared files. - `serve/test_server.py` `test_lora_key`: not re-run after the rebase. ## Limits No SYCL/HIP port, no web UI toggle, no VRAM accounting in the layer-split auto planner (80 MiB at rank 50). Docs (`docs/DETAILS.md`) and benchmark scripts/results are kept out of this PR; they live on the fork's `feat/lora-adapter` branch. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 14:41:57 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#3507
No description provided.