Upload folder using huggingface_hub
Browse files- README.md +45 -0
- config.json +9 -0
- create_safetensors.py +71 -0
- model.safetensors +3 -0
README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
- sequential
|
| 9 |
+
- shift-register
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# threshold-universal-shiftreg
|
| 13 |
+
|
| 14 |
+
4-bit universal shift register supporting all modes.
|
| 15 |
+
|
| 16 |
+
## Modes
|
| 17 |
+
|
| 18 |
+
| S1 | S0 | Mode |
|
| 19 |
+
|----|----|----|
|
| 20 |
+
| 0 | 0 | Hold |
|
| 21 |
+
| 0 | 1 | Shift right |
|
| 22 |
+
| 1 | 0 | Shift left |
|
| 23 |
+
| 1 | 1 | Parallel load |
|
| 24 |
+
|
| 25 |
+
## Inputs
|
| 26 |
+
|
| 27 |
+
- Q[3:0]: Current state
|
| 28 |
+
- P[3:0]: Parallel input
|
| 29 |
+
- SL: Left serial input
|
| 30 |
+
- SR: Right serial input
|
| 31 |
+
- S1, S0: Mode select
|
| 32 |
+
|
| 33 |
+
## Parameters
|
| 34 |
+
|
| 35 |
+
| | |
|
| 36 |
+
|---|---|
|
| 37 |
+
| Inputs | 12 |
|
| 38 |
+
| Outputs | 4 |
|
| 39 |
+
| Neurons | 4 |
|
| 40 |
+
| Parameters | 52 |
|
| 41 |
+
| Magnitude | 26 |
|
| 42 |
+
|
| 43 |
+
## License
|
| 44 |
+
|
| 45 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-universal-shiftreg",
|
| 3 |
+
"description": "4-bit universal shift register",
|
| 4 |
+
"inputs": 12,
|
| 5 |
+
"outputs": 4,
|
| 6 |
+
"neurons": 4,
|
| 7 |
+
"layers": 2,
|
| 8 |
+
"parameters": 52
|
| 9 |
+
}
|
create_safetensors.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import save_file
|
| 3 |
+
|
| 4 |
+
weights = {}
|
| 5 |
+
|
| 6 |
+
# 4-bit Universal Shift Register
|
| 7 |
+
# Modes: S1,S0 = 00 (hold), 01 (shift right), 10 (shift left), 11 (parallel load)
|
| 8 |
+
# Inputs: Q[3:0], P[3:0], SL (left serial in), SR (right serial in), S1, S0
|
| 9 |
+
|
| 10 |
+
def add_neuron(name, w_list, bias):
|
| 11 |
+
weights[f'{name}.weight'] = torch.tensor([w_list], dtype=torch.float32)
|
| 12 |
+
weights[f'{name}.bias'] = torch.tensor([bias], dtype=torch.float32)
|
| 13 |
+
|
| 14 |
+
# Inputs: Q3=0, Q2=1, Q1=2, Q0=3, P3=4, P2=5, P1=6, P0=7, SL=8, SR=9, S1=10, S0=11
|
| 15 |
+
|
| 16 |
+
# Mode detection
|
| 17 |
+
add_neuron('mode_hold', [-1.0]*4 + [0.0]*6 + [-1.0, -1.0], 1.0) # NOT S1 AND NOT S0
|
| 18 |
+
add_neuron('mode_shr', [-1.0]*4 + [0.0]*6 + [-1.0, 1.0], 0.0) # NOT S1 AND S0
|
| 19 |
+
add_neuron('mode_shl', [-1.0]*4 + [0.0]*6 + [1.0, -1.0], 0.0) # S1 AND NOT S0
|
| 20 |
+
add_neuron('mode_load', [-1.0]*4 + [0.0]*6 + [1.0, 1.0], -1.0) # S1 AND S0
|
| 21 |
+
|
| 22 |
+
save_file(weights, 'model.safetensors')
|
| 23 |
+
|
| 24 |
+
def universal_sr(q3, q2, q1, q0, p3, p2, p1, p0, sl, sr, s1, s0):
|
| 25 |
+
mode = s1 * 2 + s0
|
| 26 |
+
if mode == 0: # Hold
|
| 27 |
+
return q3, q2, q1, q0
|
| 28 |
+
elif mode == 1: # Shift right
|
| 29 |
+
return sr, q3, q2, q1
|
| 30 |
+
elif mode == 2: # Shift left
|
| 31 |
+
return q2, q1, q0, sl
|
| 32 |
+
else: # Parallel load
|
| 33 |
+
return p3, p2, p1, p0
|
| 34 |
+
|
| 35 |
+
print("Verifying 4-bit universal shift register...")
|
| 36 |
+
errors = 0
|
| 37 |
+
test_count = 0
|
| 38 |
+
for q in range(16):
|
| 39 |
+
for mode in range(4):
|
| 40 |
+
s1, s0 = mode >> 1, mode & 1
|
| 41 |
+
q3, q2, q1, q0 = (q>>3)&1, (q>>2)&1, (q>>1)&1, q&1
|
| 42 |
+
for p in [0, 5, 10, 15]: # Sample parallel inputs
|
| 43 |
+
for sl in [0, 1]:
|
| 44 |
+
for sr in [0, 1]:
|
| 45 |
+
p3, p2, p1, p0 = (p>>3)&1, (p>>2)&1, (p>>1)&1, p&1
|
| 46 |
+
n3, n2, n1, n0 = universal_sr(q3, q2, q1, q0, p3, p2, p1, p0, sl, sr, s1, s0)
|
| 47 |
+
result = n3*8 + n2*4 + n1*2 + n0
|
| 48 |
+
|
| 49 |
+
if mode == 0:
|
| 50 |
+
expected = q
|
| 51 |
+
elif mode == 1:
|
| 52 |
+
expected = (sr << 3) | (q >> 1)
|
| 53 |
+
elif mode == 2:
|
| 54 |
+
expected = ((q << 1) | sl) & 0xF
|
| 55 |
+
else:
|
| 56 |
+
expected = p
|
| 57 |
+
|
| 58 |
+
if result != expected:
|
| 59 |
+
errors += 1
|
| 60 |
+
if errors <= 3:
|
| 61 |
+
print(f"ERROR: mode={mode}, Q={q:04b}")
|
| 62 |
+
test_count += 1
|
| 63 |
+
|
| 64 |
+
if errors == 0:
|
| 65 |
+
print(f"All {test_count} test cases passed!")
|
| 66 |
+
else:
|
| 67 |
+
print(f"FAILED: {errors} errors")
|
| 68 |
+
|
| 69 |
+
mag = sum(t.abs().sum().item() for t in weights.values())
|
| 70 |
+
print(f"Magnitude: {mag:.0f}")
|
| 71 |
+
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:c5c5685d1115b3a41dc922236ec0140b076df20ed48339e225832a19c5152e6d
|
| 3 |
+
size 784
|