Made hook clone code sane, made clear ObjectPatchHook and SetInjectionsHook are not yet operational

This commit is contained in:
Jedrzej Kosinski 2025-01-05 22:25:51 -06:00
parent 8270ff312f
commit 4446c86052
2 changed files with 31 additions and 34 deletions

View File

@ -101,10 +101,8 @@ class Hook:
def reset(self): def reset(self):
self.hook_keyframe.reset() self.hook_keyframe.reset()
def clone(self, subtype: Callable=None): def clone(self):
if subtype is None: c: Hook = self.__class__()
subtype = type(self)
c: Hook = subtype()
c.hook_type = self.hook_type c.hook_type = self.hook_type
c.hook_ref = self.hook_ref c.hook_ref = self.hook_ref
c.hook_id = self.hook_id c.hook_id = self.hook_id
@ -182,10 +180,8 @@ class WeightHook(Hook):
return True return True
# TODO: add logs about any keys that were not applied # TODO: add logs about any keys that were not applied
def clone(self, subtype: Callable=None): def clone(self):
if subtype is None: c: WeightHook = super().clone()
subtype = type(self)
c: WeightHook = super().clone(subtype)
c.weights = self.weights c.weights = self.weights
c.weights_clip = self.weights_clip c.weights_clip = self.weights_clip
c.need_weight_init = self.need_weight_init c.need_weight_init = self.need_weight_init
@ -194,17 +190,21 @@ class WeightHook(Hook):
return c return c
class ObjectPatchHook(Hook): class ObjectPatchHook(Hook):
def __init__(self): def __init__(self, object_patches: dict[str]=None):
super().__init__(hook_type=EnumHookType.ObjectPatch) super().__init__(hook_type=EnumHookType.ObjectPatch)
self.object_patches: dict = None self.object_patches = object_patches
def clone(self, subtype: Callable=None): def clone(self):
if subtype is None: c: ObjectPatchHook = super().clone()
subtype = type(self)
c: ObjectPatchHook = super().clone(subtype)
c.object_patches = self.object_patches c.object_patches = self.object_patches
return c return c
# TODO: add functionality
def add_hook_patches(self, model: ModelPatcher, model_options: dict, target_dict: dict[str], registered: HookGroup):
raise NotImplementedError("ObjectPatchHook is not supported yet in ComfyUI.")
if not self.should_register(model, model_options, target_dict, registered):
return False
registered.add(self)
return True
class AddModelsHook(Hook): class AddModelsHook(Hook):
''' '''
@ -219,12 +219,10 @@ class AddModelsHook(Hook):
self.append_when_same = True self.append_when_same = True
'''Curently does nothing.''' '''Curently does nothing.'''
def clone(self, subtype: Callable=None): def clone(self):
if subtype is None: c: AddModelsHook = super().clone()
subtype = type(self)
c: AddModelsHook = super().clone(subtype)
c.key = self.key
c.models = self.models.copy() if self.models else self.models c.models = self.models.copy() if self.models else self.models
c.key = self.key
c.append_when_same = self.append_when_same c.append_when_same = self.append_when_same
return c return c
@ -242,10 +240,8 @@ class TransformerOptionsHook(Hook):
super().__init__(hook_type=EnumHookType.TransformerOptions) super().__init__(hook_type=EnumHookType.TransformerOptions)
self.transformers_dict = wrappers_dict self.transformers_dict = wrappers_dict
def clone(self, subtype: Callable=None): def clone(self):
if subtype is None: c: TransformerOptionsHook = super().clone()
subtype = type(self)
c: WrapperHook = super().clone(subtype)
c.transformers_dict = self.transformers_dict c.transformers_dict = self.transformers_dict
return c return c
@ -265,11 +261,8 @@ class TransformerOptionsHook(Hook):
def on_apply_hooks(self, model: ModelPatcher, transformer_options: dict[str]): def on_apply_hooks(self, model: ModelPatcher, transformer_options: dict[str]):
comfy.patcher_extension.merge_nested_dicts(transformer_options, self.transformers_dict, copy_dict1=False) comfy.patcher_extension.merge_nested_dicts(transformer_options, self.transformers_dict, copy_dict1=False)
class WrapperHook(TransformerOptionsHook): WrapperHook = TransformerOptionsHook
''' '''Only here for backwards compatibility, WrapperHook is identical to TransformerOptionsHook.'''
For backwards compatibility, this hook is identical to TransformerOptionsHook.
'''
pass
class SetInjectionsHook(Hook): class SetInjectionsHook(Hook):
def __init__(self, key: str=None, injections: list[PatcherInjection]=None): def __init__(self, key: str=None, injections: list[PatcherInjection]=None):
@ -277,14 +270,19 @@ class SetInjectionsHook(Hook):
self.key = key self.key = key
self.injections = injections self.injections = injections
def clone(self, subtype: Callable=None): def clone(self):
if subtype is None: c: SetInjectionsHook = super().clone()
subtype = type(self)
c: SetInjectionsHook = super().clone(subtype)
c.key = self.key c.key = self.key
c.injections = self.injections.copy() if self.injections else self.injections c.injections = self.injections.copy() if self.injections else self.injections
return c return c
def add_hook_patches(self, model: ModelPatcher, model_options: dict, target_dict: dict[str], registered: HookGroup):
raise NotImplementedError("SetInjectionsHook is not supported yet in ComfyUI.")
if not self.should_register(model, model_options, target_dict, registered):
return False
registered.add(self)
return True
def add_hook_injections(self, model: ModelPatcher): def add_hook_injections(self, model: ModelPatcher):
# TODO: add functionality # TODO: add functionality
pass pass

View File

@ -157,4 +157,3 @@ def prepare_model_patcher(model: 'ModelPatcher', conds, model_options: dict):
comfy.patcher_extension.merge_nested_dicts(to_load_options.setdefault(wc_name, {}), model_options["transformer_options"][wc_name], comfy.patcher_extension.merge_nested_dicts(to_load_options.setdefault(wc_name, {}), model_options["transformer_options"][wc_name],
copy_dict1=False) copy_dict1=False)
return to_load_options return to_load_options