File size: 3,574 Bytes
f36c5fb
710d085
f36c5fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e92568
43f6405
f36c5fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43f6405
f36c5fb
a3e8ff1
3132114
 
 
a3e8ff1
8560bf5
 
 
 
 
 
 
f36c5fb
3f45a8d
2201866
264503f
 
 
 
 
 
 
 
2201866
264503f
05158d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f36c5fb
 
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
from transformers import PretrainedConfig
import torch

class ImpressoConfig(PretrainedConfig):
    model_type = "stacked_bert"

    def __init__(
        self,
        vocab_size=30522,
        hidden_size=768,
        num_hidden_layers=12,
        num_attention_heads=12,
        intermediate_size=3072,
        hidden_act="gelu",
        hidden_dropout_prob=0.1,
        attention_probs_dropout_prob=0.1,
        max_position_embeddings=512,
        type_vocab_size=2,
        initializer_range=0.02,
        layer_norm_eps=1e-12,
        pad_token_id=0,
        position_embedding_type="absolute",
        use_cache=True,
        classifier_dropout=None,
        pretrained_config=None,
        values_override=None,
        label_map=None,
        **kwargs,
    ):
        super().__init__(pad_token_id=pad_token_id, **kwargs)

        self.vocab_size = vocab_size
        self.hidden_size = hidden_size
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads
        self.hidden_act = hidden_act
        self.intermediate_size = intermediate_size
        self.hidden_dropout_prob = hidden_dropout_prob
        self.attention_probs_dropout_prob = attention_probs_dropout_prob
        self.max_position_embeddings = max_position_embeddings
        self.type_vocab_size = type_vocab_size
        self.initializer_range = initializer_range
        self.layer_norm_eps = layer_norm_eps
        self.position_embedding_type = position_embedding_type
        self.use_cache = use_cache
        self.classifier_dropout = classifier_dropout
        self.pretrained_config = pretrained_config
        self.label_map = label_map

        self.values_override = values_override or {}
        self.outputs = {
            "logits": {"shape": [None, None, self.hidden_size], "dtype": "float32"}
        }

    @classmethod
    def is_torch_support_available(cls):
        """
        Indicate whether Torch support is available for this configuration.
        Required for compatibility with certain parts of the Transformers library.
        """
        return True

    @classmethod
    def patch_ops(self):
        """
        A method required by some Hugging Face utilities to modify operator mappings.
        Currently, it performs no operation and is included for compatibility.
        Args:
            ops: A dictionary of operations to potentially patch.
        Returns:
            The (unmodified) ops dictionary.
        """
        return None

    def generate_dummy_inputs(self, tokenizer, batch_size=1, seq_length=8, framework="pt"):
        """
        Generate dummy inputs for testing or export.
        Args:
            tokenizer: The tokenizer used to tokenize inputs.
            batch_size: Number of input samples in the batch.
            seq_length: Length of each sequence.
            framework: Framework ("pt" for PyTorch, "tf" for TensorFlow).
        Returns:
            Dummy inputs as a dictionary.
        """
        if framework == "pt":
            input_ids = torch.randint(
                low=0,
                high=self.vocab_size,
                size=(batch_size, seq_length),
                dtype=torch.long
            )
            attention_mask = torch.ones((batch_size, seq_length), dtype=torch.long)
            return {"input_ids": input_ids, "attention_mask": attention_mask}
        else:
            raise ValueError("Framework '{}' not supported.".format(framework))

# Register the configuration with the transformers library
ImpressoConfig.register_for_auto_class()