Download beamboot/memory.py from StarAtNyte1/beam-boot: direct link, hf CLI and curl.
- Browser
- Download file 2.67 kB
-
https://huggingface.co/StarAtNyte1/beam-boot/resolve/main/beamboot/memory.py
- Command line
-
hf download hf://StarAtNyte1/beam-boot/beamboot/memory.py
-
curl -L -o memory.py https://huggingface.co/StarAtNyte1/beam-boot/resolve/main/beamboot/memory.py
2.67 kB
| """An experimental causal attention memory, not an implementation of Beam. | |
| Each write is a key/value entry in an append-only cache. A query attends to the | |
| latest matching address using positional bias. All operations here are scalar | |
| Python arithmetic so the experiment runs without a machine learning framework. | |
| """ | |
| from dataclasses import dataclass | |
| import math | |
| class Entry: | |
| key: tuple[float, ...] | |
| value: float | |
| address: int | |
| position: int | |
| class AttentionMemory: | |
| def __init__(self, address_bits=4, window=None, temperature=0.01): | |
| if address_bits < 1 or temperature <= 0 or (window is not None and window < 1): | |
| raise ValueError('invalid attention configuration') | |
| self.address_bits = address_bits | |
| self.window = window | |
| self.temperature = temperature | |
| self.cache: list[Entry] = [] | |
| def _key(self, address): | |
| if not isinstance(address, int) or not 0 <= address < 2 ** self.address_bits: | |
| raise ValueError('address outside configured address space') | |
| # Bipolar encoding: an exact match has dot product address_bits; | |
| # any nonmatch scores at least two less. | |
| return tuple(1.0 if address & (1 << i) else -1.0 for i in range(self.address_bits)) | |
| def write(self, address, value): | |
| if not isinstance(value, int) or not 0 <= value <= 255: | |
| raise ValueError('value must be a byte') | |
| self.cache.append(Entry(self._key(address), float(value), address, len(self.cache))) | |
| def read(self, address): | |
| query = self._key(address) | |
| entries = self.cache[-self.window:] if self.window else self.cache | |
| if not entries: | |
| return None | |
| # A bounded recency term breaks ties without allowing an address mismatch | |
| # to beat a match. This is a constructed attention score, not Beam weights. | |
| n = len(self.cache) | |
| scores = [sum(a*b for a, b in zip(query, e.key)) + (e.position + 1) / (n + 1) for e in entries] | |
| peak = max(scores) | |
| weights = [math.exp((s - peak) / self.temperature) for s in scores] | |
| total = sum(weights) | |
| value = sum(w * e.value for w, e in zip(weights, entries)) / total | |
| # Softmax alone cannot express missing keys. Report confidence and let the | |
| # host detect whether the selected key matches; do not hide that boundary. | |
| winner = max(range(len(entries)), key=lambda i: scores[i]) | |
| if entries[winner].key != query: | |
| return None | |
| return value | |
| def read_byte(self, address): | |
| result = self.read(address) | |
| return None if result is None else round(result) | |