Skip to content

StateManager, Hook, State: missing context setting (cond or uncond) in pipelines #12760

Open

Description

@toilaluan

In diffusion denoising model, we usually use classifier free guidance to control qualities. With State Manager, we want each context hold different state, so we have to set which context when calling self.transformer, here is a good example

But some popular pipelines (Flux2) or pipeline variants (Kontext, Image to image, Inpainting) don't implement this. For example, Flux T2I has it while Flux Kontext is missing

A Fix should be simple but require us to check all pipelines

with self.transformer.cache_context("cond"):
    noise_pred = self.transformer(...)
with self.transformer.cache_context("uncond"):
    neg_noise_pred = self.transformer(...)

Activity

  1. toilaluan commented on Dec 1, 2025

    @toilaluan
    ContributorAuthor

    By using a simple script that counts self.transformer( and self.transformer.cache_context occurrences, there are pipelines need to be fixed:

    src/diffusers/pipelines/omnigen/pipeline_omnigen.py
    src/diffusers/pipelines/kandinsky/text_encoder.py
    src/diffusers/pipelines/controlnet_sd3/pipeline_stable_diffusion_3_controlnet.py
    src/diffusers/pipelines/controlnet_sd3/pipeline_stable_diffusion_3_controlnet_inpainting.py
    src/diffusers/pipelines/unidiffuser/modeling_text_decoder.py
    src/diffusers/pipelines/unidiffuser/modeling_uvit.py
    src/diffusers/pipelines/chroma/pipeline_chroma_img2img.py
    src/diffusers/pipelines/chroma/pipeline_chroma.py
    src/diffusers/pipelines/flux/pipeline_flux_control_inpaint.py
    src/diffusers/pipelines/flux/pipeline_flux_controlnet_inpainting.py
    src/diffusers/pipelines/flux/pipeline_flux_controlnet.py
    src/diffusers/pipelines/flux/pipeline_flux_kontext_inpaint.py
    src/diffusers/pipelines/flux/pipeline_flux_control_img2img.py
    src/diffusers/pipelines/flux/pipeline_flux_fill.py
    src/diffusers/pipelines/flux/pipeline_flux_kontext.py
    src/diffusers/pipelines/flux/pipeline_flux_inpaint.py
    src/diffusers/pipelines/flux/pipeline_flux_img2img.py
    src/diffusers/pipelines/flux/pipeline_flux_controlnet_image_to_image.py
    src/diffusers/pipelines/flux/pipeline_flux_control.py
    src/diffusers/pipelines/stable_audio/pipeline_stable_audio.py
    src/diffusers/pipelines/bria/pipeline_bria.py
    src/diffusers/pipelines/aura_flow/pipeline_aura_flow.py
    src/diffusers/pipelines/flux2/pipeline_flux2.py
    src/diffusers/pipelines/cosmos/pipeline_cosmos2_text2image.py
    src/diffusers/pipelines/cosmos/pipeline_cosmos_video2world.py
    src/diffusers/pipelines/cosmos/pipeline_cosmos2_video2world.py
    src/diffusers/pipelines/cosmos/pipeline_cosmos_text2world.py
    src/diffusers/pipelines/sana/pipeline_sana_sprint_img2img.py
    src/diffusers/pipelines/sana/pipeline_sana_sprint.py
    src/diffusers/pipelines/sana/pipeline_sana.py
    src/diffusers/pipelines/sana/pipeline_sana_controlnet.py
    src/diffusers/pipelines/chronoedit/pipeline_chronoedit.py
    src/diffusers/pipelines/consisid/pipeline_consisid.py
    src/diffusers/pipelines/amused/pipeline_amused_inpaint.py
    src/diffusers/pipelines/amused/pipeline_amused_img2img.py
    src/diffusers/pipelines/amused/pipeline_amused.py
    src/diffusers/pipelines/lumina/pipeline_lumina.py
    src/diffusers/pipelines/cogview3/pipeline_cogview3plus.py
    src/diffusers/pipelines/cogview4/pipeline_cogview4_control.py
    src/diffusers/pipelines/easyanimate/pipeline_easyanimate.py
    src/diffusers/pipelines/easyanimate/pipeline_easyanimate_inpaint.py
    src/diffusers/pipelines/easyanimate/pipeline_easyanimate_control.py
    src/diffusers/pipelines/hidream_image/pipeline_hidream_image.py
    src/diffusers/pipelines/latte/pipeline_latte.py
    src/diffusers/pipelines/sana_video/pipeline_sana_video_i2v.py
    src/diffusers/pipelines/sana_video/pipeline_sana_video.py
    src/diffusers/pipelines/hunyuan_video/pipeline_hunyuan_skyreels_image2video.py
    src/diffusers/pipelines/hunyuan_video/pipeline_hunyuan_video_image2video.py
    src/diffusers/pipelines/hunyuan_video/pipeline_hunyuan_video_framepack.py
    src/diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2_i2v.py
    src/diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2.py
    src/diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2_diffusion_forcing_v2v.py
    src/diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2_diffusion_forcing_i2v.py
    src/diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2_diffusion_forcing.py
    src/diffusers/pipelines/kandinsky5/pipeline_kandinsky.py
    src/diffusers/pipelines/wan/pipeline_wan_video2video.py
    src/diffusers/pipelines/z_image/pipeline_z_image.py
    src/diffusers/pipelines/visualcloze/pipeline_visualcloze_generation.py
    src/diffusers/pipelines/dit/pipeline_dit.py
    src/diffusers/pipelines/prx/pipeline_prx.py
    src/diffusers/pipelines/controlnet_hunyuandit/pipeline_hunyuandit_controlnet.py
    src/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py
    src/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_img2img.py
    src/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_inpaint.py
    src/diffusers/pipelines/lumina2/pipeline_lumina2.py
    src/diffusers/pipelines/hunyuandit/pipeline_hunyuandit.py
    src/diffusers/pipelines/pixart_alpha/pipeline_pixart_alpha.py
    src/diffusers/pipelines/pixart_alpha/pipeline_pixart_sigma.py
    src/diffusers/pipelines/bria_fibo/pipeline_bria_fibo.py
    src/diffusers/pipelines/allegro/pipeline_allegro.py
    src/diffusers/pipelines/pag/pipeline_pag_hunyuandit.py
    src/diffusers/pipelines/pag/pipeline_pag_sd_3.py
    src/diffusers/pipelines/pag/pipeline_pag_sd_3_img2img.py
    src/diffusers/pipelines/pag/pipeline_pag_sana.py
    src/diffusers/pipelines/pag/pipeline_pag_pixart_sigma.py
    src/diffusers/pipelines/deprecated/vq_diffusion/pipeline_vq_diffusion.py
    

    Check Script

    #!/bin/bash
    
    # Usage: ./script.sh [directory]
    # If no directory is provided, it uses the current directory.
    
    directory="${1:-.}"
    
    find "$directory" -type f | while read -r file; do
        N=$(grep -I -o -F "self.transformer(" "$file" | wc -l)
        K=$(grep -I -o -F "self.transformer.cache_context(" "$file" | wc -l)
        if [ "$N" -ne "$K" ]; then
            echo "$file"
        fi
    done
  2. omkar-334 commented on Dec 14, 2025

    @omkar-334

    @DN6 @toilaluan can you review my PR

  3. github-actions commented on Jan 9, 2026

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

    Please note that issues that do not follow the contributing guidelines are likely to be ignored.

  4. removed
    staleIssues that haven't received updates
    on Jan 15, 2026
  5. github-actions commented on Feb 8, 2026

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

    Please note that issues that do not follow the contributing guidelines are likely to be ignored.

  6. fusheng-ji commented on Oct 5, 2026

    @fusheng-ji

    @toilaluan @DN6 I'd like to pick this up, since #12833 was closed without merging.

    On current main, enabling a stateful cache (TaylorSeer, FirstBlockCache, MagCache) on these pipelines still fails with ValueError: No cache context is set:

    • Flux: Kontext, Kontext Inpaint, ControlNet, ControlNet Inpaint, Control Inpaint, Img2Img, Inpaint
    • Chroma: text-to-image, Img2Img, Inpainting
    • Flux2
    • Wan video-to-video

    I'd wrap their transformer calls in cache_context("cond") / cache_context("uncond") the same way FluxPipeline does, and add a small test mixin that checks every transformer call runs inside a named context.

    I tested this locally on FLUX.1-dev, FLUX.1-Kontext-dev, Chroma1-HD, FLUX.2-dev and Wan 2.1 1.3B. Without a cache, the outputs are bitwise identical to main. With a cache, they now run instead of raising.

    I would leave out ControlNet SD3 and OmniGen, because their transformers don't support caching yet. The remaining pipelines from the list above could follow in separate PRs. #14900 also touches Flux Kontext, so I can rebase on it if it lands first.

    Does this scope work for you?

  7. sayakpaul commented on Oct 5, 2026

    @sayakpaul
    Member

    I think this works for us, but in general, we'd prefer smaller PRs so that it's easier to review.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions