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

394. Add token and position embeddings for a GPT input batch

EasyAttention and transformers
Asked atAmazon

Why it matters GPT-2 style models start every forward pass with this exact sum.

The first layer of a GPT model turns ids into vectors: one learned vector per token id plus one learned vector per position.

Write embed(ids, tok_emb, pos_emb). ids is an int array of shape (B, T). tok_emb has shape (V, d) and pos_emb has shape (L, d), where L is the maximum context length. Return tok_emb[ids] + pos_emb[:T], shape (B, T, d). If T > L, raise ValueError.

Inputs can be Python lists or NumPy arrays, so convert them with np.asarray first.

Functiondef embed(ids, tok_emb, pos_emb)

Example 1

Input: tok_emb = [[1, 0], [0, 1], [2, 2]], pos_emb = [[0.1, 0.1], [0.2, 0.2], [0.3, 0.3]], ids = [[2, 0]]

Output: [[[2.1, 2.1], [1.2, 0.2]]]

Example 2

Input: ids = [[0, 1, 1], [2, 2, 0]], tok_emb = [[1, 0], [0, 1], [2, 2]], pos_emb = [[0.1, 0.0], [0.0, 0.1], [0.5, 0.5]]

Output: [[[1.1, 0.0], [0.0, 1.1], [0.5, 1.5]], [[2.1, 2.0], [2.0, 2.1], [1.5, 0.5]]]

Constraints

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

Fancy indexing tok_emb[ids] gives (B, T, d); pos_emb[:T] broadcasts over the batch.

Hint 2

Indexing with an integer array gathers rows.

Hint 3

Positions are 0..T-1, not the ids.

Common mistakes
  • Indexing positions by token id.
  • Not checking that T fits in the position table.
Understand the idea

A token id is just a number, so the model gives each id a learned vector, like a dictionary entry. But "dog bites man" and "man bites dog" contain the same tokens, so the model also needs to know where each token sits. GPT-style models learn a second table with one vector per position and add the two.

Worked example

token table rows are [1,0], [0,1], [2,2], and position table rows are [0.1,0.1], [0.2,0.2]. The input is ids [2, 0]. Position 0 holds token 2: [2,2] + [0.1,0.1] = [2.1,2.1]. Position 1 holds token 0: [1,0] + [0.2,0.2] = [1.2,0.2].

x[b, t] = tok_emb[ids[b, t]] + pos_emb[t], valid only for t < L

This sum is the input to the first transformer block in GPT-2 and similar models. Newer models such as Llama drop the position table and rotate queries and keys instead.

Shapes and data flow
ids (B, T) -> tok_emb[ids] (B, T, d) + pos_emb[:T] (T, d) -> (B, T, d)
Topics
Attention and transformers
Companies1
  • Amazon a candidate reported this question Amazon Machine Learning Engineer Interview | Screna AI

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

Similar questions
  • Generate sinusoidal position encodingsEasy
  • Measure how focused each attention row is with entropyEasy
  • Count attention FLOPs for a sequenceEasy
  • Show that an embedding lookup is a one-hot matrix multiplyEasy
All Attention and transformers problems
Report
394 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def embed(ids, tok_emb, pos_emb):
# your code here
pass
​

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

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