"""Verify HalfKAv2_hm features match Stockfish NNUE exactly""" import chess from python.model.feature_extractor import fen_to_features from python.stockfish_wrapper import NNUEEvaluator from python.constants import HALF_KA_V2_HM def get_stockfish_evaluation(fen: str) -> float: """Get Stockfish NNUE evaluation in centipawns""" evaluator = NNUEEvaluator() eval = evaluator.evaluate(fen) evaluator.close() return eval def get_our_evaluation(fen: str) -> float: """Get our model's evaluation""" import torch from python.model.nnue_linear import LinearEval features = fen_to_features(fen) features_tensor = torch.tensor([features], dtype=torch.float32) model = LinearEval() with torch.no_grad(): eval = model(features_tensor)[0, 0].item() return eval # Test positions test_positions = [ "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", # Starting "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1", # Black to move "8/8/8/8/8/8/8/8 w KQkq - 0 1", # Empty board ] print("Position\t\t\t\tStockfish\t\tOur Model\tDiff") print("-" * 80) for fen in test_positions: try: stockfish_eval = get_stockfish_evaluation(fen) our_eval = get_our_evaluation(fen) diff = abs(stockfish_eval - our_eval) print(f"{fen[:25]:25}\t{stockfish_eval:10.2f}\t{our_eval:10.2f}\t{diff:.2f}") except Exception as e: print(f"{fen[:25]:25}\tERROR: {e}")