Add Lc0-compatible NN inference foundation for MCTS #15
+4,184
−1,048
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.
Implements architectural foundation for Lc0-compatible neural network evaluation in MCTS. Creates complete interface layer and data structures; Metal transformer backend and protobuf weight loading require subsequent implementation (~4-5 weeks).
Architecture
New module:
src/nn/Test Infrastructure
tests/test_lc0_comparison.cppwith 15 benchmark positions (starting, Kiwipete, tactical, endgame). Framework validates encoding and provides comparison harness once inference is implemented.Integration
hybrid-and-mcts-fix./metalfish_tests lc0_nnMerge Notes
This PR is stacked on top of
hybrid-and-mcts-fixbranch. Merge conflicts with the refactored test suite have been resolved. The base branch has a pre-existing LTO linking issue (GPUTuningParams::select_strategyundefined reference) that affects all tests and needs to be addressed separately.What Remains
Critical path to 100% Lc0 compatibility:
.pbweight parsingSee
src/nn/README.mdfor detailed implementation roadmap.Example Usage (when complete)
Original prompt
This section details on the original issue you should resolve
<issue_title>[Feature] Implement Lc0-compatible neural network inference for MCTS with Metal backend</issue_title>
<issue_description>## Summary
Implement neural network inference for MetalFish's MCTS search that uses Lc0-format network weights (
.pbfiles) and produces identical results to Lc0 for the same positions. The implementation should be heavily optimized for Apple Silicon's unified memory architecture.Background
MetalFish currently has a working MCTS implementation (
src/mcts/thread_safe_mcts.cpp) that uses NNUE evaluation. To achieve stronger play, we need to integrate transformer-based neural network evaluation similar to Lc0 (Leela Chess Zero).We have:
networks/BT4-1024x15x32h-swa-6147500.pb(365MB transformer network)reference/lc0/- the official Lc0 source codeRequirements
1. Neural Network Components
Create
src/nn/directory with:Weight Loading (
loader.h,loader.cpp).pband.pb.gz)Position Encoding (
encoder.h,encoder.cpp)Policy Tables (
policy_tables.h,policy_tables.cpp)Metal Backend (
metal/)2. MCTS Integration
Update
src/mcts/:NNMCTSEvaluator (
nn_mcts_evaluator.h,nn_mcts_evaluator.cpp)ThreadSafeMCTS updates
3. Verification Test Suite
Create
tests/test_lc0_comparison.cppthat verifies:Test criteria (100% match required):
Raw NN Output Match
MCTS Best Move Match
Implementation Notes
Lc0 Conventions to Match
Board Representation: Lc0 stores the board from the side-to-move's perspective. When black is to move, the board is mirrored vertically.
Q Value Convention: Q values are stored from the perspective of "the player who just moved" to reach a node. During backpropagation, negate at each level.
Policy Encoding: Use
MoveToNNIndex()andMoveFromNNIndex()with the correct transform for the position.Edge Sorting: After applying NN policy, sort edges by policy value (descending).
Apple Silicon Optimizations
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.