phanerozoic commited on
Commit
185e212
Β·
verified Β·
1 Parent(s): bf721a0

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +31 -32
  2. config.json +4 -4
  3. model.py +8 -8
  4. model.safetensors +2 -2
README.md CHANGED
@@ -10,29 +10,29 @@ tags:
10
 
11
  # threshold-equal
12
 
13
- 4-bit equality comparator. Returns 1 if a = b, 0 otherwise.
14
 
15
  ## Circuit
16
 
17
  ```
18
- a0 b0 a1 b1 a2 b2 a3 b3
19
- β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
20
- β””β”€β”¬β”€β”˜ β””β”€β”¬β”€β”˜ β””β”€β”¬β”€β”˜ β””β”€β”¬β”€β”˜
21
- β–Ό β–Ό β–Ό β–Ό
22
- β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”
23
- β”‚XOR 0β”‚ β”‚XOR 1β”‚ β”‚XOR 2β”‚ β”‚XOR 3β”‚ Layer 1-2
24
- β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜
25
- β”‚ β”‚ β”‚ β”‚
26
- β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
27
- β–Ό
28
- β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
29
- β”‚ NOR β”‚ Layer 3
30
- β”‚ w: -1Γ—4 β”‚
31
- β”‚ b: 0 β”‚
32
- β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
33
- β”‚
34
- β–Ό
35
- (a = b)
36
  ```
37
 
38
  ## Mechanism
@@ -70,20 +70,19 @@ The NOR gate acts as a "zero detector" - it fires when all its inputs are silent
70
  |---|---|-------|
71
  | 0 | 0 | 1 |
72
  | 0 | 1 | 0 |
73
- | 5 | 5 | 1 |
74
- | 5 | 10 | 0 |
75
- | 15 | 15 | 1 |
76
- | 7 | 8 | 0 |
77
-
78
- All 256 input combinations verified correct.
79
 
80
  ## Architecture
81
 
82
  | Component | Neurons | Parameters |
83
  |-----------|---------|------------|
84
- | XOR Γ— 4 | 12 | 36 |
85
- | NOR | 1 | 5 |
86
- | **Total** | **13** | **41** |
87
 
88
  **Layers: 3** (XOR: 2, NOR: 1)
89
 
@@ -103,14 +102,14 @@ from safetensors.torch import load_file
103
  w = load_file('model.safetensors')
104
 
105
  def equal(a, b):
106
- """a, b: 4-bit lists [a0,a1,a2,a3] (LSB first)
107
  Returns: 1 if a == b, 0 otherwise"""
108
  # See model.py for full implementation
109
  pass
110
 
111
- # Example: 7 == 7?
112
- a = [1, 1, 1, 0] # 7 in LSB-first
113
- b = [1, 1, 1, 0] # 7 in LSB-first
114
  result = equal(a, b) # Returns 1
115
  ```
116
 
 
10
 
11
  # threshold-equal
12
 
13
+ 8-bit equality comparator. Returns 1 if a = b, 0 otherwise.
14
 
15
  ## Circuit
16
 
17
  ```
18
+ a0 b0 a1 b1 a2 b2 a3 b3 a4 b4 a5 b5 a6 b6 a7 b7
19
+ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
20
+ β””β”¬β”€β”˜ β””β”¬β”€β”˜ β””β”¬β”€β”˜ β””β”¬β”€β”˜ β””β”¬β”€β”˜ β””β”¬β”€β”˜ β””β”¬β”€β”˜ β””β”¬β”€β”˜
21
+ β–Ό β–Ό β–Ό β–Ό β–Ό β–Ό β–Ό β–Ό
22
+ β”Œβ”€β”€β”€β” β”Œβ”€β”€β”€β” β”Œβ”€β”€β”€β” β”Œβ”€β”€β”€β” β”Œβ”€β”€β”€β” β”Œβ”€β”€β”€β” β”Œβ”€β”€β”€β” β”Œβ”€β”€β”€β”
23
+ β”‚XORβ”‚ β”‚XORβ”‚ β”‚XORβ”‚ β”‚XORβ”‚ β”‚XORβ”‚ β”‚XORβ”‚ β”‚XORβ”‚ β”‚XORβ”‚ L1-2
24
+ β””β”€β”€β”€β”˜ β””β”€β”€β”€β”˜ β””β”€β”€β”€β”˜ β””β”€β”€β”€β”˜ β””β”€β”€β”€β”˜ β””β”€β”€β”€β”˜ β””β”€β”€β”€β”˜ β””β”€β”€β”€β”˜
25
+ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
26
+ β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”¬β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜
27
+ β–Ό
28
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
29
+ β”‚ NOR β”‚ L3
30
+ β”‚ w: -1Γ—8 β”‚
31
+ β”‚ b: 0 β”‚
32
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
33
+ β”‚
34
+ β–Ό
35
+ (a = b)
36
  ```
37
 
38
  ## Mechanism
 
70
  |---|---|-------|
71
  | 0 | 0 | 1 |
72
  | 0 | 1 | 0 |
73
+ | 127 | 127 | 1 |
74
+ | 127 | 128 | 0 |
75
+ | 255 | 255 | 1 |
76
+ | 255 | 0 | 0 |
77
+ | 100 | 100 | 1 |
 
78
 
79
  ## Architecture
80
 
81
  | Component | Neurons | Parameters |
82
  |-----------|---------|------------|
83
+ | XOR Γ— 8 | 24 | 72 |
84
+ | NOR | 1 | 9 |
85
+ | **Total** | **25** | **81** |
86
 
87
  **Layers: 3** (XOR: 2, NOR: 1)
88
 
 
102
  w = load_file('model.safetensors')
103
 
104
  def equal(a, b):
105
+ """a, b: 8-bit lists [a0..a7] (LSB first)
106
  Returns: 1 if a == b, 0 otherwise"""
107
  # See model.py for full implementation
108
  pass
109
 
110
+ # Example: 127 == 127?
111
+ a = [(127 >> i) & 1 for i in range(8)]
112
+ b = [(127 >> i) & 1 for i in range(8)]
113
  result = equal(a, b) # Returns 1
114
  ```
115
 
config.json CHANGED
@@ -1,9 +1,9 @@
1
  {
2
  "name": "threshold-equal",
3
- "description": "4-bit equality comparator as threshold circuit",
4
- "inputs": 8,
5
  "outputs": 1,
6
- "neurons": 13,
7
  "layers": 3,
8
- "parameters": 41
9
  }
 
1
  {
2
  "name": "threshold-equal",
3
+ "description": "8-bit equality comparator as threshold circuit",
4
+ "inputs": 16,
5
  "outputs": 1,
6
+ "neurons": 25,
7
  "layers": 3,
8
+ "parameters": 81
9
  }
model.py CHANGED
@@ -10,21 +10,21 @@ def xor_gate(a, b, w, idx):
10
  return int((l1 @ w[f'xor{idx}.layer2.weight'].T + w[f'xor{idx}.layer2.bias'] >= 0).item())
11
 
12
  def equal(a, b, weights):
13
- """4-bit equality comparator.
14
- a, b: lists of 4 bits each (LSB first)
15
  Returns: 1 if a == b, 0 otherwise
16
  """
17
- xors = [xor_gate(a[i], b[i], weights, i) for i in range(4)]
18
  xor_vec = torch.tensor([float(x) for x in xors])
19
  return int((xor_vec @ weights['nor.weight'].T + weights['nor.bias'] >= 0).item())
20
 
21
  if __name__ == '__main__':
22
  w = load_model()
23
- print('4-bit Equal Comparator')
24
  print('a == b tests:')
25
- tests = [(0, 0), (0, 1), (5, 5), (5, 10), (15, 15), (7, 8)]
26
  for a_val, b_val in tests:
27
- a = [(a_val >> i) & 1 for i in range(4)]
28
- b = [(b_val >> i) & 1 for i in range(4)]
29
  result = equal(a, b, w)
30
- print(f'{a_val:2d} == {b_val:2d} = {result}')
 
10
  return int((l1 @ w[f'xor{idx}.layer2.weight'].T + w[f'xor{idx}.layer2.bias'] >= 0).item())
11
 
12
  def equal(a, b, weights):
13
+ """8-bit equality comparator.
14
+ a, b: lists of 8 bits each (LSB first)
15
  Returns: 1 if a == b, 0 otherwise
16
  """
17
+ xors = [xor_gate(a[i], b[i], weights, i) for i in range(8)]
18
  xor_vec = torch.tensor([float(x) for x in xors])
19
  return int((xor_vec @ weights['nor.weight'].T + weights['nor.bias'] >= 0).item())
20
 
21
  if __name__ == '__main__':
22
  w = load_model()
23
+ print('8-bit Equal Comparator')
24
  print('a == b tests:')
25
+ tests = [(0, 0), (0, 1), (127, 127), (127, 128), (255, 255), (255, 0), (100, 100)]
26
  for a_val, b_val in tests:
27
+ a = [(a_val >> i) & 1 for i in range(8)]
28
+ b = [(b_val >> i) & 1 for i in range(8)]
29
  result = equal(a, b, w)
30
+ print(f'{a_val:3d} == {b_val:3d} = {result}')
model.safetensors CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:37c8658216c91731dfe0dc104eb66c594b7324eea390dfa2c84ed7a99480a910
3
- size 1468
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:828bc4ccd6ed542dafd4178ba934b4632a43f28778621e9bdf362be94d397cd6
3
+ size 2820