Use split Q/K/V projections in attention - #7
Closed
apolinario wants to merge 1 commit into
Closed
Conversation
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>
There was a problem hiding this comment.
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
qkvprojection withto_q,to_k,to_vprojections. - Replace output projection
owith ato_outcontainer. - 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Replaces the fused
attention.qkv/attention.olinear layers inIdeogram4Attentionwith splitto_q/to_k/to_vandto_out, mirroring the diffusers loader change in huggingface/diffusers#13859 (commitfbe4750).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:
ideogram-ai/ideogram-4-nf4(PR: https://huggingface.co/ideogram-ai/ideogram-4-nf4/discussions/2)ideogram-ai/ideogram-4-fp8(PR: https://huggingface.co/ideogram-ai/ideogram-4-fp8/discussions/10)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
revisionif 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).