






'''simple docstring'''







import unittest

from transformers import XLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import (
			    XLMForMultipleChoice,
			    XLMForQuestionAnswering,
			    XLMForQuestionAnsweringSimple,
			    XLMForSequenceClassification,
			    XLMForTokenClassification,
			    XLMModel,
			    XLMWithLMHeadModel,
			)
			from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST





class 	SCREAMING_SNAKE_CASE					:






			def __init__( self      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			int=13  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=7  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=True  ,						__SCREAMING_SNAKE_CASE      :			Any=True  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]=True  ,						__SCREAMING_SNAKE_CASE      :			Tuple=True  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=True  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=False  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=False  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=False  ,						__SCREAMING_SNAKE_CASE      :			List[str]=2  ,						__SCREAMING_SNAKE_CASE      :			Any=99  ,						__SCREAMING_SNAKE_CASE      :			Tuple=0  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=32  ,						__SCREAMING_SNAKE_CASE      :			Tuple=5  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]=4  ,						__SCREAMING_SNAKE_CASE      :			Tuple=0.1  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=0.1  ,						__SCREAMING_SNAKE_CASE      :			Any=512  ,						__SCREAMING_SNAKE_CASE      :			Any=2  ,						__SCREAMING_SNAKE_CASE      :			str=0.02  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=2  ,						__SCREAMING_SNAKE_CASE      :			List[str]=4  ,						__SCREAMING_SNAKE_CASE      :			Tuple="last"  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=True  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=None  ,						__SCREAMING_SNAKE_CASE      :			Any=0  ,						)			->			List[str]:
							a_		:  int											=      parent
							a_		:  Dict											=      batch_size
							a_		:  Dict											=      seq_length
							a_		:  Dict											=      is_training
							a_		:  Tuple											=      use_input_lengths
							a_		:  str											=      use_token_type_ids
							a_		:  List[str]											=      use_labels
							a_		:  Any											=      gelu_activation
							a_		:  int											=      sinusoidal_embeddings
							a_		:  Union[str, Any]											=      causal
							a_		:  str											=      asm
							a_		:  Dict											=      n_langs
							a_		:  Union[str, Any]											=      vocab_size
							a_		:  Dict											=      n_special
							a_		:  List[str]											=      hidden_size
							a_		:  Union[str, Any]											=      num_hidden_layers
							a_		:  Dict											=      num_attention_heads
							a_		:  List[str]											=      hidden_dropout_prob
							a_		:  int											=      attention_probs_dropout_prob
							a_		:  int											=      max_position_embeddings
							a_		:  Dict											=      type_sequence_label_size
							a_		:  Any											=      initializer_range
							a_		:  List[str]											=      num_labels
							a_		:  Union[str, Any]											=      num_choices
							a_		:  Tuple											=      summary_type
							a_		:  List[Any]											=      use_proj
							a_		:  Dict											=      scope
							a_		:  Union[str, Any]											=      bos_token_id






			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			Tuple:
							a_		:  Union[str, Any]											=      ids_tensor([self.batch_size, self.seq_length]  ,						self.vocab_size					)
							a_		:  Optional[Any]											=      random_attention_mask([self.batch_size, self.seq_length]					)

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

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

							a_		:  List[Any]											=      None
							a_		:  List[str]											=      None
							a_		:  Optional[int]											=      None
							if self.use_labels:
											a_		:  List[Any]											=      ids_tensor([self.batch_size]  ,						self.type_sequence_label_size					)
											a_		:  Optional[Any]											=      ids_tensor([self.batch_size, self.seq_length]  ,						self.num_labels					)
											a_		:  str											=      ids_tensor([self.batch_size]  ,						2					).float()
											a_		:  int											=      ids_tensor([self.batch_size]  ,						self.num_choices					)

							a_		:  int											=      self.get_config()

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






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			int:
							return 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  ,						num_labels=self.num_labels  ,						bos_token_id=self.bos_token_id  ,						)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			str  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			str  ,						)			->			str:
							a_		:  Tuple											=      XLMModel(config=lowerCamelCase_					)
							model.to(lowerCamelCase_					)
							model.eval()
							a_		:  Any											=      model(lowerCamelCase_  ,						lengths=lowerCamelCase_  ,						langs=lowerCamelCase_					)
							a_		:  Optional[int]											=      model(lowerCamelCase_  ,						langs=lowerCamelCase_					)
							a_		:  Tuple											=      model(lowerCamelCase_					)
							self.parent.assertEqual(result.last_hidden_state.shape  ,						(self.batch_size, self.seq_length, self.hidden_size)					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			str  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			List[Any]  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			List[str]  ,						)			->			Tuple:
							a_		:  Any											=      XLMWithLMHeadModel(lowerCamelCase_					)
							model.to(lowerCamelCase_					)
							model.eval()

							a_		:  Union[str, Any]											=      model(lowerCamelCase_  ,						token_type_ids=lowerCamelCase_  ,						labels=lowerCamelCase_					)
							self.parent.assertEqual(result.loss.shape  ,						()					)
							self.parent.assertEqual(result.logits.shape  ,						(self.batch_size, self.seq_length, self.vocab_size)					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			int  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			str  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			List[Any]  ,						)			->			Optional[Any]:
							a_		:  List[str]											=      XLMForQuestionAnsweringSimple(lowerCamelCase_					)
							model.to(lowerCamelCase_					)
							model.eval()

							a_		:  int											=      model(lowerCamelCase_					)

							a_		:  Optional[int]											=      model(lowerCamelCase_  ,						start_positions=lowerCamelCase_  ,						end_positions=lowerCamelCase_					)
							a_		:  Union[str, Any]											=      outputs
							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 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			str  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						)			->			int:
							a_		:  Dict											=      XLMForQuestionAnswering(lowerCamelCase_					)
							model.to(lowerCamelCase_					)
							model.eval()

							a_		:  Union[str, Any]											=      model(lowerCamelCase_					)

							a_		:  str											=      model(
							    lowerCamelCase_  ,						start_positions=lowerCamelCase_  ,						end_positions=lowerCamelCase_  ,						cls_index=lowerCamelCase_  ,						is_impossible=lowerCamelCase_  ,						p_mask=lowerCamelCase_  ,						)

							a_		:  Tuple											=      model(
							    lowerCamelCase_  ,						start_positions=lowerCamelCase_  ,						end_positions=lowerCamelCase_  ,						cls_index=lowerCamelCase_  ,						is_impossible=lowerCamelCase_  ,						)

							(a_					)		:  List[Any]											=      result_with_labels.to_tuple()

							a_		:  Dict											=      model(lowerCamelCase_  ,						start_positions=lowerCamelCase_  ,						end_positions=lowerCamelCase_					)

							(a_					)		:  Optional[Any]											=      result_with_labels.to_tuple()

							self.parent.assertEqual(result_with_labels.loss.shape  ,						()					)
							self.parent.assertEqual(result.start_top_log_probs.shape  ,						(self.batch_size, model.config.start_n_top)					)
							self.parent.assertEqual(result.start_top_index.shape  ,						(self.batch_size, model.config.start_n_top)					)
							self.parent.assertEqual(
							    result.end_top_log_probs.shape  ,						(self.batch_size, model.config.start_n_top * model.config.end_n_top)					)
							self.parent.assertEqual(
							    result.end_top_index.shape  ,						(self.batch_size, model.config.start_n_top * model.config.end_n_top)					)
							self.parent.assertEqual(result.cls_logits.shape  ,						(self.batch_size,)					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			int  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]  ,						)			->			List[str]:
							a_		:  Optional[Any]											=      XLMForSequenceClassification(lowerCamelCase_					)
							model.to(lowerCamelCase_					)
							model.eval()

							a_		:  Tuple											=      model(lowerCamelCase_					)
							a_		:  Any											=      model(lowerCamelCase_  ,						labels=lowerCamelCase_					)
							self.parent.assertEqual(result.loss.shape  ,						()					)
							self.parent.assertEqual(result.logits.shape  ,						(self.batch_size, self.type_sequence_label_size)					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			int  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			Tuple  ,						)			->			Any:
							a_		:  Any											=      self.num_labels
							a_		:  Optional[int]											=      XLMForTokenClassification(lowerCamelCase_					)
							model.to(lowerCamelCase_					)
							model.eval()

							a_		:  List[str]											=      model(lowerCamelCase_  ,						attention_mask=lowerCamelCase_  ,						labels=lowerCamelCase_					)
							self.parent.assertEqual(result.logits.shape  ,						(self.batch_size, self.seq_length, self.num_labels)					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			str  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						)			->			Optional[Any]:
							a_		:  Dict											=      self.num_choices
							a_		:  List[Any]											=      XLMForMultipleChoice(config=lowerCamelCase_					)
							model.to(lowerCamelCase_					)
							model.eval()
							a_		:  Dict											=      input_ids.unsqueeze(1					).expand(-1  ,						self.num_choices  ,						-1					).contiguous()
							a_		:  List[Any]											=      token_type_ids.unsqueeze(1					).expand(-1  ,						self.num_choices  ,						-1					).contiguous()
							a_		:  Tuple											=      input_mask.unsqueeze(1					).expand(-1  ,						self.num_choices  ,						-1					).contiguous()
							a_		:  int											=      model(
							    lowerCamelCase_  ,						attention_mask=lowerCamelCase_  ,						token_type_ids=lowerCamelCase_  ,						labels=lowerCamelCase_  ,						)
							self.parent.assertEqual(result.logits.shape  ,						(self.batch_size, self.num_choices)					)







			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			int:
							a_		:  int											=      self.prepare_config_and_inputs()
							(
							    a_					
							)		:  List[str]											=      config_and_inputs
							a_		:  List[Any]											=      {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''lengths''': input_lengths}
							return config, inputs_dict





@require_torch
class 	SCREAMING_SNAKE_CASE					(					_UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase	,   unittest.TestCase       ):
			snake_case__  =				(
			    (
			        XLMModel,
			        XLMWithLMHeadModel,
			        XLMForQuestionAnswering,
			        XLMForSequenceClassification,
			        XLMForQuestionAnsweringSimple,
			        XLMForTokenClassification,
			        XLMForMultipleChoice,
			    )
			    if is_torch_available()
			    else ()
			)
			snake_case__  =				(
			    (XLMWithLMHeadModel,) if is_torch_available() else ()
			)  # TODO (PVP): Check other models whether language generation is also applicable
			snake_case__  =				(
			    {
			        "feature-extraction": XLMModel,
			        "fill-mask": XLMWithLMHeadModel,
			        "question-answering": XLMForQuestionAnsweringSimple,
			        "text-classification": XLMForSequenceClassification,
			        "text-generation": XLMWithLMHeadModel,
			        "token-classification": XLMForTokenClassification,
			        "zero-shot": XLMForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			List[Any]  ,						__SCREAMING_SNAKE_CASE      :			str  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]					)			->			Tuple:




							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 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			Any=False					)			->			Any:
							a_		:  Optional[Any]											=      super()._prepare_for_class(lowerCamelCase_  ,						lowerCamelCase_  ,						return_labels=lowerCamelCase_					)

							if return_labels:
											if model_class.__name__ == "XLMForQuestionAnswering":
															a_		:  int											=      torch.zeros(
															    self.model_tester.batch_size  ,						dtype=torch.long  ,						device=lowerCamelCase_					)
															a_		:  List[str]											=      torch.zeros(
															    self.model_tester.batch_size  ,						dtype=torch.long  ,						device=lowerCamelCase_					)

							return inputs_dict






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			int:
							a_		:  List[str]											=      XLMModelTester(self					)
							a_		:  Dict											=      ConfigTester(self  ,						config_class=lowerCamelCase_  ,						emb_dim=37					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]					)			->			Dict:
							self.config_tester.run_common_tests()






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			int:
							a_		:  Union[str, Any]											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_model(*lowerCamelCase_					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]					)			->			List[Any]:
							a_		:  Optional[int]											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_lm_head(*lowerCamelCase_					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			Union[str, Any]:
							a_		:  Union[str, Any]											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_simple_qa(*lowerCamelCase_					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			Optional[Any]:
							a_		:  Tuple											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_qa(*lowerCamelCase_					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			Optional[Any]:
							a_		:  int											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_sequence_classif(*lowerCamelCase_					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			Tuple:
							a_		:  Tuple											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_token_classif(*lowerCamelCase_					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			Dict:
							a_		:  List[str]											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_for_multiple_choice(*lowerCamelCase_					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			str=False  ,						__SCREAMING_SNAKE_CASE      :			str=1					)			->			Union[str, Any]:
							self.assertIsInstance(lowerCamelCase_  ,						lowerCamelCase_					)
							self.assertListEqual(
							    [isinstance(lowerCamelCase_  ,						lowerCamelCase_					) for iter_attentions in attentions]  ,						[True] * len(lowerCamelCase_					)					)
							self.assertEqual(len(lowerCamelCase_					)  ,						(max_length - min_length) * num_beam_groups					)

							for idx, iter_attentions in enumerate(lowerCamelCase_					):
											# adds PAD dummy token
											a_		:  Any											=      min_length + idx + 1
											a_		:  Any											=      min_length + idx + 1

											a_		:  int											=      (
											    batch_size * num_beam_groups,
											    config.num_attention_heads,
											    tgt_len,
											    src_len,
											)
											# check attn size
											self.assertListEqual(
											    [layer_attention.shape for layer_attention in iter_attentions]  ,						[expected_shape] * len(lowerCamelCase_					)					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=False  ,						__SCREAMING_SNAKE_CASE      :			Dict=1					)			->			Tuple:
							self.assertIsInstance(lowerCamelCase_  ,						lowerCamelCase_					)
							self.assertListEqual(
							    [isinstance(lowerCamelCase_  ,						lowerCamelCase_					) for iter_hidden_states in hidden_states]  ,						[True] * len(lowerCamelCase_					)  ,						)
							self.assertEqual(len(lowerCamelCase_					)  ,						(max_length - min_length) * num_beam_groups					)

							for idx, iter_hidden_states in enumerate(lowerCamelCase_					):
											# adds PAD dummy token
											a_		:  List[str]											=      min_length + idx + 1
											a_		:  int											=      (batch_size * num_beam_groups, seq_len, config.hidden_size)
											# check hidden size
											self.assertListEqual(
											    [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states]  ,						[expected_shape] * len(lowerCamelCase_					)  ,						)
							pass







			@slow
			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]					)			->			Union[str, Any]:




							for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											a_		:  Union[str, Any]											=      XLMModel.from_pretrained(lowerCamelCase_					)
											self.assertIsNotNone(lowerCamelCase_					)





@require_torch
class 	SCREAMING_SNAKE_CASE					(					unittest.TestCase       ):






			@slow
			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			Dict:
							a_		:  Tuple											=      XLMWithLMHeadModel.from_pretrained('''xlm-mlm-en-2048'''					)
							model.to(lowerCamelCase_					)
							a_		:  int											=      torch.tensor([[14, 447]]  ,						dtype=torch.long  ,						device=lowerCamelCase_					)  # the president
							a_		:  int											=      [
							    14,
							    447,
							    14,
							    447,
							    14,
							    447,
							    14,
							    447,
							    14,
							    447,
							    14,
							    447,
							    14,
							    447,
							    14,
							    447,
							    14,
							    447,
							    14,
							    447,
							]  # 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
							a_		:  Optional[int]											=      model.generate(lowerCamelCase_  ,						do_sample=lowerCamelCase_					)
							self.assertListEqual(output_ids[0].cpu().numpy().tolist()  ,						lowerCamelCase_					)












'''simple docstring'''







import unittest

import numpy as np
import torch

from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device

from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()





class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_	,   unittest.TestCase       ):
			snake_case__  =				DDIMPipeline
			snake_case__  =				UNCONDITIONAL_IMAGE_GENERATION_PARAMS
			snake_case__  =				PipelineTesterMixin.required_optional_params - {
			    "num_images_per_prompt",
			    "latents",
			    "callback",
			    "callback_steps",
			}
			snake_case__  =				UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS
			snake_case__  =				False






			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			Optional[Any]:
							torch.manual_seed(0					)
							a_		:  int											=      UNetaDModel(
							    block_out_channels=(32, 64)  ,						layers_per_block=2  ,						sample_size=32  ,						in_channels=3  ,						out_channels=3  ,						down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')  ,						up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')  ,						)
							a_		:  str											=      DDIMScheduler()
							a_		:  Union[str, Any]											=      {'''unet''': unet, '''scheduler''': scheduler}
							return components






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			str  ,						__SCREAMING_SNAKE_CASE      :			Tuple=0					)			->			str:
							if str(__SCREAMING_SNAKE_CASE					).startswith('''mps'''					):
											a_		:  Dict											=      torch.manual_seed(__SCREAMING_SNAKE_CASE					)
							else:
											a_		:  Union[str, Any]											=      torch.Generator(device=__SCREAMING_SNAKE_CASE					).manual_seed(__SCREAMING_SNAKE_CASE					)
							a_		:  Union[str, Any]											=      {
							    '''batch_size''': 1,
							    '''generator''': generator,
							    '''num_inference_steps''': 2,
							    '''output_type''': '''numpy''',
							}
							return inputs






			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			Union[str, Any]:
							a_		:  Dict											=      '''cpu'''

							a_		:  List[Any]											=      self.get_dummy_components()
							a_		:  List[str]											=      self.pipeline_class(**__SCREAMING_SNAKE_CASE					)
							pipe.to(__SCREAMING_SNAKE_CASE					)
							pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

							a_		:  Tuple											=      self.get_dummy_inputs(__SCREAMING_SNAKE_CASE					)
							a_		:  Optional[Any]											=      pipe(**__SCREAMING_SNAKE_CASE					).images
							a_		:  List[str]											=      image[0, -3:, -3:, -1]

							self.assertEqual(image.shape  ,						(1, 32, 32, 3)					)
							a_		:  int											=      np.array(
							    [1.000e00, 5.717e-01, 4.717e-01, 1.000e00, 0.000e00, 1.000e00, 3.000e-04, 0.000e00, 9.000e-04]					)
							a_		:  Union[str, Any]											=      np.abs(image_slice.flatten() - expected_slice					).max()
							self.assertLessEqual(__SCREAMING_SNAKE_CASE  ,						1e-3					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			str:
							super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			Optional[int]:
							super().test_save_load_local(expected_max_difference=3e-3					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			List[str]:
							super().test_save_load_optional_components(expected_max_difference=3e-3					)







			def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			Optional[int]:
							super().test_inference_batch_single_identical(expected_max_diff=3e-3					)





@slow
@require_torch_gpu
class 	SCREAMING_SNAKE_CASE					(					unittest.TestCase       ):






			def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			Any:
							a_		:  Optional[Any]											=      '''google/ddpm-cifar10-32'''

							a_		:  Optional[Any]											=      UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE					)
							a_		:  Dict											=      DDIMScheduler()

							a_		:  List[str]											=      DDIMPipeline(unet=__SCREAMING_SNAKE_CASE  ,						scheduler=__SCREAMING_SNAKE_CASE					)
							ddim.to(__SCREAMING_SNAKE_CASE					)
							ddim.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

							a_		:  Tuple											=      torch.manual_seed(0					)
							a_		:  Tuple											=      ddim(generator=__SCREAMING_SNAKE_CASE  ,						eta=0.0  ,						output_type='''numpy'''					).images

							a_		:  List[Any]											=      image[0, -3:, -3:, -1]

							assert image.shape == (1, 32, 32, 3)
							a_		:  List[str]											=      np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453]					)

							assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-2







			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			str:
							a_		:  int											=      '''google/ddpm-ema-bedroom-256'''

							a_		:  str											=      UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE					)
							a_		:  Tuple											=      DDIMScheduler.from_pretrained(__SCREAMING_SNAKE_CASE					)

							a_		:  Any											=      DDIMPipeline(unet=__SCREAMING_SNAKE_CASE  ,						scheduler=__SCREAMING_SNAKE_CASE					)
							ddpm.to(__SCREAMING_SNAKE_CASE					)
							ddpm.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

							a_		:  Tuple											=      torch.manual_seed(0					)
							a_		:  List[Any]											=      ddpm(generator=__SCREAMING_SNAKE_CASE  ,						output_type='''numpy'''					).images

							a_		:  Optional[int]											=      image[0, -3:, -3:, -1]

							assert image.shape == (1, 256, 256, 3)
							a_		:  Optional[Any]											=      np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069]					)

							assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-2




