DZRobo
		
	commited on
		
		
					Commit 
							
							·
						
						bb62070
	
1
								Parent(s):
							
							3d335ed
								
Optimize memory usage
Browse filesImproves memory management by releasing temporary tensors throughout the CADE2.5 easy and hard modules, reducing RAM/VRAM peaks. Adds optional auto-save functionality for final images with configurable compression. Updates README with memory tips and clarifies workflow steps. Adjusts presets for sharpen, guidance, NAG, and mid-frequency stabilizer parameters to further optimize performance and output quality.
- README.md +8 -3
- mod/easy/mg_cade25_easy.py +225 -8
- mod/hard/mg_cade25.py +208 -10
- pressets/mg_cade25.cfg +52 -36
- pressets/mg_controlfusion.cfg +5 -5
    	
        README.md
    CHANGED
    
    | @@ -66,7 +66,12 @@ Photo Dog | |
| 66 | 
             
            - Notes
         | 
| 67 | 
             
              - Lowering the starting latent (e.g., 512x768) or lower, reduces both VRAM and RAM.
         | 
| 68 | 
             
              - Disabling hi-res depth/edges (ControlFusion) reduces peaks. (not recommended!)
         | 
| 69 | 
            -
              - Depth weights add a bit of RAM on load; models live under `depth-anything/`. | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 70 |  | 
| 71 |  | 
| 72 | 
             
            ## Install (ComfyUI 0.3.60, tested on this version)
         | 
| @@ -89,7 +94,7 @@ Folder `workflows/` contains ready-to-use graphs: | |
| 89 | 
             
            You can save this workflow to ComfyUI `ComfyUI\user\default\workflows`
         | 
| 90 | 
             
            6. Restart ComfyUI. Nodes appear under the "MagicNodes" categories.
         | 
| 91 |  | 
| 92 | 
            -
             | 
| 93 |  | 
| 94 |  | 
| 95 | 
             
            ## 🚀 "One-Node" Quickstart (MG_SuperSimple)
         | 
| @@ -104,6 +109,7 @@ Notes: | |
| 104 | 
             
            - When "Custom" is off, presets fully drive parameters
         | 
| 105 | 
             
            - When "Custom" is on, the visible CADE controls override the Step presets across all steps; Step 1 still enforces `denoise=1.0`
         | 
| 106 | 
             
            - CLIP Vision (if connected) is applied from Step 2 onward; if no reference image is provided, SuperSimple uses the previous step image as reference
         | 
|  | |
| 107 |  | 
| 108 | 
             
            ## ❗Tips
         | 
| 109 | 
             
            (!) There are almost always artifacts in the first step, don't pay attention to them, they will be removed in the next steps. Keep your prompt clean and logical, don't duplicate details and be careful with symbols.
         | 
| @@ -141,7 +147,6 @@ Notes: | |
| 141 | 
             
            15) The 4th step sometimes saves the image for a long time, just wait for the end of the process, it depends on the initial resolution you set.
         | 
| 142 |  | 
| 143 |  | 
| 144 | 
            -
             | 
| 145 | 
             
            ## Repository Layout
         | 
| 146 | 
             
            ```
         | 
| 147 | 
             
            MagicNodes/
         | 
|  | |
| 66 | 
             
            - Notes
         | 
| 67 | 
             
              - Lowering the starting latent (e.g., 512x768) or lower, reduces both VRAM and RAM.
         | 
| 68 | 
             
              - Disabling hi-res depth/edges (ControlFusion) reduces peaks. (not recommended!)
         | 
| 69 | 
            +
              - Depth weights add a bit of RAM on load; models live under `depth-anything/`.
         | 
| 70 | 
            +
             | 
| 71 | 
            +
            ## 💥 Memory and ComfyUI
         | 
| 72 | 
            +
            - !!!!!!!During VAE tiling at ultra-high resolutions, more than those that fit into the memory of your video card, some of the processes stick in RAM. These are the nuance of ComfyUI, just restart ComfyUI. For my part, I clear everything that can be cleared from RAM and VRAM.!!!!!! 
         | 
| 73 | 
            +
            - At each step, the image is upscaled from the previous step! Keep this in mind, the final image may not fit into your PC's memory if the starting latency is high.
         | 
| 74 | 
            +
             
         | 
| 75 |  | 
| 76 |  | 
| 77 | 
             
            ## Install (ComfyUI 0.3.60, tested on this version)
         | 
|  | |
| 94 | 
             
            You can save this workflow to ComfyUI `ComfyUI\user\default\workflows`
         | 
| 95 | 
             
            6. Restart ComfyUI. Nodes appear under the "MagicNodes" categories.
         | 
| 96 |  | 
| 97 | 
            +
            I strongly recommend use `mg_Easy-Workflow` workflow + default settings + your model and my negative LoRA `mg_7lambda_negative.safetensors`, for best result.
         | 
| 98 |  | 
| 99 |  | 
| 100 | 
             
            ## 🚀 "One-Node" Quickstart (MG_SuperSimple)
         | 
|  | |
| 109 | 
             
            - When "Custom" is off, presets fully drive parameters
         | 
| 110 | 
             
            - When "Custom" is on, the visible CADE controls override the Step presets across all steps; Step 1 still enforces `denoise=1.0`
         | 
| 111 | 
             
            - CLIP Vision (if connected) is applied from Step 2 onward; if no reference image is provided, SuperSimple uses the previous step image as reference
         | 
| 112 | 
            +
            - Step 1 and Step 2 it's a prewarming step.
         | 
| 113 |  | 
| 114 | 
             
            ## ❗Tips
         | 
| 115 | 
             
            (!) There are almost always artifacts in the first step, don't pay attention to them, they will be removed in the next steps. Keep your prompt clean and logical, don't duplicate details and be careful with symbols.
         | 
|  | |
| 147 | 
             
            15) The 4th step sometimes saves the image for a long time, just wait for the end of the process, it depends on the initial resolution you set.
         | 
| 148 |  | 
| 149 |  | 
|  | |
| 150 | 
             
            ## Repository Layout
         | 
| 151 | 
             
            ```
         | 
| 152 | 
             
            MagicNodes/
         | 
    	
        mod/easy/mg_cade25_easy.py
    CHANGED
    
    | @@ -295,7 +295,41 @@ def _clipseg_build_mask(image_bhwc: torch.Tensor, | |
| 295 | 
             
                        except Exception:
         | 
| 296 | 
             
                            pass
         | 
| 297 | 
             
                    m = (m * float(max(0.0, gain))).clamp(0, 1)
         | 
| 298 | 
            -
                     | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 299 | 
             
                except Exception as e:
         | 
| 300 | 
             
                    if not globals().get("_CLIPSEG_WARNED", False):
         | 
| 301 | 
             
                        print(f"[CADE2.5][CLIPSeg] mask failed: {e}")
         | 
| @@ -1052,16 +1086,27 @@ def safe_decode(vae, lat, tile=512, ovlp=64): | |
| 1052 | 
             
                        out = out.detach()
         | 
| 1053 | 
             
                    except Exception:
         | 
| 1054 | 
             
                        pass
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 1055 | 
             
                    try:
         | 
| 1056 | 
            -
                         | 
| 1057 | 
             
                    except Exception:
         | 
| 1058 | 
             
                        pass
         | 
| 1059 | 
             
                    if torch.cuda.is_available():
         | 
| 1060 | 
            -
                         | 
| 1061 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 1062 | 
             
                except Exception:
         | 
| 1063 | 
            -
                     | 
| 1064 | 
            -
                return out
         | 
| 1065 |  | 
| 1066 |  | 
| 1067 | 
             
            def safe_encode(vae, img, tile=512, ovlp=64):
         | 
| @@ -1953,6 +1998,10 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 1953 | 
             
                            "clipseg_blend": (["fuse", "replace", "intersect"], {"default": "fuse", "tooltip": "How to combine CLIPSeg with ONNX mask."}),
         | 
| 1954 | 
             
                            "clipseg_ref_gate": ("BOOLEAN", {"default": False, "tooltip": "If reference provided, boost mask when far from reference (CLIP-Vision)."}),
         | 
| 1955 | 
             
                            "clipseg_ref_threshold": ("FLOAT", {"default": 0.03, "min": 0.0, "max": 0.2, "step": 0.001}),
         | 
|  | |
|  | |
|  | |
|  | |
| 1956 |  | 
| 1957 | 
             
                            # Polish mode (final hi-res refinement)
         | 
| 1958 | 
             
                            "polish_enable": ("BOOLEAN", {"default": False, "tooltip": "Polish: keep low-frequency shape from reference while allowing high-frequency details to refine."}),
         | 
| @@ -1993,6 +2042,7 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 1993 | 
             
                                 clipseg_gain=1.0, clipseg_blend="fuse", clipseg_ref_gate=False, clipseg_ref_threshold=0.03,
         | 
| 1994 | 
             
                                polish_enable=False, polish_keep_low=0.4, polish_edge_lock=0.2, polish_sigma=1.0,
         | 
| 1995 | 
             
                               polish_start_after=1, polish_keep_low_ramp=0.2,
         | 
|  | |
| 1996 | 
             
                                 preset_step="Step 1", custom_override=False):
         | 
| 1997 | 
             
                    # Cooperative cancel before any heavy work
         | 
| 1998 | 
             
                    model_management.throw_exception_if_processing_interrupted()
         | 
| @@ -2063,7 +2113,7 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 2063 | 
             
                    aq_tile = int(pv("aq_tile", 32))
         | 
| 2064 | 
             
                    aq_stride = int(pv("aq_stride", 16))
         | 
| 2065 | 
             
                    aq_alpha = float(pv("aq_alpha", 2.0))
         | 
| 2066 | 
            -
                    aq_ema_beta = float(pv("aq_ema_beta", 0. | 
| 2067 | 
             
                    midfreq_enable = bool(pv("midfreq_enable", False))
         | 
| 2068 | 
             
                    midfreq_gain = float(pv("midfreq_gain", 0.0))
         | 
| 2069 | 
             
                    midfreq_sigma_lo = float(pv("midfreq_sigma_lo", 0.8))
         | 
| @@ -2258,6 +2308,18 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 2258 | 
             
                                            CURRENT_ONNX_MASK_BCHW = None
         | 
| 2259 | 
             
                                    except Exception:
         | 
| 2260 | 
             
                                        CURRENT_ONNX_MASK_BCHW = None
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 2261 | 
             
                                # One-time damping from area (disabled by default)
         | 
| 2262 | 
             
                                if False:
         | 
| 2263 | 
             
                                    try:
         | 
| @@ -2340,7 +2402,11 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 2340 | 
             
                                        device='cpu',
         | 
| 2341 | 
             
                                        generator=gen,
         | 
| 2342 | 
             
                                    ).to(current_latent["samples"].device)
         | 
| 2343 | 
            -
                                    current_latent["samples"]  | 
|  | |
|  | |
|  | |
|  | |
| 2344 |  | 
| 2345 | 
             
                                # Pre-sampling ONNX detectors: handled once below (kept compact)
         | 
| 2346 |  | 
| @@ -2433,6 +2499,20 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 2433 | 
             
                                                    CURRENT_ONNX_MASK_BCHW = None
         | 
| 2434 | 
             
                                except Exception:
         | 
| 2435 | 
             
                                    pass
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 2436 |  | 
| 2437 | 
             
                                # Sampler model prepared once above; reused across iterations (no-op here)
         | 
| 2438 | 
             
                                sampler_model = sampler_model
         | 
| @@ -2525,6 +2605,26 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 2525 | 
             
                                                current_latent = lat_b
         | 
| 2526 | 
             
                                            else:
         | 
| 2527 | 
             
                                                current_latent = lat_a
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 2528 | 
             
                                except Exception:
         | 
| 2529 | 
             
                                    pass
         | 
| 2530 |  | 
| @@ -2578,6 +2678,31 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 2578 |  | 
| 2579 | 
             
                                # cooperative cancel immediately after sampling
         | 
| 2580 | 
             
                                model_management.throw_exception_if_processing_interrupted()
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 2581 |  | 
| 2582 | 
             
                                if bool(latent_compare):
         | 
| 2583 | 
             
                                    _cur = current_latent["samples"]
         | 
| @@ -2598,6 +2723,10 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 2598 | 
             
                                        current_denoise = max(0.20, current_denoise * damp)
         | 
| 2599 | 
             
                                        cfg_damp = 0.997 if damp > 0.9 else 0.99
         | 
| 2600 | 
             
                                        current_cfg = max(1.0, current_cfg * cfg_damp)
         | 
|  | |
|  | |
|  | |
|  | |
| 2601 |  | 
| 2602 | 
             
                                # AQClip-Lite: adaptive soft clipping in latent space (before decode)
         | 
| 2603 | 
             
                                try:
         | 
| @@ -2619,6 +2748,14 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 2619 | 
             
                                            H_override=H_override,
         | 
| 2620 | 
             
                                        )
         | 
| 2621 | 
             
                                        current_latent["samples"] = z_new
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 2622 | 
             
                                except Exception:
         | 
| 2623 | 
             
                                    pass
         | 
| 2624 |  | 
| @@ -2705,6 +2842,33 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 2705 | 
             
                                        # Feed back to latent for next steps
         | 
| 2706 | 
             
                                        current_latent = {"samples": safe_encode(vae, img2)}
         | 
| 2707 | 
             
                                        image = img2
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 2708 | 
             
                                    except Exception:
         | 
| 2709 | 
             
                                        pass
         | 
| 2710 |  | 
| @@ -2850,6 +3014,31 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 2850 | 
             
                    except Exception:
         | 
| 2851 | 
             
                        pass
         | 
| 2852 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 2853 | 
             
                    return current_latent, image, int(current_steps), float(current_cfg), float(current_denoise), onnx_mask_img
         | 
| 2854 |  | 
| 2855 |  | 
| @@ -3107,6 +3296,34 @@ def _smart_seed_select(model, | |
| 3107 | 
             
                            if score > best_score:
         | 
| 3108 | 
             
                                best_score = score
         | 
| 3109 | 
             
                                best_seed = sd
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 3110 | 
             
                        except Exception as e:
         | 
| 3111 | 
             
                            # do not swallow user interruption; also honour sentinel
         | 
| 3112 | 
             
                            if isinstance(e, model_management.InterruptProcessingException) or globals().get("_MG_CANCEL_REQUESTED", False):
         | 
|  | |
| 295 | 
             
                        except Exception:
         | 
| 296 | 
             
                            pass
         | 
| 297 | 
             
                    m = (m * float(max(0.0, gain))).clamp(0, 1)
         | 
| 298 | 
            +
                    out_mask = m.unsqueeze(0).unsqueeze(-1)  # BHWC with B=1,C=1
         | 
| 299 | 
            +
                    # Best-effort release of temporaries to reduce RAM peak
         | 
| 300 | 
            +
                    try:
         | 
| 301 | 
            +
                        del inputs
         | 
| 302 | 
            +
                    except Exception:
         | 
| 303 | 
            +
                        pass
         | 
| 304 | 
            +
                    try:
         | 
| 305 | 
            +
                        del outputs
         | 
| 306 | 
            +
                    except Exception:
         | 
| 307 | 
            +
                        pass
         | 
| 308 | 
            +
                    try:
         | 
| 309 | 
            +
                        del logits
         | 
| 310 | 
            +
                    except Exception:
         | 
| 311 | 
            +
                        pass
         | 
| 312 | 
            +
                    try:
         | 
| 313 | 
            +
                        del prob
         | 
| 314 | 
            +
                    except Exception:
         | 
| 315 | 
            +
                        pass
         | 
| 316 | 
            +
                    try:
         | 
| 317 | 
            +
                        del pil_img
         | 
| 318 | 
            +
                    except Exception:
         | 
| 319 | 
            +
                        pass
         | 
| 320 | 
            +
                    try:
         | 
| 321 | 
            +
                        del arr
         | 
| 322 | 
            +
                    except Exception:
         | 
| 323 | 
            +
                        pass
         | 
| 324 | 
            +
                    try:
         | 
| 325 | 
            +
                        del x
         | 
| 326 | 
            +
                    except Exception:
         | 
| 327 | 
            +
                        pass
         | 
| 328 | 
            +
                    try:
         | 
| 329 | 
            +
                        del img
         | 
| 330 | 
            +
                    except Exception:
         | 
| 331 | 
            +
                        pass
         | 
| 332 | 
            +
                    return out_mask
         | 
| 333 | 
             
                except Exception as e:
         | 
| 334 | 
             
                    if not globals().get("_CLIPSEG_WARNED", False):
         | 
| 335 | 
             
                        print(f"[CADE2.5][CLIPSeg] mask failed: {e}")
         | 
|  | |
| 1086 | 
             
                        out = out.detach()
         | 
| 1087 | 
             
                    except Exception:
         | 
| 1088 | 
             
                        pass
         | 
| 1089 | 
            +
                    out_cpu = out
         | 
| 1090 | 
            +
                    try:
         | 
| 1091 | 
            +
                        out_cpu = out_cpu.to('cpu')
         | 
| 1092 | 
            +
                    except Exception:
         | 
| 1093 | 
            +
                        pass
         | 
| 1094 | 
             
                    try:
         | 
| 1095 | 
            +
                        del out
         | 
| 1096 | 
             
                    except Exception:
         | 
| 1097 | 
             
                        pass
         | 
| 1098 | 
             
                    if torch.cuda.is_available():
         | 
| 1099 | 
            +
                        try:
         | 
| 1100 | 
            +
                            torch.cuda.synchronize()
         | 
| 1101 | 
            +
                        except Exception:
         | 
| 1102 | 
            +
                            pass
         | 
| 1103 | 
            +
                        try:
         | 
| 1104 | 
            +
                            torch.cuda.empty_cache()
         | 
| 1105 | 
            +
                        except Exception:
         | 
| 1106 | 
            +
                            pass
         | 
| 1107 | 
            +
                    return out_cpu
         | 
| 1108 | 
             
                except Exception:
         | 
| 1109 | 
            +
                    return out
         | 
|  | |
| 1110 |  | 
| 1111 |  | 
| 1112 | 
             
            def safe_encode(vae, img, tile=512, ovlp=64):
         | 
|  | |
| 1998 | 
             
                            "clipseg_blend": (["fuse", "replace", "intersect"], {"default": "fuse", "tooltip": "How to combine CLIPSeg with ONNX mask."}),
         | 
| 1999 | 
             
                            "clipseg_ref_gate": ("BOOLEAN", {"default": False, "tooltip": "If reference provided, boost mask when far from reference (CLIP-Vision)."}),
         | 
| 2000 | 
             
                            "clipseg_ref_threshold": ("FLOAT", {"default": 0.03, "min": 0.0, "max": 0.2, "step": 0.001}),
         | 
| 2001 | 
            +
                            # Under-the-hood saving (disabled by default to avoid duplicate saves)
         | 
| 2002 | 
            +
                            "auto_save": ("BOOLEAN", {"default": False, "tooltip": "Save final IMAGE directly from CADE (uses low PNG compress to reduce RAM)."}),
         | 
| 2003 | 
            +
                            "save_prefix": ("STRING", {"default": "ComfyUI", "multiline": False}),
         | 
| 2004 | 
            +
                            "save_compress": ("INT", {"default": 1, "min": 0, "max": 9, "step": 1}),
         | 
| 2005 |  | 
| 2006 | 
             
                            # Polish mode (final hi-res refinement)
         | 
| 2007 | 
             
                            "polish_enable": ("BOOLEAN", {"default": False, "tooltip": "Polish: keep low-frequency shape from reference while allowing high-frequency details to refine."}),
         | 
|  | |
| 2042 | 
             
                                 clipseg_gain=1.0, clipseg_blend="fuse", clipseg_ref_gate=False, clipseg_ref_threshold=0.03,
         | 
| 2043 | 
             
                                polish_enable=False, polish_keep_low=0.4, polish_edge_lock=0.2, polish_sigma=1.0,
         | 
| 2044 | 
             
                               polish_start_after=1, polish_keep_low_ramp=0.2,
         | 
| 2045 | 
            +
                                auto_save=False, save_prefix="ComfyUI", save_compress=1,
         | 
| 2046 | 
             
                                 preset_step="Step 1", custom_override=False):
         | 
| 2047 | 
             
                    # Cooperative cancel before any heavy work
         | 
| 2048 | 
             
                    model_management.throw_exception_if_processing_interrupted()
         | 
|  | |
| 2113 | 
             
                    aq_tile = int(pv("aq_tile", 32))
         | 
| 2114 | 
             
                    aq_stride = int(pv("aq_stride", 16))
         | 
| 2115 | 
             
                    aq_alpha = float(pv("aq_alpha", 2.0))
         | 
| 2116 | 
            +
                    aq_ema_beta = float(pv("aq_ema_beta", 0.85))
         | 
| 2117 | 
             
                    midfreq_enable = bool(pv("midfreq_enable", False))
         | 
| 2118 | 
             
                    midfreq_gain = float(pv("midfreq_gain", 0.0))
         | 
| 2119 | 
             
                    midfreq_sigma_lo = float(pv("midfreq_sigma_lo", 0.8))
         | 
|  | |
| 2308 | 
             
                                            CURRENT_ONNX_MASK_BCHW = None
         | 
| 2309 | 
             
                                    except Exception:
         | 
| 2310 | 
             
                                        CURRENT_ONNX_MASK_BCHW = None
         | 
| 2311 | 
            +
                                    try:
         | 
| 2312 | 
            +
                                        del onnx_mask
         | 
| 2313 | 
            +
                                    except Exception:
         | 
| 2314 | 
            +
                                        pass
         | 
| 2315 | 
            +
                                    try:
         | 
| 2316 | 
            +
                                        del om
         | 
| 2317 | 
            +
                                    except Exception:
         | 
| 2318 | 
            +
                                        pass
         | 
| 2319 | 
            +
                                    try:
         | 
| 2320 | 
            +
                                        del img_preview
         | 
| 2321 | 
            +
                                    except Exception:
         | 
| 2322 | 
            +
                                        pass
         | 
| 2323 | 
             
                                # One-time damping from area (disabled by default)
         | 
| 2324 | 
             
                                if False:
         | 
| 2325 | 
             
                                    try:
         | 
|  | |
| 2402 | 
             
                                        device='cpu',
         | 
| 2403 | 
             
                                        generator=gen,
         | 
| 2404 | 
             
                                    ).to(current_latent["samples"].device)
         | 
| 2405 | 
            +
                                    current_latent["samples"] = current_latent["samples"] + (noise_offset * fade) * eps
         | 
| 2406 | 
            +
                                    try:
         | 
| 2407 | 
            +
                                        del eps
         | 
| 2408 | 
            +
                                    except Exception:
         | 
| 2409 | 
            +
                                        pass
         | 
| 2410 |  | 
| 2411 | 
             
                                # Pre-sampling ONNX detectors: handled once below (kept compact)
         | 
| 2412 |  | 
|  | |
| 2499 | 
             
                                                    CURRENT_ONNX_MASK_BCHW = None
         | 
| 2500 | 
             
                                except Exception:
         | 
| 2501 | 
             
                                    pass
         | 
| 2502 | 
            +
                                try:
         | 
| 2503 | 
            +
                                    del img_prev2
         | 
| 2504 | 
            +
                                except Exception:
         | 
| 2505 | 
            +
                                    pass
         | 
| 2506 | 
            +
                                try:
         | 
| 2507 | 
            +
                                    del em2
         | 
| 2508 | 
            +
                                except Exception:
         | 
| 2509 | 
            +
                                    pass
         | 
| 2510 | 
            +
                                try:
         | 
| 2511 | 
            +
                                    del cmask
         | 
| 2512 | 
            +
                                    del fused
         | 
| 2513 | 
            +
                                    del om
         | 
| 2514 | 
            +
                                except Exception:
         | 
| 2515 | 
            +
                                    pass
         | 
| 2516 |  | 
| 2517 | 
             
                                # Sampler model prepared once above; reused across iterations (no-op here)
         | 
| 2518 | 
             
                                sampler_model = sampler_model
         | 
|  | |
| 2605 | 
             
                                                current_latent = lat_b
         | 
| 2606 | 
             
                                            else:
         | 
| 2607 | 
             
                                                current_latent = lat_a
         | 
| 2608 | 
            +
                                            try:
         | 
| 2609 | 
            +
                                                del img_roi
         | 
| 2610 | 
            +
                                            except Exception:
         | 
| 2611 | 
            +
                                                pass
         | 
| 2612 | 
            +
                                            try:
         | 
| 2613 | 
            +
                                                del roi
         | 
| 2614 | 
            +
                                            except Exception:
         | 
| 2615 | 
            +
                                                pass
         | 
| 2616 | 
            +
                                            try:
         | 
| 2617 | 
            +
                                                del lat_in_a
         | 
| 2618 | 
            +
                                                del lat_a
         | 
| 2619 | 
            +
                                                del img_a
         | 
| 2620 | 
            +
                                            except Exception:
         | 
| 2621 | 
            +
                                                pass
         | 
| 2622 | 
            +
                                            try:
         | 
| 2623 | 
            +
                                                del lat_in_b
         | 
| 2624 | 
            +
                                                del lat_b
         | 
| 2625 | 
            +
                                                del img_b
         | 
| 2626 | 
            +
                                            except Exception:
         | 
| 2627 | 
            +
                                                pass
         | 
| 2628 | 
             
                                except Exception:
         | 
| 2629 | 
             
                                    pass
         | 
| 2630 |  | 
|  | |
| 2678 |  | 
| 2679 | 
             
                                # cooperative cancel immediately after sampling
         | 
| 2680 | 
             
                                model_management.throw_exception_if_processing_interrupted()
         | 
| 2681 | 
            +
                                # Release heavy temporaries from sampler path
         | 
| 2682 | 
            +
                                try:
         | 
| 2683 | 
            +
                                    del lat_img
         | 
| 2684 | 
            +
                                except Exception:
         | 
| 2685 | 
            +
                                    pass
         | 
| 2686 | 
            +
                                try:
         | 
| 2687 | 
            +
                                    del noise
         | 
| 2688 | 
            +
                                except Exception:
         | 
| 2689 | 
            +
                                    pass
         | 
| 2690 | 
            +
                                try:
         | 
| 2691 | 
            +
                                    del noise_mask
         | 
| 2692 | 
            +
                                except Exception:
         | 
| 2693 | 
            +
                                    pass
         | 
| 2694 | 
            +
                                try:
         | 
| 2695 | 
            +
                                    del callback
         | 
| 2696 | 
            +
                                except Exception:
         | 
| 2697 | 
            +
                                    pass
         | 
| 2698 | 
            +
                                try:
         | 
| 2699 | 
            +
                                    del sampler_obj
         | 
| 2700 | 
            +
                                except Exception:
         | 
| 2701 | 
            +
                                    pass
         | 
| 2702 | 
            +
                                try:
         | 
| 2703 | 
            +
                                    del sigmas
         | 
| 2704 | 
            +
                                except Exception:
         | 
| 2705 | 
            +
                                    pass
         | 
| 2706 |  | 
| 2707 | 
             
                                if bool(latent_compare):
         | 
| 2708 | 
             
                                    _cur = current_latent["samples"]
         | 
|  | |
| 2723 | 
             
                                        current_denoise = max(0.20, current_denoise * damp)
         | 
| 2724 | 
             
                                        cfg_damp = 0.997 if damp > 0.9 else 0.99
         | 
| 2725 | 
             
                                        current_cfg = max(1.0, current_cfg * cfg_damp)
         | 
| 2726 | 
            +
                                try:
         | 
| 2727 | 
            +
                                    del prev_samples
         | 
| 2728 | 
            +
                                except Exception:
         | 
| 2729 | 
            +
                                    pass
         | 
| 2730 |  | 
| 2731 | 
             
                                # AQClip-Lite: adaptive soft clipping in latent space (before decode)
         | 
| 2732 | 
             
                                try:
         | 
|  | |
| 2748 | 
             
                                            H_override=H_override,
         | 
| 2749 | 
             
                                        )
         | 
| 2750 | 
             
                                        current_latent["samples"] = z_new
         | 
| 2751 | 
            +
                                        try:
         | 
| 2752 | 
            +
                                            del H_override
         | 
| 2753 | 
            +
                                        except Exception:
         | 
| 2754 | 
            +
                                            pass
         | 
| 2755 | 
            +
                                        try:
         | 
| 2756 | 
            +
                                            del Hm
         | 
| 2757 | 
            +
                                        except Exception:
         | 
| 2758 | 
            +
                                            pass
         | 
| 2759 | 
             
                                except Exception:
         | 
| 2760 | 
             
                                    pass
         | 
| 2761 |  | 
|  | |
| 2842 | 
             
                                        # Feed back to latent for next steps
         | 
| 2843 | 
             
                                        current_latent = {"samples": safe_encode(vae, img2)}
         | 
| 2844 | 
             
                                        image = img2
         | 
| 2845 | 
            +
                                        try:
         | 
| 2846 | 
            +
                                            del x
         | 
| 2847 | 
            +
                                            del r
         | 
| 2848 | 
            +
                                            del low_x
         | 
| 2849 | 
            +
                                            del low_r
         | 
| 2850 | 
            +
                                            del high_x
         | 
| 2851 | 
            +
                                            del low_mix
         | 
| 2852 | 
            +
                                            del new
         | 
| 2853 | 
            +
                                            del micro
         | 
| 2854 | 
            +
                                            del gray
         | 
| 2855 | 
            +
                                            del sobel_x
         | 
| 2856 | 
            +
                                            del sobel_y
         | 
| 2857 | 
            +
                                            del gx
         | 
| 2858 | 
            +
                                            del gy
         | 
| 2859 | 
            +
                                            del mag
         | 
| 2860 | 
            +
                                            del m_edge
         | 
| 2861 | 
            +
                                            del g_depth
         | 
| 2862 | 
            +
                                            del g
         | 
| 2863 | 
            +
                                            del ref_n
         | 
| 2864 | 
            +
                                            del ref
         | 
| 2865 | 
            +
                                            del img
         | 
| 2866 | 
            +
                                        except Exception:
         | 
| 2867 | 
            +
                                            pass
         | 
| 2868 | 
            +
                                        try:
         | 
| 2869 | 
            +
                                            clear_gpu_and_ram_cache()
         | 
| 2870 | 
            +
                                        except Exception:
         | 
| 2871 | 
            +
                                            pass
         | 
| 2872 | 
             
                                    except Exception:
         | 
| 2873 | 
             
                                        pass
         | 
| 2874 |  | 
|  | |
| 3014 | 
             
                    except Exception:
         | 
| 3015 | 
             
                        pass
         | 
| 3016 |  | 
| 3017 | 
            +
                    # Under-the-hood preview downscale for UI/output IMAGE to cap RAM during save/preview
         | 
| 3018 | 
            +
                    try:
         | 
| 3019 | 
            +
                        B, H, W, C = image.shape
         | 
| 3020 | 
            +
                        max_side = max(int(H), int(W))
         | 
| 3021 | 
            +
                        cap = 4096
         | 
| 3022 | 
            +
                        if max_side > cap:
         | 
| 3023 | 
            +
                            scale = float(cap) / float(max_side)
         | 
| 3024 | 
            +
                            nh = max(1, int(round(H * scale)))
         | 
| 3025 | 
            +
                            nw = max(1, int(round(W * scale)))
         | 
| 3026 | 
            +
                            x = image.movedim(-1, 1)
         | 
| 3027 | 
            +
                            x = F.interpolate(x, size=(nh, nw), mode='bilinear', align_corners=False)
         | 
| 3028 | 
            +
                            image = x.movedim(1, -1).clamp(0, 1).to(dtype=image.dtype)
         | 
| 3029 | 
            +
                    except Exception:
         | 
| 3030 | 
            +
                        pass
         | 
| 3031 | 
            +
             | 
| 3032 | 
            +
                    # Optional: save from node with low PNG compress to reduce RAM spike; ignore UI wiring
         | 
| 3033 | 
            +
                    try:
         | 
| 3034 | 
            +
                        if bool(auto_save):
         | 
| 3035 | 
            +
                            from comfy_api.latest._ui import ImageSaveHelper, FolderType
         | 
| 3036 | 
            +
                            _ = ImageSaveHelper.save_images(
         | 
| 3037 | 
            +
                                [image], filename_prefix=str(save_prefix), folder_type=FolderType.output,
         | 
| 3038 | 
            +
                                cls=CADEEasyUI, compress_level=int(save_compress))
         | 
| 3039 | 
            +
                    except Exception:
         | 
| 3040 | 
            +
                        pass
         | 
| 3041 | 
            +
             | 
| 3042 | 
             
                    return current_latent, image, int(current_steps), float(current_cfg), float(current_denoise), onnx_mask_img
         | 
| 3043 |  | 
| 3044 |  | 
|  | |
| 3296 | 
             
                            if score > best_score:
         | 
| 3297 | 
             
                                best_score = score
         | 
| 3298 | 
             
                                best_seed = sd
         | 
| 3299 | 
            +
                            try:
         | 
| 3300 | 
            +
                                del img
         | 
| 3301 | 
            +
                            except Exception:
         | 
| 3302 | 
            +
                                pass
         | 
| 3303 | 
            +
                            try:
         | 
| 3304 | 
            +
                                del lat_out
         | 
| 3305 | 
            +
                            except Exception:
         | 
| 3306 | 
            +
                                pass
         | 
| 3307 | 
            +
                            try:
         | 
| 3308 | 
            +
                                del lat_in
         | 
| 3309 | 
            +
                            except Exception:
         | 
| 3310 | 
            +
                                pass
         | 
| 3311 | 
            +
                            try:
         | 
| 3312 | 
            +
                                del lch_small
         | 
| 3313 | 
            +
                            except Exception:
         | 
| 3314 | 
            +
                                pass
         | 
| 3315 | 
            +
                            try:
         | 
| 3316 | 
            +
                                del lap
         | 
| 3317 | 
            +
                            except Exception:
         | 
| 3318 | 
            +
                                pass
         | 
| 3319 | 
            +
                            try:
         | 
| 3320 | 
            +
                                del cand_embed
         | 
| 3321 | 
            +
                            except Exception:
         | 
| 3322 | 
            +
                                pass
         | 
| 3323 | 
            +
                            try:
         | 
| 3324 | 
            +
                                del cmask
         | 
| 3325 | 
            +
                            except Exception:
         | 
| 3326 | 
            +
                                pass
         | 
| 3327 | 
             
                        except Exception as e:
         | 
| 3328 | 
             
                            # do not swallow user interruption; also honour sentinel
         | 
| 3329 | 
             
                            if isinstance(e, model_management.InterruptProcessingException) or globals().get("_MG_CANCEL_REQUESTED", False):
         | 
    	
        mod/hard/mg_cade25.py
    CHANGED
    
    | @@ -156,7 +156,41 @@ def _clipseg_build_mask(image_bhwc: torch.Tensor, | |
| 156 | 
             
                        except Exception:
         | 
| 157 | 
             
                            pass
         | 
| 158 | 
             
                    m = (m * float(max(0.0, gain))).clamp(0, 1)
         | 
| 159 | 
            -
                     | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 160 | 
             
                except Exception as e:
         | 
| 161 | 
             
                    if not globals().get("_CLIPSEG_WARNED", False):
         | 
| 162 | 
             
                        print(f"[CADE2.5][CLIPSeg] mask failed: {e}")
         | 
| @@ -723,12 +757,42 @@ def _scheduler_names(): | |
| 723 |  | 
| 724 |  | 
| 725 | 
             
            def safe_decode(vae, lat, tile=512, ovlp=64):
         | 
| 726 | 
            -
                 | 
| 727 | 
            -
                 | 
| 728 | 
            -
                     | 
| 729 | 
            -
                     | 
| 730 | 
            -
             | 
| 731 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 732 |  | 
| 733 |  | 
| 734 | 
             
            def safe_encode(vae, img, tile=512, ovlp=64):
         | 
| @@ -1456,6 +1520,10 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 1456 | 
             
                            "clipseg_blend": (["fuse", "replace", "intersect"], {"default": "fuse", "tooltip": "How to combine CLIPSeg with any pre-mask (if present)."}),
         | 
| 1457 | 
             
                            "clipseg_ref_gate": ("BOOLEAN", {"default": False, "tooltip": "If reference provided, boost mask when far from reference (CLIP-Vision)."}),
         | 
| 1458 | 
             
                            "clipseg_ref_threshold": ("FLOAT", {"default": 0.03, "min": 0.0, "max": 0.2, "step": 0.001}),
         | 
|  | |
|  | |
|  | |
|  | |
| 1459 |  | 
| 1460 | 
             
                            # Polish mode (final hi-res refinement)
         | 
| 1461 | 
             
                            "polish_enable": ("BOOLEAN", {"default": False, "tooltip": "Polish: keep low-frequency shape from reference while allowing high-frequency details to refine."}),
         | 
| @@ -1493,8 +1561,9 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 1493 | 
             
                                 clipseg_threshold=0.40, clipseg_blur=7.0, clipseg_dilate=4,
         | 
| 1494 | 
             
                                 clipseg_gain=1.0, clipseg_blend="fuse", clipseg_ref_gate=False, clipseg_ref_threshold=0.03,
         | 
| 1495 | 
             
                                 polish_enable=False, polish_keep_low=0.4, polish_edge_lock=0.2, polish_sigma=1.0,
         | 
| 1496 | 
            -
             | 
| 1497 | 
            -
             | 
|  | |
| 1498 | 
             
                    # Hard reset of any sticky globals from prior runs
         | 
| 1499 | 
             
                    try:
         | 
| 1500 | 
             
                        global CURRENT_ONNX_MASK_BCHW
         | 
| @@ -1615,6 +1684,23 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 1615 | 
             
                            clipseg_enable = False
         | 
| 1616 | 
             
                            # Depth gate cache for micro-detail injection (reuse per resolution)
         | 
| 1617 | 
             
                            depth_gate_cache = {"size": None, "mask": None}
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 1618 | 
             
                            # Prepare guided sampler once per node run to avoid cloning model each iteration
         | 
| 1619 | 
             
                            sampler_model = _wrap_model_with_guidance(
         | 
| 1620 | 
             
                                  model, guidance_mode, rescale_multiplier, momentum_beta, cfg_curve, perp_damp,
         | 
| @@ -1665,7 +1751,11 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 1665 | 
             
                                        device='cpu',
         | 
| 1666 | 
             
                                        generator=gen,
         | 
| 1667 | 
             
                                    ).to(current_latent["samples"].device)
         | 
| 1668 | 
            -
                                    current_latent["samples"]  | 
|  | |
|  | |
|  | |
|  | |
| 1669 |  | 
| 1670 | 
             
                                # ONNX pre-sampling detectors removed
         | 
| 1671 |  | 
| @@ -1695,6 +1785,23 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 1695 | 
             
                                            # No local guidance toggles here; keep optional mask hook clear
         | 
| 1696 | 
             
                                except Exception:
         | 
| 1697 | 
             
                                    pass
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 1698 |  | 
| 1699 | 
             
                                # Sampler model prepared once above; reuse it here (no-op assignment)
         | 
| 1700 | 
             
                                sampler_model = sampler_model
         | 
| @@ -1749,6 +1856,31 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 1749 |  | 
| 1750 | 
             
                                # cooperative cancel right after sampling, before further heavy work
         | 
| 1751 | 
             
                                model_management.throw_exception_if_processing_interrupted()
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 1752 |  | 
| 1753 | 
             
                                if bool(latent_compare):
         | 
| 1754 | 
             
                                    _cur = current_latent["samples"]
         | 
| @@ -1769,6 +1901,10 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 1769 | 
             
                                        current_denoise = max(0.20, current_denoise * damp)
         | 
| 1770 | 
             
                                        cfg_damp = 0.997 if damp > 0.9 else 0.99
         | 
| 1771 | 
             
                                        current_cfg = max(1.0, current_cfg * cfg_damp)
         | 
|  | |
|  | |
|  | |
|  | |
| 1772 |  | 
| 1773 | 
             
                                # AQClip-Lite: adaptive soft clipping in latent space (before decode)
         | 
| 1774 | 
             
                                try:
         | 
| @@ -1790,6 +1926,14 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 1790 | 
             
                                            H_override=H_override,
         | 
| 1791 | 
             
                                        )
         | 
| 1792 | 
             
                                        current_latent["samples"] = z_new
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 1793 | 
             
                                except Exception:
         | 
| 1794 | 
             
                                    pass
         | 
| 1795 |  | 
| @@ -1880,6 +2024,35 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 1880 | 
             
                                        # Feed back to latent for next steps
         | 
| 1881 | 
             
                                        current_latent = {"samples": safe_encode(vae, img2)}
         | 
| 1882 | 
             
                                        image = img2
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 1883 | 
             
                                    except Exception:
         | 
| 1884 | 
             
                                        pass
         | 
| 1885 |  | 
| @@ -1989,6 +2162,31 @@ class ComfyAdaptiveDetailEnhancer25: | |
| 1989 | 
             
                    except Exception:
         | 
| 1990 | 
             
                        pass
         | 
| 1991 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 1992 | 
             
                    # Cleanup KV pruning state to avoid leaking into other nodes
         | 
| 1993 | 
             
                    try:
         | 
| 1994 | 
             
                        if hasattr(sa_patch, "set_kv_prune"):
         | 
|  | |
| 156 | 
             
                        except Exception:
         | 
| 157 | 
             
                            pass
         | 
| 158 | 
             
                    m = (m * float(max(0.0, gain))).clamp(0, 1)
         | 
| 159 | 
            +
                    out_mask = m.unsqueeze(0).unsqueeze(-1)  # BHWC with B=1,C=1
         | 
| 160 | 
            +
                    # Best-effort release of temporaries to reduce RAM peak
         | 
| 161 | 
            +
                    try:
         | 
| 162 | 
            +
                        del inputs
         | 
| 163 | 
            +
                    except Exception:
         | 
| 164 | 
            +
                        pass
         | 
| 165 | 
            +
                    try:
         | 
| 166 | 
            +
                        del outputs
         | 
| 167 | 
            +
                    except Exception:
         | 
| 168 | 
            +
                        pass
         | 
| 169 | 
            +
                    try:
         | 
| 170 | 
            +
                        del logits
         | 
| 171 | 
            +
                    except Exception:
         | 
| 172 | 
            +
                        pass
         | 
| 173 | 
            +
                    try:
         | 
| 174 | 
            +
                        del prob
         | 
| 175 | 
            +
                    except Exception:
         | 
| 176 | 
            +
                        pass
         | 
| 177 | 
            +
                    try:
         | 
| 178 | 
            +
                        del pil_img
         | 
| 179 | 
            +
                    except Exception:
         | 
| 180 | 
            +
                        pass
         | 
| 181 | 
            +
                    try:
         | 
| 182 | 
            +
                        del arr
         | 
| 183 | 
            +
                    except Exception:
         | 
| 184 | 
            +
                        pass
         | 
| 185 | 
            +
                    try:
         | 
| 186 | 
            +
                        del x
         | 
| 187 | 
            +
                    except Exception:
         | 
| 188 | 
            +
                        pass
         | 
| 189 | 
            +
                    try:
         | 
| 190 | 
            +
                        del img
         | 
| 191 | 
            +
                    except Exception:
         | 
| 192 | 
            +
                        pass
         | 
| 193 | 
            +
                    return out_mask
         | 
| 194 | 
             
                except Exception as e:
         | 
| 195 | 
             
                    if not globals().get("_CLIPSEG_WARNED", False):
         | 
| 196 | 
             
                        print(f"[CADE2.5][CLIPSeg] mask failed: {e}")
         | 
|  | |
| 757 |  | 
| 758 |  | 
| 759 | 
             
            def safe_decode(vae, lat, tile=512, ovlp=64):
         | 
| 760 | 
            +
                # Avoid building autograd graphs and release GPU memory early
         | 
| 761 | 
            +
                with torch.inference_mode():
         | 
| 762 | 
            +
                    h, w = lat["samples"].shape[-2:]
         | 
| 763 | 
            +
                    if min(h, w) > 1024:
         | 
| 764 | 
            +
                        # Increase overlap for ultra-hires to reduce seam artifacts
         | 
| 765 | 
            +
                        ov = 128 if max(h, w) > 2048 else ovlp
         | 
| 766 | 
            +
                        out = vae.decode_tiled(lat["samples"], tile_x=tile, tile_y=tile, overlap=ov)
         | 
| 767 | 
            +
                    else:
         | 
| 768 | 
            +
                        out = vae.decode(lat["samples"])
         | 
| 769 | 
            +
                # Move to CPU and free VRAM ASAP
         | 
| 770 | 
            +
                try:
         | 
| 771 | 
            +
                    try:
         | 
| 772 | 
            +
                        out = out.detach()
         | 
| 773 | 
            +
                    except Exception:
         | 
| 774 | 
            +
                        pass
         | 
| 775 | 
            +
                    out_cpu = out
         | 
| 776 | 
            +
                    try:
         | 
| 777 | 
            +
                        out_cpu = out_cpu.to('cpu')
         | 
| 778 | 
            +
                    except Exception:
         | 
| 779 | 
            +
                        pass
         | 
| 780 | 
            +
                    try:
         | 
| 781 | 
            +
                        del out
         | 
| 782 | 
            +
                    except Exception:
         | 
| 783 | 
            +
                        pass
         | 
| 784 | 
            +
                    if torch.cuda.is_available():
         | 
| 785 | 
            +
                        try:
         | 
| 786 | 
            +
                            torch.cuda.synchronize()
         | 
| 787 | 
            +
                        except Exception:
         | 
| 788 | 
            +
                            pass
         | 
| 789 | 
            +
                        try:
         | 
| 790 | 
            +
                            torch.cuda.empty_cache()
         | 
| 791 | 
            +
                        except Exception:
         | 
| 792 | 
            +
                            pass
         | 
| 793 | 
            +
                    return out_cpu
         | 
| 794 | 
            +
                except Exception:
         | 
| 795 | 
            +
                    return out
         | 
| 796 |  | 
| 797 |  | 
| 798 | 
             
            def safe_encode(vae, img, tile=512, ovlp=64):
         | 
|  | |
| 1520 | 
             
                            "clipseg_blend": (["fuse", "replace", "intersect"], {"default": "fuse", "tooltip": "How to combine CLIPSeg with any pre-mask (if present)."}),
         | 
| 1521 | 
             
                            "clipseg_ref_gate": ("BOOLEAN", {"default": False, "tooltip": "If reference provided, boost mask when far from reference (CLIP-Vision)."}),
         | 
| 1522 | 
             
                            "clipseg_ref_threshold": ("FLOAT", {"default": 0.03, "min": 0.0, "max": 0.2, "step": 0.001}),
         | 
| 1523 | 
            +
                            # Under-the-hood saving (disabled by default)
         | 
| 1524 | 
            +
                            "auto_save": ("BOOLEAN", {"default": False, "tooltip": "Save final IMAGE directly from CADE (uses low PNG compress to reduce RAM)."}),
         | 
| 1525 | 
            +
                            "save_prefix": ("STRING", {"default": "ComfyUI", "multiline": False}),
         | 
| 1526 | 
            +
                            "save_compress": ("INT", {"default": 1, "min": 0, "max": 9, "step": 1}),
         | 
| 1527 |  | 
| 1528 | 
             
                            # Polish mode (final hi-res refinement)
         | 
| 1529 | 
             
                            "polish_enable": ("BOOLEAN", {"default": False, "tooltip": "Polish: keep low-frequency shape from reference while allowing high-frequency details to refine."}),
         | 
|  | |
| 1561 | 
             
                                 clipseg_threshold=0.40, clipseg_blur=7.0, clipseg_dilate=4,
         | 
| 1562 | 
             
                                 clipseg_gain=1.0, clipseg_blend="fuse", clipseg_ref_gate=False, clipseg_ref_threshold=0.03,
         | 
| 1563 | 
             
                                 polish_enable=False, polish_keep_low=0.4, polish_edge_lock=0.2, polish_sigma=1.0,
         | 
| 1564 | 
            +
                                  polish_start_after=1, polish_keep_low_ramp=0.2,
         | 
| 1565 | 
            +
                                  auto_save=False, save_prefix="ComfyUI", save_compress=1,
         | 
| 1566 | 
            +
                                  kv_prune_enable=False, kv_keep=0.85, kv_min_tokens=128):
         | 
| 1567 | 
             
                    # Hard reset of any sticky globals from prior runs
         | 
| 1568 | 
             
                    try:
         | 
| 1569 | 
             
                        global CURRENT_ONNX_MASK_BCHW
         | 
|  | |
| 1684 | 
             
                            clipseg_enable = False
         | 
| 1685 | 
             
                            # Depth gate cache for micro-detail injection (reuse per resolution)
         | 
| 1686 | 
             
                            depth_gate_cache = {"size": None, "mask": None}
         | 
| 1687 | 
            +
                            # Release preflight temporaries to avoid keeping big tensors alive
         | 
| 1688 | 
            +
                            try:
         | 
| 1689 | 
            +
                                del cmask
         | 
| 1690 | 
            +
                            except Exception:
         | 
| 1691 | 
            +
                                pass
         | 
| 1692 | 
            +
                            try:
         | 
| 1693 | 
            +
                                del om
         | 
| 1694 | 
            +
                            except Exception:
         | 
| 1695 | 
            +
                                pass
         | 
| 1696 | 
            +
                            try:
         | 
| 1697 | 
            +
                                del pre_mask
         | 
| 1698 | 
            +
                            except Exception:
         | 
| 1699 | 
            +
                                pass
         | 
| 1700 | 
            +
                            try:
         | 
| 1701 | 
            +
                                del image
         | 
| 1702 | 
            +
                            except Exception:
         | 
| 1703 | 
            +
                                pass
         | 
| 1704 | 
             
                            # Prepare guided sampler once per node run to avoid cloning model each iteration
         | 
| 1705 | 
             
                            sampler_model = _wrap_model_with_guidance(
         | 
| 1706 | 
             
                                  model, guidance_mode, rescale_multiplier, momentum_beta, cfg_curve, perp_damp,
         | 
|  | |
| 1751 | 
             
                                        device='cpu',
         | 
| 1752 | 
             
                                        generator=gen,
         | 
| 1753 | 
             
                                    ).to(current_latent["samples"].device)
         | 
| 1754 | 
            +
                                    current_latent["samples"] = current_latent["samples"] + (noise_offset * fade) * eps
         | 
| 1755 | 
            +
                                    try:
         | 
| 1756 | 
            +
                                        del eps
         | 
| 1757 | 
            +
                                    except Exception:
         | 
| 1758 | 
            +
                                        pass
         | 
| 1759 |  | 
| 1760 | 
             
                                # ONNX pre-sampling detectors removed
         | 
| 1761 |  | 
|  | |
| 1785 | 
             
                                            # No local guidance toggles here; keep optional mask hook clear
         | 
| 1786 | 
             
                                except Exception:
         | 
| 1787 | 
             
                                    pass
         | 
| 1788 | 
            +
                                # release heavy temporaries from CLIPSeg path
         | 
| 1789 | 
            +
                                try:
         | 
| 1790 | 
            +
                                    del img_prev2
         | 
| 1791 | 
            +
                                except Exception:
         | 
| 1792 | 
            +
                                    pass
         | 
| 1793 | 
            +
                                try:
         | 
| 1794 | 
            +
                                    del cmask
         | 
| 1795 | 
            +
                                except Exception:
         | 
| 1796 | 
            +
                                    pass
         | 
| 1797 | 
            +
                                try:
         | 
| 1798 | 
            +
                                    del fused
         | 
| 1799 | 
            +
                                except Exception:
         | 
| 1800 | 
            +
                                    pass
         | 
| 1801 | 
            +
                                try:
         | 
| 1802 | 
            +
                                    del om
         | 
| 1803 | 
            +
                                except Exception:
         | 
| 1804 | 
            +
                                    pass
         | 
| 1805 |  | 
| 1806 | 
             
                                # Sampler model prepared once above; reuse it here (no-op assignment)
         | 
| 1807 | 
             
                                sampler_model = sampler_model
         | 
|  | |
| 1856 |  | 
| 1857 | 
             
                                # cooperative cancel right after sampling, before further heavy work
         | 
| 1858 | 
             
                                model_management.throw_exception_if_processing_interrupted()
         | 
| 1859 | 
            +
                                # release sampler temporaries (best-effort)
         | 
| 1860 | 
            +
                                try:
         | 
| 1861 | 
            +
                                    del lat_img
         | 
| 1862 | 
            +
                                except Exception:
         | 
| 1863 | 
            +
                                    pass
         | 
| 1864 | 
            +
                                try:
         | 
| 1865 | 
            +
                                    del noise
         | 
| 1866 | 
            +
                                except Exception:
         | 
| 1867 | 
            +
                                    pass
         | 
| 1868 | 
            +
                                try:
         | 
| 1869 | 
            +
                                    del noise_mask
         | 
| 1870 | 
            +
                                except Exception:
         | 
| 1871 | 
            +
                                    pass
         | 
| 1872 | 
            +
                                try:
         | 
| 1873 | 
            +
                                    del callback
         | 
| 1874 | 
            +
                                except Exception:
         | 
| 1875 | 
            +
                                    pass
         | 
| 1876 | 
            +
                                try:
         | 
| 1877 | 
            +
                                    del sampler_obj
         | 
| 1878 | 
            +
                                except Exception:
         | 
| 1879 | 
            +
                                    pass
         | 
| 1880 | 
            +
                                try:
         | 
| 1881 | 
            +
                                    del sigmas
         | 
| 1882 | 
            +
                                except Exception:
         | 
| 1883 | 
            +
                                    pass
         | 
| 1884 |  | 
| 1885 | 
             
                                if bool(latent_compare):
         | 
| 1886 | 
             
                                    _cur = current_latent["samples"]
         | 
|  | |
| 1901 | 
             
                                        current_denoise = max(0.20, current_denoise * damp)
         | 
| 1902 | 
             
                                        cfg_damp = 0.997 if damp > 0.9 else 0.99
         | 
| 1903 | 
             
                                        current_cfg = max(1.0, current_cfg * cfg_damp)
         | 
| 1904 | 
            +
                                try:
         | 
| 1905 | 
            +
                                    del prev_samples
         | 
| 1906 | 
            +
                                except Exception:
         | 
| 1907 | 
            +
                                    pass
         | 
| 1908 |  | 
| 1909 | 
             
                                # AQClip-Lite: adaptive soft clipping in latent space (before decode)
         | 
| 1910 | 
             
                                try:
         | 
|  | |
| 1926 | 
             
                                            H_override=H_override,
         | 
| 1927 | 
             
                                        )
         | 
| 1928 | 
             
                                        current_latent["samples"] = z_new
         | 
| 1929 | 
            +
                                        try:
         | 
| 1930 | 
            +
                                            del H_override
         | 
| 1931 | 
            +
                                        except Exception:
         | 
| 1932 | 
            +
                                            pass
         | 
| 1933 | 
            +
                                        try:
         | 
| 1934 | 
            +
                                            del Hm
         | 
| 1935 | 
            +
                                        except Exception:
         | 
| 1936 | 
            +
                                            pass
         | 
| 1937 | 
             
                                except Exception:
         | 
| 1938 | 
             
                                    pass
         | 
| 1939 |  | 
|  | |
| 2024 | 
             
                                        # Feed back to latent for next steps
         | 
| 2025 | 
             
                                        current_latent = {"samples": safe_encode(vae, img2)}
         | 
| 2026 | 
             
                                        image = img2
         | 
| 2027 | 
            +
                                        # best-effort release of large temporaries
         | 
| 2028 | 
            +
                                        try:
         | 
| 2029 | 
            +
                                            del x
         | 
| 2030 | 
            +
                                            del r
         | 
| 2031 | 
            +
                                            del low_x
         | 
| 2032 | 
            +
                                            del low_r
         | 
| 2033 | 
            +
                                            del high_x
         | 
| 2034 | 
            +
                                            del low_mix
         | 
| 2035 | 
            +
                                            del new
         | 
| 2036 | 
            +
                                            del micro
         | 
| 2037 | 
            +
                                            del gray
         | 
| 2038 | 
            +
                                            del sobel_x
         | 
| 2039 | 
            +
                                            del sobel_y
         | 
| 2040 | 
            +
                                            del gx
         | 
| 2041 | 
            +
                                            del gy
         | 
| 2042 | 
            +
                                            del mag
         | 
| 2043 | 
            +
                                            del m_edge
         | 
| 2044 | 
            +
                                            del g_edge
         | 
| 2045 | 
            +
                                            del g_depth
         | 
| 2046 | 
            +
                                            del g
         | 
| 2047 | 
            +
                                            del ref_n
         | 
| 2048 | 
            +
                                            del ref
         | 
| 2049 | 
            +
                                            del img
         | 
| 2050 | 
            +
                                        except Exception:
         | 
| 2051 | 
            +
                                            pass
         | 
| 2052 | 
            +
                                        try:
         | 
| 2053 | 
            +
                                            clear_gpu_and_ram_cache()
         | 
| 2054 | 
            +
                                        except Exception:
         | 
| 2055 | 
            +
                                            pass
         | 
| 2056 | 
             
                                    except Exception:
         | 
| 2057 | 
             
                                        pass
         | 
| 2058 |  | 
|  | |
| 2162 | 
             
                    except Exception:
         | 
| 2163 | 
             
                        pass
         | 
| 2164 |  | 
| 2165 | 
            +
                    # Under-the-hood preview downscale for UI/output IMAGE to cap RAM during save/preview
         | 
| 2166 | 
            +
                    try:
         | 
| 2167 | 
            +
                        B, H, W, C = image.shape
         | 
| 2168 | 
            +
                        max_side = max(int(H), int(W))
         | 
| 2169 | 
            +
                        cap = 4096
         | 
| 2170 | 
            +
                        if max_side > cap:
         | 
| 2171 | 
            +
                            scale = float(cap) / float(max_side)
         | 
| 2172 | 
            +
                            nh = max(1, int(round(H * scale)))
         | 
| 2173 | 
            +
                            nw = max(1, int(round(W * scale)))
         | 
| 2174 | 
            +
                            x = image.movedim(-1, 1)
         | 
| 2175 | 
            +
                            x = F.interpolate(x, size=(nh, nw), mode='bilinear', align_corners=False)
         | 
| 2176 | 
            +
                            image = x.movedim(1, -1).clamp(0, 1).to(dtype=image.dtype)
         | 
| 2177 | 
            +
                    except Exception:
         | 
| 2178 | 
            +
                        pass
         | 
| 2179 | 
            +
             | 
| 2180 | 
            +
                    # Optional: save from node with low PNG compress to reduce RAM spike; ignore UI wiring
         | 
| 2181 | 
            +
                    try:
         | 
| 2182 | 
            +
                        if bool(auto_save):
         | 
| 2183 | 
            +
                            from comfy_api.latest._ui import ImageSaveHelper, FolderType
         | 
| 2184 | 
            +
                            _ = ImageSaveHelper.save_images(
         | 
| 2185 | 
            +
                                [image], filename_prefix=str(save_prefix), folder_type=FolderType.output,
         | 
| 2186 | 
            +
                                cls=ComfyAdaptiveDetailEnhancer25, compress_level=int(save_compress))
         | 
| 2187 | 
            +
                    except Exception:
         | 
| 2188 | 
            +
                        pass
         | 
| 2189 | 
            +
             | 
| 2190 | 
             
                    # Cleanup KV pruning state to avoid leaking into other nodes
         | 
| 2191 | 
             
                    try:
         | 
| 2192 | 
             
                        if hasattr(sa_patch, "set_kv_prune"):
         | 
    	
        pressets/mg_cade25.cfg
    CHANGED
    
    | @@ -15,7 +15,7 @@ cfg_delta: 0.03 | |
| 15 | 
             
            denoise_delta: 0.28
         | 
| 16 |  | 
| 17 | 
             
            # toggles
         | 
| 18 | 
            -
            apply_sharpen:  | 
| 19 | 
             
            apply_upscale: true
         | 
| 20 | 
             
            apply_ids: true
         | 
| 21 | 
             
            clip_clean: true
         | 
| @@ -42,9 +42,9 @@ ref_cooldown: 2 | |
| 42 |  | 
| 43 | 
             
            # guidance
         | 
| 44 | 
             
            guidance_mode: ZeResFDG
         | 
| 45 | 
            -
            rescale_multiplier: 0. | 
| 46 | 
            -
            momentum_beta: 0. | 
| 47 | 
            -
            cfg_curve:  | 
| 48 | 
             
            perp_damp: 0.80
         | 
| 49 |  | 
| 50 | 
             
            # NAG
         | 
| @@ -58,10 +58,12 @@ use_zero_init: false | |
| 58 | 
             
            zero_init_steps: 0
         | 
| 59 |  | 
| 60 | 
             
            # FDG / ZE thresholds
         | 
| 61 | 
            -
            fdg_low: 0.25
         | 
|  | |
| 62 | 
             
            fdg_high: 0.7
         | 
| 63 | 
             
            fdg_sigma: 1.20
         | 
| 64 | 
            -
            ze_res_zero_steps: 10
         | 
|  | |
| 65 | 
             
            ze_adaptive: true
         | 
| 66 | 
             
            ze_r_switch_hi: 0.85
         | 
| 67 | 
             
            ze_r_switch_lo: 0.25
         | 
| @@ -108,7 +110,7 @@ midfreq_sigma_hi: 2.00 | |
| 108 | 
             
            # QSilk-AQClip-Lite (adaptive latent clipping)
         | 
| 109 | 
             
            aqclip_enable: true
         | 
| 110 | 
             
            aq_tile: 32
         | 
| 111 | 
            -
            aq_stride:  | 
| 112 | 
             
            aq_alpha: 2.0
         | 
| 113 |  | 
| 114 | 
             
            aq_attn: true
         | 
| @@ -118,8 +120,8 @@ aq_attn: true | |
| 118 | 
             
            seed: 0
         | 
| 119 | 
             
            control_after_generate: randomize
         | 
| 120 | 
             
            steps: 30
         | 
| 121 | 
            -
            cfg:  | 
| 122 | 
            -
            denoise: 0. | 
| 123 | 
             
            sampler_name: ddim
         | 
| 124 | 
             
            scheduler: MGHybrid
         | 
| 125 | 
             
            iterations: 2
         | 
| @@ -154,14 +156,14 @@ ref_cooldown: 2 | |
| 154 | 
             
            # guidance
         | 
| 155 | 
             
            guidance_mode: ZeResFDG
         | 
| 156 | 
             
            #rescale_multiplier: 0.75
         | 
| 157 | 
            -
            rescale_multiplier: 0. | 
| 158 | 
             
            momentum_beta: 0.15
         | 
| 159 | 
             
            cfg_curve: 0.85
         | 
| 160 | 
             
            perp_damp: 0.80
         | 
| 161 |  | 
| 162 | 
             
            # NAG
         | 
| 163 | 
             
            use_nag: true
         | 
| 164 | 
            -
            nag_scale:  | 
| 165 | 
             
            nag_tau: 2.50
         | 
| 166 | 
             
            nag_alpha: 0.25
         | 
| 167 |  | 
| @@ -170,7 +172,7 @@ use_zero_init: false | |
| 170 | 
             
            zero_init_steps: 0
         | 
| 171 |  | 
| 172 | 
             
            # FDG / ZE thresholds
         | 
| 173 | 
            -
            fdg_low: 0. | 
| 174 | 
             
            fdg_high: 0.7
         | 
| 175 | 
             
            fdg_sigma: 1.10
         | 
| 176 | 
             
            ze_res_zero_steps: 12
         | 
| @@ -220,8 +222,8 @@ midfreq_sigma_hi: 2.10 | |
| 220 |  | 
| 221 | 
             
            # QSilk-AQClip-Lite (adaptive latent clipping)
         | 
| 222 | 
             
            aqclip_enable: true
         | 
| 223 | 
            -
            aq_tile:  | 
| 224 | 
            -
            aq_stride:  | 
| 225 | 
             
            aq_alpha: 2.0
         | 
| 226 |  | 
| 227 | 
             
            aq_attn: true
         | 
| @@ -231,7 +233,7 @@ aq_attn: true | |
| 231 | 
             
            seed: 0
         | 
| 232 | 
             
            control_after_generate: randomize
         | 
| 233 | 
             
            steps: 25
         | 
| 234 | 
            -
            cfg:  | 
| 235 | 
             
            denoise: 0.60
         | 
| 236 | 
             
            sampler_name: ddim
         | 
| 237 | 
             
            scheduler: MGHybrid
         | 
| @@ -267,14 +269,15 @@ ref_cooldown: 2 | |
| 267 |  | 
| 268 | 
             
            # guidance
         | 
| 269 | 
             
            guidance_mode: ZeResFDG
         | 
| 270 | 
            -
            rescale_multiplier: 1.10
         | 
|  | |
| 271 | 
             
            momentum_beta: 0.37
         | 
| 272 | 
             
            cfg_curve: 0.65
         | 
| 273 | 
             
            perp_damp: 0.95
         | 
| 274 |  | 
| 275 | 
             
            # NAG
         | 
| 276 | 
             
            use_nag: true
         | 
| 277 | 
            -
            nag_scale: 4. | 
| 278 | 
             
            nag_tau: 2.50
         | 
| 279 | 
             
            nag_alpha: 0.25
         | 
| 280 |  | 
| @@ -283,10 +286,10 @@ use_zero_init: false | |
| 283 | 
             
            zero_init_steps: 0
         | 
| 284 |  | 
| 285 | 
             
            # FDG / ZE thresholds
         | 
| 286 | 
            -
            fdg_low: 0. | 
| 287 | 
             
            fdg_high: 0.7
         | 
| 288 | 
             
            fdg_sigma: 1.10
         | 
| 289 | 
            -
            ze_res_zero_steps:  | 
| 290 | 
             
            ze_adaptive: true
         | 
| 291 | 
             
            ze_r_switch_hi: 0.85
         | 
| 292 | 
             
            ze_r_switch_lo: 0.25
         | 
| @@ -325,22 +328,32 @@ polish_start_after: 1 | |
| 325 | 
             
            polish_keep_low_ramp: 0.10
         | 
| 326 |  | 
| 327 | 
             
            # mid-frequency stabilizer (hands/objects scale)
         | 
| 328 | 
            -
            midfreq_enable: true
         | 
| 329 | 
            -
            #midfreq_gain: 0. | 
| 330 | 
            -
            #midfreq_sigma_lo: 0. | 
| 331 | 
            -
            #midfreq_sigma_hi: 2 | 
| 332 |  | 
| 333 | 
            -
             | 
| 334 | 
            -
             | 
| 335 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 336 |  | 
| 337 | 
             
            # QSilk-AQClip-Lite (adaptive latent clipping)
         | 
| 338 | 
             
            aqclip_enable: true
         | 
| 339 | 
            -
            aq_tile:  | 
| 340 | 
            -
            aq_stride:  | 
| 341 | 
             
            aq_alpha: 1.6
         | 
|  | |
|  | |
|  | |
| 342 |  | 
| 343 | 
            -
            aq_attn: false
         | 
| 344 | 
             
            # KV pruning (self-attention speedup)
         | 
| 345 | 
             
            kv_prune_enable: true
         | 
| 346 | 
             
            kv_keep: 0.80
         | 
| @@ -353,9 +366,10 @@ kv_min_tokens: 256 | |
| 353 | 
             
            seed: 0
         | 
| 354 | 
             
            control_after_generate: randomize
         | 
| 355 | 
             
            steps: 25
         | 
| 356 | 
            -
            cfg: 6.0
         | 
|  | |
| 357 | 
             
            #0.75
         | 
| 358 | 
            -
            denoise: 0. | 
| 359 | 
             
            sampler_name: ddim
         | 
| 360 | 
             
            scheduler: MGHybrid
         | 
| 361 | 
             
            iterations: 2
         | 
| @@ -396,7 +410,7 @@ perp_damp: 0.75 | |
| 396 |  | 
| 397 | 
             
            # NAG
         | 
| 398 | 
             
            use_nag: true
         | 
| 399 | 
            -
            nag_scale: 4. | 
| 400 | 
             
            nag_tau: 2.50
         | 
| 401 | 
             
            nag_alpha: 0.25
         | 
| 402 |  | 
| @@ -450,14 +464,16 @@ polish_keep_low_ramp: 0.10 | |
| 450 | 
             
            # mid-frequency stabilizer (hands/objects scale)
         | 
| 451 | 
             
            midfreq_enable: true
         | 
| 452 | 
             
            midfreq_gain: 0.72
         | 
| 453 | 
            -
            midfreq_sigma_lo: 0. | 
| 454 | 
            -
            midfreq_sigma_hi: 1. | 
|  | |
| 455 |  | 
| 456 | 
             
            # QSilk-AQClip-Lite (adaptive latent clipping)
         | 
| 457 | 
             
            aqclip_enable: true
         | 
| 458 | 
             
            aq_tile: 64
         | 
| 459 | 
            -
            aq_stride: 8
         | 
| 460 | 
            -
            aq_alpha:  | 
|  | |
| 461 |  | 
| 462 | 
             
            aq_attn: true
         | 
| 463 | 
             
            # KV pruning (self-attention speedup)
         | 
|  | |
| 15 | 
             
            denoise_delta: 0.28
         | 
| 16 |  | 
| 17 | 
             
            # toggles
         | 
| 18 | 
            +
            apply_sharpen: false
         | 
| 19 | 
             
            apply_upscale: true
         | 
| 20 | 
             
            apply_ids: true
         | 
| 21 | 
             
            clip_clean: true
         | 
|  | |
| 42 |  | 
| 43 | 
             
            # guidance
         | 
| 44 | 
             
            guidance_mode: ZeResFDG
         | 
| 45 | 
            +
            rescale_multiplier: 0.82
         | 
| 46 | 
            +
            momentum_beta: 0.22
         | 
| 47 | 
            +
            cfg_curve: 0.85
         | 
| 48 | 
             
            perp_damp: 0.80
         | 
| 49 |  | 
| 50 | 
             
            # NAG
         | 
|  | |
| 58 | 
             
            zero_init_steps: 0
         | 
| 59 |  | 
| 60 | 
             
            # FDG / ZE thresholds
         | 
| 61 | 
            +
            #fdg_low: 0.25
         | 
| 62 | 
            +
            fdg_low: 0.45
         | 
| 63 | 
             
            fdg_high: 0.7
         | 
| 64 | 
             
            fdg_sigma: 1.20
         | 
| 65 | 
            +
            #ze_res_zero_steps: 10
         | 
| 66 | 
            +
            ze_res_zero_steps: 11
         | 
| 67 | 
             
            ze_adaptive: true
         | 
| 68 | 
             
            ze_r_switch_hi: 0.85
         | 
| 69 | 
             
            ze_r_switch_lo: 0.25
         | 
|  | |
| 110 | 
             
            # QSilk-AQClip-Lite (adaptive latent clipping)
         | 
| 111 | 
             
            aqclip_enable: true
         | 
| 112 | 
             
            aq_tile: 32
         | 
| 113 | 
            +
            aq_stride: 8
         | 
| 114 | 
             
            aq_alpha: 2.0
         | 
| 115 |  | 
| 116 | 
             
            aq_attn: true
         | 
|  | |
| 120 | 
             
            seed: 0
         | 
| 121 | 
             
            control_after_generate: randomize
         | 
| 122 | 
             
            steps: 30
         | 
| 123 | 
            +
            cfg: 6.5
         | 
| 124 | 
            +
            denoise: 0.55
         | 
| 125 | 
             
            sampler_name: ddim
         | 
| 126 | 
             
            scheduler: MGHybrid
         | 
| 127 | 
             
            iterations: 2
         | 
|  | |
| 156 | 
             
            # guidance
         | 
| 157 | 
             
            guidance_mode: ZeResFDG
         | 
| 158 | 
             
            #rescale_multiplier: 0.75
         | 
| 159 | 
            +
            rescale_multiplier: 0.75
         | 
| 160 | 
             
            momentum_beta: 0.15
         | 
| 161 | 
             
            cfg_curve: 0.85
         | 
| 162 | 
             
            perp_damp: 0.80
         | 
| 163 |  | 
| 164 | 
             
            # NAG
         | 
| 165 | 
             
            use_nag: true
         | 
| 166 | 
            +
            nag_scale: 4.0
         | 
| 167 | 
             
            nag_tau: 2.50
         | 
| 168 | 
             
            nag_alpha: 0.25
         | 
| 169 |  | 
|  | |
| 172 | 
             
            zero_init_steps: 0
         | 
| 173 |  | 
| 174 | 
             
            # FDG / ZE thresholds
         | 
| 175 | 
            +
            fdg_low: 0.35
         | 
| 176 | 
             
            fdg_high: 0.7
         | 
| 177 | 
             
            fdg_sigma: 1.10
         | 
| 178 | 
             
            ze_res_zero_steps: 12
         | 
|  | |
| 222 |  | 
| 223 | 
             
            # QSilk-AQClip-Lite (adaptive latent clipping)
         | 
| 224 | 
             
            aqclip_enable: true
         | 
| 225 | 
            +
            aq_tile: 64
         | 
| 226 | 
            +
            aq_stride: 8
         | 
| 227 | 
             
            aq_alpha: 2.0
         | 
| 228 |  | 
| 229 | 
             
            aq_attn: true
         | 
|  | |
| 233 | 
             
            seed: 0
         | 
| 234 | 
             
            control_after_generate: randomize
         | 
| 235 | 
             
            steps: 25
         | 
| 236 | 
            +
            cfg: 4.0
         | 
| 237 | 
             
            denoise: 0.60
         | 
| 238 | 
             
            sampler_name: ddim
         | 
| 239 | 
             
            scheduler: MGHybrid
         | 
|  | |
| 269 |  | 
| 270 | 
             
            # guidance
         | 
| 271 | 
             
            guidance_mode: ZeResFDG
         | 
| 272 | 
            +
            #rescale_multiplier: 1.10
         | 
| 273 | 
            +
            rescale_multiplier: 0.75
         | 
| 274 | 
             
            momentum_beta: 0.37
         | 
| 275 | 
             
            cfg_curve: 0.65
         | 
| 276 | 
             
            perp_damp: 0.95
         | 
| 277 |  | 
| 278 | 
             
            # NAG
         | 
| 279 | 
             
            use_nag: true
         | 
| 280 | 
            +
            nag_scale: 4.5
         | 
| 281 | 
             
            nag_tau: 2.50
         | 
| 282 | 
             
            nag_alpha: 0.25
         | 
| 283 |  | 
|  | |
| 286 | 
             
            zero_init_steps: 0
         | 
| 287 |  | 
| 288 | 
             
            # FDG / ZE thresholds
         | 
| 289 | 
            +
            fdg_low: 0.40
         | 
| 290 | 
             
            fdg_high: 0.7
         | 
| 291 | 
             
            fdg_sigma: 1.10
         | 
| 292 | 
            +
            ze_res_zero_steps: 10
         | 
| 293 | 
             
            ze_adaptive: true
         | 
| 294 | 
             
            ze_r_switch_hi: 0.85
         | 
| 295 | 
             
            ze_r_switch_lo: 0.25
         | 
|  | |
| 328 | 
             
            polish_keep_low_ramp: 0.10
         | 
| 329 |  | 
| 330 | 
             
            # mid-frequency stabilizer (hands/objects scale)
         | 
| 331 | 
            +
            #midfreq_enable: true
         | 
| 332 | 
            +
            #midfreq_gain: 0.62
         | 
| 333 | 
            +
            #midfreq_sigma_lo: 0.50
         | 
| 334 | 
            +
            #midfreq_sigma_hi: 1.2
         | 
| 335 |  | 
| 336 | 
            +
            # QSilk-AQClip-Lite (adaptive latent clipping)
         | 
| 337 | 
            +
            #aqclip_enable: true
         | 
| 338 | 
            +
            #aq_tile: 128
         | 
| 339 | 
            +
            #aq_stride: 8
         | 
| 340 | 
            +
            #aq_alpha: 2.0
         | 
| 341 | 
            +
             | 
| 342 | 
            +
            # mid-frequency stabilizer (hands/objects scale)
         | 
| 343 | 
            +
            midfreq_enable: true
         | 
| 344 | 
            +
            midfreq_gain: 0.85
         | 
| 345 | 
            +
            midfreq_sigma_lo: 1.10
         | 
| 346 | 
            +
            midfreq_sigma_hi: 2.1
         | 
| 347 |  | 
| 348 | 
             
            # QSilk-AQClip-Lite (adaptive latent clipping)
         | 
| 349 | 
             
            aqclip_enable: true
         | 
| 350 | 
            +
            aq_tile: 64
         | 
| 351 | 
            +
            aq_stride: 8 
         | 
| 352 | 
             
            aq_alpha: 1.6
         | 
| 353 | 
            +
            aq_ema_beta: 0.90
         | 
| 354 | 
            +
             | 
| 355 | 
            +
            aq_attn: true
         | 
| 356 |  | 
|  | |
| 357 | 
             
            # KV pruning (self-attention speedup)
         | 
| 358 | 
             
            kv_prune_enable: true
         | 
| 359 | 
             
            kv_keep: 0.80
         | 
|  | |
| 366 | 
             
            seed: 0
         | 
| 367 | 
             
            control_after_generate: randomize
         | 
| 368 | 
             
            steps: 25
         | 
| 369 | 
            +
            #cfg: 6.0
         | 
| 370 | 
            +
            cfg: 5.0
         | 
| 371 | 
             
            #0.75
         | 
| 372 | 
            +
            denoise: 0.55
         | 
| 373 | 
             
            sampler_name: ddim
         | 
| 374 | 
             
            scheduler: MGHybrid
         | 
| 375 | 
             
            iterations: 2
         | 
|  | |
| 410 |  | 
| 411 | 
             
            # NAG
         | 
| 412 | 
             
            use_nag: true
         | 
| 413 | 
            +
            nag_scale: 4.5
         | 
| 414 | 
             
            nag_tau: 2.50
         | 
| 415 | 
             
            nag_alpha: 0.25
         | 
| 416 |  | 
|  | |
| 464 | 
             
            # mid-frequency stabilizer (hands/objects scale)
         | 
| 465 | 
             
            midfreq_enable: true
         | 
| 466 | 
             
            midfreq_gain: 0.72
         | 
| 467 | 
            +
            midfreq_sigma_lo: 0.55
         | 
| 468 | 
            +
            midfreq_sigma_hi: 1.3
         | 
| 469 | 
            +
             | 
| 470 |  | 
| 471 | 
             
            # QSilk-AQClip-Lite (adaptive latent clipping)
         | 
| 472 | 
             
            aqclip_enable: true
         | 
| 473 | 
             
            aq_tile: 64
         | 
| 474 | 
            +
            aq_stride: 8 
         | 
| 475 | 
            +
            aq_alpha: 1.8
         | 
| 476 | 
            +
            aq_ema_beta: 0.85
         | 
| 477 |  | 
| 478 | 
             
            aq_attn: true
         | 
| 479 | 
             
            # KV pruning (self-attention speedup)
         | 
    	
        pressets/mg_controlfusion.cfg
    CHANGED
    
    | @@ -25,7 +25,7 @@ strength_neg: 1.10 | |
| 25 |  | 
| 26 | 
             
            # schedule window
         | 
| 27 | 
             
            start_percent: 0.000
         | 
| 28 | 
            -
            end_percent:  | 
| 29 | 
             
            preview_res: 1024
         | 
| 30 | 
             
            mask_brightness: 1.00
         | 
| 31 | 
             
            preview_show_strength: true
         | 
| @@ -78,7 +78,7 @@ strength_neg: 1.10 | |
| 78 |  | 
| 79 | 
             
            # schedule window
         | 
| 80 | 
             
            start_percent: 0.000
         | 
| 81 | 
            -
            end_percent: 0. | 
| 82 | 
             
            preview_res: 1024
         | 
| 83 | 
             
            mask_brightness: 1.00
         | 
| 84 | 
             
            preview_show_strength: true
         | 
| @@ -94,7 +94,7 @@ split_apply: true | |
| 94 | 
             
            edge_start_percent: 0.000
         | 
| 95 | 
             
            edge_end_percent: 0.400
         | 
| 96 | 
             
            depth_start_percent: 0.000
         | 
| 97 | 
            -
            depth_end_percent: 0. | 
| 98 |  | 
| 99 | 
             
            # multipliers & shape
         | 
| 100 | 
             
            edge_strength_mul: 0.50
         | 
| @@ -131,7 +131,7 @@ strength_neg: 1.00 | |
| 131 |  | 
| 132 | 
             
            # schedule window
         | 
| 133 | 
             
            start_percent: 0.000
         | 
| 134 | 
            -
            end_percent: 0. | 
| 135 | 
             
            preview_res: 1024
         | 
| 136 | 
             
            mask_brightness: 1.00
         | 
| 137 | 
             
            preview_show_strength: true
         | 
| @@ -146,7 +146,7 @@ split_apply: true | |
| 146 | 
             
            # split timings
         | 
| 147 | 
             
            edge_start_percent: 0.000
         | 
| 148 | 
             
            #0.350
         | 
| 149 | 
            -
            edge_end_percent: 0. | 
| 150 | 
             
            depth_start_percent: 0.000
         | 
| 151 | 
             
            depth_end_percent: 1.000
         | 
| 152 |  | 
|  | |
| 25 |  | 
| 26 | 
             
            # schedule window
         | 
| 27 | 
             
            start_percent: 0.000
         | 
| 28 | 
            +
            end_percent: 0.700
         | 
| 29 | 
             
            preview_res: 1024
         | 
| 30 | 
             
            mask_brightness: 1.00
         | 
| 31 | 
             
            preview_show_strength: true
         | 
|  | |
| 78 |  | 
| 79 | 
             
            # schedule window
         | 
| 80 | 
             
            start_percent: 0.000
         | 
| 81 | 
            +
            end_percent: 0.800
         | 
| 82 | 
             
            preview_res: 1024
         | 
| 83 | 
             
            mask_brightness: 1.00
         | 
| 84 | 
             
            preview_show_strength: true
         | 
|  | |
| 94 | 
             
            edge_start_percent: 0.000
         | 
| 95 | 
             
            edge_end_percent: 0.400
         | 
| 96 | 
             
            depth_start_percent: 0.000
         | 
| 97 | 
            +
            depth_end_percent: 0.900
         | 
| 98 |  | 
| 99 | 
             
            # multipliers & shape
         | 
| 100 | 
             
            edge_strength_mul: 0.50
         | 
|  | |
| 131 |  | 
| 132 | 
             
            # schedule window
         | 
| 133 | 
             
            start_percent: 0.000
         | 
| 134 | 
            +
            end_percent: 0.750
         | 
| 135 | 
             
            preview_res: 1024
         | 
| 136 | 
             
            mask_brightness: 1.00
         | 
| 137 | 
             
            preview_show_strength: true
         | 
|  | |
| 146 | 
             
            # split timings
         | 
| 147 | 
             
            edge_start_percent: 0.000
         | 
| 148 | 
             
            #0.350
         | 
| 149 | 
            +
            edge_end_percent: 0.550
         | 
| 150 | 
             
            depth_start_percent: 0.000
         | 
| 151 | 
             
            depth_end_percent: 1.000
         | 
| 152 |  | 
