Skip to content

TransformerBlock instantiates CrossAttentionBlock even when with_cross_attention=False #8845

Description

@AhmedZaalouk122576

Describe the bug
Hi ,

I noticed that TransformerBlock always instantiates cross-attention layers even when with_cross_attention=False.

Current implementation:

self.with_cross_attention = with_cross_attention

self.norm_cross_attn = nn.LayerNorm(hidden_size)

self.cross_attn = CrossAttentionBlock(
    hidden_size=hidden_size,
    num_heads=num_heads,
    dropout_rate=dropout_rate,
    qkv_bias=qkv_bias,
    causal=False,
    use_flash_attention=use_flash_attention,
)

but in forward:

if self.with_cross_attention:
    x = x + self.cross_attn(self.norm_cross_attn(x), context=context)

So when with_cross_attention=False the cross-attention modules are never used but their parameters still appear ( registered) in model.parameters()

To Reproduce
With self.with_cross_attention =False ,

 
    for name, p in model.named_parameters():
        if p.grad is None:
            print("[NO GRAD] ", name)

[NO GRAD] encoder.vit.vit.blocks.1.norm_cross_attn.weight
[NO GRAD] encoder.vit.vit.blocks.1.norm_cross_attn.bias
[NO GRAD] encoder.vit.vit.blocks.1.cross_attn.out_proj.weight
[NO GRAD] encoder.vit.vit.blocks.1.cross_attn.out_proj.bias
[NO GRAD] encoder.vit.vit.blocks.1.cross_attn.to_q.weight
[NO GRAD] encoder.vit.vit.blocks.1.cross_attn.to_q.bias
[NO GRAD] encoder.vit.vit.blocks.1.cross_attn.to_k.weight
[NO GRAD] encoder.vit.vit.blocks.1.cross_attn.to_k.bias
[NO GRAD] encoder.vit.vit.blocks.1.cross_attn.to_v.weight
[NO GRAD] encoder.vit.vit.blocks.1.cross_attn.to_v.bias

Expected behavior
There should be no params registered for the cross attention module , if the transformer block doesnt use cross attention.

Activity

  1. added 5 commits that reference this issue on May 12, 2026
    f0103a9
    5ce030a
    1645458
    ff6d3e7
    374a9fa
  2. AlexanderSanin commented on May 18, 2026

    @AlexanderSanin
    Contributor

    Hi @AhmedZaalouk122576 , I've submitted a fix for this in #8860. It wraps the cross-attention layer creation in a conditional so CrossAttentionBlock and its LayerNorm are only instantiated when with_cross_attention=True. Happy to adjust if needed.

  3. added 9 commits that reference this issue on May 20, 2026
    b5111f3
    9469d64
    18ac108
    28741ed
    e30693a
    40e0d5b
    d9c5bef
    eb49911
    e94ebb2
  4. added a commit that references this issue on May 27, 2026
    e33941c
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions