





'''simple docstring'''







import collections
import inspect
import unittest

from transformers import DonutSwinConfig
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import is_torch_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch
					from torch import nn

					from transformers import DonutSwinModel
					from transformers.models.donut.modeling_donut_swin import DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST


class        __a   :

							def __init__(				self				:  Optional[Any]							,  __magic_name__				:  Any							,  __magic_name__				:  List[Any]=13							,  __magic_name__				:  List[str]=32							,  __magic_name__				:  Any=2							,  __magic_name__				:  List[str]=3							,  __magic_name__				:  List[str]=16							,  __magic_name__				:  Union[str, Any]=[1, 2, 1]							,  __magic_name__				:  Dict=[2, 2, 4]							,  __magic_name__				:  List[str]=2							,  __magic_name__				:  Union[str, Any]=2.0							,  __magic_name__				:  Tuple=True							,  __magic_name__				:  List[str]=0.0							,  __magic_name__				:  Union[str, Any]=0.0							,  __magic_name__				:  int=0.1							,  __magic_name__				:  str="gelu"							,  __magic_name__				:  Optional[Any]=False							,  __magic_name__				:  Optional[int]=True							,  __magic_name__				:  Union[str, Any]=0.0_2							,  __magic_name__				:  int=1E-5							,  __magic_name__				:  int=True							,  __magic_name__				:  Dict=None							,  __magic_name__				:  List[Any]=True							,  __magic_name__				:  Optional[Any]=10							,  __magic_name__				:  Optional[int]=8							,  )       ->							Optional[Any]:







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

							def      UpperCAmelCase__						(				self				:  Optional[Any] )       ->							List[Any]:







													"""simple docstring"""
													UpperCAmelCase_      :   int      =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

													UpperCAmelCase_      :   Union[str, Any]      =	None
													if self.use_labels:
																			UpperCAmelCase_      :   List[str]      =	ids_tensor([self.batch_size]							,  self.type_sequence_label_size )

													UpperCAmelCase_      :   Dict      =	self.get_config()

													return config, pixel_values, labels

							def      UpperCAmelCase__						(				self				:  int )       ->							Tuple:







													"""simple docstring"""
													return DonutSwinConfig(
													    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				:  Optional[Any]							,  __magic_name__				:  Optional[int]							,  __magic_name__				:  Optional[int]							,  __magic_name__				:  List[str] )       ->							Tuple:







													"""simple docstring"""
													UpperCAmelCase_      :   Optional[int]      =	DonutSwinModel(config=_lowerCamelCase )
													model.to(_lowerCamelCase )
													model.eval()
													UpperCAmelCase_      :   Optional[Any]      =	model(_lowerCamelCase )

													UpperCAmelCase_      :   Optional[Any]      =	((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1))
													UpperCAmelCase_      :   Tuple      =	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				:  Union[str, Any] )       ->							Optional[Any]:







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





@require_torch
class        __a   (_lowerCamelCase							,		_lowerCamelCase							,		unittest.TestCase  ):
							__a       : Tuple          =							(DonutSwinModel,) if is_torch_available() else ()
							__a       : Union[str, Any]          =							{"feature-extraction": DonutSwinModel} if is_torch_available() else {}
							__a       : str          =							True

							__a       : List[str]          =							False
							__a       : Optional[int]          =							False
							__a       : int          =							False

							def      UpperCAmelCase__						(				self				:  Dict )       ->							Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_      :   Optional[Any]      =	DonutSwinModelTester(self )
													UpperCAmelCase_      :   str      =	ConfigTester(self							,  config_class=_lowerCamelCase							,  embed_dim=37 )

							def      UpperCAmelCase__						(				self				:  Any )       ->							List[Any]:







													"""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				:  Any )       ->							int:







													"""simple docstring"""
													return

							def      UpperCAmelCase__						(				self				:  int )       ->							Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_      :   str      =	self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*_lowerCamelCase )

							def      UpperCAmelCase__						(				self				:  Union[str, Any] )       ->							Dict:







													"""simple docstring"""
													# DonutSwin does not use inputs_embeds
													pass

							def      UpperCAmelCase__						(				self				:  str )       ->							Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_      :   List[Any]      =	self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																			UpperCAmelCase_      :   str      =	model_class(_lowerCamelCase )
																			self.assertIsInstance(model.get_input_embeddings()							,  (nn.Module) )
																			UpperCAmelCase_      :   Dict      =	model.get_output_embeddings()
																			self.assertTrue(x is None or isinstance(_lowerCamelCase							,  nn.Linear ) )

							def      UpperCAmelCase__						(				self				:  Dict )       ->							Any:







													"""simple docstring"""
													UpperCAmelCase_      :   List[str]      =	self.model_tester.prepare_config_and_inputs_for_common()

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

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

							def      UpperCAmelCase__						(				self				:  List[Any] )       ->							List[str]:







													"""simple docstring"""
													UpperCAmelCase_      :   Tuple      =	self.model_tester.prepare_config_and_inputs_for_common()
													UpperCAmelCase_      :   Tuple      =	True

													for model_class in self.all_model_classes:
																			UpperCAmelCase_      :   str      =	True
																			UpperCAmelCase_      :   str      =	False
																			UpperCAmelCase_      :   List[Any]      =	True
																			UpperCAmelCase_      :   List[Any]      =	model_class(_lowerCamelCase )
																			model.to(_lowerCamelCase )
																			model.eval()
																			with torch.no_grad():
																									UpperCAmelCase_      :   Optional[int]      =	model(**self._prepare_for_class(_lowerCamelCase							,  _lowerCamelCase ) )
																			UpperCAmelCase_      :   Any      =	outputs.attentions
																			UpperCAmelCase_      :   List[str]      =	len(self.model_tester.depths )
																			self.assertEqual(len(_lowerCamelCase )							,  _lowerCamelCase )

																			# check that output_attentions also work using config
																			del inputs_dict["output_attentions"]
																			UpperCAmelCase_      :   Optional[int]      =	True
																			UpperCAmelCase_      :   Optional[Any]      =	config.window_size**2
																			UpperCAmelCase_      :   Optional[Any]      =	model_class(_lowerCamelCase )
																			model.to(_lowerCamelCase )
																			model.eval()
																			with torch.no_grad():
																									UpperCAmelCase_      :   Tuple      =	model(**self._prepare_for_class(_lowerCamelCase							,  _lowerCamelCase ) )
																			UpperCAmelCase_      :   Any      =	outputs.attentions
																			self.assertEqual(len(_lowerCamelCase )							,  _lowerCamelCase )

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

																			# Check attention is always last and order is fine
																			UpperCAmelCase_      :   int      =	True
																			UpperCAmelCase_      :   Tuple      =	True
																			UpperCAmelCase_      :   Optional[int]      =	model_class(_lowerCamelCase )
																			model.to(_lowerCamelCase )
																			model.eval()
																			with torch.no_grad():
																									UpperCAmelCase_      :   List[Any]      =	model(**self._prepare_for_class(_lowerCamelCase							,  _lowerCamelCase ) )

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

																			UpperCAmelCase_      :   List[str]      =	outputs.attentions

																			self.assertEqual(len(_lowerCamelCase )							,  _lowerCamelCase )

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

							def      UpperCAmelCase__						(				self				:  Tuple							,  __magic_name__				:  List[Any]							,  __magic_name__				:  Union[str, Any]							,  __magic_name__				:  str							,  __magic_name__				:  Tuple )       ->							List[Any]:







													"""simple docstring"""
													UpperCAmelCase_      :   Union[str, Any]      =	model_class(_lowerCamelCase )
													model.to(_lowerCamelCase )
													model.eval()

													with torch.no_grad():
																			UpperCAmelCase_      :   Any      =	model(**self._prepare_for_class(_lowerCamelCase							,  _lowerCamelCase ) )

													UpperCAmelCase_      :   Optional[Any]      =	outputs.hidden_states

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

													# DonutSwin has a different seq_length
													UpperCAmelCase_      :   Tuple      =	(
													    config.patch_size
													    if isinstance(config.patch_size							,  collections.abc.Iterable )
													    else (config.patch_size, 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_      :   List[str]      =	outputs.reshaped_hidden_states
													self.assertEqual(len(_lowerCamelCase )							,  _lowerCamelCase )

													UpperCAmelCase_      :   Tuple      =	reshaped_hidden_states[0].shape
													UpperCAmelCase_      :   Any      =	(
													    reshaped_hidden_states[0].view(_lowerCamelCase							,  _lowerCamelCase							,  height * width ).permute(0							,  2							,  1 )
													)
													self.assertListEqual(
													    list(reshaped_hidden_states.shape[-2:] )							,  [num_patches, self.model_tester.embed_dim]							,  )

							def      UpperCAmelCase__						(				self				:  Union[str, Any] )       ->							Dict:







													"""simple docstring"""
													UpperCAmelCase_      :   int      =	self.model_tester.prepare_config_and_inputs_for_common()

													UpperCAmelCase_      :   List[Any]      =	(
													    self.model_tester.image_size
													    if isinstance(self.model_tester.image_size							,  collections.abc.Iterable )
													    else (self.model_tester.image_size, self.model_tester.image_size)
													)

													for model_class in self.all_model_classes:
																			UpperCAmelCase_      :   str      =	True
																			self.check_hidden_states_output(_lowerCamelCase							,  _lowerCamelCase							,  _lowerCamelCase							,  _lowerCamelCase )

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

																			self.check_hidden_states_output(_lowerCamelCase							,  _lowerCamelCase							,  _lowerCamelCase							,  _lowerCamelCase )

							def      UpperCAmelCase__						(				self				:  Tuple )       ->							Dict:







													"""simple docstring"""
													UpperCAmelCase_      :   List[str]      =	self.model_tester.prepare_config_and_inputs_for_common()
													UpperCAmelCase_      :   Any      =	3

													UpperCAmelCase_      :   Any      =	(
													    self.model_tester.image_size
													    if isinstance(self.model_tester.image_size							,  collections.abc.Iterable )
													    else (self.model_tester.image_size, self.model_tester.image_size)
													)
													UpperCAmelCase_      :   Tuple      =	(
													    config.patch_size
													    if isinstance(config.patch_size							,  collections.abc.Iterable )
													    else (config.patch_size, config.patch_size)
													)

													UpperCAmelCase_      :   Optional[Any]      =	image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
													UpperCAmelCase_      :   Tuple      =	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(_lowerCamelCase							,  _lowerCamelCase							,  _lowerCamelCase							,  (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(_lowerCamelCase							,  _lowerCamelCase							,  _lowerCamelCase							,  (padded_height, padded_width) )

							@slow
							def      UpperCAmelCase__						(				self				:  Tuple )       ->							Dict:







													"""simple docstring"""
													for model_name in DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			UpperCAmelCase_      :   Dict      =	DonutSwinModel.from_pretrained(_lowerCamelCase )
																			self.assertIsNotNone(_lowerCamelCase )



							def      UpperCAmelCase__						(				self				:  Dict )       ->							Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_      :   List[str]      =	self.model_tester.prepare_config_and_inputs_for_common()

													UpperCAmelCase_      :   Optional[int]      =	_config_zero_init(_lowerCamelCase )
													for model_class in self.all_model_classes:
																			UpperCAmelCase_      :   Tuple      =	model_class(config=_lowerCamelCase )
																			for name, param in model.named_parameters():
																									if "embeddings" not in name and param.requires_grad:
																															self.assertIn(
																															    ((param.data.mean() * 1E9).round() / 1E9).item()							,  [0.0, 1.0]							,  msg=F"""Parameter {name} of model {model_class} seems not properly initialized"""							,  )






'''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       __lowercase		:







     """simple docstring"""






     def __init__(		self  ,      _lowerCamelCase  ,      ):
         __UpperCamelCase						:						str		     =	parent
         __UpperCamelCase						:						Tuple		     =	1_3
         __UpperCamelCase						:						str		     =	7
         __UpperCamelCase						:						List[str]		     =	True
         __UpperCamelCase						:						Optional[Any]		     =	True
         __UpperCamelCase						:						Tuple		     =	True
         __UpperCamelCase						:						Tuple		     =	True
         __UpperCamelCase						:						int		     =	True
         __UpperCamelCase						:						int		     =	False
         __UpperCamelCase						:						Optional[Any]		     =	False
         __UpperCamelCase						:						Any		     =	False
         __UpperCamelCase						:						Any		     =	2
         __UpperCamelCase						:						str		     =	9_9
         __UpperCamelCase						:						int		     =	0
         __UpperCamelCase						:						Any		     =	3_2
         __UpperCamelCase						:						Tuple		     =	2
         __UpperCamelCase						:						Optional[int]		     =	4
         __UpperCamelCase						:						List[str]		     =	0.1
         __UpperCamelCase						:						List[Any]		     =	0.1
         __UpperCamelCase						:						List[str]		     =	5_1_2
         __UpperCamelCase						:						int		     =	1_6
         __UpperCamelCase						:						Any		     =	2
         __UpperCamelCase						:						Dict		     =	0.0_2
         __UpperCamelCase						:						Any		     =	3
         __UpperCamelCase						:						str		     =	4
         __UpperCamelCase						:						Any		     =	'last'
         __UpperCamelCase						:						int		     =	True
         __UpperCamelCase						:						List[str]		     =	None
         __UpperCamelCase						:						Union[str, Any]		     =	0







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						List[str]		     =	ids_tensor([self.batch_size, self.seq_length]  ,      self.vocab_size     )
         __UpperCamelCase						:						str		     =	random_attention_mask([self.batch_size, self.seq_length]  ,      dtype=tf.floataa     )

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

         __UpperCamelCase						:						int		     =	None
         if self.use_token_type_ids:
             __UpperCamelCase						:						Union[str, Any]		     =	ids_tensor([self.batch_size, self.seq_length]  ,      self.n_langs     )

         __UpperCamelCase						:						Optional[Any]		     =	None
         __UpperCamelCase						:						Optional[Any]		     =	None
         __UpperCamelCase						:						Tuple		     =	None
         if self.use_labels:
             __UpperCamelCase						:						Tuple		     =	ids_tensor([self.batch_size]  ,      self.type_sequence_label_size     )
             __UpperCamelCase						:						Dict		     =	ids_tensor([self.batch_size, self.seq_length]  ,      self.num_labels     )
             __UpperCamelCase						:						Optional[int]		     =	ids_tensor([self.batch_size]  ,      2  ,      dtype=tf.floataa     )
             __UpperCamelCase						:						int		     =	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 lowerCAmelCase  (		self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      ):
         __UpperCamelCase						:						str		     =	TFXLMModel(config=_lowerCamelCase     )
         __UpperCamelCase						:						Optional[Any]		     =	{'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
         __UpperCamelCase						:						str		     =	model(_lowerCamelCase     )

         __UpperCamelCase						:						Dict		     =	[input_ids, input_mask]
         __UpperCamelCase						:						Tuple		     =	model(_lowerCamelCase     )
         self.parent.assertEqual(result.last_hidden_state.shape  ,      (self.batch_size, self.seq_length, self.hidden_size)     )







     def lowerCAmelCase  (		self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      ):
         __UpperCamelCase						:						int		     =	TFXLMWithLMHeadModel(_lowerCamelCase     )

         __UpperCamelCase						:						List[str]		     =	{'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
         __UpperCamelCase						:						List[str]		     =	model(_lowerCamelCase     )

         __UpperCamelCase						:						Tuple		     =	outputs

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







     def lowerCAmelCase  (		self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      ):
         __UpperCamelCase						:						Tuple		     =	TFXLMForQuestionAnsweringSimple(_lowerCamelCase     )

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

         __UpperCamelCase						:						Optional[Any]		     =	model(_lowerCamelCase     )

         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 lowerCAmelCase  (		self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      ):
         __UpperCamelCase						:						Optional[Any]		     =	TFXLMForSequenceClassification(_lowerCamelCase     )

         __UpperCamelCase						:						Union[str, Any]		     =	{'input_ids': input_ids, 'lengths': input_lengths}

         __UpperCamelCase						:						Dict		     =	model(_lowerCamelCase     )

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







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







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







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						List[str]		     =	self.prepare_config_and_inputs()
         (
             (
             __UpperCamelCase
         )     ,       (
             __UpperCamelCase
         )     ,       (
             __UpperCamelCase
         )     ,       (
             __UpperCamelCase
         )     ,       (
             __UpperCamelCase
         )     ,       (
             __UpperCamelCase
         )     ,       (
             __UpperCamelCase
         )     ,       (
             __UpperCamelCase
         )     ,       (
             __UpperCamelCase
         )     ,       
         )						:						Any		     =	config_and_inputs
         __UpperCamelCase						:						Union[str, 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       __lowercase		(    _lowerCamelCase      ,				_lowerCamelCase      ,				unittest.TestCase    ):







     """simple docstring"""






     SCREAMING_SNAKE_CASE__		 =    (
         (
             TFXLMModel,
             TFXLMWithLMHeadModel,
             TFXLMForSequenceClassification,
             TFXLMForQuestionAnsweringSimple,
             TFXLMForTokenClassification,
             TFXLMForMultipleChoice,
         )
         if is_tf_available()
         else ()
     )
     SCREAMING_SNAKE_CASE__		 =    (
         (TFXLMWithLMHeadModel,) if is_tf_available() else ()
     )  # TODO (PVP): Check other models whether language generation is also applicable
     SCREAMING_SNAKE_CASE__		 =    (
         {
             '''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 {}
     )
     SCREAMING_SNAKE_CASE__		 =    False
     SCREAMING_SNAKE_CASE__		 =    False







     def lowerCAmelCase  (		self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase     ):
         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 lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Optional[Any]		     =	TFXLMModelTester(self     )
         __UpperCamelCase						:						Union[str, Any]		     =	ConfigTester(self  ,      config_class=_lowerCamelCase  ,      emb_dim=3_7     )







     def lowerCAmelCase  (		self     ):
         self.config_tester.run_common_tests()







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Optional[int]		     =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_model(*_lowerCamelCase     )







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Optional[int]		     =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_lm_head(*_lowerCamelCase     )







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Optional[Any]		     =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_qa(*_lowerCamelCase     )







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Optional[Any]		     =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_sequence_classif(*_lowerCamelCase     )







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Any		     =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_for_token_classification(*_lowerCamelCase     )







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Dict		     =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_xlm_for_multiple_choice(*_lowerCamelCase     )







     @slow
     def lowerCAmelCase  (		self     ):
         for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             __UpperCamelCase						:						List[Any]		     =	TFXLMModel.from_pretrained(_lowerCamelCase     )
             self.assertIsNotNone(_lowerCamelCase     )






@require_tf
class       __lowercase		(    unittest.TestCase    ):







     """simple docstring"""






     @slow
     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Union[str, Any]		     =	TFXLMWithLMHeadModel.from_pretrained('xlm-mlm-en-2048'     )
         __UpperCamelCase						:						Union[str, Any]		     =	tf.convert_to_tensor([[1_4, 4_4_7]]  ,      dtype=tf.intaa     )  # the president
         __UpperCamelCase						:						List[Any]		     =	[
             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						:						str		     =	model.generate(_lowerCamelCase  ,      do_sample=_lowerCamelCase     )
         self.assertListEqual(output_ids[0].numpy().tolist()  ,      _lowerCamelCase     )



