import unittest

from transformers import EsmConfig, is_torch_available
from transformers.testing_utils import TestCasePlus, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import EsmForMaskedLM, EsmForSequenceClassification, EsmForTokenClassification, EsmModel
    from transformers.models.esm.modeling_esm import (
        ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
        EsmEmbeddings,
        create_position_ids_from_input_ids,
    )




class 	__snake_case	:





      '''simple docstring'''






      def __init__(					self     :				Optional[int]    ,  _UpperCamelCase     :				Any    ,  _UpperCamelCase     :				List[str]=13    ,  _UpperCamelCase     :				Dict=7    ,  _UpperCamelCase     :				Union[str, Any]=False    ,  _UpperCamelCase     :				Any=True    ,  _UpperCamelCase     :				Dict=False    ,  _UpperCamelCase     :				Optional[Any]=True    ,  _UpperCamelCase     :				Tuple=33    ,  _UpperCamelCase     :				Dict=32    ,  _UpperCamelCase     :				Optional[int]=5    ,  _UpperCamelCase     :				Tuple=4    ,  _UpperCamelCase     :				Tuple=37    ,  _UpperCamelCase     :				Optional[Any]="gelu"    ,  _UpperCamelCase     :				Optional[Any]=0.1    ,  _UpperCamelCase     :				Any=0.1    ,  _UpperCamelCase     :				Tuple=512    ,  _UpperCamelCase     :				int=16    ,  _UpperCamelCase     :				Any=2    ,  _UpperCamelCase     :				Tuple=0.0_2    ,  _UpperCamelCase     :				Optional[Any]=3    ,  _UpperCamelCase     :				int=4    ,  _UpperCamelCase     :				Any=None    ,  )				->int:


          """simple docstring"""


          _lowerCamelCase     :		List[str] 						=						parent
          _lowerCamelCase     :		Tuple 						=						batch_size
          _lowerCamelCase     :		List[Any] 						=						seq_length
          _lowerCamelCase     :		Optional[Any] 						=						is_training
          _lowerCamelCase     :		str 						=						use_input_mask
          _lowerCamelCase     :		int 						=						use_token_type_ids
          _lowerCamelCase     :		Optional[int] 						=						use_labels
          _lowerCamelCase     :		Optional[Any] 						=						vocab_size
          _lowerCamelCase     :		int 						=						hidden_size
          _lowerCamelCase     :		Union[str, Any] 						=						num_hidden_layers
          _lowerCamelCase     :		Dict 						=						num_attention_heads
          _lowerCamelCase     :		Any 						=						intermediate_size
          _lowerCamelCase     :		Optional[Any] 						=						hidden_act
          _lowerCamelCase     :		Tuple 						=						hidden_dropout_prob
          _lowerCamelCase     :		str 						=						attention_probs_dropout_prob
          _lowerCamelCase     :		Any 						=						max_position_embeddings
          _lowerCamelCase     :		Optional[int] 						=						type_vocab_size
          _lowerCamelCase     :		Tuple 						=						type_sequence_label_size
          _lowerCamelCase     :		Union[str, Any] 						=						initializer_range
          _lowerCamelCase     :		List[str] 						=						num_labels
          _lowerCamelCase     :		Any 						=						num_choices
          _lowerCamelCase     :		Optional[Any] 						=						scope



      def 	_SCREAMING_SNAKE_CASE    (					self     :				str)				->int:


          """simple docstring"""


          _lowerCamelCase     :		List[str] 						=						ids_tensor([self.batch_size, self.seq_length]    ,  self.vocab_size)

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

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

          _lowerCamelCase     :		Optional[Any] 						=						self.get_config()

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



      def 	_SCREAMING_SNAKE_CASE    (					self     :				int)				->Tuple:


          """simple docstring"""


          return EsmConfig(
              vocab_size=self.vocab_size    ,  hidden_size=self.hidden_size    ,  pad_token_id=1    ,  num_hidden_layers=self.num_hidden_layers    ,  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    ,  )



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Any    ,  _UpperCamelCase     :				Union[str, Any]    ,  _UpperCamelCase     :				Any    ,  _UpperCamelCase     :				Dict    ,  _UpperCamelCase     :				Optional[int]    ,  _UpperCamelCase     :				List[Any]    ,  _UpperCamelCase     :				Optional[Any])				->Union[str, Any]:


          """simple docstring"""


          _lowerCamelCase     :		List[str] 						=						EsmModel(config=lowerCAmelCase_)
          model.to(lowerCAmelCase_)
          model.eval()
          _lowerCamelCase     :		str 						=						model(lowerCAmelCase_    ,  attention_mask=lowerCAmelCase_)
          _lowerCamelCase     :		List[Any] 						=						model(lowerCAmelCase_)
          _lowerCamelCase     :		Dict 						=						model(lowerCAmelCase_)

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



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Any    ,  _UpperCamelCase     :				Union[str, Any]    ,  _UpperCamelCase     :				List[str]    ,  _UpperCamelCase     :				List[Any]    ,  _UpperCamelCase     :				List[Any]    ,  _UpperCamelCase     :				Tuple    ,  _UpperCamelCase     :				Dict)				->Any:


          """simple docstring"""


          _lowerCamelCase     :		Tuple 						=						EsmForMaskedLM(config=lowerCAmelCase_)
          model.to(lowerCAmelCase_)
          model.eval()
          _lowerCamelCase     :		Optional[int] 						=						model(lowerCAmelCase_    ,  attention_mask=lowerCAmelCase_    ,  labels=lowerCAmelCase_)
          self.parent.assertEqual(result.logits.shape    ,  (self.batch_size, self.seq_length, self.vocab_size))



      def 	_SCREAMING_SNAKE_CASE    (					self     :				str    ,  _UpperCamelCase     :				Tuple    ,  _UpperCamelCase     :				Optional[int]    ,  _UpperCamelCase     :				int    ,  _UpperCamelCase     :				List[Any]    ,  _UpperCamelCase     :				int    ,  _UpperCamelCase     :				Tuple)				->str:


          """simple docstring"""


          _lowerCamelCase     :		str 						=						self.num_labels
          _lowerCamelCase     :		Tuple 						=						EsmForTokenClassification(config=lowerCAmelCase_)
          model.to(lowerCAmelCase_)
          model.eval()
          _lowerCamelCase     :		Optional[Any] 						=						model(lowerCAmelCase_    ,  attention_mask=lowerCAmelCase_    ,  labels=lowerCAmelCase_)
          self.parent.assertEqual(result.logits.shape    ,  (self.batch_size, self.seq_length, self.num_labels))




      def 	_SCREAMING_SNAKE_CASE    (					self     :				str)				->Optional[int]:


          """simple docstring"""


          _lowerCamelCase     :		str 						=						self.prepare_config_and_inputs()
          (
              (
              _lowerCamelCase    
          )						,							(
              _lowerCamelCase    
          )						,							(
              _lowerCamelCase    
          )						,							(
              _lowerCamelCase    
          )						,							(
              _lowerCamelCase    
          )						,							(
              _lowerCamelCase    
          )						,							    
          )     :		Tuple 						=						config_and_inputs
          _lowerCamelCase     :		List[Any] 						=						{"""input_ids""": input_ids, """attention_mask""": input_mask}
          return config, inputs_dict





@require_torch
class 	__snake_case	( __UpperCAmelCase ,  __UpperCAmelCase ,  unittest.TestCase		):





      '''simple docstring'''






      _snake_case    		=       False

      _snake_case    		=       (
          (
              EsmForMaskedLM,
              EsmModel,
              EsmForSequenceClassification,
              EsmForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      _snake_case    		=       ()
      _snake_case    		=       (
          {
              'feature-extraction': EsmModel,
              'fill-mask': EsmForMaskedLM,
              'text-classification': EsmForSequenceClassification,
              'token-classification': EsmForTokenClassification,
              'zero-shot': EsmForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      _snake_case    		=       True



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[Any])				->Optional[Any]:


          """simple docstring"""


          _lowerCamelCase     :		Dict 						=						EsmModelTester(self)
          _lowerCamelCase     :		Any 						=						ConfigTester(self    ,  config_class=lowerCAmelCase_    ,  hidden_size=37)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Tuple)				->Optional[Any]:


          """simple docstring"""


          self.config_tester.run_common_tests()



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Dict)				->int:


          """simple docstring"""


          _lowerCamelCase     :		Optional[Any] 						=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowerCAmelCase_)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[int])				->Any:


          """simple docstring"""


          _lowerCamelCase     :		List[str] 						=						self.model_tester.prepare_config_and_inputs()
          for type in ["absolute", "relative_key", "relative_key_query"]:
              _lowerCamelCase     :		int 						=						type
              self.model_tester.create_and_check_model(*lowerCAmelCase_)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Union[str, Any])				->str:


          """simple docstring"""


          _lowerCamelCase     :		List[Any] 						=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase_)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				int)				->int:


          """simple docstring"""


          _lowerCamelCase     :		Any 						=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase_)



      @slow
      def 	_SCREAMING_SNAKE_CASE    (					self     :				Any)				->List[str]:


          """simple docstring"""


          for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _lowerCamelCase     :		str 						=						EsmModel.from_pretrained(lowerCAmelCase_)
              self.assertIsNotNone(lowerCAmelCase_)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[int])				->List[Any]:


          """simple docstring"""


          _lowerCamelCase     :		Union[str, Any] 						=						self.model_tester.prepare_config_and_inputs()[0]
          _lowerCamelCase     :		Optional[Any] 						=						EsmEmbeddings(config=lowerCAmelCase_)

          _lowerCamelCase     :		int 						=						torch.as_tensor([[12, 31, 13, model.padding_idx]])
          _lowerCamelCase     :		Union[str, Any] 						=						torch.as_tensor(
              [
                  [
                      0 + model.padding_idx + 1,
                      1 + model.padding_idx + 1,
                      2 + model.padding_idx + 1,
                      model.padding_idx,
                  ]
              ])
          _lowerCamelCase     :		Tuple 						=						create_position_ids_from_input_ids(lowerCAmelCase_    ,  model.padding_idx)
          self.assertEqual(position_ids.shape    ,  expected_positions.shape)
          self.assertTrue(torch.all(torch.eq(lowerCAmelCase_    ,  lowerCAmelCase_)))



      def 	_SCREAMING_SNAKE_CASE    (					self     :				str)				->List[str]:


          """simple docstring"""


          _lowerCamelCase     :		List[str] 						=						self.model_tester.prepare_config_and_inputs()[0]
          _lowerCamelCase     :		int 						=						EsmEmbeddings(config=lowerCAmelCase_)

          _lowerCamelCase     :		List[str] 						=						torch.empty(2    ,  4    ,  30)
          _lowerCamelCase     :		List[str] 						=						[
              0 + embeddings.padding_idx + 1,
              1 + embeddings.padding_idx + 1,
              2 + embeddings.padding_idx + 1,
              3 + embeddings.padding_idx + 1,
          ]
          _lowerCamelCase     :		List[Any] 						=						torch.as_tensor([expected_single_positions, expected_single_positions])
          _lowerCamelCase     :		Optional[int] 						=						embeddings.create_position_ids_from_inputs_embeds(lowerCAmelCase_)
          self.assertEqual(position_ids.shape    ,  expected_positions.shape)
          self.assertTrue(torch.all(torch.eq(lowerCAmelCase_    ,  lowerCAmelCase_)))



      @unittest.skip("""Esm does not support embedding resizing""")
      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[str])				->Optional[Any]:


          """simple docstring"""


          pass



      @unittest.skip("""Esm does not support embedding resizing""")
      def 	_SCREAMING_SNAKE_CASE    (					self     :				Any)				->int:


          """simple docstring"""


          pass




      @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""")
      def 	_SCREAMING_SNAKE_CASE    (					self     :				str)				->Optional[int]:


          """simple docstring"""


          pass





@require_torch
class 	__snake_case	( __UpperCAmelCase		):





      '''simple docstring'''






      @slow
      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[Any])				->List[str]:


          """simple docstring"""


          with torch.no_grad():
              _lowerCamelCase     :		int 						=						EsmForMaskedLM.from_pretrained("""facebook/esm2_t6_8M_UR50D""")
              model.eval()
              _lowerCamelCase     :		Dict 						=						torch.tensor([[0, 1, 2, 3, 4, 5]])
              _lowerCamelCase     :		Union[str, Any] 						=						model(lowerCAmelCase_)[0]

              _lowerCamelCase     :		str 						=						33

              _lowerCamelCase     :		Optional[Any] 						=						torch.Size((1, 6, vocab_size))
              self.assertEqual(output.shape    ,  lowerCAmelCase_)

              _lowerCamelCase     :		Any 						=						torch.tensor(
                  [[[8.9_2_1_5, -1_0.5_8_9_8, -6.4_6_7_1], [-6.3_9_6_7, -1_3.9_1_1_4, -1.1_2_1_2], [-7.7_8_1_2, -1_3.9_5_1_6, -3.7_4_0_6]]])
              self.assertTrue(torch.allclose(output[:, :3, :3]    ,  lowerCAmelCase_    ,  atol=1E-4))




      @slow
      def 	_SCREAMING_SNAKE_CASE    (					self     :				Tuple)				->Optional[int]:


          """simple docstring"""


          with torch.no_grad():
              _lowerCamelCase     :		List[str] 						=						EsmModel.from_pretrained("""facebook/esm2_t6_8M_UR50D""")
              model.eval()

              _lowerCamelCase     :		Tuple 						=						torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]])
              _lowerCamelCase     :		Optional[Any] 						=						model(lowerCAmelCase_)[0]
              # compare the actual values for a slice.
              _lowerCamelCase     :		str 						=						torch.tensor(
                  [[[0.1_4_4_4, 0.5_4_1_3, 0.3_2_4_8], [0.3_0_3_4, 0.0_0_5_3, 0.3_1_0_8], [0.3_2_2_8, -0.2_4_9_9, 0.3_4_1_5]]])
              self.assertTrue(torch.allclose(output[:, :3, :3]    ,  lowerCAmelCase_    ,  atol=1E-4))





import argparse
from pathlib import Path

import torch
from packaging import version
from torch.onnx import export

from diffusers import AutoencoderKL


lowerCAmelCase			:							Tuple     =version.parse(version.parse(torch.__version__).base_version) < version.parse("1.11")







def 						A__  (					__A   , __A   , __A   , __A   , __A   , __A   , __A   , __A=False   , ):







    '''simple docstring'''
    output_path.parent.mkdir(parents=__A   , exist_ok=__A	)
    # PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11,
    # so we check the torch version for backwards compatibility
    if is_torch_less_than_1_11:
        export(
            __A   , __A   , f=output_path.as_posix()   , input_names=__A   , output_names=__A   , dynamic_axes=__A   , do_constant_folding=__A   , use_external_data_format=__A   , enable_onnx_checker=__A   , opset_version=__A   , )
    else:
        export(
            __A   , __A   , f=output_path.as_posix()   , input_names=__A   , output_names=__A   , dynamic_axes=__A   , do_constant_folding=__A   , opset_version=__A   , )







@torch.no_grad()
def 						A__  (					__A   , __A   , __A   , __A = False	):







    '''simple docstring'''
    _lowerCamelCase     :		Tuple 						=						torch.floataa if fpaa else torch.floataa
    if fpaa and torch.cuda.is_available():
        _lowerCamelCase     :		str 						=						"""cuda"""
    elif fpaa and not torch.cuda.is_available():
        raise ValueError("""`float16` model export is only supported on GPUs with CUDA"""	)
    else:
        _lowerCamelCase     :		List[str] 						=						"""cpu"""
    _lowerCamelCase     :		Dict 						=						Path(__A	)

    # VAE DECODER
    _lowerCamelCase     :		Optional[Any] 						=						AutoencoderKL.from_pretrained(model_path + """/vae"""	)
    _lowerCamelCase     :		List[str] 						=						vae_decoder.config.latent_channels
    # forward only through the decoder part
    _lowerCamelCase     :		Tuple 						=						vae_decoder.decode
    onnx_export(
        __A   , model_args=(
            torch.randn(1   , __A   , 25   , 25	).to(device=__A   , dtype=__A	),
            False,
        )   , output_path=output_path / """vae_decoder""" / """model.onnx"""   , ordered_input_names=["""latent_sample""", """return_dict"""]   , output_names=["""sample"""]   , dynamic_axes={
            """latent_sample""": {0: """batch""", 1: """channels""", 2: """height""", 3: """width"""},
        }   , opset=__A   , )
    del vae_decoder


if __name__ == "__main__":
    lowerCAmelCase			:							Optional[int]     =argparse.ArgumentParser()

    parser.add_argument(
        "--model_path",
        type=str,
        required=True,
        help="Path to the `diffusers` checkpoint to convert (either a local directory or on the Hub).",
    )

    parser.add_argument("--output_path", type=str, required=True, help="Path to the output model.")
    parser.add_argument(
        "--opset",
        default=14,
        type=int,
        help="The version of the ONNX operator set to use.",
    )
    parser.add_argument("--fp16", action="store_true", default=False, help="Export the models in `float16` mode")

    lowerCAmelCase			:							Optional[Any]     =parser.parse_args()
    print(args.output_path)
    convert_models(args.model_path, args.output_path, args.opset, args.fpaa)
    print("SD: Done: ONNX")




