File size: 2,198 Bytes
96d9c09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
---
license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
- comparison
---

# threshold-comparator8bit

8-bit magnitude comparator. Compares two 8-bit unsigned values.

## Function

compare8(A, B) -> (GT, LT, EQ)

- GT = 1 if A > B
- LT = 1 if A < B
- EQ = 1 if A = B

Exactly one output is always active.

## Architecture

```
A[7:0]          B[7:0]
   β”‚               β”‚
   β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”
    β”‚             β”‚
    β–Ό             β–Ό
  β”Œβ”€β”€β”€β”         β”Œβ”€β”€β”€β”
  β”‚GT β”‚         β”‚LT β”‚   Layer 1
  β”‚A-Bβ”‚         β”‚B-Aβ”‚
  β”‚>=1β”‚         β”‚>=1β”‚
  β””β”€β”€β”€β”˜         β””β”€β”€β”€β”˜
    β”‚             β”‚
    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
           β”‚
           β–Ό
        β”Œβ”€β”€β”€β”€β”€β”
        β”‚ EQ  β”‚         Layer 2
        β”‚ NOR β”‚
        β””β”€β”€β”€β”€β”€β”˜
```

Uses positional weighting: treats inputs as binary numbers.

**GT neuron:** weights A bits positively (128,64,32,16,8,4,2,1), B bits negatively.
Fires when weighted sum A - B >= 1.

**LT neuron:** opposite weights. Fires when B - A >= 1.

**EQ neuron:** NOR of GT and LT. Fires when both are zero.

## Parameters

| | |
|---|---|
| Inputs | 16 |
| Outputs | 3 |
| Neurons | 3 |
| Layers | 2 |
| Parameters | 37 |
| Magnitude | 1024 |

## Truth Table (examples)

| A | B | GT | LT | EQ |
|-----|-----|----|----|---|
| 0 | 0 | 0 | 0 | 1 |
| 100 | 50 | 1 | 0 | 0 |
| 50 | 100 | 0 | 1 | 0 |
| 255 | 255 | 0 | 0 | 1 |

## Usage

```python
from safetensors.torch import load_file
import torch

w = load_file('model.safetensors')

def compare(a, b):
    a_bits = [(a >> (7-i)) & 1 for i in range(8)]
    b_bits = [(b >> (7-i)) & 1 for i in range(8)]
    inp = torch.tensor([float(x) for x in a_bits + b_bits])
    gt = int((inp @ w['gt.weight'].T + w['gt.bias'] >= 0).item())
    lt = int((inp @ w['lt.weight'].T + w['lt.bias'] >= 0).item())
    eq = int((torch.tensor([float(gt), float(lt)]) @ w['eq.weight'].T + w['eq.bias'] >= 0).item())
    return gt, lt, eq

# compare(200, 100) = (1, 0, 0)  # 200 > 100
```

## License

MIT