I'm trying to understand the NNUE by re-implementing the NNUE in Numpy to ensure that I understand the NNUE accurately. Something is wrong here.
What is wrong?
Okay, let's analyze the provided Python code and the Stockfish NNUE implementation to pinpoint the errors. The core issue is that the numpy implementation of the feature transformer and the network propagation does not precisely match what the Stockfish engine does.
Here's a breakdown of the problems and how to fix them:
1. Incorrect Feature Transformation:
Reorientation Logic: The reorientation logic in the python code does not match the reorientation logic of the C++ code. The OrientTBL in half_ka_v2_hm.h is implemented with a lookup table, not with bitwise operations, and the lookup table is not replicated in the python code.
Incorrect indexing: The indexing into the weights and psqts arrays is flawed. It should be weights[piece_type*2 + is_black_piece, (8*row+column)^our_reorientation] for each piece, and weights and psqts are not indexed by the king position.
Missing bias: The biases are not added in the feature transformation stage.
2. Incorrect NNUE Propagation:
Incorrect Clipped ReLU: The clipped relu implementation clipped_relu(x) performs a shift by 6, not by 7, or by 9.
Incorrect Sqr Clipped ReLU: The sqr clipped relu implementation is not correct. (x*x)>>19 is not the same as (x*x).astype(np.int64)>>19 for negative numbers, because x*x is always positive after squaring, where the C++ implementation will preserve the sign.
Missing Bias Scaling: The bias term of the first layer is scaled by 600 * 16 / 127 * 64
Inaccurate Layer Dimensions: The code assumes the layer dimensions are (3072), 15, 32 when they should be (3072), 16, 32 in the big net.
3. Other Issues:
Hardcoded Constants: The code uses hardcoded values (e.g., 127, 600, 16, 64, 19) instead of constants from the Stockfish source. This makes the code less readable and harder to maintain.
Missing Bucket Calculation: The bucket calculation is performed at the wrong time, before the biases have been added.
Corrected Feature Transformation: The feature indices are now correctly calculated using the reorientation lookup table, and the biases are added correctly. The indexing of the weights and psqts arrays is fixed.
Corrected NNUE Propagation: The clipped ReLU and Sqr clipped ReLU implementations are fixed, layer dimensions are corrected, and the bias scaling is added to the first layer.
Clearer Code: Constants are used instead of magic numbers, and comments are added to explain the code.
To Use the Corrected Code:
Save: Save the corrected Python code as a .py file (e.g., nnue_numpy.py).
Run: Execute the script. It will print the NNUE evaluation for the starting position.
Important Notes:
Network Architecture: Ensure the code is consistent with the specific NNUE architecture (big or small).
Data Loading: Make sure the .npz file is loaded correctly, and biases, weights, etc., are not modified unintentionally before being used.
Testing: Compare the output of your corrected Numpy implementation with the output of the Stockfish engine for various positions to verify that the two implementations are equivalent.
Small Net: This code implements only the big net. The small net should be implemented separately.
Vectorization: This numpy implementation does not implement any SIMD vectorization optimization that can be found in the C++ code.
Bucket: The bucket calculation is performed after adding the biases.
By addressing these errors, you should have a much more accurate and functional NNUE implementation in Numpy, allowing you to better understand how the Stockfish NNUE works. Let me know if you have any other questions.