(executor, *args, **kwargs)
| 93 | return hooks |
| 94 | |
| 95 | def acn_outer_sample_wrapper(executor, *args, **kwargs): |
| 96 | controlnets_modified = False |
| 97 | guider: comfy.samplers.CFGGuider = executor.class_obj |
| 98 | model = guider.model_patcher |
| 99 | orig_conds = guider.conds |
| 100 | orig_model_options = guider.model_options |
| 101 | try: |
| 102 | new_model_options = orig_model_options |
| 103 | # if context options present, perform some special actions that may be required |
| 104 | context_refs = [] |
| 105 | if has_sliding_context_windows(guider.model_patcher): |
| 106 | new_model_options = comfy.model_patcher.create_model_options_clone(new_model_options) |
| 107 | # convert all CNs to Advanced if needed |
| 108 | controlnets_modified, conds = support_sliding_context_windows(orig_conds) |
| 109 | if controlnets_modified: |
| 110 | guider.conds = conds |
| 111 | # enable ContextRef, if requested |
| 112 | existing_contextref_obj = get_contextref_obj(guider.model_patcher) |
| 113 | if existing_contextref_obj is not None: |
| 114 | context_refs = handle_context_ref_setup(existing_contextref_obj, new_model_options["transformer_options"], guider.conds) |
| 115 | controlnets_modified = True |
| 116 | # look for Advanced ControlNets that will require intervention to work |
| 117 | ref_set = set() |
| 118 | for outer_cond in guider.conds.values(): |
| 119 | for cond in outer_cond: |
| 120 | if "control" in cond: |
| 121 | ref_set.update(get_refcn(cond["control"])) |
| 122 | # if no ref cn found, do original function immediately |
| 123 | if len(ref_set) == 0 and len(context_refs) == 0: |
| 124 | return executor(*args, **kwargs) |
| 125 | # otherwise, injection time |
| 126 | try: |
| 127 | # inject |
| 128 | # storage for all Reference-related injections |
| 129 | reference_injections = ReferenceInjections() |
| 130 | |
| 131 | # first, handle attn module injection |
| 132 | all_modules = torch_dfs(model.model) |
| 133 | attn_modules: list[RefBasicTransformerBlock] = [] |
| 134 | for module in all_modules: |
| 135 | if isinstance(module, BasicTransformerBlock): |
| 136 | attn_modules.append(module) |
| 137 | attn_modules = [module for module in all_modules if isinstance(module, BasicTransformerBlock)] |
| 138 | attn_modules = sorted(attn_modules, key=lambda x: -x.norm1.normalized_shape[0]) |
| 139 | for i, module in enumerate(attn_modules): |
| 140 | injection_holder = InjectionBasicTransformerBlockHolder(block=module, idx=i) |
| 141 | injection_holder.attn_weight = float(i) / float(len(attn_modules)) |
| 142 | if hasattr(module, "_forward"): # backward compatibility |
| 143 | module._forward = _forward_inject_BasicTransformerBlock.__get__(module, type(module)) |
| 144 | else: |
| 145 | module.forward = _forward_inject_BasicTransformerBlock.__get__(module, type(module)) |
| 146 | module.injection_holder = injection_holder |
| 147 | reference_injections.attn_modules.append(module) |
| 148 | # figure out which module is middle block |
| 149 | if hasattr(model.model.diffusion_model, "middle_block"): |
| 150 | mid_modules = torch_dfs(model.model.diffusion_model.middle_block) |
| 151 | mid_attn_modules: list[RefBasicTransformerBlock] = [module for module in mid_modules if isinstance(module, BasicTransformerBlock)] |
| 152 | for module in mid_attn_modules: |
nothing calls this directly
no test coverage detected