"""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					:


    def __init__(       self						:	List[str] ,					__lowerCAmelCase						:	Dict ,					):
      _UpperCAmelCase    			=   parent
      _UpperCAmelCase    			=   13
      _UpperCAmelCase    			=   7
      _UpperCAmelCase    			=   True
      _UpperCAmelCase    			=   True
      _UpperCAmelCase    			=   True
      _UpperCAmelCase    			=   99
      _UpperCAmelCase    			=   32
      _UpperCAmelCase    			=   2
      _UpperCAmelCase    			=   4
      _UpperCAmelCase    			=   37
      _UpperCAmelCase    			=   '''gelu'''
      _UpperCAmelCase    			=   0.1
      _UpperCAmelCase    			=   0.1
      _UpperCAmelCase    			=   512
      _UpperCAmelCase    			=   16
      _UpperCAmelCase    			=   2
      _UpperCAmelCase    			=   0.02
      _UpperCAmelCase    			=   3
      _UpperCAmelCase    			=   4
      _UpperCAmelCase    			=   None


    def   lowerCAmelCase_	(       self						:	int       ):
      _UpperCAmelCase    			=   ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size       )

      _UpperCAmelCase    			=   None
      if self.use_input_mask:
        _UpperCAmelCase    			=   random_attention_mask([self.batch_size, self.seq_length]       )

      _UpperCAmelCase    			=   None
      _UpperCAmelCase    			=   None
      _UpperCAmelCase    			=   None
      if self.use_labels:
        _UpperCAmelCase    			=   ids_tensor([self.batch_size] ,					self.type_sequence_label_size       )
        _UpperCAmelCase    			=   ids_tensor([self.batch_size, self.seq_length] ,					self.num_labels       )
        _UpperCAmelCase    			=   ids_tensor([self.batch_size] ,					self.num_choices       )

      _UpperCAmelCase    			=   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   lowerCAmelCase_	(       self						:	int       ):
      (
          _UpperCAmelCase      
      )    			=   self.prepare_config_and_inputs()

      _UpperCAmelCase    			=   True
      _UpperCAmelCase    			=   floats_tensor([self.batch_size, self.seq_length, self.hidden_size]       )
      _UpperCAmelCase    			=   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   lowerCAmelCase_	(       self						:	Dict ,					__lowerCAmelCase						:	Optional[Any] ,					__lowerCAmelCase						:	Optional[int] ,					__lowerCAmelCase						:	List[Any] ,					__lowerCAmelCase						:	str ,					__lowerCAmelCase						:	List[Any] ,					__lowerCAmelCase						:	Tuple       ):
      _UpperCAmelCase    			=   TFEsmModel(config=__SCREAMING_SNAKE_CASE       )
      _UpperCAmelCase    			=   {'''input_ids''': input_ids, '''attention_mask''': input_mask}
      _UpperCAmelCase    			=   model(__SCREAMING_SNAKE_CASE       )

      _UpperCAmelCase    			=   [input_ids, input_mask]
      _UpperCAmelCase    			=   model(__SCREAMING_SNAKE_CASE       )

      _UpperCAmelCase    			=   model(__SCREAMING_SNAKE_CASE       )

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


    def   lowerCAmelCase_	(       self						:	Union[str, Any] ,					__lowerCAmelCase						:	List[Any] ,					__lowerCAmelCase						:	int ,					__lowerCAmelCase						:	Any ,					__lowerCAmelCase						:	List[Any] ,					__lowerCAmelCase						:	List[Any] ,					__lowerCAmelCase						:	Tuple ,					__lowerCAmelCase						:	Any ,					__lowerCAmelCase						:	List[str] ,					):
      _UpperCAmelCase    			=   True

      _UpperCAmelCase    			=   TFEsmModel(config=__SCREAMING_SNAKE_CASE       )
      _UpperCAmelCase    			=   {
          '''input_ids''': input_ids,
          '''attention_mask''': input_mask,
          '''encoder_hidden_states''': encoder_hidden_states,
          '''encoder_attention_mask''': encoder_attention_mask,
      }
      _UpperCAmelCase    			=   model(__SCREAMING_SNAKE_CASE       )

      _UpperCAmelCase    			=   [input_ids, input_mask]
      _UpperCAmelCase    			=   model(__SCREAMING_SNAKE_CASE ,					encoder_hidden_states=__SCREAMING_SNAKE_CASE       )

      # Also check the case where encoder outputs are not passed
      _UpperCAmelCase    			=   model(__SCREAMING_SNAKE_CASE ,					attention_mask=__SCREAMING_SNAKE_CASE       )

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


    def   lowerCAmelCase_	(       self						:	Dict ,					__lowerCAmelCase						:	Optional[int] ,					__lowerCAmelCase						:	List[str] ,					__lowerCAmelCase						:	List[Any] ,					__lowerCAmelCase						:	Union[str, Any] ,					__lowerCAmelCase						:	int ,					__lowerCAmelCase						:	Any       ):
      _UpperCAmelCase    			=   TFEsmForMaskedLM(config=__SCREAMING_SNAKE_CASE       )
      _UpperCAmelCase    			=   model([input_ids, input_mask]       )
      self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.vocab_size)       )


    def   lowerCAmelCase_	(       self						:	List[str] ,					__lowerCAmelCase						:	Optional[int] ,					__lowerCAmelCase						:	Optional[Any] ,					__lowerCAmelCase						:	Optional[Any] ,					__lowerCAmelCase						:	int ,					__lowerCAmelCase						:	Dict ,					__lowerCAmelCase						:	Any       ):
      _UpperCAmelCase    			=   self.num_labels
      _UpperCAmelCase    			=   TFEsmForTokenClassification(config=__SCREAMING_SNAKE_CASE       )
      _UpperCAmelCase    			=   {'''input_ids''': input_ids, '''attention_mask''': input_mask}
      _UpperCAmelCase    			=   model(__SCREAMING_SNAKE_CASE       )
      self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.num_labels)       )


    def   lowerCAmelCase_	(       self						:	Optional[Any]       ):
      _UpperCAmelCase    			=   self.prepare_config_and_inputs()
      (
          _UpperCAmelCase      
      )    			=   config_and_inputs
      _UpperCAmelCase    			=   {'''input_ids''': input_ids, '''attention_mask''': input_mask}
      return config, inputs_dict



@require_tf
class 							a					(		SCREAMING_SNAKE_CASE_						,				SCREAMING_SNAKE_CASE_						,				unittest.TestCase ):
    _snake_case  :	Union[str, Any]  				=				(
        (
            TFEsmModel,
            TFEsmForMaskedLM,
            TFEsmForSequenceClassification,
            TFEsmForTokenClassification,
        )
        if is_tf_available()
        else ()
    )
    _snake_case  :	str  				=				(
        {
            'feature-extraction': TFEsmModel,
            'fill-mask': TFEsmForMaskedLM,
            'text-classification': TFEsmForSequenceClassification,
            'token-classification': TFEsmForTokenClassification,
            'zero-shot': TFEsmForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )
    _snake_case  :	Optional[Any]  				=				False
    _snake_case  :	str  				=				False


    def   lowerCAmelCase_	(       self						:	Any       ):
      _UpperCAmelCase    			=   TFEsmModelTester(self       )
      _UpperCAmelCase    			=   ConfigTester(self ,					config_class=__SCREAMING_SNAKE_CASE ,					hidden_size=37       )


    def   lowerCAmelCase_	(       self						:	List[Any]       ):
      self.config_tester.run_common_tests()


    def   lowerCAmelCase_	(       self						:	Union[str, Any]       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE       )


    def   lowerCAmelCase_	(       self						:	List[str]       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_model_as_decoder(*__SCREAMING_SNAKE_CASE       )


    def   lowerCAmelCase_	(       self						:	List[Any]       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE       )


    def   lowerCAmelCase_	(       self						:	str       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE       )


    @slow
    def   lowerCAmelCase_	(       self						:	int       ):
      for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        _UpperCAmelCase    			=   TFEsmModel.from_pretrained(__SCREAMING_SNAKE_CASE       )
        self.assertIsNotNone(__SCREAMING_SNAKE_CASE       )


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


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


    def   lowerCAmelCase_	(       self						:	Union[str, Any]       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        _UpperCAmelCase    			=   model_class(__SCREAMING_SNAKE_CASE       )
        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    			=   model.get_bias()
          assert isinstance(__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE       )
          for k, v in name.items():
            assert isinstance(__SCREAMING_SNAKE_CASE ,					tf.Variable       )
        else:
          _UpperCAmelCase    			=   model.get_output_embeddings()
          assert x is None
          _UpperCAmelCase    			=   model.get_bias()
          assert name is None



@require_tf
class 							a					(		unittest.TestCase ):


    @slow
    def   lowerCAmelCase_	(       self						:	str       ):
      _UpperCAmelCase    			=   TFEsmForMaskedLM.from_pretrained("""facebook/esm2_t6_8M_UR50D"""       )

      _UpperCAmelCase    			=   tf.constant([[0, 1, 2, 3, 4, 5]]       )
      _UpperCAmelCase    			=   model(__SCREAMING_SNAKE_CASE       )[0]
      _UpperCAmelCase    			=   [1, 6, 33]
      self.assertEqual(list(output.numpy().shape       ) ,					__SCREAMING_SNAKE_CASE       )
      # compare the actual values for a slice.
      _UpperCAmelCase    			=   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   lowerCAmelCase_	(       self						:	Tuple       ):
      _UpperCAmelCase    			=   TFEsmModel.from_pretrained("""facebook/esm2_t6_8M_UR50D"""       )

      _UpperCAmelCase    			=   tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]       )
      _UpperCAmelCase    			=   model(__SCREAMING_SNAKE_CASE       )[0]
      # compare the actual values for a slice.
      _UpperCAmelCase    			=   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'''







# This script creates a super tiny model that is useful inside tests, when we just want to test that
# the machinery works, without needing to the check the quality of the outcomes.
#
# This version creates a tiny model through reduction of a normal pre-trained model, but keeping the
# full vocab, merges file, and thus also resulting in a larger model due to a large vocab size.
# This gives ~3MB in total for all files.
#
# If you want a 50 times smaller than this see `fsmt-make-super-tiny-model.py`, which is slightly more complicated
#
#
# It will be used then as "stas/tiny-wmt19-en-de"

# Build
from transformers import FSMTTokenizer, FSMTConfig, FSMTForConditionalGeneration

__lowerCAmelCase									=   'facebook/wmt19-en-de'
__lowerCAmelCase									=   FSMTTokenizer.from_pretrained(mname)
# get the correct vocab sizes, etc. from the master model
__lowerCAmelCase									=   FSMTConfig.from_pretrained(mname)
config.update(
    dict(
        d_model=4,
        encoder_layers=1,
        decoder_layers=1,
        encoder_ffn_dim=4,
        decoder_ffn_dim=4,
        encoder_attention_heads=1,
        decoder_attention_heads=1,
    )
)

__lowerCAmelCase									=   FSMTForConditionalGeneration(config)
print(F"""num of params {tiny_model.num_parameters()}""")

# Test
__lowerCAmelCase									=   tokenizer(['Making tiny model'], return_tensors='pt')
__lowerCAmelCase									=   tiny_model(**batch)

print('test output:', len(outputs.logits[0]))

# Save
__lowerCAmelCase									=   'tiny-wmt19-en-de'
tiny_model.half()  # makes it smaller
tiny_model.save_pretrained(mname_tiny)
tokenizer.save_pretrained(mname_tiny)

print(F"""Generated {mname_tiny}""")

# Upload
# transformers-cli upload tiny-wmt19-en-de




