
'''simple docstring'''
from dataclasses import asdict, dataclass
from typing import Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging


a_											=						logging.get_logger(__name__)

# TODO Update this
a_											=						{
    "facebook/esm-1b": "https://huggingface.co/facebook/esm-1b/resolve/main/config.json",
    # See all ESM models at https://huggingface.co/models?filter=esm
}






class    UpperCAmelCase_				(	snake_case__					):
 UpperCAmelCase_		    =						"""esm"""





 def __init__(    self ,       lowercase_=None ,       lowercase_=None ,       lowercase_=None ,       lowercase_=7_68 ,       lowercase_=12 ,       lowercase_=12 ,       lowercase_=30_72 ,       lowercase_=0.1 ,       lowercase_=0.1 ,       lowercase_=10_26 ,       lowercase_=0.02 ,       lowercase_=1E-12 ,       lowercase_="absolute" ,       lowercase_=True ,       lowercase_=None ,       lowercase_=False ,       lowercase_=False ,       lowercase_=None ,       lowercase_=None ,       **lowercase_ ,       ):
     super().__init__(pad_token_id=lowercase_ ,       mask_token_id=lowercase_ ,       **lowercase_)

     snake_case_ :    Optional[Any]     			=						vocab_size
     snake_case_ :    Optional[int]     			=						hidden_size
     snake_case_ :    List[Any]     			=						num_hidden_layers
     snake_case_ :    str     			=						num_attention_heads
     snake_case_ :    Optional[int]     			=						intermediate_size
     snake_case_ :    Optional[Any]     			=						hidden_dropout_prob
     snake_case_ :    Union[str, Any]     			=						attention_probs_dropout_prob
     snake_case_ :    Tuple     			=						max_position_embeddings
     snake_case_ :    Tuple     			=						initializer_range
     snake_case_ :    List[Any]     			=						layer_norm_eps
     snake_case_ :    List[str]     			=						position_embedding_type
     snake_case_ :    List[Any]     			=						use_cache
     snake_case_ :    Dict     			=						emb_layer_norm_before
     snake_case_ :    str     			=						token_dropout
     snake_case_ :    Any     			=						is_folding_model
     if is_folding_model:
         if esmfold_config is None:
             logger.info("No esmfold_config supplied for folding model, using default values.")
             snake_case_ :    Optional[int]     			=						EsmFoldConfig()
         elif isinstance(lowercase_ ,       lowercase_):
             snake_case_ :    List[Any]     			=						EsmFoldConfig(**lowercase_)
         snake_case_ :    Dict     			=						esmfold_config
         if vocab_list is None:
             logger.warning("No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!")
             snake_case_ :    List[str]     			=						get_default_vocab_list()
         else:
             snake_case_ :    Dict     			=						vocab_list
     else:
         snake_case_ :    Dict     			=						None
         snake_case_ :    int     			=						None
     if self.esmfold_config is not None and getattr(self.esmfold_config ,       "use_esm_attn_map" ,       lowercase_):
         raise ValueError("The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!")





 def        snake_case__		(    self):
     snake_case_ :    List[Any]     			=						super().to_dict()
     if isinstance(self.esmfold_config ,       lowercase_):
         snake_case_ :    Tuple     			=						self.esmfold_config.to_dict()
     return output






@dataclass
class    UpperCAmelCase_				:
 UpperCAmelCase_		    =						None
 UpperCAmelCase_		    =						True
 UpperCAmelCase_		    =						False
 UpperCAmelCase_		    =						False
 UpperCAmelCase_		    =						False
 UpperCAmelCase_		    =						0

 UpperCAmelCase_		    =						True
 UpperCAmelCase_		    =						False

 UpperCAmelCase_		    =						1_2_8
 UpperCAmelCase_		    =						None





 def        snake_case__		(    self):
     if self.trunk is None:
         snake_case_ :    List[str]     			=						TrunkConfig()
     elif isinstance(self.trunk ,       lowercase_):
         snake_case_ :    str     			=						TrunkConfig(**self.trunk)





 def        snake_case__		(    self):
     snake_case_ :    str     			=						asdict(self)
     snake_case_ :    Optional[Any]     			=						self.trunk.to_dict()
     return output






@dataclass
class    UpperCAmelCase_				:
 UpperCAmelCase_		    =						4_8
 UpperCAmelCase_		    =						1_0_2_4
 UpperCAmelCase_		    =						1_2_8
 UpperCAmelCase_		    =						3_2
 UpperCAmelCase_		    =						3_2
 UpperCAmelCase_		    =						3_2
 UpperCAmelCase_		    =						0
 UpperCAmelCase_		    =						0
 UpperCAmelCase_		    =						False
 UpperCAmelCase_		    =						4
 UpperCAmelCase_		    =						1_2_8
 UpperCAmelCase_		    =						None





 def        snake_case__		(    self):
     if self.structure_module is None:
         snake_case_ :    str     			=						StructureModuleConfig()
     elif isinstance(self.structure_module ,       lowercase_):
         snake_case_ :    List[Any]     			=						StructureModuleConfig(**self.structure_module)

     if self.max_recycles <= 0:
         raise ValueError(F'`max_recycles` should be positive, got {self.max_recycles}.')
     if self.sequence_state_dim % self.sequence_state_dim != 0:
         raise ValueError(
             "`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got"
             F' {self.sequence_state_dim} and {self.sequence_state_dim}.')
     if self.pairwise_state_dim % self.pairwise_state_dim != 0:
         raise ValueError(
             "`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got"
             F' {self.pairwise_state_dim} and {self.pairwise_state_dim}.')

     snake_case_ :    str     			=						self.sequence_state_dim // self.sequence_head_width
     snake_case_ :    Tuple     			=						self.pairwise_state_dim // self.pairwise_head_width

     if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width:
         raise ValueError(
             "`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got"
             F' {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.')
     if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width:
         raise ValueError(
             "`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got"
             F' {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.')
     if self.pairwise_state_dim % 2 != 0:
         raise ValueError(F'`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.')

     if self.dropout >= 0.4:
         raise ValueError(F'`dropout` should not be greater than 0.4, got {self.dropout}.')





 def        snake_case__		(    self):
     snake_case_ :    str     			=						asdict(self)
     snake_case_ :    int     			=						self.structure_module.to_dict()
     return output






@dataclass
class    UpperCAmelCase_				:
 UpperCAmelCase_		    =						3_8_4
 UpperCAmelCase_		    =						1_2_8
 UpperCAmelCase_		    =						1_6
 UpperCAmelCase_		    =						1_2_8
 UpperCAmelCase_		    =						1_2
 UpperCAmelCase_		    =						4
 UpperCAmelCase_		    =						8
 UpperCAmelCase_		    =						0.1
 UpperCAmelCase_		    =						8
 UpperCAmelCase_		    =						1
 UpperCAmelCase_		    =						2
 UpperCAmelCase_		    =						7
 UpperCAmelCase_		    =						1_0
 UpperCAmelCase_		    =						1e-8
 UpperCAmelCase_		    =						1e5





 def        snake_case__		(    self):
     return asdict(self)


def 						UpperCamelCase_     (	):
    """simple docstring"""






    return (
        "<cls>",
        "<pad>",
        "<eos>",
        "<unk>",
        "L",
        "A",
        "G",
        "V",
        "S",
        "E",
        "R",
        "T",
        "I",
        "D",
        "P",
        "K",
        "Q",
        "N",
        "F",
        "Y",
        "M",
        "H",
        "W",
        "C",
        "X",
        "B",
        "U",
        "Z",
        "O",
        ".",
        "-",
        "<null_1>",
        "<mask>",
    )






'''simple docstring'''
import gc
import math
import unittest

import torch

from diffusers import UNetaDModel
from diffusers.utils import floats_tensor, logging, slow, torch_all_close, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


a_											=						logging.get_logger(__name__)

enable_full_determinism()






class    UpperCAmelCase_				(	snake_case__    ,						snake_case__    ,						unittest.TestCase					):
 UpperCAmelCase_		    =						UNetaDModel
 UpperCAmelCase_		    =						"""sample"""





 @property
 def        snake_case__		(    self):
     snake_case_ :    Optional[Any]     			=						4
     snake_case_ :    int     			=						3
     snake_case_ :    Dict     			=						(32, 32)

     snake_case_ :    str     			=						floats_tensor((batch_size, num_channels) + sizes).to(lowercase_)
     snake_case_ :    Union[str, Any]     			=						torch.tensor([10]).to(lowercase_)

     return {"sample": noise, "timestep": time_step}





 @property
 def        snake_case__		(    self):
     return (3, 32, 32)





 @property
 def        snake_case__		(    self):
     return (3, 32, 32)





 def        snake_case__		(    self):
     snake_case_ :    str     			=						{
         "block_out_channels": (32, 64),
         "down_block_types": ("DownBlock2D", "AttnDownBlock2D"),
         "up_block_types": ("AttnUpBlock2D", "UpBlock2D"),
         "attention_head_dim": 3,
         "out_channels": 3,
         "in_channels": 3,
         "layers_per_block": 2,
         "sample_size": 32,
     }
     snake_case_ :    List[Any]     			=						self.dummy_input
     return init_dict, inputs_dict






class    UpperCAmelCase_				(	snake_case__    ,						snake_case__    ,						unittest.TestCase					):
 UpperCAmelCase_		    =						UNetaDModel
 UpperCAmelCase_		    =						"""sample"""





 @property
 def        snake_case__		(    self):
     snake_case_ :    List[Any]     			=						4
     snake_case_ :    Dict     			=						4
     snake_case_ :    Dict     			=						(32, 32)

     snake_case_ :    Dict     			=						floats_tensor((batch_size, num_channels) + sizes).to(lowercase_)
     snake_case_ :    Any     			=						torch.tensor([10]).to(lowercase_)

     return {"sample": noise, "timestep": time_step}





 @property
 def        snake_case__		(    self):
     return (4, 32, 32)





 @property
 def        snake_case__		(    self):
     return (4, 32, 32)





 def        snake_case__		(    self):
     snake_case_ :    Any     			=						{
         "sample_size": 32,
         "in_channels": 4,
         "out_channels": 4,
         "layers_per_block": 2,
         "block_out_channels": (32, 64),
         "attention_head_dim": 32,
         "down_block_types": ("DownBlock2D", "DownBlock2D"),
         "up_block_types": ("UpBlock2D", "UpBlock2D"),
     }
     snake_case_ :    Any     			=						self.dummy_input
     return init_dict, inputs_dict





 def        snake_case__		(    self):
     snake_case_			, snake_case_ :    Tuple     			=						UNetaDModel.from_pretrained("fusing/unet-ldm-dummy-update" ,       output_loading_info=lowercase_)

     self.assertIsNotNone(lowercase_)
     self.assertEqual(len(loading_info["missing_keys"]) ,       0)

     model.to(lowercase_)
     snake_case_ :    int     			=						model(**self.dummy_input).sample

     assert image is not None, "Make sure output is not None"





 @unittest.skipIf(torch_device != "cuda" ,       "This test is supposed to run on GPU")
 def        snake_case__		(    self):
     snake_case_			, snake_case_ :    Optional[Any]     			=						UNetaDModel.from_pretrained("fusing/unet-ldm-dummy-update" ,       output_loading_info=lowercase_)
     model.to(lowercase_)
     snake_case_ :    str     			=						model(**self.dummy_input).sample

     assert image is not None, "Make sure output is not None"





 @unittest.skipIf(torch_device != "cuda" ,       "This test is supposed to run on GPU")
 def        snake_case__		(    self):
     # by defautl model loading will use accelerate as `low_cpu_mem_usage=True`
     snake_case_			, snake_case_ :    Any     			=						UNetaDModel.from_pretrained("fusing/unet-ldm-dummy-update" ,       output_loading_info=lowercase_)
     model_accelerate.to(lowercase_)
     model_accelerate.eval()

     snake_case_ :    Optional[int]     			=						torch.randn(
         1 ,       model_accelerate.config.in_channels ,       model_accelerate.config.sample_size ,       model_accelerate.config.sample_size ,       generator=torch.manual_seed(0) ,       )
     snake_case_ :    List[str]     			=						noise.to(lowercase_)
     snake_case_ :    Any     			=						torch.tensor([10] * noise.shape[0]).to(lowercase_)

     snake_case_ :    Optional[int]     			=						model_accelerate(lowercase_ ,       lowercase_)["sample"]

     # two models don't need to stay in the device at the same time
     del model_accelerate
     torch.cuda.empty_cache()
     gc.collect()

     snake_case_			, snake_case_ :    Union[str, Any]     			=						UNetaDModel.from_pretrained(
         "fusing/unet-ldm-dummy-update" ,       output_loading_info=lowercase_ ,       low_cpu_mem_usage=lowercase_)
     model_normal_load.to(lowercase_)
     model_normal_load.eval()
     snake_case_ :    Optional[Any]     			=						model_normal_load(lowercase_ ,       lowercase_)["sample"]

     assert torch_all_close(lowercase_ ,       lowercase_ ,       rtol=1E-3)





 def        snake_case__		(    self):
     snake_case_ :    Any     			=						UNetaDModel.from_pretrained("fusing/unet-ldm-dummy-update")
     model.eval()
     model.to(lowercase_)

     snake_case_ :    List[str]     			=						torch.randn(
         1 ,       model.config.in_channels ,       model.config.sample_size ,       model.config.sample_size ,       generator=torch.manual_seed(0) ,       )
     snake_case_ :    List[str]     			=						noise.to(lowercase_)
     snake_case_ :    str     			=						torch.tensor([10] * noise.shape[0]).to(lowercase_)

     with torch.no_grad():
         snake_case_ :    Tuple     			=						model(lowercase_ ,       lowercase_).sample

     snake_case_ :    Optional[int]     			=						output[0, -1, -3:, -3:].flatten().cpu()
     # fmt: off
     snake_case_ :    Tuple     			=						torch.tensor([-13.3_258, -20.1_100, -15.9_873, -17.6_617, -23.0_596, -17.9_419, -13.3_675, -16.1_889, -12.3_800])
     # fmt: on

     self.assertTrue(torch_all_close(lowercase_ ,       lowercase_ ,       rtol=1E-3))






class    UpperCAmelCase_				(	snake_case__    ,						snake_case__    ,						unittest.TestCase					):
 UpperCAmelCase_		    =						UNetaDModel
 UpperCAmelCase_		    =						"""sample"""





 @property
 def        snake_case__		(    self ,       lowercase_=(32, 32)):
     snake_case_ :    List[Any]     			=						4
     snake_case_ :    str     			=						3

     snake_case_ :    str     			=						floats_tensor((batch_size, num_channels) + sizes).to(lowercase_)
     snake_case_ :    Dict     			=						torch.tensor(batch_size * [10]).to(dtype=torch.intaa ,       device=lowercase_)

     return {"sample": noise, "timestep": time_step}





 @property
 def        snake_case__		(    self):
     return (3, 32, 32)





 @property
 def        snake_case__		(    self):
     return (3, 32, 32)





 def        snake_case__		(    self):
     snake_case_ :    List[str]     			=						{
         "block_out_channels": [32, 64, 64, 64],
         "in_channels": 3,
         "layers_per_block": 1,
         "out_channels": 3,
         "time_embedding_type": "fourier",
         "norm_eps": 1E-6,
         "mid_block_scale_factor": math.sqrt(2.0),
         "norm_num_groups": None,
         "down_block_types": [
             "SkipDownBlock2D",
             "AttnSkipDownBlock2D",
             "SkipDownBlock2D",
             "SkipDownBlock2D",
         ],
         "up_block_types": [
             "SkipUpBlock2D",
             "SkipUpBlock2D",
             "AttnSkipUpBlock2D",
             "SkipUpBlock2D",
         ],
     }
     snake_case_ :    Optional[Any]     			=						self.dummy_input
     return init_dict, inputs_dict





 @slow
 def        snake_case__		(    self):
     snake_case_			, snake_case_ :    Optional[int]     			=						UNetaDModel.from_pretrained("google/ncsnpp-celebahq-256" ,       output_loading_info=lowercase_)
     self.assertIsNotNone(lowercase_)
     self.assertEqual(len(loading_info["missing_keys"]) ,       0)

     model.to(lowercase_)
     snake_case_ :    Dict     			=						self.dummy_input
     snake_case_ :    Tuple     			=						floats_tensor((4, 3) + (2_56, 2_56)).to(lowercase_)
     snake_case_ :    Tuple     			=						noise
     snake_case_ :    Tuple     			=						model(**lowercase_)

     assert image is not None, "Make sure output is not None"





 @slow
 def        snake_case__		(    self):
     snake_case_ :    Dict     			=						UNetaDModel.from_pretrained("google/ncsnpp-celebahq-256")
     model.to(lowercase_)

     snake_case_ :    List[Any]     			=						4
     snake_case_ :    str     			=						3
     snake_case_ :    Dict     			=						(2_56, 2_56)

     snake_case_ :    Tuple     			=						torch.ones((batch_size, num_channels) + sizes).to(lowercase_)
     snake_case_ :    List[Any]     			=						torch.tensor(batch_size * [1E-4]).to(lowercase_)

     with torch.no_grad():
         snake_case_ :    int     			=						model(lowercase_ ,       lowercase_).sample

     snake_case_ :    List[str]     			=						output[0, -3:, -3:, -1].flatten().cpu()
     # fmt: off
     snake_case_ :    Union[str, Any]     			=						torch.tensor([-4_842.8_691, -6_499.6_631, -3_800.1_953, -7_978.2_686, -10_980.7_129, -20_028.8_535, 8_148.2_822, 2_342.2_905, 567.7_608])
     # fmt: on

     self.assertTrue(torch_all_close(lowercase_ ,       lowercase_ ,       rtol=1E-2))





 def        snake_case__		(    self):
     snake_case_ :    List[Any]     			=						UNetaDModel.from_pretrained("fusing/ncsnpp-ffhq-ve-dummy-update")
     model.to(lowercase_)

     snake_case_ :    Dict     			=						4
     snake_case_ :    str     			=						3
     snake_case_ :    List[Any]     			=						(32, 32)

     snake_case_ :    int     			=						torch.ones((batch_size, num_channels) + sizes).to(lowercase_)
     snake_case_ :    List[Any]     			=						torch.tensor(batch_size * [1E-4]).to(lowercase_)

     with torch.no_grad():
         snake_case_ :    Optional[Any]     			=						model(lowercase_ ,       lowercase_).sample

     snake_case_ :    List[Any]     			=						output[0, -3:, -3:, -1].flatten().cpu()
     # fmt: off
     snake_case_ :    Union[str, Any]     			=						torch.tensor([-0.0_325, -0.0_900, -0.0_869, -0.0_332, -0.0_725, -0.0_270, -0.0_101, 0.0_227, 0.0_256])
     # fmt: on

     self.assertTrue(torch_all_close(lowercase_ ,       lowercase_ ,       rtol=1E-2))





 def        snake_case__		(    self):
     # not required for this model
     pass




