Upload folder using huggingface_hub
Browse files- README.md +130 -0
- config.json +9 -0
- model.py +39 -0
- model.safetensors +3 -0
README.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
- decoder
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# threshold-thermometertobinary
|
| 12 |
+
|
| 13 |
+
Converts 7-bit thermometer code to 3-bit binary. The inverse of BinaryToThermometer.
|
| 14 |
+
|
| 15 |
+
## Circuit
|
| 16 |
+
|
| 17 |
+
```
|
| 18 |
+
t₀ t₁ t₂ t₃ t₄ t₅ t₆
|
| 19 |
+
│ │ │ │ │ │ │
|
| 20 |
+
│ │ │ │ │ │ │
|
| 21 |
+
├───┴───┴───┴───┴───┴───┘
|
| 22 |
+
│ │ │ │
|
| 23 |
+
│ ├───────┼───────┼──────────► b₂ (direct from t₃)
|
| 24 |
+
│ │ │ │
|
| 25 |
+
│ │ ┌───┴───┐ │
|
| 26 |
+
│ │ │t₁∧¬t₃ │ │
|
| 27 |
+
│ │ └───┬───┘ │
|
| 28 |
+
│ │ │ │
|
| 29 |
+
│ │ ├───OR──┴──────────► b₁
|
| 30 |
+
│ │ │ │
|
| 31 |
+
│ │ │ t₅
|
| 32 |
+
│ │ │
|
| 33 |
+
├───┴───┐ ┌─┴─┐ ┌───┐
|
| 34 |
+
│t₀∧¬t₁│ │...│ │t₆ │
|
| 35 |
+
└───┬───┘ └─┬─┘ └─┬─┘
|
| 36 |
+
│ │ │
|
| 37 |
+
└───────┴─OR──┴────────────► b₀
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
## Conversion Table
|
| 41 |
+
|
| 42 |
+
| Thermometer | Value | Binary |
|
| 43 |
+
|-------------|-------|--------|
|
| 44 |
+
| 0000000 | 0 | 000 |
|
| 45 |
+
| 1000000 | 1 | 001 |
|
| 46 |
+
| 1100000 | 2 | 010 |
|
| 47 |
+
| 1110000 | 3 | 011 |
|
| 48 |
+
| 1111000 | 4 | 100 |
|
| 49 |
+
| 1111100 | 5 | 101 |
|
| 50 |
+
| 1111110 | 6 | 110 |
|
| 51 |
+
| 1111111 | 7 | 111 |
|
| 52 |
+
|
| 53 |
+
## Mechanism
|
| 54 |
+
|
| 55 |
+
For valid thermometer (monotonic ones then zeros), tᵢ = 1 iff value > i.
|
| 56 |
+
|
| 57 |
+
**b₂ (bit 2)**: Directly equals t₃.
|
| 58 |
+
- t₃ = 1 iff value ≥ 4, which is exactly when b₂ = 1
|
| 59 |
+
|
| 60 |
+
**b₁ (bit 1)**: Fires for values {2, 3, 6, 7}.
|
| 61 |
+
```
|
| 62 |
+
b₁ = (t₁ AND NOT(t₃)) OR t₅
|
| 63 |
+
```
|
| 64 |
+
- t₁ AND NOT(t₃) catches values 2, 3 (≥2 but <4)
|
| 65 |
+
- t₅ catches values 6, 7 (≥6)
|
| 66 |
+
|
| 67 |
+
**b₀ (bit 0)**: Fires for odd values {1, 3, 5, 7}.
|
| 68 |
+
```
|
| 69 |
+
b₀ = (t₀ AND NOT(t₁)) OR (t₂ AND NOT(t₃)) OR (t₄ AND NOT(t₅)) OR t₆
|
| 70 |
+
```
|
| 71 |
+
Each AND-NOT term detects a transition from 1 to 0 at an odd position.
|
| 72 |
+
|
| 73 |
+
## The Transition Detection Insight
|
| 74 |
+
|
| 75 |
+
In valid thermometer code, the value equals the position of the last 1. The formula detects "where does the thermometer stop?"
|
| 76 |
+
|
| 77 |
+
| Value | Last 1 at | Detected by |
|
| 78 |
+
|-------|-----------|-------------|
|
| 79 |
+
| 1 | t₀ | t₀ AND NOT(t₁) |
|
| 80 |
+
| 3 | t₂ | t₂ AND NOT(t₃) |
|
| 81 |
+
| 5 | t₄ | t₄ AND NOT(t₅) |
|
| 82 |
+
| 7 | t₆ | t₆ (no t₇ to check) |
|
| 83 |
+
|
| 84 |
+
## Architecture
|
| 85 |
+
|
| 86 |
+
| Output | Neurons | Parameters |
|
| 87 |
+
|--------|---------|------------|
|
| 88 |
+
| b₂ | 1 | 8 |
|
| 89 |
+
| b₁ | 2 | 11 |
|
| 90 |
+
| b₀ | 4 | 29 |
|
| 91 |
+
| **Total** | **7** | **48** |
|
| 92 |
+
|
| 93 |
+
**Layers: 2**
|
| 94 |
+
|
| 95 |
+
## Asymmetry Note
|
| 96 |
+
|
| 97 |
+
BinaryToThermometer is single-layer (7 parallel thresholds). ThermometerToBinary requires 2 layers because extracting individual bits from a sum needs non-trivial logic.
|
| 98 |
+
|
| 99 |
+
## Usage
|
| 100 |
+
|
| 101 |
+
```python
|
| 102 |
+
from safetensors.torch import load_file
|
| 103 |
+
import torch
|
| 104 |
+
|
| 105 |
+
w = load_file('model.safetensors')
|
| 106 |
+
|
| 107 |
+
def therm_to_binary(therm):
|
| 108 |
+
"""therm: 7-element list"""
|
| 109 |
+
# See model.py for full implementation
|
| 110 |
+
pass
|
| 111 |
+
|
| 112 |
+
# Thermometer for 5 -> binary 101
|
| 113 |
+
therm = [1, 1, 1, 1, 1, 0, 0]
|
| 114 |
+
b2, b1, b0 = therm_to_binary(therm)
|
| 115 |
+
print(b2, b1, b0) # 1, 0, 1
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
## Files
|
| 119 |
+
|
| 120 |
+
```
|
| 121 |
+
threshold-thermometertobinary/
|
| 122 |
+
├── model.safetensors
|
| 123 |
+
├── model.py
|
| 124 |
+
├── config.json
|
| 125 |
+
└── README.md
|
| 126 |
+
```
|
| 127 |
+
|
| 128 |
+
## License
|
| 129 |
+
|
| 130 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-thermometertobinary",
|
| 3 |
+
"description": "7-bit thermometer to 3-bit binary converter",
|
| 4 |
+
"inputs": 7,
|
| 5 |
+
"outputs": 3,
|
| 6 |
+
"neurons": 7,
|
| 7 |
+
"layers": 2,
|
| 8 |
+
"parameters": 48
|
| 9 |
+
}
|
model.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import load_file
|
| 3 |
+
|
| 4 |
+
def load_model(path='model.safetensors'):
|
| 5 |
+
return load_file(path)
|
| 6 |
+
|
| 7 |
+
def thermometer_to_binary(therm, weights):
|
| 8 |
+
"""Convert 7-bit thermometer to 3-bit binary.
|
| 9 |
+
Returns (b2, b1, b0).
|
| 10 |
+
"""
|
| 11 |
+
t = torch.tensor([float(x) for x in therm])
|
| 12 |
+
|
| 13 |
+
# b2 = t3 (direct)
|
| 14 |
+
b2 = int((t * weights['b2.weight']).sum() + weights['b2.bias'] >= 0)
|
| 15 |
+
|
| 16 |
+
# b1 = (t1 AND NOT(t3)) OR t5
|
| 17 |
+
and_result = int((t * weights['b1_and.weight']).sum() + weights['b1_and.bias'] >= 0)
|
| 18 |
+
b1_inputs = torch.tensor([float(and_result), float(therm[5])])
|
| 19 |
+
b1 = int((b1_inputs * weights['b1_or.weight']).sum() + weights['b1_or.bias'] >= 0)
|
| 20 |
+
|
| 21 |
+
# b0 = (t0 AND NOT(t1)) OR (t2 AND NOT(t3)) OR (t4 AND NOT(t5)) OR t6
|
| 22 |
+
and0 = int((t * weights['b0_and0.weight']).sum() + weights['b0_and0.bias'] >= 0)
|
| 23 |
+
and2 = int((t * weights['b0_and2.weight']).sum() + weights['b0_and2.bias'] >= 0)
|
| 24 |
+
and4 = int((t * weights['b0_and4.weight']).sum() + weights['b0_and4.bias'] >= 0)
|
| 25 |
+
b0_inputs = torch.tensor([float(and0), float(and2), float(and4), float(therm[6])])
|
| 26 |
+
b0 = int((b0_inputs * weights['b0_or.weight']).sum() + weights['b0_or.bias'] >= 0)
|
| 27 |
+
|
| 28 |
+
return b2, b1, b0
|
| 29 |
+
|
| 30 |
+
if __name__ == '__main__':
|
| 31 |
+
w = load_model()
|
| 32 |
+
print('Thermometer to Binary Converter')
|
| 33 |
+
thermometers = [
|
| 34 |
+
[0,0,0,0,0,0,0], [1,0,0,0,0,0,0], [1,1,0,0,0,0,0], [1,1,1,0,0,0,0],
|
| 35 |
+
[1,1,1,1,0,0,0], [1,1,1,1,1,0,0], [1,1,1,1,1,1,0], [1,1,1,1,1,1,1],
|
| 36 |
+
]
|
| 37 |
+
for therm in thermometers:
|
| 38 |
+
b2, b1, b0 = thermometer_to_binary(therm, w)
|
| 39 |
+
print(f"{''.join(map(str,therm))} -> {b2*4 + b1*2 + b0}")
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:87a8ceb7cf9e8bd69188e85f5efb3504d21f478f98b60224a667cdf6618b4458
|
| 3 |
+
size 1136
|