BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Attention and transformers7 of 23 in topic

397. Split and merge attention heads

MediumAttention and transformers
Asked atAmazonAnthropicAppleByteDance

Multi-head attention runs several smaller attentions in parallel. The trick is all reshapes: split the model dimension into heads, attend per head, glue the heads back.

Write multihead_attention(x, Wq, Wk, Wv, Wo, n_heads) for x of shape (T, d) and square (d, d) weights. Steps:
1. Q = x Wq, K = x Wk, V = x Wv.
2. Reshape each to (T, n_heads, d_head) and move heads first: (n_heads, T, d_head).
3. Per head: softmax(Q K^T / sqrt(d_head)) V, no mask.
4. Move back to (T, n_heads, d_head), reshape to (T, d), multiply by Wo.

Functiondef multihead_attention(x, Wq, Wk, Wv, Wo, n_heads)

Example 1

Input: multihead_attention([[1.0, 0.0], [0.0, 1.0]], [[1.0, 0.0], [0.0, 1.0]], [[0.0, 0.0], [0.0, 0.0]], [[1.0, 0.0], [0.0, 1.0]], [[1.0, 0.0], [0.0, 1.0]], 2)

Output: [[0.5, 0.5], [0.5, 0.5]]

Constraints

  • x has shape (T, d)
  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 3 tests to be accepted.
Hint 1

reshape(T, h, dh).transpose(1, 0, 2) to split; transpose(1, 0, 2).reshape(T, d) to merge.

Hint 2

Use a stable softmax over the last axis.

Common mistakes
  • Reshaping straight to (h, T, dh), as the buggy version does, mixes features from different tokens into one head.
Understand the idea

Multi-head attention runs several smaller attentions side by side, each on its own slice of the features. The tricky part is only reshaping: each token's features must be cut into head-sized slices without mixing tokens. Reshape (T, d) to (T, heads, d_head) first, then move the head axis to the front.

Worked example

T = 2, d = 4, 2 heads, d_head = 2. Q = [[0, 1, 2, 3], [4, 5, 6, 7]]. Correct split: head 0 gets [[0, 1], [4, 5]] and head 1 gets [[2, 3], [6, 7]], so each head sees both tokens. Reshaping straight to (2, 2, 2) would give head 0 [[0, 1], [2, 3]], treating halves of token 0 as two tokens, which is wrong. Each head then computes softmax(Q K^T / sqrt(2)) V with no mask; afterwards move heads back to (T, heads, d_head), reshape to (T, d) and multiply by Wo. Every transformer layer in GPT, BERT and Llama does this split and merge.

Topics
Attention and transformers
Companies11
  • Amazon a candidate reported this question Multi-Head Attention From Scratch in NumPy — Amazon MLE Phone Screen | Screna AI
  • Anthropic a candidate reported this question Anthropic Senior SWE phone screen experience (Screna)
  • Apple a candidate reported this question Apple Machine Learning Engineer Interview | Screna AI
  • ByteDance a candidate reported this question TikTok Machine Learning Engineer Interview (questions, process, prep)
  • DeepMind the topic appears in interview reports AI Engineering Interview Questions Company Wise (Outcome School)
  • Google a candidate reported this question Google Interview Prep (Deep-ML)
  • Meta a candidate reported this question Meta Research Engineer Interview Guide (Dataford)
  • Mistral the topic appears in interview reports Mistral AI Interview Guide 2026: European Frontier Lab, Open-Weight Strategy, MoE, and La Plateforme
  • NVIDIA the topic appears in interview reports NVIDIA Interview Prep: Questions, Process & Study Plan
  • OpenAI the topic appears in interview reports OpenAI Research Engineer Interview Guide (Dataford)
  • Uber a candidate reported this question Uber Interview Questions — Page 13 | Screna AI

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • GPT decoder: causal self-attention for one headMedium
  • Build the ALiBi attention bias for every headMedium
  • Apply a key padding mask in batched attentionMedium
  • Bucket relative positions like T5Medium
All Attention and transformers problems
Report
397 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def multihead_attention(x, Wq, Wk, Wv, Wo, n_heads):
# your code here
pass
​
multihead_attention(_x, _Wq, _Wk, _Wv, _Wo, 2)

This case uses generated data, so it runs as written. Press + to add your own case.

Run checks these cases in your browser, no sign-in needed. Submit checks all 3 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS