This repository was archived by the owner on Sep 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 814
Testing T5Model #1848
Merged
pmabbo13
merged 15 commits into
pytorch:main
from
pmabbo13:feature/t5-integration-tests
Jul 21, 2022
Merged
Testing T5Model #1848
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d8fe63e
test bundler api
pmabbo13 bea35bc
upload reference results to verify model correctness
pmabbo13 9837f4a
test for model correctness against reference results
pmabbo13 da193c9
Revert "test for model correctness against reference results"
pmabbo13 a6ee730
Revert "upload reference results to verify model correctness"
pmabbo13 4712437
Revert "test bundler api"
pmabbo13 9961fbe
test bundler api
pmabbo13 dc4cf1e
test bundler api
pmabbo13 d28fc65
Merge branch 'feature/t5-integration-tests' of https://github.com/pma…
pmabbo13 5f914ba
upload reference results to verify model correctness
pmabbo13 aa13fa2
test for model correctness against reference results
pmabbo13 38a5d24
nit correction
pmabbo13 b9df22f
test bundler when model is encoder-only
pmabbo13 e32c99e
correcting typo
pmabbo13 d1a23f0
remove redundant test for encoder-only
pmabbo13 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
Binary file not shown.
Binary file not shown.
Empty file.
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,35 @@ | ||
| import torch | ||
| from test.common.assets import get_asset_path | ||
| from test.common.torchtext_test_case import TorchtextTestCase | ||
| from torchtext.prototype.models import ( | ||
| T5_BASE_ENCODER, | ||
| T5_BASE, | ||
| ) | ||
|
|
||
|
|
||
| class TestT5(TorchtextTestCase): | ||
| def _t5_model(self, t5_model, expected_asset_name, model_input): | ||
| """Verify that pre-trained T5 models in torchtext produce | ||
| the same output as the HuggingFace reference implementation. | ||
| """ | ||
| expected_asset_path = get_asset_path(expected_asset_name) | ||
| model = t5_model.get_model() | ||
| model = model.eval() | ||
|
|
||
| if model.encoder_only: | ||
| actual = model(model_input)["encoder_output"] | ||
| else: | ||
| actual = model(model_input)["decoder_output"] | ||
|
|
||
| expected = torch.load(expected_asset_path) | ||
| torch.testing.assert_close(actual, expected) | ||
|
|
||
| def test_t5_base_encoder_model(self): | ||
| expected_asset_name = "t5.base.encoder.output.pt" | ||
| model_input = torch.tensor([[1, 2, 3, 4, 5, 6], [7, 8, 9, 0, 0, 0]]) | ||
| self._t5_model(t5_model=T5_BASE_ENCODER, expected_asset_name=expected_asset_name, model_input=model_input) | ||
|
|
||
| def test_t5_base_model(self): | ||
| expected_asset_name = "t5.base.output.pt" | ||
| model_input = torch.tensor([[1, 2, 3, 4, 5, 6], [7, 8, 9, 0, 0, 0]]) | ||
| self._t5_model(t5_model=T5_BASE, expected_asset_name=expected_asset_name, model_input=model_input) | ||
Empty file.
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,117 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| from test.common.torchtext_test_case import TorchtextTestCase | ||
|
|
||
|
|
||
| class TestModels(TorchtextTestCase): | ||
| def test_t5_bundler_build_model(self): | ||
| from torchtext.prototype.models import T5Conf, T5Model, T5Bundle | ||
|
|
||
| # case: user provide encoder checkpoint state dict | ||
| dummy_encoder_conf = T5Conf( | ||
| encoder_only=True, | ||
| vocab_size=10, | ||
| embedding_dim=16, | ||
| ffn_dimension=64, | ||
| num_attention_heads=2, | ||
| num_encoder_layers=2, | ||
| ) | ||
| dummy_t5_encoder = T5Model(dummy_encoder_conf) | ||
| t5_encoder_model = T5Bundle.build_model(config=dummy_encoder_conf, checkpoint=dummy_t5_encoder.state_dict()) | ||
| self.assertEqual(t5_encoder_model.state_dict(), dummy_t5_encoder.state_dict()) | ||
|
|
||
| # case: user provide encoder-decoder checkpoint state dict | ||
| dummy_t5_conf = T5Conf( | ||
| encoder_only=False, | ||
| vocab_size=10, | ||
| embedding_dim=16, | ||
| ffn_dimension=64, | ||
| num_attention_heads=2, | ||
| num_encoder_layers=2, | ||
| ) | ||
| dummy_t5 = T5Model(dummy_t5_conf) | ||
| t5_model = T5Bundle.build_model(config=dummy_t5_conf, checkpoint=dummy_t5.state_dict()) | ||
| self.assertEqual(t5_model.state_dict(), dummy_t5.state_dict()) | ||
|
|
||
| @patch("logging.Logger.warning") | ||
| def test_t5_bundler_get_model(self, mock): | ||
| from torchtext.prototype.models import T5Conf, T5Bundle | ||
|
|
||
| # encoder-only | ||
| dummy_encoder_conf = T5Conf( | ||
| encoder_only=True, | ||
| vocab_size=10, | ||
| embedding_dim=16, | ||
| ffn_dimension=64, | ||
| num_attention_heads=2, | ||
| num_encoder_layers=2, | ||
| ) | ||
| encoder_bundle = T5Bundle(dummy_encoder_conf) | ||
| encoder_bundle.get_model(load_weights=False, freeze_model=True) | ||
| mock.assert_called_with( | ||
| "The model is not loaded with pre-trained weights. Setting freeze_model to True will hinder model from learning appropriate weights." | ||
| ) | ||
|
|
||
| # encoder-decoder | ||
| dummy_t5_conf = T5Conf( | ||
| encoder_only=False, | ||
| vocab_size=10, | ||
| embedding_dim=16, | ||
| ffn_dimension=64, | ||
| num_attention_heads=2, | ||
| num_encoder_layers=2, | ||
| ) | ||
| t5_bundle = T5Bundle(dummy_t5_conf) | ||
| t5_bundle.get_model(load_weights=False, freeze_model=True) | ||
| mock.assert_called_with( | ||
| "The model is not loaded with pre-trained weights. Setting freeze_model to True will hinder model from learning appropriate weights." | ||
| ) | ||
|
|
||
| def test_t5_bundler_raise_checkpoint(self): | ||
| from torchtext.prototype.models import T5Conf, T5Bundle | ||
|
|
||
| # encoder-only | ||
| with self.assertRaises(TypeError): | ||
| dummy_encoder_conf = T5Conf( | ||
| encoder_only=True, | ||
| vocab_size=10, | ||
| embedding_dim=16, | ||
| ffn_dimension=64, | ||
| num_attention_heads=2, | ||
| num_encoder_layers=2, | ||
| ) | ||
| T5Bundle.build_model( | ||
| config=dummy_encoder_conf, | ||
| freeze_model=True, | ||
| checkpoint=1, | ||
| ) | ||
|
|
||
| # encoder-decoder | ||
| with self.assertRaises(TypeError): | ||
| dummy_t5_conf = T5Conf( | ||
| encoder_only=False, | ||
| vocab_size=10, | ||
| embedding_dim=16, | ||
| ffn_dimension=64, | ||
| num_attention_heads=2, | ||
| num_encoder_layers=2, | ||
| ) | ||
| T5Bundle.build_model( | ||
| config=dummy_t5_conf, | ||
| freeze_model=True, | ||
| checkpoint=1, | ||
| ) | ||
|
|
||
| def test_t5_bundler_conf_property(self): | ||
pmabbo13 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from torchtext.prototype.models import T5Conf, T5Bundle | ||
|
|
||
| dummy_t5_conf = T5Conf( | ||
| encoder_only=False, | ||
| vocab_size=10, | ||
| embedding_dim=16, | ||
| ffn_dimension=64, | ||
| num_attention_heads=2, | ||
| num_encoder_layers=2, | ||
| ) | ||
| t5_bundle = T5Bundle(dummy_t5_conf) | ||
| self.assertTrue(isinstance(t5_bundle.config, T5Conf)) | ||
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 something to note, when we implement the transform for the model, we probably want to update the test to pass in an input string to the
_t5_modelmethod. The helper function will be responsible for applying the transform on the input string to get the tensor that can be passed into the model (code pointer). TheT5Bundleclass will also need to be updated to store the model transform as a member variable (code pointer).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.
sounds good, will keep this in mind for the next task!