[WIP] Implement Lc0-compatible neural network inference for MCTS #35
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.
src/nn/directory for neural network components.pband.pb.gz)tests/test_nn_comparison.cpp)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)Implementation Strategy: Copy from Lc0
Lc0 is open source (GPL-3.0). You are encouraged to directly copy entire code files from
reference/lc0/usingcpormvcommands. Do not rewrite from scratch what already exists.Copyright Header Requirement
All copied files MUST have their copyright headers replaced with the MetalFish header:
Namespace and Naming Requirements (CRITICAL)
There must be NO mention of "lc0", "lczero", "leela", or "Leela" anywhere in the final code.
This includes:
lczero::→ ✅MetalFish::orMF::Lc0Network→ ✅NeuralNetworklc0_encode()→ ✅encode_position()lc0_weights→ ✅nn_weights// Lc0-style encoding→ ✅// Position encodinglc0_backend.cpp→ ✅nn_backend.cppLC0_API→ ✅METALFISH_APIor removeLC0_NEURAL_H→ ✅METALFISH_NN_HExample transformation:
Directory Guidelines
DO:
src/nn/,src/mcts/,src/gpu/)src/nn/,src/nn/metal/)DO NOT:
lc0_implementation/,lc0_copy/,external_lc0/lczero::namespace referencesExample Workflow
Files to Consider Copying
From
reference/lc0/src/:neural/network.h- Network interfaceneural/encoder.cc- Position encoding (112 planes)neural/writer.cc- Protobuf parsingneural/metal/- Metal backend (MPSGraph)mcts/node.cc- MCTS node structuremcts/search.cc- Search algorithmschess/board.cc- Board representation (for encoding compatibility)utils/weights_adapter.cc- Weight loading utilitiesRequirements
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/ornn/metal/)2. MCTS Integration
Update
src/mcts/:...
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.