






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     a    ( lowercase__   ):



      """simple docstring"""


      a  : jnp.ndarray
      a  : jnp.ndarray


class     a    ( nn.Module   ):



      """simple docstring"""


      a  : int
      a  : Tuple[int]							       =	(16, 32, 96, 256)
      a  : jnp.dtype							       =	jnp.floataa
      def 	UpperCAmelCase      (      self					:			Optional[int]					)							->      Dict:
        __UpperCAmelCase							:	Optional[Any]           =  nn.Conv(
            self.block_out_channels[0]		,      kernel_size=(3, 3)		,      padding=((1, 1), (1, 1))		,      dtype=self.dtype		,      )

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

        __UpperCAmelCase							:	Optional[Any]           =  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					:			Union[str, Any]		,      __lowercase					:			List[Any]					)							->      Union[str, Any]:
        __UpperCAmelCase							:	List[Any]           =  self.conv_in(__lowercase					)
        __UpperCAmelCase							:	int           =  nn.silu(__lowercase					)

        for block in self.blocks:
          __UpperCAmelCase							:	int           =  block(__lowercase					)
          __UpperCAmelCase							:	Union[str, Any]           =  nn.silu(__lowercase					)

        __UpperCAmelCase							:	str           =  self.conv_out(__lowercase					)

        return embedding


@flax_register_to_config
class     a    ( nn.Module							,       lowercase__							,       lowercase__   ):



      """simple docstring"""


      a  : int							       =	32
      a  : int							       =	4
      a  : Tuple[str]							       =	(
          "CrossAttnDownBlock2D",
          "CrossAttnDownBlock2D",
          "CrossAttnDownBlock2D",
          "DownBlock2D",
      )
      a  : Union[bool, Tuple[bool]]							       =	False
      a  : Tuple[int]							       =	(320, 640, 1_280, 1_280)
      a  : int							       =	2
      a  : Union[int, Tuple[int]]							       =	8
      a  : Optional[Union[int, Tuple[int]]]							       =	None
      a  : int							       =	1_280
      a  : float							       =	0.0
      a  : bool							       =	False
      a  : jnp.dtype							       =	jnp.floataa
      a  : bool							       =	True
      a  : int							       =	0
      a  : str							       =	"rgb"
      a  : Tuple[int]							       =	(16, 32, 96, 256)
      def 	UpperCAmelCase      (      self					:			Tuple		,      __lowercase					:			jax.random.KeyArray					)							->      FrozenDict:
        # init input tensors
        __UpperCAmelCase							:	Any           =  (1, self.in_channels, self.sample_size, self.sample_size)
        __UpperCAmelCase							:	Dict           =  jnp.zeros(__lowercase		,      dtype=jnp.floataa					)
        __UpperCAmelCase							:	str           =  jnp.ones((1,)		,      dtype=jnp.intaa					)
        __UpperCAmelCase							:	Optional[int]           =  jnp.zeros((1, 1, self.cross_attention_dim)		,      dtype=jnp.floataa					)
        __UpperCAmelCase							:	int           =  (1, 3, self.sample_size * 8, self.sample_size * 8)
        __UpperCAmelCase							:	int           =  jnp.zeros(__lowercase		,      dtype=jnp.floataa					)

        __UpperCAmelCase      , __UpperCAmelCase							:	Optional[int]           =  jax.random.split(__lowercase					)
        __UpperCAmelCase							:	List[Any]           =  {"""params""": params_rng, """dropout""": dropout_rng}

        return self.init(__lowercase		,      __lowercase		,      __lowercase		,      __lowercase		,      __lowercase					)["params"]
      def 	UpperCAmelCase      (      self					:			Union[str, Any]					)							->      List[Any]:
        __UpperCAmelCase							:	Union[str, Any]           =  self.block_out_channels
        __UpperCAmelCase							:	str           =  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.
        __UpperCAmelCase							:	Optional[int]           =  self.num_attention_heads or self.attention_head_dim

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

        # time
        __UpperCAmelCase							:	Union[str, Any]           =  FlaxTimesteps(
            block_out_channels[0]		,      flip_sin_to_cos=self.flip_sin_to_cos		,      freq_shift=self.config.freq_shift					)
        __UpperCAmelCase							:	Any           =  FlaxTimestepEmbedding(__lowercase		,      dtype=self.dtype					)

        __UpperCAmelCase							:	str           =  FlaxControlNetConditioningEmbedding(
            conditioning_embedding_channels=block_out_channels[0]		,      block_out_channels=self.conditioning_embedding_out_channels		,      )

        __UpperCAmelCase							:	Union[str, Any]           =  self.only_cross_attention
        if isinstance(__lowercase		,      __lowercase					):
          __UpperCAmelCase							:	List[Any]           =  (only_cross_attention,) * len(self.down_block_types					)

        if isinstance(__lowercase		,      __lowercase					):
          __UpperCAmelCase							:	Optional[int]           =  (num_attention_heads,) * len(self.down_block_types					)

        # down
        __UpperCAmelCase							:	Dict           =  []
        __UpperCAmelCase							:	List[Any]           =  []

        __UpperCAmelCase							:	str           =  block_out_channels[0]

        __UpperCAmelCase							:	int           =  nn.Conv(
            __lowercase		,      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(__lowercase					)

        for i, down_block_type in enumerate(self.down_block_types					):
          __UpperCAmelCase							:	Tuple           =  output_channel
          __UpperCAmelCase							:	Union[str, Any]           =  block_out_channels[i]
          __UpperCAmelCase							:	int           =  i == len(__lowercase					) - 1

          if down_block_type == "CrossAttnDownBlock2D":
            __UpperCAmelCase							:	Union[str, Any]           =  FlaxCrossAttnDownBlockaD(
                in_channels=__lowercase		,      out_channels=__lowercase		,      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:
            __UpperCAmelCase							:	Dict           =  FlaxDownBlockaD(
                in_channels=__lowercase		,      out_channels=__lowercase		,      dropout=self.dropout		,      num_layers=self.layers_per_block		,      add_downsample=not is_final_block		,      dtype=self.dtype		,      )

          down_blocks.append(__lowercase					)

          for _ in range(self.layers_per_block					):
            __UpperCAmelCase							:	Dict           =  nn.Conv(
                __lowercase		,      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(__lowercase					)

          if not is_final_block:
            __UpperCAmelCase							:	Any           =  nn.Conv(
                __lowercase		,      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(__lowercase					)

        __UpperCAmelCase							:	int           =  down_blocks
        __UpperCAmelCase							:	Union[str, Any]           =  controlnet_down_blocks

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

        __UpperCAmelCase							:	List[Any]           =  nn.Conv(
            __lowercase		,      kernel_size=(1, 1)		,      padding="""VALID"""		,      kernel_init=nn.initializers.zeros_init()		,      bias_init=nn.initializers.zeros_init()		,      dtype=self.dtype		,      )




      def __call__(      self					:			List[Any]		,      __lowercase					:			Union[str, Any]		,      __lowercase					:			int		,      __lowercase					:			List[str]		,      __lowercase					:			Any		,      __lowercase					:			float = 1.0		,      __lowercase					:			bool = True		,      __lowercase					:			bool = False		,      )							->      Union[FlaxControlNetOutput, Tuple]:
        __UpperCAmelCase							:	Union[str, Any]           =  self.controlnet_conditioning_channel_order
        if channel_order == "bgr":
          __UpperCAmelCase							:	List[Any]           =  jnp.flip(__lowercase		,      axis=1					)

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

        __UpperCAmelCase							:	Any           =  self.time_proj(__lowercase					)
        __UpperCAmelCase							:	Optional[Any]           =  self.time_embedding(__lowercase					)

        # 2. pre-process
        __UpperCAmelCase							:	int           =  jnp.transpose(__lowercase		,      (0, 2, 3, 1)					)
        __UpperCAmelCase							:	Union[str, Any]           =  self.conv_in(__lowercase					)

        __UpperCAmelCase							:	Tuple           =  jnp.transpose(__lowercase		,      (0, 2, 3, 1)					)
        __UpperCAmelCase							:	Union[str, Any]           =  self.controlnet_cond_embedding(__lowercase					)
        sample += controlnet_cond

        # 3. down
        __UpperCAmelCase							:	Union[str, Any]           =  (sample,)
        for down_block in self.down_blocks:
          if isinstance(__lowercase		,      __lowercase					):
            __UpperCAmelCase      , __UpperCAmelCase							:	str           =  down_block(__lowercase		,      __lowercase		,      __lowercase		,      deterministic=not train					)
          else:
            __UpperCAmelCase      , __UpperCAmelCase							:	Optional[int]           =  down_block(__lowercase		,      __lowercase		,      deterministic=not train					)
          down_block_res_samples += res_samples

        # 4. mid
        __UpperCAmelCase							:	Tuple           =  self.mid_block(__lowercase		,      __lowercase		,      __lowercase		,      deterministic=not train					)

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

        __UpperCAmelCase							:	Dict           =  controlnet_down_block_res_samples

        __UpperCAmelCase							:	Optional[int]           =  self.controlnet_mid_block(__lowercase					)

        # 6. scaling
        __UpperCAmelCase							:	List[str]           =  [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=__lowercase		,      mid_block_res_sample=__lowercase					)










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


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

a		:       List[Any]			     = {
    "facebook/xglm-564M": "https://huggingface.co/facebook/xglm-564M/resolve/main/config.json",
    # See all XGLM models at https://huggingface.co/models?filter=xglm
}
class     a    ( lowercase__   ):



      """simple docstring"""


      a  : List[Any]							       =	'xglm'
      a  : str							       =	['past_key_values']

      a  : Any							       =	{
          'num_attention_heads': 'attention_heads',
          'hidden_size': 'd_model',
          'num_hidden_layers': 'num_layers',
      }
      def __init__(      self					:			Optional[int]		,      __lowercase					:			int=256008		,      __lowercase					:			Tuple=2048		,      __lowercase					:			List[Any]=1024		,      __lowercase					:			str=4096		,      __lowercase					:			Optional[Any]=24		,      __lowercase					:			Optional[int]=16		,      __lowercase					:			List[Any]="gelu"		,      __lowercase					:			str=0.1		,      __lowercase					:			Dict=0.1		,      __lowercase					:			Tuple=0.0		,      __lowercase					:			Optional[int]=0.0		,      __lowercase					:			Dict=0.02		,      __lowercase					:			Optional[int]=True		,      __lowercase					:			Any=True		,      __lowercase					:			Dict=2		,      __lowercase					:			Optional[Any]=1		,      __lowercase					:			List[Any]=0		,      __lowercase					:			Optional[Any]=2		,      **__lowercase					:			List[str]		,      )							->      Optional[int]:
        __UpperCAmelCase							:	List[str]           =  vocab_size
        __UpperCAmelCase							:	Optional[Any]           =  max_position_embeddings
        __UpperCAmelCase							:	Optional[Any]           =  d_model
        __UpperCAmelCase							:	str           =  ffn_dim
        __UpperCAmelCase							:	List[str]           =  num_layers
        __UpperCAmelCase							:	Dict           =  attention_heads
        __UpperCAmelCase							:	str           =  activation_function
        __UpperCAmelCase							:	Optional[Any]           =  dropout
        __UpperCAmelCase							:	Any           =  attention_dropout
        __UpperCAmelCase							:	int           =  activation_dropout
        __UpperCAmelCase							:	Tuple           =  layerdrop
        __UpperCAmelCase							:	Tuple           =  init_std
        __UpperCAmelCase							:	List[str]           =  scale_embedding  # scale factor will be sqrt(d_model) if True
        __UpperCAmelCase							:	Union[str, Any]           =  use_cache

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


