|
@@ -3,8 +3,8 @@
|
|
|
#include <chrono>
|
|
#include <chrono>
|
|
|
#include <cstring>
|
|
#include <cstring>
|
|
|
#include <filesystem>
|
|
#include <filesystem>
|
|
|
-#include <iostream>
|
|
|
|
|
#include <thread>
|
|
#include <thread>
|
|
|
|
|
+#include "logger.h"
|
|
|
#include "model_detector.h"
|
|
#include "model_detector.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
extern "C" {
|
|
@@ -17,6 +17,8 @@ public:
|
|
|
std::string lastError;
|
|
std::string lastError;
|
|
|
std::mutex contextMutex;
|
|
std::mutex contextMutex;
|
|
|
bool verbose = false;
|
|
bool verbose = false;
|
|
|
|
|
+ std::string currentModelPath;
|
|
|
|
|
+ StableDiffusionWrapper::GenerationParams currentModelParams;
|
|
|
|
|
|
|
|
Impl() {
|
|
Impl() {
|
|
|
// Initialize any required resources
|
|
// Initialize any required resources
|
|
@@ -46,7 +48,7 @@ public:
|
|
|
// Get absolute path for logging
|
|
// Get absolute path for logging
|
|
|
std::filesystem::path absModelPath = std::filesystem::absolute(modelPath);
|
|
std::filesystem::path absModelPath = std::filesystem::absolute(modelPath);
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Loading model from absolute path: " << absModelPath << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Loading model from absolute path: " + std::filesystem::absolute(modelPath).string());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Create persistent string copies to fix lifetime issues
|
|
// Create persistent string copies to fix lifetime issues
|
|
@@ -83,7 +85,7 @@ public:
|
|
|
if (modelFileName.find("qwen") != std::string::npos) {
|
|
if (modelFileName.find("qwen") != std::string::npos) {
|
|
|
isQwenModel = true;
|
|
isQwenModel = true;
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Detected Qwen model from filename: " << modelFileName << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Detected Qwen model from filename: " + modelFileName);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -91,12 +93,12 @@ public:
|
|
|
if (parentDirName == "diffusion_models" || parentDirName == "diffusion") {
|
|
if (parentDirName == "diffusion_models" || parentDirName == "diffusion") {
|
|
|
useDiffusionModelPath = true;
|
|
useDiffusionModelPath = true;
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Model is in " << parentDirName << " directory, using diffusion_model_path" << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Model is in " + parentDirName + " directory, using diffusion_model_path");
|
|
|
}
|
|
}
|
|
|
} else if (parentDirName == "checkpoints" || parentDirName == "stable-diffusion") {
|
|
} else if (parentDirName == "checkpoints" || parentDirName == "stable-diffusion") {
|
|
|
useDiffusionModelPath = false;
|
|
useDiffusionModelPath = false;
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Model is in " << parentDirName << " directory, using model_path" << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Model is in " + parentDirName + " directory, using model_path");
|
|
|
}
|
|
}
|
|
|
} else if (parentDirName == "sd_models" || parentDirName.empty()) {
|
|
} else if (parentDirName == "sd_models" || parentDirName.empty()) {
|
|
|
// Handle models in root /data/SD_MODELS/ directory
|
|
// Handle models in root /data/SD_MODELS/ directory
|
|
@@ -105,12 +107,12 @@ public:
|
|
|
useDiffusionModelPath = true;
|
|
useDiffusionModelPath = true;
|
|
|
detectionSource = "qwen_root_detection";
|
|
detectionSource = "qwen_root_detection";
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Qwen model in root directory, preferring diffusion_model_path" << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Qwen model in root directory, preferring diffusion_model_path");
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
// For non-Qwen models in root, try architecture detection
|
|
// For non-Qwen models in root, try architecture detection
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Model is in root directory '" << parentDirName << "', attempting architecture detection" << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Model is in root directory '" + parentDirName + "', attempting architecture detection");
|
|
|
}
|
|
}
|
|
|
detectionSource = "architecture_fallback";
|
|
detectionSource = "architecture_fallback";
|
|
|
|
|
|
|
@@ -118,10 +120,10 @@ public:
|
|
|
detectionResult = ModelDetector::detectModel(modelPath);
|
|
detectionResult = ModelDetector::detectModel(modelPath);
|
|
|
detectionSuccessful = true;
|
|
detectionSuccessful = true;
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Architecture detection found: " << detectionResult.architectureName << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Architecture detection found: " + detectionResult.architectureName);
|
|
|
}
|
|
}
|
|
|
} catch (const std::exception& e) {
|
|
} catch (const std::exception& e) {
|
|
|
- std::cerr << "Warning: Architecture detection failed: " << e.what() << ". Using default loading method." << std::endl;
|
|
|
|
|
|
|
+ LOG_ERROR("Warning: Architecture detection failed: " + std::string(e.what()) + ". Using default loading method.");
|
|
|
detectionResult.architecture = ModelArchitecture::UNKNOWN;
|
|
detectionResult.architecture = ModelArchitecture::UNKNOWN;
|
|
|
detectionResult.architectureName = "Unknown";
|
|
detectionResult.architectureName = "Unknown";
|
|
|
}
|
|
}
|
|
@@ -148,7 +150,7 @@ public:
|
|
|
// Unknown architectures fall back to model_path for backward compatibility
|
|
// Unknown architectures fall back to model_path for backward compatibility
|
|
|
useDiffusionModelPath = false;
|
|
useDiffusionModelPath = false;
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Warning: Unknown model architecture detected, using default model_path for backward compatibility" << std::endl;
|
|
|
|
|
|
|
+ LOG_WARNING("Warning: Unknown model architecture detected, using default model_path for backward compatibility");
|
|
|
}
|
|
}
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
@@ -160,7 +162,7 @@ public:
|
|
|
} else {
|
|
} else {
|
|
|
// Unknown directory - try architecture detection
|
|
// Unknown directory - try architecture detection
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Model is in unknown directory '" << parentDirName << "', attempting architecture detection as fallback" << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Model is in unknown directory '" + parentDirName + "', attempting architecture detection as fallback");
|
|
|
}
|
|
}
|
|
|
detectionSource = "architecture_fallback";
|
|
detectionSource = "architecture_fallback";
|
|
|
|
|
|
|
@@ -168,10 +170,10 @@ public:
|
|
|
detectionResult = ModelDetector::detectModel(modelPath);
|
|
detectionResult = ModelDetector::detectModel(modelPath);
|
|
|
detectionSuccessful = true;
|
|
detectionSuccessful = true;
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Fallback detection found architecture: " << detectionResult.architectureName << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Fallback detection found architecture: " + detectionResult.architectureName);
|
|
|
}
|
|
}
|
|
|
} catch (const std::exception& e) {
|
|
} catch (const std::exception& e) {
|
|
|
- std::cerr << "Warning: Fallback model detection failed: " << e.what() << ". Using default loading method." << std::endl;
|
|
|
|
|
|
|
+ LOG_ERROR("Warning: Fallback model detection failed: " + std::string(e.what()) + ". Using default loading method.");
|
|
|
detectionResult.architecture = ModelArchitecture::UNKNOWN;
|
|
detectionResult.architecture = ModelArchitecture::UNKNOWN;
|
|
|
detectionResult.architectureName = "Unknown";
|
|
detectionResult.architectureName = "Unknown";
|
|
|
}
|
|
}
|
|
@@ -198,7 +200,7 @@ public:
|
|
|
// Unknown architectures fall back to model_path for backward compatibility
|
|
// Unknown architectures fall back to model_path for backward compatibility
|
|
|
useDiffusionModelPath = false;
|
|
useDiffusionModelPath = false;
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Warning: Unknown model architecture detected, using default model_path for backward compatibility" << std::endl;
|
|
|
|
|
|
|
+ LOG_WARNING("Warning: Unknown model architecture detected, using default model_path for backward compatibility");
|
|
|
}
|
|
}
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
@@ -213,13 +215,13 @@ public:
|
|
|
ctxParams.diffusion_model_path = persistentModelPath.c_str();
|
|
ctxParams.diffusion_model_path = persistentModelPath.c_str();
|
|
|
ctxParams.model_path = nullptr; // Clear the traditional path
|
|
ctxParams.model_path = nullptr; // Clear the traditional path
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Using diffusion_model_path (source: " << detectionSource << ")" << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Using diffusion_model_path (source: " + detectionSource + ")");
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
ctxParams.model_path = persistentModelPath.c_str();
|
|
ctxParams.model_path = persistentModelPath.c_str();
|
|
|
ctxParams.diffusion_model_path = nullptr; // Clear the modern path
|
|
ctxParams.diffusion_model_path = nullptr; // Clear the modern path
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Using model_path (source: " << detectionSource << ")" << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Using model_path (source: " + detectionSource + ")");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -227,13 +229,13 @@ public:
|
|
|
if (!persistentClipLPath.empty()) {
|
|
if (!persistentClipLPath.empty()) {
|
|
|
ctxParams.clip_l_path = persistentClipLPath.c_str();
|
|
ctxParams.clip_l_path = persistentClipLPath.c_str();
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Using CLIP-L path: " << std::filesystem::absolute(persistentClipLPath) << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Using CLIP-L path: " + std::filesystem::absolute(persistentClipLPath).string());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
if (!persistentClipGPath.empty()) {
|
|
if (!persistentClipGPath.empty()) {
|
|
|
ctxParams.clip_g_path = persistentClipGPath.c_str();
|
|
ctxParams.clip_g_path = persistentClipGPath.c_str();
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Using CLIP-G path: " << std::filesystem::absolute(persistentClipGPath) << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Using CLIP-G path: " + std::filesystem::absolute(persistentClipGPath).string());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
if (!persistentVaePath.empty()) {
|
|
if (!persistentVaePath.empty()) {
|
|
@@ -241,12 +243,11 @@ public:
|
|
|
if (std::filesystem::exists(persistentVaePath)) {
|
|
if (std::filesystem::exists(persistentVaePath)) {
|
|
|
ctxParams.vae_path = persistentVaePath.c_str();
|
|
ctxParams.vae_path = persistentVaePath.c_str();
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Using VAE path: " << std::filesystem::absolute(persistentVaePath) << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Using VAE path: " + std::filesystem::absolute(persistentVaePath).string());
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "VAE file not found: " << std::filesystem::absolute(persistentVaePath)
|
|
|
|
|
- << " - continuing without VAE" << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("VAE file not found: " + std::filesystem::absolute(persistentVaePath).string() + " - continuing without VAE");
|
|
|
}
|
|
}
|
|
|
ctxParams.vae_path = nullptr;
|
|
ctxParams.vae_path = nullptr;
|
|
|
}
|
|
}
|
|
@@ -254,25 +255,25 @@ public:
|
|
|
if (!persistentTaesdPath.empty()) {
|
|
if (!persistentTaesdPath.empty()) {
|
|
|
ctxParams.taesd_path = persistentTaesdPath.c_str();
|
|
ctxParams.taesd_path = persistentTaesdPath.c_str();
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Using TAESD path: " << std::filesystem::absolute(persistentTaesdPath) << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Using TAESD path: " + std::filesystem::absolute(persistentTaesdPath).string());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
if (!persistentControlNetPath.empty()) {
|
|
if (!persistentControlNetPath.empty()) {
|
|
|
ctxParams.control_net_path = persistentControlNetPath.c_str();
|
|
ctxParams.control_net_path = persistentControlNetPath.c_str();
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Using ControlNet path: " << std::filesystem::absolute(persistentControlNetPath) << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Using ControlNet path: " + std::filesystem::absolute(persistentControlNetPath).string());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
if (!persistentLoraModelDir.empty()) {
|
|
if (!persistentLoraModelDir.empty()) {
|
|
|
ctxParams.lora_model_dir = persistentLoraModelDir.c_str();
|
|
ctxParams.lora_model_dir = persistentLoraModelDir.c_str();
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Using LoRA model directory: " << std::filesystem::absolute(persistentLoraModelDir) << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Using LoRA model directory: " + std::filesystem::absolute(persistentLoraModelDir).string());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
if (!persistentEmbeddingDir.empty()) {
|
|
if (!persistentEmbeddingDir.empty()) {
|
|
|
ctxParams.embedding_dir = persistentEmbeddingDir.c_str();
|
|
ctxParams.embedding_dir = persistentEmbeddingDir.c_str();
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Using embedding directory: " << std::filesystem::absolute(persistentEmbeddingDir) << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Using embedding directory: " + std::filesystem::absolute(persistentEmbeddingDir).string());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -290,17 +291,17 @@ public:
|
|
|
|
|
|
|
|
// Create the stable-diffusion context
|
|
// Create the stable-diffusion context
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Attempting to create stable-diffusion context with selected parameters..." << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Attempting to create stable-diffusion context with selected parameters...");
|
|
|
}
|
|
}
|
|
|
sdContext = new_sd_ctx(&ctxParams);
|
|
sdContext = new_sd_ctx(&ctxParams);
|
|
|
if (!sdContext) {
|
|
if (!sdContext) {
|
|
|
lastError = "Failed to create stable-diffusion context";
|
|
lastError = "Failed to create stable-diffusion context";
|
|
|
- std::cerr << "Error: " << lastError << " with initial attempt" << std::endl;
|
|
|
|
|
|
|
+ LOG_ERROR("Error: " + lastError + " with initial attempt");
|
|
|
|
|
|
|
|
// If we used diffusion_model_path and it failed, try fallback to model_path
|
|
// If we used diffusion_model_path and it failed, try fallback to model_path
|
|
|
if (useDiffusionModelPath) {
|
|
if (useDiffusionModelPath) {
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Warning: Failed to load with diffusion_model_path. Attempting fallback to model_path..." << std::endl;
|
|
|
|
|
|
|
+ LOG_WARNING("Warning: Failed to load with diffusion_model_path. Attempting fallback to model_path...");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Re-initialize context parameters
|
|
// Re-initialize context parameters
|
|
@@ -351,18 +352,18 @@ public:
|
|
|
ctxParams.wtype = StableDiffusionWrapper::stringToModelType(params.modelType);
|
|
ctxParams.wtype = StableDiffusionWrapper::stringToModelType(params.modelType);
|
|
|
|
|
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Attempting to create context with fallback model_path..." << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Attempting to create context with fallback model_path...");
|
|
|
}
|
|
}
|
|
|
// Try creating context again with fallback
|
|
// Try creating context again with fallback
|
|
|
sdContext = new_sd_ctx(&ctxParams);
|
|
sdContext = new_sd_ctx(&ctxParams);
|
|
|
if (!sdContext) {
|
|
if (!sdContext) {
|
|
|
lastError = "Failed to create stable-diffusion context with both diffusion_model_path and model_path fallback";
|
|
lastError = "Failed to create stable-diffusion context with both diffusion_model_path and model_path fallback";
|
|
|
- std::cerr << "Error: " << lastError << std::endl;
|
|
|
|
|
|
|
+ LOG_ERROR("Error: " + lastError);
|
|
|
|
|
|
|
|
// Additional fallback: try with minimal parameters for GGUF models
|
|
// Additional fallback: try with minimal parameters for GGUF models
|
|
|
if (modelFileName.find(".gguf") != std::string::npos || modelFileName.find(".ggml") != std::string::npos) {
|
|
if (modelFileName.find(".gguf") != std::string::npos || modelFileName.find(".ggml") != std::string::npos) {
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Detected GGUF/GGML model, attempting minimal parameter fallback..." << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Detected GGUF/GGML model, attempting minimal parameter fallback...");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Re-initialize with minimal parameters
|
|
// Re-initialize with minimal parameters
|
|
@@ -375,32 +376,32 @@ public:
|
|
|
ctxParams.wtype = StableDiffusionWrapper::stringToModelType(params.modelType);
|
|
ctxParams.wtype = StableDiffusionWrapper::stringToModelType(params.modelType);
|
|
|
|
|
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Attempting to create context with minimal GGUF parameters..." << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Attempting to create context with minimal GGUF parameters...");
|
|
|
}
|
|
}
|
|
|
sdContext = new_sd_ctx(&ctxParams);
|
|
sdContext = new_sd_ctx(&ctxParams);
|
|
|
|
|
|
|
|
if (!sdContext) {
|
|
if (!sdContext) {
|
|
|
lastError = "Failed to create stable-diffusion context even with minimal GGUF parameters";
|
|
lastError = "Failed to create stable-diffusion context even with minimal GGUF parameters";
|
|
|
- std::cerr << "Error: " << lastError << std::endl;
|
|
|
|
|
|
|
+ LOG_ERROR("Error: " + lastError);
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Successfully loaded GGUF model with minimal parameters: " << absModelPath << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Successfully loaded GGUF model with minimal parameters: " + absModelPath.string());
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Successfully loaded model with fallback to model_path: " << absModelPath << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Successfully loaded model with fallback to model_path: " + absModelPath.string());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
// Try minimal fallback for non-diffusion_model_path failures
|
|
// Try minimal fallback for non-diffusion_model_path failures
|
|
|
if (modelFileName.find(".gguf") != std::string::npos || modelFileName.find(".ggml") != std::string::npos) {
|
|
if (modelFileName.find(".gguf") != std::string::npos || modelFileName.find(".ggml") != std::string::npos) {
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Detected GGUF/GGML model, attempting minimal parameter fallback..." << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Detected GGUF/GGML model, attempting minimal parameter fallback...");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Re-initialize with minimal parameters
|
|
// Re-initialize with minimal parameters
|
|
@@ -412,21 +413,21 @@ public:
|
|
|
ctxParams.wtype = StableDiffusionWrapper::stringToModelType(params.modelType);
|
|
ctxParams.wtype = StableDiffusionWrapper::stringToModelType(params.modelType);
|
|
|
|
|
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Attempting to create context with minimal GGUF parameters..." << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Attempting to create context with minimal GGUF parameters...");
|
|
|
}
|
|
}
|
|
|
sdContext = new_sd_ctx(&ctxParams);
|
|
sdContext = new_sd_ctx(&ctxParams);
|
|
|
|
|
|
|
|
if (!sdContext) {
|
|
if (!sdContext) {
|
|
|
lastError = "Failed to create stable-diffusion context even with minimal GGUF parameters";
|
|
lastError = "Failed to create stable-diffusion context even with minimal GGUF parameters";
|
|
|
- std::cerr << "Error: " << lastError << std::endl;
|
|
|
|
|
|
|
+ LOG_ERROR("Error: " + lastError);
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Successfully loaded GGUF model with minimal parameters: " << absModelPath << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Successfully loaded GGUF model with minimal parameters: " + absModelPath.string());
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
- std::cerr << "Error: " << lastError << std::endl;
|
|
|
|
|
|
|
+ LOG_ERROR("Error: " + lastError);
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -434,24 +435,28 @@ public:
|
|
|
|
|
|
|
|
// Log successful loading with detection information
|
|
// Log successful loading with detection information
|
|
|
if (params.verbose) {
|
|
if (params.verbose) {
|
|
|
- std::cout << "Successfully loaded model: " << absModelPath << std::endl;
|
|
|
|
|
- std::cout << " Detection source: " << detectionSource << std::endl;
|
|
|
|
|
- std::cout << " Loading method: " << (useDiffusionModelPath ? "diffusion_model_path" : "model_path") << std::endl;
|
|
|
|
|
- std::cout << " Parent directory: " << parentDirName << std::endl;
|
|
|
|
|
- std::cout << " Model filename: " << modelFileName << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Successfully loaded model: " + absModelPath.string());
|
|
|
|
|
+ LOG_DEBUG(" Detection source: " + detectionSource);
|
|
|
|
|
+ LOG_DEBUG(" Loading method: " + std::string(useDiffusionModelPath ? "diffusion_model_path" : "model_path"));
|
|
|
|
|
+ LOG_DEBUG(" Parent directory: " + parentDirName);
|
|
|
|
|
+ LOG_DEBUG(" Model filename: " + modelFileName);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Log additional model properties if architecture detection was performed
|
|
// Log additional model properties if architecture detection was performed
|
|
|
if (detectionSuccessful && params.verbose) {
|
|
if (detectionSuccessful && params.verbose) {
|
|
|
- std::cout << " Architecture: " << detectionResult.architectureName << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG(" Architecture: " + detectionResult.architectureName);
|
|
|
if (detectionResult.textEncoderDim > 0) {
|
|
if (detectionResult.textEncoderDim > 0) {
|
|
|
- std::cout << " Text encoder dimension: " << detectionResult.textEncoderDim << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG(" Text encoder dimension: " + std::to_string(detectionResult.textEncoderDim));
|
|
|
}
|
|
}
|
|
|
if (detectionResult.needsVAE) {
|
|
if (detectionResult.needsVAE) {
|
|
|
- std::cout << " Requires VAE: " << (detectionResult.recommendedVAE.empty() ? "Yes" : detectionResult.recommendedVAE) << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG(" Requires VAE: " + (detectionResult.recommendedVAE.empty() ? std::string("Yes") : detectionResult.recommendedVAE));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // Store current model info for potential reload after upscaling
|
|
|
|
|
+ currentModelPath = modelPath;
|
|
|
|
|
+ currentModelParams = params;
|
|
|
|
|
+
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -461,9 +466,12 @@ public:
|
|
|
free_sd_ctx(sdContext);
|
|
free_sd_ctx(sdContext);
|
|
|
sdContext = nullptr;
|
|
sdContext = nullptr;
|
|
|
if (verbose) {
|
|
if (verbose) {
|
|
|
- std::cout << "Unloaded stable-diffusion model" << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("Unloaded stable-diffusion model");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ // Clear stored model info
|
|
|
|
|
+ currentModelPath.clear();
|
|
|
|
|
+ currentModelParams = StableDiffusionWrapper::GenerationParams();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
bool isModelLoaded() const {
|
|
bool isModelLoaded() const {
|
|
@@ -520,14 +528,14 @@ public:
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Generate the image
|
|
// Generate the image
|
|
|
- std::cout << "[TIMING_ANALYSIS] Starting generate_image() call" << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("[TIMING_ANALYSIS] Starting generate_image() call");
|
|
|
auto generationCallStart = std::chrono::high_resolution_clock::now();
|
|
auto generationCallStart = std::chrono::high_resolution_clock::now();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
sd_image_t* sdImages = generate_image(sdContext, &genParams);
|
|
sd_image_t* sdImages = generate_image(sdContext, &genParams);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
auto generationCallEnd = std::chrono::high_resolution_clock::now();
|
|
auto generationCallEnd = std::chrono::high_resolution_clock::now();
|
|
|
auto generationCallTime = std::chrono::duration_cast<std::chrono::milliseconds>(generationCallEnd - generationCallStart).count();
|
|
auto generationCallTime = std::chrono::duration_cast<std::chrono::milliseconds>(generationCallEnd - generationCallStart).count();
|
|
|
- std::cout << "[TIMING_ANALYSIS] generate_image() call completed in " << generationCallTime << "ms" << std::endl;
|
|
|
|
|
|
|
+ LOG_DEBUG("[TIMING_ANALYSIS] generate_image() call completed in " + std::to_string(generationCallTime) + "ms");
|
|
|
|
|
|
|
|
// Clear and clean up progress callback - FIX: Wait for any pending callbacks
|
|
// Clear and clean up progress callback - FIX: Wait for any pending callbacks
|
|
|
sd_set_progress_callback(nullptr, nullptr);
|
|
sd_set_progress_callback(nullptr, nullptr);
|
|
@@ -951,6 +959,18 @@ public:
|
|
|
|
|
|
|
|
auto startTime = std::chrono::high_resolution_clock::now();
|
|
auto startTime = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
|
|
+ // Unload stable diffusion checkpoint before loading upscaler to prevent memory conflicts
|
|
|
|
|
+ {
|
|
|
|
|
+ std::lock_guard<std::mutex> lock(contextMutex);
|
|
|
|
|
+ if (sdContext) {
|
|
|
|
|
+ if (verbose) {
|
|
|
|
|
+ LOG_DEBUG("Unloading stable diffusion checkpoint before loading upscaler model");
|
|
|
|
|
+ }
|
|
|
|
|
+ free_sd_ctx(sdContext);
|
|
|
|
|
+ sdContext = nullptr;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// Create upscaler context
|
|
// Create upscaler context
|
|
|
upscaler_ctx_t* upscalerCtx = new_upscaler_ctx(
|
|
upscaler_ctx_t* upscalerCtx = new_upscaler_ctx(
|
|
|
esrganPath.c_str(),
|
|
esrganPath.c_str(),
|