-
Notifications
You must be signed in to change notification settings - Fork 246
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 ALBERT Presets #655
Merged
Merged
Add ALBERT Presets #655
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
afebf96
Add ALBERT presets
abheesht17 e5a0b66
Add GDrive links
abheesht17 edaa68b
Fix preset UTs
abheesht17 7d7eb33
Fix hash
abheesht17 a0a062a
Regenerate XL and XXL
abheesht17 e47c904
Merge branch 'keras-team:master' into albert-presets
abheesht17 f7d034e
Remove cruft
abheesht17 d0ade0d
Add GCP URLs
abheesht17 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 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 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 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,114 @@ | ||
# 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. | ||
"""ALBERT model preset configurations.""" | ||
|
||
|
||
backbone_presets = { | ||
"albert_base_en_uncased": { | ||
"config": { | ||
"vocabulary_size": 30000, | ||
"num_layers": 12, | ||
"num_heads": 12, | ||
"num_groups": 1, | ||
"num_inner_repetitions": 1, | ||
"embedding_dim": 128, | ||
"hidden_dim": 768, | ||
"intermediate_dim": 3072, | ||
"dropout": 0.0, | ||
"max_sequence_length": 512, | ||
"num_segments": 2, | ||
}, | ||
"preprocessor_config": {}, | ||
"description": ( | ||
"Base size of ALBERT where all input is lowercased. " | ||
"Trained on English Wikipedia + BooksCorpus." | ||
), | ||
"weights_url": "https://drive.google.com/uc?export=download&id=1RzTTa8nMcBc84nARvJmHal5SndpKbDUa", | ||
"weights_hash": "b83ccf3418dd84adc569324183176813", | ||
"spm_proto_url": "https://drive.google.com/uc?export=download&id=1-0C5mWLPRxNaY3yuzdqut-Wi7VaKo2gX", | ||
"spm_proto_hash": "73e62ff8e90f951f24c8b907913039a5", | ||
}, | ||
"albert_large_en_uncased": { | ||
"config": { | ||
"vocabulary_size": 30000, | ||
"num_layers": 24, | ||
"num_heads": 16, | ||
"num_groups": 1, | ||
"num_inner_repetitions": 1, | ||
"embedding_dim": 128, | ||
"hidden_dim": 1024, | ||
"intermediate_dim": 4096, | ||
"dropout": 0, | ||
"max_sequence_length": 512, | ||
"num_segments": 2, | ||
}, | ||
"preprocessor_config": {}, | ||
jbischof marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"description": ( | ||
"Large size of ALBERT where all input is lowercased. " | ||
"Trained on English Wikipedia + BooksCorpus." | ||
), | ||
"weights_url": "https://drive.google.com/uc?export=download&id=1PfMgFPNIb4K9hiLx1Ik5Qcgol_3pOsLE", | ||
"weights_hash": "c7754804efb245f06dd6e7ced32e082c", | ||
"spm_proto_url": "https://drive.google.com/uc?export=download&id=1-0C5mWLPRxNaY3yuzdqut-Wi7VaKo2gX", | ||
"spm_proto_hash": "73e62ff8e90f951f24c8b907913039a5", | ||
}, | ||
"albert_extra_large_en_uncased": { | ||
"config": { | ||
"vocabulary_size": 30000, | ||
"num_layers": 24, | ||
"num_heads": 16, | ||
"num_groups": 1, | ||
"num_inner_repetitions": 1, | ||
"embedding_dim": 128, | ||
"hidden_dim": 2048, | ||
"intermediate_dim": 8192, | ||
"dropout": 0, | ||
"max_sequence_length": 512, | ||
"num_segments": 2, | ||
}, | ||
"preprocessor_config": {}, | ||
"description": ( | ||
"Extra Large size of ALBERT where all input is lowercased. " | ||
"Trained on English Wikipedia + BooksCorpus." | ||
), | ||
"weights_url": "https://drive.google.com/uc?export=download&id=1IS6P97tqU31cztKLTp_xps3hBcUdfU32", | ||
"weights_hash": "af966fb03961c9dafa3925f962337552", | ||
"spm_proto_url": "https://drive.google.com/uc?export=download&id=1-0C5mWLPRxNaY3yuzdqut-Wi7VaKo2gX", | ||
"spm_proto_hash": "73e62ff8e90f951f24c8b907913039a5", | ||
}, | ||
"albert_extra_extra_large_en_uncased": { | ||
jbischof marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"config": { | ||
"vocabulary_size": 30000, | ||
"num_layers": 12, | ||
"num_heads": 64, | ||
"num_groups": 1, | ||
"num_inner_repetitions": 1, | ||
"embedding_dim": 128, | ||
"hidden_dim": 4096, | ||
"intermediate_dim": 16384, | ||
"dropout": 0, | ||
"max_sequence_length": 512, | ||
"num_segments": 2, | ||
}, | ||
"preprocessor_config": {}, | ||
"description": ( | ||
"Extra Extra Large size of ALBERT where all input is lowercased. " | ||
"Trained on English Wikipedia + BooksCorpus." | ||
), | ||
"weights_url": "https://drive.google.com/uc?export=download&id=1NcmanywBpby6n1uolJw2G4QPp_V0fHlx", | ||
"weights_hash": "5d44017f3a68b2a7e6f755c1070ad3ed", | ||
"spm_proto_url": "https://drive.google.com/uc?export=download&id=1-0C5mWLPRxNaY3yuzdqut-Wi7VaKo2gX", | ||
"spm_proto_hash": "73e62ff8e90f951f24c8b907913039a5", | ||
}, | ||
} |
This file contains 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,127 @@ | ||
# 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. | ||
"""Tests for loading pretrained model presets.""" | ||
|
||
import pytest | ||
import tensorflow as tf | ||
from absl.testing import parameterized | ||
|
||
from keras_nlp.models.albert.albert_backbone import AlbertBackbone | ||
from keras_nlp.models.albert.albert_preprocessor import AlbertPreprocessor | ||
from keras_nlp.models.albert.albert_tokenizer import AlbertTokenizer | ||
|
||
|
||
@pytest.mark.large | ||
class AlbertPresetSmokeTest(tf.test.TestCase, parameterized.TestCase): | ||
""" | ||
A smoke test for ALBERT presets we run continuously. | ||
|
||
This only tests the smallest weights we have available. Run with: | ||
`pytest keras_nlp/models/albert/albert_presets_test.py --run_large` | ||
""" | ||
|
||
def test_tokenizer_output(self): | ||
tokenizer = AlbertTokenizer.from_preset( | ||
"albert_base_en_uncased", | ||
) | ||
outputs = tokenizer("The quick brown fox.") | ||
expected_outputs = [13, 1, 438, 2231, 886, 2385, 9] | ||
self.assertAllEqual(outputs, expected_outputs) | ||
|
||
def test_preprocessor_output(self): | ||
preprocessor = AlbertPreprocessor.from_preset( | ||
"albert_base_en_uncased", | ||
sequence_length=4, | ||
) | ||
outputs = preprocessor("The quick brown fox.")["token_ids"] | ||
expected_outputs = [2, 13, 1, 3] | ||
self.assertAllEqual(outputs, expected_outputs) | ||
|
||
@parameterized.named_parameters( | ||
("preset_weights", True), ("random_weights", False) | ||
) | ||
def test_backbone_output(self, load_weights): | ||
input_data = { | ||
"token_ids": tf.constant([[2, 13, 1, 3]]), | ||
"segment_ids": tf.constant([[0, 0, 0, 0]]), | ||
"padding_mask": tf.constant([[1, 1, 1, 1]]), | ||
} | ||
model = AlbertBackbone.from_preset( | ||
"albert_base_en_uncased", load_weights=load_weights | ||
) | ||
outputs = model(input_data) | ||
if load_weights: | ||
outputs = outputs["sequence_output"][0, 0, :5] | ||
expected = [1.830863, 1.698645, -1.819195, -0.53382, -0.38114] | ||
self.assertAllClose(outputs, expected, atol=0.01, rtol=0.01) | ||
|
||
@parameterized.named_parameters( | ||
("albert_tokenizer", AlbertTokenizer), | ||
("albert_preprocessor", AlbertPreprocessor), | ||
("albert", AlbertBackbone), | ||
) | ||
def test_preset_docstring(self, cls): | ||
"""Check we did our docstring formatting correctly.""" | ||
for name in cls.presets: | ||
self.assertRegex(cls.from_preset.__doc__, name) | ||
|
||
@parameterized.named_parameters( | ||
("albert_tokenizer", AlbertTokenizer), | ||
("albert_preprocessor", AlbertPreprocessor), | ||
("albert", AlbertBackbone), | ||
) | ||
def test_unknown_preset_error(self, cls): | ||
# Not a preset name | ||
with self.assertRaises(ValueError): | ||
cls.from_preset("albert_base_en_uncased_clowntown") | ||
|
||
|
||
@pytest.mark.extra_large | ||
class AlbertPresetFullTest(tf.test.TestCase, parameterized.TestCase): | ||
""" | ||
Test the full enumeration of our preset. | ||
|
||
This tests every ALBERT preset and is only run manually. | ||
Run with: | ||
`pytest keras_nlp/models/albert/albert_presets_test.py --run_extra_large` | ||
""" | ||
|
||
@parameterized.named_parameters( | ||
("preset_weights", True), ("random_weights", False) | ||
) | ||
def test_load_albert(self, load_weights): | ||
for preset in AlbertBackbone.presets: | ||
model = AlbertBackbone.from_preset( | ||
preset, load_weights=load_weights | ||
) | ||
input_data = { | ||
"token_ids": tf.random.uniform( | ||
shape=(1, 512), dtype=tf.int64, maxval=model.vocabulary_size | ||
), | ||
"segment_ids": tf.constant( | ||
[0] * 200 + [1] * 312, shape=(1, 512) | ||
), | ||
"padding_mask": tf.constant([1] * 512, shape=(1, 512)), | ||
} | ||
model(input_data) | ||
|
||
def test_load_tokenizers(self): | ||
for preset in AlbertTokenizer.presets: | ||
tokenizer = AlbertTokenizer.from_preset(preset) | ||
tokenizer("The quick brown fox.") | ||
|
||
def test_load_preprocessors(self): | ||
for preset in AlbertPreprocessor.presets: | ||
preprocessor = AlbertPreprocessor.from_preset(preset) | ||
preprocessor("The quick brown fox.") |
This file contains 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
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.
Still need to review, but this is awesome! Super great to model how to do this for contributors.
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.
@mattdangerw, there is a cap on the size of the file you can download from GDrive. For example,
extra_large
andextra_extra_large
tests fail because their size > 200MB. I have observed the same for FNet. Instead of downloading the actual file, an HTML file is downloaded.