Skip to content

Commit 0f9f23c

Browse files
committed
Detect and use z-image
1 parent 326df7b commit 0f9f23c

4 files changed

Lines changed: 25 additions & 6 deletions

File tree

include/image_generator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class ImageGenerator {
101101
std::string current_clip_l_path_;
102102
std::string current_clip_g_path_;
103103
std::string current_t5xxl_path_;
104+
std::string current_llm_path_;
104105
std::string current_taesd_path_;
105106
std::string current_lora_model_dir_;
106107
std::string current_embeddings_dir_;

include/model_detection.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ std::string detectModelFormat(const std::string& model_path);
1313
std::vector<std::string> extractTensorKeys(const std::string& model_path, const std::string& format);
1414

1515
// Determine model type from tensor keys
16-
// Returns: "clip_l", "clip_g", "t5xxl", or "vae" (default)
16+
// Returns: "clip_l", "clip_g", "t5xxl", "llm", or "vae" (default)
1717
std::string inferModelTypeFromTensorKeys(const std::vector<std::string>& tensor_keys);
1818

1919
// Inspect a model file and determine its type
2020
// Supports both GGUF and safetensors formats
21-
// Returns: "vae", "clip_l", "clip_g", "t5xxl", or "unknown"
21+
// Returns: "vae", "clip_l", "clip_g", "t5xxl", "llm", or "unknown"
2222
std::string inspectModelType(const std::string& model_path);
2323

2424
#endif // MODEL_DETECTION_H

src/image_generator.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ bool ImageGenerator::ensureModelLoaded(const std::string& controlnet_model) {
400400
std::string clip_l_path_str;
401401
std::string clip_g_path_str;
402402
std::string t5xxl_path_str;
403+
std::string llm_path_str;
403404

404405
if (options.has("forge_additional_modules")) {
405406
auto modules = options["forge_additional_modules"];
@@ -423,6 +424,8 @@ bool ImageGenerator::ensureModelLoaded(const std::string& controlnet_model) {
423424
clip_g_path_str = module_full_path;
424425
} else if (model_type == "t5xxl") {
425426
t5xxl_path_str = module_full_path;
427+
} else if (model_type == "llm") {
428+
llm_path_str = module_full_path;
426429
} else {
427430
LOG_WARNING("Unknown model type for: %s (detected as: %s)", module_full_path.c_str(),
428431
model_type.c_str());
@@ -437,7 +440,7 @@ bool ImageGenerator::ensureModelLoaded(const std::string& controlnet_model) {
437440
bool needs_reload = !initialized_ || !sd_ctx_ || model_path != current_model_path_ ||
438441
vae_path_str != current_vae_path_ || clip_l_path_str != current_clip_l_path_ ||
439442
clip_g_path_str != current_clip_g_path_ || t5xxl_path_str != current_t5xxl_path_ ||
440-
controlnet_path_str != current_controlnet_path_;
443+
llm_path_str != current_llm_path_ || controlnet_path_str != current_controlnet_path_;
441444

442445
if (!needs_reload) {
443446
LOG_DEBUG("Model already loaded: %s", model_path.c_str());
@@ -471,7 +474,8 @@ bool ImageGenerator::ensureModelLoaded(const std::string& controlnet_model) {
471474
params.free_params_immediately = false;
472475

473476
// Check if we have additional modules (VAE, CLIP, etc.)
474-
bool has_additional_modules = !clip_l_path_str.empty() || !clip_g_path_str.empty() || !t5xxl_path_str.empty();
477+
bool has_additional_modules =
478+
!clip_l_path_str.empty() || !clip_g_path_str.empty() || !t5xxl_path_str.empty() || !llm_path_str.empty();
475479

476480
if (has_additional_modules) {
477481
LOG_INFO("Using additional modules - loading diffusion model separately");
@@ -488,6 +492,7 @@ bool ImageGenerator::ensureModelLoaded(const std::string& controlnet_model) {
488492
params.clip_l_path = clip_l_path_str.empty() ? nullptr : clip_l_path_str.c_str();
489493
params.clip_g_path = clip_g_path_str.empty() ? nullptr : clip_g_path_str.c_str();
490494
params.t5xxl_path = t5xxl_path_str.empty() ? nullptr : t5xxl_path_str.c_str();
495+
params.llm_path = llm_path_str.empty() ? nullptr : llm_path_str.c_str();
491496
params.taesd_path = nullptr;
492497
params.control_net_path = controlnet_path_str.empty() ? nullptr : controlnet_path_str.c_str();
493498
params.lora_model_dir = lora_dir_str.empty() ? nullptr : lora_dir_str.c_str();
@@ -507,6 +512,9 @@ bool ImageGenerator::ensureModelLoaded(const std::string& controlnet_model) {
507512
if (!t5xxl_path_str.empty()) {
508513
LOG_INFO("Loading T5XXL model: %s", t5xxl_path_str.c_str());
509514
}
515+
if (!llm_path_str.empty()) {
516+
LOG_INFO("Loading LLM model: %s", llm_path_str.c_str());
517+
}
510518
if (!controlnet_path_str.empty()) {
511519
LOG_INFO("Loading ControlNet model: %s", controlnet_path_str.c_str());
512520
}
@@ -530,6 +538,7 @@ bool ImageGenerator::ensureModelLoaded(const std::string& controlnet_model) {
530538
current_clip_l_path_ = clip_l_path_str;
531539
current_clip_g_path_ = clip_g_path_str;
532540
current_t5xxl_path_ = t5xxl_path_str;
541+
current_llm_path_ = llm_path_str;
533542
current_taesd_path_ = "";
534543
current_lora_model_dir_ = lora_dir_str;
535544
current_embeddings_dir_ = "";

src/model_detection.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,21 @@ static std::vector<std::string> getGGUFTensorKeys(const std::string& model_path)
7575
}
7676

7777
// Helper function to determine model type from tensor keys
78-
// Returns: "clip_l", "clip_g", "t5xxl", or "vae" (default)
78+
// Returns: "clip_l", "clip_g", "t5xxl", "llm", or "vae" (default)
7979
std::string inferModelTypeFromTensorKeys(const std::vector<std::string>& tensor_keys) {
8080
if (tensor_keys.empty()) {
8181
return "vae"; // Default to VAE if we can't determine
8282
}
8383

84+
// Check for LLM model indicators
85+
for (const std::string& name : tensor_keys) {
86+
if (name.find("blk.35.attn_k.weight") != std::string::npos ||
87+
name.find("model.layers.35.post_attention_layernorm.weight") != std::string::npos) {
88+
LOG_DEBUG("Detected LLM model");
89+
return "llm";
90+
}
91+
}
92+
8493
bool has_text_model = false;
8594
bool has_text_projection = false;
8695
bool has_position_ids = false;
@@ -209,7 +218,7 @@ std::vector<std::string> extractTensorKeys(const std::string& model_path, const
209218

210219
// Helper function to inspect a model file and determine its type
211220
// Supports both GGUF and safetensors formats
212-
// Returns: "vae", "clip_l", "clip_g", "t5xxl", or "unknown"
221+
// Returns: "vae", "clip_l", "clip_g", "t5xxl", "llm", or "unknown"
213222
std::string inspectModelType(const std::string& model_path) {
214223
// First, detect the file type
215224
std::string format = detectModelFormat(model_path);

0 commit comments

Comments
 (0)