Einops for Tensor Reshaping
from einops import rearrange
# split heads: (B, T, d_model) -> (B, H, T, d_k)
Q = rearrange(Q, "b t (h d) -> b h t d", h=n_heads)
# merge heads
out = rearrange(attn_out, "b h t d -> b t (h d)")
Why it matters: The operation is readable at the call site — no mental bookkeeping of .view().transpose().contiguous() chains. Dimensions are named. Shape bugs drop to near zero.