






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







	"""simple docstring"""



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

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

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

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

							lowercase_      :   Dict        =			self.get_config()

							return (
							    config,
							    input_ids,
							    token_type_ids,
							    input_lengths,
							    sequence_labels,
							    token_labels,
							    is_impossible_labels,
							    choice_labels,
							    input_mask,
							)
	def        lowerCamelCase__		(							self       )						->  Optional[Any]:
							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        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      )						->  int:
							lowercase_      :   Any        =			XLMModel(config=_lowercase       )
							model.to(_lowercase       )
							model.eval()
							lowercase_      :   List[str]        =			model(_lowercase  ,      lengths=_lowercase  ,      langs=_lowercase       )
							lowercase_      :   Tuple        =			model(_lowercase  ,      langs=_lowercase       )
							lowercase_      :   Tuple        =			model(_lowercase       )
							self.parent.assertEqual(result.last_hidden_state.shape  ,      (self.batch_size, self.seq_length, self.hidden_size)       )
	def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      )						->  List[Any]:
							lowercase_      :   int        =			XLMWithLMHeadModel(_lowercase       )
							model.to(_lowercase       )
							model.eval()

							lowercase_      :   Optional[int]        =			model(_lowercase  ,      token_type_ids=_lowercase  ,      labels=_lowercase       )
							self.parent.assertEqual(result.loss.shape  ,      ()       )
							self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.seq_length, self.vocab_size)       )
	def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      )						->  Tuple:
							lowercase_      :   Union[str, Any]        =			XLMForQuestionAnsweringSimple(_lowercase       )
							model.to(_lowercase       )
							model.eval()

							lowercase_      :   str        =			model(_lowercase       )

							lowercase_      :   str        =			model(_lowercase  ,      start_positions=_lowercase  ,      end_positions=_lowercase       )
							lowercase_      :   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        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      )						->  int:
							lowercase_      :   int        =			XLMForQuestionAnswering(_lowercase       )
							model.to(_lowercase       )
							model.eval()

							lowercase_      :   Union[str, Any]        =			model(_lowercase       )

							lowercase_      :   Any        =			model(
							    _lowercase  ,      start_positions=_lowercase  ,      end_positions=_lowercase  ,      cls_index=_lowercase  ,      is_impossible=_lowercase  ,      p_mask=_lowercase  ,      )

							lowercase_      :   Any        =			model(
							    _lowercase  ,      start_positions=_lowercase  ,      end_positions=_lowercase  ,      cls_index=_lowercase  ,      is_impossible=_lowercase  ,      )

							((lowercase_)  ,      )      :   str        =			result_with_labels.to_tuple()

							lowercase_      :   Tuple        =			model(_lowercase  ,      start_positions=_lowercase  ,      end_positions=_lowercase       )

							((lowercase_)  ,      )      :   Union[str, 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        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      )						->  Optional[Any]:
							lowercase_      :   str        =			XLMForSequenceClassification(_lowercase       )
							model.to(_lowercase       )
							model.eval()

							lowercase_      :   int        =			model(_lowercase       )
							lowercase_      :   Any        =			model(_lowercase  ,      labels=_lowercase       )
							self.parent.assertEqual(result.loss.shape  ,      ()       )
							self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.type_sequence_label_size)       )
	def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      )						->  Optional[Any]:
							lowercase_      :   int        =			self.num_labels
							lowercase_      :   Any        =			XLMForTokenClassification(_lowercase       )
							model.to(_lowercase       )
							model.eval()

							lowercase_      :   Dict        =			model(_lowercase  ,      attention_mask=_lowercase  ,      labels=_lowercase       )
							self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.seq_length, self.num_labels)       )
	def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      )						->  Optional[Any]:
							lowercase_      :   List[Any]        =			self.num_choices
							lowercase_      :   int        =			XLMForMultipleChoice(config=_lowercase       )
							model.to(_lowercase       )
							model.eval()
							lowercase_      :   List[Any]        =			input_ids.unsqueeze(1       ).expand(-1  ,      self.num_choices  ,      -1       ).contiguous()
							lowercase_      :   List[Any]        =			token_type_ids.unsqueeze(1       ).expand(-1  ,      self.num_choices  ,      -1       ).contiguous()
							lowercase_      :   Optional[Any]        =			input_mask.unsqueeze(1       ).expand(-1  ,      self.num_choices  ,      -1       ).contiguous()
							lowercase_      :   str        =			model(
							    _lowercase  ,      attention_mask=_lowercase  ,      token_type_ids=_lowercase  ,      labels=_lowercase  ,      )
							self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.num_choices)       )







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






@require_torch
class 							__magic_name__     (	UpperCAmelCase_,						UpperCAmelCase_,						UpperCAmelCase_,						unittest.TestCase				):







	"""simple docstring"""



	SCREAMING_SNAKE_CASE_					:      Union[str, Any]        =  (
	    (
	        XLMModel,
	        XLMWithLMHeadModel,
	        XLMForQuestionAnswering,
	        XLMForSequenceClassification,
	        XLMForQuestionAnsweringSimple,
	        XLMForTokenClassification,
	        XLMForMultipleChoice,
	    )
	    if is_torch_available()
	    else ()
	)
	SCREAMING_SNAKE_CASE_					:      int        =  (
	    (XLMWithLMHeadModel,) if is_torch_available() else ()
	)  # TODO (PVP): Check other models whether language generation is also applicable
	SCREAMING_SNAKE_CASE_					:      Optional[int]        =  (
	    {
	        '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        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase       )						->  Optional[Any]:
							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  ,      _lowercase  ,      _lowercase  ,      _lowercase=False       )						->  Optional[Any]:
							lowercase_      :   Dict        =			super()._prepare_for_class(_lowercase  ,      _lowercase  ,      return_labels=_lowercase       )

							if return_labels:
													if model_class.__name__ == "XLMForQuestionAnswering":
																			lowercase_      :   str        =			torch.zeros(
																			    self.model_tester.batch_size  ,      dtype=torch.long  ,      device=_lowercase       )
																			lowercase_      :   Union[str, Any]        =			torch.zeros(
																			    self.model_tester.batch_size  ,      dtype=torch.long  ,      device=_lowercase       )

							return inputs_dict
	def        lowerCamelCase__		(							self       )						->  str:
							lowercase_      :   Optional[Any]        =			XLMModelTester(self       )
							lowercase_      :   Tuple        =			ConfigTester(self  ,      config_class=_lowercase  ,      emb_dim=37       )
	def        lowerCamelCase__		(							self       )						->  str:
							self.config_tester.run_common_tests()
	def        lowerCamelCase__		(							self       )						->  Dict:
							lowercase_      :   List[str]        =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_model(*_lowercase       )
	def        lowerCamelCase__		(							self       )						->  Any:
							lowercase_      :   Dict        =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_lm_head(*_lowercase       )
	def        lowerCamelCase__		(							self       )						->  str:
							lowercase_      :   Optional[int]        =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_simple_qa(*_lowercase       )
	def        lowerCamelCase__		(							self       )						->  Tuple:
							lowercase_      :   Tuple        =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_qa(*_lowercase       )
	def        lowerCamelCase__		(							self       )						->  List[str]:
							lowercase_      :   Optional[Any]        =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_sequence_classif(*_lowercase       )
	def        lowerCamelCase__		(							self       )						->  Dict:
							lowercase_      :   Dict        =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_token_classif(*_lowercase       )
	def        lowerCamelCase__		(							self       )						->  List[str]:
							lowercase_      :   int        =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_xlm_for_multiple_choice(*_lowercase       )
	def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase=False  ,      _lowercase=1       )						->  int:
							self.assertIsInstance(_lowercase  ,      _lowercase       )
							self.assertListEqual(
							    [isinstance(_lowercase  ,      _lowercase       ) for iter_attentions in attentions]  ,      [True] * len(_lowercase       )       )
							self.assertEqual(len(_lowercase       )  ,      (max_length - min_length) * num_beam_groups       )

							for idx, iter_attentions in enumerate(_lowercase       ):
													# adds PAD dummy token
													lowercase_      :   Optional[int]        =			min_length + idx + 1
													lowercase_      :   Optional[Any]        =			min_length + idx + 1

													lowercase_      :   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(_lowercase       )       )
	def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase=False  ,      _lowercase=1       )						->  Any:
							self.assertIsInstance(_lowercase  ,      _lowercase       )
							self.assertListEqual(
							    [isinstance(_lowercase  ,      _lowercase       ) for iter_hidden_states in hidden_states]  ,      [True] * len(_lowercase       )  ,      )
							self.assertEqual(len(_lowercase       )  ,      (max_length - min_length) * num_beam_groups       )

							for idx, iter_hidden_states in enumerate(_lowercase       ):
													# adds PAD dummy token
													lowercase_      :   Union[str, Any]        =			min_length + idx + 1
													lowercase_      :   Tuple        =			(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(_lowercase       )  ,      )
							pass







	@slow
	def        lowerCamelCase__		(							self       )						->  int:
							for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													lowercase_      :   Optional[Any]        =			XLMModel.from_pretrained(_lowercase       )
													self.assertIsNotNone(_lowercase       )






@require_torch
class 							__magic_name__     (	unittest.TestCase				):







	"""simple docstring"""



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








'''simple docstring'''

from __future__ import annotations






def 	_UpperCAmelCase    (  a		:	int = 4							)			->			list[list[int]]:





						"""simple docstring"""
						lowercase_      :   Tuple        =			abs(a							) or 4
						return [[1 + x + y * row_size for x in range(a							)] for y in range(a							)]






def 	_UpperCAmelCase    (  a		:	list[list[int]]							)			->			list[list[int]]:





						"""simple docstring"""
						return reverse_row(transpose(a							)							)
						# OR.. transpose(reverse_column(matrix))






def 	_UpperCAmelCase    (  a		:	list[list[int]]							)			->			list[list[int]]:





						"""simple docstring"""
						return reverse_row(reverse_column(a							)							)
						# OR.. reverse_column(reverse_row(matrix))






def 	_UpperCAmelCase    (  a		:	list[list[int]]							)			->			list[list[int]]:





						"""simple docstring"""
						return reverse_column(transpose(a							)							)
						# OR.. transpose(reverse_row(matrix))






def 	_UpperCAmelCase    (  a		:	list[list[int]]							)			->			list[list[int]]:





						"""simple docstring"""
						lowercase_      :   Any        =			[list(a							) for x in zip(*a							)]
						return matrix






def 	_UpperCAmelCase    (  a		:	list[list[int]]							)			->			list[list[int]]:





						"""simple docstring"""
						lowercase_      :   List[str]        =			matrix[::-1]
						return matrix






def 	_UpperCAmelCase    (  a		:	list[list[int]]							)			->			list[list[int]]:





						"""simple docstring"""
						lowercase_      :   str        =			[x[::-1] for x in matrix]
						return matrix






def 	_UpperCAmelCase    (  a		:	list[list[int]]							)			->			None:





						"""simple docstring"""
						for i in matrix:
												print(*a							)


if __name__ == "__main__":
						A:      Dict  				=				make_matrix()
						print("\norigin:\n")
						print_matrix(matrix)
						print("\nrotate 90 counterclockwise:\n")
						print_matrix(rotate_aa(matrix))

						A:      List[Any]  				=				make_matrix()
						print("\norigin:\n")
						print_matrix(matrix)
						print("\nrotate 180:\n")
						print_matrix(rotate_aaa(matrix))

						A:      List[str]  				=				make_matrix()
						print("\norigin:\n")
						print_matrix(matrix)
						print("\nrotate 270 counterclockwise:\n")
						print_matrix(rotate_aaa(matrix))
