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
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
//! Transformer Config + InferenceOverrides + kv_dim.
use super::*;
// ---------------------------------------------------------------------------
// Config
// ---------------------------------------------------------------------------
/// Transformer model configuration — superset of both katgpt-rs and riir-engine.
///
/// Fields are ordered by descending alignment to minimize padding:
/// usize/u64 → f64 → enums (usize-discriminant) → f32 → Vec → u16 → u8/bool.
#[derive(Clone)]
pub struct Config {
// --- usize / pointer-sized fields (8-byte aligned) ---
pub vocab_size: usize,
pub block_size: usize,
pub n_embd: usize,
pub n_head: usize,
pub head_dim: usize,
pub mlp_hidden: usize,
pub n_layer: usize,
pub n_kv_head: usize,
pub bos_token: usize,
pub draft_lookahead: usize,
pub tree_budget: usize,
pub parallel_threshold: usize,
pub lora_rank: usize,
pub early_exit_patience: usize,
pub mtp_activation_threshold: usize,
pub mtp_cluster_vocab_threshold: usize,
pub mtp_shared_kv_prompt_threshold: usize,
pub mtp_cluster_size: usize,
/// Minimum expected output tokens for MTP speculative decoding.
/// If remaining tokens < this threshold, MTP is skipped (single-token path).
/// Prevents MoE overhead on short texts (Plan 117 Phase 2).
pub mtp_min_output_tokens: usize,
/// Top-K cluster selection for clustered LM head (Plan 117 T20).
/// When K > 1, compute logits for tokens in top-K clusters instead of just top-1.
/// Default 1 = backward compatible (single cluster = current behavior).
pub mtp_cluster_topk: usize,
pub mask_token: usize,
pub sp_kv_window: usize,
pub sp_kv_predictor_hidden: usize,
pub width_rollouts: usize,
pub d2f_block_size: usize,
/// Number of last layers to sum before LM head. 0 = disabled (standard).
/// (Plan 104: Research 68)
pub mls_layers: usize,
// --- f64 (8-byte aligned) ---
pub rms_norm_eps: f64,
// --- f32 (4-byte aligned) ---
pub sp_kv_predictor_lr_mult: f32,
pub temperature: f32,
pub lora_alpha: f32,
pub lora_dropout: f32,
// Screening Pruner (Plan 021)
pub screening_threshold: f32,
// Sparse MLP (Plan 022)
pub sparse_threshold: f32,
// Early exit (Plan 026: AutoTTS)
pub early_exit_gap: f32,
pub hla_decay: f32,
pub rope_theta: f32,
pub attn_logit_softcapping: f32,
pub final_logit_softcapping: f32,
pub sp_kv_threshold: f32,
pub early_stop_threshold: f32,
// Parallax Attention (Plan 135: Parameterized Local Linear Attention)
/// Parallax covariance correction gate scale. 0.0 = disabled (pure softmax),
/// 1.0 = full correction. Only meaningful when `parallax_attn` feature is enabled
/// and R projection weights are loaded.
pub parallax_gate_scale: f32,
/// Desperation score threshold for emotion-aware session flagging (Plan 162 T12).
/// When the mean desperation projection exceeds this value, `is_desperate_session()` returns true.
/// Default: 0.5 (moderate desperation). Range: [0.0, 1.0].
pub emotion_desperation_threshold: f32,
// --- Vec (pointer-sized, 8-byte aligned) ---
pub lora_targets: Vec<String>,
// --- #[repr(u8)] enums (1-byte) + bool fields (1-byte), tail-packed ---
// HLA Attention (Plan 057: Higher-order Linear Attention)
pub hla_mode: HlaMode,
// Gemma 2 architecture fields (Plan 087)
pub model_arch: ModelArchitecture,
// D2F Discrete Diffusion Forcing (Plan 066)
pub attention_mode: AttentionMode,
// EqR Convergence Selection (Plan 119)
pub convergence_selector: ConvergenceSelector,
// LT2 Looped Inference Pipeline (Plan 108, Research 73)
pub loop_mode: LoopMode,
pub hybrid_pattern: HybridPattern,
// Any-Time LT2 Dispatch (Issue 035, Research 273 — ELT arXiv:2604.09168).
// `loop_min` = floor for elastic override (refuse exit below this).
// `loop_max` = trained max loop count; 0 = sentinel meaning "derive from
// loop_mode" (i.e. use WeightShared.loop_count).
// Hard ceiling for elastic over-iteration is `2 × loop_max` (ELT §1.5:
// modest over-looping beyond L_max is regularized by training; cap at 2×
// to prevent runaway). Both default to 0 = "derive from loop_mode".
pub loop_min: usize,
pub loop_max: usize,
pub weight_dtype: WeightDtype,
pub hla_normalize: bool,
pub rms_norm_offset: bool,
pub tied_embeddings: bool,
pub use_rope: bool,
pub post_norm: bool,
pub gated_attn: bool,
/// Whether W_R starts zeroed (true = recover exact softmax at init).
pub parallax_zero_init: bool,
// --- Loop Stability Fix (Plan 428, Research 414) ---
/// Inter-loop stabilization mode for weight-shared looped inference.
/// `None` = byte-identical to pre-Plan-428 behavior (zero cost).
/// `InterLoopNorm` = normalize hidden state between loop iterations.
/// Only effective when `loop_mode` is `WeightShared` and `loop_count > 1`.
#[cfg(feature = "loop_stability_fix")]
pub loop_stability_mode: super::LoopStabilityMode,
// --- Hydra Adaptive Layer Budget (Research 148, Plan 165) ---
/// Per-layer Hydra importance profiles. Empty = disabled.
/// Populated from calibration data via `calibrate_profiles()`.
#[cfg(feature = "hydra_budget")]
pub hydra_profiles: Vec<super::HydraLayerProfile>,
// --- DeltaNet Inference (Plan 182: Luce Megakernel Distill) ---
/// Per-layer type map: DeltaNet vs standard Attention.
/// Length = n_layer. Empty = all layers are Attention (backward compatible).
/// Only used when model_arch = QwenDeltaNet.
#[cfg(feature = "deltanet_inference")]
pub layer_types: Vec<DeltaNetLayerType>,
/// Depthwise conv kernel size for DeltaNet layers (typically 4).
#[cfg(feature = "deltanet_inference")]
pub deltanet_conv_kernel_size: usize,
/// Recurrence state dimension per head (key_dim * value_dim, typically 128*128 = 16384).
#[cfg(feature = "deltanet_inference")]
pub deltanet_state_dim: usize,
/// Linear attention key/value head dimension (128 for Qwen 3.5).
/// Separate from `head_dim` which refers to full attention heads.
#[cfg(feature = "deltanet_inference")]
pub deltanet_linear_head_dim: usize,
/// Number of linear attention key heads (16 for Qwen 3.5).
/// Separate from `n_head` which refers to full attention heads.
#[cfg(feature = "deltanet_inference")]
pub deltanet_linear_n_heads: usize,
/// Number of linear attention value heads (16 for Qwen 3.5).
/// Usually equals `deltanet_linear_n_heads`.
#[cfg(feature = "deltanet_inference")]
pub deltanet_linear_n_value_heads: usize,
// --- RiM Reasoning Buffer Slots (Plan 172, Research 192) ---
/// Number of reasoning buffer blocks (K in RiM paper). 0 = disabled.
#[cfg(feature = "rim_slots")]
pub rim_block_count: usize,
/// Tokens per buffer block (M in RiM paper). Default 2.
///
/// The M=2 default suits the RiM paper's pause-token use case. For reasoning
/// tasks (LOTUS-style latent CoT), LOTUS Table 7 proves an M≥5 floor: M=1→5
/// is a +17.8pp cliff on GSM8K and quality saturates at M≥25. Callers
/// enabling RiM for reasoning should set M≥5 (ideally ≥25); see
/// `.issues/156` T1 and `.research/442` §2.4.
#[cfg(feature = "rim_slots")]
pub rim_tokens_per_block: usize,
/// Token ID used for buffer positions (default: bos_token, reused as buffer).
#[cfg(feature = "rim_slots")]
pub rim_buffer_token: usize,
// --- Wall Attention (Plan 173) ---
/// Wall attention config. None = use RoPE/fallback.
#[cfg(feature = "wall_attention")]
pub wall_config: Option<WallConfig>,
// --- Collapse-Aware Adaptive Thinking (Plan 212) ---
/// Per-instance adaptive budget for collapse-aware thinking.
#[cfg(feature = "collapse_aware_thinking")]
pub collapse_budget: ThinkingBudget,
// --- NextLat Belief-State Speculative Drafter (Plan 217) ---
/// Path to `nextlat.bin` MLP weights. None = random init.
#[cfg(feature = "belief_drafter")]
pub belief_drafter_path: Option<String>,
/// Entropy threshold for belief drafter variable-length stopping.
/// Lower = more conservative drafts. Higher = more aggressive.
/// Default: 2.0. Only used when `belief_drafter` feature is enabled.
#[cfg(feature = "belief_drafter")]
pub belief_drafter_entropy_threshold: f32,
}
impl Config {
/// Compute the effective loop count for `forward_looped`, applying an
/// optional elastic override clamped to `[loop_min, 2×loop_max]`.
///
/// (Issue 035, Research 273 — ELT arXiv:2604.09168 Any-Time inference.)
///
/// - `elastic_override = None` → use `loop_mode`'s natural loop count
/// (byte-identical to pre-Issue-035 behavior).
/// - `elastic_override = Some(L)` with `LoopMode::WeightShared` → clamp L
/// to `[max(loop_min, 1), 2 × max(loop_max, base)]`.
/// - Below `loop_min` (default 1): clamped up. ELT §1.4 establishes a
/// minimum depth for representational capacity (`1N × 32L` collapsed
/// to FID 10.30 vs 2.83 for `16N × 2L`).
/// - Above `2 × loop_max`: clamped down. ELT §1.5 shows modest
/// over-looping beyond L_max is regularized (UCF-101 peak FVD at L=6
/// with L_max=4), but quality eventually deteriorates — 2× is the cap.
/// - `elastic_override = Some(_)` with `LoopMode::None` or `TrainingFree` →
/// refused (returns base); there is no weight-shared loop to elastically
/// exit from.
///
/// `loop_max == 0` is a sentinel meaning "derive from `loop_mode`" (use
/// `WeightShared.loop_count`). This keeps the 12 existing Config
/// constructors unchanged in semantics — they default to `loop_max: 0`
/// which resolves to the natural loop count.
#[inline]
pub fn effective_loop_count(&self, elastic_override: Option<usize>) -> usize {
let base = match self.loop_mode {
LoopMode::WeightShared { loop_count } => loop_count,
LoopMode::None | LoopMode::TrainingFree => 1,
};
let requested = match elastic_override {
None => return base,
Some(o) => o,
};
// Refuse elastic override when there's no weight-shared loop to exit.
if !matches!(self.loop_mode, LoopMode::WeightShared { .. }) {
return base;
}
let lo = self.loop_min.max(1);
let max_base = if self.loop_max == 0 {
base
} else {
self.loop_max
};
let hi = max_base.max(base).max(lo);
let hard_cap = 2 * hi;
requested.clamp(lo, hard_cap)
}
/// Micro GPT config matching [talos-vs-macbook](https://github.com/AlexCheema/talos-vs-macbook) reference:
/// vocab=27, block=16, n_layer=1, n_head=4, n_embd=16, head_dim=4,
/// RMSNorm (no learnable gain), ReLU MLP (4x), no biases, untied lm_head.
pub fn micro() -> Self {
Self {
vocab_size: 27,
block_size: 16,
n_embd: 16,
n_head: 4,
head_dim: 4,
mlp_hidden: 64,
n_layer: 1,
n_kv_head: 4,
bos_token: 26,
temperature: 0.5,
draft_lookahead: 8,
tree_budget: 16,
parallel_threshold: 128,
lora_rank: 4,
lora_alpha: 8.0,
lora_dropout: 0.0,
lora_targets: Vec::new(),
screening_threshold: 0.0,
sparse_threshold: 0.8,
early_exit_patience: 0,
early_exit_gap: 0.0,
mtp_activation_threshold: usize::MAX,
mtp_cluster_vocab_threshold: usize::MAX,
mtp_shared_kv_prompt_threshold: usize::MAX,
mtp_cluster_size: 512,
mtp_min_output_tokens: usize::MAX,
mtp_cluster_topk: 1,
hla_mode: HlaMode::Standard,
hla_normalize: false,
hla_decay: 1.0,
model_arch: ModelArchitecture::Generic,
rms_norm_eps: 1e-5,
rms_norm_offset: false,
tied_embeddings: false,
use_rope: false,
rope_theta: 10000.0,
post_norm: false,
attn_logit_softcapping: 0.0,
final_logit_softcapping: 0.0,
weight_dtype: WeightDtype::F32,
mask_token: 0,
attention_mode: AttentionMode::Causal,
sp_kv_window: 128,
sp_kv_threshold: 0.5,
sp_kv_predictor_hidden: 0,
sp_kv_predictor_lr_mult: 5.0,
width_rollouts: 1,
early_stop_threshold: 0.0,
convergence_selector: ConvergenceSelector::default(),
d2f_block_size: 8,
mls_layers: 0,
loop_mode: LoopMode::None,
hybrid_pattern: HybridPattern::Uniform,
loop_min: 0,
loop_max: 0,
gated_attn: false,
parallax_gate_scale: 0.0,
emotion_desperation_threshold: 0.5,
parallax_zero_init: true,
#[cfg(feature = "loop_stability_fix")]
loop_stability_mode: super::LoopStabilityMode::None,
#[cfg(feature = "hydra_budget")]
hydra_profiles: Vec::new(),
#[cfg(feature = "deltanet_inference")]
layer_types: Vec::new(),
#[cfg(feature = "deltanet_inference")]
deltanet_conv_kernel_size: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_state_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_head_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_heads: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_value_heads: 0,
#[cfg(feature = "rim_slots")]
rim_block_count: 0,
#[cfg(feature = "rim_slots")]
rim_tokens_per_block: 2,
#[cfg(feature = "rim_slots")]
rim_buffer_token: 0,
#[cfg(feature = "wall_attention")]
wall_config: None,
#[cfg(feature = "collapse_aware_thinking")]
collapse_budget: ThinkingBudget::default(),
#[cfg(feature = "belief_drafter")]
belief_drafter_path: None,
#[cfg(feature = "belief_drafter")]
belief_drafter_entropy_threshold: 2.0,
}
}
/// Micro config with LoRA defaults (Plan 008).
pub fn micro_lora() -> Self {
let mut c = Self::micro();
c.lora_rank = 4;
c.lora_alpha = 8.0;
c.lora_dropout = 0.0;
c.lora_targets = vec![
"q".into(),
"k".into(),
"v".into(),
"o".into(),
"mlp1".into(),
"mlp2".into(),
];
c
}
/// Micro config for Discrete Diffusion Language Model training (Plan 068: D2F).
/// Bidirectional attention by default, mask_token = vocab_size - 1.
pub fn micro_dllm() -> Self {
Self {
attention_mode: AttentionMode::Bidirectional,
mask_token: 26,
d2f_block_size: 8,
..Self::micro()
}
}
/// Game config for Bomberman LoRA training (Plan 041).
/// Tiny Transformer for board state → action prediction.
/// 10-token vocab: 4 board cells (0-3) + 6 actions (4-9).
/// 170-token sequences: 169 board cells + 1 action.
/// ~18K params total, ~1.5K LoRA params (rank=4).
pub fn game() -> Self {
Self {
vocab_size: 10,
block_size: 170,
n_embd: 32,
n_head: 4,
head_dim: 8,
mlp_hidden: 128,
n_layer: 1,
n_kv_head: 4,
bos_token: 0,
temperature: 1.0,
draft_lookahead: 0,
tree_budget: 0,
parallel_threshold: 128,
lora_rank: 4,
lora_alpha: 8.0,
lora_dropout: 0.0,
lora_targets: vec![
"q".into(),
"k".into(),
"v".into(),
"o".into(),
"mlp1".into(),
"mlp2".into(),
],
screening_threshold: 0.0,
sparse_threshold: 0.8,
early_exit_patience: 0,
early_exit_gap: 0.0,
mtp_activation_threshold: usize::MAX,
mtp_cluster_vocab_threshold: usize::MAX,
mtp_shared_kv_prompt_threshold: usize::MAX,
mtp_cluster_size: 512,
mtp_min_output_tokens: usize::MAX,
mtp_cluster_topk: 1,
hla_mode: HlaMode::Standard,
hla_normalize: false,
hla_decay: 1.0,
model_arch: ModelArchitecture::Generic,
rms_norm_eps: 1e-5,
rms_norm_offset: false,
tied_embeddings: false,
use_rope: false,
rope_theta: 10000.0,
post_norm: false,
attn_logit_softcapping: 0.0,
final_logit_softcapping: 0.0,
weight_dtype: WeightDtype::F32,
mask_token: 0,
attention_mode: AttentionMode::Causal,
sp_kv_window: 128,
sp_kv_threshold: 0.5,
sp_kv_predictor_hidden: 0,
sp_kv_predictor_lr_mult: 5.0,
width_rollouts: 1,
early_stop_threshold: 0.0,
convergence_selector: ConvergenceSelector::default(),
d2f_block_size: 8,
mls_layers: 0,
loop_mode: LoopMode::None,
hybrid_pattern: HybridPattern::Uniform,
loop_min: 0,
loop_max: 0,
gated_attn: false,
parallax_gate_scale: 0.0,
emotion_desperation_threshold: 0.5,
parallax_zero_init: true,
#[cfg(feature = "loop_stability_fix")]
loop_stability_mode: super::LoopStabilityMode::None,
#[cfg(feature = "hydra_budget")]
hydra_profiles: Vec::new(),
#[cfg(feature = "deltanet_inference")]
layer_types: Vec::new(),
#[cfg(feature = "deltanet_inference")]
deltanet_conv_kernel_size: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_state_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_head_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_heads: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_value_heads: 0,
#[cfg(feature = "rim_slots")]
rim_block_count: 0,
#[cfg(feature = "rim_slots")]
rim_tokens_per_block: 2,
#[cfg(feature = "rim_slots")]
rim_buffer_token: 0,
#[cfg(feature = "wall_attention")]
wall_config: None,
#[cfg(feature = "collapse_aware_thinking")]
collapse_budget: ThinkingBudget::default(),
#[cfg(feature = "belief_drafter")]
belief_drafter_path: None,
#[cfg(feature = "belief_drafter")]
belief_drafter_entropy_threshold: 2.0,
}
}
/// Game config for Go 9×9 LoRA training (Plan 078).
/// Tiny Transformer for board state → move prediction.
/// 85-token vocab: 3 board cells (Empty=0, Black=1, White=2) + 81 positions (3..83) + 1 pass (84).
/// 82-token sequences: 81 board cells + 1 action.
/// ~16K params total, ~1.3K LoRA params (rank=4).
pub fn game_go() -> Self {
Self {
vocab_size: 85,
block_size: 82,
n_embd: 32,
n_head: 4,
head_dim: 8,
mlp_hidden: 128,
n_layer: 1,
n_kv_head: 4,
bos_token: 0,
temperature: 1.0,
draft_lookahead: 0,
tree_budget: 0,
parallel_threshold: 128,
lora_rank: 4,
lora_alpha: 8.0,
lora_dropout: 0.0,
lora_targets: vec![
"q".into(),
"k".into(),
"v".into(),
"o".into(),
"mlp1".into(),
"mlp2".into(),
],
screening_threshold: 0.0,
sparse_threshold: 0.8,
early_exit_patience: 0,
early_exit_gap: 0.0,
mtp_activation_threshold: usize::MAX,
mtp_cluster_vocab_threshold: usize::MAX,
mtp_shared_kv_prompt_threshold: usize::MAX,
mtp_cluster_size: 512,
mtp_min_output_tokens: usize::MAX,
mtp_cluster_topk: 1,
hla_mode: HlaMode::Standard,
hla_normalize: false,
hla_decay: 1.0,
model_arch: ModelArchitecture::Generic,
rms_norm_eps: 1e-5,
rms_norm_offset: false,
tied_embeddings: false,
use_rope: false,
rope_theta: 10000.0,
post_norm: false,
attn_logit_softcapping: 0.0,
final_logit_softcapping: 0.0,
weight_dtype: WeightDtype::F32,
mask_token: 0,
attention_mode: AttentionMode::Causal,
sp_kv_window: 128,
sp_kv_threshold: 0.5,
sp_kv_predictor_hidden: 0,
sp_kv_predictor_lr_mult: 5.0,
width_rollouts: 1,
early_stop_threshold: 0.0,
convergence_selector: ConvergenceSelector::default(),
d2f_block_size: 8,
mls_layers: 0,
loop_mode: LoopMode::None,
hybrid_pattern: HybridPattern::Uniform,
loop_min: 0,
loop_max: 0,
gated_attn: false,
parallax_gate_scale: 0.0,
emotion_desperation_threshold: 0.5,
parallax_zero_init: true,
#[cfg(feature = "loop_stability_fix")]
loop_stability_mode: super::LoopStabilityMode::None,
#[cfg(feature = "hydra_budget")]
hydra_profiles: Vec::new(),
#[cfg(feature = "deltanet_inference")]
layer_types: Vec::new(),
#[cfg(feature = "deltanet_inference")]
deltanet_conv_kernel_size: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_state_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_head_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_heads: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_value_heads: 0,
#[cfg(feature = "rim_slots")]
rim_block_count: 0,
#[cfg(feature = "rim_slots")]
rim_tokens_per_block: 2,
#[cfg(feature = "rim_slots")]
rim_buffer_token: 0,
#[cfg(feature = "wall_attention")]
wall_config: None,
#[cfg(feature = "collapse_aware_thinking")]
collapse_budget: ThinkingBudget::default(),
#[cfg(feature = "belief_drafter")]
belief_drafter_path: None,
#[cfg(feature = "belief_drafter")]
belief_drafter_entropy_threshold: 2.0,
}
}
/// Game config for FFT Tactics Arena LoRA training (Plan 296 T7.3).
/// Tiny Transformer for battle state → action prediction.
///
/// # Token Layout
///
/// - State vocab (values 0..9): team(0-1), class(0-5), hp_bucket(0-7),
/// mp_bucket(0-3), pos_x(0-7), pos_y(0-7), alive(0-1).
/// - Action tokens: 10..19 (9 FFT ActionTypes).
///
/// Sequence (58 tokens):
/// `[tick, u0_team, u0_class, u0_hp, u0_mp, u0_x, u0_y, u0_alive,
/// u1_..., ..., u7_..., action_token]`
///
/// Per-unit = 7 tokens × 8 units = 56, +1 tick +1 action = 58 tokens.
/// ~18K params total, ~1.5K LoRA params (rank=4). Comparable to Bomber/Go.
pub fn game_fft() -> Self {
Self {
vocab_size: 19,
block_size: 58,
n_embd: 32,
n_head: 4,
head_dim: 8,
mlp_hidden: 128,
n_layer: 1,
n_kv_head: 4,
bos_token: 0,
temperature: 1.0,
draft_lookahead: 0,
tree_budget: 0,
parallel_threshold: 128,
lora_rank: 4,
lora_alpha: 8.0,
lora_dropout: 0.0,
lora_targets: vec![
"q".into(),
"k".into(),
"v".into(),
"o".into(),
"mlp1".into(),
"mlp2".into(),
],
screening_threshold: 0.0,
sparse_threshold: 0.8,
early_exit_patience: 0,
early_exit_gap: 0.0,
mtp_activation_threshold: usize::MAX,
mtp_cluster_vocab_threshold: usize::MAX,
mtp_shared_kv_prompt_threshold: usize::MAX,
mtp_cluster_size: 512,
mtp_min_output_tokens: usize::MAX,
mtp_cluster_topk: 1,
hla_mode: HlaMode::Standard,
hla_normalize: false,
hla_decay: 1.0,
model_arch: ModelArchitecture::Generic,
rms_norm_eps: 1e-5,
rms_norm_offset: false,
tied_embeddings: false,
use_rope: false,
rope_theta: 10000.0,
post_norm: false,
attn_logit_softcapping: 0.0,
final_logit_softcapping: 0.0,
weight_dtype: WeightDtype::F32,
mask_token: 0,
attention_mode: AttentionMode::Causal,
sp_kv_window: 128,
sp_kv_threshold: 0.5,
sp_kv_predictor_hidden: 0,
sp_kv_predictor_lr_mult: 5.0,
width_rollouts: 1,
early_stop_threshold: 0.0,
convergence_selector: ConvergenceSelector::default(),
d2f_block_size: 8,
mls_layers: 0,
loop_mode: LoopMode::None,
hybrid_pattern: HybridPattern::Uniform,
loop_min: 0,
loop_max: 0,
gated_attn: false,
parallax_gate_scale: 0.0,
emotion_desperation_threshold: 0.5,
parallax_zero_init: true,
#[cfg(feature = "loop_stability_fix")]
loop_stability_mode: super::LoopStabilityMode::None,
#[cfg(feature = "hydra_budget")]
hydra_profiles: Vec::new(),
#[cfg(feature = "deltanet_inference")]
layer_types: Vec::new(),
#[cfg(feature = "deltanet_inference")]
deltanet_conv_kernel_size: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_state_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_head_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_heads: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_value_heads: 0,
#[cfg(feature = "rim_slots")]
rim_block_count: 0,
#[cfg(feature = "rim_slots")]
rim_tokens_per_block: 2,
#[cfg(feature = "rim_slots")]
rim_buffer_token: 0,
#[cfg(feature = "wall_attention")]
wall_config: None,
#[cfg(feature = "collapse_aware_thinking")]
collapse_budget: ThinkingBudget::default(),
#[cfg(feature = "belief_drafter")]
belief_drafter_path: None,
#[cfg(feature = "belief_drafter")]
belief_drafter_entropy_threshold: 2.0,
}
}
/// Lightweight draft model for speculative decoding (~4× smaller than target).
/// Same vocab/block to share embeddings, but embd=4, heads=2, mlp=16.
pub fn draft() -> Self {
Self {
vocab_size: 27,
block_size: 16,
n_embd: 4,
n_head: 2,
head_dim: 2,
mlp_hidden: 16,
n_layer: 1,
n_kv_head: 2,
bos_token: 26,
temperature: 0.5,
draft_lookahead: 8,
tree_budget: 16,
parallel_threshold: 128,
lora_rank: 4,
lora_alpha: 8.0,
lora_dropout: 0.0,
lora_targets: Vec::new(),
screening_threshold: 0.0,
sparse_threshold: 0.8,
early_exit_patience: 0,
early_exit_gap: 0.0,
mtp_activation_threshold: usize::MAX,
mtp_cluster_vocab_threshold: usize::MAX,
mtp_shared_kv_prompt_threshold: usize::MAX,
mtp_cluster_size: 512,
mtp_min_output_tokens: usize::MAX,
mtp_cluster_topk: 1,
hla_mode: HlaMode::Standard,
hla_normalize: false,
hla_decay: 1.0,
model_arch: ModelArchitecture::Generic,
rms_norm_eps: 1e-5,
rms_norm_offset: false,
tied_embeddings: false,
use_rope: false,
rope_theta: 10000.0,
post_norm: false,
attn_logit_softcapping: 0.0,
final_logit_softcapping: 0.0,
weight_dtype: WeightDtype::F32,
mask_token: 0,
attention_mode: AttentionMode::Causal,
sp_kv_window: 128,
sp_kv_threshold: 0.5,
sp_kv_predictor_hidden: 0,
sp_kv_predictor_lr_mult: 5.0,
width_rollouts: 1,
early_stop_threshold: 0.0,
convergence_selector: ConvergenceSelector::default(),
d2f_block_size: 8,
mls_layers: 0,
loop_mode: LoopMode::None,
hybrid_pattern: HybridPattern::Uniform,
loop_min: 0,
loop_max: 0,
gated_attn: false,
parallax_gate_scale: 0.0,
emotion_desperation_threshold: 0.5,
parallax_zero_init: true,
#[cfg(feature = "loop_stability_fix")]
loop_stability_mode: super::LoopStabilityMode::None,
#[cfg(feature = "hydra_budget")]
hydra_profiles: Vec::new(),
#[cfg(feature = "deltanet_inference")]
layer_types: Vec::new(),
#[cfg(feature = "deltanet_inference")]
deltanet_conv_kernel_size: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_state_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_head_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_heads: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_value_heads: 0,
#[cfg(feature = "rim_slots")]
rim_block_count: 0,
#[cfg(feature = "rim_slots")]
rim_tokens_per_block: 2,
#[cfg(feature = "rim_slots")]
rim_buffer_token: 0,
#[cfg(feature = "wall_attention")]
wall_config: None,
#[cfg(feature = "collapse_aware_thinking")]
collapse_budget: ThinkingBudget::default(),
#[cfg(feature = "belief_drafter")]
belief_drafter_path: None,
#[cfg(feature = "belief_drafter")]
belief_drafter_entropy_threshold: 2.0,
}
}
/// Small target model for multi-layer testing.
/// vocab=4096, block=256, n_layer=4, n_head=4, n_embd=64, head_dim=16,
/// MLP hidden=256.
pub fn small_target() -> Self {
Self {
vocab_size: 4096,
block_size: 256,
n_embd: 64,
n_head: 4,
head_dim: 16,
mlp_hidden: 256,
n_layer: 4,
n_kv_head: 4,
bos_token: 0,
temperature: 0.8,
draft_lookahead: 5,
tree_budget: 32,
parallel_threshold: 128,
lora_rank: 4,
lora_alpha: 8.0,
lora_dropout: 0.0,
lora_targets: Vec::new(),
screening_threshold: 0.0,
sparse_threshold: 0.8,
early_exit_patience: 0,
early_exit_gap: 0.0,
mtp_activation_threshold: 64,
mtp_cluster_vocab_threshold: usize::MAX,
mtp_shared_kv_prompt_threshold: 128,
mtp_cluster_size: 512,
mtp_min_output_tokens: 16,
mtp_cluster_topk: 1,
hla_mode: HlaMode::Standard,
hla_normalize: false,
hla_decay: 1.0,
model_arch: ModelArchitecture::Generic,
rms_norm_eps: 1e-5,
rms_norm_offset: false,
tied_embeddings: false,
use_rope: false,
rope_theta: 10000.0,
post_norm: false,
attn_logit_softcapping: 0.0,
final_logit_softcapping: 0.0,
weight_dtype: WeightDtype::F32,
mask_token: 0,
attention_mode: AttentionMode::Causal,
sp_kv_window: 128,
sp_kv_threshold: 0.5,
sp_kv_predictor_hidden: 0,
sp_kv_predictor_lr_mult: 5.0,
width_rollouts: 1,
early_stop_threshold: 0.0,
convergence_selector: ConvergenceSelector::default(),
d2f_block_size: 16,
mls_layers: 0,
loop_mode: LoopMode::None,
hybrid_pattern: HybridPattern::Uniform,
loop_min: 0,
loop_max: 0,
gated_attn: false,
parallax_gate_scale: 0.0,
emotion_desperation_threshold: 0.5,
parallax_zero_init: true,
#[cfg(feature = "loop_stability_fix")]
loop_stability_mode: super::LoopStabilityMode::None,
#[cfg(feature = "hydra_budget")]
hydra_profiles: Vec::new(),
#[cfg(feature = "deltanet_inference")]
layer_types: Vec::new(),
#[cfg(feature = "deltanet_inference")]
deltanet_conv_kernel_size: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_state_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_head_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_heads: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_value_heads: 0,
#[cfg(feature = "rim_slots")]
rim_block_count: 0,
#[cfg(feature = "rim_slots")]
rim_tokens_per_block: 2,
#[cfg(feature = "rim_slots")]
rim_buffer_token: 0,
#[cfg(feature = "wall_attention")]
wall_config: None,
#[cfg(feature = "collapse_aware_thinking")]
collapse_budget: ThinkingBudget::default(),
#[cfg(feature = "belief_drafter")]
belief_drafter_path: None,
#[cfg(feature = "belief_drafter")]
belief_drafter_entropy_threshold: 2.0,
}
}
/// GQA draft config: 8 Q heads, 2 KV heads (4:1 ratio, 4× KV cache reduction).
pub fn gqa_draft() -> Self {
Self {
vocab_size: 4096,
block_size: 256,
n_embd: 64,
n_head: 8,
head_dim: 8,
mlp_hidden: 256,
n_layer: 4,
n_kv_head: 2,
bos_token: 0,
temperature: 0.8,
draft_lookahead: 5,
tree_budget: 32,
parallel_threshold: 128,
lora_rank: 4,
lora_alpha: 8.0,
lora_dropout: 0.0,
lora_targets: Vec::new(),
screening_threshold: 0.0,
sparse_threshold: 0.8,
early_exit_patience: 0,
early_exit_gap: 0.0,
mtp_activation_threshold: 64,
mtp_cluster_vocab_threshold: usize::MAX,
mtp_shared_kv_prompt_threshold: 128,
mtp_cluster_size: 512,
mtp_min_output_tokens: 16,
mtp_cluster_topk: 1,
hla_mode: HlaMode::Standard,
hla_normalize: false,
hla_decay: 1.0,
model_arch: ModelArchitecture::Generic,
rms_norm_eps: 1e-5,
rms_norm_offset: false,
tied_embeddings: false,
use_rope: false,
rope_theta: 10000.0,
post_norm: false,
attn_logit_softcapping: 0.0,
final_logit_softcapping: 0.0,
weight_dtype: WeightDtype::F32,
mask_token: 0,
attention_mode: AttentionMode::Causal,
sp_kv_window: 128,
sp_kv_threshold: 0.5,
sp_kv_predictor_hidden: 0,
sp_kv_predictor_lr_mult: 5.0,
width_rollouts: 1,
early_stop_threshold: 0.0,
convergence_selector: ConvergenceSelector::default(),
d2f_block_size: 16,
mls_layers: 0,
loop_mode: LoopMode::None,
hybrid_pattern: HybridPattern::Uniform,
loop_min: 0,
loop_max: 0,
gated_attn: false,
parallax_gate_scale: 0.0,
emotion_desperation_threshold: 0.5,
parallax_zero_init: true,
#[cfg(feature = "loop_stability_fix")]
loop_stability_mode: super::LoopStabilityMode::None,
#[cfg(feature = "hydra_budget")]
hydra_profiles: Vec::new(),
#[cfg(feature = "deltanet_inference")]
layer_types: Vec::new(),
#[cfg(feature = "deltanet_inference")]
deltanet_conv_kernel_size: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_state_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_head_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_heads: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_value_heads: 0,
#[cfg(feature = "rim_slots")]
rim_block_count: 0,
#[cfg(feature = "rim_slots")]
rim_tokens_per_block: 2,
#[cfg(feature = "rim_slots")]
rim_buffer_token: 0,
#[cfg(feature = "wall_attention")]
wall_config: None,
#[cfg(feature = "collapse_aware_thinking")]
collapse_budget: ThinkingBudget::default(),
#[cfg(feature = "belief_drafter")]
belief_drafter_path: None,
#[cfg(feature = "belief_drafter")]
belief_drafter_entropy_threshold: 2.0,
}
}
/// BPE tokenizer config for Rust source code.
/// vocab=4096, block=256, n_layer=1, n_head=4, n_embd=32, head_dim=8,
/// MLP hidden=128.
pub fn bpe() -> Self {
Self {
vocab_size: 4096,
block_size: 256,
n_embd: 32,
n_head: 4,
head_dim: 8,
mlp_hidden: 128,
n_layer: 1,
n_kv_head: 4,
bos_token: 1,
temperature: 0.8,
draft_lookahead: 8,
tree_budget: 32,
parallel_threshold: 128,
lora_rank: 4,
lora_alpha: 8.0,
lora_dropout: 0.0,
lora_targets: Vec::new(),
screening_threshold: 0.0,
sparse_threshold: 0.8,
early_exit_patience: 0,
early_exit_gap: 0.0,
mtp_activation_threshold: 32,
mtp_cluster_vocab_threshold: 4096,
mtp_shared_kv_prompt_threshold: 64,
mtp_cluster_size: 512,
mtp_min_output_tokens: 16,
mtp_cluster_topk: 8,
hla_mode: HlaMode::Standard,
hla_normalize: false,
hla_decay: 1.0,
model_arch: ModelArchitecture::Generic,
rms_norm_eps: 1e-5,
rms_norm_offset: false,
tied_embeddings: false,
use_rope: false,
rope_theta: 10000.0,
post_norm: false,
attn_logit_softcapping: 0.0,
final_logit_softcapping: 0.0,
weight_dtype: WeightDtype::F32,
mask_token: 0,
attention_mode: AttentionMode::Causal,
sp_kv_window: 128,
sp_kv_threshold: 0.5,
sp_kv_predictor_hidden: 0,
sp_kv_predictor_lr_mult: 5.0,
width_rollouts: 1,
early_stop_threshold: 0.0,
convergence_selector: ConvergenceSelector::default(),
d2f_block_size: 16,
mls_layers: 0,
loop_mode: LoopMode::None,
hybrid_pattern: HybridPattern::Uniform,
loop_min: 0,
loop_max: 0,
gated_attn: false,
parallax_gate_scale: 0.0,
emotion_desperation_threshold: 0.5,
parallax_zero_init: true,
#[cfg(feature = "loop_stability_fix")]
loop_stability_mode: super::LoopStabilityMode::None,
#[cfg(feature = "hydra_budget")]
hydra_profiles: Vec::new(),
#[cfg(feature = "deltanet_inference")]
layer_types: Vec::new(),
#[cfg(feature = "deltanet_inference")]
deltanet_conv_kernel_size: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_state_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_head_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_heads: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_value_heads: 0,
#[cfg(feature = "rim_slots")]
rim_block_count: 0,
#[cfg(feature = "rim_slots")]
rim_tokens_per_block: 2,
#[cfg(feature = "rim_slots")]
rim_buffer_token: 0,
#[cfg(feature = "wall_attention")]
wall_config: None,
#[cfg(feature = "collapse_aware_thinking")]
collapse_budget: ThinkingBudget::default(),
#[cfg(feature = "belief_drafter")]
belief_drafter_path: None,
#[cfg(feature = "belief_drafter")]
belief_drafter_entropy_threshold: 2.0,
}
}
/// BPE draft model (smaller for speculative decoding).
/// Same vocab/block as bpe(), but embd=16, heads=2, mlp=64.
pub fn bpe_draft() -> Self {
Self {
vocab_size: 4096,
block_size: 256,
n_embd: 16,
n_head: 2,
head_dim: 8,
mlp_hidden: 64,
n_layer: 1,
n_kv_head: 2,
bos_token: 1,
temperature: 0.8,
draft_lookahead: 8,
tree_budget: 32,
parallel_threshold: 128,
lora_rank: 4,
lora_alpha: 8.0,
lora_dropout: 0.0,
lora_targets: Vec::new(),
screening_threshold: 0.0,
sparse_threshold: 0.8,
early_exit_patience: 0,
early_exit_gap: 0.0,
mtp_activation_threshold: 16,
mtp_cluster_vocab_threshold: 4096,
mtp_shared_kv_prompt_threshold: 64,
mtp_cluster_size: 512,
mtp_min_output_tokens: usize::MAX,
mtp_cluster_topk: 1,
hla_mode: HlaMode::Standard,
hla_normalize: false,
hla_decay: 1.0,
model_arch: ModelArchitecture::Generic,
rms_norm_eps: 1e-5,
rms_norm_offset: false,
tied_embeddings: false,
use_rope: false,
rope_theta: 10000.0,
post_norm: false,
attn_logit_softcapping: 0.0,
final_logit_softcapping: 0.0,
weight_dtype: WeightDtype::F32,
mask_token: 0,
attention_mode: AttentionMode::Causal,
sp_kv_window: 128,
sp_kv_threshold: 0.5,
sp_kv_predictor_hidden: 0,
sp_kv_predictor_lr_mult: 5.0,
width_rollouts: 1,
early_stop_threshold: 0.0,
convergence_selector: ConvergenceSelector::default(),
d2f_block_size: 16,
mls_layers: 0,
loop_mode: LoopMode::None,
hybrid_pattern: HybridPattern::Uniform,
loop_min: 0,
loop_max: 0,
gated_attn: false,
parallax_gate_scale: 0.0,
emotion_desperation_threshold: 0.5,
parallax_zero_init: true,
#[cfg(feature = "loop_stability_fix")]
loop_stability_mode: super::LoopStabilityMode::None,
#[cfg(feature = "hydra_budget")]
hydra_profiles: Vec::new(),
#[cfg(feature = "deltanet_inference")]
layer_types: Vec::new(),
#[cfg(feature = "deltanet_inference")]
deltanet_conv_kernel_size: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_state_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_head_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_heads: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_value_heads: 0,
#[cfg(feature = "rim_slots")]
rim_block_count: 0,
#[cfg(feature = "rim_slots")]
rim_tokens_per_block: 2,
#[cfg(feature = "rim_slots")]
rim_buffer_token: 0,
#[cfg(feature = "wall_attention")]
wall_config: None,
#[cfg(feature = "collapse_aware_thinking")]
collapse_budget: ThinkingBudget::default(),
#[cfg(feature = "belief_drafter")]
belief_drafter_path: None,
#[cfg(feature = "belief_drafter")]
belief_drafter_entropy_threshold: 2.0,
}
}
/// Gemma 2 2B config for real model inference (Plan 087).
/// hidden_size=2304, intermediate_size=9216, vocab=256000, layers=26,
/// heads=8, kv_heads=4, head_dim=256, max_seq=8192.
/// Uses GeGLU MLP, RoPE, RMSNorm offset, tied embeddings, post-norm.
pub fn gemma2_2b() -> Self {
Self {
vocab_size: 256000,
block_size: 8192,
n_embd: 2304,
n_head: 8,
head_dim: 256,
mlp_hidden: 9216,
n_layer: 26,
n_kv_head: 4,
bos_token: 2, // Gemma 2 BOS token
temperature: 0.8,
draft_lookahead: 0,
tree_budget: 0,
parallel_threshold: 8192,
lora_rank: 0,
lora_alpha: 1.0,
lora_dropout: 0.0,
lora_targets: Vec::new(),
screening_threshold: 0.0,
sparse_threshold: 0.0,
early_exit_patience: 0,
early_exit_gap: 0.0,
mtp_activation_threshold: 0,
mtp_cluster_vocab_threshold: 256000,
mtp_shared_kv_prompt_threshold: 8192,
mtp_cluster_size: 1024,
mtp_min_output_tokens: 16,
mtp_cluster_topk: 1,
hla_mode: HlaMode::Standard,
hla_normalize: false,
hla_decay: 1.0,
model_arch: ModelArchitecture::Gemma2,
rms_norm_eps: 1e-6,
rms_norm_offset: true,
tied_embeddings: true,
use_rope: true,
rope_theta: 10000.0,
post_norm: true,
attn_logit_softcapping: 50.0,
final_logit_softcapping: 30.0,
weight_dtype: WeightDtype::BF16,
mask_token: 0,
attention_mode: AttentionMode::Causal,
sp_kv_window: 128,
sp_kv_threshold: 0.5,
sp_kv_predictor_hidden: 0,
sp_kv_predictor_lr_mult: 5.0,
width_rollouts: 1,
early_stop_threshold: 0.0,
convergence_selector: ConvergenceSelector::default(),
d2f_block_size: 16,
mls_layers: 0,
loop_mode: LoopMode::None,
hybrid_pattern: HybridPattern::Uniform,
loop_min: 0,
loop_max: 0,
gated_attn: false,
parallax_gate_scale: 0.0,
emotion_desperation_threshold: 0.5,
parallax_zero_init: true,
#[cfg(feature = "loop_stability_fix")]
loop_stability_mode: super::LoopStabilityMode::None,
#[cfg(feature = "hydra_budget")]
hydra_profiles: Vec::new(),
#[cfg(feature = "deltanet_inference")]
layer_types: Vec::new(),
#[cfg(feature = "deltanet_inference")]
deltanet_conv_kernel_size: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_state_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_head_dim: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_heads: 0,
#[cfg(feature = "deltanet_inference")]
deltanet_linear_n_value_heads: 0,
#[cfg(feature = "rim_slots")]
rim_block_count: 0,
#[cfg(feature = "rim_slots")]
rim_tokens_per_block: 2,
#[cfg(feature = "rim_slots")]
rim_buffer_token: 0,
#[cfg(feature = "wall_attention")]
wall_config: None,
#[cfg(feature = "collapse_aware_thinking")]
collapse_budget: ThinkingBudget::default(),
#[cfg(feature = "belief_drafter")]
belief_drafter_path: None,
#[cfg(feature = "belief_drafter")]
belief_drafter_entropy_threshold: 2.0,
}
}
/// Config for Qwen 3.5-0.8B hybrid DeltaNet/Attention model (Plan 182).
///
/// Typical layout: early layers use DeltaNet (linear recurrence, no KV cache),
/// later layers use standard attention. The `layer_types` vec specifies per-layer.
/// If `layer_types` is empty, all layers default to Attention (backward compatible).
#[cfg(feature = "deltanet_inference")]
pub fn qwen_deltanet(n_layer: usize, layer_types: Vec<DeltaNetLayerType>) -> Self {
let n_head = 16;
let head_dim = 128;
let n_embd = n_head * head_dim; // 2048
let mlp_hidden = n_embd * 4; // 8192 (SwiGLU: gate+up = 2× mlp_hidden)
let n_kv_head = n_head; // MHA (no GQA for 0.8B)
Self {
vocab_size: 151936,
block_size: 32768,
n_embd,
n_head,
head_dim,
mlp_hidden,
n_layer,
n_kv_head,
bos_token: 151643, // Qwen BOS
temperature: 0.8,
draft_lookahead: 0,
tree_budget: 0,
parallel_threshold: 8192,
lora_rank: 0,
lora_alpha: 1.0,
lora_dropout: 0.0,
lora_targets: Vec::new(),
screening_threshold: 0.0,
sparse_threshold: 0.0,
early_exit_patience: 0,
early_exit_gap: 0.0,
mtp_activation_threshold: 0,
mtp_cluster_vocab_threshold: 151936,
mtp_shared_kv_prompt_threshold: 32768,
mtp_cluster_size: 1024,
mtp_min_output_tokens: 16,
mtp_cluster_topk: 1,
hla_mode: HlaMode::Standard,
hla_normalize: false,
hla_decay: 1.0,
model_arch: ModelArchitecture::QwenDeltaNet,
rms_norm_eps: 1e-6,
rms_norm_offset: false,
tied_embeddings: false,
use_rope: true,
rope_theta: 10000.0,
post_norm: false,
attn_logit_softcapping: 0.0,
final_logit_softcapping: 0.0,
weight_dtype: WeightDtype::BF16,
mask_token: 0,
attention_mode: AttentionMode::Causal,
sp_kv_window: 128,
sp_kv_threshold: 0.5,
sp_kv_predictor_hidden: 0,
sp_kv_predictor_lr_mult: 5.0,
width_rollouts: 1,
early_stop_threshold: 0.0,
convergence_selector: ConvergenceSelector::default(),
d2f_block_size: 16,
mls_layers: 0,
loop_mode: LoopMode::None,
hybrid_pattern: HybridPattern::Uniform,
loop_min: 0,
loop_max: 0,
gated_attn: false,
parallax_gate_scale: 0.0,
emotion_desperation_threshold: 0.5,
parallax_zero_init: true,
#[cfg(feature = "loop_stability_fix")]
loop_stability_mode: super::LoopStabilityMode::None,
#[cfg(feature = "hydra_budget")]
hydra_profiles: Vec::new(),
layer_types,
deltanet_conv_kernel_size: 4,
deltanet_state_dim: head_dim * head_dim, // 128 × 128 = 16384 per head
deltanet_linear_head_dim: head_dim,
deltanet_linear_n_heads: n_head,
deltanet_linear_n_value_heads: n_kv_head,
#[cfg(feature = "rim_slots")]
rim_block_count: 0,
#[cfg(feature = "rim_slots")]
rim_tokens_per_block: 2,
#[cfg(feature = "rim_slots")]
rim_buffer_token: 0,
#[cfg(feature = "wall_attention")]
wall_config: None,
#[cfg(feature = "collapse_aware_thinking")]
collapse_budget: ThinkingBudget::default(),
#[cfg(feature = "belief_drafter")]
belief_drafter_path: None,
#[cfg(feature = "belief_drafter")]
belief_drafter_entropy_threshold: 2.0,
}
}
/// Validate config consistency. Returns Err with message on invalid config.
pub fn validate(&self) -> Result<(), String> {
if !self.n_head.is_multiple_of(self.n_kv_head) {
return Err(format!(
"n_head ({}) must be divisible by n_kv_head ({})",
self.n_head, self.n_kv_head
));
}
// Gemma 2 intentionally has q_dim != n_embd (e.g., 8*256=2048 != 2304)
// LLaMA with GQA may also have q_dim != n_embd
// QwenDeltaNet also has q_dim == n_embd but is excluded for forward compat
let arch_exempt = match self.model_arch {
ModelArchitecture::Gemma2 | ModelArchitecture::Llama => true,
_ => {
#[cfg(feature = "deltanet_inference")]
if self.model_arch == ModelArchitecture::QwenDeltaNet {
// layer_types length must match n_layer when non-empty
if !self.layer_types.is_empty() && self.layer_types.len() != self.n_layer {
return Err(format!(
"layer_types length ({}) must match n_layer ({})",
self.layer_types.len(),
self.n_layer
));
}
// deltanet_state_dim must be head_dim^2
let expected = self.head_dim * self.head_dim;
if self.deltanet_state_dim != expected {
return Err(format!(
"deltanet_state_dim ({}) must equal head_dim^2 ({})",
self.deltanet_state_dim, expected
));
}
true
} else {
false
}
#[cfg(not(feature = "deltanet_inference"))]
false
}
};
if !arch_exempt && self.n_head * self.head_dim != self.n_embd {
return Err(format!(
"n_head ({}) * head_dim ({}) must equal n_embd ({})",
self.n_head, self.head_dim, self.n_embd
));
}
if self.n_kv_head * self.head_dim > self.n_embd {
return Err(format!(
"n_kv_head ({}) * head_dim ({}) must not exceed n_embd ({})",
self.n_kv_head, self.head_dim, self.n_embd
));
}
// MTP thresholds must be consistent (only for Generic arch; Gemma 2 and Llama don't use MTP)
if self.model_arch == ModelArchitecture::Generic && self.mtp_cluster_size == 0 {
return Err("mtp_cluster_size must be > 0".into());
}
if self.mtp_cluster_topk == 0 {
return Err("mtp_cluster_topk must be >= 1".into());
}
Ok(())
}
/// Apply per-domain inference overrides, returning a new Config.
///
/// Total number of buffer tokens when RiM slots are active: K × M.
/// Returns 0 when disabled (rim_block_count == 0).
#[cfg(feature = "rim_slots")]
#[inline]
pub fn rim_total_buffer_tokens(&self) -> usize {
if self.rim_block_count == 0 {
0
} else {
self.rim_block_count * self.rim_tokens_per_block
}
}
/// Whether RiM buffer slots are active.
#[cfg(feature = "rim_slots")]
#[inline]
pub fn rim_enabled(&self) -> bool {
self.rim_block_count > 0
}
/// Whether Wall Attention is active (Plan 173).
/// True when feature is enabled AND config has wall_config set.
#[cfg(feature = "wall_attention")]
pub fn wall_enabled(&self) -> bool {
self.wall_config.is_some()
}
/// `None` fields are left unchanged; `Some` fields replace the current value.
/// Used by the router to inject domain-specific budgets from TOML config.
pub fn with_overrides(mut self, overrides: &InferenceOverrides) -> Self {
if let Some(v) = overrides.tree_budget {
self.tree_budget = v;
}
if let Some(v) = overrides.draft_lookahead {
self.draft_lookahead = v;
}
if let Some(v) = overrides.parallel_threshold {
self.parallel_threshold = v;
}
if let Some(v) = overrides.screening_threshold {
self.screening_threshold = v;
}
if let Some(v) = overrides.temperature {
self.temperature = v;
}
if let Some(v) = overrides.sparse_threshold {
self.sparse_threshold = v;
}
if let Some(v) = overrides.early_exit_patience {
self.early_exit_patience = v;
}
if let Some(v) = overrides.early_exit_gap {
self.early_exit_gap = v;
}
if let Some(v) = overrides.mtp_activation_threshold {
self.mtp_activation_threshold = v;
}
if let Some(v) = overrides.mtp_cluster_vocab_threshold {
self.mtp_cluster_vocab_threshold = v;
}
if let Some(v) = overrides.mtp_shared_kv_prompt_threshold {
self.mtp_shared_kv_prompt_threshold = v;
}
if let Some(v) = overrides.mtp_cluster_size {
self.mtp_cluster_size = v;
}
if let Some(v) = overrides.mtp_min_output_tokens {
self.mtp_min_output_tokens = v;
}
if let Some(v) = overrides.mtp_cluster_topk {
self.mtp_cluster_topk = v;
}
if let Some(v) = overrides.sp_kv_threshold {
self.sp_kv_threshold = v;
}
if let Some(v) = overrides.width_rollouts {
self.width_rollouts = v;
}
if let Some(v) = overrides.early_stop_threshold {
self.early_stop_threshold = v;
}
if let Some(v) = overrides.convergence_selector {
self.convergence_selector = v;
}
if let Some(v) = overrides.mls_layers {
self.mls_layers = v;
}
// SR²AM horizon truncation override (Plan 112 T11)
if let Some(v) = overrides.max_plan_horizon {
self.draft_lookahead = self.draft_lookahead.min(v);
}
// Hydra Adaptive Layer Budget overrides (Research 148, Plan 165)
// Applied via HydraBudgetConfig at call site, not directly on Config.
// The overrides fields exist on InferenceOverrides for downstream consumption.
self
}
}
// ---------------------------------------------------------------------------
// InferenceOverrides
// ---------------------------------------------------------------------------
/// Override DTO for applying per-domain inference budget to a [`Config`].
///
/// All fields are `Option` — `None` means "keep Config's current value".
/// This is a plain struct (no serde) to keep `katgpt-core` dependency-free
/// from router/TOML types. Conversion from the router's `InferenceBudget`
/// happens at the router boundary.
///
/// Note: `decode_strategy` is NOT included here because it depends on
/// project-specific types. Each project handles it at the call site.
///
/// See Plan 026 (AutoTTS Dynamic Inference Budget).
#[derive(Debug, Clone, Default)]
// Fields ordered by descending alignment to minimize padding:
// Option<usize>/Option<PathBuf> (16/32 bytes) → Option<f32> (8 bytes) →
// Option<#[repr(u8)] enum> (2 bytes).
pub struct InferenceOverrides {
// --- Option<usize> (16 bytes each, 8-byte aligned) ---
pub tree_budget: Option<usize>,
pub draft_lookahead: Option<usize>,
pub parallel_threshold: Option<usize>,
pub early_exit_patience: Option<usize>,
// MTP Drafter overrides (Plan 055: Gemma 4 MTP)
pub mtp_activation_threshold: Option<usize>,
pub mtp_cluster_vocab_threshold: Option<usize>,
pub mtp_shared_kv_prompt_threshold: Option<usize>,
pub mtp_cluster_size: Option<usize>,
/// Minimum expected output tokens for MTP (Plan 117 T15).
/// When overridden, skips MTP when remaining tokens < threshold.
pub mtp_min_output_tokens: Option<usize>,
/// Top-K cluster selection for clustered LM head (Plan 117 T22).
/// When K > 1, compute logits for tokens in top-K clusters instead of just top-1.
pub mtp_cluster_topk: Option<usize>,
// PTRM width scaling (Plan 083)
pub width_rollouts: Option<usize>,
// MLS Multi-Layer Sum override (Plan 104)
pub mls_layers: Option<usize>,
// SR²AM horizon truncation override (Plan 112 T11)
pub max_plan_horizon: Option<usize>,
// --- Option<PathBuf> (32 bytes, 8-byte aligned) ---
// Drafter LoRA path (Plan 117: MTP LoRA Drafter)
pub drafter_lora_path: Option<std::path::PathBuf>,
// --- Option<f32> (8 bytes each, 4-byte aligned) ---
pub screening_threshold: Option<f32>,
pub temperature: Option<f32>,
pub sparse_threshold: Option<f32>,
pub early_exit_gap: Option<f32>,
// SP-KV inference-time threshold knob (Plan 070)
pub sp_kv_threshold: Option<f32>,
pub early_stop_threshold: Option<f32>,
// --- Option<#[repr(u8) enum> (2 bytes each, 1-byte aligned) ---
// EqR Convergence Selection (Plan 119)
pub convergence_selector: Option<ConvergenceSelector>,
// --- Hydra Adaptive Layer Budget (Research 148, Plan 165) ---
/// Override Hydra skip threshold.
#[cfg(feature = "hydra_budget")]
pub hydra_skip_threshold: Option<f32>,
/// Override Hydra erasure-skip-draft flag.
#[cfg(feature = "hydra_budget")]
pub hydra_skip_erasure_draft: Option<bool>,
// --- Adaptive Depth Tier (Plan 284) ---
/// Override depth tier for layer count capping at inference time.
/// When set, caps the layer loop to tier.max_layers().
/// None = use all layers (backward compatible).
pub depth_tier: Option<DepthTier>,
}
impl Default for Config {
fn default() -> Self {
Self::micro()
}
}
// ---------------------------------------------------------------------------
// KV dimension helper
// ---------------------------------------------------------------------------
/// KV dimension: total float count per token in KV cache.
#[inline(always)]
pub fn kv_dim(config: &Config) -> usize {
config.n_kv_head * config.head_dim
}