


'''simple docstring'''






from __future__ import annotations

import inspect
import unittest
from math import floor

import numpy as np

from transformers import CvtConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers import TFCvtForImageClassification, TFCvtModel
  from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor






class    __snake_case       (   _SCREAMING_SNAKE_CASE):







    """simple docstring"""



    def __lowercase   (   self    :  Dict		)   ->  Optional[Any]:
      lowerCAmelCase_			:      Optional[int]			=   self.config_class(**self.inputs_dict		)
      self.parent.assertTrue(hasattr(lowerCamelCase    ,						"""embed_dim"""		)		)
      self.parent.assertTrue(hasattr(lowerCamelCase    ,						"""num_heads"""		)		)







class    __snake_case       :







    """simple docstring"""



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

    def __lowercase   (   self    :  Union[str, Any]		)   ->  Optional[int]:
      lowerCAmelCase_			:      Optional[Any]			=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

      lowerCAmelCase_			:      Any			=   None
      if self.use_labels:
        # create a random int32 tensor of given shape
        lowerCAmelCase_			:      List[str]			=   ids_tensor([self.batch_size]    ,						self.num_labels		)

      lowerCAmelCase_			:      Tuple			=   self.get_config()
      return config, pixel_values, labels

    def __lowercase   (   self    :  List[str]		)   ->  Optional[int]:
      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 __lowercase   (   self    :  Optional[Any]    ,						lowerCamelCase    :  Optional[int]    ,						lowerCamelCase    :  str    ,						lowerCamelCase    :  Tuple		)   ->  Optional[int]:
      lowerCAmelCase_			:      List[str]			=   TFCvtModel(config=lowerCamelCase		)
      lowerCAmelCase_			:      Dict			=   model(lowerCamelCase    ,						training=lowerCamelCase		)
      lowerCAmelCase_			:      Dict			=   (self.image_size, self.image_size)
      lowerCAmelCase_,							lowerCAmelCase_			:      int			=   image_size[0], image_size[1]
      for i in range(len(self.depth		)		):
        lowerCAmelCase_			:      Optional[Any]			=   floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1		)
        lowerCAmelCase_			:      List[Any]			=   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 __lowercase   (   self    :  Union[str, Any]    ,						lowerCamelCase    :  Union[str, Any]    ,						lowerCamelCase    :  Tuple    ,						lowerCamelCase    :  List[str]		)   ->  List[Any]:
      lowerCAmelCase_			:      List[str]			=   self.num_labels
      lowerCAmelCase_			:      List[Any]			=   TFCvtForImageClassification(lowerCamelCase		)
      lowerCAmelCase_			:      str			=   model(lowerCamelCase    ,						labels=lowerCamelCase    ,						training=lowerCamelCase		)
      self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_labels)		)





    def __lowercase   (   self    :  Union[str, Any]		)   ->  Any:
      lowerCAmelCase_			:      Tuple			=   self.prepare_config_and_inputs()
      lowerCAmelCase_,							lowerCAmelCase_,							lowerCAmelCase_			:      List[str]			=   config_and_inputs
      lowerCAmelCase_			:      Optional[Any]			=   {"""pixel_values""": pixel_values}
      return config, inputs_dict







@require_tf
class    __snake_case       (   _SCREAMING_SNAKE_CASE	,_SCREAMING_SNAKE_CASE	,unittest.TestCase):







    """simple docstring"""



    lowercase    =					(TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
    lowercase    =					(
        {'feature-extraction': TFCvtModel, 'image-classification': TFCvtForImageClassification}
        if is_tf_available()
        else {}
    )
    lowercase    =					False
    lowercase    =					False
    lowercase    =					False
    lowercase    =					False
    lowercase    =					False

    def __lowercase   (   self    :  List[Any]		)   ->  str:
      lowerCAmelCase_			:      Union[str, Any]			=   TFCvtModelTester(self		)
      lowerCAmelCase_			:      Dict			=   TFCvtConfigTester(self    ,						config_class=lowerCamelCase    ,						has_text_modality=lowerCamelCase    ,						hidden_size=37		)

    def __lowercase   (   self    :  Any		)   ->  Union[str, Any]:
      self.config_tester.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()

    @unittest.skip(reason="""Cvt does not output attentions"""		)
    def __lowercase   (   self    :  Optional[Any]		)   ->  List[str]:
      pass

    @unittest.skip(reason="""Cvt does not use inputs_embeds"""		)
    def __lowercase   (   self    :  Tuple		)   ->  Tuple:
      pass

    @unittest.skip(reason="""Cvt does not support input and output embeddings"""		)
    def __lowercase   (   self    :  Optional[Any]		)   ->  Union[str, Any]:
      pass

    @unittest.skipIf(
        not is_tf_available() or len(tf.config.list_physical_devices("""GPU"""		)		) == 0    ,						reason="""TF does not support backprop for grouped convolutions on CPU."""    ,						)
    def __lowercase   (   self    :  str		)   ->  str:
      super().test_dataset_conversion()

    @unittest.skipIf(
        not is_tf_available() or len(tf.config.list_physical_devices("""GPU"""		)		) == 0    ,						reason="""TF does not support backprop for grouped convolutions on CPU."""    ,						)
    @slow
    def __lowercase   (   self    :  List[Any]		)   ->  int:
      super().test_keras_fit()

    @unittest.skip(reason="""Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8"""		)
    def __lowercase   (   self    :  Optional[Any]		)   ->  Any:
      lowerCAmelCase_			:      Dict			=   tf.keras.mixed_precision.Policy("""mixed_float16"""		)
      tf.keras.mixed_precision.set_global_policy(lowerCamelCase		)
      super().test_keras_fit()
      tf.keras.mixed_precision.set_global_policy("""float32"""		)

    def __lowercase   (   self    :  Union[str, Any]		)   ->  Union[str, Any]:
      lowerCAmelCase_,							lowerCAmelCase_			:      List[Any]			=   self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        lowerCAmelCase_			:      str			=   model_class(lowerCamelCase		)
        lowerCAmelCase_			:      List[Any]			=   inspect.signature(model.call		)
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        lowerCAmelCase_			:      Dict			=   [*signature.parameters.keys()]

        lowerCAmelCase_			:      Union[str, Any]			=   ["""pixel_values"""]
        self.assertListEqual(arg_names[:1]    ,						lowerCamelCase		)

    def __lowercase   (   self    :  int		)   ->  List[str]:
      def check_hidden_states_output(lowerCamelCase    :  List[Any]    ,						lowerCamelCase    :  Optional[Any]    ,						lowerCamelCase    :  Any		):
        lowerCAmelCase_			:      str			=   model_class(lowerCamelCase		)

        lowerCAmelCase_			:      str			=   model(**self._prepare_for_class(lowerCamelCase    ,						lowerCamelCase		)		)
        lowerCAmelCase_			:      int			=   outputs.hidden_states

        lowerCAmelCase_			:      List[str]			=   len(self.model_tester.depth		)
        self.assertEqual(len(lowerCamelCase		)    ,						lowerCamelCase		)

        # 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_			:      Union[str, Any]			=   self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        lowerCAmelCase_			:      Dict			=   True
        check_hidden_states_output(lowerCamelCase    ,						lowerCamelCase    ,						lowerCamelCase		)

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

        check_hidden_states_output(lowerCamelCase    ,						lowerCamelCase    ,						lowerCamelCase		)

    def __lowercase   (   self    :  List[Any]		)   ->  Optional[Any]:
      lowerCAmelCase_			:      str			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*lowerCamelCase		)

    def __lowercase   (   self    :  List[str]		)   ->  Union[str, Any]:
      lowerCAmelCase_			:      List[str]			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*lowerCamelCase		)





    @slow
    def __lowercase   (   self    :  Optional[Any]		)   ->  Optional[int]:
      for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowerCAmelCase_			:      Union[str, Any]			=   TFCvtModel.from_pretrained(lowerCamelCase		)
        self.assertIsNotNone(lowerCamelCase		)







def 				UpperCamelCase_			(    ):
  '''simple docstring'''





  lowerCAmelCase_			:      List[Any]			=   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
  return image






@require_tf
@require_vision
class    __snake_case       (   unittest.TestCase):







    """simple docstring"""



    @cached_property
    def __lowercase   (   self    :  Optional[Any]		)   ->  List[str]:
      return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)





    @slow
    def __lowercase   (   self    :  Optional[Any]		)   ->  List[str]:
      lowerCAmelCase_			:      int			=   TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)

      lowerCAmelCase_			:      Optional[int]			=   self.default_image_processor
      lowerCAmelCase_			:      List[str]			=   prepare_img()
      lowerCAmelCase_			:      List[Any]			=   image_processor(images=lowerCamelCase    ,						return_tensors="""tf"""		)

      # forward pass
      lowerCAmelCase_			:      Union[str, Any]			=   model(**lowerCamelCase		)

      # verify the logits
      lowerCAmelCase_			:      Dict			=   tf.TensorShape((1, 10_00)		)
      self.assertEqual(outputs.logits.shape    ,						lowerCamelCase		)

      lowerCAmelCase_			:      str			=   tf.constant([0.9_285, 0.9_015, -0.3_150]		)
      self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()    ,						lowerCamelCase    ,						atol=1E-4		)		)







'''simple docstring'''






import json
import os
import unittest

from transformers.models.biogpt.tokenization_biogpt import VOCAB_FILES_NAMES, BioGptTokenizer
from transformers.testing_utils import slow

from ...test_tokenization_common import TokenizerTesterMixin






class    __snake_case       (   _SCREAMING_SNAKE_CASE	,unittest.TestCase):







    """simple docstring"""



    lowercase    =					BioGptTokenizer
    lowercase    =					False

    def __lowercase   (   self    :  List[Any]		)   ->  List[str]:
      super().setUp()

      # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
      lowerCAmelCase_			:      Optional[Any]			=   [
          """l""",
          """o""",
          """w""",
          """e""",
          """r""",
          """s""",
          """t""",
          """i""",
          """d""",
          """n""",
          """w</w>""",
          """r</w>""",
          """t</w>""",
          """lo""",
          """low""",
          """er</w>""",
          """low</w>""",
          """lowest</w>""",
          """newer</w>""",
          """wider</w>""",
          """<unk>""",
      ]
      lowerCAmelCase_			:      int			=   dict(zip(lowerCamelCase    ,						range(len(lowerCamelCase		)		)		)		)
      lowerCAmelCase_			:      int			=   ["""l o 123""", """lo w 1456""", """e r</w> 1789""", """"""]

      lowerCAmelCase_			:      Optional[int]			=   os.path.join(self.tmpdirname    ,						VOCAB_FILES_NAMES["""vocab_file"""]		)
      lowerCAmelCase_			:      Union[str, Any]			=   os.path.join(self.tmpdirname    ,						VOCAB_FILES_NAMES["""merges_file"""]		)
      with open(self.vocab_file    ,						"""w"""		) as fp:
        fp.write(json.dumps(lowerCamelCase		)		)
      with open(self.merges_file    ,						"""w"""		) as fp:
        fp.write("""\n""".join(lowerCamelCase		)		)

    def __lowercase   (   self    :  Union[str, Any]    ,						lowerCamelCase    :  Any		)   ->  List[str]:
      lowerCAmelCase_			:      Dict			=   """lower newer"""
      lowerCAmelCase_			:      str			=   """lower newer"""
      return input_text, output_text

    def __lowercase   (   self    :  Optional[int]		)   ->  str:
      lowerCAmelCase_			:      Any			=   BioGptTokenizer(self.vocab_file    ,						self.merges_file		)

      lowerCAmelCase_			:      int			=   """lower"""
      lowerCAmelCase_			:      str			=   ["""low""", """er</w>"""]
      lowerCAmelCase_			:      Any			=   tokenizer.tokenize(lowerCamelCase		)
      self.assertListEqual(lowerCamelCase    ,						lowerCamelCase		)

      lowerCAmelCase_			:      Dict			=   tokens + ["""<unk>"""]
      lowerCAmelCase_			:      Optional[Any]			=   [14, 15, 20]
      self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase		)    ,						lowerCamelCase		)





    @slow
    def __lowercase   (   self    :  str		)   ->  Optional[Any]:
      lowerCAmelCase_			:      Dict			=   BioGptTokenizer.from_pretrained("""microsoft/biogpt"""		)

      lowerCAmelCase_			:      int			=   tokenizer.encode("""sequence builders"""    ,						add_special_tokens=lowerCamelCase		)
      lowerCAmelCase_			:      Optional[Any]			=   tokenizer.encode("""multi-sequence build"""    ,						add_special_tokens=lowerCamelCase		)

      lowerCAmelCase_			:      str			=   tokenizer.build_inputs_with_special_tokens(lowerCamelCase		)
      lowerCAmelCase_			:      Optional[int]			=   tokenizer.build_inputs_with_special_tokens(lowerCamelCase    ,						lowerCamelCase		)

      self.assertTrue(encoded_sentence == [2] + text		)
      self.assertTrue(encoded_pair == [2] + text + [2] + text_a		)



