

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    :


		"""simple docstring"""




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


		def 		A__					(				self):
			_UpperCamelCase   :		Any			=						ids_tensor([self.batch_size, self.seq_length]	,       self.vocab_size)
			_UpperCamelCase   :		Dict			=						random_attention_mask([self.batch_size, self.seq_length])

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

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

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

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


		def 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       ):
			_UpperCamelCase   :		Dict			=						XLMWithLMHeadModel(__snake_case)
			model.to(__snake_case)
			model.eval()

			_UpperCamelCase   :		List[Any]			=						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 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       ):
			_UpperCamelCase   :		Any			=						XLMForQuestionAnsweringSimple(__snake_case)
			model.to(__snake_case)
			model.eval()

			_UpperCamelCase   :		List[Any]			=						model(__snake_case)

			_UpperCamelCase   :		Any			=						model(__snake_case	,       start_positions=__snake_case	,       end_positions=__snake_case)
			_UpperCamelCase   :		List[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 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       ):
			_UpperCamelCase   :		List[str]			=						XLMForQuestionAnswering(__snake_case)
			model.to(__snake_case)
			model.eval()

			_UpperCamelCase   :		str			=						model(__snake_case)

			_UpperCamelCase   :		Union[str, Any]			=						model(
			    __snake_case	,       start_positions=__snake_case	,       end_positions=__snake_case	,       cls_index=__snake_case	,       is_impossible=__snake_case	,       p_mask=__snake_case	,       )

			_UpperCamelCase   :		Any			=						model(
			    __snake_case	,       start_positions=__snake_case	,       end_positions=__snake_case	,       cls_index=__snake_case	,       is_impossible=__snake_case	,       )

			(_UpperCamelCase						)   :		str			=						result_with_labels.to_tuple()

			_UpperCamelCase   :		Any			=						model(__snake_case	,       start_positions=__snake_case	,       end_positions=__snake_case)

			(_UpperCamelCase						)   :		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 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       ):
			_UpperCamelCase   :		List[str]			=						XLMForSequenceClassification(__snake_case)
			model.to(__snake_case)
			model.eval()

			_UpperCamelCase   :		str			=						model(__snake_case)
			_UpperCamelCase   :		int			=						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 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       ):
			_UpperCamelCase   :		Optional[int]			=						self.num_labels
			_UpperCamelCase   :		List[str]			=						XLMForTokenClassification(__snake_case)
			model.to(__snake_case)
			model.eval()

			_UpperCamelCase   :		int			=						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 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       ):
			_UpperCamelCase   :		str			=						self.num_choices
			_UpperCamelCase   :		Union[str, Any]			=						XLMForMultipleChoice(config=__snake_case)
			model.to(__snake_case)
			model.eval()
			_UpperCamelCase   :		List[Any]			=						input_ids.unsqueeze(1).expand(-1	,       self.num_choices	,       -1).contiguous()
			_UpperCamelCase   :		List[str]			=						token_type_ids.unsqueeze(1).expand(-1	,       self.num_choices	,       -1).contiguous()
			_UpperCamelCase   :		List[str]			=						input_mask.unsqueeze(1).expand(-1	,       self.num_choices	,       -1).contiguous()
			_UpperCamelCase   :		Optional[int]			=						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 		A__					(				self):
			_UpperCamelCase   :		List[str]			=						self.prepare_config_and_inputs()
			(
			    _UpperCamelCase						
			)   :		int			=						config_and_inputs
			_UpperCamelCase   :		Any			=						{"input_ids": input_ids, "token_type_ids": token_type_ids, "lengths": input_lengths}
			return config, inputs_dict







@require_torch
class 	lowercase    (				_A    ,							_A    ,							_A    ,							unittest.TestCase							):


		"""simple docstring"""




		a__					       =  (
		    (
		        XLMModel,
		        XLMWithLMHeadModel,
		        XLMForQuestionAnswering,
		        XLMForSequenceClassification,
		        XLMForQuestionAnsweringSimple,
		        XLMForTokenClassification,
		        XLMForMultipleChoice,
		    )
		    if is_torch_available()
		    else ()
		)
		a__					       =  (
		    (XLMWithLMHeadModel,) if is_torch_available() else ()
		)  # TODO (PVP): Check other models whether language generation is also applicable
		a__					       =  (
		    {
		        "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	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case):
			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	,       __snake_case	,       __snake_case	,       __snake_case=False):
			_UpperCamelCase   :		Tuple			=						super()._prepare_for_class(__snake_case	,       __snake_case	,       return_labels=__snake_case)

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

			return inputs_dict


		def 		A__					(				self):
			_UpperCamelCase   :		Any			=						XLMModelTester(self)
			_UpperCamelCase   :		Union[str, Any]			=						ConfigTester(self	,       config_class=__snake_case	,       emb_dim=37)


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


		def 		A__					(				self):
			_UpperCamelCase   :		Tuple			=						self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_xlm_model(*__snake_case)


		def 		A__					(				self):
			_UpperCamelCase   :		Optional[Any]			=						self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_xlm_lm_head(*__snake_case)


		def 		A__					(				self):
			_UpperCamelCase   :		Any			=						self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_xlm_simple_qa(*__snake_case)


		def 		A__					(				self):
			_UpperCamelCase   :		List[Any]			=						self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_xlm_qa(*__snake_case)


		def 		A__					(				self):
			_UpperCamelCase   :		List[str]			=						self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_xlm_sequence_classif(*__snake_case)


		def 		A__					(				self):
			_UpperCamelCase   :		Tuple			=						self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_xlm_token_classif(*__snake_case)


		def 		A__					(				self):
			_UpperCamelCase   :		str			=						self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_xlm_for_multiple_choice(*__snake_case)


		def 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case=False	,       __snake_case=1):
			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
				_UpperCamelCase   :		List[str]			=						min_length + idx + 1
				_UpperCamelCase   :		Dict			=						min_length + idx + 1

				_UpperCamelCase   :		List[str]			=						(
				    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 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case=False	,       __snake_case=1):
			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
				_UpperCamelCase   :		Dict			=						min_length + idx + 1
				_UpperCamelCase   :		str			=						(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 		A__					(				self):
			for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				_UpperCamelCase   :		str			=						XLMModel.from_pretrained(__snake_case)
				self.assertIsNotNone(__snake_case)







@require_torch
class 	lowercase    (				unittest.TestCase							):


		"""simple docstring"""




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







import functools






def 					lowerCamelCase_     (							UpperCAmelCase_							:     list[int]							,       UpperCAmelCase_							:     list[int]	)						->				int:




	'''simple docstring'''




	if not isinstance(UpperCAmelCase_							,       UpperCAmelCase_	) or not all(isinstance(UpperCAmelCase_							,       UpperCAmelCase_	) for day in days	):
		raise ValueError('The parameter days should be a list of integers'	)

	if len(UpperCAmelCase_	) != 3 or not all(isinstance(UpperCAmelCase_							,       UpperCAmelCase_	) for cost in costs	):
		raise ValueError('The parameter costs should be a list of three integers'	)

	if len(UpperCAmelCase_	) == 0:
		return 0

	if min(UpperCAmelCase_	) <= 0:
		raise ValueError('All days elements should be greater than 0'	)

	if max(UpperCAmelCase_	) >= 3_6_6:
		raise ValueError('All days elements should be less than 366'	)

	_UpperCamelCase   :		Union[str, Any]			=						set(UpperCAmelCase_	)

	@functools.cache
	def dynamic_programming(UpperCAmelCase_							:     int	) -> int:
		if index > 3_6_5:
			return 0

		if index not in days_set:
			return dynamic_programming(index + 1	)

		return min(
		    costs[0] + dynamic_programming(index + 1	)							,       costs[1] + dynamic_programming(index + 7	)							,       costs[2] + dynamic_programming(index + 3_0	)							,       )

	return dynamic_programming(1	)


if __name__ == "__main__":
							import doctest

							doctest.testmod()




