






from typing import Optional

import numpy as np
import torch
from torch import nn
from transformers import GPTaConfig, GPTaLMHeadModel
from transformers.modeling_utils import ModuleUtilsMixin

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin






class 			_UpperCamelCase(       __lowerCamelCase		,				__lowerCamelCase		,				__lowerCamelCase		):
		__SCREAMING_SNAKE_CASE  :       Optional[int]							=							[r'''h\.\d+\.attn\.bias''', r'''h\.\d+\.attn\.masked_bias''']

		@register_to_config
		def __init__(					self		:    List[str]    ,       SCREAMING_SNAKE_CASE__		:    Optional[int]    ,       SCREAMING_SNAKE_CASE__		:    Dict    ,       SCREAMING_SNAKE_CASE__		:    List[Any] = None    ,       SCREAMING_SNAKE_CASE__		:    Optional[int] = 5_0_2_5_7    ,       SCREAMING_SNAKE_CASE__		:    Union[str, Any] = 1_0_2_4    ,       SCREAMING_SNAKE_CASE__		:    Union[str, Any] = 7_6_8    ,       SCREAMING_SNAKE_CASE__		:    Optional[int] = 1_2    ,       SCREAMING_SNAKE_CASE__		:    Any = 1_2    ,       SCREAMING_SNAKE_CASE__		:    List[Any] = None    ,       SCREAMING_SNAKE_CASE__		:    Tuple = "gelu_new"    ,       SCREAMING_SNAKE_CASE__		:    Dict = 0.1    ,       SCREAMING_SNAKE_CASE__		:    str = 0.1    ,       SCREAMING_SNAKE_CASE__		:    Union[str, Any] = 0.1    ,       SCREAMING_SNAKE_CASE__		:    Optional[int] = 1e-5    ,       SCREAMING_SNAKE_CASE__		:    Union[str, Any] = 0.02    ,       SCREAMING_SNAKE_CASE__		:    Optional[int] = True    ,       SCREAMING_SNAKE_CASE__		:    Dict = True    ,       SCREAMING_SNAKE_CASE__		:    List[Any] = False    ,       SCREAMING_SNAKE_CASE__		:    Dict = False    ,       ):



					'''simple docstring'''



					super().__init__()

					__a						:  Dict									=     prefix_length

					if prefix_inner_dim != n_embd and prefix_hidden_dim is None:
								raise ValueError(
								    f'''`prefix_hidden_dim` cannot be `None` when `prefix_inner_dim`: {prefix_hidden_dim} and'''
								    f''' `n_embd`: {n_embd} are not equal.'''    )

					__a						:  List[str]									=     prefix_inner_dim
					__a						:  Optional[Any]									=     prefix_hidden_dim

					__a						:  Any									=     (
					    nn.Linear(self.prefix_inner_dim    ,       self.prefix_hidden_dim    )
					    if self.prefix_hidden_dim is not None
					    else nn.Identity()
					)
					__a						:  Dict									=     (
					    nn.Linear(self.prefix_hidden_dim    ,       A_    ) if self.prefix_hidden_dim is not None else nn.Identity()
					)

					__a						:  Union[str, Any]									=     GPTaConfig(
					    vocab_size=A_    ,       n_positions=A_    ,       n_embd=A_    ,       n_layer=A_    ,       n_head=A_    ,       n_inner=A_    ,       activation_function=A_    ,       resid_pdrop=A_    ,       embd_pdrop=A_    ,       attn_pdrop=A_    ,       layer_norm_epsilon=A_    ,       initializer_range=A_    ,       scale_attn_weights=A_    ,       use_cache=A_    ,       scale_attn_by_inverse_layer_idx=A_    ,       reorder_and_upcast_attn=A_    ,       )
					__a						:  int									=     GPTaLMHeadModel(A_    )

		def    __lowerCAmelCase  (					self		:    List[Any]    ,       SCREAMING_SNAKE_CASE__		:    Optional[Any]    ,       SCREAMING_SNAKE_CASE__		:    Optional[int]    ,       SCREAMING_SNAKE_CASE__		:    Any = None    ,       SCREAMING_SNAKE_CASE__		:    Dict = None    ,       ):



					'''simple docstring'''



					__a						:  Union[str, Any]									=     self.transformer.transformer.wte(A_    )
					__a						:  List[str]									=     self.encode_prefix(A_    )
					__a						:  List[Any]									=     self.decode_prefix(A_    )
					__a						:  Optional[int]									=     torch.cat((prefix_embeds, embedding_text)    ,       dim=1    )

					if labels is not None:
								__a						:  List[str]									=     self.get_dummy_token(input_ids.shape[0]    ,       input_ids.device    )
								__a						:  Optional[Any]									=     torch.cat((dummy_token, input_ids)    ,       dim=1    )
					__a						:  Union[str, Any]									=     self.transformer(inputs_embeds=A_    ,       labels=A_    ,       attention_mask=A_    )
					if self.prefix_hidden_dim is not None:
								return out, hidden
					else:
								return out

		def    __lowerCAmelCase  (					self		:    List[Any]    ,       SCREAMING_SNAKE_CASE__		:    Any    ,       SCREAMING_SNAKE_CASE__		:    Optional[Any]    ):



					'''simple docstring'''



					return torch.zeros(A_    ,       self.prefix_length    ,       dtype=torch.intaa    ,       device=A_    )

		def    __lowerCAmelCase  (					self		:    Any    ,       SCREAMING_SNAKE_CASE__		:    List[str]    ):



					'''simple docstring'''



					return self.encode_prefix(A_    )

		@torch.no_grad()
		def    __lowerCAmelCase  (					self		:    Any    ,       SCREAMING_SNAKE_CASE__		:    Dict    ,       SCREAMING_SNAKE_CASE__		:    List[str]    ,       SCREAMING_SNAKE_CASE__		:    Optional[int]    ):



					'''simple docstring'''



					__a						:  str									=     torch.split(A_    ,       1    ,       dim=0    )
					__a						:  List[str]									=     []
					__a						:  Dict									=     []
					for feature in features:
								__a						:  Tuple									=     self.decode_prefix(feature.to(A_    )    )  # back to the clip feature
								# Only support beam search for now
								__a						,					__a						:  List[str]									=     self.generate_beam(
								    input_embeds=A_    ,       device=A_    ,       eos_token_id=A_    )
								generated_tokens.append(output_tokens[0]    )
								generated_seq_lengths.append(seq_lengths[0]    )
					__a						:  str									=     torch.stack(A_    )
					__a						:  Dict									=     torch.stack(A_    )
					return generated_tokens, generated_seq_lengths



		@torch.no_grad()
		def    __lowerCAmelCase  (					self		:    Optional[Any]    ,       SCREAMING_SNAKE_CASE__		:    Optional[int]=None    ,       SCREAMING_SNAKE_CASE__		:    Optional[Any]=None    ,       SCREAMING_SNAKE_CASE__		:    List[Any]=None    ,       SCREAMING_SNAKE_CASE__		:    List[str] = 5    ,       SCREAMING_SNAKE_CASE__		:    Tuple = 6_7    ,       SCREAMING_SNAKE_CASE__		:    List[Any] = 1.0    ,       SCREAMING_SNAKE_CASE__		:    Optional[int] = None    ,       ):



					'''simple docstring'''



					__a						:  Union[str, Any]									=     eos_token_id
					__a						:  Union[str, Any]									=     None
					__a						:  Optional[int]									=     None
					__a						:  str									=     torch.ones(A_    ,       device=A_    ,       dtype=torch.int    )
					__a						:  Tuple									=     torch.zeros(A_    ,       device=A_    ,       dtype=torch.bool    )

					if input_embeds is not None:
								__a						:  Tuple									=     input_embeds
					else:
								__a						:  Any									=     self.transformer.transformer.wte(A_    )

					for i in range(A_    ):
								__a						:  Union[str, Any]									=     self.transformer(inputs_embeds=A_    )
								__a						:  List[str]									=     outputs.logits
								__a						:  Tuple									=     logits[:, -1, :] / (temperature if temperature > 0 else 1.0)
								__a						:  Dict									=     logits.softmax(-1    ).log()

								if scores is None:
											__a						,					__a						:  str									=     logits.topk(A_    ,       -1    )
											__a						:  List[str]									=     generated.expand(A_    ,       *generated.shape[1:]    )
											__a						,					__a						:  Any									=     next_tokens.permute(1    ,       0    ), scores.squeeze(0    )
											if tokens is None:
														__a						:  Optional[int]									=     next_tokens
											else:
														__a						:  Any									=     tokens.expand(A_    ,       *tokens.shape[1:]    )
														__a						:  Optional[Any]									=     torch.cat((tokens, next_tokens)    ,       dim=1    )
								else:
											__a						:  List[Any]									=     -float(np.inf    )
											__a						:  List[str]									=     0
											__a						:  Any									=     scores[:, None] + logits
											seq_lengths[~is_stopped] += 1
											__a						:  Any									=     scores_sum / seq_lengths[:, None]
											__a						,					__a						:  List[str]									=     scores_sum_average.view(-1    ).topk(A_    ,       -1    )
											__a						:  Union[str, Any]									=     next_tokens // scores_sum.shape[1]
											__a						:  List[str]									=     seq_lengths[next_tokens_source]
											__a						:  Any									=     next_tokens % scores_sum.shape[1]
											__a						:  str									=     next_tokens.unsqueeze(1    )
											__a						:  Optional[Any]									=     tokens[next_tokens_source]
											__a						:  Optional[int]									=     torch.cat((tokens, next_tokens)    ,       dim=1    )
											__a						:  Dict									=     generated[next_tokens_source]
											__a						:  Optional[Any]									=     scores_sum_average * seq_lengths
											__a						:  List[Any]									=     is_stopped[next_tokens_source]

								__a						:  List[Any]									=     self.transformer.transformer.wte(next_tokens.squeeze()    ).view(generated.shape[0]    ,       1    ,       -1    )
								__a						:  List[str]									=     torch.cat((generated, next_token_embed)    ,       dim=1    )
								__a						:  Dict									=     is_stopped + next_tokens.eq(A_    ).squeeze()
								if is_stopped.all():
											break

					__a						:  Any									=     scores / seq_lengths
					__a						:  str									=     scores.argsort(descending=A_    )
					# tokens tensors are already padded to max_seq_length
					__a						:  str									=     [tokens[i] for i in order]
					__a						:  List[Any]									=     torch.stack(A_    ,       dim=0    )
					__a						:  Any									=     torch.tensor([seq_lengths[i] for i in order]    ,       dtype=seq_lengths.dtype    )
					return output_texts, seq_lengths












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






 '''simple docstring'''





 __UpperCamelCase       =      42
 __UpperCamelCase       =      42

class     snake_case_		(   nn.Module							):






 '''simple docstring'''





 __UpperCamelCase       =      42
 __UpperCamelCase       =      (1_6, 3_2, 9_6, 2_5_6)
 __UpperCamelCase       =      jnp.floataa

 def  __UpperCAmelCase						(  self		)	->       Any:
    UpperCAmelCase__						=nn.Conv(
        self.block_out_channels[0],		kernel_size=(3, 3),		padding=((1, 1), (1, 1)),		dtype=self.dtype,		)

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

    UpperCAmelCase__						=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,		A_		)	->       Any:
    UpperCAmelCase__						=self.conv_in(A_		)
    UpperCAmelCase__						=nn.silu(A_		)

    for block in self.blocks:
       UpperCAmelCase__						=block(A_		)
       UpperCAmelCase__						=nn.silu(A_		)

    UpperCAmelCase__						=self.conv_out(A_		)

    return embedding

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






 '''simple docstring'''





 __UpperCamelCase       =      3_2
 __UpperCamelCase       =      4
 __UpperCamelCase       =      (
     "CrossAttnDownBlock2D",
     "CrossAttnDownBlock2D",
     "CrossAttnDownBlock2D",
     "DownBlock2D",
 )
 __UpperCamelCase       =      False
 __UpperCamelCase       =      (3_2_0, 6_4_0, 1_2_8_0, 1_2_8_0)
 __UpperCamelCase       =      2
 __UpperCamelCase       =      8
 __UpperCamelCase       =      None
 __UpperCamelCase       =      1_2_8_0
 __UpperCamelCase       =      0.0
 __UpperCamelCase       =      False
 __UpperCamelCase       =      jnp.floataa
 __UpperCamelCase       =      True
 __UpperCamelCase       =      0
 __UpperCamelCase       =      "rgb"
 __UpperCamelCase       =      (1_6, 3_2, 9_6, 2_5_6)

 def  __UpperCAmelCase						(  self,		A_		)	->       FrozenDict:
    # init input tensors
    UpperCAmelCase__						=(1, self.in_channels, self.sample_size, self.sample_size)
    UpperCAmelCase__						=jnp.zeros(A_,		dtype=jnp.floataa		)
    UpperCAmelCase__						=jnp.ones((1,),		dtype=jnp.intaa		)
    UpperCAmelCase__						=jnp.zeros((1, 1, self.cross_attention_dim),		dtype=jnp.floataa		)
    UpperCAmelCase__						=(1, 3, self.sample_size * 8, self.sample_size * 8)
    UpperCAmelCase__						=jnp.zeros(A_,		dtype=jnp.floataa		)

    UpperCAmelCase__			,							UpperCAmelCase__						=jax.random.split(A_		)
    UpperCAmelCase__						={"params": params_rng, "dropout": dropout_rng}

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

 def  __UpperCAmelCase						(  self		)	->       int:
    UpperCAmelCase__						=self.block_out_channels
    UpperCAmelCase__						=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__						=self.num_attention_heads or self.attention_head_dim

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

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

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

    UpperCAmelCase__						=self.only_cross_attention
    if isinstance(A_,		A_		):
       UpperCAmelCase__						=(only_cross_attention,) * len(self.down_block_types		)

    if isinstance(A_,		A_		):
       UpperCAmelCase__						=(num_attention_heads,) * len(self.down_block_types		)

    # down
    UpperCAmelCase__						=[]
    UpperCAmelCase__						=[]

    UpperCAmelCase__						=block_out_channels[0]

    UpperCAmelCase__						=nn.Conv(
        A_,		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(A_		)

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

       if down_block_type == "CrossAttnDownBlock2D":
          UpperCAmelCase__						=FlaxCrossAttnDownBlockaD(
              in_channels=A_,		out_channels=A_,		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__						=FlaxDownBlockaD(
              in_channels=A_,		out_channels=A_,		dropout=self.dropout,		num_layers=self.layers_per_block,		add_downsample=not is_final_block,		dtype=self.dtype,		)

       down_blocks.append(A_		)

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

       if not is_final_block:
          UpperCAmelCase__						=nn.Conv(
              A_,		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(A_		)

    UpperCAmelCase__						=down_blocks
    UpperCAmelCase__						=controlnet_down_blocks

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

    UpperCAmelCase__						=nn.Conv(
        A_,		kernel_size=(1, 1),		padding="VALID",		kernel_init=nn.initializers.zeros_init(),		bias_init=nn.initializers.zeros_init(),		dtype=self.dtype,		)






 def __call__(  self,		A_,		A_,		A_,		A_,		A_ = 1.0,		A_ = True,		A_ = False,		)	->       Union[FlaxControlNetOutput, Tuple]:
    UpperCAmelCase__						=self.controlnet_conditioning_channel_order
    if channel_order == "bgr":
       UpperCAmelCase__						=jnp.flip(A_,		axis=1		)

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

    UpperCAmelCase__						=self.time_proj(A_		)
    UpperCAmelCase__						=self.time_embedding(A_		)

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

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

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

    # 4. mid
    UpperCAmelCase__						=self.mid_block(A_,		A_,		A_,		deterministic=not train		)

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

    UpperCAmelCase__						=controlnet_down_block_res_samples

    UpperCAmelCase__						=self.controlnet_mid_block(A_		)

    # 6. scaling
    UpperCAmelCase__						=[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=A_,		mid_block_res_sample=A_		)



