From 8e03755d4bc0636d8d5d06342adf85ad61b2d993 Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Sat, 1 Feb 2025 09:41:52 -0500 Subject: [PATCH] add setting for prompt linebreaks Signed-off-by: Vladimir Mandic --- CHANGELOG.md | 8 +++++++- modules/prompt_parser.py | 2 +- modules/prompt_parser_diffusers.py | 21 ++++++++++++++++++--- modules/shared.py | 1 + 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68c45cb77..575c0a985 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Change Log for SD.Next -## Update for 2025-01-31 +## Update for 2025-02-01 - **GitHub** - rename core repo from to @@ -16,9 +16,15 @@ to force torch upgrade, either start with new installation or use `--reinstall` flag - add support for torch **tunable ops**, this can speed up operations by up to *10-30%* on some platforms *set in settings -> backend settings -> torch* and *paths -> tunable ops cache* + - add support for stream-loading, this can speed up model loading when models are located on network drives + *set in settings -> models & loading -> model load using streams* - enhanced error logging - **Other**: - **Networks**: imporove search/filter and add visual indicators for types + - **balanced offload** new defaults: *lowvram/4gb min threshold: 0, medvram/8gb min threshold: 0, default min threshold 0.25* + - **prompt parser**: log stats with tokens, sections and min/avg/max weights + - **prompt parser**: add setting to ignore line breaks in prompt + set in *settings -> prompt settings -> use line breaks* - **Refactor**: - unified trace handler with configurable tracebacks - **Fixes**: diff --git a/modules/prompt_parser.py b/modules/prompt_parser.py index 1eb7a77ee..2a1dac2cc 100644 --- a/modules/prompt_parser.py +++ b/modules/prompt_parser.py @@ -326,7 +326,7 @@ def parse_prompt_attention(text): whitespace = '' else: re_attention = re_attention_v1 - if native: + if native and opts.sd_textencder_linebreak: text = text.replace('\n', ' BREAK ') else: text = text.replace('\n', ' ') diff --git a/modules/prompt_parser_diffusers.py b/modules/prompt_parser_diffusers.py index 4e31c747a..99796e770 100644 --- a/modules/prompt_parser_diffusers.py +++ b/modules/prompt_parser_diffusers.py @@ -354,13 +354,28 @@ def get_prompts_with_weights(pipe, prompt: str): if shared.opts.prompt_mean_norm: texts_and_weights = normalize_prompt(texts_and_weights) texts, text_weights = zip(*texts_and_weights) - if debug_enabled: + avg_weight = 0 + min_weight = 1 + max_weight = 0 + sections = 0 + + try: all_tokens = 0 - for text in texts: + for text, weight in zip(texts, text_weights): tokens = get_tokens(pipe, 'section', text) all_tokens += tokens - debug(f'Prompt tokenizer: parser={shared.opts.prompt_attention} tokens={all_tokens}') + avg_weight += tokens*weight + min_weight = min(min_weight, weight) + max_weight = max(max_weight, weight) + if text != 'BREAK': + sections += 1 + if all_tokens > 0: + avg_weight = avg_weight / all_tokens + shared.log.debug(f'Prompt tokenizer: parser={shared.opts.prompt_attention} len={len(prompt)} sections={sections} tokens={all_tokens} weights={min_weight:.2f}/{avg_weight:.2f}/{max_weight:.2f}') + except Exception: + pass debug(f'Prompt: weights={texts_and_weights} time={(time.time() - t0):.3f}') + return texts, text_weights diff --git a/modules/shared.py b/modules/shared.py index 6a13d1314..d9a3b1572 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -499,6 +499,7 @@ options_templates.update(options_section(('text_encoder', "Text Encoder"), { "sd_textencoder_cache": OptionInfo(True, "Cache text encoder results", gr.Checkbox, {"visible": False}), "sd_textencoder_cache_size": OptionInfo(4, "Text encoder cache size", gr.Slider, {"minimum": 0, "maximum": 16, "step": 1}), "comma_padding_backtrack": OptionInfo(20, "Prompt padding", gr.Slider, {"minimum": 0, "maximum": 74, "step": 1, "visible": not native }), + "sd_textencder_linebreak": OptionInfo(True, "Use line break as prompt segment marker", gr.Checkbox), "diffusers_zeros_prompt_pad": OptionInfo(False, "Use zeros for prompt padding", gr.Checkbox), "diffusers_pooled": OptionInfo("default", "Diffusers SDXL pooled embeds", gr.Radio, {"choices": ['default', 'weighted']}), }))