← 返回 google 的题目列表ML Coding: Implement a Simplified Transformer Forward Pass
类型:online_judge
Implement a simplified Transformer block forward pass. You will be given the input tensor and weight matrices and must compute the output according to the specified formulas.
Task
Given:
Input sequence representation X (length T, hidden size D)
Single-head self-attention parameters: Wq, Wk, Wv, Wo
Feed-forward network (FFN) parameters: W1, b1, W2, b2
Optional attention mask (to block certain positions)
Implement the forward computation to produce output Y, typically:
Linear projections: Q = X Wq, K = X Wk, V = X Wv
Attention scores: S = (Q K^T) / sqrt(D) (apply mask before softmax if provided)
A = softmax(S)
H = A V
Z = H Wo
FFN: FFN(Z) = (relu(Z W1 + b1)) W2 + b2
Output: Y = FFN(Z)
The interviewer may specify whether residuals / layer norm / dropout are included; implement exactly the requested variant.
I/O (incomplete)
The shared experience does not provide a precise I/O interface. In the interview, follow the shapes and function signature given by the interviewer. Common convention:
Input: X with shape (T, D)
Output: Y with shape (T, D)
Constraints
T and D are small to medium (suitable for doc-based coding).
Examples (missing)
No reproducible numeric examples were provided.