File size: 8,869 Bytes
32b542e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import torch
from torch import nn

from uniperceiver.config import configurable
from uniperceiver.config import kfg
from .build import PREDICTOR_REGISTRY
import math
import torch.nn.functional as F

__all__ = ["BasePredictor", "RobertaLMHead","TwoLayerPredictor", "RobertaRegressionHead"]

@PREDICTOR_REGISTRY.register()
class BasePredictor(nn.Module):
    @configurable
    def __init__(
        self,
        *,
        hidden_size: int,
        vocab_size: int,   # include <BOS>/<EOS>
        dropout: float
    ):
        super(BasePredictor, self).__init__()
        self.logits = nn.Linear(hidden_size, vocab_size)
        self.dropout = nn.Dropout(dropout) if dropout > 0.0 else None

    @classmethod
    def from_config(cls, cfg):
        return {
            "hidden_size": cfg.MODEL.DECODER_DIM,
            "vocab_size": cfg.MODEL.VOCAB_SIZE,
            "dropout": cfg.MODEL.PRED_DROPOUT
        }

    @classmethod
    def add_config(cls, cfg):
        pass

    def forward(self, batched_inputs):
        hidden_states = batched_inputs[kfg.G_HIDDEN_STATES]
        if isinstance(hidden_states, list):
            hidden_states = hidden_states[-1]        
        if self.dropout:  
            hidden_states = self.dropout(hidden_states)
        logits = self.logits(hidden_states)
        return { kfg.G_LOGITS: logits }
    
def gelu_accurate(x):
    if not hasattr(gelu_accurate, "_a"):
        gelu_accurate._a = math.sqrt(2 / math.pi)
    return (
        0.5 * x * (1 + torch.tanh(gelu_accurate._a * (x + 0.044715 * torch.pow(x, 3))))
    )


def gelu(x: torch.Tensor) -> torch.Tensor:
    return torch.nn.functional.gelu(x.float()).type_as(x)

@PREDICTOR_REGISTRY.register()
class TwoLayerPredictor(nn.Module):
    @configurable
    def __init__(
        self,
        *,
        hidden_size: int,
        vocab_size: int,   # include <BOS>/<EOS>
        dropout: float
    ):
        super(TwoLayerPredictor, self).__init__()
        
        self.dense = nn.Linear(hidden_size, hidden_size)
        self.activation_fn = gelu
        self.layer_norm = nn.LayerNorm(hidden_size)
        
        self.logits = nn.Linear(hidden_size, vocab_size)
        
        self.dropout = nn.Dropout(dropout) if dropout > 0.0 else None
    
    def replace_logits(self, shared_weights):
        self.logits.weight = shared_weights

    @classmethod
    def from_config(cls, cfg):
        return {
            "hidden_size": cfg.MODEL.DECODER_DIM,
            "vocab_size": cfg.MODEL.VOCAB_SIZE,
            "dropout": cfg.MODEL.PRED_DROPOUT
        }

    @classmethod
    def add_config(cls, cfg):
        pass

    def forward(self, batched_inputs):
        hidden_states = batched_inputs[kfg.G_HIDDEN_STATES]
        if isinstance(hidden_states, list):
            hidden_states = hidden_states[-1]                 
        
        x = self.dense(hidden_states)
        x = self.activation_fn(x)
        x = self.layer_norm(x)
        
        logits = self.logits(x)
        return { kfg.G_LOGITS: logits }


@PREDICTOR_REGISTRY.register()
class RobertaLMHead(nn.Module):
    @configurable
    def __init__(
        self,
        *,
        hidden_size: int,
        vocab_size: int,   # include <BOS>/<EOS>
        dropout: float,
        untie_weight_embedding: bool,
        use_bias: bool,
        share_hidden: bool,
    ):
        super(RobertaLMHead, self).__init__()
        
        
        self.activation_fn = gelu
        
        if untie_weight_embedding is True:
            self.weight = nn.Linear(hidden_size, vocab_size, bias=False).weight
        else:
            self.weight = None
            
        if share_hidden:
            self.dense = None
            self.layer_norm = None
        else:
            self.dense = nn.Linear(hidden_size, hidden_size)
            self.layer_norm = nn.LayerNorm(hidden_size)
        
        if use_bias:
            self.bias = nn.Parameter(torch.zeros(vocab_size))
        else:
            self.bias = None
        
        self.dropout = nn.Dropout(dropout) if dropout > 0.0 else None
        # print("dropout: {}".format(self.dropout))
    
    def replace_weight(self, weight):
        if self.weight is None:
            self.weight = weight
        else:
            print('already has weight, please set UNTIE_WEIGHT_EMBEDDING to False')
            
    def replace_module_hidden(self, dense, layer_norm):
        if (self.dense is None) and (self.layer_norm is None):
            self.dense = dense
            self.layer_norm = layer_norm
        else:
            print('already has hidden layers!')
            raise ValueError
        
            
    @classmethod
    def from_config(cls, cfg):
        return {
            "hidden_size": cfg.MODEL.DECODER_DIM,
            "vocab_size": cfg.MODEL.VOCAB_SIZE,
            "dropout": cfg.MODEL.PRED_DROPOUT,
            "untie_weight_embedding": cfg.MODEL.UNTIE_WEIGHT_EMBEDDING,
            "use_bias": cfg.MODEL.USE_PREDICTOR_BIAS,
            "share_hidden": cfg.MODEL.SHARE_PREDICTOR_HIDDEN,
        }

    @classmethod
    def add_config(cls, cfg):
        pass

    def forward(self, batched_inputs):
        
        if kfg.G_HIDDEN_STATES in batched_inputs:
            hidden_states = batched_inputs[kfg.G_HIDDEN_STATES]
            if isinstance(hidden_states, list):
                hidden_states = hidden_states[-1]  
                               
            if kfg.G_TARGET_IDS in batched_inputs:
                mask_tokens = batched_inputs[kfg.G_TARGET_IDS].ne(-1) 
                hidden_states = hidden_states[mask_tokens]  
                batched_inputs[kfg.G_TARGET_IDS] = batched_inputs[kfg.G_TARGET_IDS][mask_tokens] 
            logits = self._forward(hidden_states)
            
            return { kfg.G_LOGITS: logits }

        elif kfg.U_HIDDEN_STATES in batched_inputs:
            hidden_states = batched_inputs[kfg.U_HIDDEN_STATES]
            if isinstance(hidden_states, list):
                hidden_states = hidden_states[-1]
            
            mask_tokens = batched_inputs[kfg.U_TARGET_IDS].ne(-1) 
            hidden_states = hidden_states[mask_tokens]  
            batched_inputs[kfg.U_TARGET_IDS] = batched_inputs[kfg.U_TARGET_IDS][mask_tokens]         
            logits = self._forward(hidden_states)
        
            return { kfg.U_LOGITS: logits }

    def _forward(self, x):
        x = self.dense(x)
        x = self.activation_fn(x)
        x = self.layer_norm(x)
        
        if self.dropout:  
            x = self.dropout(x)
        if self.bias is not None:
            logits = F.linear(x, self.weight) + self.bias
        else:
            logits = F.linear(x, self.weight)
        return logits

@PREDICTOR_REGISTRY.register()
class RobertaRegressionHead(nn.Module):
    @configurable
    def __init__(
        self, 
        *,
        hidden_size,
        feat_dim,
        transform,
        sigmoid
    ):
        super(RobertaRegressionHead, self).__init__()
        self.transform = transform
        self.decoder = nn.Linear(hidden_size, feat_dim)
        self.output_sigmoid = sigmoid
        
        
    @classmethod
    def from_config(cls, cfg):
        return {
            "hidden_size": cfg.MODEL.DECODER_DIM,
            "feat_dim":  cfg.MODEL.LABELS_NUM,
            "sigmoid":  cfg.MODEL.SIGMOID,
            "transform": BertPooler(cfg)
        }

    @classmethod
    def add_config(cls, cfg):
        pass
    
    def test_forward(self, u_logits):
        # for Single stream similarity
        return { kfg.OUTPUT: u_logits }

    def forward(self, batched_inputs):
        ret = {}
        if kfg.G_HIDDEN_STATES in batched_inputs:
            hidden_states = batched_inputs[kfg.G_HIDDEN_STATES]
            if isinstance(hidden_states, list):
                hidden_states = hidden_states[-1]    
            hidden_states = self.transform(hidden_states)
            logits = self.decoder(hidden_states)   
            if self.output_sigmoid:
                logits = torch.sigmoid(logits)
            ret.update({ kfg.G_LOGITS: logits })          
            if not self.training:
                ret_test = self.test_forward(logits)
                ret.update(ret_test)
            return ret

        elif kfg.U_HIDDEN_STATES in batched_inputs:
            hidden_states = batched_inputs[kfg.U_HIDDEN_STATES]
            if isinstance(hidden_states, list):
                hidden_states = hidden_states[-1]    
            hidden_states = self.transform(hidden_states)
            logits = self.decoder(hidden_states)      
            if self.output_sigmoid:
                logits = torch.sigmoid(logits)       
            ret.update({ kfg.U_LOGITS: logits })          
            if not self.training:
                ret_test = self.test_forward(logits)
                ret.update(ret_test)
            return ret