Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add task_type_id to BERT to support ERNIE-2.0 and ERNIE-3.0 models #18686

Merged
merged 23 commits into from
Sep 9, 2022
Merged
74 changes: 74 additions & 0 deletions docs/source/en/model_doc/ernie.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!--Copyright 2022 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
-->

# Ernie

## Overview
ERNIE is a series of powerful models proposed by baidu, especially in Chinese tasks,
including [ERNIE1.0](https://arxiv.org/abs/1904.09223), [ERNIE2.0](https://ojs.aaai.org/index.php/AAAI/article/view/6428),
[ERNIE3.0](https://arxiv.org/abs/2107.02137), [ERNIE-Gram](https://arxiv.org/abs/2010.12148), [ERNIE-health](https://arxiv.org/abs/2110.07244), etc.

These models are contributed by [nghuyong](https://huggingface.co/nghuyong) and the official code can be found in [PaddleNLP](https://github.com/PaddlePaddle/PaddleNLP) (in PaddlePaddle).

nghuyong marked this conversation as resolved.
Show resolved Hide resolved
## ErnieConfig

[[autodoc]] ErnieConfig
- all

## Ernie specific outputs

[[autodoc]] models.ernie.modeling_ernie.ErnieForPreTrainingOutput

## ErnieModel

[[autodoc]] ErnieModel
- forward

## ErnieForPreTraining

[[autodoc]] ErnieForPreTraining
- forward

## ErnieLMHeadModel

[[autodoc]] ErnieLMHeadModel
- forward

## ErnieForMaskedLM

[[autodoc]] ErnieForMaskedLM
- forward

## ErnieForNextSentencePrediction

[[autodoc]] ErnieForNextSentencePrediction
- forward

## ErnieForSequenceClassification

[[autodoc]] ErnieForSequenceClassification
- forward

## ErnieForMultipleChoice

[[autodoc]] ErnieForMultipleChoice
- forward

## ErnieForTokenClassification

[[autodoc]] ErnieForTokenClassification
- forward

## ErnieForQuestionAnswering

[[autodoc]] ErnieForQuestionAnswering
- forward
38 changes: 38 additions & 0 deletions src/transformers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@
"BertTokenizer",
"WordpieceTokenizer",
],
"models.ernie": [
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
"ERNIE_PRETRAINED_CONFIG_ARCHIVE_MAP",
"ErnieConfig",
],
"models.bert_generation": ["BertGenerationConfig"],
"models.bert_japanese": ["BertJapaneseTokenizer", "CharacterTokenizer", "MecabTokenizer"],
"models.bertweet": ["BertweetTokenizer"],
Expand Down Expand Up @@ -888,6 +892,22 @@
"load_tf_weights_in_bert",
]
)
_import_structure["models.ernie"].extend(
[
"ERNIE_PRETRAINED_MODEL_ARCHIVE_LIST",
"ErnieForMaskedLM",
"ErnieForMultipleChoice",
"ErnieForNextSentencePrediction",
"ErnieForPreTraining",
"ErnieForQuestionAnswering",
"ErnieForSequenceClassification",
"ErnieForTokenClassification",
"ErnieLayer",
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
"ErnieLMHeadModel",
"ErnieModel",
"ErniePreTrainedModel",
]
)
_import_structure["models.bert_generation"].extend(
[
"BertGenerationDecoder",
Expand Down Expand Up @@ -2989,6 +3009,10 @@
BertTokenizer,
WordpieceTokenizer,
)
from .models.ernie import (
ERNIE_PRETRAINED_CONFIG_ARCHIVE_MAP,
ErnieConfig,
)
from .models.bert_generation import BertGenerationConfig
from .models.bert_japanese import BertJapaneseTokenizer, CharacterTokenizer, MecabTokenizer
from .models.bertweet import BertweetTokenizer
Expand Down Expand Up @@ -3630,6 +3654,20 @@
BertPreTrainedModel,
load_tf_weights_in_bert,
)
from .models.ernie import (
ERNIE_PRETRAINED_MODEL_ARCHIVE_LIST,
ErnieForMaskedLM,
ErnieForMultipleChoice,
ErnieForNextSentencePrediction,
ErnieForPreTraining,
ErnieForQuestionAnswering,
ErnieForSequenceClassification,
ErnieForTokenClassification,
ErnieLayer,
ErnieLMHeadModel,
ErnieModel,
ErniePreTrainedModel,
)
from .models.bert_generation import (
BertGenerationDecoder,
BertGenerationEncoder,
Expand Down
1 change: 1 addition & 0 deletions src/transformers/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
bartpho,
beit,
bert,
ernie,
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
bert_generation,
bert_japanese,
bertweet,
Expand Down
3 changes: 3 additions & 0 deletions src/transformers/models/auto/configuration_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
("bart", "BartConfig"),
("beit", "BeitConfig"),
("bert", "BertConfig"),
("ernie", "ErnieConfig"),
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
("bert-generation", "BertGenerationConfig"),
("big_bird", "BigBirdConfig"),
("bigbird_pegasus", "BigBirdPegasusConfig"),
Expand Down Expand Up @@ -162,6 +163,7 @@
("bart", "BART_PRETRAINED_CONFIG_ARCHIVE_MAP"),
("beit", "BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP"),
("bert", "BERT_PRETRAINED_CONFIG_ARCHIVE_MAP"),
("ernie", "ERNIE_PRETRAINED_CONFIG_ARCHIVE_MAP"),
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
("big_bird", "BIG_BIRD_PRETRAINED_CONFIG_ARCHIVE_MAP"),
("bigbird_pegasus", "BIGBIRD_PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP"),
("blenderbot", "BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP"),
Expand Down Expand Up @@ -278,6 +280,7 @@
("bartpho", "BARTpho"),
("beit", "BEiT"),
("bert", "BERT"),
("ernie", "Ernie"),
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
("bert-generation", "Bert Generation"),
("bert-japanese", "BertJapanese"),
("bertweet", "BERTweet"),
Expand Down
10 changes: 10 additions & 0 deletions src/transformers/models/auto/modeling_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
("bart", "BartModel"),
("beit", "BeitModel"),
("bert", "BertModel"),
("ernie", "ErnieModel"),
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
("bert-generation", "BertGenerationEncoder"),
("big_bird", "BigBirdModel"),
("bigbird_pegasus", "BigBirdPegasusModel"),
Expand Down Expand Up @@ -155,6 +156,7 @@
("albert", "AlbertForPreTraining"),
("bart", "BartForConditionalGeneration"),
("bert", "BertForPreTraining"),
("ernie", "ErnieForPreTraining"),
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
("big_bird", "BigBirdForPreTraining"),
("bloom", "BloomForCausalLM"),
("camembert", "CamembertForMaskedLM"),
Expand Down Expand Up @@ -208,6 +210,7 @@
("albert", "AlbertForMaskedLM"),
("bart", "BartForConditionalGeneration"),
("bert", "BertForMaskedLM"),
("ernie", "ErnieForMaskedLM"),
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
("big_bird", "BigBirdForMaskedLM"),
("bigbird_pegasus", "BigBirdPegasusForConditionalGeneration"),
("blenderbot-small", "BlenderbotSmallForConditionalGeneration"),
Expand Down Expand Up @@ -272,6 +275,7 @@
# Model for Causal LM mapping
("bart", "BartForCausalLM"),
("bert", "BertLMHeadModel"),
("ernie", "ErnieLMHeadModel"),
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
("bert-generation", "BertGenerationDecoder"),
("big_bird", "BigBirdForCausalLM"),
("bigbird_pegasus", "BigBirdPegasusForCausalLM"),
Expand Down Expand Up @@ -404,6 +408,7 @@
("albert", "AlbertForMaskedLM"),
("bart", "BartForConditionalGeneration"),
("bert", "BertForMaskedLM"),
("ernie", "ErnieForMaskedLM"),
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
("big_bird", "BigBirdForMaskedLM"),
("camembert", "CamembertForMaskedLM"),
("convbert", "ConvBertForMaskedLM"),
Expand Down Expand Up @@ -489,6 +494,7 @@
("albert", "AlbertForSequenceClassification"),
("bart", "BartForSequenceClassification"),
("bert", "BertForSequenceClassification"),
("ernie", "ErnieForSequenceClassification"),
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
("big_bird", "BigBirdForSequenceClassification"),
("bigbird_pegasus", "BigBirdPegasusForSequenceClassification"),
("bloom", "BloomForSequenceClassification"),
Expand Down Expand Up @@ -547,6 +553,7 @@
("albert", "AlbertForQuestionAnswering"),
("bart", "BartForQuestionAnswering"),
("bert", "BertForQuestionAnswering"),
("ernie", "ErnieForQuestionAnswering"),
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
("big_bird", "BigBirdForQuestionAnswering"),
("bigbird_pegasus", "BigBirdPegasusForQuestionAnswering"),
("camembert", "CamembertForQuestionAnswering"),
Expand Down Expand Up @@ -608,6 +615,7 @@
# Model for Token Classification mapping
("albert", "AlbertForTokenClassification"),
("bert", "BertForTokenClassification"),
("ernie", "ErnieForTokenClassification"),
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
("big_bird", "BigBirdForTokenClassification"),
("bloom", "BloomForTokenClassification"),
("camembert", "CamembertForTokenClassification"),
Expand Down Expand Up @@ -651,6 +659,7 @@
# Model for Multiple Choice mapping
("albert", "AlbertForMultipleChoice"),
("bert", "BertForMultipleChoice"),
("ernie", "ErnieForMultipleChoice"),
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
("big_bird", "BigBirdForMultipleChoice"),
("camembert", "CamembertForMultipleChoice"),
("canine", "CanineForMultipleChoice"),
Expand Down Expand Up @@ -686,6 +695,7 @@
MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES = OrderedDict(
[
("bert", "BertForNextSentencePrediction"),
("ernie", "ErnieForNextSentencePrediction"),
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
("fnet", "FNetForNextSentencePrediction"),
("megatron-bert", "MegatronBertForNextSentencePrediction"),
("mobilebert", "MobileBertForNextSentencePrediction"),
Expand Down
1 change: 1 addition & 0 deletions src/transformers/models/auto/tokenization_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
),
("bartpho", ("BartphoTokenizer", None)),
("bert", ("BertTokenizer", "BertTokenizerFast" if is_tokenizers_available() else None)),
("ernie", ("BertTokenizer", "BertTokenizerFast" if is_tokenizers_available() else None)),
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
("bert-generation", ("BertGenerationTokenizer" if is_sentencepiece_available() else None, None)),
("bert-japanese", ("BertJapaneseTokenizer", None)),
("bertweet", ("BertweetTokenizer", None)),
Expand Down
88 changes: 88 additions & 0 deletions src/transformers/models/ernie/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# flake8: noqa
# There's no way to ignore "F401 '...' imported but unused" warnings in this
# module, but to preserve other warnings. So, don't check this module at all.

# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import TYPE_CHECKING

from ...utils import (
OptionalDependencyNotAvailable,
_LazyModule,
is_tensorflow_text_available,
is_torch_available,
)


_import_structure = {
"configuration_ernie": ["ERNIE_PRETRAINED_CONFIG_ARCHIVE_MAP", "ErnieConfig", "ErnieOnnxConfig"],
}

try:
if not is_torch_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
_import_structure["modeling_ernie"] = [
"ERNIE_PRETRAINED_MODEL_ARCHIVE_LIST",
"ErnieForMaskedLM",
"ErnieForMultipleChoice",
"ErnieForNextSentencePrediction",
"ErnieForPreTraining",
"ErnieForQuestionAnswering",
"ErnieForSequenceClassification",
"ErnieForTokenClassification",
"ErnieLayer",
"ErnieLMHeadModel",
"ErnieModel",
"ErniePreTrainedModel",
]

try:
if not is_tensorflow_text_available():
nghuyong marked this conversation as resolved.
Show resolved Hide resolved
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
_import_structure["tokenization_ernie_tf"] = ["TFBertTokenizer"]

if TYPE_CHECKING:
from .configuration_ernie import ERNIE_PRETRAINED_CONFIG_ARCHIVE_MAP, ErnieConfig, ErnieOnnxConfig

try:
if not is_torch_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
from .modeling_ernie import (
ERNIE_PRETRAINED_MODEL_ARCHIVE_LIST,
ErnieForMaskedLM,
ErnieForMultipleChoice,
ErnieForNextSentencePrediction,
ErnieForPreTraining,
ErnieForQuestionAnswering,
ErnieForSequenceClassification,
ErnieForTokenClassification,
ErnieLayer,
ErnieLMHeadModel,
ErnieModel,
ErniePreTrainedModel,
)

else:
import sys
sys.modules[__name__] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
Loading