train: value network trainer on Lc0 data
A bullet trainer for a (768 -> 128)x2 -> 1 SCReLU value network, reading linrock's Lc0-derived Stockfish binpacks directly (no conversion). The target blends the game result (0.4) with the search score; 40 superbatches by default, about 12 minutes on an RTX 3070. train/value/train.ps1 sets up CUDA 13.3 and cargo, unpacks the default binpack if needed, builds and runs. train/README.md documents the data, the setup and the network file layout for the engine side. No engine change. Bench: 546127 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
241ad4a546
commit
530c4d67ed
6 files changed
+844
No files matched your search
@@ -0,0 +1,125 @@
|
||||
//! Rigby value network trainer.
|
||||
//!
|
||||
//! Architecture: (768 -> HIDDEN)x2 -> 1, SCReLU, dual perspective.
|
||||
//! Data: Lc0 self-play positions converted to Stockfish binpacks (linrock/test80).
|
||||
//! Target: WDL * game result + (1 - WDL) * sigmoid(score / SCALE).
|
||||
//!
|
||||
//! Usage:
|
||||
//! cargo run --release -- [--data <file.binpack>]... [--superbatches N] [--id NAME]
|
||||
//!
|
||||
//! The network is written to checkpoints/<id>-<superbatch>/quantised.bin every
|
||||
//! SAVE_RATE superbatches and at the end. Inference in Rigby has to use the same
|
||||
//! HIDDEN, SCALE, QA and QB.
|
||||
|
||||
use bullet_lib::{
|
||||
game::inputs::Chess768,
|
||||
nn::optimiser::AdamW,
|
||||
trainer::{
|
||||
save::SavedFormat,
|
||||
schedule::{TrainingSchedule, TrainingSteps, lr, wdl},
|
||||
settings::LocalSettings,
|
||||
},
|
||||
value::{
|
||||
ValueTrainerBuilder,
|
||||
loader::sfbinpack::{MoveType, PieceType, SfBinpackLoader, TrainingDataEntry},
|
||||
},
|
||||
};
|
||||
|
||||
const HIDDEN: usize = 128;
|
||||
const SCALE: i32 = 400;
|
||||
const QA: i16 = 255;
|
||||
const QB: i16 = 64;
|
||||
|
||||
const DEFAULT_DATA: &str = "../data/test80-2024-02-feb-2tb7p.min-v2.v6.binpack";
|
||||
const DEFAULT_SUPERBATCHES: usize = 40;
|
||||
const SAVE_RATE: usize = 10;
|
||||
const WDL: f32 = 0.4;
|
||||
const LOADER_THREADS: usize = 6;
|
||||
|
||||
/// Keep quiet, settled positions: past the opening, not in check, a quiet best
|
||||
/// move and a sane score.
|
||||
fn filter(entry: &TrainingDataEntry) -> bool {
|
||||
entry.ply >= 16
|
||||
&& !entry.pos.is_checked(entry.pos.side_to_move())
|
||||
&& entry.score.unsigned_abs() <= 10000
|
||||
&& entry.mv.mtype() == MoveType::Normal
|
||||
&& entry.pos.piece_at(entry.mv.to()).piece_type() == PieceType::None
|
||||
}
|
||||
|
||||
struct Args {
|
||||
data: Vec<String>,
|
||||
superbatches: usize,
|
||||
id: String,
|
||||
}
|
||||
|
||||
fn parse_args() -> Args {
|
||||
let mut args = Args { data: Vec::new(), superbatches: DEFAULT_SUPERBATCHES, id: "rigby-value".to_string() };
|
||||
let mut it = std::env::args().skip(1);
|
||||
while let Some(arg) = it.next() {
|
||||
match arg.as_str() {
|
||||
"--data" => args.data.push(it.next().expect("--data needs a path")),
|
||||
"--superbatches" => {
|
||||
args.superbatches = it.next().expect("--superbatches needs a number").parse().expect("not a number")
|
||||
}
|
||||
"--id" => args.id = it.next().expect("--id needs a name"),
|
||||
other => panic!("unknown argument: {other}"),
|
||||
}
|
||||
}
|
||||
if args.data.is_empty() {
|
||||
args.data.push(DEFAULT_DATA.to_string());
|
||||
}
|
||||
for path in &args.data {
|
||||
assert!(std::path::Path::new(path).exists(), "data file not found: {path}");
|
||||
}
|
||||
args
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = parse_args();
|
||||
|
||||
let mut trainer = ValueTrainerBuilder::default()
|
||||
.dual_perspective()
|
||||
.optimiser(AdamW)
|
||||
.inputs(Chess768)
|
||||
.save_format(&[
|
||||
SavedFormat::id("l0w").round().quantise::<i16>(QA),
|
||||
SavedFormat::id("l0b").round().quantise::<i16>(QA),
|
||||
SavedFormat::id("l1w").round().quantise::<i16>(QB),
|
||||
SavedFormat::id("l1b").round().quantise::<i16>(QA * QB),
|
||||
])
|
||||
.loss_fn(|output, target| output.sigmoid().squared_error(target))
|
||||
.build(|builder, stm_inputs, ntm_inputs| {
|
||||
let l0 = builder.new_affine("l0", 768, HIDDEN);
|
||||
let l1 = builder.new_affine("l1", 2 * HIDDEN, 1);
|
||||
|
||||
let stm_hidden = l0.forward(stm_inputs).screlu();
|
||||
let ntm_hidden = l0.forward(ntm_inputs).screlu();
|
||||
l1.forward(stm_hidden.concat(ntm_hidden))
|
||||
});
|
||||
|
||||
let schedule = TrainingSchedule {
|
||||
net_id: args.id.clone(),
|
||||
eval_scale: SCALE as f32,
|
||||
steps: TrainingSteps {
|
||||
batch_size: 16_384,
|
||||
batches_per_superbatch: 6104,
|
||||
start_superbatch: 1,
|
||||
end_superbatch: args.superbatches,
|
||||
},
|
||||
wdl_scheduler: wdl::ConstantWDL { value: WDL },
|
||||
lr_scheduler: lr::CosineDecayLR {
|
||||
initial_lr: 0.001,
|
||||
final_lr: 0.001 * 0.3f32.powi(5),
|
||||
final_superbatch: args.superbatches,
|
||||
},
|
||||
save_rate: SAVE_RATE,
|
||||
};
|
||||
|
||||
let settings =
|
||||
LocalSettings { threads: LOADER_THREADS, test_set: None, output_directory: "checkpoints", batch_queue_size: 64 };
|
||||
|
||||
let paths: Vec<&str> = args.data.iter().map(String::as_str).collect();
|
||||
let data_loader = SfBinpackLoader::new_concat_multiple(&paths, 1024, LOADER_THREADS, filter);
|
||||
|
||||
trainer.run(&schedule, &settings, &data_loader);
|
||||
}
|
||||
Reference in new issue
Block a user