Senior interview questionAsked at ByteDance · interview write-up
Training a retrieval model over a catalogue of millions of items, you use sampled softmax. Why does it need a correction, and how do you choose the classification loss?
Model answer
A full softmax over millions of items is too costly, so each positive competes against sampled negatives. In-batch negatives appear in proportion to popularity, so popular items get over-penalised. The logQ correction subtracts each item's log sampling probability from its logit, which makes the sampled loss approximate the full softmax.
import torch
import torch.nn.functional as F
def in_batch_loss(u, items, log_q):
# u, items: [B, d]; row i of items is user i's
# positive; log_q: [B] log sampling probability
logits = u @ items.T - log_q[None, :]
labels = torch.arange(len(u), device=u.device)
return F.cross_entropy(logits, labels)
Loss: softmax cross-entropy when exactly one item is right among many; per-item binary cross-entropy for multi-label targets or calibrated click probabilities.
Practise the rest
40 ML system design questions, 492 in all, each with a model answer. Free.
More from ByteDance interview questions.
More ML system design interview questions
- You need to copy 500 GB of weights to 1,000 GPU workers over shared 10 Gbps links. What's the lower bound on time, and what would you build?
- Design an LLM coding assistant end to end: data, model and evaluation.
- Design a job scheduler for ML training and batch inference on a shared GPU cluster.
- Design an inference API that converts PDFs into Markdown using several models in stages.
- How can LLMs be incorporated into recommendation systems, from metadata enrichment and cold start to generative retrieval and semantic reranking?
- In the recall stage of a recommender, design a model that chooses how many candidates K to retrieve per request given latency and compute cost.