Late Move Reductions
Late Move Reduction (LMR) is another unsound forward pruning technique in the main search code. They are based on a simple idea but they are kind of a pain in the butt to get right. The idea is that if you have good move ordering, the best moves are considered first and by the time you are considering the later moves at some position they are not likely to be very good. We can make a bet with them: reduce the depth at which we search them (and thus spend fewer nodes on them). If they are really "bad moves", we win the bet and get to re-invest the saved nodes to increase search depth.
I started hearing about LMR even back in 2005, when I was last working on the engine. Everyone was doing it and talking about how much of a strength win it was. Some people called it "history pruning" because they used the dynamic history of a move (in addition to its lateness) as a signal about the risk of reducing it. I think Fruit was one of the first engines using this technique and Glaurung was the first codebase I read with LMR.
But whenever I tried to implement it, it didn't really work very well for me. I ended up with a "poor man's" version where I basically reduced every move after some point by ONE_PLY and called it day. This did benefit the engine strength but I always thought it was a stupid implementation of the idea.
When I began work on typhoon again recently I resolved to make
LMR really work and started trying a bunch of ideas. Most chess
programs reduce the later moves by something proportional to the
log(move ordinal) * log(remaining depth). I tried this
too and found that it was barely better than my stupid blanket
ONE_PLY heuristic, at least for me.
This caused me to start to really think about LMR. The first thing I tried was a simple rule: reduce moves proportionally to their risk. That is, if a move is likely to fail high (be good), don't reduce it as much and if a move looks really terrible, reduce it more.
How do we know if a move is good or bad? I decided to create a giant multi-dimensional array with one feature per dimension. My first stab was used: remaining depth, move number (this is "late" move reductions, after all), gap below alpha, and the move value estimate as its dimensions. I would do week-long data collection passes where I would run thousands of positions through the engine and accumulate counters: how many times did I see a move with remaining depth D with a gap to alpha of G and a move value estimate of M? How many times did it fail high (prove to be good)? I had to run for ~a week to get enough evidence in the dustiest corners of this table to trust the signal.
This table was a ~good idea but it was also a total pain. As time went by, I thought of new features that might signal when a move is good but each time I did it added another dimension to the table (which made it larger). I would then need to run another trillion positions through it because every time I added a dimension it also made the dusty corners of the array even dustier -- fewer and fewer moves met the conditions tracked there.
Finally one day I decided to stop counting in a table a build a
regression model. By this time I had collected a bunch of signals
about the quality of the move and really had reached the limit to
what I could keep track of in a table. A regression model plots
each training example (and all of its features) in K-dimensional
space and assigns a weight to each feature to compute a single
number called the "logit": z = bias + w1 x1 + w2 x2 + ... + wK xK
Regression training adjusts the weights and bias (typically via gradient descent or convex optimization) to minimize log-loss (binary cross-entropy) across all training examples.
Once the model weights are learned, inference basically just involves computing the weights for a new situation (move and node) and then performing one multiply and add per model feature. This is ~fast, much faster than most other ML models.
With a regression model:
- features add up linearly instead of exploding in size: Adding a new feature adds one weight rather than multiplying the number of table buckets.
- inference is fast and branch-free: a dot product plus a sigmoid lookup avoids deep tree-branching penalties and heavy neural-net overhead in hot search loops.
So, in short, I am now on my 6th version of my little regression model that predicts the probability that a move will fail high now. It's pretty accurate and has a handful of features both about the move and the current search situation.
When I had a good estimate for whether or not a move would fail high I thought for sure that LMR would be simple. With LMR, there are only four possibilities:
| The move failed low when reduced, the move would have failed low at full depth (FLFL). This is a pure win, you save nodes proportional to the reduction and can spend them later on deeper searches. |
The move failed low when reduced but would have failed high if searched to full depth (FLFH). Here, you made a mistake and you lose. You missed something -- a refutation. You caused a blind spot in the engine. |
| The move failed high at reduced depth, would have failed low at full depth (FHFL). Anytime a move fails high, we verify the FH so this will be detected and fixed. The only cost is the nodes spent on the verification search itself. |
The move failed high at reduced depth and would have failed high at full depth (FHFH). Again, any time a move fails high, we verify it and pay the extra cost of a verification search. |
So I started reducing the moves that looked unlikely to fail high (according to my model) heavily and... it didn't work. So, I kept thinking about LMR and started reading other engines' code. Most are dead simple implementation and I'm not sure they work very well at all. But stockfish was doing lots of interesting things. They seemed to have a bunch of features (the same kind of things I was putting my regression model). And they were doing this one odd trick: reducing by an extra 3-4 ply when the node was a strong CUT node.
What is the deal with this CUT node extra reduction? I started to think about it and came up with two possibilities:
- They do this because they are pretty sure a move will fail high here and do not want to pay the cost of a barely-reduced initial search followed immediately by a full-depth verification search. This is about efficiency?
- They do this because if you miss a fail high (because you reduced the move too much to see it), at a CUT node, if a later sibling move fails high, it will "cover" your mistake and the tree shape will remain the same.
I don't know for sure which one of these it is, maybe a little of both. But thinking about it is what caused me to come up with my current LMR implementation (which ~works): The question LMR has to answer, for every candidate R, is whether the expected savings from reducing are worth the expected risk of being wrong.
Two numbers, measured empirically per move and per candidate reduction R, drive the bet:
- m(R) — the miss hazard: given a move that truly fails high at full depth, the probability that reducing it by R plies causes the search to report fail low instead.
- f(R) — the false-alarm hazard: given a move that truly fails low at full depth, the probability that reducing it by R plies makes the search incorrectly report fail-high (rarer, but not zero — it's what triggers an unnecessary, if harmless, full-depth re-search).
If you look back at the four possible outcomes table, these "hazard" rates correspond to the FLFH (silent miss) quadrant, when you pay some unknown price because you missed something and corrupted your search tree, and the FHFL (spurious FH) quadrant, where you pay for a needless verification search and become less efficient.
But how do you know these hazard curves? Why, measure them of course. One of the reasons I'm writing this doc is because there was a real dearth of good information online about LMR when I was pondering it so, if you're a chess programmer, pay attention to this idea... it's a good one. From pretty early on I was instrumenting my builds to measure the "silent miss" (FLFH) outcomes because they are the only ones you can't see in production builds. Think about it, if your reduced move fails high, standard LMR re-searches the move to verify (or not) the fail high result before trusting it. Thus, you can count the FHFL and FHFH outcomes easily. But if your reduced move fails low, how do you know whether it was the FLFL (win!) outcome or the FLFH (silent miss and corrupt your tree!) outcome? Well, I basically remember the FL and then immediately re-search the move (unconditionally) at full depth and count.
Then one day I was thinking about what happens after one of these FLFH silent misses happens. How bad is it, anyway? It happens all the time and the tree doesn't suffer... much. Why? Let's walk through the life of a "silent miss":
- You reduce a move too much and it FL but if it would have been searched to full depth, it would have FH. You screwed up.
- In prod builds, you have no idea this happened. It looks the same as a FLFL.
- But if you would not have reduced, the node would have failed high as soon as this move returned its score from its recursive search.
- Instead, the node keeps searching later siblings of the move that should have failed high. This is a waste -- these searches should never have happened.
- However, if one of these later sibling moves ends up failing high, it kinda "covers" your mistake in that it causes the search tree shape to remain the same. The node that would have failed high had you not reduced the move too much still fails high. It does so with a different score (coin toss whether the later sibling's score is better or worse than the missed FH). This is a kind of corruption but it's way better than if no later siblings fail high and your mistake propagates.
- If no later siblings "cover" for you, the whole node fails low erroneously. At this node, at depth N, your mistake manifests as a missed fail high.
- The whole node failing low causes the move from the parent node to the node where you made the mistake to fail high erroneously. This is due to negamax searching -- if a child node fails low the move to it from the parent fails high when the score inverts. So at the parent node, at depth N-1 in the tree, your mistake manifests as a spurious fail high (unless it was caught by a later sibling).
- If the move from the parent to the site of the mistake was, itself, reduced, this spurious fail high is verified! This is key. All LMR fail high results are re-searched at full depth. Thus, this parent node has a "safety net" if the move to the child was reduced. We get another chance, with more depth, to explore the same subtree and hopefully not make the same FLFH mistake this time!
- If the move from the parent to the site of the mistake was either not reduced (and thus not verified) or we make the same FLFH mistake in the verification search too, the parent node fails high. It does not search any later siblings that it should have had your mistake not caused it to erroneously failed high. This is an opportunity cost. If any of these later siblings had a good move, you miss it.
- Because the parent node failed high erroneously, the move from the grandparent node (at depth N-2) to the parent node (at depth N-1) fails low erroneously. A missed fail high again!
- If you think about this, at every "even parity node" (including the site of the original mistake), at depths (N, N-2, N-4, ...) your mistake manifests as a missed fail high and can be corrected by a later sibling failing high to cover for you. This rarely ever happens unless the missed fail high was a very early move of a CUT node, FWIW.
- And at every "odd parity node" at depths (N-1, N-3, N-5, ...) your mistake manifests as a false fail high and can be corrected by a verification search (LMR re-search or PV re-search).
- Yes, there are two kinds of re-searches. We covered one of them: the LMR re-search. A reduced depth move FH and we verify it at full depth. But the same thing happens at PV ancestor nodes if they are searching below moves 2..N and one fails high on alpha. Typically they re-search these moves with a full alpha..beta window instead of a minimal window search (MWS) on alpha. So this is yet another "safety net" for false fail highs.
So that's why your search doesn't go to hell when you make some FLFH mistakes -- there are several lines of defense waiting to fix it. If they fail, though, the false FH or missed FH climbs back up the search stack and arrives at a PV ancestor node which may (if it's an odd parity node) have one last "safety net" verification search. If that fails, though, your move affects the PV -- it changes what the engine thinks the best move in its path from the root to the best eval is. Bad stuff. And it can keep climbing up, affecting more "best" moves in the PV. This is where the mistake starts to really cost you strength.
So, how often does that happen? Well, I wanted to find out so I changed my build that just counted FLFH misses to actually trace what happens afterwards. Instead of just returning a score from search I started returning a score with a corruption record saying if the score was too low or too high and why. Go read the JOINT_EXTRA_VERIFY build for all the details. And, the answer to the "how often?" question is: it depends:
- How many reduced depth moves from odd parity ancestors are above you? Each one creates a "safety net" verification search that has a chance to fix the mistake.
- How many early CUT node ancestors at even parity are above you? Each one of them has a chance to fix the mistake when a later sibling "covers" for you.
- How many PV nodes are there at an odd parity distance from you? Each one of these creates a PV verification opportunity.
With that JOINT_EXTRA_VERIFY build I could not only count the misses but also how many of then ended up affecting the PV and also track the aggregate number of corrupt plies in the search. Remember, affecting the PV is not the only way these FLFHs hurt you: there is wasted work (spent searching later siblings that should have been skipped), opportunity cost (not searching moves that should have been searched), and score corruption (when a later sibling "covers" for you but returns an inferior score). I worry about these less than outright corrupting the PV... in some ways the "wasted work" and "opportunity cost" ~balance each other out. Flip a coin. If you missed a great signal from an opportunity cost move maybe you'll catch one from a wasted work move. Once I had this build, though, it made LMR much, much easier to reason about and experiment with. Almost like I had an answer key!
Given a move's own predicted probability of failing high at full depth, p (from the same per-move regression model used elsewhere — see Recent Changes on the overview page), the four possible outcomes of reducing by R have these probabilities:
P(reduced reports FAIL-LOW) = (1-p)(1-f(R)) + p * m(R)
P(reduced reports FAIL-HIGH) = (1-p) * f(R) + p * (1-m(R))
The first term of each line is the "correct" case (truly FL reported as FL, or truly FH reported as FH); the second term is the error case (a real fail-high silently missed, or a real fail-low falsely flagged). Each outcome carries a cost.
Reducing by R plies only costs a fraction V(R) of a full-depth search, e.g. V(1 ply) ≈ 15.6%, falling to V(6 ply) ≈ 0.07%.
If the reduced search reports fail-low, that's the end of it — it banks a savings of S(R) = 1 − V(R) relative to a full-depth search.
If the reduced search reports fail high, the move gets a full-depth verification re-search anyway (standard LMR behavior), so that branch costs V(R) plus the price of the verification itself — modeled against a baseline of "never reduce at all" (always cost 1).
Thus, the expected net benefit of reducing by R, in those cost units, is:
Delta(R) = S(R) * P(reduced reports FAIL-LOW) - V(R) * P(reduced reports FAIL-HIGH)
That's the expected payoff.
The risk sitting against it is the rate of the one outcome that
actually matters — a genuine fail-high silently
missed, FLFH(R) = p * m(R) — since every other outcome
either costs nothing extra or gets self-corrected by the
verification re-search. Dividing payoff by risk gives a single
dimensionless number:
Cstar(R) = Delta(R) / Flfh(R)
C*(R) answers "how many units of expected search-cost savings do we buy per unit of silent-corruption risk, at this reduction?" Typhoon sweeps candidate whole-ply reductions from a log(depth)-capped ceiling down to 1 and picks the largest R whose C*(R) still clears a required bar:
C*(R) - K(situational safety bucket) >= C_min
We fall back to R = 0 (no reduction at all) if nothing clears it, rather than a hand-picked token minimum. C_min was set empirically using the JOINT_EXTRA_VERIFY build.
K is the one place "situational safety" (how likely a silent miss here is to actually corrupt the principal variation — see Recent Changes on the overview page) enters the formula: another simple regression model computes the probability that a FLFH miss will affect the PV based on nearby guarded ancestors, early cut ancestors, and the distance to the PV itself. Then, a penalty is subtracted straight out of C* before the comparison.
All of this runs in integer, parts-per-100000 fixed-point arithmetic
rather than floating point — cheap enough to evaluate for every
candidate R at every reduction-eligible move, with no
per-move pow() or log() calls (the m/f/V
tables are precomputed once at startup).
None of this is finished science. The effective branching factor implied by the measured V(R) curve isn't constant — it decays from roughly 6 at R=1 ply toward roughly 3 by R=6 ply, so a single textbook branching-factor constant understates how cheap deep reductions really are. And the verification-re-search cost term currently assumes a fresh, cold full-depth search, when in reality it's a warm re-search of a position just explored moments ago — almost certainly cheaper, though nobody has measured exactly how much cheaper yet.
Late Move Pruning: LMR pushed to R = full depth
When I was working on the engine in the early 2000s I read a paper (and bought a book) by Ernst Heinz, the author of the engine Darkthought, about Extended Futility Pruning (EFP). The basic idea is that, in QSearch nodes, you are likely already doing normal futility pruning: if a move seems not to be valuable enough to raise the static evaluation score above alpha, we skip it. If you're down a rook, taking a pawn is not likely to help matters. This is standard engine practice.
Given that, if you are 1 ply above the horizon to QSearch (where you will apply normal futility pruning) and want to gamble a little, you can pad out the move's value with a fudge factor and do the same thing higher in the tree (thus saving more nodes). If you're a rook down one ply above the QSearch, taking a pawn is still not likely to help. Crafty does this kind of thing way up the tree with an ever-growing fudge factor and an interesting comment in their codebase:
************************************************************
* *
* Futility. First attempt at forward pruning based on *
* the futility idea. *
* *
* We have an array of pruning margin values that are *
* indexed by depth (remaining plies left until we drop *
* into the quiescence search) and which increase with *
* depth since more depth means a greater chance of *
* bringing the score back up to alpha or beyond. If the *
* current material + the bonus is less than alpha, we *
* simply avoid searching this move at all, and skip to *
* the next move without expending any more effort. Note *
* that this is classic forward-pruning and can certainly *
* introduce errors into the search. However, cluster *
* testing has shown that this improves play in real *
* games. The current implementation only prunes in the *
* last 6 plies before quiescence, although this can be *
* tuned with the "eval" command changing the "pruning *
* depth" value to something other than 7 (test is for *
* depth < pruning depth, current value is 7 which prunes *
* in last 6 plies only). Testing shows no benefit in *
* larger values than 7, although this might change in *
* future versions as other things are modified. *
...
But my quest to make LMR really work well in the engine left me with a cool regression model that predicts the probability that a move will fail high from a bunch of signals (only a few of which are the move's estimated value and the gap to alpha). I thought that my model was smarter than Ernst's heuristic and thus ditched EFP in favor of Late Move Pruning (LMP): I basically just look at the move's predicted FH% and skip it if it's too low.
LMP, therefore, is a forward-pruning sibling of LMR (see above). Like EFP, it only fires in the last few plies before the horizon. Unlike LMR, instead of searching a late, unpromising move to a reduced depth, LMP skips it outright.
I think of LMP as using the exact LMR cost model above with one small substitution: set R = D, i.e. reduce all the way down to nothing, and watch what happens to each term.
At shallow remaining depth, LMR is disabled altogether (today's reduction ceiling only opens up a few plies in because I never want to reduce a move so much that it drops directly into QSearch and never gets at least one more ply of full width search). So, near the QSearch horizon, there's no partial reduction to compare against. A pruned move there was going to cost 100% of whatever a search of it would have cost. When we skip it, and the savings S is the full 100% — V = 0. That's the simplest case, and it's also where LMP does the most work and carries the most risk: a silently missed fail-high at D=1, with a 0-ply "search" has no shallow probe at all to catch it, just the parent's own verification re-search as a backstop.
Deeper up, at the remaining plies where LMR itself would normally have picked some partial reduction, RLMR(D) < D, LMP's outright pruning isn't a potential free 100% savings any more. The real counterfactual isn't "search at full depth vs. not at all" but rather it's "search at RLMR(D) vs. not at all". LMR would already have captured some to most of the possible savings at that depth by reducing, so LMP's incremental gain over just using LMR there is only the gap between full pruning and LMR's own reduced-search cost, V(RLMR(D)).
Plug R = D into the
same S(R)/V(R)/Δ(R)/C*(R) machinery from the
LMR section above, just with the miss-rate curve
m(D) and false-alarm
curve f(D) for "never search this move at all"
in place of LMR's partial-reduction versions. Those curves are
necessarily worse than any LMR curve at a smaller R (you can't do
better than zero information), which is exactly why production LMP's
required bar is stricter than LMR's, and why it only fires on the
already least-promising slice of moves — today that's driven by a
hand-calibrated threshold table on the regression model's predicted
fail-high probability
(ShouldLatePrune's uLMPStaticMaxPctThousandths[]
curve
in searchsup.c,
one bar per remaining ply) rather than running the
full C* sweep per move — but it's answering the same
underlying cost-vs-corruption-risk question LMR is, just at
the R = D extreme of it.
That threshold table itself has been retuned more than once by exactly this kind of reasoning. An earlier version tightened the bar at D=6+ on the theory that deeper, more move-ordering-confident pruning decisions deserved more scrutiny — but tightening it there moved prune rate without moving the silent-miss rate that actually corrupts the principal variation; the damage was concentrating at the shallow end (D=1) all along, not the deep end. The table currently in production reverted to a flatter, more permissive curve at low D and leans on the same "did this specific miss ever reach the PV" corruption telemetry that calibrates the LMR formula's situational-safety score, rather than tightening blindly by depth.