Download tests/test_attention_cpu.py from StarAtNyte1/beam-boot: direct link, hf CLI and curl.
- Browser
- Download file 3.04 kB
-
https://huggingface.co/StarAtNyte1/beam-boot/resolve/main/tests/test_attention_cpu.py
- Command line
-
hf download hf://StarAtNyte1/beam-boot/tests/test_attention_cpu.py
-
curl -L -o test_attention_cpu.py https://huggingface.co/StarAtNyte1/beam-boot/resolve/main/tests/test_attention_cpu.py
3.04 kB
| import random | |
| import unittest | |
| from beamboot.attention_cpu import AttentionCPU, Runtime | |
| class AttentionCPUTests(unittest.TestCase): | |
| def test_attention_memory_end_to_end(self): | |
| program = [('LDI', 9), ('STORE', 4), ('LDI', 3), ('STORE', 5), | |
| ('LDI', 0), ('LOAD', 4), ('OUT', 0), ('LOAD', 5), | |
| ('OUT', 0), ('LDI', 7), ('STORE', 4), ('LOAD', 4), | |
| ('OUT', 0), ('HALT', 0)] | |
| runtime = Runtime(AttentionCPU(program)) | |
| steps = [] | |
| while not runtime.halted: steps.append(runtime.step()) | |
| self.assertEqual([s.acc for s in steps if s.output], [9, 3, 7]) | |
| self.assertEqual([(s.address, s.value) for s in steps if s.write], [(4, 9), (5, 3), (4, 7)]) | |
| self.assertEqual(runtime.writes, 3) | |
| def test_loop_and_uninitialized_ram(self): | |
| program = [('LOAD', 8), ('JZ', 3), ('HALT', 0), ('LDI', 3), | |
| ('OUT', 0), ('SUB', 1), ('JZ', 8), ('JMP', 4), ('HALT', 0)] | |
| runtime = Runtime(AttentionCPU(program)) | |
| outputs = [] | |
| for _ in range(30): | |
| step = runtime.step() | |
| if step.output: outputs.append(step.acc) | |
| if step.halt: break | |
| self.assertTrue(runtime.halted) | |
| self.assertEqual(outputs, [3, 2, 1]) | |
| def test_seeded_programs_against_independent_reference(self): | |
| rng = random.Random(218) | |
| for _ in range(5): | |
| instructions = [(rng.choice(['LDI','ADD','SUB','LOAD','STORE','OUT']), rng.randrange(16)) for _ in range(20)] | |
| instructions.append(('HALT', 0)) | |
| runtime, memory, acc = Runtime(AttentionCPU(instructions)), [0]*16, 0 | |
| for pc, (op, arg) in enumerate(instructions): | |
| step = runtime.step() | |
| if op == 'LDI': acc = arg | |
| if op == 'ADD': acc = (acc + arg) & 15 | |
| if op == 'SUB': acc = (acc - arg) & 15 | |
| if op == 'LOAD': acc = memory[arg] | |
| if op == 'STORE': memory[arg] = acc | |
| self.assertEqual(step.acc, acc) | |
| self.assertEqual(step.write, op == 'STORE') | |
| self.assertEqual(step.output, op == 'OUT') | |
| self.assertEqual(step.halt, op == 'HALT') | |
| self.assertEqual(step.pc, pc if op == 'HALT' else pc + 1) | |
| def test_write_capacity_and_validation(self): | |
| runtime = Runtime(AttentionCPU([('STORE', 0), ('JMP', 0)], max_writes=1)) | |
| runtime.step(); runtime.step() | |
| with self.assertRaisesRegex(RuntimeError, 'capacity'): runtime.step() | |
| with self.assertRaises(ValueError): AttentionCPU([('LOAD', 16)]) | |
| class MonitorTests(unittest.TestCase): | |
| def test_interactive_firmware(self): | |
| from beamboot.monitor import Monitor | |
| monitor = Monitor() | |
| self.assertEqual(monitor.boot(), b'>') | |
| self.assertEqual(monitor.command(1), b'OK') | |
| self.assertEqual(monitor.command(2), b'HI') | |
| self.assertEqual(monitor.command(3), b'') | |
| self.assertEqual(monitor.command(1), b'OK') | |