HaileyStorm
commited on
Commit
•
8b4892f
1
Parent(s):
746013b
Upload chess-gpt-eval/main.py with huggingface_hub
Browse files- chess-gpt-eval/main.py +34 -32
chess-gpt-eval/main.py
CHANGED
@@ -375,6 +375,7 @@ def add_random_moves(
|
|
375 |
for i in range(num_moves * 2): # Full moves to half moves
|
376 |
legal_moves = list(board.legal_moves)
|
377 |
if not legal_moves:
|
|
|
378 |
return None, None, 0 # Game over, discard the game
|
379 |
|
380 |
move = board.san(random.choice(legal_moves))
|
@@ -386,9 +387,11 @@ def add_random_moves(
|
|
386 |
game_state += f" {move}"
|
387 |
|
388 |
if board.is_game_over():
|
|
|
389 |
return None, None, 0 # Game over, discard the game
|
390 |
|
391 |
game_state = game_state.strip()
|
|
|
392 |
return game_state, board, num_moves
|
393 |
|
394 |
|
@@ -481,22 +484,26 @@ def play_games(
|
|
481 |
book_opening: bool = False,
|
482 |
random_opening: bool = False,
|
483 |
random_opening_moves: int = 20,
|
|
|
484 |
):
|
485 |
unique_games = set()
|
486 |
games_saved = 0
|
487 |
while games_saved < max_games:
|
488 |
print(f"\nGame {games_saved} of {max_games}\n")
|
489 |
|
490 |
-
|
491 |
-
|
|
|
492 |
board = chess.Board()
|
493 |
|
494 |
if book_opening:
|
495 |
game_state, board, opening_moves = random_book_opening(game_state, board)
|
496 |
elif random_opening:
|
497 |
-
|
498 |
-
|
499 |
-
if
|
|
|
|
|
500 |
break
|
501 |
else:
|
502 |
opening_moves = 0
|
@@ -523,6 +530,18 @@ def play_games(
|
|
523 |
with open("game.txt", "w") as f:
|
524 |
f.write(game_state)
|
525 |
current_move_num = f"{board.fullmove_number if move_num_in_gamestate else ''}."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
526 |
total_moves += 1
|
527 |
# I increment legal moves here so player_two isn't penalized for the game ending before its turn
|
528 |
player_one_legal_moves += 1
|
@@ -633,16 +652,16 @@ def play_games(
|
|
633 |
RUN_FOR_ANALYSIS = True
|
634 |
MAX_MOVES = 999 # Due to nanogpt max input length of 1024
|
635 |
recording_file = "logs/determine.csv" # default recording file. Because we are using list [player_ones], recording_file is overwritten
|
636 |
-
player_ones = ["50M/anneal/
|
637 |
player_two_recording_name = "lc0_sweep" #"stockfish_sweep"
|
638 |
move_num_in_gamestate = False
|
639 |
-
book_opening =
|
640 |
random_opening = True
|
641 |
-
random_opening_moves =
|
642 |
if __name__ == "__main__":
|
643 |
for nanogpt_player in player_ones:
|
644 |
i = 0
|
645 |
-
for rm in range(
|
646 |
#for i in [0]: # [3] #range(11):
|
647 |
num_games = 500
|
648 |
# player_one = GPTPlayer(model="gpt-3.5-turbo-instruct")
|
@@ -652,9 +671,11 @@ if __name__ == "__main__":
|
|
652 |
# player_one = StockfishPlayer(skill_level=-1, play_time=0.1)
|
653 |
|
654 |
player_one_recording_name = nanogpt_player
|
655 |
-
player_one = NanoGptPlayer(model_name=player_one_recording_name, move_num_in_gamestate=move_num_in_gamestate)
|
656 |
-
player_one_recording_name = "
|
657 |
-
|
|
|
|
|
658 |
#player_two = StockfishPlayer(skill_level=i, play_time=0.1)
|
659 |
player_two = LC0PLayer(skill=i)
|
660 |
|
@@ -664,24 +685,5 @@ if __name__ == "__main__":
|
|
664 |
#print(f"\n\nSTARTING GAMES AGAINST STOCKFISH LEVEL {i}\n\n")
|
665 |
print(f"\n\nSTARTING GAMES AGAINST LC0 LEVEL {i}\n\n")
|
666 |
|
667 |
-
play_games(player_one, player_two, num_games, book_opening=book_opening, random_opening=random_opening, random_opening_moves=rm)
|
668 |
|
669 |
-
print("\n\n\n********\nFinal Statistics:\n********\n")
|
670 |
-
for nanogpt_player in player_ones:
|
671 |
-
player_one_recording_name = "xformer_" + nanogpt_player
|
672 |
-
if RUN_FOR_ANALYSIS:
|
673 |
-
csv_file_path = f"logs/{player_one_recording_name}_vs_{player_two_recording_name}"
|
674 |
-
csv_file_path = csv_file_path.replace(".", "_") # Because I'm using ckpt filenames for nanogpt models
|
675 |
-
csv_file_path += ".csv"
|
676 |
-
else:
|
677 |
-
csv_file_path = recording_file
|
678 |
-
|
679 |
-
try:
|
680 |
-
stats = calculate_stats(csv_file_path)
|
681 |
-
if stats:
|
682 |
-
print(f"\nStatistics for {nanogpt_player}:")
|
683 |
-
for key, value in stats.items():
|
684 |
-
print(f"{key}: {value}")
|
685 |
-
except:
|
686 |
-
print(f"Couldn't get stats for {csv_file_path}")
|
687 |
-
print("\n\n\n********\nDONE!\n********\n\n\n")
|
|
|
375 |
for i in range(num_moves * 2): # Full moves to half moves
|
376 |
legal_moves = list(board.legal_moves)
|
377 |
if not legal_moves:
|
378 |
+
print("Random moves: no legal moves left.")
|
379 |
return None, None, 0 # Game over, discard the game
|
380 |
|
381 |
move = board.san(random.choice(legal_moves))
|
|
|
387 |
game_state += f" {move}"
|
388 |
|
389 |
if board.is_game_over():
|
390 |
+
print("Random moves: game over.")
|
391 |
return None, None, 0 # Game over, discard the game
|
392 |
|
393 |
game_state = game_state.strip()
|
394 |
+
print(f"{num_moves} Random moves added, returning: {game_state}")
|
395 |
return game_state, board, num_moves
|
396 |
|
397 |
|
|
|
484 |
book_opening: bool = False,
|
485 |
random_opening: bool = False,
|
486 |
random_opening_moves: int = 20,
|
487 |
+
random_move_start: int = 0,
|
488 |
):
|
489 |
unique_games = set()
|
490 |
games_saved = 0
|
491 |
while games_saved < max_games:
|
492 |
print(f"\nGame {games_saved} of {max_games}\n")
|
493 |
|
494 |
+
# with open("gpt_inputs/prompt.txt", "r") as f:
|
495 |
+
# game_state = f.read()
|
496 |
+
game_state = ""
|
497 |
board = chess.Board()
|
498 |
|
499 |
if book_opening:
|
500 |
game_state, board, opening_moves = random_book_opening(game_state, board)
|
501 |
elif random_opening:
|
502 |
+
for _ in range(10):
|
503 |
+
g, b, opening_moves = add_random_moves(game_state, board, random_opening_moves)
|
504 |
+
if g is not None:
|
505 |
+
game_state = g
|
506 |
+
board = b
|
507 |
break
|
508 |
else:
|
509 |
opening_moves = 0
|
|
|
530 |
with open("game.txt", "w") as f:
|
531 |
f.write(game_state)
|
532 |
current_move_num = f"{board.fullmove_number if move_num_in_gamestate else ''}."
|
533 |
+
|
534 |
+
if total_moves == random_move_start:
|
535 |
+
for _ in range(10):
|
536 |
+
g, b, opening_moves = add_random_moves(game_state, board, random_opening_moves)
|
537 |
+
if g is not None:
|
538 |
+
game_state = g
|
539 |
+
board = b
|
540 |
+
break
|
541 |
+
total_moves += random_opening_moves
|
542 |
+
continue
|
543 |
+
|
544 |
+
|
545 |
total_moves += 1
|
546 |
# I increment legal moves here so player_two isn't penalized for the game ending before its turn
|
547 |
player_one_legal_moves += 1
|
|
|
652 |
RUN_FOR_ANALYSIS = True
|
653 |
MAX_MOVES = 999 # Due to nanogpt max input length of 1024
|
654 |
recording_file = "logs/determine.csv" # default recording file. Because we are using list [player_ones], recording_file is overwritten
|
655 |
+
player_ones = ["50M/anneal/anneal_complete_round3.pt"]
|
656 |
player_two_recording_name = "lc0_sweep" #"stockfish_sweep"
|
657 |
move_num_in_gamestate = False
|
658 |
+
book_opening = True
|
659 |
random_opening = True
|
660 |
+
random_opening_moves = 10
|
661 |
if __name__ == "__main__":
|
662 |
for nanogpt_player in player_ones:
|
663 |
i = 0
|
664 |
+
for rm in [25]: #range(5, 25, 5):
|
665 |
#for i in [0]: # [3] #range(11):
|
666 |
num_games = 500
|
667 |
# player_one = GPTPlayer(model="gpt-3.5-turbo-instruct")
|
|
|
671 |
# player_one = StockfishPlayer(skill_level=-1, play_time=0.1)
|
672 |
|
673 |
player_one_recording_name = nanogpt_player
|
674 |
+
# player_one = NanoGptPlayer(model_name=player_one_recording_name, move_num_in_gamestate=move_num_in_gamestate)
|
675 |
+
#player_one_recording_name = f"xformer_rdm_{rm}"
|
676 |
+
player_one = MambaPlayer(model_name=player_one_recording_name, move_num_in_gamestate=move_num_in_gamestate)
|
677 |
+
player_one_recording_name = f"random_mamba_start/mamba_rdmstart_{rm}"
|
678 |
+
|
679 |
#player_two = StockfishPlayer(skill_level=i, play_time=0.1)
|
680 |
player_two = LC0PLayer(skill=i)
|
681 |
|
|
|
685 |
#print(f"\n\nSTARTING GAMES AGAINST STOCKFISH LEVEL {i}\n\n")
|
686 |
print(f"\n\nSTARTING GAMES AGAINST LC0 LEVEL {i}\n\n")
|
687 |
|
688 |
+
play_games(player_one, player_two, num_games, book_opening=book_opening, random_opening=random_opening, random_opening_moves=random_opening_moves, random_move_start=rm)
|
689 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|