
"""simple docstring"""

import gc
import unittest

from parameterized import parameterized

from diffusers import FlaxUNetaDConditionModel
from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow


if is_flax_available():
   import jax
   import jax.numpy as jnp
@slow
@require_flax
class   _UpperCAmelCase				(		unittest.TestCase				):




      def      a_   (				self ,							lowercase_ ,							lowercase_				)    ->       Any:
          return F"gaussian_noise_s={seed}_shape={'_'.join([str(_lowerCamelCase				) for s in shape]				)}.npy"




      def      a_   (				self				)    ->       List[Any]:
          super().tearDown()
          gc.collect()




      def      a_   (				self ,							lowercase_=0 ,							lowercase_=(4, 4, 6_4, 6_4) ,							lowercase_=False				)    ->       Optional[int]:
          UpperCAmelCase          =       jnp.bfloataa if fpaa else jnp.floataa
          UpperCAmelCase          =       jnp.array(load_hf_numpy(self.get_file_format(_lowerCamelCase ,							_lowerCamelCase				)				) ,							dtype=_lowerCamelCase				)
          return image




      def      a_   (				self ,							lowercase_=False ,							lowercase_="CompVis/stable-diffusion-v1-4"				)    ->       Tuple:
          UpperCAmelCase          =       jnp.bfloataa if fpaa else jnp.floataa
          UpperCAmelCase          =       'bf16' if fpaa else None

          UpperCAmelCase      ,				UpperCAmelCase          =       FlaxUNetaDConditionModel.from_pretrained(
              _lowerCamelCase ,							subfolder='unet' ,							dtype=_lowerCamelCase ,							revision=_lowerCamelCase				)
          return model, params




      def      a_   (				self ,							lowercase_=0 ,							lowercase_=(4, 7_7, 7_6_8) ,							lowercase_=False				)    ->       Any:
          UpperCAmelCase          =       jnp.bfloataa if fpaa else jnp.floataa
          UpperCAmelCase          =       jnp.array(load_hf_numpy(self.get_file_format(_lowerCamelCase ,							_lowerCamelCase				)				) ,							dtype=_lowerCamelCase				)
          return hidden_states




      @parameterized.expand(
          [
              # fmt: off
              [8_3, 4, [-0.2_3_2_3, -0.1_3_0_4, 0.0_8_1_3, -0.3_0_9_3, -0.0_9_1_9, -0.1_5_7_1, -0.1_1_2_5, -0.5_8_0_6]],
              [1_7, 0.5_5, [-0.0_8_3_1, -0.2_4_4_3, 0.0_9_0_1, -0.0_9_1_9, 0.3_3_9_6, 0.0_1_0_3, -0.3_7_4_3, 0.0_7_0_1]],
              [8, 0.8_9, [-0.4_8_6_3, 0.0_8_5_9, 0.0_8_7_5, -0.1_6_5_8, 0.9_1_9_9, -0.0_1_1_4, 0.4_8_3_9, 0.4_6_3_9]],
              [3, 1_0_0_0, [-0.5_6_4_9, 0.2_4_0_2, -0.5_5_1_8, 0.1_2_4_8, 1.1_3_2_8, -0.2_4_4_3, -0.0_3_2_5, -1.0_0_7_8]],
              # fmt: on
          ]				)
      def      a_   (				self ,							lowercase_ ,							lowercase_ ,							lowercase_				)    ->       int:
          UpperCAmelCase      ,				UpperCAmelCase          =       self.get_unet_model(model_id='CompVis/stable-diffusion-v1-4' ,							fpaa=_lowerCamelCase				)
          UpperCAmelCase          =       self.get_latents(_lowerCamelCase ,							fpaa=_lowerCamelCase				)
          UpperCAmelCase          =       self.get_encoder_hidden_states(_lowerCamelCase ,							fpaa=_lowerCamelCase				)

          UpperCAmelCase          =       model.apply(
              {'params': params} ,							_lowerCamelCase ,							jnp.array(_lowerCamelCase ,							dtype=jnp.intaa				) ,							encoder_hidden_states=_lowerCamelCase ,							).sample

          assert sample.shape == latents.shape

          UpperCAmelCase          =       jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())				) ,							dtype=jnp.floataa				)
          UpperCAmelCase          =       jnp.array(_lowerCamelCase ,							dtype=jnp.floataa				)

          # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware
          assert jnp.allclose(_lowerCamelCase ,							_lowerCamelCase ,							atol=1E-2				)




      @parameterized.expand(
          [
              # fmt: off
              [8_3, 4, [0.1_5_1_4, 0.0_8_0_7, 0.1_6_2_4, 0.1_0_1_6, -0.1_8_9_6, 0.0_2_6_3, 0.0_6_7_7, 0.2_3_1_0]],
              [1_7, 0.5_5, [0.1_1_6_4, -0.0_2_1_6, 0.0_1_7_0, 0.1_5_8_9, -0.3_1_2_0, 0.1_0_0_5, -0.0_5_8_1, -0.1_4_5_8]],
              [8, 0.8_9, [-0.1_7_5_8, -0.0_1_6_9, 0.1_0_0_4, -0.1_4_1_1, 0.1_3_1_2, 0.1_1_0_3, -0.1_9_9_6, 0.2_1_3_9]],
              [3, 1_0_0_0, [0.1_2_1_4, 0.0_3_5_2, -0.0_7_3_1, -0.1_5_6_2, -0.0_9_9_4, -0.0_9_0_6, -0.2_3_4_0, -0.0_5_3_9]],
              # fmt: on
          ]				)
      def      a_   (				self ,							lowercase_ ,							lowercase_ ,							lowercase_				)    ->       Dict:
          UpperCAmelCase      ,				UpperCAmelCase          =       self.get_unet_model(model_id='stabilityai/stable-diffusion-2' ,							fpaa=_lowerCamelCase				)
          UpperCAmelCase          =       self.get_latents(_lowerCamelCase ,							shape=(4, 4, 9_6, 9_6) ,							fpaa=_lowerCamelCase				)
          UpperCAmelCase          =       self.get_encoder_hidden_states(_lowerCamelCase ,							shape=(4, 7_7, 1_0_2_4) ,							fpaa=_lowerCamelCase				)

          UpperCAmelCase          =       model.apply(
              {'params': params} ,							_lowerCamelCase ,							jnp.array(_lowerCamelCase ,							dtype=jnp.intaa				) ,							encoder_hidden_states=_lowerCamelCase ,							).sample

          assert sample.shape == latents.shape

          UpperCAmelCase          =       jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())				) ,							dtype=jnp.floataa				)
          UpperCAmelCase          =       jnp.array(_lowerCamelCase ,							dtype=jnp.floataa				)

          # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware
          assert jnp.allclose(_lowerCamelCase ,							_lowerCamelCase ,							atol=1E-2				)





"""simple docstring"""






import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin
from .feature_extraction_wavaveca import WavaVecaFeatureExtractor
from .tokenization_wavaveca import WavaVecaCTCTokenizer

class 					__a     ( __a       ):



							'''simple docstring'''







							_lowerCamelCase					:			List[Any]          =   """Wav2Vec2FeatureExtractor"""
							_lowerCamelCase					:			Any          =   """AutoTokenizer"""
							def __init__(     self  ,    _lowerCamelCase  ,    _lowerCamelCase       )			->							List[str]:




										'''simple docstring'''
										super().__init__(_lowerCamelCase  ,    _lowerCamelCase       )
										__lowercase       =       self.feature_extractor
										__lowercase       =       False
							@classmethod
							def   SCREAMING_SNAKE_CASE						(     cls  ,    _lowerCamelCase  ,    **_lowerCamelCase       )			->							Any:




										'''simple docstring'''
										try:
													return super().from_pretrained(_lowerCamelCase  ,    **_lowerCamelCase       )
										except OSError:
													warnings.warn(
													    f'''Loading a tokenizer inside {cls.__name__} from a config that does not'''
													    " include a `tokenizer_class` attribute is deprecated and will be "
													    "removed in v5. Please add `'tokenizer_class': 'Wav2Vec2CTCTokenizer'`"
													    " attribute to either your `config.json` or `tokenizer_config.json` "
													    "file to suppress this warning: "  ,    _lowerCamelCase  ,    )

													__lowercase       =       WavaVecaFeatureExtractor.from_pretrained(_lowerCamelCase  ,    **_lowerCamelCase       )
													__lowercase       =       WavaVecaCTCTokenizer.from_pretrained(_lowerCamelCase  ,    **_lowerCamelCase       )

													return cls(feature_extractor=_lowerCamelCase  ,    tokenizer=_lowerCamelCase       )
							def __call__(     self  ,    *_lowerCamelCase  ,    **_lowerCamelCase       )			->							List[Any]:




										'''simple docstring'''
										# For backward compatibility
										if self._in_target_context_manager:
													return self.current_processor(*_lowerCamelCase  ,    **_lowerCamelCase       )

										if "raw_speech" in kwargs:
													warnings.warn("Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead."       )
													__lowercase       =       kwargs.pop("raw_speech"       )
										else:
													__lowercase       =       kwargs.pop("audio"  ,    _lowerCamelCase       )
										__lowercase       =       kwargs.pop("sampling_rate"  ,    _lowerCamelCase       )
										__lowercase       =       kwargs.pop("text"  ,    _lowerCamelCase       )
										if len(_lowerCamelCase       ) > 0:
													__lowercase       =       args[0]
													__lowercase       =       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:
													__lowercase       =       self.feature_extractor(_lowerCamelCase  ,    *_lowerCamelCase  ,    sampling_rate=_lowerCamelCase  ,    **_lowerCamelCase       )
										if text is not None:
													__lowercase       =       self.tokenizer(_lowerCamelCase  ,    **_lowerCamelCase       )

										if text is None:
													return inputs
										elif audio is None:
													return encodings
										else:
													__lowercase       =       encodings["input_ids"]
													return inputs
							def   SCREAMING_SNAKE_CASE						(     self  ,    *_lowerCamelCase  ,    **_lowerCamelCase       )			->							List[str]:




										'''simple docstring'''
										# For backward compatibility
										if self._in_target_context_manager:
													return self.current_processor.pad(*_lowerCamelCase  ,    **_lowerCamelCase       )

										__lowercase       =       kwargs.pop("input_features"  ,    _lowerCamelCase       )
										__lowercase       =       kwargs.pop("labels"  ,    _lowerCamelCase       )
										if len(_lowerCamelCase       ) > 0:
													__lowercase       =       args[0]
													__lowercase       =       args[1:]

										if input_features is not None:
													__lowercase       =       self.feature_extractor.pad(_lowerCamelCase  ,    *_lowerCamelCase  ,    **_lowerCamelCase       )
										if labels is not None:
													__lowercase       =       self.tokenizer.pad(_lowerCamelCase  ,    **_lowerCamelCase       )

										if labels is None:
													return input_features
										elif input_features is None:
													return labels
										else:
													__lowercase       =       labels["input_ids"]
													return input_features
							def   SCREAMING_SNAKE_CASE						(     self  ,    *_lowerCamelCase  ,    **_lowerCamelCase       )			->							Optional[int]:




										'''simple docstring'''
										return self.tokenizer.batch_decode(*_lowerCamelCase  ,    **_lowerCamelCase       )
							def   SCREAMING_SNAKE_CASE						(     self  ,    *_lowerCamelCase  ,    **_lowerCamelCase       )			->							Dict:




										'''simple docstring'''
										return self.tokenizer.decode(*_lowerCamelCase  ,    **_lowerCamelCase       )





							@contextmanager
							def   SCREAMING_SNAKE_CASE						(     self       )			->							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."       )
										__lowercase       =       True
										__lowercase       =       self.tokenizer
										yield
										__lowercase       =       self.feature_extractor
										__lowercase       =       False





