File size: 7,653 Bytes
906715b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""attach / AttachHandle / detach — the runtime verbs.

attach(model, anchor) wraps every bound decoder layer; adapters follow
their block's device (device_map="auto" compatible). detach(verify=True)
restores the retained layers and asserts BIT-EXACT equality with a
pre-attach probe (fp32; dtype-conditional under bf16).
"""
from __future__ import annotations

import contextlib
from typing import Sequence

import torch
import torch.nn as nn

from ..binding.resolver import resolve
from ..core.adapter import AdapterSpec, BlockWithAdapter, RelayPatchwork
from ..core.dispatch import AnchorDispatch, BlockWithDispatch
from ..io.checkpoint import (AnchorCheckpoint, DispatchCheckpoint,
                             load_anchor, load_dispatch)


def _per_block_state(adapters: dict, i: int) -> dict:
    pre = f"{i}."
    return {k[len(pre):]: v for k, v in adapters.items()
            if k.startswith(pre)}


def _probe(model, binding) -> torch.Tensor:
    """Deterministic logits fingerprint on a tiny fixed input."""
    dev = next(model.parameters()).device
    ids = torch.arange(8, dtype=torch.long, device=dev).unsqueeze(0) % 7 + 1
    with torch.no_grad():
        out = model(input_ids=ids)
    logits = out.logits if hasattr(out, "logits") else out[0]
    return logits.detach().float().cpu()


class AttachHandle:
    def __init__(self, model, binding, original_layers, names,
                 per_block_modules, fingerprint):
        self.model = model
        self.binding = binding
        self._original = original_layers
        self.names = list(names)
        self._blocks = per_block_modules   # BlockWithAdapter|BlockWithDispatch list
        self._fingerprint = fingerprint

    # -- masking -------------------------------------------------------
    def set_mask(self, mask: dict[str, bool]) -> None:
        enabled = [mask.get(n, True) for n in self.names]
        for b in self._blocks:
            if isinstance(b, BlockWithDispatch):
                b.disp.enabled = list(enabled)
            else:
                b.enabled = enabled[0]

    def enable(self, *names):
        self.set_mask({n: (n in names or not names) for n in self.names})

    def disable(self, *names):
        self.set_mask({n: n not in names for n in self.names})

    @contextlib.contextmanager
    def only(self, *names):
        prev = self._snapshot()
        self.set_mask({n: n in names for n in self.names})
        try:
            yield self
        finally:
            self._restore(prev)

    @contextlib.contextmanager
    def all_off(self):
        prev = self._snapshot()
        self.set_mask({n: False for n in self.names})
        try:
            yield self
        finally:
            self._restore(prev)

    def _snapshot(self):
        out = []
        for b in self._blocks:
            out.append(list(b.disp.enabled)
                       if isinstance(b, BlockWithDispatch) else b.enabled)
        return out

    def _restore(self, snap):
        for b, s in zip(self._blocks, snap):
            if isinstance(b, BlockWithDispatch):
                b.disp.enabled = list(s)
            else:
                b.enabled = s

    # -- telemetry -----------------------------------------------------
    def telemetry(self, on: bool) -> None:
        for b in self._blocks:
            if isinstance(b, BlockWithDispatch):
                b.disp.rec = [] if on else None

    def usage(self) -> dict[str, float]:
        import torch as _t
        ids = [b.disp._last_shadow.flatten() for b in self._blocks
               if isinstance(b, BlockWithDispatch)
               and b.disp._last_shadow is not None]
        if not ids:
            return {}
        counts = _t.bincount(_t.cat(ids), minlength=len(self.names)).float()
        p = counts / counts.sum()
        return {n: round(float(v), 4) for n, v in zip(self.names, p)}

    def amplitude(self) -> dict[str, float]:
        recs = [b.disp.rec for b in self._blocks
                if isinstance(b, BlockWithDispatch) and b.disp.rec]
        if not recs:
            return {}
        amp = torch.stack([torch.stack(r).mean(0) for r in recs]).mean(0)
        return {n: round(float(a), 5) for n, a in zip(self.names, amp)}

    # -- detach --------------------------------------------------------
    def detach(self, *, verify: bool = True) -> nn.Module:
        self.binding.set_layers(self.model, list(self._original))
        if verify:
            post = _probe(self.model, self.binding)
            if not torch.equal(post, self._fingerprint):
                raise RuntimeError(
                    "detach verification FAILED: post-detach logits are "
                    "not bit-exact with the pre-attach fingerprint")
        return self.model


def attach(model, anchors, dispatch=None, *, binding=None,
           spec: AdapterSpec | None = None,
           strict: bool = True) -> AttachHandle:
    b = resolve(model, binding)
    layers = list(b.layers(model))
    d = b.hidden_size(model)
    fingerprint = _probe(model, b)

    def _as_ckpt(a):
        if isinstance(a, AnchorCheckpoint):
            return a
        return load_anchor(a)

    single = not isinstance(anchors, (list, tuple))
    anchor_list = [anchors] if single else list(anchors)
    ckpts = [_as_ckpt(a) for a in anchor_list]
    names = [c.meta.get("name", f"anchor{i}")
             for i, c in enumerate(ckpts)]

    if strict:
        for c in ckpts:
            bid = c.meta.get("base_model_id")
            live = getattr(model.config, "_name_or_path", None)
            if bid and live and bid not in str(live):
                raise ValueError(
                    f"anchor '{c.meta.get('name')}' was trained on "
                    f"{bid}, live model is {live} (pass strict=False "
                    "to override)")

    blocks = []
    if single and dispatch is None:
        ck = ckpts[0]
        for i, layer in enumerate(layers):
            st = _per_block_state(ck.adapters, i)
            if not st:
                blocks.append(layer)
                continue
            a = RelayPatchwork(d, spec)
            a.load_state_dict(st)
            a.to(next(layer.parameters()).device)   # device-following
            wrapped = BlockWithAdapter(layer, a)
            blocks.append(wrapped)
    else:
        if dispatch is None:
            raise ValueError("multiple anchors require a dispatch "
                             "(align one, or pass dispatch='init')")
        disp_ck = (None if dispatch == "init"
                   else dispatch if isinstance(dispatch, DispatchCheckpoint)
                   else load_dispatch(dispatch))
        for i, layer in enumerate(layers):
            stack = nn.ModuleList()
            for ck in ckpts:
                a = RelayPatchwork(d, spec)
                a.load_state_dict(_per_block_state(ck.adapters, i))
                for p in a.parameters():
                    p.requires_grad_(False)
                stack.append(a)
            dev = next(layer.parameters()).device
            dp = AnchorDispatch(stack.to(dev), d).to(dev)
            if disp_ck is not None:
                dp.load_state_dict(disp_ck.dispatch[i], strict=False)
            blocks.append(BlockWithDispatch(layer, dp))

    b.set_layers(model, blocks)
    wrapped_blocks = [x for x in blocks
                      if isinstance(x, (BlockWithAdapter,
                                        BlockWithDispatch))]
    return AttachHandle(model, b, layers, names, wrapped_blocks,
                        fingerprint)


def detach(handle: AttachHandle, *, verify: bool = True) -> nn.Module:
    return handle.detach(verify=verify)