Repository navigation
fix: support flux-2-klein-4b Q4_0 GGUF checkpoint #2122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| #include "model_builders.h" | ||
| #include "model_loader.h" | ||
| #include "model_manager.h" | ||
| #include "model_io/gguf_io.h" | ||
| #include "stable-diffusion.h" | ||
|
|
||
| #include "conditioning/conditioner.hpp" | ||
|
|
@@ -716,13 +717,41 @@ void StableDiffusionGGML::load_alphas_cumprod() { | |
| LOG_VERBOSE("loaded alphas_cumprod from model file"); | ||
| } | ||
|
|
||
| // Some Flux/Flux2 GGUFs store their tensor names without the leading | ||
| // "model.diffusion_model." prefix that the rest of sd-cli (version detection and | ||
| // the model builders) expects. Detect that case and load the model with the | ||
| // prefix so tensor lookups match. A GGUF that already carries the prefix | ||
| // (leejet standard) is left untouched. | ||
| static std::string get_flux_model_prefix(const std::string& file_path) { | ||
| std::vector<TensorStorage> storages; | ||
| std::string error; | ||
| if (!read_gguf_file(file_path, storages, &error)) { | ||
| return ""; // unreadable -> fall back to default (no prefix) | ||
| } | ||
| bool has_flux_marker = false; | ||
| bool has_prefix = false; | ||
| for (const auto& s : storages) { | ||
| if (s.name.find("double_stream_modulation_img") != std::string::npos) { | ||
| has_flux_marker = true; | ||
| } | ||
| if (starts_with(s.name, "model.diffusion_model.")) { | ||
| has_prefix = true; | ||
| } | ||
| } | ||
| if (has_flux_marker && !has_prefix) { | ||
| return "model.diffusion_model."; | ||
| } | ||
| return ""; | ||
| } | ||
|
Comment on lines
+720
to
+745
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: #!/bin/bash
rg -n 'double_stream_modulation_img|double_blocks\.|single_transformer_blocks\.' srcRepository: leejet/stable-diffusion.cpp Length of output: 4788 Narrow the Flux marker and reuse the prefix constant. The marker 🤖 Prompt for AI Agents |
||
|
|
||
| bool StableDiffusionGGML::init_model_loader(ModelLoader& model_loader, ModelConfig& configuration) { | ||
| const auto* sd_ctx_params = &configuration.params; | ||
| auto& use_tae = configuration.use_tae; | ||
| auto& use_audio_vae = configuration.use_audio_vae; | ||
| if (strlen(SAFE_STR(sd_ctx_params->model_path)) > 0) { | ||
| const std::string model_prefix = get_flux_model_prefix(sd_ctx_params->model_path); | ||
| LOG_INFO("loading model from '%s'", sd_ctx_params->model_path); | ||
| if (!model_loader.init_from_file(sd_ctx_params->model_path)) { | ||
| if (!model_loader.init_from_file(sd_ctx_params->model_path, model_prefix)) { | ||
| LOG_ERROR("init model loader from file failed: '%s'", sd_ctx_params->model_path); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: leejet/stable-diffusion.cpp
Length of output: 3459
The bare substring match can classify tensor names from other model families as Flux.
find("double_blocks.")also matches prefixed names such ashunyuan_double_blocks.or other names that are not rooted atdouble_blocks.. Restrict the unprefixed checks to tensor names that start withdouble_blocks.orsingle_transformer_blocks..🤖 Prompt for AI Agents