
"""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  _snake_case	(    unittest.TestCase							):






							def 				__lowercase	(       self    :      Dict    ,			snake_case__    :      bool = False     ):






												"""simple docstring"""



												__a:    Union[str, Any]		     =       1_0

												if only_cross_attention:
																	__a:    Union[str, Any]		     =       1_2
												else:
																	# when only cross attention is not set, the cross attention dim must be the same as the query dim
																	__a:    List[str]		     =       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 				__lowercase	(       self    :      Tuple    ,			snake_case__    :      List[str]    ,			snake_case__    :      int     ):






												"""simple docstring"""



												__a:    Union[str, Any]		     =       2

												__a:    Optional[int]		     =       torch.rand(snake_case__    ,			snake_case__    ,			3    ,			2     )
												__a:    str		     =       torch.rand(snake_case__    ,			4    ,			snake_case__     )
												__a:    List[str]		     =       None

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






							def 				__lowercase	(       self    :      List[str]     ):






												"""simple docstring"""



												torch.manual_seed(0     )

												__a:    List[Any]		     =       self.get_constructor_arguments(only_cross_attention=snake_case__     )
												__a:    Optional[int]		     =       Attention(**snake_case__     )

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

												__a:    List[Any]		     =       self.get_forward_arguments(
												    query_dim=constructor_args["""query_dim"""]    ,			added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]     )

												__a:    int		     =       attn(**snake_case__     )

												# only self attention

												torch.manual_seed(0     )

												__a:    Union[str, Any]		     =       self.get_constructor_arguments(only_cross_attention=snake_case__     )
												__a:    Any		     =       Attention(**snake_case__     )

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

												__a:    Optional[Any]		     =       self.get_forward_arguments(
												    query_dim=constructor_args["""query_dim"""]    ,			added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]     )

												__a:    Optional[Any]		     =       attn(**snake_case__     )

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






class  _snake_case	(    unittest.TestCase							):






							def 				__lowercase	(       self    :      List[str]     ):






												"""simple docstring"""



												__a:    List[Any]		     =       DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""    ,			safety_checker=snake_case__     )

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

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

												__a:    Optional[int]		     =       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(snake_case__     )

																	# can also load the converted weights
																	__a:    Dict		     =       DiffusionPipeline.from_pretrained(snake_case__    ,			device_map="""sequential"""    ,			safety_checker=snake_case__     )

												__a:    Optional[int]		     =       pipe(
												    """foo"""    ,			num_inference_steps=2    ,			generator=torch.Generator("""cpu"""     ).manual_seed(0     )    ,			output_type="""np"""    ,			).images

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


"""simple docstring"""
import unittest

from transformers import BlenderbotTokenizer, BlenderbotTokenizerFast
from transformers.utils import cached_property




class  _snake_case	(    unittest.TestCase							):






							@cached_property
							def 				__lowercase	(       self    :      str     ):






												"""simple docstring"""



												return BlenderbotTokenizer.from_pretrained("""facebook/blenderbot-3B"""     )






							@cached_property
							def 				__lowercase	(       self    :      str     ):






												"""simple docstring"""



												return BlenderbotTokenizerFast.from_pretrained("""facebook/blenderbot-3B"""     )






							def 				__lowercase	(       self    :      int     ):






												"""simple docstring"""



												__a:    Optional[int]		     =       self.tokenizer_ab
												__a:    Union[str, Any]		     =       """ I am a small frog."""
												__a:    str		     =       tok([src_text]    ,			padding=snake_case__    ,			truncation=snake_case__     )["""input_ids"""]
												__a:    Optional[Any]		     =       tok.batch_decode(snake_case__    ,			skip_special_tokens=snake_case__    ,			clean_up_tokenization_spaces=snake_case__     )[0]
												assert src_text == decoded






							def 				__lowercase	(       self    :      List[Any]     ):






												"""simple docstring"""



												__a:    str		     =       self.rust_tokenizer_ab
												__a:    Optional[Any]		     =       """ I am a small frog."""
												__a:    str		     =       tok([src_text]    ,			padding=snake_case__    ,			truncation=snake_case__     )["""input_ids"""]
												__a:    Union[str, Any]		     =       tok.batch_decode(snake_case__    ,			skip_special_tokens=snake_case__    ,			clean_up_tokenization_spaces=snake_case__     )[0]
												assert src_text == decoded






							def 				__lowercase	(       self    :      List[str]     ):






												"""simple docstring"""



												assert self.tokenizer_ab.add_prefix_space
												assert self.tokenizer_ab([""" Sam""", """Sam"""]     ).input_ids == [[5_5_0_2, 2], [5_5_0_2, 2]]






							def 				__lowercase	(       self    :      Any     ):






												"""simple docstring"""



												assert self.rust_tokenizer_ab.add_prefix_space
												assert self.rust_tokenizer_ab([""" Sam""", """Sam"""]     ).input_ids == [[5_5_0_2, 2], [5_5_0_2, 2]]
