File size: 10,182 Bytes
a10898b
 
 
 
1085007
a10898b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63fe031
a10898b
1085007
 
a10898b
1085007
 
 
a10898b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1085007
 
 
a10898b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63fe031
a10898b
1085007
 
a10898b
 
1085007
a10898b
 
 
 
 
 
 
 
 
 
1085007
 
a10898b
 
 
 
 
 
 
 
 
 
b0c4b27
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
286
287
288
289
290
291
292
293
294
295
296
297
298
import torch
import torch.nn as nn
import torch.nn.functional as F
from .rope_embeddings import RotaryEmbedding
from transformers import PreTrainedModel, AutoConfig, AutoModelForSequenceClassification
from transformers.modeling_outputs import SequenceClassifierOutput

from .configuration_emcoder import EmCoderConfig


class RMSNorm(nn.Module):
    def __init__(self, dim: int, eps: float = 1e-6):
        super().__init__()
        self.eps = eps
        self.weight = nn.Parameter(torch.ones(dim))

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        variance = x.pow(2).mean(-1, keepdim=True)
        return x * torch.rsqrt(variance + self.eps) * self.weight


class SwiGLU(nn.Module):
    def __init__(self, d_model: int, d_ffn: int):
        super().__init__()
        self.wi = nn.Linear(d_model, 2 * d_ffn, bias=False)
        self.wo = nn.Linear(d_ffn, d_model, bias=False)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        x1, x2 = self.wi(x).chunk(2, dim=-1)
        return self.wo(x1 * F.silu(x2))




class EmCoderEncoderLayer(nn.Module):
    """Custom Pre-LN Transformer Encoder Layer with RoPE and FlashAttention."""

    def __init__(self, config: EmCoderConfig, rope: RotaryEmbedding):
        super().__init__()
        self.n_head = config.n_head
        self.d_head = config.d_model // config.n_head
        self.rope = rope

        # Attention projections
        self.q_proj = nn.Linear(config.d_model, config.d_model, bias=False)
        self.k_proj = nn.Linear(config.d_model, config.d_model, bias=False)
        self.v_proj = nn.Linear(config.d_model, config.d_model, bias=False)
        self.out_proj = nn.Linear(config.d_model, config.d_model, bias=False)

        self.ln1 = RMSNorm(config.d_model)
        self.ln2 = RMSNorm(config.d_model)

        self.ffn = SwiGLU(config.d_model, config.d_ffn)

        self.dropout = nn.Dropout(config.dropout)

        # mark for initialization
        self.out_proj._is_residual = True
        self.ffn.wo._is_residual = True

    def forward(self, x: torch.Tensor, attn_mask: torch.Tensor) -> torch.Tensor:
        # MULTI-HEAD ATTENTION
        residual = x
        nx = self.ln1(x)
        B, S, _ = nx.shape

        # Projections -> (B, H, S, D_head)
        q = self.q_proj(nx).view(B, S, self.n_head, self.d_head).transpose(1, 2)
        k = self.k_proj(nx).view(B, S, self.n_head, self.d_head).transpose(1, 2)
        v = self.v_proj(nx).view(B, S, self.n_head, self.d_head).transpose(1, 2)

        q = self.rope.rotate_queries_or_keys(q)
        k = self.rope.rotate_queries_or_keys(k)

        attn_out = F.scaled_dot_product_attention(
            q,
            k,
            v,
            attn_mask=attn_mask,
            dropout_p=self.dropout.p if self.dropout.training else 0.0,
        )

        # Join heads -> (B, S, D_model)
        attn_out = attn_out.transpose(1, 2).contiguous().view(B, S, -1)
        x = residual + self.dropout(self.out_proj(attn_out))

        x = x + self.dropout(self.ffn(self.ln2(x)))
        return x


class EmCoderEncoder(nn.Module):
    """The core encoder architecture of EmCoder Transformer."""

    def __init__(self, config: EmCoderConfig):
        super().__init__()
        self.token_embedding = nn.Embedding(config.vocab_size, config.d_model)
        self.embed_norm = RMSNorm(config.d_model)
        self.dropout = nn.Dropout(config.dropout)

        self.rope = RotaryEmbedding(dim=config.d_model // config.n_head)

        self.layers = nn.ModuleList(
            [EmCoderEncoderLayer(config, self.rope) for _ in range(config.n_layers)]
        )

        self.final_norm = RMSNorm(config.d_model)

    def forward(self, x: torch.Tensor, mask: torch.Tensor) -> torch.Tensor:
        """Standard forward pass through the encoder."""
        x = self.token_embedding(x)
        x = self.embed_norm(x)
        x = self.dropout(x)

        B, S = mask.shape
        attn_mask = mask.view(B, 1, 1, S).to(dtype=torch.bool)

        for layer in self.layers:
            x = layer(x, attn_mask)

        return self.final_norm(x)



class EmCoder(PreTrainedModel):
    """The full EmCoder model, including the backbone encoder and the classification head."""

    config_class = EmCoderConfig

    def __init__(self, config: EmCoderConfig):
        super().__init__(config)

        self.encoder = EmCoderEncoder(config)

        self.classifier = nn.Sequential(
            nn.Linear(config.d_model, config.d_model),
            nn.GELU(),
            nn.Dropout(config.dropout),
            nn.Linear(config.d_model, config.num_labels),
        )

        self.post_init()

    
    def _init_weights(self, module: nn.Module) -> None:
        if isinstance(module, nn.Linear):
            # scale down the init for residual connections
            if getattr(module, "_is_residual", False):
                std = 0.02 / ((2 * self.config.n_layers) ** 0.5)
            else:
                std = 0.02

            nn.init.trunc_normal_(module.weight, std=std)
            if module.bias is not None:
                nn.init.zeros_(module.bias)

        elif isinstance(module, nn.Embedding):
            nn.init.trunc_normal_(module.weight, std=0.02)

        elif isinstance(module, RMSNorm):
            nn.init.ones_(module.weight)



    def _set_mc_dropout(self, active: bool = True):
        for m in self.modules():
            if isinstance(m, nn.Dropout):
                m.train(active)


    @staticmethod
    def _masked_mean_pooling(
        features: torch.Tensor, mask: torch.Tensor
    ) -> torch.Tensor:
        mask = mask.unsqueeze(-1)  # (B, S, 1)
        masked_features = features * mask  # (B, S, D)
        sum_masked_features = masked_features.sum(dim=1)  # (B, D)
        count_tokens = torch.clamp(mask.sum(dim=1), min=1e-9)  # (B, 1)
        return sum_masked_features / count_tokens  # (B, D)


    def mc_forward(
        self,
        input_ids: torch.Tensor | None = None,
        attention_mask: torch.Tensor | None = None,
        labels: torch.Tensor | None = None,
        n_samples: int = 10,
        max_batch_size: int | None = None,
        return_dict: bool | None = None,
        **kwargs,
    ) -> tuple[torch.Tensor, ...] | SequenceClassifierOutput:
        """
        Performs Monte Carlo Dropout inference to quantify uncertainty.

        Args:
            input_ids: Input token IDs of shape (B, S).
            attention_mask: Attention mask of shape (B, S).
            n_samples: Total number of Monte Carlo samples.
            max_batch_size: Maximum number of samples in one forward pass.

        Returns:
            Logits of shape (n_samples, B, num_labels).
        """
        return_dict = return_dict if return_dict is not None else True

        x = input_ids if input_ids is not None else kwargs.get("input_ids")
        mask = attention_mask if attention_mask is not None else kwargs.get("attention_mask")

        if x is None or mask is None:
            raise ValueError("input_ids and attention_mask must be provided")
        
        if max_batch_size is None:
            max_batch_size = n_samples


        B, S = x.shape
        num_labels = self.classifier[-1].out_features

        all_logits = torch.empty((n_samples, B, num_labels), device=x.device)

        is_training = self.training
        self._set_mc_dropout(active=True)
        try:
            with torch.no_grad():
                for i in range(0, n_samples, max_batch_size):
                    batch_samples = min(max_batch_size, n_samples - i)

                    x_stacked = x.repeat(batch_samples, 1) # (batch_samples * B, S)
                    mask_stacked = mask.repeat(batch_samples, 1) # (batch_samples * B, S)

                    features = self.encoder(
                        x_stacked, mask_stacked
                    )  # (batch_samples * B, S, D)

                    pooled = self._masked_mean_pooling(features, mask_stacked)
                    logits = self.classifier(pooled)  # (n_samples * B, num_labels)

                    all_logits[i : i + batch_samples] = logits.view(batch_samples, B, -1)
        finally:
            self._set_mc_dropout(active=is_training)

        loss = None
        if labels is not None:
            loss_fct = nn.BCEWithLogitsLoss()
            logits_mean = all_logits.mean(dim=0)  # (B, num_labels)
            target_labels = labels.to(dtype=all_logits.dtype).view(logits_mean.shape)
            loss = loss_fct(logits_mean, target_labels)

        if not return_dict:
            output = (all_logits,)
            return ((loss,) + output) if loss is not None else output

        return SequenceClassifierOutput(
            loss=loss,
            logits=all_logits,
            hidden_states=None,
            attentions=None,
        )


    def forward(
        self,
        input_ids: torch.Tensor | None = None,
        attention_mask: torch.Tensor | None = None,
        labels: torch.Tensor | None = None,
        return_dict: bool | None = None,
        **kwargs,
    ) -> tuple[torch.Tensor, ...] | SequenceClassifierOutput:
        """Standard forward pass without MC Dropout."""
        return_dict = return_dict if return_dict is not None else True

        x = input_ids if input_ids is not None else kwargs.get("input_ids")
        mask = attention_mask if attention_mask is not None else kwargs.get("attention_mask")
        
        if x is None or mask is None:
            raise ValueError("input_ids and attention_mask must be provided")

        features = self.encoder(x, mask)

        pooled = self._masked_mean_pooling(features, mask)

        logits = self.classifier(pooled)

        loss = None
        if labels is not None:
            loss_fct = nn.BCEWithLogitsLoss()
            target_labels = labels.to(dtype=logits.dtype).view(logits.shape)
            loss = loss_fct(logits, target_labels)

        if not return_dict:
            output = (logits,)
            return ((loss,) + output) if loss is not None else output

        return SequenceClassifierOutput(
            loss=loss,
            logits=logits,
            hidden_states=None,
            attentions=None,
        )