File size: 1,729 Bytes
f4de00e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
---

# threshold-comparator4bit

4-bit magnitude comparator. Compares two 4-bit unsigned integers.

## Function

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

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

## Inputs

| Input | Description |
|-------|-------------|
| A3-A0 | 4-bit number A (A3 is MSB) |
| B3-B0 | 4-bit number B (B3 is MSB) |

## Architecture

```
A3 A2 A1 A0 B3 B2 B1 B0
 |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+
 |                    |
 v                    v
[GT: A-B >= 1]    [LT: B-A >= 1]
     |                 |
     +--------+--------+
              |
              v
         [EQ: NOR(GT,LT)]
```

Layer 1:
- GT: weights [8, 4, 2, 1, -8, -4, -2, -1], bias -1
- LT: weights [-8, -4, -2, -1, 8, 4, 2, 1], bias -1

Layer 2:
- EQ: weights [-1, -1], bias 0 (NOR gate)

## Parameters

| | |
|---|---|
| Inputs | 8 |
| Outputs | 3 (GT, LT, EQ) |
| Neurons | 3 |
| Layers | 2 |
| Parameters | 21 |
| Magnitude | 64 |

## Usage

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

w = load_file('model.safetensors')

def compare4(a3, a2, a1, a0, b3, b2, b1, b0):
    inp = torch.tensor([float(a3), float(a2), float(a1), float(a0),
                        float(b3), float(b2), float(b1), float(b0)])
    gt = int((inp @ w['gt.weight'].T + w['gt.bias'] >= 0).item())
    lt = int((inp @ w['lt.weight'].T + w['lt.bias'] >= 0).item())
    gt_lt = torch.tensor([float(gt), float(lt)])
    eq = int((gt_lt @ w['eq.weight'].T + w['eq.bias'] >= 0).item())
    return gt, lt, eq

# Compare 5 vs 3
gt, lt, eq = compare4(0, 1, 0, 1, 0, 0, 1, 1)
print(f"5 vs 3: GT={gt}, LT={lt}, EQ={eq}")  # GT=1, LT=0, EQ=0
```

## License

MIT