From two engines to one
For most of the project, the game and the training of its AI ran on two separate copies of the rules. Why it was built that way, what keeping the copies identical cost, and why they are now one engine.
Why there were two
The game you play in the browser was written in TypeScript. A game interface wants units and tiles as objects, hover previews that answer instantly and animations, and it had to run as a static web page with no server behind it. So the rules lived there, and that engine was the authority on what the rules are.
Training wants something else entirely. The bot learns by playing about a thousand games at once, and the neural network that chooses the moves runs on the GPU. The natural way to feed it was to keep the games on the GPU too: every game as rows of numbers in one big table, and the rules written as operations on that table in PyTorch, the library that trains the network, advancing all thousand games in a single step. No objects, no loops over units, and no copying back and forth between the processor and the graphics card.
One engine for both jobs was considered at the start and rejected. A Python engine in the browser means a server round trip for every hover, or a large download before the page does anything, and an engine built for the interface would have been far too slow to train on. So the same rules were written twice, once for each job. That left one question that never goes away: do the two copies agree?
Keeping two copies in step
The answer had to be measured, not assumed. A test generated random positions in the game's engine, applied the same random legal action in both engines and compared the resulting positions number for number. The bar was complete agreement on every kind of action, with no tolerated residual.
It earned its keep. The first time it ran at real volume, the two engines agreed on 97.6% of actions, and on attacks, the case that matters most, only 60 to 87% of the time. Later a remaining gap of a tenth of a percent, long accepted as harmless, turned out to be a real difference in when the two engines resolved captures. And the comparison was a second opinion in both directions: some of the bugs it found were in the game's own engine, not in the copy.
It also showed where it could not help. When the price of repairing a unit changed, both engines updated what a repair costs and both forgot to update the check of whether a repair is allowed. They agreed perfectly and were both wrong. Agreement is not correctness, so the rules that matter most also got tests that assert the rule itself, on positions built by hand.
What two engines cost
Over time, the costs became clearer than the reasons.
- Every rule change had to be written twice, plus the tests proving that the two versions agreed. The training copy ended up changing more often than the original.
- A few bugs meant that training played a slightly different game from the one on the website, for days or even months before they were found.
- In the training copy, a position was a table of decimal numbers, and that brought its own traps: an amount of credits that cannot be stored exactly, a division that the graphics card carries out as a multiplication and rounds twice. Its picture of a position differed from the game's by less than a ten-millionth in a few places, which was enough to send games down different paths.
- And the main reason for the second engine turned out to be weaker than assumed. When I finally measured it, running the rules on the graphics card took 16 to 19% of every training update, and since the card does one thing at a time, nothing else happened while it did.
Deciding to merge them
The question came up when another game project of mine started out with a single simulation written in Rust: what would one engine cost here? I had three conditions. Training must not get slower. The game must still be a static web page that runs in the browser. And the interface, which is still far from final, must not get harder to change.
The measurements settled the first condition. The rules of a thousand games turned out to run faster on a handful of processor threads than on the graphics card next to the network, even counting the cost of copying every position over to the card at each step.
The language was decided by where the one engine has to run: in the browser, including the background workers the AI uses to think ahead, and inside the Python program that trains the network, on several threads at once. Rust does both. It compiles to WebAssembly for the browser, and to a native module that runs inside the training program. Keeping the TypeScript engine as the only one came second, but there is no fast way to run it inside the training program, and Python in the browser means downloading more than ten megabytes before anything happens. Rust also lets the compiler enforce what used to be written down as warnings: whole-number arithmetic only in the rules, a fixed order for everything, and every unit type handled explicitly wherever a rule depends on it, so a new unit cannot silently inherit another's behaviour.
How the move went
The rules were kept unchanged while the new engine was written, so that both old engines could serve as references for it. The Rust engine had to agree with them exactly, on every kind of action and on every number the network reads, before training was allowed to use it. Since the network sees exactly the same input as before, the current bot carried straight over, with no retraining.
Training switched first. A training update became 13% faster, and evaluations, which play many games and take about half of the graphics card's time, became almost twice as fast. The next change to the rules, a new unit, came right after, and it was the last one that had to be written twice: into the new engine and into the browser's.
The browser moved last. My plan was to wait for a redesign of the interface that I have in mind, so that the engine would only need to answer the questions the final interface asks. In the end that seemed too cautious: a redesign will move the buttons around, but they will still be the same buttons, asking the engine the same questions. So the game on this site now runs the same Rust engine as training, compiled to WebAssembly. It weighs about 90 kilobytes, a small fraction of the AI it sits next to, and the bot you play against plays by exactly the rules it was trained on.
What PyTorch does now
PyTorch used to do two jobs: run the network and run the rules. Now it does only the one it is made for. At every step the Rust engine advances the thousand games on eight processor threads and hands over two things for each game: the position, as the numbers the network reads, and which actions are legal. PyTorch copies them to the graphics card, runs the network on all the games at once, picks an action for each and hands the choices back to the engine. The learning itself, where the network is adjusted on the games it has just played, happens entirely in PyTorch on the card, as it always did.
The browser has no PyTorch at all. A finished network is exported from PyTorch to a file that a small runtime in the page can run, next to the same Rust engine. The old PyTorch engine is kept, frozen on the rules it knew, for comparisons with the past; nothing trains on it.
What one engine gives up
Two engines had one real advantage: each was an independent check on the other. One engine has nothing to disagree with. Its replacement is a test inside the engine itself. The engine answers the same question in two separate ways: which actions are legal right now, as a list, and whether one given action can actually be carried out, by trying it. For every possible action in thousands of positions, the two answers have to match: everything offered can be played, and nothing that can be played is withheld. Two bugs that once slipped through the old setup, both of them actions that were offered and then refused, each fail this test when they are put back in on purpose.
The rules still have their hand-built scenarios, which assert the rule itself. The old TypeScript engine stayed for a week, frozen, as a reference on the rules it knew. The first change to the rules after the move made it obsolete: a last comparison showed that every difference between the two was the new rule, and then it was deleted. That change was written once.
The general point
Writing the same logic twice can be the right call, and when it is, agreement between the copies is something to measure rather than assume. But the cost of the second copy deserves a measurement too. The reasons for it can stop being true while the cost keeps running, and here it took a single measurement to see that they had.