





import functools
import operator

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


_UpperCAmelCase							:			Tuple        =							logging.get_logger(__name__)

_UpperCAmelCase							:			Optional[int]        =							{
    """microsoft/unispeech-large-1500h-cv""": (
        """https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json"""
    ),
    # See all UniSpeech models at https://huggingface.co/models?filter=unispeech
}

class   lowerCAmelCase				(   __UpperCamelCase     ):
 UpperCAmelCase__						=						"""unispeech"""
 def __init__( self    : Dict     ,	UpperCAmelCase    : Any=32     ,	UpperCAmelCase    : Optional[Any]=768     ,	UpperCAmelCase    : Union[str, Any]=12     ,	UpperCAmelCase    : Dict=12     ,	UpperCAmelCase    : int=3072     ,	UpperCAmelCase    : Any="gelu"     ,	UpperCAmelCase    : Union[str, Any]=0.1     ,	UpperCAmelCase    : Tuple=0.1     ,	UpperCAmelCase    : List[Any]=0.1     ,	UpperCAmelCase    : Optional[int]=0.0     ,	UpperCAmelCase    : Optional[Any]=0.0     ,	UpperCAmelCase    : Any=0.1     ,	UpperCAmelCase    : Dict=0.1     ,	UpperCAmelCase    : Optional[Any]=0.0_2     ,	UpperCAmelCase    : Any=1e-5     ,	UpperCAmelCase    : str="group"     ,	UpperCAmelCase    : Union[str, Any]="gelu"     ,	UpperCAmelCase    : List[str]=(512, 512, 512, 512, 512, 512, 512)     ,	UpperCAmelCase    : Any=(5, 2, 2, 2, 2, 2, 2)     ,	UpperCAmelCase    : Any=(10, 3, 3, 3, 3, 2, 2)     ,	UpperCAmelCase    : str=False     ,	UpperCAmelCase    : int=128     ,	UpperCAmelCase    : List[str]=16     ,	UpperCAmelCase    : str=False     ,	UpperCAmelCase    : int=True     ,	UpperCAmelCase    : List[Any]=0.0_5     ,	UpperCAmelCase    : str=10     ,	UpperCAmelCase    : int=2     ,	UpperCAmelCase    : Any=0.0     ,	UpperCAmelCase    : Tuple=10     ,	UpperCAmelCase    : int=0     ,	UpperCAmelCase    : Dict=320     ,	UpperCAmelCase    : Union[str, Any]=2     ,	UpperCAmelCase    : Optional[Any]=0.1     ,	UpperCAmelCase    : List[Any]=100     ,	UpperCAmelCase    : List[str]=256     ,	UpperCAmelCase    : Dict=256     ,	UpperCAmelCase    : Optional[int]=0.1     ,	UpperCAmelCase    : Union[str, Any]="mean"     ,	UpperCAmelCase    : List[Any]=False     ,	UpperCAmelCase    : List[str]=False     ,	UpperCAmelCase    : int=256     ,	UpperCAmelCase    : Optional[Any]=80     ,	UpperCAmelCase    : Dict=0     ,	UpperCAmelCase    : Optional[int]=1     ,	UpperCAmelCase    : int=2     ,	UpperCAmelCase    : Optional[int]=0.5     ,	**UpperCAmelCase    : List[str]     ,	)     ->      str:
     super().__init__(**UpperCAmelCase     ,	pad_token_id=UpperCAmelCase     ,	bos_token_id=UpperCAmelCase     ,	eos_token_id=UpperCAmelCase      )
     lowerCamelCase__       : Any   					=						hidden_size
     lowerCamelCase__       : Tuple   					=						feat_extract_norm
     lowerCamelCase__       : Dict   					=						feat_extract_activation
     lowerCamelCase__       : Optional[int]   					=						list(UpperCAmelCase      )
     lowerCamelCase__       : Optional[int]   					=						list(UpperCAmelCase      )
     lowerCamelCase__       : Optional[Any]   					=						list(UpperCAmelCase      )
     lowerCamelCase__       : Optional[int]   					=						conv_bias
     lowerCamelCase__       : List[str]   					=						num_conv_pos_embeddings
     lowerCamelCase__       : Dict   					=						num_conv_pos_embedding_groups
     lowerCamelCase__       : List[str]   					=						len(self.conv_dim      )
     lowerCamelCase__       : str   					=						num_hidden_layers
     lowerCamelCase__       : int   					=						intermediate_size
     lowerCamelCase__       : Tuple   					=						hidden_act
     lowerCamelCase__       : int   					=						num_attention_heads
     lowerCamelCase__       : Dict   					=						hidden_dropout
     lowerCamelCase__       : Optional[int]   					=						attention_dropout
     lowerCamelCase__       : Any   					=						activation_dropout
     lowerCamelCase__       : str   					=						feat_proj_dropout
     lowerCamelCase__       : Union[str, Any]   					=						final_dropout
     lowerCamelCase__       : int   					=						layerdrop
     lowerCamelCase__       : Optional[int]   					=						layer_norm_eps
     lowerCamelCase__       : List[Any]   					=						initializer_range
     lowerCamelCase__       : List[str]   					=						num_ctc_classes
     lowerCamelCase__       : List[str]   					=						vocab_size
     lowerCamelCase__       : int   					=						do_stable_layer_norm
     lowerCamelCase__       : Optional[int]   					=						use_weighted_layer_sum
     lowerCamelCase__       : Optional[Any]   					=						classifier_proj_size

     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
     lowerCamelCase__       : Optional[int]   					=						apply_spec_augment
     lowerCamelCase__       : Dict   					=						mask_time_prob
     lowerCamelCase__       : Tuple   					=						mask_time_length
     lowerCamelCase__       : List[Any]   					=						mask_time_min_masks
     lowerCamelCase__       : Optional[int]   					=						mask_feature_prob
     lowerCamelCase__       : List[str]   					=						mask_feature_length
     lowerCamelCase__       : Tuple   					=						mask_feature_min_masks

     # parameters for pretraining with codevector quantized representations
     lowerCamelCase__       : int   					=						num_codevectors_per_group
     lowerCamelCase__       : Tuple   					=						num_codevector_groups
     lowerCamelCase__       : List[Any]   					=						contrastive_logits_temperature
     lowerCamelCase__       : List[Any]   					=						feat_quantizer_dropout
     lowerCamelCase__       : Any   					=						num_negatives
     lowerCamelCase__       : Union[str, Any]   					=						codevector_dim
     lowerCamelCase__       : Dict   					=						proj_codevector_dim
     lowerCamelCase__       : str   					=						diversity_loss_weight

     # ctc loss
     lowerCamelCase__       : Union[str, Any]   					=						ctc_loss_reduction
     lowerCamelCase__       : Union[str, Any]   					=						ctc_zero_infinity

     # pretraining loss
     lowerCamelCase__       : str   					=						replace_prob







 @property
 def 	A_      ( self    : Union[str, Any]      )     ->      int:
     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   lowerCAmelCase				(   unittest.TestCase     ):
 def 	A_      ( self    : Any      )     ->      int:
     # clean up the VRAM after each test
     super().tearDown()
     gc.collect()
 def 	A_      ( self    : Optional[Any]      )     ->      Optional[int]:
     lowerCamelCase__					,				lowerCamelCase__       : str   					=						FlaxControlNetModel.from_pretrained(
         'lllyasviel/sd-controlnet-canny'     ,	from_pt=UpperCAmelCase     ,	dtype=jnp.bfloataa      )
     lowerCamelCase__					,				lowerCamelCase__       : int   					=						FlaxStableDiffusionControlNetPipeline.from_pretrained(
         'runwayml/stable-diffusion-v1-5'     ,	controlnet=UpperCAmelCase     ,	from_pt=UpperCAmelCase     ,	dtype=jnp.bfloataa      )
     lowerCamelCase__       : int   					=						controlnet_params

     lowerCamelCase__       : Any   					=						'bird'
     lowerCamelCase__       : Dict   					=						jax.device_count()
     lowerCamelCase__       : Any   					=						pipe.prepare_text_inputs([prompts] * num_samples      )

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

     lowerCamelCase__       : List[str]   					=						jax.random.PRNGKey(0      )
     lowerCamelCase__       : Optional[int]   					=						jax.random.split(UpperCAmelCase     ,	jax.device_count()      )

     lowerCamelCase__       : List[Any]   					=						replicate(UpperCAmelCase      )
     lowerCamelCase__       : Dict   					=						shard(UpperCAmelCase      )
     lowerCamelCase__       : Any   					=						shard(UpperCAmelCase      )

     lowerCamelCase__       : Any   					=						pipe(
         prompt_ids=UpperCAmelCase     ,	image=UpperCAmelCase     ,	params=UpperCAmelCase     ,	prng_seed=UpperCAmelCase     ,	num_inference_steps=50     ,	jit=UpperCAmelCase     ,	).images
     assert images.shape == (jax.device_count(), 1, 768, 512, 3)

     lowerCamelCase__       : Dict   					=						images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]      )
     lowerCamelCase__       : List[str]   					=						images[0, 253:256, 253:256, -1]

     lowerCamelCase__       : Tuple   					=						jnp.asarray(jax.device_get(image_slice.flatten()      )      )
     lowerCamelCase__       : Tuple   					=						jnp.array(
         [0.1_6_7_9_6_9, 0.1_1_6_6_9_9, 0.0_8_1_5_4_3, 0.1_5_4_2_9_7, 0.1_3_2_8_1_2, 0.1_0_8_8_8_7, 0.1_6_9_9_2_2, 0.1_6_9_9_2_2, 0.2_0_5_0_7_8]      )
     print(F"""output_slice: {output_slice}"""      )
     assert jnp.abs(output_slice - expected_slice      ).max() < 1e-2







 def 	A_      ( self    : List[str]      )     ->      Union[str, Any]:
     lowerCamelCase__					,				lowerCamelCase__       : Dict   					=						FlaxControlNetModel.from_pretrained(
         'lllyasviel/sd-controlnet-openpose'     ,	from_pt=UpperCAmelCase     ,	dtype=jnp.bfloataa      )
     lowerCamelCase__					,				lowerCamelCase__       : Optional[Any]   					=						FlaxStableDiffusionControlNetPipeline.from_pretrained(
         'runwayml/stable-diffusion-v1-5'     ,	controlnet=UpperCAmelCase     ,	from_pt=UpperCAmelCase     ,	dtype=jnp.bfloataa      )
     lowerCamelCase__       : Optional[int]   					=						controlnet_params

     lowerCamelCase__       : Any   					=						'Chef in the kitchen'
     lowerCamelCase__       : str   					=						jax.device_count()
     lowerCamelCase__       : Union[str, Any]   					=						pipe.prepare_text_inputs([prompts] * num_samples      )

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

     lowerCamelCase__       : Tuple   					=						jax.random.PRNGKey(0      )
     lowerCamelCase__       : List[str]   					=						jax.random.split(UpperCAmelCase     ,	jax.device_count()      )

     lowerCamelCase__       : Dict   					=						replicate(UpperCAmelCase      )
     lowerCamelCase__       : List[str]   					=						shard(UpperCAmelCase      )
     lowerCamelCase__       : Tuple   					=						shard(UpperCAmelCase      )

     lowerCamelCase__       : Union[str, Any]   					=						pipe(
         prompt_ids=UpperCAmelCase     ,	image=UpperCAmelCase     ,	params=UpperCAmelCase     ,	prng_seed=UpperCAmelCase     ,	num_inference_steps=50     ,	jit=UpperCAmelCase     ,	).images
     assert images.shape == (jax.device_count(), 1, 768, 512, 3)

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

     lowerCamelCase__       : Optional[int]   					=						jnp.asarray(jax.device_get(image_slice.flatten()      )      )
     lowerCamelCase__       : int   					=						jnp.array(
         [[0.2_7_1_4_8_4, 0.2_6_1_7_1_9, 0.2_7_5_3_9_1, 0.2_7_7_3_4_4, 0.2_7_9_2_9_7, 0.2_9_1_0_1_6, 0.2_9_4_9_2_2, 0.3_0_2_7_3_4, 0.3_0_2_7_3_4]]      )
     print(F"""output_slice: {output_slice}"""      )
     assert jnp.abs(output_slice - expected_slice      ).max() < 1e-2





