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 mobilebert onnx configs #17029

Merged
merged 8 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/source/en/model_doc/auto.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ Likewise, if your `NewModel` is a subclass of [`PreTrainedModel`], make sure its

[[autodoc]] TFAutoModelForMultipleChoice

## TFAutoModelForNextSentencePrediction

[[autodoc]] TFAutoModelForNextSentencePrediction

## TFAutoModelForTableQuestionAnswering

[[autodoc]] TFAutoModelForTableQuestionAnswering
Expand Down
1 change: 1 addition & 0 deletions docs/source/en/serialization.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Ready-made configurations include the following architectures:
- M2M100
- Marian
- mBART
- MobileBert
- OpenAI GPT-2
- PLBart
- RoBERTa
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1798,6 +1798,7 @@
"TFAutoModelForSeq2SeqLM",
"TFAutoModelForSequenceClassification",
"TFAutoModelForSpeechSeq2Seq",
"TFAutoModelForNextSentencePrediction",
"TFAutoModelForTableQuestionAnswering",
"TFAutoModelForTokenClassification",
"TFAutoModelForVision2Seq",
Expand Down Expand Up @@ -3964,6 +3965,7 @@
TFAutoModelForImageClassification,
TFAutoModelForMaskedLM,
TFAutoModelForMultipleChoice,
TFAutoModelForNextSentencePrediction,
TFAutoModelForPreTraining,
TFAutoModelForQuestionAnswering,
TFAutoModelForSeq2SeqLM,
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/models/auto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"TFAutoModelForSeq2SeqLM",
"TFAutoModelForSequenceClassification",
"TFAutoModelForSpeechSeq2Seq",
"TFAutoModelForNextSentencePrediction",
"TFAutoModelForTableQuestionAnswering",
"TFAutoModelForTokenClassification",
"TFAutoModelForVision2Seq",
Expand Down Expand Up @@ -224,6 +225,7 @@
TFAutoModelForImageClassification,
TFAutoModelForMaskedLM,
TFAutoModelForMultipleChoice,
TFAutoModelForNextSentencePrediction,
TFAutoModelForPreTraining,
TFAutoModelForQuestionAnswering,
TFAutoModelForSeq2SeqLM,
Expand Down
12 changes: 10 additions & 2 deletions src/transformers/models/mobilebert/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@


_import_structure = {
"configuration_mobilebert": ["MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MobileBertConfig"],
"configuration_mobilebert": [
"MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP",
"MobileBertConfig",
"MobileBertOnnxConfig",
],
"tokenization_mobilebert": ["MobileBertTokenizer"],
}

Expand Down Expand Up @@ -62,7 +66,11 @@


if TYPE_CHECKING:
from .configuration_mobilebert import MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileBertConfig
from .configuration_mobilebert import (
MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
MobileBertConfig,
MobileBertOnnxConfig,
)
from .tokenization_mobilebert import MobileBertTokenizer

if is_tokenizers_available():
Expand Down
20 changes: 20 additions & 0 deletions src/transformers/models/mobilebert/configuration_mobilebert.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
""" MobileBERT model configuration"""
from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


Expand Down Expand Up @@ -165,3 +168,20 @@ def __init__(
self.true_hidden_size = hidden_size

self.classifier_dropout = classifier_dropout


# Copied from transformers.models.bert.configuration_bert.BertOnnxConfig with Bert->MobileBert
class MobileBertOnnxConfig(OnnxConfig):
manandey marked this conversation as resolved.
Show resolved Hide resolved
@property
def inputs(self) -> Mapping[str, Mapping[int, str]]:
if self.task == "multiple-choice":
dynamic_axis = {0: "batch", 1: "choice", 2: "sequence"}
else:
dynamic_axis = {0: "batch", 1: "sequence"}
return OrderedDict(
[
("input_ids", dynamic_axis),
("attention_mask", dynamic_axis),
("token_type_ids", dynamic_axis),
]
)
16 changes: 16 additions & 0 deletions src/transformers/onnx/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from ..models.m2m_100 import M2M100OnnxConfig
from ..models.marian import MarianOnnxConfig
from ..models.mbart import MBartOnnxConfig
from ..models.mobilebert import MobileBertOnnxConfig
from ..models.roberta import RobertaOnnxConfig
from ..models.roformer import RoFormerOnnxConfig
from ..models.t5 import T5OnnxConfig
Expand All @@ -43,6 +44,7 @@
AutoModelForMaskedImageModeling,
AutoModelForMaskedLM,
AutoModelForMultipleChoice,
AutoModelForNextSentencePrediction,
AutoModelForQuestionAnswering,
AutoModelForSeq2SeqLM,
AutoModelForSequenceClassification,
Expand All @@ -54,6 +56,7 @@
TFAutoModelForCausalLM,
TFAutoModelForMaskedLM,
TFAutoModelForMultipleChoice,
TFAutoModelForNextSentencePrediction,
TFAutoModelForQuestionAnswering,
TFAutoModelForSeq2SeqLM,
TFAutoModelForSequenceClassification,
Expand Down Expand Up @@ -107,6 +110,7 @@ class FeaturesManager:
"question-answering": AutoModelForQuestionAnswering,
"image-classification": AutoModelForImageClassification,
"masked-im": AutoModelForMaskedImageModeling,
"next-sentence-prediction": AutoModelForNextSentencePrediction,
}
if is_tf_available():
_TASKS_TO_TF_AUTOMODELS = {
Expand All @@ -118,6 +122,7 @@ class FeaturesManager:
"token-classification": TFAutoModelForTokenClassification,
"multiple-choice": TFAutoModelForMultipleChoice,
"question-answering": TFAutoModelForQuestionAnswering,
"next-sentence-prediction": TFAutoModelForNextSentencePrediction,
}

# Set of model topologies we support associated to the features supported by each topology and the factory
Expand Down Expand Up @@ -152,6 +157,7 @@ class FeaturesManager:
"multiple-choice",
"token-classification",
"question-answering",
"next-sentence-prediction",
manandey marked this conversation as resolved.
Show resolved Hide resolved
onnx_config_cls=BertOnnxConfig,
),
"big-bird": supported_features_mapping(
Expand Down Expand Up @@ -304,6 +310,16 @@ class FeaturesManager:
"question-answering",
onnx_config_cls=MBartOnnxConfig,
),
"mobilebert": supported_features_mapping(
"default",
"masked-lm",
"next-sentence-prediction",
"sequence-classification",
"multiple-choice",
"token-classification",
"question-answering",
onnx_config_cls=MobileBertOnnxConfig,
),
"m2m-100": supported_features_mapping(
"default", "default-with-past", "seq2seq-lm", "seq2seq-lm-with-past", onnx_config_cls=M2M100OnnxConfig
),
Expand Down
1 change: 1 addition & 0 deletions tests/onnx/test_onnx_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def test_values_override(self):
("electra", "google/electra-base-generator"),
("roberta", "roberta-base"),
("roformer", "junnyu/roformer_chinese_base"),
("mobilebert", "google/mobilebert-uncased"),
("xlm-roberta", "xlm-roberta-base"),
("layoutlm", "microsoft/layoutlm-base-uncased"),
("vit", "google/vit-base-patch16-224"),
Expand Down