Experimental clip skip interpolation

This commit is contained in:
AI-Casanova
2024-02-09 11:49:15 -06:00
committed by Vladimir Mandic
parent 5f6ec0872d
commit 33c8285e69
3 changed files with 15 additions and 9 deletions
+13 -7
View File
@@ -1,4 +1,5 @@
import os
import math
import time
import typing
import torch
@@ -17,15 +18,20 @@ def compel_hijack(self, token_ids: torch.Tensor,
attention_mask,
output_hidden_states=needs_hidden_states,
return_dict=True)
if not needs_hidden_states:
return text_encoder_output.last_hidden_state
normalized = self.returned_embeddings_type > 0
if normalized and needs_hidden_states:
clip_skip_hidden_state = text_encoder_output.hidden_states[-abs(self.returned_embeddings_type)]
return self.text_encoder.text_model.final_layer_norm(clip_skip_hidden_state)
if needs_hidden_states:
clip_skip_hidden_state = text_encoder_output.hidden_states[-abs(self.returned_embeddings_type)]
return clip_skip_hidden_state
clip_skip = math.floor(abs(self.returned_embeddings_type))
interpolation = abs(self.returned_embeddings_type) - clip_skip
if interpolation:
hidden_state = (1 - interpolation) * text_encoder_output.hidden_states[-clip_skip] + interpolation * text_encoder_output.hidden_states[-(clip_skip+1)]
else:
hidden_state = text_encoder_output.hidden_states[-clip_skip]
if normalized:
hidden_state = self.text_encoder.text_model.final_layer_norm(hidden_state)
return hidden_state
return text_encoder_output.last_hidden_state
EmbeddingsProvider._encode_token_ids_to_embeddings = compel_hijack
+1 -1
View File
@@ -109,7 +109,7 @@ def create_advanced_inputs(tab):
diffusers_guidance_rescale = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, label='Guidance rescale', value=0.7, elem_id=f"{tab}_image_cfg_rescale", visible=shared.backend == shared.Backend.DIFFUSERS)
diffusers_sag_scale = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, label='Attention guidance', value=0.0, elem_id=f"{tab}_image_sag_scale", visible=shared.backend == shared.Backend.DIFFUSERS)
with gr.Row():
clip_skip = gr.Slider(label='CLIP skip', value=1, minimum=0, maximum=12, step=1, elem_id=f"{tab}_clip_skip", interactive=True)
clip_skip = gr.Slider(label='CLIP skip', value=1, minimum=0, maximum=12, step=0.1, elem_id=f"{tab}_clip_skip", interactive=True)
with gr.Group():
gr.HTML('<br>')
with gr.Row():
+1 -1
View File
@@ -233,7 +233,7 @@ axis_options = [
AxisOption("CFG End", float, apply_field("cfg_end")),
AxisOption("Variation seed", int, apply_field("subseed")),
AxisOption("Variation strength", float, apply_field("subseed_strength")),
AxisOption("Clip skip", int, apply_clip_skip),
AxisOption("Clip skip", float, apply_clip_skip),
AxisOption("Denoising strength", float, apply_field("denoising_strength")),
AxisOption("Prompt order", str_permutations, apply_order, fmt=format_value_join_list),
AxisOption("Model dictionary", str, apply_dict, fmt=format_value, cost=1.0, choices=lambda: ['None'] + list(sd_models.checkpoints_list)),