File size: 2,190 Bytes
28f1640
 
 
 
 
 
 
 
9015082
 
 
 
 
 
 
 
 
28f1640
 
 
 
 
 
 
 
9015082
 
 
 
 
 
 
 
 
 
28f1640
9015082
28f1640
 
 
 
 
 
9015082
 
 
 
 
 
 
 
28f1640
9015082
 
 
 
 
 
 
 
 
 
 
28f1640
 
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
"""
Custom HuggingFace-compatible wrapper for the DeBERTa-v3-small MCQ scorer
trained in the Kaggle notebook (DeBERTaMCQModel).

This makes the model loadable on the Hub via:
    AutoConfig.from_pretrained(repo_id, trust_remote_code=True)
    AutoModel.from_pretrained(repo_id, trust_remote_code=True)
"""

import torch
import torch.nn as nn
from transformers import PretrainedConfig, PreTrainedModel, AutoModel


class DebertaMCQConfig(PretrainedConfig):
    model_type = "deberta_mcq"

    def __init__(
        self,
        base_model_name="microsoft/deberta-v3-small",
        hidden_dropout=0.3,
        num_options=5,
        max_len=64,
        **kwargs,
    ):
        self.base_model_name = base_model_name
        self.hidden_dropout = hidden_dropout
        self.num_options = num_options
        self.max_len = max_len
        super().__init__(**kwargs)


class DebertaMCQForMultipleChoice(PreTrainedModel):
    config_class = DebertaMCQConfig

    def __init__(self, config: DebertaMCQConfig):
        super().__init__(config)
        self.deberta = AutoModel.from_pretrained(
            config.base_model_name,
            torch_dtype=torch.float32,
            low_cpu_mem_usage=False,
            device_map=None,
        )
        hidden_size = self.deberta.config.hidden_size
        self.classifier = nn.Sequential(
            nn.Dropout(config.hidden_dropout),
            nn.Linear(hidden_size, 1),
        )
        self.post_init()

    def forward(self, input_ids, attention_mask=None, labels=None):
        # input_ids / attention_mask shape: (batch, num_options, seq_len)
        batch_size, num_options, seq_len = input_ids.shape
        input_ids = input_ids.view(-1, seq_len)
        attention_mask = attention_mask.view(-1, seq_len)

        outputs = self.deberta(input_ids=input_ids, attention_mask=attention_mask)
        cls_output = outputs.last_hidden_state[:, 0, :].float()
        scores = self.classifier(cls_output).view(batch_size, num_options)

        loss = None
        if labels is not None:
            loss = nn.CrossEntropyLoss()(scores, labels)

        return {"loss": loss, "logits": scores} if loss is not None else {"logits": scores}