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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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
"""Tap plans: several hidden-state outputs from one forward pass per batch.

A tap names one hidden state and what to keep from it. ``HiddenTap`` keeps the rows the mask selects
(biological residues in a residue run; CLS, residues and EOS in a token run) or pools them
(``Pooler`` in a residue run, ``pool_token_rows`` in a token run). ``ReducedTap`` hands the state to a
caller's reducer, such as a sparse-autoencoder encoder and its pooling. ``StreamingTap`` reduces layers
as they arrive without saving their full hidden states. A plan runs one forward pass per
batch that stops once the deepest tapped state exists.

Layer indices follow the FastPLMs hidden-state order: index ``i`` is the input to block ``i``,
index ``n`` (the block count) is the final normalized state, and negative indices count back
from it, so ``-1`` is the final state.

Symbols: b sequences of a batch; l token columns of the padded batch (CLS, residues, EOS, padding); d hidden
width; n attended rows of a batch; r residues of one sequence. In a residue run the mask is false on CLS, EOS and
padding, so a sequence is r rows of an ``(n, d)`` output. In a token run (canonical) it is false on padding only, so a
sequence is r + 2 rows (row 0 CLS, rows 1..r residues, row r + 1 EOS) in every ``(n, d)`` output and every pooling.
"""

from __future__ import annotations

import json
import math
import torch

from collections.abc import Callable, Mapping, Sequence
from dataclasses import dataclass, field
from typing import Any, Protocol
from torch import Tensor

from types import MappingProxyType
from .pooling import Pooler
from ..features.layouts import TopKRow


@dataclass(frozen=True, slots=True)
class RowSelection:
    """The rows of a ``(b, l)`` grid that a tap keeps, known on the host so no device value is read.

    ``flat_index`` holds each kept row's position in the row-major ``(b * l)`` grid, ``owner`` the
    sequence it belongs to, and ``counts`` how many rows each sequence keeps. Gathering with an index
    built from host-known lengths never stalls the host the way boolean indexing does.
    """

    flat_index: Tensor  # (n,) int64 on the device, n = sum of counts
    owner: Tensor  # (n,) int64 on the device, the sequence of each kept row
    counts: tuple[int, ...]  # (b,) kept rows per sequence, on the host
    sizes: Tensor  # (b,) int64 on the device, the same counts

    def gather(self, X: Tensor) -> Tensor:
        """The kept rows of ``X`` in sequence order, without padding."""
        # X: (b, l, w) -> (n, w)
        return X.reshape(-1, X.shape[-1]).index_select(0, self.flat_index)  # (n, w)


@dataclass(frozen=True, slots=True)
class TapBatch:
    """One batch of a tapped hidden state, as a ``ReducedTap`` reducer receives it.

    ``X`` has shape ``(b, l, d)``. ``token_mask`` has shape ``(b, l)`` and marks every attended
    token, BOS and EOS included. ``residue_mask`` has shape ``(b, l)`` and marks the rows that
    ``HiddenTap`` keeps and pools: the biological residues, or, when the run keeps the special
    tokens, every attended token (``l`` then counts CLS and EOS). All three share X's device.
    ``rows`` is the host-known selection of ``residue_mask`` when the executor has one, and
    ``cache`` lets taps of one batch share an intermediate such as an SAE encoding.
    """

    X: Tensor
    token_mask: Tensor
    residue_mask: Tensor
    rows: RowSelection | None = None
    cache: dict[Any, Any] = field(default_factory=dict)

    def selection(self) -> RowSelection:
        """The kept rows, taken from the executor when it supplied them, else read from the mask."""
        if self.rows is not None:
            return self.rows
        kept = self.residue_mask.bool()  # (b, l)
        owner, position = kept.nonzero(as_tuple=True)  # (n,), (n,)
        sizes = kept.sum(dim=1)  # (b,)
        return RowSelection(owner * kept.shape[1] + position, owner, tuple(sizes.tolist()), sizes)


@dataclass(frozen=True, slots=True)
class HiddenTap:
    """One hidden state, kept as ragged rows or pooled.

    ``pooling=None`` keeps one ``(r_i, d)`` tensor of mask-selected rows per sequence: ``r_i`` biological
    residues in a residue run, ``l_i + 2`` rows (CLS, residues, EOS) in a token run. Pooler
    names give one pooled vector per sequence over those same rows, concatenated in request order. ``parti`` needs the
    attention graph of a full pass, so a tap rejects it. ``dtype`` overrides the run's dtype
    for this output, starting from the original captured state; None inherits the run dtype.
    """

    name: str
    layer: int
    pooling: str | Sequence[str] | None = None
    dtype: torch.dtype | None = None

    def __post_init__(self) -> None:
        _require_name_and_layer(self.name, self.layer)
        if self.dtype is not None and self.dtype not in (
            torch.float16, torch.bfloat16, torch.float32, torch.float64
        ):
            raise ValueError(
                "A hidden tap dtype must be float16, bfloat16, float32, float64, "
                "or None to inherit the run dtype."
            )
        if self.pooling is None:
            return
        names = Pooler(self.pooling).names  # validates names and rejects duplicates
        if "parti" in names:
            raise ValueError(
                f"Tap {self.name!r} cannot pool with 'parti', which needs the attention graph "
                "of a full forward pass."
            )
        object.__setattr__(self, "pooling", names)


@dataclass(frozen=True, slots=True)
class ReducedTap:
    """One hidden state reduced by a caller's function.

    ``reduce`` maps a ``TapBatch`` to a tensor with one row per sequence, shape ``(b, ...)``.
    ``identity`` describes the reducer in plain data: strings, numbers, booleans, None, lists,
    and string-keyed mappings. The run fingerprint records it, so two reducers with equal
    identities must return equal outputs.
    """

    name: str
    layer: int
    reduce: Callable[[TapBatch], Tensor]
    identity: Mapping[str, Any]

    def __post_init__(self) -> None:
        _require_name_and_layer(self.name, self.layer)
        if not callable(self.reduce):
            raise TypeError(f"Tap {self.name!r} needs a callable reduce.")
        if not isinstance(self.identity, Mapping) or not self.identity:
            raise TypeError(f"Tap {self.name!r} needs a non-empty identity mapping.")
        _require_plain_data(self.identity, f"identity of tap {self.name!r}")
        # A private canonical copy, so later changes to the caller's mapping cannot change the
        # fingerprint of this tap.
        canonical = json.loads(json.dumps(dict(self.identity), sort_keys=True, allow_nan=False))
        object.__setattr__(self, "identity", MappingProxyType(canonical))


@dataclass(frozen=True, slots=True)
class SparseResidueTap:
    """Reduce one state to sparse codes per kept row, in sequence and row order.

    The reducer receives the same masks as dense taps and returns one ``TopKRow`` per sequence.
    Its output must retain every row the mask keeps: the biological residues in a residue run, all
    ``l_i + 2`` attended tokens in a token run. Both integer indices and floating
    values remain sparse through extraction and persistence.
    """

    name: str
    layer: int
    reduce: Callable[[TapBatch], Sequence[TopKRow]]
    identity: Mapping[str, Any]
    codebook_size: int
    sparse_count: int
    # A run that keeps CLS and EOS reads every sequence's codes as one packed (n, k) pair, n = sum(l_i + 2),
    # and splits nothing: the token executor calls this instead of ``reduce``. Not part of the identity.
    reduce_packed: Callable[[TapBatch], TopKRow] | None = None

    def __post_init__(self) -> None:
        checked = ReducedTap(self.name, self.layer, self.reduce, self.identity)
        object.__setattr__(self, "identity", checked.identity)
        if (type(self.codebook_size) is not int or not 1 <= self.codebook_size <= 2**31
                or type(self.sparse_count) is not int
                or not 1 <= self.sparse_count <= self.codebook_size):
            raise ValueError(
                "Sparse residue taps require integer 1 <= sparse_count <= codebook_size <= 2**31."
            )


class LayerAccumulator(Protocol):
    """Batch-local state for a streaming reduction. Never mutate the borrowed hidden state."""

    def update(self, layer: int, batch: TapBatch) -> None: ...

    def finish(self) -> Tensor:
        """Return a token-aligned tensor of shape (b, l, c)."""
        ...


@dataclass(frozen=True, slots=True)
class StreamingTap:
    """Reduce selected layers as they arrive, retaining only the accumulator's own state.

    ``begin`` creates a fresh accumulator for each batch. ``update`` borrows each original
    hidden state, before the run's output dtype conversion, in ascending layer order. ``finish``
    returns token-aligned residue features; the engine applies its biological mask and restores
    input order. Reducers own their arithmetic and describe it in ``identity``. They must not
    retain or mutate borrowed states. No callback is installed on the model between calls.

    ``pooling`` (token runs only) pools the finished rows of each sequence over every attended token, as a
    pooled ``HiddenTap`` does, so one value per sequence is kept instead of one per token; ``dtype`` converts
    the finished rows first (float32 for float32 moments of a 16-bit reducer).
    """

    name: str
    layers: tuple[int, ...]
    begin: Callable[[], LayerAccumulator]
    identity: Mapping[str, Any]
    required_state_count: int | None = None
    pooling: str | Sequence[str] | None = None
    dtype: torch.dtype | None = None

    def __post_init__(self) -> None:
        if self.dtype is not None and self.dtype not in (torch.float16, torch.bfloat16, torch.float32, torch.float64):
            raise ValueError("A streaming tap dtype must be float16, bfloat16, float32, float64, or None.")
        if self.pooling is not None:
            names = Pooler(self.pooling).names  # validates names and rejects duplicates
            if "parti" in names:
                raise ValueError(f"Tap {self.name!r} cannot pool with 'parti', which needs the attention graph.")
            object.__setattr__(self, "pooling", names)
        layers = tuple(self.layers)
        if not layers or any(type(layer) is not int or layer < 0 for layer in layers):
            raise ValueError("Streaming layers must be nonempty nonnegative integer indices.")
        if tuple(sorted(set(layers))) != layers:
            raise ValueError("Streaming layers must be distinct and ascending.")
        object.__setattr__(self, "layers", layers)
        if self.required_state_count is not None and (
            type(self.required_state_count) is not int or self.required_state_count <= layers[-1]
        ):
            raise ValueError("required_state_count must include every streamed layer.")
        # Reuse the reducer identity validation and detached canonical copy.
        checked = ReducedTap(self.name, layers[-1], self.begin, self.identity)
        object.__setattr__(self, "identity", checked.identity)

    @property
    def layer(self) -> int:
        """The deepest required state, for the existing early-stop plan."""
        return self.layers[-1]


Tap = HiddenTap | ReducedTap | StreamingTap | SparseResidueTap


@dataclass(frozen=True, slots=True)
class TapPlan:
    """Validated taps, each layer resolved to a hidden-state index in ``0..n``."""

    taps: tuple[Tap, ...]
    layers: tuple[int, ...]

    @property
    def captured_layers(self) -> tuple[int, ...]:
        """The distinct hidden states the forward pass must record, in ascending order."""

        return tuple(sorted({
            layer for tap, layer in zip(self.taps, self.layers, strict=True)
            if not isinstance(tap, StreamingTap)
        }))

    @property
    def streamed_layers(self) -> tuple[int, ...]:
        return tuple(sorted({
            layer for tap in self.taps if isinstance(tap, StreamingTap) for layer in tap.layers
        }))

    @property
    def deepest_layer(self) -> int:
        """The hidden state after which the forward pass stops."""

        return max(self.layers)

    @property
    def pooling_names(self) -> frozenset[str]:
        """Every pooler name the plan's hidden taps request."""

        return frozenset(
            name
            for tap in self.taps
            if isinstance(tap, HiddenTap) and tap.pooling is not None
            for name in tap.pooling
        )

    def identity(self) -> list[dict[str, Any]]:
        """Every tap in plan order, as the run fingerprint and metadata record it."""

        described: list[dict[str, Any]] = []
        for tap, layer in zip(self.taps, self.layers, strict=True):
            if isinstance(tap, HiddenTap):
                pooling = None if tap.pooling is None else list(tap.pooling)
                described.append(
                    {
                        "name": tap.name,
                        "kind": "hidden",
                        "layer": layer,
                        "pooling": pooling,
                        "dtype": (
                            str(tap.dtype).removeprefix("torch.") if tap.dtype is not None else None
                        ),
                    }
                )
            elif isinstance(tap, StreamingTap):
                record = {
                    "name": tap.name, "kind": "streaming", "layers": list(tap.layers),
                    "identity": dict(tap.identity),
                    "required_state_count": tap.required_state_count,
                }
                if tap.pooling is not None:  # absent for a per-token tap, so its existing fingerprint holds
                    record.update(pooling=list(tap.pooling),
                                  dtype=None if tap.dtype is None else str(tap.dtype).removeprefix("torch."))
                described.append(record)
            elif isinstance(tap, SparseResidueTap):
                described.append({
                    "name": tap.name, "kind": "sparse_residue", "layer": layer,
                    "identity": dict(tap.identity), "codebook_size": tap.codebook_size,
                    "sparse_count": tap.sparse_count,
                })
            else:
                described.append(
                    {
                        "name": tap.name,
                        "kind": "reduced",
                        "layer": layer,
                        "identity": dict(tap.identity),
                    }
                )
        return described


def plan_taps(taps: object, state_count: int) -> TapPlan:
    """Validate ``taps`` against a model that exposes ``state_count`` hidden states.

    ``taps`` is the caller's ``embed_dataset`` argument, checked here rather than trusted.
    """

    if isinstance(taps, (str, bytes)) or not isinstance(taps, Sequence):
        raise TypeError(
            "taps must be a sequence of HiddenTap, ReducedTap, StreamingTap "
            "or SparseResidueTap values."
        )
    if not taps:
        raise ValueError("taps must contain at least one tap.")
    checked: list[Tap] = []
    for tap in taps:
        if not isinstance(tap, (HiddenTap, ReducedTap, StreamingTap, SparseResidueTap)):
            raise TypeError(
                "taps must contain HiddenTap, ReducedTap, StreamingTap or SparseResidueTap values; "
                f"found {type(tap).__name__}."
            )
        checked.append(tap)
    names = [tap.name for tap in checked]
    repeated = sorted({name for name in names if names.count(name) > 1})
    if repeated:
        raise ValueError(f"Tap names must be unique; repeated: {repeated}.")
    layers: list[int] = []
    for tap in checked:
        if isinstance(tap, StreamingTap) and tap.required_state_count not in (None, state_count):
            raise ValueError(
                f"Tap {tap.name!r} requires {tap.required_state_count} hidden states, "
                f"not {state_count}."
            )
        if not -state_count <= tap.layer < state_count:
            raise ValueError(
                f"Tap {tap.name!r} names layer {tap.layer}, outside this model's hidden states "
                f"{-state_count}..{state_count - 1}. Index i is the input to block i, and "
                f"{state_count - 1} or -1 is the final normalized state."
            )
        layers.append(tap.layer % state_count)
    return TapPlan(taps=tuple(checked), layers=tuple(layers))


def _require_name_and_layer(name: object, layer: object) -> None:
    if not isinstance(name, str) or not name:
        raise ValueError("A tap name must be a non-empty string.")
    if not isinstance(layer, int) or isinstance(layer, bool):
        raise TypeError(f"Tap {name!r} layer must be an integer hidden-state index.")


def _require_plain_data(value: object, where: str) -> None:
    """Reject content whose serialized form could differ between runs of one reducer."""

    if value is None or isinstance(value, (str, bool, int)):
        return
    if isinstance(value, float):
        if not math.isfinite(value):
            raise ValueError(f"The {where} holds a non-finite number.")
        return
    if isinstance(value, Mapping):
        for key, item in value.items():
            if not isinstance(key, str):
                raise TypeError(f"The {where} has a non-string key {key!r}.")
            _require_plain_data(item, where)
        return
    if isinstance(value, (list, tuple)):
        for item in value:
            _require_plain_data(item, where)
        return
    raise TypeError(
        f"The {where} holds a {type(value).__name__}. An identity holds only strings, numbers, "
        "booleans, None, lists, and string-keyed mappings, so its fingerprint is stable."
    )


__all__ = [
    "HiddenTap",
    "LayerAccumulator",
    "ReducedTap",
    "RowSelection",
    "SparseResidueTap",
    "StreamingTap",
    "Tap",
    "TapBatch",
    "TapPlan",
    "plan_taps",
]