Skip to content

API Reference

triadic_head

triadic_head

triadic-head — Drop-in triadic projection head for any HuggingFace transformer.

Adds interpretable prime-factor semantic signatures to language models at zero language cost. A single linear layer (n_embd -> n_bits) produces discrete prime composites that enable exact algebraic operations: subsumption, composition, analogy, and gap analysis.

Quick start

from triadic_head import TriadicWrapper

model = TriadicWrapper("gpt2", n_bits=64) sigs = model.encode(["king", "queen", "dog"]) result = model.compare("king", "queen")

Algebra Module

Pure Python module with zero external dependencies. Provides exact algebraic operations on prime-factor signatures.

triadic_head.algebra

Prime-factor algebra for neurosymbolic semantic operations.

Provides exact algebraic operations (subsumption, composition, gap analysis) on prime-factor signatures — things impossible with cosine similarity.

Zero external dependencies.

PrimeMapper

Maps continuous projections (tanh outputs in [-1, 1]) to composite prime integers.

Each bit position is assigned a unique prime. If projection[i] > 0, prime[i] is included in the composite product.

Example

projections = [0.5, -0.2, 0.8, 0.1] (4 bits) primes = [2, 3, 5, 7 ] composite = 2 * 5 * 7 = 70 (bits 0, 2, 3 are positive)

Source code in triadic-head-src/triadic-head/triadic_head/algebra.py
 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
class PrimeMapper:
    """
    Maps continuous projections (tanh outputs in [-1, 1]) to composite prime integers.

    Each bit position is assigned a unique prime. If projection[i] > 0,
    prime[i] is included in the composite product.

    Example:
        projections = [0.5, -0.2, 0.8, 0.1]  (4 bits)
        primes      = [2,    3,    5,   7  ]
        composite   = 2 * 5 * 7 = 70  (bits 0, 2, 3 are positive)
    """

    def __init__(self, n_bits: int):
        self.n_bits = n_bits
        self.primes = [nth_prime(i + 1) for i in range(n_bits)]

    def encode(self, projections) -> int:
        """
        Convert projection values to a composite prime integer.

        Args:
            projections: sequence of floats (tanh outputs in [-1, 1])
                         or a 1D tensor.

        Returns:
            int — composite prime product.
        """
        composite = 1
        for proj, prime in zip(projections, self.primes):
            val = float(proj)
            if val > 0:
                composite *= prime
        # Return 1 (identity element) when all projections negative,
        # rather than prime 2 which has specific semantic meaning.
        return composite

    # Alias for compatibility with src/triadic.py which uses map()
    map = encode

    def get_bits(self, projections) -> List[int]:
        """Return binary bit pattern from projections."""
        return [1 if float(p) > 0 else 0 for p in projections]

    def explain(self, composite: int) -> Dict:
        """Decompose a composite into its constituent prime factors."""
        factors = []
        bit_indices = []
        for i, prime in enumerate(self.primes):
            if composite % prime == 0:
                factors.append(prime)
                bit_indices.append(i)
        return {
            'composite': composite,
            'factors': factors,
            'active_bits': bit_indices,
            'n_active': len(factors),
            'n_total': self.n_bits,
        }

    def similarity(self, a: int, b: int) -> float:
        """Jaccard similarity over prime factor sets."""
        return TriadicValidator.similarity(a, b)

encode(projections) -> int

Convert projection values to a composite prime integer.

Parameters:

Name Type Description Default
projections

sequence of floats (tanh outputs in [-1, 1]) or a 1D tensor.

required

Returns:

Type Description
int

int — composite prime product.

Source code in triadic-head-src/triadic-head/triadic_head/algebra.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def encode(self, projections) -> int:
    """
    Convert projection values to a composite prime integer.

    Args:
        projections: sequence of floats (tanh outputs in [-1, 1])
                     or a 1D tensor.

    Returns:
        int — composite prime product.
    """
    composite = 1
    for proj, prime in zip(projections, self.primes):
        val = float(proj)
        if val > 0:
            composite *= prime
    # Return 1 (identity element) when all projections negative,
    # rather than prime 2 which has specific semantic meaning.
    return composite

get_bits(projections) -> List[int]

Return binary bit pattern from projections.

Source code in triadic-head-src/triadic-head/triadic_head/algebra.py
110
111
112
def get_bits(self, projections) -> List[int]:
    """Return binary bit pattern from projections."""
    return [1 if float(p) > 0 else 0 for p in projections]

explain(composite: int) -> Dict

Decompose a composite into its constituent prime factors.

Source code in triadic-head-src/triadic-head/triadic_head/algebra.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def explain(self, composite: int) -> Dict:
    """Decompose a composite into its constituent prime factors."""
    factors = []
    bit_indices = []
    for i, prime in enumerate(self.primes):
        if composite % prime == 0:
            factors.append(prime)
            bit_indices.append(i)
    return {
        'composite': composite,
        'factors': factors,
        'active_bits': bit_indices,
        'n_active': len(factors),
        'n_total': self.n_bits,
    }

similarity(a: int, b: int) -> float

Jaccard similarity over prime factor sets.

Source code in triadic-head-src/triadic-head/triadic_head/algebra.py
130
131
132
def similarity(self, a: int, b: int) -> float:
    """Jaccard similarity over prime factor sets."""
    return TriadicValidator.similarity(a, b)

TriadicValidator

Verifies semantic relationships using prime-factor algebra.

Three operations IMPOSSIBLE with cosine similarity
  1. Subsumption — Does concept A contain all features of B?
  2. Composition — Create a concept with features of A and B.
  3. Gap Analysis — Exactly WHICH features differ between A and B?
Source code in triadic-head-src/triadic-head/triadic_head/algebra.py
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
class TriadicValidator:
    """
    Verifies semantic relationships using prime-factor algebra.

    Three operations IMPOSSIBLE with cosine similarity:
      1. Subsumption — Does concept A contain all features of B?
      2. Composition — Create a concept with features of A and B.
      3. Gap Analysis — Exactly WHICH features differ between A and B?
    """

    @staticmethod
    def subsumes(a: int, b: int) -> bool:
        """Does concept A contain ALL semantic features of B?  (A % B == 0)"""
        if b == 0:
            return False
        return a % b == 0

    @staticmethod
    def compose(*concepts: int) -> int:
        """Combine features from multiple concepts (LCM)."""
        result = concepts[0]
        for c in concepts[1:]:
            result = (result * c) // math.gcd(result, c)
        return result

    @staticmethod
    def explain_gap(a: int, b: int) -> Dict:
        """Explain exactly WHY two concepts differ."""
        shared = math.gcd(a, b)
        only_a = a // shared
        only_b = b // shared
        return {
            'shared': shared,
            'shared_factors': prime_factors(shared),
            'only_in_a': only_a,
            'only_in_a_factors': prime_factors(only_a),
            'only_in_b': only_b,
            'only_in_b_factors': prime_factors(only_b),
            'a_contains_b': (a % b == 0) if b > 0 else False,
            'b_contains_a': (b % a == 0) if a > 0 else False,
        }

    @staticmethod
    def similarity(a: int, b: int) -> float:
        """Jaccard similarity over prime factor sets. Range: [0.0, 1.0]."""
        fa = set(prime_factors(a))
        fb = set(prime_factors(b))
        if not fa and not fb:
            return 1.0
        total = fa | fb
        return len(fa & fb) / len(total) if total else 0.0

    @staticmethod
    def analogy(a: int, b: int, c: int) -> int:
        """
        Analogy: A is to B as C is to ?

        Computes the transformation from A->B (factors removed/added),
        then applies it to C to find D.

        D = (C / GCD(C, only_in_a)) * only_in_b
        where only_in_a = A/GCD(A,B) and only_in_b = B/GCD(A,B)

        Two steps: (1) remove A-specific factors from C, (2) add B-specific.
        """
        shared_ab = math.gcd(a, b)
        only_in_a = a // shared_ab  # factors to remove
        only_in_b = b // shared_ab  # factors to add

        # Remove A-specific factors from C (where they overlap)
        c_reduced = c // math.gcd(c, only_in_a)
        # Add B-specific factors (avoiding duplicates via GCD)
        return (c_reduced * only_in_b) // math.gcd(c_reduced, only_in_b)

subsumes(a: int, b: int) -> bool staticmethod

Does concept A contain ALL semantic features of B? (A % B == 0)

Source code in triadic-head-src/triadic-head/triadic_head/algebra.py
149
150
151
152
153
154
@staticmethod
def subsumes(a: int, b: int) -> bool:
    """Does concept A contain ALL semantic features of B?  (A % B == 0)"""
    if b == 0:
        return False
    return a % b == 0

compose(*concepts: int) -> int staticmethod

Combine features from multiple concepts (LCM).

Source code in triadic-head-src/triadic-head/triadic_head/algebra.py
156
157
158
159
160
161
162
@staticmethod
def compose(*concepts: int) -> int:
    """Combine features from multiple concepts (LCM)."""
    result = concepts[0]
    for c in concepts[1:]:
        result = (result * c) // math.gcd(result, c)
    return result

explain_gap(a: int, b: int) -> Dict staticmethod

Explain exactly WHY two concepts differ.

Source code in triadic-head-src/triadic-head/triadic_head/algebra.py
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
@staticmethod
def explain_gap(a: int, b: int) -> Dict:
    """Explain exactly WHY two concepts differ."""
    shared = math.gcd(a, b)
    only_a = a // shared
    only_b = b // shared
    return {
        'shared': shared,
        'shared_factors': prime_factors(shared),
        'only_in_a': only_a,
        'only_in_a_factors': prime_factors(only_a),
        'only_in_b': only_b,
        'only_in_b_factors': prime_factors(only_b),
        'a_contains_b': (a % b == 0) if b > 0 else False,
        'b_contains_a': (b % a == 0) if a > 0 else False,
    }

similarity(a: int, b: int) -> float staticmethod

Jaccard similarity over prime factor sets. Range: [0.0, 1.0].

Source code in triadic-head-src/triadic-head/triadic_head/algebra.py
181
182
183
184
185
186
187
188
189
@staticmethod
def similarity(a: int, b: int) -> float:
    """Jaccard similarity over prime factor sets. Range: [0.0, 1.0]."""
    fa = set(prime_factors(a))
    fb = set(prime_factors(b))
    if not fa and not fb:
        return 1.0
    total = fa | fb
    return len(fa & fb) / len(total) if total else 0.0

analogy(a: int, b: int, c: int) -> int staticmethod

Analogy: A is to B as C is to ?

Computes the transformation from A->B (factors removed/added), then applies it to C to find D.

D = (C / GCD(C, only_in_a)) * only_in_b where only_in_a = A/GCD(A,B) and only_in_b = B/GCD(A,B)

Two steps: (1) remove A-specific factors from C, (2) add B-specific.

Source code in triadic-head-src/triadic-head/triadic_head/algebra.py
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
@staticmethod
def analogy(a: int, b: int, c: int) -> int:
    """
    Analogy: A is to B as C is to ?

    Computes the transformation from A->B (factors removed/added),
    then applies it to C to find D.

    D = (C / GCD(C, only_in_a)) * only_in_b
    where only_in_a = A/GCD(A,B) and only_in_b = B/GCD(A,B)

    Two steps: (1) remove A-specific factors from C, (2) add B-specific.
    """
    shared_ab = math.gcd(a, b)
    only_in_a = a // shared_ab  # factors to remove
    only_in_b = b // shared_ab  # factors to add

    # Remove A-specific factors from C (where they overlap)
    c_reduced = c // math.gcd(c, only_in_a)
    # Add B-specific factors (avoiding duplicates via GCD)
    return (c_reduced * only_in_b) // math.gcd(c_reduced, only_in_b)

sieve_primes(limit: int) -> List[int]

Return all primes up to limit via Sieve of Eratosthenes.

Source code in triadic-head-src/triadic-head/triadic_head/algebra.py
18
19
20
21
22
23
24
25
26
27
28
def sieve_primes(limit: int) -> List[int]:
    """Return all primes up to `limit` via Sieve of Eratosthenes."""
    if limit < 2:
        return []
    is_prime = [True] * (limit + 1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(limit ** 0.5) + 1):
        if is_prime[i]:
            for j in range(i * i, limit + 1, i):
                is_prime[j] = False
    return [i for i in range(2, limit + 1) if is_prime[i]]

nth_prime(n: int) -> int

Return the nth prime (1-indexed: nth_prime(1) = 2).

Source code in triadic-head-src/triadic-head/triadic_head/algebra.py
34
35
36
37
38
39
40
41
42
43
44
45
def nth_prime(n: int) -> int:
    """Return the nth prime (1-indexed: nth_prime(1) = 2)."""
    if n <= 0:
        raise ValueError("n must be >= 1")
    while len(_PRIMES_CACHE) < n:
        candidate = _PRIMES_CACHE[-1] + 2
        while True:
            if all(candidate % p != 0 for p in _PRIMES_CACHE if p * p <= candidate):
                _PRIMES_CACHE.append(candidate)
                break
            candidate += 2
    return _PRIMES_CACHE[n - 1]

prime_factors(n: int) -> List[int]

Return sorted list of unique prime factors of n.

Source code in triadic-head-src/triadic-head/triadic_head/algebra.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def prime_factors(n: int) -> List[int]:
    """Return sorted list of unique prime factors of n."""
    if n <= 1:
        return []
    factors = []
    d = 2
    temp = n
    while d * d <= temp:
        if temp % d == 0:
            factors.append(d)
            while temp % d == 0:
                temp //= d
        d += 1
    if temp > 1:
        factors.append(temp)
    return factors

Wrapper Module

PyTorch wrapper for HuggingFace transformers. Adds a triadic projection head to any causal language model.

triadic_head.wrapper

TriadicWrapper — Drop-in triadic projection head for any HuggingFace transformer.

Usage

from triadic_head import TriadicWrapper

model = TriadicWrapper("gpt2", n_bits=64) model.freeze_backbone()

... train phase 1 (triadic head only) ...

model.unfreeze_last_n(2)

... train phase 2 (joint optimization) ...

sigs = model.encode(["king", "queen", "dog"]) model.compare("king", "queen") # -> 0.89

TriadicHead

Bases: Module

Single linear projection: hidden_states -> tanh -> n_bits continuous values.

This is the only new parameter added to the backbone model. For GPT-2 (768D, 64 bits): 768 * 64 = 49,152 parameters.

Source code in triadic-head-src/triadic-head/triadic_head/wrapper.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
class TriadicHead(nn.Module):
    """
    Single linear projection: hidden_states -> tanh -> n_bits continuous values.

    This is the only new parameter added to the backbone model.
    For GPT-2 (768D, 64 bits): 768 * 64 = 49,152 parameters.
    """

    def __init__(self, n_embd: int, n_bits: int):
        super().__init__()
        self.proj = nn.Linear(n_embd, n_bits, bias=False)
        nn.init.normal_(self.proj.weight, mean=0.0, std=0.02)

    def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
        """(B, T, n_embd) -> (B, T, n_bits) in [-1, 1]."""
        return torch.tanh(self.proj(hidden_states))

forward(hidden_states: torch.Tensor) -> torch.Tensor

(B, T, n_embd) -> (B, T, n_bits) in [-1, 1].

Source code in triadic-head-src/triadic-head/triadic_head/wrapper.py
113
114
115
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
    """(B, T, n_embd) -> (B, T, n_bits) in [-1, 1]."""
    return torch.tanh(self.proj(hidden_states))

TriadicWrapper

Bases: Module

Wraps any HuggingFace causal LM with a triadic projection head.

The wrapper adds a single linear layer (n_embd -> n_bits) that produces discrete prime-factor signatures alongside normal language model outputs. Training uses a multi-component triadic loss that transfers semantic structure from the model's own embeddings to the triadic head.

Parameters:

Name Type Description Default
model Union[str, Module]

HuggingFace model instance or model name string (e.g. "gpt2").

required
n_bits int

Number of triadic bits (default 64). Each bit maps to a prime.

64
align_mode str

Alignment loss type — "mse", "rank", or "infonce". Use "infonce" for pre-trained models, "mse" for from-scratch.

'infonce'
device Optional[str]

Device to place model on (auto-detected if None).

None
Source code in triadic-head-src/triadic-head/triadic_head/wrapper.py
 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
class TriadicWrapper(nn.Module):
    """
    Wraps any HuggingFace causal LM with a triadic projection head.

    The wrapper adds a single linear layer (n_embd -> n_bits) that produces
    discrete prime-factor signatures alongside normal language model outputs.
    Training uses a multi-component triadic loss that transfers semantic
    structure from the model's own embeddings to the triadic head.

    Args:
        model: HuggingFace model instance or model name string (e.g. "gpt2").
        n_bits: Number of triadic bits (default 64). Each bit maps to a prime.
        align_mode: Alignment loss type — "mse", "rank", or "infonce".
                    Use "infonce" for pre-trained models, "mse" for from-scratch.
        device: Device to place model on (auto-detected if None).
    """

    def __init__(
        self,
        model: Union[str, nn.Module],
        n_bits: int = 64,
        align_mode: str = 'infonce',
        device: Optional[str] = None,
    ):
        super().__init__()

        # Load model from string if needed
        if isinstance(model, str):
            from transformers import AutoModelForCausalLM
            model = AutoModelForCausalLM.from_pretrained(model)

        self._arch = _detect_architecture(model)
        self.backbone_model = model
        self.n_bits = n_bits
        self.n_embd = self._arch['n_embd']
        self.align_mode = align_mode
        self.block_size = self._arch.get('block_size', 1024)

        # The one new thing: triadic projection head
        self.triadic_head = TriadicHead(self.n_embd, n_bits)

        if device:
            self.to(device)

    # ----------------------------------------------------------
    # Configuration — view and change settings
    # ----------------------------------------------------------

    def config(self, **kwargs) -> Dict:
        """
        View or update model configuration.

        Call with no arguments to view current config.
        Pass keyword arguments to update:

            model.config(align_mode='rank', n_bits=32)

        Changeable settings:
            align_mode: 'mse' | 'rank' | 'infonce'

        Read-only (shown but not changeable):
            n_bits, n_embd, backbone, block_size, triadic_params, total_params
        """
        VALID_ALIGN = ('mse', 'rank', 'infonce')

        if 'align_mode' in kwargs:
            mode = kwargs['align_mode']
            if mode not in VALID_ALIGN:
                raise ValueError(f"align_mode must be one of {VALID_ALIGN}, got {mode!r}")
            self.align_mode = mode

        return {
            'align_mode': self.align_mode,
            'n_bits': self.n_bits,
            'n_embd': self.n_embd,
            'backbone': type(self.backbone_model).__name__,
            'block_size': self.block_size,
            'triadic_params': self.triadic_params(),
            'total_params': self.num_params(),
        }

    # ----------------------------------------------------------
    # Freeze / unfreeze
    # ----------------------------------------------------------

    def freeze_backbone(self):
        """Freeze entire backbone. Only triadic head receives gradients."""
        for param in self.backbone_model.parameters():
            param.requires_grad = False
        for param in self.triadic_head.parameters():
            param.requires_grad = True

    def unfreeze_last_n(self, n: int = 2):
        """Unfreeze the last N transformer layers + final layer norm."""
        if 'ln_f' in self._arch:
            for param in self._arch['ln_f'].parameters():
                param.requires_grad = True
        layers = self._arch['layers']
        total = len(layers)
        for i in range(max(0, total - n), total):
            for param in layers[i].parameters():
                param.requires_grad = True

    def unfreeze_all(self):
        """Unfreeze everything."""
        for param in self.parameters():
            param.requires_grad = True

    # ----------------------------------------------------------
    # Forward pass
    # ----------------------------------------------------------

    def forward(
        self,
        input_ids: torch.Tensor,
        attention_mask: Optional[torch.Tensor] = None,
        labels: Optional[torch.Tensor] = None,
    ):
        """
        Forward pass through backbone + triadic head.

        Returns:
            logits:       (B, T, vocab_size)
            triadic_proj: (B, T, n_bits) in [-1, 1]
            lang_loss:    scalar or None
        """
        backbone = self._arch['backbone']
        outputs = backbone(
            input_ids=input_ids,
            attention_mask=attention_mask,
        )
        # Handle different return types
        if hasattr(outputs, 'last_hidden_state'):
            hidden = outputs.last_hidden_state
        elif isinstance(outputs, tuple):
            hidden = outputs[0]
        else:
            hidden = outputs

        # LM head
        lm_head = self._arch.get('lm_head')
        if lm_head is not None:
            logits = lm_head(hidden)
        else:
            logits = self.backbone_model.lm_head(hidden)

        # Triadic head
        triadic_proj = self.triadic_head(hidden)

        # Language loss
        lang_loss = None
        if labels is not None:
            shift_logits = logits[..., :-1, :].contiguous()
            shift_labels = labels[..., 1:].contiguous()
            lang_loss = F.cross_entropy(
                shift_logits.view(-1, shift_logits.size(-1)),
                shift_labels.view(-1),
                ignore_index=-100,
            )

        return logits, triadic_proj, lang_loss

    # ----------------------------------------------------------
    # Triadic loss
    # ----------------------------------------------------------

    def triadic_loss(
        self,
        triadic_proj: torch.Tensor,
        input_ids: Optional[torch.Tensor] = None,
        alpha: float = 0.05,
        entropy_weight: float = 1.0,
        align_weight: float = 5.0,
        align_mode: Optional[str] = None,
    ) -> torch.Tensor:
        """
        Compute multi-component triadic loss.

        Components:
          1. Diversity — each bit should fire ~50% of the time.
          2. Contrastive — different sequences should produce different signatures.
          3. Entropy — prevent dead bits (bits stuck at +1 or -1).
          4. Embedding alignment — transfer semantic structure from embeddings.

        Args:
            triadic_proj: (B, T, n_bits) from forward().
            input_ids: (B, T) needed for alignment loss.
            alpha: Weight of total triadic loss (default 0.05, DO NOT exceed 0.10).
            entropy_weight: Weight for entropy term (default 1.0).
            align_weight: Weight for alignment term (default 5.0).
            align_mode: Override self.align_mode for this call.

        Returns:
            Weighted triadic loss scalar (already multiplied by alpha).
        """
        mode = align_mode or self.align_mode

        if triadic_proj.size(1) < 2:
            return torch.tensor(0.0, device=triadic_proj.device)

        # Force float32 for numerical stability (critical under AMP/mixed precision)
        triadic_proj = triadic_proj.float()

        B, T, n_bits = triadic_proj.shape

        # 1. Diversity: push per-bit mean toward 0
        bit_means = triadic_proj.mean(dim=(0, 1))
        diversity = (bit_means ** 2).mean()

        # 2. Contrastive: push sequence-level projections apart
        if B > 1:
            seq_proj = F.normalize(triadic_proj.mean(dim=1), dim=-1)
            sim = seq_proj @ seq_proj.T
            mask = ~torch.eye(B, device=sim.device, dtype=torch.bool)
            contrastive = sim[mask].pow(2).mean()
        else:
            contrastive = torch.tensor(0.0, device=triadic_proj.device)

        # 3. Entropy: maximize per-bit entropy across the batch
        entropy = torch.tensor(0.0, device=triadic_proj.device)
        if entropy_weight > 0:
            flat = triadic_proj.reshape(-1, n_bits)
            probs = ((flat.mean(dim=0) + 1.0) / 2.0).clamp(1e-7, 1 - 1e-7)
            H = -(probs * probs.log() + (1 - probs) * (1 - probs).log())
            entropy = (1.0 - H / math.log(2)).mean()

        # 4. Embedding alignment
        alignment = torch.tensor(0.0, device=triadic_proj.device)
        if align_weight > 0 and input_ids is not None:
            with torch.no_grad():
                embeds = self._arch['embed_layer'](input_ids).detach().float()

            if mode == 'mse':
                alignment = self._align_mse(triadic_proj, embeds)
            elif mode == 'rank':
                alignment = self._align_rank(triadic_proj, embeds)
            elif mode == 'infonce':
                alignment = self._align_infonce(triadic_proj, embeds)

        raw = diversity + contrastive
        if entropy_weight > 0:
            raw = raw + entropy_weight * entropy
        if align_weight > 0:
            raw = raw + align_weight * alignment

        return alpha * raw

    # ----------------------------------------------------------
    # Alignment loss implementations
    # ----------------------------------------------------------

    def _align_mse(self, proj, embeds, n_pairs=64):
        """MSE on absolute cosine similarity values. Best for from-scratch."""
        B, T, n_bits = proj.shape
        idx = torch.randint(0, T, (B, n_pairs, 2), device=proj.device)
        i, j = idx[:, :, 0], idx[:, :, 1]

        e_i = torch.gather(embeds, 1, i.unsqueeze(-1).expand(-1, -1, embeds.size(-1)))
        e_j = torch.gather(embeds, 1, j.unsqueeze(-1).expand(-1, -1, embeds.size(-1)))
        embed_sim = F.cosine_similarity(e_i, e_j, dim=-1)

        p_i = torch.gather(proj, 1, i.unsqueeze(-1).expand(-1, -1, n_bits))
        p_j = torch.gather(proj, 1, j.unsqueeze(-1).expand(-1, -1, n_bits))
        tri_sim = F.cosine_similarity(p_i, p_j, dim=-1)

        return F.mse_loss(tri_sim, embed_sim)

    def _align_rank(self, proj, embeds, n_anchors=32, n_cands=16, margin=0.1):
        """Margin ranking: preserve similarity ORDERING. Best for analogies."""
        B, T, n_bits = proj.shape
        d = embeds.size(-1)

        a_idx = torch.randint(0, T, (B, n_anchors), device=proj.device)
        c_idx = torch.randint(0, T, (B, n_anchors, n_cands), device=proj.device)

        a_e = torch.gather(embeds, 1, a_idx.unsqueeze(-1).expand(-1, -1, d))
        c_e = torch.gather(
            embeds, 1, c_idx.reshape(B, -1).unsqueeze(-1).expand(-1, -1, d)
        ).reshape(B, n_anchors, n_cands, d)

        e_sim = F.cosine_similarity(a_e.unsqueeze(2), c_e, dim=-1)
        pos_local = e_sim.argmax(dim=-1)
        neg_local = e_sim.argmin(dim=-1)

        pos_idx = torch.gather(c_idx, 2, pos_local.unsqueeze(-1)).squeeze(-1)
        neg_idx = torch.gather(c_idx, 2, neg_local.unsqueeze(-1)).squeeze(-1)

        a_p = torch.gather(proj, 1, a_idx.unsqueeze(-1).expand(-1, -1, n_bits))
        pos_p = torch.gather(proj, 1, pos_idx.unsqueeze(-1).expand(-1, -1, n_bits))
        neg_p = torch.gather(proj, 1, neg_idx.unsqueeze(-1).expand(-1, -1, n_bits))

        pos_sim = F.cosine_similarity(a_p, pos_p, dim=-1)
        neg_sim = F.cosine_similarity(a_p, neg_p, dim=-1)

        return F.relu(margin - (pos_sim - neg_sim)).mean()

    def _align_infonce(self, proj, embeds, n_anchors=32, temperature=0.1):
        """InfoNCE with embedding-mined positives. Best for pre-trained models."""
        B, T, n_bits = proj.shape
        d = embeds.size(-1)

        a_idx = torch.randint(0, T, (B, n_anchors), device=proj.device)
        p_idx = torch.randint(0, T, (B, n_anchors), device=proj.device)

        a_e = F.normalize(
            torch.gather(embeds, 1, a_idx.unsqueeze(-1).expand(-1, -1, d)), dim=-1)
        p_e = F.normalize(
            torch.gather(embeds, 1, p_idx.unsqueeze(-1).expand(-1, -1, d)), dim=-1)

        e_sim = torch.bmm(a_e, p_e.transpose(1, 2))
        pos_labels = e_sim.argmax(dim=-1)

        a_p = F.normalize(
            torch.gather(proj, 1, a_idx.unsqueeze(-1).expand(-1, -1, n_bits)), dim=-1)
        p_p = F.normalize(
            torch.gather(proj, 1, p_idx.unsqueeze(-1).expand(-1, -1, n_bits)), dim=-1)

        logits = torch.bmm(a_p, p_p.transpose(1, 2)) / temperature
        return F.cross_entropy(logits.reshape(-1, n_anchors), pos_labels.reshape(-1))

    # ----------------------------------------------------------
    # Encode: text -> prime signatures
    # ----------------------------------------------------------

    @torch.no_grad()
    def encode(
        self,
        texts: Union[str, List[str]],
        tokenizer=None,
    ) -> Dict[str, dict]:
        """
        Encode text(s) to prime-factor signatures.

        Args:
            texts: Single string or list of strings.
            tokenizer: HuggingFace tokenizer. If None, uses AutoTokenizer
                       matching the backbone model.

        Returns:
            Dict mapping each text to:
              - 'composite': int (prime product)
              - 'bits': List[int] (binary pattern)
              - 'projection': List[float] (raw tanh values)
              - 'n_active': int (number of active bits)
        """
        from .algebra import PrimeMapper

        if isinstance(texts, str):
            texts = [texts]

        if tokenizer is None:
            from transformers import AutoTokenizer
            config = getattr(self.backbone_model, 'config', None)
            model_name = getattr(config, '_name_or_path', 'gpt2')
            tokenizer = AutoTokenizer.from_pretrained(model_name)
            if tokenizer.pad_token is None:
                tokenizer.pad_token = tokenizer.eos_token

        device = next(self.parameters()).device
        mapper = PrimeMapper(self.n_bits)
        results = {}

        for text in texts:
            tokens = tokenizer(text, return_tensors='pt', truncation=True,
                               max_length=self.block_size).to(device)
            _, triadic_proj, _ = self(tokens['input_ids'],
                                     attention_mask=tokens.get('attention_mask'))
            # Average across all token positions
            avg_proj = triadic_proj.mean(dim=1).squeeze(0)  # (n_bits,)
            proj_list = avg_proj.cpu().tolist()

            composite = mapper.encode(proj_list)
            bits = mapper.get_bits(proj_list)

            results[text] = {
                'composite': composite,
                'bits': bits,
                'projection': proj_list,
                'n_active': sum(bits),
                'factors': mapper.explain(composite)['factors'],
            }

        return results

    @torch.no_grad()
    def compare(
        self,
        text_a: str,
        text_b: str,
        tokenizer=None,
    ) -> Dict:
        """
        Compare two texts using prime-factor algebra.

        Returns:
            Dict with 'similarity', 'shared_factors', 'only_in_a', 'only_in_b'.
        """
        from .algebra import TriadicValidator

        sigs = self.encode([text_a, text_b], tokenizer=tokenizer)
        a = sigs[text_a]['composite']
        b = sigs[text_b]['composite']

        gap = TriadicValidator.explain_gap(a, b)
        gap['similarity'] = TriadicValidator.similarity(a, b)
        gap['a_text'] = text_a
        gap['b_text'] = text_b
        return gap

    # ----------------------------------------------------------
    # Validation — Is training working?
    # ----------------------------------------------------------

    @torch.no_grad()
    def validate(
        self,
        tokenizer=None,
        word_groups: Optional[Dict[str, List[str]]] = None,
        verbose: bool = True,
        training_steps: int = 0,
    ) -> Dict:
        """
        Run diagnostic checks to verify triadic head training quality.

        Checks:
          1. Diversity — are signatures unique? (>75% = PASS)
          2. Active bits — are enough bits firing? (15-85% = PASS)
          3. Semantic ordering — related words more similar than unrelated? (gap > 0 = PASS)
          4. Random baseline — is the gap significantly above what random bits would produce?

        Also provides per-group breakdown so you can see which semantic
        categories the model handles well and which need more training.

        Args:
            tokenizer: HuggingFace tokenizer (auto-loaded if None).
            word_groups: Custom word groups to test, e.g.
                         {"animals": ["dog", "cat"], "colors": ["red", "blue"]}.
                         Uses built-in groups if None.
            verbose: Print results to console.
            training_steps: Total training steps completed (for guidance messages).

        Returns:
            Dict with check results, per-group breakdown, random baseline,
            overall PASS/FAIL, config snapshot, and all signatures.
        """
        import random as _random
        from .algebra import PrimeMapper, TriadicValidator as TV

        if word_groups is None:
            word_groups = {
                'royalty': ['king', 'queen', 'prince', 'throne'],
                'animals': ['dog', 'cat', 'fish', 'bird'],
                'emotions': ['happy', 'sad', 'angry', 'love'],
                'food': ['bread', 'water', 'rice', 'fruit'],
            }

        all_words = [w for group in word_groups.values() for w in group]
        sigs = self.encode(all_words, tokenizer=tokenizer)

        # --- Check 1: Diversity ---
        composites = [sigs[w]['composite'] for w in all_words]
        unique_ratio = len(set(composites)) / len(composites)

        # --- Check 2: Active bits ---
        active_counts = [sigs[w]['n_active'] for w in all_words]
        avg_active = sum(active_counts) / len(active_counts)
        active_frac = avg_active / self.n_bits

        # --- Check 3: Semantic ordering (global + per-group) ---
        groups = list(word_groups.values())
        intra_sims, inter_sims = [], []
        group_details = {}

        for gname, group in word_groups.items():
            g_intra = []
            for i, w1 in enumerate(group):
                for w2 in group[i + 1:]:
                    sim = TV.similarity(sigs[w1]['composite'], sigs[w2]['composite'])
                    g_intra.append(sim)
                    intra_sims.append(sim)
            # Inter: this group vs all other groups
            g_inter = []
            for other_name, other_group in word_groups.items():
                if other_name == gname:
                    continue
                for w1 in group:
                    for w2 in other_group:
                        sim = TV.similarity(sigs[w1]['composite'], sigs[w2]['composite'])
                        g_inter.append(sim)
                        inter_sims.append(sim)

            avg_gi = sum(g_intra) / len(g_intra) if g_intra else 0
            avg_ge = sum(g_inter) / len(g_inter) if g_inter else 0
            group_details[gname] = {
                'words': group,
                'intra_sim': avg_gi,
                'inter_sim': avg_ge,
                'gap': avg_gi - avg_ge,
                'pass': (avg_gi - avg_ge) > 0,
            }

        # Deduplicate inter_sims (each pair counted from both group perspectives)
        # Recalculate cleanly
        inter_sims_clean = []
        for gi, g1 in enumerate(groups):
            for g2 in groups[gi + 1:]:
                for w1 in g1:
                    for w2 in g2:
                        inter_sims_clean.append(
                            TV.similarity(sigs[w1]['composite'], sigs[w2]['composite'])
                        )

        avg_intra = sum(intra_sims) / len(intra_sims) if intra_sims else 0
        avg_inter = sum(inter_sims_clean) / len(inter_sims_clean) if inter_sims_clean else 0
        semantic_gap = avg_intra - avg_inter

        # --- Check 4: Random baseline ---
        # Generate random bit patterns and measure what gap you'd get by pure chance.
        # This tells the user how much of the measured gap is real signal vs noise.
        n_words = len(all_words)
        n_trials = 100  # average over 100 random assignments
        mapper = PrimeMapper(self.n_bits)
        random_gaps = []
        _random.seed(42)

        for _ in range(n_trials):
            # Generate random composites with similar active-bit ratio as model
            random_composites = []
            for _ in range(n_words):
                bits = [1 if _random.random() < active_frac else 0 for _ in range(self.n_bits)]
                proj = [(b * 2.0 - 1.0) for b in bits]  # convert to [-1, 1]
                random_composites.append(mapper.encode(proj))

            # Compute intra/inter using same group structure
            r_intra, r_inter = [], []
            word_idx = 0
            for group in groups:
                g_composites = random_composites[word_idx:word_idx + len(group)]
                word_idx += len(group)
                for ii in range(len(g_composites)):
                    for jj in range(ii + 1, len(g_composites)):
                        r_intra.append(TV.similarity(g_composites[ii], g_composites[jj]))

            # Inter: all cross-group pairs
            word_idx = 0
            group_composites = []
            for group in groups:
                group_composites.append(random_composites[word_idx:word_idx + len(group)])
                word_idx += len(group)

            for gi in range(len(group_composites)):
                for gj in range(gi + 1, len(group_composites)):
                    for c1 in group_composites[gi]:
                        for c2 in group_composites[gj]:
                            r_inter.append(TV.similarity(c1, c2))

            r_avg_intra = sum(r_intra) / len(r_intra) if r_intra else 0
            r_avg_inter = sum(r_inter) / len(r_inter) if r_inter else 0
            random_gaps.append(r_avg_intra - r_avg_inter)

        random_gap_mean = sum(random_gaps) / len(random_gaps)
        random_gap_std = (sum((g - random_gap_mean) ** 2 for g in random_gaps) / len(random_gaps)) ** 0.5
        signal_above_random = semantic_gap - random_gap_mean

        # The gap is meaningful if it's at least 2 standard deviations above random
        baseline_pass = signal_above_random > max(2 * random_gap_std, 0.01)

        baseline_info = {
            'random_gap_mean': random_gap_mean,
            'random_gap_std': random_gap_std,
            'model_gap': semantic_gap,
            'signal_above_random': signal_above_random,
            'pass': baseline_pass,
            'detail': (f"model gap {semantic_gap:+.1%} vs random baseline {random_gap_mean:+.1%} "
                       f"(signal {signal_above_random:+.1%}, random std {random_gap_std:.1%})"),
        }

        # --- Build results ---
        checks = {
            'diversity': {
                'value': unique_ratio,
                'pass': unique_ratio > 0.75,
                'detail': f"{len(set(composites))}/{len(composites)} unique signatures ({unique_ratio:.0%})",
            },
            'active_bits': {
                'value': active_frac,
                'pass': 0.15 < active_frac < 0.85,
                'detail': f"{avg_active:.1f}/{self.n_bits} bits active on avg ({active_frac:.0%})",
            },
            'semantic_ordering': {
                'intra': avg_intra,
                'inter': avg_inter,
                'gap': semantic_gap,
                'pass': semantic_gap > 0,
                'detail': f"within-group {avg_intra:.1%} vs between-group {avg_inter:.1%} (gap {semantic_gap:+.1%})",
            },
            'random_baseline': baseline_info,
        }

        overall = all(c['pass'] for c in checks.values())

        # --- Training guidance ---
        guidance = self._training_guidance(training_steps, semantic_gap, signal_above_random, baseline_pass)

        if verbose:
            print(f"\n{'=' * 60}")
            print("  TRIADIC HEAD — VALIDATION REPORT")
            print(f"{'=' * 60}")
            print(f"  Config: {self.n_bits} bits | align_mode={self.align_mode} | "
                  f"{self.triadic_params():,} triadic params")
            if training_steps > 0:
                print(f"  Training: {training_steps:,} steps completed")
            print(f"{'-' * 60}")

            for name, check in checks.items():
                status = "PASS" if check['pass'] else "FAIL"
                print(f"  [{status}] {name}: {check['detail']}")

            # Per-group breakdown
            print("\n  PER-GROUP BREAKDOWN:")
            for gname, gd in group_details.items():
                status = "PASS" if gd['pass'] else "FAIL"
                print(f"    [{status}] {gname}: intra {gd['intra_sim']:.0%} vs inter {gd['inter_sim']:.0%} "
                      f"(gap {gd['gap']:+.1%}) — {gd['words']}")

            # Per-word active bits
            print("\n  PER-WORD ACTIVE BITS:")
            for w in all_words:
                print(f"    {w:>12}: {sigs[w]['n_active']:2d}/{self.n_bits} bits active")

            print(f"{'-' * 60}")
            if overall:
                print("  RESULT: PASS — Triadic head is producing meaningful signatures.")
                print(f"  Signal above random: {signal_above_random:+.1%}")
            else:
                failed = [n for n, c in checks.items() if not c['pass']]
                print(f"  RESULT: FAIL — Issues detected: {', '.join(failed)}")
                if 'diversity' in failed:
                    print("    -> Signatures are too similar. Train longer or increase entropy_weight.")
                if 'active_bits' in failed:
                    if active_frac <= 0.15:
                        print("    -> Too few bits active. Increase entropy_weight to activate dead bits.")
                    else:
                        print("    -> Too many bits active. Decrease alpha or increase training steps.")
                if 'semantic_ordering' in failed:
                    print("    -> Related words aren't more similar than unrelated.")
                    print("       Try: increase align_weight, or use align_mode='infonce' for pre-trained models.")
                if 'random_baseline' in failed:
                    print("    -> Gap is not significantly above random chance.")
                    print("       The model may be producing hash-like signatures without real semantics.")
                    print("       This usually means MORE TRAINING is needed (see guidance below).")
                failing_groups = [g for g, d in group_details.items() if not d['pass']]
                if failing_groups:
                    print(f"    -> Weak groups: {', '.join(failing_groups)} — consider adding more training data for these domains.")

            # Always show training guidance
            if guidance:
                print("\n  TRAINING GUIDANCE:")
                for line in guidance:
                    print(f"    {line}")

            print(f"{'=' * 60}")

        return {
            'checks': checks,
            'overall_pass': overall,
            'group_details': group_details,
            'random_baseline': baseline_info,
            'guidance': guidance,
            'config': self.config(),
            'signatures': sigs,
        }

    @staticmethod
    def _training_guidance(training_steps: int, semantic_gap: float,
                           signal_above_random: float, baseline_pass: bool) -> List[str]:
        """Generate actionable training guidance based on current results."""
        lines = []

        # Recommended minimums (from 26 training runs in Triadic MicroGPT research)
        RECOMMENDED = {
            'smoke_test': 5_000,
            'minimum_viable': 20_000,
            'good_quality': 50_000,
            'production': 100_000,
        }

        if training_steps > 0 and training_steps < RECOMMENDED['minimum_viable']:
            lines.append(f"WARNING: {training_steps:,} steps is a quick smoke test, not a full training run.")
            lines.append(f"Minimum recommended: {RECOMMENDED['minimum_viable']:,} steps for meaningful results.")
            lines.append("")

        lines.append("Recommended training steps:")
        lines.append(f"  Smoke test:      {RECOMMENDED['smoke_test']:>8,} steps  (verify pipeline works)")
        lines.append(f"  Minimum viable:  {RECOMMENDED['minimum_viable']:>8,} steps  (basic semantic ordering)")
        lines.append(f"  Good quality:    {RECOMMENDED['good_quality']:>8,} steps  (reliable word relationships)")
        lines.append(f"  Production:      {RECOMMENDED['production']:>8,} steps+ (publish-ready signatures)")

        if not baseline_pass:
            lines.append("")
            lines.append(f"Your model's semantic gap ({semantic_gap:+.1%}) is close to random noise ({signal_above_random:+.1%} above random).")
            lines.append("This is expected with short training. The model needs more steps to")
            lines.append("learn real word relationships vs statistical coincidence.")
        elif signal_above_random > 0 and signal_above_random < 0.05:
            lines.append("")
            lines.append(f"Your model shows weak signal above random ({signal_above_random:+.1%}).")
            lines.append("More training will strengthen real semantic relationships.")
        elif signal_above_random >= 0.05:
            lines.append("")
            lines.append(f"Good signal above random: {signal_above_random:+.1%}")

        lines.append("")
        lines.append("For large models (LLaMA, Mistral, etc.), expect to need more steps.")
        lines.append("Monitor the semantic gap: it should grow steadily during training.")
        lines.append("If gap plateaus, try: increase align_weight or switch align_mode.")

        return lines

    # ----------------------------------------------------------
    # Explore — Discover relationships between words
    # ----------------------------------------------------------

    @torch.no_grad()
    def explore(
        self,
        words: List[str],
        tokenizer=None,
        top_k: int = 0,
        threshold: Optional[float] = None,
        show_factors: bool = False,
        verbose: bool = True,
    ) -> Dict:
        """
        Full audit of relationships between words with similarity matrix.

        Args:
            words: List of words/phrases to compare.
            tokenizer: HuggingFace tokenizer (auto-loaded if None).
            top_k: Show only top-K and bottom-K pairs (0 = show ALL pairs).
            threshold: If set, flag pairs above this similarity for review.
            show_factors: Show shared/unique prime factors for every pair.
            verbose: Print results to console.

        Returns:
            Dict with similarity matrix, ranked pairs, signatures, and flagged pairs.
        """
        from .algebra import TriadicValidator as TV

        sigs = self.encode(words, tokenizer=tokenizer)
        n = len(words)

        # Build similarity matrix
        matrix = [[0.0] * n for _ in range(n)]
        pairs = []

        for i in range(n):
            matrix[i][i] = 1.0
            for j in range(i + 1, n):
                sim = TV.similarity(
                    sigs[words[i]]['composite'],
                    sigs[words[j]]['composite'],
                )
                gap_info = TV.explain_gap(
                    sigs[words[i]]['composite'],
                    sigs[words[j]]['composite'],
                )
                matrix[i][j] = sim
                matrix[j][i] = sim
                pairs.append({
                    'similarity': sim,
                    'word_a': words[i],
                    'word_b': words[j],
                    'shared_factors': gap_info['shared_factors'],
                    'only_a_factors': gap_info['only_in_a_factors'],
                    'only_b_factors': gap_info['only_in_b_factors'],
                    'n_shared': len(gap_info['shared_factors']),
                    'n_only_a': len(gap_info['only_in_a_factors']),
                    'n_only_b': len(gap_info['only_in_b_factors']),
                })

        pairs.sort(key=lambda p: p['similarity'], reverse=True)

        # Flag pairs above threshold
        flagged = []
        if threshold is not None:
            flagged = [p for p in pairs if p['similarity'] >= threshold]

        if verbose:
            col_w = min(max(max(len(w) for w in words), 6), 10)

            print(f"\n{'=' * 60}")
            print("  SIMILARITY MATRIX")
            print(f"{'=' * 60}")

            # Header
            header = " " * (col_w + 2) + "".join(f"{w[:col_w]:>{col_w + 1}}" for w in words)
            print(header)

            for i, w in enumerate(words):
                row_vals = []
                for j in range(n):
                    if i == j:
                        row_vals.append(f"{'---':>{col_w + 1}}")
                    else:
                        row_vals.append(f"{matrix[i][j]:>{col_w}.0%} ")
                print(f"{w[:col_w]:>{col_w}}  {''.join(row_vals)}")

            # Pairs listing
            if top_k > 0 and len(pairs) > top_k * 2:
                print(f"\n  TOP {top_k} most similar:")
                for p in pairs[:top_k]:
                    line = f"    {p['word_a']} <-> {p['word_b']}: {p['similarity']:.0%} ({p['n_shared']} shared)"
                    if show_factors:
                        line += f" shared={p['shared_factors']}"
                    print(line)
                print(f"\n  TOP {top_k} least similar:")
                for p in pairs[-top_k:]:
                    line = f"    {p['word_a']} <-> {p['word_b']}: {p['similarity']:.0%}"
                    print(line)
            else:
                print("\n  ALL PAIRS (ranked by similarity):")
                for p in pairs:
                    line = f"    {p['word_a']:>12} <-> {p['word_b']:<12}: {p['similarity']:.0%} ({p['n_shared']} shared, {p['n_only_a']} only-a, {p['n_only_b']} only-b)"
                    if show_factors:
                        line += f"\n{'':>42}shared={p['shared_factors']}"
                        if p['only_a_factors']:
                            line += f"\n{'':>42}only-{p['word_a']}={p['only_a_factors']}"
                        if p['only_b_factors']:
                            line += f"\n{'':>42}only-{p['word_b']}={p['only_b_factors']}"
                    print(line)

            # Flagged pairs
            if threshold is not None:
                print(f"\n  FLAGGED (similarity >= {threshold:.0%}): {len(flagged)} pairs")
                if flagged:
                    for p in flagged:
                        print(f"    {p['word_a']} <-> {p['word_b']}: {p['similarity']:.0%}")
                else:
                    print("    (none)")

            # Per-word signature card
            print("\n  PER-WORD SIGNATURES:")
            for i, w in enumerate(words):
                sims_for_w = [matrix[i][j] for j in range(n) if i != j]
                avg = sum(sims_for_w) / len(sims_for_w)
                most_sim_j = max((j for j in range(n) if j != i), key=lambda j: matrix[i][j])
                least_sim_j = min((j for j in range(n) if j != i), key=lambda j: matrix[i][j])
                primes = sigs[w]['factors']
                print(f"    {w:>12}: {sigs[w]['n_active']} bits | avg sim {avg:.0%} "
                      f"| closest: {words[most_sim_j]} ({matrix[i][most_sim_j]:.0%}) "
                      f"| furthest: {words[least_sim_j]} ({matrix[i][least_sim_j]:.0%})")
                if show_factors:
                    print(f"{'':>16}primes={primes}")

            # Factor index: which words share each prime?
            if show_factors:
                from .algebra import nth_prime as _nth_prime
                factor_index = {}  # prime -> list of words
                for w in words:
                    for p in sigs[w]['factors']:
                        factor_index.setdefault(p, []).append(w)

                # Sort by number of words sharing the factor (most shared first)
                sorted_factors = sorted(factor_index.items(),
                                        key=lambda x: len(x[1]), reverse=True)

                print("\n  FACTOR INDEX (what each prime means):")
                print(f"    {'prime':>8}  {'bit':>3}  {'words':>5}  shared by")
                print(f"    {'-'*8}  {'-'*3}  {'-'*5}  {'-'*30}")
                for prime, sharing_words in sorted_factors:
                    # Find bit index for this prime
                    bit_idx = next(
                        (i for i in range(self.n_bits) if _nth_prime(i + 1) == prime),
                        -1
                    )
                    count = len(sharing_words)
                    label = ', '.join(sharing_words)
                    if count == len(words):
                        label += '  [UNIVERSAL]'
                    elif count == 1:
                        label += '  [UNIQUE]'
                    print(f"    {prime:>8}  {bit_idx:>3}  {count:>5}  {label}")

            print(f"{'=' * 60}")

        return {
            'matrix': matrix,
            'words': words,
            'pairs_ranked': pairs,
            'signatures': sigs,
            'flagged': flagged,
        }

    # ----------------------------------------------------------
    # Generation (pass-through to backbone)
    # ----------------------------------------------------------

    @torch.no_grad()
    def generate(self, input_ids, max_new_tokens=100, temperature=0.7, top_k=50):
        """Autoregressive text generation."""
        for _ in range(max_new_tokens):
            idx = input_ids[:, -self.block_size:]
            logits, _, _ = self(idx)
            logits = logits[:, -1, :] / temperature
            if top_k is not None:
                v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
                logits[logits < v[:, [-1]]] = float('-inf')
            probs = F.softmax(logits, dim=-1)
            next_id = torch.multinomial(probs, num_samples=1)
            input_ids = torch.cat([input_ids, next_id], dim=1)
        return input_ids

    # ----------------------------------------------------------
    # Utilities
    # ----------------------------------------------------------

    def num_params(self, trainable_only=False) -> int:
        if trainable_only:
            return sum(p.numel() for p in self.parameters() if p.requires_grad)
        return sum(p.numel() for p in self.parameters())

    def triadic_params(self) -> int:
        """Number of parameters in the triadic head only."""
        return sum(p.numel() for p in self.triadic_head.parameters())

    def __repr__(self):
        return (
            f"TriadicWrapper(\n"
            f"  backbone={type(self.backbone_model).__name__},\n"
            f"  n_embd={self.n_embd}, n_bits={self.n_bits},\n"
            f"  align_mode='{self.align_mode}',\n"
            f"  triadic_params={self.triadic_params():,},\n"
            f"  total_params={self.num_params():,}\n"
            f")"
        )

config(**kwargs) -> Dict

View or update model configuration.

Call with no arguments to view current config. Pass keyword arguments to update:

model.config(align_mode='rank', n_bits=32)
Changeable settings

align_mode: 'mse' | 'rank' | 'infonce'

Read-only (shown but not changeable): n_bits, n_embd, backbone, block_size, triadic_params, total_params

Source code in triadic-head-src/triadic-head/triadic_head/wrapper.py
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
def config(self, **kwargs) -> Dict:
    """
    View or update model configuration.

    Call with no arguments to view current config.
    Pass keyword arguments to update:

        model.config(align_mode='rank', n_bits=32)

    Changeable settings:
        align_mode: 'mse' | 'rank' | 'infonce'

    Read-only (shown but not changeable):
        n_bits, n_embd, backbone, block_size, triadic_params, total_params
    """
    VALID_ALIGN = ('mse', 'rank', 'infonce')

    if 'align_mode' in kwargs:
        mode = kwargs['align_mode']
        if mode not in VALID_ALIGN:
            raise ValueError(f"align_mode must be one of {VALID_ALIGN}, got {mode!r}")
        self.align_mode = mode

    return {
        'align_mode': self.align_mode,
        'n_bits': self.n_bits,
        'n_embd': self.n_embd,
        'backbone': type(self.backbone_model).__name__,
        'block_size': self.block_size,
        'triadic_params': self.triadic_params(),
        'total_params': self.num_params(),
    }

freeze_backbone()

Freeze entire backbone. Only triadic head receives gradients.

Source code in triadic-head-src/triadic-head/triadic_head/wrapper.py
207
208
209
210
211
212
def freeze_backbone(self):
    """Freeze entire backbone. Only triadic head receives gradients."""
    for param in self.backbone_model.parameters():
        param.requires_grad = False
    for param in self.triadic_head.parameters():
        param.requires_grad = True

unfreeze_last_n(n: int = 2)

Unfreeze the last N transformer layers + final layer norm.

Source code in triadic-head-src/triadic-head/triadic_head/wrapper.py
214
215
216
217
218
219
220
221
222
223
def unfreeze_last_n(self, n: int = 2):
    """Unfreeze the last N transformer layers + final layer norm."""
    if 'ln_f' in self._arch:
        for param in self._arch['ln_f'].parameters():
            param.requires_grad = True
    layers = self._arch['layers']
    total = len(layers)
    for i in range(max(0, total - n), total):
        for param in layers[i].parameters():
            param.requires_grad = True

unfreeze_all()

Unfreeze everything.

Source code in triadic-head-src/triadic-head/triadic_head/wrapper.py
225
226
227
228
def unfreeze_all(self):
    """Unfreeze everything."""
    for param in self.parameters():
        param.requires_grad = True

forward(input_ids: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None)

Forward pass through backbone + triadic head.

Returns:

Name Type Description
logits

(B, T, vocab_size)

triadic_proj

(B, T, n_bits) in [-1, 1]

lang_loss

scalar or None

Source code in triadic-head-src/triadic-head/triadic_head/wrapper.py
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
def forward(
    self,
    input_ids: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
    labels: Optional[torch.Tensor] = None,
):
    """
    Forward pass through backbone + triadic head.

    Returns:
        logits:       (B, T, vocab_size)
        triadic_proj: (B, T, n_bits) in [-1, 1]
        lang_loss:    scalar or None
    """
    backbone = self._arch['backbone']
    outputs = backbone(
        input_ids=input_ids,
        attention_mask=attention_mask,
    )
    # Handle different return types
    if hasattr(outputs, 'last_hidden_state'):
        hidden = outputs.last_hidden_state
    elif isinstance(outputs, tuple):
        hidden = outputs[0]
    else:
        hidden = outputs

    # LM head
    lm_head = self._arch.get('lm_head')
    if lm_head is not None:
        logits = lm_head(hidden)
    else:
        logits = self.backbone_model.lm_head(hidden)

    # Triadic head
    triadic_proj = self.triadic_head(hidden)

    # Language loss
    lang_loss = None
    if labels is not None:
        shift_logits = logits[..., :-1, :].contiguous()
        shift_labels = labels[..., 1:].contiguous()
        lang_loss = F.cross_entropy(
            shift_logits.view(-1, shift_logits.size(-1)),
            shift_labels.view(-1),
            ignore_index=-100,
        )

    return logits, triadic_proj, lang_loss

triadic_loss(triadic_proj: torch.Tensor, input_ids: Optional[torch.Tensor] = None, alpha: float = 0.05, entropy_weight: float = 1.0, align_weight: float = 5.0, align_mode: Optional[str] = None) -> torch.Tensor

Compute multi-component triadic loss.

Components
  1. Diversity — each bit should fire ~50% of the time.
  2. Contrastive — different sequences should produce different signatures.
  3. Entropy — prevent dead bits (bits stuck at +1 or -1).
  4. Embedding alignment — transfer semantic structure from embeddings.

Parameters:

Name Type Description Default
triadic_proj Tensor

(B, T, n_bits) from forward().

required
input_ids Optional[Tensor]

(B, T) needed for alignment loss.

None
alpha float

Weight of total triadic loss (default 0.05, DO NOT exceed 0.10).

0.05
entropy_weight float

Weight for entropy term (default 1.0).

1.0
align_weight float

Weight for alignment term (default 5.0).

5.0
align_mode Optional[str]

Override self.align_mode for this call.

None

Returns:

Type Description
Tensor

Weighted triadic loss scalar (already multiplied by alpha).

Source code in triadic-head-src/triadic-head/triadic_head/wrapper.py
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
def triadic_loss(
    self,
    triadic_proj: torch.Tensor,
    input_ids: Optional[torch.Tensor] = None,
    alpha: float = 0.05,
    entropy_weight: float = 1.0,
    align_weight: float = 5.0,
    align_mode: Optional[str] = None,
) -> torch.Tensor:
    """
    Compute multi-component triadic loss.

    Components:
      1. Diversity — each bit should fire ~50% of the time.
      2. Contrastive — different sequences should produce different signatures.
      3. Entropy — prevent dead bits (bits stuck at +1 or -1).
      4. Embedding alignment — transfer semantic structure from embeddings.

    Args:
        triadic_proj: (B, T, n_bits) from forward().
        input_ids: (B, T) needed for alignment loss.
        alpha: Weight of total triadic loss (default 0.05, DO NOT exceed 0.10).
        entropy_weight: Weight for entropy term (default 1.0).
        align_weight: Weight for alignment term (default 5.0).
        align_mode: Override self.align_mode for this call.

    Returns:
        Weighted triadic loss scalar (already multiplied by alpha).
    """
    mode = align_mode or self.align_mode

    if triadic_proj.size(1) < 2:
        return torch.tensor(0.0, device=triadic_proj.device)

    # Force float32 for numerical stability (critical under AMP/mixed precision)
    triadic_proj = triadic_proj.float()

    B, T, n_bits = triadic_proj.shape

    # 1. Diversity: push per-bit mean toward 0
    bit_means = triadic_proj.mean(dim=(0, 1))
    diversity = (bit_means ** 2).mean()

    # 2. Contrastive: push sequence-level projections apart
    if B > 1:
        seq_proj = F.normalize(triadic_proj.mean(dim=1), dim=-1)
        sim = seq_proj @ seq_proj.T
        mask = ~torch.eye(B, device=sim.device, dtype=torch.bool)
        contrastive = sim[mask].pow(2).mean()
    else:
        contrastive = torch.tensor(0.0, device=triadic_proj.device)

    # 3. Entropy: maximize per-bit entropy across the batch
    entropy = torch.tensor(0.0, device=triadic_proj.device)
    if entropy_weight > 0:
        flat = triadic_proj.reshape(-1, n_bits)
        probs = ((flat.mean(dim=0) + 1.0) / 2.0).clamp(1e-7, 1 - 1e-7)
        H = -(probs * probs.log() + (1 - probs) * (1 - probs).log())
        entropy = (1.0 - H / math.log(2)).mean()

    # 4. Embedding alignment
    alignment = torch.tensor(0.0, device=triadic_proj.device)
    if align_weight > 0 and input_ids is not None:
        with torch.no_grad():
            embeds = self._arch['embed_layer'](input_ids).detach().float()

        if mode == 'mse':
            alignment = self._align_mse(triadic_proj, embeds)
        elif mode == 'rank':
            alignment = self._align_rank(triadic_proj, embeds)
        elif mode == 'infonce':
            alignment = self._align_infonce(triadic_proj, embeds)

    raw = diversity + contrastive
    if entropy_weight > 0:
        raw = raw + entropy_weight * entropy
    if align_weight > 0:
        raw = raw + align_weight * alignment

    return alpha * raw

encode(texts: Union[str, List[str]], tokenizer=None) -> Dict[str, dict]

Encode text(s) to prime-factor signatures.

Parameters:

Name Type Description Default
texts Union[str, List[str]]

Single string or list of strings.

required
tokenizer

HuggingFace tokenizer. If None, uses AutoTokenizer matching the backbone model.

None

Returns:

Type Description
Dict[str, dict]

Dict mapping each text to: - 'composite': int (prime product) - 'bits': List[int] (binary pattern) - 'projection': List[float] (raw tanh values) - 'n_active': int (number of active bits)

Source code in triadic-head-src/triadic-head/triadic_head/wrapper.py
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
@torch.no_grad()
def encode(
    self,
    texts: Union[str, List[str]],
    tokenizer=None,
) -> Dict[str, dict]:
    """
    Encode text(s) to prime-factor signatures.

    Args:
        texts: Single string or list of strings.
        tokenizer: HuggingFace tokenizer. If None, uses AutoTokenizer
                   matching the backbone model.

    Returns:
        Dict mapping each text to:
          - 'composite': int (prime product)
          - 'bits': List[int] (binary pattern)
          - 'projection': List[float] (raw tanh values)
          - 'n_active': int (number of active bits)
    """
    from .algebra import PrimeMapper

    if isinstance(texts, str):
        texts = [texts]

    if tokenizer is None:
        from transformers import AutoTokenizer
        config = getattr(self.backbone_model, 'config', None)
        model_name = getattr(config, '_name_or_path', 'gpt2')
        tokenizer = AutoTokenizer.from_pretrained(model_name)
        if tokenizer.pad_token is None:
            tokenizer.pad_token = tokenizer.eos_token

    device = next(self.parameters()).device
    mapper = PrimeMapper(self.n_bits)
    results = {}

    for text in texts:
        tokens = tokenizer(text, return_tensors='pt', truncation=True,
                           max_length=self.block_size).to(device)
        _, triadic_proj, _ = self(tokens['input_ids'],
                                 attention_mask=tokens.get('attention_mask'))
        # Average across all token positions
        avg_proj = triadic_proj.mean(dim=1).squeeze(0)  # (n_bits,)
        proj_list = avg_proj.cpu().tolist()

        composite = mapper.encode(proj_list)
        bits = mapper.get_bits(proj_list)

        results[text] = {
            'composite': composite,
            'bits': bits,
            'projection': proj_list,
            'n_active': sum(bits),
            'factors': mapper.explain(composite)['factors'],
        }

    return results

compare(text_a: str, text_b: str, tokenizer=None) -> Dict

Compare two texts using prime-factor algebra.

Returns:

Type Description
Dict

Dict with 'similarity', 'shared_factors', 'only_in_a', 'only_in_b'.

Source code in triadic-head-src/triadic-head/triadic_head/wrapper.py
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
@torch.no_grad()
def compare(
    self,
    text_a: str,
    text_b: str,
    tokenizer=None,
) -> Dict:
    """
    Compare two texts using prime-factor algebra.

    Returns:
        Dict with 'similarity', 'shared_factors', 'only_in_a', 'only_in_b'.
    """
    from .algebra import TriadicValidator

    sigs = self.encode([text_a, text_b], tokenizer=tokenizer)
    a = sigs[text_a]['composite']
    b = sigs[text_b]['composite']

    gap = TriadicValidator.explain_gap(a, b)
    gap['similarity'] = TriadicValidator.similarity(a, b)
    gap['a_text'] = text_a
    gap['b_text'] = text_b
    return gap

validate(tokenizer=None, word_groups: Optional[Dict[str, List[str]]] = None, verbose: bool = True, training_steps: int = 0) -> Dict

Run diagnostic checks to verify triadic head training quality.

Checks
  1. Diversity — are signatures unique? (>75% = PASS)
  2. Active bits — are enough bits firing? (15-85% = PASS)
  3. Semantic ordering — related words more similar than unrelated? (gap > 0 = PASS)
  4. Random baseline — is the gap significantly above what random bits would produce?

Also provides per-group breakdown so you can see which semantic categories the model handles well and which need more training.

Parameters:

Name Type Description Default
tokenizer

HuggingFace tokenizer (auto-loaded if None).

None
word_groups Optional[Dict[str, List[str]]]

Custom word groups to test, e.g. {"animals": ["dog", "cat"], "colors": ["red", "blue"]}. Uses built-in groups if None.

None
verbose bool

Print results to console.

True
training_steps int

Total training steps completed (for guidance messages).

0

Returns:

Type Description
Dict

Dict with check results, per-group breakdown, random baseline,

Dict

overall PASS/FAIL, config snapshot, and all signatures.

Source code in triadic-head-src/triadic-head/triadic_head/wrapper.py
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
@torch.no_grad()
def validate(
    self,
    tokenizer=None,
    word_groups: Optional[Dict[str, List[str]]] = None,
    verbose: bool = True,
    training_steps: int = 0,
) -> Dict:
    """
    Run diagnostic checks to verify triadic head training quality.

    Checks:
      1. Diversity — are signatures unique? (>75% = PASS)
      2. Active bits — are enough bits firing? (15-85% = PASS)
      3. Semantic ordering — related words more similar than unrelated? (gap > 0 = PASS)
      4. Random baseline — is the gap significantly above what random bits would produce?

    Also provides per-group breakdown so you can see which semantic
    categories the model handles well and which need more training.

    Args:
        tokenizer: HuggingFace tokenizer (auto-loaded if None).
        word_groups: Custom word groups to test, e.g.
                     {"animals": ["dog", "cat"], "colors": ["red", "blue"]}.
                     Uses built-in groups if None.
        verbose: Print results to console.
        training_steps: Total training steps completed (for guidance messages).

    Returns:
        Dict with check results, per-group breakdown, random baseline,
        overall PASS/FAIL, config snapshot, and all signatures.
    """
    import random as _random
    from .algebra import PrimeMapper, TriadicValidator as TV

    if word_groups is None:
        word_groups = {
            'royalty': ['king', 'queen', 'prince', 'throne'],
            'animals': ['dog', 'cat', 'fish', 'bird'],
            'emotions': ['happy', 'sad', 'angry', 'love'],
            'food': ['bread', 'water', 'rice', 'fruit'],
        }

    all_words = [w for group in word_groups.values() for w in group]
    sigs = self.encode(all_words, tokenizer=tokenizer)

    # --- Check 1: Diversity ---
    composites = [sigs[w]['composite'] for w in all_words]
    unique_ratio = len(set(composites)) / len(composites)

    # --- Check 2: Active bits ---
    active_counts = [sigs[w]['n_active'] for w in all_words]
    avg_active = sum(active_counts) / len(active_counts)
    active_frac = avg_active / self.n_bits

    # --- Check 3: Semantic ordering (global + per-group) ---
    groups = list(word_groups.values())
    intra_sims, inter_sims = [], []
    group_details = {}

    for gname, group in word_groups.items():
        g_intra = []
        for i, w1 in enumerate(group):
            for w2 in group[i + 1:]:
                sim = TV.similarity(sigs[w1]['composite'], sigs[w2]['composite'])
                g_intra.append(sim)
                intra_sims.append(sim)
        # Inter: this group vs all other groups
        g_inter = []
        for other_name, other_group in word_groups.items():
            if other_name == gname:
                continue
            for w1 in group:
                for w2 in other_group:
                    sim = TV.similarity(sigs[w1]['composite'], sigs[w2]['composite'])
                    g_inter.append(sim)
                    inter_sims.append(sim)

        avg_gi = sum(g_intra) / len(g_intra) if g_intra else 0
        avg_ge = sum(g_inter) / len(g_inter) if g_inter else 0
        group_details[gname] = {
            'words': group,
            'intra_sim': avg_gi,
            'inter_sim': avg_ge,
            'gap': avg_gi - avg_ge,
            'pass': (avg_gi - avg_ge) > 0,
        }

    # Deduplicate inter_sims (each pair counted from both group perspectives)
    # Recalculate cleanly
    inter_sims_clean = []
    for gi, g1 in enumerate(groups):
        for g2 in groups[gi + 1:]:
            for w1 in g1:
                for w2 in g2:
                    inter_sims_clean.append(
                        TV.similarity(sigs[w1]['composite'], sigs[w2]['composite'])
                    )

    avg_intra = sum(intra_sims) / len(intra_sims) if intra_sims else 0
    avg_inter = sum(inter_sims_clean) / len(inter_sims_clean) if inter_sims_clean else 0
    semantic_gap = avg_intra - avg_inter

    # --- Check 4: Random baseline ---
    # Generate random bit patterns and measure what gap you'd get by pure chance.
    # This tells the user how much of the measured gap is real signal vs noise.
    n_words = len(all_words)
    n_trials = 100  # average over 100 random assignments
    mapper = PrimeMapper(self.n_bits)
    random_gaps = []
    _random.seed(42)

    for _ in range(n_trials):
        # Generate random composites with similar active-bit ratio as model
        random_composites = []
        for _ in range(n_words):
            bits = [1 if _random.random() < active_frac else 0 for _ in range(self.n_bits)]
            proj = [(b * 2.0 - 1.0) for b in bits]  # convert to [-1, 1]
            random_composites.append(mapper.encode(proj))

        # Compute intra/inter using same group structure
        r_intra, r_inter = [], []
        word_idx = 0
        for group in groups:
            g_composites = random_composites[word_idx:word_idx + len(group)]
            word_idx += len(group)
            for ii in range(len(g_composites)):
                for jj in range(ii + 1, len(g_composites)):
                    r_intra.append(TV.similarity(g_composites[ii], g_composites[jj]))

        # Inter: all cross-group pairs
        word_idx = 0
        group_composites = []
        for group in groups:
            group_composites.append(random_composites[word_idx:word_idx + len(group)])
            word_idx += len(group)

        for gi in range(len(group_composites)):
            for gj in range(gi + 1, len(group_composites)):
                for c1 in group_composites[gi]:
                    for c2 in group_composites[gj]:
                        r_inter.append(TV.similarity(c1, c2))

        r_avg_intra = sum(r_intra) / len(r_intra) if r_intra else 0
        r_avg_inter = sum(r_inter) / len(r_inter) if r_inter else 0
        random_gaps.append(r_avg_intra - r_avg_inter)

    random_gap_mean = sum(random_gaps) / len(random_gaps)
    random_gap_std = (sum((g - random_gap_mean) ** 2 for g in random_gaps) / len(random_gaps)) ** 0.5
    signal_above_random = semantic_gap - random_gap_mean

    # The gap is meaningful if it's at least 2 standard deviations above random
    baseline_pass = signal_above_random > max(2 * random_gap_std, 0.01)

    baseline_info = {
        'random_gap_mean': random_gap_mean,
        'random_gap_std': random_gap_std,
        'model_gap': semantic_gap,
        'signal_above_random': signal_above_random,
        'pass': baseline_pass,
        'detail': (f"model gap {semantic_gap:+.1%} vs random baseline {random_gap_mean:+.1%} "
                   f"(signal {signal_above_random:+.1%}, random std {random_gap_std:.1%})"),
    }

    # --- Build results ---
    checks = {
        'diversity': {
            'value': unique_ratio,
            'pass': unique_ratio > 0.75,
            'detail': f"{len(set(composites))}/{len(composites)} unique signatures ({unique_ratio:.0%})",
        },
        'active_bits': {
            'value': active_frac,
            'pass': 0.15 < active_frac < 0.85,
            'detail': f"{avg_active:.1f}/{self.n_bits} bits active on avg ({active_frac:.0%})",
        },
        'semantic_ordering': {
            'intra': avg_intra,
            'inter': avg_inter,
            'gap': semantic_gap,
            'pass': semantic_gap > 0,
            'detail': f"within-group {avg_intra:.1%} vs between-group {avg_inter:.1%} (gap {semantic_gap:+.1%})",
        },
        'random_baseline': baseline_info,
    }

    overall = all(c['pass'] for c in checks.values())

    # --- Training guidance ---
    guidance = self._training_guidance(training_steps, semantic_gap, signal_above_random, baseline_pass)

    if verbose:
        print(f"\n{'=' * 60}")
        print("  TRIADIC HEAD — VALIDATION REPORT")
        print(f"{'=' * 60}")
        print(f"  Config: {self.n_bits} bits | align_mode={self.align_mode} | "
              f"{self.triadic_params():,} triadic params")
        if training_steps > 0:
            print(f"  Training: {training_steps:,} steps completed")
        print(f"{'-' * 60}")

        for name, check in checks.items():
            status = "PASS" if check['pass'] else "FAIL"
            print(f"  [{status}] {name}: {check['detail']}")

        # Per-group breakdown
        print("\n  PER-GROUP BREAKDOWN:")
        for gname, gd in group_details.items():
            status = "PASS" if gd['pass'] else "FAIL"
            print(f"    [{status}] {gname}: intra {gd['intra_sim']:.0%} vs inter {gd['inter_sim']:.0%} "
                  f"(gap {gd['gap']:+.1%}) — {gd['words']}")

        # Per-word active bits
        print("\n  PER-WORD ACTIVE BITS:")
        for w in all_words:
            print(f"    {w:>12}: {sigs[w]['n_active']:2d}/{self.n_bits} bits active")

        print(f"{'-' * 60}")
        if overall:
            print("  RESULT: PASS — Triadic head is producing meaningful signatures.")
            print(f"  Signal above random: {signal_above_random:+.1%}")
        else:
            failed = [n for n, c in checks.items() if not c['pass']]
            print(f"  RESULT: FAIL — Issues detected: {', '.join(failed)}")
            if 'diversity' in failed:
                print("    -> Signatures are too similar. Train longer or increase entropy_weight.")
            if 'active_bits' in failed:
                if active_frac <= 0.15:
                    print("    -> Too few bits active. Increase entropy_weight to activate dead bits.")
                else:
                    print("    -> Too many bits active. Decrease alpha or increase training steps.")
            if 'semantic_ordering' in failed:
                print("    -> Related words aren't more similar than unrelated.")
                print("       Try: increase align_weight, or use align_mode='infonce' for pre-trained models.")
            if 'random_baseline' in failed:
                print("    -> Gap is not significantly above random chance.")
                print("       The model may be producing hash-like signatures without real semantics.")
                print("       This usually means MORE TRAINING is needed (see guidance below).")
            failing_groups = [g for g, d in group_details.items() if not d['pass']]
            if failing_groups:
                print(f"    -> Weak groups: {', '.join(failing_groups)} — consider adding more training data for these domains.")

        # Always show training guidance
        if guidance:
            print("\n  TRAINING GUIDANCE:")
            for line in guidance:
                print(f"    {line}")

        print(f"{'=' * 60}")

    return {
        'checks': checks,
        'overall_pass': overall,
        'group_details': group_details,
        'random_baseline': baseline_info,
        'guidance': guidance,
        'config': self.config(),
        'signatures': sigs,
    }

explore(words: List[str], tokenizer=None, top_k: int = 0, threshold: Optional[float] = None, show_factors: bool = False, verbose: bool = True) -> Dict

Full audit of relationships between words with similarity matrix.

Parameters:

Name Type Description Default
words List[str]

List of words/phrases to compare.

required
tokenizer

HuggingFace tokenizer (auto-loaded if None).

None
top_k int

Show only top-K and bottom-K pairs (0 = show ALL pairs).

0
threshold Optional[float]

If set, flag pairs above this similarity for review.

None
show_factors bool

Show shared/unique prime factors for every pair.

False
verbose bool

Print results to console.

True

Returns:

Type Description
Dict

Dict with similarity matrix, ranked pairs, signatures, and flagged pairs.

Source code in triadic-head-src/triadic-head/triadic_head/wrapper.py
 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
@torch.no_grad()
def explore(
    self,
    words: List[str],
    tokenizer=None,
    top_k: int = 0,
    threshold: Optional[float] = None,
    show_factors: bool = False,
    verbose: bool = True,
) -> Dict:
    """
    Full audit of relationships between words with similarity matrix.

    Args:
        words: List of words/phrases to compare.
        tokenizer: HuggingFace tokenizer (auto-loaded if None).
        top_k: Show only top-K and bottom-K pairs (0 = show ALL pairs).
        threshold: If set, flag pairs above this similarity for review.
        show_factors: Show shared/unique prime factors for every pair.
        verbose: Print results to console.

    Returns:
        Dict with similarity matrix, ranked pairs, signatures, and flagged pairs.
    """
    from .algebra import TriadicValidator as TV

    sigs = self.encode(words, tokenizer=tokenizer)
    n = len(words)

    # Build similarity matrix
    matrix = [[0.0] * n for _ in range(n)]
    pairs = []

    for i in range(n):
        matrix[i][i] = 1.0
        for j in range(i + 1, n):
            sim = TV.similarity(
                sigs[words[i]]['composite'],
                sigs[words[j]]['composite'],
            )
            gap_info = TV.explain_gap(
                sigs[words[i]]['composite'],
                sigs[words[j]]['composite'],
            )
            matrix[i][j] = sim
            matrix[j][i] = sim
            pairs.append({
                'similarity': sim,
                'word_a': words[i],
                'word_b': words[j],
                'shared_factors': gap_info['shared_factors'],
                'only_a_factors': gap_info['only_in_a_factors'],
                'only_b_factors': gap_info['only_in_b_factors'],
                'n_shared': len(gap_info['shared_factors']),
                'n_only_a': len(gap_info['only_in_a_factors']),
                'n_only_b': len(gap_info['only_in_b_factors']),
            })

    pairs.sort(key=lambda p: p['similarity'], reverse=True)

    # Flag pairs above threshold
    flagged = []
    if threshold is not None:
        flagged = [p for p in pairs if p['similarity'] >= threshold]

    if verbose:
        col_w = min(max(max(len(w) for w in words), 6), 10)

        print(f"\n{'=' * 60}")
        print("  SIMILARITY MATRIX")
        print(f"{'=' * 60}")

        # Header
        header = " " * (col_w + 2) + "".join(f"{w[:col_w]:>{col_w + 1}}" for w in words)
        print(header)

        for i, w in enumerate(words):
            row_vals = []
            for j in range(n):
                if i == j:
                    row_vals.append(f"{'---':>{col_w + 1}}")
                else:
                    row_vals.append(f"{matrix[i][j]:>{col_w}.0%} ")
            print(f"{w[:col_w]:>{col_w}}  {''.join(row_vals)}")

        # Pairs listing
        if top_k > 0 and len(pairs) > top_k * 2:
            print(f"\n  TOP {top_k} most similar:")
            for p in pairs[:top_k]:
                line = f"    {p['word_a']} <-> {p['word_b']}: {p['similarity']:.0%} ({p['n_shared']} shared)"
                if show_factors:
                    line += f" shared={p['shared_factors']}"
                print(line)
            print(f"\n  TOP {top_k} least similar:")
            for p in pairs[-top_k:]:
                line = f"    {p['word_a']} <-> {p['word_b']}: {p['similarity']:.0%}"
                print(line)
        else:
            print("\n  ALL PAIRS (ranked by similarity):")
            for p in pairs:
                line = f"    {p['word_a']:>12} <-> {p['word_b']:<12}: {p['similarity']:.0%} ({p['n_shared']} shared, {p['n_only_a']} only-a, {p['n_only_b']} only-b)"
                if show_factors:
                    line += f"\n{'':>42}shared={p['shared_factors']}"
                    if p['only_a_factors']:
                        line += f"\n{'':>42}only-{p['word_a']}={p['only_a_factors']}"
                    if p['only_b_factors']:
                        line += f"\n{'':>42}only-{p['word_b']}={p['only_b_factors']}"
                print(line)

        # Flagged pairs
        if threshold is not None:
            print(f"\n  FLAGGED (similarity >= {threshold:.0%}): {len(flagged)} pairs")
            if flagged:
                for p in flagged:
                    print(f"    {p['word_a']} <-> {p['word_b']}: {p['similarity']:.0%}")
            else:
                print("    (none)")

        # Per-word signature card
        print("\n  PER-WORD SIGNATURES:")
        for i, w in enumerate(words):
            sims_for_w = [matrix[i][j] for j in range(n) if i != j]
            avg = sum(sims_for_w) / len(sims_for_w)
            most_sim_j = max((j for j in range(n) if j != i), key=lambda j: matrix[i][j])
            least_sim_j = min((j for j in range(n) if j != i), key=lambda j: matrix[i][j])
            primes = sigs[w]['factors']
            print(f"    {w:>12}: {sigs[w]['n_active']} bits | avg sim {avg:.0%} "
                  f"| closest: {words[most_sim_j]} ({matrix[i][most_sim_j]:.0%}) "
                  f"| furthest: {words[least_sim_j]} ({matrix[i][least_sim_j]:.0%})")
            if show_factors:
                print(f"{'':>16}primes={primes}")

        # Factor index: which words share each prime?
        if show_factors:
            from .algebra import nth_prime as _nth_prime
            factor_index = {}  # prime -> list of words
            for w in words:
                for p in sigs[w]['factors']:
                    factor_index.setdefault(p, []).append(w)

            # Sort by number of words sharing the factor (most shared first)
            sorted_factors = sorted(factor_index.items(),
                                    key=lambda x: len(x[1]), reverse=True)

            print("\n  FACTOR INDEX (what each prime means):")
            print(f"    {'prime':>8}  {'bit':>3}  {'words':>5}  shared by")
            print(f"    {'-'*8}  {'-'*3}  {'-'*5}  {'-'*30}")
            for prime, sharing_words in sorted_factors:
                # Find bit index for this prime
                bit_idx = next(
                    (i for i in range(self.n_bits) if _nth_prime(i + 1) == prime),
                    -1
                )
                count = len(sharing_words)
                label = ', '.join(sharing_words)
                if count == len(words):
                    label += '  [UNIVERSAL]'
                elif count == 1:
                    label += '  [UNIQUE]'
                print(f"    {prime:>8}  {bit_idx:>3}  {count:>5}  {label}")

        print(f"{'=' * 60}")

    return {
        'matrix': matrix,
        'words': words,
        'pairs_ranked': pairs,
        'signatures': sigs,
        'flagged': flagged,
    }

generate(input_ids, max_new_tokens=100, temperature=0.7, top_k=50)

Autoregressive text generation.

Source code in triadic-head-src/triadic-head/triadic_head/wrapper.py
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
@torch.no_grad()
def generate(self, input_ids, max_new_tokens=100, temperature=0.7, top_k=50):
    """Autoregressive text generation."""
    for _ in range(max_new_tokens):
        idx = input_ids[:, -self.block_size:]
        logits, _, _ = self(idx)
        logits = logits[:, -1, :] / temperature
        if top_k is not None:
            v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
            logits[logits < v[:, [-1]]] = float('-inf')
        probs = F.softmax(logits, dim=-1)
        next_id = torch.multinomial(probs, num_samples=1)
        input_ids = torch.cat([input_ids, next_id], dim=1)
    return input_ids

triadic_params() -> int

Number of parameters in the triadic head only.

Source code in triadic-head-src/triadic-head/triadic_head/wrapper.py
1043
1044
1045
def triadic_params(self) -> int:
    """Number of parameters in the triadic head only."""
    return sum(p.numel() for p in self.triadic_head.parameters())