Download tests/test_chip8_fast.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_chip8_fast.py
- Command line
-
hf download hf://StarAtNyte1/beam-boot/tests/test_chip8_fast.py
-
curl -L -o test_chip8_fast.py https://huggingface.co/StarAtNyte1/beam-boot/resolve/main/tests/test_chip8_fast.py
3.04 kB
| """beamboot/chip8_fast.py: checkpointed frames + speculative verification equal the emulator, | |
| and a wrong draft token is rejected and replaced by the model's own greedy decoding.""" | |
| import json | |
| import os | |
| import pytest | |
| import torch | |
| from beamboot import chip8_fast as F | |
| from beamboot import chip8_lm as C | |
| from beamboot.superbeam_gym import SuperBeamEnv, frames_from_events | |
| ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| DEV = 'cuda' if torch.cuda.is_available() else 'cpu' | |
| ROM = open(os.path.join(ROOT, 'roms', 'superbeam.ch8'), 'rb').read() | |
| EVENTS = json.load(open(os.path.join(ROOT, 'roms', 'superbeam_inputs.json'))) | |
| EVENTS = EVENTS['events'] if isinstance(EVENTS, dict) else EVENTS | |
| def emulator_rows(n): | |
| env = SuperBeamEnv(backend='emulator') | |
| env.reset() | |
| out = [] | |
| for keys in frames_from_events(EVENTS, n): | |
| env.backend.advance(keys) | |
| out.append(env.backend.m.display_rows()) | |
| return out | |
| def model(): | |
| return C.get_model(300, DEV) | |
| def test_frames_equal_emulator_and_drafts_accepted(model): | |
| n = 6 | |
| s = F.run(ROM, EVENTS, frames=n, device=DEV, model=model) | |
| assert s.frame_rows == emulator_rows(n) | |
| assert all(x.accepted_all for x in s.stats) | |
| # constant-size context: the prompt is the non-zero RAM + registers, so after frame 0 (before | |
| # `start` copies the level tables into RAM) it only moves by a few tokens, it never grows | |
| sizes = [x.prompt_tokens for x in s.stats] | |
| assert max(sizes[1:]) - min(sizes[1:]) <= 8 and max(sizes) - min(sizes) <= 200 | |
| def test_wrong_draft_is_rejected_and_model_decides(model): | |
| s = F.FastSession(ROM, DEV, model=model) | |
| real = s._draft | |
| def corrupt(evs): | |
| d = real(evs) | |
| j = len(d) - 3 # the tick record's CLKH | |
| k, v, a = C.untok(d[j]) | |
| d[j] = C.tok(k, (v + 2) % C.SIZES[k], a) | |
| return d | |
| s._draft = corrupt | |
| s.advance([(k, a == 'down') for g, k, a in EVENTS if g == 0]) | |
| st = s.stats[0] | |
| assert not st.accepted_all and st.first_reject is not None | |
| assert s.frame_rows == emulator_rows(1) # the model's greedy tokens, not the draft | |
| def test_checkpoint_rejects_open_subroutine(): | |
| from beamboot.chip8 import Chip8 | |
| c = Chip8() | |
| c.sp = 1 | |
| with pytest.raises(ValueError): | |
| F.checkpoint_prompt(c) | |
| def test_key_release_after_checkpoint(model): | |
| # a key held at a checkpoint and released in the next frame (regression: the release used to | |
| # tie with the checkpoint's KEY(down) write at the same clock, and the model read 'down') | |
| events = [[0, 6, 'down'], [3, 6, 'up'], [4, 4, 'down'], [5, 4, 'up']] | |
| s = F.run(ROM, events, frames=6, device=DEV, model=model) | |
| env = SuperBeamEnv(backend='emulator') | |
| env.reset() | |
| ref = [] | |
| for keys in frames_from_events(events, 6): | |
| env.backend.advance(keys) | |
| ref.append(env.backend.m.display_rows()) | |
| assert s.frame_rows == ref and all(x.accepted_all for x in s.stats) | |