Add clip skip option to prompt node

This commit is contained in:
Sergey Borisov 2023-07-06 16:09:40 +03:00
parent 94e38e9769
commit 04b57c408f
2 changed files with 20 additions and 0 deletions

View File

@ -44,6 +44,7 @@ class CompelInvocation(BaseInvocation):
prompt: str = Field(default="", description="Prompt")
clip: ClipField = Field(None, description="Clip to use")
clip_skip: int = Field(0, description="Layers to skip in text_encoder")
# Schema customisation
class Config(InvocationConfig):
@ -95,6 +96,7 @@ class CompelInvocation(BaseInvocation):
with ModelPatcher.apply_lora_text_encoder(text_encoder_info.context.model, _lora_loader()),\
ModelPatcher.apply_ti(tokenizer_info.context.model, text_encoder_info.context.model, ti_list) as (tokenizer, ti_manager),\
ModelPatcher.apply_clip_skip(text_encoder_info.context.model, self.clip_skip),\
text_encoder_info as text_encoder:
compel = Compel(

View File

@ -615,6 +615,24 @@ class ModelPatcher:
text_encoder.resize_token_embeddings(init_tokens_count)
@classmethod
@contextmanager
def apply_clip_skip(
cls,
text_encoder: CLIPTextModel,
clip_skip: int,
):
skipped_layers = []
try:
for i in range(clip_skip):
skipped_layers.append(text_encoder.text_model.encoder.layers.pop(-1))
yield
finally:
while len(skipped_layers) > 0:
text_encoder.text_model.encoder.layers.append(skipped_layers.pop())
class TextualInversionModel:
name: str
embedding: torch.Tensor # [n, 768]|[n, 1280]