






'''simple docstring'''





import os
import unittest

from transformers import RealmTokenizerFast
from transformers.models.bert.tokenization_bert import (
    VOCAB_FILES_NAMES,
    BasicTokenizer,
    WordpieceTokenizer,
    _is_control,
    _is_punctuation,
    _is_whitespace,
)
from transformers.models.realm.tokenization_realm import RealmTokenizer
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english


@require_tokenizers
class 				snake_case__     (			UpperCamelCase      ,							unittest.TestCase):
					a_       =			RealmTokenizer
					a_       =			RealmTokenizerFast
					a_       =			True
					a_       =			True
					a_       =			filter_non_english




					def 						A     (       self       :						List[Any]     )  ->    Any:
								super().setUp()

								UpperCAmelCase_     :   Dict       =   [
								    '''[UNK]''',
								    '''[CLS]''',
								    '''[SEP]''',
								    '''[PAD]''',
								    '''[MASK]''',
								    '''want''',
								    '''##want''',
								    '''##ed''',
								    '''wa''',
								    '''un''',
								    '''runn''',
								    '''##ing''',
								    ''',''',
								    '''low''',
								    '''lowest''',
								]
								UpperCAmelCase_     :   List[str]       =   os.path.join(self.tmpdirname							,				VOCAB_FILES_NAMES['''vocab_file''']     )
								with open(self.vocab_file							,				'''w'''							,				encoding='''utf-8'''     ) as vocab_writer:
											vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens]     )     )




					def 						A     (       self       :						List[Any]							,				_A       :						List[Any]     )  ->    Dict:
								UpperCAmelCase_     :   List[Any]       =   '''UNwant\u00E9d,running'''
								UpperCAmelCase_     :   Any       =   '''unwanted, running'''
								return input_text, output_text




					def 						A     (       self       :						List[Any]     )  ->    Tuple:
								UpperCAmelCase_     :   Optional[int]       =   self.tokenizer_class(self.vocab_file     )

								UpperCAmelCase_     :   str       =   tokenizer.tokenize('''UNwant\u00E9d,running'''     )
								self.assertListEqual(_A							,				['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing''']     )
								self.assertListEqual(tokenizer.convert_tokens_to_ids(_A     )							,				[9, 6, 7, 12, 10, 11]     )




					def 						A     (       self       :						Union[str, Any]     )  ->    str:
								if not self.test_rust_tokenizer:
											return

								UpperCAmelCase_     :   Optional[int]       =   self.get_tokenizer()
								UpperCAmelCase_     :   Optional[Any]       =   self.get_rust_tokenizer()

								UpperCAmelCase_     :   str       =   '''UNwant\u00E9d,running'''

								UpperCAmelCase_     :   List[str]       =   tokenizer.tokenize(_A     )
								UpperCAmelCase_     :   Any       =   rust_tokenizer.tokenize(_A     )
								self.assertListEqual(_A							,				_A     )

								UpperCAmelCase_     :   Tuple       =   tokenizer.encode(_A							,				add_special_tokens=_A     )
								UpperCAmelCase_     :   int       =   rust_tokenizer.encode(_A							,				add_special_tokens=_A     )
								self.assertListEqual(_A							,				_A     )

								UpperCAmelCase_     :   Tuple       =   self.get_rust_tokenizer()
								UpperCAmelCase_     :   List[Any]       =   tokenizer.encode(_A     )
								UpperCAmelCase_     :   Dict       =   rust_tokenizer.encode(_A     )
								self.assertListEqual(_A							,				_A     )

								# With lower casing
								UpperCAmelCase_     :   int       =   self.get_tokenizer(do_lower_case=_A     )
								UpperCAmelCase_     :   str       =   self.get_rust_tokenizer(do_lower_case=_A     )

								UpperCAmelCase_     :   Optional[Any]       =   '''UNwant\u00E9d,running'''

								UpperCAmelCase_     :   str       =   tokenizer.tokenize(_A     )
								UpperCAmelCase_     :   List[str]       =   rust_tokenizer.tokenize(_A     )
								self.assertListEqual(_A							,				_A     )

								UpperCAmelCase_     :   Union[str, Any]       =   tokenizer.encode(_A							,				add_special_tokens=_A     )
								UpperCAmelCase_     :   Optional[int]       =   rust_tokenizer.encode(_A							,				add_special_tokens=_A     )
								self.assertListEqual(_A							,				_A     )

								UpperCAmelCase_     :   Union[str, Any]       =   self.get_rust_tokenizer()
								UpperCAmelCase_     :   str       =   tokenizer.encode(_A     )
								UpperCAmelCase_     :   Any       =   rust_tokenizer.encode(_A     )
								self.assertListEqual(_A							,				_A     )




					def 						A     (       self       :						List[Any]     )  ->    Any:
								UpperCAmelCase_     :   Tuple       =   BasicTokenizer()

								self.assertListEqual(tokenizer.tokenize('''ah\u535A\u63A8zz'''     )							,				['''ah''', '''\u535A''', '''\u63A8''', '''zz''']     )




					def 						A     (       self       :						Optional[Any]     )  ->    Dict:
								UpperCAmelCase_     :   Union[str, Any]       =   BasicTokenizer(do_lower_case=_A     )

								self.assertListEqual(
								    tokenizer.tokenize(''' \tHeLLo!how  \n Are yoU?  '''     )							,				['''hello''', '''!''', '''how''', '''are''', '''you''', '''?''']     )
								self.assertListEqual(tokenizer.tokenize('''H\u00E9llo'''     )							,				['''hello''']     )




					def 						A     (       self       :						str     )  ->    List[Any]:
								UpperCAmelCase_     :   Optional[int]       =   BasicTokenizer(do_lower_case=_A							,				strip_accents=_A     )

								self.assertListEqual(
								    tokenizer.tokenize(''' \tHäLLo!how  \n Are yoU?  '''     )							,				['''hällo''', '''!''', '''how''', '''are''', '''you''', '''?''']     )
								self.assertListEqual(tokenizer.tokenize('''H\u00E9llo'''     )							,				['''h\u00E9llo''']     )




					def 						A     (       self       :						Optional[int]     )  ->    int:
								UpperCAmelCase_     :   int       =   BasicTokenizer(do_lower_case=_A							,				strip_accents=_A     )

								self.assertListEqual(
								    tokenizer.tokenize(''' \tHäLLo!how  \n Are yoU?  '''     )							,				['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?''']     )
								self.assertListEqual(tokenizer.tokenize('''H\u00E9llo'''     )							,				['''hello''']     )




					def 						A     (       self       :						Tuple     )  ->    List[Any]:
								UpperCAmelCase_     :   str       =   BasicTokenizer(do_lower_case=_A     )

								self.assertListEqual(
								    tokenizer.tokenize(''' \tHäLLo!how  \n Are yoU?  '''     )							,				['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?''']     )
								self.assertListEqual(tokenizer.tokenize('''H\u00E9llo'''     )							,				['''hello''']     )




					def 						A     (       self       :						str     )  ->    Optional[int]:
								UpperCAmelCase_     :   str       =   BasicTokenizer(do_lower_case=_A     )

								self.assertListEqual(
								    tokenizer.tokenize(''' \tHeLLo!how  \n Are yoU?  '''     )							,				['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''']     )




					def 						A     (       self       :						str     )  ->    Tuple:
								UpperCAmelCase_     :   Optional[Any]       =   BasicTokenizer(do_lower_case=_A							,				strip_accents=_A     )

								self.assertListEqual(
								    tokenizer.tokenize(''' \tHäLLo!how  \n Are yoU?  '''     )							,				['''HäLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''']     )




					def 						A     (       self       :						Union[str, Any]     )  ->    Any:
								UpperCAmelCase_     :   Tuple       =   BasicTokenizer(do_lower_case=_A							,				strip_accents=_A     )

								self.assertListEqual(
								    tokenizer.tokenize(''' \tHäLLo!how  \n Are yoU?  '''     )							,				['''HaLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''']     )




					def 						A     (       self       :						Dict     )  ->    int:
								UpperCAmelCase_     :   Union[str, Any]       =   BasicTokenizer(do_lower_case=_A							,				never_split=['''[UNK]''']     )

								self.assertListEqual(
								    tokenizer.tokenize(''' \tHeLLo!how  \n Are yoU? [UNK]'''     )							,				['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''', '''[UNK]''']     )




					def 						A     (       self       :						List[Any]     )  ->    Union[str, Any]:
								UpperCAmelCase_     :   Any       =   ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''']

								UpperCAmelCase_     :   Optional[int]       =   {}
								for i, token in enumerate(_A     ):
											UpperCAmelCase_     :   Union[str, Any]       =   i
								UpperCAmelCase_     :   List[str]       =   WordpieceTokenizer(vocab=_A							,				unk_token='''[UNK]'''     )

								self.assertListEqual(tokenizer.tokenize(''''''     )							,				[]     )

								self.assertListEqual(tokenizer.tokenize('''unwanted running'''     )							,				['''un''', '''##want''', '''##ed''', '''runn''', '''##ing''']     )

								self.assertListEqual(tokenizer.tokenize('''unwantedX running'''     )							,				['''[UNK]''', '''runn''', '''##ing''']     )




					def 						A     (       self       :						Tuple     )  ->    Optional[int]:
								self.assertTrue(_is_whitespace(''' '''     )     )
								self.assertTrue(_is_whitespace('''\t'''     )     )
								self.assertTrue(_is_whitespace('''\r'''     )     )
								self.assertTrue(_is_whitespace('''\n'''     )     )
								self.assertTrue(_is_whitespace('''\u00A0'''     )     )

								self.assertFalse(_is_whitespace('''A'''     )     )
								self.assertFalse(_is_whitespace('''-'''     )     )




					def 						A     (       self       :						int     )  ->    str:
								self.assertTrue(_is_control('''\u0005'''     )     )

								self.assertFalse(_is_control('''A'''     )     )
								self.assertFalse(_is_control(''' '''     )     )
								self.assertFalse(_is_control('''\t'''     )     )
								self.assertFalse(_is_control('''\r'''     )     )




					def 						A     (       self       :						List[Any]     )  ->    List[Any]:
								self.assertTrue(_is_punctuation('''-'''     )     )
								self.assertTrue(_is_punctuation('''$'''     )     )
								self.assertTrue(_is_punctuation('''`'''     )     )
								self.assertTrue(_is_punctuation('''.'''     )     )

								self.assertFalse(_is_punctuation('''A'''     )     )
								self.assertFalse(_is_punctuation(''' '''     )     )




					def 						A     (       self       :						str     )  ->    List[Any]:
								UpperCAmelCase_     :   Dict       =   self.get_tokenizer()

								# Example taken from the issue https://github.com/huggingface/tokenizers/issues/340
								self.assertListEqual([tokenizer.tokenize(_A     ) for t in ['''Test''', '''\xad''', '''test''']]							,				[['''[UNK]'''], [], ['''[UNK]''']]     )

								if self.test_rust_tokenizer:
											UpperCAmelCase_     :   List[str]       =   self.get_rust_tokenizer()
											self.assertListEqual(
											    [rust_tokenizer.tokenize(_A     ) for t in ['''Test''', '''\xad''', '''test''']]							,				[['''[UNK]'''], [], ['''[UNK]''']]     )




					@slow
					def 						A     (       self       :						Optional[int]     )  ->    Union[str, Any]:
								UpperCAmelCase_     :   List[Any]       =   self.tokenizer_class.from_pretrained('''bert-base-uncased'''     )

								UpperCAmelCase_     :   Optional[Any]       =   tokenizer.encode('''sequence builders'''							,				add_special_tokens=_A     )
								UpperCAmelCase_     :   List[Any]       =   tokenizer.encode('''multi-sequence build'''							,				add_special_tokens=_A     )

								UpperCAmelCase_     :   Dict       =   tokenizer.build_inputs_with_special_tokens(_A     )
								UpperCAmelCase_     :   int       =   tokenizer.build_inputs_with_special_tokens(_A							,				_A     )

								assert encoded_sentence == [1_01] + text + [1_02]
								assert encoded_pair == [1_01] + text + [1_02] + text_a + [1_02]




					def 						A     (       self       :						Union[str, Any]     )  ->    str:
								for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
											with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})"     ):
														UpperCAmelCase_     :   Union[str, Any]       =   self.rust_tokenizer_class.from_pretrained(_A							,				**_A     )

														UpperCAmelCase_     :   Optional[Any]       =   F"A, naïve {tokenizer_r.mask_token} AllenNLP sentence."
														UpperCAmelCase_     :   List[Any]       =   tokenizer_r.encode_plus(
														    _A							,				return_attention_mask=_A							,				return_token_type_ids=_A							,				return_offsets_mapping=_A							,				add_special_tokens=_A							,				)

														UpperCAmelCase_     :   Tuple       =   tokenizer_r.do_lower_case if hasattr(_A							,				'''do_lower_case'''     ) else False
														UpperCAmelCase_     :   int       =   (
														    [
														        ((0, 0), tokenizer_r.cls_token),
														        ((0, 1), '''A'''),
														        ((1, 2), ''','''),
														        ((3, 5), '''na'''),
														        ((5, 6), '''##ï'''),
														        ((6, 8), '''##ve'''),
														        ((9, 15), tokenizer_r.mask_token),
														        ((16, 21), '''Allen'''),
														        ((21, 23), '''##NL'''),
														        ((23, 24), '''##P'''),
														        ((25, 33), '''sentence'''),
														        ((33, 34), '''.'''),
														        ((0, 0), tokenizer_r.sep_token),
														    ]
														    if not do_lower_case
														    else [
														        ((0, 0), tokenizer_r.cls_token),
														        ((0, 1), '''a'''),
														        ((1, 2), ''','''),
														        ((3, 8), '''naive'''),
														        ((9, 15), tokenizer_r.mask_token),
														        ((16, 21), '''allen'''),
														        ((21, 23), '''##nl'''),
														        ((23, 24), '''##p'''),
														        ((25, 33), '''sentence'''),
														        ((33, 34), '''.'''),
														        ((0, 0), tokenizer_r.sep_token),
														    ]
														)

														self.assertEqual(
														    [e[1] for e in expected_results]							,				tokenizer_r.convert_ids_to_tokens(tokens['''input_ids''']     )     )
														self.assertEqual([e[0] for e in expected_results]							,				tokens['''offset_mapping''']     )





					@slow
					def 						A     (       self       :						Dict     )  ->    int:
								for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
											with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})"     ):
														UpperCAmelCase_     :   List[str]       =   self.rust_tokenizer_class.from_pretrained(_A							,				**_A     )
														UpperCAmelCase_     :   List[Any]       =   self.tokenizer_class.from_pretrained(_A							,				**_A     )
														UpperCAmelCase_     :   List[Any]       =   [['''Hello world!''', '''Nice to meet you!'''], ['''The cute cat.''', '''The adorable dog.''']]

														UpperCAmelCase_     :   Optional[Any]       =   tokenizer_r.batch_encode_candidates(_A							,				max_length=10							,				return_tensors='''np'''     )
														UpperCAmelCase_     :   Union[str, Any]       =   tokenizer_p.batch_encode_candidates(_A							,				max_length=10							,				return_tensors='''np'''     )

														UpperCAmelCase_     :   Tuple       =   (2, 2, 10)

														self.assertEqual(encoded_sentence_r['''input_ids'''].shape							,				_A     )
														self.assertEqual(encoded_sentence_r['''attention_mask'''].shape							,				_A     )
														self.assertEqual(encoded_sentence_r['''token_type_ids'''].shape							,				_A     )

														self.assertEqual(encoded_sentence_p['''input_ids'''].shape							,				_A     )
														self.assertEqual(encoded_sentence_p['''attention_mask'''].shape							,				_A     )
														self.assertEqual(encoded_sentence_p['''token_type_ids'''].shape							,				_A     )










'''simple docstring'''





import os
import tempfile
import unittest

import numpy as np
import torch

from diffusers.models import ModelMixin, UNetaDConditionModel
from diffusers.models.attention_processor import AttnProcessor, LoRAAttnProcessor
from diffusers.utils import (
    floats_tensor,
    logging,
    skip_mps,
    torch_device,
)
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()

_UpperCamelCase		:		Dict								=			logging.get_logger(__name__)
def __UpperCAmelCase (			A			:	List[Any]	,   A			:	bool = True						)  -> List[Any]:
			UpperCAmelCase_     :   Any       =   {}
			for name in model.attn_processors.keys():
						UpperCAmelCase_     :   List[Any]       =   name.endswith('''attn2.processor'''						) and not (
						    name.startswith('''transformer_in'''						) or '''temp_attentions''' in name.split('''.'''						)
						)
						UpperCAmelCase_     :   Optional[Any]       =   model.config.cross_attention_dim if has_cross_attention else None
						if name.startswith('''mid_block'''						):
									UpperCAmelCase_     :   Dict       =   model.config.block_out_channels[-1]
						elif name.startswith('''up_blocks'''						):
									UpperCAmelCase_     :   Any       =   int(name[len('''up_blocks.'''						)]						)
									UpperCAmelCase_     :   int       =   list(reversed(model.config.block_out_channels						)						)[block_id]
						elif name.startswith('''down_blocks'''						):
									UpperCAmelCase_     :   Dict       =   int(name[len('''down_blocks.'''						)]						)
									UpperCAmelCase_     :   Any       =   model.config.block_out_channels[block_id]
						elif name.startswith('''transformer_in'''						):
									# Note that the `8 * ...` comes from: https://github.com/huggingface/diffusers/blob/7139f0e874f10b2463caa8cbd585762a309d12d6/src/diffusers/models/unet_3d_condition.py#L148
									UpperCAmelCase_     :   int       =   8 * model.config.attention_head_dim

						UpperCAmelCase_     :   Optional[int]       =   LoRAAttnProcessor(hidden_size=A	,   cross_attention_dim=A						)
						UpperCAmelCase_     :   Tuple       =   lora_attn_procs[name].to(model.device						)

						if mock_weights:
									# add 1 to weights to mock trained weights
									with torch.no_grad():
												lora_attn_procs[name].to_q_lora.up.weight += 1
												lora_attn_procs[name].to_k_lora.up.weight += 1
												lora_attn_procs[name].to_v_lora.up.weight += 1
												lora_attn_procs[name].to_out_lora.up.weight += 1

			return lora_attn_procs







@skip_mps
class 				snake_case__     (			UpperCamelCase      ,							UpperCamelCase      ,							unittest.TestCase):
					a_       =			UNetaDConditionModel
					a_       =			"sample"




					@property
					def 						A     (       self       :						Tuple     )  ->    int:
								UpperCAmelCase_     :   Optional[Any]       =   4
								UpperCAmelCase_     :   int       =   4
								UpperCAmelCase_     :   List[Any]       =   4
								UpperCAmelCase_     :   int       =   (32, 32)

								UpperCAmelCase_     :   Optional[int]       =   floats_tensor((batch_size, num_channels, num_frames) + sizes     ).to(_A     )
								UpperCAmelCase_     :   Any       =   torch.tensor([10]     ).to(_A     )
								UpperCAmelCase_     :   Optional[Any]       =   floats_tensor((batch_size, 4, 32)     ).to(_A     )

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




					@property
					def 						A     (       self       :						Dict     )  ->    Dict:
								return (4, 4, 32, 32)




					@property
					def 						A     (       self       :						Union[str, Any]     )  ->    Union[str, Any]:
								return (4, 4, 32, 32)




					def 						A     (       self       :						Dict     )  ->    List[Any]:
								UpperCAmelCase_     :   List[str]       =   {
								    '''block_out_channels''': (32, 64),
								    '''down_block_types''': (
								        '''CrossAttnDownBlock3D''',
								        '''DownBlock3D''',
								    ),
								    '''up_block_types''': ('''UpBlock3D''', '''CrossAttnUpBlock3D'''),
								    '''cross_attention_dim''': 32,
								    '''attention_head_dim''': 8,
								    '''out_channels''': 4,
								    '''in_channels''': 4,
								    '''layers_per_block''': 1,
								    '''sample_size''': 32,
								}
								UpperCAmelCase_     :   Union[str, Any]       =   self.dummy_input
								return init_dict, inputs_dict




					@unittest.skipIf(
					    torch_device != '''cuda''' or not is_xformers_available()							,				reason='''XFormers attention is only available with CUDA and `xformers` installed'''							,				)
					def 						A     (       self       :						Union[str, Any]     )  ->    Optional[int]:
								UpperCAmelCase_  ,     UpperCAmelCase_     :   Optional[int]       =   self.prepare_init_args_and_inputs_for_common()
								UpperCAmelCase_     :   List[str]       =   self.model_class(**_A     )

								model.enable_xformers_memory_efficient_attention()

								assert (
								    model.mid_block.attentions[0].transformer_blocks[0].attna.processor.__class__.__name__
								    == "XFormersAttnProcessor"
								), "xformers is not enabled"




					def 						A     (       self       :						Optional[Any]     )  ->    List[str]:
								UpperCAmelCase_  ,     UpperCAmelCase_     :   Tuple       =   self.prepare_init_args_and_inputs_for_common()

								UpperCAmelCase_     :   str       =   32

								UpperCAmelCase_     :   Union[str, Any]       =   self.model_class(**_A     )
								model.to(_A     )
								model.eval()

								with torch.no_grad():
											UpperCAmelCase_     :   Any       =   model(**_A     )

											if isinstance(_A							,				_A     ):
														UpperCAmelCase_     :   Optional[Any]       =   output.sample

								self.assertIsNotNone(_A     )
								UpperCAmelCase_     :   int       =   inputs_dict['''sample'''].shape
								self.assertEqual(output.shape							,				_A							,				'''Input and output shapes do not match'''     )




					def 						A     (       self       :						Union[str, Any]     )  ->    Optional[int]:
								UpperCAmelCase_  ,     UpperCAmelCase_     :   Optional[Any]       =   self.prepare_init_args_and_inputs_for_common()
								UpperCAmelCase_     :   Optional[Any]       =   self.model_class(**_A     )
								model.to(_A     )
								model.eval()

								with torch.no_grad():
											# Warmup pass when using mps (see #372)
											if torch_device == "mps" and isinstance(_A							,				_A     ):
														model(**self.dummy_input     )

											UpperCAmelCase_     :   List[Any]       =   model(**_A     )
											if isinstance(_A							,				_A     ):
														UpperCAmelCase_     :   List[Any]       =   first.sample

											UpperCAmelCase_     :   Tuple       =   model(**_A     )
											if isinstance(_A							,				_A     ):
														UpperCAmelCase_     :   Union[str, Any]       =   second.sample

								UpperCAmelCase_     :   Tuple       =   first.cpu().numpy()
								UpperCAmelCase_     :   Tuple       =   second.cpu().numpy()
								UpperCAmelCase_     :   Tuple       =   out_a[~np.isnan(_A     )]
								UpperCAmelCase_     :   List[str]       =   out_a[~np.isnan(_A     )]
								UpperCAmelCase_     :   Optional[int]       =   np.amax(np.abs(out_a - out_a     )     )
								self.assertLessEqual(_A							,				1e-5     )




					def 						A     (       self       :						str     )  ->    str:
								UpperCAmelCase_  ,     UpperCAmelCase_     :   Optional[Any]       =   self.prepare_init_args_and_inputs_for_common()

								UpperCAmelCase_     :   Union[str, Any]       =   8

								UpperCAmelCase_     :   Tuple       =   self.model_class(**_A     )
								model.to(_A     )
								model.eval()

								model.set_attention_slice('''auto'''     )
								with torch.no_grad():
											UpperCAmelCase_     :   Dict       =   model(**_A     )
								assert output is not None

								model.set_attention_slice('''max'''     )
								with torch.no_grad():
											UpperCAmelCase_     :   Union[str, Any]       =   model(**_A     )
								assert output is not None

								model.set_attention_slice(2     )
								with torch.no_grad():
											UpperCAmelCase_     :   int       =   model(**_A     )
								assert output is not None




					def 						A     (       self       :						List[Any]     )  ->    Any:
								UpperCAmelCase_  ,     UpperCAmelCase_     :   List[Any]       =   self.prepare_init_args_and_inputs_for_common()

								UpperCAmelCase_     :   Dict       =   8

								UpperCAmelCase_     :   List[Any]       =   self.model_class(**_A     )
								model.to(_A     )

								with torch.no_grad():
											UpperCAmelCase_     :   int       =   model(**_A     ).sample

								UpperCAmelCase_     :   Any       =   create_lora_layers(_A     )

								# make sure we can set a list of attention processors
								model.set_attn_processor(_A     )
								model.to(_A     )

								# test that attn processors can be set to itself
								model.set_attn_processor(model.attn_processors     )

								with torch.no_grad():
											UpperCAmelCase_     :   int       =   model(**_A							,				cross_attention_kwargs={'''scale''': 0.0}     ).sample
											UpperCAmelCase_     :   int       =   model(**_A							,				cross_attention_kwargs={'''scale''': 0.5}     ).sample
											UpperCAmelCase_     :   int       =   model(**_A							,				cross_attention_kwargs={'''scale''': 0.5}     ).sample

								assert (samplea - samplea).abs().max() < 3e-3
								assert (samplea - samplea).abs().max() < 3e-3

								# sample 2 and sample 3 should be different
								assert (samplea - samplea).abs().max() > 3e-3




					def 						A     (       self       :						Union[str, Any]     )  ->    Tuple:
								UpperCAmelCase_  ,     UpperCAmelCase_     :   List[str]       =   self.prepare_init_args_and_inputs_for_common()

								UpperCAmelCase_     :   str       =   8

								torch.manual_seed(0     )
								UpperCAmelCase_     :   Optional[int]       =   self.model_class(**_A     )
								model.to(_A     )

								with torch.no_grad():
											UpperCAmelCase_     :   Union[str, Any]       =   model(**_A     ).sample

								UpperCAmelCase_     :   int       =   create_lora_layers(_A     )
								model.set_attn_processor(_A     )

								with torch.no_grad():
											UpperCAmelCase_     :   Optional[int]       =   model(**_A							,				cross_attention_kwargs={'''scale''': 0.5}     ).sample

								with tempfile.TemporaryDirectory() as tmpdirname:
											model.save_attn_procs(_A     )
											self.assertTrue(os.path.isfile(os.path.join(_A							,				'''pytorch_lora_weights.bin'''     )     )     )
											torch.manual_seed(0     )
											UpperCAmelCase_     :   Optional[Any]       =   self.model_class(**_A     )
											new_model.to(_A     )
											new_model.load_attn_procs(_A     )

								with torch.no_grad():
											UpperCAmelCase_     :   Dict       =   new_model(**_A							,				cross_attention_kwargs={'''scale''': 0.5}     ).sample

								assert (sample - new_sample).abs().max() < 1e-3

								# LoRA and no LoRA should NOT be the same
								assert (sample - old_sample).abs().max() > 1e-4




					def 						A     (       self       :						Tuple     )  ->    Union[str, Any]:
								UpperCAmelCase_  ,     UpperCAmelCase_     :   Dict       =   self.prepare_init_args_and_inputs_for_common()

								UpperCAmelCase_     :   List[str]       =   8

								torch.manual_seed(0     )
								UpperCAmelCase_     :   str       =   self.model_class(**_A     )
								model.to(_A     )

								with torch.no_grad():
											UpperCAmelCase_     :   Dict       =   model(**_A     ).sample

								UpperCAmelCase_     :   Tuple       =   create_lora_layers(_A     )
								model.set_attn_processor(_A     )

								with torch.no_grad():
											UpperCAmelCase_     :   Dict       =   model(**_A							,				cross_attention_kwargs={'''scale''': 0.5}     ).sample

								with tempfile.TemporaryDirectory() as tmpdirname:
											model.save_attn_procs(_A							,				safe_serialization=_A     )
											self.assertTrue(os.path.isfile(os.path.join(_A							,				'''pytorch_lora_weights.safetensors'''     )     )     )
											torch.manual_seed(0     )
											UpperCAmelCase_     :   Dict       =   self.model_class(**_A     )
											new_model.to(_A     )
											new_model.load_attn_procs(_A     )

								with torch.no_grad():
											UpperCAmelCase_     :   Union[str, Any]       =   new_model(**_A							,				cross_attention_kwargs={'''scale''': 0.5}     ).sample

								assert (sample - new_sample).abs().max() < 3e-3

								# LoRA and no LoRA should NOT be the same
								assert (sample - old_sample).abs().max() > 1e-4




					def 						A     (       self       :						Union[str, Any]     )  ->    Dict:
								# enable deterministic behavior for gradient checkpointing
								UpperCAmelCase_  ,     UpperCAmelCase_     :   List[Any]       =   self.prepare_init_args_and_inputs_for_common()

								UpperCAmelCase_     :   Any       =   8

								torch.manual_seed(0     )
								UpperCAmelCase_     :   Optional[Any]       =   self.model_class(**_A     )
								model.to(_A     )

								UpperCAmelCase_     :   str       =   create_lora_layers(_A							,				mock_weights=_A     )
								model.set_attn_processor(_A     )
								# Saving as torch, properly reloads with directly filename
								with tempfile.TemporaryDirectory() as tmpdirname:
											model.save_attn_procs(_A     )
											self.assertTrue(os.path.isfile(os.path.join(_A							,				'''pytorch_lora_weights.bin'''     )     )     )
											torch.manual_seed(0     )
											UpperCAmelCase_     :   Union[str, Any]       =   self.model_class(**_A     )
											new_model.to(_A     )
											new_model.load_attn_procs(_A							,				weight_name='''pytorch_lora_weights.bin'''     )




					def 						A     (       self       :						Optional[Any]     )  ->    Optional[int]:
								UpperCAmelCase_  ,     UpperCAmelCase_     :   Optional[int]       =   self.prepare_init_args_and_inputs_for_common()

								UpperCAmelCase_     :   List[Any]       =   8

								torch.manual_seed(0     )
								UpperCAmelCase_     :   Dict       =   self.model_class(**_A     )
								model.to(_A     )

								UpperCAmelCase_     :   Tuple       =   create_lora_layers(_A							,				mock_weights=_A     )
								model.set_attn_processor(_A     )
								# Saving as torch, properly reloads with directly filename
								with tempfile.TemporaryDirectory() as tmpdirname:
											model.save_attn_procs(_A     )
											self.assertTrue(os.path.isfile(os.path.join(_A							,				'''pytorch_lora_weights.bin'''     )     )     )
											torch.manual_seed(0     )
											UpperCAmelCase_     :   Optional[Any]       =   self.model_class(**_A     )
											new_model.to(_A     )
											with self.assertRaises(_A     ) as e:
														new_model.load_attn_procs(_A							,				use_safetensors=_A     )
											self.assertIn('''Error no file named pytorch_lora_weights.safetensors'''							,				str(e.exception     )     )




					def 						A     (       self       :						int     )  ->    Tuple:
								UpperCAmelCase_  ,     UpperCAmelCase_     :   Any       =   self.prepare_init_args_and_inputs_for_common()

								UpperCAmelCase_     :   Dict       =   8

								torch.manual_seed(0     )
								UpperCAmelCase_     :   Optional[Any]       =   self.model_class(**_A     )
								model.to(_A     )

								with torch.no_grad():
											UpperCAmelCase_     :   str       =   model(**_A     ).sample

								UpperCAmelCase_     :   str       =   create_lora_layers(_A     )
								model.set_attn_processor(_A     )

								with torch.no_grad():
											UpperCAmelCase_     :   Optional[int]       =   model(**_A							,				cross_attention_kwargs={'''scale''': 0.0}     ).sample

								model.set_attn_processor(AttnProcessor()     )

								with torch.no_grad():
											UpperCAmelCase_     :   int       =   model(**_A     ).sample

								assert (sample - new_sample).abs().max() < 1e-4
								assert (sample - old_sample).abs().max() < 3e-3




					@unittest.skipIf(
					    torch_device != '''cuda''' or not is_xformers_available()							,				reason='''XFormers attention is only available with CUDA and `xformers` installed'''							,				)
					def 						A     (       self       :						str     )  ->    Optional[Any]:
								# enable deterministic behavior for gradient checkpointing
								UpperCAmelCase_  ,     UpperCAmelCase_     :   Any       =   self.prepare_init_args_and_inputs_for_common()

								UpperCAmelCase_     :   Dict       =   4

								torch.manual_seed(0     )
								UpperCAmelCase_     :   List[Any]       =   self.model_class(**_A     )
								model.to(_A     )
								UpperCAmelCase_     :   List[str]       =   create_lora_layers(_A     )
								model.set_attn_processor(_A     )

								# default
								with torch.no_grad():
											UpperCAmelCase_     :   List[str]       =   model(**_A     ).sample

											model.enable_xformers_memory_efficient_attention()
											UpperCAmelCase_     :   List[str]       =   model(**_A     ).sample

											model.disable_xformers_memory_efficient_attention()
											UpperCAmelCase_     :   Any       =   model(**_A     ).sample

								assert (sample - on_sample).abs().max() < 1e-4
								assert (sample - off_sample).abs().max() < 1e-4





					def 						A     (       self       :						Tuple     )  ->    Union[str, Any]:
								UpperCAmelCase_  ,     UpperCAmelCase_     :   Optional[int]       =   self.prepare_init_args_and_inputs_for_common()
								UpperCAmelCase_     :   int       =   32

								UpperCAmelCase_     :   Any       =   self.model_class(**_A     )
								model.to(_A     )
								model.eval()

								with torch.no_grad():
											UpperCAmelCase_     :   Tuple       =   model(**_A     )[0]

								model.enable_forward_chunking()
								with torch.no_grad():
											UpperCAmelCase_     :   Optional[Any]       =   model(**_A     )[0]

								self.assertEqual(output.shape							,				output_a.shape							,				'''Shape doesn\'t match'''     )
								assert np.abs(output.cpu() - output_a.cpu()     ).max() < 1e-2


# (todo: sayakpaul) implement SLOW tests.


