-
Notifications
You must be signed in to change notification settings - Fork 310
Add FNet Presets #659
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
Merged
Add FNet Presets #659
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a32c927
Add FNet presets
abheesht17 976bc5b
Small edits
abheesht17 da5d7f9
Merge branch 'keras-team:master' into f_net-presets
abheesht17 2cbc068
Remove backbone, preprocessor cruft
abheesht17 bda81db
Add GCP URLs
abheesht17 18cee5c
Fix
abheesht17 f176561
Fixes
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 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
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,55 @@ | ||
| # 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. | ||
| """FNet model preset configurations.""" | ||
|
|
||
| backbone_presets = { | ||
| "f_net_base_en": { | ||
| "config": { | ||
| "vocabulary_size": 32000, | ||
| "num_layers": 12, | ||
| "hidden_dim": 768, | ||
| "intermediate_dim": 3072, | ||
| "dropout": 0.1, | ||
| "max_sequence_length": 512, | ||
| "num_segments": 4, | ||
| }, | ||
| "preprocessor_config": {}, | ||
| "description": ( | ||
| "Base size of FNet. Trained on the C4 dataset (English)." | ||
| ), | ||
| "weights_url": "https://drive.google.com/uc?export=download&id=1WdEkxGkgWgo7nZMubwqE3A5N0rQDSiGt", | ||
| "weights_hash": "35db90842b85a985a0e54c86c00746fe", | ||
| "spm_proto_url": "https://drive.google.com/uc?export=download&id=1-2Qo9kNkV0ZRuGnK-n4a3m_ZAO_k-9rk", | ||
| "spm_proto_hash": "71c5f4610bef1daf116998a113a01f3d", | ||
| }, | ||
| "f_net_large_en": { | ||
| "config": { | ||
| "vocabulary_size": 32000, | ||
| "num_layers": 24, | ||
| "hidden_dim": 1024, | ||
| "intermediate_dim": 4096, | ||
| "dropout": 0.1, | ||
| "max_sequence_length": 512, | ||
| "num_segments": 4, | ||
| }, | ||
| "preprocessor_config": {}, | ||
| "description": ( | ||
| "Large size of FNet. Trained on the C4 dataset (English)." | ||
| ), | ||
| "weights_url": "https://drive.google.com/uc?export=download&id=1c2rnxtikHqovK4TqQt4G1yt1QF_69AkF", | ||
| "weights_hash": "7ae4a3faa67ff054f8cecffb5619f779", | ||
| "spm_proto_url": "https://drive.google.com/uc?export=download&id=1U3BxKgxmRxp-66DyPyBTQVuPCp4oHTwH", | ||
| "spm_proto_hash": "71c5f4610bef1daf116998a113a01f3d", | ||
| }, | ||
| } |
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,123 @@ | ||
| # 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.f_net.f_net_backbone import FNetBackbone | ||
| from keras_nlp.models.f_net.f_net_preprocessor import FNetPreprocessor | ||
| from keras_nlp.models.f_net.f_net_tokenizer import FNetTokenizer | ||
|
|
||
|
|
||
| @pytest.mark.large | ||
| class FNetPresetSmokeTest(tf.test.TestCase, parameterized.TestCase): | ||
| """ | ||
| A smoke test for FNet presets we run continuously. | ||
|
|
||
| This only tests the smallest weights we have available. Run with: | ||
| `pytest keras_nlp/models/f_net/f_net_presets_test.py --run_large` | ||
| """ | ||
|
|
||
| def test_tokenizer_output(self): | ||
| tokenizer = FNetTokenizer.from_preset( | ||
| "f_net_base_en", | ||
| ) | ||
| outputs = tokenizer("The quick brown fox.") | ||
| expected_outputs = [97, 1467, 5187, 26, 2521, 16678] | ||
| self.assertAllEqual(outputs, expected_outputs) | ||
|
|
||
| def test_preprocessor_output(self): | ||
| preprocessor = FNetPreprocessor.from_preset( | ||
| "f_net_base_en", | ||
| sequence_length=4, | ||
| ) | ||
| outputs = preprocessor("The quick brown fox.")["token_ids"] | ||
| expected_outputs = [4, 97, 1467, 5] | ||
| 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([[4, 97, 1467, 5]]), | ||
| "segment_ids": tf.constant([[0, 0, 0, 0]]), | ||
| } | ||
| model = FNetBackbone.from_preset( | ||
| "f_net_base_en", load_weights=load_weights | ||
| ) | ||
| outputs = model(input_data) | ||
| if load_weights: | ||
| outputs = outputs[0, 0, :5] | ||
| expected = [0.418, -0.116, -0.122, -1.847, -0.035] | ||
| self.assertAllClose(outputs, expected, atol=0.01, rtol=0.01) | ||
|
|
||
| @parameterized.named_parameters( | ||
| ("f_net_tokenizer", FNetTokenizer), | ||
| ("f_net_preprocessor", FNetPreprocessor), | ||
| ("f_net", FNetBackbone), | ||
| ) | ||
| 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( | ||
| ("f_net_tokenizer", FNetTokenizer), | ||
| ("f_net_preprocessor", FNetPreprocessor), | ||
| ("f_net", FNetBackbone), | ||
| ) | ||
| def test_unknown_preset_error(self, cls): | ||
| # Not a preset name | ||
| with self.assertRaises(ValueError): | ||
| cls.from_preset("f_net_base_en_clowntown") | ||
|
|
||
|
|
||
| @pytest.mark.extra_large | ||
| class FNetPresetFullTest(tf.test.TestCase, parameterized.TestCase): | ||
| """ | ||
| Test the full enumeration of our preset. | ||
|
|
||
| This tests every FNet preset and is only run manually. | ||
| Run with: | ||
| `pytest keras_nlp/models/f_net/f_net_presets_test.py --run_extra_large` | ||
| """ | ||
|
|
||
| @parameterized.named_parameters( | ||
| ("preset_weights", True), ("random_weights", False) | ||
| ) | ||
| def test_load_f_net(self, load_weights): | ||
| for preset in FNetBackbone.presets: | ||
| model = FNetBackbone.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) | ||
| ), | ||
| } | ||
| model(input_data) | ||
|
|
||
| def test_load_tokenizers(self): | ||
| for preset in FNetTokenizer.presets: | ||
| tokenizer = FNetTokenizer.from_preset(preset) | ||
| tokenizer("The quick brown fox.") | ||
|
|
||
| def test_load_preprocessors(self): | ||
| for preset in FNetPreprocessor.presets: | ||
| preprocessor = FNetPreprocessor.from_preset(preset) | ||
| preprocessor("The quick brown fox.") |
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
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.
this can be removed now, and the from_preset redefinition