mirror of
https://github.com/harvard-edge/cs249r_book.git
synced 2026-07-17 08:28:07 -05:00
[PR #1875] [CLOSED] fix(attention): mask reshape assumes square (seq_q==seq_k), breaks cross-attention #33428
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/harvard-edge/cs249r_book/pull/1875
Author: @Shashank-Tripathi-07
Created: 6/16/2026
Status: ❌ Closed
Base:
dev← Head:fix/attention-mask-reshape-cross-attention📝 Commits (10+)
c1531caMerge dev into main: workflow site_only standardization + book caption work + PR #1744 (ch1 PDF layout)e61c8eeUpdate contributors list [skip ci]6736f9dMerge dev into main: auto-tidy fix for book-format hooks on intro.qmd5f53073Merge remote-tracking branch 'origin/main'd562a28Merge dev into main: revert failed v0.7.0 bump + contributors sync2854c1dMerge remote-tracking branch 'origin/dev'f26abeb🔥 TinyTorch tinytorch-v0.2.0: Module clarity improvements for 04_losses (loss landscape ASCII art), 05_dataloader, 06_autograd. Pyproject sphinxcontrib-mermaid version bump.2e04a1fMerge dev into main: revert v0.7.0/v0.2.0 bumps after cancelled publish2169a1e🔥 TinyTorch tinytorch-v0.1.12: Module clarity improvements for 04_losses (loss landscape ASCII art), 05_dataloader, 06_autograd. Pyproject sphinxcontrib-mermaid version bump.df88ff7fix(ci): isolate dispatch/call runs in tinytorch+labs validate concurrency📊 Changes
5 files changed (+229 additions, -5 deletions)
View changed files
📝
.github/workflows/labs-validate-dev.yml(+5 -1)📝
.github/workflows/tinytorch-validate-dev.yml(+6 -1)➕
site/newsletter/posts/2026/2026-05-19_from-the-builders-gap-to-a-builders-stack.md(+106 -0)➕
site/newsletter/posts/2026/2026-05-22_team-newsletter-beyond-the-spiral-tinyml-pedagogy-at-the-hardware-edge.md(+108 -0)📝
tinytorch/src/12_attention/12_attention.py(+4 -3)📄 Description
What breaks
MultiHeadAttention.forward()reshapes a 3-D attention mask(batch, seq_q, seq_k)to(batch, 1, seq_q, seq_k)for multi-head broadcasting. The old code:The
_discardsseq_k. The reshape then usesseq_len_mask(which isseq_q) for both the query and key dimensions. For self-attention whereseq_q == seq_k, this accidentally works. For cross-attention (e.g. encoder-decoder attention, or attention with a different context length),seq_q != seq_k, so the reshape either:ValueErrorifseq_q * seq_q != seq_q * seq_k(total elements don't match)Fix
Unpack both dimensions explicitly. Correct for square and rectangular masks.
Test plan
pytest tinytorch/tests/12_attention/passes(B, T, T)still works(B, T_q, T_k)whereT_q != T_know works🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.