Repository navigation
[Feature] Enable safe per-request LoRA loading (implementation included) #1143
Copy link
Copy link
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Feature Summary
Support for safe, per-request LoRA loading when running in HTTP Server mode
Detailed Description
I would like to propose a patch to enable per-request LoRA loading when running in HTTP Server. Currently, the server ignores LoRA tags (e.g., lora:name:0.5) in prompts because the generation parameters are initialized with an empty string for the LoRA directory.
I am unsure of the original motivation for explicitly disabling this path, but simply enabling it causes "weight stacking" (state pollution) on the persistent server process.
The attached diff implements the following:
- Enables Parsing: Updates gen_params.process_and_check to use ctx_params.lora_model_dir, allowing the backend to find and load the LoRA files.
- Enables Safety: Forces ctx_params.lora_apply_mode = LORA_APPLY_AT_RUNTIME during server initialization. This ensures LoRA calculations are applied dynamically during graph execution without permanently altering the base model weights.
Proposed Change
Diff
diff --git a/examples/server/main.cpp b/examples/server/main.cpp
index 5c951c0..4fb57b0 100644
--- a/examples/server/main.cpp
+++ b/examples/server/main.cpp
@@ -282,6 +282,7 @@ int main(int argc, const char** argv) {
LOG_DEBUG("%s", default_gen_params.to_string().c_str());
sd_ctx_params_t sd_ctx_params = ctx_params.to_sd_ctx_params_t(false, false, false);
+ ctx_params.lora_apply_mode = LORA_APPLY_AT_RUNTIME;
sd_ctx_t* sd_ctx = new_sd_ctx(&sd_ctx_params);
if (sd_ctx == nullptr) {
@@ -392,7 +393,7 @@ int main(int argc, const char** argv) {
return;
}
- if (!gen_params.process_and_check(IMG_GEN, "")) {
+ if (!gen_params.process_and_check(IMG_GEN, ctx_params.lora_model_dir)) {
res.status = 400;
res.set_content(R"({"error":"invalid params"})", "application/json");
return;
@@ -570,7 +571,7 @@ int main(int argc, const char** argv) {
return;
}
- if (!gen_params.process_and_check(IMG_GEN, "")) {
+ if (!gen_params.process_and_check(IMG_GEN, ctx_params.lora_model_dir)) {
res.status = 400;
res.set_content(R"({"error":"invalid params"})", "application/json");
return;Alternatives you considered
- Naive Implementation: Just passing the directory without changing the apply mode. This resulted in model corruption after a few requests due to weight accumulation.
- External Management: Running separate server instances for different LoRA configurations, which is resource-heavy and lacks flexibility.
Additional context
- File: examples/server/main.cpp
- Logic: common.hpp (Lines ~440) describes at_runtime as the method that avoids precision issues and permanent weight modification, making it suitable for a long-running server process.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request
Activity
stduhpf commentedon Dec 24, 2025
I think setting the lora model dir in http server is disabled for security reasons. Letting remote users attempt to load data from arbitrary paths on your machine is no good.
MateusGPe commentedon Dec 24, 2025
@stduhpf That is a valid concern regarding Directory Traversal. However, given that ctx_params.lora_model_dir is already strictly defined by the server admin at startup, I believe the correct approach is to sanitize the input rather than disable the feature entirely.
If we ensure that the parsed LoRA name cannot contain directory separators (/, \) or relative path components (..), the server is safe because it can only load files physically present in the admin-defined lora_model_dir.
I propose we add a simple sanitizer or a check that enforces the resolved path remains within lora_model_dir. Alternatively, we could gate this behavior behind a CLI flag (e.g., --http-enable-lora) so it is an opt-in feature for admins who need it.
stduhpf commentedon Dec 24, 2025
Ah I misunderstood what you meant, I thought you were asking for setting the lora model dir via http request.
#830 should handle the sanitization of lora paths.
MateusGPe commentedon Dec 24, 2025
@stduhpf I'm new to this, and I'm not aware of some implications in the context of sd.cpp. I'm only a hobbyist who messes around with C++, but I think that something like this solves the issue:
MateusGPe commentedon Dec 25, 2025
I used this for a while and found an issue. It's possible to access the folder itself. This fixes the previous fix:
MrDrMcCoy commentedon Dec 29, 2025
@MateusGPe I am using your patches and they seem to be working well for me. Why not submit a PR?
MateusGPe commentedon Dec 31, 2025
@MrDrMcCoy i tried, see: #1156
Danmoreng commentedon Jan 1, 2026
Also stumbled accross the problem, that Loras are not reset if I use the same context for another image generation after using one with lora. Would be great to have that fixed. Gemini suggested resetting the adapter when no Lora is present: