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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
"""chipformer: a decoder-only transformer whose weights were set by hand to execute CHIP-8.



The network is ordinary: token embedding plus a position embedding from [t, t^2], causal

softmax attention heads with residual connections, ReGLU feed-forward layers and a linear

unembedding, all in float64, with no normalization. Nothing about CHIP-8 is in this file

except the host loop at the bottom (ChipformerGame), which only feeds the boot prompt,

injects TICK and key tokens, and reads the screen back from the model's own writes.



    model = AutoModelForCausalLM.from_pretrained("divagr1925/chipformer", trust_remote_code=True,

                                                 dtype=torch.float64)

    game = model.play(open("tetris.ch8", "rb").read(), quirks="shift,loadstore,clip,jump", ipf=15)

    for _ in range(60):

        game.frame()

    print(game.ascii())

"""

import warnings
from dataclasses import dataclass
from pathlib import Path

import torch
from torch import nn
from transformers import PreTrainedModel
from transformers.modeling_outputs import CausalLMOutputWithPast

from .configuration_chipformer import ChipformerConfig

SPARSE_MIN = 100_000  # weights; smaller matrices stay dense


class ChipformerCache:
    """Keys and values per layer, shaped (batch, heads, positions, width), plus the length.



    Buffers grow by doubling, so appending one token costs O(1) amortized.

    """

    def __init__(self):
        self.kv: dict[int, list[torch.Tensor]] = {}
        self.length = 0

    def get_seq_length(self, layer_idx: int = 0) -> int:
        return self.length

    def append(self, li: int, k: torch.Tensor, v: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
        """Store a layer's new keys/values; returns all keys and values so far."""
        start, n = self.length, k.shape[-2]
        if li not in self.kv:
            cap = max(1024, 2 * n)
            self.kv[li] = [x.new_zeros(*x.shape[:-2], cap, x.shape[-1]) for x in (k, v)]
        buf = self.kv[li]
        if start + n > buf[0].shape[-2]:
            cap = max(2 * buf[0].shape[-2], start + n)
            for j, old in enumerate(buf):
                buf[j] = old.new_zeros(*old.shape[:-2], cap, old.shape[-1])
                buf[j][..., :start, :] = old[..., :start, :]
        buf[0][..., start:start + n, :] = k
        buf[1][..., start:start + n, :] = v
        return buf[0][..., :start + n, :], buf[1][..., :start + n, :]


class ChipformerHead(nn.Module):
    def __init__(self, d: int, qk: int, v: int):
        super().__init__()
        self.wq = nn.Parameter(torch.zeros(qk, d, dtype=torch.float64))
        self.wk = nn.Parameter(torch.zeros(qk, d, dtype=torch.float64))
        self.wv = nn.Parameter(torch.zeros(v, d, dtype=torch.float64))
        self.wo = nn.Parameter(torch.zeros(d, v, dtype=torch.float64))


class ChipformerFFN(nn.Module):
    """ReGLU: w_down (relu(w_gate x) * (w_up x))."""

    def __init__(self, d: int, units: int):
        super().__init__()
        self.w_gate = nn.Parameter(torch.zeros(units, d, dtype=torch.float64))
        self.w_up = nn.Parameter(torch.zeros(units, d, dtype=torch.float64))
        self.w_down = nn.Parameter(torch.zeros(d, units, dtype=torch.float64))


class ChipformerLayer(nn.Module):
    def __init__(self, d: int, spec: dict):
        super().__init__()
        self.attn = nn.ModuleList(ChipformerHead(d, qk, v) for qk, v in spec["heads"])
        self.ffn = ChipformerFFN(d, spec["ffn_units"])


class ChipformerEmbedding(nn.Module):
    def __init__(self, vocab: int, d: int):
        super().__init__()
        self.token = nn.Parameter(torch.zeros(vocab, d, dtype=torch.float64))
        self.pos_poly = nn.Parameter(torch.zeros(d, 2, dtype=torch.float64))


class ChipformerPreTrainedModel(PreTrainedModel):
    config_class = ChipformerConfig
    base_model_prefix = "chipformer"
    _supports_cache_class = False

    def _init_weights(self, module):
        pass  # every weight comes from the checkpoint; there is nothing to initialize


class ChipformerForCausalLM(ChipformerPreTrainedModel):
    def __init__(self, config: ChipformerConfig):
        super().__init__(config)
        d = config.d_model
        self.embed = ChipformerEmbedding(config.vocab_size, d)
        self.layers = nn.ModuleList(ChipformerLayer(d, s) for s in config.layers)
        self.unembed = nn.Parameter(torch.zeros(config.vocab_size, d, dtype=torch.float64))
        self._csr: dict[str, torch.Tensor] | None = None
        self._fused: list[tuple[torch.Tensor, ...]] | None = None
        self.post_init()

    def sparse(self, enable: bool = True) -> "ChipformerForCausalLM":
        """Multiply by sparse (CSR) copies of the feed-forward and unembedding matrices.



        Only 0.4% of the weights are nonzero, and the dense feed-forward matrices are most of

        the 259 MB that every decoded token otherwise reads. The arithmetic is exact either

        way (the circuits only produce values float64 represents exactly), so the outputs

        are identical.

        """
        if not enable:
            self._csr = None
            return self
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")  # "sparse CSR support is in beta"
            # small matrices are faster dense; only the big ones are worth a sparse kernel
            self._csr = {name: p.detach().to_sparse_csr() for name, p in self.named_parameters()
                         if (".ffn." in name or name == "unembed") and p.numel() >= SPARSE_MIN}
        return self

    def _lin(self, x: torch.Tensor, w: torch.Tensor, name: str) -> torch.Tensor:
        """x @ w.T, through the sparse copy when enabled."""
        m = None if self._csr is None else self._csr.get(name)
        if m is None:
            return x @ w.T
        y = m @ x.reshape(-1, x.shape[-1]).T
        return y.T.reshape(*x.shape[:-1], m.shape[0])

    def _heads(self, li: int) -> tuple[torch.Tensor, ...] | None:
        """A layer's heads stacked into (heads, width, d) tensors, zero-padded to one width.



        Padding adds exact zeros, so this is the same computation as running each head alone.

        Built on first use; call ``refresh()`` after changing the weights in place.

        """
        if self._fused is None:
            self._fused = []
            for layer in self.layers:
                if not len(layer.attn):
                    self._fused.append(None)
                    continue
                qk = max(h.wq.shape[0] for h in layer.attn)
                vd = max(h.wv.shape[0] for h in layer.attn)

                def stack(ws, width):
                    return torch.stack([nn.functional.pad(w.detach(), (0, 0, 0, width - w.shape[0]))
                                        for w in ws])
                self._fused.append((
                    stack([h.wq for h in layer.attn], qk), stack([h.wk for h in layer.attn], qk),
                    stack([h.wv for h in layer.attn], vd),
                    stack([h.wo.T for h in layer.attn], vd).transpose(-1, -2)))
        return self._fused[li]

    def refresh(self) -> None:
        """Rebuild the stacked/sparse copies after editing weights in place."""
        self._fused = None
        if self._csr is not None:
            self.sparse()

    def get_input_embeddings(self):
        return None

    def _attend(self, q, k, v, start: int) -> torch.Tensor:
        """Causal softmax attention; query i sits at position start + i, key j at position j."""
        s = q @ k.transpose(-1, -2)
        n, total = s.shape[-2], s.shape[-1]
        if n > 1:
            qpos = torch.arange(start, start + n, device=s.device)[:, None]
            s = s.masked_fill(torch.arange(total, device=s.device)[None, :] > qpos, float("-inf"))
        s = s - s.amax(-1, keepdim=True)
        p = torch.exp(self.config.beta * s)
        return (p / p.sum(-1, keepdim=True)) @ v

    def forward(self, input_ids: torch.LongTensor, past_key_values: ChipformerCache | None = None,

                use_cache: bool | None = None, labels: torch.LongTensor | None = None,

                logits_to_keep: int = 0, return_dict: bool | None = None, **kwargs):
        """input_ids: (batch, n). Positions continue from the cache length (absolute index)."""
        cache = past_key_values if past_key_values is not None else ChipformerCache()
        start = cache.length
        n = input_ids.shape[-1]
        if start + n > self.config.max_position:
            raise ValueError(f"position {start + n} exceeds max_position {self.config.max_position}; "
                             "restart the context from a state dump (see ChipformerGame.rollover)")
        t = torch.arange(start, start + n, dtype=torch.float64, device=input_ids.device)
        r = self.embed.token[input_ids] + torch.stack([t, t * t], 1) @ self.embed.pos_poly.T
        for li, layer in enumerate(self.layers):
            heads = self._heads(li)
            if heads is not None:  # some layers are feed-forward only
                wq, wk, wv, wo = heads
                x = r.unsqueeze(-3)  # (batch, 1, n, d): broadcast over heads
                k, v = cache.append(li, x @ wk.transpose(-1, -2), x @ wv.transpose(-1, -2))
                out = self._attend(x @ wq.transpose(-1, -2), k, v, start)  # (batch, heads, n, width)
                r = r + (out @ wo.transpose(-1, -2)).sum(-3)
            f, p = layer.ffn, f"layers.{li}.ffn"
            hidden = torch.relu(self._lin(r, f.w_gate, f"{p}.w_gate")) * self._lin(r, f.w_up, f"{p}.w_up")
            r = r + self._lin(hidden, f.w_down, f"{p}.w_down")
        logits = self._lin(r if not logits_to_keep else r[..., -logits_to_keep:, :], self.unembed, "unembed")
        loss = None
        if labels is not None:
            loss = nn.functional.cross_entropy(logits[..., :-1, :].reshape(-1, logits.shape[-1]),
                                               labels[..., 1:].reshape(-1))
        cache.length = start + n
        return CausalLMOutputWithPast(loss=loss, logits=logits,
                                      past_key_values=None if use_cache is False else cache)

    def play(self, rom: "bytes | str | Path", quirks="octo", ipf: int = 10,

             keep_tokens: bool = False, sparse: bool = True) -> "ChipformerGame":
        """Boot a CHIP-8 ROM; call ``.frame()`` on the result once per 60 Hz frame.



        ``sparse`` switches on the exact sparse matmuls (see ``sparse()``); pass False to run

        the dense weights as loaded.

        """
        if sparse and self._csr is None:
            self.sparse()
        elif not sparse:
            self.sparse(False)
        return ChipformerGame(self, rom, quirks=quirks, ipf=ipf, keep_tokens=keep_tokens)


# --- host loop -----------------------------------------------------------------------------

MAX_RECORD = 2 * 40 + 2  # longer than any valid record


@dataclass
class _Machine:
    """Token layout and boot constants, read from config.machine."""

    VAL_BASE: int
    ADDR_BASE: int
    STEP: int
    TICK: int
    BOOT: int
    NUM_CELLS: int
    NULL: int
    CLS: int
    PC: int
    RNG: int
    KEYS: int
    DISPLAY: int
    DISPLAY_W: int
    DISPLAY_H: int
    PROGRAM_START: int
    FONT_BASE: int
    RNG_SEED: int
    QUIRKS: int
    FONT: list
    QUIRK_NAMES: list
    PROFILES: dict


class ChipformerGame:
    """Runs a CHIP-8 program on the model, the way the Rust/wasm engine does.



    The host never computes machine semantics. It (1) feeds the boot prompt

    ``BOOT <state dump> STEP``, (2) injects ``TICK`` and key writes ``ADDR k, VAL v, STEP``

    right after a STEP, and (3) keeps a mirror of the cells from the model's own writes so it

    can show the screen. Every other token is the model's greedy prediction.

    """

    def __init__(self, model: ChipformerForCausalLM, rom, quirks="octo", ipf: int = 10,

                 keep_tokens: bool = False):
        if isinstance(rom, (str, Path)):
            rom = Path(rom).read_bytes()
        self.model = model
        self.m = _Machine(**model.config.machine)
        self.ipf = ipf
        self.keep_tokens = keep_tokens
        self.tokens: list[int] = []
        self.instructions = 0
        self.rollovers = 0
        self.fed = 0  # tokens fed since boot, across rollovers
        m = self.m
        if len(rom) > 0x1000 - m.PROGRAM_START:
            raise ValueError(f"ROM too large: {len(rom)} bytes")
        cells = [0] * m.NUM_CELLS
        cells[m.FONT_BASE:m.FONT_BASE + len(m.FONT)] = m.FONT
        cells[m.PROGRAM_START:m.PROGRAM_START + len(rom)] = list(rom)
        cells[m.PC] = m.PROGRAM_START
        cells[m.RNG] = m.RNG_SEED
        for a, v in self._quirk_cells(quirks).items():
            cells[a] = v
        self.cells = cells
        self._new_context()

    def _quirk_cells(self, quirks) -> dict[int, int]:
        m = self.m
        if quirks is None:
            on = []
        elif isinstance(quirks, str):
            on = m.PROFILES[quirks] if quirks in m.PROFILES else [q for q in quirks.split(",") if q]
        else:
            on = list(quirks)
        bad = set(on) - set(m.QUIRK_NAMES)
        if bad:
            raise ValueError(f"unknown quirks {sorted(bad)}; known: {m.QUIRK_NAMES} or {list(m.PROFILES)}")
        return {m.QUIRKS + i: int(q in on) for i, q in enumerate(m.QUIRK_NAMES)}

    def boot_tokens(self) -> list[int]:
        """BOOT, the CLS write, every other nonzero cell in address order, STEP."""
        m, c = self.m, self.cells
        out = [m.BOOT, m.ADDR_BASE + m.CLS, m.VAL_BASE + c[m.CLS]]
        for a, v in enumerate(c):
            if a not in (m.CLS, m.NULL) and v:
                out += [m.ADDR_BASE + a, m.VAL_BASE + v]
        return out + [m.STEP]

    def _new_context(self) -> None:
        self.cache = ChipformerCache()
        self._feed(self.boot_tokens())

    @torch.inference_mode()
    def _feed(self, toks: list[int]) -> None:
        ids = torch.tensor([toks], dtype=torch.long, device=self.model.device)
        out = self.model(ids, past_key_values=self.cache, use_cache=True, logits_to_keep=1)
        self.cache = out.past_key_values
        self.pred = int(out.logits[0, -1].argmax())
        self.fed += len(toks)
        if self.keep_tokens:
            self.tokens += toks

    @property
    def position(self) -> int:
        return self.cache.length

    def _apply(self, writes) -> None:
        m = self.m
        for a, v in writes:
            if a == m.CLS:
                n = m.DISPLAY_W * m.DISPLAY_H // 8
                self.cells[m.DISPLAY:m.DISPLAY + n] = [0] * n
            self.cells[a] = v

    def _record(self) -> list[tuple[int, int]]:
        """Let the model write until it emits STEP."""
        m, writes = self.m, []
        for _ in range(MAX_RECORD):
            tok = self.pred
            if tok == m.STEP:
                self._feed([tok])
                self._apply(writes)
                return writes
            if not m.ADDR_BASE <= tok < m.STEP:
                raise RuntimeError(f"model emitted {tok} where ADDR/STEP was expected at {self.position}")
            self._feed([tok])
            val = self.pred
            if not m.VAL_BASE <= val < m.ADDR_BASE:
                raise RuntimeError(f"model emitted {val} after an ADDR at {self.position}")
            self._feed([val])
            writes.append((tok - m.ADDR_BASE, val - m.VAL_BASE))
        raise RuntimeError("record never ended")

    def step(self) -> list[tuple[int, int]]:
        """Execute one instruction (the model writes its effects)."""
        self.instructions += 1
        return self._record()

    def tick(self) -> list[tuple[int, int]]:
        """60 Hz timer tick: inject TICK, the model writes DT and ST."""
        self._feed([self.m.TICK])
        return self._record()

    def set_key(self, key: int, down: bool) -> None:
        m = self.m
        a = m.KEYS + key
        if bool(self.cells[a]) == down:
            return
        self._feed([m.ADDR_BASE + a, m.VAL_BASE + int(down), m.STEP])
        self.cells[a] = int(down)

    def rollover(self) -> None:
        """Restart the context from a state dump of the mirror (keeps positions bounded)."""
        self._new_context()
        self.rollovers += 1

    def frame(self, keys=()) -> None:
        """One 60 Hz frame: key changes, one TICK, then ``ipf`` instructions.



        ``keys``: the CHIP-8 keys (0..15) held this frame, or a 16-bit mask.

        """
        held = {k for k in range(16) if keys >> k & 1} if isinstance(keys, int) else set(keys)
        for k in range(16):
            self.set_key(k, k in held)
        self.tick()
        for _ in range(self.ipf):
            self.step()
        if self.position > self.model.config.rollover_at:
            self.rollover()

    def pixels(self) -> list[list[int]]:
        m = self.m
        w8 = m.DISPLAY_W // 8
        return [[self.cells[m.DISPLAY + y * w8 + x // 8] >> (7 - x % 8) & 1 for x in range(m.DISPLAY_W)]
                for y in range(m.DISPLAY_H)]

    def ascii(self) -> str:
        """The 64x32 screen as 16 lines of half-block characters."""
        p = self.pixels()
        chars = {(0, 0): " ", (1, 0): "▀", (0, 1): "▄", (1, 1): "█"}
        return "\n".join("".join(chars[p[y][x], p[y + 1][x]] for x in range(len(p[0])))
                         for y in range(0, len(p), 2))