File size: 1,822 Bytes
f78d5a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Shared candidate scorer; checkpoint parameter names are preserved."""

from torch import nn

TARGETS = [
    "q_proj",
    "k_proj",
    "v_proj",
    "o_proj",
    "gate_proj",
    "up_proj",
    "down_proj",
    "in_proj_qkv",
    "in_proj_z",
    "in_proj_a",
    "in_proj_b",
    "out_proj",
]


class CandidateHead(nn.Module):
    def __init__(self, hidden=1024, width=256, comparison=False):
        super().__init__()
        self.width = width
        self.comparison = comparison
        # Common layers are initialized in the same order for both variants.
        self.norm = nn.LayerNorm(hidden)
        self.project = nn.Linear(hidden, width)
        self.ff_norm = nn.LayerNorm(width)
        self.ff = nn.Sequential(
            nn.Linear(width, width * 2), nn.GELU(), nn.Linear(width * 2, width)
        )
        self.final_norm = nn.LayerNorm(width)
        self.scorer = nn.Linear(width, 1)
        if comparison:
            self.set_norm = nn.LayerNorm(width)
            self.attention = nn.MultiheadAttention(
                width, 4, dropout=0.0, batch_first=True
            )
            # Start as the independent head; learn interactions without perturbing its initial logits.
            nn.init.zeros_(self.attention.out_proj.weight)
            nn.init.zeros_(self.attention.out_proj.bias)

    def forward(self, h, valid, return_features=False):
        u = self.project(self.norm(h.float()))
        if self.comparison:
            q = self.set_norm(u)
            delta, _ = self.attention(
                q, q, q, key_padding_mask=~valid, need_weights=False
            )
            u = u + delta
        u = u + self.ff(self.ff_norm(u))
        u = self.final_norm(u)
        z = self.scorer(u).squeeze(-1).masked_fill(~valid, -1e9)
        return (z, u) if return_features else z