Upload folder using huggingface_hub
Browse files- README.md +16 -0
- config.json +9 -0
- create_safetensors.py +55 -0
- model.safetensors +3 -0
README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# threshold-fp-normalize
|
| 11 |
+
|
| 12 |
+
fp-normalize threshold logic implementation.
|
| 13 |
+
|
| 14 |
+
## License
|
| 15 |
+
|
| 16 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-fp-normalize",
|
| 3 |
+
"description": "fp-normalize circuit",
|
| 4 |
+
"inputs": 8,
|
| 5 |
+
"outputs": 8,
|
| 6 |
+
"neurons": 8,
|
| 7 |
+
"layers": 2,
|
| 8 |
+
"parameters": 64
|
| 9 |
+
}
|
create_safetensors.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import save_file
|
| 3 |
+
|
| 4 |
+
weights = {}
|
| 5 |
+
|
| 6 |
+
# Minifloat Normalizer (4-bit mantissa)
|
| 7 |
+
# Shifts mantissa left until MSB is 1, adjusts exponent
|
| 8 |
+
|
| 9 |
+
def add_neuron(name, w_list, bias):
|
| 10 |
+
weights[f'{name}.weight'] = torch.tensor([w_list], dtype=torch.float32)
|
| 11 |
+
weights[f'{name}.bias'] = torch.tensor([bias], dtype=torch.float32)
|
| 12 |
+
|
| 13 |
+
# Input: M3,M2,M1,M0 (mantissa), E2,E1,E0 (exponent)
|
| 14 |
+
# Detect leading zeros
|
| 15 |
+
add_neuron('m3_is1', [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], -1.0) # M3=1, no shift
|
| 16 |
+
add_neuron('m2_lead', [-1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0], 0.0) # M3=0,M2=1, shift 1
|
| 17 |
+
add_neuron('m1_lead', [-1.0, -1.0, 1.0, 0.0, 0.0, 0.0, 0.0], 1.0) # shift 2
|
| 18 |
+
add_neuron('m0_lead', [-1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 0.0], 2.0) # shift 3
|
| 19 |
+
|
| 20 |
+
save_file(weights, 'model.safetensors')
|
| 21 |
+
|
| 22 |
+
def normalize(m3, m2, m1, m0, e2, e1, e0):
|
| 23 |
+
m = m3*8 + m2*4 + m1*2 + m0
|
| 24 |
+
e = e2*4 + e1*2 + e0
|
| 25 |
+
|
| 26 |
+
if m == 0:
|
| 27 |
+
return 0, 0, 0, 0, 0, 0, 0
|
| 28 |
+
|
| 29 |
+
shift = 0
|
| 30 |
+
while (m & 8) == 0 and shift < 4:
|
| 31 |
+
m = (m << 1) & 0xF
|
| 32 |
+
shift += 1
|
| 33 |
+
|
| 34 |
+
e = max(0, e - shift)
|
| 35 |
+
return (m>>3)&1, (m>>2)&1, (m>>1)&1, m&1, (e>>2)&1, (e>>1)&1, e&1
|
| 36 |
+
|
| 37 |
+
print("Verifying FP normalize...")
|
| 38 |
+
errors = 0
|
| 39 |
+
for m in range(16):
|
| 40 |
+
for e in range(8):
|
| 41 |
+
m3, m2, m1, m0 = (m>>3)&1, (m>>2)&1, (m>>1)&1, m&1
|
| 42 |
+
e2, e1, e0 = (e>>2)&1, (e>>1)&1, e&1
|
| 43 |
+
result = normalize(m3, m2, m1, m0, e2, e1, e0)
|
| 44 |
+
# Verify MSB is 1 (or value is 0)
|
| 45 |
+
if m != 0 and result[0] != 1:
|
| 46 |
+
errors += 1
|
| 47 |
+
|
| 48 |
+
if errors == 0:
|
| 49 |
+
print("All test cases passed!")
|
| 50 |
+
else:
|
| 51 |
+
print(f"FAILED: {errors} errors")
|
| 52 |
+
|
| 53 |
+
mag = sum(t.abs().sum().item() for t in weights.values())
|
| 54 |
+
print(f"Magnitude: {mag:.0f}")
|
| 55 |
+
print(f"Parameters: {sum(t.numel() for t in weights.values())}")
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:42e79c802b7a472c1f36b1598d209bf2e5398d14a488b633da7642dfb1ebe170
|
| 3 |
+
size 680
|