




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	:   Optional[Any]					,							snake_case__	:   Tuple					,							snake_case__	:   List[str]=13					,							snake_case__	:   int=32					,							snake_case__	:   List[Any]=3					,							snake_case__	:   List[str]=4					,							snake_case__	:   Optional[int]=[10, 20, 30, 40]					,							snake_case__	:   Optional[Any]=[2, 2, 3, 2]					,							snake_case__	:   Tuple=True					,							snake_case__	:   List[str]=True					,							snake_case__	:   List[str]=37					,							snake_case__	:   Any="gelu"					,							snake_case__	:   Tuple=10					,							snake_case__	:   List[Any]=0.02					,							snake_case__	:   List[str]=["stage2", "stage3", "stage4"]					,							snake_case__	:   Optional[int]=[2, 3, 4]					,							snake_case__	:   Union[str, Any]=None					,							):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							parent
								SCREAMING_SNAKE_CASE_					      =							batch_size
								SCREAMING_SNAKE_CASE_					      =							image_size
								SCREAMING_SNAKE_CASE_					      =							num_channels
								SCREAMING_SNAKE_CASE_					      =							num_stages
								SCREAMING_SNAKE_CASE_					      =							hidden_sizes
								SCREAMING_SNAKE_CASE_					      =							depths
								SCREAMING_SNAKE_CASE_					      =							is_training
								SCREAMING_SNAKE_CASE_					      =							use_labels
								SCREAMING_SNAKE_CASE_					      =							intermediate_size
								SCREAMING_SNAKE_CASE_					      =							hidden_act
								SCREAMING_SNAKE_CASE_					      =							num_labels
								SCREAMING_SNAKE_CASE_					      =							initializer_range
								SCREAMING_SNAKE_CASE_					      =							out_features
								SCREAMING_SNAKE_CASE_					      =							out_indices
								SCREAMING_SNAKE_CASE_					      =							scope



						def 	__a					(			self	:   str  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

								SCREAMING_SNAKE_CASE_					      =							None
								if self.use_labels:
										SCREAMING_SNAKE_CASE_					      =							ids_tensor([self.batch_size]					,							self.num_labels  )

								SCREAMING_SNAKE_CASE_					      =							self.get_config()
								return config, pixel_values, labels



						def 	__a					(			self	:   int  ):

								"""simple docstring"""

								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=snake_case__					,							initializer_range=self.initializer_range					,							out_features=self.out_features					,							out_indices=self.out_indices					,							num_labels=self.num_labels					,							)



						def 	__a					(			self	:   Union[str, Any]					,							snake_case__	:   str					,							snake_case__	:   List[str]					,							snake_case__	:   List[Any]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							ConvNextModel(config=snake_case__  )
								model.to(snake_case__  )
								model.eval()
								SCREAMING_SNAKE_CASE_					      =							model(snake_case__  )
								# 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 	__a					(			self	:   List[Any]					,							snake_case__	:   int					,							snake_case__	:   Any					,							snake_case__	:   Union[str, Any]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							ConvNextForImageClassification(snake_case__  )
								model.to(snake_case__  )
								model.eval()
								SCREAMING_SNAKE_CASE_					      =							model(snake_case__					,							labels=snake_case__  )
								self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.num_labels)  )



						def 	__a					(			self	:   Tuple					,							snake_case__	:   Dict					,							snake_case__	:   Tuple					,							snake_case__	:   int  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							ConvNextBackbone(config=snake_case__  )
								model.to(snake_case__  )
								model.eval()
								SCREAMING_SNAKE_CASE_					      =							model(snake_case__  )

								# 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
								SCREAMING_SNAKE_CASE_					      =							None
								SCREAMING_SNAKE_CASE_					      =							ConvNextBackbone(config=snake_case__  )
								model.to(snake_case__  )
								model.eval()
								SCREAMING_SNAKE_CASE_					      =							model(snake_case__  )

								# 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 	__a					(			self	:   str  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							self.prepare_config_and_inputs()
								SCREAMING_SNAKE_CASE_					      =							config_and_inputs
								SCREAMING_SNAKE_CASE_					      =							{"pixel_values": pixel_values}
								return config, inputs_dict



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

						lowerCAmelCase__			      =True
						lowerCAmelCase__			      =False
						lowerCAmelCase__			      =False
						lowerCAmelCase__			      =False
						lowerCAmelCase__			      =False



						def 	__a					(			self	:   Tuple  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							ConvNextModelTester(self  )
								SCREAMING_SNAKE_CASE_					      =							ConfigTester(self					,							config_class=snake_case__					,							has_text_modality=snake_case__					,							hidden_size=37  )



						def 	__a					(			self	:   List[Any]  ):

								"""simple docstring"""

								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 	__a					(			self	:   List[str]  ):

								"""simple docstring"""

								return



						@unittest.skip(reason='ConvNext does not use inputs_embeds'  )
						def 	__a					(			self	:   Dict  ):

								"""simple docstring"""

								pass



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

								"""simple docstring"""

								pass



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

								"""simple docstring"""

								pass



						def 	__a					(			self	:   Union[str, Any]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							self.model_tester.prepare_config_and_inputs_for_common()

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

										SCREAMING_SNAKE_CASE_					      =							["pixel_values"]
										self.assertListEqual(arg_names[:1]					,							snake_case__  )



						def 	__a					(			self	:   Any  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*snake_case__  )



						def 	__a					(			self	:   List[str]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_backbone(*snake_case__  )



						def 	__a					(			self	:   Union[str, Any]  ):

								"""simple docstring"""

								def check_hidden_states_output(snake_case__	:   Tuple					,							snake_case__	:   int					,							snake_case__	:   Any  ):
										SCREAMING_SNAKE_CASE_					      =							model_class(snake_case__  )
										model.to(snake_case__  )
										model.eval()

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

										SCREAMING_SNAKE_CASE_					      =							outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

										SCREAMING_SNAKE_CASE_					      =							self.model_tester.num_stages
										self.assertEqual(len(snake_case__  )					,							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]					,							)

								SCREAMING_SNAKE_CASE_					      =							self.model_tester.prepare_config_and_inputs_for_common()

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

										# check that output_hidden_states also work using config
										del inputs_dict["output_hidden_states"]
										SCREAMING_SNAKE_CASE_					      =							True

										check_hidden_states_output(snake_case__					,							snake_case__					,							snake_case__  )



						def 	__a					(			self	:   List[Any]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*snake_case__  )






						@slow
						def 	__a					(			self	:   Dict  ):

								"""simple docstring"""

								for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										SCREAMING_SNAKE_CASE_					      =							ConvNextModel.from_pretrained(snake_case__  )
										self.assertIsNotNone(snake_case__  )


def 					_a							(					)->	Optional[int]:
		SCREAMING_SNAKE_CASE_					      =							Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' )
		return image



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



						@cached_property
						def 	__a					(			self	:   Tuple  ):

								"""simple docstring"""

								return AutoImageProcessor.from_pretrained('facebook/convnext-tiny-224'  ) if is_vision_available() else None






						@slow
						def 	__a					(			self	:   str  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							ConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224'  ).to(snake_case__  )

								SCREAMING_SNAKE_CASE_					      =							self.default_image_processor
								SCREAMING_SNAKE_CASE_					      =							prepare_img()
								SCREAMING_SNAKE_CASE_					      =							image_processor(images=snake_case__					,							return_tensors='pt'  ).to(snake_case__  )

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

								# verify the logits
								SCREAMING_SNAKE_CASE_					      =							torch.Size((1, 10_00)  )
								self.assertEqual(outputs.logits.shape					,							snake_case__  )

								SCREAMING_SNAKE_CASE_					      =							torch.tensor([-0.02_60, -0.47_39, 0.19_11]  ).to(snake_case__  )

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



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

						lowerCAmelCase__			      =False



						def 	__a					(			self	:   Tuple  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							ConvNextModelTester(self  )



import os

from bleurt import score  # From: git+https://github.com/google-research/bleurt.git

import datasets


SCREAMING_SNAKE_CASE__     					=			datasets.logging.get_logger(__name__)


SCREAMING_SNAKE_CASE__     					=			"\\n@inproceedings{bleurt,\n  title={BLEURT: Learning Robust Metrics for Text Generation},\n  author={Thibault Sellam and Dipanjan Das and Ankur P. Parikh},\n  booktitle={ACL},\n  year={2020},\n  url={https://arxiv.org/abs/2004.04696}\n}\n"

SCREAMING_SNAKE_CASE__     					=			"\\nBLEURT a learnt evaluation metric for Natural Language Generation. It is built using multiple phases of transfer learning starting from a pretrained BERT model (Devlin et al. 2018)\nand then employing another pre-training phrase using synthetic data. Finally it is trained on WMT human annotations. You may run BLEURT out-of-the-box or fine-tune\nit for your specific application (the latter is expected to perform better).\n\nSee the project's README at https://github.com/google-research/bleurt#readme for more information.\n"

SCREAMING_SNAKE_CASE__     					=			"\nBLEURT score.\n\nArgs:\n    `predictions` (list of str): prediction/candidate sentences\n    `references` (list of str): reference sentences\n    `checkpoint` BLEURT checkpoint. Will default to BLEURT-tiny if None.\n\nReturns:\n    'scores': List of scores.\nExamples:\n\n    >>> predictions = [\"hello there\", \"general kenobi\"]\n    >>> references = [\"hello there\", \"general kenobi\"]\n    >>> bleurt = datasets.load_metric(\"bleurt\")\n    >>> results = bleurt.compute(predictions=predictions, references=references)\n    >>> print([round(v, 2) for v in results[\"scores\"]])\n    [1.03, 1.04]\n"

SCREAMING_SNAKE_CASE__     					=			{
    "bleurt-tiny-128": "https://storage.googleapis.com/bleurt-oss/bleurt-tiny-128.zip",
    "bleurt-tiny-512": "https://storage.googleapis.com/bleurt-oss/bleurt-tiny-512.zip",
    "bleurt-base-128": "https://storage.googleapis.com/bleurt-oss/bleurt-base-128.zip",
    "bleurt-base-512": "https://storage.googleapis.com/bleurt-oss/bleurt-base-512.zip",
    "bleurt-large-128": "https://storage.googleapis.com/bleurt-oss/bleurt-large-128.zip",
    "bleurt-large-512": "https://storage.googleapis.com/bleurt-oss/bleurt-large-512.zip",
    "BLEURT-20-D3": "https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D3.zip",
    "BLEURT-20-D6": "https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D6.zip",
    "BLEURT-20-D12": "https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D12.zip",
    "BLEURT-20": "https://storage.googleapis.com/bleurt-oss-21/BLEURT-20.zip",
}



@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION		,		_KWARGS_DESCRIPTION							)
class 						_UpperCAmelCase      (     datasets.Metric							):






		def _snake_case		(						self				:     List[str]):
				return datasets.MetricInfo(
				    description=_DESCRIPTION   ,  citation=_CITATION   ,  homepage="https://github.com/google-research/bleurt"   ,  inputs_description=_KWARGS_DESCRIPTION   ,  features=datasets.Features(
				        {
				            "predictions": datasets.Value("string"   ,  id="sequence"),
				            "references": datasets.Value("string"   ,  id="sequence"),
				        })   ,  codebase_urls=["https://github.com/google-research/bleurt"]   ,  reference_urls=["https://github.com/google-research/bleurt", "https://arxiv.org/abs/2004.04696"]   ,  )






		def _snake_case		(						self				:     Any   ,  UpperCAmelCase				:     Any):
				# check that config name specifies a valid BLEURT model
				if self.config_name == "default":
						logger.warning(
						    "Using default BLEURT-Base checkpoint for sequence maximum length 128. "
						    "You can use a bigger model for better results with e.g.: datasets.load_metric('bleurt', 'bleurt-large-512').")
						SCREAMING_SNAKE_CASE_				:str				     =						"bleurt-base-128"

				if self.config_name.lower() in CHECKPOINT_URLS:
						SCREAMING_SNAKE_CASE_				:Dict				     =						self.config_name.lower()

				elif self.config_name.upper() in CHECKPOINT_URLS:
						SCREAMING_SNAKE_CASE_				:Dict				     =						self.config_name.upper()

				else:
						raise KeyError(
						    F"{self.config_name} model not found. You should supply the name of a model checkpoint for bleurt in {CHECKPOINT_URLS.keys()}")

				# download the model checkpoint specified by self.config_name and set up the scorer
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						dl_manager.download_and_extract(CHECKPOINT_URLS[checkpoint_name])
				SCREAMING_SNAKE_CASE_				:Dict				     =						score.BleurtScorer(os.path.join(UpperCAmelCase   ,  UpperCAmelCase))







		def _snake_case		(						self				:     Union[str, Any]   ,  UpperCAmelCase				:     List[str]   ,  UpperCAmelCase				:     Union[str, Any]):
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						self.scorer.score(references=UpperCAmelCase   ,  candidates=UpperCAmelCase)
				return {"scores": scores}






