





'''simple docstring'''


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		: Any     ,				lowercase		: Dict     ,				lowercase		: Union[str, Any]=13     ,				lowercase		: Union[str, Any]=7     ,				lowercase		: List[str]=False     ,				lowercase		: Tuple=True     ,				lowercase		: int=False     ,				lowercase		: str=False     ,				lowercase		: str=19     ,				lowercase		: Union[str, Any]=32     ,				lowercase		: int=5     ,				lowercase		: Dict=4     ,				lowercase		: List[str]=37     ,				lowercase		: Dict="gelu"     ,				lowercase		: Optional[int]=0.1     ,				lowercase		: Tuple=0.1     ,				lowercase		: Optional[Any]=512     ,				lowercase		: Optional[Any]=16     ,				lowercase		: Dict=2     ,				lowercase		: str=0.02     ,				lowercase		: Dict=3     ,				lowercase		: str=4     ,				lowercase		: Dict=None     ,				):





        """simple docstring"""




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





     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




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

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

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

        lowercase_      :int         =			self.get_config()

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





     def 							lowercase__				(			self		: Optional[int]						):





        """simple docstring"""




        lowercase_      :Dict         =			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=lowercase     ,				esmfold_config={"trunk": {"num_blocks": 2}, "fp16_esm": False}     ,				)
        return config





     def 							lowercase__				(			self		: List[Any]     ,				lowercase		: Any     ,				lowercase		: Union[str, Any]     ,				lowercase		: Tuple     ,				lowercase		: List[str]     ,				lowercase		: Any     ,				lowercase		: List[str]						):





        """simple docstring"""




        lowercase_      :Optional[Any]         =			EsmForProteinFolding(config=lowercase						).float()
        model.to(lowercase						)
        model.eval()
        lowercase_      :Tuple         =			model(lowercase     ,				attention_mask=lowercase						)
        lowercase_      :Tuple         =			model(lowercase						)
        lowercase_      :Optional[int]         =			model(lowercase						)

        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 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :str         =			self.prepare_config_and_inputs()
        (
            lowercase_							
        )      :str         =			config_and_inputs
        lowercase_      :Tuple         =			{"input_ids": input_ids, "attention_mask": input_mask}
        return config, inputs_dict







@require_torch
class 				a_       (    _lowerCAmelCase     ,						_lowerCAmelCase     ,						unittest.TestCase							):
     __A								=      False

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





     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :int         =			EsmFoldModelTester(self						)
        lowercase_      :Dict         =			ConfigTester(self     ,				config_class=lowercase     ,				hidden_size=37						)





     def 							lowercase__				(			self		: str						):





        """simple docstring"""




        self.config_tester.run_common_tests()





     def 							lowercase__				(			self		: int						):





        """simple docstring"""




        lowercase_      :Dict         =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowercase						)





     @unittest.skip("Does not support attention outputs"						)
     def 							lowercase__				(			self		: List[str]						):





        """simple docstring"""




        pass





     @unittest.skip
     def 							lowercase__				(			self		: int						):





        """simple docstring"""




        pass





     @unittest.skip("Esm does not support embedding resizing"						)
     def 							lowercase__				(			self		: List[str]						):





        """simple docstring"""




        pass





     @unittest.skip("Esm does not support embedding resizing"						)
     def 							lowercase__				(			self		: List[Any]						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold does not support passing input embeds!"						)
     def 							lowercase__				(			self		: Dict						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold does not support head pruning."						)
     def 							lowercase__				(			self		: List[str]						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold does not support head pruning."						)
     def 							lowercase__				(			self		: Dict						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold does not support head pruning."						)
     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold does not support head pruning."						)
     def 							lowercase__				(			self		: List[Any]						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold does not support head pruning."						)
     def 							lowercase__				(			self		: int						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold does not output hidden states in the normal way."						)
     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        pass





     @unittest.skip("ESMfold does not output hidden states in the normal way."						)
     def 							lowercase__				(			self		: Optional[int]						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold only has one output format."						)
     def 							lowercase__				(			self		: str						):





        """simple docstring"""




        pass





     @unittest.skip("This test doesn't work for ESMFold and doesn't test core functionality"						)
     def 							lowercase__				(			self		: Optional[int]						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold does not support input chunking."						)
     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold doesn't respect you and it certainly doesn't respect your initialization arguments."						)
     def 							lowercase__				(			self		: Any						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold doesn't support torchscript compilation."						)
     def 							lowercase__				(			self		: Any						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold doesn't support torchscript compilation."						)
     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold doesn't support torchscript compilation."						)
     def 							lowercase__				(			self		: Any						):





        """simple docstring"""




        pass





     @unittest.skip("ESMFold doesn't support data parallel."						)
     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        pass







     @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."						)
     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        pass







@require_torch
class 				a_       (    _lowerCAmelCase							):





     @slow
     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :Optional[int]         =			EsmForProteinFolding.from_pretrained("facebook/esmfold_v1"						).float()
        model.eval()
        lowercase_      :List[str]         =			torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]						)
        lowercase_      :Union[str, Any]         =			model(lowercase						)["positions"]
        lowercase_      :Any         =			torch.tensor([2.58_28, 0.79_93, -10.93_34]     ,				dtype=torch.floataa						)
        self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0]     ,				lowercase     ,				atol=1e-4						)						)









'''simple docstring'''


import tempfile

import torch

from diffusers import (
    DEISMultistepScheduler,
    DPMSolverMultistepScheduler,
    DPMSolverSinglestepScheduler,
    UniPCMultistepScheduler,
)

from .test_schedulers import SchedulerCommonTest
class 				a_       (    _lowerCAmelCase							):
     __A								=      (UniPCMultistepScheduler,)
     __A								=      (("num_inference_steps", 25),)





     def 							lowercase__				(			self		: Tuple     ,				**lowercase		: Optional[Any]						):





        """simple docstring"""




        lowercase_      :Any         =			{
            "num_train_timesteps": 1_000,
            "beta_start": 0.00_01,
            "beta_end": 0.02,
            "beta_schedule": "linear",
            "solver_order": 2,
            "solver_type": "bh2",
        }

        config.update(**lowercase						)
        return config





     def 							lowercase__				(			self		: Optional[Any]     ,				lowercase		: str=0     ,				**lowercase		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :Tuple         =			dict(self.forward_default_kwargs						)
        lowercase_      :Tuple         =			kwargs.pop("num_inference_steps"     ,				lowercase						)
        lowercase_      :Union[str, Any]         =			self.dummy_sample
        lowercase_      :Union[str, Any]         =			0.1 * sample
        lowercase_      :Any         =			[residual + 0.2, residual + 0.15, residual + 0.10]

        for scheduler_class in self.scheduler_classes:
           lowercase_      :Tuple         =			self.get_scheduler_config(**lowercase						)
           lowercase_      :Tuple         =			scheduler_class(**lowercase						)
           scheduler.set_timesteps(lowercase						)
           # copy over dummy past residuals
           lowercase_      :str         =			dummy_past_residuals[: scheduler.config.solver_order]

           with tempfile.TemporaryDirectory() as tmpdirname:
              scheduler.save_config(lowercase						)
              lowercase_      :str         =			scheduler_class.from_pretrained(lowercase						)
              new_scheduler.set_timesteps(lowercase						)
              # copy over dummy past residuals
              lowercase_      :List[Any]         =			dummy_past_residuals[: new_scheduler.config.solver_order]

           lowercase_      ,							lowercase_      :Tuple         =			sample, sample
           for t in range(lowercase     ,				time_step + scheduler.config.solver_order + 1						):
              lowercase_      :Optional[int]         =			scheduler.step(lowercase     ,				lowercase     ,				lowercase     ,				**lowercase						).prev_sample
              lowercase_      :Union[str, Any]         =			new_scheduler.step(lowercase     ,				lowercase     ,				lowercase     ,				**lowercase						).prev_sample

              assert torch.sum(torch.abs(output - new_output						)						) < 1e-5, "Scheduler outputs are not identical"





     def 							lowercase__				(			self		: str     ,				lowercase		: Any=0     ,				**lowercase		: str						):





        """simple docstring"""




        lowercase_      :Tuple         =			dict(self.forward_default_kwargs						)
        lowercase_      :Dict         =			kwargs.pop("num_inference_steps"     ,				lowercase						)
        lowercase_      :str         =			self.dummy_sample
        lowercase_      :Optional[int]         =			0.1 * sample
        lowercase_      :Optional[int]         =			[residual + 0.2, residual + 0.15, residual + 0.10]

        for scheduler_class in self.scheduler_classes:
           lowercase_      :Any         =			self.get_scheduler_config()
           lowercase_      :Tuple         =			scheduler_class(**lowercase						)
           scheduler.set_timesteps(lowercase						)

           # copy over dummy past residuals (must be after setting timesteps)
           lowercase_      :str         =			dummy_past_residuals[: scheduler.config.solver_order]

           with tempfile.TemporaryDirectory() as tmpdirname:
              scheduler.save_config(lowercase						)
              lowercase_      :int         =			scheduler_class.from_pretrained(lowercase						)
              # copy over dummy past residuals
              new_scheduler.set_timesteps(lowercase						)

              # copy over dummy past residual (must be after setting timesteps)
              lowercase_      :Optional[Any]         =			dummy_past_residuals[: new_scheduler.config.solver_order]

           lowercase_      :Dict         =			scheduler.step(lowercase     ,				lowercase     ,				lowercase     ,				**lowercase						).prev_sample
           lowercase_      :str         =			new_scheduler.step(lowercase     ,				lowercase     ,				lowercase     ,				**lowercase						).prev_sample

           assert torch.sum(torch.abs(output - new_output						)						) < 1e-5, "Scheduler outputs are not identical"





     def 							lowercase__				(			self		: List[str]     ,				lowercase		: Optional[int]=None     ,				**lowercase		: int						):





        """simple docstring"""




        if scheduler is None:
           lowercase_      :int         =			self.scheduler_classes[0]
           lowercase_      :Optional[Any]         =			self.get_scheduler_config(**lowercase						)
           lowercase_      :Optional[Any]         =			scheduler_class(**lowercase						)

        lowercase_      :str         =			self.scheduler_classes[0]
        lowercase_      :Optional[int]         =			self.get_scheduler_config(**lowercase						)
        lowercase_      :List[Any]         =			scheduler_class(**lowercase						)

        lowercase_      :str         =			10
        lowercase_      :List[str]         =			self.dummy_model()
        lowercase_      :Optional[int]         =			self.dummy_sample_deter
        scheduler.set_timesteps(lowercase						)

        for i, t in enumerate(scheduler.timesteps						):
           lowercase_      :int         =			model(lowercase     ,				lowercase						)
           lowercase_      :Dict         =			scheduler.step(lowercase     ,				lowercase     ,				lowercase						).prev_sample

        return sample





     def 							lowercase__				(			self		: int						):





        """simple docstring"""




        lowercase_      :str         =			dict(self.forward_default_kwargs						)

        lowercase_      :Union[str, Any]         =			kwargs.pop("num_inference_steps"     ,				lowercase						)

        for scheduler_class in self.scheduler_classes:
           lowercase_      :Optional[Any]         =			self.get_scheduler_config()
           lowercase_      :Optional[int]         =			scheduler_class(**lowercase						)

           lowercase_      :Union[str, Any]         =			self.dummy_sample
           lowercase_      :str         =			0.1 * sample

           if num_inference_steps is not None and hasattr(lowercase     ,				"set_timesteps"						):
              scheduler.set_timesteps(lowercase						)
           elif num_inference_steps is not None and not hasattr(lowercase     ,				"set_timesteps"						):
              lowercase_      :str         =			num_inference_steps

           # copy over dummy past residuals (must be done after set_timesteps)
           lowercase_      :Dict         =			[residual + 0.2, residual + 0.15, residual + 0.10]
           lowercase_      :Any         =			dummy_past_residuals[: scheduler.config.solver_order]

           lowercase_      :Optional[int]         =			scheduler.timesteps[5]
           lowercase_      :List[Any]         =			scheduler.timesteps[6]

           lowercase_      :Optional[Any]         =			scheduler.step(lowercase     ,				lowercase     ,				lowercase     ,				**lowercase						).prev_sample
           lowercase_      :List[str]         =			scheduler.step(lowercase     ,				lowercase     ,				lowercase     ,				**lowercase						).prev_sample

           self.assertEqual(output_a.shape     ,				sample.shape						)
           self.assertEqual(output_a.shape     ,				output_a.shape						)





     def 							lowercase__				(			self		: List[str]						):





        """simple docstring"""




        lowercase_      :List[Any]         =			UniPCMultistepScheduler(**self.get_scheduler_config()						)
        lowercase_      :Dict         =			self.full_loop(scheduler=lowercase						)
        lowercase_      :List[Any]         =			torch.mean(torch.abs(lowercase						)						)

        assert abs(result_mean.item() - 0.24_64						) < 1e-3

        lowercase_      :List[str]         =			DPMSolverSinglestepScheduler.from_config(scheduler.config						)
        lowercase_      :Tuple         =			DEISMultistepScheduler.from_config(scheduler.config						)
        lowercase_      :Optional[int]         =			DPMSolverMultistepScheduler.from_config(scheduler.config						)
        lowercase_      :Tuple         =			UniPCMultistepScheduler.from_config(scheduler.config						)

        lowercase_      :Tuple         =			self.full_loop(scheduler=lowercase						)
        lowercase_      :Tuple         =			torch.mean(torch.abs(lowercase						)						)

        assert abs(result_mean.item() - 0.24_64						) < 1e-3





     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        for timesteps in [25, 50, 100, 999, 1_000]:
           self.check_over_configs(num_train_timesteps=lowercase						)





     def 							lowercase__				(			self		: int						):





        """simple docstring"""




        self.check_over_configs(thresholding=lowercase						)
        for order in [1, 2, 3]:
           for solver_type in ["bh1", "bh2"]:
              for threshold in [0.5, 1.0, 2.0]:
                 for prediction_type in ["epsilon", "sample"]:
                    self.check_over_configs(
                        thresholding=lowercase     ,				prediction_type=lowercase     ,				sample_max_value=lowercase     ,				solver_order=lowercase     ,				solver_type=lowercase     ,				)





     def 							lowercase__				(			self		: List[str]						):





        """simple docstring"""




        for prediction_type in ["epsilon", "v_prediction"]:
           self.check_over_configs(prediction_type=lowercase						)





     def 							lowercase__				(			self		: Optional[int]						):





        """simple docstring"""




        for solver_type in ["bh1", "bh2"]:
           for order in [1, 2, 3]:
              for prediction_type in ["epsilon", "sample"]:
                 self.check_over_configs(
                     solver_order=lowercase     ,				solver_type=lowercase     ,				prediction_type=lowercase     ,				)
                 lowercase_      :Tuple         =			self.full_loop(
                     solver_order=lowercase     ,				solver_type=lowercase     ,				prediction_type=lowercase     ,				)
                 assert not torch.isnan(lowercase						).any(), "Samples have nan numbers"





     def 							lowercase__				(			self		: List[Any]						):





        """simple docstring"""




        self.check_over_configs(lower_order_final=lowercase						)
        self.check_over_configs(lower_order_final=lowercase						)





     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1_000]:
           self.check_over_forward(num_inference_steps=lowercase     ,				time_step=0						)





     def 							lowercase__				(			self		: Dict						):





        """simple docstring"""




        lowercase_      :List[str]         =			self.full_loop()
        lowercase_      :List[str]         =			torch.mean(torch.abs(lowercase						)						)

        assert abs(result_mean.item() - 0.24_64						) < 1e-3





     def 							lowercase__				(			self		: Any						):





        """simple docstring"""




        lowercase_      :Dict         =			self.full_loop(prediction_type="v_prediction"						)
        lowercase_      :Any         =			torch.mean(torch.abs(lowercase						)						)

        assert abs(result_mean.item() - 0.10_14						) < 1e-3





     def 							lowercase__				(			self		: Optional[Any]						):





        """simple docstring"""




        lowercase_      :Union[str, Any]         =			self.scheduler_classes[0]
        lowercase_      :str         =			self.get_scheduler_config(thresholding=lowercase     ,				dynamic_thresholding_ratio=0						)
        lowercase_      :Tuple         =			scheduler_class(**lowercase						)

        lowercase_      :Optional[int]         =			10
        lowercase_      :Optional[int]         =			self.dummy_model()
        lowercase_      :Any         =			self.dummy_sample_deter.half()
        scheduler.set_timesteps(lowercase						)

        for i, t in enumerate(scheduler.timesteps						):
           lowercase_      :Tuple         =			model(lowercase     ,				lowercase						)
           lowercase_      :Any         =			scheduler.step(lowercase     ,				lowercase     ,				lowercase						).prev_sample

        assert sample.dtype == torch.floataa







     def 							lowercase__				(			self		: List[Any]     ,				**lowercase		: Union[str, Any]						):





        """simple docstring"""




        for scheduler_class in self.scheduler_classes:
           lowercase_      :Optional[int]         =			self.get_scheduler_config(**lowercase						)
           lowercase_      :Dict         =			scheduler_class(**lowercase						)

           scheduler.set_timesteps(scheduler.config.num_train_timesteps						)
           assert len(scheduler.timesteps.unique()						) == scheduler.num_inference_steps


