





'''simple docstring'''






import gc
import unittest

from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline
from diffusers.utils import is_flax_available, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
		import jax
		import jax.numpy as jnp
		from flax.jax_utils import replicate
		from flax.training.common_utils import shard







@slow
@require_flax
class 			lowerCamelCase					(						unittest.TestCase  ):


					def    SCREAMING_SNAKE_CASE_		(					self	:  Union[str, Any]       ):





											'''simple docstring'''





											super().tearDown()
											gc.collect()


					def    SCREAMING_SNAKE_CASE_		(					self	:  Tuple       ):





											'''simple docstring'''





											_snake_case:   Union[str, Any]  					=		FlaxStableDiffusionPipeline.from_pretrained(
											    'stabilityai/stable-diffusion-2'       ,     revision='bf16'       ,     dtype=jnp.bfloataa       ,     )

											_snake_case:   int  					=		'A painting of a squirrel eating a burger'
											_snake_case:   Optional[Any]  					=		jax.device_count()
											_snake_case:   Dict  					=		num_samples * [prompt]
											_snake_case:   int  					=		sd_pipe.prepare_inputs(__snake_case       )

											_snake_case:   Optional[Any]  					=		replicate(__snake_case       )
											_snake_case:   Dict  					=		shard(__snake_case       )

											_snake_case:   Any  					=		jax.random.PRNGKey(0       )
											_snake_case:   Any  					=		jax.random.split(__snake_case       ,     jax.device_count()       )

											_snake_case:   Optional[Any]  					=		sd_pipe(__snake_case       ,     __snake_case       ,     __snake_case       ,     num_inference_steps=25       ,     jit=__snake_case       )[0]
											assert images.shape == (jax.device_count(), 1, 7_68, 7_68, 3)

											_snake_case:   Any  					=		images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]       )
											_snake_case:   Dict  					=		images[0, 2_53:2_56, 2_53:2_56, -1]

											_snake_case:   List[Any]  					=		jnp.asarray(jax.device_get(image_slice.flatten()       )       )
											_snake_case:   List[str]  					=		jnp.array([0.4_238, 0.4_414, 0.4_395, 0.4_453, 0.4_629, 0.4_590, 0.4_531, 0.45_508, 0.4_512]       )
											print(f'''output_slice: {output_slice}'''       )
											assert jnp.abs(output_slice - expected_slice       ).max() < 1e-2




					def    SCREAMING_SNAKE_CASE_		(					self	:  int       ):





											'''simple docstring'''





											_snake_case:   Tuple  					=		'stabilityai/stable-diffusion-2'
											_snake_case:   Optional[int]  					=		FlaxDPMSolverMultistepScheduler.from_pretrained(__snake_case       ,     subfolder='scheduler'       )
											_snake_case:   int  					=		FlaxStableDiffusionPipeline.from_pretrained(
											    __snake_case       ,     scheduler=__snake_case       ,     revision='bf16'       ,     dtype=jnp.bfloataa       ,     )
											_snake_case:   Any  					=		scheduler_params

											_snake_case:   Dict  					=		'A painting of a squirrel eating a burger'
											_snake_case:   List[str]  					=		jax.device_count()
											_snake_case:   Optional[int]  					=		num_samples * [prompt]
											_snake_case:   str  					=		sd_pipe.prepare_inputs(__snake_case       )

											_snake_case:   List[Any]  					=		replicate(__snake_case       )
											_snake_case:   List[str]  					=		shard(__snake_case       )

											_snake_case:   Dict  					=		jax.random.PRNGKey(0       )
											_snake_case:   Any  					=		jax.random.split(__snake_case       ,     jax.device_count()       )

											_snake_case:   List[Any]  					=		sd_pipe(__snake_case       ,     __snake_case       ,     __snake_case       ,     num_inference_steps=25       ,     jit=__snake_case       )[0]
											assert images.shape == (jax.device_count(), 1, 7_68, 7_68, 3)

											_snake_case:   List[Any]  					=		images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]       )
											_snake_case:   List[str]  					=		images[0, 2_53:2_56, 2_53:2_56, -1]

											_snake_case:   Any  					=		jnp.asarray(jax.device_get(image_slice.flatten()       )       )
											_snake_case:   str  					=		jnp.array([0.4_336, 0.42_969, 0.4_453, 0.4_199, 0.4_297, 0.4_531, 0.4_434, 0.4_434, 0.4_297]       )
											print(f'''output_slice: {output_slice}'''       )
											assert jnp.abs(output_slice - expected_slice       ).max() < 1e-2












'''simple docstring'''






import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin







class 			lowerCamelCase					(						__UpperCAmelCase  ):
					_SCREAMING_SNAKE_CASE							   =    "Speech2TextFeatureExtractor"
					_SCREAMING_SNAKE_CASE							   =    "Speech2TextTokenizer"


					def __init__(					self	:  List[str]       ,     __snake_case	:  Union[str, Any]       ,     __snake_case	:  List[str]       ):





											'''simple docstring'''





											super().__init__(__snake_case       ,     __snake_case       )
											_snake_case:   Optional[int]  					=		self.feature_extractor
											_snake_case:   Dict  					=		False


					def __call__(					self	:  Optional[int]       ,     *__snake_case	:  Optional[Any]       ,     **__snake_case	:  Union[str, Any]       ):





											'''simple docstring'''





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

											if "raw_speech" in kwargs:
																	warnings.warn('Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'       )
																	_snake_case:   int  					=		kwargs.pop('raw_speech'       )
											else:
																	_snake_case:   Tuple  					=		kwargs.pop('audio'       ,     __snake_case       )
											_snake_case:   Optional[Any]  					=		kwargs.pop('sampling_rate'       ,     __snake_case       )
											_snake_case:   Optional[Any]  					=		kwargs.pop('text'       ,     __snake_case       )
											if len(__snake_case       ) > 0:
																	_snake_case:   Optional[Any]  					=		args[0]
																	_snake_case:   Union[str, Any]  					=		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:
																	_snake_case:   List[Any]  					=		self.feature_extractor(__snake_case       ,     *__snake_case       ,     sampling_rate=__snake_case       ,     **__snake_case       )
											if text is not None:
																	_snake_case:   Tuple  					=		self.tokenizer(__snake_case       ,     **__snake_case       )

											if text is None:
																	return inputs
											elif audio is None:
																	return encodings
											else:
																	_snake_case:   List[Any]  					=		encodings['input_ids']
																	return inputs


					def    SCREAMING_SNAKE_CASE_		(					self	:  Dict       ,     *__snake_case	:  Optional[int]       ,     **__snake_case	:  str       ):





											'''simple docstring'''





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


					def    SCREAMING_SNAKE_CASE_		(					self	:  Dict       ,     *__snake_case	:  List[str]       ,     **__snake_case	:  Tuple       ):





											'''simple docstring'''





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




					@contextmanager
					def    SCREAMING_SNAKE_CASE_		(					self	:  List[str]       ):





											'''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.'       )
											_snake_case:   Any  					=		True
											_snake_case:   str  					=		self.tokenizer
											yield
											_snake_case:   Union[str, Any]  					=		self.feature_extractor
											_snake_case:   Tuple  					=		False





