Skip to content

Fix: Anima regional prompting should mirror width/height calculation for correct mask size - #9356

Merged
lstein merged 5 commits into
invoke-ai:mainfrom
FrankBro:fix/anima-regional-prompting-odd-resolution
Jul 24, 2026
Merged

Fix: Anima regional prompting should mirror width/height calculation for correct mask size#9356
lstein merged 5 commits into
invoke-ai:mainfrom
FrankBro:fix/anima-regional-prompting-odd-resolution

Conversation

@FrankBro

@FrankBro FrankBro commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Summary

When using regional prompting with the Anima models, I would receive this error:

Server Error (2)
RuntimeError: The expanded size of the tensor (8160) must match the existing size (8040) at non-singleton dimension 2. Target sizes: [1, 16, 8160, 1024]. Tensor sizes: [1, 1, 8040, 1024]

An inverted ceil vs floor type division caused an off by 1 error.

QA Instructions

Claude used this script to confirm the math was wrong, based on my resolution (1080x1920) and the error message.

import math
for (w,h) in [(1080,1920),(1920,1080),(1024,1024)]:
    lh, lw = h//8, w//8
    old = (lh//2)*(lw//2)
    new = math.ceil(lh/2)*math.ceil(lw/2)
    def pad_tokens(d, p=2):
        pad = (p - d % p) % p
        return (d+pad)//p
    actual = pad_tokens(lh)*pad_tokens(lw)
    print(f'{w}x{h}: latent={lw}x{lh} old_mask={old} new_mask={new} transformer={actual} match={new==actual}')

# 1080x1920: latent=135x240 old_mask=8040 new_mask=8160 transformer=8160 match=True
# 1920x1080: latent=240x135 old_mask=8040 new_mask=8160 transformer=8160 match=True
# 1024x1024: latent=128x128 old_mask=4096 new_mask=4096 transformer=4096 match=True

I build my version of the code locally and can confirm the popup error does not appear anymore.
Additionally, I extracted the calculation into its own static function before adding a unit test to check for this regression.

Checklist

  • The PR has a short but descriptive title, suitable for a changelog
  • Tests added / updated (if applicable)
  • ❗Changes to a redux slice have a corresponding migration
  • Documentation added / updated (if applicable)
  • Updated What's New copy (if doing a release after this PR)

FrankBro and others added 2 commits July 14, 2026 21:33
Extract the image token grid computation into
AnimaDenoiseInvocation._compute_img_token_grid so it can be unit tested,
and add TestComputeImgTokenGrid. The key test cross-checks the grid
against the transformer's real MiniTrainDIT._pad_to_patch_size so the
mask sizing and the transformer's patchified grid cannot silently drift,
plus a regression case for the 1080x1920 (8160 vs 8040) mask shape
mismatch.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added python PRs that change python files invocations PRs that change invocations python-tests PRs that change python tests labels Jul 15, 2026
The img_token grid uses ceiling division (ceil(latent / patch_size)) to
mirror the transformer's padding, but two docstrings still described it as
floor division (H // patch_size). Update them to match the actual math.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@lstein lstein left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the full diff and traced the fix through to consumption. This is a correct, well-scoped bugfix.

Correctness (verified): The new _compute_img_token_grid ceil division exactly mirrors the transformer's MiniTrainDIT._pad_to_patch_size, which pads the latent H/W up to a multiple of patch_spatial before patchifying. I traced the mask through preprocess_regional_prompt_mask — it resizes to (img_token_height, img_token_width) and flattens to img_seq_len, so mask and transformer now agree on the ceil-padded token count. This is the only token-grid computation on the regional path, so nothing else needed the same fix.

Tests: test_matches_transformer_padding is the highlight — it cross-checks the computed grid against the transformer's real padding logic (via a lightweight SimpleNamespace stub) rather than a hand-copied expectation, so the two sides can't silently drift. The explicit 8160-token regression case documents the original crash nicely. All 24 tests pass locally.

One small follow-up I pushed directly (1e9ff93, maintainerCanModify was on): two docstrings — in _load_text_conditionings and preprocess_regional_prompt_mask — still described the grid as H // patch_size (floor), which is exactly the thing this PR fixes. Updated them to ceil(latent / patch_size) so the docs match the math. No functional change.

Nice work extracting the computation into a testable static method — that's the right shape for this fix. LGTM.

@github-actions github-actions Bot added the backend PRs that change backend files label Jul 24, 2026
@lstein
lstein enabled auto-merge (squash) July 24, 2026 02:41
@lstein
lstein merged commit 350eda6 into invoke-ai:main Jul 24, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend PRs that change backend files invocations PRs that change invocations python PRs that change python files python-tests PRs that change python tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants