File size: 3,031 Bytes
d2a1a41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Model configuration and YAML profile loading."""
from __future__ import annotations

from dataclasses import asdict, dataclass, fields
from pathlib import Path

import yaml


@dataclass
class ModelConfig:
    architecture: str = "transformer"  # old checkpoints default to the original transformer
    vocab_size: int = 9280
    d_model: int = 512
    n_layers: int = 8
    n_heads: int = 8
    n_kv_heads: int = 8  # < n_heads => grouped-query attention
    ffn_mult: float = 8 / 3  # SwiGLU hidden = ffn_mult * d_model, rounded up to a multiple of 128
    max_seq_len: int = 1024
    rope_theta: float = 10000.0
    norm_eps: float = 1e-5
    vis_positions: int = 64  # spatial slots per frame (8x8 grid); 0 disables visual position embedding
    visual_offset: int = 0  # first visual-code token id (from UnifiedVocab)
    visual_size: int = 0  # number of visual-code ids
    tie_embeddings: bool = True
    init_std: float = 0.02
    grad_checkpoint: bool = False
    attention_layers: tuple[int, ...] = (3, 7, 11)  # zero-based Axiom-1.1 indices
    d_cell: int = 1280
    n_cells: int = 2
    top_k: int = 2
    scan_chunk: int = 64
    balance_weight: float = 0.01

    def __post_init__(self):
        assert self.d_model % self.n_heads == 0, "d_model must divide by n_heads"
        assert self.n_heads % self.n_kv_heads == 0, "n_heads must divide by n_kv_heads"
        if self.architecture not in ("transformer", "axiom1_1"):
            raise ValueError(f"unknown architecture: {self.architecture}")
        if self.architecture == "axiom1_1":
            self.attention_layers = tuple(self.attention_layers)
            if self.n_cells != 2 or self.top_k != 2:
                raise ValueError("Axiom-1.1 uses two cells, both active; sparse routing is not implemented")
            if self.n_kv_heads != self.n_heads or self.head_dim % 2:
                raise ValueError("Axiom-1.1 requires MHA and an even head dimension")
            if len(set(self.attention_layers)) != len(self.attention_layers) or any(i < 0 or i >= self.n_layers for i in self.attention_layers):
                raise ValueError("attention_layers must contain distinct zero-based layer indices")
            if self.scan_chunk < 1 or self.d_cell < 1:
                raise ValueError("scan_chunk and d_cell must be positive")

    @property
    def head_dim(self) -> int:
        return self.d_model // self.n_heads

    @property
    def ffn_hidden(self) -> int:
        return -(-int(self.ffn_mult * self.d_model) // 128) * 128

    def to_dict(self) -> dict:
        return asdict(self)

    @classmethod
    def from_dict(cls, d: dict) -> "ModelConfig":
        names = {f.name for f in fields(cls)}
        unknown = set(d) - names
        if unknown:
            raise ValueError(f"unknown model config keys: {sorted(unknown)}")
        return cls(**d)


def load_yaml(path: str | Path) -> dict:
    with open(path) as f:
        return yaml.safe_load(f) or {}