Skip to content

Commit e03d2dd

Browse files
authored
Merge pull request #440 from xyltt/master
[new] add elasticbert
2 parents 6f21084 + b94cbd3 commit e03d2dd

File tree

7 files changed

+1890
-1
lines changed

7 files changed

+1890
-1
lines changed

fastNLP/transformers/torch/models/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
from .bart import *
33
from .bert import *
44
from .cpt import *
5+
from .elasticbert import *
56
from .gpt2 import *
67
from .roberta import *

fastNLP/transformers/torch/models/auto/configuration_auto.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
("bert", "BertConfig"),
3232
("gpt2", "GPT2Config"),
3333
("cpt", "CPTConfig"),
34+
("elasticbert", "ElasticBertConfig"),
3435
]
3536
)
3637

@@ -42,6 +43,7 @@
4243
("gpt2", "GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP"),
4344
("roberta", "ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP"),
4445
("cpt", "BART_PRETRAINED_CONFIG_ARCHIVE_MAP"),
46+
("elasticbert", "ELASTICBERT_PRETRAINED_CONFIG_ARCHIVE_MAP"),
4547
]
4648
)
4749

@@ -52,7 +54,8 @@
5254
("roberta", "RoBERTa"),
5355
("bert", "BERT"),
5456
("gpt2", "OpenAI GPT-2"),
55-
("cpt", "CPT")
57+
("cpt", "CPT"),
58+
("elasticbert", "ElasticBERT")
5659
]
5760
)
5861

fastNLP/transformers/torch/models/auto/modeling_auto.py

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
("bert", "BertModel"),
2929
("gpt2", "GPT2Model"),
3030
("cpt", "CPTModel"),
31+
("elasticbert", "ElasticBertModel"),
3132
]
3233
)
3334

@@ -38,6 +39,7 @@
3839
("bert", "BertForPreTraining"),
3940
("gpt2", "GPT2LMHeadModel"),
4041
("cpt", "CPTForConditionalGeneration"),
42+
("elasticbert", "ElasticBertForPreTraining"),
4143
]
4244
)
4345

@@ -49,6 +51,7 @@
4951
("bert", "BertForMaskedLM"),
5052
("gpt2", "GPT2LMHeadModel"),
5153
("cpt", "CPTForConditionalGeneration"),
54+
("elasticbert", "ElasticBertForMaskedLM"),
5255
]
5356
)
5457

@@ -59,6 +62,7 @@
5962
("bert", "BertLMHeadModel"),
6063
("gpt2", "GPT2LMHeadModel"),
6164
("bart", "BartForCausalLM"),
65+
("elasticbert", "ElasticBertLMHeadModel"),
6266
]
6367
)
6468

@@ -71,6 +75,7 @@
7175
("roberta", "RobertaForMaskedLM"),
7276
("bert", "BertForMaskedLM"),
7377
("cpt", "CPTForConditionalGeneration"),
78+
("elasticbert", "ElasticBertForMaskedLM"),
7479
]
7580
)
7681

@@ -94,6 +99,7 @@
9499
("bert", "BertForSequenceClassification"),
95100
("gpt2", "GPT2ForSequenceClassification"),
96101
("cpt", "CPTForSequenceClassification"),
102+
("elasticbert", "ElasticBertForSequenceClassification"),
97103
]
98104
)
99105

@@ -104,6 +110,7 @@
104110
("roberta", "RobertaForQuestionAnswering"),
105111
("bert", "BertForQuestionAnswering"),
106112
("cpt", "CPTForQuestionAnswering"),
113+
("elasticbert", "ElasticBertForQuestionAnswering"),
107114
]
108115
)
109116

@@ -115,6 +122,7 @@
115122
("roberta", "RobertaForTokenClassification"),
116123
("bert", "BertForTokenClassification"),
117124
("gpt2", "GPT2ForTokenClassification"),
125+
("elasticbert", "ElasticBertForTokenClassification"),
118126
]
119127
)
120128

@@ -123,6 +131,7 @@
123131
# Model for Multiple Choice mapping
124132
("roberta", "RobertaForMultipleChoice"),
125133
("bert", "BertForMultipleChoice"),
134+
("elasticbert", "ElasticBertForMultipleChoice"),
126135
]
127136
)
128137

fastNLP/transformers/torch/models/auto/tokenization_auto.py

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
("roberta", ("RobertaTokenizer", None)),
5151
("bert", ("BertTokenizer", None)),
5252
("gpt2", ("GPT2Tokenizer", None)),
53+
("elasticbert", ("BertTokenizer", None)),
5354
]
5455
)
5556

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
__all__ = [
2+
"ELASTICBERT_PRETRAINED_CONFIG_ARCHIVE_MAP",
3+
"ElasticBertConfig",
4+
5+
"ELASTICBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
6+
"ElasticBertForMultipleChoice",
7+
"ElasticBertForPreTraining",
8+
"ElasticBertForQuestionAnswering",
9+
"ElasticBertForSequenceClassification",
10+
"ElasticBertForTokenClassification",
11+
"ElasticBertLayer",
12+
"ElasticBertModel",
13+
"ElasticBertLMHeadModel",
14+
"ElasticBertForMaskedLM",
15+
"ElasticBertPreTrainedModel",
16+
]
17+
18+
from .configuration_elasticbert import ElasticBertConfig, ELASTICBERT_PRETRAINED_CONFIG_ARCHIVE_MAP
19+
from .modeling_elasticbert import ELASTICBERT_PRETRAINED_MODEL_ARCHIVE_LIST, ElasticBertForMultipleChoice, ElasticBertForPreTraining, \
20+
ElasticBertForQuestionAnswering, ElasticBertForSequenceClassification, ElasticBertForTokenClassification, \
21+
ElasticBertLayer, ElasticBertModel, ElasticBertPreTrainedModel, ElasticBertForMaskedLM, ElasticBertLMHeadModel
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# coding=utf-8
2+
# Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team.
3+
# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
""" ElasticBERT model configuration """
17+
18+
19+
from fastNLP.core.log import logger
20+
from fastNLP.transformers.torch.configuration_utils import PretrainedConfig
21+
22+
23+
__all__ = [
24+
"ELASTICBERT_PRETRAINED_CONFIG_ARCHIVE_MAP",
25+
"ElasticBertConfig",
26+
]
27+
28+
ELASTICBERT_PRETRAINED_CONFIG_ARCHIVE_MAP = {
29+
"elasticbert-base": "https://huggingface.co/fnlp/elasticbert-base/resolve/main/config.json",
30+
"elasticbert-large": "https://huggingface.co/fnlp/elasticbert-large/resolve/main/config.json",
31+
"elasticbert-base-chinese": "https://huggingface.co/fnlp/elasticbert-chinese-base/resolve/main/config.json"
32+
}
33+
34+
35+
class ElasticBertConfig(PretrainedConfig):
36+
r"""
37+
This is the configuration class to store the configuration of a :class:`ElasticBertModel`
38+
39+
Args:
40+
max_output_layers (:obj: `int`, default to 12):
41+
The maximum number of classification layers.
42+
num_output_layers (:obj: `int`, default to 1):
43+
The number of classification layers. Used to specify how many classification layers there are.
44+
It is 1 in static usage, and equal to num_hidden_layers in dynamic usage.
45+
"""
46+
47+
model_type = "elasticbert"
48+
49+
def __init__(
50+
self,
51+
vocab_size=30522,
52+
hidden_size=768,
53+
num_hidden_layers=12,
54+
num_attention_heads=12,
55+
max_output_layers=12,
56+
num_output_layers=12,
57+
intermediate_size=3072,
58+
hidden_act="gelu",
59+
hidden_dropout_prob=0.1,
60+
attention_probs_dropout_prob=0.1,
61+
max_position_embeddings=512,
62+
type_vocab_size=2,
63+
initializer_range=0.02,
64+
layer_norm_eps=1e-12,
65+
pad_token_id=0,
66+
gradient_checkpointing=False,
67+
position_embedding_type="absolute",
68+
use_cache=True,
69+
**kwargs
70+
):
71+
super().__init__(pad_token_id=pad_token_id, **kwargs)
72+
73+
self.vocab_size = vocab_size
74+
self.hidden_size = hidden_size
75+
self.num_hidden_layers = num_hidden_layers
76+
self.num_attention_heads = num_attention_heads
77+
self.max_output_layers = max_output_layers
78+
self.num_output_layers = num_output_layers
79+
self.hidden_act = hidden_act
80+
self.intermediate_size = intermediate_size
81+
self.hidden_dropout_prob = hidden_dropout_prob
82+
self.attention_probs_dropout_prob = attention_probs_dropout_prob
83+
self.max_position_embeddings = max_position_embeddings
84+
self.type_vocab_size = type_vocab_size
85+
self.initializer_range = initializer_range
86+
self.layer_norm_eps = layer_norm_eps
87+
self.gradient_checkpointing = gradient_checkpointing
88+
self.position_embedding_type = position_embedding_type
89+
self.use_cache = use_cache

0 commit comments

Comments
 (0)