

"""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    snake_case     :

				def __init__(     self							:    Dict					,  UpperCamelCase__							:    Any					,  UpperCamelCase__							:    List[Any]=1_3					,  UpperCamelCase__							:    Optional[Any]=7					,  UpperCamelCase__							:    Dict=True					,  UpperCamelCase__							:    List[Any]=True					,  UpperCamelCase__							:    Any=True					,  UpperCamelCase__							:    Union[str, Any]=True					,  UpperCamelCase__							:    Dict=True					,  UpperCamelCase__							:    Tuple=False					,  UpperCamelCase__							:    List[Any]=False					,  UpperCamelCase__							:    str=False					,  UpperCamelCase__							:    List[str]=2					,  UpperCamelCase__							:    int=9_9					,  UpperCamelCase__							:    Optional[int]=0					,  UpperCamelCase__							:    Dict=3_2					,  UpperCamelCase__							:    Any=5					,  UpperCamelCase__							:    List[str]=4					,  UpperCamelCase__							:    List[Any]=0.1					,  UpperCamelCase__							:    Dict=0.1					,  UpperCamelCase__							:    Union[str, Any]=5_1_2					,  UpperCamelCase__							:    Tuple=2					,  UpperCamelCase__							:    int=0.02					,  UpperCamelCase__							:    int=2					,  UpperCamelCase__							:    int=4					,  UpperCamelCase__							:    List[Any]="last"					,  UpperCamelCase__							:    List[str]=True					,  UpperCamelCase__							:    str=None					,  UpperCamelCase__							:    int=0					,  )->       Optional[int]:



						'''simple docstring'''


						__lowerCAmelCase:	Optional[int]   				=      parent
						__lowerCAmelCase:	Tuple   				=      batch_size
						__lowerCAmelCase:	List[Any]   				=      seq_length
						__lowerCAmelCase:	Dict   				=      is_training
						__lowerCAmelCase:	Optional[Any]   				=      use_input_lengths
						__lowerCAmelCase:	Optional[Any]   				=      use_token_type_ids
						__lowerCAmelCase:	List[str]   				=      use_labels
						__lowerCAmelCase:	str   				=      gelu_activation
						__lowerCAmelCase:	int   				=      sinusoidal_embeddings
						__lowerCAmelCase:	List[Any]   				=      causal
						__lowerCAmelCase:	List[str]   				=      asm
						__lowerCAmelCase:	int   				=      n_langs
						__lowerCAmelCase:	List[Any]   				=      vocab_size
						__lowerCAmelCase:	Optional[Any]   				=      n_special
						__lowerCAmelCase:	Tuple   				=      hidden_size
						__lowerCAmelCase:	List[Any]   				=      num_hidden_layers
						__lowerCAmelCase:	Optional[Any]   				=      num_attention_heads
						__lowerCAmelCase:	Union[str, Any]   				=      hidden_dropout_prob
						__lowerCAmelCase:	Any   				=      attention_probs_dropout_prob
						__lowerCAmelCase:	List[Any]   				=      max_position_embeddings
						__lowerCAmelCase:	List[Any]   				=      type_sequence_label_size
						__lowerCAmelCase:	str   				=      initializer_range
						__lowerCAmelCase:	Optional[int]   				=      num_labels
						__lowerCAmelCase:	Optional[int]   				=      num_choices
						__lowerCAmelCase:	List[Any]   				=      summary_type
						__lowerCAmelCase:	int   				=      use_proj
						__lowerCAmelCase:	str   				=      scope
						__lowerCAmelCase:	Dict   				=      bos_token_id

				def 							lowercase_					(     self							:    List[str])->       str:



						'''simple docstring'''


						__lowerCAmelCase:	int   				=      ids_tensor([self.batch_size, self.seq_length]					,  self.vocab_size)
						__lowerCAmelCase:	List[str]   				=      random_attention_mask([self.batch_size, self.seq_length])

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

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

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

						__lowerCAmelCase:	str   				=      self.get_config()

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

				def 							lowercase_					(     self							:    int)->       Optional[int]:



						'''simple docstring'''


						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 							lowercase_					(     self							:    str					,  UpperCamelCase__							:    List[Any]					,  UpperCamelCase__							:    int					,  UpperCamelCase__							:    Optional[int]					,  UpperCamelCase__							:    Optional[Any]					,  UpperCamelCase__							:    List[Any]					,  UpperCamelCase__							:    str					,  UpperCamelCase__							:    Any					,  UpperCamelCase__							:    Tuple					,  UpperCamelCase__							:    Any					,  )->       Any:



						'''simple docstring'''


						__lowerCAmelCase:	Any   				=      XLMModel(config=UpperCamelCase__)
						model.to(UpperCamelCase__)
						model.eval()
						__lowerCAmelCase:	Optional[Any]   				=      model(UpperCamelCase__					,  lengths=UpperCamelCase__					,  langs=UpperCamelCase__)
						__lowerCAmelCase:	Union[str, Any]   				=      model(UpperCamelCase__					,  langs=UpperCamelCase__)
						__lowerCAmelCase:	int   				=      model(UpperCamelCase__)
						self.parent.assertEqual(result.last_hidden_state.shape					,  (self.batch_size, self.seq_length, self.hidden_size))

				def 							lowercase_					(     self							:    int					,  UpperCamelCase__							:    List[Any]					,  UpperCamelCase__							:    List[str]					,  UpperCamelCase__							:    int					,  UpperCamelCase__							:    Union[str, Any]					,  UpperCamelCase__							:    List[str]					,  UpperCamelCase__							:    int					,  UpperCamelCase__							:    List[str]					,  UpperCamelCase__							:    Optional[Any]					,  UpperCamelCase__							:    Optional[Any]					,  )->       str:



						'''simple docstring'''


						__lowerCAmelCase:	str   				=      XLMWithLMHeadModel(UpperCamelCase__)
						model.to(UpperCamelCase__)
						model.eval()

						__lowerCAmelCase:	Optional[int]   				=      model(UpperCamelCase__					,  token_type_ids=UpperCamelCase__					,  labels=UpperCamelCase__)
						self.parent.assertEqual(result.loss.shape					,  ())
						self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.seq_length, self.vocab_size))

				def 							lowercase_					(     self							:    Any					,  UpperCamelCase__							:    Optional[int]					,  UpperCamelCase__							:    int					,  UpperCamelCase__							:    Union[str, Any]					,  UpperCamelCase__							:    Optional[Any]					,  UpperCamelCase__							:    Any					,  UpperCamelCase__							:    Optional[int]					,  UpperCamelCase__							:    Dict					,  UpperCamelCase__							:    Dict					,  UpperCamelCase__							:    Optional[int]					,  )->       List[Any]:



						'''simple docstring'''


						__lowerCAmelCase:	Optional[int]   				=      XLMForQuestionAnsweringSimple(UpperCamelCase__)
						model.to(UpperCamelCase__)
						model.eval()

						__lowerCAmelCase:	Any   				=      model(UpperCamelCase__)

						__lowerCAmelCase:	Optional[int]   				=      model(UpperCamelCase__					,  start_positions=UpperCamelCase__					,  end_positions=UpperCamelCase__)
						__lowerCAmelCase:	Optional[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 							lowercase_					(     self							:    Any					,  UpperCamelCase__							:    int					,  UpperCamelCase__							:    Dict					,  UpperCamelCase__							:    str					,  UpperCamelCase__							:    List[Any]					,  UpperCamelCase__							:    Optional[Any]					,  UpperCamelCase__							:    Dict					,  UpperCamelCase__							:    int					,  UpperCamelCase__							:    List[Any]					,  UpperCamelCase__							:    Union[str, Any]					,  )->       Optional[Any]:



						'''simple docstring'''


						__lowerCAmelCase:	Dict   				=      XLMForQuestionAnswering(UpperCamelCase__)
						model.to(UpperCamelCase__)
						model.eval()

						__lowerCAmelCase:	Any   				=      model(UpperCamelCase__)

						__lowerCAmelCase:	List[str]   				=      model(
						    UpperCamelCase__					,  start_positions=UpperCamelCase__					,  end_positions=UpperCamelCase__					,  cls_index=UpperCamelCase__					,  is_impossible=UpperCamelCase__					,  p_mask=UpperCamelCase__					,  )

						__lowerCAmelCase:	List[Any]   				=      model(
						    UpperCamelCase__					,  start_positions=UpperCamelCase__					,  end_positions=UpperCamelCase__					,  cls_index=UpperCamelCase__					,  is_impossible=UpperCamelCase__					,  )

						((__lowerCAmelCase)				,			):	Union[str, Any]   				=      result_with_labels.to_tuple()

						__lowerCAmelCase:	Union[str, Any]   				=      model(UpperCamelCase__					,  start_positions=UpperCamelCase__					,  end_positions=UpperCamelCase__)

						((__lowerCAmelCase)				,			):	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 							lowercase_					(     self							:    List[str]					,  UpperCamelCase__							:    int					,  UpperCamelCase__							:    Optional[Any]					,  UpperCamelCase__							:    Optional[Any]					,  UpperCamelCase__							:    List[Any]					,  UpperCamelCase__							:    Union[str, Any]					,  UpperCamelCase__							:    Tuple					,  UpperCamelCase__							:    Optional[int]					,  UpperCamelCase__							:    List[str]					,  UpperCamelCase__							:    List[Any]					,  )->       Optional[Any]:



						'''simple docstring'''


						__lowerCAmelCase:	Union[str, Any]   				=      XLMForSequenceClassification(UpperCamelCase__)
						model.to(UpperCamelCase__)
						model.eval()

						__lowerCAmelCase:	List[Any]   				=      model(UpperCamelCase__)
						__lowerCAmelCase:	Tuple   				=      model(UpperCamelCase__					,  labels=UpperCamelCase__)
						self.parent.assertEqual(result.loss.shape					,  ())
						self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.type_sequence_label_size))

				def 							lowercase_					(     self							:    Any					,  UpperCamelCase__							:    str					,  UpperCamelCase__							:    Dict					,  UpperCamelCase__							:    Dict					,  UpperCamelCase__							:    Dict					,  UpperCamelCase__							:    Any					,  UpperCamelCase__							:    Tuple					,  UpperCamelCase__							:    Any					,  UpperCamelCase__							:    Optional[int]					,  UpperCamelCase__							:    Tuple					,  )->       List[str]:



						'''simple docstring'''


						__lowerCAmelCase:	str   				=      self.num_labels
						__lowerCAmelCase:	Tuple   				=      XLMForTokenClassification(UpperCamelCase__)
						model.to(UpperCamelCase__)
						model.eval()

						__lowerCAmelCase:	Tuple   				=      model(UpperCamelCase__					,  attention_mask=UpperCamelCase__					,  labels=UpperCamelCase__)
						self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.seq_length, self.num_labels))

				def 							lowercase_					(     self							:    Union[str, Any]					,  UpperCamelCase__							:    str					,  UpperCamelCase__							:    int					,  UpperCamelCase__							:    Optional[int]					,  UpperCamelCase__							:    List[str]					,  UpperCamelCase__							:    Dict					,  UpperCamelCase__							:    Tuple					,  UpperCamelCase__							:    Optional[int]					,  UpperCamelCase__							:    Union[str, Any]					,  UpperCamelCase__							:    Optional[Any]					,  )->       List[str]:



						'''simple docstring'''


						__lowerCAmelCase:	str   				=      self.num_choices
						__lowerCAmelCase:	Dict   				=      XLMForMultipleChoice(config=UpperCamelCase__)
						model.to(UpperCamelCase__)
						model.eval()
						__lowerCAmelCase:	Optional[int]   				=      input_ids.unsqueeze(1).expand(-1					,  self.num_choices					,  -1).contiguous()
						__lowerCAmelCase:	Union[str, Any]   				=      token_type_ids.unsqueeze(1).expand(-1					,  self.num_choices					,  -1).contiguous()
						__lowerCAmelCase:	Any   				=      input_mask.unsqueeze(1).expand(-1					,  self.num_choices					,  -1).contiguous()
						__lowerCAmelCase:	Dict   				=      model(
						    UpperCamelCase__					,  attention_mask=UpperCamelCase__					,  token_type_ids=UpperCamelCase__					,  labels=UpperCamelCase__					,  )
						self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.num_choices))




				def 							lowercase_					(     self							:    List[str])->       Tuple:



						'''simple docstring'''


						__lowerCAmelCase:	Optional[Any]   				=      self.prepare_config_and_inputs()
						(
						    (
						    __lowerCAmelCase
						)				,			(
						    __lowerCAmelCase
						)				,			(
						    __lowerCAmelCase
						)				,			(
						    __lowerCAmelCase
						)				,			(
						    __lowerCAmelCase
						)				,			(
						    __lowerCAmelCase
						)				,			(
						    __lowerCAmelCase
						)				,			(
						    __lowerCAmelCase
						)				,			(
						    __lowerCAmelCase
						)				,			
						):	Dict   				=      config_and_inputs
						__lowerCAmelCase:	str   				=      {"input_ids": input_ids, "token_type_ids": token_type_ids, "lengths": input_lengths}
						return config, inputs_dict





@require_torch
class    snake_case     (    __snake_case,							__snake_case,							__snake_case,							unittest.TestCase  ):
				SCREAMING_SNAKE_CASE_       :				List[str]       				=  (
				    (
				        XLMModel,
				        XLMWithLMHeadModel,
				        XLMForQuestionAnswering,
				        XLMForSequenceClassification,
				        XLMForQuestionAnsweringSimple,
				        XLMForTokenClassification,
				        XLMForMultipleChoice,
				    )
				    if is_torch_available()
				    else ()
				)
				SCREAMING_SNAKE_CASE_       :				List[str]       				=  (
				    (XLMWithLMHeadModel,) if is_torch_available() else ()
				)  # TODO (PVP): Check other models whether language generation is also applicable
				SCREAMING_SNAKE_CASE_       :				Dict       				=  (
				    {
				        """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 							lowercase_					(     self							:    Optional[Any]					,  UpperCamelCase__							:    List[Any]					,  UpperCamelCase__							:    Optional[Any]					,  UpperCamelCase__							:    Tuple					,  UpperCamelCase__							:    Union[str, Any]					,  UpperCamelCase__							:    Optional[int])->       int:



						'''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 							lowercase_					(     self							:    int					,  UpperCamelCase__							:    List[str]					,  UpperCamelCase__							:    Dict					,  UpperCamelCase__							:    Optional[Any]=False)->       List[Any]:



						'''simple docstring'''


						__lowerCAmelCase:	Dict   				=      super()._prepare_for_class(UpperCamelCase__					,  UpperCamelCase__					,  return_labels=UpperCamelCase__)

						if return_labels:
								if model_class.__name__ == "XLMForQuestionAnswering":
										__lowerCAmelCase:	Optional[int]   				=      torch.zeros(
										    self.model_tester.batch_size					,  dtype=torch.long					,  device=UpperCamelCase__)
										__lowerCAmelCase:	Dict   				=      torch.zeros(
										    self.model_tester.batch_size					,  dtype=torch.long					,  device=UpperCamelCase__)

						return inputs_dict

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



						'''simple docstring'''


						__lowerCAmelCase:	List[str]   				=      XLMModelTester(self)
						__lowerCAmelCase:	Any   				=      ConfigTester(self					,  config_class=UpperCamelCase__					,  emb_dim=3_7)

				def 							lowercase_					(     self							:    Union[str, Any])->       List[str]:



						'''simple docstring'''


						self.config_tester.run_common_tests()

				def 							lowercase_					(     self							:    Union[str, Any])->       Optional[Any]:



						'''simple docstring'''


						__lowerCAmelCase:	Optional[Any]   				=      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_xlm_model(*UpperCamelCase__)

				def 							lowercase_					(     self							:    Union[str, Any])->       Optional[int]:



						'''simple docstring'''


						__lowerCAmelCase:	Any   				=      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_xlm_lm_head(*UpperCamelCase__)

				def 							lowercase_					(     self							:    Dict)->       List[str]:



						'''simple docstring'''


						__lowerCAmelCase:	List[Any]   				=      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_xlm_simple_qa(*UpperCamelCase__)

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



						'''simple docstring'''


						__lowerCAmelCase:	Any   				=      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_xlm_qa(*UpperCamelCase__)

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



						'''simple docstring'''


						__lowerCAmelCase:	List[str]   				=      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_xlm_sequence_classif(*UpperCamelCase__)

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



						'''simple docstring'''


						__lowerCAmelCase:	Any   				=      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_xlm_token_classif(*UpperCamelCase__)

				def 							lowercase_					(     self							:    Optional[Any])->       Union[str, Any]:



						'''simple docstring'''


						__lowerCAmelCase:	Tuple   				=      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_xlm_for_multiple_choice(*UpperCamelCase__)

				def 							lowercase_					(     self							:    Tuple					,  UpperCamelCase__							:    Optional[int]					,  UpperCamelCase__							:    str					,  UpperCamelCase__							:    str					,  UpperCamelCase__							:    Dict					,  UpperCamelCase__							:    Optional[Any]					,  UpperCamelCase__							:    Optional[int]=False					,  UpperCamelCase__							:    List[str]=1)->       str:



						'''simple docstring'''


						self.assertIsInstance(UpperCamelCase__					,  UpperCamelCase__)
						self.assertListEqual(
						    [isinstance(UpperCamelCase__					,  UpperCamelCase__) for iter_attentions in attentions]					,  [True] * len(UpperCamelCase__))
						self.assertEqual(len(UpperCamelCase__)					,  (max_length - min_length) * num_beam_groups)

						for idx, iter_attentions in enumerate(UpperCamelCase__):
								# adds PAD dummy token
								__lowerCAmelCase:	Union[str, Any]   				=      min_length + idx + 1
								__lowerCAmelCase:	Optional[Any]   				=      min_length + idx + 1

								__lowerCAmelCase:	Optional[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(UpperCamelCase__))

				def 							lowercase_					(     self							:    List[str]					,  UpperCamelCase__							:    Any					,  UpperCamelCase__							:    str					,  UpperCamelCase__							:    Optional[Any]					,  UpperCamelCase__							:    List[str]					,  UpperCamelCase__							:    Union[str, Any]					,  UpperCamelCase__							:    Tuple=False					,  UpperCamelCase__							:    Optional[int]=1)->       int:



						'''simple docstring'''


						self.assertIsInstance(UpperCamelCase__					,  UpperCamelCase__)
						self.assertListEqual(
						    [isinstance(UpperCamelCase__					,  UpperCamelCase__) for iter_hidden_states in hidden_states]					,  [True] * len(UpperCamelCase__)					,  )
						self.assertEqual(len(UpperCamelCase__)					,  (max_length - min_length) * num_beam_groups)

						for idx, iter_hidden_states in enumerate(UpperCamelCase__):
								# adds PAD dummy token
								__lowerCAmelCase:	Tuple   				=      min_length + idx + 1
								__lowerCAmelCase:	Any   				=      (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(UpperCamelCase__)					,  )
						pass




				@slow
				def 							lowercase_					(     self							:    Union[str, Any])->       str:



						'''simple docstring'''


						for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								__lowerCAmelCase:	str   				=      XLMModel.from_pretrained(UpperCamelCase__)
								self.assertIsNotNone(UpperCamelCase__)





@require_torch
class    snake_case     (    unittest.TestCase  ):

				@slow
				def 							lowercase_					(     self							:    List[Any])->       Dict:



						'''simple docstring'''


						__lowerCAmelCase:	List[str]   				=      XLMWithLMHeadModel.from_pretrained("xlm-mlm-en-2048")
						model.to(UpperCamelCase__)
						__lowerCAmelCase:	Optional[int]   				=      torch.tensor([[1_4, 4_4_7]]					,  dtype=torch.long					,  device=UpperCamelCase__)  # the president
						__lowerCAmelCase:	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
						__lowerCAmelCase:	List[str]   				=      model.generate(UpperCamelCase__					,  do_sample=UpperCamelCase__)
						self.assertListEqual(output_ids[0].cpu().numpy().tolist()					,  UpperCamelCase__)





"""simple docstring"""


import os

from bleurt import score  # From: git+https://github.com/google-research/bleurt.git

import datasets


__A						       =      datasets.logging.get_logger(__name__)


__A						       =      "\\n@inproceedings{bleurt,\n  title={BLEURT: Learning Robust Metrics for Text Generation},\n  author={Thibault Sellam and Dipanjan Das and Ankur P. Parikh},\n  booktitle={ACL},\n  year={2020},\n  url={https://arxiv.org/abs/2004.04696}\n}\n"

__A						       =      "\\nBLEURT a learnt evaluation metric for Natural Language Generation. It is built using multiple phases of transfer learning starting from a pretrained BERT model (Devlin et al. 2018)\nand then employing another pre-training phrase using synthetic data. Finally it is trained on WMT human annotations. You may run BLEURT out-of-the-box or fine-tune\nit for your specific application (the latter is expected to perform better).\n\nSee the project's README at https://github.com/google-research/bleurt#readme for more information.\n"

__A						       =      "\nBLEURT score.\n\nArgs:\n    `predictions` (list of str): prediction/candidate sentences\n    `references` (list of str): reference sentences\n    `checkpoint` BLEURT checkpoint. Will default to BLEURT-tiny if None.\n\nReturns:\n    'scores': List of scores.\nExamples:\n\n    >>> predictions = [\"hello there\", \"general kenobi\"]\n    >>> references = [\"hello there\", \"general kenobi\"]\n    >>> bleurt = datasets.load_metric(\"bleurt\")\n    >>> results = bleurt.compute(predictions=predictions, references=references)\n    >>> print([round(v, 2) for v in results[\"scores\"]])\n    [1.03, 1.04]\n"

__A						       =      {
    "bleurt-tiny-128": "https://storage.googleapis.com/bleurt-oss/bleurt-tiny-128.zip",
    "bleurt-tiny-512": "https://storage.googleapis.com/bleurt-oss/bleurt-tiny-512.zip",
    "bleurt-base-128": "https://storage.googleapis.com/bleurt-oss/bleurt-base-128.zip",
    "bleurt-base-512": "https://storage.googleapis.com/bleurt-oss/bleurt-base-512.zip",
    "bleurt-large-128": "https://storage.googleapis.com/bleurt-oss/bleurt-large-128.zip",
    "bleurt-large-512": "https://storage.googleapis.com/bleurt-oss/bleurt-large-512.zip",
    "BLEURT-20-D3": "https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D3.zip",
    "BLEURT-20-D6": "https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D6.zip",
    "BLEURT-20-D12": "https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D12.zip",
    "BLEURT-20": "https://storage.googleapis.com/bleurt-oss-21/BLEURT-20.zip",
}





@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION,							_KWARGS_DESCRIPTION  )
class    snake_case     (    datasets.Metric  ):

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



						'''simple docstring'''


						return datasets.MetricInfo(
						    description=_DESCRIPTION					,  citation=_CITATION					,  homepage="https://github.com/google-research/bleurt"					,  inputs_description=_KWARGS_DESCRIPTION					,  features=datasets.Features(
						        {
						            "predictions": datasets.Value("string"					,  id="sequence"),
						            "references": datasets.Value("string"					,  id="sequence"),
						        })					,  codebase_urls=["https://github.com/google-research/bleurt"]					,  reference_urls=["https://github.com/google-research/bleurt", "https://arxiv.org/abs/2004.04696"]					,  )

				def 							lowercase_					(     self							:    int					,  UpperCamelCase__							:    Dict)->       List[str]:



						'''simple docstring'''


						if self.config_name == "default":
								logger.warning(
								    "Using default BLEURT-Base checkpoint for sequence maximum length 128. "
								    "You can use a bigger model for better results with e.g.: datasets.load_metric('bleurt', 'bleurt-large-512').")
								__lowerCAmelCase:	List[str]   				=      "bleurt-base-128"

						if self.config_name.lower() in CHECKPOINT_URLS:
								__lowerCAmelCase:	Optional[int]   				=      self.config_name.lower()

						elif self.config_name.upper() in CHECKPOINT_URLS:
								__lowerCAmelCase:	Tuple   				=      self.config_name.upper()

						else:
								raise KeyError(
								    f"{self.config_name} model not found. You should supply the name of a model checkpoint for bleurt in {CHECKPOINT_URLS.keys()}")

						# download the model checkpoint specified by self.config_name and set up the scorer
						__lowerCAmelCase:	Union[str, Any]   				=      dl_manager.download_and_extract(CHECKPOINT_URLS[checkpoint_name])
						__lowerCAmelCase:	Dict   				=      score.BleurtScorer(os.path.join(UpperCamelCase__					,  UpperCamelCase__))




				def 							lowercase_					(     self							:    Optional[int]					,  UpperCamelCase__							:    Tuple					,  UpperCamelCase__							:    int)->       str:



						'''simple docstring'''


						__lowerCAmelCase:	str   				=      self.scorer.score(references=UpperCamelCase__					,  candidates=UpperCamelCase__)
						return {"scores": scores}


