
"""simple docstring"""







import inspect
import unittest
from math import floor

from transformers import CvtConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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 CvtForImageClassification, CvtModel
							from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor




class 			A__	(     __snake_case):

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Any						      =				self.config_class(**self.inputs_dict			)
			self.parent.assertTrue(hasattr(_SCREAMING_SNAKE_CASE	,		'embed_dim'			)			)
			self.parent.assertTrue(hasattr(_SCREAMING_SNAKE_CASE	,		'num_heads'			)			)




class 			A__	:

		def __init__(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE=13	,		_SCREAMING_SNAKE_CASE=64	,		_SCREAMING_SNAKE_CASE=3	,		_SCREAMING_SNAKE_CASE=[16, 48, 96]	,		_SCREAMING_SNAKE_CASE=[1, 3, 6]	,		_SCREAMING_SNAKE_CASE=[1, 2, 10]	,		_SCREAMING_SNAKE_CASE=[7, 3, 3]	,		_SCREAMING_SNAKE_CASE=[4, 2, 2]	,		_SCREAMING_SNAKE_CASE=[2, 1, 1]	,		_SCREAMING_SNAKE_CASE=[2, 2, 2]	,		_SCREAMING_SNAKE_CASE=[False, False, True]	,		_SCREAMING_SNAKE_CASE=[0.0, 0.0, 0.0]	,		_SCREAMING_SNAKE_CASE=0.02	,		_SCREAMING_SNAKE_CASE=1E-12	,		_SCREAMING_SNAKE_CASE=True	,		_SCREAMING_SNAKE_CASE=True	,		_SCREAMING_SNAKE_CASE=2	,		):
			__lowerCAmelCase      :						Optional[int]						      =				parent
			__lowerCAmelCase      :						Any						      =				batch_size
			__lowerCAmelCase      :						Any						      =				image_size
			__lowerCAmelCase      :						List[str]						      =				patch_sizes
			__lowerCAmelCase      :						Union[str, Any]						      =				patch_stride
			__lowerCAmelCase      :						Tuple						      =				patch_padding
			__lowerCAmelCase      :						List[Any]						      =				is_training
			__lowerCAmelCase      :						Tuple						      =				use_labels
			__lowerCAmelCase      :						int						      =				num_labels
			__lowerCAmelCase      :						Union[str, Any]						      =				num_channels
			__lowerCAmelCase      :						Tuple						      =				embed_dim
			__lowerCAmelCase      :						Dict						      =				num_heads
			__lowerCAmelCase      :						Optional[Any]						      =				stride_kv
			__lowerCAmelCase      :						Optional[int]						      =				depth
			__lowerCAmelCase      :						str						      =				cls_token
			__lowerCAmelCase      :						int						      =				attention_drop_rate
			__lowerCAmelCase      :						Any						      =				initializer_range
			__lowerCAmelCase      :						Any						      =				layer_norm_eps

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Tuple						      =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

			__lowerCAmelCase      :						Optional[Any]						      =				self.get_config()
			return config, pixel_values, labels

		def 							__lowerCamelCase							(				self			):
			return CvtConfig(
			    image_size=self.image_size	,		num_labels=self.num_labels	,		num_channels=self.num_channels	,		embed_dim=self.embed_dim	,		num_heads=self.num_heads	,		patch_sizes=self.patch_sizes	,		patch_padding=self.patch_padding	,		patch_stride=self.patch_stride	,		stride_kv=self.stride_kv	,		depth=self.depth	,		cls_token=self.cls_token	,		attention_drop_rate=self.attention_drop_rate	,		initializer_range=self.initializer_range	,		)

		def 							__lowerCamelCase							(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			):
			__lowerCAmelCase      :						Optional[int]						      =				CvtModel(config=_SCREAMING_SNAKE_CASE			)
			model.to(_SCREAMING_SNAKE_CASE			)
			model.eval()
			__lowerCAmelCase      :						List[Any]						      =				model(_SCREAMING_SNAKE_CASE			)
			__lowerCAmelCase      :						int						      =				(self.image_size, self.image_size)
			__lowerCAmelCase      ,       __lowerCAmelCase      :						Union[str, Any]						      =				image_size[0], image_size[1]
			for i in range(len(self.depth			)			):
				__lowerCAmelCase      :						Dict						      =				floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1			)
				__lowerCAmelCase      :						Optional[int]						      =				floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1			)
			self.parent.assertEqual(result.last_hidden_state.shape	,		(self.batch_size, self.embed_dim[-1], height, width)			)

		def 							__lowerCamelCase							(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			):
			__lowerCAmelCase      :						Optional[Any]						      =				self.num_labels
			__lowerCAmelCase      :						Any						      =				CvtForImageClassification(_SCREAMING_SNAKE_CASE			)
			model.to(_SCREAMING_SNAKE_CASE			)
			model.eval()
			__lowerCAmelCase      :						Optional[int]						      =				model(_SCREAMING_SNAKE_CASE	,		labels=_SCREAMING_SNAKE_CASE			)
			self.parent.assertEqual(result.logits.shape	,		(self.batch_size, self.num_labels)			)







		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Dict						      =				self.prepare_config_and_inputs()
			__lowerCAmelCase      ,       __lowerCAmelCase      ,       __lowerCAmelCase      :						Tuple						      =				config_and_inputs
			__lowerCAmelCase      :						Any						      =				{'pixel_values': pixel_values}
			return config, inputs_dict




@require_torch
class 			A__	(     __snake_case	,					__snake_case	,					unittest.TestCase):
		A_						:	List[str]           =     (CvtModel, CvtForImageClassification) if is_torch_available() else ()
		A_						:	List[Any]           =     (
		    {'feature-extraction': CvtModel, 'image-classification': CvtForImageClassification}
		    if is_torch_available()
		    else {}
		)

		A_						:	Union[str, Any]           =     False
		A_						:	Optional[Any]           =     False
		A_						:	str           =     False
		A_						:	Optional[Any]           =     False
		A_						:	Any           =     False

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						List[Any]						      =				CvtModelTester(self			)
			__lowerCAmelCase      :						Optional[Any]						      =				ConfigTester(self	,		config_class=_SCREAMING_SNAKE_CASE	,		has_text_modality=_SCREAMING_SNAKE_CASE	,		hidden_size=37			)

		def 							__lowerCamelCase							(				self			):
			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			):
			return

		@unittest.skip(reason='Cvt does not output attentions'			)
		def 							__lowerCamelCase							(				self			):
			pass

		@unittest.skip(reason='Cvt does not use inputs_embeds'			)
		def 							__lowerCamelCase							(				self			):
			pass

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

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      ,       __lowerCAmelCase      :						Any						      =				self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
				__lowerCAmelCase      :						List[str]						      =				model_class(_SCREAMING_SNAKE_CASE			)
				__lowerCAmelCase      :						Tuple						      =				inspect.signature(model.forward			)
				# signature.parameters is an OrderedDict => so arg_names order is deterministic
				__lowerCAmelCase      :						Optional[int]						      =				[*signature.parameters.keys()]

				__lowerCAmelCase      :						Dict						      =				['pixel_values']
				self.assertListEqual(arg_names[:1]	,		_SCREAMING_SNAKE_CASE			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Dict						      =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE			)

		def 							__lowerCamelCase							(				self			):
			def check_hidden_states_output(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			):
				__lowerCAmelCase      :						Dict						      =				model_class(_SCREAMING_SNAKE_CASE			)
				model.to(_SCREAMING_SNAKE_CASE			)
				model.eval()

				with torch.no_grad():
					__lowerCAmelCase      :						Optional[int]						      =				model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)			)

				__lowerCAmelCase      :						str						      =				outputs.hidden_states

				__lowerCAmelCase      :						int						      =				len(self.model_tester.depth			)
				self.assertEqual(len(_SCREAMING_SNAKE_CASE			)	,		_SCREAMING_SNAKE_CASE			)

				# verify the first hidden states (first block)
				self.assertListEqual(
				    list(hidden_states[0].shape[-3:]			)	,		[
				        self.model_tester.embed_dim[0],
				        self.model_tester.image_size // 4,
				        self.model_tester.image_size // 4,
				    ]	,		)

			__lowerCAmelCase      ,       __lowerCAmelCase      :						List[str]						      =				self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
				__lowerCAmelCase      :						Dict						      =				True
				check_hidden_states_output(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)

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

				check_hidden_states_output(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Dict						      =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE			)

		@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'			)
		def 							__lowerCamelCase							(				self			):
			pass







		@slow
		def 							__lowerCamelCase							(				self			):
			for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__lowerCAmelCase      :						Optional[int]						      =				CvtModel.from_pretrained(_SCREAMING_SNAKE_CASE			)
				self.assertIsNotNone(_SCREAMING_SNAKE_CASE			)



def    __lowerCAmelCase    ():
	__lowerCAmelCase      :						int						      =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'		)
	return image




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

		@cached_property
		def 							__lowerCamelCase							(				self			):
			return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]			)







		@slow
		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Optional[Any]						      =				CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]			).to(_SCREAMING_SNAKE_CASE			)

			__lowerCAmelCase      :						str						      =				self.default_image_processor
			__lowerCAmelCase      :						Union[str, Any]						      =				prepare_img()
			__lowerCAmelCase      :						Any						      =				image_processor(images=_SCREAMING_SNAKE_CASE	,		return_tensors='pt'			).to(_SCREAMING_SNAKE_CASE			)

			# forward pass
			with torch.no_grad():
				__lowerCAmelCase      :						Optional[int]						      =				model(**_SCREAMING_SNAKE_CASE			)

			# verify the logits
			__lowerCAmelCase      :						Dict						      =				torch.Size((1, 10_00)			)
			self.assertEqual(outputs.logits.shape	,		_SCREAMING_SNAKE_CASE			)

			__lowerCAmelCase      :						int						      =				torch.tensor([0.9285, 0.9015, -0.3150]			).to(_SCREAMING_SNAKE_CASE			)

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

"""simple docstring"""







from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCamelCase__        =		logging.get_logger(__name__)

lowerCamelCase__        =		{
    """facebook/nllb-moe-54B""": """https://huggingface.co/facebook/nllb-moe-54b/resolve/main/config.json""",
}




class 			A__	(     _lowerCamelCase):
		A_						:	str           =     'nllb-moe'
		A_						:	Optional[Any]           =     ['past_key_values']
		A_						:	List[str]           =     {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}

		def __init__(				self	,		_SCREAMING_SNAKE_CASE=12_81_12	,		_SCREAMING_SNAKE_CASE=10_24	,		_SCREAMING_SNAKE_CASE=12	,		_SCREAMING_SNAKE_CASE=40_96	,		_SCREAMING_SNAKE_CASE=16	,		_SCREAMING_SNAKE_CASE=12	,		_SCREAMING_SNAKE_CASE=40_96	,		_SCREAMING_SNAKE_CASE=16	,		_SCREAMING_SNAKE_CASE=0.05	,		_SCREAMING_SNAKE_CASE=0.05	,		_SCREAMING_SNAKE_CASE=True	,		_SCREAMING_SNAKE_CASE=True	,		_SCREAMING_SNAKE_CASE="relu"	,		_SCREAMING_SNAKE_CASE=10_24	,		_SCREAMING_SNAKE_CASE=0.1	,		_SCREAMING_SNAKE_CASE=0.1	,		_SCREAMING_SNAKE_CASE=0.0	,		_SCREAMING_SNAKE_CASE=0.02	,		_SCREAMING_SNAKE_CASE=2	,		_SCREAMING_SNAKE_CASE=True	,		_SCREAMING_SNAKE_CASE=False	,		_SCREAMING_SNAKE_CASE="float32"	,		_SCREAMING_SNAKE_CASE=False	,		_SCREAMING_SNAKE_CASE=1_28	,		_SCREAMING_SNAKE_CASE=64	,		_SCREAMING_SNAKE_CASE=4	,		_SCREAMING_SNAKE_CASE=4	,		_SCREAMING_SNAKE_CASE=0.001	,		_SCREAMING_SNAKE_CASE=0.001	,		_SCREAMING_SNAKE_CASE="all"	,		_SCREAMING_SNAKE_CASE=False	,		_SCREAMING_SNAKE_CASE=False	,		_SCREAMING_SNAKE_CASE=1.0	,		_SCREAMING_SNAKE_CASE=0.2	,		_SCREAMING_SNAKE_CASE=1	,		_SCREAMING_SNAKE_CASE=0	,		_SCREAMING_SNAKE_CASE=2	,		_SCREAMING_SNAKE_CASE=False	,		**_SCREAMING_SNAKE_CASE	,		):
			__lowerCAmelCase      :						int						      =				vocab_size
			__lowerCAmelCase      :						str						      =				max_position_embeddings
			__lowerCAmelCase      :						Dict						      =				d_model
			__lowerCAmelCase      :						Tuple						      =				encoder_ffn_dim
			__lowerCAmelCase      :						Optional[Any]						      =				encoder_layers
			__lowerCAmelCase      :						Any						      =				encoder_attention_heads
			__lowerCAmelCase      :						Tuple						      =				decoder_ffn_dim
			__lowerCAmelCase      :						Dict						      =				decoder_layers
			__lowerCAmelCase      :						str						      =				decoder_attention_heads
			__lowerCAmelCase      :						str						      =				dropout
			__lowerCAmelCase      :						List[str]						      =				attention_dropout
			__lowerCAmelCase      :						Optional[int]						      =				activation_dropout
			__lowerCAmelCase      :						List[Any]						      =				activation_function
			__lowerCAmelCase      :						List[str]						      =				init_std
			__lowerCAmelCase      :						Union[str, Any]						      =				encoder_layerdrop
			__lowerCAmelCase      :						List[Any]						      =				decoder_layerdrop
			__lowerCAmelCase      :						Optional[int]						      =				use_cache
			__lowerCAmelCase      :						Optional[Any]						      =				encoder_layers
			__lowerCAmelCase      :						Optional[int]						      =				scale_embedding  # scale factor will be sqrt(d_model) if True
			__lowerCAmelCase      :						Union[str, Any]						      =				router_z_loss_coef
			__lowerCAmelCase      :						Optional[Any]						      =				router_aux_loss_coef
			__lowerCAmelCase      :						int						      =				decoder_sparse_step
			__lowerCAmelCase      :						str						      =				encoder_sparse_step
			__lowerCAmelCase      :						Tuple						      =				num_experts
			__lowerCAmelCase      :						Dict						      =				expert_capacity
			__lowerCAmelCase      :						Union[str, Any]						      =				router_bias
			if router_dtype not in ["float32", "float16", "bfloat16"]:
				raise ValueError(f"`router_dtype` must be one of 'float32', 'float16' or 'bfloat16', got {router_dtype}"			)
			__lowerCAmelCase      :						Union[str, Any]						      =				router_dtype

			__lowerCAmelCase      :						Any						      =				router_ignore_padding_tokens
			__lowerCAmelCase      :						str						      =				batch_prioritized_routing
			__lowerCAmelCase      :						Tuple						      =				second_expert_policy
			__lowerCAmelCase      :						List[str]						      =				normalize_router_prob_before_dropping
			__lowerCAmelCase      :						Dict						      =				moe_eval_capacity_token_fraction
			__lowerCAmelCase      :						Union[str, Any]						      =				moe_token_dropout
			__lowerCAmelCase      :						List[Any]						      =				output_router_logits
			super().__init__(
			    pad_token_id=_SCREAMING_SNAKE_CASE	,		bos_token_id=_SCREAMING_SNAKE_CASE	,		eos_token_id=_SCREAMING_SNAKE_CASE	,		is_encoder_decoder=_SCREAMING_SNAKE_CASE	,		decoder_start_token_id=_SCREAMING_SNAKE_CASE	,		**_SCREAMING_SNAKE_CASE	,		)