[Chess] Fix stale INIT_ZOBRIST_HASH: derive it from the Zobrist tables at import time#1320
Open
gweber wants to merge 1 commit into
Open
[Chess] Fix stale INIT_ZOBRIST_HASH: derive it from the Zobrist tables at import time#1320gweber wants to merge 1 commit into
gweber wants to merge 1 commit into
Conversation
The Zobrist tables are generated at import time from jax.random, whose outputs are not stable across JAX versions (e.g. the threefry partitionable change). The hardcoded INIT_ZOBRIST_HASH no longer matches _zobrist_hash(GameState()) under current JAX, so the initial position's hash_history entry never matches later recomputed hashes: - repetitions involving the start position are counted one short (a g1f3/g8f6/f3g1/f6g8 knight shuffle terminates at ply 9 instead of 8) - the repetition observation planes are wrong for the startpos entry Compute the constant from the actual tables at import time so it can never go stale again. No behavior change on JAX versions where the old constant happened to match.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
The Zobrist tables in
pgx/_src/games/chess.pyare generated at import time fromjax.random, whose outputs are not stable across JAX versions (e.g. thethreefry_partitionabledefault change). The hardcoded constantno longer matches
_zobrist_hash(GameState())under current JAX (0.10.1 gives[1875025768, 182641400]). SinceGameState()seedshash_history[0]with the stale constant while every later entry is recomputed, the initial position's history entry never matches:g1f3 g8f6 f3g1 f6g8 g1f3 g8f6 f3g1 f6g8is a threefold repetition at ply 8, but pgx terminates only at ply 9 (when the position after 1.Nf3 repeats a third time instead).rep0/rep1) are wrong for the startpos history entry.Fix
Compute the constant from the actual tables at import time (same XOR-reduce idiom as
_zobrist_hash), so it can never go stale again, under any JAX version. On JAX versions where the old constant happened to match, this is a no-op.Note:
gardner_chess.pyandanimal_shogi.pyhardcode similar init-hash constants and likely have the same issue; this PR fixes chess only.