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				,						UpperCAmelCase				,						UpperCAmelCase=1_3				,						UpperCAmelCase=7				,						UpperCAmelCase=False				,						UpperCAmelCase=True				,						UpperCAmelCase=False				,						UpperCAmelCase=False				,						UpperCAmelCase=1_9				,						UpperCAmelCase=3_2				,						UpperCAmelCase=5				,						UpperCAmelCase=4				,						UpperCAmelCase=3_7				,						UpperCAmelCase="gelu"				,						UpperCAmelCase=0.1				,						UpperCAmelCase=0.1				,						UpperCAmelCase=5_1_2				,						UpperCAmelCase=1_6				,						UpperCAmelCase=2				,						UpperCAmelCase=0.02				,						UpperCAmelCase=3				,						UpperCAmelCase=4				,						UpperCAmelCase=None				,						)			->   Dict:
								__a		 =       parent
								__a		 =       batch_size
								__a		 =       seq_length
								__a		 =       is_training
								__a		 =       use_input_mask
								__a		 =       use_token_type_ids
								__a		 =       use_labels
								__a		 =       vocab_size
								__a		 =       hidden_size
								__a		 =       num_hidden_layers
								__a		 =       num_attention_heads
								__a		 =       intermediate_size
								__a		 =       hidden_act
								__a		 =       hidden_dropout_prob
								__a		 =       attention_probs_dropout_prob
								__a		 =       max_position_embeddings
								__a		 =       type_vocab_size
								__a		 =       type_sequence_label_size
								__a		 =       initializer_range
								__a		 =       num_labels
								__a		 =       num_choices
								__a		 =       scope



			def __SCREAMING_SNAKE_CASE							(						self   )			->   str:
								__a		 =       ids_tensor([self.batch_size, self.seq_length]				,						self.vocab_size   )

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

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

								__a		 =       self.get_config()

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



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Any:
								__a		 =       EsmConfig(
								    vocab_size=3_3				,						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=UpperCAmelCase				,						esmfold_config={'trunk': {'num_blocks': 2}, 'fp16_esm': False}				,						)
								return config



			def __SCREAMING_SNAKE_CASE							(						self				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase   )			->   Dict:
								__a		 =       EsmForProteinFolding(config=UpperCAmelCase   ).float()
								model.to(UpperCAmelCase   )
								model.eval()
								__a		 =       model(UpperCAmelCase				,						attention_mask=UpperCAmelCase   )
								__a		 =       model(UpperCAmelCase   )
								__a		 =       model(UpperCAmelCase   )

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







			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[int]:
								__a		 =       self.prepare_config_and_inputs()
								(
								    (
								    __a
								) ,				(
								    __a
								) ,				(
								    __a
								) ,				(
								    __a
								) ,				(
								    __a
								) ,				(
								    __a
								) ,				
								)		 =       config_and_inputs
								__a		 =       {'input_ids': input_ids, 'attention_mask': input_mask}
								return config, inputs_dict






@require_torch
class 						a__							(       __snake_case				,						__snake_case				,						unittest.TestCase		):
			A__    :       Dict        =      False

			A__    :       Optional[int]        =      (EsmForProteinFolding,) if is_torch_available() else ()
			A__    :       Tuple        =      ()
			A__    :       Tuple        =      {} if is_torch_available() else {}
			A__    :       Dict        =      False



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[Any]:
								__a		 =       EsmFoldModelTester(self   )
								__a		 =       ConfigTester(self				,						config_class=UpperCAmelCase				,						hidden_size=3_7   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Union[str, Any]:
								self.config_tester.run_common_tests()



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[int]:
								__a		 =       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*UpperCAmelCase   )



			@unittest.skip('Does not support attention outputs'   )
			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[int]:
								pass



			@unittest.skip
			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[int]:
								pass



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



			@unittest.skip('Esm does not support embedding resizing'   )
			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[int]:
								pass



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



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



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



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



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



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



			@unittest.skip('ESMFold does not output hidden states in the normal way.'   )
			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[Any]:
								pass



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



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



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



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



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



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



			@unittest.skip('ESMFold doesn\'t support torchscript compilation.'   )
			def __SCREAMING_SNAKE_CASE							(						self   )			->   List[Any]:
								pass



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



			@unittest.skip('ESMFold doesn\'t support data parallel.'   )
			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[int]:
								pass







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






@require_torch
class 						a__							(       __snake_case		):



			@slow
			def __SCREAMING_SNAKE_CASE							(						self   )			->   Tuple:
								__a		 =       EsmForProteinFolding.from_pretrained('facebook/esmfold_v1'   ).float()
								model.eval()
								__a		 =       torch.tensor([[0, 6, 4, 1_3, 5, 4, 1_6, 1_2, 1_1, 7, 2]]   )
								__a		 =       model(UpperCAmelCase   )['positions']
								__a		 =       torch.tensor([2.5_828, 0.7_993, -10.9_334]				,						dtype=torch.floataa   )
								self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0]				,						UpperCAmelCase				,						atol=1e-4   )   )





from dataclasses import dataclass
from typing import Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, randn_tensor
from .scheduling_utils import SchedulerMixin


@dataclass
class 						a__							(       __snake_case		):
			A__    :       torch.FloatTensor
			A__    :       torch.FloatTensor
			A__    :       Optional[torch.FloatTensor]        =      None






class 						a__							(       __snake_case				,						__snake_case		):
			A__    :       Optional[Any]        =      2



			@register_to_config
			def __init__(						self				,						UpperCAmelCase = 0.02				,						UpperCAmelCase = 1_0_0				,						UpperCAmelCase = 1.007				,						UpperCAmelCase = 8_0				,						UpperCAmelCase = 0.05				,						UpperCAmelCase = 5_0				,						)			->   Optional[Any]:
								# standard deviation of the initial noise distribution
								__a		 =       sigma_max

								# setable values
								__a		 =       None
								__a		 =       None
								__a		 =       None  # sigma(t_i)



			def __SCREAMING_SNAKE_CASE							(						self				,						UpperCAmelCase				,						UpperCAmelCase = None   )			->   torch.FloatTensor:
								return sample



			def __SCREAMING_SNAKE_CASE							(						self				,						UpperCAmelCase				,						UpperCAmelCase = None   )			->   int:
								__a		 =       num_inference_steps
								__a		 =       np.arange(0				,						self.num_inference_steps   )[::-1].copy()
								__a		 =       torch.from_numpy(UpperCAmelCase   ).to(UpperCAmelCase   )
								__a		 =       [
								    (
								        self.config.sigma_max**2
								        * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1))
								    )
								    for i in self.timesteps
								]
								__a		 =       torch.tensor(UpperCAmelCase				,						dtype=torch.floataa				,						device=UpperCAmelCase   )



			def __SCREAMING_SNAKE_CASE							(						self				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase = None   )			->   Tuple[torch.FloatTensor, float]:
								if self.config.s_min <= sigma <= self.config.s_max:
													__a		 =       min(self.config.s_churn / self.num_inference_steps				,						2**0.5 - 1   )
								else:
													__a		 =       0

								# sample eps ~ N(0, S_noise^2 * I)
								__a		 =       self.config.s_noise * randn_tensor(sample.shape				,						generator=UpperCAmelCase   ).to(sample.device   )
								__a		 =       sigma + gamma * sigma
								__a		 =       sample + ((sigma_hat**2 - sigma**2) ** 0.5 * eps)

								return sample_hat, sigma_hat



			def __SCREAMING_SNAKE_CASE							(						self				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase = True				,						)			->   Union[KarrasVeOutput, Tuple]:
								__a		 =       sample_hat + sigma_hat * model_output
								__a		 =       (sample_hat - pred_original_sample) / sigma_hat
								__a		 =       sample_hat + (sigma_prev - sigma_hat) * derivative

								if not return_dict:
													return (sample_prev, derivative)

								return KarrasVeOutput(
								    prev_sample=UpperCAmelCase				,						derivative=UpperCAmelCase				,						pred_original_sample=UpperCAmelCase   )



			def __SCREAMING_SNAKE_CASE							(						self				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase = True				,						)			->   Union[KarrasVeOutput, Tuple]:
								__a		 =       sample_prev + sigma_prev * model_output
								__a		 =       (sample_prev - pred_original_sample) / sigma_prev
								__a		 =       sample_hat + (sigma_prev - sigma_hat) * (0.5 * derivative + 0.5 * derivative_corr)

								if not return_dict:
													return (sample_prev, derivative)

								return KarrasVeOutput(
								    prev_sample=UpperCAmelCase				,						derivative=UpperCAmelCase				,						pred_original_sample=UpperCAmelCase   )







			def __SCREAMING_SNAKE_CASE							(						self				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase   )			->   List[Any]:
								raise NotImplementedError()




