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




   """simple docstring"""







   def __init__(				self						:					Tuple       ,   _snake_case						:					Optional[Any]       ,   _snake_case						:					Union[str, Any]=13       ,   _snake_case						:					List[str]=7       ,   _snake_case						:					List[str]=True       ,   _snake_case						:					Optional[int]=True       ,   _snake_case						:					Any=True       ,   _snake_case						:					int=True       ,   _snake_case						:					List[str]=True       ,   _snake_case						:					Any=False       ,   _snake_case						:					int=False       ,   _snake_case						:					Tuple=False       ,   _snake_case						:					int=2       ,   _snake_case						:					Tuple=99       ,   _snake_case						:					Dict=0       ,   _snake_case						:					List[str]=32       ,   _snake_case						:					Dict=5       ,   _snake_case						:					Optional[int]=4       ,   _snake_case						:					Tuple=0.1       ,   _snake_case						:					int=0.1       ,   _snake_case						:					Union[str, Any]=512       ,   _snake_case						:					Any=2       ,   _snake_case						:					Optional[Any]=0.0_2       ,   _snake_case						:					Dict=2       ,   _snake_case						:					List[str]=4       ,   _snake_case						:					List[str]="last"       ,   _snake_case						:					Optional[Any]=True       ,   _snake_case						:					Dict=None       ,   _snake_case						:					Tuple=0       ,   )      -> str:







        """simple docstring"""
        A_				 =						parent
        A_				 =						batch_size
        A_				 =						seq_length
        A_				 =						is_training
        A_				 =						use_input_lengths
        A_				 =						use_token_type_ids
        A_				 =						use_labels
        A_				 =						gelu_activation
        A_				 =						sinusoidal_embeddings
        A_				 =						causal
        A_				 =						asm
        A_				 =						n_langs
        A_				 =						vocab_size
        A_				 =						n_special
        A_				 =						hidden_size
        A_				 =						num_hidden_layers
        A_				 =						num_attention_heads
        A_				 =						hidden_dropout_prob
        A_				 =						attention_probs_dropout_prob
        A_				 =						max_position_embeddings
        A_				 =						type_sequence_label_size
        A_				 =						initializer_range
        A_				 =						num_labels
        A_				 =						num_choices
        A_				 =						summary_type
        A_				 =						use_proj
        A_				 =						scope
        A_				 =						bos_token_id




   def      lowerCamelCase__     (				self						:					str	)      -> Any:







        """simple docstring"""
        A_				 =						ids_tensor([self.batch_size, self.seq_length]       ,   self.vocab_size	)
        A_				 =						random_attention_mask([self.batch_size, self.seq_length]	)

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

        A_				 =						None
        if self.use_token_type_ids:
             A_				 =						ids_tensor([self.batch_size, self.seq_length]       ,   self.n_langs	)

        A_				 =						None
        A_				 =						None
        A_				 =						None
        if self.use_labels:
             A_				 =						ids_tensor([self.batch_size]       ,   self.type_sequence_label_size	)
             A_				 =						ids_tensor([self.batch_size, self.seq_length]       ,   self.num_labels	)
             A_				 =						ids_tensor([self.batch_size]       ,   2	).float()
             A_				 =						ids_tensor([self.batch_size]       ,   self.num_choices	)

        A_				 =						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						:					List[str]	)      -> Union[str, Any]:







        """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      lowerCamelCase__     (				self						:					Tuple       ,   _snake_case						:					Optional[Any]       ,   _snake_case						:					Dict       ,   _snake_case						:					List[str]       ,   _snake_case						:					List[Any]       ,   _snake_case						:					Union[str, Any]       ,   _snake_case						:					Dict       ,   _snake_case						:					Dict       ,   _snake_case						:					Optional[Any]       ,   _snake_case						:					List[Any]       ,   )      -> Optional[int]:







        """simple docstring"""
        A_				 =						XLMModel(config=_snake_case	)
        model.to(_snake_case	)
        model.eval()
        A_				 =						model(_snake_case       ,   lengths=_snake_case       ,   langs=_snake_case	)
        A_				 =						model(_snake_case       ,   langs=_snake_case	)
        A_				 =						model(_snake_case	)
        self.parent.assertEqual(result.last_hidden_state.shape       ,   (self.batch_size, self.seq_length, self.hidden_size)	)




   def      lowerCamelCase__     (				self						:					List[str]       ,   _snake_case						:					Optional[Any]       ,   _snake_case						:					Any       ,   _snake_case						:					List[Any]       ,   _snake_case						:					Dict       ,   _snake_case						:					Union[str, Any]       ,   _snake_case						:					Any       ,   _snake_case						:					Tuple       ,   _snake_case						:					Union[str, Any]       ,   _snake_case						:					Any       ,   )      -> List[str]:







        """simple docstring"""
        A_				 =						XLMWithLMHeadModel(_snake_case	)
        model.to(_snake_case	)
        model.eval()

        A_				 =						model(_snake_case       ,   token_type_ids=_snake_case       ,   labels=_snake_case	)
        self.parent.assertEqual(result.loss.shape       ,   ()	)
        self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.seq_length, self.vocab_size)	)




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







        """simple docstring"""
        A_				 =						XLMForQuestionAnsweringSimple(_snake_case	)
        model.to(_snake_case	)
        model.eval()

        A_				 =						model(_snake_case	)

        A_				 =						model(_snake_case       ,   start_positions=_snake_case       ,   end_positions=_snake_case	)
        A_				 =						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						:					Union[str, Any]       ,   _snake_case						:					Optional[int]       ,   _snake_case						:					List[str]       ,   _snake_case						:					Optional[int]       ,   _snake_case						:					Union[str, Any]       ,   _snake_case						:					Any       ,   _snake_case						:					Union[str, Any]       ,   _snake_case						:					Tuple       ,   _snake_case						:					int       ,   _snake_case						:					Optional[Any]       ,   )      -> Union[str, Any]:







        """simple docstring"""
        A_				 =						XLMForQuestionAnswering(_snake_case	)
        model.to(_snake_case	)
        model.eval()

        A_				 =						model(_snake_case	)

        A_				 =						model(
            _snake_case       ,   start_positions=_snake_case       ,   end_positions=_snake_case       ,   cls_index=_snake_case       ,   is_impossible=_snake_case       ,   p_mask=_snake_case       ,   )

        A_				 =						model(
            _snake_case       ,   start_positions=_snake_case       ,   end_positions=_snake_case       ,   cls_index=_snake_case       ,   is_impossible=_snake_case       ,   )

        (A_							)				 =						result_with_labels.to_tuple()

        A_				 =						model(_snake_case       ,   start_positions=_snake_case       ,   end_positions=_snake_case	)

        (A_							)				 =						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						:					int       ,   _snake_case						:					int       ,   _snake_case						:					Union[str, Any]       ,   _snake_case						:					Union[str, Any]       ,   _snake_case						:					Tuple       ,   _snake_case						:					Optional[Any]       ,   _snake_case						:					Tuple       ,   _snake_case						:					str       ,   _snake_case						:					Optional[Any]       ,   _snake_case						:					Optional[Any]       ,   )      -> Optional[Any]:







        """simple docstring"""
        A_				 =						XLMForSequenceClassification(_snake_case	)
        model.to(_snake_case	)
        model.eval()

        A_				 =						model(_snake_case	)
        A_				 =						model(_snake_case       ,   labels=_snake_case	)
        self.parent.assertEqual(result.loss.shape       ,   ()	)
        self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.type_sequence_label_size)	)




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







        """simple docstring"""
        A_				 =						self.num_labels
        A_				 =						XLMForTokenClassification(_snake_case	)
        model.to(_snake_case	)
        model.eval()

        A_				 =						model(_snake_case       ,   attention_mask=_snake_case       ,   labels=_snake_case	)
        self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.seq_length, self.num_labels)	)




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







        """simple docstring"""
        A_				 =						self.num_choices
        A_				 =						XLMForMultipleChoice(config=_snake_case	)
        model.to(_snake_case	)
        model.eval()
        A_				 =						input_ids.unsqueeze(1	).expand(-1       ,   self.num_choices       ,   -1	).contiguous()
        A_				 =						token_type_ids.unsqueeze(1	).expand(-1       ,   self.num_choices       ,   -1	).contiguous()
        A_				 =						input_mask.unsqueeze(1	).expand(-1       ,   self.num_choices       ,   -1	).contiguous()
        A_				 =						model(
            _snake_case       ,   attention_mask=_snake_case       ,   token_type_ids=_snake_case       ,   labels=_snake_case       ,   )
        self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.num_choices)	)






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







        """simple docstring"""
        A_				 =						self.prepare_config_and_inputs()
        (
            A_							
        )				 =						config_and_inputs
        A_				 =						{"input_ids": input_ids, "token_type_ids": token_type_ids, "lengths": input_lengths}
        return config, inputs_dict





@require_torch
class __lowerCAmelCase				(					lowercase__     ,				lowercase__     ,				lowercase__     ,				unittest.TestCase      ):




   """simple docstring"""







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







        """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      lowerCamelCase__     (				self						:					Optional[Any]       ,   _snake_case						:					Tuple       ,   _snake_case						:					Tuple       ,   _snake_case						:					List[str]=False	)      -> Union[str, Any]:







        """simple docstring"""
        A_				 =						super()._prepare_for_class(_snake_case       ,   _snake_case       ,   return_labels=_snake_case	)

        if return_labels:
             if model_class.__name__ == "XLMForQuestionAnswering":
                  A_				 =						torch.zeros(
                      self.model_tester.batch_size       ,   dtype=torch.long       ,   device=_snake_case	)
                  A_				 =						torch.zeros(
                      self.model_tester.batch_size       ,   dtype=torch.long       ,   device=_snake_case	)

        return inputs_dict




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







        """simple docstring"""
        A_				 =						XLMModelTester(self	)
        A_				 =						ConfigTester(self       ,   config_class=_snake_case       ,   emb_dim=37	)




   def      lowerCamelCase__     (				self						:					Any	)      -> str:







        """simple docstring"""
        self.config_tester.run_common_tests()




   def      lowerCamelCase__     (				self						:					Any	)      -> Tuple:







        """simple docstring"""
        A_				 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_model(*_snake_case	)




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







        """simple docstring"""
        A_				 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_lm_head(*_snake_case	)




   def      lowerCamelCase__     (				self						:					List[str]	)      -> int:







        """simple docstring"""
        A_				 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_simple_qa(*_snake_case	)




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







        """simple docstring"""
        A_				 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_qa(*_snake_case	)




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







        """simple docstring"""
        A_				 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_sequence_classif(*_snake_case	)




   def      lowerCamelCase__     (				self						:					int	)      -> Any:







        """simple docstring"""
        A_				 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_token_classif(*_snake_case	)




   def      lowerCamelCase__     (				self						:					int	)      -> Any:







        """simple docstring"""
        A_				 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_for_multiple_choice(*_snake_case	)




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







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

        for idx, iter_attentions in enumerate(_snake_case	):
             # adds PAD dummy token
             A_				 =						min_length + idx + 1
             A_				 =						min_length + idx + 1

             A_				 =						(
                 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(_snake_case	)	)




   def      lowerCamelCase__     (				self						:					Any       ,   _snake_case						:					str       ,   _snake_case						:					Tuple       ,   _snake_case						:					Any       ,   _snake_case						:					Any       ,   _snake_case						:					int       ,   _snake_case						:					Union[str, Any]=False       ,   _snake_case						:					str=1	)      -> List[str]:







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

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






   @slow
   def      lowerCamelCase__     (				self						:					Optional[Any]	)      -> Any:







        """simple docstring"""
        for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             A_				 =						XLMModel.from_pretrained(_snake_case	)
             self.assertIsNotNone(_snake_case	)





@require_torch
class __lowerCAmelCase				(					unittest.TestCase      ):




   """simple docstring"""







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







        """simple docstring"""
        A_				 =						XLMWithLMHeadModel.from_pretrained("xlm-mlm-en-2048"	)
        model.to(_snake_case	)
        A_				 =						torch.tensor([[14, 447]]       ,   dtype=torch.long       ,   device=_snake_case	)  # the president
        A_				 =						[
            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_				 =						model.generate(_snake_case       ,   do_sample=_snake_case	)
        self.assertListEqual(output_ids[0].cpu().numpy().tolist()       ,   _snake_case	)






"""simple docstring"""





from __future__ import annotations

import math

UpperCamelCase_					:						List[str]				= '''2020.9.26'''
UpperCamelCase_					:						List[Any]				= '''xcodz-dot, cclaus, dhruvmanila'''
def 							A_     (__a				,   __a				,   __a				,   __a				,   __a				):

					'''simple docstring'''





					if not all(isinstance(__a				,   (float, int)				) for val in locals().values()				):
										A_				 =						f'Input values must either be float or int: {list(locals().values()				)}'
										raise TypeError(__a				)
					A_				 =						((x * distance) / (z + distance)) * scale
					A_				 =						((y * distance) / (z + distance)) * scale
					return projected_x, projected_y
def 							A_     (__a				,   __a				,   __a				,   __a				,   __a				):

					'''simple docstring'''





					if not isinstance(__a				,   __a				):
										raise TypeError("Axis must be a str"				)
					A_				 =						locals()
					del input_variables["axis"]
					if not all(isinstance(__a				,   (float, int)				) for val in input_variables.values()				):
										A_				 =						(
										    "Input values except axis must either be float or int: "
										    f'{list(input_variables.values()				)}'
										)
										raise TypeError(__a				)
					A_				 =						(angle % 360) / 450 * 180 / math.pi
					if axis == "z":
										A_				 =						x * math.cos(__a				) - y * math.sin(__a				)
										A_				 =						y * math.cos(__a				) + x * math.sin(__a				)
										A_				 =						z
					elif axis == "x":
										A_				 =						y * math.cos(__a				) - z * math.sin(__a				)
										A_				 =						z * math.cos(__a				) + y * math.sin(__a				)
										A_				 =						x
					elif axis == "y":
										A_				 =						x * math.cos(__a				) - z * math.sin(__a				)
										A_				 =						z * math.cos(__a				) + x * math.sin(__a				)
										A_				 =						y
					else:
										raise ValueError("not a valid axis, choose one of 'x', 'y', 'z'"				)

					return new_x, new_y, new_z


if __name__ == "__main__":
		import doctest

		doctest.testmod()
		print(F"""{convert_to_ad(1.0, 2.0, 3.0, 10.0, 10.0) = }""")
		print(F"""{rotate(1.0, 2.0, 3.0, 'y', 90.0) = }""")




