


"""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        a_       (    self					:				List[str]			,      a__					:				bool = False						)				->  Dict:





						'''simple docstring'''







						_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					:				Union[str, Any]			,      a__					:				int			,      a__					:				Tuple						)				->  List[str]:





						'''simple docstring'''







						_A         =	2

						_A         =	torch.rand(a__			,      a__			,      3			,      2						)
						_A         =	torch.rand(a__			,      4			,      a__						)
						_A         =	None

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







		def        a_       (    self					:				Tuple						)				->  Optional[int]:





						'''simple docstring'''







						torch.manual_seed(0						)

						_A         =	self.get_constructor_arguments(only_cross_attention=a__						)
						_A         =	Attention(**a__						)

						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(**a__						)

						# only self attention

						torch.manual_seed(0						)

						_A         =	self.get_constructor_arguments(only_cross_attention=a__						)
						_A         =	Attention(**a__						)

						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(**a__						)

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





class snake_case					(     unittest.TestCase):







		def        a_       (    self					:				Dict						)				->  Optional[int]:





						'''simple docstring'''







						_A         =	DiffusionPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-pipe"			,      safety_checker=a__						)

						_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=a__						)

						_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(a__						)

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

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

						self.assertTrue(np.allclose(a__			,      a__			,      atol=1E-5						)						)
						self.assertTrue(np.allclose(a__			,      a__			,      atol=1E-5						)						)



"""simple docstring"""


import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin
class snake_case					(     _UpperCamelCase):
		__UpperCamelCase							=	'AutoFeatureExtractor'
		__UpperCamelCase							=	'Speech2Text2Tokenizer'







		def __init__(    self					:				int			,      a__					:				Tuple			,      a__					:				Optional[Any]						)				->  str:





						'''simple docstring'''







						super().__init__(a__			,      a__						)
						_A         =	self.feature_extractor
						_A         =	False







		def __call__(    self					:				Optional[int]			,      *a__					:				List[str]			,      **a__					:				Any						)				->  List[Any]:





						'''simple docstring'''







						if self._in_target_context_manager:
										return self.current_processor(*a__			,      **a__						)

						if "raw_speech" in kwargs:
										warnings.warn("Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead."						)
										_A         =	kwargs.pop("raw_speech"						)
						else:
										_A         =	kwargs.pop("audio"			,      a__						)
						_A         =	kwargs.pop("sampling_rate"			,      a__						)
						_A         =	kwargs.pop("text"			,      a__						)
						if len(a__						) > 0:
										_A         =	args[0]
										_A         =	args[1:]

						if audio is None and text is None:
										raise ValueError("You need to specify either an `audio` or `text` input to process."						)

						if audio is not None:
										_A         =	self.feature_extractor(a__			,      *a__			,      sampling_rate=a__			,      **a__						)
						if text is not None:
										_A         =	self.tokenizer(a__			,      **a__						)

						if text is None:
										return inputs
						elif audio is None:
										return encodings
						else:
										_A         =	encodings["input_ids"]
										return inputs







		def        a_       (    self					:				List[Any]			,      *a__					:				Tuple			,      **a__					:				List[Any]						)				->  List[Any]:





						'''simple docstring'''







						return self.tokenizer.batch_decode(*a__			,      **a__						)







		def        a_       (    self					:				Any			,      *a__					:				Dict			,      **a__					:				Tuple						)				->  Union[str, Any]:





						'''simple docstring'''







						return self.tokenizer.decode(*a__			,      **a__						)







		@contextmanager
		def        a_       (    self					:				Optional[int]						)				->  Optional[int]:





						'''simple docstring'''







						warnings.warn(
						    "`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your "
						    "labels by using the argument `text` of the regular `__call__` method (either in the same call as "
						    "your audio inputs, or in a separate call."						)
						_A         =	True
						_A         =	self.tokenizer
						yield
						_A         =	self.feature_extractor
						_A         =	False