| 1 | """Maia-3 über ONNX: Kodierung und UCI-Verhalten, ohne das echte Modell.""" |
| 2 | |
| 3 | import unittest |
| 4 | from collections import deque |
| 5 | |
| 6 | import chess |
| 7 | import chess.engine |
| 8 | import numpy as np |
| 9 | |
| 10 | from schach import maia_onnx |
| 11 | |
| 12 | |
| 13 | class EncodingTest(unittest.TestCase): |
| 14 | def test_vocabulary(self): |
| 15 | self.assertEqual(len(maia_onnx.ALL_MOVES), 4352) |
| 16 | self.assertEqual(len(set(maia_onnx.ALL_MOVES)), 4352) |
| 17 | self.assertEqual(maia_onnx.ALL_MOVES[0], "a1a1") |
| 18 | self.assertEqual(maia_onnx.ALL_MOVES[4096], "a7a8q") |
| 19 | |
| 20 | def test_black_sees_board_mirrored(self): |
| 21 | board = chess.Board() |
| 22 | board.push_san("e4") |
| 23 | black = maia_onnx.tokenize(board) |
| 24 | board.push_san("e5") |
| 25 | # Nach 1.e4 aus Schwarz' Sicht: eigene Bauern auf Reihe 2, Bauer e4 → gegnerischer Bauer auf e5 |
| 26 | self.assertEqual(black[chess.E2, 0], 1) |
| 27 | self.assertEqual(black[chess.E5, 6], 1) |
| 28 | self.assertEqual(black[chess.E7, 6], 0) |
| 29 | self.assertEqual(black.sum(), 32) |
| 30 | |
| 31 | def test_legal_index_covers_all_moves(self): |
| 32 | for fen in (chess.STARTING_FEN, "8/1P4k1/8/8/8/8/6Kp/8 b - - 0 1", "8/1P4k1/8/8/8/8/6Kp/8 w - - 0 1", |
| 33 | "r3k2r/8/8/8/8/8/8/R3K2R b KQkq - 0 1"): |
| 34 | board = chess.Board(fen) |
| 35 | legal = maia_onnx.legal_index(board) |
| 36 | self.assertEqual(set(legal.values()), set(board.legal_moves), fen) |
| 37 | |
| 38 | def test_history_pads_with_oldest(self): |
| 39 | a, b = np.zeros((64, 12), np.float32), np.ones((64, 12), np.float32) |
| 40 | hist = maia_onnx.history_tensor(deque([a, b])) |
| 41 | self.assertEqual(hist.shape, (64, 96)) |
| 42 | self.assertEqual(hist[:, :84].sum(), 0) # 7× die älteste Stellung |
| 43 | self.assertEqual(hist[:, 84:].sum(), 64 * 12) # aktuelle zuletzt |
| 44 | |
| 45 | |
| 46 | class FakeModel: |
| 47 | """Bevorzugt immer e2e4/e7e5 (gespiegelt dasselbe Vokabularfeld) und merkt sich die Elo.""" |
| 48 | |
| 49 | def __init__(self): |
| 50 | self.calls = [] |
| 51 | |
| 52 | def run(self, histories, self_elos, oppo_elos): |
| 53 | self.calls.append((len(histories), list(self_elos), list(oppo_elos))) |
| 54 | logits = np.zeros((len(histories), 4352), np.float32) |
| 55 | logits[:, maia_onnx.MOVE_INDEX["e2e4"]] = 5 |
| 56 | logits[:, maia_onnx.MOVE_INDEX["d2d4"]] = 4 |
| 57 | values = np.tile(np.array([0.0, 0.0, 30.0], np.float32), (len(histories), 1)) # Gewinn für die Seite am Zug |
| 58 | return logits, values |
| 59 | |
| 60 | |
| 61 | class UciTest(unittest.TestCase): |
| 62 | def setUp(self): |
| 63 | self.model = FakeModel() |
| 64 | self.engine = maia_onnx.Engine(lambda: self.model, temperature=0, multipv=3, seed=1) |
| 65 | self.out = [] |
| 66 | |
| 67 | def send(self, *lines): |
| 68 | for line in lines: |
| 69 | self.engine.handle(line, self.out.append) |
| 70 | |
| 71 | def test_go_reports_multipv_and_bestmove(self): |
| 72 | self.send("uci", "isready", "setoption name Elo value 1700", "position startpos moves e2e4", "go movetime 100") |
| 73 | self.assertIn("uciok", self.out) |
| 74 | self.assertIn("readyok", self.out) |
| 75 | infos = [l for l in self.out if l.startswith("info")] |
| 76 | self.assertEqual(len(infos), 3) |
| 77 | self.assertIn(" pv e7e5 string prob ", infos[0]) # e2e4 gespiegelt = e7e5 für Schwarz |
| 78 | self.assertIn(" pv d7d5 string prob ", infos[1]) |
| 79 | self.assertEqual(self.out[-1], "bestmove e7e5") |
| 80 | self.assertEqual(self.model.calls[0], (1, [1700], [1700])) |
| 81 | self.assertEqual(len(self.model.calls), 1) # ein Durchgang, keine Bewertung je Kandidat |
| 82 | |
| 83 | def test_go_reports_wdl_once(self): |
| 84 | self.send("isready", "position startpos", "go movetime 100") |
| 85 | infos = [l for l in self.out if l.startswith("info")] |
| 86 | self.assertIn(" wdl 1000 0 0 ", infos[0]) # FakeModel: Gewinn für die Seite am Zug |
| 87 | self.assertNotIn(" wdl ", infos[1]) |
| 88 | |
| 89 | def test_python_chess_reads_wdl(self): |
| 90 | self.send("isready", "position startpos moves e2e4", "go movetime 100") |
| 91 | line = next(l for l in self.out if l.startswith("info")) |
| 92 | board = chess.Board() |
| 93 | board.push_uci("e2e4") |
| 94 | info = chess.engine._parse_uci_info(line.split(" ", 1)[1], board, chess.engine.INFO_ALL) |
| 95 | self.assertEqual(info["wdl"].white(), chess.engine.Wdl(0, 0, 1000)) # Schwarz am Zug gewinnt |
| 96 | |
| 97 | def test_wdl_permille_order_and_sum(self): |
| 98 | win, draw, loss = maia_onnx.wdl_permille([0.0, 1.0, 2.0]) # Verlust < Remis < Gewinn |
| 99 | self.assertGreater(win, draw) |
| 100 | self.assertGreater(draw, loss) |
| 101 | self.assertEqual(win + draw + loss, 1000) |
| 102 | |
| 103 | def test_history_follows_moves(self): |
| 104 | self.send("position startpos moves e2e4 e7e5 g1f3") |
| 105 | self.assertEqual(len(self.engine.history), 4) |
| 106 | self.send("position fen 8/8/8/8/8/8/6k1/4K2R w K - 0 1") |
| 107 | self.assertEqual(len(self.engine.history), 1) |
| 108 | |
| 109 | def test_illegal_move_keeps_old_position(self): |
| 110 | self.send("position startpos moves e2e4") |
| 111 | self.send("position startpos moves e2e5") |
| 112 | self.assertEqual(self.engine.board.move_stack, [chess.Move.from_uci("e2e4")]) |
| 113 | |
| 114 | |
| 115 | if __name__ == "__main__": |
| 116 | unittest.main() |