feat: policy network trainer on Lc0 visit distributions
train/policy is a fork of montytrain's policy trainer (the one Monty, Vine and Jackal use: bullet at a pinned revision plus two CUDA operations, SelectAffine over the legal moves' output rows and a softmax cross-entropy against the visit distribution). Rigby's version drops Monty's SEE split of the outputs (3920 rows, as in Vine), takes its arguments on the command line and checks a checkpoint with --check. The Stockfish binpacks used for the value net only keep the played move, so the data comes from Lc0's raw V6 training tars instead. lc02monty rebuilds each game from its first record and the played moves and checks every record: pieces must match, and the moves Lc0 gave a probability must be exactly the legal moves. Lc0 writes castling as king-takes-rook; Chess960 games keep their rook files. On a test91 tar all 351,454 records convert. fetch.ps1 downloads a run's hourly tars day by day, converts and deletes them, and interleave shuffles the day files into one. Defaults sized for a 20-minute run on the RTX 3070: 1024 hidden, 10 superbatches of 100M positions (~1M positions/s). The pinned cudarc does not know CUDA 13.3, so env.ps1 sets CUDARC_CUDA_VERSION=13000. Checked: a smoke run on one tar lowers the loss from 2.89 to 2.38 and gives plausible opening priors; a checkpoint reloads to identical output; interleaved data trains. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9643c7b0a5
commit
1c9d2beec3
23 files changed
+2850
No files matched your search
@@ -0,0 +1,92 @@
|
||||
mod loss;
|
||||
mod select_affine;
|
||||
|
||||
use acyclib::{
|
||||
device::tensor::Shape,
|
||||
graph::{builder::GraphBuilder, Graph, GraphNodeId, GraphNodeIdTy},
|
||||
trainer::dataloader::PreparedBatchDevice,
|
||||
};
|
||||
use bullet_cuda_backend::CudaDevice;
|
||||
use montyformat::chess::{Castling, Move, Position};
|
||||
|
||||
use crate::{
|
||||
data::{loader::prepare, reader::DecompressedData},
|
||||
inputs::{INPUT_SIZE, MAX_ACTIVE_BASE, MAX_MOVES, NUM_MOVES_INDICES},
|
||||
};
|
||||
|
||||
pub fn make(device: CudaDevice, hl: usize) -> (Graph<CudaDevice>, GraphNodeId) {
|
||||
let builder = GraphBuilder::default();
|
||||
|
||||
let inputs = builder.new_sparse_input("inputs", Shape::new(INPUT_SIZE, 1), MAX_ACTIVE_BASE);
|
||||
let targets = builder.new_dense_input("targets", Shape::new(MAX_MOVES, 1));
|
||||
let moves = builder.new_sparse_input("moves", Shape::new(NUM_MOVES_INDICES, 1), MAX_MOVES);
|
||||
|
||||
let l0 = builder.new_affine("l0", INPUT_SIZE, hl);
|
||||
let l1 = builder.new_affine("l1", hl / 2, NUM_MOVES_INDICES);
|
||||
|
||||
let hl = l0.forward(inputs).crelu().pairwise_mul();
|
||||
|
||||
let logits = builder.apply(select_affine::SelectAffine::new(l1, hl, moves));
|
||||
|
||||
let ones = builder.new_constant(Shape::new(1, MAX_MOVES), &[1.0; MAX_MOVES]);
|
||||
let loss = builder.apply(loss::OptimisedSoftmaxCrossEntropy::new(logits, targets));
|
||||
let _ = ones.matmul(loss);
|
||||
|
||||
let node = GraphNodeId::new(loss.annotated_node().idx, GraphNodeIdTy::Ancillary(0));
|
||||
(builder.build(device), node)
|
||||
}
|
||||
|
||||
pub fn eval(graph: &mut Graph<CudaDevice>, node: GraphNodeId, fen: &str) {
|
||||
let mut castling = Castling::default();
|
||||
let pos = Position::parse_fen(fen, &mut castling);
|
||||
|
||||
let mut moves = [(0, 0); 64];
|
||||
let mut num = 0;
|
||||
|
||||
pos.map_legal_moves(&castling, |mov| {
|
||||
moves[num] = (u16::from(mov), 1);
|
||||
num += 1;
|
||||
});
|
||||
|
||||
let point = DecompressedData { pos, castling, moves, num };
|
||||
|
||||
let data = prepare(&[point], 1);
|
||||
|
||||
let mut on_device = PreparedBatchDevice::new(vec![graph.device()], &data).unwrap();
|
||||
|
||||
on_device.load_into_graph(graph).unwrap();
|
||||
|
||||
let _ = graph.forward().unwrap();
|
||||
|
||||
let dist = graph.get(node).unwrap().get_dense_vals().unwrap();
|
||||
|
||||
let mut out: Vec<(String, f32)> =
|
||||
(0..num).map(|i| (Move::from(moves[i].0).to_uci(&castling), dist[i] * 100.0)).collect();
|
||||
out.sort_by(|a, b| b.1.total_cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
|
||||
|
||||
println!();
|
||||
println!("{fen}");
|
||||
for (uci, pct) in out {
|
||||
println!(" {uci:<6} {pct:6.2}%");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save_quantised(graph: &Graph<CudaDevice>, path: &str) -> std::io::Result<()> {
|
||||
use std::io::Write;
|
||||
|
||||
let mut file = std::fs::File::create(path).unwrap();
|
||||
|
||||
let mut quant = Vec::new();
|
||||
|
||||
for id in ["l0w", "l0b", "l1w", "l1b"] {
|
||||
let vals = graph.get_weights(id).get_dense_vals().unwrap();
|
||||
|
||||
for x in vals {
|
||||
let q = (x * 128.0).round() as i8;
|
||||
assert_eq!((x * 128.0).round(), f32::from(q));
|
||||
quant.extend_from_slice(&q.to_le_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
file.write_all(&quant)
|
||||
}
|
||||
Reference in new issue
Block a user