chans

a whole chess game in a few dozen characters

White to move
Start position

PGN

Examples

Click one to load it. Common openings are what the model expects, so they take very few characters.

Openings

    Famous games

      Curiosities

        Internals

        #Move Rank Probability Bits State

        The view above shows the decoding process: the first 16 bits are the starting state, then each decision reads the bits its probability asks for. Encoding in ANS happens in reverse order: the encoder starts with the final state, which holds a 13-bit check of the game, the outcome and the side the board is seen from; codes END, unless the rules end the game (checkmate, stalemate, the 75-move rule, insufficient material); then codes all moves and promotions backwards, the last first; and ends by writing out its state, which the decoder reads as its starting state.

        In every position a small neural network scores the legal moves and END, and the scores become frequencies out of 32768: the likelier a move, the fewer bits it costs. A promotion's piece has fixed frequencies. Click a row to show its position and what the model gave every move there.

        Decision

        How rANS codes a symbol

        The coder has a state x, a number in [65536, 131072). The candidates share the 32768 values of x mod 32768, its slots: each has as many as its frequency f, one run after another in canonical order (by origin, then destination square, END last).

        Decoding in state x gives the candidate whose run holds x mod 32768. Its run starts at c; x becomes f · floor(x / 32768) + (x mod 32768 − c), which is in [2f, 4f), and is then doubled, one bit of the stream read into its low end each time, until it is back at 65536 or more. A likely candidate has a long run and reads few bits: about log2(32768 / f).

        Encoding does the same backwards. To code a candidate from state x, the encoder writes x's low bits until x is below 4f, then sets x to floor(x / f) · 32768 + c + (x mod f). Since each step undoes the other, a game is encoded last symbol first and decoded first to last.

        A candidate costs −log2(f / 32768) bits, to within about a hundredth of a bit, and there are no tables: the frequencies can be new at every move, which is what a model needs.

        The arrow keys step through the game, the window following.

        FEN of the shown position

        About chans

        A whole game in a few dozen characters

        A chans code holds a complete chess game: every move, the result, and which side the board is seen from. Load it here and the game comes back move for move. A typical game fits in about 45 characters, so you can:

        How it works

        1. A very small neural network looks at each position and gives every legal move a probability: how likely a player is to choose it.
        2. An entropy coder turns the moves actually played into a packed stream of bits, spending few bits on likely moves and more on surprising ones: a move of probability p costs −log2 p bits.
        3. A base conversion turns the bits into characters: base 32, in the 32 letters and digits of bech32.

        On average this takes about 2.3 bits a move on longer games, plus a constant of about 15 bits a game (the coder's state, which also carries the check, the result and the side). For example:

        ← → move through the game, Home End jump, f flips the board, Backspace undoes the last move.

        The name

        ch is for chess, ans for asymmetric numeral systems (ANS), the kind of entropy coder that packs the moves. And chans sounds like chance: all of it runs on probabilities, the chance the model gives every move. The empty game, no moves yet, spells the name: ch1ans.

        The rest of this page is a look under the hood; the exact rules are in the specification.

        The neural network

        The network is a small transformer, built like the one of Maia-3 (Chessformer), but tiny: some 79,000 weights, a 117 KB file. Every square of the board is a token, as the side to move sees it: 14 on/off inputs for which piece stands there and whether each side attacks it. One more token stands for the game: castling rights, en passant and check. Each token becomes 32 numbers, plus a learned vector for the piece standing on that square, and four layers of attention let every square look at every other, with a learned bias for how far apart and in which direction two squares lie, each followed by a small feed-forward layer.

        A move's score is the dot product of a query of the square it leaves and a key of the square it reaches, plus a bias for that pair of squares and the piece that moves. A last pseudo-move, END, stands for the game ending there (by resignation, time or agreement), and is scored from the game's token. The softmax of the scores gives the probabilities; END's is kept at least 1/64, so a game can end anywhere for at most 6 bits.

        All of it runs in integer arithmetic, with weights quantized to 8 bits and values to 16, so every browser and computer computes exactly the same probabilities, which the decoder needs to read the code back. In the browser it runs in WebAssembly, about half a millisecond a move.

        The move modelThe position becomes 65 tokens, one for each square and one for the game, which four transformer layers turn into 32 numbers each; each legal move is scored by the query of its origin square times the key of its destination, plus a bias for the pair, END from the game token, and the scores become probabilities for the coder.Positionpieces, side to move, castling, en passantLegal moves, and ENDeach a pair of squares: from, toas the side to move sees it65 tokens64 squares: piece, attacked by either sideand the game: castling, en passant, check32 numbers each, plus the piece-square vector4 transformer layersattention: 4 heads, every square to every other,a learned bias per offset between squares;then a feed-forward layer. 9,400 weights eachFinal tokens32 numbers for each square and the gamefrom, toquery, keyScore of each movequery(from) · key(to) + bias (pair, piece); END: gameProbabilities → rANS codersoftmax; END at least 1/64

        In the specification: the model's inputs, the network, frequencies, the model file.

        How it was trained

        The training data is a sample of 2.1 billion positions, with the move played (or the game's end), from games of the Lichess open database of 2021 to 2026: rated games of players rated 1300 or more, three minutes or longer. First a much larger transformer (256 numbers a token, eight layers; 4.3 million weights) was trained on a graphics card to predict the move played. The small one then learned from it by knowledge distillation: mostly to match the large network's whole probability distribution, which says more than the one move played, over a billion positions.

        On positions held out of training, the large network needs 1.99 bits a move, the small one 2.27, and 2.29 as the coder uses it (in integers, END at least 1/64). The transformer before it, twice the size and four times the work, needed about the same; the network before that, built like the NNUE networks of chess engines, 2.71 on its own test; a hand-made scheme before that, which sorted the moves by kind and coded their place in the list, about 3.9.

        The entropy coder: rANS

        The coder is range asymmetric numeral systems (rANS). Its state is a 16-bit number. The probabilities are rounded to shares of 32,768, and coding a move of share f folds it into the state, which grows by a factor of about 32,768/f; whenever the state gets too large, its low bits go out into the stream. Coding runs backwards, from the last move to the first, so that decoding reads the game from the start; in all it costs within about 0.01 bits a move of the ideal −log2 p.

        The coder starts from a state holding a 13-bit hash of the whole game, the result and the side it is seen from, and ends by writing its state out. The decoder ends up back in that first state: it recomputes the hash of the game it read, and refuses a code whose hash does not match. Because rANS has to write its state out anyway, the check, the result and the side cost nearly nothing.

        In the specification: the coder.

        From bits to characters

        The stream of bits is read as one large number and written in base 32, 5 bits a character, with bech32's characters: the digits and lower-case letters but 1, b, i and o, which are easily taken for others. After ch1 (the 1 cannot be one of them, so it marks where the prefix ends), as in bech32. The conversion is itself done by a small rANS-style converter, in linear time rather than with big-number division, and it is a bijection: every stream has exactly one code, and all but 1 in a million codes are some stream's. Fixed values XORed into the last three characters make the game with no moves read ch1ans.

        The QR code holds the same characters, upper case, behind chans://: QR codes store 0-9 and A-Z at 5.5 bits each in their alphanumeric mode, against 8 for anything else.

        In the specification: the text and the URI.

        Specification

        The format's specification (moves.md in the source): everything a decoder needs but the model's weights, which are a file in the format it gives. Like the format, it is not final yet.