Skip to content

Use split Q/K/V projections in attention - #7

Closed
apolinario wants to merge 1 commit into
ideogram-oss:mainfrom
apolinario:split-qkv-attention
Closed

Use split Q/K/V projections in attention#7
apolinario wants to merge 1 commit into
ideogram-oss:mainfrom
apolinario:split-qkv-attention

Conversation

@apolinario

Copy link
Copy Markdown

What

Replaces the fused attention.qkv / attention.o linear layers in Ideogram4Attention with split to_q / to_k / to_v and to_out, mirroring the diffusers loader change in huggingface/diffusers#13859 (commit fbe4750).

q/k/v are contiguous row-slices of the old fused weight, so this is mathematically identical — just a different on-disk layout.

Why

diffusers #13859 switched the canonical Ideogram 4 checkpoint layout to split q/k/v. The split-format weights are now published for the quantized repos:

This change makes the reference code load those split-format checkpoints. The quantized loaders (load_bnb4bit_state_dict, load_fp8_state_dict) are key-name agnostic, so no loader changes are needed.

Compatibility note

This is a clean cutover: the updated code expects split-format weights and will not load old fused checkpoints. Users on the previous code + previously-downloaded fused weights are unaffected until they upgrade; pin a pre-change weights revision if you need the old layout.

Verification

End-to-end generation with this code + the split nf4 weights produces correct images (nf4, bf16 activations, A100). Output is identical to the original fused code + fused weights for the same caption/seed (q/k/v are exact slices).

Replace the fused attention.qkv / attention.o linear layers with split
to_q/to_k/to_v and to_out, mirroring the diffusers loader change in
huggingface/diffusers#13859 (commit fbe4750). q/k/v are contiguous
row-slices of the old fused weight, so this is mathematically identical.

Loads the split-format checkpoints now published for
ideogram-ai/ideogram-4-nf4 and ideogram-ai/ideogram-4-fp8.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 4, 2026 00:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Refactors the attention projection layers in Ideogram4 by splitting the fused QKV projection into separate Q/K/V linear layers and renaming the output projection.

Changes:

  • Replace single qkv projection with to_q, to_k, to_v projections.
  • Replace output projection o with a to_out container.
  • Update attention forward pass to use the new projection modules.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

self.norm_q = Ideogram4RMSNorm(self.head_dim, eps=eps)
self.norm_k = Ideogram4RMSNorm(self.head_dim, eps=eps)
self.o = nn.Linear(hidden_size, hidden_size, bias=False)
self.to_out = nn.ModuleList([nn.Linear(hidden_size, hidden_size, bias=False), nn.Dropout(0.0)])
out = F.scaled_dot_product_attention(q, k, v, attn_mask=attn_mask)
out = out.transpose(1, 2).reshape(batch_size, seq_len, self.hidden_size)
return self.o(out)
return self.to_out[0](out)
Comment on lines +125 to +130
self.to_q = nn.Linear(hidden_size, hidden_size, bias=False)
self.to_k = nn.Linear(hidden_size, hidden_size, bias=False)
self.to_v = nn.Linear(hidden_size, hidden_size, bias=False)
self.norm_q = Ideogram4RMSNorm(self.head_dim, eps=eps)
self.norm_k = Ideogram4RMSNorm(self.head_dim, eps=eps)
self.o = nn.Linear(hidden_size, hidden_size, bias=False)
self.to_out = nn.ModuleList([nn.Linear(hidden_size, hidden_size, bias=False), nn.Dropout(0.0)])
Comment on lines +141 to +143
q = self.to_q(x).view(batch_size, seq_len, self.num_heads, self.head_dim)
k = self.to_k(x).view(batch_size, seq_len, self.num_heads, self.head_dim)
v = self.to_v(x).view(batch_size, seq_len, self.num_heads, self.head_dim)
@apolinario apolinario closed this Jun 4, 2026
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.

2 participants