| ---
|
| 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
|
| |