Skip to content

Commit 2acae50

Browse files
authored
Reduce the time spent for the TF slow tests (#10152)
* rework savedmodel slow test * Improve savedmodel tests * Remove useless content
1 parent 14ed3b9 commit 2acae50

7 files changed

+91
-166
lines changed

src/transformers/modeling_tf_utils.py

-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ def booleans_processing(config, **kwargs):
283283

284284
if "use_cache" in kwargs:
285285
final_booleans["use_cache"] = kwargs["use_cache"] if kwargs["use_cache"] is not None else config.use_cache
286-
287286
else:
288287
if (
289288
kwargs["output_attentions"] is not None

tests/test_modeling_tf_common.py

+48-93
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,54 @@ def test_saved_model_creation(self):
202202
saved_model_dir = os.path.join(tmpdirname, "saved_model", "1")
203203
self.assertTrue(os.path.exists(saved_model_dir))
204204

205+
@slow
206+
def test_saved_model_creation_extended(self):
207+
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
208+
config.output_hidden_states = True
209+
config.output_attentions = True
210+
211+
if hasattr(config, "use_cache"):
212+
config.use_cache = True
213+
214+
encoder_seq_length = getattr(self.model_tester, "encoder_seq_length", self.model_tester.seq_length)
215+
encoder_key_length = getattr(self.model_tester, "key_length", encoder_seq_length)
216+
217+
for model_class in self.all_model_classes:
218+
class_inputs_dict = self._prepare_for_class(inputs_dict, model_class)
219+
model = model_class(config)
220+
num_out = len(model(class_inputs_dict))
221+
222+
with tempfile.TemporaryDirectory() as tmpdirname:
223+
model.save_pretrained(tmpdirname, saved_model=True)
224+
saved_model_dir = os.path.join(tmpdirname, "saved_model", "1")
225+
model = tf.keras.models.load_model(saved_model_dir)
226+
outputs = model(class_inputs_dict)
227+
228+
if self.is_encoder_decoder:
229+
output_hidden_states = outputs["encoder_hidden_states"]
230+
output_attentions = outputs["encoder_attentions"]
231+
else:
232+
output_hidden_states = outputs["hidden_states"]
233+
output_attentions = outputs["attentions"]
234+
235+
self.assertEqual(len(outputs), num_out)
236+
237+
expected_num_layers = getattr(
238+
self.model_tester, "expected_num_hidden_layers", self.model_tester.num_hidden_layers + 1
239+
)
240+
241+
self.assertEqual(len(output_hidden_states), expected_num_layers)
242+
self.assertListEqual(
243+
list(output_hidden_states[0].shape[-2:]),
244+
[self.model_tester.seq_length, self.model_tester.hidden_size],
245+
)
246+
247+
self.assertEqual(len(output_attentions), self.model_tester.num_hidden_layers)
248+
self.assertListEqual(
249+
list(output_attentions[0].shape[-3:]),
250+
[self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length],
251+
)
252+
205253
def test_onnx_compliancy(self):
206254
if not self.test_onnx:
207255
return
@@ -263,98 +311,6 @@ def test_onnx_runtime_optimize(self):
263311

264312
onnxruntime.InferenceSession(onnx_model.SerializeToString())
265313

266-
@slow
267-
def test_saved_model_creation_extended(self):
268-
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
269-
config.output_hidden_states = True
270-
config.output_attentions = True
271-
272-
if hasattr(config, "use_cache"):
273-
config.use_cache = True
274-
275-
for model_class in self.all_model_classes:
276-
class_inputs_dict = self._prepare_for_class(inputs_dict, model_class)
277-
model = model_class(config)
278-
279-
model(class_inputs_dict)
280-
281-
with tempfile.TemporaryDirectory() as tmpdirname:
282-
model.save_pretrained(tmpdirname, saved_model=True)
283-
saved_model_dir = os.path.join(tmpdirname, "saved_model", "1")
284-
self.assertTrue(os.path.exists(saved_model_dir))
285-
286-
@slow
287-
def test_saved_model_with_hidden_states_output(self):
288-
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
289-
config.output_hidden_states = True
290-
config.output_attentions = False
291-
292-
if hasattr(config, "use_cache"):
293-
config.use_cache = False
294-
295-
for model_class in self.all_model_classes:
296-
class_inputs_dict = self._prepare_for_class(inputs_dict, model_class)
297-
model = model_class(config)
298-
num_out = len(model(class_inputs_dict))
299-
300-
with tempfile.TemporaryDirectory() as tmpdirname:
301-
model.save_pretrained(tmpdirname, saved_model=True)
302-
saved_model_dir = os.path.join(tmpdirname, "saved_model", "1")
303-
model = tf.keras.models.load_model(saved_model_dir)
304-
outputs = model(class_inputs_dict)
305-
306-
if self.is_encoder_decoder:
307-
output = outputs["encoder_hidden_states"]
308-
else:
309-
output = outputs["hidden_states"]
310-
311-
self.assertEqual(len(outputs), num_out)
312-
313-
expected_num_layers = getattr(
314-
self.model_tester, "expected_num_hidden_layers", self.model_tester.num_hidden_layers + 1
315-
)
316-
317-
self.assertEqual(len(output), expected_num_layers)
318-
self.assertListEqual(
319-
list(output[0].shape[-2:]),
320-
[self.model_tester.seq_length, self.model_tester.hidden_size],
321-
)
322-
323-
@slow
324-
def test_saved_model_with_attentions_output(self):
325-
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
326-
config.output_attentions = True
327-
config.output_hidden_states = False
328-
329-
if hasattr(config, "use_cache"):
330-
config.use_cache = False
331-
332-
encoder_seq_length = getattr(self.model_tester, "encoder_seq_length", self.model_tester.seq_length)
333-
encoder_key_length = getattr(self.model_tester, "key_length", encoder_seq_length)
334-
335-
for model_class in self.all_model_classes:
336-
class_inputs_dict = self._prepare_for_class(inputs_dict, model_class)
337-
model = model_class(config)
338-
num_out = len(model(class_inputs_dict))
339-
340-
with tempfile.TemporaryDirectory() as tmpdirname:
341-
model.save_pretrained(tmpdirname, saved_model=True)
342-
saved_model_dir = os.path.join(tmpdirname, "saved_model", "1")
343-
model = tf.keras.models.load_model(saved_model_dir)
344-
outputs = model(class_inputs_dict)
345-
346-
if self.is_encoder_decoder:
347-
output = outputs["encoder_attentions"]
348-
else:
349-
output = outputs["attentions"]
350-
351-
self.assertEqual(len(outputs), num_out)
352-
self.assertEqual(len(output), self.model_tester.num_hidden_layers)
353-
self.assertListEqual(
354-
list(output[0].shape[-3:]),
355-
[self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length],
356-
)
357-
358314
def test_mixed_precision(self):
359315
tf.keras.mixed_precision.experimental.set_policy("mixed_float16")
360316

@@ -554,7 +510,6 @@ def test_train_pipeline_custom_model(self):
554510
shared = TFSharedEmbeddings(self.model_tester.vocab_size, self.model_tester.hidden_size, name="shared")
555511
config.use_cache = False
556512
main_layer = main_layer_class(config, embed_tokens=shared)
557-
del inputs_dict["use_cache"]
558513
else:
559514
main_layer = main_layer_class(config)
560515

tests/test_modeling_tf_convbert.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,13 @@ def test_for_token_classification(self):
273273
self.model_tester.create_and_check_for_token_classification(*config_and_inputs)
274274

275275
@slow
276-
def test_saved_model_with_attentions_output(self):
276+
def test_saved_model_creation_extended(self):
277277
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
278+
config.output_hidden_states = True
278279
config.output_attentions = True
279-
config.output_hidden_states = False
280280

281281
if hasattr(config, "use_cache"):
282-
config.use_cache = False
282+
config.use_cache = True
283283

284284
encoder_seq_length = getattr(self.model_tester, "encoder_seq_length", self.model_tester.seq_length)
285285
encoder_key_length = getattr(self.model_tester, "key_length", encoder_seq_length)
@@ -291,14 +291,32 @@ def test_saved_model_with_attentions_output(self):
291291

292292
with tempfile.TemporaryDirectory() as tmpdirname:
293293
model.save_pretrained(tmpdirname, saved_model=True)
294-
model = tf.keras.models.load_model(os.path.join(tmpdirname, "saved_model", "1"))
294+
saved_model_dir = os.path.join(tmpdirname, "saved_model", "1")
295+
model = tf.keras.models.load_model(saved_model_dir)
295296
outputs = model(class_inputs_dict)
296-
output = outputs["attentions"]
297+
298+
if self.is_encoder_decoder:
299+
output_hidden_states = outputs["encoder_hidden_states"]
300+
output_attentions = outputs["encoder_attentions"]
301+
else:
302+
output_hidden_states = outputs["hidden_states"]
303+
output_attentions = outputs["attentions"]
297304

298305
self.assertEqual(len(outputs), num_out)
299-
self.assertEqual(len(output), self.model_tester.num_hidden_layers)
306+
307+
expected_num_layers = getattr(
308+
self.model_tester, "expected_num_hidden_layers", self.model_tester.num_hidden_layers + 1
309+
)
310+
311+
self.assertEqual(len(output_hidden_states), expected_num_layers)
312+
self.assertListEqual(
313+
list(output_hidden_states[0].shape[-2:]),
314+
[self.model_tester.seq_length, self.model_tester.hidden_size],
315+
)
316+
317+
self.assertEqual(len(output_attentions), self.model_tester.num_hidden_layers)
300318
self.assertListEqual(
301-
list(output[0].shape[-3:]),
319+
list(output_attentions[0].shape[-3:]),
302320
[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length],
303321
)
304322

tests/test_modeling_tf_led.py

-17
Original file line numberDiff line numberDiff line change
@@ -370,27 +370,10 @@ def test_xla_mode(self):
370370
# TODO JP: Make LED XLA compliant
371371
pass
372372

373-
def test_saved_model_with_attentions_output(self):
374-
# Temporarily disable this test in order to find
375-
# how to better handle it without timing out the CI
376-
pass
377-
378-
@slow
379-
def test_saved_model_with_hidden_states_output(self):
380-
# Temporarily disable this test in order to find
381-
# how to better handle it without timing out the CI
382-
pass
383-
384373
def test_saved_model_creation(self):
385374
# This test is too long (>30sec) and makes fail the CI
386375
pass
387376

388-
@slow
389-
def test_saved_model_creation_extended(self):
390-
# Temporarily disable this test in order to find
391-
# how to better handle it without timing out the CI
392-
pass
393-
394377

395378
def _assert_tensors_equal(a, b, atol=1e-12, prefix=""):
396379
"""If tensors not close, or a and b arent both tensors, raise a nice Assertion error."""

tests/test_modeling_tf_longformer.py

-18
Original file line numberDiff line numberDiff line change
@@ -339,28 +339,10 @@ def test_for_multiple_choice(self):
339339
config_and_inputs = self.model_tester.prepare_config_and_inputs()
340340
self.model_tester.create_and_check_for_multiple_choice(*config_and_inputs)
341341

342-
@slow
343-
def test_saved_model_with_attentions_output(self):
344-
# Temporarily disable this test in order to find
345-
# how to better handle it without timing out the CI
346-
pass
347-
348-
@slow
349-
def test_saved_model_with_hidden_states_output(self):
350-
# Temporarily disable this test in order to find
351-
# how to better handle it without timing out the CI
352-
pass
353-
354342
def test_saved_model_creation(self):
355343
# This test is too long (>30sec) and makes fail the CI
356344
pass
357345

358-
@slow
359-
def test_saved_model_creation_extended(self):
360-
# Temporarily disable this test in order to find
361-
# how to better handle it without timing out the CI
362-
pass
363-
364346
def test_mixed_precision(self):
365347
# TODO JP: Make Longformer float16 compliant
366348
pass

tests/test_modeling_tf_lxmert.py

+18-29
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import os
1617
import tempfile
1718
import unittest
1819

@@ -710,23 +711,34 @@ def test_mixed_precision(self):
710711
pass
711712

712713
@slow
713-
def test_saved_model_with_hidden_states_output(self):
714+
def test_saved_model_creation_extended(self):
714715
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
715716
config.output_hidden_states = True
717+
config.output_attentions = True
718+
719+
if hasattr(config, "use_cache"):
720+
config.use_cache = True
721+
722+
encoder_seq_length = getattr(self.model_tester, "encoder_seq_length", self.model_tester.seq_length)
723+
encoder_key_length = getattr(self.model_tester, "key_length", encoder_seq_length)
716724

717725
for model_class in self.all_model_classes:
718726
class_inputs_dict = self._prepare_for_class(inputs_dict, model_class)
719727
model = model_class(config)
720-
model._saved_model_inputs_spec = None
721-
model._set_save_spec(class_inputs_dict)
728+
num_out = len(model(class_inputs_dict))
722729

723730
with tempfile.TemporaryDirectory() as tmpdirname:
724-
tf.saved_model.save(model, tmpdirname)
725-
model = tf.keras.models.load_model(tmpdirname)
731+
model.save_pretrained(tmpdirname, saved_model=True)
732+
saved_model_dir = os.path.join(tmpdirname, "saved_model", "1")
733+
model = tf.keras.models.load_model(saved_model_dir)
726734
outputs = model(class_inputs_dict)
727-
728735
language_hidden_states = outputs["language_hidden_states"]
729736
vision_hidden_states = outputs["vision_hidden_states"]
737+
language_attentions = outputs["language_attentions"]
738+
vision_attentions = outputs["vision_attentions"]
739+
cross_encoder_attentions = outputs["cross_encoder_attentions"]
740+
741+
self.assertEqual(len(outputs), num_out)
730742

731743
self.assertEqual(len(language_hidden_states), self.model_tester.num_hidden_layers["language"] + 1)
732744
self.assertEqual(len(vision_hidden_states), self.model_tester.num_hidden_layers["vision"] + 1)
@@ -743,29 +755,6 @@ def test_saved_model_with_hidden_states_output(self):
743755
[num_visual_features, self.model_tester.hidden_size],
744756
)
745757

746-
@slow
747-
def test_saved_model_with_attentions_output(self):
748-
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
749-
config.output_attentions = True
750-
751-
encoder_seq_length = getattr(self.model_tester, "encoder_seq_length", self.model_tester.seq_length)
752-
encoder_key_length = getattr(self.model_tester, "key_length", encoder_seq_length)
753-
754-
for model_class in self.all_model_classes:
755-
class_inputs_dict = self._prepare_for_class(inputs_dict, model_class)
756-
model = model_class(config)
757-
model._saved_model_inputs_spec = None
758-
model._set_save_spec(class_inputs_dict)
759-
760-
with tempfile.TemporaryDirectory() as tmpdirname:
761-
tf.saved_model.save(model, tmpdirname)
762-
model = tf.keras.models.load_model(tmpdirname)
763-
outputs = model(class_inputs_dict)
764-
765-
language_attentions = outputs["language_attentions"]
766-
vision_attentions = outputs["vision_attentions"]
767-
cross_encoder_attentions = outputs["cross_encoder_attentions"]
768-
769758
self.assertEqual(len(language_attentions), self.model_tester.num_hidden_layers["language"])
770759
self.assertEqual(len(vision_attentions), self.model_tester.num_hidden_layers["vision"])
771760
self.assertEqual(len(cross_encoder_attentions), self.model_tester.num_hidden_layers["cross_encoder"])

tests/test_modeling_tf_t5.py

-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ def prepare_config_and_inputs_for_common(self):
237237
"input_ids": input_ids,
238238
"decoder_input_ids": input_ids,
239239
"decoder_attention_mask": input_mask,
240-
"use_cache": False,
241240
}
242241
return config, inputs_dict
243242

0 commit comments

Comments
 (0)