File size: 10,589 Bytes
3e363e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
273
274
275
276
277
278
279
280
281
282
283
284
285
"""Output heads: answer selection, relation, support, contradiction, confidence.

The selection head is the substantive addition.  Previously ``reason`` returned
*every* path the verifier accepted, so the answer set was "everything reachable"
and exact-match against a specific gold set was unattainable by construction.
Selection is the model's job: :class:`PathSelectionHead` emits a per-path logit
and a learned decision threshold, so the model commits to a subset and is
scored on that commitment.
"""

from __future__ import annotations

from dataclasses import dataclass

import torch
from torch import Tensor, nn


@dataclass(frozen=True)
class SelectionOutput:
    """Per-path selection logits and the resulting boolean decision."""

    logits: Tensor
    keep: Tensor
    threshold: Tensor


class PathSelectionHead(nn.Module):
    """Score candidate paths and decide which ones support the answer.

    The threshold is **query-dependent**: a global bias plus an offset predicted
    from the query and the pooled candidate evidence.  A single global scalar
    cannot express both "this query has one answer" and "this one has seven", and
    the measured symptom was a threshold that drifted without becoming
    discriminative -- selection precision flat near 0.20 while path recall fell
    from 0.761 to 0.368, and a wider beam scoring *worse* than greedy.
    """

    def __init__(self, hidden_dim: int, dropout: float = 0.1) -> None:
        super().__init__()
        self.score = nn.Sequential(
            nn.Linear(hidden_dim * 3, hidden_dim),
            nn.GELU(),
            nn.Dropout(dropout),
            nn.Linear(hidden_dim, hidden_dim // 2),
            nn.GELU(),
            nn.Linear(hidden_dim // 2, 1),
        )
        self.threshold = nn.Parameter(torch.zeros(1))
        self.threshold_offset = nn.Sequential(
            nn.Linear(hidden_dim * 2, hidden_dim // 2),
            nn.GELU(),
            nn.Linear(hidden_dim // 2, 1),
        )

    def forward(
        self, reasoning_states: Tensor, path_states: Tensor, query_state: Tensor
    ) -> SelectionOutput:
        if query_state.ndim == 1:
            query_state = query_state.unsqueeze(0)
        if query_state.shape[0] == 1 and reasoning_states.shape[0] != 1:
            query_state = query_state.expand(reasoning_states.shape[0], -1)
        features = torch.cat((reasoning_states, path_states, query_state), dim=-1)
        logits = self.score(features).squeeze(-1)
        # One threshold per query, from the query plus the pooled evidence.
        context = torch.cat(
            (query_state[:1], path_states.mean(0, keepdim=True)), dim=-1
        )
        threshold = self.threshold + self.threshold_offset(context).reshape(())
        return SelectionOutput(logits, logits > threshold, threshold)


class NextHopHead(nn.Module):
    """Score each candidate transition for beam expansion (PLAN section 14)."""

    def __init__(self, hidden_dim: int, dropout: float = 0.1) -> None:
        super().__init__()
        self.score = nn.Sequential(
            nn.Linear(hidden_dim * 4, hidden_dim),
            nn.GELU(),
            nn.Dropout(dropout),
            nn.Linear(hidden_dim, 1),
        )

    def forward(
        self,
        reasoning_states: Tensor,
        source_states: Tensor,
        relation_states: Tensor,
        target_states: Tensor,
    ) -> Tensor:
        features = torch.cat(
            (reasoning_states, source_states, relation_states, target_states), dim=-1
        )
        return self.score(features).squeeze(-1)


class EntityAnswerHead(nn.Module):
    """Rank candidate answer entities against the reasoning state."""

    def __init__(self, hidden_dim: int) -> None:
        super().__init__()
        self.project = nn.Linear(hidden_dim, hidden_dim)
        self.scale = nn.Parameter(torch.ones(1))

    def forward(self, reasoning_state: Tensor, entity_states: Tensor) -> Tensor:
        if reasoning_state.ndim == 1:
            reasoning_state = reasoning_state.unsqueeze(0)
        projected = self.project(reasoning_state)
        return self.scale * (entity_states @ projected.transpose(-2, -1)).squeeze(-1)


class RelationAnswerHead(nn.Module):
    """Predict the relation that answers a relation-mode query."""

    def __init__(self, hidden_dim: int, num_relations: int) -> None:
        super().__init__()
        self.score = nn.Sequential(
            nn.Linear(hidden_dim * 2, hidden_dim),
            nn.GELU(),
            nn.Linear(hidden_dim, num_relations),
        )

    def forward(self, reasoning_state: Tensor, relation_context: Tensor) -> Tensor:
        return self.score(torch.cat((reasoning_state, relation_context), dim=-1))


class VerificationStack(nn.Module):
    """Shared neural verification trunk consumed by the support-side heads.

    PLAN section 20 describes neural verification as consuming the query, the PAT
    path representation, the graph representation, the candidate answer and the
    constraint state.  That is a real inference problem, not a linear readout, so
    it gets a residual MLP trunk of configurable depth and width whose output is
    shared by the support, contradiction, count and confidence heads.

    Sharing one trunk is also what makes the heads bucket reach its section 6
    budget honestly: the capacity does inference work rather than padding a
    per-head linear layer to hit a number.
    """

    def __init__(
        self,
        hidden_dim: int,
        depth: int = 2,
        width_multiplier: int = 4,
        dropout: float = 0.1,
        num_signals: int = 0,
    ) -> None:
        super().__init__()
        if depth <= 0 or width_multiplier <= 0:
            raise ValueError("verification depth and width_multiplier must be positive")
        if num_signals < 0:
            raise ValueError("num_signals must not be negative")
        inner = hidden_dim * width_multiplier
        self.num_signals = num_signals
        self.input_projection = nn.Linear(hidden_dim * 3, hidden_dim)
        # Scalar evidence summaries, projected and added rather than concatenated so
        # the trunk width is unchanged.  These exist because the pooled evidence mean
        # this trunk otherwise receives is built from *different distributions* at
        # training and inference -- gold positives plus sampled negatives during
        # training, the beam's own candidates at inference -- and the support head
        # learned a rule that does not transfer: it abstained on 25% of answerable
        # examples and only 12.5% of unanswerable ones, worse than chance in the
        # direction that matters.  A signal computed identically in both regimes fixes
        # the mismatch rather than reweighting around it.
        self.signal_projection = (
            nn.Linear(num_signals, hidden_dim) if num_signals else None
        )
        self.blocks = nn.ModuleList(
            [
                nn.Sequential(
                    nn.LayerNorm(hidden_dim),
                    nn.Linear(hidden_dim, inner),
                    nn.GELU(),
                    nn.Dropout(dropout),
                    nn.Linear(inner, hidden_dim),
                )
                for _ in range(depth)
            ]
        )
        self.final_norm = nn.LayerNorm(hidden_dim)

    def forward(
        self,
        reasoning_state: Tensor,
        evidence_state: Tensor,
        query_state: Tensor,
        signals: Tensor | None = None,
    ) -> Tensor:
        states = self.input_projection(
            torch.cat((reasoning_state, evidence_state, query_state), dim=-1)
        )
        if self.signal_projection is not None:
            if signals is None:
                raise ValueError(
                    f"this verification stack expects {self.num_signals} evidence "
                    "signals but none were supplied"
                )
            if signals.ndim == 1:
                signals = signals.unsqueeze(0)
            if signals.shape[-1] != self.num_signals:
                raise ValueError(
                    f"expected {self.num_signals} evidence signals, "
                    f"got {signals.shape[-1]}"
                )
            states = states + self.signal_projection(signals.to(states.dtype))
        for block in self.blocks:
            states = states + block(states)
        return self.final_norm(states)


class SupportHead(nn.Module):
    """Estimate ``P(answer supported | query, evidence, graph)``."""

    def __init__(self, hidden_dim: int) -> None:
        super().__init__()
        self.score = nn.Sequential(
            nn.Linear(hidden_dim, hidden_dim // 2),
            nn.GELU(),
            nn.Linear(hidden_dim // 2, 1),
        )

    def forward(self, verified_state: Tensor) -> Tensor:
        return self.score(verified_state).squeeze(-1)


class ContradictionHead(nn.Module):
    """Four-way status classifier over the PLAN section 19 output states."""

    def __init__(self, hidden_dim: int) -> None:
        super().__init__()
        self.score = nn.Sequential(
            nn.Linear(hidden_dim, hidden_dim // 2),
            nn.GELU(),
            nn.Linear(hidden_dim // 2, 4),
        )

    def forward(self, verified_state: Tensor) -> Tensor:
        return self.score(verified_state)


class CountHead(nn.Module):
    """Predict an aggregate count for count/aggregate answer modes."""

    def __init__(self, hidden_dim: int, max_count: int = 32) -> None:
        super().__init__()
        self.max_count = max_count
        self.score = nn.Sequential(
            nn.Linear(hidden_dim, hidden_dim // 2),
            nn.GELU(),
            nn.Linear(hidden_dim // 2, max_count + 1),
        )

    def forward(self, verified_state: Tensor) -> Tensor:
        return self.score(verified_state)


class ConfidenceHead(nn.Module):
    """Emit a calibrated-in-training confidence logit (PLAN section 47)."""

    def __init__(self, hidden_dim: int) -> None:
        super().__init__()
        self.score = nn.Sequential(
            nn.Linear(hidden_dim + 2, hidden_dim // 2),
            nn.GELU(),
            nn.Linear(hidden_dim // 2, 1),
        )

    def forward(
        self,
        verified_state: Tensor,
        support_logit: Tensor,
        selected_fraction: Tensor,
    ) -> Tensor:
        features = torch.cat(
            (
                verified_state.reshape(1, -1),
                support_logit.reshape(1, -1),
                selected_fraction.reshape(1, -1),
            ),
            dim=-1,
        )
        return self.score(features).squeeze(-1)