

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.models.esm.modeling_esmfold import EsmForProteinFolding
class  _A		:

  def __init__(				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE=13 ,					_SCREAMING_SNAKE_CASE=7 ,					_SCREAMING_SNAKE_CASE=False ,					_SCREAMING_SNAKE_CASE=True ,					_SCREAMING_SNAKE_CASE=False ,					_SCREAMING_SNAKE_CASE=False ,					_SCREAMING_SNAKE_CASE=19 ,					_SCREAMING_SNAKE_CASE=32 ,					_SCREAMING_SNAKE_CASE=5 ,					_SCREAMING_SNAKE_CASE=4 ,					_SCREAMING_SNAKE_CASE=37 ,					_SCREAMING_SNAKE_CASE="gelu" ,					_SCREAMING_SNAKE_CASE=0.1 ,					_SCREAMING_SNAKE_CASE=0.1 ,					_SCREAMING_SNAKE_CASE=512 ,					_SCREAMING_SNAKE_CASE=16 ,					_SCREAMING_SNAKE_CASE=2 ,					_SCREAMING_SNAKE_CASE=0.02 ,					_SCREAMING_SNAKE_CASE=3 ,					_SCREAMING_SNAKE_CASE=4 ,					_SCREAMING_SNAKE_CASE=None ,					):
        SCREAMING_SNAKE_CASE_				:  int           =     parent
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     batch_size
        SCREAMING_SNAKE_CASE_				:  Tuple           =     seq_length
        SCREAMING_SNAKE_CASE_				:  int           =     is_training
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     use_input_mask
        SCREAMING_SNAKE_CASE_				:  Dict           =     use_token_type_ids
        SCREAMING_SNAKE_CASE_				:  List[str]           =     use_labels
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     vocab_size
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     hidden_size
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     num_hidden_layers
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     num_attention_heads
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     intermediate_size
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     hidden_act
        SCREAMING_SNAKE_CASE_				:  Dict           =     hidden_dropout_prob
        SCREAMING_SNAKE_CASE_				:  str           =     attention_probs_dropout_prob
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     max_position_embeddings
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     type_vocab_size
        SCREAMING_SNAKE_CASE_				:  List[str]           =     type_sequence_label_size
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     initializer_range
        SCREAMING_SNAKE_CASE_				:  str           =     num_labels
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     num_choices
        SCREAMING_SNAKE_CASE_				:  str           =     scope

  def  UpperCAmelCase    (				self					):
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size					)

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

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

        SCREAMING_SNAKE_CASE_				:  Dict           =     self.get_config()

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

  def  UpperCAmelCase    (				self					):
        SCREAMING_SNAKE_CASE_				:  str           =     EsmConfig(
            vocab_size=33 ,					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 ,					is_folding_model=_SCREAMING_SNAKE_CASE ,					esmfold_config={'trunk': {'num_blocks': 2}, 'fp16_esm': False} ,					)
        return config

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     EsmForProteinFolding(config=_SCREAMING_SNAKE_CASE					).float()
        model.to(_SCREAMING_SNAKE_CASE					)
        model.eval()
        SCREAMING_SNAKE_CASE_				:  Dict           =     model(_SCREAMING_SNAKE_CASE ,					attention_mask=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Dict           =     model(_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Dict           =     model(_SCREAMING_SNAKE_CASE					)

        self.parent.assertEqual(result.positions.shape ,					(8, self.batch_size, self.seq_length, 14, 3)					)
        self.parent.assertEqual(result.angles.shape ,					(8, self.batch_size, self.seq_length, 7, 2)					)



  def  UpperCAmelCase    (				self					):
        SCREAMING_SNAKE_CASE_				:  Dict           =     self.prepare_config_and_inputs()
        (
            SCREAMING_SNAKE_CASE_
        )				:  str           =     config_and_inputs
        SCREAMING_SNAKE_CASE_				:  Dict           =     {"""input_ids""": input_ids, """attention_mask""": input_mask}
        return config, inputs_dict

@require_torch
class  _A		(  A_   ,     A_   ,     unittest.TestCase):
  SCREAMING_SNAKE_CASE    :      str       =  False

  SCREAMING_SNAKE_CASE    :      Optional[int]       =  (EsmForProteinFolding,) if is_torch_available() else ()
  SCREAMING_SNAKE_CASE    :      Optional[Any]       =  ()
  SCREAMING_SNAKE_CASE    :      Any       =  {} if is_torch_available() else {}
  SCREAMING_SNAKE_CASE    :      Any       =  False

  def  UpperCAmelCase    (				self					):
        SCREAMING_SNAKE_CASE_				:  List[str]           =     EsmFoldModelTester(self					)
        SCREAMING_SNAKE_CASE_				:  str           =     ConfigTester(self ,					config_class=_SCREAMING_SNAKE_CASE ,					hidden_size=37					)

  def  UpperCAmelCase    (				self					):
        self.config_tester.run_common_tests()

  def  UpperCAmelCase    (				self					):
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE					)

  @unittest.skip('Does not support attention outputs'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('Esm does not support embedding resizing'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('Esm does not support embedding resizing'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('ESMFold does not support passing input embeds!'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('ESMFold does not support head pruning.'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('ESMFold does not support head pruning.'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('ESMFold does not support head pruning.'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('ESMFold does not support head pruning.'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('ESMFold does not support head pruning.'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('ESMFold does not output hidden states in the normal way.'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('ESMfold does not output hidden states in the normal way.'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('ESMFold only has one output format.'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('This test doesn\'t work for ESMFold and doesn\'t test core functionality'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('ESMFold does not support input chunking.'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('ESMFold doesn\'t respect you and it certainly doesn\'t respect your initialization arguments.'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('ESMFold doesn\'t support torchscript compilation.'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('ESMFold doesn\'t support torchscript compilation.'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('ESMFold doesn\'t support torchscript compilation.'					)
  def  UpperCAmelCase    (				self					):
        pass

  @unittest.skip('ESMFold doesn\'t support data parallel.'					)
  def  UpperCAmelCase    (				self					):
        pass



  @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'					)
  def  UpperCAmelCase    (				self					):
        pass

@require_torch
class  _A		(  A_):

  @slow
  def  UpperCAmelCase    (				self					):
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     EsmForProteinFolding.from_pretrained('facebook/esmfold_v1'					).float()
        model.eval()
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]					)
        SCREAMING_SNAKE_CASE_				:  Any           =     model(_SCREAMING_SNAKE_CASE					)["""positions"""]
        SCREAMING_SNAKE_CASE_				:  List[str]           =     torch.tensor([2.5828, 0.7993, -10.9334] ,					dtype=torch.floataa					)
        self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0] ,					_SCREAMING_SNAKE_CASE ,					atol=1e-4					)					)









import argparse

from transformers import TaConfig, TaForConditionalGeneration, load_tf_weights_in_ta
from transformers.utils import logging


logging.set_verbosity_info()

def    A_  (							a					,   a					,   a  ):

      """simple docstring"""



      SCREAMING_SNAKE_CASE_				:  int           =     TaConfig.from_json_file(a  )
      print(f"Building PyTorch model from configuration: {config}"  )
      SCREAMING_SNAKE_CASE_				:  Tuple           =     TaForConditionalGeneration(a  )

      # Load weights from tf checkpoint
      load_tf_weights_in_ta(a					,   a					,   a  )

      # Save pytorch-model
      print(f"Save PyTorch model to {pytorch_dump_path}"  )
      model.save_pretrained(a  )


if __name__ == "__main__":
    lowerCAmelCase     :   int						     =  argparse.ArgumentParser()
    # Required parameters
    parser.add_argument(
        '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.'
    )
    parser.add_argument(
        '--config_file',
        default=None,
        type=str,
        required=True,
        help=(
            'The config json file corresponding to the pre-trained T5 model. \nThis specifies the model architecture.'
        ),
    )
    parser.add_argument(
        '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
    )
    lowerCAmelCase     :   List[str]						     =  parser.parse_args()
    convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)






