Skip to content

Fix swapped tensor order in TransformerBlockSkipHook for SkipLayerGuidance#7

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-management-67b0
Draft

Fix swapped tensor order in TransformerBlockSkipHook for SkipLayerGuidance#7
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-management-67b0

Conversation

@cursor

@cursor cursor Bot commented Jun 22, 2026

Copy link
Copy Markdown

Bug and impact

TransformerBlockSkipHook (used by SkipLayerGuidance, PerturbedAttentionGuidance, and AutoGuidance) silently corrupted inference for dual-stream transformers such as SD3, Flux, and QwenImage.

When a layer is fully skipped (skip_attention=True, skip_ff=True, dropout=1.0), the hook returned (hidden_states, encoder_hidden_states) unconditionally. Blocks like JointTransformerBlock and FluxTransformerBlock actually return (encoder_hidden_states, hidden_states). Callers unpack in that order, so every subsequent block received swapped image/text tensors with no exception.

Root cause

The hook ignored TransformerBlockRegistry return indices (return_hidden_states_index, return_encoder_hidden_states_index) and always built the tuple in the wrong order for blocks where hidden_states is at index 1.

Fix and validation

  • Build the skipped-layer output tuple using registry indices (same approach as MagCacheBlockHook).
  • Added tests/hooks/test_layer_skip.py with a Flux-like block registered at index 1/0; verifies a skipped first block preserves correct (encoder_hidden_states, hidden_states) order through the forward loop.
python3 -m pytest tests/hooks/test_layer_skip.py -xvs
Open in Web View Automation 

When skipping dual-stream transformer blocks (JointTransformerBlock,
FluxTransformerBlock, QwenImageTransformerBlock), the hook returned
(hidden_states, encoder_hidden_states) regardless of each block's actual
return tuple order. Callers unpack as (encoder_hidden_states, hidden_states),
so SkipLayerGuidance silently swapped image and text tensors on skipped layers.

Build the skip output using TransformerBlockRegistry indices, matching
MagCacheBlockHook.

Co-authored-by: Simon Lynch <srlynch1@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant