Implement and explain positional encoding

Quick Overview

This question evaluates knowledge of positional encoding mechanisms for Transformer language models, covering embedding mathematics, tensor shapes and broadcasting, PyTorch implementation details, expected training and inference symptoms when positional information is omitted, and methods for empirical verification and ablation.

Implement and explain positional encoding

Company: Applied Intuition

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Technical Screen

Implement positional encodings for a Transformer-based language model. Choose either sinusoidal or learned, show PyTorch code to compute and add them to token embeddings, explain the equations and tensor shapes, and integrate them into the model. Discuss expected symptoms if positional information is omitted and how you would verify the fix empirically.

Quick Answer: This question evaluates knowledge of positional encoding mechanisms for Transformer language models, covering embedding mathematics, tensor shapes and broadcasting, PyTorch implementation details, expected training and inference symptoms when positional information is omitted, and methods for empirical verification and ablation.

|Home/Machine Learning/Applied Intuition
Applied Intuition logo
Applied Intuition
Sep 6, 2025, 12:00 AM
mediumMachine Learning EngineerTechnical ScreenMachine Learning
14
0

Implement Positional Encodings for a Transformer Language Model

You are building a Transformer-based language model. Transformers are permutation-equivariant without positional information, so you must inject token order. Implement positional encodings and integrate them into a minimal PyTorch Transformer LM.

Requirements:

  1. Choose either sinusoidal or learned positional encodings (you may show both).
  2. Provide PyTorch code that:
    • Computes positional encodings.
    • Adds them to token embeddings with correct tensor shapes and broadcasting.
    • Integrates them into a simple Transformer-based language model.
  3. Explain the equations and tensor shapes involved.
  4. Discuss expected training/inference symptoms if positional information is omitted.
  5. Describe how you would verify the fix empirically (ablations, metrics, sanity checks).
Loading comments...