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:
rektdieandclaude committed 2026-11-15 21:27:08 +01:00
1 parent 9643c7b0a5
commit 1c9d2beec3
23 files changed
+2850

No files matched your search

@@ -0,0 +1,67 @@
#ifndef STUFF
#define IN_SIZE 1024
#endif
constexpr int in_size = IN_SIZE;
extern "C" __global__ void kernel(
const float* weights,
const float* input,
const int* moves,
const float* output_grad,
float* input_grad,
float* weights_grad,
float* biases_grad
) {
extern __shared__ float sdata[];
const int batch_size = gridDim.y;
const int loc_in_batch = blockIdx.y;
const int loc_in_moves = blockIdx.x;
const int tid = threadIdx.x;
const int locmb = loc_in_batch * 64 + loc_in_moves;
const int move = moves[locmb];
if (move != -1)
{
const float grd = output_grad[locmb];
const float4* tW = reinterpret_cast<const float4*>(weights + in_size * move);
const float4* tI = reinterpret_cast<const float4*>(input + in_size * loc_in_batch);
if (tid == 0) atomicAdd(biases_grad + move, grd);
for (int idx = tid; idx < in_size / 4; idx += blockDim.x)
{
const int section = 4 * blockDim.x * (idx / blockDim.x) + tid;
const float4 ti = tI[idx];
const float4 tw = tW[idx];
sdata[4 * tid ] = ti.x;
sdata[4 * tid + 1] = ti.y;
sdata[4 * tid + 2] = ti.z;
sdata[4 * tid + 3] = ti.w;
__syncthreads();
float* tWg = weights_grad + in_size * move + section;
atomicAdd(tWg , grd * sdata[tid ]);
atomicAdd(tWg + blockDim.x , grd * sdata[tid + blockDim.x ]);
atomicAdd(tWg + blockDim.x * 2, grd * sdata[tid + blockDim.x * 2]);
atomicAdd(tWg + blockDim.x * 3, grd * sdata[tid + blockDim.x * 3]);
__syncthreads();
sdata[4 * tid ] = tw.x;
sdata[4 * tid + 1] = tw.y;
sdata[4 * tid + 2] = tw.z;
sdata[4 * tid + 3] = tw.w;
__syncthreads();
float* tIg = input_grad + in_size * loc_in_batch + section;
atomicAdd(tIg , grd * sdata[tid ]);
atomicAdd(tIg + blockDim.x , grd * sdata[tid + blockDim.x ]);
atomicAdd(tIg + blockDim.x * 2, grd * sdata[tid + blockDim.x * 2]);
atomicAdd(tIg + blockDim.x * 3, grd * sdata[tid + blockDim.x * 3]);
__syncthreads();
}
}
}
@@ -0,0 +1,68 @@
#ifndef STUFF
#define THREADS 512
#define IN_SIZE 1024
#endif
constexpr int threads = THREADS;
constexpr int in_size = IN_SIZE;
extern "C" __global__ void kernel(
const float* weights,
const float* biases,
const float* input,
const int* moves,
float* output
) {
extern __shared__ float sdata[];
const int batch_size = gridDim.y;
const int loc_in_batch = blockIdx.y;
const int loc_in_moves = blockIdx.x;
const int tid = threadIdx.x;
const int locmb = loc_in_batch * 64 + loc_in_moves;
const int move = moves[locmb];
const float4* tW = reinterpret_cast<const float4*>(weights + in_size * move);
const float4* tI = reinterpret_cast<const float4*>(input + in_size * loc_in_batch);
if (move != -1)
{
float local = 0.0F;
#pragma unroll
for (int idx = tid; idx < in_size / 4; idx += threads)
{
const float4 tw = tW[idx];
const float4 ti = tI[idx];
local += tw.x * ti.x + tw.y * ti.y + tw.z * ti.z + tw.w * ti.w;
}
sdata[tid] = local;
__syncthreads();
if constexpr (threads >= 1024) { if (tid < 512) sdata[tid] += sdata[tid + 512]; __syncthreads(); }
if constexpr (threads >= 512) { if (tid < 256) sdata[tid] += sdata[tid + 256]; __syncthreads(); }
if constexpr (threads >= 256) { if (tid < 128) sdata[tid] += sdata[tid + 128]; __syncthreads(); }
if constexpr (threads >= 128) { if (tid < 64) sdata[tid] += sdata[tid + 64]; __syncthreads(); }
if (tid < 32)
{
float partial = sdata[tid];
if constexpr (threads >= 64) { partial += sdata[tid + 32]; }
partial += __shfl_down_sync(0xffffffff, partial, 16);
partial += __shfl_down_sync(0xffffffff, partial, 8);
partial += __shfl_down_sync(0xffffffff, partial, 4);
partial += __shfl_down_sync(0xffffffff, partial, 2);
partial += __shfl_down_sync(0xffffffff, partial, 1);
if (tid == 0)
{
output[locmb] = partial + biases[move];
}
}
}
else if (tid == 0)
{
output[locmb] = -10000.0F;
}
}