'''simple docstring'''


from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import SwinConfig
from transformers.testing_utils import require_tf, require_vision, slow, to_atuple
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers.models.swin.modeling_tf_swin import (
     TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
     TFSwinForImageClassification,
     TFSwinForMaskedImageModeling,
     TFSwinModel,
 )


if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor






class      __SCREAMING_SNAKE_CASE						:







  '''simple docstring'''






  def __init__( self      ,		snake_case_      ,		snake_case_=1_3      ,		snake_case_=3_2      ,		snake_case_=2      ,		snake_case_=3      ,		snake_case_=1_6      ,		snake_case_=[1, 2, 1]      ,		snake_case_=[2, 2, 4]      ,		snake_case_=2      ,		snake_case_=2.0      ,		snake_case_=True      ,		snake_case_=0.0      ,		snake_case_=0.0      ,		snake_case_=0.1      ,		snake_case_="gelu"      ,		snake_case_=False      ,		snake_case_=True      ,		snake_case_=0.02      ,		snake_case_=1E-5      ,		snake_case_=True      ,		snake_case_=None      ,		snake_case_=True      ,		snake_case_=1_0      ,		snake_case_=8      ,		):




       '''simple docstring'''

       UpperCAmelCase_							:   Any   =      parent
       UpperCAmelCase_							:   int   =      batch_size
       UpperCAmelCase_							:   Any   =      image_size
       UpperCAmelCase_							:   List[str]   =      patch_size
       UpperCAmelCase_							:   List[str]   =      num_channels
       UpperCAmelCase_							:   Union[str, Any]   =      embed_dim
       UpperCAmelCase_							:   Any   =      depths
       UpperCAmelCase_							:   Tuple   =      num_heads
       UpperCAmelCase_							:   Any   =      window_size
       UpperCAmelCase_							:   List[str]   =      mlp_ratio
       UpperCAmelCase_							:   List[str]   =      qkv_bias
       UpperCAmelCase_							:   Union[str, Any]   =      hidden_dropout_prob
       UpperCAmelCase_							:   str   =      attention_probs_dropout_prob
       UpperCAmelCase_							:   Any   =      drop_path_rate
       UpperCAmelCase_							:   Any   =      hidden_act
       UpperCAmelCase_							:   Tuple   =      use_absolute_embeddings
       UpperCAmelCase_							:   Optional[int]   =      patch_norm
       UpperCAmelCase_							:   int   =      layer_norm_eps
       UpperCAmelCase_							:   Union[str, Any]   =      initializer_range
       UpperCAmelCase_							:   int   =      is_training
       UpperCAmelCase_							:   List[str]   =      scope
       UpperCAmelCase_							:   List[Any]   =      use_labels
       UpperCAmelCase_							:   Optional[Any]   =      type_sequence_label_size
       UpperCAmelCase_							:   Tuple   =      encoder_stride


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Dict   =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

       UpperCAmelCase_							:   int   =      None
       if self.use_labels:
            UpperCAmelCase_							:   Optional[int]   =      ids_tensor([self.batch_size]      ,		self.type_sequence_label_size						)

       UpperCAmelCase_							:   Optional[int]   =      self.get_config()

       return config, pixel_values, labels


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       return SwinConfig(
           image_size=self.image_size      ,		patch_size=self.patch_size      ,		num_channels=self.num_channels      ,		embed_dim=self.embed_dim      ,		depths=self.depths      ,		num_heads=self.num_heads      ,		window_size=self.window_size      ,		mlp_ratio=self.mlp_ratio      ,		qkv_bias=self.qkv_bias      ,		hidden_dropout_prob=self.hidden_dropout_prob      ,		attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,		drop_path_rate=self.drop_path_rate      ,		hidden_act=self.hidden_act      ,		use_absolute_embeddings=self.use_absolute_embeddings      ,		path_norm=self.patch_norm      ,		layer_norm_eps=self.layer_norm_eps      ,		initializer_range=self.initializer_range      ,		encoder_stride=self.encoder_stride      ,		)


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:   List[Any]   =      TFSwinModel(config=snake_case_						)
       UpperCAmelCase_							:   List[str]   =      model(snake_case_						)

       UpperCAmelCase_							:   Any   =      ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths						) - 1))
       UpperCAmelCase_							:   List[str]   =      int(config.embed_dim * 2 ** (len(config.depths						) - 1)						)

       self.parent.assertEqual(result.last_hidden_state.shape      ,		(self.batch_size, expected_seq_len, expected_dim)						)


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:   Optional[int]   =      TFSwinForMaskedImageModeling(config=snake_case_						)
       UpperCAmelCase_							:   Tuple   =      model(snake_case_						)
       self.parent.assertEqual(
           result.logits.shape      ,		(self.batch_size, self.num_channels, self.image_size, self.image_size)						)

       # test greyscale images
       UpperCAmelCase_							:   Union[str, Any]   =      1
       UpperCAmelCase_							:   Any   =      TFSwinForMaskedImageModeling(snake_case_						)

       UpperCAmelCase_							:   Tuple   =      floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
       UpperCAmelCase_							:   Dict   =      model(snake_case_						)
       self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, 1, self.image_size, self.image_size)						)


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:   Tuple   =      self.type_sequence_label_size
       UpperCAmelCase_							:   str   =      TFSwinForImageClassification(snake_case_						)
       UpperCAmelCase_							:   Dict   =      model(snake_case_      ,		labels=snake_case_						)
       self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.type_sequence_label_size)						)

       # test greyscale images
       UpperCAmelCase_							:   str   =      1
       UpperCAmelCase_							:   int   =      TFSwinForImageClassification(snake_case_						)
       UpperCAmelCase_							:   Optional[int]   =      floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
       UpperCAmelCase_							:   str   =      model(snake_case_						)
       self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.type_sequence_label_size)						)






  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   List[str]   =      self.prepare_config_and_inputs()
       UpperCAmelCase_   ,			UpperCAmelCase_   ,			UpperCAmelCase_							:   Optional[int]   =      config_and_inputs
       UpperCAmelCase_							:   Any   =      {'pixel_values': pixel_values}
       return config, inputs_dict







@require_tf
class      __SCREAMING_SNAKE_CASE						(       lowerCamelCase_						,	lowerCamelCase_						,	unittest.TestCase       ):







  '''simple docstring'''






  lowerCamelCase_		:Any						    =		(
      (
          TFSwinModel,
          TFSwinForImageClassification,
          TFSwinForMaskedImageModeling,
      )
      if is_tf_available()
      else ()
  )
  lowerCamelCase_		:Any						    =		(
      {'''feature-extraction''': TFSwinModel, '''image-classification''': TFSwinForImageClassification}
      if is_tf_available()
      else {}
  )

  lowerCamelCase_		:Tuple						    =		False
  lowerCamelCase_		:Tuple						    =		False
  lowerCamelCase_		:Union[str, Any]						    =		False
  lowerCamelCase_		:str						    =		False


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Optional[Any]   =      TFSwinModelTester(self						)
       UpperCAmelCase_							:   Union[str, Any]   =      ConfigTester(self      ,		config_class=snake_case_      ,		embed_dim=3_7						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       self.create_and_test_config_common_properties()
       self.config_tester.create_and_test_config_to_json_string()
       self.config_tester.create_and_test_config_to_json_file()
       self.config_tester.create_and_test_config_from_and_save_pretrained()
       self.config_tester.create_and_test_config_with_num_labels()
       self.config_tester.check_config_can_be_init_without_params()
       self.config_tester.check_config_arguments_init()


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       return


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   List[str]   =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*snake_case_						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Any   =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*snake_case_						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   List[Any]   =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*snake_case_						)


  @unittest.skip(reason='Swin does not use inputs_embeds'						)
  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       pass


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_   ,			UpperCAmelCase_							:   Any   =      self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
            UpperCAmelCase_							:   List[Any]   =      model_class(snake_case_						)
            self.assertIsInstance(model.get_input_embeddings()      ,		tf.keras.layers.Layer						)
            UpperCAmelCase_							:   Tuple   =      model.get_output_embeddings()
            self.assertTrue(x is None or isinstance(snake_case_      ,		tf.keras.layers.Dense						)						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_   ,			UpperCAmelCase_							:   str   =      self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
            UpperCAmelCase_							:   Union[str, Any]   =      model_class(snake_case_						)
            UpperCAmelCase_							:   str   =      inspect.signature(model.call						)
            # signature.parameters is an OrderedDict => so arg_names order is deterministic
            UpperCAmelCase_							:   Optional[Any]   =      [*signature.parameters.keys()]

            UpperCAmelCase_							:   List[Any]   =      ['pixel_values']
            self.assertListEqual(arg_names[:1]      ,		snake_case_						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_   ,			UpperCAmelCase_							:   int   =      self.model_tester.prepare_config_and_inputs_for_common()
       UpperCAmelCase_							:   List[str]   =      True

       for model_class in self.all_model_classes:
            UpperCAmelCase_							:   Union[str, Any]   =      True
            UpperCAmelCase_							:   Dict   =      False
            UpperCAmelCase_							:   int   =      True
            UpperCAmelCase_							:   str   =      model_class(snake_case_						)
            UpperCAmelCase_							:   str   =      model(**self._prepare_for_class(snake_case_      ,		snake_case_						)						)
            UpperCAmelCase_							:   int   =      outputs.attentions
            UpperCAmelCase_							:   List[str]   =      len(self.model_tester.depths						)
            self.assertEqual(len(snake_case_						)      ,		snake_case_						)

            # check that output_attentions also work using config
            del inputs_dict["output_attentions"]
            UpperCAmelCase_							:   Optional[Any]   =      True
            UpperCAmelCase_							:   List[Any]   =      config.window_size**2
            UpperCAmelCase_							:   List[Any]   =      model_class(snake_case_						)
            UpperCAmelCase_							:   Optional[Any]   =      model(**self._prepare_for_class(snake_case_      ,		snake_case_						)						)
            UpperCAmelCase_							:   Tuple   =      outputs.attentions
            self.assertEqual(len(snake_case_						)      ,		snake_case_						)

            self.assertListEqual(
                list(attentions[0].shape[-3:]						)      ,		[self.model_tester.num_heads[0], window_size_squared, window_size_squared]      ,		)
            UpperCAmelCase_							:   List[Any]   =      len(snake_case_						)

            # Check attention is always last and order is fine
            UpperCAmelCase_							:   int   =      True
            UpperCAmelCase_							:   Union[str, Any]   =      True
            UpperCAmelCase_							:   int   =      model_class(snake_case_						)
            UpperCAmelCase_							:   List[Any]   =      model(**self._prepare_for_class(snake_case_      ,		snake_case_						)						)

            if hasattr(self.model_tester      ,		'num_hidden_states_types'						):
                 UpperCAmelCase_							:   Union[str, Any]   =      self.model_tester.num_hidden_states_types
            else:
                 # also another +1 for reshaped_hidden_states
                 UpperCAmelCase_							:   Optional[Any]   =      2
            self.assertEqual(out_len + added_hidden_states      ,		len(snake_case_						)						)

            UpperCAmelCase_							:   Optional[int]   =      outputs.attentions

            self.assertEqual(len(snake_case_						)      ,		snake_case_						)

            self.assertListEqual(
                list(self_attentions[0].shape[-3:]						)      ,		[self.model_tester.num_heads[0], window_size_squared, window_size_squared]      ,		)


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:   Optional[int]   =      model_class(snake_case_						)
       UpperCAmelCase_							:   List[str]   =      model(**self._prepare_for_class(snake_case_      ,		snake_case_						)						)
       UpperCAmelCase_							:   Dict   =      outputs.hidden_states

       UpperCAmelCase_							:   str   =      getattr(
           self.model_tester      ,		'expected_num_hidden_layers'      ,		len(self.model_tester.depths						) + 1						)
       self.assertEqual(len(snake_case_						)      ,		snake_case_						)

       # Swin has a different seq_length
       UpperCAmelCase_							:   List[Any]   =      to_atuple(config.patch_size						)

       UpperCAmelCase_							:   str   =      (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

       self.assertListEqual(
           list(hidden_states[0].shape[-2:]						)      ,		[num_patches, self.model_tester.embed_dim]      ,		)

       UpperCAmelCase_							:   str   =      outputs.reshaped_hidden_states
       self.assertEqual(len(snake_case_						)      ,		snake_case_						)

       UpperCAmelCase_   ,			UpperCAmelCase_   ,			UpperCAmelCase_   ,			UpperCAmelCase_							:   Optional[Any]   =      reshaped_hidden_states[0].shape

       UpperCAmelCase_							:   Tuple   =      tf.reshape(reshaped_hidden_states[0]      ,		(batch_size, num_channels, height * width)						)
       UpperCAmelCase_							:   Tuple   =      tf.transpose(snake_case_      ,		(0, 2, 1)						)

       self.assertListEqual(
           list(reshaped_hidden_states.shape[-2:]						)      ,		[num_patches, self.model_tester.embed_dim]      ,		)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_   ,			UpperCAmelCase_							:   Dict   =      self.model_tester.prepare_config_and_inputs_for_common()

       UpperCAmelCase_							:   Optional[Any]   =      to_atuple(self.model_tester.image_size						)

       for model_class in self.all_model_classes:
            UpperCAmelCase_							:   Optional[int]   =      True
            self.check_hidden_states_output(snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_						)

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            UpperCAmelCase_							:   Any   =      True

            self.check_hidden_states_output(snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_   ,			UpperCAmelCase_							:   int   =      self.model_tester.prepare_config_and_inputs_for_common()
       UpperCAmelCase_							:   Tuple   =      3

       UpperCAmelCase_							:   Optional[int]   =      to_atuple(self.model_tester.image_size						)
       UpperCAmelCase_							:   List[str]   =      to_atuple(config.patch_size						)

       UpperCAmelCase_							:   Any   =      image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
       UpperCAmelCase_							:   Dict   =      image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

       for model_class in self.all_model_classes:
            UpperCAmelCase_							:   Optional[Any]   =      True
            self.check_hidden_states_output(snake_case_      ,		snake_case_      ,		snake_case_      ,		(padded_height, padded_width)						)

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            UpperCAmelCase_							:   str   =      True
            self.check_hidden_states_output(snake_case_      ,		snake_case_      ,		snake_case_      ,		(padded_height, padded_width)						)






  @slow
  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCAmelCase_							:   Tuple   =      TFSwinModel.from_pretrained(snake_case_						)
            self.assertIsNotNone(snake_case_						)







@require_vision
@require_tf
class      __SCREAMING_SNAKE_CASE						(       unittest.TestCase       ):







  '''simple docstring'''






  @cached_property
  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       return (
           AutoImageProcessor.from_pretrained('microsoft/swin-tiny-patch4-window7-224'						)
           if is_vision_available()
           else None
       )






  @slow
  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   List[Any]   =      TFSwinForImageClassification.from_pretrained('microsoft/swin-tiny-patch4-window7-224'						)
       UpperCAmelCase_							:   List[str]   =      self.default_image_processor

       UpperCAmelCase_							:   Tuple   =      Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
       UpperCAmelCase_							:   Optional[Any]   =      image_processor(images=snake_case_      ,		return_tensors='tf'						)

       # forward pass
       UpperCAmelCase_							:   Optional[int]   =      model(snake_case_						)

       # verify the logits
       UpperCAmelCase_							:   Tuple   =      tf.TensorShape((1, 1_0_0_0)						)
       self.assertEqual(outputs.logits.shape      ,		snake_case_						)
       UpperCAmelCase_							:   Union[str, Any]   =      tf.constant([-0.09_48, -0.64_54, -0.09_21]						)
       self.assertTrue(np.allclose(outputs.logits[0, :3]      ,		snake_case_      ,		atol=1E-4						)						)







'''simple docstring'''


from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers import (
     TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
     TFXLMForMultipleChoice,
     TFXLMForQuestionAnsweringSimple,
     TFXLMForSequenceClassification,
     TFXLMForTokenClassification,
     TFXLMModel,
     TFXLMWithLMHeadModel,
     XLMConfig,
 )






class      __SCREAMING_SNAKE_CASE						:







  '''simple docstring'''






  def __init__( self      ,		snake_case_      ,		):




       '''simple docstring'''

       UpperCAmelCase_							:   str   =      parent
       UpperCAmelCase_							:   Tuple   =      1_3
       UpperCAmelCase_							:   Dict   =      7
       UpperCAmelCase_							:   Union[str, Any]   =      True
       UpperCAmelCase_							:   Optional[Any]   =      True
       UpperCAmelCase_							:   Tuple   =      True
       UpperCAmelCase_							:   Dict   =      True
       UpperCAmelCase_							:   List[Any]   =      True
       UpperCAmelCase_							:   Union[str, Any]   =      False
       UpperCAmelCase_							:   Any   =      False
       UpperCAmelCase_							:   List[str]   =      False
       UpperCAmelCase_							:   int   =      2
       UpperCAmelCase_							:   Union[str, Any]   =      9_9
       UpperCAmelCase_							:   Dict   =      0
       UpperCAmelCase_							:   List[str]   =      3_2
       UpperCAmelCase_							:   str   =      2
       UpperCAmelCase_							:   int   =      4
       UpperCAmelCase_							:   Dict   =      0.1
       UpperCAmelCase_							:   int   =      0.1
       UpperCAmelCase_							:   List[str]   =      5_1_2
       UpperCAmelCase_							:   Union[str, Any]   =      1_6
       UpperCAmelCase_							:   List[Any]   =      2
       UpperCAmelCase_							:   int   =      0.02
       UpperCAmelCase_							:   Union[str, Any]   =      3
       UpperCAmelCase_							:   Union[str, Any]   =      4
       UpperCAmelCase_							:   Optional[int]   =      'last'
       UpperCAmelCase_							:   List[Any]   =      True
       UpperCAmelCase_							:   Union[str, Any]   =      None
       UpperCAmelCase_							:   int   =      0


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Tuple   =      ids_tensor([self.batch_size, self.seq_length]      ,		self.vocab_size						)
       UpperCAmelCase_							:   Tuple   =      random_attention_mask([self.batch_size, self.seq_length]      ,		dtype=tf.floataa						)

       UpperCAmelCase_							:   Optional[Any]   =      None
       if self.use_input_lengths:
            UpperCAmelCase_							:   Any   =      (
                ids_tensor([self.batch_size]      ,		vocab_size=2						) + self.seq_length - 2
            )  # small variation of seq_length

       UpperCAmelCase_							:   Optional[Any]   =      None
       if self.use_token_type_ids:
            UpperCAmelCase_							:   Tuple   =      ids_tensor([self.batch_size, self.seq_length]      ,		self.n_langs						)

       UpperCAmelCase_							:   Any   =      None
       UpperCAmelCase_							:   Union[str, Any]   =      None
       UpperCAmelCase_							:   Optional[int]   =      None
       if self.use_labels:
            UpperCAmelCase_							:   Union[str, Any]   =      ids_tensor([self.batch_size]      ,		self.type_sequence_label_size						)
            UpperCAmelCase_							:   Any   =      ids_tensor([self.batch_size, self.seq_length]      ,		self.num_labels						)
            UpperCAmelCase_							:   Union[str, Any]   =      ids_tensor([self.batch_size]      ,		2      ,		dtype=tf.floataa						)
            UpperCAmelCase_							:   Any   =      ids_tensor([self.batch_size]      ,		self.num_choices						)

       UpperCAmelCase_							:   int   =      XLMConfig(
           vocab_size=self.vocab_size      ,		n_special=self.n_special      ,		emb_dim=self.hidden_size      ,		n_layers=self.num_hidden_layers      ,		n_heads=self.num_attention_heads      ,		dropout=self.hidden_dropout_prob      ,		attention_dropout=self.attention_probs_dropout_prob      ,		gelu_activation=self.gelu_activation      ,		sinusoidal_embeddings=self.sinusoidal_embeddings      ,		asm=self.asm      ,		causal=self.causal      ,		n_langs=self.n_langs      ,		max_position_embeddings=self.max_position_embeddings      ,		initializer_range=self.initializer_range      ,		summary_type=self.summary_type      ,		use_proj=self.use_proj      ,		bos_token_id=self.bos_token_id      ,		)

       return (
           config,
           input_ids,
           token_type_ids,
           input_lengths,
           sequence_labels,
           token_labels,
           is_impossible_labels,
           choice_labels,
           input_mask,
       )


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		):




       '''simple docstring'''

       UpperCAmelCase_							:   Tuple   =      TFXLMModel(config=snake_case_						)
       UpperCAmelCase_							:   int   =      {'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
       UpperCAmelCase_							:   Tuple   =      model(snake_case_						)

       UpperCAmelCase_							:   Any   =      [input_ids, input_mask]
       UpperCAmelCase_							:   Optional[Any]   =      model(snake_case_						)
       self.parent.assertEqual(result.last_hidden_state.shape      ,		(self.batch_size, self.seq_length, self.hidden_size)						)


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		):




       '''simple docstring'''

       UpperCAmelCase_							:   List[str]   =      TFXLMWithLMHeadModel(snake_case_						)

       UpperCAmelCase_							:   Union[str, Any]   =      {'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
       UpperCAmelCase_							:   Optional[Any]   =      model(snake_case_						)

       UpperCAmelCase_							:   Tuple   =      outputs

       self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.seq_length, self.vocab_size)						)


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		):




       '''simple docstring'''

       UpperCAmelCase_							:   List[str]   =      TFXLMForQuestionAnsweringSimple(snake_case_						)

       UpperCAmelCase_							:   Optional[int]   =      {'input_ids': input_ids, 'lengths': input_lengths}

       UpperCAmelCase_							:   Optional[int]   =      model(snake_case_						)

       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)						)


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		):




       '''simple docstring'''

       UpperCAmelCase_							:   List[str]   =      TFXLMForSequenceClassification(snake_case_						)

       UpperCAmelCase_							:   Dict   =      {'input_ids': input_ids, 'lengths': input_lengths}

       UpperCAmelCase_							:   Tuple   =      model(snake_case_						)

       self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.type_sequence_label_size)						)


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		):




       '''simple docstring'''

       UpperCAmelCase_							:   int   =      self.num_labels
       UpperCAmelCase_							:   List[str]   =      TFXLMForTokenClassification(config=snake_case_						)
       UpperCAmelCase_							:   str   =      {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
       UpperCAmelCase_							:   Optional[int]   =      model(snake_case_						)
       self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.seq_length, self.num_labels)						)


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		):




       '''simple docstring'''

       UpperCAmelCase_							:   List[Any]   =      self.num_choices
       UpperCAmelCase_							:   Optional[Any]   =      TFXLMForMultipleChoice(config=snake_case_						)
       UpperCAmelCase_							:   Tuple   =      tf.tile(tf.expand_dims(snake_case_      ,		1						)      ,		(1, self.num_choices, 1)						)
       UpperCAmelCase_							:   Dict   =      tf.tile(tf.expand_dims(snake_case_      ,		1						)      ,		(1, self.num_choices, 1)						)
       UpperCAmelCase_							:   str   =      tf.tile(tf.expand_dims(snake_case_      ,		1						)      ,		(1, self.num_choices, 1)						)
       UpperCAmelCase_							:   Tuple   =      {
           'input_ids': multiple_choice_inputs_ids,
           'attention_mask': multiple_choice_input_mask,
           'token_type_ids': multiple_choice_token_type_ids,
       }
       UpperCAmelCase_							:   int   =      model(snake_case_						)
       self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.num_choices)						)






  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Optional[int]   =      self.prepare_config_and_inputs()
       (
           (
           UpperCAmelCase_
       )   ,			(
           UpperCAmelCase_
       )   ,			(
           UpperCAmelCase_
       )   ,			(
           UpperCAmelCase_
       )   ,			(
           UpperCAmelCase_
       )   ,			(
           UpperCAmelCase_
       )   ,			(
           UpperCAmelCase_
       )   ,			(
           UpperCAmelCase_
       )   ,			(
           UpperCAmelCase_
       )   ,			
       )							:   Optional[int]   =      config_and_inputs
       UpperCAmelCase_							:   Any   =      {
           'input_ids': input_ids,
           'token_type_ids': token_type_ids,
           'langs': token_type_ids,
           'lengths': input_lengths,
       }
       return config, inputs_dict







@require_tf
class      __SCREAMING_SNAKE_CASE						(       lowerCamelCase_						,	lowerCamelCase_						,	unittest.TestCase       ):







  '''simple docstring'''






  lowerCamelCase_		:List[Any]						    =		(
      (
          TFXLMModel,
          TFXLMWithLMHeadModel,
          TFXLMForSequenceClassification,
          TFXLMForQuestionAnsweringSimple,
          TFXLMForTokenClassification,
          TFXLMForMultipleChoice,
      )
      if is_tf_available()
      else ()
  )
  lowerCamelCase_		:str						    =		(
      (TFXLMWithLMHeadModel,) if is_tf_available() else ()
  )  # TODO (PVP): Check other models whether language generation is also applicable
  lowerCamelCase_		:int						    =		(
      {
          '''feature-extraction''': TFXLMModel,
          '''fill-mask''': TFXLMWithLMHeadModel,
          '''question-answering''': TFXLMForQuestionAnsweringSimple,
          '''text-classification''': TFXLMForSequenceClassification,
          '''text-generation''': TFXLMWithLMHeadModel,
          '''token-classification''': TFXLMForTokenClassification,
          '''zero-shot''': TFXLMForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  lowerCamelCase_		:List[Any]						    =		False
  lowerCamelCase_		:Dict						    =		False


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_						):




       '''simple docstring'''

       if (
           pipeline_test_casse_name == "QAPipelineTests"
           and tokenizer_name is not None
           and not tokenizer_name.endswith('Fast'						)
       ):
            # `QAPipelineTests` fails for a few models when the slower tokenizer are used.
            # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
            # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
            return True

       return False


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   str   =      TFXLMModelTester(self						)
       UpperCAmelCase_							:   int   =      ConfigTester(self      ,		config_class=snake_case_      ,		emb_dim=3_7						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       self.config_tester.run_common_tests()


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Any   =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_model(*snake_case_						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Optional[int]   =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_lm_head(*snake_case_						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   List[Any]   =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_qa(*snake_case_						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Tuple   =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_sequence_classif(*snake_case_						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Union[str, Any]   =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_for_token_classification(*snake_case_						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Union[str, Any]   =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_for_multiple_choice(*snake_case_						)






  @slow
  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCAmelCase_							:   int   =      TFXLMModel.from_pretrained(snake_case_						)
            self.assertIsNotNone(snake_case_						)







@require_tf
class      __SCREAMING_SNAKE_CASE						(       unittest.TestCase       ):







  '''simple docstring'''






  @slow
  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Any   =      TFXLMWithLMHeadModel.from_pretrained('xlm-mlm-en-2048'						)
       UpperCAmelCase_							:   List[str]   =      tf.convert_to_tensor([[1_4, 4_4_7]]      ,		dtype=tf.intaa						)  # the president
       UpperCAmelCase_							:   Optional[int]   =      [
           1_4,
           4_4_7,
           1_4,
           4_4_7,
           1_4,
           4_4_7,
           1_4,
           4_4_7,
           1_4,
           4_4_7,
           1_4,
           4_4_7,
           1_4,
           4_4_7,
           1_4,
           4_4_7,
           1_4,
           4_4_7,
           1_4,
           4_4_7,
       ]  # the president the president the president the president the president the president the president the president the president the president
       # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
       UpperCAmelCase_							:   Optional[int]   =      model.generate(snake_case_      ,		do_sample=snake_case_						)
       self.assertListEqual(output_ids[0].numpy().tolist()      ,		snake_case_						)






