






'''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 _lowercase      (							unittest.TestCase						):




							def 						lowerCamelCase_							(				self:							List[str]     ,				UpperCamelCase__:							bool = False	):
														lowerCamelCase__   :      int			    =			10

														if only_cross_attention:
																					lowerCamelCase__   :      str			    =			12
														else:
																					# when only cross attention is not set, the cross attention dim must be the same as the query dim
																					lowerCamelCase__   :      Any			    =			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 						lowerCamelCase_							(				self:							List[Any]     ,				UpperCamelCase__:							Optional[Any]     ,				UpperCamelCase__:							int	):
														lowerCamelCase__   :      Optional[int]			    =			2

														lowerCamelCase__   :      Dict			    =			torch.rand(UpperCamelCase__     ,				UpperCamelCase__     ,				3     ,				2	)
														lowerCamelCase__   :      Dict			    =			torch.rand(UpperCamelCase__     ,				4     ,				UpperCamelCase__	)
														lowerCamelCase__   :      Union[str, Any]			    =			None

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




							def 						lowerCamelCase_							(				self:							List[Any]	):
														# self and cross attention

														torch.manual_seed(0	)

														lowerCamelCase__   :      Dict			    =			self.get_constructor_arguments(only_cross_attention=UpperCamelCase__	)
														lowerCamelCase__   :      List[Any]			    =			Attention(**UpperCamelCase__	)

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

														lowerCamelCase__   :      Dict			    =			self.get_forward_arguments(
														    query_dim=constructor_args["""query_dim"""]     ,				added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]	)

														lowerCamelCase__   :      Optional[Any]			    =			attn(**UpperCamelCase__	)

														# only self attention

														torch.manual_seed(0	)

														lowerCamelCase__   :      Dict			    =			self.get_constructor_arguments(only_cross_attention=UpperCamelCase__	)
														lowerCamelCase__   :      Union[str, Any]			    =			Attention(**UpperCamelCase__	)

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

														lowerCamelCase__   :      Union[str, Any]			    =			self.get_forward_arguments(
														    query_dim=constructor_args["""query_dim"""]     ,				added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]	)

														lowerCamelCase__   :      List[str]			    =			attn(**UpperCamelCase__	)

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



class _lowercase      (							unittest.TestCase						):




							def 						lowerCamelCase_							(				self:							Optional[Any]	):
														lowerCamelCase__   :      List[Any]			    =			DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""     ,				safety_checker=UpperCamelCase__	)

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

														# the initial conversion succeeds
														lowerCamelCase__   :      Union[str, Any]			    =			DiffusionPipeline.from_pretrained(
														    """hf-internal-testing/tiny-stable-diffusion-pipe"""     ,				device_map="""sequential"""     ,				safety_checker=UpperCamelCase__	)

														lowerCamelCase__   :      Any			    =			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(UpperCamelCase__	)

																					# can also load the converted weights
																					lowerCamelCase__   :      List[str]			    =			DiffusionPipeline.from_pretrained(UpperCamelCase__     ,				device_map="""sequential"""     ,				safety_checker=UpperCamelCase__	)

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

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









'''simple docstring'''







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


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

_A      :     Tuple       	={
    '''tau/splinter-base''': '''https://huggingface.co/tau/splinter-base/resolve/main/config.json''',
    '''tau/splinter-base-qass''': '''https://huggingface.co/tau/splinter-base-qass/resolve/main/config.json''',
    '''tau/splinter-large''': '''https://huggingface.co/tau/splinter-large/resolve/main/config.json''',
    '''tau/splinter-large-qass''': '''https://huggingface.co/tau/splinter-large-qass/resolve/main/config.json''',
    # See all Splinter models at https://huggingface.co/models?filter=splinter
}

class _lowercase      (							_lowercase						):
							a							 =			"""splinter"""




							def __init__(				self:							List[str]     ,				UpperCamelCase__:							List[Any]=30_522     ,				UpperCamelCase__:							Union[str, Any]=768     ,				UpperCamelCase__:							str=12     ,				UpperCamelCase__:							Dict=12     ,				UpperCamelCase__:							Tuple=3_072     ,				UpperCamelCase__:							Optional[int]="gelu"     ,				UpperCamelCase__:							Optional[int]=0.1     ,				UpperCamelCase__:							str=0.1     ,				UpperCamelCase__:							List[Any]=512     ,				UpperCamelCase__:							List[str]=2     ,				UpperCamelCase__:							Any=0.02     ,				UpperCamelCase__:							Optional[Any]=1e-12     ,				UpperCamelCase__:							Dict=True     ,				UpperCamelCase__:							str=0     ,				UpperCamelCase__:							int=104     ,				**UpperCamelCase__:							Any     ,				):
														super().__init__(pad_token_id=UpperCamelCase__     ,				**UpperCamelCase__	)

														lowerCamelCase__   :      Union[str, Any]			    =			vocab_size
														lowerCamelCase__   :      str			    =			max_position_embeddings
														lowerCamelCase__   :      Tuple			    =			hidden_size
														lowerCamelCase__   :      Dict			    =			num_hidden_layers
														lowerCamelCase__   :      Tuple			    =			num_attention_heads
														lowerCamelCase__   :      Tuple			    =			intermediate_size
														lowerCamelCase__   :      Any			    =			hidden_act
														lowerCamelCase__   :      int			    =			hidden_dropout_prob
														lowerCamelCase__   :      List[str]			    =			attention_probs_dropout_prob
														lowerCamelCase__   :      Dict			    =			initializer_range
														lowerCamelCase__   :      List[Any]			    =			type_vocab_size
														lowerCamelCase__   :      Tuple			    =			layer_norm_eps
														lowerCamelCase__   :      Optional[Any]			    =			use_cache
														lowerCamelCase__   :      Optional[Any]			    =			question_token_id

