






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


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


			def      UpperCAmelCase__						(				self					:  Tuple ):
										lowerCamelCase_	:							List[Any]									=ids_tensor([self.batch_size, self.seq_length]       ,	self.vocab_size )
										lowerCamelCase_	:							Optional[int]									=random_attention_mask([self.batch_size, self.seq_length] )

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

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

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

										lowerCamelCase_	:							Optional[int]									=self.get_config()

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


			def      UpperCAmelCase__						(				self					:  Union[str, 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      UpperCAmelCase__						(				self					:  Dict       ,	snake_case__					:  Union[str, Any]       ,	snake_case__					:  Tuple       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  int       ,	snake_case__					:  Union[str, Any]       ,	snake_case__					:  Tuple       ,	snake_case__					:  Tuple       ,	snake_case__					:  Tuple       ,	snake_case__					:  Tuple       ,	):
										lowerCamelCase_	:							str									=XLMModel(config=_a )
										model.to(_a )
										model.eval()
										lowerCamelCase_	:							Optional[int]									=model(_a       ,	lengths=_a       ,	langs=_a )
										lowerCamelCase_	:							Union[str, Any]									=model(_a       ,	langs=_a )
										lowerCamelCase_	:							Optional[Any]									=model(_a )
										self.parent.assertEqual(result.last_hidden_state.shape       ,	(self.batch_size, self.seq_length, self.hidden_size) )


			def      UpperCAmelCase__						(				self					:  Optional[int]       ,	snake_case__					:  Optional[Any]       ,	snake_case__					:  List[str]       ,	snake_case__					:  int       ,	snake_case__					:  Union[str, Any]       ,	snake_case__					:  Tuple       ,	snake_case__					:  Optional[Any]       ,	snake_case__					:  int       ,	snake_case__					:  List[str]       ,	snake_case__					:  Dict       ,	):
										lowerCamelCase_	:							List[str]									=XLMWithLMHeadModel(_a )
										model.to(_a )
										model.eval()

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


			def      UpperCAmelCase__						(				self					:  Dict       ,	snake_case__					:  Optional[Any]       ,	snake_case__					:  str       ,	snake_case__					:  Tuple       ,	snake_case__					:  List[Any]       ,	snake_case__					:  Dict       ,	snake_case__					:  int       ,	snake_case__					:  Union[str, Any]       ,	snake_case__					:  Dict       ,	snake_case__					:  List[Any]       ,	):
										lowerCamelCase_	:							Any									=XLMForQuestionAnsweringSimple(_a )
										model.to(_a )
										model.eval()

										lowerCamelCase_	:							List[str]									=model(_a )

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

										lowerCamelCase_	:							Optional[Any]									=model(_a )

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

										lowerCamelCase_	:							Optional[int]									=model(
										    _a       ,	start_positions=_a       ,	end_positions=_a       ,	cls_index=_a       ,	is_impossible=_a       ,	)

										((lowerCamelCase_       )							,						       )	:							Dict									=result_with_labels.to_tuple()

										lowerCamelCase_	:							Tuple									=model(_a       ,	start_positions=_a       ,	end_positions=_a )

										((lowerCamelCase_       )							,						       )	:							List[str]									=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      UpperCAmelCase__						(				self					:  List[str]       ,	snake_case__					:  List[str]       ,	snake_case__					:  Tuple       ,	snake_case__					:  int       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  List[str]       ,	snake_case__					:  List[str]       ,	snake_case__					:  Any       ,	snake_case__					:  Union[str, Any]       ,	snake_case__					:  Union[str, Any]       ,	):
										lowerCamelCase_	:							List[Any]									=XLMForSequenceClassification(_a )
										model.to(_a )
										model.eval()

										lowerCamelCase_	:							Union[str, Any]									=model(_a )
										lowerCamelCase_	:							Tuple									=model(_a       ,	labels=_a )
										self.parent.assertEqual(result.loss.shape       ,	() )
										self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.type_sequence_label_size) )


			def      UpperCAmelCase__						(				self					:  str       ,	snake_case__					:  List[Any]       ,	snake_case__					:  Any       ,	snake_case__					:  List[str]       ,	snake_case__					:  List[Any]       ,	snake_case__					:  Any       ,	snake_case__					:  List[Any]       ,	snake_case__					:  List[Any]       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  Union[str, Any]       ,	):
										lowerCamelCase_	:							List[Any]									=self.num_labels
										lowerCamelCase_	:							Optional[Any]									=XLMForTokenClassification(_a )
										model.to(_a )
										model.eval()

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


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


			def      UpperCAmelCase__						(				self					:  int ):
										lowerCamelCase_	:							Dict									=self.prepare_config_and_inputs()
										(
										    (
										    lowerCamelCase_       
										)							,						(
										    lowerCamelCase_       
										)							,						(
										    lowerCamelCase_       
										)							,						(
										    lowerCamelCase_       
										)							,						(
										    lowerCamelCase_       
										)							,						(
										    lowerCamelCase_       
										)							,						(
										    lowerCamelCase_       
										)							,						(
										    lowerCamelCase_       
										)							,						(
										    lowerCamelCase_       
										)							,						       
										)	:							Optional[Any]									=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        lowercase__      (							__lowercase,       __lowercase,       __lowercase,       unittest.TestCase       ):
			_UpperCAmelCase		:str             =			(
			    (
			        XLMModel,
			        XLMWithLMHeadModel,
			        XLMForQuestionAnswering,
			        XLMForSequenceClassification,
			        XLMForQuestionAnsweringSimple,
			        XLMForTokenClassification,
			        XLMForMultipleChoice,
			    )
			    if is_torch_available()
			    else ()
			)
			_UpperCAmelCase		:Any             =			(
			    (XLMWithLMHeadModel,) if is_torch_available() else ()
			)  # TODO (PVP): Check other models whether language generation is also applicable
			_UpperCAmelCase		:Union[str, 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      UpperCAmelCase__						(				self					:  str       ,	snake_case__					:  Tuple       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  Any       ,	snake_case__					:  Tuple       ,	snake_case__					:  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      UpperCAmelCase__						(				self					:  List[Any]       ,	snake_case__					:  int       ,	snake_case__					:  Dict       ,	snake_case__					:  List[Any]=False ):
										lowerCamelCase_	:							List[str]									=super()._prepare_for_class(_a       ,	_a       ,	return_labels=_a )

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

										return inputs_dict


			def      UpperCAmelCase__						(				self					:  Dict ):
										lowerCamelCase_	:							Tuple									=XLMModelTester(self )
										lowerCamelCase_	:							str									=ConfigTester(self       ,	config_class=_a       ,	emb_dim=37 )


			def      UpperCAmelCase__						(				self					:  Any ):
										self.config_tester.run_common_tests()


			def      UpperCAmelCase__						(				self					:  Tuple ):
										lowerCamelCase_	:							Dict									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_model(*_a )


			def      UpperCAmelCase__						(				self					:  int ):
										lowerCamelCase_	:							Optional[Any]									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_lm_head(*_a )


			def      UpperCAmelCase__						(				self					:  int ):
										lowerCamelCase_	:							Optional[Any]									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_simple_qa(*_a )


			def      UpperCAmelCase__						(				self					:  List[Any] ):
										lowerCamelCase_	:							List[str]									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_qa(*_a )


			def      UpperCAmelCase__						(				self					:  Optional[Any] ):
										lowerCamelCase_	:							Optional[Any]									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_sequence_classif(*_a )


			def      UpperCAmelCase__						(				self					:  int ):
										lowerCamelCase_	:							str									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_token_classif(*_a )


			def      UpperCAmelCase__						(				self					:  Dict ):
										lowerCamelCase_	:							Dict									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_for_multiple_choice(*_a )


			def      UpperCAmelCase__						(				self					:  List[str]       ,	snake_case__					:  Union[str, Any]       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  List[str]       ,	snake_case__					:  List[str]       ,	snake_case__					:  str       ,	snake_case__					:  Optional[int]=False       ,	snake_case__					:  Tuple=1 ):
										self.assertIsInstance(_a       ,	_a )
										self.assertListEqual(
										    [isinstance(_a       ,	_a ) for iter_attentions in attentions]       ,	[True] * len(_a ) )
										self.assertEqual(len(_a )       ,	(max_length - min_length) * num_beam_groups )

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

																	lowerCamelCase_	:							Tuple									=(
																	    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(_a ) )


			def      UpperCAmelCase__						(				self					:  List[Any]       ,	snake_case__					:  List[Any]       ,	snake_case__					:  int       ,	snake_case__					:  Any       ,	snake_case__					:  int       ,	snake_case__					:  List[Any]       ,	snake_case__					:  Optional[Any]=False       ,	snake_case__					:  Any=1 ):
										self.assertIsInstance(_a       ,	_a )
										self.assertListEqual(
										    [isinstance(_a       ,	_a ) for iter_hidden_states in hidden_states]       ,	[True] * len(_a )       ,	)
										self.assertEqual(len(_a )       ,	(max_length - min_length) * num_beam_groups )

										for idx, iter_hidden_states in enumerate(_a ):
																	# adds PAD dummy token
																	lowerCamelCase_	:							Optional[int]									=min_length + idx + 1
																	lowerCamelCase_	:							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(_a )       ,	)
										pass


			@slow
			def      UpperCAmelCase__						(				self					:  int ):
										for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowerCamelCase_	:							Dict									=XLMModel.from_pretrained(_a )
																	self.assertIsNotNone(_a )







@require_torch
class        lowercase__      (							unittest.TestCase       ):


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







"""simple docstring"""



from __future__ import annotations

from collections import namedtuple

def 			_snake_case    (					lowerCamelCase__     :   float    ,						lowerCamelCase__     :   float    ,						lowerCamelCase__     :   float  )    ->     tuple:
							lowerCamelCase_	:							Optional[Any]									=namedtuple("result"    ,						"name value"  )
							if (voltage, current, power).count(0  ) != 1:
														raise ValueError("Only one argument must be 0"  )
							elif power < 0:
														raise ValueError(
														    "Power cannot be negative in any electrical/electronics system"  )
							elif voltage == 0:
														return result("voltage"    ,						power / current  )
							elif current == 0:
														return result("current"    ,						power / voltage  )
							elif power == 0:
														return result("power"    ,						float(round(abs(voltage * current  )    ,						2  )  )  )
							else:
														raise ValueError("Exactly one argument must be 0"  )


if __name__ == "__main__":
						import doctest

						doctest.testmod()



