diff --git a/modules/prompt_parser_diffusers.py b/modules/prompt_parser_diffusers.py index 9539a12f2..e51aab794 100644 --- a/modules/prompt_parser_diffusers.py +++ b/modules/prompt_parser_diffusers.py @@ -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 diff --git a/modules/ui_sections.py b/modules/ui_sections.py index 95a4d150d..3c026d3ca 100644 --- a/modules/ui_sections.py +++ b/modules/ui_sections.py @@ -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('
') with gr.Row(): diff --git a/scripts/xyz_grid.py b/scripts/xyz_grid.py index a76a668e4..ae668d11a 100644 --- a/scripts/xyz_grid.py +++ b/scripts/xyz_grid.py @@ -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)),