File size: 10,110 Bytes
a38f163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
from __future__ import annotations

import json
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Sequence

import torch
import torch.nn.functional as F
from torch import Tensor, nn

from .modeling import DEFAULT_BASE_MODEL, _get_decoder_layers
from .sharded_moe import (
    ShardedMoECeNNConfig,
    ShardedMoECeNNReplacementLayer,
    build_sharded_moe_student,
)


@dataclass(frozen=True)
class StoryV2Config:
    memory_rank: int = 32
    head_rank: int = 4

    def validate(self, hidden_size: int, vocab_size: int) -> None:
        if not 1 <= self.memory_rank <= hidden_size:
            raise ValueError("memory_rank must be in [1, hidden_size]")
        if not 1 <= self.head_rank <= min(hidden_size, vocab_size):
            raise ValueError("head_rank must be positive and <= hidden/vocab size")

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

    @classmethod
    def from_dict(cls, data: dict) -> "StoryV2Config":
        return cls(**dict(data))


class CausalStoryMemory(nn.Module):
    """Cheap global prefix memory with no attention and no token-by-token Python loop."""

    def __init__(self, hidden_size: int, rank: int) -> None:
        super().__init__()
        self.down = nn.Linear(hidden_size, rank, bias=False)
        self.up = nn.Linear(rank, hidden_size, bias=False)
        nn.init.normal_(self.down.weight, mean=0.0, std=0.02)
        nn.init.zeros_(self.up.weight)

    def forward(self, hidden_states: Tensor) -> Tensor:
        dtype = hidden_states.dtype
        work = hidden_states.float()
        prefix_sum = work.cumsum(dim=1)
        denom = torch.arange(
            1,
            hidden_states.shape[1] + 1,
            device=hidden_states.device,
            dtype=work.dtype,
        ).view(1, -1, 1)
        prefix_mean = (prefix_sum / denom).to(dtype=dtype)
        return self.up(F.silu(self.down(prefix_mean)))


class StoryV2ReplacementLayer(nn.Module):
    def __init__(self, cenn: nn.Module, hidden_size: int, story_config: StoryV2Config) -> None:
        super().__init__()
        self.cenn = cenn
        self.story_memory = CausalStoryMemory(hidden_size, story_config.memory_rank)

    def forward(self, hidden_states: Tensor, *args, **kwargs) -> Tensor:
        if kwargs.get("use_cache", False):
            raise RuntimeError("TinyCeNN Story-v2 requires use_cache=False")
        if kwargs.get("output_attentions", False):
            raise RuntimeError("TinyCeNN Story-v2 has no attention matrices")
        memory = self.story_memory(hidden_states)
        enriched = hidden_states + memory
        return hidden_states + self.cenn(enriched)


class LowRankLMHeadAdapter(nn.Module):
    """Frozen language-model head plus a tiny trainable low-rank story adapter."""

    def __init__(self, base_head: nn.Module, hidden_size: int, vocab_size: int, rank: int) -> None:
        super().__init__()
        self.base_head = base_head
        for parameter in self.base_head.parameters():
            parameter.requires_grad = False
        self.down = nn.Linear(hidden_size, rank, bias=False)
        self.up = nn.Linear(rank, vocab_size, bias=False)
        nn.init.normal_(self.down.weight, mean=0.0, std=0.02)
        nn.init.zeros_(self.up.weight)

    @property
    def weight(self):
        return getattr(self.base_head, "weight", None)

    def forward(self, hidden_states: Tensor) -> Tensor:
        return self.base_head(hidden_states) + self.up(F.silu(self.down(hidden_states)))


def upgrade_sharded_model_to_story_v2(model: nn.Module, story_config: StoryV2Config) -> nn.Module:
    hidden_size = int(model.config.hidden_size)
    vocab_size = int(model.config.vocab_size)
    story_config.validate(hidden_size, vocab_size)

    layers = _get_decoder_layers(model)
    replaced = 0
    for index, layer in enumerate(list(layers)):
        if isinstance(layer, ShardedMoECeNNReplacementLayer):
            layers[index] = StoryV2ReplacementLayer(layer.cenn, hidden_size, story_config)
            replaced += 1
    if replaced == 0:
        raise RuntimeError("no Sharded MoE-CeNN layer found to upgrade")

    base_head = model.get_output_embeddings()
    if base_head is None:
        raise RuntimeError("base model has no output embedding/head")
    if not isinstance(base_head, LowRankLMHeadAdapter):
        model.set_output_embeddings(
            LowRankLMHeadAdapter(base_head, hidden_size, vocab_size, story_config.head_rank)
        )
    model.config.use_cache = False
    if hasattr(model, "generation_config"):
        model.generation_config.use_cache = False
    return model


def freeze_story_v2_interfaces(model: nn.Module) -> None:
    for parameter in model.parameters():
        parameter.requires_grad = False
    for module in model.modules():
        if isinstance(module, StoryV2ReplacementLayer):
            for parameter in module.cenn.parameters():
                parameter.requires_grad = True
            for parameter in module.story_memory.parameters():
                parameter.requires_grad = True
        elif isinstance(module, LowRankLMHeadAdapter):
            for parameter in module.down.parameters():
                parameter.requires_grad = True
            for parameter in module.up.parameters():
                parameter.requires_grad = True


def story_v2_router_stats(model: nn.Module) -> dict[str, Tensor]:
    layer = next((m for m in model.modules() if isinstance(m, StoryV2ReplacementLayer)), None)
    if layer is None or not getattr(layer.cenn, "last_router_stats", None):
        raise RuntimeError("Story-v2 router statistics unavailable; run a forward pass first")
    return layer.cenn.last_router_stats


def story_v2_parameter_summary(model: nn.Module) -> dict[str, int | float]:
    total = sum(p.numel() for p in model.parameters())
    trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
    memory = sum(
        p.numel() for m in model.modules() if isinstance(m, CausalStoryMemory) for p in m.parameters()
    )
    head_adapter = sum(
        p.numel()
        for m in model.modules()
        if isinstance(m, LowRankLMHeadAdapter)
        for sub in (m.down, m.up)
        for p in sub.parameters()
    )
    return {
        "total": total,
        "trainable": trainable,
        "memory": memory,
        "head_adapter": head_adapter,
        "trainable_percent": 100.0 * trainable / max(total, 1),
    }


def _story_v2_state_dict(model: nn.Module) -> dict[str, Tensor]:
    state: dict[str, Tensor] = {}
    for name, tensor in model.state_dict().items():
        if ".cenn." in name or ".story_memory." in name:
            state[name] = tensor.detach().cpu()
        elif "lm_head.down." in name or "lm_head.up." in name:
            state[name] = tensor.detach().cpu()
    if not state:
        raise RuntimeError("no Story-v2 trainable state found")
    return state


def save_story_v2_student(
    model: nn.Module,
    output_dir: str | Path,
    *,
    story_config: StoryV2Config,
    sharded_config: ShardedMoECeNNConfig,
    base_model: str = DEFAULT_BASE_MODEL,
    layer_indices: Sequence[int] = (0,),
    extra_metadata: dict | None = None,
) -> Path:
    output_dir = Path(output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)
    torch.save(_story_v2_state_dict(model), output_dir / "story_v2_student.pt")
    metadata = {
        "format_version": 1,
        "architecture": "tinycenn-story-v2-memory-head",
        "base_model": base_model,
        "layer_indices": list(layer_indices),
        "sharded_moe_cenn": sharded_config.to_dict(),
        "story_v2": story_config.to_dict(),
    }
    if extra_metadata:
        metadata["training"] = extra_metadata
    (output_dir / "story_v2_config.json").write_text(
        json.dumps(metadata, indent=2), encoding="utf-8"
    )
    return output_dir


def load_story_v2_weights(model: nn.Module, student_dir: str | Path) -> nn.Module:
    state = torch.load(Path(student_dir) / "story_v2_student.pt", map_location="cpu", weights_only=True)
    incompatible = model.load_state_dict(state, strict=False)
    expected = set(_story_v2_state_dict(model))
    missing = [key for key in incompatible.missing_keys if key in expected]
    if missing:
        raise RuntimeError(f"missing Story-v2 keys: {missing}")
    if incompatible.unexpected_keys:
        raise RuntimeError(f"unexpected Story-v2 keys: {incompatible.unexpected_keys}")
    return model


def build_story_v2_student(
    student_dir: str | Path,
    *,
    device=None,
    dtype=None,
    attn_implementation: str = "sdpa",
):
    from transformers import AutoModelForCausalLM
    from .sharded_moe import replace_transformer_with_sharded_moe_cenn

    student_dir = Path(student_dir)
    metadata = json.loads((student_dir / "story_v2_config.json").read_text())
    if metadata.get("architecture") != "tinycenn-story-v2-memory-head":
        raise ValueError("checkpoint is not a TinyCeNN Story-v2 model")

    kwargs = {"attn_implementation": attn_implementation}
    if dtype is not None:
        kwargs["dtype"] = dtype
    model = AutoModelForCausalLM.from_pretrained(metadata["base_model"], **kwargs)
    sharded_config = ShardedMoECeNNConfig.from_dict(metadata["sharded_moe_cenn"])
    replace_transformer_with_sharded_moe_cenn(model, sharded_config, tuple(metadata["layer_indices"]))
    story_config = StoryV2Config.from_dict(metadata["story_v2"])
    upgrade_sharded_model_to_story_v2(model, story_config)
    load_story_v2_weights(model, student_dir)

    move_kwargs = {}
    if device is not None:
        move_kwargs["device"] = device
    if dtype is not None:
        move_kwargs["dtype"] = dtype
    if move_kwargs:
        model.to(**move_kwargs)
    model.config.use_cache = False
    return model


def build_story_v2_from_story_v1(
    story_v1_dir: str | Path,
    *,
    story_config: StoryV2Config,
    device=None,
    dtype=None,
):
    model = build_sharded_moe_student(story_v1_dir, device=device, dtype=dtype)
    upgrade_sharded_model_to_story_v2(model, story_config)
    freeze_story_v2_interfaces(model)
    return model