
"""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				,   lowercase_				,   ):

     """simple docstring"""
     UpperCAmelCase_					:					Optional[int]			     =    parent
     UpperCAmelCase_					:					Any			     =    13
     UpperCAmelCase_					:					Optional[int]			     =    7
     UpperCAmelCase_					:					Tuple			     =    True
     UpperCAmelCase_					:					int			     =    True
     UpperCAmelCase_					:					Any			     =    True
     UpperCAmelCase_					:					Optional[int]			     =    99
     UpperCAmelCase_					:					Tuple			     =    32
     UpperCAmelCase_					:					Union[str, Any]			     =    2
     UpperCAmelCase_					:					Optional[Any]			     =    4
     UpperCAmelCase_					:					List[str]			     =    37
     UpperCAmelCase_					:					Optional[int]			     =    "gelu"
     UpperCAmelCase_					:					Union[str, Any]			     =    0.1
     UpperCAmelCase_					:					Optional[int]			     =    0.1
     UpperCAmelCase_					:					Dict			     =    512
     UpperCAmelCase_					:					str			     =    16
     UpperCAmelCase_					:					Any			     =    2
     UpperCAmelCase_					:					int			     =    0.02
     UpperCAmelCase_					:					str			     =    3
     UpperCAmelCase_					:					Optional[Any]			     =    4
     UpperCAmelCase_					:					List[str]			     =    None







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Any			     =    ids_tensor([self.batch_size, self.seq_length]				,   self.vocab_size  )

     UpperCAmelCase_					:					str			     =    None
     if self.use_input_mask:
        UpperCAmelCase_					:					Dict			     =    random_attention_mask([self.batch_size, self.seq_length]  )

     UpperCAmelCase_					:					List[str]			     =    None
     UpperCAmelCase_					:					Tuple			     =    None
     UpperCAmelCase_					:					Optional[Any]			     =    None
     if self.use_labels:
        UpperCAmelCase_					:					Tuple			     =    ids_tensor([self.batch_size]				,   self.type_sequence_label_size  )
        UpperCAmelCase_					:					Tuple			     =    ids_tensor([self.batch_size, self.seq_length]				,   self.num_labels  )
        UpperCAmelCase_					:					Optional[Any]			     =    ids_tensor([self.batch_size]				,   self.num_choices  )

     UpperCAmelCase_					:					Dict			     =    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        UpperCamelCase__					(		self  ):

     """simple docstring"""
     (
         (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   
     )					:					Union[str, Any]			     =    self.prepare_config_and_inputs()

     UpperCAmelCase_					:					List[str]			     =    True
     UpperCAmelCase_					:					Optional[Any]			     =    floats_tensor([self.batch_size, self.seq_length, self.hidden_size]  )
     UpperCAmelCase_					:					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        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_  ):

     """simple docstring"""
     UpperCAmelCase_					:					Union[str, Any]			     =    TFEsmModel(config=lowercase_  )
     UpperCAmelCase_					:					List[Any]			     =    {"input_ids": input_ids, "attention_mask": input_mask}
     UpperCAmelCase_					:					Union[str, Any]			     =    model(lowercase_  )

     UpperCAmelCase_					:					Optional[Any]			     =    [input_ids, input_mask]
     UpperCAmelCase_					:					Tuple			     =    model(lowercase_  )

     UpperCAmelCase_					:					List[str]			     =    model(lowercase_  )

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







  def        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   ):

     """simple docstring"""
     UpperCAmelCase_					:					Dict			     =    True

     UpperCAmelCase_					:					str			     =    TFEsmModel(config=lowercase_  )
     UpperCAmelCase_					:					str			     =    {
         "input_ids": input_ids,
         "attention_mask": input_mask,
         "encoder_hidden_states": encoder_hidden_states,
         "encoder_attention_mask": encoder_attention_mask,
     }
     UpperCAmelCase_					:					Any			     =    model(lowercase_  )

     UpperCAmelCase_					:					Optional[Any]			     =    [input_ids, input_mask]
     UpperCAmelCase_					:					str			     =    model(lowercase_				,   encoder_hidden_states=lowercase_  )

     # Also check the case where encoder outputs are not passed
     UpperCAmelCase_					:					Optional[int]			     =    model(lowercase_				,   attention_mask=lowercase_  )

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







  def        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_  ):

     """simple docstring"""
     UpperCAmelCase_					:					Dict			     =    TFEsmForMaskedLM(config=lowercase_  )
     UpperCAmelCase_					:					List[Any]			     =    model([input_ids, input_mask]  )
     self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.seq_length, self.vocab_size)  )







  def        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_  ):

     """simple docstring"""
     UpperCAmelCase_					:					List[Any]			     =    self.num_labels
     UpperCAmelCase_					:					Union[str, Any]			     =    TFEsmForTokenClassification(config=lowercase_  )
     UpperCAmelCase_					:					List[Any]			     =    {"input_ids": input_ids, "attention_mask": input_mask}
     UpperCAmelCase_					:					List[str]			     =    model(lowercase_  )
     self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.seq_length, self.num_labels)  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					int			     =    self.prepare_config_and_inputs()
     (
         (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   
     )					:					List[str]			     =    config_and_inputs
     UpperCAmelCase_					:					str			     =    {"input_ids": input_ids, "attention_mask": input_mask}
     return config, inputs_dict




@require_tf
class A_	(lowercase__				,lowercase__				,unittest.TestCase      ):




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







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					int			     =    TFEsmModelTester(self  )
     UpperCAmelCase_					:					Optional[Any]			     =    ConfigTester(self				,   config_class=lowercase_				,   hidden_size=37  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     self.config_tester.run_common_tests()







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					List[str]			     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Dict			     =    self.model_tester.prepare_config_and_inputs_for_decoder()
     self.model_tester.create_and_check_model_as_decoder(*lowercase_  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					str			     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*lowercase_  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					int			     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*lowercase_  )







  @slow
  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCAmelCase_					:					Optional[Any]			     =    TFEsmModel.from_pretrained(lowercase_  )
        self.assertIsNotNone(lowercase_  )







  @unittest.skip("Protein models do not support embedding resizing."  )
  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     pass







  @unittest.skip("Protein models do not support embedding resizing."  )
  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     pass







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_     ,   UpperCAmelCase_					:					List[Any]			     =    self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
        UpperCAmelCase_					:					Union[str, Any]			     =    model_class(lowercase_  )
        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
           UpperCAmelCase_					:					List[str]			     =    model.get_bias()
           assert isinstance(lowercase_				,   lowercase_  )
           for k, v in name.items():
              assert isinstance(lowercase_				,   tf.Variable  )
        else:
           UpperCAmelCase_					:					Union[str, Any]			     =    model.get_output_embeddings()
           assert x is None
           UpperCAmelCase_					:					Optional[int]			     =    model.get_bias()
           assert name is None




@require_tf
class A_	(unittest.TestCase      ):




  '''simple docstring'''







  @slow
  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Union[str, Any]			     =    TFEsmForMaskedLM.from_pretrained("facebook/esm2_t6_8M_UR50D"  )

     UpperCAmelCase_					:					int			     =    tf.constant([[0, 1, 2, 3, 4, 5]]  )
     UpperCAmelCase_					:					Dict			     =    model(lowercase_  )[0]
     UpperCAmelCase_					:					List[Any]			     =    [1, 6, 33]
     self.assertEqual(list(output.numpy().shape  )				,   lowercase_  )
     # compare the actual values for a slice.
     UpperCAmelCase_					:					Tuple			     =    tf.constant(
         [
             [
                 [8.92_15_18, -10.58_98_14, -6.4_67_13_07],
                 [-6.3_96_71_56, -13.91_13_77, -1.1_21_19_15],
                 [-7.78_12_47, -13.95_15_57, -3.74_05_92],
             ]
         ]  )
     self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()				,   expected_slice.numpy()				,   atol=1E-2  )  )







  @slow
  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Union[str, Any]			     =    TFEsmModel.from_pretrained("facebook/esm2_t6_8M_UR50D"  )

     UpperCAmelCase_					:					Optional[int]			     =    tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]  )
     UpperCAmelCase_					:					Union[str, Any]			     =    model(lowercase_  )[0]
     # compare the actual values for a slice.
     UpperCAmelCase_					:					Tuple			     =    tf.constant(
         [
             [
                 [0.14_44_30_92, 0.54_12_53_27, 0.3_24_77_39],
                 [0.30_34_04_84, 0.00_52_66_76, 0.31_07_77_22],
                 [0.32_27_80_43, -0.24_98_70_96, 0.3_41_46_28],
             ]
         ]  )
     self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()				,   expected_slice.numpy()				,   atol=1E-4  )  )




"""simple docstring"""





import argparse
import os
import re


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_dummies.py
_a		       =							'src/diffusers'

# Matches is_xxx_available()
_a		       =							re.compile(R'is\_([a-z_]*)_available\(\)')
# Matches from xxx import bla
_a		       =							re.compile(R'\s+from\s+\S*\s+import\s+([^\(\s].*)\n')


_a		       =							'\n{0} = None\n'

_a		       =							'\nclass {0}(metaclass=DummyObject):\n    _backends = {1}\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, {1})\n\n    @classmethod\n    def from_config(cls, *args, **kwargs):\n        requires_backends(cls, {1})\n\n    @classmethod\n    def from_pretrained(cls, *args, **kwargs):\n        requires_backends(cls, {1})\n'


_a		       =							'\ndef {0}(*args, **kwargs):\n    requires_backends({0}, {1})\n'







def 	__a     (		__lowerCamelCase  ):
   UpperCAmelCase_					:					int			     =    _re_backend.findall(__lowerCamelCase  )
   if len(__lowerCamelCase  ) == 0:
      return None

   return "_and_".join(__lowerCamelCase  )







def 	__a     (		):
   with open(os.path.join(__lowerCamelCase,		"__init__.py"  ),		"r",		encoding="utf-8",		newline="\n"  ) as f:
      UpperCAmelCase_					:					Optional[int]			     =    f.readlines()

   # Get to the point we do the actual imports for type checking
   UpperCAmelCase_					:					Union[str, Any]			     =    0
   UpperCAmelCase_					:					Optional[int]			     =    {}
   # Go through the end of the file
   while line_index < len(__lowerCamelCase  ):
      # If the line contains is_backend_available, we grab all objects associated with the `else` block
      UpperCAmelCase_					:					Union[str, Any]			     =    find_backend(lines[line_index]  )
      if backend is not None:
         while not lines[line_index].startswith("else:"  ):
            line_index += 1
         line_index += 1
         UpperCAmelCase_					:					List[str]			     =    []
         # Until we unindent, add backend objects to the list
         while line_index < len(__lowerCamelCase  ) and len(lines[line_index]  ) > 1:
            UpperCAmelCase_					:					Union[str, Any]			     =    lines[line_index]
            UpperCAmelCase_					:					Optional[Any]			     =    _re_single_line_import.search(__lowerCamelCase  )
            if single_line_import_search is not None:
               objects.extend(single_line_import_search.groups()[0].split(", "  )  )
            elif line.startswith(" " * 8  ):
               objects.append(line[8:-2]  )
            line_index += 1

         if len(__lowerCamelCase  ) > 0:
            UpperCAmelCase_					:					Optional[int]			     =    objects
      else:
         line_index += 1

   return backend_specific_objects







def 	__a     (		__lowerCamelCase,		__lowerCamelCase  ):
   if name.isupper():
      return DUMMY_CONSTANT.format(__lowerCamelCase  )
   elif name.islower():
      return DUMMY_FUNCTION.format(__lowerCamelCase,		__lowerCamelCase  )
   else:
      return DUMMY_CLASS.format(__lowerCamelCase,		__lowerCamelCase  )







def 	__a     (		__lowerCamelCase=None  ):
   if backend_specific_objects is None:
      UpperCAmelCase_					:					Tuple			     =    read_init()
   # For special correspondence backend to module name as used in the function requires_modulename
   UpperCAmelCase_					:					str			     =    {}

   for backend, objects in backend_specific_objects.items():
      UpperCAmelCase_					:					int			     =    "[" + ", ".join(f"""\"{b}\"""" for b in backend.split("_and_"  )  ) + "]"
      UpperCAmelCase_					:					Dict			     =    "# This file is autogenerated by the command `make fix-copies`, do not edit.\n"
      dummy_file += "from ..utils import DummyObject, requires_backends\n\n"
      dummy_file += "\n".join([create_dummy_object(__lowerCamelCase,		__lowerCamelCase  ) for o in objects]  )
      UpperCAmelCase_					:					int			     =    dummy_file

   return dummy_files







def 	__a     (		__lowerCamelCase=False  ):
   UpperCAmelCase_					:					Optional[Any]			     =    create_dummy_files()
   # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py
   UpperCAmelCase_					:					Union[str, Any]			     =    {"torch": "pt"}

   # Locate actual dummy modules and read their content.
   UpperCAmelCase_					:					List[str]			     =    os.path.join(__lowerCamelCase,		"utils"  )
   UpperCAmelCase_					:					Optional[int]			     =    {
       backend: os.path.join(__lowerCamelCase,		f"""dummy_{short_names.get(__lowerCamelCase,		__lowerCamelCase  )}_objects.py"""  )
       for backend in dummy_files.keys()
   }

   UpperCAmelCase_					:					Any			     =    {}
   for backend, file_path in dummy_file_paths.items():
      if os.path.isfile(__lowerCamelCase  ):
         with open(__lowerCamelCase,		"r",		encoding="utf-8",		newline="\n"  ) as f:
            UpperCAmelCase_					:					Optional[int]			     =    f.read()
      else:
         UpperCAmelCase_					:					Any			     =    ""

   for backend in dummy_files.keys():
      if dummy_files[backend] != actual_dummies[backend]:
         if overwrite:
            print(
                f"""Updating diffusers.utils.dummy_{short_names.get(__lowerCamelCase,		__lowerCamelCase  )}_objects.py as the main """
                "__init__ has new objects."  )
            with open(dummy_file_paths[backend],		"w",		encoding="utf-8",		newline="\n"  ) as f:
               f.write(dummy_files[backend]  )
         else:
            raise ValueError(
                "The main __init__ has objects that are not present in "
                f"""diffusers.utils.dummy_{short_names.get(__lowerCamelCase,		__lowerCamelCase  )}_objects.py. Run `make fix-copies` """
                "to fix this."  )


if __name__ == "__main__":
    _a		       =							argparse.ArgumentParser()
    parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.')
    _a		       =							parser.parse_args()

    check_dummies(args.fix_and_overwrite)


