

'''simple docstring'''
import inspect
import unittest

from transformers import ConvNextVaConfig
from transformers.models.auto import get_values
from transformers.models.auto.modeling_auto import MODEL_FOR_BACKBONE_MAPPING_NAMES, MODEL_MAPPING_NAMES
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_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 ConvNextVaBackbone, ConvNextVaForImageClassification, ConvNextVaModel
    from transformers.models.convnextva.modeling_convnextva import CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import AutoImageProcessor


class        a__  :


   def __init__(			self      ,    _UpperCamelCase      ,    _UpperCamelCase=13      ,    _UpperCamelCase=32      ,    _UpperCamelCase=3      ,    _UpperCamelCase=4      ,    _UpperCamelCase=[10, 20, 30, 40]      ,    _UpperCamelCase=[2, 2, 3, 2]      ,    _UpperCamelCase=True      ,    _UpperCamelCase=True      ,    _UpperCamelCase=37      ,    _UpperCamelCase="gelu"      ,    _UpperCamelCase=10      ,    _UpperCamelCase=0.0_2      ,    _UpperCamelCase=["stage2", "stage3", "stage4"]      ,    _UpperCamelCase=[2, 3, 4]      ,    _UpperCamelCase=None      ,    ):





      """simple docstring"""





      _lowercase      :				Tuple      					=       parent
      _lowercase      :				Optional[int]      					=       batch_size
      _lowercase      :				Optional[Any]      					=       image_size
      _lowercase      :				List[str]      					=       num_channels
      _lowercase      :				List[Any]      					=       num_stages
      _lowercase      :				Dict      					=       hidden_sizes
      _lowercase      :				Any      					=       depths
      _lowercase      :				List[str]      					=       is_training
      _lowercase      :				List[str]      					=       use_labels
      _lowercase      :				Union[str, Any]      					=       intermediate_size
      _lowercase      :				int      					=       hidden_act
      _lowercase      :				List[str]      					=       num_labels
      _lowercase      :				Any      					=       initializer_range
      _lowercase      :				Tuple      					=       out_features
      _lowercase      :				Optional[int]      					=       out_indices
      _lowercase      :				int      					=       scope


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Optional[Any]      					=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

      _lowercase      :				Any      					=       None
      if self.use_labels:
         _lowercase      :				Dict      					=       ids_tensor([self.batch_size]      ,    self.num_labels  )

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

      return config, pixel_values, labels


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      return ConvNextVaConfig(
          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=_UpperCamelCase      ,    initializer_range=self.initializer_range      ,    out_features=self.out_features      ,    out_indices=self.out_indices      ,    num_labels=self.num_labels      ,    )


   def 						_lowerCamelCase       (			self      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase  ):





      """simple docstring"""





      _lowercase      :				int      					=       ConvNextVaModel(config=_UpperCamelCase  )
      model.to(_UpperCamelCase  )
      model.eval()
      _lowercase      :				str      					=       model(_UpperCamelCase  )
      # 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      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase  ):





      """simple docstring"""





      _lowercase      :				Union[str, Any]      					=       ConvNextVaForImageClassification(_UpperCamelCase  )
      model.to(_UpperCamelCase  )
      model.eval()
      _lowercase      :				Optional[int]      					=       model(_UpperCamelCase      ,    labels=_UpperCamelCase  )
      self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.num_labels)  )


   def 						_lowerCamelCase       (			self      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase  ):





      """simple docstring"""





      _lowercase      :				Dict      					=       ConvNextVaBackbone(config=_UpperCamelCase  )
      model.to(_UpperCamelCase  )
      model.eval()
      _lowercase      :				Dict      					=       model(_UpperCamelCase  )

      # 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
      _lowercase      :				Dict      					=       None
      _lowercase      :				List[Any]      					=       ConvNextVaBackbone(config=_UpperCamelCase  )
      model.to(_UpperCamelCase  )
      model.eval()
      _lowercase      :				Dict      					=       model(_UpperCamelCase  )

      # 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  ):





      """simple docstring"""





      _lowercase      :				Dict      					=       self.prepare_config_and_inputs()
      _lowercase					,				_lowercase					,				_lowercase      :				Optional[Any]      					=       config_and_inputs
      _lowercase      :				Optional[int]      					=       {"pixel_values": pixel_values}
      return config, inputs_dict






   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				List[Any]      					=       self.prepare_config_and_inputs()
      _lowercase					,				_lowercase					,				_lowercase      :				Union[str, Any]      					=       config_and_inputs
      _lowercase      :				Dict      					=       {"pixel_values": pixel_values, "labels": labels}
      return config, inputs_dict




@require_torch
class        a__  (							lowerCamelCase_  ,				lowerCamelCase_  ,				unittest.TestCase  ):
   _SCREAMING_SNAKE_CASE   :					Tuple     	=							(
       (
           ConvNextVaModel,
           ConvNextVaForImageClassification,
           ConvNextVaBackbone,
       )
       if is_torch_available()
       else ()
   )
   _SCREAMING_SNAKE_CASE   :					Optional[Any]     	=							(
       {'feature-extraction': ConvNextVaModel, 'image-classification': ConvNextVaForImageClassification}
       if is_torch_available()
       else {}
   )

   _SCREAMING_SNAKE_CASE   :					Any     	=							False
   _SCREAMING_SNAKE_CASE   :					Tuple     	=							False
   _SCREAMING_SNAKE_CASE   :					str     	=							False
   _SCREAMING_SNAKE_CASE   :					Union[str, Any]     	=							False
   _SCREAMING_SNAKE_CASE   :					str     	=							False


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				List[Any]      					=       ConvNextVaModelTester(self  )
      _lowercase      :				Optional[int]      					=       ConfigTester(self      ,    config_class=_UpperCamelCase      ,    has_text_modality=_UpperCamelCase      ,    hidden_size=37  )


   def 						_lowerCamelCase       (			self  ):





      """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 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      return


   @unittest.skip(reason="ConvNextV2 does not use inputs_embeds"  )
   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      pass


   @unittest.skip(reason="ConvNextV2 does not support input and output embeddings"  )
   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      pass


   @unittest.skip(reason="ConvNextV2 does not use feedforward chunking"  )
   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      pass


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      if not self.model_tester.is_training:
         return

      for model_class in self.all_model_classes:
         _lowercase					,				_lowercase      :				str      					=       self.model_tester.prepare_config_and_inputs_with_labels()
         _lowercase      :				List[Any]      					=       True

         if model_class.__name__ in [
             *get_values(_UpperCamelCase  ),
             *get_values(_UpperCamelCase  ),
         ]:
            continue

         _lowercase      :				List[str]      					=       model_class(_UpperCamelCase  )
         model.to(_UpperCamelCase  )
         model.train()
         _lowercase      :				List[Any]      					=       self._prepare_for_class(_UpperCamelCase      ,    _UpperCamelCase      ,    return_labels=_UpperCamelCase  )
         _lowercase      :				Tuple      					=       model(**_UpperCamelCase  ).loss
         loss.backward()


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      if not self.model_tester.is_training:
         return

      for model_class in self.all_model_classes:
         _lowercase					,				_lowercase      :				List[str]      					=       self.model_tester.prepare_config_and_inputs_with_labels()
         _lowercase      :				str      					=       False
         _lowercase      :				Any      					=       True

         if (
             model_class.__name__
             in [*get_values(_UpperCamelCase  ), *get_values(_UpperCamelCase  )]
             or not model_class.supports_gradient_checkpointing
         ):
            continue

         _lowercase      :				Tuple      					=       model_class(_UpperCamelCase  )
         model.to(_UpperCamelCase  )
         model.gradient_checkpointing_enable()
         model.train()
         _lowercase      :				Optional[int]      					=       self._prepare_for_class(_UpperCamelCase      ,    _UpperCamelCase      ,    return_labels=_UpperCamelCase  )
         _lowercase      :				Any      					=       model(**_UpperCamelCase  ).loss
         loss.backward()


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase					,				_lowercase      :				Optional[int]      					=       self.model_tester.prepare_config_and_inputs_for_common()

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

         _lowercase      :				List[str]      					=       ["pixel_values"]
         self.assertListEqual(arg_names[:1]      ,    _UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Tuple      					=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*_UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      def check_hidden_states_output(_UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase  ):
         _lowercase      :				Any      					=       model_class(_UpperCamelCase  )
         model.to(_UpperCamelCase  )
         model.eval()

         with torch.no_grad():
            _lowercase      :				Optional[Any]      					=       model(**self._prepare_for_class(_UpperCamelCase      ,    _UpperCamelCase  )  )

         _lowercase      :				str      					=       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

         _lowercase      :				Optional[int]      					=       self.model_tester.num_stages
         self.assertEqual(len(_UpperCamelCase  )      ,    expected_num_stages + 1  )

         # ConvNextV2'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]      ,    )

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

      for model_class in self.all_model_classes:
         _lowercase      :				List[str]      					=       True
         check_hidden_states_output(_UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase  )

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

         check_hidden_states_output(_UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				str      					=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase  )






   @slow
   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _lowercase      :				Any      					=       ConvNextVaModel.from_pretrained(_UpperCamelCase  )
         self.assertIsNotNone(_UpperCamelCase  )







def  _A		(     )      ->     int:
   _lowercase      :				List[str]      					=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					)
   return image


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


   @cached_property
   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      return AutoImageProcessor.from_pretrained("facebook/convnextv2-tiny-1k-224"  ) if is_vision_available() else None






   @slow
   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				int      					=       ConvNextVaForImageClassification.from_pretrained("facebook/convnextv2-tiny-1k-224"  ).to(_UpperCamelCase  )

      _lowercase      :				List[str]      					=       self.default_image_processor
      _lowercase      :				Union[str, Any]      					=       prepare_img()
      _lowercase      :				Optional[Any]      					=       preprocessor(images=_UpperCamelCase      ,    return_tensors="pt"  ).to(_UpperCamelCase  )

      # forward pass
      with torch.no_grad():
         _lowercase      :				List[Any]      					=       model(**_UpperCamelCase  )

      # verify the logits
      _lowercase      :				int      					=       torch.Size((1, 1000)  )
      self.assertEqual(outputs.logits.shape      ,    _UpperCamelCase  )

      _lowercase      :				List[Any]      					=       torch.tensor([0.9_9_9_6, 0.1_9_6_6, -0.4_3_8_6]  ).to(_UpperCamelCase  )
      self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,    _UpperCamelCase      ,    atol=1E-4  )  )









'''simple docstring'''
import numpy as np
from scipy.spatial.distance import cdist
from sklearn.metrics import fa_score

import datasets


_snake_case         =  '\\n    @inproceedings{kakwani2020indicnlpsuite,\n    title={{IndicNLPSuite: Monolingual Corpora, Evaluation Benchmarks and Pre-trained Multilingual Language Models for Indian Languages}},\n    author={Divyanshu Kakwani and Anoop Kunchukuttan and Satish Golla and Gokul N.C. and Avik Bhattacharyya and Mitesh M. Khapra and Pratyush Kumar},\n    year={2020},\n    booktitle={Findings of EMNLP},\n}\n'

_snake_case         =  '\\n    IndicGLUE is a natural language understanding benchmark for Indian languages. It contains a wide\n    variety of tasks and covers 11 major Indian languages - as, bn, gu, hi, kn, ml, mr, or, pa, ta, te.\n'

_snake_case         =  '\nCompute IndicGLUE evaluation metric associated to each IndicGLUE dataset.\nArgs:\n    predictions: list of predictions to score (as int64),\n        except for \'cvit-mkb-clsr\' where each prediction is a vector (of float32).\n    references: list of ground truth labels corresponding to the predictions (as int64),\n        except for \'cvit-mkb-clsr\' where each reference is a vector (of float32).\nReturns: depending on the IndicGLUE subset, one or several of:\n    "accuracy": Accuracy\n    "f1": F1 score\n    "precision": Precision@10\nExamples:\n\n    >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'wnli\')  # \'wnli\' or any of ["copa", "sna", "csqa", "wstp", "inltkh", "bbca", "iitp-mr", "iitp-pr", "actsa-sc", "md"]\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'accuracy\': 1.0}\n\n    >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'wiki-ner\')\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'accuracy\': 1.0, \'f1\': 1.0}\n\n    >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'cvit-mkb-clsr\')\n    >>> references = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]\n    >>> predictions = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]\n    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'precision@10\': 1.0}\n\n'







def  _A		(     snake_case		,							snake_case					)      ->     int:
   return float((preds == labels).mean()					)







def  _A		(     snake_case		,							snake_case					)      ->     Union[str, Any]:
   _lowercase      :				Any      					=       simple_accuracy(snake_case		,							snake_case					)
   _lowercase      :				List[Any]      					=       float(fa_score(y_true=snake_case		,							y_pred=snake_case					)					)
   return {
       "accuracy": acc,
       "f1": fa,
   }







def  _A		(     snake_case		,							snake_case					)      ->     List[str]:
   _lowercase      :				Any      					=       np.array(snake_case					)
   _lowercase      :				Any      					=       np.array(snake_case					)
   _lowercase      :				str      					=       en_sentvecs.shape[0]

   # mean centering
   _lowercase      :				List[Any]      					=       en_sentvecs - np.mean(snake_case		,							axis=0					)
   _lowercase      :				Tuple      					=       in_sentvecs - np.mean(snake_case		,							axis=0					)

   _lowercase      :				Any      					=       cdist(snake_case		,							snake_case		,							"cosine"					)
   _lowercase      :				Any      					=       np.array(range(snake_case					)					)
   _lowercase      :				Dict      					=       sim.argsort(axis=1					)[:, :10]
   _lowercase      :				List[str]      					=       np.any(preds == actual[:, None]		,							axis=1					)
   return float(matches.mean()					)


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


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      if self.config_name not in [
          "wnli",
          "copa",
          "sna",
          "csqa",
          "wstp",
          "inltkh",
          "bbca",
          "cvit-mkb-clsr",
          "iitp-mr",
          "iitp-pr",
          "actsa-sc",
          "md",
          "wiki-ner",
      ]:
         raise KeyError(
             "You should supply a configuration name selected in "
             "[\"wnli\", \"copa\", \"sna\", \"csqa\", \"wstp\", \"inltkh\", \"bbca\", "
             "\"cvit-mkb-clsr\", \"iitp-mr\", \"iitp-pr\", \"actsa-sc\", \"md\", "
             "\"wiki-ner\"]"  )
      return datasets.MetricInfo(
          description=_DESCRIPTION      ,    citation=_CITATION      ,    inputs_description=_KWARGS_DESCRIPTION      ,    features=datasets.Features(
              {
                  "predictions": datasets.Value("int64"  )
                  if self.config_name != "cvit-mkb-clsr"
                  else datasets.Sequence(datasets.Value("float32"  )  ),
                  "references": datasets.Value("int64"  )
                  if self.config_name != "cvit-mkb-clsr"
                  else datasets.Sequence(datasets.Value("float32"  )  ),
              }  )      ,    codebase_urls=[]      ,    reference_urls=[]      ,    format="numpy" if self.config_name != "cvit-mkb-clsr" else None      ,    )






   def 						_lowerCamelCase       (			self      ,    _UpperCamelCase      ,    _UpperCamelCase  ):





      """simple docstring"""





      if self.config_name == "cvit-mkb-clsr":
         return {"precision@10": precision_at_aa(_UpperCamelCase      ,    _UpperCamelCase  )}
      elif self.config_name in ["wiki-ner"]:
         return acc_and_fa(_UpperCamelCase      ,    _UpperCamelCase  )
      elif self.config_name in [
          "wnli",
          "copa",
          "sna",
          "csqa",
          "wstp",
          "inltkh",
          "bbca",
          "iitp-mr",
          "iitp-pr",
          "actsa-sc",
          "md",
      ]:
         return {"accuracy": simple_accuracy(_UpperCamelCase      ,    _UpperCamelCase  )}
      else:
         raise KeyError(
             "You should supply a configuration name selected in "
             "[\"wnli\", \"copa\", \"sna\", \"csqa\", \"wstp\", \"inltkh\", \"bbca\", "
             "\"cvit-mkb-clsr\", \"iitp-mr\", \"iitp-pr\", \"actsa-sc\", \"md\", "
             "\"wiki-ner\"]"  )






