Skip to content

Fix/prompt encoding fixes#4964

Merged
vladmandic merged 4 commits into
devfrom
fix/prompt-encoding-fixes
Jun 25, 2026
Merged

Fix/prompt encoding fixes#4964
vladmandic merged 4 commits into
devfrom
fix/prompt-encoding-fixes

Conversation

@CalamitousFelicitousness

Copy link
Copy Markdown
Collaborator

Description

Here are 4 hours of my life I'm not getting back.

Restore textual inversion (embeddings), which stopped applying in newer Transformers versions. Two independent regressions in the prompt-encoding path, plus a related clip-skip options-API crash.

Notes

Three root causes:

  • Multi-vector expansion (modules/textual_inversion.py): transformers 5 defaults add_tokens() to normalized=True, so CLIP lowercases added embedding names. tokenizer.tokenize() returns the lowercased surface, maybe_convert_prompt no longer matches the original-case name in added_tokens_encoder, and multi-vector embeddings never expand (each collapses to its first vector, so a 16-vector negative applies 1/16). Adding tokens as AddedToken(name, normalized=False) keeps them case-sensitive; convert_tokens_to_ids and encoding are unchanged.
  • clip-skip >= 2 crash (prompt_parser_diffusers.py, prompt_parser_xhinker.py, vendored pag/pipe_sd.py, apg/pipeline_stable_diffusion_apg.py, control/units/xs_pipe.py, scripts/differential_diffusion.py): transformers 5.6 flattened CLIPTextModel and dropped the .text_model wrapper. The clip-skip branch dereferences it, so SD1.5 at clip-skip >= 2 raised AttributeError, which processing_prompt caught and silently fell back to fixed-attention encoding (embeddings and prompt weighting lost). getattr(te, 'text_model', te) handles flattened CLIPTextModel, CLIPTextModelWithProjection (still nested), and transformers < 5.6.
  • Compat-option API crash (options_handler.py, processing.py): clip_skip and uni_pc_* are compatibility_opts (in opts.data, no data_labels entry). Options.set() read data_labels[key].onchange unconditionally -> KeyError -> 500 on POST /sdapi/v1/options {clip_skip}; the override_settings restore path had the same unguarded access. Guarded both with getattr(opts, k).

Environment and Testing

  • Linux (WSL2), NVIDIA RTX 3090, CUDA13.0; Python 3.13
  • 20 or so embeddings each for SD1.5 and SDXL chosen at random and ran a gen.

transformers 5 defaults tokenizer.add_tokens() to normalized=True, so the CLIP
tokenizer lowercases added embedding names. tokenizer.tokenize() then returns the
lowercased surface, maybe_convert_prompt never matches mixed-case names in
added_tokens_encoder, and multi-vector expansion is skipped. Every mixed-case
multi-vector embedding collapsed to its first vector and looked ignored.

Add embedding tokens as AddedToken(name, normalized=False) so they stay
case-sensitive and tokenize() surfaces them verbatim. Valid on transformers 4.x
and 5.x; convert_tokens_to_ids and encoding are unaffected.
transformers 5.6 flattened CLIPTextModel, removing the .text_model wrapper that
compel_hijack and the xhinker parser dereference on the normalized clip-skip path.
On SD1.5 at clip-skip >= 2 this raised AttributeError, which processing_prompt
caught and silently fell back to fixed-attention encoding, dropping textual
inversion and prompt weighting.

Resolve the submodule via getattr(te, 'text_model', te), correct for flattened
CLIPTextModel, CLIPTextModelWithProjection (still nested), and transformers < 5.6.
The vendored encode_prompt copies in the PAG, APG, ControlNet-XS and differential
diffusion pipelines dereference the .text_model wrapper that transformers 5.6
removed from CLIPTextModel, so their clip-skip path crashes on SD1.5 and SDXL TE1.
Apply the same getattr(te, 'text_model', te) fix as the core parser.

Mirrors upstream diffusers, which still carries this deref in pipeline
encode_prompt; only the single-file loader was fixed there.
clip_skip and the uni_pc_* opts live in opts.data without an OptionInfo in
data_labels (compatibility_opts). Options.set() read data_labels[key].onchange
unconditionally, so setting clip_skip via /sdapi/v1/options raised KeyError and
returned 500; the override_settings restore path had the same unguarded
data_labels[k] access for falsy-valued compat opts.

Guard the onchange lookup and read the stored value via getattr(opts, k), which
already falls back through data then data_labels.
@vladmandic

Copy link
Copy Markdown
Owner

lgtm!

@vladmandic vladmandic merged commit 5004377 into dev Jun 25, 2026
2 checks passed
@vladmandic vladmandic deleted the fix/prompt-encoding-fixes branch June 25, 2026 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants