




'''simple docstring'''







from __future__ import annotations

import unittest

from transformers import EsmConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

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


if is_tf_available():
      import numpy
      import tensorflow as tf

      from transformers.models.esm.modeling_tf_esm import (
          TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFEsmForMaskedLM,
          TFEsmForSequenceClassification,
          TFEsmForTokenClassification,
          TFEsmModel,
      )







class 					_a :





     '''simple docstring'''



     def __init__(				self							,__a							,)      ->				Optional[int]:
          snake_case			:      List[Any]        =			parent
          snake_case			:      Union[str, Any]        =			13
          snake_case			:      Union[str, Any]        =			7
          snake_case			:      List[Any]        =			True
          snake_case			:      int        =			True
          snake_case			:      List[str]        =			True
          snake_case			:      Tuple        =			99
          snake_case			:      Any        =			32
          snake_case			:      Optional[Any]        =			2
          snake_case			:      Union[str, Any]        =			4
          snake_case			:      Dict        =			37
          snake_case			:      Optional[Any]        =			"gelu"
          snake_case			:      List[str]        =			0.1
          snake_case			:      int        =			0.1
          snake_case			:      List[str]        =			512
          snake_case			:      Tuple        =			16
          snake_case			:      Any        =			2
          snake_case			:      Optional[int]        =			0.02
          snake_case			:      Optional[Any]        =			3
          snake_case			:      List[Any]        =			4
          snake_case			:      Optional[int]        =			None







     def 			snake_case_      (				self							)      ->				Any:
          snake_case			:      Any        =			ids_tensor([self.batch_size, self.seq_length]							,self.vocab_size							)

          snake_case			:      List[Any]        =			None
          if self.use_input_mask:
               snake_case			:      Optional[int]        =			random_attention_mask([self.batch_size, self.seq_length]							)

          snake_case			:      int        =			None
          snake_case			:      Tuple        =			None
          snake_case			:      Dict        =			None
          if self.use_labels:
               snake_case			:      Tuple        =			ids_tensor([self.batch_size]							,self.type_sequence_label_size							)
               snake_case			:      List[Any]        =			ids_tensor([self.batch_size, self.seq_length]							,self.num_labels							)
               snake_case			:      Dict        =			ids_tensor([self.batch_size]							,self.num_choices							)

          snake_case			:      Union[str, Any]        =			EsmConfig(
              vocab_size=self.vocab_size							,hidden_size=self.hidden_size							,num_hidden_layers=self.num_hidden_layers							,pad_token_id=1							,num_attention_heads=self.num_attention_heads							,intermediate_size=self.intermediate_size							,hidden_act=self.hidden_act							,hidden_dropout_prob=self.hidden_dropout_prob							,attention_probs_dropout_prob=self.attention_probs_dropout_prob							,max_position_embeddings=self.max_position_embeddings							,type_vocab_size=self.type_vocab_size							,initializer_range=self.initializer_range							,)

          return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels







     def 			snake_case_      (				self							)      ->				Any:
          (
              snake_case			
          )			:      Any        =			self.prepare_config_and_inputs()

          snake_case			:      Optional[Any]        =			True
          snake_case			:      List[str]        =			floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
          snake_case			:      List[str]        =			ids_tensor([self.batch_size, self.seq_length]							,vocab_size=2							)

          return (
              config,
              input_ids,
              input_mask,
              sequence_labels,
              token_labels,
              choice_labels,
              encoder_hidden_states,
              encoder_attention_mask,
          )







     def 			snake_case_      (				self							,__a							,__a							,__a							,__a							,__a							,__a							)      ->				Any:
          snake_case			:      List[str]        =			TFEsmModel(config=__a							)
          snake_case			:      int        =			{"input_ids": input_ids, "attention_mask": input_mask}
          snake_case			:      Optional[int]        =			model(__a							)

          snake_case			:      Any        =			[input_ids, input_mask]
          snake_case			:      List[Any]        =			model(__a							)

          snake_case			:      List[Any]        =			model(__a							)

          self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, self.seq_length, self.hidden_size)							)







     def 			snake_case_      (				self							,__a							,__a							,__a							,__a							,__a							,__a							,__a							,__a							,)      ->				List[Any]:
          snake_case			:      Optional[int]        =			True

          snake_case			:      str        =			TFEsmModel(config=__a							)
          snake_case			:      Any        =			{
              "input_ids": input_ids,
              "attention_mask": input_mask,
              "encoder_hidden_states": encoder_hidden_states,
              "encoder_attention_mask": encoder_attention_mask,
          }
          snake_case			:      Dict        =			model(__a							)

          snake_case			:      int        =			[input_ids, input_mask]
          snake_case			:      Any        =			model(__a							,encoder_hidden_states=__a							)

          # Also check the case where encoder outputs are not passed
          snake_case			:      Any        =			model(__a							,attention_mask=__a							)

          self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, self.seq_length, self.hidden_size)							)







     def 			snake_case_      (				self							,__a							,__a							,__a							,__a							,__a							,__a							)      ->				Union[str, Any]:
          snake_case			:      Optional[Any]        =			TFEsmForMaskedLM(config=__a							)
          snake_case			:      Union[str, Any]        =			model([input_ids, input_mask]							)
          self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.vocab_size)							)







     def 			snake_case_      (				self							,__a							,__a							,__a							,__a							,__a							,__a							)      ->				Optional[int]:
          snake_case			:      List[Any]        =			self.num_labels
          snake_case			:      Optional[int]        =			TFEsmForTokenClassification(config=__a							)
          snake_case			:      Optional[Any]        =			{"input_ids": input_ids, "attention_mask": input_mask}
          snake_case			:      Tuple        =			model(__a							)
          self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.num_labels)							)







     def 			snake_case_      (				self							)      ->				str:
          snake_case			:      Optional[Any]        =			self.prepare_config_and_inputs()
          (
              snake_case			
          )			:      int        =			config_and_inputs
          snake_case			:      int        =			{"input_ids": input_ids, "attention_mask": input_mask}
          return config, inputs_dict







@require_tf
class 					_a (_UpperCamelCase,		_UpperCamelCase,		unittest.TestCase	):





     '''simple docstring'''



     lowerCAmelCase_  :     Optional[Any]          =					(
         (
             TFEsmModel,
             TFEsmForMaskedLM,
             TFEsmForSequenceClassification,
             TFEsmForTokenClassification,
         )
         if is_tf_available()
         else ()
     )
     lowerCAmelCase_  :     Any          =					(
         {
             'feature-extraction': TFEsmModel,
             'fill-mask': TFEsmForMaskedLM,
             'text-classification': TFEsmForSequenceClassification,
             'token-classification': TFEsmForTokenClassification,
             'zero-shot': TFEsmForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     lowerCAmelCase_  :     int          =					False
     lowerCAmelCase_  :     Optional[int]          =					False







     def 			snake_case_      (				self							)      ->				int:
          snake_case			:      List[str]        =			TFEsmModelTester(self							)
          snake_case			:      List[str]        =			ConfigTester(self							,config_class=__a							,hidden_size=37							)







     def 			snake_case_      (				self							)      ->				Optional[int]:
          self.config_tester.run_common_tests()







     def 			snake_case_      (				self							)      ->				int:
          snake_case			:      Tuple        =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__a							)







     def 			snake_case_      (				self							)      ->				Any:
          snake_case			:      List[str]        =			self.model_tester.prepare_config_and_inputs_for_decoder()
          self.model_tester.create_and_check_model_as_decoder(*__a							)







     def 			snake_case_      (				self							)      ->				str:
          snake_case			:      str        =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*__a							)







     def 			snake_case_      (				self							)      ->				str:
          snake_case			:      Tuple        =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*__a							)







     @slow
     def 			snake_case_      (				self							)      ->				Tuple:
          for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               snake_case			:      List[Any]        =			TFEsmModel.from_pretrained(__a							)
               self.assertIsNotNone(__a							)







     @unittest.skip("""Protein models do not support embedding resizing."""							)
     def 			snake_case_      (				self							)      ->				Union[str, Any]:
          pass







     @unittest.skip("""Protein models do not support embedding resizing."""							)
     def 			snake_case_      (				self							)      ->				Dict:
          pass







     def 			snake_case_      (				self							)      ->				Optional[Any]:
          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(__a							)
               assert isinstance(model.get_input_embeddings()							,tf.keras.layers.Layer							)
               if model_class is TFEsmForMaskedLM:
                    # Output embedding test differs from the main test because they're a matrix, not a layer
                    snake_case			:      Dict        =			model.get_bias()
                    assert isinstance(__a							,__a							)
                    for k, v in name.items():
                         assert isinstance(__a							,tf.Variable							)
               else:
                    snake_case			:      List[str]        =			model.get_output_embeddings()
                    assert x is None
                    snake_case			:      List[Any]        =			model.get_bias()
                    assert name is None







@require_tf
class 					_a (unittest.TestCase	):





     '''simple docstring'''



     @slow
     def 			snake_case_      (				self							)      ->				str:
          snake_case			:      Union[str, Any]        =			TFEsmForMaskedLM.from_pretrained("""facebook/esm2_t6_8M_UR50D"""							)

          snake_case			:      Tuple        =			tf.constant([[0, 1, 2, 3, 4, 5]]							)
          snake_case			:      str        =			model(__a							)[0]
          snake_case			:      Dict        =			[1, 6, 33]
          self.assertEqual(list(output.numpy().shape							)							,__a							)
          # compare the actual values for a slice.
          snake_case			:      Union[str, Any]        =			tf.constant(
              [
                  [
                      [8.921_518, -10.589_814, -6.4_671_307],
                      [-6.3_967_156, -13.911_377, -1.1_211_915],
                      [-7.781_247, -13.951_557, -3.740_592],
                  ]
              ]							)
          self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()							,expected_slice.numpy()							,atol=1E-2							)							)







     @slow
     def 			snake_case_      (				self							)      ->				Any:
          snake_case			:      List[Any]        =			TFEsmModel.from_pretrained("""facebook/esm2_t6_8M_UR50D"""							)

          snake_case			:      Tuple        =			tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]							)
          snake_case			:      Optional[Any]        =			model(__a							)[0]
          # compare the actual values for a slice.
          snake_case			:      Any        =			tf.constant(
              [
                  [
                      [0.14_443_092, 0.54_125_327, 0.3_247_739],
                      [0.30_340_484, 0.00_526_676, 0.31_077_722],
                      [0.32_278_043, -0.24_987_096, 0.3_414_628],
                  ]
              ]							)
          self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()							,expected_slice.numpy()							,atol=1E-4							)							)








"""simple docstring"""




import unittest

from transformers import is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
 from PIL import Image
else:






 class       UpperCAmelCase_    :



       @staticmethod
       def 		snake_case_   (	*A	:      Union[str, Any]	,      **A	:      Dict		):
            pass






@is_pipeline_test
@require_vision
class       UpperCAmelCase_    (							unittest.TestCase		):



      @require_torch
      def 		snake_case_   (	self	:      Union[str, Any]		):
           _UpperCAmelCase     :      Union[str, Any]														=       pipeline(
               model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification"	,      )
           _UpperCAmelCase     :      Union[str, Any]														=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
           _UpperCAmelCase     :      Tuple														=       image_classifier(A	,      candidate_labels=["a", "b", "c"]		)

           # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
           # python and torch versions.
           self.assertIn(
               nested_simplify(A		)	,      [
                   [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}],
                   [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "c"}, {"score": 0.333, "label": "b"}],
               ]	,      )

           _UpperCAmelCase     :      str														=       image_classifier([image] * 5	,      candidate_labels=["A", "B", "C"]	,      batch_size=2		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
               ]	,      )



      @require_tf
      def 		snake_case_   (	self	:      Any		):
           _UpperCAmelCase     :      List[str]														=       pipeline(
               model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification"	,      framework="tf"		)
           _UpperCAmelCase     :      int														=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
           _UpperCAmelCase     :      Union[str, Any]														=       image_classifier(A	,      candidate_labels=["a", "b", "c"]		)

           self.assertEqual(
               nested_simplify(A		)	,      [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}]	,      )

           _UpperCAmelCase     :      Optional[Any]														=       image_classifier([image] * 5	,      candidate_labels=["A", "B", "C"]	,      batch_size=2		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
               ]	,      )



      @slow
      @require_torch
      def 		snake_case_   (	self	:      Any		):
           _UpperCAmelCase     :      Optional[Any]														=       pipeline(
               task="zero-shot-image-classification"	,      model="openai/clip-vit-base-patch32"	,      )
           # This is an image of 2 cats with remotes and no planes
           _UpperCAmelCase     :      Dict														=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
           _UpperCAmelCase     :      List[Any]														=       image_classifier(A	,      candidate_labels=["cat", "plane", "remote"]		)

           self.assertEqual(
               nested_simplify(A		)	,      [
                   {"score": 0.511, "label": "remote"},
                   {"score": 0.485, "label": "cat"},
                   {"score": 0.004, "label": "plane"},
               ]	,      )

           _UpperCAmelCase     :      Tuple														=       image_classifier([image] * 5	,      candidate_labels=["cat", "plane", "remote"]	,      batch_size=2		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   [
                       {"score": 0.511, "label": "remote"},
                       {"score": 0.485, "label": "cat"},
                       {"score": 0.004, "label": "plane"},
                   ],
               ]
               * 5	,      )



      @slow
      @require_tf
      def 		snake_case_   (	self	:      Any		):
           _UpperCAmelCase     :      List[Any]														=       pipeline(
               task="zero-shot-image-classification"	,      model="openai/clip-vit-base-patch32"	,      framework="tf"		)
           # This is an image of 2 cats with remotes and no planes
           _UpperCAmelCase     :      Union[str, Any]														=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
           _UpperCAmelCase     :      Union[str, Any]														=       image_classifier(A	,      candidate_labels=["cat", "plane", "remote"]		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   {"score": 0.511, "label": "remote"},
                   {"score": 0.485, "label": "cat"},
                   {"score": 0.004, "label": "plane"},
               ]	,      )

           _UpperCAmelCase     :      Union[str, Any]														=       image_classifier([image] * 5	,      candidate_labels=["cat", "plane", "remote"]	,      batch_size=2		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   [
                       {"score": 0.511, "label": "remote"},
                       {"score": 0.485, "label": "cat"},
                       {"score": 0.004, "label": "plane"},
                   ],
               ]
               * 5	,      )
