feat: enhance decision in --lora-apply-mode auto for quantized parameters by akleine · Pull Request #2162 · leejet/stable-diffusion.cpp · GitHub
Skip to content

feat: enhance decision in --lora-apply-mode auto for quantized parameters - #2162

Merged
leejet merged 2 commits into
leejet:masterfrom
akleine:master
Oct 20, 2026
Merged

leejet merged 2 commits into
leejet:masterfrom
akleine:master

Conversation

@akleine

@akleine akleine commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

If runnnig LoRAs using --lora-apply-mode auto the automatic decision between modes "immediately" and "at_runtime" works very well, but there is still a minor gap if using command line options like --type q4_0 etc.

This PR should solve the problem by checking the --type option.

Summary

Two examples will show the problem:

This first runs very well in at_runtime mode:

./sd-cli  -m ~/SD_models/sd1/picxReal_10.safetensors_Q4_0.gguf  \
  --lora-model-dir ~/SD_models/loras_sd15 \
  -pL "A cute cat<lora:LCM-lora-sdv1-5:1>" \
  --sampling-method lcm --steps 4 --cfg-scale 

The sceond is similar but will crash because the --lora-apply-mode is is wrong state.
It runs in immediately mode - but should not:

./sd-cli  -m ~/SD_models/sd1/picxReal_10.safetensors \
  --lora-model-dir ~/SD_models/loras_sd15 \
  -p "A cute cat<lora:LCM-lora-sdv1-5:1>" \
  --sampling-method lcm --steps 4 --cfg-scale 1 \
  --type q4_0

At all: using --type q4_0 is the difference.

Related Issue / Discussion

Additional Information

#3  0x00005cc43b03dbd7 in ggml_cuda_error(char const*, char const*, char const*, int, char const*) ()
#4  0x00005cc43b051580 in ggml_cuda_mul_mat_cublas(ggml_backend_cuda_context&, ggml_tensor const*, ggml_tensor const*, ggmusing command line options like  --type q4_0 l_tensor*) ()
#5  0x00005cc43b0567f2 in ggml_backend_cuda_graph_compute(ggml_backend*, ggml_cgraph*) ()
#6  0x00005cc43be91ce5 in ggml_backend_graph_compute ()
#7  0x00005cc43aad6bc8 in GGMLRunner::execute_segment(ggml_cgraph*, int) ()
#8  0x00005cc43aada37a in GGMLRunner::execute_graph(ggml_cgraph*, int, bool, std::function<bool ()> const&) ()
#9  0x00005cc43aadbd2f in GGMLRunner::compute(std::function<ggml_cgraph* ()>, int, bool, bool, std::function<bool ()> const&) ()
#10 0x00005cc43aa6fd24 in LoraModel::apply(std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, ggml_tensor*, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, ggml_tensor*> > >, std::set<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, SDVersion, int, bool) ()
#11 0x00005cc43aa6cda8 in ModelManager::apply_loras_to_params(std::vector<ModelManager::TensorState*, std::allocator<ModelManager::TensorState*> > const&) ()

Checklist

Summary by CodeRabbit

  • Bug Fixes
    • Corrected automatic LoRA application selection for quantized weight configurations, so they use runtime application rather than immediate application.

If runnnig LoRAs using default --lora-apply-mode auto
the automatic decision between modes "immediately" and "at_runtime" works very well,
but there is still a minor gap if using command line options like  --type q4_0  etc.
@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

@leejet leejet left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we check each tensor’s effective type (expected_type when set, otherwise type) instead of only the global wtype?

--tensor-type-rules takes precedence over --type, but get_wtype_stat() only counts the original storage types. With a floating-point model:

  • --tensor-type-rules 'model.diffusion_model.*=q4_0' still selects immediately, leaving the same class of issue unresolved.
  • --type q4_0 --tensor-type-rules '.*=f16' now selects at_runtime, even though the resulting weights are not quantized.

Checking the effective types would cover both cases while preserving the existing INT8 tensorwise handling.

@akleine

akleine commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

@leejet Thank you for your review!
Actually, I had never used the --tensor-type-rules option - as you might have guessed.

So here comes an update that addresses the mentioned issues.
Perhaps some parts can be shortened a bit more. (Please feel free to edit some more.)
Once again thank you for your work ❤️

@leejet
leejet merged commit a068716 into leejet:master Oct 20, 2026
11 checks passed
@leejet

leejet commented Oct 20, 2026

Copy link
Copy Markdown
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants