Repository: huggingface/transformers
File: tests/models/seamless_m4t/test_modeling_seamless_m4t.py (Python)

Code before:
            inputs_dict["output_hidden_states"] = True
            model = model_class(config)
            model.to(torch_device)
            model.eval()
            with torch.no_grad():
                outputs = model(**self._prepare_for_class(inputs_dict, model_class))

            if hasattr(self.model_tester, "num_hidden_states_types"):
                added_hidden_states = self.model_tester.num_hidden_states_types
            elif self.is_encoder_decoder:
                added_hidden_states = 2
            else:
                added_hidden_states = 1
            self.assertEqual(out_len + added_hidden_states, len(outputs))

            self_attentions = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions

            self.assertEqual(len(self_attentions), self.model_tester.num_hidden_layers)
            self.assertListEqual(
                list(self_attentions[0].shape[-3:]),
                [self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length],
            )

    def test_retain_grad_hidden_states_attentions(self):
        # When training the model, speech encoder layerdrop can skip layers and return None attentions.
        # Disable layerdrop for this test to ensure retain_grad targets are present.
        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
        config.speech_encoder_layerdrop = 0.0
        config.encoder_layerdrop = 0.0
        config.decoder_layerdrop = 0.0

        for k in config.sub_configs:
            if getattr(config, k) is not None:
                getattr(config, k).output_hidden_states = True

        config.output_hidden_states = True
        config.output_attentions = self.has_attentions

        for k in config.sub_configs:
            if getattr(config, k) is not None:
                getattr(config, k).output_attentions = self.has_attentions

        if self.has_attentions:
            config._attn_implementation = "eager"

        model_class = self.all_model_classes[0]
        model = model_class._from_config(config, attn_implementation="eager")
        model.to(torch_device)

        inputs = self._prepare_for_class(inputs_dict, model_class)
        outputs = model(**inputs)

Review comment (suggestion):
This fix is fine, but maybe we could just disable layerdrop in the test config for this model to reduce LOC changes? It seems like an obvious source of massive variability, and layerdrop is never used in modern models.

Code after:
            inputs_dict["output_hidden_states"] = True
            model = model_class(config)
            model.to(torch_device)
            model.eval()
            with torch.no_grad():
                outputs = model(**self._prepare_for_class(inputs_dict, model_class))

            if hasattr(self.model_tester, "num_hidden_states_types"):
                added_hidden_states = self.model_tester.num_hidden_states_types
            elif self.is_encoder_decoder:
                added_hidden_states = 2
            else:
                added_hidden_states = 1
            self.assertEqual(out_len + added_hidden_states, len(outputs))

            self_attentions = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions

            self.assertEqual(len(self_attentions), self.model_tester.num_hidden_layers)
            self.assertListEqual(
                list(self_attentions[0].shape[-3:]),
                [self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length],
            )

    def _prepare_config_and_inputs_for_retain_grad_hidden_states_attentions(self):
        # Layerdrop can skip layers and return None attentions. Disable it for this test.
        config, inputs_dict = super()._prepare_config_and_inputs_for_retain_grad_hidden_states_attentions()
        config.speech_encoder_layerdrop = 0.0
        config.encoder_layerdrop = 0.0
        config.decoder_layerdrop = 0.0
        return config, inputs_dict


@require_torch
class SeamlessM4TModelWithTextInputTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
    is_encoder_decoder = True
    test_missing_keys = False

    test_resize_embeddings = True

    all_model_classes = (
        (
            SeamlessM4TModel,
            SeamlessM4TForTextToSpeech,
            SeamlessM4TForTextToText,
        )
        if is_torch_available()
        else ()
    )
    # Doesn't run generation tests. Has custom generation method with a different interface
    all_generative_model_classes = ()
    pipeline_model_mapping = (