Skip to content

Fix SD text encoder crash: infer sequence length from model#103

Merged
stikves merged 4 commits into
apple:mainfrom
stikves:fix/sd-text-encoder-seq-length
Jul 16, 2026
Merged

Fix SD text encoder crash: infer sequence length from model#103
stikves merged 4 commits into
apple:mainfrom
stikves:fix/sd-text-encoder-seq-length

Conversation

@stikves

@stikves stikves commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Fixes #102.

StableDiffusionPipeline.encodeText() passed raw token count as the sequence dimension, but CLIP text encoders are exported with a fixed seq_len (77 for SD 1.5/2.x). When a prompt tokenizes to a different length, resolvingDynamicDimensions crashes with:

Shape at dimension 1 of 96 is not a valid substitution for source shape 77

Fix:

  • Use CoreAITextEncoder.encode() which already pads/truncates correctly
  • Infer sequence length from the model's input descriptor at load time instead of hardcoding 77
  • Add inferSequenceLength() helper on CoreAIDiffusionModelFunction

stikves and others added 3 commits July 15, 2026 10:43
StableDiffusionPipeline.encodeText() passed raw token count as the
sequence dimension, but CLIP text encoders are exported with a fixed
seq_len (77). When a prompt tokenizes to a different length,
resolvingDynamicDimensions crashes.

Fix: use CoreAITextEncoder.encode() which pads/truncates correctly,
and infer the sequence length from the model input descriptor at
load time instead of hardcoding 77.

Fixes apple#102.
@stikves stikves marked this pull request as ready for review July 15, 2026 19:42
@stikves stikves self-assigned this Jul 15, 2026
@stikves stikves merged commit 917dc99 into apple:main Jul 16, 2026
3 checks passed
@stikves stikves deleted the fix/sd-text-encoder-seq-length branch July 16, 2026 02:01
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.

SD 2.1: resolvingDynamicDimensions fails with "Task 156365: Fatal error: Shape at dimension 1 of 96 is not a valid substitution for source shape 77"

2 participants