File size: 46,340 Bytes
5d1bf1e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 | //! A small policy and value network, run on the CPU in plain Rust (#390 Track B). Trained by `rust/rl` (PyTorch),
//! written as a `.pmnet` file: one line of JSON naming each tensor and its shape, then the tensors as little-endian
//! f32 in that order.
//!
//! Fast CPU execution:
//! - Linear weights are repacked at load time into `[out_blocks, inp, 8]` panels (`wp`) for a 4x8 register-blocked
//! SIMD GEMM (`matmul_rows`) that vectorizes cleanly to AVX2 FMA on x86_64 and NEON FMLA on ARM64 without horizontal
//! reductions.
//! - `Tx::forward` and `Tx::state` reuse a thread-local flat `TxWorkspace` (zero per-layer heap allocations and no
//! `Vec<Vec<f32>>` Q/K/V copies).
//! - Supports both v1 `Tx` checkpoints (ReLU + linear value head) and upgraded `Tx` checkpoints (`val_mlp=1` dedicated
//! 2-layer value head reading `[t0, mean_ent, max_ent, glob]`, and `gelu=1` tanh-approx GELU).
use crate::obs::{self, Obs, ACT_F, ENT_F, GLOB_F, GLOB_F_V1, GLOB_F_V2, NO_ENT, NUM_REL_TYPES};
use std::cell::RefCell;
use std::collections::HashMap;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::sync::{Arc, Condvar, Mutex, OnceLock};
use std::time::{Duration, Instant};
#[inline(always)]
fn act_val(x: f32, gelu: bool) -> f32 {
if gelu {
// Matches PyTorch F.gelu(x, approximate="tanh")
const K: f32 = 0.797_884_6; // sqrt(2 / pi)
0.5 * x * (1.0 + (K * (x + 0.044_715 * x * x * x)).tanh())
} else {
x.max(0.0)
}
}
/// A dense layer: `w` is `[out][in]` flat, plus `wp` repacked into `[out/8][in][8]` panels for 4x8 SIMD GEMM.
#[derive(Clone, Debug, Default)]
pub struct Linear {
pub w: Vec<f32>,
pub b: Vec<f32>,
pub inp: usize,
pub out: usize,
/// Repacked weights: `blocks = out / 8` panels of `inp * 8` floats (contiguous 8 output lanes per `k`).
wp: Vec<f32>,
}
impl Linear {
pub fn new(w: Vec<f32>, b: Vec<f32>, out: usize, inp: usize) -> Linear {
let blocks = out / 8;
let mut wp = vec![0f32; blocks * inp * 8];
for blk in 0..blocks {
let ob = blk * 8;
let dst_blk = &mut wp[blk * inp * 8..(blk + 1) * inp * 8];
for k in 0..inp {
let dst = &mut dst_blk[k * 8..(k + 1) * 8];
for j in 0..8 {
dst[j] = w[(ob + j) * inp + k];
}
}
}
Linear { w, b, inp, out, wp }
}
/// Single-row apply into `y` (`y` resized to `self.out`).
#[inline]
fn apply(&self, x: &[f32], y: &mut Vec<f32>, relu: bool) {
self.apply_act(x, y, relu, false);
}
fn apply_act(&self, x: &[f32], y: &mut Vec<f32>, do_act: bool, gelu: bool) {
debug_assert_eq!(x.len(), self.inp);
y.resize(self.out, 0.0);
let inp = self.inp;
let blocks = self.out / 8;
for blk in 0..blocks {
let ob = blk * 8;
let mut acc = [
self.b[ob],
self.b[ob + 1],
self.b[ob + 2],
self.b[ob + 3],
self.b[ob + 4],
self.b[ob + 5],
self.b[ob + 6],
self.b[ob + 7],
];
let panel = &self.wp[blk * inp * 8..(blk + 1) * inp * 8];
for (k, &xk) in x.iter().enumerate() {
let w = &panel[k * 8..(k + 1) * 8];
for j in 0..8 {
acc[j] += xk * w[j];
}
}
if do_act {
for j in 0..8 {
y[ob + j] = act_val(acc[j], gelu);
}
} else {
y[ob..ob + 8].copy_from_slice(&acc);
}
}
for o in blocks * 8..self.out {
let row = &self.w[o * inp..(o + 1) * inp];
let s = self.b[o] + dot(row, x);
y[o] = if do_act { act_val(s, gelu) } else { s };
}
}
/// Flat batched matmul: `x` is `[rows, inp]`, writes `y` as `[rows, out]`.
/// Uses `matrixmultiply::sgemm` (AVX2+FMA / NEON cache-blocked microkernels) for multi-row projections,
/// and the packed 8-lane kernel for single rows.
fn matmul_rows(&self, x: &[f32], rows: usize, y: &mut Vec<f32>, do_act: bool, gelu: bool, add_to_y: bool) {
let inp = self.inp;
let out = self.out;
debug_assert_eq!(x.len(), rows * inp);
if rows == 0 {
if !add_to_y {
y.clear();
}
return;
}
if rows == 1 && !add_to_y {
self.apply_act(x, y, do_act, gelu);
return;
}
if !add_to_y {
y.resize(rows * out, 0.0);
} else {
debug_assert_eq!(y.len(), rows * out);
debug_assert!(!do_act, "add_to_y with activation not supported");
}
let beta = if add_to_y { 1.0 } else { 0.0 };
unsafe {
matrixmultiply::sgemm(
rows,
inp,
out,
1.0,
x.as_ptr(),
inp as isize,
1,
self.w.as_ptr(),
1,
inp as isize,
beta,
y.as_mut_ptr(),
out as isize,
1,
);
}
if do_act {
if gelu {
for r in 0..rows {
let row = &mut y[r * out..(r + 1) * out];
for (dst, &b) in row.iter_mut().zip(&self.b) {
*dst = act_val(*dst + b, true);
}
}
} else {
for r in 0..rows {
let row = &mut y[r * out..(r + 1) * out];
for (dst, &b) in row.iter_mut().zip(&self.b) {
*dst = (*dst + b).max(0.0);
}
}
}
} else {
for r in 0..rows {
let row = &mut y[r * out..(r + 1) * out];
for (dst, &b) in row.iter_mut().zip(&self.b) {
*dst += b;
}
}
}
}
}
/// A dot product in 8 lanes: the compiler vectorizes it (one running sum can't be reordered, so it doesn't)
#[inline]
pub fn dot(a: &[f32], b: &[f32]) -> f32 {
let n = a.len().min(b.len());
let (a, b) = (&a[..n], &b[..n]);
let mut acc = [0f32; 8];
let (ac, ar) = a.as_chunks::<8>();
let (bc, br) = b.as_chunks::<8>();
for (x, y) in ac.iter().zip(bc) {
for i in 0..8 {
acc[i] += x[i] * y[i];
}
}
let mut s = acc.iter().sum::<f32>();
for (x, y) in ar.iter().zip(br) {
s += x * y;
}
s
}
/// The first network (#390): entities pooled by mean and max
#[derive(Debug, Clone)]
pub struct Mlp {
pub name: String,
pub emb: Vec<f32>,
pub dim: usize,
pub ent1: Linear,
pub ent2: Linear,
pub glob1: Linear,
pub trunk1: Linear,
pub trunk2: Linear,
pub value: Linear,
pub act1: Linear,
pub act2: Linear,
pub query: Linear,
pub abias: Linear,
pub belief: Option<Linear>,
}
/// The network's outputs for one observation
#[derive(Debug, Clone, Default)]
pub struct Output {
/// one logit per action
pub logits: Vec<f32>,
/// the logit of the seat winning
pub value: f32,
/// the state, for the belief
pub h: Vec<f32>,
}
impl Mlp {
/// Reads a `.pmnet` file's bytes
pub fn parse(name: &str, bytes: &[u8]) -> Result<Mlp, String> {
let (_, mut tensors) = read(bytes)?;
let mut take = |base: &str| -> Result<Linear, String> { take_linear(&mut tensors, base) };
let ent1 = take("ent1")?;
let ent2 = take("ent2")?;
let glob1 = take("glob1")?;
let trunk1 = take("trunk1")?;
let trunk2 = take("trunk2")?;
let value = take("value")?;
let act1 = take("act1")?;
let act2 = take("act2")?;
let query = take("query")?;
let abias = take("abias")?;
let belief = take("belief").ok();
let (es, emb) = tensors.remove("emb.weight").ok_or("no emb.weight")?;
let dim = es[1];
if ent1.inp != dim + ENT_F || (glob1.inp != GLOB_F && glob1.inp != GLOB_F_V2 && glob1.inp != GLOB_F_V1) || act1.inp != dim + ACT_F {
return Err(format!(
"{name}: made for other features (entity {}, global {}, action {}; this build has {}, {} or {}, {})",
ent1.inp - dim,
glob1.inp,
act1.inp - dim,
ENT_F,
GLOB_F_V1,
GLOB_F,
ACT_F
));
}
Ok(Mlp {
name: name.to_string(),
emb,
dim,
ent1,
ent2,
glob1,
trunk1,
trunk2,
value,
act1,
act2,
query,
abias,
belief,
})
}
fn embedding(&self, id: u32) -> &[f32] {
let i = (id as usize).min(self.emb.len() / self.dim - 1);
&self.emb[i * self.dim..(i + 1) * self.dim]
}
/// The state `h` and the value
pub fn state(&self, o: &Obs) -> (Vec<f32>, f32) {
let h1 = self.ent2.out;
let mut mean = vec![0f32; h1];
let mut max = vec![0f32; h1];
let n = o.n_ents();
let (mut x, mut y, mut z) = (Vec::with_capacity(self.ent1.inp), Vec::new(), Vec::new());
for i in 0..n {
x.clear();
x.extend_from_slice(self.embedding(o.ent_ids[i]));
x.extend_from_slice(&o.ents[i * ENT_F..(i + 1) * ENT_F]);
self.ent1.apply(&x, &mut y, true);
self.ent2.apply(&y, &mut z, true);
for k in 0..h1 {
mean[k] += z[k] / n as f32;
max[k] = if i == 0 { z[k] } else { max[k].max(z[k]) };
}
}
let mut gbuf = [0f32; GLOB_F];
let gn = self.glob1.inp.min(o.glob.len());
gbuf[..gn].copy_from_slice(&o.glob[..gn]);
self.glob1.apply(&gbuf[..self.glob1.inp], &mut y, true);
let mut t = mean;
t.extend(max);
t.extend_from_slice(&y);
self.trunk1.apply(&t, &mut y, true);
let mut h = Vec::new();
self.trunk2.apply(&y, &mut h, true);
self.value.apply(&h, &mut y, false);
(h, y[0])
}
/// Every action's logit, and the value
pub fn forward(&self, o: &Obs) -> Output {
let (h, value) = self.state(o);
let mut q = Vec::new();
self.query.apply(&h, &mut q, false);
let (mut x, mut y, mut a, mut b) = (Vec::with_capacity(self.act1.inp), Vec::new(), Vec::new(), Vec::new());
let mut logits = Vec::with_capacity(o.n_acts());
for i in 0..o.n_acts() {
x.clear();
x.extend_from_slice(self.embedding(o.act_ids[i]));
x.extend_from_slice(&o.acts[i * ACT_F..(i + 1) * ACT_F]);
self.act1.apply(&x, &mut y, true);
self.act2.apply(&y, &mut a, false);
self.abias.apply(&a, &mut b, false);
logits.push(dot(&q, &a) + b[0]);
}
Output { logits, value, h }
}
/// Each card's logit of being in the opponent's hand, from the state `h` (0 for a net without a belief head)
pub fn belief(&self, h: &[f32], cards: &[u32]) -> Vec<f32> {
let Some(bl) = &self.belief else { return vec![0.0; cards.len()] };
let mut q = Vec::new();
bl.apply(h, &mut q, false);
cards.iter().map(|c| dot(self.embedding(*c), &q)).collect()
}
}
/// A `.pmnet` file's header and tensors by name
type Tensors = HashMap<String, (Vec<usize>, Vec<f32>)>;
fn read(bytes: &[u8]) -> Result<(serde_json::Value, Tensors), String> {
let nl = bytes.iter().position(|b| *b == b'\n').ok_or("no header line")?;
let header: serde_json::Value = serde_json::from_slice(&bytes[..nl]).map_err(|e| e.to_string())?;
let mut data = &bytes[nl + 1..];
let mut tensors = HashMap::new();
for t in header["tensors"].as_array().ok_or("no tensors")? {
let tname = t[0].as_str().ok_or("tensor name")?.to_string();
let shape: Vec<usize> =
t[1].as_array().ok_or("tensor shape")?.iter().map(|v| v.as_u64().unwrap_or(0) as usize).collect();
let n: usize = shape.iter().product();
if data.len() < n * 4 {
return Err(format!("{tname}: file too short"));
}
let v = data[..n * 4].as_chunks::<4>().0.iter().map(|c| f32::from_le_bytes(*c)).collect();
data = &data[n * 4..];
tensors.insert(tname, (shape, v));
}
Ok((header, tensors))
}
#[derive(Clone, Debug, Default)]
pub struct LayerNorm {
pub w: Vec<f32>,
pub b: Vec<f32>,
}
impl LayerNorm {
#[inline]
fn apply_slice(&self, x: &[f32], y: &mut [f32]) {
let n = x.len() as f32;
let mean = x.iter().sum::<f32>() / n;
let var = x.iter().map(|&v| { let d = v - mean; d * d }).sum::<f32>() / n;
let inv = 1.0 / (var + 1e-5).sqrt();
for ((&v, (&w, &b)), dst) in x.iter().zip(self.w.iter().zip(&self.b)).zip(y.iter_mut()) {
*dst = (v - mean) * inv * w + b;
}
}
fn apply_rows(&self, x: &[f32], rows: usize, y: &mut Vec<f32>) {
let d = self.w.len();
y.resize(rows * d, 0.0);
for r in 0..rows {
self.apply_slice(&x[r * d..(r + 1) * d], &mut y[r * d..(r + 1) * d]);
}
}
}
#[derive(Clone, Debug, Default)]
pub struct Block {
pub ln1: LayerNorm,
pub qkv: Linear,
pub out: Linear,
pub ln2: LayerNorm,
pub ff1: Linear,
pub ff2: Linear,
}
#[derive(Clone, Debug)]
pub struct ValMlp {
pub ln: LayerNorm,
pub fc1: Linear,
pub fc2: Linear,
}
/// Reusable thread-local scratch buffers for `Tx::forward` / `Tx::state` (zero heap allocs in the hot loop).
#[derive(Default)]
struct TxWorkspace {
ent_in_buf: Vec<f32>,
x: Vec<f32>,
norm: Vec<f32>,
qkv: Vec<f32>,
att: Vec<f32>,
ff: Vec<f32>,
scores: Vec<f32>,
act_in_buf: Vec<f32>,
act: Vec<f32>,
act_norm: Vec<f32>,
act_q: Vec<f32>,
act_kv: Vec<f32>,
act_att: Vec<f32>,
act_bias: Vec<f32>,
tmp1: Vec<f32>,
tmp2: Vec<f32>,
tmp3: Vec<f32>,
}
thread_local! {
static TX_WS: RefCell<TxWorkspace> = RefCell::new(TxWorkspace::default());
}
/// The transformer network (#400 R2; `rust/rl/model.py` `Tx`): a global token and one per entity through pre-norm
/// self-attention blocks, then each candidate attending to the tokens once.
#[derive(Clone, Debug, Default)]
pub struct Tx {
pub name: String,
pub emb: Vec<f32>,
pub dim: usize,
pub d: usize,
pub heads: usize,
pub gelu: bool,
pub glob_f: usize,
pub rel_bias: Option<Vec<f32>>,
pub act_ent: bool,
pub ent_in: Linear,
pub glob_in: Linear,
pub blocks: Vec<Block>,
pub ln_f: LayerNorm,
pub trunk1: Linear,
pub value: Option<Linear>,
pub val_mlp: Option<ValMlp>,
pub belief: Linear,
pub act_in: Linear,
pub act_ln: LayerNorm,
pub act_q: Linear,
pub act_kv: Linear,
pub act_o: Linear,
pub act_ff: Linear,
pub query: Linear,
pub abias: Linear,
}
/// Flat multi-head attention reading strided Q, K, V directly from buffers (`qkv` or `act_q`/`act_kv`).
#[allow(clippy::too_many_arguments)]
fn attend_strided(
q: &[f32],
q_stride: usize,
q_off: usize,
nq: usize,
k: &[f32],
k_stride: usize,
k_off: usize,
v: &[f32],
v_stride: usize,
v_off: usize,
nk: usize,
d: usize,
heads: usize,
rel: Option<(&[u8], &[u8], &[f32])>,
out: &mut Vec<f32>,
scores: &mut Vec<f32>,
) {
let hd = d / heads;
let scale = 1.0 / (hd as f32).sqrt();
out.resize(nq * d, 0.0);
out.fill(0.0);
scores.resize(nk, 0.0);
for qi in 0..nq {
let q_base = qi * q_stride + q_off;
let out_base = qi * d;
for h in 0..heads {
let ho = h * hd;
let qr = &q[q_base + ho..q_base + ho + hd];
let mut m = f32::MIN;
for j in 0..nk {
let kr = &k[j * k_stride + k_off + ho..j * k_stride + k_off + ho + hd];
let s = dot(qr, kr) * scale;
scores[j] = s;
m = m.max(s);
}
if let Some((r_idx, r_typ, rb)) = rel {
if qi >= 1 {
let k_ent = qi - 1;
let ne = r_idx.len().min(nk.saturating_sub(1));
if k_ent < ne {
let rt = r_typ[k_ent] as usize;
if rt >= 1 && rt <= NUM_REL_TYPES {
let tgt = r_idx[k_ent] as usize;
if tgt < ne {
let j = 1 + tgt;
scores[j] += rb[(rt - 1) * heads + h];
m = m.max(scores[j]);
}
}
for j_ent in 0..ne {
let rt = r_typ[j_ent] as usize;
if rt >= 1 && rt <= NUM_REL_TYPES && r_idx[j_ent] as usize == k_ent {
let j = 1 + j_ent;
scores[j] += rb[(NUM_REL_TYPES + rt - 1) * heads + h];
m = m.max(scores[j]);
}
}
}
}
}
let mut z = 0.0f32;
for s in scores.iter_mut() {
*s = (*s - m).exp();
z += *s;
}
let inv_z = 1.0 / z;
let o = &mut out[out_base + ho..out_base + ho + hd];
for j in 0..nk {
let p = scores[j] * inv_z;
let vr = &v[j * v_stride + v_off + ho..j * v_stride + v_off + ho + hd];
for (oo, &vv) in o.iter_mut().zip(vr) {
*oo += p * vv;
}
}
}
}
}
fn take_linear(t: &mut Tensors, b: &str) -> Result<Linear, String> {
let (ws, w) = t.remove(&format!("{b}.weight")).ok_or(format!("no {b}.weight"))?;
let (_, bb) = t.remove(&format!("{b}.bias")).ok_or(format!("no {b}.bias"))?;
Ok(Linear::new(w, bb, ws[0], ws[1]))
}
fn take_norm(t: &mut Tensors, b: &str) -> Result<LayerNorm, String> {
let (_, w) = t.remove(&format!("{b}.weight")).ok_or(format!("no {b}.weight"))?;
let (_, bb) = t.remove(&format!("{b}.bias")).ok_or(format!("no {b}.bias"))?;
Ok(LayerNorm { w, b: bb })
}
impl Tx {
pub fn parse(name: &str, bytes: &[u8]) -> Result<Tx, String> {
let (header, mut t) = read(bytes)?;
let cfg = &header["config"];
let d = cfg["d"].as_u64().ok_or("no d")? as usize;
let heads = cfg["heads"].as_u64().ok_or("no heads")? as usize;
let layers = cfg["layers"].as_u64().ok_or("no layers")? as usize;
let gelu = cfg["gelu"].as_u64().unwrap_or(0) != 0;
let use_val_mlp = cfg["val_mlp"].as_u64().unwrap_or(0) != 0;
let use_rel = cfg["rel"].as_u64().unwrap_or(0) != 0;
let act_ent = cfg["act_ent"].as_u64().unwrap_or(0) != 0;
let t = &mut t;
let mut blocks = Vec::new();
for i in 0..layers {
let p = format!("blocks.{i}");
blocks.push(Block {
ln1: take_norm(t, &format!("{p}.ln1"))?,
qkv: take_linear(t, &format!("{p}.qkv"))?,
out: take_linear(t, &format!("{p}.out"))?,
ln2: take_norm(t, &format!("{p}.ln2"))?,
ff1: take_linear(t, &format!("{p}.ff1"))?,
ff2: take_linear(t, &format!("{p}.ff2"))?,
});
}
let (es, emb) = t.remove("emb.weight").ok_or("no emb.weight")?;
let rel_bias = if use_rel {
let (rs, rb) = t.remove("rel_bias").ok_or("no rel_bias")?;
if rs != vec![2 * NUM_REL_TYPES, heads] {
return Err(format!("{name}: bad rel_bias shape {rs:?}"));
}
Some(rb)
} else {
None
};
let (value, val_mlp) = if use_val_mlp {
(
None,
Some(ValMlp {
ln: take_norm(t, "val_ln")?,
fc1: take_linear(t, "val_fc1")?,
fc2: take_linear(t, "val_fc2")?,
}),
)
} else {
(Some(take_linear(t, "value")?), None)
};
let glob_in = take_linear(t, "glob_in")?;
let glob_f = glob_in.inp;
let net = Tx {
name: name.to_string(),
emb,
dim: es[1],
d,
heads,
gelu,
glob_f,
rel_bias,
act_ent,
ent_in: take_linear(t, "ent_in")?,
glob_in,
blocks,
ln_f: take_norm(t, "ln_f")?,
trunk1: take_linear(t, "trunk1")?,
value,
val_mlp,
belief: take_linear(t, "belief")?,
act_in: take_linear(t, "act_in")?,
act_ln: take_norm(t, "act_ln")?,
act_q: take_linear(t, "act_q")?,
act_kv: take_linear(t, "act_kv")?,
act_o: take_linear(t, "act_o")?,
act_ff: take_linear(t, "act_ff")?,
query: take_linear(t, "query")?,
abias: take_linear(t, "abias")?,
};
let want_act = net.dim + ACT_F + if net.act_ent { net.d } else { 0 };
if net.ent_in.inp != net.dim + ENT_F || (net.glob_f != GLOB_F && net.glob_f != GLOB_F_V2 && net.glob_f != GLOB_F_V1) || net.act_in.inp != want_act {
return Err(format!("{name}: made for other features than this build's"));
}
Ok(net)
}
#[inline]
fn embedding(&self, id: u32) -> &[f32] {
let i = (id as usize).min(self.emb.len() / self.dim - 1);
&self.emb[i * self.dim..(i + 1) * self.dim]
}
/// Runs the transformer encoder into `ws.norm` (shape `[1 + n_ents, d]`), returning `nt = 1 + n_ents`.
fn run_tokens(&self, o: &Obs, ws: &mut TxWorkspace) -> usize {
let n_ents = o.n_ents();
let nt = 1 + n_ents;
let d = self.d;
ws.x.resize(nt * d, 0.0);
// Row 0: global token (slice/pad to self.glob_f so v1=65 and v2=81 models both work)
let mut gbuf = [0f32; GLOB_F];
let gn = self.glob_f.min(o.glob.len());
gbuf[..gn].copy_from_slice(&o.glob[..gn]);
self.glob_in.apply_act(&gbuf[..self.glob_f], &mut ws.tmp1, false, false);
ws.x[..d].copy_from_slice(&ws.tmp1);
// Rows 1..nt: entity tokens (when an Obs v2 81-glob obs is fed to a legacy v1 glob_f=65 model, skip
// graveyard/exile tokens that v1 never saw during training, while keeping legacy 65-glob parity fixtures exact)
let v1_filter = self.glob_f == GLOB_F_V1 && o.glob.len() > GLOB_F_V1;
let mut kept = 0usize;
if n_ents > 0 {
let ein = self.dim + ENT_F;
ws.ent_in_buf.clear();
ws.ent_in_buf.reserve(n_ents * ein);
for i in 0..n_ents {
let ef = &o.ents[i * ENT_F..(i + 1) * ENT_F];
if v1_filter && (ef[4] > 0.5 || ef[5] > 0.5) {
continue;
}
ws.ent_in_buf.extend_from_slice(self.embedding(o.ent_ids[i]));
ws.ent_in_buf.extend_from_slice(ef);
kept += 1;
}
if kept > 0 {
self.ent_in.matmul_rows(&ws.ent_in_buf, kept, &mut ws.tmp2, false, false, false);
ws.x.resize((1 + kept) * d, 0.0);
ws.x[d..(1 + kept) * d].copy_from_slice(&ws.tmp2);
} else {
ws.x.resize(d, 0.0);
}
}
let nt = 1 + kept;
let rel = self.rel_bias.as_deref().and_then(|rb| {
if o.ent_rel_idx.len() == n_ents && o.ent_rel_type.len() == n_ents {
Some((o.ent_rel_idx.as_slice(), o.ent_rel_type.as_slice(), rb))
} else {
None
}
});
for b in &self.blocks {
b.ln1.apply_rows(&ws.x, nt, &mut ws.norm);
b.qkv.matmul_rows(&ws.norm, nt, &mut ws.qkv, false, false, false);
attend_strided(
&ws.qkv,
3 * d,
0,
nt,
&ws.qkv,
3 * d,
d,
&ws.qkv,
3 * d,
2 * d,
nt,
d,
self.heads,
rel,
&mut ws.att,
&mut ws.scores,
);
b.out.matmul_rows(&ws.att, nt, &mut ws.x, false, false, true);
b.ln2.apply_rows(&ws.x, nt, &mut ws.norm);
b.ff1.matmul_rows(&ws.norm, nt, &mut ws.ff, true, self.gelu, false);
b.ff2.matmul_rows(&ws.ff, nt, &mut ws.x, false, false, true);
}
self.ln_f.apply_rows(&ws.x, nt, &mut ws.norm);
nt
}
/// Computes state `h` and value logit from final normed tokens in `ws.norm` (`[nt, d]`).
fn run_head(&self, o: &Obs, nt: usize, ws: &mut TxWorkspace) -> (Vec<f32>, f32) {
let d = self.d;
let t = &ws.norm;
let n_ents = nt - 1;
ws.tmp1.resize(2 * d, 0.0);
ws.tmp1[..d].copy_from_slice(&t[..d]);
let mean = &mut ws.tmp1[d..2 * d];
mean.fill(0.0);
if n_ents > 0 {
let inv = 1.0 / n_ents as f32;
for i in 1..nt {
let row = &t[i * d..(i + 1) * d];
for (m, &r) in mean.iter_mut().zip(row) {
*m += r * inv;
}
}
}
let mut h = Vec::with_capacity(self.trunk1.out);
self.trunk1.apply_act(&ws.tmp1, &mut h, true, self.gelu);
let value = if let Some(vm) = &self.val_mlp {
let vin = 3 * d + self.glob_f;
ws.tmp2.resize(vin, 0.0);
ws.tmp2[..2 * d].copy_from_slice(&ws.tmp1[..2 * d]);
let mx = &mut ws.tmp2[2 * d..3 * d];
if n_ents > 0 {
mx.copy_from_slice(&t[d..2 * d]);
for i in 2..nt {
let row = &t[i * d..(i + 1) * d];
for (m, &r) in mx.iter_mut().zip(row) {
if r > *m {
*m = r;
}
}
}
} else {
mx.fill(0.0);
}
let gn = self.glob_f.min(o.glob.len());
ws.tmp2[3 * d..3 * d + gn].copy_from_slice(&o.glob[..gn]);
if gn < self.glob_f {
ws.tmp2[3 * d + gn..vin].fill(0.0);
}
ws.tmp3.resize(vin, 0.0);
vm.ln.apply_slice(&ws.tmp2, &mut ws.tmp3);
vm.fc1.apply_act(&ws.tmp3, &mut ws.tmp1, true, self.gelu);
vm.fc2.apply_act(&ws.tmp1, &mut ws.tmp2, false, false);
ws.tmp2[0]
} else {
self.value.as_ref().unwrap().apply_act(&h, &mut ws.tmp2, false, false);
ws.tmp2[0]
};
(h, value)
}
pub fn state(&self, o: &Obs) -> (Vec<f32>, f32) {
TX_WS.with(|cell| {
let mut guard = cell.borrow_mut();
let ws = &mut *guard;
let nt = self.run_tokens(o, ws);
self.run_head(o, nt, ws)
})
}
pub fn forward(&self, o: &Obs) -> Output {
TX_WS.with(|cell| {
let mut guard = cell.borrow_mut();
let ws = &mut *guard;
let nt = self.run_tokens(o, ws);
let (h, value) = self.run_head(o, nt, ws);
let na = o.n_acts();
if na == 0 {
return Output { logits: Vec::new(), value, h };
}
let d = self.d;
// Key/Value over all tokens
self.act_kv.matmul_rows(&ws.norm, nt, &mut ws.act_kv, false, false, false);
// Query from state h
self.query.apply_act(&h, &mut ws.tmp1, false, false);
// Action inputs (optionally concatenated with contextualized entity token `ws.norm[1 + act_ent_idx[i]]`)
let n_ents = nt - 1;
let ain = self.dim + ACT_F + if self.act_ent { d } else { 0 };
ws.act_in_buf.resize(na * ain, 0.0);
for i in 0..na {
let dst = &mut ws.act_in_buf[i * ain..(i + 1) * ain];
dst[..self.dim].copy_from_slice(self.embedding(o.act_ids[i]));
dst[self.dim..self.dim + ACT_F].copy_from_slice(&o.acts[i * ACT_F..(i + 1) * ACT_F]);
if self.act_ent {
let ei = o.act_ent_idx.get(i).copied().unwrap_or(NO_ENT) as usize;
let ctx = &mut dst[self.dim + ACT_F..];
if ei < n_ents {
ctx.copy_from_slice(&ws.norm[(1 + ei) * d..(2 + ei) * d]);
} else {
ctx.fill(0.0);
}
}
}
self.act_in.matmul_rows(&ws.act_in_buf, na, &mut ws.act, false, false, false);
self.act_ln.apply_rows(&ws.act, na, &mut ws.act_norm);
self.act_q.matmul_rows(&ws.act_norm, na, &mut ws.act_q, false, false, false);
attend_strided(
&ws.act_q,
d,
0,
na,
&ws.act_kv,
2 * d,
0,
&ws.act_kv,
2 * d,
d,
nt,
d,
self.heads,
None,
&mut ws.act_att,
&mut ws.scores,
);
self.act_o.matmul_rows(&ws.act_att, na, &mut ws.act, false, false, true);
// Residual FFN: a = a + act(act_ff(a))
self.act_ff.matmul_rows(&ws.act, na, &mut ws.act_norm, true, self.gelu, false);
for (dst, &u) in ws.act.iter_mut().zip(&ws.act_norm) {
*dst += u;
}
self.abias.matmul_rows(&ws.act, na, &mut ws.act_bias, false, false, false);
let mut logits = Vec::with_capacity(na);
for i in 0..na {
logits.push(dot(&ws.tmp1, &ws.act[i * d..(i + 1) * d]) + ws.act_bias[i]);
}
Output { logits, value, h }
})
}
pub fn belief(&self, h: &[f32], cards: &[u32]) -> Vec<f32> {
let mut q = Vec::new();
self.belief.apply_act(h, &mut q, false, false);
cards.iter().map(|c| dot(self.embedding(*c), &q)).collect()
}
}
struct PendingReq {
obs: Obs,
reply: Arc<(Mutex<Option<Output>>, Condvar)>,
}
struct BatchQueue {
reqs: Vec<PendingReq>,
closed: bool,
}
/// Batched TCP client for `rust/rl/infer_server.py` (GPU-accelerated self-play).
pub struct RemoteNet {
pub name: String,
queue: Arc<(Mutex<BatchQueue>, Condvar)>,
emb: Vec<f32>,
dim: usize,
belief: Option<Linear>,
}
impl std::fmt::Debug for RemoteNet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RemoteNet").field("name", &self.name).field("dim", &self.dim).finish()
}
}
fn read_u32(s: &mut TcpStream) -> std::io::Result<u32> {
let mut b = [0u8; 4];
s.read_exact(&mut b)?;
Ok(u32::from_le_bytes(b))
}
fn read_f32s(s: &mut TcpStream, n: usize) -> std::io::Result<Vec<f32>> {
let mut bytes = vec![0u8; n * 4];
s.read_exact(&mut bytes)?;
Ok(bytes.as_chunks::<4>().0.iter().map(|c| f32::from_le_bytes(*c)).collect())
}
impl RemoteNet {
pub fn connect(url: &str) -> Result<RemoteNet, String> {
let addr = url.strip_prefix("tcp://").ok_or("expected tcp://host:port")?;
let mut stream = TcpStream::connect(addr).map_err(|e| format!("{url}: {e}"))?;
stream.set_nodelay(true).ok();
let dim = read_u32(&mut stream).map_err(|e| format!("{url} handshake: {e}"))? as usize;
let buckets = read_u32(&mut stream).map_err(|e| format!("{url} handshake: {e}"))? as usize;
let trunk = read_u32(&mut stream).map_err(|e| format!("{url} handshake: {e}"))? as usize;
let mut has_b = [0u8; 1];
stream.read_exact(&mut has_b).map_err(|e| format!("{url} handshake: {e}"))?;
let (emb, belief) = if has_b[0] != 0 {
let emb = read_f32s(&mut stream, buckets * dim).map_err(|e| format!("{url} emb: {e}"))?;
let bw = read_f32s(&mut stream, dim * trunk).map_err(|e| format!("{url} bw: {e}"))?;
let bb = read_f32s(&mut stream, dim).map_err(|e| format!("{url} bb: {e}"))?;
(emb, Some(Linear::new(bw, bb, dim, trunk)))
} else {
(Vec::new(), None)
};
let queue = Arc::new((Mutex::new(BatchQueue { reqs: Vec::new(), closed: false }), Condvar::new()));
let q_bg = Arc::clone(&queue);
let max_batch: usize = std::env::var("PLAYMAT_GPU_BATCH").ok().and_then(|v| v.parse().ok()).unwrap_or(64);
// Default 0: never call wait_timeout on Windows (where sub-15.6ms sleeps round up to 15.6ms!);
// natural coalescing happens while the previous GPU batch is in flight.
let wait_us: u64 = std::env::var("PLAYMAT_GPU_WAIT_US").ok().and_then(|v| v.parse().ok()).unwrap_or(0);
std::thread::spawn(move || {
let mut payload = Vec::<u8>::with_capacity(2 << 20);
let mut resp_buf = Vec::<u8>::with_capacity(1 << 18);
loop {
let batch = {
let (lock, cv) = &*q_bg;
let mut g = lock.lock().unwrap();
while g.reqs.is_empty() && !g.closed {
g = cv.wait(g).unwrap();
}
if g.reqs.is_empty() && g.closed {
break;
}
if g.reqs.len() < max_batch && wait_us > 0 {
let deadline = Instant::now() + Duration::from_micros(wait_us);
while g.reqs.len() < max_batch && !g.closed {
let rem = deadline.saturating_duration_since(Instant::now());
if rem.is_zero() {
break;
}
let (ng, to) = cv.wait_timeout(g, rem).unwrap();
g = ng;
if to.timed_out() {
break;
}
}
}
let take = g.reqs.len().min(max_batch);
g.reqs.drain(..take).collect::<Vec<_>>()
};
let b = batch.len();
let max_e = batch.iter().map(|r| r.obs.n_ents()).max().unwrap_or(0);
let max_a = batch.iter().map(|r| r.obs.n_acts()).max().unwrap_or(0);
let w_e = ((max_e + 7) / 8 * 8).max(8);
let w_a = ((max_a + 3) / 4 * 4).max(4);
// Pre-pack 10 flat contiguous arrays so Python infer_server.py runs with ZERO Python loops
let glob_bytes = b * GLOB_F * 4;
let eids_bytes = b * w_e * 2;
let ents_bytes = b * w_e * ENT_F * 4;
let em_bytes = b * w_e;
let eridx_bytes = b * w_e;
let ertyp_bytes = b * w_e;
let aids_bytes = b * w_a * 2;
let acts_bytes = b * w_a * ACT_F * 4;
let am_bytes = b * w_a;
let aent_bytes = b * w_a;
let total_bytes = 12
+ glob_bytes
+ eids_bytes
+ ents_bytes
+ em_bytes
+ eridx_bytes
+ ertyp_bytes
+ aids_bytes
+ acts_bytes
+ am_bytes
+ aent_bytes;
payload.clear();
payload.resize(total_bytes, 0);
payload[0..4].copy_from_slice(&(b as u32).to_le_bytes());
payload[4..8].copy_from_slice(&(w_e as u32).to_le_bytes());
payload[8..12].copy_from_slice(&(w_a as u32).to_le_bytes());
let o_glob = 12;
let o_eids = o_glob + glob_bytes;
let o_ents = o_eids + eids_bytes;
let o_em = o_ents + ents_bytes;
let o_eridx = o_em + em_bytes;
let o_ertyp = o_eridx + eridx_bytes;
let o_aids = o_ertyp + ertyp_bytes;
let o_acts = o_aids + aids_bytes;
let o_am = o_acts + acts_bytes;
let o_aent = o_am + am_bytes;
// Default sentinel NO_ENT (255) for entity/action indices
payload[o_eridx..o_eridx + eridx_bytes].fill(NO_ENT);
payload[o_aent..o_aent + aent_bytes].fill(NO_ENT);
for (i, req) in batch.iter().enumerate() {
let o = &req.obs;
// glob
let gd = &mut payload[o_glob + i * GLOB_F * 4..o_glob + (i + 1) * GLOB_F * 4];
for (k, &v) in o.glob.iter().take(GLOB_F).enumerate() {
gd[k * 4..(k + 1) * 4].copy_from_slice(&v.to_le_bytes());
}
// entities
let ne = o.n_ents().min(w_e);
if ne > 0 {
let id_base = o_eids + i * w_e * 2;
for k in 0..ne {
payload[id_base + k * 2..id_base + k * 2 + 2]
.copy_from_slice(&(o.ent_ids[k] as u16).to_le_bytes());
}
let ef_base = o_ents + i * w_e * ENT_F * 4;
for (k, &v) in o.ents[..ne * ENT_F].iter().enumerate() {
payload[ef_base + k * 4..ef_base + k * 4 + 4].copy_from_slice(&v.to_le_bytes());
}
let em_base = o_em + i * w_e;
payload[em_base..em_base + ne].fill(1);
if o.ent_rel_idx.len() >= ne && o.ent_rel_type.len() >= ne {
payload[o_eridx + i * w_e..o_eridx + i * w_e + ne].copy_from_slice(&o.ent_rel_idx[..ne]);
payload[o_ertyp + i * w_e..o_ertyp + i * w_e + ne].copy_from_slice(&o.ent_rel_type[..ne]);
}
}
// actions
let na = o.n_acts().min(w_a);
if na > 0 {
let id_base = o_aids + i * w_a * 2;
for k in 0..na {
payload[id_base + k * 2..id_base + k * 2 + 2]
.copy_from_slice(&(o.act_ids[k] as u16).to_le_bytes());
}
let af_base = o_acts + i * w_a * ACT_F * 4;
for (k, &v) in o.acts[..na * ACT_F].iter().enumerate() {
payload[af_base + k * 4..af_base + k * 4 + 4].copy_from_slice(&v.to_le_bytes());
}
let am_base = o_am + i * w_a;
payload[am_base..am_base + na].fill(1);
if o.act_ent_idx.len() >= na {
payload[o_aent + i * w_a..o_aent + i * w_a + na].copy_from_slice(&o.act_ent_idx[..na]);
}
}
}
if stream.write_all(&payload).is_err() {
break;
}
let Ok(rb) = read_u32(&mut stream) else { break };
let Ok(rtrunk) = read_u32(&mut stream) else { break };
debug_assert_eq!(rb as usize, b);
let trunk = rtrunk as usize;
let need = (b + b * w_a + b * trunk) * 4;
resp_buf.resize(need, 0);
if stream.read_exact(&mut resp_buf).is_err() {
break;
}
let floats: Vec<f32> = resp_buf.as_chunks::<4>().0.iter().map(|c| f32::from_le_bytes(*c)).collect();
let vs = &floats[..b];
let all_logits = &floats[b..b + b * w_a];
let all_h = &floats[b + b * w_a..];
for (i, req) in batch.into_iter().enumerate() {
let na = req.obs.n_acts().min(w_a);
let logits = all_logits[i * w_a..i * w_a + na].to_vec();
let h = all_h[i * trunk..(i + 1) * trunk].to_vec();
let (rlock, rcv) = &*req.reply;
*rlock.lock().unwrap() = Some(Output { logits, value: vs[i], h });
rcv.notify_one();
}
}
});
Ok(RemoteNet { name: url.to_string(), queue, emb, dim, belief })
}
pub fn forward(&self, o: &Obs) -> Output {
let reply = Arc::new((Mutex::new(None), Condvar::new()));
{
let (lock, cv) = &*self.queue;
let mut g = lock.lock().unwrap();
g.reqs.push(PendingReq { obs: o.clone(), reply: Arc::clone(&reply) });
cv.notify_one();
}
let (rlock, rcv) = &*reply;
let mut rg = rlock.lock().unwrap();
while rg.is_none() {
rg = rcv.wait(rg).unwrap();
}
rg.take().unwrap()
}
pub fn state(&self, o: &Obs) -> (Vec<f32>, f32) {
let out = self.forward(o);
(out.h, out.value)
}
pub fn belief(&self, h: &[f32], cards: &[u32]) -> Vec<f32> {
let Some(bl) = &self.belief else { return vec![0.0; cards.len()] };
let mut q = Vec::new();
bl.apply(h, &mut q, false);
let n_emb = self.emb.len() / self.dim.max(1);
cards
.iter()
.map(|c| {
let i = (*c as usize).min(n_emb.saturating_sub(1));
dot(&self.emb[i * self.dim..(i + 1) * self.dim], &q)
})
.collect()
}
}
/// A network: the first, pooled one, the transformer, or a GPU-batched remote server (`tcp://...`)
#[derive(Clone, Debug)]
pub enum Net {
Mlp(Mlp),
Tx(Tx),
Remote(Arc<RemoteNet>),
}
impl Net {
/// Reads a `.pmnet` file's bytes (the header's `arch` says which network)
pub fn parse(name: &str, bytes: &[u8]) -> Result<Net, String> {
let (header, _) = read(bytes)?;
if header["arch"].as_str() == Some("tx") {
Ok(Net::Tx(Tx::parse(name, bytes)?))
} else {
Ok(Net::Mlp(Mlp::parse(name, bytes)?))
}
}
pub fn name(&self) -> &str {
match self {
Net::Mlp(n) => &n.name,
Net::Tx(n) => &n.name,
Net::Remote(n) => &n.name,
}
}
/// The state `h` and the value
pub fn state(&self, o: &Obs) -> (Vec<f32>, f32) {
match self {
Net::Mlp(n) => n.state(o),
Net::Tx(n) => n.state(o),
Net::Remote(n) => n.state(o),
}
}
/// Every action's logit, and the value
pub fn forward(&self, o: &Obs) -> Output {
match self {
Net::Mlp(n) => n.forward(o),
Net::Tx(n) => n.forward(o),
Net::Remote(n) => n.forward(o),
}
}
pub fn has_belief(&self) -> bool {
match self {
Net::Mlp(n) => n.belief.is_some(),
Net::Tx(_) => true,
Net::Remote(n) => n.belief.is_some(),
}
}
/// Each card's logit of being in the opponent's hand
pub fn belief(&self, h: &[f32], cards: &[u32]) -> Vec<f32> {
match self {
Net::Mlp(n) => n.belief(h, cards),
Net::Tx(n) => n.belief(h, cards),
Net::Remote(n) => n.belief(h, cards),
}
}
}
/// Softmax of `logits / temp`
pub fn softmax(logits: &[f32], temp: f32) -> Vec<f32> {
let t = temp.max(1e-3);
let m = logits.iter().copied().fold(f32::MIN, f32::max);
let e: Vec<f32> = logits.iter().map(|l| ((l - m) / t).exp()).collect();
let s: f32 = e.iter().sum();
e.iter().map(|v| v / s).collect()
}
static CACHE: OnceLock<Mutex<HashMap<String, Arc<Net>>>> = OnceLock::new();
/// The network at `path`, read once per process (`builtin`: shipped net; `tcp://host:port`: GPU inference server)
pub fn load(path: &str) -> Result<Arc<Net>, String> {
let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new()));
// Hold the cache lock while loading/connecting so `--jobs 64` worker threads share ONE RemoteNet
// TCP connection and coalesce into true B=32..64 GPU batches instead of opening 64 separate B=1 sockets!
let mut map = cache.lock().unwrap();
if let Some(n) = map.get(path) {
return Ok(Arc::clone(n));
}
let net = if path == "builtin" {
Net::parse("builtin", BUILTIN)?
} else if path.starts_with("tcp://") {
Net::Remote(Arc::new(RemoteNet::connect(path)?))
} else {
let bytes = std::fs::read(path).map_err(|e| format!("{path}: {e}"))?;
Net::parse(path, &bytes)?
};
let net = Arc::new(net);
map.insert(path.to_string(), Arc::clone(&net));
Ok(net)
}
/// The network this build ships (`rust/rl` writes it): what `net=builtin` and an unset `Config::net` load
pub const BUILTIN: &[u8] = include_bytes!("net.pmnet");
/// The net a config names (`Config::net`, else the built-in one)
pub fn of(net: &Option<String>) -> Arc<Net> {
let path = net.as_deref().unwrap_or("builtin");
load(path).unwrap_or_else(|e| panic!("AI network: {e}"))
}
/// The value head as a score on the fitted score's scale (a logit times 1000), `WIN`/`LOSE` for a finished game
pub fn score(net: &Net, game: &playmat_engine::state::Game, seat: playmat_engine::state::Seat) -> i64 {
let over = crate::eval::evaluate(game, seat);
if over >= crate::eval::WIN || over <= crate::eval::LOSE {
return over;
}
let q = playmat_engine::decide::Query::Priority { playable: Vec::new() };
let o = obs::observe(game, seat, &q, &[]);
(net.state(&o).1 as f64 * 1000.0).clamp(-30_000.0, 30_000.0) as i64
}
|