Spaces:
Running on Zero
Running on Zero
Add LoRA injector to replace pre-merged LoRAs
Browse filesFiles changed (7)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667from copy import deepcopy
def inject(assembler, chain_definition, chain_items):
if not chain_items:
return
start_node_name = chain_definition.get('start')
start_node_id = None
if start_node_name:
if start_node_name not in assembler.node_map:
print(f"Warning: Start node '{start_node_name}' for dynamic LoRA chain not found. Skipping chain.")
return
start_node_id = assembler.node_map[start_node_name]
output_map = chain_definition.get('output_map', {})
current_connections = {}
for key, type_name in output_map.items():
if ':' in str(key):
node_name, idx_str = key.split(':')
if node_name not in assembler.node_map:
print(f"Warning: Node '{node_name}' in chain's output_map not found. Skipping.")
continue
node_id = assembler.node_map[node_name]
start_output_idx = int(idx_str)
current_connections[type_name] = [node_id, start_output_idx]
elif start_node_id:
start_output_idx = int(key)
current_connections[type_name] = [start_node_id, start_output_idx]
else:
print(f"Warning: LoRA chain has no 'start' node defined, and an output_map key '{key}' is not in 'node:index' format. Skipping this connection.")
input_map = chain_definition.get('input_map', {})
chain_output_map = chain_definition.get('template_output_map', { "0": "model", "1": "clip" })
for item_data in chain_items:
template_name = chain_definition['template']
template = assembler._get_node_template(template_name)
node_data = deepcopy(template)
for param_name, value in item_data.items():
if param_name in node_data['inputs']:
node_data['inputs'][param_name] = value
for type_name, input_name in input_map.items():
if type_name in current_connections:
node_data['inputs'][input_name] = current_connections[type_name]
new_node_id = assembler._get_unique_id()
assembler.workflow[new_node_id] = node_data
for idx_str, type_name in chain_output_map.items():
current_connections[type_name] = [new_node_id, int(idx_str)]
end_input_map = chain_definition.get('end_input_map', {})
for type_name, targets in end_input_map.items():
if type_name in current_connections:
if not isinstance(targets, list):
targets = [targets]
for target_str in targets:
end_node_name, end_input_name = target_str.split(':')
if end_node_name in assembler.node_map:
end_node_id = assembler.node_map[end_node_name]
assembler.workflow[end_node_id]['inputs'][end_input_name] = current_connections[type_name]
else:
print(f"Warning: End node '{end_node_name}' for dynamic chain not found. Skipping connection.")123423678910114567896 unmodified lines1617182526192021303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031042223249 unmodified lines343536120373839404142431271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651664445import gc
from typing import Dict, List, Any, Set
import torch
from typing import List
import gradio as gr
from comfy import model_management
from core.settings import ALL_MODEL_MAP, CHECKPOINT_DIR, LORA_DIR, DIFFUSION_MODELS_DIR, VAE_DIR, TEXT_ENCODERS_DIR
from comfy_integration.nodes import LoraLoader
from nodes import NODE_CLASS_MAPPINGS
from utils.app_utils import get_value_at_index, _ensure_model_downloaded
from core.settings import ALL_MODEL_MAP
from utils.app_utils import _ensure_model_downloaded
class ModelManager:
_instance = None6 unmodified lines def __init__(self):
if hasattr(self, 'initialized'):
return
self.loaded_models: Dict[str, Any] = {}
self.last_active_loras: List[Dict[str, Any]] = []
self.initialized = True
print("✅ ModelManager initialized.")
def get_loaded_model_names(self) -> Set[str]:
return set(self.loaded_models.keys())
def _load_model_combo(self, display_name: str, active_loras: List[Dict[str, Any]], progress) -> Dict[str, Any]:
print(f"--- [ModelManager] Loading model combo: '{display_name}' ---")
if display_name not in ALL_MODEL_MAP:
raise ValueError(f"Model '{display_name}' not found in configuration.")
_, components, _, _ = ALL_MODEL_MAP[display_name]
unet_filename = components.get('unet')
clip_filename = components.get('clip')
vae_filename = components.get('vae')
if not all([unet_filename, clip_filename, vae_filename]):
raise ValueError(f"Model '{display_name}' is missing required components (unet, clip, or vae) in model_list.yaml.")
unet_loader = NODE_CLASS_MAPPINGS["UNETLoader"]()
clip_loader = NODE_CLASS_MAPPINGS["CLIPLoader"]()
vae_loader = NODE_CLASS_MAPPINGS["VAELoader"]()
print(" - Loading UNET...")
unet_tuple = unet_loader.load_unet(unet_name=unet_filename, weight_dtype="default")
print(" - Loading CLIP...")
clip_tuple = clip_loader.load_clip(clip_name=clip_filename, type="flux2", device="default")
print(" - Loading VAE...")
vae_tuple = vae_loader.load_vae(vae_name=vae_filename)
unet_object = get_value_at_index(unet_tuple, 0)
clip_object = get_value_at_index(clip_tuple, 0)
if active_loras:
print(f"--- [ModelManager] Applying {len(active_loras)} LoRAs on CPU... ---")
lora_loader = LoraLoader()
patched_unet, patched_clip = unet_object, clip_object
for lora_info in active_loras:
patched_unet, patched_clip = lora_loader.load_lora(
model=patched_unet,
clip=patched_clip,
lora_name=lora_info["lora_name"],
strength_model=lora_info["strength_model"],
strength_clip=lora_info["strength_clip"]
)
unet_object = patched_unet
clip_object = patched_clip
print(f"--- [ModelManager] ✅ All LoRAs merged into the model on CPU. ---")
loaded_combo = {
"unet": (unet_object,),
"clip": (clip_object,),
"vae": vae_tuple,
}
print(f"--- [ModelManager] ✅ Successfully loaded combo '{display_name}' to CPU/RAM ---")
return loaded_combo
def move_models_to_gpu(self, required_models: List[str]):
print(f"--- [ModelManager] Moving models to GPU: {required_models} ---")
models_to_load_gpu = []
for name in required_models:
if name in self.loaded_models:
model_combo = self.loaded_models[name]
models_to_load_gpu.append(get_value_at_index(model_combo.get("unet"), 0))
if models_to_load_gpu:
model_management.load_models_gpu(models_to_load_gpu)
print("--- [ModelManager] ✅ Models successfully moved to GPU. ---")
else:
print("--- [ModelManager] ⚠️ No component models found to move to GPU. ---")
def ensure_models_downloaded(self, required_models: List[str], progress):
print(f"--- [ModelManager] Ensuring models are downloaded: {required_models} ---")
9 unmodified lines
for i, filename in enumerate(files_to_download):
if progress and hasattr(progress, '__call__'):
progress(i / total_files, desc=f"Checking file: {filename}")
progress(i / total_files if total_files > 0 else 0, desc=f"Checking file: {filename}")
try:
_ensure_model_downloaded(filename, progress)
except Exception as e:
raise gr.Error(f"Failed to download model component '{filename}'. Reason: {e}")
print(f"--- [ModelManager] ✅ All required models are present on disk. ---")
def load_managed_models(self, required_models: List[str], active_loras: List[Dict[str, Any]], progress) -> Dict[str, Any]:
required_set = set(required_models)
current_set = set(self.loaded_models.keys())
loras_changed = active_loras != self.last_active_loras
models_to_unload = current_set - required_set
if models_to_unload or loras_changed:
if models_to_unload:
print(f"--- [ModelManager] Models to unload: {models_to_unload} ---")
if loras_changed and not models_to_unload:
models_to_unload = current_set.intersection(required_set)
if active_loras:
print(f"--- [ModelManager] LoRA configuration changed. Reloading base model(s): {models_to_unload} ---")
else:
print(f"--- [ModelManager] LoRAs removed. Reloading base model(s) to clear patches: {models_to_unload} ---")
model_management.unload_all_models()
self.loaded_models.clear()
gc.collect()
torch.cuda.empty_cache()
print("--- [ModelManager] All models unloaded to free RAM. ---")
models_to_load = required_set if (models_to_unload or loras_changed) else required_set - current_set
if models_to_load:
print(f"--- [ModelManager] Models to load: {models_to_load} ---")
for i, display_name in enumerate(models_to_load):
progress(i / len(models_to_load), desc=f"Loading model: {display_name}")
try:
loaded_model_data = self._load_model_combo(display_name, active_loras, progress)
self.loaded_models[display_name] = loaded_model_data
except Exception as e:
raise gr.Error(f"Failed to load model combo or apply LoRA for '{display_name}'. Reason: {e}")
else:
print(f"--- [ModelManager] All required models are already loaded. ---")
self.last_active_loras = active_loras
return {name: self.loaded_models[name] for name in required_models}
model_manager = ModelManager()112 unmodified lines11311411511611611711811912012111912012412512112212812913013113213313413513613713812312412530 unmodified lines156157158159175176177178179180181182183184185186187188189160161162163164165166167168169170171172173174175191192193176177178158 unmodified lines33733833934034134234314 unmodified lines358359360378361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404112 unmodified lines
return get_value_at_index(computed_outputs[image_source_node_id], image_source_index)
def _gpu_logic(self, ui_inputs: Dict, loras_string: str, required_models_for_gpu: List[str], workflow: Dict[str, Any], assembler: WorkflowAssembler, progress=gr.Progress(track_tqdm=True)):
def _gpu_logic(self, ui_inputs: Dict, loras_string: str, workflow: Dict[str, Any], assembler: WorkflowAssembler, progress=gr.Progress(track_tqdm=True)):
model_display_name = ui_inputs['model_display_name']
progress(0.1, desc="Moving models to GPU...")
self.model_manager.move_models_to_gpu(required_models_for_gpu)
progress(0.4, desc="Executing workflow...")
loaded_model_combo = self.model_manager.loaded_models[model_display_name]
initial_objects = {}
unet_loader_id = assembler.node_map.get("unet_loader")
clip_loader_id = assembler.node_map.get("clip_loader")
vae_loader_id = assembler.node_map.get("vae_loader")
if unet_loader_id: initial_objects[unet_loader_id] = loaded_model_combo.get("unet")
if clip_loader_id: initial_objects[clip_loader_id] = loaded_model_combo.get("clip")
if vae_loader_id: initial_objects[vae_loader_id] = loaded_model_combo.get("vae")
if not all([unet_loader_id, clip_loader_id, vae_loader_id]):
raise RuntimeError("Workflow is missing one or more required loaders (unet_loader, clip_loader, vae_loader).")
decoded_images_tensor = self._execute_workflow(workflow, initial_objects=initial_objects)
output_images = []30 unmodified lines
lora_data = ui_inputs.get('lora_data', [])
active_loras_for_gpu, active_loras_for_meta = [], []
if lora_data:
sources, ids, scales, files = lora_data[0::4], lora_data[1::4], lora_data[2::4], lora_data[3::4]
for i, (source, lora_id, scale, _) in enumerate(zip(sources, ids, scales, files)):
if scale > 0 and lora_id and lora_id.strip():
lora_filename = None
if source == "File":
lora_filename = sanitize_filename(lora_id)
elif source == "Civitai":
local_path, status = get_lora_path(source, lora_id, ui_inputs['civitai_api_key'], progress)
if local_path: lora_filename = os.path.basename(local_path)
else: raise gr.Error(f"Failed to prepare LoRA {lora_id}: {status}")
if lora_filename:
active_loras_for_gpu.append({"lora_name": lora_filename, "strength_model": scale, "strength_clip": scale})
active_loras_for_meta.append(f"{source} {lora_id}:{scale}")
sources, ids, scales, files = lora_data[0::4], lora_data[1::4], lora_data[2::4], lora_data[3::4]
for i, (source, lora_id, scale, _) in enumerate(zip(sources, ids, scales, files)):
if scale > 0 and lora_id and lora_id.strip():
lora_filename = None
if source == "File":
lora_filename = sanitize_filename(lora_id)
elif source == "Civitai":
local_path, status = get_lora_path(source, lora_id, ui_inputs['civitai_api_key'], progress)
if local_path: lora_filename = os.path.basename(local_path)
else: raise gr.Error(f"Failed to prepare LoRA {lora_id}: {status}")
if lora_filename:
active_loras_for_gpu.append({"lora_name": lora_filename, "strength_model": scale, "strength_clip": scale})
active_loras_for_meta.append(f"{source} {lora_id}:{scale}")
progress(0.1, desc="Loading models into RAM...")
self.model_manager.load_managed_models(required_models, active_loras=active_loras_for_gpu, progress=progress)
ui_inputs['denoise'] = 1.0
if task_type == 'img2img': ui_inputs['denoise'] = ui_inputs.get('img2img_denoise', 0.7)
elif task_type == 'hires_fix': ui_inputs['denoise'] = ui_inputs.get('hires_denoise', 0.55)158 unmodified lines "unet_name": components['unet'],
"clip_name": components['clip'],
"vae_name": ui_inputs.get('vae_name', components['vae']),
"lora_chain": active_loras_for_gpu,
"conditioning_chain": active_conditioning,
"reference_latent_chain": active_reference_latents,
}14 unmodified lines task_name=f"ImageGen ({task_type})",
ui_inputs=ui_inputs,
loras_string=loras_string,
required_models_for_gpu=required_models,
workflow=workflow,
assembler=assembler,
progress=progress
)
import json
import glob
from PIL import PngImagePlugin
prompt_json = json.dumps(workflow)
out_dir = os.path.abspath(OUTPUT_DIR)
os.makedirs(out_dir, exist_ok=True)
try:
existing_files = glob.glob(os.path.join(out_dir, "gen_*.png"))
existing_files.sort(key=os.path.getmtime)
while len(existing_files) > 50:
os.remove(existing_files.pop(0))
except Exception as e:
print(f"Warning: Failed to cleanup output dir: {e}")
final_results = []
for img in results:
if not isinstance(img, Image.Image):
final_results.append(img)
continue
metadata = PngImagePlugin.PngInfo()
params_string = img.info.get("parameters", "")
if params_string:
metadata.add_text("parameters", params_string)
metadata.add_text("prompt", prompt_json)
filename = f"gen_{random.randint(1000000, 9999999)}.png"
filepath = os.path.join(out_dir, filename)
img.save(filepath, "PNG", pnginfo=metadata)
final_results.append(filepath)
results = final_results
finally:
for temp_file in temp_files_to_clean:
if temp_file and os.path.exists(temp_file):37 unmodified lines3839404142434445464748495051525354555637 unmodified lines - from: "neg_prompt:0"
to: "ksampler:negative"
dynamic_lora_chains:
lora_chain:
template: "LoraLoader"
output_map:
"unet_loader:0": "model"
"clip_loader:0": "clip"
input_map:
"model": "model"
"clip": "clip"
end_input_map:
"model": ["ksampler:model"]
"clip": ["pos_prompt:clip", "neg_prompt:clip"]
dynamic_conditioning_chains:
conditioning_chain:
ksampler_node: "ksampler"1234123456712 unmodified lines2021222324242526272829303132333435363738394041364243445 unmodified lines50515248535455565754585960comfyui-frontend-package==1.37.11
comfyui-workflow-templates==0.8.24
comfyui-embedded-docs==0.4.0
torch==2.9.1
comfyui-frontend-package==1.42.10
comfyui-workflow-templates==0.9.47
comfyui-embedded-docs==0.4.3
torch
torchsde
torchvision
torchaudio12 unmodified linespsutil
alembic
SQLAlchemy
filelock
av>=14.2.0
comfy-kitchen>=0.2.7
comfy-kitchen>=0.2.8
comfy-aimdo>=0.2.12
requests
simpleeval>=1.0.0
blake3
#non essential dependencies:
kornia>=0.7.1
spandrel
pydantic~=2.0
pydantic-settings~=2.0
PyOpenGL
glfw
addict
albumentations
filelock
ftfy
fvcore
huggingface-hub5 unmodified linesomegaconf
opencv-python>=4.7.0.72
python-dateutil
requests
scikit-image
scikit-learn
soundfile
spaces
svglib
torchsde
trimesh[easy]
yacs
yapf26 unmodified lines272829303031323334353435363726 unmodified lines "Other versions are also available: "
"[Z-Image](https://huggingface.co/spaces/RioShiina/ImageGen-Z-Image), "
"[Qwen-Image](https://huggingface.co/spaces/RioShiina/ImageGen-Qwen-Image), "
"[NewBie-Image](https://huggingface.co/spaces/RioShiina/ImageGen-NewBie-Image), "
"[Anima](https://huggingface.co/spaces/RioShiina/ImageGen-Anima), "
"[Illstrious](https://huggingface.co/spaces/RioShiina/ImageGen-Illstrious), "
"[NoobAI](https://huggingface.co/spaces/RioShiina/ImageGen-NoobAI), "
"[Pony](https://huggingface.co/spaces/RioShiina/ImageGen-Pony1), "
"[SDXL](https://huggingface.co/spaces/RioShiina/ImageGen-SDXL), "
"[SD1.5](https://huggingface.co/spaces/RioShiina/ImageGen-SD15)"
"[SDXL](https://huggingface.co/spaces/RioShiina/ImageGen-SDXL)"
)
with gr.Tabs(elem_id="tabs_container") as tabs:
with gr.TabItem("FLUX.2", id=0):12345676891011912injector_definitions:
dynamic_lora_chains:
module: "chain_injectors.lora_injector"
dynamic_conditioning_chains:
module: "chain_injectors.conditioning_injector"
dynamic_reference_latent_chains:
module: "chain_injectors.reference_latent_injector"
injector_order:
- dynamic_lora_chains
- dynamic_reference_latent_chains
- dynamic_conditioning_chains - dynamic_conditioning_chainschain_injectors/lora_injector.pyAdded+67
core/model_manager.py+4-127
core/pipelines/sd_image_pipeline.py+55-36
core/pipelines/workflow_recipes/_partials/conditioning/flux2.yaml+13
requirements.txt+11-8
ui/layout.py+2-3
yaml/injectors.yaml+5-2