Commit 7bb4eec1 authored by Your Name's avatar Your Name

Fix: Improve sd.cpp model loading fallback logic

- Enhanced model resolution for sd.cpp fallback path
- Added multiple fallback strategies:
  1. Try HuggingFace GGUF resolution (existing)
  2. Fallback to direct file path check
  3. Fallback to cached model lookup
  4. Last resort: attempt download as URL
- Better error logging and handling
- Ensures model loading attempts all possible resolution paths before failing
parent 63460a13
...@@ -701,86 +701,45 @@ async def create_image_generation(request: ImageGenerationRequest, http_request: ...@@ -701,86 +701,45 @@ async def create_image_generation(request: ImageGenerationRequest, http_request:
print(f"Could not resolve as HuggingFace model: {e}") print(f"Could not resolve as HuggingFace model: {e}")
if model_path is None: if model_path is None:
print("Warning: Could not resolve sd.cpp model path") print("Warning: Could not resolve sd.cpp model path via HuggingFace GGUF resolution")
sd_cpp_error = "Could not resolve model path" # Fallback: try to use the model name as a direct path (for local models or if HF resolution failed)
else: print(f"Fallback: attempting to use '{model_to_use}' as direct model path")
# Load sd.cpp model if os.path.isfile(model_to_use):
# Determine backend to use based on CLI args model_path = model_to_use
backend = getattr(global_args, 'backend', 'auto') print(f"Using local file: {model_path}")
image_backend = getattr(global_args, 'image_backend', 'auto')
# Use CUDA only if explicitly requested via --backend nvidia or --image-backend nvidia
use_cuda = (backend == 'nvidia' or backend == 'cuda' or
image_backend == 'nvidia' or image_backend == 'cuda')
if use_cuda:
print(f"Using CUDA backend for sd.cpp image generation")
else:
print(f"Using Vulkan backend for sd.cpp image generation")
# Build kwargs for stable-diffusion-cpp with CLI args
sd_kwargs = {'diffusion_model_path': model_path}
# Add VAE path from CLI args if provided
vae_path = getattr(global_args, 'vae_path', None)
if vae_path:
# Check if it's a URL and download if needed
if vae_path.startswith('http://') or vae_path.startswith('https://'):
cached = multi_model_manager.get_cached_model_path(vae_path)
if cached:
sd_kwargs['vae_path'] = cached
print(f"Using cached VAE model: {cached}")
else:
cache_dir = multi_model_manager.get_model_cache_dir()
sd_kwargs['vae_path'] = multi_model_manager.download_model(vae_path, cache_dir)
else: else:
sd_kwargs['vae_path'] = vae_path # Not a local file, check if it might be a cached model under a different name
cached_path = multi_model_manager.get_cached_model_path(model_to_use)
# Add LLM/CLIP path from CLI args if provided if cached_path:
llm_path = getattr(global_args, 'llm_path', None) model_path = cached_path
if llm_path: print(f"Using cached model: {model_path}")
if llm_path.startswith('http://') or llm_path.startswith('https://'):
cached = multi_model_manager.get_cached_model_path(llm_path)
if cached:
sd_kwargs['llm_path'] = cached
print(f"Using cached LLM model: {cached}")
else: else:
# Last resort: try to download it as if it were a URL
print(f"Attempting to download '{model_to_use}' as model URL")
try:
cache_dir = multi_model_manager.get_model_cache_dir() cache_dir = multi_model_manager.get_model_cache_dir()
sd_kwargs['llm_path'] = multi_model_manager.download_model(llm_path, cache_dir) model_path = multi_model_manager.download_model(model_to_use, cache_dir)
else: print(f"Downloaded to: {model_path}")
sd_kwargs['llm_path'] = llm_path except Exception as download_error:
print(f"Download failed: {download_error}")
model_path = None
# Add T5XXL path from CLI args if provided if model_path is None:
t5xxl_path = getattr(global_args, 't5xxl_path', None) print("Error: Could not resolve sd.cpp model path through any method")
if t5xxl_path: sd_cpp_error = "Could not resolve model path"
if t5xxl_path.startswith('http://') or t5xxl_path.startswith('https://'):
cached = multi_model_manager.get_cached_model_path(t5xxl_path)
if cached:
sd_kwargs['t5xxl_path'] = cached
print(f"Using cached T5XXL model: {cached}")
else:
cache_dir = multi_model_manager.get_model_cache_dir()
sd_kwargs['t5xxl_path'] = multi_model_manager.download_model(t5xxl_path, cache_dir)
else: else:
sd_kwargs['t5xxl_path'] = t5xxl_path # Load sd.cpp model (continue below)
pass
# Add clip_on_cpu if specified
if getattr(global_args, 'clip_on_cpu', False):
sd_kwargs['keep_clip_on_cpu'] = True
print(f"DEBUG: Running CLIP on CPU to save VRAM (keep_clip_on_cpu=True)")
# Use all available CPU cores
import psutil
sd_kwargs['n_threads'] = psutil.cpu_count()
sd_model = StableDiffusion(**sd_kwargs)
# Cache the model for reuse on subsequent requests # Load sd.cpp model if we have a valid path
cache_key = f"image:{model_path}" if model_path is not None:
multi_model_manager.add_model(cache_key, sd_model) # Check if it's a stable-diffusion-cpp model (has generate method from sd.cpp)
try:
from stable_diffusion_cpp import StableDiffusion
if isinstance(sd_model, StableDiffusion):
print(f"Using stable-diffusion-cpp-python for image generation") print(f"Using stable-diffusion-cpp-python for image generation")
# Use sd.cpp for generation
# Generate images # Parse size
width, height = 512, 512 width, height = 512, 512
if request.size: if request.size:
parts = request.size.split("x") parts = request.size.split("x")
...@@ -791,8 +750,10 @@ async def create_image_generation(request: ImageGenerationRequest, http_request: ...@@ -791,8 +750,10 @@ async def create_image_generation(request: ImageGenerationRequest, http_request:
except ValueError: except ValueError:
pass pass
steps = 4 # Use default steps for Z-Image Turbo (very fast)
steps = 4 # Default for fast generation
# Generate images using sd.cpp (run in thread to not block event loop)
# Use request seed if provided, otherwise use CLI default seed # Use request seed if provided, otherwise use CLI default seed
seed = request.seed if request.seed is not None else getattr(global_args, 'image_seed', None) seed = request.seed if request.seed is not None else getattr(global_args, 'image_seed', None)
...@@ -824,6 +785,17 @@ async def create_image_generation(request: ImageGenerationRequest, http_request: ...@@ -824,6 +785,17 @@ async def create_image_generation(request: ImageGenerationRequest, http_request:
"created": int(time.time()), "created": int(time.time()),
"data": images "data": images
} }
except ImportError as e:
# stable-diffusion-cpp not available
sd_cpp_error = str(e)
print(f"stable-diffusion-cpp-python not available: {sd_cpp_error}")
except Exception as e:
print(f"sd.cpp generation error: {e}")
sd_cpp_error = str(e)
else:
# model_path is None after all fallback attempts
print("Error: Could not resolve sd.cpp model path through any method")
sd_cpp_error = "Could not resolve model path"
except ImportError as e: except ImportError as e:
sd_cpp_error = str(e) sd_cpp_error = str(e)
print(f"stable-diffusion-cpp-python not available: {sd_cpp_error}") print(f"stable-diffusion-cpp-python not available: {sd_cpp_error}")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment