fix(z_image): align cap_pos length with cap_out by passing raw caption length#13576
Open
Anai-Guo wants to merge 2 commits intohuggingface:mainfrom
Open
fix(z_image): align cap_pos length with cap_out by passing raw caption length#13576Anai-Guo wants to merge 2 commits intohuggingface:mainfrom
Anai-Guo wants to merge 2 commits intohuggingface:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #13574. In
transformer_z_image.py(and the# Copied from-markedcontrolnet_z_image.pypath) the caption call to_pad_with_idspassesa pre-padded
pos_grid_sizewhile the function itself also pads. Thisdouble-pads the position-id tensor while the feature tensor only gets
single-padded, so they end up with different lengths and downstream
torch.catproduces acap_pos_idslonger thancap_out.For
len(cap_item) = 120andSEQ_MULTI_OF = 32:pos_grid_sizeflat_pad_with_idspad_len((-120) % 32)cap_out(120 + 8)pos_ids(128 + 8)_pad_with_idsalready roundsori_lenup toSEQ_MULTI_OFandgenerates the trailing pad pos-ids itself. The image / siglip call
sites correctly pass the original
(F_t, H_t, W_t)/(1, sig_H, sig_W)grid, only the caption call sites pass the pre-padded length.
Fix
In both
patchify_and_embed(basic mode) andpatchify_and_embed_omni,pass
(len(cap_feat), 1, 1)/(len(cap_item), 1, 1)to_pad_with_idsinstead of
(len(cap_feat) + (-len(cap_feat)) % SEQ_MULTI_OF, 1, 1).After the fix,
cap_pos_idsandcap_outare bothtotal_len(
ori_len + pad_len), so the per-batchtorch.catand downstreamattention shapes line up.
The same
_pad_with_idsbody is duplicated incontrolnet_z_image.pyvia a# Copied frommarker; the basic-modecaller there has the same bug and is fixed identically.
Test plan
len(all_cap_pos_ids[i]) == len(all_cap_out[i])for every batchelement with non-multiple-of-32 caption lengths.
🤖 Generated with Claude Code