KatInterview

Designing Query, Key, and Value Vectors for Recommender Cross-Attention

Machine LearningModel answer
HardModel answerMachine Learningtransformersattentionrecommendation-systems

Last updated

Explain and contrast Query-Key-Value representations for cross-attention mechanisms in candidate-aware recommendation systems.

When building deep-learning recommenders (such as CTR models or video ranking systems), Transformer-style cross-attention is widely used to capture fine-grained interactions between user history, target items, and context.

Consider a model with the following available representations:

  • User Engagement History: An ordered list of $L$ historical items the user consumed, represented as embedding vectors of dimension $d$.
  • Candidate Item: A target item being scored for the user, embedded as a vector of dimension $d$.
  • Context: Additional features (e.g., time of day, location, current network condition) embedded into a vector of dimension $d$.

In this setup:

  1. Provide a primary formulation defining the Query ($Q$), Key ($K$), and Value ($V$) matrices for a cross-attention layer. Detail the semantic meaning and tensor shape for each.
  2. Formulate two distinct alternative architectures for setting up $Q$, $K$, and $V$. For every approach (including the primary one), explain:
    • What inputs map to $Q$, $K$, and $V$.
    • The specific interaction being modeled.
    • Trade-offs, computational complexity, and where in a recommendation pipeline (e.g., candidate generation vs. candidate ranking) it should be applied.
  3. Compare cross-attention over user history against running self-attention over user history. Explain why cross-attention is beneficial in production recommendation systems.

Hints

Hint 1

Think of attention as a soft dictionary lookup: Query is what you are searching for, Key is what you match against, and Value is what you retrieve.

Hint 2

Consider how candidate-dependent queries affect offline inference vs. online evaluation. Can a user embedding be cached if the Query depends on the target candidate?

Hint 3

Pay attention to tensor dimensions when setting history items or multi-field candidate features as Keys/Values—single-token Keys make softmax trivial.