| license: mit | |
| tags: | |
| - pytorch | |
| - safetensors | |
| - threshold-logic | |
| - neuromorphic | |
| # threshold-popcount4 | |
| 4-bit population count. Counts the number of 1 bits in a 4-bit input. | |
| ## Function | |
| popcount4(a, b, c, d) = binary count of 1 bits | |
| Output [y2, y1, y0] is the 3-bit binary representation of the count (0-4). | |
| ## Truth Table | |
| | a | b | c | d | count | y2 | y1 | y0 | | |
| |---|---|---|---|-------|----|----|-----| | |
| | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | |
| | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | | |
| | 0 | 0 | 1 | 1 | 2 | 0 | 1 | 0 | | |
| | 0 | 1 | 1 | 1 | 3 | 0 | 1 | 1 | | |
| | 1 | 1 | 1 | 1 | 4 | 1 | 0 | 0 | | |
| ## Architecture | |
| ``` | |
| a b c d | |
| | | | | | |
| +----+---+---+---+----+ | |
| | | | |
| Layer 1: | | |
| y2 = (sum >= 4) | | |
| ge2 = (sum >= 2) | | |
| le3 = (sum <= 3) | | |
| XOR(a,b) components | | |
| XOR(c,d) components | | |
| | | | |
| Layer 2: | | |
| y1 = AND(ge2, le3) | | |
| xor_ab = XOR(a,b) | | |
| xor_cd = XOR(c,d) | | |
| | | | |
| Layer 3: | | |
| XOR(xor_ab, xor_cd) | | |
| components | | |
| | | | |
| Layer 4: | | |
| y0 = XOR(xor_ab, xor_cd) | |
| ``` | |
| - **y2** (MSB): 1 when count = 4 | |
| - **y1**: 1 when count is 2 or 3 | |
| - **y0** (LSB): 1 when count is odd (parity = XOR4) | |
| ## Parameters | |
| | | | | |
| |---|---| | |
| | Inputs | 4 | | |
| | Outputs | 3 | | |
| | Neurons | 13 | | |
| | Layers | 4 | | |
| | Parameters | 53 | | |
| | Magnitude | 55 | | |
| ## Usage | |
| ```python | |
| from safetensors.torch import load_file | |
| import torch | |
| w = load_file('model.safetensors') | |
| # See model.py for full implementation | |
| # popcount4(1, 0, 1, 1) returns [0, 1, 1] (count=3 = binary 011) | |
| ``` | |
| ## License | |
| MIT | |