-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Fix a bunch of slow tests #8634
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -12,8 +12,7 @@ | |
| # 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. | ||
|
|
||
|
|
||
| import tempfile | ||
| import unittest | ||
|
|
||
| from transformers import is_tf_available | ||
|
|
@@ -124,8 +123,6 @@ def prepare_config_and_inputs(self): | |
| type_vocab_size=self.type_vocab_size, | ||
| is_decoder=False, | ||
| initializer_range=self.initializer_range, | ||
| # MODIFY | ||
|
Contributor
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. Deleted all those #MODIFY statements at the cost of having to initialize the |
||
| return_dict=False, | ||
| ) | ||
| config = DPRConfig(projection_dim=self.projection_dim, **config.to_dict()) | ||
|
|
||
|
|
@@ -137,7 +134,7 @@ def create_and_check_dpr_context_encoder( | |
| model = TFDPRContextEncoder(config=config) | ||
| result = model(input_ids, attention_mask=input_mask, token_type_ids=token_type_ids) | ||
| result = model(input_ids, token_type_ids=token_type_ids) | ||
| result = model(input_ids, return_dict=True) # MODIFY | ||
| result = model(input_ids) | ||
| self.parent.assertEqual(result.pooler_output.shape, (self.batch_size, self.projection_dim or self.hidden_size)) | ||
|
|
||
| def create_and_check_dpr_question_encoder( | ||
|
|
@@ -146,14 +143,14 @@ def create_and_check_dpr_question_encoder( | |
| model = TFDPRQuestionEncoder(config=config) | ||
| result = model(input_ids, attention_mask=input_mask, token_type_ids=token_type_ids) | ||
| result = model(input_ids, token_type_ids=token_type_ids) | ||
| result = model(input_ids, return_dict=True) # MODIFY | ||
| result = model(input_ids) | ||
| self.parent.assertEqual(result.pooler_output.shape, (self.batch_size, self.projection_dim or self.hidden_size)) | ||
|
|
||
| def create_and_check_dpr_reader( | ||
| self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels | ||
| ): | ||
| model = TFDPRReader(config=config) | ||
| result = model(input_ids, attention_mask=input_mask, return_dict=True) # MODIFY | ||
| result = model(input_ids, attention_mask=input_mask) | ||
|
|
||
| self.parent.assertEqual(result.start_logits.shape, (self.batch_size, self.seq_length)) | ||
| self.parent.assertEqual(result.end_logits.shape, (self.batch_size, self.seq_length)) | ||
|
|
@@ -214,27 +211,61 @@ def test_dpr_reader_model(self): | |
| @slow | ||
| def test_model_from_pretrained(self): | ||
| for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: | ||
| model = TFDPRContextEncoder.from_pretrained(model_name, from_pt=True) | ||
| model = TFDPRContextEncoder.from_pretrained(model_name) | ||
| self.assertIsNotNone(model) | ||
|
|
||
| for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: | ||
| model = TFDPRContextEncoder.from_pretrained(model_name, from_pt=True) | ||
| model = TFDPRContextEncoder.from_pretrained(model_name) | ||
| self.assertIsNotNone(model) | ||
|
|
||
| for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: | ||
| model = TFDPRQuestionEncoder.from_pretrained(model_name, from_pt=True) | ||
| model = TFDPRQuestionEncoder.from_pretrained(model_name) | ||
| self.assertIsNotNone(model) | ||
|
|
||
| for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: | ||
| model = TFDPRReader.from_pretrained(model_name, from_pt=True) | ||
| model = TFDPRReader.from_pretrained(model_name) | ||
| self.assertIsNotNone(model) | ||
|
|
||
| @slow | ||
| def test_saved_model_with_attentions_output(self): | ||
| config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common() | ||
| config.output_attentions = True | ||
|
|
||
| encoder_seq_length = getattr(self.model_tester, "encoder_seq_length", self.model_tester.seq_length) | ||
| encoder_key_length = getattr(self.model_tester, "key_length", encoder_seq_length) | ||
|
|
||
| for model_class in self.all_model_classes: | ||
| print(model_class) | ||
| class_inputs_dict = self._prepare_for_class(inputs_dict, model_class) | ||
| model = model_class(config) | ||
| num_out = len(model(class_inputs_dict)) | ||
| model._saved_model_inputs_spec = None | ||
| model._set_save_spec(class_inputs_dict) | ||
|
|
||
| with tempfile.TemporaryDirectory() as tmpdirname: | ||
| tf.saved_model.save(model, tmpdirname) | ||
| model = tf.keras.models.load_model(tmpdirname) | ||
| outputs = model(class_inputs_dict) | ||
|
|
||
| if self.is_encoder_decoder: | ||
| output = outputs["encoder_attentions"] if isinstance(outputs, dict) else outputs[-1] | ||
| else: | ||
| output = outputs["attentions"] if isinstance(outputs, dict) else outputs[-1] | ||
|
|
||
| attentions = [t.numpy() for t in output] | ||
| self.assertEqual(len(outputs), num_out) | ||
| self.assertEqual(len(attentions), self.model_tester.num_hidden_layers) | ||
| self.assertListEqual( | ||
| list(attentions[0].shape[-3:]), | ||
| [self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length], | ||
| ) | ||
|
|
||
|
|
||
| @require_tf | ||
| class TFDPRModelIntegrationTest(unittest.TestCase): | ||
| @slow | ||
| def test_inference_no_head(self): | ||
| model = TFDPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base", return_dict=False) | ||
| model = TFDPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base") | ||
|
|
||
| input_ids = tf.constant( | ||
| [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]] | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
@LysandreJik @sgugger - for some reason, these outputs need to be initialized with something or else our much-loved tests
test_compile_tf_modelandtest_saved_model_with_...fail. Tbh, I don't know why this is - seems to be some weird tf graph problem with positional arguments...-> @sgugger is this fine for you?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.
Yes, I remember that now. Had to do the same for all tf ModelOutput. This is fine indeed!
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.
Great catch! :-)