Fix: Anima regional prompting should mirror width/height calculation for correct mask size - #9356
Conversation
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>
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
left a comment
There was a problem hiding this comment.
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.
Summary
When using regional prompting with the Anima models, I would receive this error:
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.
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
What's Newcopy (if doing a release after this PR)