File size: 15,348 Bytes
0d085ac
6851316
0d085ac
6851316
0d085ac
 
6851316
 
 
 
0d085ac
 
 
 
 
 
 
 
a27a735
0d085ac
 
 
a27a735
0d085ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e220065
0d085ac
 
 
e220065
0d085ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e220065
0d085ac
 
 
e220065
0d085ac
 
 
 
 
 
 
 
 
 
 
e220065
0d085ac
 
 
 
 
e220065
0d085ac
d8d40ee
0d085ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e220065
0d085ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e220065
0d085ac
e220065
0d085ac
 
 
a27a735
6e42c14
e220065
 
 
 
 
 
 
 
a27a735
6e42c14
a27a735
 
 
 
 
 
 
 
e220065
0d085ac
 
 
e220065
0d085ac
 
 
 
 
e220065
0d085ac
e220065
0d085ac
e220065
 
0d085ac
e220065
0d085ac
 
 
 
 
e220065
0d085ac
6851316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import torch
import torch.nn as nn
from transformers.models.roberta.modeling_roberta import RobertaModel
from transformers import RobertaConfig, PreTrainedModel
from .embeddings import BoundaryAwareEmbeddings
from .bias_utils import create_bias_matrix
from transformers.modeling_outputs import MaskedLMOutput
from transformers.modeling_outputs import SequenceClassifierOutput



class MorphemeAwareRobertaModel(RobertaModel):
    """
    PhoBERT mở rộng với:
    - BoundaryAwareEmbeddings (BMES + gate)
    - BMES bias hook trên attention head, hỗ trợ batch
    """

    def __init__(self, config, target_heads=None, alpha=0.1, beta=-0.05, gamma=0.0, delta=0.0, block_bmes_emb = False ,**kwargs):
        super().__init__(config, **kwargs)

        self.embeddings = BoundaryAwareEmbeddings(config, **kwargs)
        self.block_bmes_emb = block_bmes_emb

        # Bias params
        self.target_heads = target_heads or {}
        self.alpha = alpha
        self.beta = beta
        self.gamma = gamma
        self.delta = delta

        self.tokenizer = None
        self.patched_forwards = {}
        self.bias_matrix = None

    def set_tokenizer(self, tokenizer):
        assert tokenizer is not None
        self.tokenizer = tokenizer

    def set_bias_matrix(self, bmes_tags):
        """
        bmes_tags: tensor [B, seq_len] hoặc [seq_len]
        Trả về tensor [B, num_heads, seq_len, seq_len]
        """
        if isinstance(bmes_tags, torch.Tensor) and bmes_tags.dim() == 1:
            bmes_tags = bmes_tags.unsqueeze(0)

        batch_size, seq_len = bmes_tags.shape
        bias_np = create_bias_matrix(bmes_tags, alpha=self.alpha, beta=self.beta, gamma=self.gamma, delta=self.delta)
        bias_tensor = torch.tensor(bias_np, dtype=torch.float32, device=next(self.parameters()).device)
        num_heads = self.config.num_attention_heads
        bias_tensor = bias_tensor.unsqueeze(1).repeat(1, num_heads, 1, 1)
        self.bias_matrix = bias_tensor

    def _create_patched_forward(self, layer_idx, head_indices, original_forward, attn_module):
        """
        Tạo forward function mới có cộng bias vào attention scores trước softmax
        """
        def patched_forward(
            hidden_states,
            attention_mask=None,
            head_mask=None,
            encoder_hidden_states=None,
            encoder_attention_mask=None,
            past_key_value=None,
            output_attentions=False,
            **kwargs
        ):
            batch_size, seq_length = hidden_states.shape[:2]
            
            # Tính Q, K, V
            query_layer = attn_module.query(hidden_states)
            
            # Xử lý key và value
            is_cross_attention = encoder_hidden_states is not None
            
            if is_cross_attention:
                key_layer = attn_module.key(encoder_hidden_states)
                value_layer = attn_module.value(encoder_hidden_states)
            elif past_key_value is not None:
                key_layer = attn_module.key(hidden_states)
                value_layer = attn_module.value(hidden_states)
                key_layer = torch.cat([past_key_value[0], key_layer], dim=1)
                value_layer = torch.cat([past_key_value[1], value_layer], dim=1)
            else:
                key_layer = attn_module.key(hidden_states)
                value_layer = attn_module.value(hidden_states)
            
            # Reshape để split heads
            def split_heads(tensor, num_heads, head_dim):
                new_shape = tensor.size()[:-1] + (num_heads, head_dim)
                tensor = tensor.view(new_shape)
                return tensor.permute(0, 2, 1, 3)
            
            num_heads = attn_module.num_attention_heads
            head_dim = attn_module.attention_head_size
            
            query_layer = split_heads(query_layer, num_heads, head_dim)
            key_layer = split_heads(key_layer, num_heads, head_dim)
            value_layer = split_heads(value_layer, num_heads, head_dim)
            
            if hasattr(attn_module, 'is_decoder') and attn_module.is_decoder:
                past_key_value = (key_layer, value_layer)
            
            # Tính attention scores
            attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2))
            attention_scores = attention_scores / torch.sqrt(
                torch.tensor(head_dim, dtype=attention_scores.dtype, device=attention_scores.device)
            )
            
            # ✅ CỘNG BIAS VÀO ĐÂY - TRƯỚC SOFTMAX
            if self.bias_matrix is not None:
                # print("Adding bias matrix")
                B, H, L, _ = attention_scores.shape
                bias = self.bias_matrix
                
                if bias.size(0) != B:
                    bias = bias[:B]
                if bias.size(-1) != L:
                    bias = bias[:, :, :L, :L]
                
                for h in head_indices:
                    if h < H:
                        attention_scores[:, h, :, :] = attention_scores[:, h, :, :] + bias[:, h, :, :]
            
            if attention_mask is not None:
                attention_scores = attention_scores + attention_mask
            
            attention_probs = torch.nn.functional.softmax(attention_scores, dim=-1)
            attention_probs = attn_module.dropout(attention_probs)
            
            if head_mask is not None:
                attention_probs = attention_probs * head_mask
            
            # Tính context layer
            context_layer = torch.matmul(attention_probs, value_layer)
            context_layer = context_layer.permute(0, 2, 1, 3).contiguous()
            new_context_layer_shape = context_layer.size()[:-2] + (attn_module.all_head_size,)
            context_layer = context_layer.view(new_context_layer_shape)
            
            outputs = (context_layer, attention_probs) if output_attentions else (context_layer,)
            
            if hasattr(attn_module, 'is_decoder') and attn_module.is_decoder:
                outputs = outputs + (past_key_value,)
            
            return outputs
        
        return patched_forward

    def _patch_attention_layer(self, layer_idx, head_indices):
        """
        Monkey patch forward method của attention layer
        """
        attn_module = self.encoder.layer[layer_idx].attention.self
        
        if layer_idx not in self.patched_forwards:
            original_forward = attn_module.forward
            self.patched_forwards[layer_idx] = (attn_module, original_forward)
            
            patched_forward = self._create_patched_forward(
                layer_idx, head_indices, original_forward, attn_module
            )
            attn_module.forward = patched_forward

    def prepare_bias_patches(self):
        """
        Patch tất cả các layer có target heads
        """
        self.remove_bias_patches()
        for layer_idx, heads in self.target_heads.items():
            self._patch_attention_layer(layer_idx, heads)

    def remove_bias_patches(self):
        """
        Khôi phục lại original forward methods
        """
        for layer_idx, (attn_module, original_forward) in self.patched_forwards.items():
            attn_module.forward = original_forward
        self.patched_forwards = {}

    def forward(
        self,
        input_ids=None,
        attention_mask=None,
        token_type_ids=None,
        position_ids=None,
        head_mask=None,
        inputs_embeds=None,
        bmes_ids=None,
        bmes_tags=None,
        output_attentions=None,
        output_hidden_states=None,
        return_dict=None,
        past_key_values_length=0,  # ✅ Thêm param này cho embedding layer
    ):
        # Xử lý bmes_ids/bmes_tags
        if bmes_ids is None and bmes_tags is not None:
            bmes_ids = bmes_tags

        if inputs_embeds is None and self.block_bmes_emb == False:
            # print("Using bmes embeddings")
            inputs_embeds = self.embeddings(
                input_ids=input_ids,
                token_type_ids=token_type_ids,
                position_ids=position_ids,
                bmes_ids=bmes_ids,  # ✅ Truyền bmes_ids vào embedding
                past_key_values_length=past_key_values_length
            )

        if self.block_bmes_emb == True:
            # print("Block bmes embeddings")
            inputs_embeds = self.embeddings(
                input_ids=input_ids,
                token_type_ids=token_type_ids,
                position_ids=position_ids,
                bmes_ids=None,  # ✅ Không truyền BMES, chỉ dùng embedding gốc
                past_key_values_length=past_key_values_length
            )

        # Set bias matrix nếu có bmes_ids
        if bmes_ids is not None:
            self.set_bias_matrix(bmes_ids)

        # Patch attention layers nếu có target heads
        if self.target_heads:
            self.prepare_bias_patches()

        output_attentions = True if output_attentions is None else output_attentions

        # ✅ Gọi parent forward NHƯNG truyền inputs_embeds thay vì input_ids
        outputs = super().forward(
            input_ids=None,  # ✅ Set None vì đã có inputs_embeds
            attention_mask=attention_mask,
            token_type_ids=None,  # ✅ Set None vì đã được xử lý trong embedding
            position_ids=None,  # ✅ Set None vì đã được xử lý trong embedding
            head_mask=head_mask,
            inputs_embeds=inputs_embeds,  # ✅ Dùng embedding đã tính
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
        )

        # Cleanup patches
        self.remove_bias_patches()
        return outputs
    

class MorphemeAwareRobertaForMaskedLM(PreTrainedModel):
    """
    HuTieuBert mở rộng cho Masked Language Modeling.
    Hỗ trợ bias attention theo BMES và tham số hóa alpha/beta/gamma.
    """
    config_class = RobertaConfig

    def __init__(
        self,
        config,
        target_heads=None,
        alpha=0.1,
        beta=-0.05,
        gamma=0.0,
        delta=0.0,
    ):
        super().__init__(config)

        # ✅ Truyền tham số xuống MorphemeAwareRobertaModel
        self.roberta = MorphemeAwareRobertaModel(
            config,
            target_heads=target_heads,
            alpha=alpha,
            beta=beta,
            gamma=gamma,
            delta=delta,
        )

        # Head để dự đoán token bị che
        self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)

        # Tie weight: chia sẻ embedding giữa input và output
        self.tie_weights()
        self.init_weights()

    def tie_weights(self):
        self.lm_head.weight = self.roberta.embeddings.word_embeddings.weight

    def forward(
        self,
        input_ids=None,
        attention_mask=None,
        token_type_ids=None,
        position_ids=None,
        head_mask=None,
        inputs_embeds=None,
        bmes_ids=None,
        bmes_tags=None,
        labels=None,
        output_attentions=None,
        output_hidden_states=None,
        return_dict=True,
    ):
        # ✅ Forward qua Roberta backbone có BMES bias
        outputs = self.roberta(
            input_ids=input_ids,
            attention_mask=attention_mask,
            token_type_ids=token_type_ids,
            position_ids=position_ids,
            head_mask=head_mask,
            inputs_embeds=inputs_embeds,
            bmes_ids=bmes_ids,
            bmes_tags=bmes_tags,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
        )

        sequence_output = outputs[0]
        prediction_scores = self.lm_head(sequence_output)

        # ✅ Tính loss nếu có label
        loss = None
        if labels is not None:
            loss_fct = nn.CrossEntropyLoss()
            loss = loss_fct(
                prediction_scores.view(-1, self.config.vocab_size),
                labels.view(-1)
            )

        if not return_dict:
            output = (prediction_scores,) + outputs[1:]
            return ((loss,) + output) if loss is not None else output

        return MaskedLMOutput(
            loss=loss,
            logits=prediction_scores,
            hidden_states=outputs.hidden_states,
            attentions=outputs.attentions,
        )
    

class MorphemeAwareRobertaForSequenceClassification(PreTrainedModel):
    """
    HuTieuBert cho classification tasks.
    Sử dụng MorphemeAwareRobertaModel làm encoder + classification head.
    """
    config_class = RobertaConfig

    def __init__(
        self,
        config,
        num_labels=2,
        target_heads=None,
        alpha=0.1,
        beta=-0.05,
        gamma=0.0,
        delta=0.0,
    ):
        super().__init__(config)
        self.num_labels = num_labels
        self.config = config

        self.roberta = MorphemeAwareRobertaModel(
            config,
            target_heads=target_heads,
            alpha=alpha,
            beta=beta,
            gamma=gamma,
            delta=delta,
        )

        self.classifier = nn.Sequential(
            nn.Dropout(config.hidden_dropout_prob),
            nn.Linear(config.hidden_size, num_labels)
        )

        self.init_weights()

    def forward(
        self,
        input_ids=None,
        attention_mask=None,
        token_type_ids=None,
        position_ids=None,
        head_mask=None,
        inputs_embeds=None,
        bmes_ids=None,
        bmes_tags=None,
        labels=None,
        output_attentions=None,
        output_hidden_states=None,
        return_dict=True,
    ):
        outputs = self.roberta(
            input_ids=input_ids,
            attention_mask=attention_mask,
            token_type_ids=token_type_ids,
            position_ids=position_ids,
            head_mask=head_mask,
            inputs_embeds=inputs_embeds,
            bmes_ids=bmes_ids,
            bmes_tags=bmes_tags,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
        )

        sequence_output = outputs[0]  # [batch_size, seq_len, hidden_size]
        cls_output = sequence_output[:, 0, :]  # [batch_size, hidden_size]

        logits = self.classifier(cls_output)  # [batch_size, num_labels]

        loss = None
        if labels is not None:
            if self.num_labels == 1:
                # Regression
                loss_fct = nn.MSELoss()
                loss = loss_fct(logits.squeeze(), labels.squeeze())
            else:
                # Classification
                loss_fct = nn.CrossEntropyLoss()
                loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))

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

        return SequenceClassifierOutput(
            loss=loss,
            logits=logits,
            hidden_states=outputs.hidden_states,
            attentions=outputs.attentions,
        )