Upload folder using huggingface_hub
Browse files- README.md +30 -0
- config.json +9 -0
- create_safetensors.py +57 -0
- model.safetensors +3 -0
README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
- sorting
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# threshold-bitonic-sort
|
| 12 |
+
|
| 13 |
+
4-element bitonic sorting network. Sorts single-bit elements in ascending order.
|
| 14 |
+
|
| 15 |
+
## Circuit
|
| 16 |
+
|
| 17 |
+
Outputs sorted bits: Y0 ≤ Y1 ≤ Y2 ≤ Y3
|
| 18 |
+
|
| 19 |
+
## Parameters
|
| 20 |
+
|
| 21 |
+
| | |
|
| 22 |
+
|---|---|
|
| 23 |
+
| Elements | 4 |
|
| 24 |
+
| Neurons | 4 |
|
| 25 |
+
| Parameters | 20 |
|
| 26 |
+
| Magnitude | 26 |
|
| 27 |
+
|
| 28 |
+
## License
|
| 29 |
+
|
| 30 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-bitonic-sort",
|
| 3 |
+
"description": "4-element bitonic sort network",
|
| 4 |
+
"inputs": 4,
|
| 5 |
+
"outputs": 4,
|
| 6 |
+
"neurons": 4,
|
| 7 |
+
"layers": 1,
|
| 8 |
+
"parameters": 20
|
| 9 |
+
}
|
create_safetensors.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import save_file
|
| 3 |
+
|
| 4 |
+
weights = {}
|
| 5 |
+
|
| 6 |
+
# 4-element Bitonic Sort Network (single-bit elements)
|
| 7 |
+
# Sorts 4 bits into ascending order
|
| 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 |
+
# For single-bit sort, output Y[i] = 1 iff at least (i+1) input bits are 1
|
| 14 |
+
# Y0 (min) = 1 iff all 4 inputs are 1 = AND
|
| 15 |
+
# Y1 = 1 iff at least 3 inputs are 1
|
| 16 |
+
# Y2 = 1 iff at least 2 inputs are 1
|
| 17 |
+
# Y3 (max) = 1 iff at least 1 input is 1 = OR
|
| 18 |
+
|
| 19 |
+
add_neuron('y0', [1.0, 1.0, 1.0, 1.0], -4.0) # >= 4
|
| 20 |
+
add_neuron('y1', [1.0, 1.0, 1.0, 1.0], -3.0) # >= 3
|
| 21 |
+
add_neuron('y2', [1.0, 1.0, 1.0, 1.0], -2.0) # >= 2
|
| 22 |
+
add_neuron('y3', [1.0, 1.0, 1.0, 1.0], -1.0) # >= 1
|
| 23 |
+
|
| 24 |
+
save_file(weights, 'model.safetensors')
|
| 25 |
+
|
| 26 |
+
def bitonic_sort4(a, b, c, d):
|
| 27 |
+
count = a + b + c + d
|
| 28 |
+
# Sorted output: count 1s at top
|
| 29 |
+
y0 = 1 if count >= 4 else 0
|
| 30 |
+
y1 = 1 if count >= 3 else 0
|
| 31 |
+
y2 = 1 if count >= 2 else 0
|
| 32 |
+
y3 = 1 if count >= 1 else 0
|
| 33 |
+
return y0, y1, y2, y3
|
| 34 |
+
|
| 35 |
+
print("Verifying 4-element bitonic sort (single-bit)...")
|
| 36 |
+
errors = 0
|
| 37 |
+
for val in range(16):
|
| 38 |
+
a, b, c, d = (val>>3)&1, (val>>2)&1, (val>>1)&1, val&1
|
| 39 |
+
y0, y1, y2, y3 = bitonic_sort4(a, b, c, d)
|
| 40 |
+
|
| 41 |
+
# Verify sorted order
|
| 42 |
+
if not (y0 <= y1 <= y2 <= y3):
|
| 43 |
+
errors += 1
|
| 44 |
+
print(f"ERROR: not sorted")
|
| 45 |
+
|
| 46 |
+
# Verify count preserved
|
| 47 |
+
if sum([a, b, c, d]) != sum([y0, y1, y2, y3]):
|
| 48 |
+
errors += 1
|
| 49 |
+
|
| 50 |
+
if errors == 0:
|
| 51 |
+
print("All 16 test cases passed!")
|
| 52 |
+
else:
|
| 53 |
+
print(f"FAILED: {errors} errors")
|
| 54 |
+
|
| 55 |
+
mag = sum(t.abs().sum().item() for t in weights.values())
|
| 56 |
+
print(f"Magnitude: {mag:.0f}")
|
| 57 |
+
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:cc9a06c0ab80741d20f98131f1421546b1352b8a4db3e7d74d9128b58eee7679
|
| 3 |
+
size 592
|