






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   :				int							,					_lowerCAmelCase   :				Dict							,					_lowerCAmelCase   :				int=13							,					_lowerCAmelCase   :				Optional[int]=7							,					_lowerCAmelCase   :				List[Any]=True							,					_lowerCAmelCase   :				Union[str, Any]=True							,					_lowerCAmelCase   :				int=True							,					_lowerCAmelCase   :				Any=True							,					_lowerCAmelCase   :				Optional[Any]=True							,					_lowerCAmelCase   :				List[str]=False							,					_lowerCAmelCase   :				Union[str, Any]=False							,					_lowerCAmelCase   :				Optional[Any]=False							,					_lowerCAmelCase   :				Any=2							,					_lowerCAmelCase   :				Union[str, Any]=99							,					_lowerCAmelCase   :				int=0							,					_lowerCAmelCase   :				Tuple=32							,					_lowerCAmelCase   :				Union[str, Any]=5							,					_lowerCAmelCase   :				Any=4							,					_lowerCAmelCase   :				Any=0.1							,					_lowerCAmelCase   :				Optional[int]=0.1							,					_lowerCAmelCase   :				List[Any]=512							,					_lowerCAmelCase   :				List[Any]=2							,					_lowerCAmelCase   :				Optional[int]=0.02							,					_lowerCAmelCase   :				List[str]=2							,					_lowerCAmelCase   :				Optional[int]=4							,					_lowerCAmelCase   :				Dict="last"							,					_lowerCAmelCase   :				Tuple=True							,					_lowerCAmelCase   :				int=None							,					_lowerCAmelCase   :				str=0							,					)		->			List[Any]:
		"""simple docstring"""







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







	def 			_a (	self   :				Any      )		->			Any:
		"""simple docstring"""







		__lowercase   							=	ids_tensor([self.batch_size, self.seq_length]							,					self.vocab_size      )
		__lowercase   							=	random_attention_mask([self.batch_size, self.seq_length]      )

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

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

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

		__lowercase   							=	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   :				List[Any]      )		->			str:
		"""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 			_a (	self   :				Dict							,					_lowerCAmelCase   :				List[str]							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				List[Any]							,					_lowerCAmelCase   :				str							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				List[str]							,					_lowerCAmelCase   :				Tuple							,					_lowerCAmelCase   :				Dict							,					)		->			int:
		"""simple docstring"""







		__lowercase   							=	XLMModel(config=_lowerCAmelCase      )
		model.to(_lowerCAmelCase      )
		model.eval()
		__lowercase   							=	model(_lowerCAmelCase							,					lengths=_lowerCAmelCase							,					langs=_lowerCAmelCase      )
		__lowercase   							=	model(_lowerCAmelCase							,					langs=_lowerCAmelCase      )
		__lowercase   							=	model(_lowerCAmelCase      )
		self.parent.assertEqual(result.last_hidden_state.shape							,					(self.batch_size, self.seq_length, self.hidden_size)      )







	def 			_a (	self   :				Tuple							,					_lowerCAmelCase   :				Tuple							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				Any							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				Tuple							,					_lowerCAmelCase   :				Optional[Any]							,					_lowerCAmelCase   :				Dict							,					_lowerCAmelCase   :				List[str]							,					)		->			List[Any]:
		"""simple docstring"""







		__lowercase   							=	XLMWithLMHeadModel(_lowerCAmelCase      )
		model.to(_lowerCAmelCase      )
		model.eval()

		__lowercase   							=	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   :				List[str]							,					_lowerCAmelCase   :				List[str]							,					_lowerCAmelCase   :				List[Any]							,					_lowerCAmelCase   :				Union[str, Any]							,					_lowerCAmelCase   :				List[str]							,					_lowerCAmelCase   :				Tuple							,					_lowerCAmelCase   :				Any							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				Tuple							,					_lowerCAmelCase   :				str							,					)		->			str:
		"""simple docstring"""







		__lowercase   							=	XLMForQuestionAnsweringSimple(_lowerCAmelCase      )
		model.to(_lowerCAmelCase      )
		model.eval()

		__lowercase   							=	model(_lowerCAmelCase      )

		__lowercase   							=	model(_lowerCAmelCase							,					start_positions=_lowerCAmelCase							,					end_positions=_lowerCAmelCase      )
		__lowercase   							=	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   :				Any							,					_lowerCAmelCase   :				str							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				Optional[int]							,					_lowerCAmelCase   :				Any							,					_lowerCAmelCase   :				Any							,					_lowerCAmelCase   :				List[Any]							,					_lowerCAmelCase   :				str							,					_lowerCAmelCase   :				Tuple							,					_lowerCAmelCase   :				int							,					)		->			List[str]:
		"""simple docstring"""







		__lowercase   							=	XLMForQuestionAnswering(_lowerCAmelCase      )
		model.to(_lowerCAmelCase      )
		model.eval()

		__lowercase   							=	model(_lowerCAmelCase      )

		__lowercase   							=	model(
		    _lowerCAmelCase							,					start_positions=_lowerCAmelCase							,					end_positions=_lowerCAmelCase							,					cls_index=_lowerCAmelCase							,					is_impossible=_lowerCAmelCase							,					p_mask=_lowerCAmelCase							,					)

		__lowercase   							=	model(
		    _lowerCAmelCase							,					start_positions=_lowerCAmelCase							,					end_positions=_lowerCAmelCase							,					cls_index=_lowerCAmelCase							,					is_impossible=_lowerCAmelCase							,					)

		((__lowercase)							,    )   							=	result_with_labels.to_tuple()

		__lowercase   							=	model(_lowerCAmelCase							,					start_positions=_lowerCAmelCase							,					end_positions=_lowerCAmelCase      )

		((__lowercase)							,    )   							=	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   :				Optional[int]							,					_lowerCAmelCase   :				str							,					_lowerCAmelCase   :				Tuple							,					_lowerCAmelCase   :				Optional[int]							,					_lowerCAmelCase   :				List[Any]							,					_lowerCAmelCase   :				Optional[int]							,					_lowerCAmelCase   :				Optional[Any]							,					_lowerCAmelCase   :				List[Any]							,					_lowerCAmelCase   :				str							,					_lowerCAmelCase   :				Any							,					)		->			Dict:
		"""simple docstring"""







		__lowercase   							=	XLMForSequenceClassification(_lowerCAmelCase      )
		model.to(_lowerCAmelCase      )
		model.eval()

		__lowercase   							=	model(_lowerCAmelCase      )
		__lowercase   							=	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   :				int							,					_lowerCAmelCase   :				Tuple							,					_lowerCAmelCase   :				Any							,					_lowerCAmelCase   :				Dict							,					_lowerCAmelCase   :				Optional[int]							,					_lowerCAmelCase   :				str							,					_lowerCAmelCase   :				Optional[Any]							,					_lowerCAmelCase   :				Optional[int]							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				Union[str, Any]							,					)		->			Tuple:
		"""simple docstring"""







		__lowercase   							=	self.num_labels
		__lowercase   							=	XLMForTokenClassification(_lowerCAmelCase      )
		model.to(_lowerCAmelCase      )
		model.eval()

		__lowercase   							=	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   :				Tuple							,					_lowerCAmelCase   :				List[str]							,					_lowerCAmelCase   :				List[str]							,					_lowerCAmelCase   :				Tuple							,					_lowerCAmelCase   :				Optional[int]							,					_lowerCAmelCase   :				Tuple							,					_lowerCAmelCase   :				Dict							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				List[str]							,					_lowerCAmelCase   :				List[str]							,					)		->			Optional[int]:
		"""simple docstring"""







		__lowercase   							=	self.num_choices
		__lowercase   							=	XLMForMultipleChoice(config=_lowerCAmelCase      )
		model.to(_lowerCAmelCase      )
		model.eval()
		__lowercase   							=	input_ids.unsqueeze(1      ).expand(-1							,					self.num_choices							,					-1      ).contiguous()
		__lowercase   							=	token_type_ids.unsqueeze(1      ).expand(-1							,					self.num_choices							,					-1      ).contiguous()
		__lowercase   							=	input_mask.unsqueeze(1      ).expand(-1							,					self.num_choices							,					-1      ).contiguous()
		__lowercase   							=	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   :				List[Any]      )		->			Union[str, Any]:
		"""simple docstring"""







		__lowercase   							=	self.prepare_config_and_inputs()
		(
		    (
		    __lowercase
		)							,    (
		    __lowercase
		)							,    (
		    __lowercase
		)							,    (
		    __lowercase
		)							,    (
		    __lowercase
		)							,    (
		    __lowercase
		)							,    (
		    __lowercase
		)							,    (
		    __lowercase
		)							,    (
		    __lowercase
		)							,    
		)   							=	config_and_inputs
		__lowercase   							=	{"""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 ):
	__snake_case						:Dict =							(
	    (
	        XLMModel,
	        XLMWithLMHeadModel,
	        XLMForQuestionAnswering,
	        XLMForSequenceClassification,
	        XLMForQuestionAnsweringSimple,
	        XLMForTokenClassification,
	        XLMForMultipleChoice,
	    )
	    if is_torch_available()
	    else ()
	)
	__snake_case						:Tuple =							(
	    (XLMWithLMHeadModel,) if is_torch_available() else ()
	)  # TODO (PVP): Check other models whether language generation is also applicable
	__snake_case						: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   :				Optional[int]							,					_lowerCAmelCase   :				Optional[int]							,					_lowerCAmelCase   :				List[Any]							,					_lowerCAmelCase   :				Optional[int]							,					_lowerCAmelCase   :				List[Any]							,					_lowerCAmelCase   :				Tuple      )		->			int:
		"""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 			_a (	self   :				Tuple							,					_lowerCAmelCase   :				List[Any]							,					_lowerCAmelCase   :				Optional[int]							,					_lowerCAmelCase   :				Optional[Any]=False      )		->			Union[str, Any]:
		"""simple docstring"""







		__lowercase   							=	super()._prepare_for_class(_lowerCAmelCase							,					_lowerCAmelCase							,					return_labels=_lowerCAmelCase      )

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

		return inputs_dict







	def 			_a (	self   :				Dict      )		->			Tuple:
		"""simple docstring"""







		__lowercase   							=	XLMModelTester(self      )
		__lowercase   							=	ConfigTester(self							,					config_class=_lowerCAmelCase							,					emb_dim=37      )







	def 			_a (	self   :				List[Any]      )		->			Any:
		"""simple docstring"""







		self.config_tester.run_common_tests()







	def 			_a (	self   :				List[Any]      )		->			Optional[int]:
		"""simple docstring"""







		__lowercase   							=	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_xlm_model(*_lowerCAmelCase      )







	def 			_a (	self   :				Union[str, Any]      )		->			List[Any]:
		"""simple docstring"""







		__lowercase   							=	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_xlm_lm_head(*_lowerCAmelCase      )







	def 			_a (	self   :				Union[str, Any]      )		->			Optional[Any]:
		"""simple docstring"""







		__lowercase   							=	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_xlm_simple_qa(*_lowerCAmelCase      )







	def 			_a (	self   :				List[Any]      )		->			Dict:
		"""simple docstring"""







		__lowercase   							=	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_xlm_qa(*_lowerCAmelCase      )







	def 			_a (	self   :				List[Any]      )		->			List[str]:
		"""simple docstring"""







		__lowercase   							=	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_xlm_sequence_classif(*_lowerCAmelCase      )







	def 			_a (	self   :				Union[str, Any]      )		->			Dict:
		"""simple docstring"""







		__lowercase   							=	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_xlm_token_classif(*_lowerCAmelCase      )







	def 			_a (	self   :				Any      )		->			Union[str, Any]:
		"""simple docstring"""







		__lowercase   							=	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_xlm_for_multiple_choice(*_lowerCAmelCase      )







	def 			_a (	self   :				List[Any]							,					_lowerCAmelCase   :				List[str]							,					_lowerCAmelCase   :				Tuple							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				Tuple							,					_lowerCAmelCase   :				List[str]							,					_lowerCAmelCase   :				Any=False							,					_lowerCAmelCase   :				List[str]=1      )		->			Tuple:
		"""simple docstring"""







		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
			__lowercase   							=	min_length + idx + 1
			__lowercase   							=	min_length + idx + 1

			__lowercase   							=	(
			    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   :				Dict							,					_lowerCAmelCase   :				List[Any]							,					_lowerCAmelCase   :				List[Any]							,					_lowerCAmelCase   :				Optional[int]							,					_lowerCAmelCase   :				Optional[int]							,					_lowerCAmelCase   :				List[str]							,					_lowerCAmelCase   :				Optional[int]=False							,					_lowerCAmelCase   :				Dict=1      )		->			int:
		"""simple docstring"""







		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
			__lowercase   							=	min_length + idx + 1
			__lowercase   							=	(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   :				Tuple      )		->			Optional[Any]:
		"""simple docstring"""







		for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			__lowercase   							=	XLMModel.from_pretrained(_lowerCAmelCase      )
			self.assertIsNotNone(_lowerCAmelCase      )


@require_torch
class 							__UpperCamelCase     (  unittest.TestCase ):







	@slow
	def 			_a (	self   :				List[str]      )		->			Tuple:
		"""simple docstring"""







		__lowercase   							=	XLMWithLMHeadModel.from_pretrained("""xlm-mlm-en-2048"""      )
		model.to(_lowerCAmelCase      )
		__lowercase   							=	torch.tensor([[14, 447]]							,					dtype=torch.long							,					device=_lowerCAmelCase      )  # the president
		__lowercase   							=	[
		    14,
		    447,
		    14,
		    447,
		    14,
		    447,
		    14,
		    447,
		    14,
		    447,
		    14,
		    447,
		    14,
		    447,
		    14,
		    447,
		    14,
		    447,
		    14,
		    447,
		]  # the president the president the president the president the president the president the president the president the president the president
		# TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
		__lowercase   							=	model.generate(_lowerCAmelCase							,					do_sample=_lowerCAmelCase      )
		self.assertListEqual(output_ids[0].cpu().numpy().tolist()							,					_lowerCAmelCase      )







'''simple docstring'''






import inspect
import unittest

from transformers import ConvNextConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import ConvNextBackbone, ConvNextForImageClassification, ConvNextModel
      from transformers.models.convnext.modeling_convnext import CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from PIL import Image

      from transformers import AutoImageProcessor






class        lowercase_      :





     def __init__(     self     :					Tuple     ,							__lowerCamelCase     :					Optional[Any]     ,							__lowerCamelCase     :					Optional[Any]=13     ,							__lowerCamelCase     :					List[Any]=32     ,							__lowerCamelCase     :					List[str]=3     ,							__lowerCamelCase     :					Any=4     ,							__lowerCamelCase     :					str=[10, 20, 30, 40]     ,							__lowerCamelCase     :					Any=[2, 2, 3, 2]     ,							__lowerCamelCase     :					List[Any]=True     ,							__lowerCamelCase     :					List[Any]=True     ,							__lowerCamelCase     :					List[Any]=37     ,							__lowerCamelCase     :					int="gelu"     ,							__lowerCamelCase     :					List[Any]=10     ,							__lowerCamelCase     :					Tuple=0.0_2     ,							__lowerCamelCase     :					str=["stage2", "stage3", "stage4"]     ,							__lowerCamelCase     :					Optional[Any]=[2, 3, 4]     ,							__lowerCamelCase     :					Dict=None     ,							):
           snake_case__       :   List[str]	      =    parent
           snake_case__       :   Optional[Any]	      =    batch_size
           snake_case__       :   int	      =    image_size
           snake_case__       :   Tuple	      =    num_channels
           snake_case__       :   Any	      =    num_stages
           snake_case__       :   Any	      =    hidden_sizes
           snake_case__       :   Optional[Any]	      =    depths
           snake_case__       :   Optional[Any]	      =    is_training
           snake_case__       :   Dict	      =    use_labels
           snake_case__       :   Any	      =    intermediate_size
           snake_case__       :   int	      =    hidden_act
           snake_case__       :   Any	      =    num_labels
           snake_case__       :   Optional[int]	      =    initializer_range
           snake_case__       :   Union[str, Any]	      =    out_features
           snake_case__       :   Optional[Any]	      =    out_indices
           snake_case__       :   Optional[int]	      =    scope





     def 			_lowerCAmelCase      (     self     :					List[Any]			):
           snake_case__       :   Union[str, Any]	      =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

           snake_case__       :   Optional[int]	      =    None
           if self.use_labels:
                 snake_case__       :   Tuple	      =    ids_tensor([self.batch_size]     ,							self.num_labels			)

           snake_case__       :   Any	      =    self.get_config()
           return config, pixel_values, labels





     def 			_lowerCAmelCase      (     self     :					Tuple			):
           return ConvNextConfig(
               num_channels=self.num_channels     ,							hidden_sizes=self.hidden_sizes     ,							depths=self.depths     ,							num_stages=self.num_stages     ,							hidden_act=self.hidden_act     ,							is_decoder=__lowerCamelCase     ,							initializer_range=self.initializer_range     ,							out_features=self.out_features     ,							out_indices=self.out_indices     ,							num_labels=self.num_labels     ,							)





     def 			_lowerCAmelCase      (     self     :					List[Any]     ,							__lowerCamelCase     :					List[str]     ,							__lowerCamelCase     :					Tuple     ,							__lowerCamelCase     :					Tuple			):
           snake_case__       :   Optional[int]	      =    ConvNextModel(config=__lowerCamelCase			)
           model.to(__lowerCamelCase			)
           model.eval()
           snake_case__       :   Union[str, Any]	      =    model(__lowerCamelCase			)
           # expected last hidden states: B, C, H // 32, W // 32
           self.parent.assertEqual(
               result.last_hidden_state.shape     ,							(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)     ,							)





     def 			_lowerCAmelCase      (     self     :					Any     ,							__lowerCamelCase     :					str     ,							__lowerCamelCase     :					str     ,							__lowerCamelCase     :					List[Any]			):
           snake_case__       :   Optional[Any]	      =    ConvNextForImageClassification(__lowerCamelCase			)
           model.to(__lowerCamelCase			)
           model.eval()
           snake_case__       :   Optional[Any]	      =    model(__lowerCamelCase     ,							labels=__lowerCamelCase			)
           self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.num_labels)			)





     def 			_lowerCAmelCase      (     self     :					List[str]     ,							__lowerCamelCase     :					int     ,							__lowerCamelCase     :					Dict     ,							__lowerCamelCase     :					int			):
           snake_case__       :   Dict	      =    ConvNextBackbone(config=__lowerCamelCase			)
           model.to(__lowerCamelCase			)
           model.eval()
           snake_case__       :   str	      =    model(__lowerCamelCase			)

           # verify hidden states
           self.parent.assertEqual(len(result.feature_maps			)     ,							len(config.out_features			)			)
           self.parent.assertListEqual(list(result.feature_maps[0].shape			)     ,							[self.batch_size, self.hidden_sizes[1], 4, 4]			)

           # verify channels
           self.parent.assertEqual(len(model.channels			)     ,							len(config.out_features			)			)
           self.parent.assertListEqual(model.channels     ,							config.hidden_sizes[1:]			)

           # verify backbone works with out_features=None
           snake_case__       :   List[str]	      =    None
           snake_case__       :   Union[str, Any]	      =    ConvNextBackbone(config=__lowerCamelCase			)
           model.to(__lowerCamelCase			)
           model.eval()
           snake_case__       :   int	      =    model(__lowerCamelCase			)

           # verify feature maps
           self.parent.assertEqual(len(result.feature_maps			)     ,							1			)
           self.parent.assertListEqual(list(result.feature_maps[0].shape			)     ,							[self.batch_size, self.hidden_sizes[-1], 1, 1]			)

           # verify channels
           self.parent.assertEqual(len(model.channels			)     ,							1			)
           self.parent.assertListEqual(model.channels     ,							[config.hidden_sizes[-1]]			)





     def 			_lowerCAmelCase      (     self     :					Tuple			):
           snake_case__       :   Dict	      =    self.prepare_config_and_inputs()
           snake_case__      ,							snake_case__      ,							snake_case__       :   Optional[Any]	      =    config_and_inputs
           snake_case__       :   Optional[int]	      =    {'pixel_values': pixel_values}
           return config, inputs_dict






@require_torch
class        lowercase_      (						lowerCAmelCase_						,       lowerCAmelCase_						,       unittest.TestCase							):
     A_											=			(
         (
             ConvNextModel,
             ConvNextForImageClassification,
             ConvNextBackbone,
         )
         if is_torch_available()
         else ()
     )
     A_											=			(
         {"feature-extraction": ConvNextModel, "image-classification": ConvNextForImageClassification}
         if is_torch_available()
         else {}
     )

     A_											=			True
     A_											=			False
     A_											=			False
     A_											=			False
     A_											=			False





     def 			_lowerCAmelCase      (     self     :					Any			):
           snake_case__       :   Tuple	      =    ConvNextModelTester(self			)
           snake_case__       :   Optional[int]	      =    ConfigTester(self     ,							config_class=__lowerCamelCase     ,							has_text_modality=__lowerCamelCase     ,							hidden_size=37			)





     def 			_lowerCAmelCase      (     self     :					Optional[Any]			):
           self.create_and_test_config_common_properties()
           self.config_tester.create_and_test_config_to_json_string()
           self.config_tester.create_and_test_config_to_json_file()
           self.config_tester.create_and_test_config_from_and_save_pretrained()
           self.config_tester.create_and_test_config_with_num_labels()
           self.config_tester.check_config_can_be_init_without_params()
           self.config_tester.check_config_arguments_init()





     def 			_lowerCAmelCase      (     self     :					Dict			):
           return





     @unittest.skip(reason='ConvNext does not use inputs_embeds'			)
     def 			_lowerCAmelCase      (     self     :					List[Any]			):
           pass





     @unittest.skip(reason='ConvNext does not support input and output embeddings'			)
     def 			_lowerCAmelCase      (     self     :					Tuple			):
           pass





     @unittest.skip(reason='ConvNext does not use feedforward chunking'			)
     def 			_lowerCAmelCase      (     self     :					List[Any]			):
           pass





     def 			_lowerCAmelCase      (     self     :					str			):
           snake_case__      ,							snake_case__       :   Any	      =    self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                 snake_case__       :   Dict	      =    model_class(__lowerCamelCase			)
                 snake_case__       :   Optional[Any]	      =    inspect.signature(model.forward			)
                 # signature.parameters is an OrderedDict => so arg_names order is deterministic
                 snake_case__       :   Optional[Any]	      =    [*signature.parameters.keys()]

                 snake_case__       :   str	      =    ['pixel_values']
                 self.assertListEqual(arg_names[:1]     ,							__lowerCamelCase			)





     def 			_lowerCAmelCase      (     self     :					int			):
           snake_case__       :   List[Any]	      =    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__lowerCamelCase			)





     def 			_lowerCAmelCase      (     self     :					int			):
           snake_case__       :   List[Any]	      =    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_backbone(*__lowerCamelCase			)





     def 			_lowerCAmelCase      (     self     :					str			):
           def check_hidden_states_output(__lowerCamelCase     :					Tuple     ,							__lowerCamelCase     :					Any     ,							__lowerCamelCase     :					Tuple			):
                 snake_case__       :   int	      =    model_class(__lowerCamelCase			)
                 model.to(__lowerCamelCase			)
                 model.eval()

                 with torch.no_grad():
                       snake_case__       :   int	      =    model(**self._prepare_for_class(__lowerCamelCase     ,							__lowerCamelCase			)			)

                 snake_case__       :   Optional[Any]	      =    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                 snake_case__       :   Optional[Any]	      =    self.model_tester.num_stages
                 self.assertEqual(len(__lowerCamelCase			)     ,							expected_num_stages + 1			)

                 # ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
                 self.assertListEqual(
                     list(hidden_states[0].shape[-2:]			)     ,							[self.model_tester.image_size // 4, self.model_tester.image_size // 4]     ,							)

           snake_case__      ,							snake_case__       :   List[Any]	      =    self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                 snake_case__       :   Dict	      =    True
                 check_hidden_states_output(__lowerCamelCase     ,							__lowerCamelCase     ,							__lowerCamelCase			)

                 # check that output_hidden_states also work using config
                 del inputs_dict["output_hidden_states"]
                 snake_case__       :   Tuple	      =    True

                 check_hidden_states_output(__lowerCamelCase     ,							__lowerCamelCase     ,							__lowerCamelCase			)





     def 			_lowerCAmelCase      (     self     :					List[Any]			):
           snake_case__       :   Any	      =    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase			)





     @slow
     def 			_lowerCAmelCase      (     self     :					str			):
           for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 snake_case__       :   Optional[int]	      =    ConvNextModel.from_pretrained(__lowerCamelCase			)
                 self.assertIsNotNone(__lowerCamelCase			)






def 		UpperCamelCase__      (		)    ->   Optional[Any]:
      snake_case__       :   Any	      =    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'		)
      return image






@require_torch
@require_vision
class        lowercase_      (						unittest.TestCase							):





     @cached_property
     def 			_lowerCAmelCase      (     self     :					List[Any]			):
           return AutoImageProcessor.from_pretrained('facebook/convnext-tiny-224'			) if is_vision_available() else None





     @slow
     def 			_lowerCAmelCase      (     self     :					Optional[int]			):
           snake_case__       :   str	      =    ConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224'			).to(__lowerCamelCase			)

           snake_case__       :   str	      =    self.default_image_processor
           snake_case__       :   List[str]	      =    prepare_img()
           snake_case__       :   List[Any]	      =    image_processor(images=__lowerCamelCase     ,							return_tensors='pt'			).to(__lowerCamelCase			)

           # forward pass
           with torch.no_grad():
                 snake_case__       :   int	      =    model(**__lowerCamelCase			)

           # verify the logits
           snake_case__       :   Tuple	      =    torch.Size((1, 1000)			)
           self.assertEqual(outputs.logits.shape     ,							__lowerCamelCase			)

           snake_case__       :   int	      =    torch.tensor([-0.0_2_6_0, -0.4_7_3_9, 0.1_9_1_1]			).to(__lowerCamelCase			)

           self.assertTrue(torch.allclose(outputs.logits[0, :3]     ,							__lowerCamelCase     ,							atol=1E-4			)			)






@require_torch
class        lowercase_      (						unittest.TestCase						,       lowerCAmelCase_							):
     A_											=			(ConvNextBackbone,) if is_torch_available() else ()
     A_											=			ConvNextConfig

     A_											=			False





     def 			_lowerCAmelCase      (     self     :					List[Any]			):
           snake_case__       :   List[Any]	      =    ConvNextModelTester(self			)
