



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,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_=13,     SCREAMING_SNAKE_CASE_=7,     SCREAMING_SNAKE_CASE_=False,     SCREAMING_SNAKE_CASE_=True,     SCREAMING_SNAKE_CASE_=False,     SCREAMING_SNAKE_CASE_=False,     SCREAMING_SNAKE_CASE_=19,     SCREAMING_SNAKE_CASE_=32,     SCREAMING_SNAKE_CASE_=5,     SCREAMING_SNAKE_CASE_=4,     SCREAMING_SNAKE_CASE_=37,     SCREAMING_SNAKE_CASE_="gelu",     SCREAMING_SNAKE_CASE_=0.1,     SCREAMING_SNAKE_CASE_=0.1,     SCREAMING_SNAKE_CASE_=512,     SCREAMING_SNAKE_CASE_=16,     SCREAMING_SNAKE_CASE_=2,     SCREAMING_SNAKE_CASE_=0.02,     SCREAMING_SNAKE_CASE_=3,     SCREAMING_SNAKE_CASE_=4,     SCREAMING_SNAKE_CASE_=None,     )	->						Tuple:
          UpperCamelCase       :    Dict		      =      parent
          UpperCamelCase       :    Optional[int]		      =      batch_size
          UpperCamelCase       :    Optional[int]		      =      seq_length
          UpperCamelCase       :    Union[str, Any]		      =      is_training
          UpperCamelCase       :    List[Any]		      =      use_input_mask
          UpperCamelCase       :    Optional[Any]		      =      use_token_type_ids
          UpperCamelCase       :    Union[str, Any]		      =      use_labels
          UpperCamelCase       :    Optional[Any]		      =      vocab_size
          UpperCamelCase       :    str		      =      hidden_size
          UpperCamelCase       :    Union[str, Any]		      =      num_hidden_layers
          UpperCamelCase       :    str		      =      num_attention_heads
          UpperCamelCase       :    int		      =      intermediate_size
          UpperCamelCase       :    int		      =      hidden_act
          UpperCamelCase       :    Union[str, Any]		      =      hidden_dropout_prob
          UpperCamelCase       :    Union[str, Any]		      =      attention_probs_dropout_prob
          UpperCamelCase       :    Dict		      =      max_position_embeddings
          UpperCamelCase       :    List[Any]		      =      type_vocab_size
          UpperCamelCase       :    Any		      =      type_sequence_label_size
          UpperCamelCase       :    Dict		      =      initializer_range
          UpperCamelCase       :    List[Any]		      =      num_labels
          UpperCamelCase       :    Dict		      =      num_choices
          UpperCamelCase       :    Any		      =      scope
       def    snake_case_			(     self			)	->						Optional[int]:
          UpperCamelCase       :    Any		      =      ids_tensor([self.batch_size, self.seq_length],     self.vocab_size			)

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

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

          UpperCamelCase       :    Any		      =      self.get_config()

          return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
       def    snake_case_			(     self			)	->						str:
          UpperCamelCase       :    str		      =      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=SCREAMING_SNAKE_CASE_,     esmfold_config={'trunk': {'num_blocks': 2}, 'fp16_esm': False},     )
          return config
       def    snake_case_			(     self,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_			)	->						str:
          UpperCamelCase       :    List[str]		      =      EsmForProteinFolding(config=SCREAMING_SNAKE_CASE_			).float()
          model.to(SCREAMING_SNAKE_CASE_			)
          model.eval()
          UpperCamelCase       :    List[str]		      =      model(SCREAMING_SNAKE_CASE_,     attention_mask=SCREAMING_SNAKE_CASE_			)
          UpperCamelCase       :    List[str]		      =      model(SCREAMING_SNAKE_CASE_			)
          UpperCamelCase       :    Union[str, Any]		      =      model(SCREAMING_SNAKE_CASE_			)

          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:
          UpperCamelCase       :    Union[str, Any]		      =      self.prepare_config_and_inputs()
          (
              (
              UpperCamelCase
          )      ,	(
              UpperCamelCase
          )      ,	(
              UpperCamelCase
          )      ,	(
              UpperCamelCase
          )      ,	(
              UpperCamelCase
          )      ,	(
              UpperCamelCase
          )      ,	
          )       :    Tuple		      =      config_and_inputs
          UpperCamelCase       :    Tuple		      =      {'input_ids': input_ids, 'attention_mask': input_mask}
          return config, inputs_dict






@require_torch
class 							lowerCAmelCase_						(    a__			,       a__			,       unittest.TestCase				):
       UpperCAmelCase__							:   Tuple  	=    False

       UpperCAmelCase__							:   Tuple  	=    (EsmForProteinFolding,) if is_torch_available() else ()
       UpperCAmelCase__							:   Tuple  	=    ()
       UpperCAmelCase__							:   str  	=    {} if is_torch_available() else {}
       UpperCAmelCase__							:   Optional[int]  	=    False
       def    snake_case_			(     self			)	->						Optional[Any]:
          UpperCamelCase       :    Dict		      =      EsmFoldModelTester(self			)
          UpperCamelCase       :    Any		      =      ConfigTester(self,     config_class=SCREAMING_SNAKE_CASE_,     hidden_size=37			)
       def    snake_case_			(     self			)	->						Any:
          self.config_tester.run_common_tests()
       def    snake_case_			(     self			)	->						int:
          UpperCamelCase       :    Optional[int]		      =      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_			)
       @unittest.skip('Does not support attention outputs'			)
       def    snake_case_			(     self			)	->						str:
          pass
       @unittest.skip
       def    snake_case_			(     self			)	->						Optional[Any]:
          pass
       @unittest.skip('Esm does not support embedding resizing'			)
       def    snake_case_			(     self			)	->						List[str]:
          pass
       @unittest.skip('Esm does not support embedding resizing'			)
       def    snake_case_			(     self			)	->						int:
          pass
       @unittest.skip('ESMFold does not support passing input embeds!'			)
       def    snake_case_			(     self			)	->						Optional[int]:
          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			)	->						Dict:
          pass
       @unittest.skip('ESMFold does not support head pruning.'			)
       def    snake_case_			(     self			)	->						str:
          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			)	->						str:
          pass
       @unittest.skip('ESMFold does not output hidden states in the normal way.'			)
       def    snake_case_			(     self			)	->						Any:
          pass
       @unittest.skip('ESMfold does not output hidden states in the normal way.'			)
       def    snake_case_			(     self			)	->						Tuple:
          pass
       @unittest.skip('ESMFold only has one output format.'			)
       def    snake_case_			(     self			)	->						Any:
          pass
       @unittest.skip('This test doesn\'t work for ESMFold and doesn\'t test core functionality'			)
       def    snake_case_			(     self			)	->						List[str]:
          pass
       @unittest.skip('ESMFold does not support input chunking.'			)
       def    snake_case_			(     self			)	->						Any:
          pass
       @unittest.skip('ESMFold doesn\'t respect you and it certainly doesn\'t respect your initialization arguments.'			)
       def    snake_case_			(     self			)	->						int:
          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			)	->						List[str]:
          pass
       @unittest.skip('ESMFold doesn\'t support torchscript compilation.'			)
       def    snake_case_			(     self			)	->						List[Any]:
          pass
       @unittest.skip('ESMFold doesn\'t support data parallel.'			)
       def    snake_case_			(     self			)	->						List[Any]:
          pass






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






@require_torch
class 							lowerCAmelCase_						(    a__				):
       @slow
       def    snake_case_			(     self			)	->						List[Any]:
          UpperCamelCase       :    str		      =      EsmForProteinFolding.from_pretrained('facebook/esmfold_v1'			).float()
          model.eval()
          UpperCamelCase       :    str		      =      torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]			)
          UpperCamelCase       :    List[str]		      =      model(SCREAMING_SNAKE_CASE_			)['positions']
          UpperCamelCase       :    List[str]		      =      torch.tensor([2.58_28, 0.79_93, -10.93_34],     dtype=torch.floataa			)
          self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0],     SCREAMING_SNAKE_CASE_,     atol=1e-4			)			)










import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


_lowerCAmelCase				:   Any						       =						argparse.ArgumentParser('''Stable Diffusion script with intel optimization''', add_help=False)
parser.add_argument('''--dpm''', action='''store_true''', help='''Enable DPMSolver or not''')
parser.add_argument('''--steps''', default=None, type=int, help='''Num inference steps''')
_lowerCAmelCase				:   Tuple						       =						parser.parse_args()


_lowerCAmelCase				:   Any						       =						'''cpu'''
_lowerCAmelCase				:   Dict						       =						'''a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings'''

_lowerCAmelCase				:   Union[str, Any]						       =						'''path-to-your-trained-model'''
_lowerCAmelCase				:   Tuple						       =						StableDiffusionPipeline.from_pretrained(model_id)
if args.dpm:
 _lowerCAmelCase				:   List[str]						       =						DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
_lowerCAmelCase				:   List[Any]						       =						pipe.to(device)

# to channels last
_lowerCAmelCase				:   Any						       =						pipe.unet.to(memory_format=torch.channels_last)
_lowerCAmelCase				:   int						       =						pipe.vae.to(memory_format=torch.channels_last)
_lowerCAmelCase				:   Optional[Any]						       =						pipe.text_encoder.to(memory_format=torch.channels_last)
if pipe.requires_safety_checker:
 _lowerCAmelCase				:   Optional[int]						       =						pipe.safety_checker.to(memory_format=torch.channels_last)

# optimize with ipex
_lowerCAmelCase				:   Tuple						       =						torch.randn(2, 4, 64, 64)
_lowerCAmelCase				:   List[str]						       =						torch.rand(1) * 999
_lowerCAmelCase				:   List[Any]						       =						torch.randn(2, 77, 768)
_lowerCAmelCase				:   int						       =						(sample, timestep, encoder_hidden_status)
try:
 _lowerCAmelCase				:   Tuple						       =						ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example)
except Exception:
 _lowerCAmelCase				:   Optional[Any]						       =						ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True)
_lowerCAmelCase				:   Dict						       =						ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True)
_lowerCAmelCase				:   Dict						       =						ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True)
if pipe.requires_safety_checker:
 _lowerCAmelCase				:   Tuple						       =						ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True)

# compute
_lowerCAmelCase				:   Dict						       =						666
_lowerCAmelCase				:   Tuple						       =						torch.Generator(device).manual_seed(seed)
_lowerCAmelCase				:   Tuple						       =						{'''generator''': generator}
if args.steps is not None:
 _lowerCAmelCase				:   Tuple						       =						args.steps

with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa):
 _lowerCAmelCase				:   int						       =						pipe(prompt, **generate_kwargs).images[0]

# save image
image.save('''generated.png''')
