Integer inference on chain
A polyp's forward pass runs in the contract with nothing but integer adds, multiplies and shifts. This note shows how the network fits in a few dozen bytes, why every rounding rule is part of the model, and how we check that the browser and the chain always agree.
Why integers
The EVM has no floating point. Emulating it in fixed point is possible, but every library rounds a little differently, and a model that predicts one number in the browser and another on chain is worse than useless: it makes the public record disagree with the tool that built it.
So we went the other way. The model is defined in integers from the start. During training a float network stands in for it, but what gets stored, run and scored is an integer program with exactly specified rounding. We call that program the genome.
The genome, byte by byte
A genome is a bytes value of 19 to 138 bytes, big-endian, version 1:
| Bytes | Field | Range |
|---|---|---|
| 0 | Version | 1 |
| 1 | Target market id | 0 to 7 today |
| 2 | Horizon code | 15, 30 or 60 minutes |
| 3, 4 | nIn, nHid | 1 to 8 each |
| 5, 6 | shift1, shift2 | 0 to 16 |
| 7 | Reserved | 0 |
| 5 per input | Market, kind (MOM or DEV), window code, divisor (uint16) | divisor 1 to 65535 |
| nHid × nIn | W1, int8, row per hidden unit | -128 to 127 |
| 2 × nHid | b1, int16 | -32768 to 32767 |
| nHid | W2, int8 | -128 to 127 |
| 2 | b2, int16 | -32768 to 32767 |
The length is 10 + 5·nIn + nHid·nIn + 3·nHid. At spawn the contract rejects anything that does not decode exactly: unknown version, sizes out of range, a shift above 16, an unknown market, kind or window, a zero divisor, a non-zero reserved byte or a length that does not match the header. The same check is a public view, checkGenome(bytes), so a client can ask before paying gas.
The float network is a training device. The genome is the model.
Neuropolypse lab notes
The forward pass
Each pulse turns raw tick features into a probability in basis points:
x_i = clamp(trunc(raw_i × 127 / div_i), -127, 127) a_j = b1_j + Σ_i W1[j][i] × x_i h_j = a_j ≤ 0 ? 0 : min(a_j >> shift1, 127) o = b2 + Σ_j W2[j] × h_j z = clamp(o >> shift2, -128, 128) p = SIGMOID_BPS[z + 128]
Inputs are squeezed into the int8 range by a per-feature divisor learned from the training split. Hidden units are ReLUs, capped at 127 so the next layer stays small. The output z is the logit in sixteenths, which is exactly what the lookup table is indexed by.
Nothing here can overflow a 256-bit word: the largest hidden pre-activation is about 32,767 plus 8 × 128 × 127, and the output is of the same order.
Rounding is part of the model
Two operations round, and they round differently on purpose, because that is what Solidity does natively:
- Normalising a feature uses division, which truncates toward zero.
- The two shifts are arithmetic shifts on signed integers, which floor toward negative infinity.
| Expression | Truncate | Floor |
|---|---|---|
| -5 / 2 | -2 | -3 |
| -7 / 4 | -1 | -2 |
| -129 / 64 | -2 | -3 |
| 5 / 2 | 2 | 2 |
| -1 / 2 | 0 | -1 |
The engine in the browser writes these as Math.trunc and Math.floor(v / 2 ** shift). Mixing them up moves a probability by one table step on some inputs and nowhere else, which is the worst kind of bug: rare, silent and enough to make two records differ.
A 257-entry sigmoid
The probability comes from a table rather than an exponential. Entry k holds round(10000 / (1 + e^(-(k - 128) / 16))) for k from 0 to 256, so z covers plus or minus 8 logits in steps of a sixteenth. The table runs from 3 to 9997 basis points and is stored in the contract as one 514-byte constant of big-endian uint16 values.
SIGMOID_BPS
Probability in basis points against z. Near 50% one step is about 1.6 points of probability.
A worked example
Here is the demo polyp from the product pages on one sample, computed in your browser by js/engine.js as this page loads. The contract would return the same integers for the same raw features.
The table appears here when the page has loaded.
Proving the two sides agree
The browser engine is the reference. The contract has to match it bit for bit, and we check that three ways:
- Vectors. A script writes more than five hundred random genomes with random features, plus edge cases: shifts of 0 and 16, all-negative weights, inputs that clamp, outputs at both ends of the table. The forge suite runs every one through the contract and compares raw features, z and p.
- Mock pools. Pools with scripted tick cumulatives check the TWAP arithmetic, including negative floors and markets priced as token1.
- Fork tests. A copy of Robinhood Chain state with the eight real pools: spawn, pulse, wait, settle, and compare with the engine on the same reads.
What it costs
The arithmetic is the cheap part. An 8 by 8 network is 64 multiply-adds and a handful of shifts. Most of a pulse is spent reading oracles: each distinct market gets one observe() call with every point its features need, and each point is a binary search through the pool's observation ring.
In fork tests against mainnet, the heaviest possible genome, eight features over eight markets, cost about 0.8 to 0.85 million gas for a pulse with cold storage, most of it inside Uniswap's own observe(). A settle is about 56 thousand. Typical polyps read one to three markets and cost roughly 150 to 350 thousand.
Each observe() call gets a fixed gas budget of 40,000 plus 80,000 per point, and the transaction reverts if the caller cannot afford it. That closes a subtle hole: without it, a caller could pick a gas limit that makes a healthy oracle look broken.
What we gave up
One hidden layer, at most eight units, ReLU only, one output. Probabilities move in table steps. These are real limits, and we chose them deliberately: the job of a polyp is a calibrated lean on one market over one horizon, computed where anyone can watch. A model that needs more than this probably should not live in a contract yet.