From f56d210854d57faf5432d61989d7798ad0be56f8 Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Thu, 8 Feb 2024 19:40:26 -0500 Subject: [PATCH] update clip-skip code --- CHANGELOG.md | 2 +- cli/image-grid.py | 4 ++-- modules/processing_diffusers.py | 11 +++++------ modules/prompt_parser_diffusers.py | 3 +-- modules/ui_sections.py | 2 +- 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8db78f5ae..20188ce9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ *settings -> compute -> model compile -> deep-cache* and *settings -> compute -> model compile -> cache interval* - **other**: - improved `clip-skip` value handling in diffusers, thanks @AI-Casanova & @Disty0 - *note*: reproducing old genarations may need to have clip-skip value offset by +1 for sdxl + now clip-skip range is 0-12 where previously lowest value was 1 (default is still 1) - add `--theme` cli param to force theme on startup - **fixes**: - `installer` refresh package cache on any install diff --git a/cli/image-grid.py b/cli/image-grid.py index 743a12bdf..8a48aecd9 100755 --- a/cli/image-grid.py +++ b/cli/image-grid.py @@ -52,12 +52,12 @@ def grid(images, labels = None, width = 0, height = 0, border = 0, square = Fals h = round(height / rows) size = tuple(size) image = Image.new('RGB', size = size, color = 'black') # pylint: disable=redefined-outer-name - font = ImageFont.truetype('DejaVuSansMono', round(w / 20)) + font = ImageFont.truetype('DejaVuSansMono', round(w / 40)) for i, img in enumerate(images): # pylint: disable=redefined-outer-name x = (i % cols * w) + (i % cols * border) y = (i // cols * h) + (i // cols * border) img.thumbnail((w, h), Image.Resampling.HAMMING) - image.paste(img, box=(x, y)) + image.paste(img, box=(x + int(border / 2), y + int(border / 2))) if labels is not None and len(images) == len(labels): ctx = ImageDraw.Draw(image) label = wrap(labels[i], font, w) diff --git a/modules/processing_diffusers.py b/modules/processing_diffusers.py index 2fa1d814d..e2da25cf5 100644 --- a/modules/processing_diffusers.py +++ b/modules/processing_diffusers.py @@ -174,21 +174,20 @@ def process_diffusers(p: processing.StableDiffusionProcessing): generator = [torch.Generator(generator_device).manual_seed(s) for s in p.seeds] prompts, negative_prompts, prompts_2, negative_prompts_2 = fix_prompts(prompts, negative_prompts, prompts_2, negative_prompts_2) parser = 'Fixed attention' + clip_skip = kwargs.pop("clip_skip", None) + if clip_skip is not None: + clip_skip -= 1 if shared.opts.prompt_attention != 'Fixed attention' and 'StableDiffusion' in model.__class__.__name__ and 'Onnx' not in model.__class__.__name__: try: - prompt_parser_diffusers.encode_prompts(model, p, prompts, negative_prompts, kwargs.get("num_inference_steps", 1), kwargs.pop("clip_skip", None)) + prompt_parser_diffusers.encode_prompts(model, p, prompts, negative_prompts, steps=kwargs.get("num_inference_steps", 1), clip_skip=clip_skip) parser = shared.opts.prompt_attention except Exception as e: shared.log.error(f'Prompt parser encode: {e}') if os.environ.get('SD_PROMPT_DEBUG', None) is not None: errors.display(e, 'Prompt parser encode') if parser == 'Fixed attention': - clip_skip = kwargs.pop("clip_skip", None) if clip_skip is not None: - if 'XL' in model.__class__.__name__: - args['clip_skip'] = clip_skip - 2 - else: - args['clip_skip'] = clip_skip - 1 + args['clip_skip'] = clip_skip if 'prompt' in possible: if hasattr(model, 'text_encoder') and 'prompt_embeds' in possible and len(p.prompt_embeds) > 0 and p.prompt_embeds[0] is not None: args['prompt_embeds'] = p.prompt_embeds[0] diff --git a/modules/prompt_parser_diffusers.py b/modules/prompt_parser_diffusers.py index 3b3c81ba2..42957ff97 100644 --- a/modules/prompt_parser_diffusers.py +++ b/modules/prompt_parser_diffusers.py @@ -89,8 +89,7 @@ def get_prompt_schedule(prompt, steps): return temp, len(schedule) > 1 -def encode_prompts(pipe, p, prompts: list, negative_prompts: list, steps: int, - clip_skip: typing.Optional[int] = None): +def encode_prompts(pipe, p, prompts: list, negative_prompts: list, steps: int, clip_skip: typing.Optional[int] = None): if 'StableDiffusion' not in pipe.__class__.__name__ and 'DemoFusion': shared.log.warning(f"Prompt parser not supported: {pipe.__class__.__name__}") return None, None, None, None diff --git a/modules/ui_sections.py b/modules/ui_sections.py index 811da23be..95a4d150d 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=1, maximum=13, step=1, elem_id=f"{tab}_clip_skip", interactive=True) + clip_skip = gr.Slider(label='CLIP skip', value=1, minimum=0, maximum=12, step=1, elem_id=f"{tab}_clip_skip", interactive=True) with gr.Group(): gr.HTML('
') with gr.Row():