Download src/pat/config.py from ICLRPAT27/PAT-code: direct link, hf CLI and curl.
- Browser
- Download file 4.87 kB
-
https://huggingface.co/ICLRPAT27/PAT-code/resolve/main/src/pat/config.py
- Command line
-
hf download hf://ICLRPAT27/PAT-code/src/pat/config.py
-
curl -L -o config.py https://huggingface.co/ICLRPAT27/PAT-code/resolve/main/src/pat/config.py
4.87 kB
| """The publication architecture, with only model geometry left configurable.""" | |
| from __future__ import annotations | |
| import math | |
| from dataclasses import asdict, dataclass | |
| from typing import Any | |
| EOD_ID = 50_256 | |
| PAD_ID = 50_257 | |
| BOS_ID = 50_258 | |
| EOC_ID = 50_259 | |
| class PATConfig: | |
| """PAT and its full-attention ablation share this fixed decoder contract. | |
| ``memory_slots`` gives the number of summary rows written at each layer. | |
| ``qk_key_counts`` and ``qk_reference_key_counts`` are corpus-calibrated | |
| visible-key quantiles, used once to initialize the learned QK scale. | |
| The reference uses two summary slots. They are required for PAT, so a new | |
| geometry cannot silently reuse another experiment's initialization. | |
| ``ce_chunk_size`` limits vocabulary-projection workspace, not attention. | |
| """ | |
| architecture: str = "PAT" | |
| d_model: int = 2048 | |
| n_heads: int = 32 | |
| n_layers: int = 32 | |
| vocab_size: int = 50_304 | |
| chunk_size: int = 16 | |
| context_length: int = 16_384 | |
| memory_slots: tuple[int, ...] = () | |
| ffw_mult_by_layer: tuple[float, ...] = () | |
| qk_key_counts: tuple[float, ...] = () | |
| qk_reference_key_counts: tuple[float, ...] = () | |
| seed: int = 42 | |
| ce_chunk_size: int = 1024 | |
| def __post_init__(self) -> None: | |
| if self.architecture not in {"PAT", "FA"}: | |
| raise ValueError("architecture must be PAT or FA") | |
| for name in ( | |
| "d_model", | |
| "n_heads", | |
| "n_layers", | |
| "vocab_size", | |
| "chunk_size", | |
| "context_length", | |
| "ce_chunk_size", | |
| ): | |
| value = getattr(self, name) | |
| if type(value) is not int or value <= 0: | |
| raise ValueError(f"{name} must be a positive integer") | |
| if self.vocab_size <= EOC_ID or self.d_model % self.n_heads: | |
| raise ValueError( | |
| "vocabulary must contain the fixed special IDs; width must divide evenly into heads" | |
| ) | |
| if self.chunk_size not in {16, 32}: | |
| raise ValueError("chunk_size must be 16 or 32") | |
| if self.context_length % self.chunk_size or self.context_length < 4 * self.chunk_size: | |
| raise ValueError( | |
| "context_length must be a multiple of chunk_size and contain at least four chunks" | |
| ) | |
| if self.head_dim % 4 or not 20 <= self.head_dim <= 128: | |
| raise ValueError("head dimension must be divisible by four and lie in [20, 128]") | |
| slots = ( | |
| tuple(self.memory_slots) or ((2 if self.architecture == "PAT" else 0),) * self.n_layers | |
| ) | |
| multipliers = tuple(float(x) for x in self.ffw_mult_by_layer) or (4.0,) * self.n_layers | |
| if len(slots) != self.n_layers or any(type(x) is not int or x < 0 for x in slots): | |
| raise ValueError("memory_slots needs one nonnegative integer per layer") | |
| if self.architecture == "PAT" and any(x == 0 for x in slots): | |
| raise ValueError("PAT needs positive memory_slots in every layer") | |
| if self.architecture == "FA" and any(slots): | |
| raise ValueError("FA has zero memory_slots") | |
| if len(multipliers) != self.n_layers or any( | |
| not math.isfinite(x) or x < 1 for x in multipliers | |
| ): | |
| raise ValueError("ffw_mult_by_layer needs one finite multiplier >= 1 per layer") | |
| object.__setattr__(self, "memory_slots", slots) | |
| object.__setattr__(self, "ffw_mult_by_layer", multipliers) | |
| for name in ("qk_key_counts", "qk_reference_key_counts"): | |
| values = tuple(float(x) for x in getattr(self, name)) | |
| if self.architecture == "PAT": | |
| if len(values) != self.n_layers or any( | |
| not math.isfinite(x) or x < 1 for x in values | |
| ): | |
| raise ValueError(f"{name} needs one calibrated count >= 1 per layer for PAT") | |
| elif values: | |
| raise ValueError( | |
| "FA uses the fixed QK scale initialization and has no key-count calibration" | |
| ) | |
| object.__setattr__(self, name, values) | |
| def head_dim(self) -> int: | |
| return self.d_model // self.n_heads | |
| def max_memory_size(self) -> int: | |
| return self.context_length // self.chunk_size | |
| def qk_scale(self, layer: int) -> float: | |
| if self.architecture == "FA": | |
| return 10.0 | |
| actual, reference = self.qk_key_counts[layer], self.qk_reference_key_counts[layer] | |
| if min(actual, reference) < 2: | |
| return 10.0 | |
| return 10.0 * math.log(actual * (actual - 1)) / math.log(reference * (reference - 1)) | |
| def to_dict(self) -> dict[str, Any]: | |
| return asdict(self) | |
| def from_dict(cls, values: dict[str, Any]) -> PATConfig: | |
| return cls(**values) | |