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

				def __init__( self      ,					_lowerCamelCase      ,					_lowerCamelCase=13      ,					_lowerCamelCase=7      ,					_lowerCamelCase=True      ,					_lowerCamelCase=True      ,					_lowerCamelCase=True      ,					_lowerCamelCase=True      ,					_lowerCamelCase=True      ,					_lowerCamelCase=False      ,					_lowerCamelCase=False      ,					_lowerCamelCase=False      ,					_lowerCamelCase=2      ,					_lowerCamelCase=99      ,					_lowerCamelCase=0      ,					_lowerCamelCase=32      ,					_lowerCamelCase=5      ,					_lowerCamelCase=4      ,					_lowerCamelCase=0.1      ,					_lowerCamelCase=0.1      ,					_lowerCamelCase=512      ,					_lowerCamelCase=2      ,					_lowerCamelCase=0.0_2      ,					_lowerCamelCase=2      ,					_lowerCamelCase=4      ,					_lowerCamelCase="last"      ,					_lowerCamelCase=True      ,					_lowerCamelCase=None      ,					_lowerCamelCase=0      ,					):
										lowerCamelCase__   =parent
										lowerCamelCase__   =batch_size
										lowerCamelCase__   =seq_length
										lowerCamelCase__   =is_training
										lowerCamelCase__   =use_input_lengths
										lowerCamelCase__   =use_token_type_ids
										lowerCamelCase__   =use_labels
										lowerCamelCase__   =gelu_activation
										lowerCamelCase__   =sinusoidal_embeddings
										lowerCamelCase__   =causal
										lowerCamelCase__   =asm
										lowerCamelCase__   =n_langs
										lowerCamelCase__   =vocab_size
										lowerCamelCase__   =n_special
										lowerCamelCase__   =hidden_size
										lowerCamelCase__   =num_hidden_layers
										lowerCamelCase__   =num_attention_heads
										lowerCamelCase__   =hidden_dropout_prob
										lowerCamelCase__   =attention_probs_dropout_prob
										lowerCamelCase__   =max_position_embeddings
										lowerCamelCase__   =type_sequence_label_size
										lowerCamelCase__   =initializer_range
										lowerCamelCase__   =num_labels
										lowerCamelCase__   =num_choices
										lowerCamelCase__   =summary_type
										lowerCamelCase__   =use_proj
										lowerCamelCase__   =scope
										lowerCamelCase__   =bos_token_id

				def 						_a      ( self ):
										lowerCamelCase__   =ids_tensor([self.batch_size, self.seq_length]      ,					self.vocab_size )
										lowerCamelCase__   =random_attention_mask([self.batch_size, self.seq_length] )

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

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

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

										lowerCamelCase__   =self.get_config()

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

				def 						_a      ( self ):
										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 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					):
										lowerCamelCase__   =XLMModel(config=_lowerCamelCase )
										model.to(_lowerCamelCase )
										model.eval()
										lowerCamelCase__   =model(_lowerCamelCase      ,					lengths=_lowerCamelCase      ,					langs=_lowerCamelCase )
										lowerCamelCase__   =model(_lowerCamelCase      ,					langs=_lowerCamelCase )
										lowerCamelCase__   =model(_lowerCamelCase )
										self.parent.assertEqual(result.last_hidden_state.shape      ,					(self.batch_size, self.seq_length, self.hidden_size) )

				def 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					):
										lowerCamelCase__   =XLMWithLMHeadModel(_lowerCamelCase )
										model.to(_lowerCamelCase )
										model.eval()

										lowerCamelCase__   =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 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					):
										lowerCamelCase__   =XLMForQuestionAnsweringSimple(_lowerCamelCase )
										model.to(_lowerCamelCase )
										model.eval()

										lowerCamelCase__   =model(_lowerCamelCase )

										lowerCamelCase__   =model(_lowerCamelCase      ,					start_positions=_lowerCamelCase      ,					end_positions=_lowerCamelCase )
										lowerCamelCase__   =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 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					):
										lowerCamelCase__   =XLMForQuestionAnswering(_lowerCamelCase )
										model.to(_lowerCamelCase )
										model.eval()

										lowerCamelCase__   =model(_lowerCamelCase )

										lowerCamelCase__   =model(
										    _lowerCamelCase      ,					start_positions=_lowerCamelCase      ,					end_positions=_lowerCamelCase      ,					cls_index=_lowerCamelCase      ,					is_impossible=_lowerCamelCase      ,					p_mask=_lowerCamelCase      ,					)

										lowerCamelCase__   =model(
										    _lowerCamelCase      ,					start_positions=_lowerCamelCase      ,					end_positions=_lowerCamelCase      ,					cls_index=_lowerCamelCase      ,					is_impossible=_lowerCamelCase      ,					)

										((lowerCamelCase__)   , )   =result_with_labels.to_tuple()

										lowerCamelCase__   =model(_lowerCamelCase      ,					start_positions=_lowerCamelCase      ,					end_positions=_lowerCamelCase )

										((lowerCamelCase__)   , )   =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 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					):
										lowerCamelCase__   =XLMForSequenceClassification(_lowerCamelCase )
										model.to(_lowerCamelCase )
										model.eval()

										lowerCamelCase__   =model(_lowerCamelCase )
										lowerCamelCase__   =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 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					):
										lowerCamelCase__   =self.num_labels
										lowerCamelCase__   =XLMForTokenClassification(_lowerCamelCase )
										model.to(_lowerCamelCase )
										model.eval()

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

				def 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					):
										lowerCamelCase__   =self.num_choices
										lowerCamelCase__   =XLMForMultipleChoice(config=_lowerCamelCase )
										model.to(_lowerCamelCase )
										model.eval()
										lowerCamelCase__   =input_ids.unsqueeze(1 ).expand(-1      ,					self.num_choices      ,					-1 ).contiguous()
										lowerCamelCase__   =token_type_ids.unsqueeze(1 ).expand(-1      ,					self.num_choices      ,					-1 ).contiguous()
										lowerCamelCase__   =input_mask.unsqueeze(1 ).expand(-1      ,					self.num_choices      ,					-1 ).contiguous()
										lowerCamelCase__   =model(
										    _lowerCamelCase      ,					attention_mask=_lowerCamelCase      ,					token_type_ids=_lowerCamelCase      ,					labels=_lowerCamelCase      ,					)
										self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.num_choices) )






				def 						_a      ( self ):
										lowerCamelCase__   =self.prepare_config_and_inputs()
										(
										    (
										    lowerCamelCase__
										)   , (
										    lowerCamelCase__
										)   , (
										    lowerCamelCase__
										)   , (
										    lowerCamelCase__
										)   , (
										    lowerCamelCase__
										)   , (
										    lowerCamelCase__
										)   , (
										    lowerCamelCase__
										)   , (
										    lowerCamelCase__
										)   , (
										    lowerCamelCase__
										)   , 
										)   =config_and_inputs
										lowerCamelCase__   ={"input_ids": input_ids, "token_type_ids": token_type_ids, "lengths": input_lengths}
										return config, inputs_dict






@require_torch
class   __UpperCAmelCase		(      __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , unittest.TestCase      ):
				A__ :  List[str]    				=			(
				    (
				        XLMModel,
				        XLMWithLMHeadModel,
				        XLMForQuestionAnswering,
				        XLMForSequenceClassification,
				        XLMForQuestionAnsweringSimple,
				        XLMForTokenClassification,
				        XLMForMultipleChoice,
				    )
				    if is_torch_available()
				    else ()
				)
				A__ :  Any    				=			(
				    (XLMWithLMHeadModel,) if is_torch_available() else ()
				)  # TODO (PVP): Check other models whether language generation is also applicable
				A__ :  Optional[Any]    				=			(
				    {
				        '''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 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase ):
										if (
										    pipeline_test_casse_name == "QAPipelineTests"
										    and tokenizer_name is not None
										    and not tokenizer_name.endswith("Fast" )
										):
																# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
																# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
																# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
																return True

										return False

				def 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase=False ):
										lowerCamelCase__   =super()._prepare_for_class(_lowerCamelCase      ,					_lowerCamelCase      ,					return_labels=_lowerCamelCase )

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

										return inputs_dict

				def 						_a      ( self ):
										lowerCamelCase__   =XLMModelTester(self )
										lowerCamelCase__   =ConfigTester(self      ,					config_class=_lowerCamelCase      ,					emb_dim=37 )

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

				def 						_a      ( self ):
										lowerCamelCase__   =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_model(*_lowerCamelCase )

				def 						_a      ( self ):
										lowerCamelCase__   =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_lm_head(*_lowerCamelCase )

				def 						_a      ( self ):
										lowerCamelCase__   =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_simple_qa(*_lowerCamelCase )

				def 						_a      ( self ):
										lowerCamelCase__   =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_qa(*_lowerCamelCase )

				def 						_a      ( self ):
										lowerCamelCase__   =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_sequence_classif(*_lowerCamelCase )

				def 						_a      ( self ):
										lowerCamelCase__   =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_token_classif(*_lowerCamelCase )

				def 						_a      ( self ):
										lowerCamelCase__   =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_for_multiple_choice(*_lowerCamelCase )

				def 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase=False      ,					_lowerCamelCase=1 ):
										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
																lowerCamelCase__   =min_length + idx + 1
																lowerCamelCase__   =min_length + idx + 1

																lowerCamelCase__   =(
																    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 						_a      ( self      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase      ,					_lowerCamelCase=False      ,					_lowerCamelCase=1 ):
										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
																lowerCamelCase__   =min_length + idx + 1
																lowerCamelCase__   =(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 						_a      ( self ):
										for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																lowerCamelCase__   =XLMModel.from_pretrained(_lowerCamelCase )
																self.assertIsNotNone(_lowerCamelCase )






@require_torch
class   __UpperCAmelCase		(      unittest.TestCase      ):

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



"""simple docstring"""


from collections.abc import Callable
from math import pi, sqrt
from random import uniform
from statistics import mean





def      lowerCamelCase_					(   __lowerCAmelCase							)				->   List[Any]:




						'''simple docstring'''



						def is_in_circle(__lowerCAmelCase  ,       __lowerCAmelCase							) -> bool:
												lowerCamelCase__   =sqrt((x**2) + (y**2)							)
												# Our circle has a radius of 1, so a distance
												# greater than 1 would land outside the circle.
												return distance_from_centre <= 1

						# The proportion of guesses that landed in the circle
						lowerCamelCase__   =mean(
						    int(is_in_circle(uniform(-1.0  ,       1.0							)  ,       uniform(-1.0  ,       1.0							)							)							)
						    for _ in range(__lowerCAmelCase							)							)
						# The ratio of the area for circle to square is pi/4.
						lowerCamelCase__   =proportion * 4
						print(F'''The estimated value of pi is {pi_estimate}'''							)
						print(F'''The numpy value of pi is {pi}'''							)
						print(F'''The total error is {abs(pi - pi_estimate							)}'''							)





def      lowerCamelCase_					(   __lowerCAmelCase  ,       __lowerCAmelCase  ,       __lowerCAmelCase = 0.0  ,       __lowerCAmelCase = 1.0  ,       )				->   float:




						'''simple docstring'''



						return mean(
						    function_to_integrate(uniform(__lowerCAmelCase  ,       __lowerCAmelCase							)							) for _ in range(__lowerCAmelCase							)							) * (max_value - min_value)





def      lowerCamelCase_					(   __lowerCAmelCase  ,       __lowerCAmelCase = 0.0  ,       __lowerCAmelCase = 1.0							)				->   None:




						'''simple docstring'''



						def identity_function(__lowerCAmelCase							) -> float:
												return x

						lowerCamelCase__   =area_under_curve_estimator(
						    __lowerCAmelCase  ,       __lowerCAmelCase  ,       __lowerCAmelCase  ,       __lowerCAmelCase							)
						lowerCamelCase__   =(max_value * max_value - min_value * min_value) / 2

						print("******************"							)
						print(F'''Estimating area under y=x where x varies from {min_value} to {max_value}'''							)
						print(F'''Estimated value is {estimated_value}'''							)
						print(F'''Expected value is {expected_value}'''							)
						print(F'''Total error is {abs(estimated_value - expected_value							)}'''							)
						print("******************"							)





def      lowerCamelCase_					(   __lowerCAmelCase							)				->   None:




						'''simple docstring'''



						def function_to_integrate(__lowerCAmelCase							) -> float:
												return sqrt(4.0 - x * x							)

						lowerCamelCase__   =area_under_curve_estimator(
						    __lowerCAmelCase  ,       __lowerCAmelCase  ,       0.0  ,       2.0							)

						print("******************"							)
						print("Estimating pi using area_under_curve_estimator"							)
						print(F'''Estimated value is {estimated_value}'''							)
						print(F'''Expected value is {pi}'''							)
						print(F'''Total error is {abs(estimated_value - pi							)}'''							)
						print("******************"							)


if __name__ == "__main__":
	import doctest

	doctest.testmod()


