×marble
all posts
May 30, 2026·12 min read

Transformers for Personalization: What They Actually Add Over CF

Transformers can replace collaborative filtering in many production rankers. When the upgrade is worth the cost, and when it isn't.

Alex Shrestha·Founder, ×marble

Transformers for Personalization: What They Actually Add Over CF

TL;DR.

  • Transformers for personalization replace the user-item matrix with a sequence of past interactions, so the model can condition on what the user did one click, one minute, and one week ago — not just an aggregated taste vector.
  • The two reference architectures are SASRec (Kang and McAuley, 2018, unidirectional self-attention with a shifted-sequence loss) and BERT4Rec (Sun et al., 2019, bidirectional attention with a Cloze masked-item objective).
  • A 2023 RecSys replication study, "Turning Dross Into Gold Loss" by Klenitskiy and Vasilev, found that once the training loss is held constant, SASRec beats BERT4Rec on quality and training time — so the historical "BERT4Rec wins" result is largely a loss-function artifact, not architecture.
  • A transformer recommendation engine is worth the GPU spend when the catalog is large (>10^5 items), sessions are long enough to show order, and intent shifts inside a session. CF still wins for tiny catalogs, sparse signal, or pure user-similarity use cases.
  • With an item-embedding cache and a slim transformer (4-6 layers, hidden 64-128), p99 inference latency for a top-k ranker lives in the tens of milliseconds, not seconds — even on commodity GPUs.

Most "transformers for personalization" posts dump the SASRec paper, gesture at BERT4Rec, and stop there. That isn't useful when you have to decide whether to keep your matrix-factorization service running or rewrite it. This post is the version we wish we had read before we shipped our first sequence-model ranker: what a transformer actually adds over collaborative filtering, what it costs in GPU dollars and engineering time, and the surprisingly clean test for whether your data is shaped right for it.

What transformers for personalization actually do differently from CF

The classic collaborative-filtering pipeline factorizes a sparse user-item matrix into two embedding tables. A user gets one vector. An item gets one vector. The dot product approximates the rating. That assumption — one stable taste vector per user — was already aging when Wang-Cheng Kang and Julian McAuley published SASRec in 2018. What they showed is that you can drop the user vector entirely if you have an ordered sequence of items the user has interacted with, then let self-attention compute, for each position, a query against earlier positions to predict what comes next.

The architectural pieces are the same ones that power language models: token embeddings (here, item embeddings), positional embeddings, multi-head self-attention, and a feed-forward block, repeated for a few layers. The loss is autoregressive — predict item t+1 from items 1..t. At inference, the last-position hidden state becomes the query against the catalog, scored by inner product against item embeddings.

What a transformer for personalization adds over CF is two things at once. First, sequence: it conditions on order, recency, and the specific items that came before, not just the aggregated history. Second, cross-attention between items: the model learns that "watched trailer for X" followed by "viewed reviews for X" is a different intent signal than "watched trailer for X" followed by "browsed unrelated category." CF cannot see either of those facts — it sees both events as ratings in the same row of the matrix.

SASRec, BERT4Rec, P5: the three transformer recommendation engine papers to read

Three papers cover most of what a working engineer needs to know about transformer recommendation engines. Each one made one specific contribution. None of them is the last word.

SASRec (Kang and McAuley, ICDM 2018). Self-attentive sequential recommendation was the first paper to show that a transformer decoder beats RNN and CNN sequential baselines on standard benchmarks (MovieLens, Amazon Beauty, Steam) while being "an order of magnitude more efficient" to train. SASRec is unidirectional — each position attends only to the past — and uses what the paper calls a shifted-sequence objective: at every position t, predict item t+1. The pairwise binary cross-entropy loss with one negative sample per positive is light to compute, which is why most production systems we have seen still start here.

BERT4Rec (Sun, Liu, Wu, Pei, Lin, Ou, and Jiang, 2019). Sequential recommendation with bidirectional encoder representations from transformer flipped two things at once: attention became bidirectional (each position sees past and future), and the loss became BERT-style masked-item prediction — the Cloze task. The authors knew that bidirectional attention would be trivially "predicting what you can already see," so the masking is the price of admission. BERT4Rec generally reports better numbers than SASRec across the same benchmarks in its own paper.

P5 (Geng, Liu, Fu, Ge, and Zhang, RecSys 2022). "Recommendation as language processing: a unified pretrain, personalized prompt and predict paradigm" reframed recommendation as natural-language generation. Every interaction, item description, and user attribute gets serialized to a text sequence, and a T5-style encoder-decoder is fine-tuned to handle sequential rec, top-k rec, explanation, review summarization, and rating prediction inside one model. P5 is not where most production rankers are today, but it points at where they are going — and it explains why teams care about transformer-based personalization beyond the SASRec / BERT4Rec axis.

The "BERT4Rec beats SASRec" claim, audited

We used to repeat this claim. We stopped after Klenitskiy and Vasilev's 2023 RecSys paper, "Turning Dross Into Gold Loss". They held architecture constant, swapped only the training loss, and found that SASRec trained with the same full-softmax cross-entropy that BERT4Rec uses beats BERT4Rec on quality and training time on every benchmark they tested. The headline finding: BERT4Rec's advantage was a loss-function effect, not an architectural one. So if you are picking between the two for production, SASRec with a well-chosen loss is the cleaner default.

Training cost reality for a transformer recommendation engine

The papers omit the part teams actually have to budget. Here is what we tell founders to plan for when scoping a transformer ranker training run.

A SASRec-shaped model with ~10^6 items, 4 layers, hidden dimension 128, sequence length 50, and a few hundred million training interactions fits on a single A10 or L4 GPU. End-to-end training to convergence is typically a handful of hours, not days. The Klenitskiy and Vasilev replication confirms the order of magnitude: SASRec with the better loss trained faster than BERT4Rec on every benchmark they ran.

The cost curve gets ugly fast when three things stack up: a huge item vocabulary (>10^7), long sequence lengths (>200), and full-softmax training. At that point you need sampled softmax, item-tower batching, or Transformers4Rec-style Merlin pipelines on multiple GPUs to keep wall-clock training time inside a workday.

The cost teams underestimate is not GPU hours — it is the feature pipeline. A transformer recommendation engine wants clean sequences. Cleaning event streams to deduplicate, drop bot traffic, sessionize correctly, and align timestamps across event sources is the work that consumes most of the timeline. We have seen teams burn six weeks on this before the first model run.

Inference latency: the item-embedding cache changes everything

The other thing the papers do not tell you is that a transformer ranker is bottlenecked by your retrieval and embedding strategy, not the transformer itself.

A SASRec-shaped model with 4 layers and hidden 128 over a session of 50 items runs a forward pass in single-digit milliseconds on a modern GPU. The cost is dominated by the final scoring step, which is a matrix multiply between the last-position hidden state and the full item-embedding matrix. For 10^6 items at hidden 128 in float16, that is a 1 x 128 × 128 x 10^6 GEMM — roughly 2.5 x 10^8 floating-point ops, also a single-digit-millisecond operation on a half-decent GPU.

The trick to keeping p99 latency in the tens of milliseconds is precomputing the item-embedding table once per model version and serving it from a memory-resident cache (Redis, in-process, or a dedicated embedding server). With the cache hot, end-to-end ranker latency for the transformer recommendation engine is dominated by network round-trip, not compute. Without the cache, you re-embed the full catalog every request and the math no longer works.

A standard production layout we have shipped: a two-tower retrieval stage that narrows 10^6 items to the top ~1000 candidates (ANN over item embeddings — see knowledge graphs vs vector embeddings for why the right index matters), then a transformer ranker that scores those 1000 against the session sequence, then post-filters and business rules. p99 for the whole stack tends to land under 50 ms with warmed caches.

When CF still wins (and you should not over-engineer this)

If you read papers all day, you would assume the only honest answer to "which model" is "use the newest one." It is not. Collaborative filtering still wins in several real cases, and we have talked teams out of transformer rewrites more often than into them.

Tiny catalog (<10^4 items). A transformer recommendation engine needs enough item-vocabulary variety for attention patterns to actually mean something. Below a few thousand items, an ALS or item-to-item CF model is faster to train, easier to debug, and produces comparable top-k quality. Save the GPU budget.

Sparse and short sessions. If your median session length is two events and most users have fewer than five lifetime interactions, the sequence model has nothing to attend to. You are effectively running CF with extra steps. This is the bucket where the cold-start problem and day-zero personalization matters more than the choice of ranker.

User-similarity use cases. "People similar to you also liked X" is a CF-native pattern. Transformers can express this, but a user-CF model trained on co-engagement matrices produces interpretable, explainable, debuggable output that beats a transformer on simplicity. Do not fix what is already working.

Strict latency budgets without a GPU. If you are CPU-only and need <10 ms p99, factorized models served from a vector store will outpace a transformer ranker. The latency math only works once you have GPU inference in your serving stack.

The crisp summary: a transformer ranker beats CF when the data has order, the catalog has scale, and you have the infrastructure to keep an item-embedding cache hot. Outside that, CF and hybrid approaches stay competitive. The recommendation engine vs personalization layer distinction matters here — a transformer is one model, not a strategy.

Hybrid is the real production answer for transformer-based personalization

The interesting production systems are not pure-transformer or pure-CF. They are a layered stack where each layer is the simplest model that could do its job.

A typical hybrid we have seen ship in 2026: a knowledge-graph layer for cold start and day-zero coverage (the gap CF has never fixed — see why collaborative filtering is aging), a CF/ALS layer for stable taste signal at the user level, a transformer ranker for session-level intent, and a contextual-bandit head for exploration and exploitation tradeoffs. Each component is replaceable. Each one has a metric that proves it is earning its compute. That is the reference architecture for real-time personalization we keep coming back to.

The benefit of going hybrid is also organizational. Three small teams can each own one layer instead of one team owning a monolith they all fear changing. The downside is that you now have a ranking-stack-shaped problem instead of a model-shaped problem, and ranking stacks have their own debugging pathologies — explainable recommendations and the path of a recommendation walks through why explainability gets harder as the stack deepens.

How ×marble fits in

×marble is the personalization platform we built in 2026 so engineering teams do not have to assemble this stack from scratch. Under the hood, we run a knowledge graph as the long-term memory layer, with sequence models on top for ranking and a contextual-bandit head for exploration. The graph handles cold start and explanations; the transformer-style ranker handles session intent; the bandit handles "should we show the safe choice or the new thing." Our sub-products surface that stack at different layers — Vivo for daily AI video briefings, Video for personalized YouTube, Marble x Music for streaming personalization. If you would rather not stand up your own transformer ranker, item-embedding cache, and graph backend, we built it as a product. The marketing engineer's personalization stack post walks through the integration shape.

FAQ

Are transformers better than collaborative filtering for recommendations?

Transformers beat collaborative filtering when the user signal has temporal order, the catalog is large (>10^5 items), and sessions are long enough that the order matters. The SASRec and BERT4Rec papers both showed gains over RNN, CNN, and pure-CF baselines on standard benchmarks like MovieLens, Amazon Beauty, and Steam. For small catalogs, sparse user histories, or pure user-similarity use cases, CF is still faster to train and serve.

What is the difference between SASRec and BERT4Rec?

SASRec is a unidirectional transformer decoder trained to predict the next item in a user's sequence from the items before it. BERT4Rec is a bidirectional transformer encoder trained on a Cloze-style masked-item objective borrowed from BERT. A 2023 RecSys replication study, "Turning Dross Into Gold Loss," found that once both models use the same training loss, SASRec actually beats BERT4Rec on quality and training speed — the historical gap was a loss-function effect, not an architecture effect.

How much does it cost to train a transformer recommendation engine?

A SASRec-shaped ranker over 10^6 items, 4 transformer layers, hidden dimension 128, and a few hundred million interactions trains on a single A10 or L4 GPU in a handful of hours. Costs scale roughly with item-vocabulary size, sequence length, and the choice of loss (full softmax is heavy; sampled softmax is light). The pipeline cost — cleaning event streams into proper sessions — usually dominates the GPU bill.

What is the inference latency of a transformer ranker in production?

With a warm item-embedding cache and a slim transformer (4-6 layers, hidden 64-128), end-to-end p99 latency for a top-k transformer ranker over 10^6 items lands in the tens of milliseconds, dominated by network round-trip and the final scoring GEMM. Without an item-embedding cache, the same stack will not meet a real-time budget — the cache is non-negotiable.

When should I not use transformers for personalization?

Skip transformers for personalization when the catalog is small (<10^4 items), when median session length is short (under five events for most users), when you need CPU-only sub-10ms inference, or when the core use case is "users similar to you also liked X." In those cases an ALS, item-CF, or two-tower retrieval model will match transformer quality at a fraction of the engineering cost.

Further reading

the product behind these notes

×marble is the personalization graph.

One API. A living knowledge graph per user. Day-zero ready, explainable by construction. We built it so you don't have to.

See ×marble