




"""simple docstring"""







import torch

from diffusers import DDPMScheduler

from .test_schedulers import SchedulerCommonTest




class       lowerCAmelCase__       (			lowerCAmelCase_    ):


				"""simple docstring"""





				__UpperCAmelCase :     Tuple       =      (DDPMScheduler,)



				def       snake_case    (     self					:	Union[str, Any]    ,   **lowercase__					:	str						):
						__lowercase				:		List[str]												=				{
						    "num_train_timesteps": 1_0_0_0,
						    "beta_start": 0.0_0_0_1,
						    "beta_end": 0.0_2,
						    "beta_schedule": "linear",
						    "variance_type": "fixed_small",
						    "clip_sample": True,
						}

						config.update(**lowercase__						)
						return config



				def       snake_case    (     self					:	List[Any]						):
						for timesteps in [1, 5, 1_0_0, 1_0_0_0]:
								self.check_over_configs(num_train_timesteps=lowercase__						)



				def       snake_case    (     self					:	List[Any]						):
						for beta_start, beta_end in zip([0.0_0_0_1, 0.0_0_1, 0.0_1, 0.1]    ,   [0.0_0_2, 0.0_2, 0.2, 2]						):
								self.check_over_configs(beta_start=lowercase__    ,   beta_end=lowercase__						)



				def       snake_case    (     self					:	Dict						):
						for schedule in ["linear", "squaredcos_cap_v2"]:
								self.check_over_configs(beta_schedule=lowercase__						)



				def       snake_case    (     self					:	Any						):
						for variance in ["fixed_small", "fixed_large", "other"]:
								self.check_over_configs(variance_type=lowercase__						)



				def       snake_case    (     self					:	Tuple						):
						for clip_sample in [True, False]:
								self.check_over_configs(clip_sample=lowercase__						)



				def       snake_case    (     self					:	Dict						):
						self.check_over_configs(thresholding=lowercase__						)
						for threshold in [0.5, 1.0, 2.0]:
								for prediction_type in ["epsilon", "sample", "v_prediction"]:
										self.check_over_configs(
										    thresholding=lowercase__    ,   prediction_type=lowercase__    ,   sample_max_value=lowercase__    ,   )



				def       snake_case    (     self					:	Union[str, Any]						):
						for prediction_type in ["epsilon", "sample", "v_prediction"]:
								self.check_over_configs(prediction_type=lowercase__						)



				def       snake_case    (     self					:	List[Any]						):
						for t in [0, 5_0_0, 9_9_9]:
								self.check_over_forward(time_step=lowercase__						)



				def       snake_case    (     self					:	List[Any]						):
						__lowercase				:		Optional[int]												=				self.scheduler_classes[0]
						__lowercase				:		int												=				self.get_scheduler_config()
						__lowercase				:		int												=				scheduler_class(**lowercase__						)

						assert torch.sum(torch.abs(scheduler._get_variance(0						) - 0.0						)						) < 1e-5
						assert torch.sum(torch.abs(scheduler._get_variance(4_8_7						) - 0.0_0_9_7_9						)						) < 1e-5
						assert torch.sum(torch.abs(scheduler._get_variance(9_9_9						) - 0.0_2						)						) < 1e-5



				def       snake_case    (     self					:	str						):
						__lowercase				:		List[Any]												=				self.scheduler_classes[0]
						__lowercase				:		int												=				self.get_scheduler_config()
						__lowercase				:		Any												=				scheduler_class(**lowercase__						)

						__lowercase				:		Any												=				len(lowercase__						)

						__lowercase				:		str												=				self.dummy_model()
						__lowercase				:		Tuple												=				self.dummy_sample_deter
						__lowercase				:		Optional[Any]												=				torch.manual_seed(0						)

						for t in reversed(range(lowercase__						)						):
								# 1. predict noise residual
								__lowercase				:		Dict												=				model(lowercase__    ,   lowercase__						)

								# 2. predict previous mean of sample x_t-1
								__lowercase				:		Optional[int]												=				scheduler.step(lowercase__    ,   lowercase__    ,   lowercase__    ,   generator=lowercase__						).prev_sample

								# if t > 0:
								#     noise = self.dummy_sample_deter
								#     variance = scheduler.get_variance(t) ** (0.5) * noise
								#
								# sample = pred_prev_sample + variance
								__lowercase				:		Any												=				pred_prev_sample

						__lowercase				:		str												=				torch.sum(torch.abs(lowercase__						)						)
						__lowercase				:		str												=				torch.mean(torch.abs(lowercase__						)						)

						assert abs(result_sum.item() - 2_5_8.9_6_0_6						) < 1e-2
						assert abs(result_mean.item() - 0.3_3_7_2						) < 1e-3



				def       snake_case    (     self					:	int						):
						__lowercase				:		Optional[int]												=				self.scheduler_classes[0]
						__lowercase				:		int												=				self.get_scheduler_config(prediction_type="v_prediction"						)
						__lowercase				:		Optional[int]												=				scheduler_class(**lowercase__						)

						__lowercase				:		Optional[int]												=				len(lowercase__						)

						__lowercase				:		Any												=				self.dummy_model()
						__lowercase				:		Union[str, Any]												=				self.dummy_sample_deter
						__lowercase				:		Any												=				torch.manual_seed(0						)

						for t in reversed(range(lowercase__						)						):
								# 1. predict noise residual
								__lowercase				:		Dict												=				model(lowercase__    ,   lowercase__						)

								# 2. predict previous mean of sample x_t-1
								__lowercase				:		Dict												=				scheduler.step(lowercase__    ,   lowercase__    ,   lowercase__    ,   generator=lowercase__						).prev_sample

								# if t > 0:
								#     noise = self.dummy_sample_deter
								#     variance = scheduler.get_variance(t) ** (0.5) * noise
								#
								# sample = pred_prev_sample + variance
								__lowercase				:		str												=				pred_prev_sample

						__lowercase				:		Union[str, Any]												=				torch.sum(torch.abs(lowercase__						)						)
						__lowercase				:		Any												=				torch.mean(torch.abs(lowercase__						)						)

						assert abs(result_sum.item() - 2_0_2.0_2_9_6						) < 1e-2
						assert abs(result_mean.item() - 0.2_6_3_1						) < 1e-3



				def       snake_case    (     self					:	int						):
						__lowercase				:		Optional[Any]												=				self.scheduler_classes[0]
						__lowercase				:		Optional[int]												=				self.get_scheduler_config()
						__lowercase				:		Optional[int]												=				scheduler_class(**lowercase__						)

						__lowercase				:		Tuple												=				[1_0_0, 8_7, 5_0, 1, 0]

						scheduler.set_timesteps(timesteps=lowercase__						)

						__lowercase				:		List[Any]												=				scheduler.timesteps

						for i, timestep in enumerate(lowercase__						):
								if i == len(lowercase__						) - 1:
										__lowercase				:		Optional[Any]												=				-1
								else:
										__lowercase				:		str												=				timesteps[i + 1]

								__lowercase				:		int												=				scheduler.previous_timestep(lowercase__						)
								__lowercase				:		Optional[Any]												=				prev_t.item()

								self.assertEqual(lowercase__    ,   lowercase__						)



				def       snake_case    (     self					:	Optional[int]						):
						__lowercase				:		Optional[int]												=				self.scheduler_classes[0]
						__lowercase				:		Optional[int]												=				self.get_scheduler_config()
						__lowercase				:		Dict												=				scheduler_class(**lowercase__						)

						__lowercase				:		Dict												=				[1_0_0, 8_7, 5_0, 5_1, 0]

						with self.assertRaises(lowercase__    ,   msg="`custom_timesteps` must be in descending order."						):
								scheduler.set_timesteps(timesteps=lowercase__						)



				def       snake_case    (     self					:	List[str]						):
						__lowercase				:		Union[str, Any]												=				self.scheduler_classes[0]
						__lowercase				:		Optional[Any]												=				self.get_scheduler_config()
						__lowercase				:		List[Any]												=				scheduler_class(**lowercase__						)

						__lowercase				:		Tuple												=				[1_0_0, 8_7, 5_0, 1, 0]
						__lowercase				:		Dict												=				len(lowercase__						)

						with self.assertRaises(lowercase__    ,   msg="Can only pass one of `num_inference_steps` or `custom_timesteps`."						):
								scheduler.set_timesteps(num_inference_steps=lowercase__    ,   timesteps=lowercase__						)



				def       snake_case    (     self					:	str						):
						__lowercase				:		Optional[int]												=				self.scheduler_classes[0]
						__lowercase				:		Tuple												=				self.get_scheduler_config()
						__lowercase				:		List[Any]												=				scheduler_class(**lowercase__						)

						__lowercase				:		Union[str, Any]												=				[scheduler.config.num_train_timesteps]

						with self.assertRaises(
						    lowercase__    ,   msg="`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}"    ,   ):
								scheduler.set_timesteps(timesteps=lowercase__						)






"""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       lowerCAmelCase__       :


				"""simple docstring"""





				def __init__(     self					:	Tuple    ,   lowercase__					:	Tuple    ,   lowercase__					:	Tuple=1_3    ,   lowercase__					:	Optional[int]=7    ,   lowercase__					:	List[str]=False    ,   lowercase__					:	Tuple=True    ,   lowercase__					:	int=False    ,   lowercase__					:	List[str]=False    ,   lowercase__					:	Optional[Any]=1_9    ,   lowercase__					:	int=3_2    ,   lowercase__					:	List[Any]=5    ,   lowercase__					:	Optional[int]=4    ,   lowercase__					:	Any=3_7    ,   lowercase__					:	Tuple="gelu"    ,   lowercase__					:	int=0.1    ,   lowercase__					:	Tuple=0.1    ,   lowercase__					:	List[Any]=5_1_2    ,   lowercase__					:	List[Any]=1_6    ,   lowercase__					:	Union[str, Any]=2    ,   lowercase__					:	List[str]=0.0_2    ,   lowercase__					:	List[Any]=3    ,   lowercase__					:	Any=4    ,   lowercase__					:	Optional[Any]=None    ,   ):
						__lowercase				:		int												=				parent
						__lowercase				:		Tuple												=				batch_size
						__lowercase				:		Optional[int]												=				seq_length
						__lowercase				:		str												=				is_training
						__lowercase				:		List[Any]												=				use_input_mask
						__lowercase				:		Any												=				use_token_type_ids
						__lowercase				:		str												=				use_labels
						__lowercase				:		Dict												=				vocab_size
						__lowercase				:		Optional[int]												=				hidden_size
						__lowercase				:		List[Any]												=				num_hidden_layers
						__lowercase				:		int												=				num_attention_heads
						__lowercase				:		Union[str, Any]												=				intermediate_size
						__lowercase				:		Dict												=				hidden_act
						__lowercase				:		Union[str, Any]												=				hidden_dropout_prob
						__lowercase				:		Optional[int]												=				attention_probs_dropout_prob
						__lowercase				:		Optional[Any]												=				max_position_embeddings
						__lowercase				:		List[str]												=				type_vocab_size
						__lowercase				:		str												=				type_sequence_label_size
						__lowercase				:		List[str]												=				initializer_range
						__lowercase				:		Optional[Any]												=				num_labels
						__lowercase				:		Tuple												=				num_choices
						__lowercase				:		Optional[Any]												=				scope



				def       snake_case    (     self					:	Optional[int]						):
						__lowercase				:		Tuple												=				ids_tensor([self.batch_size, self.seq_length]    ,   self.vocab_size						)

						__lowercase				:		Union[str, Any]												=				None
						if self.use_input_mask:
								__lowercase				:		List[str]												=				random_attention_mask([self.batch_size, self.seq_length]						)

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

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

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



				def       snake_case    (     self					:	Any						):
						__lowercase				:		Dict												=				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=lowercase__    ,   esmfold_config={"trunk": {"num_blocks": 2}, "fp16_esm": False}    ,   )
						return config



				def       snake_case    (     self					:	str    ,   lowercase__					:	List[Any]    ,   lowercase__					:	Optional[int]    ,   lowercase__					:	List[Any]    ,   lowercase__					:	int    ,   lowercase__					:	List[Any]    ,   lowercase__					:	str						):
						__lowercase				:		List[Any]												=				EsmForProteinFolding(config=lowercase__						).float()
						model.to(lowercase__						)
						model.eval()
						__lowercase				:		List[str]												=				model(lowercase__    ,   attention_mask=lowercase__						)
						__lowercase				:		Any												=				model(lowercase__						)
						__lowercase				:		Any												=				model(lowercase__						)

						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       snake_case    (     self					:	Tuple						):
						__lowercase				:		List[str]												=				self.prepare_config_and_inputs()
						(
						    (
						    __lowercase
						)						,(
						    __lowercase
						)						,(
						    __lowercase
						)						,(
						    __lowercase
						)						,(
						    __lowercase
						)						,(
						    __lowercase
						)						,
						)				:		Any												=				config_and_inputs
						__lowercase				:		int												=				{"input_ids": input_ids, "attention_mask": input_mask}
						return config, inputs_dict





@require_torch
class       lowerCAmelCase__       (			lowerCAmelCase_  ,			lowerCAmelCase_  ,			unittest.TestCase    ):


				"""simple docstring"""





				__UpperCAmelCase :     Union[str, Any]       =      False

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



				def       snake_case    (     self					:	Tuple						):
						__lowercase				:		Tuple												=				EsmFoldModelTester(self						)
						__lowercase				:		Dict												=				ConfigTester(self    ,   config_class=lowercase__    ,   hidden_size=3_7						)



				def       snake_case    (     self					:	Dict						):
						self.config_tester.run_common_tests()



				def       snake_case    (     self					:	Optional[int]						):
						__lowercase				:		Tuple												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowercase__						)



				@unittest.skip("Does not support attention outputs"						)
				def       snake_case    (     self					:	Union[str, Any]						):
						pass



				@unittest.skip
				def       snake_case    (     self					:	Tuple						):
						pass



				@unittest.skip("Esm does not support embedding resizing"						)
				def       snake_case    (     self					:	Optional[int]						):
						pass



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



				@unittest.skip("ESMFold does not support passing input embeds!"						)
				def       snake_case    (     self					:	int						):
						pass



				@unittest.skip("ESMFold does not support head pruning."						)
				def       snake_case    (     self					:	List[Any]						):
						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[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					:	List[str]						):
						pass



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



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



				@unittest.skip("ESMFold only has one output format."						)
				def       snake_case    (     self					:	Tuple						):
						pass



				@unittest.skip("This test doesn't work for ESMFold and doesn't test core functionality"						)
				def       snake_case    (     self					:	Any						):
						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					:	Any						):
						pass



				@unittest.skip("ESMFold doesn't support torchscript compilation."						)
				def       snake_case    (     self					:	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					:	List[str]						):
						pass



				@unittest.skip("ESMFold doesn't support data parallel."						)
				def       snake_case    (     self					:	Optional[Any]						):
						pass



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





@require_torch
class       lowerCAmelCase__       (			lowerCAmelCase_    ):


				"""simple docstring"""





				@slow
				def       snake_case    (     self					:	Union[str, Any]						):
						__lowercase				:		Tuple												=				EsmForProteinFolding.from_pretrained("facebook/esmfold_v1"						).float()
						model.eval()
						__lowercase				:		Optional[int]												=				torch.tensor([[0, 6, 4, 1_3, 5, 4, 1_6, 1_2, 1_1, 7, 2]]						)
						__lowercase				:		str												=				model(lowercase__						)["positions"]
						__lowercase				:		Union[str, Any]												=				torch.tensor([2.5_8_2_8, 0.7_9_9_3, -1_0.9_3_3_4]    ,   dtype=torch.floataa						)
						self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0]    ,   lowercase__    ,   atol=1e-4						)						)
