Akahsizrr commited on
Commit
5a92ffd
·
verified ·
1 Parent(s): b93c375

Upload alea/heads.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. alea/heads.py +103 -0
alea/heads.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Set readout heads: ISAB (Induced Set Attention Block) + per-option scoring.
2
+
3
+ ISAB (Lee et al. 2019, Set Transformer): options interact through m learned
4
+ inducing points instead of directly with each other — O(mn) instead of O(n^2),
5
+ and permutation-equivariant by construction (no positional encoding on the set).
6
+ """
7
+ import torch
8
+ import torch.nn as nn
9
+
10
+
11
+ class MAB(nn.Module):
12
+ """Multihead Attention Block: X -> X + MHA(LN(X), LN(Y)); X -> X + FFN(LN(X))."""
13
+
14
+ def __init__(self, dim, heads=8, ff_mult=2):
15
+ super().__init__()
16
+ self.attn = nn.MultiheadAttention(dim, heads, batch_first=True)
17
+ self.ln_q = nn.LayerNorm(dim)
18
+ self.ln_kv = nn.LayerNorm(dim)
19
+ self.ff = nn.Sequential(
20
+ nn.Linear(dim, ff_mult * dim), nn.GELU(), nn.Linear(ff_mult * dim, dim)
21
+ )
22
+ self.ln_ff = nn.LayerNorm(dim)
23
+
24
+ def forward(self, x, y, key_padding_mask=None):
25
+ """key_padding_mask: [B, len_y] bool, True = position to ignore."""
26
+ h, _ = self.attn(self.ln_q(x), self.ln_kv(y), self.ln_kv(y),
27
+ key_padding_mask=key_padding_mask, need_weights=False)
28
+ x = x + h
29
+ x = x + self.ff(self.ln_ff(x))
30
+ return x
31
+
32
+
33
+ class ISAB(nn.Module):
34
+ def __init__(self, dim, n_inducing=16, heads=8):
35
+ super().__init__()
36
+ self.inducing = nn.Parameter(torch.randn(1, n_inducing, dim) * 0.02)
37
+ self.mab_i = MAB(dim, heads) # inducing points read the set
38
+ self.mab_o = MAB(dim, heads) # set members read inducing points
39
+
40
+ def forward(self, x, key_padding_mask=None):
41
+ """x: [B, n, d] set embeddings; key_padding_mask: [B, n] True = pad."""
42
+ I = self.inducing.expand(x.size(0), -1, -1)
43
+ H = self.mab_i(I, x, key_padding_mask=key_padding_mask)
44
+ return self.mab_o(x, H)
45
+
46
+
47
+ class SetChoiceHead(nn.Module):
48
+ """Options -> ISAB -> per-option logit -> softmax over the option set."""
49
+
50
+ def __init__(self, dim, n_inducing=16, heads=8, use_set_attn=True):
51
+ super().__init__()
52
+ self.inp = nn.Sequential(nn.LayerNorm(dim), nn.Linear(dim, dim), nn.GELU())
53
+ self.isab = ISAB(dim, n_inducing, heads) if use_set_attn else None
54
+ self.out = nn.Linear(dim, 1)
55
+
56
+ def forward(self, opt_emb, pad_mask=None):
57
+ """opt_emb: [B, n, d]; pad_mask: [B, n] True = padded.
58
+ Returns logits [B, n] with -inf on padding."""
59
+ x = self.inp(opt_emb)
60
+ if self.isab is not None:
61
+ x = self.isab(x, key_padding_mask=pad_mask)
62
+ logits = self.out(x).squeeze(-1)
63
+ if pad_mask is not None:
64
+ logits = logits.masked_fill(pad_mask, float("-inf"))
65
+ return logits
66
+
67
+
68
+ class NoulHead(nn.Module):
69
+ """Question summary -> P(yes)."""
70
+
71
+ def __init__(self, dim):
72
+ super().__init__()
73
+ self.net = nn.Sequential(
74
+ nn.LayerNorm(dim), nn.Linear(dim, dim // 4), nn.GELU(),
75
+ nn.Linear(dim // 4, 1))
76
+
77
+ def forward(self, q_summary):
78
+ return self.net(q_summary).squeeze(-1)
79
+
80
+
81
+ class RegressHead(nn.Module):
82
+ """Question summary -> monotone quantile vector over a bounded range.
83
+
84
+ Predicts a base level plus softplus increments, so quantiles are
85
+ non-decreasing by construction. N_QUANTILES evenly spaced taus.
86
+ """
87
+
88
+ N_TAU = 19 # taus at 0.05, 0.10, ..., 0.95
89
+
90
+ def __init__(self, dim):
91
+ super().__init__()
92
+ self.net = nn.Sequential(
93
+ nn.LayerNorm(dim), nn.Linear(dim, dim // 2), nn.GELU(),
94
+ nn.Linear(dim // 2, self.N_TAU))
95
+
96
+ def forward(self, q_summary):
97
+ raw = self.net(q_summary) # [B, 19]
98
+ base = raw[..., :1]
99
+ inc = torch.nn.functional.softplus(raw[..., 1:])
100
+ return torch.cat([base, base + inc.cumsum(-1)], -1)
101
+
102
+
103
+ TAUS = [0.05 * (i + 1) for i in range(RegressHead.N_TAU)]