File size: 6,297 Bytes
326e148
 
2625b05
326e148
 
 
 
2625b05
 
326e148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54ed2c9
326e148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2625b05
326e148
 
 
 
 
 
 
 
 
2625b05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326e148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2625b05
 
 
326e148
2625b05
 
326e148
 
 
 
 
 
 
 
 
 
 
 
 
2625b05
 
 
 
 
 
326e148
 
 
2625b05
326e148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2625b05
 
 
 
 
 
 
326e148
2625b05
 
 
 
 
 
 
326e148
 
 
 
2625b05
 
 
 
 
 
 
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
import torch
import torch.nn as nn
from transformers import PreTrainedModel, AutoConfig, AutoModel

from .configuration_emcoder import EmCoderConfig


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.pos_embedding = nn.Embedding(config.max_seq_len, config.d_model)
        self.embed_norm = nn.LayerNorm(config.d_model)

        encoder_layer = nn.TransformerEncoderLayer(
            d_model=config.d_model,
            nhead=config.n_head,
            dim_feedforward=config.d_ffn,
            dropout=config.dropout,
            activation="gelu",
            norm_first=True,
            batch_first=True,
        )
        self.encoder = nn.TransformerEncoder(
            encoder_layer=encoder_layer, num_layers=config.n_layers, enable_nested_tensor=False
        )

        self.final_norm = nn.LayerNorm(config.d_model)
        self.dropout = nn.Dropout(config.dropout)

    def forward(self, x: torch.Tensor, mask: torch.Tensor) -> torch.Tensor:
        """Standard forward pass through the encoder."""
        seq_len = x.size(1)
        pos_ids = torch.arange(seq_len, device=x.device).unsqueeze(0)

        x = self.token_embedding(x) + self.pos_embedding(pos_ids)

        x = self.embed_norm(x)
        x = self.dropout(x)

        padding_mask = mask == 0

        encoded = self.encoder(x, src_key_padding_mask=padding_mask)
        return self.final_norm(encoded)


class EmCoder(PreTrainedModel):
    """The full EmCoder model, including 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):
            nn.init.trunc_normal_(module.weight, std=0.02)
            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)
            if hasattr(module, "padding_idx") and module.padding_idx is not None:
                module.weight.data[module.padding_idx].zero_()
        elif isinstance(module, nn.LayerNorm):
            nn.init.ones_(module.weight)
            nn.init.zeros_(module.bias)



    def _set_mc_dropout(self, active: bool = True):
        for m in self.modules():
            if isinstance(m, nn.Dropout) or isinstance(m, nn.MultiheadAttention):
                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,
        n_samples: int = 10,
        max_batch_size: int | None = None,
        return_dict: bool | None = None,
        **kwargs,
    ) -> torch.Tensor:
        """
        Performs Monte Carlo Dropout inference to quantify epistemic uncertainty.

        Args:
            x: Input token IDs of shape (B, S).
            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).
        """
        x = input_ids if input_ids is not None else kwargs.get("x")
        mask = attention_mask if attention_mask is not None else kwargs.get("mask")
        
        if x is None or mask is None:
            raise ValueError("input_ids (x) and attention_mask (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:
            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)

        return all_logits




    def forward(
        self,
        input_ids: torch.Tensor | None = None,
        attention_mask: torch.Tensor | None = None,
        return_dict: bool | None = None,
        **kwargs,
    ) -> torch.Tensor:
        """Standard forward pass without MC Dropout."""

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

        features = self.encoder(x, mask)

        pooled = self._masked_mean_pooling(features, mask)
        return self.classifier(pooled)


try:
    AutoConfig.register("emcoder", EmCoderConfig)
    AutoModel.register(EmCoderConfig, EmCoder)
except ValueError:
    pass