waveshaker commited on
Commit
ed145f0
·
verified ·
1 Parent(s): 598781c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -44
app.py CHANGED
@@ -258,8 +258,6 @@ def patch_comfy_loader():
258
  import comfy.utils
259
  import safetensors.torch
260
  import safetensors
261
- import comfy.ldm.modules.attention
262
- from comfy.ldm.modules.attention import OptimizedAttention
263
  print("[🚀 提速补丁] [1/3] ✅ 基础模块导入成功", flush=True)
264
 
265
  _original_load_torch_file = comfy.utils.load_torch_file
@@ -340,7 +338,12 @@ def patch_comfy_loader():
340
  # =========================================================
341
  # 🌟 V3 Core Fix: More aggressive interception of OptimizedAttention._load_from_state_dict
342
  # =========================================================
343
- print("[🚀 提速补丁] [3/3] 正在替换 OptimizedAttention._load_from_state_dict...", flush=True)
 
 
 
 
 
344
  _original_load_from_state_dict = OptimizedAttention._load_from_state_dict
345
 
346
  def _patched_load_from_state_dict(self, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs):
@@ -647,6 +650,24 @@ def prepare_env(char_enable: bool, char_lora: str, unet_choice: str, task_id: st
647
  tqdm.auto.tqdm = original_tqdm
648
  _current_download_task_id.set(None)
649
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
650
  def build_workflow(
651
  prompt: str, unet_choice: str, custom_w: int, custom_h: int,
652
  steps: int, cfg: float, seed: int, sampler: str, scheduler: str,
@@ -669,61 +690,69 @@ def build_workflow(
669
  if lora_cfg["engine"] == engine:
670
  base_lora_inputs.append((lora_cfg["filename"], strength))
671
 
672
- current_model_node = "1"
673
  nodes = MODEL_CONFIG["node_types"]
674
  clip_loader_node = engine_cfg.get("clip_loader_override", nodes["clip_loader"])
675
- clip_type = engine_cfg.get("clip_type", "moss_image")
676
- clip_inputs = {"type": clip_type}
677
- te_filenames = engine_cfg.get("local_te_filename", [])
678
- if clip_loader_node == "DualCLIPLoader":
679
- if len(te_filenames) >= 2:
680
- if "clip_l" in te_filenames[0].lower():
681
- clip_inputs["clip_name1"] = te_filenames[0]
682
- clip_inputs["clip_name2"] = te_filenames[1]
683
- else:
684
- clip_inputs["clip_name1"] = te_filenames[1]
685
- clip_inputs["clip_name2"] = te_filenames[0]
686
- elif len(te_filenames) == 1:
687
- clip_inputs["clip_name1"] = te_filenames[0]
688
- else:
689
- if te_filenames: clip_inputs["clip_name"] = te_filenames[0]
690
- if "clip_device" in MODEL_CONFIG.get("loader_args", {}):
691
- clip_inputs["device"] = MODEL_CONFIG["loader_args"]["clip_device"]
692
- vae_filename = engine_cfg.get("local_vae_filename", "")
693
 
 
694
  workflow = {
695
  "1": {"class_type": nodes["unet_loader"], "inputs": {"unet_name": unet_name, "weight_dtype": "default"}},
696
- "2": {"class_type": clip_loader_node, "inputs": clip_inputs},
697
- "3": {"class_type": nodes["vae_loader"], "inputs": {"vae_name": vae_filename}},
698
  }
 
 
 
699
  node_idx = 4
 
 
700
  for lora_file, strength in base_lora_inputs:
701
- # 🌟 策略 B:强制使用 LoraLoaderModelOnly
702
- workflow[str(node_idx)] = {"class_type": "LoraLoaderModelOnly", "inputs": {"model": _ref(current_model_node), "lora_name": lora_file, "strength_model": strength}}
 
 
 
 
 
 
 
 
703
  current_model_node = str(node_idx)
 
704
  node_idx += 1
 
705
  if char_enable and char_lora:
706
- # 🌟 策略 B:强制使用 LoraLoaderModelOnly,避免全量权重合并带来的巨大延迟
707
- workflow[str(node_idx)] = {"class_type": "LoraLoaderModelOnly", "inputs": {"model": _ref(current_model_node), "lora_name": char_lora, "strength_model": char_strength}}
 
 
 
 
 
 
 
 
708
  current_model_node = str(node_idx)
 
709
  node_idx += 1
710
- final_prompt = prompt.strip()
711
- workflow[str(node_idx)] = {"class_type": "CLIPTextEncode", "inputs": {"clip": _ref("2"), "text": final_prompt}}
 
 
712
  workflow[str(node_idx+1)] = {"class_type": "ConditioningZeroOut", "inputs": {"conditioning": _ref(str(node_idx))}}
713
  workflow[str(node_idx+2)] = {"class_type": "EmptyLatentImage", "inputs": {"width": width, "height": height, "batch_size": 1}}
714
 
715
- # 🌟 策略 C:插入 Comfy Kitchen Attention 后端节点 (使用固定 ID "99" 避开索引冲突)
716
- workflow["99"] = {
717
- "class_type": "ModelAttentionBackend",
718
  "inputs": {
719
- "model": _ref(current_model_node),
720
- "attention": "comfy kitchen attention"
 
 
 
721
  }
722
  }
723
- # 将 KSampler 的模型引用指向刚插入的 "99" 节点
724
- current_model_node = "99"
725
-
726
- workflow[str(node_idx+3)] = {"class_type": "KSampler", "inputs": {"model": _ref(current_model_node), "positive": _ref(str(node_idx)), "negative": _ref(str(node_idx+1)), "latent_image": _ref(str(node_idx+2)), "seed": seed, "steps": steps, "cfg": cfg, "sampler_name": sampler, "scheduler": scheduler, "denoise": 1.0}}
727
  workflow[str(node_idx+4)] = {"class_type": "VAEDecode", "inputs": {"samples": _ref(str(node_idx+3)), "vae": _ref("3")}}
728
  workflow[str(node_idx+5)] = {"class_type": "SaveImage", "inputs": {"images": _ref(str(node_idx+4)), "filename_prefix": MODEL_CONFIG["name"].lower()}}
729
 
@@ -787,11 +816,63 @@ def generate(
787
  if lora_cfg["engine"] == engine:
788
  base_lora_inputs.append((lora_cfg["filename"], strength))
789
  try:
790
- workflow, unet_name = build_workflow(
791
- prompt, unet_choice, custom_w, custom_h,
792
- steps, cfg, effective_seed, sampler, scheduler,
793
- char_enable, char_lora, char_strength, lora_states
794
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
795
 
796
  print(f"[GPU 探针] 📦 模型节点构建完毕,准备调用 ComfyUI 执行引擎...", flush=True)
797
  img_path = _execute_workflow(workflow, task_id)[0]
 
258
  import comfy.utils
259
  import safetensors.torch
260
  import safetensors
 
 
261
  print("[🚀 提速补丁] [1/3] ✅ 基础模块导入成功", flush=True)
262
 
263
  _original_load_torch_file = comfy.utils.load_torch_file
 
338
  # =========================================================
339
  # 🌟 V3 Core Fix: More aggressive interception of OptimizedAttention._load_from_state_dict
340
  # =========================================================
341
+ print("[🚀 提速补丁] [3/3] 正在导入并替换 OptimizedAttention...", flush=True)
342
+
343
+ # 👇 核心调整:将这两个 import 挪到 2/3 替换完成之后,确保 1/3 和 2/3 的日志能完整输出
344
+ import comfy.ldm.modules.attention
345
+ from comfy.ldm.modules.attention import OptimizedAttention
346
+
347
  _original_load_from_state_dict = OptimizedAttention._load_from_state_dict
348
 
349
  def _patched_load_from_state_dict(self, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs):
 
650
  tqdm.auto.tqdm = original_tqdm
651
  _current_download_task_id.set(None)
652
 
653
+ def _build_clip_inputs(engine_cfg: dict, clip_loader_node: str) -> dict:
654
+ clip_inputs = {"type": engine_cfg.get("clip_type", "moss_image")}
655
+ te_filenames = engine_cfg.get("local_te_filename", [])
656
+ if clip_loader_node == "DualCLIPLoader":
657
+ if len(te_filenames) >= 2:
658
+ if "clip_l" in te_filenames[0].lower():
659
+ clip_inputs["clip_name1"] = te_filenames[0]
660
+ clip_inputs["clip_name2"] = te_filenames[1]
661
+ else:
662
+ clip_inputs["clip_name1"] = te_filenames[1]
663
+ clip_inputs["clip_name2"] = te_filenames[0]
664
+ elif len(te_filenames) == 1:
665
+ clip_inputs["clip_name1"] = te_filenames[0]
666
+ else:
667
+ if te_filenames:
668
+ clip_inputs["clip_name"] = te_filenames[0]
669
+ return clip_inputs
670
+
671
  def build_workflow(
672
  prompt: str, unet_choice: str, custom_w: int, custom_h: int,
673
  steps: int, cfg: float, seed: int, sampler: str, scheduler: str,
 
690
  if lora_cfg["engine"] == engine:
691
  base_lora_inputs.append((lora_cfg["filename"], strength))
692
 
 
693
  nodes = MODEL_CONFIG["node_types"]
694
  clip_loader_node = engine_cfg.get("clip_loader_override", nodes["clip_loader"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
695
 
696
+ # 处理 4:使用 0.1 的极简函数
697
  workflow = {
698
  "1": {"class_type": nodes["unet_loader"], "inputs": {"unet_name": unet_name, "weight_dtype": "default"}},
699
+ "2": {"class_type": clip_loader_node, "inputs": _build_clip_inputs(engine_cfg, clip_loader_node)},
700
+ "3": {"class_type": nodes["vae_loader"], "inputs": {"vae_name": engine_cfg.get("local_vae_filename", "")}},
701
  }
702
+
703
+ current_model_node = "1"
704
+ current_clip_node = "2" # 👈 新增:因为 LoraLoader 会改变 CLIP 的流向
705
  node_idx = 4
706
+
707
+ # 处理 1:改回原生 LoraLoader,接通 CLIP
708
  for lora_file, strength in base_lora_inputs:
709
+ workflow[str(node_idx)] = {
710
+ "class_type": "LoraLoader",
711
+ "inputs": {
712
+ "model": _ref(current_model_node),
713
+ "clip": _ref(current_clip_node),
714
+ "lora_name": lora_file,
715
+ "strength_model": strength,
716
+ "strength_clip": strength
717
+ }
718
+ }
719
  current_model_node = str(node_idx)
720
+ current_clip_node = str(node_idx) # 更新 CLIP 流向
721
  node_idx += 1
722
+
723
  if char_enable and char_lora:
724
+ workflow[str(node_idx)] = {
725
+ "class_type": "LoraLoader",
726
+ "inputs": {
727
+ "model": _ref(current_model_node),
728
+ "clip": _ref(current_clip_node),
729
+ "lora_name": char_lora,
730
+ "strength_model": char_strength,
731
+ "strength_clip": char_strength
732
+ }
733
+ }
734
  current_model_node = str(node_idx)
735
+ current_clip_node = str(node_idx) # 更新 CLIP 流向
736
  node_idx += 1
737
+
738
+ # 注意:这里从 _ref("2") 变成了 _ref(current_clip_node)
739
+ workflow[str(node_idx)] = {"class_type": "CLIPTextEncode", "inputs": {"clip": _ref(current_clip_node), "text": prompt.strip()}}
740
+
741
  workflow[str(node_idx+1)] = {"class_type": "ConditioningZeroOut", "inputs": {"conditioning": _ref(str(node_idx))}}
742
  workflow[str(node_idx+2)] = {"class_type": "EmptyLatentImage", "inputs": {"width": width, "height": height, "batch_size": 1}}
743
 
744
+ # 处理 2:去掉了 "99" (ModelAttentionBackend) 节点,直接连 KSampler
745
+ workflow[str(node_idx+3)] = {
746
+ "class_type": "KSampler",
747
  "inputs": {
748
+ "model": _ref(current_model_node), # 模型直连
749
+ "positive": _ref(str(node_idx)),
750
+ "negative": _ref(str(node_idx+1)),
751
+ "latent_image": _ref(str(node_idx+2)),
752
+ "seed": seed, "steps": steps, "cfg": cfg, "sampler_name": sampler, "scheduler": scheduler, "denoise": 1.0
753
  }
754
  }
755
+
 
 
 
756
  workflow[str(node_idx+4)] = {"class_type": "VAEDecode", "inputs": {"samples": _ref(str(node_idx+3)), "vae": _ref("3")}}
757
  workflow[str(node_idx+5)] = {"class_type": "SaveImage", "inputs": {"images": _ref(str(node_idx+4)), "filename_prefix": MODEL_CONFIG["name"].lower()}}
758
 
 
816
  if lora_cfg["engine"] == engine:
817
  base_lora_inputs.append((lora_cfg["filename"], strength))
818
  try:
819
+ current_model_node = "1"
820
+ nodes = MODEL_CONFIG["node_types"]
821
+ clip_loader_node = engine_cfg.get("clip_loader_override", nodes["clip_loader"])
822
+ clip_type = engine_cfg.get("clip_type", "moss_image")
823
+ clip_inputs = {"type": clip_type}
824
+ te_filenames = engine_cfg.get("local_te_filename", [])
825
+ if clip_loader_node == "DualCLIPLoader":
826
+ if len(te_filenames) >= 2:
827
+ if "clip_l" in te_filenames[0].lower():
828
+ clip_inputs["clip_name1"] = te_filenames[0]
829
+ clip_inputs["clip_name2"] = te_filenames[1]
830
+ else:
831
+ clip_inputs["clip_name1"] = te_filenames[1]
832
+ clip_inputs["clip_name2"] = te_filenames[0]
833
+ elif len(te_filenames) == 1:
834
+ clip_inputs["clip_name1"] = te_filenames[0]
835
+ else:
836
+ if te_filenames: clip_inputs["clip_name"] = te_filenames[0]
837
+ if "clip_device" in MODEL_CONFIG.get("loader_args", {}):
838
+ clip_inputs["device"] = MODEL_CONFIG["loader_args"]["clip_device"]
839
+ vae_filename = engine_cfg.get("local_vae_filename", "")
840
+
841
+ workflow = {
842
+ "1": {"class_type": nodes["unet_loader"], "inputs": {"unet_name": unet_name, "weight_dtype": "default"}},
843
+ "2": {"class_type": clip_loader_node, "inputs": clip_inputs},
844
+ "3": {"class_type": nodes["vae_loader"], "inputs": {"vae_name": vae_filename}},
845
+ }
846
+ node_idx = 4
847
+ for lora_file, strength in base_lora_inputs:
848
+ # 🌟 策略 B:强制使用 LoraLoaderModelOnly
849
+ workflow[str(node_idx)] = {"class_type": "LoraLoaderModelOnly", "inputs": {"model": _ref(current_model_node), "lora_name": lora_file, "strength_model": strength}}
850
+ current_model_node = str(node_idx)
851
+ node_idx += 1
852
+ if char_enable and char_lora:
853
+ # 🌟 策略 B:强制使用 LoraLoaderModelOnly,避免全量权重合并带来的巨大延迟
854
+ workflow[str(node_idx)] = {"class_type": "LoraLoaderModelOnly", "inputs": {"model": _ref(current_model_node), "lora_name": char_lora, "strength_model": char_strength}}
855
+ current_model_node = str(node_idx)
856
+ node_idx += 1
857
+ final_prompt = prompt.strip()
858
+ workflow[str(node_idx)] = {"class_type": "CLIPTextEncode", "inputs": {"clip": _ref("2"), "text": final_prompt}}
859
+ workflow[str(node_idx+1)] = {"class_type": "ConditioningZeroOut", "inputs": {"conditioning": _ref(str(node_idx))}}
860
+ workflow[str(node_idx+2)] = {"class_type": "EmptyLatentImage", "inputs": {"width": width, "height": height, "batch_size": 1}}
861
+
862
+ # 🌟 策略 C:插入 Comfy Kitchen Attention 后端节点 (使用固定 ID "99" 避开索引冲突)
863
+ workflow["99"] = {
864
+ "class_type": "ModelAttentionBackend",
865
+ "inputs": {
866
+ "model": _ref(current_model_node),
867
+ "attention": "comfy kitchen attention"
868
+ }
869
+ }
870
+ # 将 KSampler 的模型引用指向刚插入的 "99" 节点
871
+ current_model_node = "99"
872
+
873
+ workflow[str(node_idx+3)] = {"class_type": "KSampler", "inputs": {"model": _ref(current_model_node), "positive": _ref(str(node_idx)), "negative": _ref(str(node_idx+1)), "latent_image": _ref(str(node_idx+2)), "seed": effective_seed, "steps": steps, "cfg": cfg, "sampler_name": sampler, "scheduler": scheduler, "denoise": 1.0}}
874
+ workflow[str(node_idx+4)] = {"class_type": "VAEDecode", "inputs": {"samples": _ref(str(node_idx+3)), "vae": _ref("3")}}
875
+ workflow[str(node_idx+5)] = {"class_type": "SaveImage", "inputs": {"images": _ref(str(node_idx+4)), "filename_prefix": MODEL_CONFIG["name"].lower()}}
876
 
877
  print(f"[GPU 探针] 📦 模型节点构建完毕,准备调用 ComfyUI 执行引擎...", flush=True)
878
  img_path = _execute_workflow(workflow, task_id)[0]