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


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


       def 	_snake_case				(    self				)						->    Any:
              SCREAMING_SNAKE_CASE_							:				Dict		=ids_tensor([self.batch_size, self.seq_length]						,       self.vocab_size				)

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

              SCREAMING_SNAKE_CASE_							:				Dict		=None
              SCREAMING_SNAKE_CASE_							:				Optional[Any]		=None
              SCREAMING_SNAKE_CASE_							:				Any		=None
              if self.use_labels:
                     SCREAMING_SNAKE_CASE_							:				int		=ids_tensor([self.batch_size]						,       self.type_sequence_label_size				)
                     SCREAMING_SNAKE_CASE_							:				Optional[Any]		=ids_tensor([self.batch_size, self.seq_length]						,       self.num_labels				)
                     SCREAMING_SNAKE_CASE_							:				Optional[Any]		=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 	_snake_case				(    self				)						->    str:
              SCREAMING_SNAKE_CASE_							:				Any		=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=__A						,       esmfold_config={'''trunk''': {'''num_blocks''': 2}, '''fp16_esm''': False}						,       )
              return config


       def 	_snake_case				(    self						,       __A						,       __A						,       __A						,       __A						,       __A						,       __A				)						->    Dict:
              SCREAMING_SNAKE_CASE_							:				List[str]		=EsmForProteinFolding(config=__A				).float()
              model.to(__A				)
              model.eval()
              SCREAMING_SNAKE_CASE_							:				Optional[Any]		=model(__A						,       attention_mask=__A				)
              SCREAMING_SNAKE_CASE_							:				Dict		=model(__A				)
              SCREAMING_SNAKE_CASE_							:				Union[str, Any]		=model(__A				)

              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 	_snake_case				(    self				)						->    str:
              SCREAMING_SNAKE_CASE_							:				Dict		=self.prepare_config_and_inputs()
              (
                  (
                  SCREAMING_SNAKE_CASE_
              )	,     (
                  SCREAMING_SNAKE_CASE_
              )	,     (
                  SCREAMING_SNAKE_CASE_
              )	,     (
                  SCREAMING_SNAKE_CASE_
              )	,     (
                  SCREAMING_SNAKE_CASE_
              )	,     (
                  SCREAMING_SNAKE_CASE_
              )	,     
              )							:				Dict		=config_and_inputs
              SCREAMING_SNAKE_CASE_							:				Union[str, Any]		={'''input_ids''': input_ids, '''attention_mask''': input_mask}
              return config, inputs_dict






@require_torch
class 			lowercase_		(      A  ,      A  ,      unittest.TestCase       ):
       __lowerCamelCase				       =						False

       __lowerCamelCase				       =						(EsmForProteinFolding,) if is_torch_available() else ()
       __lowerCamelCase				       =						()
       __lowerCamelCase				       =						{} if is_torch_available() else {}
       __lowerCamelCase				       =						False


       def 	_snake_case				(    self				)						->    List[Any]:
              SCREAMING_SNAKE_CASE_							:				int		=EsmFoldModelTester(self				)
              SCREAMING_SNAKE_CASE_							:				List[str]		=ConfigTester(self						,       config_class=__A						,       hidden_size=37				)


       def 	_snake_case				(    self				)						->    str:
              self.config_tester.run_common_tests()


       def 	_snake_case				(    self				)						->    Any:
              SCREAMING_SNAKE_CASE_							:				int		=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*__A				)


       @unittest.skip('''Does not support attention outputs'''				)
       def 	_snake_case				(    self				)						->    str:
              pass


       @unittest.skip
       def 	_snake_case				(    self				)						->    Tuple:
              pass


       @unittest.skip('''Esm does not support embedding resizing'''				)
       def 	_snake_case				(    self				)						->    Any:
              pass


       @unittest.skip('''Esm does not support embedding resizing'''				)
       def 	_snake_case				(    self				)						->    Dict:
              pass


       @unittest.skip('''ESMFold does not support passing input embeds!'''				)
       def 	_snake_case				(    self				)						->    Optional[Any]:
              pass


       @unittest.skip('''ESMFold does not support head pruning.'''				)
       def 	_snake_case				(    self				)						->    List[str]:
              pass


       @unittest.skip('''ESMFold does not support head pruning.'''				)
       def 	_snake_case				(    self				)						->    Any:
              pass


       @unittest.skip('''ESMFold does not support head pruning.'''				)
       def 	_snake_case				(    self				)						->    Optional[int]:
              pass


       @unittest.skip('''ESMFold does not support head pruning.'''				)
       def 	_snake_case				(    self				)						->    Dict:
              pass


       @unittest.skip('''ESMFold does not support head pruning.'''				)
       def 	_snake_case				(    self				)						->    int:
              pass


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


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


       @unittest.skip('''ESMFold only has one output format.'''				)
       def 	_snake_case				(    self				)						->    Optional[Any]:
              pass


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


       @unittest.skip('''ESMFold does not support input chunking.'''				)
       def 	_snake_case				(    self				)						->    str:
              pass


       @unittest.skip('''ESMFold doesn\'t respect you and it certainly doesn\'t respect your initialization arguments.'''				)
       def 	_snake_case				(    self				)						->    List[str]:
              pass


       @unittest.skip('''ESMFold doesn\'t support torchscript compilation.'''				)
       def 	_snake_case				(    self				)						->    Union[str, Any]:
              pass


       @unittest.skip('''ESMFold doesn\'t support torchscript compilation.'''				)
       def 	_snake_case				(    self				)						->    Tuple:
              pass


       @unittest.skip('''ESMFold doesn\'t support torchscript compilation.'''				)
       def 	_snake_case				(    self				)						->    str:
              pass


       @unittest.skip('''ESMFold doesn\'t support data parallel.'''				)
       def 	_snake_case				(    self				)						->    List[Any]:
              pass





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






@require_torch
class 			lowercase_		(      A       ):


       @slow
       def 	_snake_case				(    self				)						->    Optional[Any]:
              SCREAMING_SNAKE_CASE_							:				int		=EsmForProteinFolding.from_pretrained('''facebook/esmfold_v1'''				).float()
              model.eval()
              SCREAMING_SNAKE_CASE_							:				Dict		=torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]				)
              SCREAMING_SNAKE_CASE_							:				str		=model(__A				)['''positions''']
              SCREAMING_SNAKE_CASE_							:				List[str]		=torch.tensor([2.5_828, 0.7_993, -10.9_334]						,       dtype=torch.floataa				)
              self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0]						,       __A						,       atol=1e-4				)				)


from typing import List, Optional, Tuple, Union

import torch

from ...models import UNetaDModel
from ...schedulers import KarrasVeScheduler
from ...utils import randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput


class 			lowercase_		(      A       ):
       __lowerCamelCase				       =						42
       __lowerCamelCase				       =						42


       def __init__(    self						,       __A						,       __A				)						->    List[str]:
              super().__init__()
              self.register_modules(unet=__A						,       scheduler=__A				)





       @torch.no_grad()
       def __call__(    self						,       __A = 1						,       __A = 50						,       __A = None						,       __A = "pil"						,       __A = True						,       **__A						,       )						->    Union[Tuple, ImagePipelineOutput]:
              SCREAMING_SNAKE_CASE_							:				Dict		=self.unet.config.sample_size
              SCREAMING_SNAKE_CASE_							:				Tuple		=(batch_size, 3, img_size, img_size)

              SCREAMING_SNAKE_CASE_							:				int		=self.unet

              # sample x_0 ~ N(0, sigma_0^2 * I)
              SCREAMING_SNAKE_CASE_							:				Tuple		=randn_tensor(__A						,       generator=__A						,       device=self.device				) * self.scheduler.init_noise_sigma

              self.scheduler.set_timesteps(__A				)

              for t in self.progress_bar(self.scheduler.timesteps				):
                     # here sigma_t == t_i from the paper
                     SCREAMING_SNAKE_CASE_							:				Union[str, Any]		=self.scheduler.schedule[t]
                     SCREAMING_SNAKE_CASE_							:				List[Any]		=self.scheduler.schedule[t - 1] if t > 0 else 0

                     # 1. Select temporarily increased noise level sigma_hat
                     # 2. Add new noise to move from sample_i to sample_hat
                     SCREAMING_SNAKE_CASE_	,     SCREAMING_SNAKE_CASE_							:				Dict		=self.scheduler.add_noise_to_input(__A						,       __A						,       generator=__A				)

                     # 3. Predict the noise residual given the noise magnitude `sigma_hat`
                     # The model inputs and output are adjusted by following eq. (213) in [1].
                     SCREAMING_SNAKE_CASE_							:				Tuple		=(sigma_hat / 2) * model((sample_hat + 1) / 2						,       sigma_hat / 2				).sample

                     # 4. Evaluate dx/dt at sigma_hat
                     # 5. Take Euler step from sigma to sigma_prev
                     SCREAMING_SNAKE_CASE_							:				List[Any]		=self.scheduler.step(__A						,       __A						,       __A						,       __A				)

                     if sigma_prev != 0:
                            # 6. Apply 2nd order correction
                            # The model inputs and output are adjusted by following eq. (213) in [1].
                            SCREAMING_SNAKE_CASE_							:				int		=(sigma_prev / 2) * model((step_output.prev_sample + 1) / 2						,       sigma_prev / 2				).sample
                            SCREAMING_SNAKE_CASE_							:				Dict		=self.scheduler.step_correct(
                                __A						,       __A						,       __A						,       __A						,       step_output.prev_sample						,       step_output['''derivative''']						,       )
                     SCREAMING_SNAKE_CASE_							:				Any		=step_output.prev_sample

              SCREAMING_SNAKE_CASE_							:				List[Any]		=(sample / 2 + 0.5).clamp(0						,       1				)
              SCREAMING_SNAKE_CASE_							:				Any		=sample.cpu().permute(0						,       2						,       3						,       1				).numpy()
              if output_type == "pil":
                     SCREAMING_SNAKE_CASE_							:				Optional[Any]		=self.numpy_to_pil(__A				)

              if not return_dict:
                     return (image,)

              return ImagePipelineOutput(images=__A				)
