-
Notifications
You must be signed in to change notification settings - Fork 310
Add a DistilBertMaskedLM task model #724
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
Merged
mattdangerw
merged 21 commits into
keras-team:master
from
ADITYADAS1999:second_new_branch
Feb 24, 2023
Merged
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
3a8aca4
Add files via upload
ADITYADAS1999 dcc36c5
Update __init__.py
ADITYADAS1999 1f1d6ef
Update distil_bert_tokenizer.py
ADITYADAS1999 bf203cb
Delete distil_bert_masked_lm_preprocessor_test.py
ADITYADAS1999 88ce3c5
Add files via upload
ADITYADAS1999 0772c02
Delete distil_bert_masked_lm.py
ADITYADAS1999 25b15e8
Delete distil_bert_masked_lm_test.py
ADITYADAS1999 4b8d44c
Delete distil_bert_masked_lm_preprocessor.py
ADITYADAS1999 a960f08
Delete distil_bert_masked_lm_preprocessor_test.py
ADITYADAS1999 f419bd4
Add files via upload
ADITYADAS1999 eb5b3e8
request changes
ADITYADAS1999 ccd68c4
request changes
ADITYADAS1999 1b62669
Delete distil_bert_masked_lm_preprocessor_test.py
ADITYADAS1999 8a337f5
Add files via upload
ADITYADAS1999 e361786
Merge branch 'keras-team:master' into second_new_branch
ADITYADAS1999 555ba33
Update distil_bert_tokenizer_test.py
ADITYADAS1999 e852563
Revert "Update distil_bert_tokenizer_test.py"
ADITYADAS1999 969e88d
Update distil_bert_tokenizer.py
ADITYADAS1999 82db577
Update distil_bert_tokenizer.py
ADITYADAS1999 0f8fc58
Update distil_bert_tokenizer.py
ADITYADAS1999 e0e97d9
Minor fix
mattdangerw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| # Copyright 2022 The KerasNLP Authors | ||
| # | ||
| # 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 | ||
| # | ||
| # https://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. | ||
| """DistilBERT masked lm model.""" | ||
|
|
||
| import copy | ||
|
|
||
| from tensorflow import keras | ||
|
|
||
| from keras_nlp.layers.masked_lm_head import MaskedLMHead | ||
| from keras_nlp.models.distil_bert.distil_bert_backbone import DistilBertBackbone | ||
| from keras_nlp.models.distil_bert.distil_bert_backbone import ( | ||
| distilbert_kernel_initializer, | ||
| ) | ||
| from keras_nlp.models.distil_bert.distil_bert_masked_lm_preprocessor import ( | ||
| DistilBertMaskedLMPreprocessor, | ||
| ) | ||
| from keras_nlp.models.distil_bert.distil_bert_presets import backbone_presets | ||
| from keras_nlp.models.task import Task | ||
| from keras_nlp.utils.python_utils import classproperty | ||
|
|
||
|
|
||
| @keras.utils.register_keras_serializable(package="keras_nlp") | ||
| class DistilBertMaskedLM(Task): | ||
| """An end-to-end DistilBERT model for the masked language modeling task. | ||
|
|
||
| This model will train DistilBERT on a masked language modeling task. | ||
| The model will predict labels for a number of masked tokens in the | ||
| input data. For usage of this model with pre-trained weights, see the | ||
| `from_preset()` method. | ||
|
|
||
| This model can optionally be configured with a `preprocessor` layer, in | ||
| which case inputs can be raw string features during `fit()`, `predict()`, | ||
| and `evaluate()`. Inputs will be tokenized and dynamically masked during | ||
| training and evaluation. This is done by default when creating the model | ||
| with `from_preset()`. | ||
|
|
||
| Disclaimer: Pre-trained models are provided on an "as is" basis, without | ||
| warranties or conditions of any kind. The underlying model is provided by a | ||
| third party and subject to a separate license, available | ||
| [here](https://github.com/huggingface/transformers). | ||
|
|
||
| Args: | ||
| backbone: A `keras_nlp.models.DistilBertBackbone` instance. | ||
| preprocessor: A `keras_nlp.models.DistilBertMaskedLMPreprocessor` or | ||
| `None`. If `None`, this model will not apply preprocessing, and | ||
| inputs should be preprocessed before calling the model. | ||
|
|
||
| Example usage: | ||
|
|
||
| Raw string inputs and pretrained backbone. | ||
| ```python | ||
| # Create a dataset with raw string features. Labels are inferred. | ||
| features = ["The quick brown fox jumped.", "I forgot my homework."] | ||
|
|
||
| # Create a DistilBertMaskedLM with a pretrained backbone and further train | ||
| # on an MLM task. | ||
| masked_lm = keras_nlp.models.DistilBertMaskedLM.from_preset( | ||
| "distil_bert_base_en", | ||
| ) | ||
| masked_lm.compile( | ||
| loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True), | ||
| ) | ||
| masked_lm.fit(x=features, batch_size=2) | ||
| ``` | ||
|
|
||
| Preprocessed inputs and custom backbone. | ||
| ```python | ||
| # Create a preprocessed dataset where 0 is the mask token. | ||
| preprocessed_features = { | ||
| "token_ids": tf.constant( | ||
| [[1, 2, 0, 4, 0, 6, 7, 8]] * 2, shape=(2, 8) | ||
| ), | ||
| "padding_mask": tf.constant( | ||
| [[1, 1, 1, 1, 1, 1, 1, 1]] * 2, shape=(2, 8) | ||
| ), | ||
| "mask_positions": tf.constant([[2, 4]] * 2, shape=(2, 2)) | ||
| } | ||
| # Labels are the original masked values. | ||
| labels = [[3, 5]] * 2 | ||
|
|
||
| # Randomly initialize a DistilBERT encoder | ||
| backbone = keras_nlp.models.DistilBertBackbone( | ||
| vocabulary_size=50265, | ||
| num_layers=12, | ||
| num_heads=12, | ||
| hidden_dim=768, | ||
| intermediate_dim=3072, | ||
| max_sequence_length=12 | ||
| ) | ||
| # Create a DistilBERT masked_lm and fit the data. | ||
| masked_lm = keras_nlp.models.DistilBertMaskedLM( | ||
| backbone, | ||
| preprocessor=None, | ||
| ) | ||
| masked_lm.compile( | ||
| loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True), | ||
| ) | ||
| masked_lm.fit(x=preprocessed_features, y=labels, batch_size=2) | ||
| ``` | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| backbone, | ||
| preprocessor=None, | ||
| **kwargs, | ||
| ): | ||
| inputs = { | ||
| **backbone.input, | ||
| "mask_positions": keras.Input( | ||
| shape=(None,), dtype="int32", name="mask_positions" | ||
| ), | ||
| } | ||
| backbone_outputs = backbone(backbone.input) | ||
| outputs = MaskedLMHead( | ||
| vocabulary_size=backbone.vocabulary_size, | ||
| embedding_weights=backbone.token_embedding.embeddings, | ||
| intermediate_activation="gelu", | ||
| kernel_initializer=distilbert_kernel_initializer(), | ||
| name="mlm_head", | ||
| )(backbone_outputs, inputs["mask_positions"]) | ||
|
|
||
| # Instantiate using Functional API Model constructor | ||
| super().__init__( | ||
| inputs=inputs, | ||
| outputs=outputs, | ||
| include_preprocessing=preprocessor is not None, | ||
| **kwargs, | ||
| ) | ||
| # All references to `self` below this line | ||
| self.backbone = backbone | ||
| self.preprocessor = preprocessor | ||
|
|
||
| @classproperty | ||
| def backbone_cls(cls): | ||
| return DistilBertBackbone | ||
|
|
||
| @classproperty | ||
| def preprocessor_cls(cls): | ||
| return DistilBertMaskedLMPreprocessor | ||
|
|
||
| @classproperty | ||
| def presets(cls): | ||
| return copy.deepcopy(backbone_presets) | ||
143 changes: 143 additions & 0 deletions
143
keras_nlp/models/distil_bert/distil_bert_masked_lm_preprocessor.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| # Copyright 2022 The KerasNLP Authors | ||
| # | ||
| # 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 | ||
| # | ||
| # https://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. | ||
|
|
||
| """DistilBERT masked language model preprocessor layer.""" | ||
|
|
||
| from absl import logging | ||
| from tensorflow import keras | ||
|
|
||
| from keras_nlp.layers.masked_lm_mask_generator import MaskedLMMaskGenerator | ||
| from keras_nlp.models.distil_bert.distil_bert_preprocessor import ( | ||
| DistilBertPreprocessor, | ||
| ) | ||
| from keras_nlp.utils.keras_utils import pack_x_y_sample_weight | ||
|
|
||
|
|
||
| @keras.utils.register_keras_serializable(package="keras_nlp") | ||
| class DistilBertMaskedLMPreprocessor(DistilBertPreprocessor): | ||
| """DistilBERT preprocessing for the masked language modeling task. | ||
|
|
||
| This preprocessing layer will prepare inputs for a masked language modeling | ||
| task. It is primarily intended for use with the | ||
| `keras_nlp.models.DistilBertMaskedLM` task model. Preprocessing will occur in | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix line length |
||
| multiple steps. | ||
|
|
||
| - Tokenize any number of input segments using the `tokenizer`. | ||
| - Pack the inputs together using a `keras_nlp.layers.MultiSegmentPacker`. | ||
| with the appropriate `"[CLS]"`, `"[SEP]"` and `"[PAD]"` tokens. | ||
| - Randomly select non-special tokens to mask, controlled by | ||
| `mask_selection_rate`. | ||
| - Construct a `(x, y, sample_weight)` tuple suitable for training with a | ||
| `keras_nlp.models.DistilBertMaskedLM` task model. | ||
|
|
||
| Examples: | ||
| ```python | ||
| # Load the preprocessor from a preset. | ||
| preprocessor = keras_nlp.models.DistilBertMaskedLMPreprocessor.from_preset( | ||
| "distil_bert_base_en" | ||
| ) | ||
|
|
||
| # Tokenize and mask a single sentence. | ||
| sentence = tf.constant("The quick brown fox jumped.") | ||
| preprocessor(sentence) | ||
|
|
||
| # Tokenize and mask a batch of sentences. | ||
| sentences = tf.constant( | ||
| ["The quick brown fox jumped.", "Call me Ishmael."] | ||
| ) | ||
| preprocessor(sentences) | ||
|
|
||
| # Tokenize and mask a dataset of sentences. | ||
| features = tf.constant( | ||
| ["The quick brown fox jumped.", "Call me Ishmael."] | ||
| ) | ||
| ds = tf.data.Dataset.from_tensor_slices((features)) | ||
| ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE) | ||
|
|
||
| # Alternatively, you can create a preprocessor from your own vocabulary. | ||
| # The usage is exactly the same as above. | ||
| vocab = ["[PAD]", "[UNK]", "[CLS]", "[SEP]"] | ||
| vocab += ["The", "qu", "##ick", "br", "##own", "fox", "tripped"] | ||
| vocab += ["Call", "me", "Ish", "##mael", "."] | ||
| vocab += ["Oh", "look", "a", "whale"] | ||
| vocab += ["I", "forgot", "my", "home", "##work"] | ||
| tokenizer = keras_nlp.models.DistilBertTokenizer(vocabulary=vocab) | ||
| preprocessor = keras_nlp.models.DistilBertMaskedLMPreprocessor(tokenizer) | ||
| ``` | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| tokenizer, | ||
| sequence_length=512, | ||
| truncate="round_robin", | ||
| mask_selection_rate=0.15, | ||
| mask_selection_length=96, | ||
| mask_token_rate=0.8, | ||
| random_token_rate=0.1, | ||
| **kwargs, | ||
| ): | ||
| super().__init__( | ||
| tokenizer, | ||
| sequence_length=sequence_length, | ||
| truncate=truncate, | ||
| **kwargs, | ||
| ) | ||
|
|
||
| self.masker = MaskedLMMaskGenerator( | ||
| mask_selection_rate=mask_selection_rate, | ||
| mask_selection_length=mask_selection_length, | ||
| mask_token_rate=mask_token_rate, | ||
| random_token_rate=random_token_rate, | ||
| vocabulary_size=tokenizer.vocabulary_size(), | ||
| mask_token_id=tokenizer.mask_token_id, | ||
| unselectable_token_ids=[ | ||
| tokenizer.cls_token_id, | ||
| tokenizer.sep_token_id, | ||
| tokenizer.pad_token_id, | ||
| ], | ||
| ) | ||
|
|
||
| def get_config(self): | ||
| config = super().get_config() | ||
| config.update( | ||
| { | ||
| "mask_selection_rate": self.masker.mask_selection_rate, | ||
| "mask_selection_length": self.masker.mask_selection_length, | ||
| "mask_token_rate": self.masker.mask_token_rate, | ||
| "random_token_rate": self.masker.random_token_rate, | ||
| } | ||
| ) | ||
| return config | ||
|
|
||
| def call(self, x, y=None, sample_weight=None): | ||
| if y is not None or sample_weight is not None: | ||
| logging.warning( | ||
| f"{self.__class__.__name__} generates `y` and `sample_weight` " | ||
| "based on your input data, but your data already contains `y` " | ||
| "or `sample_weight`. Your `y` and `sample_weight` will be " | ||
| "ignored." | ||
| ) | ||
|
|
||
| x = super().call(x) | ||
| token_ids, padding_mask = x["token_ids"], x["padding_mask"] | ||
| masker_outputs = self.masker(token_ids) | ||
| x = { | ||
| "token_ids": masker_outputs["token_ids"], | ||
| "padding_mask": padding_mask, | ||
| "mask_positions": masker_outputs["mask_positions"], | ||
| } | ||
| y = masker_outputs["mask_ids"] | ||
| sample_weight = masker_outputs["mask_weights"] | ||
| return pack_x_y_sample_weight(x, y, sample_weight) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a reminder, make sure that the examples actually run correctly. Thanks !