





'''simple docstring'''


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


_lowerCamelCase				:	Union[str, Any]				    =       logging.get_logger(__name__)

_lowerCamelCase				:	Optional[Any]				    =       {
    'facebook/opt-125m': 'https://huggingface.co/facebook/opt-125m/blob/main/config.json',
    'facebook/opt-350m': 'https://huggingface.co/facebook/opt-350m/blob/main/config.json',
    'facebook/opt-1.3b': 'https://huggingface.co/facebook/opt-1.3b/blob/main/config.json',
    'facebook/opt-2.7b': 'https://huggingface.co/facebook/opt-2.7b/blob/main/config.json',
    'facebook/opt-6.7b': 'https://huggingface.co/facebook/opt-6.7b/blob/main/config.json',
    'facebook/opt-13b': 'https://huggingface.co/facebook/opt-13b/blob/main/config.json',
}



class 							__UpperCAmelCase  (       A__						):







		'''simple docstring'''







		__lowerCAmelCase 				=    '''opt'''
		__lowerCAmelCase 				=    ['''past_key_values''']







		def __init__(self						:      List[str]      ,      _lowerCAmelCase						:      Union[str, Any]=5_0272      ,      _lowerCAmelCase						:      Optional[int]=768      ,      _lowerCAmelCase						:      int=12      ,      _lowerCAmelCase						:      Optional[int]=3072      ,      _lowerCAmelCase						:      str=2048      ,      _lowerCAmelCase						:      Dict=True      ,      _lowerCAmelCase						:      List[Any]=False      ,      _lowerCAmelCase						:      Union[str, Any]=None      ,      _lowerCAmelCase						:      Any=0.1      ,      _lowerCAmelCase						:      Optional[int]=0.0      ,      _lowerCAmelCase						:      str=12      ,      _lowerCAmelCase						:      Optional[Any]="relu"      ,      _lowerCAmelCase						:      Optional[Any]=0.0      ,      _lowerCAmelCase						:      int=0.02      ,      _lowerCAmelCase						:      int=True      ,      _lowerCAmelCase						:      Optional[int]=1      ,      _lowerCAmelCase						:      int=2      ,      _lowerCAmelCase						:      Dict=2      ,      _lowerCAmelCase						:      Union[str, Any]=True      ,      _lowerCAmelCase						:      List[str]=True      ,      **_lowerCAmelCase						:      int      ,      ):
			super().__init__(
			    pad_token_id=_lowerCAmelCase      ,      bos_token_id=_lowerCAmelCase      ,      eos_token_id=_lowerCAmelCase      ,      **_lowerCAmelCase      ,      )
			A							  =      vocab_size
			A							  =      max_position_embeddings
			A							  =      num_attention_heads
			A							  =      word_embed_proj_dim if word_embed_proj_dim is not None else hidden_size
			A							  =      ffn_dim
			A							  =      hidden_size
			A							  =      num_hidden_layers
			A							  =      dropout
			A							  =      attention_dropout
			A							  =      activation_function
			A							  =      init_std
			A							  =      layerdrop
			A							  =      use_cache
			A							  =      do_layer_norm_before
			# We keep these variables at `True` for backward compatibility.
			A							  =      enable_bias
			A							  =      layer_norm_elementwise_affine

			# Note that the only purpose of `_remove_final_layer_norm` is to keep backward compatibility
			# with checkpoints that have been fine-tuned before transformers v4.20.1
			# see https://github.com/facebookresearch/metaseq/pull/164
			A							  =      _remove_final_layer_norm








'''simple docstring'''


import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor



class 							__UpperCAmelCase  (       unittest.TestCase						):







		'''simple docstring'''







		def        A     (self						:      Optional[int]      ,      _lowerCAmelCase						:      bool = False      ):
			A							  =      10

			if only_cross_attention:
				A							  =      12
			else:
				# when only cross attention is not set, the cross attention dim must be the same as the query dim
				A							  =      query_dim

			return {
			    "query_dim": query_dim,
			    "cross_attention_dim": cross_attention_dim,
			    "heads": 2,
			    "dim_head": 4,
			    "added_kv_proj_dim": 6,
			    "norm_num_groups": 1,
			    "only_cross_attention": only_cross_attention,
			    "processor": AttnAddedKVProcessor(),
			}







		def        A     (self						:      Optional[int]      ,      _lowerCAmelCase						:      Dict      ,      _lowerCAmelCase						:      Any      ):
			A							  =      2

			A							  =      torch.rand(_lowerCAmelCase      ,      _lowerCAmelCase      ,      3      ,      2      )
			A							  =      torch.rand(_lowerCAmelCase      ,      4      ,      _lowerCAmelCase      )
			A							  =      None

			return {
			    "hidden_states": hidden_states,
			    "encoder_hidden_states": encoder_hidden_states,
			    "attention_mask": attention_mask,
			}







		def        A     (self						:      Optional[Any]      ):
			# self and cross attention

			torch.manual_seed(0      )

			A							  =      self.get_constructor_arguments(only_cross_attention=_lowerCAmelCase      )
			A							  =      Attention(**_lowerCAmelCase      )

			self.assertTrue(attn.to_k is not None      )
			self.assertTrue(attn.to_v is not None      )

			A							  =      self.get_forward_arguments(
			    query_dim=constructor_args["""query_dim"""]      ,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )

			A							  =      attn(**_lowerCAmelCase      )

			# only self attention

			torch.manual_seed(0      )

			A							  =      self.get_constructor_arguments(only_cross_attention=_lowerCAmelCase      )
			A							  =      Attention(**_lowerCAmelCase      )

			self.assertTrue(attn.to_k is None      )
			self.assertTrue(attn.to_v is None      )

			A							  =      self.get_forward_arguments(
			    query_dim=constructor_args["""query_dim"""]      ,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )

			A							  =      attn(**_lowerCAmelCase      )

			self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()      )






class 							__UpperCAmelCase  (       unittest.TestCase						):







		'''simple docstring'''







		def        A     (self						:      Any      ):
			A							  =      DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""      ,      safety_checker=_lowerCAmelCase      )

			A							  =      pipe(
			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images

			# the initial conversion succeeds
			A							  =      DiffusionPipeline.from_pretrained(
			    """hf-internal-testing/tiny-stable-diffusion-pipe"""      ,      device_map="""sequential"""      ,      safety_checker=_lowerCAmelCase      )

			A							  =      pipe(
			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images

			with tempfile.TemporaryDirectory() as tmpdir:
				# save the converted model
				pipe.save_pretrained(_lowerCAmelCase      )

				# can also load the converted weights
				A							  =      DiffusionPipeline.from_pretrained(_lowerCAmelCase      ,      device_map="""sequential"""      ,      safety_checker=_lowerCAmelCase      )

			A							  =      pipe(
			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images

			self.assertTrue(np.allclose(_lowerCAmelCase      ,      _lowerCAmelCase      ,      atol=1e-5      )      )
			self.assertTrue(np.allclose(_lowerCAmelCase      ,      _lowerCAmelCase      ,      atol=1e-5      )      )

