






from typing import Optional, Tuple, Union

import flax
import flax.linen as nn
import jax
import jax.numpy as jnp
from flax.core.frozen_dict import FrozenDict

from ..configuration_utils import ConfigMixin, flax_register_to_config
from ..utils import BaseOutput
from .embeddings_flax import FlaxTimestepEmbedding, FlaxTimesteps
from .modeling_flax_utils import FlaxModelMixin
from .unet_ad_blocks_flax import (
    FlaxCrossAttnDownBlockaD,
    FlaxDownBlockaD,
    FlaxUNetMidBlockaDCrossAttn,
)






@flax.struct.dataclass
class 		lowercase_					(	lowercase   ):






      '''simple docstring'''




      __snake_case       =  42
      __snake_case       =  42






class 		lowercase_					(	nn.Module   ):






      '''simple docstring'''




      __snake_case       =  42
      __snake_case       =  (16, 32, 96, 2_56)
      __snake_case       =  jnp.floataa







      def       __lowerCAmelCase					(      self      :						Dict  )    ->int:




       """simple docstring"""


       a		  =      nn.Conv(
           self.block_out_channels[0] ,					kernel_size=(3, 3) ,					padding=((1, 1), (1, 1)) ,					dtype=self.dtype ,					)

       a		  =      []
       for i in range(len(self.block_out_channels  ) - 1  ):
        a		  =      self.block_out_channels[i]
        a		  =      self.block_out_channels[i + 1]
        a		  =      nn.Conv(
            __UpperCAmelCase ,					kernel_size=(3, 3) ,					padding=((1, 1), (1, 1)) ,					dtype=self.dtype ,					)
        blocks.append(__UpperCAmelCase  )
        a		  =      nn.Conv(
            __UpperCAmelCase ,					kernel_size=(3, 3) ,					strides=(2, 2) ,					padding=((1, 1), (1, 1)) ,					dtype=self.dtype ,					)
        blocks.append(__UpperCAmelCase  )
       a		  =      blocks

       a		  =      nn.Conv(
           self.conditioning_embedding_channels ,					kernel_size=(3, 3) ,					padding=((1, 1), (1, 1)) ,					kernel_init=nn.initializers.zeros_init() ,					bias_init=nn.initializers.zeros_init() ,					dtype=self.dtype ,					)







      def __call__(      self      :						List[Any] ,					__UpperCAmelCase      :						Any  )    ->Dict:




       """simple docstring"""


       a		  =      self.conv_in(__UpperCAmelCase  )
       a		  =      nn.silu(__UpperCAmelCase  )

       for block in self.blocks:
        a		  =      block(__UpperCAmelCase  )
        a		  =      nn.silu(__UpperCAmelCase  )

       a		  =      self.conv_out(__UpperCAmelCase  )

       return embedding






@flax_register_to_config
class 		lowercase_					(	nn.Module ,  lowercase ,  lowercase   ):






      '''simple docstring'''




      __snake_case       =  32
      __snake_case       =  4
      __snake_case       =  (
          "CrossAttnDownBlock2D",
          "CrossAttnDownBlock2D",
          "CrossAttnDownBlock2D",
          "DownBlock2D",
      )
      __snake_case       =  False
      __snake_case       =  (3_20, 6_40, 12_80, 12_80)
      __snake_case       =  2
      __snake_case       =  8
      __snake_case       =  None
      __snake_case       =  12_80
      __snake_case       =  0.0
      __snake_case       =  False
      __snake_case       =  jnp.floataa
      __snake_case       =  True
      __snake_case       =  0
      __snake_case       =  "rgb"
      __snake_case       =  (16, 32, 96, 2_56)







      def       __lowerCAmelCase					(      self      :						Dict ,					__UpperCAmelCase      :						jax.random.KeyArray  )    ->FrozenDict:




       """simple docstring"""


       a		  =      (1, self.in_channels, self.sample_size, self.sample_size)
       a		  =      jnp.zeros(__UpperCAmelCase ,					dtype=jnp.floataa  )
       a		  =      jnp.ones((1,) ,					dtype=jnp.intaa  )
       a		  =      jnp.zeros((1, 1, self.cross_attention_dim) ,					dtype=jnp.floataa  )
       a		  =      (1, 3, self.sample_size * 8, self.sample_size * 8)
       a		  =      jnp.zeros(__UpperCAmelCase ,					dtype=jnp.floataa  )

       a  ,     a		  =      jax.random.split(__UpperCAmelCase  )
       a		  =      {'''params''': params_rng, '''dropout''': dropout_rng}

       return self.init(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase  )["params"]







      def       __lowerCAmelCase					(      self      :						int  )    ->List[str]:




       """simple docstring"""


       a		  =      self.block_out_channels
       a		  =      block_out_channels[0] * 4

       # If `num_attention_heads` is not defined (which is the case for most models)
       # it will default to `attention_head_dim`. This looks weird upon first reading it and it is.
       # The reason for this behavior is to correct for incorrectly named variables that were introduced
       # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131
       # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking
       # which is why we correct for the naming here.
       a		  =      self.num_attention_heads or self.attention_head_dim

       # input
       a		  =      nn.Conv(
           block_out_channels[0] ,					kernel_size=(3, 3) ,					strides=(1, 1) ,					padding=((1, 1), (1, 1)) ,					dtype=self.dtype ,					)

       # time
       a		  =      FlaxTimesteps(
           block_out_channels[0] ,					flip_sin_to_cos=self.flip_sin_to_cos ,					freq_shift=self.config.freq_shift  )
       a		  =      FlaxTimestepEmbedding(__UpperCAmelCase ,					dtype=self.dtype  )

       a		  =      FlaxControlNetConditioningEmbedding(
           conditioning_embedding_channels=block_out_channels[0] ,					block_out_channels=self.conditioning_embedding_out_channels ,					)

       a		  =      self.only_cross_attention
       if isinstance(__UpperCAmelCase ,					__UpperCAmelCase  ):
        a		  =      (only_cross_attention,) * len(self.down_block_types  )

       if isinstance(__UpperCAmelCase ,					__UpperCAmelCase  ):
        a		  =      (num_attention_heads,) * len(self.down_block_types  )

       # down
       a		  =      []
       a		  =      []

       a		  =      block_out_channels[0]

       a		  =      nn.Conv(
           __UpperCAmelCase ,					kernel_size=(1, 1) ,					padding='''VALID''' ,					kernel_init=nn.initializers.zeros_init() ,					bias_init=nn.initializers.zeros_init() ,					dtype=self.dtype ,					)
       controlnet_down_blocks.append(__UpperCAmelCase  )

       for i, down_block_type in enumerate(self.down_block_types  ):
        a		  =      output_channel
        a		  =      block_out_channels[i]
        a		  =      i == len(__UpperCAmelCase  ) - 1

        if down_block_type == "CrossAttnDownBlock2D":
         a		  =      FlaxCrossAttnDownBlockaD(
             in_channels=__UpperCAmelCase ,					out_channels=__UpperCAmelCase ,					dropout=self.dropout ,					num_layers=self.layers_per_block ,					num_attention_heads=num_attention_heads[i] ,					add_downsample=not is_final_block ,					use_linear_projection=self.use_linear_projection ,					only_cross_attention=only_cross_attention[i] ,					dtype=self.dtype ,					)
        else:
         a		  =      FlaxDownBlockaD(
             in_channels=__UpperCAmelCase ,					out_channels=__UpperCAmelCase ,					dropout=self.dropout ,					num_layers=self.layers_per_block ,					add_downsample=not is_final_block ,					dtype=self.dtype ,					)

        down_blocks.append(__UpperCAmelCase  )

        for _ in range(self.layers_per_block  ):
         a		  =      nn.Conv(
             __UpperCAmelCase ,					kernel_size=(1, 1) ,					padding='''VALID''' ,					kernel_init=nn.initializers.zeros_init() ,					bias_init=nn.initializers.zeros_init() ,					dtype=self.dtype ,					)
         controlnet_down_blocks.append(__UpperCAmelCase  )

        if not is_final_block:
         a		  =      nn.Conv(
             __UpperCAmelCase ,					kernel_size=(1, 1) ,					padding='''VALID''' ,					kernel_init=nn.initializers.zeros_init() ,					bias_init=nn.initializers.zeros_init() ,					dtype=self.dtype ,					)
         controlnet_down_blocks.append(__UpperCAmelCase  )

       a		  =      down_blocks
       a		  =      controlnet_down_blocks

       # mid
       a		  =      block_out_channels[-1]
       a		  =      FlaxUNetMidBlockaDCrossAttn(
           in_channels=__UpperCAmelCase ,					dropout=self.dropout ,					num_attention_heads=num_attention_heads[-1] ,					use_linear_projection=self.use_linear_projection ,					dtype=self.dtype ,					)

       a		  =      nn.Conv(
           __UpperCAmelCase ,					kernel_size=(1, 1) ,					padding='''VALID''' ,					kernel_init=nn.initializers.zeros_init() ,					bias_init=nn.initializers.zeros_init() ,					dtype=self.dtype ,					)







      def __call__(      self      :						Any ,					__UpperCAmelCase      :						List[str] ,					__UpperCAmelCase      :						str ,					__UpperCAmelCase      :						int ,					__UpperCAmelCase      :						Tuple ,					__UpperCAmelCase      :						float = 1.0 ,					__UpperCAmelCase      :						bool = True ,					__UpperCAmelCase      :						bool = False ,					)    ->Union[FlaxControlNetOutput, Tuple]:




       """simple docstring"""


       a		  =      self.controlnet_conditioning_channel_order
       if channel_order == "bgr":
        a		  =      jnp.flip(__UpperCAmelCase ,					axis=1  )

       # 1. time
       if not isinstance(__UpperCAmelCase ,					jnp.ndarray  ):
        a		  =      jnp.array([timesteps] ,					dtype=jnp.intaa  )
       elif isinstance(__UpperCAmelCase ,					jnp.ndarray  ) and len(timesteps.shape  ) == 0:
        a		  =      timesteps.astype(dtype=jnp.floataa  )
        a		  =      jnp.expand_dims(__UpperCAmelCase ,					0  )

       a		  =      self.time_proj(__UpperCAmelCase  )
       a		  =      self.time_embedding(__UpperCAmelCase  )

       # 2. pre-process
       a		  =      jnp.transpose(__UpperCAmelCase ,					(0, 2, 3, 1)  )
       a		  =      self.conv_in(__UpperCAmelCase  )

       a		  =      jnp.transpose(__UpperCAmelCase ,					(0, 2, 3, 1)  )
       a		  =      self.controlnet_cond_embedding(__UpperCAmelCase  )
       sample += controlnet_cond

       # 3. down
       a		  =      (sample,)
       for down_block in self.down_blocks:
        if isinstance(__UpperCAmelCase ,					__UpperCAmelCase  ):
         a  ,     a		  =      down_block(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					deterministic=not train  )
        else:
         a  ,     a		  =      down_block(__UpperCAmelCase ,					__UpperCAmelCase ,					deterministic=not train  )
        down_block_res_samples += res_samples

       # 4. mid
       a		  =      self.mid_block(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					deterministic=not train  )

       # 5. contronet blocks
       a		  =      ()
       for down_block_res_sample, controlnet_block in zip(__UpperCAmelCase ,					self.controlnet_down_blocks  ):
        a		  =      controlnet_block(__UpperCAmelCase  )
        controlnet_down_block_res_samples += (down_block_res_sample,)

       a		  =      controlnet_down_block_res_samples

       a		  =      self.controlnet_mid_block(__UpperCAmelCase  )

       # 6. scaling
       a		  =      [sample * conditioning_scale for sample in down_block_res_samples]
       mid_block_res_sample *= conditioning_scale

       if not return_dict:
        return (down_block_res_samples, mid_block_res_sample)

       return FlaxControlNetOutput(
           down_block_res_samples=__UpperCAmelCase ,					mid_block_res_sample=__UpperCAmelCase  )






'''simple docstring'''




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


__snake_case    						=logging.get_logger(__name__)

__snake_case    						={
    """facebook/s2t-wav2vec2-large-en-de""": (
        """https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json"""
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2
}

class 		UpperCAmelCase_      (  __lowercase		):
     lowerCamelCase				:	Union[str, Any]		=     '''speech_to_text_2'''
     lowerCamelCase				:	Any		=     ['''past_key_values''']
     lowerCamelCase				:	Optional[Any]		=     {'''num_attention_heads''': '''decoder_attention_heads''', '''hidden_size''': '''d_model'''}





     def __init__( self       :      Optional[int]    ,      UpperCAmelCase__       :      Optional[Any]=1_0_0_0_0    ,      UpperCAmelCase__       :      int=6    ,      UpperCAmelCase__       :      Optional[Any]=2_0_4_8    ,      UpperCAmelCase__       :      Union[str, Any]=4    ,      UpperCAmelCase__       :      List[Any]=0.0    ,      UpperCAmelCase__       :      str=True    ,      UpperCAmelCase__       :      str="relu"    ,      UpperCAmelCase__       :      Any=2_5_6    ,      UpperCAmelCase__       :      Optional[Any]=0.1    ,      UpperCAmelCase__       :      Any=0.0    ,      UpperCAmelCase__       :      Tuple=0.0    ,      UpperCAmelCase__       :      List[Any]=0.02    ,      UpperCAmelCase__       :      List[Any]=2    ,      UpperCAmelCase__       :      str=True    ,      UpperCAmelCase__       :      List[str]=1    ,      UpperCAmelCase__       :      Any=0    ,      UpperCAmelCase__       :      Dict=2    ,      UpperCAmelCase__       :      int=1_0_2_4    ,      **UpperCAmelCase__       :      Optional[Any]    ,      )				->  Dict:
        lowerCAmelCase     =			vocab_size
        lowerCAmelCase     =			d_model
        lowerCAmelCase     =			decoder_ffn_dim
        lowerCAmelCase     =			decoder_layers
        lowerCAmelCase     =			decoder_attention_heads
        lowerCAmelCase     =			dropout
        lowerCAmelCase     =			attention_dropout
        lowerCAmelCase     =			activation_dropout
        lowerCAmelCase     =			activation_function
        lowerCAmelCase     =			init_std
        lowerCAmelCase     =			decoder_layerdrop
        lowerCAmelCase     =			use_cache
        lowerCAmelCase     =			decoder_layers
        lowerCAmelCase     =			scale_embedding  # scale factor will be sqrt(d_model) if True
        lowerCAmelCase     =			max_target_positions

        super().__init__(
            pad_token_id=UpperCAmelCase__    ,      bos_token_id=UpperCAmelCase__    ,      eos_token_id=UpperCAmelCase__    ,      decoder_start_token_id=UpperCAmelCase__    ,      **UpperCAmelCase__    ,      )





