KnowML

Designing Ad Click Prediction

Follow Zoya as she scrolls past a pair of running shoes on Instagram, and the bid Dev placed for them, down to the model that turns 'will she click?' into a probability an auction can price. We build it from the obvious version, fix each failure in turn, and look at what Meta and Google published along the way.

⏱ 84 min readAssumes: classical ML (02), recommenders (16), evaluation (25)
Start reading

Zoya is standing in a coffee queue in Bengaluru on a Saturday morning, thumbing through Instagram. A friend's trek photos go by, then a cousin's engagement announcement, and then, between two posts, a photo of a pair of green running shoes from a small shop in Kochi. She slows down, because she has been meaning to replace her old pair. She taps. Dev, the shop's owner, is asleep at that hour, and he will see a single line on a dashboard tomorrow: one more click, charged at some price he agreed to in advance.

Behind that tap there was a decision made in well under a second. Tens of millions of ads were eligible to be shown to Zoya, and exactly one of them had to be chosen for that slot. The choice depended on one number per ad that nobody could observe: the chance that Zoya, in particular, would click it. There's a second problem hiding inside the first. In the simplest version of the story the platform doesn't charge for showing an ad but when someone clicks it, so the money Meta expects to make from a slot is the advertiser's bid multiplied by that unobservable probability. Every ad in the auction is therefore priced and ranked with a guess, and if the guesses are wrong in the wrong way, the wrong ads win and the advertisers who deserved the slot pay the price. A system that produces such a guess for an ad and a person is a click-through-rate (CTR) predictor, and the guess itself is the predicted CTR, often written pCTR.

So the question this chapter answers is: how does Meta turn "will this person click this ad?" into a probability that is accurate enough to run an auction on, for billions of people, using only what they did before?

We'll build the design the way an engineer would, starting from the most obvious version, "the highest bid wins", and fixing each failure in turn. That takes us through why the auction needs calibrated probabilities and not merely good rankings, the metric that measures them, hashing billions of IDs into a model, boosted trees as a feature transform, online learning for freshness, the downsampling trick and the formula that undoes it, and the deep model Meta open-sourced in 2019. After that we follow the model into its 2023 to 2026 form as a very large foundation model, then through serving, labels and the feedback loop. We use Meta's and Google's own papers and posts, date every number, and say plainly where the internals are unpublished.

01What we're building, and how big

1.1 What it has to do

The job has two sides. For the person, an ad should be something they're glad to see, which at minimum means it's relevant and not a repeat of what they scrolled past an hour ago. For the advertiser, the system should deliver the action they're paying for, such as a click, an app install or a purchase, at a price that makes sense. Between the two sits the platform, whose revenue comes from charging advertisers, so it needs a mechanism that decides which ad gets each slot and what it costs. That mechanism is an auction, and the part of the system this chapter is about is the part that feeds it: the model that predicts how likely each action is.

We'll concentrate on click prediction, because it's the cleanest case and the one the 2014 Facebook paper studies, and mention conversions (a purchase or sign-up after the click) where they change the design. Four requirements shape everything. Predictions must be calibrated, which means that among all the impressions where the model says 2%, about 2% get clicked. They must be fresh, because new ads appear every minute and tastes drift. They must be fast, because the user is waiting for the feed. And they must handle a feature space with billions of distinct values, because user IDs, ad IDs and page IDs are the most informative inputs and the sparsest.

1.2 How big is it?

Meta has published scale numbers in several years, so we'll date each one. In 2014, the Facebook ads team wrote that the platform had over 750 million daily active users and over 1 million active advertisers (He et al., ADKDD 2014). By 2026, a Meta engineering post said the company's ads system serves 3.6 billion people who use at least one of its apps daily. Google's team described the same problem in 2013 as predicting on "billions of events per day" with a correspondingly large feature space (McMahan et al., KDD 2013). In December 2024, Meta wrote that its retrieval stage narrows "tens of millions" of candidate ads to "a few thousand" before ranking begins. A March 2026 post says ads must be returned with sub-second latency, with a bounded latency on the order of 100 milliseconds for the ranking model.

Meta doesn't publish its request rate or its impression volume, and neither does it say how many impressions are logged per day. That paper says only that a small fraction of one day's data "can have many hundreds of millions of instances" and that the actual number is confidential. We'll use clearly labelled illustrative numbers when we need volumes.

Predict before you read on

Suppose a platform logs one billion ad impressions a day, and 1% of them are clicked (both figures illustrative). How many clicks is that, and how many non-clicks? If we kept every click but only 5% of the non-clicks for training, how many training rows would remain, and what fraction of them would be clicks?

Show the answer

One billion impressions at 1% is 10 million clicks and 990 million non-clicks. Keeping 5% of the non-clicks leaves 49.5 million, so training would see 59.5 million rows, roughly 17 times fewer than the full day, and 10 million of them are clicks, a click rate of 16.8%.

Two consequences follow, and the rest of the chapter returns to both. Clicks are rare, so a model trained on this data sees 99 negatives for every positive, and most of the data carries little information. And the shortcut of throwing negatives away changes the click rate the model sees from 1% to 16.8%, so its raw output is no longer a probability we can use in an auction until we correct it.

These numbers also say something about labels. A click arrives seconds after an impression, a purchase can arrive days later, and the "no click" label doesn't arrive at all. There's no button a user presses to say they ignored an ad, so every non-click is the absence of a click for some period of time. We'll come back to how Facebook handled that, because it's one of the quieter hard problems in the whole system.

02Version 1: the highest bid wins

The obvious first design is a classic auction with no machine learning in it. Each advertiser states how much they will pay for a click. When Zoya's feed has a slot, the system collects the ads whose targeting matches her, sorts them by bid, shows the top one, and charges that advertiser when she clicks. Advertisers understand it, it's easy to build, and it's what a lot of early online advertising looked like.

Version 1: the highest bid wins
ADVERTISERS Dev, Asha, Rohan each state a bid SORT BY BID highest bid first no model involved SHOW THE WINNER Asha's Rs 50 ad in Zoya's feed MEERA scrolls past (no click) The platform is paid when someone clicks, and a bid is only a price per click. An ad nobody clicks earns nothing, however high its bid, and it still took the slot. We need to know how likely Zoya is to click each ad before we can price a slot.
Follow the arrows left to right. Every step is trivial to build, and the amber line under the boxes is where it loses money.

It breaks the moment you ask what the platform earns. Suppose Asha, who sells an online course, bids Rs 50 per click and Dev bids Rs 30. If Zoya's chance of clicking Asha's ad is 1% and her chance of clicking Dev's is 2%, then showing Asha earns the platform an expected 50 × 0.01 = Rs 0.50 per impression, and showing Dev earns 30 × 0.02 = Rs 0.60. Dev's lower bid is the better ad to show, for the platform and also for the viewer, who is twice as likely to care about it. Ranking by bid alone puts the weaker ad first, and it rewards advertisers who bid high on ads nobody wants.

A better rule ranks by the expected payment per impression, the bid times the probability of the action. That product is often called the expected value of the impression, or in the advertising literature the eCPM (expected revenue per thousand impressions, when scaled by 1,000). It needs one new ingredient, the probability, and for a first version we can estimate it from history without any model at all. Count how many times each ad was shown and how many times it was clicked, and divide.

How to rank adsPlusMinus
Highest bidTrivial, transparent to advertisersIgnores what the user wants and what the platform earns; rewards bidding on unclickable ads
Bid × historical CTR of the adNeeds only counts; already aligns bidder, user and platformA new ad has no history, a rarely shown ad has a noisy ratio, and the same ad has different CTRs for different people
Bid × predicted CTR from a modelUses the person, the ad and the context together; can score a new ad from its featuresNeeds a training pipeline, and the auction now depends on the probabilities being accurate

History-based CTR is the right first step, and it fails in three predictable ways. An ad shown ten times with one click has a CTR of 10%, and that's mostly noise. A brand-new ad has no CTR at all. And the average CTR of an ad tells you nothing about Zoya in particular, who clicks on running shoes far more than on cryptocurrency. All three point at the same remedy: learn a function from the features of the ad, the person and the context to a click probability. Varian's 2007 paper, and the one by Edelman, Ostrovsky and Schwarz, analyse the pay-per-click auction, in which the click probability is a central input, and the Facebook team says in 2014 that the efficiency of an ads auction "depends on the accuracy and calibration of click prediction". Calibration is the word that decides the rest of the design, so we look at why next.

03Why the auction needs calibrated probabilities, not just good rankings

A model that ranks ads well can still be useless to an auction. Suppose we sort Zoya's candidate ads by a score and the top-ranked one is the ad she's most likely to click. That's a ranking, and it can be perfect while the numbers attached are meaningless, for instance if every score is the true probability divided by ten. A recommender that only orders items doesn't care. An auction does, because it multiplies the score by a bid, and a bid is in rupees.

3.1 The auction in numbers

Here's one auction with three ads, using the numbers from before plus a third advertiser, Rohan, who sells phones. Each advertiser has a bid per click and the model gives a predicted CTR. For each ad the system computes a score, bid times pCTR, meaning the expected payment per impression, and picks the highest.

One auction, scored by bid x predicted click probability
advertiser bid / click pCTR score = bid x pCTR (expected Rs per impression) Dev (shoes) Rs 30 2.0% 0.60 Asha (course) Rs 50 1.0% 0.50 Rohan (phones) Rs 25 1.8% 0.45 Dev's price per click if he wins: runner-up score / own pCTR = 0.50 / 0.02 = Rs 25 (less than his Rs 30 bid). If the model wrongly says Dev's pCTR is 4% (true value 2%): he still wins, but pays 0.50 / 0.04 = Rs 12.5 per click. He now pays half the true price for each impression, because the price is built from the prediction. If every ad's pCTR were doubled, the ranking and the price per click would not change at all. Calibration errors that differ between ads are the dangerous ones.
Read each row left to right. Bars show the score, the product of the first two columns. Dev wins with 0.60 and pays what he needs to stay ahead of Asha.

Dev wins with 0.60. What should he pay? In the classic design, the generalized second-price auction that Google and Yahoo used for sponsored search and that Edelman and colleagues and Varian analysed in 2007, the winner pays the smallest amount that would still have kept the top spot. Dev stays ahead of Asha as long as his score stays above hers, 0.50, so he must bid at least 0.50 divided by his pCTR of 0.02, giving Rs 25 per click. He bid Rs 30, and he pays Rs 25. If we write the winner's score as and the runner-up's as , the price per click is

where is a bid, a predicted CTR, and the subscripts number the ranked ads. Notice the winner's own prediction in the denominator; that's where calibration enters. What the platform receives per impression is the true click probability times this price, . When the model is accurate about Dev, and the platform receives exactly the runner-up's score, 0.50. If the model overestimates Dev's CTR, saying 4% when the truth is 2%, then Dev still wins, but his price per click drops to Rs 12.50 and the platform receives 0.02 × 12.50 = Rs 0.25 per impression, half of what it should. A mispredicted ad is under-charged because its price is built from the prediction.

Different auction designs price differently, and the choice is a real decision.

Auction rulePlusMinus
First price: pay your bidEasy to explainAdvertisers must guess others' bids and shade their own; strategies get complicated
Generalized second price (GSP)Pay the least that still wins your position; simple; pioneered by Google and Yahoo! for sponsored searchNot truthful with several slots, so advertisers can profit by bidding below their value
VCG (Vickrey-Clarke-Groves)Truthful: bidding your real value is the best strategy; each winner pays the harm their presence does to the othersHarder to explain; revenue can be lower; pricing depends on predictions for every ad

For a single slot, which is the case in our example, GSP and VCG give the same price, the runner-up's score divided by the winner's pCTR. They diverge when there are several positions, as in a feed with many ad slots. Meta's advertiser documentation describes the winner as the ad with the highest "total value", built from the bid, the estimated action rate and an ad-quality term. How those three combine, and the pricing rule itself, aren't in the papers and posts we use, so we'll write about the structure and not about Meta's coefficients. Everything in this chapter holds for any rule of the form "score = a function of bid, predicted rate and quality".

3.2 Which calibration errors hurt

There's a subtlety that the numbers above hide. If the model overestimated every ad's CTR by the same factor, nothing would change. Ranking by comes out the same, and so does the price , because the factor cancels. Calibration matters because errors differ between ads. A model that's too optimistic about new ads, which have little data, hands them slots they haven't earned and charges them too little. A model that's noisy picks winners whose predictions happen to be too high. We can measure both with an experiment.

Here we simulate 100,000 auctions with six ads each. True CTRs and bids are random, and 30% of the ads are brand new. We compare five prediction rules by the true expected value of the ads they pick, as a fraction of the best possible choice. We also report two calibration ratios, the average predicted CTR divided by the average true CTR, once over all ads and once over the winners only.

Try it Which prediction errors cost an auction money, and the winner's curse
import numpy as np
rng = np.random.default_rng(3)
A, K = 100_000, 6                         # auctions, ads per auction
ctr = np.exp(rng.normal(np.log(0.01), 0.5, (A, K)))   # true CTRs
bid = np.exp(rng.normal(np.log(1.0), 0.4, (A, K)))    # per-click bids
new = rng.random((A, K)) < 0.3            # 30% of ads are brand new

def run(name, est):
    win = np.argmax(bid*est, axis=1)      # rank by bid x estimate
    i = np.arange(A)
    value = (bid*ctr)[i, win].sum()       # true expected value won
    calib_all = est.mean()/ctr.mean()
    calib_win = est[i, win].sum()/ctr[i, win].sum()
    print(f"{name:26} value {value/best:5.3f}  calib all "
          f"{calib_all:4.2f}  calib winners {calib_win:4.2f}")

best = (bid*ctr).max(axis=1).sum()
noise = np.exp(rng.normal(0, 0.4, (A, K)) - 0.4**2/2)  # mean 1
run("perfect estimates", ctr)
run("every estimate x2", ctr*2)
run("noisy, unbiased", ctr*noise)
run("new ads over-estimated x2", np.where(new, ctr*2, ctr))
run("new ads under-estimated /2", np.where(new, ctr/2, ctr))
Perfect estimates win the full value (1.000). Doubling every estimate gives the same 1.000 and the same winners, with calibration 2.00. Unbiased noise (calib all 1.00) wins only 0.912 of the value, and the winners' calibration is 1.24. Overestimating new ads by two times wins 0.929 with calibration 1.30 over all ads and 1.55 over the winners, and underestimating them by half wins 0.951 with winners at 0.95.

Doubling every estimate leaves the auction untouched, with a value of 1.000 and winners identical to the perfect model's. Noise that's unbiased on average, the second row, still costs 8.8% of the available value, because the model picks the wrong winner some of the time. Look at the calibration columns for something stranger. Over all ads the model is calibrated (1.00), yet among the winners the average prediction is 24% too high. This effect is the winner's curse: an ad wins an auction partly because its estimate was high by chance, so the winners are selected for positive errors. Overestimating new ads by a factor of two is worse still, 7.1% of value lost and winners 55% overestimated, and underestimating them costs less, because the model errs towards leaving the new ads out.

Winner's-curse calibration is the reason a calibration layer is applied to the slice of predictions that get used, and not only to the whole dataset. Google's 2013 paper describes exactly this: calibration is judged "on a slice of data", with correction functions applied per slice, and it adds that "the inherent feedback loop in the system makes it impossible to provide theoretical guarantees for the impact of calibration". We return to the feedback loop in section 12.

Why not just rank by a good AUC and ignore the absolute probabilities?

Ranking tells the auction which ad is better, and the auction also needs to know by how much. An ad with a 3% CTR and a bid of Rs 10 competes with an ad that has a 1% CTR and a bid of Rs 40 only if both probabilities are on the right scale. Price paid, how long an advertiser's budget lasts, and the choice between showing an ad and showing nothing all depend on the numbers and not only on their order.

Similar logic shows up wherever a model's output feeds a calculation: fraud scores that set a manual-review budget, ETA predictions that feed a price, and risk scores that set an interest rate all need probabilities that mean what they say.

04Measuring a probability

Before we improve the predictor we need a way to tell whether it improved, and the usual choices fall short here. Accuracy is useless when 99% of impressions aren't clicked, because a model that always says "no click" scores 99%. AUC, the chance that a random clicked impression is scored higher than a random unclicked one, is better, and it measures only ranking, so it can't see the scale errors section 03 just showed us. We want a number that rewards probabilities that are right and not merely well ordered.

4.1 Log loss, and why it rewards honesty

The standard answer is the log loss, also called cross-entropy. For one impression with label (1 for a click) and a predicted click probability , the loss is

If the impression was clicked, the loss is , which is small when we predicted a high probability and large when we predicted a low one. If it wasn't clicked, the loss is , small for a low prediction. Confident mistakes are punished severely, since grows without bound as . Here's the property that matters for an auction. Suppose the true click probability of some kind of impression is and we predict . The expected loss is

where is the entropy of the true outcome, a quantity we can't reduce, and is the Kullback-Leibler divergence, a measure of how far apart the two probabilities are, zero only when . So the expected log loss is lowest exactly when the prediction equals the truth. A loss with this property is called a proper scoring rule, and it's why training with log loss pushes a model towards calibrated probabilities, while a loss that only rewards ordering doesn't.

4.2 Normalized entropy

Log loss has a flaw as a report card: its value depends on how hard the problem is. If 1% of impressions are clicked, even a model that knows nothing but that fact has a small loss, because it can predict 1% every time. The Facebook team handled this by dividing by that baseline. Let be the background CTR, the average click rate in the training data. A model that predicts for every impression has log loss , the entropy of the background rate. The 2014 paper defines normalized entropy (NE) as the model's average log loss per impression divided by that number:

The numerator is the average log loss of the model and the denominator is the log loss of the know-nothing baseline. An NE of 1 means the model is no better than quoting the background rate, and lower is better. They also note that is the relative information gain. Why divide at all? Compute the baseline for two click rates. At a background CTR of 1%, nats. At 0.1%, . A model with a log loss of 0.0079 would be excellent in the first case and barely better than nothing in the second, and the ratio puts them on one scale. It makes numbers comparable across ad surfaces whose click rates differ by a factor of ten.

Facebook pairs NE with a second metric, calibration, the ratio of the average predicted CTR to the empirical CTR, equivalently expected clicks divided by observed clicks. A calibration of 1 is perfect, and the authors call it "very important" because accurate CTR prediction is essential to bidding and auctions. They also make a point that explains why NE is the main number and not AUC: NE "implicitly reflects calibration". If a model overpredicts by 2 and we multiply every prediction by 0.5, NE improves while AUC stays exactly the same.

Offline metricPlusMinus
AccuracyEasy to explainMeaningless at a 1% positive rate
AUCInsensitive to the base rate; judges ranking qualityBlind to calibration; a model that's ten times too confident has the same AUC
Log lossProper scoring rule; rewards calibrationDepends on the base rate, so numbers aren't comparable across surfaces
Normalized entropy + calibration ratioProper, comparable across click rates, and the calibration ratio localises the problemAverages over the whole dataset unless sliced by country, device, ad type

Facebook chose NE and calibration in 2014 and used them to report every result in the paper, always as relative numbers, such as "96.58% of the trees-only model's NE". They also say why they didn't use revenue directly: it wanted to isolate "the impact of the factors to the machine learning model". Google's team in 2013 used the same family of loss, logistic loss, and reports it by slice, and they also computed it as an online loss on data before training on it, so they could use all the data for both training and testing.

A change of 0.1% in NE sounds negligible, and it matters because the traffic is enormous. That paper says a typical feature-engineering experiment "shaves off a couple of tens of a percent" of relative NE, and that even small improvements are important at scale. A prediction that's 0.1% better in log loss is a tiny improvement per impression and a large one summed over billions of auctions, so these teams run many experiments and look at such small effects.

Proper scoring rules do the same job in weather forecasting, where the Brier score judges rain probabilities, and in language models, where perplexity is exponentiated log loss.

05Billions of IDs: hashing, inclusion, embeddings

We can now measure a model, so let's build the first real one: logistic regression over the features of the person, the ad and the context. Its input for Zoya's impression is a long list of facts. Her user ID, the ad's ID, the advertiser's page ID, her city, her phone model, the hour, the ad's category. Most of these are categorical features, which take one of a huge set of values and have no natural number to represent them. A logistic regression turns each into a one-hot vector, a vector the length of the number of possible values with a single 1, and learns one weight per value. An impression's score is the sum of the weights of the values present.

Unfortunately the most informative categorical features are also the biggest. There are billions of user IDs, and millions of new ad IDs appear every day, so a weight table with one entry per ID is billions of entries, grows without limit, and is full of IDs seen once. Storing the table is a memory problem, and so is looking values up at serving time, and so is keeping it up to date as new IDs arrive. Google's 2013 paper put the size of its problem plainly, as a model "with billions of coefficients".

5.1 The hashing trick

The first fix is to stop keeping a dictionary of IDs. Instead, choose a number of buckets , say , and give every feature value an index by hashing it: . Then the model keeps one weight per bucket. A brand-new ad ID needs no registration, since it hashes to some bucket immediately, and the memory is fixed at weights however many IDs exist. This is the hashing trick, or feature hashing.

Collisions are the cost: two IDs landing in the same bucket share a weight. With distinct values hashed uniformly into buckets, the chance that a given value shares its bucket with at least one other is . So when the number of values is several times the number of buckets, almost every ID collides with some other ID.

Predict before you read on

Ten million distinct ad IDs are hashed into 222 = 4,194,304 buckets. What fraction of ad IDs share a bucket with at least one other ID? Would you expect the model to be much worse than with a private weight per ID?

Show the answer

The ratio is roughly 2.38, so of IDs share a bucket. That sounds terrible, and it hurts far less than it sounds, because IDs aren't equally important. A few ads get most of the impressions and the rest are rarely seen. A popular ad's weight is dominated by its own data even when a rare ad shares its bucket, and the rare ad, which had too little data to learn a good weight anyway, mostly borrows the popular ad's value. Collisions between two heavy IDs are what hurt, and with a large those are rare.

That's why hashing works on skewed data. Think of the weight table as a fixed-size resource you can trade against accuracy: more buckets, fewer collisions, more memory. Meta probably hashes or otherwise bounds its ID tables, since an unbounded dictionary can't fit, but its published papers don't say whether its production models hash IDs, how many buckets they use or what scheme they use to avoid collisions between important features, and we won't guess. What they do say is that sparse embedding tables are the main memory cost of these models, up to a terabyte in 2026, which we'll meet in section 09.

5.2 Dropping features, shrinking weights

Hashing bounds the table size. Google's 2013 paper adds two techniques that cut memory further, and they're good examples of the engineering that surrounds the model. The first is probabilistic feature inclusion: most feature values appear only once or twice in the training data and teach the model little, so don't give them a weight until they've appeared a few times. Two methods were tested. Poisson inclusion adds a new feature to the model with some probability each time it's seen, so a feature is admitted after about sightings on average. Bloom-filter inclusion uses a rolling set of counting Bloom filters, compact structures that can report "seen at least times" with occasional false positives, to admit a feature after its th occurrence. Requiring two sightings through a Bloom filter saved 66% of the memory and cost 0.008% in AUC loss, and Poisson inclusion at saved 60% for 0.020%.

A second technique stores each weight in fewer bits. Nearly all the weights in these models lie between −2 and +2, and fine precision isn't needed, so Google stored them in a 16-bit fixed-point format, q2.13, with randomised rounding to keep the rounding error unbiased. They report no measurable loss and a 75% saving on the memory for coefficients.

5.3 From one weight to one vector

A single weight per ID can only say "this ID raises or lowers the click chance". A richer model gives each ID a short vector of numbers, an embedding, learned along with the rest of the model, so that IDs with similar behaviour end up with similar vectors and the model can compare them. A lookup in the embedding table for ID returns row of a matrix , the same as multiplying a one-hot vector by . Our sparse model so far is the special case . Section 09 builds the model that combines embeddings at scale, and first we need a way to capture how features combine.

Handling an ID featurePlusMinus
Dictionary, one weight per IDNo collisions; easy to inspectMemory grows with the ID space; new IDs need registration
Hashing into bucketsFixed memory; new IDs work immediatelyCollisions blur some IDs together; harder to debug
Probabilistic inclusion + fewer bits (Google, 2013)Cuts memory by 55 to 75% at a tiny accuracy costA feature has no weight until it's admitted; extra moving parts
Learned embeddings ()Similar IDs share statistical strength; vectors can be compared and combinedTables reach gigabytes to terabytes and need sharding across devices

Hashing categorical features into fixed-size tables is how text classifiers handle vocabulary, how spam filters handle word pairs, and how most large-scale click models in the literature handle IDs; It's available off the shelf in libraries such as scikit-learn's FeatureHasher.

Why not give every user a private embedding?

A table with a row for each of billions of users, at 16 to 64 numbers a row, is hundreds of gigabytes before counting the optimiser state, and most users have too little history to train their row. Hashing, probabilistic inclusion and shared low-dimensional vectors are all ways to spend the memory where the data is.

06Capturing interactions: boosted trees as a feature transform

A logistic regression adds up one contribution per feature, so it can't say that a young viewer who has clicked this advertiser before is far more likely to click than the sum of those two facts suggests. It needs the two features to be combined into one. By hand, the textbook ways to do that are binning a number into ranges (so the model can learn a step-shaped curve) and crossing two categorical features into one new one with the Cartesian product, then pruning the crosses that don't help. With hundreds of features that's a lot of hand work, and a lot of crosses that never get used.

The 2014 Facebook paper had a better idea. A boosted tree ensemble learns exactly these combinations automatically, so use the trees to build the features and let a linear model weigh them. Concretely, train a model of gradient-boosted decision trees (GBDT), a sequence of small trees where each one corrects the mistakes of the ones before. Then treat each tree as a categorical feature whose value is the index of the leaf an impression falls into. Each tree therefore contributes one active entry to a one-hot vector, and the linear classifier is trained on the concatenated vectors. In the paper's own example there are two trees, the first with three leaves and the second with two. An impression that lands in leaf 2 of the first and leaf 1 of the second becomes the vector [0, 1, 0, 1, 0].

The 2014 hybrid: boosted trees turn features into rules, a linear model weighs them
AD IMPRESSION user, ad and context features TREE 1 age < 25 and clicked this advertiser leaf 1 leaf 2 leaf 3 TREE 2 ad CTR last week > 2% leaf 1 leaf 2 0 1 0 1 0 ONE-HOT OF LEAVES 3 entries + 2 entries SPARSE LOGISTIC REGRESSION one weight per leaf, learned online -> p(click)
Follow one impression downward. In each tree it lands in exactly one leaf, so the linear model sees a short binary vector of fired rules.

6.1 What a leaf is

A path from the root of a tree to a leaf is a rule: "age below 25, and has clicked this advertiser before, and the ad's CTR last week above 2%". All the impressions that follow the same path share a leaf, so the leaf's one-hot entry says "this rule fired". Its weight in the linear model says how much the rule adds to the click log-odds, so fitting the linear model on the vector is learning weights for a set of rules the trees found. They describe it as "a supervised feature encoding that converts a real-valued vector into a compact binary-valued vector".

How are the trees themselves built? Each round, the boosting procedure computes for every training impression the gradient and the second derivative of the log loss with respect to the current model's raw score . With these are

where says which way each prediction should move and says how sure the loss is about that direction. A new small tree is grown to split the impressions so that the impressions in each leaf have gradients pointing the same way, and each leaf is given the Newton step over its members, where is a small regulariser. Then the score becomes with a small learning rate . Facebook uses the classic L2-TreeBoost form of gradient boosting, which fits each new tree to the residuals of the previous ones. Nothing in this procedure needs hand-built crosses, and the trees pick the features and thresholds that help.

We can see the effect on a small synthetic dataset where the label depends on feature interactions, a threshold on two features together and an exclusive-or of two others. Our experiment grows boosted trees by hand in numpy, then compares three models on held-out data, with NE reported relative to the trees-only model, as in the paper's table.

Try it Boosted trees, then logistic regression on their leaves
import numpy as np
rng = np.random.default_rng(5)

def make(n):            # 6 features, labels hinge on interactions
    X = rng.normal(size=(n, 6))
    z = (-3.0 + 2.5*((X[:, 0] > 0.3) & (X[:, 1] < -0.2))
         + 2.0*((X[:, 2] > 0) ^ (X[:, 3] > 0)) + 0.5*X[:, 4])
    return X, (rng.random(n) < 1/(1+np.exp(-z))).astype(float)

def grow(X, g, h, depth):         # one regression tree on Newton targets
    if depth == 0 or len(g) < 200:
        return -g.sum()/(h.sum() + 1.0)
    best = (0.0, None)
    for f in range(X.shape[1]):
        for t in np.quantile(X[:, f], [.2, .4, .5, .6, .8]):
            L = X[:, f] <= t
            gl, hl = g[L].sum(), h[L].sum()
            gr, hr = g[~L].sum(), h[~L].sum()
            s = gl**2/(hl + 1) + gr**2/(hr + 1) - g.sum()**2/(h.sum() + 1)
            if s > best[0]:
                best = (s, (f, t, L))
    if best[1] is None:
        return -g.sum()/(h.sum() + 1.0)
    f, t, L = best[1]
    return (f, t, grow(X[L], g[L], h[L], depth-1),
            grow(X[~L], g[~L], h[~L], depth-1))

def leaf_ids(tree, X):            # leaf index for every row
    out = np.zeros(len(X), int); nxt = [0]
    def walk(node, rows):
        if not isinstance(node, tuple):
            out[rows] = nxt[0]; nxt[0] += 1; return
        f, t, l, r = node
        m = X[rows, f] <= t
        walk(l, rows[m]); walk(r, rows[~m])
    walk(tree, np.arange(len(X))); return out, nxt[0]

def leaf_val(tree, X):
    out = np.zeros(len(X))
    def walk(node, rows):
        if not isinstance(node, tuple):
            out[rows] = node; return
        f, t, l, r = node
        m = X[rows, f] <= t
        walk(l, rows[m]); walk(r, rows[~m])
    walk(tree, np.arange(len(X))); return out

def boost(X, y, trees=40, depth=3, lr=0.3):
    F = np.zeros(len(y)); T = []
    for _ in range(trees):
        p = 1/(1 + np.exp(-F))
        t = grow(X, p - y, p*(1 - p), depth)
        F += lr*leaf_val(t, X); T.append(t)
    return T, lr

def raw(T, lr, X):
    return lr*sum(leaf_val(t, X) for t in T)

def onehot(T, X):
    cols = []
    for t in T:
        ids, k = leaf_ids(t, X)
        M = np.zeros((len(X), k)); M[np.arange(len(X)), ids] = 1
        cols.append(M)
    return np.hstack(cols)

def lr_fit(A, y, steps=60, l2=1.0):
    A = np.c_[np.ones(len(A)), A]; b = np.zeros(A.shape[1])
    for _ in range(steps):
        p = 1/(1 + np.exp(-A@b))
        g = A.T@(p - y) + l2*b
        H = (A*(p*(1 - p))[:, None]).T@A + l2*np.eye(len(b))
        b -= np.linalg.solve(H, g)
    return b

def lr_pred(b, A):
    return 1/(1 + np.exp(-(np.c_[np.ones(len(A)), A]@b)))

def ne(y, p):
    p = np.clip(p, 1e-6, 1 - 1e-6)
    ll = -np.mean(y*np.log(p) + (1 - y)*np.log(1 - p))
    c = y.mean()
    return ll/-(c*np.log(c) + (1 - c)*np.log(1 - c))

Xa, ya = make(60_000); Xb, yb = make(60_000)    # train, test
T, lr = boost(Xa, ya)
p_tree = 1/(1 + np.exp(-raw(T, lr, Xb)))
p_lr = lr_pred(lr_fit(Xa, ya), Xb)
Ta, Tb = onehot(T, Xa), onehot(T, Xb)
p_hyb = lr_pred(lr_fit(Ta, ya), Tb)
ref = ne(yb, p_tree)
for name, p in [("trees only", p_tree), ("LR on raw features", p_lr),
                ("trees -> leaves -> LR", p_hyb)]:
    print(f"{name:24} NE {ne(yb, p):.4f}  relative {ne(yb, p)/ref:7.2%}")
print("leaf features:", Ta.shape[1])
Relative to the trees-only model (NE 0.7831), logistic regression on the raw features is at 118.69% (NE 0.9294), and the hybrid, which feeds the 320 leaf indicators to a logistic regression, is at 99.10% (NE 0.7760).

A logistic regression on the raw features, with no interactions, is 18.7% worse in NE than the trees, because the label here is mostly interaction. Meanwhile the hybrid, a logistic regression over 320 leaf features (40 trees, up to eight leaves each), beats even the trees it was built from, by 0.9%. In the Facebook paper's real data the direction is the same, with different sizes: LR plus trees reached 96.58% of the trees-only NE and logistic regression alone 99.43%, so the combination won by more than 3.4% relative to plain LR. In that paper's words, a typical feature-engineering experiment gains a couple of tenths of a percent, so a gain of that size is large. They also observe that "LR and Tree models used in isolation have comparable prediction accuracy", and that it's their combination that "yield an accuracy leap".

6.2 Why two models and not one

The hybrid has a practical reason beyond accuracy. Boosted trees train in batch, and the paper says building a model of hundreds of trees from hundreds of millions of instances "may take more than 24 hours" on a single core, and a few hours with enough cores and memory. Cheap to train, the linear layer on top can learn online, impression by impression. So the trees, which change slowly, can be retrained daily, while the linear layer keeps up with the latest minutes. Section 07 shows why that matters. This design also keeps models compact: the paper varies the number of trees from 1 to 2,000 with at most 12 leaves each, and nearly all the NE gain comes from the first 500 trees, with the last 1,000 adding less than 0.1%. Prediction time grows with the number of trees, so they stay small.

What goes into the trees? Facebook separates contextual features, which depend only on the current request (the device, the page, the hour), from historical features, which summarise earlier interactions (an ad's CTR over the last week, a user's average CTR, the count of clicks on this ad). Historical features turned out to dominate. All of the top 10 features by importance were historical, and among the top 20 only two were contextual, even though historical features made up roughly three quarters of the feature set. Dropping the contextual features cost less than 1% of NE, and dropping the historical ones cost 4.5%. Contextual features still matter in one case. For a new user or a new ad, with no history, they're the only signal, so they're indispensable for cold start.

Model structurePlusMinus
Logistic regression, hand-made crossesFast, online, easy to inspectInteractions come from engineers' guesses
Boosted trees aloneFinds interactions and thresholds automaticallyBatch training; slow to adapt; large at inference if many trees
Trees as a feature transform + online LR (2014)Automatic interactions with an online layer; NE gain of 3.4% over plain LRTwo models to keep in sync; trees must be retrained when features change
Deep network with embeddingsLearns feature representations; scales with dataHeavy memory and compute; hard to train fresh

Facebook chose the hybrid in the 2014 paper. Its engineers published a deep model, the subject of section 09, in 2019, and a November 2024 engineering post describes replacing DLRM-style architectures with sequence-learning ones, the subject of section 10. The same trick, a tree ensemble as a learned encoder for a simpler model, is common wherever tabular data meets a need for fast updates, and using a tree ensemble as a learned encoder for a simpler model is a general technique for tabular data.

07Staying fresh: online learning and the joiner

Dev launched his shoe campaign at nine this morning. By noon his ad has been shown to tens of thousands of people and the model should already know whether it's working. A model trained last night has seen none of those impressions. Facebook's team measured how much that costs. They trained a model on one day of data and tested it on each of the next six days, and the NE got steadily worse as the gap grew. For both the boosted trees and the hybrid, retraining daily and not weekly improved NE by roughly 1%.

A daily retrain is the cheapest way to get fresher, and the paper takes it further. If the trees change slowly and the linear layer is cheap, the linear layer can be trained online: update it on each labelled impression as it arrives, and publish the new weights to the ranker periodically.

7.1 One learning rate per feature

Online learning raises a question that batch training hides: how big should each update be? In stochastic gradient descent, every weight moves a step of size times its gradient. A single global rate that decays over time is the textbook choice, and it's a bad one for sparse features. A thought experiment from Google's 2013 paper shows why. Suppose we're estimating the head probability of ten coins with logistic regression, and each round one coin is flipped and we update that coin's weight. A coin that's been flipped a thousand times needs small steps. A coin flipped twice needs large ones. A global schedule that shrinks over time shrinks the step for the rare coin too, even in rounds when it wasn't flipped.

One fix gives each weight a learning rate that depends on how much that weight has been updated. For weight at step , the per-coordinate rate used in the Facebook paper (proposed in Google's 2013 paper) is

where is the gradient for weight at step , and and are two constants (the paper uses , ). That sum of squared gradients grows only when feature is present in an impression, so a popular feature's rate shrinks quickly and a rare feature keeps a large rate until it has data. Facebook compared five schedules for the logistic regression layer. Per-coordinate rates won, with an NE almost 5% lower than the worst, a per-weight rate that shrinks as . A global rate did poorly for the reason in the coin example, and the paper says it failed because it decreases the rate for features with few instances "too fast".

Google's team in 2013 combined the same per-coordinate rates with an update rule called FTRL-Proximal, which also produces sparse models, with many weights exactly zero. For each weight it keeps two numbers: , an accumulated gradient adjusted for the step sizes already taken, and , the sum of squared gradients. A weight is computed from them when needed:

Here and are the L1 and L2 regularisation strengths. While the accumulated evidence stays below the weight is exactly zero, and a zero weight needs no memory, so a model with billions of possible coefficients fits in RAM. Once the evidence crosses the threshold, the weight is the regularised value above. Underneath it's the same Adagrad-like idea as the learning rate, with an L1 penalty that works properly in the online setting.

How the linear layer learnsPlusMinus
Batch retrain, weeklySimple; can reuse the same job as the treesStale: about 1% NE worse than daily in the 2014 test
Online SGD, one global learning rateFreshRare features get their rate decayed by traffic they never saw; clearly worse NE
Online SGD, per-coordinate ratesFresh; best NE of the five schedules tested; model is a single weight per featureNeeds the joiner infrastructure below
Bayesian online probit regression (BOPR)Same NE within 0.2%; gives a full predictive distribution, useful for explore/exploitTwo numbers per weight, so twice the memory and two dot products per prediction

Facebook chose per-coordinate online logistic regression in 2014 and noted that BOPR's NE was within 0.18% of it, so the extra uncertainty estimate came at a cost in model size. Adagrad, the same idea applied to deep networks, is the optimiser DLRM's authors used in 2019 for their accuracy experiments.

7.2 Where do the labels come from?

To learn online, the trainer needs a stream of examples, each with its features and its label, arriving seconds after the impression. That's harder than it sounds, because the two halves of an example are born in different places at different times. The impression, with all the features the ranker used, gets logged when the ad is shown. Zoya's phone sends the click later, if at all. And there's no event for "didn't click".

The online loop: impressions wait for their click, then train the model
RANKER scores ads for Zoya's request shown ONLINE JOINER FIFO queue + hash map keyed by request ID waits a fixed window MEERA'S TAP click stream (x, y) TRAINING STREAM click or no-click label attached TRAINER per-coordinate online LR new model published to the ranker periodically (cadence unpublished) If the click stream stalls, every impression leaves as a non-click, the trainer sees CTR near zero, and predictions collapse. Too short a window loses late clicks; too long a window delays training and holds more impressions in memory.
Follow the amber arrows. An impression sits in the joiner until its window ends, then leaves as a click or a non-click. The pink note is the failure the 2014 paper warns about.

The 2014 paper describes an experimental piece of infrastructure it calls the online joiner. Every time the feed refreshes, a request ID is generated, and each ad shown, with its features, is added to an impression stream under that ID. Clicks flow into a click stream carrying the same ID. This joiner is a distributed stream-to-stream join built on a HashQueue: a first-in-first-out queue that holds impressions for a fixed window, with a hash map for quick lookup by request ID. When a click arrives it finds its impression in the map and labels it positive. Only when an impression's window expires without a click is it released as a negative example. Next the labelled stream goes to the trainer, which publishes models back to the ranker, closing what the authors call "a tight closed loop".

That waiting window is a real tradeoff. If it's too long, training data is delayed and the joiner has to hold more impressions in memory. If it's too short, some clicks arrive after their impression has already been released as a negative, so the click coverage, the share of clicks successfully joined, falls and the empirical CTR is biased low. With windows that need manageable memory, the authors report the bias is "decimal points of a percentage", and that it can be measured and corrected. That tension is sharper for conversions, which can arrive days after an impression, and Meta's 2023 Lattice post says engagement can span seconds for a click to days for a purchase.

They also warn about failure. If the click stream becomes stale because of some data infrastructure issue, the joiner emits training data with a very small or zero CTR, and the online trainer "will begin to incorrectly predict very low, or close to zero probabilities of click". Because the expected value of an ad depends on its click probability, the system may then show fewer ads at all. Their suggestion is anomaly detection that automatically disconnects the trainer from the joiner when the real-time data distribution changes abruptly.

Only the linear layer learns online, because trees are rebuilt from scratch in a batch job that takes hours, and there's no cheap way to update a tree's structure from one impression. A linear layer over fixed leaves is just a weight per leaf, and one impression changes a handful of those weights. This split puts the slow, expensive part on a daily schedule and the fast, cheap part on a continuous one.

Streaming joins that attach late labels to earlier events show up in fraud detection (chargebacks arrive weeks later), in recommendations (a watch completes minutes after it starts) and in A/B testing; Google's Photon system, cited by the 2014 paper, does the same for its ad clicks.

08Too much data: downsampling negatives, and the formula that undoes it

Dev's ad is one of more than a million advertisers' ads, and a fraction of one day's impressions already holds hundreds of millions of rows. Training on all of it is expensive, and most of those rows are non-clicks that carry little information each. An obvious way to cut cost is to keep a random fraction of the rows.

Facebook tested this in 2014. With uniform subsampling, using 10% of the data gave an NE only 1% worse than using all of it, and calibration didn't change. Uniform sampling is attractive because the model trained on the sample can be used as it is. A second method keeps every click and only a fraction of the non-clicks, negative downsampling. They tried and found the best performance at . Negative downsampling can both speed up training and improve the model, and it leaves a trap, because the data the model learns from no longer has the click rate the world has.

Negative downsampling and the way back
LOGGED IMPRESSIONS 1,000,000 rows 10,000 clicks (1.0%) KEEP all 10,000 clicks 5% of 990,000 others = 49,500 rows TRAIN 59,500 rows click rate 16.8% predicts p RECALIBRATE q = p / (p + (1 - p) / w) w = 0.05 Why it works: the sample keeps every click but only a fraction w of non-clicks, so the odds of a click are inflated by 1/w. Example: the model says p = 0.168 in the sampled world. True odds = w x p/(1-p) = 0.05 x 0.202 = 0.0101. So q = 0.0101 / 1.0101 = 0.0100, the 1% we started with. Skip the last box and every ad looks about seventeen times more clickable than it is.
Illustrative numbers: a 1% click rate and 5% of non-clicks kept. The model learns in the middle box's world, and the right-hand box undoes the shift before anyone sees a probability.

8.1 Undoing the shift

If the real click rate is 0.1% and we keep 1% of the non-clicks, the sampled data's click rate becomes roughly 10%, and a well-trained model will predict values near 10%. Fed to an auction, every ad would look roughly a hundred times more clickable than it is. Facebook's fix is a one-line formula, , and it's easy to derive. Work in odds, the ratio of the probability of a click to the probability of no click. In the real data a click has probability , so the odds are . In the sample every click is kept and only a fraction of the non-clicks, so the click is relatively times more common among the rows the model sees, and the odds it learns are

Here is the model's output in the sampled world, is the true click probability we want, and is the fraction of non-clicks kept. Solving the second equation for gives

which is the paper's formula. In words, multiply the model's odds by the fraction of negatives kept and convert back to a probability. Take the example from the diagram: and . Here the odds are 0.168/0.832 = 0.202, times 0.05 is 0.0101, and , the original 1%. Because the formula is monotone in , it never changes the ranking of ads, and that's why AUC doesn't notice whether it was applied.

Now the experiment. We generate two million impressions with a 2% click rate, train a logistic regression by Newton's method on all of them, then train another on a sample that keeps every click and 5% of the non-clicks, then apply the recalibration to the second model's outputs. We report calibration, NE and AUC on two million fresh impressions.

Try it Downsample negatives, then recalibrate with q = p / (p + (1 - p) / w)
import numpy as np
rng = np.random.default_rng(7)

def make(n):
    X = rng.normal(size=(n, 4))
    z = -5.0 + 1.2*X[:, 0] + 0.8*X[:, 1] - 0.6*X[:, 2]
    return X, (rng.random(n) < 1/(1+np.exp(-z))).astype(float)

def fit(X, y, steps=12):
    A = np.c_[np.ones(len(X)), X]
    b = np.zeros(A.shape[1])
    for _ in range(steps):                   # Newton steps
        p = 1/(1+np.exp(-A@b))
        H = (A*(p*(1-p))[:, None]).T@A
        b -= np.linalg.solve(H, A.T@(p-y))
    return b

def predict(b, X):
    return 1/(1+np.exp(-(np.c_[np.ones(len(X)), X]@b)))

def ne(y, p):
    ll = -np.mean(y*np.log(p) + (1-y)*np.log(1-p))
    c = y.mean()
    return ll/-(c*np.log(c) + (1-c)*np.log(1-c))

def auc(y, p):
    r = np.argsort(np.argsort(p)) + 1
    n1 = y.sum(); n0 = len(y) - n1
    return (r[y == 1].sum() - n1*(n1+1)/2)/(n1*n0)

Xtr, ytr = make(2_000_000)
Xte, yte = make(2_000_000)
w = 0.05                                     # keep 5% of negatives
keep = (ytr == 1) | (rng.random(len(ytr)) < w)
print(f"rows {len(ytr):,} -> {keep.sum():,}, "
      f"CTR {ytr.mean():.4f} -> {ytr[keep].mean():.4f}")
p_full = predict(fit(Xtr, ytr), Xte)
p_down = predict(fit(Xtr[keep], ytr[keep]), Xte)
q = p_down/(p_down + (1 - p_down)/w)         # recalibration
for name, p in [("full data", p_full), ("downsampled", p_down),
                ("downsampled + recal", q)]:
    print(f"{name:20} calib {p.mean()/yte.mean():5.2f}  "
          f"NE {ne(yte, p):.4f}  AUC {auc(yte, p):.4f}")
Negative downsampling cuts the two million training rows to 137,499 and raises the click rate from 0.0197 to 0.2866. The downsampled model's calibration is 9.89 and its NE is 2.7173, while its AUC is 0.8560, exactly the full-data model's. After q = p / (p + (1 - p) / w) the calibration is 1.01 and the NE is 0.7883, the same as the model trained on all two million rows (1.01, 0.7883, AUC 0.8560).

The sample is 14.5 times smaller (137,499 rows against two million), and its click rate rose from 2.0% to 28.7%. Its calibration is 9.89, so its probabilities are almost ten times too high, and its NE is 2.72, worse than the 1.0 of a model that knows nothing. Its AUC is 0.8560, identical to the full model's, because the ranking is intact. After the recalibration, calibration is 1.01, NE is 0.7883, and both match the model trained on everything. So the trick saves roughly 93% of the rows and costs nothing measurable here, as long as the correction is applied. A ranker whose AUC is fine can still be wrong by a factor of ten in the auction.

8.2 Reweighting instead of recalibrating

Google's 2013 paper takes a different route to the same end. It subsamples at the level of the search query: it keeps every query where at least one ad was clicked, and a fraction of the queries where none was. To keep predictions unbiased, each kept example in a no-click query gets an importance weight that scales up its loss and gradient, and everything else gets weight 1. Because the sampling probability is known, the expected weighted loss of the sample equals the loss on the full data: for an event sampled with probability , its expected contribution is . They report that even fairly aggressive subsampling of unclicked queries has "a very mild impact on accuracy".

Cutting the training dataPlusMinus
Uniform subsamplingNo correction needed; 10% of the data cost 1% NE in 2014Throws away clicks, the rare and valuable rows
Negative downsampling + recalibration (Facebook, 2014)Keeps every click; best NE at ; faster trainingRaw output is not a probability; the correction must be applied exactly and consistently
Subsample with importance weights (Google, 2013)Output is calibrated from the start; unbiased in expectationLarge weights raise the variance of gradient updates; sampling by query keeps related ads together

Facebook downsampled and recalibrated. Google reweighted. Both rest on the same fact, that the sampling rate is under the engineer's control and therefore known exactly, so the shift can be undone with arithmetic instead of estimation. Rare-event problems in fraud detection, medical diagnosis and anomaly detection use the same pair of moves, and the fraud-detection case study in this series (chapter 42) meets it again.

Why would throwing away data improve the model?

Facebook reports it as an empirical finding: the best NE appeared at a downsampling rate of 0.025, not at the full dataset. It doesn't give a mechanism, so we shouldn't claim one as fact. A plausible reading, which is ours and not the paper's, is that with a 99 to 1 imbalance the gradient from many easy negatives swamps the few informative positives, and rebalancing gives positives a louder voice per training step. Either way, the lesson is that the data volume that suits the model is a number to tune, and not "all of it".

09Going deep: embeddings, interactions and DLRM

The hybrid of section 06 has two limits. Trees only see features that engineers prepared, such as a user's click count over the last seven days, and the ID features are handled by a single weight each. A neural network can learn its own representation of each ID, as section 05 sketched, but it needs a design for categorical data, because the networks used for images and text expect dense numbers. In 2019 Meta published and open-sourced its answer, the Deep Learning Recommendation Model, DLRM (Naumov et al., arXiv, May 31, 2019).

Its authors describe DLRM as a union of two lines of work. One comes from recommender systems, where matrix factorization represents each user and each item as a vector and predicts a rating as their dot product. Predictive analytics supplies the other, where where models for click prediction moved from logistic regression to networks that turn categorical values into embeddings. DLRM takes the embedding idea from both and adds an explicit way to combine them.

9.1 The four steps

DLRM: embeddings for IDs, a small MLP for numbers, dot products in between
DENSE FEATURES ad age, past CTR, hour... USER ID categorical AD ID categorical PAGE ID categorical INTEREST categorical BOTTOM MLP numbers -> vector (d) EMBEDDING table lookup (d) EMBEDDING table lookup (d) EMBEDDING table lookup (d) EMBEDDING table lookup (d) INTERACTION dot product of every pair: n(n-1)/2 numbers append the dense vector TOP MLP layers of 512, 256 ... -> one number SIGMOID -> pCTR Embedding tables hold most of the parameters (GBs each); MLPs hold the compute.
Read bottom to top. Every sparse feature and the processed dense vector become vectors of the same length d, and the interaction layer takes every pairwise dot product.

Walk the diagram from the bottom. First, every categorical feature, such as the user ID, the ad ID, the page ID or an interest, is looked up in its own embedding table and becomes a vector of length . Second, the numerical features, such as the ad's age or its CTR last week, go through a small multilayer perceptron, the bottom MLP, which outputs a vector of the same length . Now every input is a vector in the same space. Third comes the step that gives the model its character, the interaction layer. It takes the dot product of every pair of those vectors. For vectors there are pairs, and each dot product is a single number measuring how much the two vectors agree. Fourth, those numbers are concatenated with the processed dense vector and fed to a second MLP, the top MLP, whose single output goes through a sigmoid and becomes the predicted CTR.

Take a toy case with . Zoya's user vector is and the shoe ad's vector is . Their dot product is , a large positive number meaning "these two point the same way". An ad vector of gives . During training, the embeddings move so that users and ads that tend to produce clicks end up with high dot products, the matrix-factorization idea carried over to every pair of features, with the top MLP deciding how much each pair matters.

9.2 Why dot products, and what they cost

The interaction layer descends from the factorization machine (Rendle, 2010), which adds second-order terms to a linear model: . Here is the sparse feature vector, a weight per feature, and an embedding per feature, so the weight on the pair isn't a free parameter but the dot product of two low-dimensional vectors. That factorisation is what lets the model learn a pair's effect for combinations never seen together, as long as each feature was seen with others. DLRM uses the same pairwise dot products, between whole embeddings, and passes them through an MLP, where the factorization machine adds them up linearly.

Cost is quadratic in the number of vectors but linear in . The public Criteo ad datasets in the paper's accuracy experiments have 13 continuous and 26 categorical features. That makes 27 vectors, 351 pairs, and with a 16-number dense vector a 367-wide input to the top MLP. They argue explicitly against going further: other designs such as Deep and Cross or xDeepFM build higher-order terms, and they write that interactions "beyond second-order... may not necessarily be worth the additional computational/memory cost". On the Criteo Kaggle data, a DLRM and a Deep and Cross network sized to about 540 million parameters each reached similar accuracy, with DLRM slightly ahead and without much tuning.

Predict before you read on

Take a DLRM-style model with 26 embedding tables, each with 10 million rows of 16 numbers stored as 4-byte floats (illustrative sizes; Meta doesn't publish its production tables). How much memory do the embedding tables need? Compare with an MLP whose layers are a few hundred units wide. What does that say about how to spread the model across GPUs?

Show the answer

26 × 10,000,000 × 16 × 4 bytes is roughly 16.6 GB for the embeddings. An MLP with a few layers of 512 units has well under a million weights, a few megabytes. Embeddings hold almost all the parameters, and MLPs hold almost all the arithmetic, since each example touches only a handful of rows from each table but pushes a dense vector through every layer.

That mismatch drives the training design. So the paper spreads the embedding tables across devices (model parallelism), because copying a multi-gigabyte table to every GPU is out of the question. It replicates the MLPs and splits the batch (data parallelism), because they're small and compute-heavy. Combining the two needs an all-to-all exchange of embedding vectors, which the authors implement with a "butterfly shuffle". In 2021 a follow-up from Meta (Mudigere et al.) reported training such models with up to 12 trillion parameters, at a 40 times speedup over earlier systems.

Combining features in a neural CTR modelPlusMinus
Concatenate embeddings, then an MLPSimplest; can in principle learn any interactionMLPs learn dot-product-like interactions inefficiently and need much data
Factorization machine (sum of pairwise dot products)Explicit second-order terms with linear cost in ; few parametersShallow; no higher-order or non-linear structure
DLRM (pairwise dots, then an MLP)Explicit pairs plus learned non-linearity; open-source reference; scales with embedding sizePairs only; quadratic in the number of features; huge embedding tables
Cross networks (Deep and Cross, xDeepFM)Explicit higher-order interactionsMore parameters and compute for gains DLRM's authors judged marginal

Meta chose DLRM in 2019, published it as a benchmark for hardware and software co-design, and kept building on factorization-machine ideas. Its 2025 GEM post says the model extends an architecture called Wukong built from stacked factorization machines with cross-layer attention, the same pairwise-interaction idea scaled up. That pattern, embeddings for IDs plus an interaction step plus an MLP, is shared by Wide and Deep, DeepFM and most large-scale ad and feed models, which makes DLRM a good template to know.

Every embedding has the same length because the interaction layer takes dot products, and a dot product needs two vectors of equal length. That's also why the numerical features go through a bottom MLP whose output is exactly numbers long. It brings them into the same space as the embeddings, so they can interact with the categorical features as equals.

10From features to sequences to a foundation model

DLRM answered the question of how to combine features, and it left another open: where the features come from. Look at what the historical features of section 06 are. "Number of ads Zoya clicked in the last seven days" is an aggregate that a person decided to compute, and it throws away what Zoya did in what order. Meta's engineers made the same point in November 2024, and their post lists the costs of hand-built aggregates. They lose the order of a person's events, they lose the details attached to each event once events are counted together, they depend on engineers' intuition about which aggregates matter, and they pile up overlapping variants that raise storage and compute costs.

10.1 Events, not aggregates

The fix described in that post is to feed the model the raw sequence. Each event, such as an ad click or a purchase, becomes an event-based feature, "a single coherent object that captures all key information about an event", with attributes like ad category and a timestamp. An event model turns each into an embedding with a time encoding to preserve recency and order, and a sequence model reads the whole history. Attention over a long sequence is quadratic in its length, and the post says multi-head attention pooling cuts the cost from to , where is the maximum sequence length and is a tunable number. Meta reports that this gave "2–4% more conversions on select segments" and that the work was extended with jagged-tensor kernels and a Jagged Flash Attention module, since users' histories have different lengths.

We should be careful about what the post claims: it reports conversion gains on selected segments and gives no click-through numbers, and the baseline and the experiment design aren't published.

10.2 One model for many surfaces and goals

A second kind of fragmentation lived in the model fleet. Over the years Meta had built a separate predictor for each surface, such as Feed, Stories and Reels, and each objective, such as clicks, video views and purchases. Meta's May 11, 2023 post on Meta Lattice says these had been "numerous smaller, siloed models", with hundreds of models separately trained, served and tuned. Lattice replaced many of them with one model that learns across datasets and goals. It's built on a transformer backbone as what Meta calls a Deep and Hierarchical Ensemble Network, with trillions of parameters trained on hundreds of billions of examples from thousands of data domains. It reports that joint optimisation gave about an 8% improvement in ads quality, and that early Instagram results showed knowledge shared across Feed, Stories and Reels. It also names two ideas that are easy to miss: handling delayed feedback by giving the model temporal awareness, since engagement ranges from seconds to days, and balancing many objectives with Pareto optimality and techniques such as MetaBalance so that engineers needn't hand-tune weights for thousands of domains and tens of objectives.

10.3 A model that teaches the others

The next step is the one with the most interesting tradeoff. On November 10, 2025, Meta described its Generative Ads Recommendation Model, GEM, as the largest foundation model for recommendation in the industry, trained on thousands of GPUs. GEM does not serve most traffic itself. Meta says it has hundreds of user-facing vertical models (VMs), the models that score ads inside the latency budget, and GEM's role is to improve them through post-training knowledge transfer. In direct transfer, GEM passes knowledge to the vertical models trained on the same data. In hierarchical transfer, it distils into domain-specific foundation models which then teach the vertical ones. That post also describes a Student Adapter, a lightweight component that refines the teacher's outputs using recent ground-truth data so the student isn't trained on stale supervision.

Distillation is a technique with simple arithmetic. A big teacher model produces a probability for each example, and a small student is trained to match it as well as the real label :

where is the student's prediction, is the log loss (the second term uses the teacher's probability as a soft label), and sets how much to trust the teacher. Soft labels from a teacher carry information a 0/1 click doesn't, such as "this is a 4% impression and not a 0.1% one". Meta hasn't published GEM's exact transfer objective in the posts we use, and the formula above is the textbook form, not a description of GEM. Soft labels matter for the auction as well: if the teacher is calibrated, a student that matches it inherits calibrated probabilities, and if the teacher's output goes stale, the student's does too, the problem the Student Adapter is described as addressing.

Meta reports results in terms it chose. Meta's 2025 post says that after GEM's launch, ad conversions rose 5% on Instagram and 3% on Facebook Feed in the second quarter, and that architecture changes in the third quarter doubled the gain from each added unit of data and compute. In October 2026 a follow-up post said GEM now runs as a single central model trained across Meta's ad platforms, that the central model brought an 18.9% conversion improvement and a 12% click-through improvement across surfaces, and that conversion uplift over the past year for targeted users was 5.8% on Facebook, 4.1% on Instagram and 1.6% off-site. It also introduced a new architecture called Kunlun and a transfer method called LoopFM, which Meta says doubles transfer effectiveness at no added compute or latency. These are Meta's own numbers. None of the posts states the baselines, test populations or measurement periods behind them, so they show direction and scale and can't be reproduced or compared.

Organising the model fleetPlusMinus
A separate model per surface and objectiveEach can be tuned for its own latency and data; failures stay localHundreds of models to train, serve and monitor; small surfaces have little data
One multi-domain, multi-objective model (Lattice, 2023)Shares knowledge across surfaces; fewer models; helps new surfaces with little dataOne model must meet every surface's latency; objectives can conflict
A foundation model that teaches vertical models (GEM, 2025)Giant model trained offline with no serving limit; small models stay fast; knowledge flows to all of themThe student is limited by serving cost; transfer can go stale; a distillation pipeline to maintain

Meta's fleet design changed twice in two years: from siloed models to one unified model in 2023, and from a unified model to a large teacher with many students in 2025. Serving budget, covered in section 11, seems to be what kept it from being a single model all the way, though no post says so directly. That budget also moved. In March 2026 the same team described an Adaptive Ranking Model that serves models of LLM scale inside the real-time limit, and the October 2026 post describes the central model's gains continuing to flow to the user-facing models. Teacher-student systems of this kind are standard wherever a model too slow to serve still has knowledge worth keeping, from speech recognition to search ranking.

Why not just serve the biggest model on every request?

Each request scores thousands of ads inside a latency limit of roughly a tenth of a second for the ranking model, and the traffic is enormous, so the cost per ad scored multiplies into a very large compute bill. A model that's too slow doesn't produce a better ad, it produces a late page. Distillation spends the expensive compute once, offline, and lets the cheap models inherit what it learned. Meta's 2026 serving work shows the balance can shift as hardware and software improve.

11Serving: one request through the funnel

We have a model, or a family of them, and Zoya's feed is waiting. Section 01's arithmetic returns in a new form: the model that gives the best predictions can't score every eligible ad, so the system narrows the field in stages. Meta's December 2024 post on Andromeda, its ads retrieval system, puts numbers on the first stage: retrieval takes tens of millions of ad candidates down to a few thousand, and ranking, with larger models that predict value for the person and the advertiser, picks the final ads. Facebook's 2014 paper describes the same shape, a cascade of classifiers of increasing computational cost, and says its own model is the last one.

From every eligible ad to the one Zoya sees
ELIGIBLE ADS tens of millions targeting + budget + policy filters RETRIEVAL a few thousand cheap score each, index + model RANKING predicts pCTR and other action rates (stage sizes unpublished) AUCTION bid x rate + quality -> winner Ranking has to finish inside a sub-second budget (Meta, 2026) while the same user signals serve every ad in the request. Retrieval's job is recall: do not lose the ad that would have won. Ranking's job is calibrated probabilities. The He et al. (2014) paper describes a cascade of classifiers of rising cost, and studies only the last stage.
Each stage looks at far fewer ads, so it can spend far more computation on each. Counts in the first two boxes come from Meta's 2024 retrieval post; the others are unpublished.

Retrieval has a different job from ranking. It must not lose the ad that would have won, so its measure is recall, and Andromeda reports +6% recall on selected segments along with +8% ad quality. It does this with a hierarchical index in which ads are organised in layers, so inference visits only the most relevant nodes and the cost grows sub-linearly with the number of ads, and Meta says the index and the retrieval model are trained jointly, which it contrasts with two-tower networks plus nearest-neighbour search, the design in the YouTube chapter. Ranking's job is calibrated probabilities for a few thousand ads, using rich features of the person, the ad and the pair.

Here is one request, with the details that are published and a flag where they aren't. Meta doesn't publish a per-stage latency breakdown, only the totals below.

  1. RequestZoya scrolls and the feed asks for an ad slot. It fetches her recent activity and context. Which stores hold these and how long the fetch takes is unpublished.
  2. RetrieveEligibility rules, such as targeting, budget and policy, plus the retrieval model reduce tens of millions of ads to a few thousand (Meta, December 2024). Latency for this stage isn't published.
  3. RankThe ranking model predicts click and conversion rates for each candidate. Meta's March 2026 post says the system returns ads with sub-second latency and a bounded latency of roughly 100 ms for the model, and that user signals are computed once per request and shared across all candidates, not recomputed for each ad.
  4. CalibratePredictions pass through whatever correction keeps them calibrated. In 2013 Google applied a calibration layer with per-slice correction functions, and Meta probably still applies something similar, though its posts don't describe a calibration layer.
  5. AuctionEach ad's total value is computed from its bid, its estimated action rate and its quality. Dev's ad, in our story, has the highest value and wins, and a price is computed. Neither the formula nor the pricing rule is published.
  6. LogThe impression is logged with its features and a request ID. If Zoya taps, a click event with the same ID goes to the click stream, and the joiner of section 07 attaches the label.

11.1 Computing once per request, not once per ad

The step that most shapes ranking cost is the sharing in step 3. Most of what the model knows about the request is about the person: her recent events, her embeddings, the processed sequence. Only the ad side differs between candidates. A model that recomputes the person's part for each ad pays for it a few thousand times, and one that computes it once and reuses it pays once. With illustrative costs, if the person's part costs 20 units and each ad's part costs 1, then scoring 2,000 ads costs 2,000 × 21 = 42,000 units one way and 20 + 2,000 = 2,020 the other, roughly twenty times less. Meta's 2026 post describes this under the name request-oriented computation sharing, with an in-kernel broadcast that shares the request-level embeddings across candidates inside the GPU kernel, and long user sequences processed once per request. It reports the model's complexity at on the order of 10 GFLOPs per token, the figure for top-tier LLMs, a scale of around one trillion parameters, 35% model-FLOPs utilisation, and, since launching on Instagram in the fourth quarter of 2025, a 3% increase in ad conversions and a 5% increase in click-through rate for targeted users. Embedding tables there approach terabyte size and are sharded across GPU cards.

How to score the candidatesPlusMinus
Run the whole model once per candidate adSimplest; any architecture worksThe person's part is recomputed thousands of times
Cheap model for everyone, expensive model for a short listCost falls with each stage; the 2014 cascadeAn early mistake can't be repaired later; stages must agree
Compute the request's part once and broadcast it (2026)Lets much larger models fit in the budgetNeeds architectures built to split into a request part and an ad part

Facebook's choice in 2014 was the cascade, and in 2026 the sharing design, and the two aren't exclusive: retrieval and ranking are the cascade, and sharing makes the last stage affordable. Facebook's 2014 paper has a simpler version of the same cost thinking. Prediction time depends on the number of trees, so the paper measured how much accuracy each extra tree buys and stopped adding them when the gain fell below 0.1%. Search engines, feeds and marketplaces all use the same shape of cascade, because scoring everything is never affordable.

12Training data, labels and the feedback loop

Every stage so far learns from logs, and the logs record what an earlier version of the system chose to show. This section looks at what that does to the labels and to the auction.

Start with the label. A click is an event, but a non-click is the absence of one, so each impression is held in the joiner until its window ends, and the label is "no click yet" frozen at that time. For purchases the window problem is worse. Meta's 2023 post says engagement spans seconds to days, and a model trained on the labels available at the window's end sees a purchase rate that's biased low for the newest impressions. That paper reports the bias for clicks is small and measurable, and it suggests it can be corrected. Meta doesn't say how it handles the conversion case beyond building temporal awareness into the model.

12.1 Only shown ads get labels

The deeper problem is selection. An ad that never wins an auction never gets shown, so it never gets a click or a non-click, so the model never learns that it was good. Dev's brand-new shoe ad starts with no history. If the model gives it a pessimistic prediction, it loses the auction, gets no impressions, and the prediction never has a chance to be corrected. Section 03's winner's curse is the other side: ads that did win are over-predicted, so the data on winners overstates their quality.

A standard remedy is to explore, showing some ads mainly to learn about them. To do that the model needs a measure of how uncertain it is. Facebook's 2014 paper notes that its Bayesian online model, BOPR, outputs a full predictive distribution over the click probability, so percentiles of that distribution can drive explore-and-exploit schemes, an advantage over plain logistic regression, whose output is a single number. For a simple version, give each ad a Beta distribution over its click rate. An ad shown 100 times with 1 click has a posterior with mean 2/102, roughly 2.0%, and a standard deviation near 1.4 percentage points. An ad with 1,000 clicks in 100,000 impressions has a mean of 1.0% and a standard deviation of roughly 0.03 points. The new ad's plausible range is wide and the old ad's is narrow, so an auction that scores the new ad by an optimistic quantile of its distribution gives it a chance, and once it has data the quantile tightens towards the mean. These numbers are illustrative, and the technique is called Thompson sampling when it draws a random sample from the posterior instead. Meta probably explores in some form, since an ad with no history can't otherwise win its first impressions, but how much its auction explores is unpublished.

Treating a new adPlusMinus
Score it by its predicted CTR from features aloneNo extra machineryA pessimistic guess becomes permanent; data never arrives
Boost its score by an optimism bonus that shrinks with dataGives new ads a chance; self-correctingCosts some revenue while exploring; needs uncertainty estimates
Sample from the posterior (Thompson sampling)Principled; explores in proportion to uncertaintyRandomness makes outcomes harder to reproduce and debug

Google's 2013 team is blunt about the larger loop. Calibration fixes, they write, can't come with theoretical guarantees because "the inherent feedback loop in the system" means that today's model determines tomorrow's training data. Meta's posts describe the loop operationally: GEM is "continuously trained online" so its foundation models refresh, and the vertical models consume its knowledge, which closes the loop at every level of the stack.

Bandit algorithms in news feeds and video recommenders explore in the same way, and A/B platforms often keep a small slice of traffic for random choices.

Why not show random ads to some users to collect clean data?

Random ads are bad ads for the user, and the advertiser who pays for them is charged for impressions that rarely convert. A little uniform randomness gives clean data and a visible cost in revenue and user experience. Posterior-guided exploration concentrates the cost on ads whose value is most uncertain, and the sources we've read don't say which approach Meta uses.

13How Meta knows a change helped

Dev's ad earned a click from Zoya, but we can't judge a model from one click. Published practice has two layers, just like the YouTube chapter's. Offline, the team replays logged data and computes the metrics of section 04. Online, it runs an experiment on live traffic.

Offline, the 2014 paper evaluates on "an arbitrary week of the 4th quarter of 2013", split into training and testing, and replays them in order to simulate the streaming setting. Its metrics are NE and calibration, always as relative numbers. Google's 2013 paper instead evaluates with progressive validation: each example is predicted first and used for training afterwards, so the loss is always measured on data the model hasn't yet seen, exactly as it would at serving time, and every example serves for both training and testing. Both break the loss down by slice, such as country, query topic and layout, because an average hides which slice went wrong. A model can look better overall and be badly miscalibrated on one segment.

Offline numbers have a known limit, and it's the one we met in section 12. They're computed on impressions the old system chose, so a new model that would pick different ads can't be fully judged on them. That's why every serious system ends with a live experiment. A common way to size one is the rule of thumb that to detect a difference between two proportions near with about 80% power at the usual 5% significance, each arm needs about observations. For a 1% click rate and a relative improvement of 0.5%, , which gives roughly 63 million impressions per arm, an illustrative figure that ignores the fact that one user's impressions are correlated. At the scale of these platforms that's a matter of hours, so small relative improvements can be detected and acted on.

Ad auctions add a complication that most experiments don't have: the two arms compete for the same advertisers' budgets. If the treatment arm's model wins more auctions for some advertiser, it spends that advertiser's budget faster, and the control arm sees less of it. A simple user-level split can then misestimate the effect. This is a general concern for experiments in marketplaces, and Meta probably has a way to handle it, but the papers and posts we've read don't describe one.

Publicly, results come mostly in the form of lifts on business metrics. Meta's 2023 Lattice post reports about 8% on ads quality, the 2024 sequence-learning post 2 to 4% more conversions on selected segments, the Andromeda post +6% recall and +8% ad quality, the 2025 GEM post 5% and 3% conversion gains on Instagram and Facebook Feed, and the 2026 posts the 18.9% and 12% figures and the 3% and 5% for the ranking model. None of these gives a baseline, a sample or a confidence interval, and a lift of "up to" or "on selected segments" can't be added up across posts. They tell us the direction and the order of magnitude of what these teams care about, and little more.

Why can't we skip A/B tests and trust a good offline NE?

Because the offline set was produced by the system we're trying to improve and because the metric isn't the goal. A model can have a better NE on logged impressions and win auctions differently, with different advertisers' budgets and different users' experiences. Only an experiment is a place where the model's own choices meet real people, and where revenue, advertiser outcomes and user feedback are all visible.

14What went wrong, and what it cost

Meta hasn't published post-mortems of ads-ranking incidents, so this section lists failure modes the papers and posts describe or warn about, and marks which are published and which we're reasoning from the design.

What happensWhat it costsWhat the design does
The click stream goes stale (published warning, 2014)The trainer sees near-zero CTR and predicts near zero; the system may show fewer adsAnomaly detection disconnects the trainer from the joiner when the live data's distribution changes abruptly
Downsampling applied without recalibration (reasoned from section 08)Every ad looks many times too clickable; prices and ranking against other formats are distortedApply at one place, and monitor calibration by slice
Hundreds of siloed models (published, 2023)Compute and engineering cost; small surfaces starved of dataLattice, then GEM teaching the vertical models
Aggregated features hide order and detail (published, 2024)Information lost before the model sees it; costly overlapping featuresEvent-based features and sequence models
Winner's curse (reasoned; section 03)Winning ads are systematically over-predicted, so advertisers are under-charged and weaker ads winCalibrate on the slice that wins, not only on all data
Delayed conversions (published, 2023)Recent impressions look worse than they areTemporal awareness in the model; unpublished otherwise

One more risk doesn't show up in a table. This system optimises a proxy: predicted clicks or conversions stand in for value to the person and the advertiser. An optimiser pushed hard on a proxy finds whatever raises the proxy, whether or not it raises the value, and clickable isn't the same as good. Meta's published material says little about how it checks this, and the YouTube chapter in this series shows what the same problem looked like at another company.

15The whole system

We can now draw the full design. On the left is Zoya's request, from the app to the winning ad, with Dev's bid and budget entering the auction. On the right is the loop that keeps the models current: logs, the joiner, training data with downsampled negatives, a slow trainer for the big models and a fast one for the online layer, and an experiment gate before anything reaches production.

The whole design: Zoya's request online, the learning loop offline
ONLINE: one request, well under a second OFFLINE: the loop that keeps models current MEERA'S APP feed asks for ads RETRIEVAL tens of millions -> a few thousand RANKING features + embeddings -> pCTR, pConversion calibration layer on top AUCTION total value = bid, action rate, quality winner shown; price computed DEV'S BID budget, goal, bid PACING spend over the day (details unpublished) LOGS impression stream (features, request ID) click and conversion streams ONLINE JOINER attach labels after the wait window TRAINING DATA downsample non-clicks, keep weights TRAINERS slow: big deep models, days of data fast: online layer, near real time A/B TEST + CALIBRATION CHECKS promote only if live metrics hold A model trained on yesterday's logs changes which ads win today, and that changes tomorrow's logs. Published for the late stages: sub-second latency (2026), retrieval of a few thousand ads (2024), online joiner (2014). Unpublished: stage sizes after retrieval, exact auction formula and pricing, refresh cadences, per-stage latencies. Meta's 2025-2026 posts describe one very large foundation model (GEM) that teaches the many smaller serving models.
Online path on the left, learning loop on the right. Dev's bid enters the auction; Zoya's reaction leaves through the logs and comes back as a better model.

Follow one tap through the picture. Zoya's feed asks for an ad. Retrieval cuts the eligible set to a few thousand, the ranking model predicts her click and conversion probabilities for each, and a calibration layer keeps those numbers honest. At the auction, each ad's predicted rate is multiplied by its bid, adds quality, and picks Dev's shoes. Her tap leaves through the click stream, meets its impression in the joiner, becomes a labelled example, and reaches the trainers. Tomorrow's model has a little more evidence about shoe ads and runners in Bengaluru.

ComponentWhat it doesAdded because
Expected-value rankingOrders ads by bid × predicted rateHighest bid wins loses money (§02)
Calibration layerKeeps probabilities meaning what they sayThe auction multiplies them by bids (§03)
NE and slice metricsScores probabilities, not just orderingsAccuracy and AUC are blind to scale (§04)
Hashed, embedded featuresBounded memory for billions of IDsA dictionary of IDs doesn't fit (§05)
Trees, then DLRM-style interactionsLearn feature combinationsLinear models miss interactions (§06, §09)
Online joiner and per-coordinate learningFresh labels, fresh weightsStale models cost accuracy (§07)
Negative downsampling + recalibrationCuts training costBillions of rows, mostly non-clicks (§08)
Sequence features and a foundation modelLearn from raw behaviour; teach serving modelsAggregates lose information; siloed models cost too much (§10)
Retrieval, ranking, shared computationFits large models into a sub-second budgetCan't score every ad (§11)
Exploration and A/B testsBreak the feedback loop; verify liveOnly shown ads get labels (§12, §13)

Several parts of this picture are unpublished: the stage sizes after retrieval, the exact auction formula and pricing rule, budget pacing, per-stage latencies, how often models are refreshed and how Meta handles delayed conversions. Our sources describe the system as of 2013, 2014, 2019, 2023, 2024, 2025 and 2026, and the production system will have moved since.

16Summary

  1. The money in a slot is bid times probability. Ranking by bid alone rewards ads nobody clicks, so the auction ranks by expected value, and the whole system exists to estimate the probability.
  2. Calibration matters more than ranking. A uniform scale error cancels out of both the ranking and the second-price price, and errors that differ between ads, or that are selected for by winning, misallocate slots and under-charge winners.
  3. Log loss is the right loss and normalized entropy the right report card. Expected log loss equals the entropy of the truth plus a KL term that is zero only at the true probability, and dividing by the background-CTR entropy makes results comparable across click rates (He et al., 2014).
  4. Hash billions of IDs into a fixed table. Collisions mostly hit rare IDs, and probabilistic inclusion and 16-bit weights cut memory by another 55 to 75% at a tiny accuracy cost (McMahan et al., 2013).
  5. Boosted trees can encode features for a linear model. Each tree contributes the one-hot index of its leaf, so the linear layer weighs learned rules, and the 2014 hybrid beat plain logistic regression by more than 3.4% NE.
  6. Freshness is worth about 1% NE, and it needs per-coordinate learning rates. Daily beats weekly retraining, and online learning with a rate per weight, fed by an online joiner that waits a tuned window before labelling a non-click, beat the other schedules by up to 5% NE.
  7. Downsample negatives, then undo it with one formula. With a fraction of non-clicks kept, the odds are inflated by , and restores calibration without changing the ranking.
  8. DLRM combines embeddings, a bottom MLP, pairwise dot products and a top MLP. Embeddings hold the memory and MLPs hold the compute, so embeddings are spread across devices and MLPs replicated (Naumov et al., 2019).
  9. Sequences and foundation models replaced hand-built aggregates and siloed models. Meta reported event-sequence features in 2024 and, in 2025 and 2026, a large model (GEM) that teaches hundreds of vertical models, reporting its gains without baselines.
  10. Serving is a funnel with shared computation. Tens of millions of ads become a few thousand, ranking fits a sub-second budget by computing request-level signals once and sharing them across candidates, and Meta hasn't published per-stage latencies.
  11. Logs are a feedback loop. Only shown ads get labels, winners are over-predicted, and exploration, slice calibration and live experiments are how the loop is checked.

17Build this

The corrections in this chapter are easy to nod along to and easy to get wrong. You can rebuild the heart of the system in a day with synthetic data and numpy, and watch each fix change what the auction does.

Project A small ad auction with a calibrated, online-learning click model ~6 hours · numpy only

Build a world where you know the truth: 5,000 ads whose true click probability depends on a hidden quality, a user-segment effect and an ad-by-segment interaction, with 500,000 users in 20 segments. Everything you train gets judged against that truth, which no real platform can do.

  1. Generate impressions with a click label. Give every ad and every user a 64-bit ID and hash them, together with a few categorical features, into a table of 218 buckets. Train a logistic regression on the hashed features and report NE, calibration and AUC on a held-out day. Then halve and double the bucket count and plot NE against memory.
  2. Downsample the non-clicks to 5% and train again. Report calibration before and after applying the recalibration formula, and check that AUC doesn't move.
  3. Add a hand-written boosted-tree encoder, or reuse the one from section 06, and compare the logistic regression on raw features, on leaf features and the trees alone.
  4. Stream the data in time order and train the linear layer online, once with a global learning rate and once with per-coordinate rates. Hold the labels back by a random delay and try three joiner window lengths, measuring the click coverage and the bias in the empirical CTR.
  5. Run auctions: six candidate ads per impression, scored by bid times your model's CTR with a second-price rule. Compare the true value won against a perfect model, then break your model on purpose (new ads overestimated by 2 times) and measure calibration among the winners.
You'll know it worked when NE improves as buckets grow with diminishing returns, the downsampled model is about 10 times too confident until recalibrated and has the same AUC before and after, per-coordinate rates beat the global rate on rare ads, shorter joiner windows give a lower empirical CTR, and winners' calibration is above 1 even when all-ads calibration is exactly 1.
A harder variant. Add an exploration bonus based on a Beta posterior for each new ad and measure how many ads ever get shown, and the total value won, with and without it. Then add a position effect for two ad slots and see what a GSP price does to the winner's payment when your slot-two prediction is miscalibrated.

18Interview questions

BeginnerWhy does an ad auction need calibrated probabilities and not just a good ranking?

An auction ranks ads by bid times predicted click probability and often prices the winner by the runner-up's score divided by the winner's own probability. That arithmetic needs probabilities on the right scale, because a bid is in money and a score is only comparable to another score if both are on the same scale. A uniform scale error cancels out of the ranking and of the price, but errors that differ between ads don't. If a model overestimates new ads by a factor of two, they win slots they haven't earned and are charged half the right price. AUC can't see any of this because it only measures order, and that's why Facebook's 2014 paper reports normalized entropy and a calibration ratio.

BeginnerWhat is normalized entropy and why not use accuracy or log loss?

Normalized entropy is the model's average log loss divided by the log loss of a model that always predicts the average click rate; its loss is the entropy of that rate. An NE of 1 means no better than the baseline, and lower is better. Accuracy is meaningless when 99% of impressions aren't clicked. Raw log loss depends on how hard the problem is, since a 0.1% click rate has a far smaller baseline than a 1% one, so dividing by it makes numbers comparable across surfaces. Unlike AUC, NE rewards calibration: halving an over-confident model's outputs improves NE and leaves AUC unchanged.

IntermediateHow do you handle categorical features with billions of values, and what do collisions cost?

Hash each (feature name, value) pair into one of buckets and keep one weight, or one embedding row, per bucket, so memory is fixed and new IDs need no registration. With values, about of them share a bucket with another, which sounds severe, but traffic is heavily skewed, so a popular ID's weight is dominated by its own data and rare IDs mostly borrow from it. Two heavy IDs colliding is the damaging case, which a larger makes rare. Google's 2013 paper adds probabilistic feature inclusion (Bloom filter with saved 66% of memory for 0.008% AUC loss) and 16-bit fixed-point weights (75% saved).

IntermediateYou downsampled negatives to 5%. Derive the correction for the model's output.

Work in odds. In the sample, every click is kept and a fraction of non-clicks, so the sampled odds are , the true odds divided by . So the true odds are times the model's odds, and , the formula in the 2014 Facebook paper. It's monotone in , so ranking and AUC are unchanged, and it must be applied exactly once. Google's alternative is importance weighting, which gives each kept non-click a weight so the model is calibrated from the start at the cost of noisier gradients.

IntermediateWhy did Facebook put a logistic regression on top of boosted trees?

A linear model can't represent feature interactions unless engineers cross features by hand, and boosted trees find interactions and thresholds automatically. Feeding each tree's leaf index to the linear model as a one-hot feature turns the trees into a supervised encoder, and the linear layer weighs the rules the leaves represent. In the 2014 paper, the combination reached 96.58% of the trees-only NE while plain logistic regression was at 99.43%, a gain of more than 3.4% against a typical feature experiment's few tenths of a percent. Splitting also helps operationally: trees train in batch and are retrained daily, and the linear layer can learn online.

IntermediateWhy use a learning rate per feature for online learning on sparse data?

A global decaying rate shrinks the step for every weight as traffic grows, including weights for features that have barely been seen and need large updates. Per-coordinate rates, , decay a weight's rate only as that weight accumulates gradients, so rare features keep learning quickly while popular ones settle. In Facebook's comparison of five schedules this was best, almost 5% lower in NE than the worst. Google's FTRL-Proximal uses the same rates and adds L1 regularisation, which keeps many weights exactly zero so the model is small.

DeepOffline NE improved but the live experiment shows no revenue change. What do you check?

First calibration by slice, because an NE gain can come from the large slices while the winning ads, which have a different distribution, got no better or worse, and winner's-curse calibration among the ads that win is the number the auction sees. Second, whether the offline set reflects the new model's choices at all, since logs only contain ads the old system showed. Third, auction effects: a better model for one advertiser changes budgets and pacing for others, and a user-level split in a marketplace can misestimate this. Fourth, the metric itself: the model is optimising predicted clicks or conversions, which may differ from the advertiser outcome the experiment measures. Meta's papers don't describe its own debugging process, so this list comes from the structure of the system.

DeepHow would you build labels for purchases that arrive days after the impression?

A fixed waiting window labels every impression as a non-purchase until the purchase arrives, so recent data is biased low, and a longer window delays training and holds more state. Practical designs use a short window for fast, approximate labels and correct them later with the late arrivals, or train a model of the delay distribution and weight recent examples to account for conversions still to come. Facebook's 2014 paper describes the click version of this tradeoff and says the bias was small and measurable, and Meta's 2023 Lattice post says delayed feedback is handled with temporal awareness in the model without giving the mechanism, so which correction Meta uses is unpublished.

DeepWhy does Meta train a very large model and distil it into smaller ones instead of serving it?

Serving has a hard latency budget, on the order of 100 ms for the ranking model in Meta's 2026 post, for thousands of candidate ads per request and a huge request volume. A model with no such limit can learn much more from the data, so Meta's 2025 post describes GEM as a foundation model that transfers knowledge to hundreds of vertical models through direct and hierarchical distillation, with a Student Adapter that refreshes the teacher's targets with recent ground truth. Students stay fast and calibrated against the teacher and the labels. That tradeoff moves as serving improves: the 2026 Adaptive Ranking Model describes serving much larger models in real time through request-level computation sharing, which shifts how much the teacher must do.

19Go deeper

📄
Paper
Practical Lessons from Predicting Clicks on Ads at Facebook
He et al., ADKDD 2014. Boosted trees plus logistic regression, normalized entropy, data freshness, the online joiner and the recalibration formula of sections 04 to 08.
📄
Paper
Ad Click Prediction: a View from the Trenches
McMahan et al., KDD 2013. Google's FTRL-Proximal, per-coordinate rates, probabilistic feature inclusion, importance-weighted subsampling and calibration layers.
📄
Paper
Deep Learning Recommendation Model for Personalization and Recommendation Systems
Naumov et al., 2019. DLRM: embeddings, the interaction layer and the model-parallel training design of section 09. arXiv:1906.00091
📄
Paper
Software-Hardware Co-design for Fast and Scalable Training of Deep Learning Recommendation Models
Mudigere et al., 2021. Training DLRMs with up to 12 trillion parameters. arXiv:2104.05158
📝
Company blog
Meta Lattice
Meta AI, May 11, 2023. One model across ad surfaces and objectives, replacing many siloed models.
📝
Company blog
Sequence learning: a paradigm shift for personalized ads recommendations
Engineering at Meta, November 19, 2024. Event-based features in place of hand-built aggregates.
📝
Company blog
Meta Andromeda: next-gen personalized ads retrieval engine
Engineering at Meta, December 2, 2024. Retrieval from tens of millions of ads to a few thousand, with a jointly trained hierarchical index.
📝
Company blog
Meta's Generative Ads Model (GEM)
Engineering at Meta, November 10, 2025. A foundation model that transfers knowledge to vertical ranking models.
📝
Company blog
Meta Adaptive Ranking Model
Engineering at Meta, March 31, 2026. Serving LLM-scale ranking models under a real-time latency bound.
📝
Company blog
GEM: scaling the central model for frontier ads AI
Engineering at Meta, October 13, 2026. The central model, Kunlun and LoopFM.
📄
Paper
Internet Advertising and the Generalized Second-Price Auction
Edelman, Ostrovsky and Schwarz, American Economic Review, 2007. The auction behind section 03.