| import torch
|
| from safetensors.torch import save_file
|
|
|
| weights = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
| def add_neuron(name, w_list, bias):
|
| weights[f'{name}.weight'] = torch.tensor([w_list], dtype=torch.float32)
|
| weights[f'{name}.bias'] = torch.tensor([bias], dtype=torch.float32)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| add_neuron('fb_or', [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0], -1.0)
|
| add_neuron('fb_nand', [-1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0], 1.0)
|
|
|
|
|
| for i in range(8):
|
| add_neuron(f'c{i}_shift', [0.0]*(7-i) + [1.0] + [0.0]*(i+1) if i < 7 else [0.0]*9, -1.0 if i < 7 else 0.0)
|
|
|
| save_file(weights, 'model.safetensors')
|
|
|
| def crc8_step(crc, data_bit, poly=0x07):
|
| feedback = ((crc >> 7) ^ data_bit) & 1
|
| crc = (crc << 1) & 0xFF
|
| if feedback:
|
| crc ^= poly
|
| return crc
|
|
|
| print("Verifying CRC-8 step function...")
|
| errors = 0
|
| for crc in range(256):
|
| for d in [0, 1]:
|
| result = crc8_step(crc, d)
|
|
|
| fb = ((crc >> 7) ^ d) & 1
|
| expected = ((crc << 1) & 0xFF) ^ (0x07 if fb else 0)
|
| if result != expected:
|
| errors += 1
|
|
|
| if errors == 0:
|
| print("All 512 test cases passed!")
|
| else:
|
| print(f"FAILED: {errors} errors")
|
|
|
|
|
| msg = [1, 0, 1, 0, 0, 0, 1, 1]
|
| crc = 0
|
| for bit in msg:
|
| crc = crc8_step(crc, bit)
|
| print(f"CRC-8 of message: 0x{crc:02X}")
|
|
|
| mag = sum(t.abs().sum().item() for t in weights.values())
|
| print(f"Magnitude: {mag:.0f}")
|
| print(f"Parameters: {sum(t.numel() for t in weights.values())}")
|
|
|