Figures are rounded and some details are generalized.
A backtest replays historical data through a trading strategy to answer one question: if we change this one thing, does the strategy get better? That only works if a backtest with nothing changed gives the same answer twice. With neural networks in the loop, it often does not.
One reason is batching. The same model, with the same weights and the same input, can produce a different prediction depending on where that input sits in a batch. The differences are tiny, but a trading decision is a yes or a no, and tiny differences are enough to change some of them.
2.5% of the trades in our backtest changed because of batch layout alone. The two runs used the same commit and the same data. The only difference was how inputs were batched.
The rest of the piece follows how we traced the problem, measured its effect on backtest comparisons and why there is no free fix.
The symptom
Some of our strategies run neural networks over incoming market data and use the output as a trading signal. One of our researchers added a new set of instruments to a backtest and found that it changed dozens of trading decisions on the existing ones, even though the model evaluates each input independently.
A control run with no changes at all showed the same thing. It differed from the stored baseline on about 1% of decisions.
We checked the inference settings and other hyperparameters first, but they were already pinned. The model was getting the same input in the same way and still giving slightly different answers.
The cause
Our model takes in an input and makes a decision: trade or do not trade. Most decisions are clear, and a tiny numerical difference inside the model changes nothing. Some are close calls, and for those it is enough to flip the decision.
A natural reaction is that the strategy should be robust to that. It cannot be, entirely. Any model that makes a yes or no decision has inputs arbitrarily close to the line between the two. Adding a no-trade band around that line just moves it to the edges of the band. What you can do is know which decisions are close, which we come back to below.
In batched inference, a serving engine packs many inputs into one forward pass, and the GPU kernels underneath split up their work based on how that pass is laid out. So the order of the arithmetic for a given input depends on where it sits in the batch. Different order, different rounding, slightly different output.
This is not specific to one library. It happens in GPU inference engines, plain PyTorch, and any custom CUDA kernel whose decomposition of the work depends on the shape of the batch. Floating-point addition is not associative, so a kernel that changes its summation order with batch layout changes its output with batch layout. None of this is new. Goldberg covers the floating-point side, the PyTorch reproducibility notes cover nondeterministic kernels, and Thinking Machines has a good account of batch invariance in neural network inference. What we add here is what it does to a trading backtest.
We confirmed it by moving a single input between different positions in a batch. Each position produced a distinct result, while holding the position fixed and changing the batch size or the other inputs gave bit-identical results. Position is the variable, not batch size or content.
In a backtest, inputs are sent as concurrent requests, so their positions are set by arrival timing and move from run to run. Adding new instruments shifts the position of every other input. That is how an unrelated addition changed decisions it never touched.
How much it matters
There are two things to measure, and they are easy to mix up. A model output is the number the network produces. A trading decision is whether the strategy trades on it. Outputs can move without changing any decisions. A decision changes only when it was already a close call.
We measured both on a representative sample of inputs:
The first three rows are about reproducibility. Two batched runs disagreed on whether to trade for about 0.2% of all inputs tested, while two runs with one input per pass agreed. The last row is a different problem. Even when each setup is stable on its own, the way you batch changes what the model decides.
Then we measured the effect on a strategy. We ran the same backtest twice from the same commit over a long historical period, once with large batches and once with one input per pass and counted every decision that flipped between trade and no trade. The flips came to 2.5% of the number of trades in the batched run, or about one flip for every 40 trades.
The two runs took almost the same number of trades. They disagreed about which trades to take.
The decisions that flipped in our repeat test were more than an order of magnitude closer to the line between trade and no trade than the median decision. So why care? For three reasons.
First, in a paired backtest comparison, the candidate and the control run over the same history, so market noise largely cancels in the difference between them. Batch noise does not cancel and can be the largest remaining term when the change being tested is small.
It also breaks attribution. Our symptom was adding unrelated inputs moving decisions on existing ones, and a refactor that should change nothing should produce zero differences. With batch noise, neither check is possible.
Most importantly, the mismatch with production is not noise. A backtest that batches differently from production is evaluating a slightly different function every time. Hypothesis tests can handle the variance, but no number of runs removes that bias.
The trade-off between simulation speed and matching production
Reproducibility on its own is easy to fix: run one input per pass and the backtest repeats exactly. The harder question is whether it computes what production computes.
A backtest wants large batches as it has months or years of history to process, and nobody is waiting on any single prediction. Production processes inputs as they arrive, under a latency budget, so its batch size depends on how many arrive together. The same input can be processed alone in a quiet period and alongside many others during a burst. By default, the backtest and production are evaluating slightly different functions.
There are three options:
- Batch freely: The fastest simulation, but results neither reproduce nor match production. That is acceptable only when the difference between the candidate and the control is much larger than the measured flip rate.
- Use batch-invariant kernels: These are kernels written so that an input's result does not depend on what else is in the batch or where it sits. The backtest can batch freely and stay consistent, but production has to run the same kernels, so the live path gets slower.
- Match production's batching: Production is unchanged, but the backtest gives up the throughput of large batches and has to reconstruct which inputs production would have processed together.
We measured inference time, not total backtest runtime, relative to standard kernels: freely batched for the backtest and one input per request for production.
The two options put the cost in opposite places. Invariant kernels are cheap for the simulation and expensive for the live path. Matching production's batching costs the simulation and leaves the live path alone.
Invariant kernels also do not reproduce the result of running an input on its own. They produce a third set of values. None of the three is more accurate, and the differences we measured were within floating-point rounding error. The goal is not a more correct decision but the same decision in the backtest and in production. Batch invariance buys consistency, not accuracy.
For us, that consistency is not worth more than 2.5x in production inference cost. The same compute could buy higher numerical precision, which reduces rounding error rather than just making it reproducible, or a larger model. So we leave production alone and pay to reproduce it in the backtest, but only when a research step needs it:
- Speed: Early on, when we are screening many ideas and looking for large effects, free batching gets through the most experiments. Anything that survives gets rerun under tighter conditions.
- Consistency: When we compare a candidate against a control and the difference is small, both runs have to be reproducible, so that a changed decision comes from the change we made and not from the batch.
- Production match: Before anything goes live, the backtest runs at production's batch shape so that it computes what production will compute. This is about matching production, not about predictive accuracy, and we accept the slower simulation.
Beyond batch layout
Precision and quantization
Precision raises the same trade-off at a larger scale. Running the same model in BF16, FP8 or INT8 moves predictions more than batching does. We expected that, which is why batching was the one that surprised us. On a close call, either is enough to change a trading decision. With market data, the format has to cover the range of prices and volumes we see while preserving the differences the strategy uses.
Lower precision reduces memory and latency, which can make room for a larger model in the same hardware and latency budget. That larger model may predict better than a smaller one at higher precision. So at each stage of the pipeline we decide where lower precision is acceptable, where it is not, and whether the backtest needs to run the same quantization as production.
Hardware
Different GPU architectures can also produce numerically different outputs. The same model, run one input per pass on two GPU generations, gave different outputs on every input but changed no decisions. Some decisions were close enough that a shift that size could have flipped them, so a flip was possible and this dataset just did not produce one. If the backtest and production run on different GPUs, that is another source of mismatch.
Two habits that hold regardless
Two practices are worth having whichever option a research step uses. Neither depends on a particular library or version.
- Record how close each decision was to going the other way. Close calls are the decisions numerical differences can flip, and they should be evaluated with that in mind.
- Record the full serving setup with every baseline: image digest, library version, GPU model, batching settings, precision and input data version. Check that they match before comparing PnL between two runs.
Back to the backtest
Batch layout, precision and hardware are all choices about how a model is served, and each one changes what the model computes. In trading, they also compete with each other. Compute spent making inference reproducible is compute not spent on higher precision or a larger model, under the same latency budget.
There is no universally right configuration. What does not change is the question we started with. Whatever configuration we choose, the backtest has to evaluate the one that will actually trade, and we have to know how much of a measured improvement is just noise from how the model was run.
Summary
- Batched inference makes a prediction depend on where the input sits in the batch. In one of our backtests, that alone changed 2.5% of trades.
- A fast backtest and a latency-constrained production path batch differently by default, so they compute slightly different functions. Closing the gap costs simulation speed or production latency, and we choose per research step whether to optimize for speed, consistency or production match.
- Batch invariance and precision determine inference speed, latency and the size of model you can deploy, so choosing them is part of optimizing the strategy.
- Measure run-to-run noise. A result smaller than that noise is unresolved, not positive.
