File size: 3,771 Bytes
2198727
 
 
 
 
 
 
 
 
 
 
 
370ec48
2198727
 
 
 
370ec48
 
 
 
 
 
 
 
 
 
 
2198727
 
370ec48
2198727
 
 
 
 
 
 
 
370ec48
2198727
 
 
 
 
 
 
 
370ec48
 
 
 
 
2198727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
370ec48
2198727
370ec48
 
2198727
370ec48
2198727
 
 
 
 
 
 
370ec48
2198727
 
 
 
 
 
 
 
 
370ec48
2198727
 
 
 
370ec48
 
 
2198727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
---

license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
- comparison
---


# threshold-lessthan

8-bit less-than comparator. Returns 1 if a < b, 0 otherwise.

## Circuit

```

a0 b0    a1 b1    a2 b2    a3 b3    a4 b4    a5 b5    a6 b6    a7 b7

 β”‚ β”‚ 0    β”‚ β”‚      β”‚ β”‚      β”‚ β”‚      β”‚ β”‚      β”‚ β”‚      β”‚ β”‚      β”‚ β”‚

 β””β”¬β”˜β”‚     β””β”¬β”˜      β””β”¬β”˜      β””β”¬β”˜      β””β”¬β”˜      β””β”¬β”˜      β””β”¬β”˜      β””β”¬β”˜

  β–Ό β”‚      β–Ό        β–Ό        β–Ό        β–Ό        β–Ό        β–Ό        β–Ό

β”Œβ”€β”€β”€β”β”‚  β”Œβ”€β”€β”€β”    β”Œβ”€β”€β”€β”    β”Œβ”€β”€β”€β”    β”Œβ”€β”€β”€β”    β”Œβ”€β”€β”€β”    β”Œβ”€β”€β”€β”    β”Œβ”€β”€β”€β”

β”‚FS0│┴──│FS1│────│FS2│────│FS3│────│FS4│────│FS5│────│FS6│────│FS7│──►(a<b)

β””β”€β”€β”€β”˜   β””β”€β”€β”€β”˜    β””β”€β”€β”€β”˜    β””β”€β”€β”€β”˜    β””β”€β”€β”€β”˜    β””β”€β”€β”€β”˜    β””β”€β”€β”€β”˜    β””β”€β”€β”€β”˜

  β”‚       β”‚        β”‚        β”‚        β”‚        β”‚        β”‚        β”‚

  β–Ό       β–Ό        β–Ό        β–Ό        β–Ό        β–Ό        β–Ό        β–Ό

 d0      d1       d2       d3       d4       d5       d6       d7

      (difference bits unused - only final borrow matters)

```

Uses 8 cascaded full subtractors. The final borrow output indicates a < b.

## Mechanism

The circuit computes a - b using subtraction:

- If a β‰₯ b: no borrow propagates, result is non-negative
- If a < b: borrow propagates out, indicating underflow

The difference bits (d0-d7) are computed but unused. Only the final borrow matters.

## Truth Table (sample)

| a | b | a < b |
|---|---|-------|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 0 |
| 127 | 128 | 1 |
| 255 | 0 | 0 |
| 0 | 255 | 1 |
| 100 | 100 | 0 |
| 99 | 100 | 1 |

## Full Subtractor Structure

```

        a       b

        β”‚       β”‚

        β””β”€β”€β”€β”¬β”€β”€β”€β”˜

            β–Ό

       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”

       β”‚   HS1   β”‚

       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

        β”‚       β”‚

       d1       b1

        β”‚        \

        β”‚   bin   \

        β””β”€β”€β”¬β”€β”€β”˜    \

           β–Ό        \

      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”    \

      β”‚   HS2   β”‚     β”‚

      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚

       β”‚       β”‚      β”‚

       d       b2     β”‚

               β”‚      β”‚

               β””β”€β”€β”¬β”€β”€β”€β”˜

                  β–Ό

              β”Œβ”€β”€β”€β”€β”€β”€β”

              β”‚  OR  β”‚

              β””β”€β”€β”€β”€β”€β”€β”˜

                  β”‚

                  β–Ό

                bout

```

## Architecture

| Component | Per FS | Total (8 FSs) |
|-----------|--------|---------------|
| Neurons | 9 | 72 |
| Parameters | 27 | 216 |

**Layers: 32** (8 FSs Γ— 4 layers each)

## Comparison Family

| Circuit | Condition | Implementation |
|---------|-----------|----------------|
| **LessThan** | a < b | borrow_out of (a - b) |

| Equal | a = b | NOR of all XOR bits |

| GreaterThan | a > b | LessThan(b, a) |



## Usage



```python

from safetensors.torch import load_file

w = load_file('model.safetensors')



def less_than(a, b):
    """a, b: 8-bit lists [a0..a7] (LSB first)

    Returns: 1 if a < b, 0 otherwise"""

    # See model.py for full implementation

    pass


# Example: 99 < 100?
a = [(99 >> i) & 1 for i in range(8)]
b = [(100 >> i) & 1 for i in range(8)]
result = less_than(a, b)  # Returns 1

```



## Files



```

threshold-lessthan/

β”œβ”€β”€ model.safetensors

β”œβ”€β”€ model.py

β”œβ”€β”€ config.json

└── README.md

```



## License



MIT