

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







       def __init__(				self      :			List[str]    ,   __lowercase      :			List[Any]    ,   __lowercase      :			Dict=13    ,   __lowercase      :			Tuple=7    ,   __lowercase      :			Optional[Any]=False    ,   __lowercase      :			Optional[int]=True    ,   __lowercase      :			str=False    ,   __lowercase      :			Tuple=False    ,   __lowercase      :			Optional[int]=19    ,   __lowercase      :			List[Any]=32    ,   __lowercase      :			List[Any]=5    ,   __lowercase      :			Optional[int]=4    ,   __lowercase      :			Optional[int]=37    ,   __lowercase      :			List[str]="gelu"    ,   __lowercase      :			Optional[int]=0.1    ,   __lowercase      :			Union[str, Any]=0.1    ,   __lowercase      :			List[Any]=512    ,   __lowercase      :			str=16    ,   __lowercase      :			List[str]=2    ,   __lowercase      :			Tuple=0.0_2    ,   __lowercase      :			Optional[Any]=3    ,   __lowercase      :			List[Any]=4    ,   __lowercase      :			int=None    ,   ):







            """simple docstring"""







            __lowercase      		=parent
            __lowercase      		=batch_size
            __lowercase      		=seq_length
            __lowercase      		=is_training
            __lowercase      		=use_input_mask
            __lowercase      		=use_token_type_ids
            __lowercase      		=use_labels
            __lowercase      		=vocab_size
            __lowercase      		=hidden_size
            __lowercase      		=num_hidden_layers
            __lowercase      		=num_attention_heads
            __lowercase      		=intermediate_size
            __lowercase      		=hidden_act
            __lowercase      		=hidden_dropout_prob
            __lowercase      		=attention_probs_dropout_prob
            __lowercase      		=max_position_embeddings
            __lowercase      		=type_vocab_size
            __lowercase      		=type_sequence_label_size
            __lowercase      		=initializer_range
            __lowercase      		=num_labels
            __lowercase      		=num_choices
            __lowercase      		=scope







       def       snake_case (				self      :			int   ):







            """simple docstring"""







            __lowercase      		=ids_tensor([self.batch_size, self.seq_length]    ,   self.vocab_size   )

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

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

            __lowercase      		=self.get_config()

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







       def       snake_case (				self      :			int   ):







            """simple docstring"""







            __lowercase      		=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       snake_case (				self      :			Union[str, Any]    ,   __lowercase      :			List[Any]    ,   __lowercase      :			int    ,   __lowercase      :			Optional[int]    ,   __lowercase      :			Optional[Any]    ,   __lowercase      :			Union[str, Any]    ,   __lowercase      :			str   ):







            """simple docstring"""







            __lowercase      		=EsmForProteinFolding(config=__lowercase   ).float()
            model.to(__lowercase   )
            model.eval()
            __lowercase      		=model(__lowercase    ,   attention_mask=__lowercase   )
            __lowercase      		=model(__lowercase   )
            __lowercase      		=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       snake_case (				self      :			Tuple   ):







            """simple docstring"""







            __lowercase      		=self.prepare_config_and_inputs()
            (
                (
                __lowercase
            )		,  (
                __lowercase
            )		,  (
                __lowercase
            )		,  (
                __lowercase
            )		,  (
                __lowercase
            )		,  (
                __lowercase
            )		,  
            )      		=config_and_inputs
            __lowercase      		={'input_ids': input_ids, 'attention_mask': input_mask}
            return config, inputs_dict






@require_torch
class 	lowerCAmelCase  (   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      :			Tuple   ):







            """simple docstring"""







            __lowercase      		=EsmFoldModelTester(self   )
            __lowercase      		=ConfigTester(self    ,   config_class=__lowercase    ,   hidden_size=37   )







       def       snake_case (				self      :			Optional[Any]   ):







            """simple docstring"""







            self.config_tester.run_common_tests()







       def       snake_case (				self      :			str   ):







            """simple docstring"""







            __lowercase      		=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      :			List[str]   ):







            """simple docstring"""







            pass







       @unittest.skip
       def       snake_case (				self      :			Optional[int]   ):







            """simple docstring"""







            pass







       @unittest.skip('Esm does not support embedding resizing'   )
       def       snake_case (				self      :			Any   ):







            """simple docstring"""







            pass







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







            """simple docstring"""







            pass







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







            """simple docstring"""







            pass







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







            """simple docstring"""







            pass







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







            """simple docstring"""







            pass







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







            """simple docstring"""







            pass







       @unittest.skip('ESMFold does not support head pruning.'   )
       def       snake_case (				self      :			str   ):







            """simple docstring"""







            pass







       @unittest.skip('ESMFold does not support head pruning.'   )
       def       snake_case (				self      :			str   ):







            """simple docstring"""







            pass







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







            """simple docstring"""







            pass







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







            """simple docstring"""







            pass







       @unittest.skip('ESMFold only has one output format.'   )
       def       snake_case (				self      :			List[str]   ):







            """simple docstring"""







            pass







       @unittest.skip('This test doesn\'t work for ESMFold and doesn\'t test core functionality'   )
       def       snake_case (				self      :			Any   ):







            """simple docstring"""







            pass







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







            """simple docstring"""







            pass







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







            """simple docstring"""







            pass







       @unittest.skip('ESMFold doesn\'t support torchscript compilation.'   )
       def       snake_case (				self      :			int   ):







            """simple docstring"""







            pass







       @unittest.skip('ESMFold doesn\'t support torchscript compilation.'   )
       def       snake_case (				self      :			Dict   ):







            """simple docstring"""







            pass







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







            """simple docstring"""







            pass







       @unittest.skip('ESMFold doesn\'t support data parallel.'   )
       def       snake_case (				self      :			List[str]   ):







            """simple docstring"""







            pass







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







            """simple docstring"""







            pass






@require_torch
class 	lowerCAmelCase  (   A ):







       @slow
       def       snake_case (				self      :			Dict   ):







            """simple docstring"""







            __lowercase      		=EsmForProteinFolding.from_pretrained('facebook/esmfold_v1'   ).float()
            model.eval()
            __lowercase      		=torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]   )
            __lowercase      		=model(__lowercase   )['positions']
            __lowercase      		=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   )   )





'''simple docstring'''

import unittest

import numpy as np
import torch

from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()


class 	lowerCAmelCase  (   unittest.TestCase ):







       @property
       def       snake_case (				self      :			str   ):







            """simple docstring"""







            torch.manual_seed(0   )
            __lowercase      		=UNetaDModel(
                block_out_channels=(32, 64)    ,   layers_per_block=2    ,   sample_size=32    ,   in_channels=3    ,   out_channels=3    ,   down_block_types=('DownBlock2D', 'AttnDownBlock2D')    ,   up_block_types=('AttnUpBlock2D', 'UpBlock2D')    ,   )
            return model







       def       snake_case (				self      :			int   ):







            """simple docstring"""







            __lowercase      		=self.dummy_uncond_unet
            __lowercase      		=PNDMScheduler()

            __lowercase      		=PNDMPipeline(unet=__lowercase    ,   scheduler=__lowercase   )
            pndm.to(__lowercase   )
            pndm.set_progress_bar_config(disable=__lowercase   )

            __lowercase      		=torch.manual_seed(0   )
            __lowercase      		=pndm(generator=__lowercase    ,   num_inference_steps=20    ,   output_type='numpy'   ).images

            __lowercase      		=torch.manual_seed(0   )
            __lowercase      		=pndm(generator=__lowercase    ,   num_inference_steps=20    ,   output_type='numpy'    ,   return_dict=__lowercase   )[0]

            __lowercase      		=image[0, -3:, -3:, -1]
            __lowercase      		=image_from_tuple[0, -3:, -3:, -1]

            assert image.shape == (1, 32, 32, 3)
            __lowercase      		=np.array([1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0]   )

            assert np.abs(image_slice.flatten() - expected_slice   ).max() < 1E-2
            assert np.abs(image_from_tuple_slice.flatten() - expected_slice   ).max() < 1E-2






@slow
@require_torch
class 	lowerCAmelCase  (   unittest.TestCase ):







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







            """simple docstring"""







            __lowercase      		='google/ddpm-cifar10-32'

            __lowercase      		=UNetaDModel.from_pretrained(__lowercase   )
            __lowercase      		=PNDMScheduler()

            __lowercase      		=PNDMPipeline(unet=__lowercase    ,   scheduler=__lowercase   )
            pndm.to(__lowercase   )
            pndm.set_progress_bar_config(disable=__lowercase   )
            __lowercase      		=torch.manual_seed(0   )
            __lowercase      		=pndm(generator=__lowercase    ,   output_type='numpy'   ).images

            __lowercase      		=image[0, -3:, -3:, -1]

            assert image.shape == (1, 32, 32, 3)
            __lowercase      		=np.array([0.1_5_6_4, 0.1_4_6_4_5, 0.1_4_0_6, 0.1_4_7_1_5, 0.1_2_4_2_5, 0.1_4_0_4_5, 0.1_3_1_1_5, 0.1_2_1_7_5, 0.1_2_5]   )

            assert np.abs(image_slice.flatten() - expected_slice   ).max() < 1E-2


