--- license: mit tags: - pytorch - safetensors - threshold-logic - neuromorphic --- # threshold-comparator4bit 4-bit magnitude comparator. Compares two 4-bit unsigned integers. ## Function compare(A, B) -> (GT, LT, EQ) - GT = 1 if A > B - LT = 1 if A < B - EQ = 1 if A = B ## Inputs | Input | Description | |-------|-------------| | A3-A0 | 4-bit number A (A3 is MSB) | | B3-B0 | 4-bit number B (B3 is MSB) | ## Architecture ``` A3 A2 A1 A0 B3 B2 B1 B0 | | | | | | | | +--+--+--+--+--+--+--+ | | v v [GT: A-B >= 1] [LT: B-A >= 1] | | +--------+--------+ | v [EQ: NOR(GT,LT)] ``` Layer 1: - GT: weights [8, 4, 2, 1, -8, -4, -2, -1], bias -1 - LT: weights [-8, -4, -2, -1, 8, 4, 2, 1], bias -1 Layer 2: - EQ: weights [-1, -1], bias 0 (NOR gate) ## Parameters | | | |---|---| | Inputs | 8 | | Outputs | 3 (GT, LT, EQ) | | Neurons | 3 | | Layers | 2 | | Parameters | 21 | | Magnitude | 64 | ## Usage ```python from safetensors.torch import load_file import torch w = load_file('model.safetensors') def compare4(a3, a2, a1, a0, b3, b2, b1, b0): inp = torch.tensor([float(a3), float(a2), float(a1), float(a0), float(b3), float(b2), float(b1), float(b0)]) gt = int((inp @ w['gt.weight'].T + w['gt.bias'] >= 0).item()) lt = int((inp @ w['lt.weight'].T + w['lt.bias'] >= 0).item()) gt_lt = torch.tensor([float(gt), float(lt)]) eq = int((gt_lt @ w['eq.weight'].T + w['eq.bias'] >= 0).item()) return gt, lt, eq # Compare 5 vs 3 gt, lt, eq = compare4(0, 1, 0, 1, 0, 0, 1, 1) print(f"5 vs 3: GT={gt}, LT={lt}, EQ={eq}") # GT=1, LT=0, EQ=0 ``` ## License MIT