Fix error when an IPAdapter has already been added to the model

This commit is contained in:
IDGallagher
2024-04-28 21:54:33 +01:00
parent 0da68f7e2d
commit 1f6aecba32
2 changed files with 25 additions and 1 deletions
@@ -25,6 +25,26 @@ class CrossAttentionPatchImport:
self.k_key = str(self.number*2+1) + "_to_k_ip"
self.v_key = str(self.number*2+1) + "_to_v_ip"
@classmethod
def from_cross_attention_patch(cls, patch):
instance = cls(ipadapter = patch.ipadapters[0])
instance.weights = patch.weights
instance.ipadapters = patch.ipadapters
instance.conds = patch.conds
instance.conds_alt = patch.conds_alt
instance.unconds = patch.unconds
instance.weight_types = patch.weight_types
instance.masks = patch.masks
instance.sigma_starts = patch.sigma_starts
instance.sigma_ends = patch.sigma_ends
instance.unfold_batch = patch.unfold_batch
instance.embeds_scaling = patch.embeds_scaling
instance.number = patch.number
instance.layers = patch.layers
instance.k_key = patch.k_key
instance.v_key = patch.v_key
return instance
def set_new_condition(self, ipadapter=None, number=0, weight=1.0, cond=None, cond_alt=None, uncond=None, weight_type="linear", mask=None, sigma_start=0.0, sigma_end=1.0, unfold_batch=False, image_schedule=None, embeds_scaling='V only'):
self.weights.append(weight)
self.ipadapters.append(ipadapter)
@@ -142,7 +142,11 @@ def set_model_patch_replace(model, patch_kwargs, key):
if key not in to["patches_replace"]["attn2"]:
to["patches_replace"]["attn2"][key] = CrossAttentionPatchImport(**patch_kwargs)
else:
to["patches_replace"]["attn2"][key].set_new_condition(**patch_kwargs)
if isinstance(to["patches_replace"]["attn2"][key], CrossAttentionPatchImport):
to["patches_replace"]["attn2"][key].set_new_condition(**patch_kwargs)
else:
to["patches_replace"]["attn2"][key] = CrossAttentionPatchImport.from_cross_attention_patch(to["patches_replace"]["attn2"][key])
to["patches_replace"]["attn2"][key].set_new_condition(**patch_kwargs)
def ipadapter_execute(model,
ipadapter,