

import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_UpperCAmelCase		:       Optional[int]						    =	logging.get_logger(__name__)

_UpperCAmelCase		:       List[Any]						    =	{
    """microsoft/unispeech-sat-base-100h-libri-ft""": (
        """https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json"""
    ),
    # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat
}







class 			lowercase    (	lowercase_      ):
    __SCREAMING_SNAKE_CASE :			Union[str, Any]	      =      '''unispeech-sat'''




    def __init__( self				,       snake_case=32				,       snake_case=768				,       snake_case=12				,       snake_case=12				,       snake_case=3072				,       snake_case="gelu"				,       snake_case=0.1				,       snake_case=0.1				,       snake_case=0.1				,       snake_case=0.0				,       snake_case=0.0				,       snake_case=0.1				,       snake_case=0.1				,       snake_case=0.02				,       snake_case=1e-5				,       snake_case="group"				,       snake_case="gelu"				,       snake_case=(512, 512, 512, 512, 512, 512, 512)				,       snake_case=(5, 2, 2, 2, 2, 2, 2)				,       snake_case=(10, 3, 3, 3, 3, 2, 2)				,       snake_case=False				,       snake_case=128				,       snake_case=16				,       snake_case=False				,       snake_case=True				,       snake_case=0.05				,       snake_case=10				,       snake_case=2				,       snake_case=0.0				,       snake_case=10				,       snake_case=0				,       snake_case=320				,       snake_case=2				,       snake_case=0.1				,       snake_case=100				,       snake_case=256				,       snake_case=256				,       snake_case=0.1				,       snake_case="mean"				,       snake_case=False				,       snake_case=False				,       snake_case=256				,       snake_case=(512, 512, 512, 512, 1500)				,       snake_case=(5, 3, 3, 1, 1)				,       snake_case=(1, 2, 3, 1, 1)				,       snake_case=512				,       snake_case=0				,       snake_case=1				,       snake_case=2				,       snake_case=504				,       **snake_case				,       ):
           super().__init__(**snake_case				,       pad_token_id=snake_case				,       bos_token_id=snake_case				,       eos_token_id=snake_case	)
           snake_case_          =   hidden_size
           snake_case_          =   feat_extract_norm
           snake_case_          =   feat_extract_activation
           snake_case_          =   list(snake_case	)
           snake_case_          =   list(snake_case	)
           snake_case_          =   list(snake_case	)
           snake_case_          =   conv_bias
           snake_case_          =   num_conv_pos_embeddings
           snake_case_          =   num_conv_pos_embedding_groups
           snake_case_          =   len(self.conv_dim	)
           snake_case_          =   num_hidden_layers
           snake_case_          =   intermediate_size
           snake_case_          =   hidden_act
           snake_case_          =   num_attention_heads
           snake_case_          =   hidden_dropout
           snake_case_          =   attention_dropout
           snake_case_          =   activation_dropout
           snake_case_          =   feat_proj_dropout
           snake_case_          =   final_dropout
           snake_case_          =   layerdrop
           snake_case_          =   layer_norm_eps
           snake_case_          =   initializer_range
           snake_case_          =   vocab_size
           snake_case_          =   num_clusters
           snake_case_          =   do_stable_layer_norm
           snake_case_          =   use_weighted_layer_sum

           if (
               (len(self.conv_stride	) != self.num_feat_extract_layers)
               or (len(self.conv_kernel	) != self.num_feat_extract_layers)
               or (len(self.conv_dim	) != self.num_feat_extract_layers)
           ):
                  raise ValueError(
                      'Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =='
                      ' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ='
                      F''' {len(self.conv_dim	)}`, `len(config.conv_stride) = {len(self.conv_stride	)}`,'''
                      F''' `len(config.conv_kernel) = {len(self.conv_kernel	)}`.'''	)

           # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
           snake_case_          =   apply_spec_augment
           snake_case_          =   mask_time_prob
           snake_case_          =   mask_time_length
           snake_case_          =   mask_time_min_masks
           snake_case_          =   mask_feature_prob
           snake_case_          =   mask_feature_length
           snake_case_          =   mask_feature_min_masks

           # parameters for pretraining with codevector quantized representations
           snake_case_          =   num_codevectors_per_group
           snake_case_          =   num_codevector_groups
           snake_case_          =   contrastive_logits_temperature
           snake_case_          =   feat_quantizer_dropout
           snake_case_          =   num_negatives
           snake_case_          =   codevector_dim
           snake_case_          =   proj_codevector_dim
           snake_case_          =   diversity_loss_weight

           # ctc loss
           snake_case_          =   ctc_loss_reduction
           snake_case_          =   ctc_zero_infinity

           # SequenceClassification-specific parameter. Feel free to ignore for other classes.
           snake_case_          =   classifier_proj_size

           # XVector-specific parameters. Feel free to ignore for other classes.
           snake_case_          =   list(snake_case	)
           snake_case_          =   list(snake_case	)
           snake_case_          =   list(snake_case	)
           snake_case_          =   xvector_output_dim




    @property
    def    a							( self	):
           return functools.reduce(operator.mul				,       self.conv_stride				,       1	)







import gc
import unittest

from diffusers import FlaxControlNetModel, FlaxStableDiffusionControlNetPipeline
from diffusers.utils import is_flax_available, load_image, 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 			lowercase    (	unittest.TestCase      ):




				def    a							( self	):
											# clean up the VRAM after each test
											super().tearDown()
											gc.collect()




				def    a							( self	):
											snake_case_    ,   snake_case_          =   FlaxControlNetModel.from_pretrained(
											    'lllyasviel/sd-controlnet-canny'				,       from_pt=snake_case				,       dtype=jnp.bfloataa	)
											snake_case_    ,   snake_case_          =   FlaxStableDiffusionControlNetPipeline.from_pretrained(
											    'runwayml/stable-diffusion-v1-5'				,       controlnet=snake_case				,       from_pt=snake_case				,       dtype=jnp.bfloataa	)
											snake_case_          =   controlnet_params

											snake_case_          =   'bird'
											snake_case_          =   jax.device_count()
											snake_case_          =   pipe.prepare_text_inputs([prompts] * num_samples	)

											snake_case_          =   load_image(
											    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png'	)
											snake_case_          =   pipe.prepare_image_inputs([canny_image] * num_samples	)

											snake_case_          =   jax.random.PRNGKey(0	)
											snake_case_          =   jax.random.split(snake_case				,       jax.device_count()	)

											snake_case_          =   replicate(snake_case	)
											snake_case_          =   shard(snake_case	)
											snake_case_          =   shard(snake_case	)

											snake_case_          =   pipe(
											    prompt_ids=snake_case				,       image=snake_case				,       params=snake_case				,       prng_seed=snake_case				,       num_inference_steps=50				,       jit=snake_case				,       ).images
											assert images.shape == (jax.device_count(), 1, 768, 512, 3)

											snake_case_          =   images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]	)
											snake_case_          =   images[0, 253:256, 253:256, -1]

											snake_case_          =   jnp.asarray(jax.device_get(image_slice.flatten()	)	)
											snake_case_          =   jnp.array(
											    [0.16_79_69, 0.11_66_99, 0.08_15_43, 0.15_42_97, 0.13_28_12, 0.10_88_87, 0.16_99_22, 0.16_99_22, 0.20_50_78]	)
											print(F'''output_slice: {output_slice}'''	)
											assert jnp.abs(output_slice - expected_slice	).max() < 1e-2




				def    a							( self	):
											snake_case_    ,   snake_case_          =   FlaxControlNetModel.from_pretrained(
											    'lllyasviel/sd-controlnet-openpose'				,       from_pt=snake_case				,       dtype=jnp.bfloataa	)
											snake_case_    ,   snake_case_          =   FlaxStableDiffusionControlNetPipeline.from_pretrained(
											    'runwayml/stable-diffusion-v1-5'				,       controlnet=snake_case				,       from_pt=snake_case				,       dtype=jnp.bfloataa	)
											snake_case_          =   controlnet_params

											snake_case_          =   'Chef in the kitchen'
											snake_case_          =   jax.device_count()
											snake_case_          =   pipe.prepare_text_inputs([prompts] * num_samples	)

											snake_case_          =   load_image(
											    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/pose.png'	)
											snake_case_          =   pipe.prepare_image_inputs([pose_image] * num_samples	)

											snake_case_          =   jax.random.PRNGKey(0	)
											snake_case_          =   jax.random.split(snake_case				,       jax.device_count()	)

											snake_case_          =   replicate(snake_case	)
											snake_case_          =   shard(snake_case	)
											snake_case_          =   shard(snake_case	)

											snake_case_          =   pipe(
											    prompt_ids=snake_case				,       image=snake_case				,       params=snake_case				,       prng_seed=snake_case				,       num_inference_steps=50				,       jit=snake_case				,       ).images
											assert images.shape == (jax.device_count(), 1, 768, 512, 3)

											snake_case_          =   images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]	)
											snake_case_          =   images[0, 253:256, 253:256, -1]

											snake_case_          =   jnp.asarray(jax.device_get(image_slice.flatten()	)	)
											snake_case_          =   jnp.array(
											    [[0.27_14_84, 0.26_17_19, 0.27_53_91, 0.27_73_44, 0.27_92_97, 0.29_10_16, 0.29_49_22, 0.30_27_34, 0.30_27_34]]	)
											print(F'''output_slice: {output_slice}'''	)
											assert jnp.abs(output_slice - expected_slice	).max() < 1e-2




