Skip to content

Commit

Permalink
add_ernie
Browse files Browse the repository at this point in the history
  • Loading branch information
nghuyong committed Aug 27, 2022
1 parent bbbb453 commit 1e954cd
Show file tree
Hide file tree
Showing 11 changed files with 2,788 additions and 0 deletions.
74 changes: 74 additions & 0 deletions docs/source/en/model_doc/ernie.mdx
@@ -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).

## 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
44 changes: 44 additions & 0 deletions src/transformers/__init__.py
Expand Up @@ -143,6 +143,13 @@
"BertTokenizer",
"WordpieceTokenizer",
],
"models.ernie": [
"ERNIE_PRETRAINED_CONFIG_ARCHIVE_MAP",
"BasicTokenizer",
"ErnieConfig",

"WordpieceTokenizer",
],
"models.bert_generation": ["BertGenerationConfig"],
"models.bert_japanese": ["BertJapaneseTokenizer", "CharacterTokenizer", "MecabTokenizer"],
"models.bertweet": ["BertweetTokenizer"],
Expand Down Expand Up @@ -886,6 +893,22 @@
"load_tf_weights_in_bert",
]
)
_import_structure["models.ernie"].extend(
[
"ERNIE_PRETRAINED_MODEL_ARCHIVE_LIST",
"ErnieForMaskedLM",
"ErnieForMultipleChoice",
"ErnieForNextSentencePrediction",
"ErnieForPreTraining",
"ErnieForQuestionAnswering",
"ErnieForSequenceClassification",
"ErnieForTokenClassification",
"ErnieLayer",
"ErnieLMHeadModel",
"ErnieModel",
"ErniePreTrainedModel",
]
)
_import_structure["models.bert_generation"].extend(
[
"BertGenerationDecoder",
Expand Down Expand Up @@ -2950,6 +2973,13 @@
BertTokenizer,
WordpieceTokenizer,
)
from .models.ernie import (
ERNIE_PRETRAINED_CONFIG_ARCHIVE_MAP,
BasicTokenizer,
ErnieConfig,

WordpieceTokenizer,
)
from .models.bert_generation import BertGenerationConfig
from .models.bert_japanese import BertJapaneseTokenizer, CharacterTokenizer, MecabTokenizer
from .models.bertweet import BertweetTokenizer
Expand Down Expand Up @@ -3589,6 +3619,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
Expand Up @@ -24,6 +24,7 @@
bartpho,
beit,
bert,
ernie,
bert_generation,
bert_japanese,
bertweet,
Expand Down
3 changes: 3 additions & 0 deletions src/transformers/models/auto/configuration_auto.py
Expand Up @@ -33,6 +33,7 @@
("bart", "BartConfig"),
("beit", "BeitConfig"),
("bert", "BertConfig"),
("ernie", "ErnieConfig"),
("bert-generation", "BertGenerationConfig"),
("big_bird", "BigBirdConfig"),
("bigbird_pegasus", "BigBirdPegasusConfig"),
Expand Down Expand Up @@ -161,6 +162,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"),
("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 @@ -276,6 +278,7 @@
("bartpho", "BARTpho"),
("beit", "BEiT"),
("bert", "BERT"),
("ernie", "Ernie"),
("bert-generation", "Bert Generation"),
("bert-japanese", "BertJapanese"),
("bertweet", "BERTweet"),
Expand Down
10 changes: 10 additions & 0 deletions src/transformers/models/auto/modeling_auto.py
Expand Up @@ -32,6 +32,7 @@
("bart", "BartModel"),
("beit", "BeitModel"),
("bert", "BertModel"),
("ernie", "ErnieModel"),
("bert-generation", "BertGenerationEncoder"),
("big_bird", "BigBirdModel"),
("bigbird_pegasus", "BigBirdPegasusModel"),
Expand Down Expand Up @@ -154,6 +155,7 @@
("albert", "AlbertForPreTraining"),
("bart", "BartForConditionalGeneration"),
("bert", "BertForPreTraining"),
("ernie", "ErnieForPreTraining"),
("big_bird", "BigBirdForPreTraining"),
("bloom", "BloomForCausalLM"),
("camembert", "CamembertForMaskedLM"),
Expand Down Expand Up @@ -207,6 +209,7 @@
("albert", "AlbertForMaskedLM"),
("bart", "BartForConditionalGeneration"),
("bert", "BertForMaskedLM"),
("ernie", "ErnieForMaskedLM"),
("big_bird", "BigBirdForMaskedLM"),
("bigbird_pegasus", "BigBirdPegasusForConditionalGeneration"),
("blenderbot-small", "BlenderbotSmallForConditionalGeneration"),
Expand Down Expand Up @@ -270,6 +273,7 @@
# Model for Causal LM mapping
("bart", "BartForCausalLM"),
("bert", "BertLMHeadModel"),
("ernie", "ErnieLMHeadModel"),
("bert-generation", "BertGenerationDecoder"),
("big_bird", "BigBirdForCausalLM"),
("bigbird_pegasus", "BigBirdPegasusForCausalLM"),
Expand Down Expand Up @@ -402,6 +406,7 @@
("albert", "AlbertForMaskedLM"),
("bart", "BartForConditionalGeneration"),
("bert", "BertForMaskedLM"),
("ernie", "ErnieForMaskedLM"),
("big_bird", "BigBirdForMaskedLM"),
("camembert", "CamembertForMaskedLM"),
("convbert", "ConvBertForMaskedLM"),
Expand Down Expand Up @@ -486,6 +491,7 @@
("albert", "AlbertForSequenceClassification"),
("bart", "BartForSequenceClassification"),
("bert", "BertForSequenceClassification"),
("ernie", "ErnieForSequenceClassification"),
("big_bird", "BigBirdForSequenceClassification"),
("bigbird_pegasus", "BigBirdPegasusForSequenceClassification"),
("bloom", "BloomForSequenceClassification"),
Expand Down Expand Up @@ -544,6 +550,7 @@
("albert", "AlbertForQuestionAnswering"),
("bart", "BartForQuestionAnswering"),
("bert", "BertForQuestionAnswering"),
("ernie", "ErnieForQuestionAnswering"),
("big_bird", "BigBirdForQuestionAnswering"),
("bigbird_pegasus", "BigBirdPegasusForQuestionAnswering"),
("camembert", "CamembertForQuestionAnswering"),
Expand Down Expand Up @@ -605,6 +612,7 @@
# Model for Token Classification mapping
("albert", "AlbertForTokenClassification"),
("bert", "BertForTokenClassification"),
("ernie", "ErnieForTokenClassification"),
("big_bird", "BigBirdForTokenClassification"),
("bloom", "BloomForTokenClassification"),
("camembert", "CamembertForTokenClassification"),
Expand Down Expand Up @@ -648,6 +656,7 @@
# Model for Multiple Choice mapping
("albert", "AlbertForMultipleChoice"),
("bert", "BertForMultipleChoice"),
("ernie", "ErnieForMultipleChoice"),
("big_bird", "BigBirdForMultipleChoice"),
("camembert", "CamembertForMultipleChoice"),
("canine", "CanineForMultipleChoice"),
Expand Down Expand Up @@ -683,6 +692,7 @@
MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES = OrderedDict(
[
("bert", "BertForNextSentencePrediction"),
("ernie", "ErnieForNextSentencePrediction"),
("fnet", "FNetForNextSentencePrediction"),
("megatron-bert", "MegatronBertForNextSentencePrediction"),
("mobilebert", "MobileBertForNextSentencePrediction"),
Expand Down
1 change: 1 addition & 0 deletions src/transformers/models/auto/tokenization_auto.py
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)),
("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
@@ -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():
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__)

0 comments on commit 1e954cd

Please sign in to comment.