Skip to content

Raise a clear error when an SDXL UNet is used without added_cond_kwargs - #15501

Open
jimburtoft wants to merge 1 commit into
huggingface:mainfrom
jimburtoft:sd-pipeline-reject-sdxl-unet
Open

jimburtoft wants to merge 1 commit into
huggingface:mainfrom
jimburtoft:sd-pipeline-reject-sdxl-unet

Conversation

@jimburtoft

Copy link
Copy Markdown

What does this PR do?

An SDXL UNet (addition_embed_type="text_time") can be loaded into StableDiffusionPipeline with no error. The pipeline then fails at the first denoising step with an unhelpful error:

File ".../unet_2d_condition.py", in get_aug_embed
    if "text_embeds" not in added_cond_kwargs:
TypeError: argument of type 'NoneType' is not iterable

The SD 1.x pipelines pass added_cond_kwargs=None (or a dict with only IP-Adapter embeds). The UNet's check assumes a dict, so instead of raising its own ValueError it hits a TypeError. #4649 and #6996 report this traceback. Both were closed without a code change.

This is easy to hit from the Hub: some repos ship an SDXL UNet with a model_index.json that names StableDiffusionPipeline. For example, jd09876/miaomiao-harem gets about 11k downloads a month.

This PR:

  • Fails early in the SD pipelines. StableDiffusionPipeline, StableDiffusionImg2ImgPipeline and StableDiffusionInpaintPipeline raise in __init__ when given a text_time UNet, and name the SDXL pipeline to use instead. This is next to the existing __init__ checks on unet.config.
  • Fixes the pattern at the model level. UNet2DConditionModel, UNetMotionModel, ControlNetModel, ControlNetUnionModel and UNetControlNetXSModel treat added_cond_kwargs=None like a missing key. That makes every other caller get the model's existing ValueError ("... requires the keyword argument text_embeds ...") instead of the TypeError. Other callers include ControlNet, PAG, LCM, depth2img, community pipelines and user code.

StableDiffusionPipeline.from_pipe(sdxl_pipe) now raises at construction. Before, it built a pipeline that failed at its first step. AutoPipeline, the XL pipelines and single-file loading are unaffected.

Tests

New tests:

  • test_rejects_sdxl_unet in the SD, img2img and inpaint pipeline tests (4 including TestStableDiffusionSimpleInpaintPipeline).
  • test_text_time_without_added_cond_kwargs in the UNet model tests.

All 5 fail on main and pass with this PR:

$ pytest tests/pipelines/stable_diffusion/test_stable_diffusion{,_img2img,_inpaint}.py tests/models/unets/test_models_unet_2d_condition.py -k "rejects_sdxl_unet or text_time_without_added_cond_kwargs"
main:     5 failed (4x "DID NOT RAISE ValueError", 1x "TypeError: argument of type 'NoneType' is not iterable")
this PR:  5 passed

Regression run on CPU: tests/pipelines/{stable_diffusion,stable_diffusion_xl,controlnet}/, tests/pipelines/pag/test_pag_sd{,xl}.py, tests/models/unets/test_models_unet_{2d_condition,motion,controlnetxs}.py and tests/models/controlnets/. All pass except test_stable_diffusion_inpaint, which fails the same way on main in my environment.

ruff check, ruff format --check and utils/check_copies.py are clean.

Self-review notes

I ran the self-review skill. Changes made after it:

  • Fixed the pattern at the model level, not only in 3 pipelines.
  • Shortened the error message to one sentence and named the matching XL class.
  • Added img2img and inpaint tests.

Left for review:

  • Whether to keep the pipeline-level __init__ error on top of the model fix. It is redundant for correctness, but it gives the actionable "use StableDiffusionXLPipeline" message at load time.
  • The other SD 1.x pipelines (depth2img, instruct_pix2pix, upscale, PAG-SD, ControlNet-SD, LCM, etc.) get the model-level error only. I did not add the __init__ check to them, to keep the diff small.

Fixes #4649
Fixes #6996

Who can review?

@yiyixuxu @asomoza

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant