Skip to content

fix: resolve issue #13811#13813

Open
Zhu1116 wants to merge 1 commit into
huggingface:mainfrom
Zhu1116:fix/issue-13811
Open

fix: resolve issue #13811#13813
Zhu1116 wants to merge 1 commit into
huggingface:mainfrom
Zhu1116:fix/issue-13811

Conversation

@Zhu1116
Copy link
Copy Markdown

@Zhu1116 Zhu1116 commented May 26, 2026

What does this PR do?

Fixes #13811

I found an issue in the cond ids processing in train_dreambooth_lora_flux2_img2img.py in diffusers 0.38.0, around lines 1703-1709 of the package. This issue also exists in the main branch.

With the original code:

cond_model_input_list = [cond_model_input[i].unsqueeze(0) for i in range(cond_model_input.shape[0])]
cond_model_input_ids = Flux2Pipeline._prepare_image_ids(cond_model_input_list).to(
    device=cond_model_input.device
)
cond_model_input_ids = cond_model_input_ids.view(
    cond_model_input.shape[0], -1, model_input_ids.shape[-1]
)

When batch size is 2, the output cond_model_input_ids looks like this:

tensor([[[10,  0,  0,  0],
         [10,  0,  1,  0],
         [10,  0,  2,  0],
         ...,
         [10, 31, 29,  0],
         [10, 31, 30,  0],
         [10, 31, 31,  0]],

        [[20,  0,  0,  0],
         [20,  0,  1,  0],
         [20,  0,  2,  0],
         ...,
         [20, 31, 29,  0],
         [20, 31, 30,  0],
         [20, 31, 31,  0]]], device='cuda:0')

However, cond ids within the same batch should not be different.
Flux2Pipeline._prepare_image_ids is designed for multiple conditioning images from the same sample.

With the fixed code:

model_input_ids = Flux2Pipeline._prepare_latent_ids(model_input).to(device=model_input.device)
cond_model_input_ids = Flux2Pipeline._prepare_image_ids([cond_model_input[0:1]]).to(
    device=cond_model_input.device
)
cond_model_input_ids = cond_model_input_ids.expand(
    cond_model_input.shape[0], -1, -1
)

The output becomes correct (same cond ids for the whole batch):

tensor([[[10,  0,  0,  0],
         [10,  0,  1,  0],
         [10,  0,  2,  0],
         ...,
         [10, 31, 29,  0],
         [10, 31, 30,  0],
         [10, 31, 31,  0]],

        [[10,  0,  0,  0],
         [10,  0,  1,  0],
         [10,  0,  2,  0],
         ...,
         [10, 31, 29,  0],
         [10, 31, 30,  0],
         [10, 31, 31,  0]]], device='cuda:0')

I'm sorry if I have misunderstood the underlying logic.

Before submitting

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@github-actions github-actions Bot added fixes-issue examples size/S PR with diff < 50 LOC and removed fixes-issue labels May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples size/S PR with diff < 50 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix incorrect batch handling in _prepare_image_ids usage in train_dreambooth_lora_flux2_img2img.py

1 participant