



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







					'''simple docstring'''







					lowerCAmelCase_				:    jnp.ndarray
					lowerCAmelCase_				:    jnp.ndarray





class 	lowerCamelCase      (						nn.Module  ):







					'''simple docstring'''







					lowerCAmelCase_				:    int
					lowerCAmelCase_				:    Tuple[int]									=  (16, 32, 96, 256)
					lowerCAmelCase_				:    jnp.dtype									=  jnp.floataa



					def    A__		(			self     ):
							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(
									    lowerCAmelCase    ,	kernel_size=(3, 3)    ,	padding=((1, 1), (1, 1))    ,	dtype=self.dtype    ,	)
									blocks.append(lowerCAmelCase     )
									UpperCAmelCase_						=				nn.Conv(
									    lowerCAmelCase    ,	kernel_size=(3, 3)    ,	strides=(2, 2)    ,	padding=((1, 1), (1, 1))    ,	dtype=self.dtype    ,	)
									blocks.append(lowerCAmelCase     )
							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    ,	lowerCAmelCase     ):
							UpperCAmelCase_						=				self.conv_in(lowerCAmelCase     )
							UpperCAmelCase_						=				nn.silu(lowerCAmelCase     )

							for block in self.blocks:
									UpperCAmelCase_						=				block(lowerCAmelCase     )
									UpperCAmelCase_						=				nn.silu(lowerCAmelCase     )

							UpperCAmelCase_						=				self.conv_out(lowerCAmelCase     )

							return embedding





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







					'''simple docstring'''







					lowerCAmelCase_				:    int									=  32
					lowerCAmelCase_				:    int									=  4
					lowerCAmelCase_				:    Tuple[str]									=  (
					    "CrossAttnDownBlock2D",
					    "CrossAttnDownBlock2D",
					    "CrossAttnDownBlock2D",
					    "DownBlock2D",
					)
					lowerCAmelCase_				:    Union[bool, Tuple[bool]]									=  False
					lowerCAmelCase_				:    Tuple[int]									=  (320, 640, 1280, 1280)
					lowerCAmelCase_				:    int									=  2
					lowerCAmelCase_				:    Union[int, Tuple[int]]									=  8
					lowerCAmelCase_				:    Optional[Union[int, Tuple[int]]]									=  None
					lowerCAmelCase_				:    int									=  1280
					lowerCAmelCase_				:    float									=  0.0
					lowerCAmelCase_				:    bool									=  False
					lowerCAmelCase_				:    jnp.dtype									=  jnp.floataa
					lowerCAmelCase_				:    bool									=  True
					lowerCAmelCase_				:    int									=  0
					lowerCAmelCase_				:    str									=  "rgb"
					lowerCAmelCase_				:    Tuple[int]									=  (16, 32, 96, 256)



					def    A__		(			self    ,	lowerCAmelCase     ):
							# init input tensors
							UpperCAmelCase_						=				(1, self.in_channels, self.sample_size, self.sample_size)
							UpperCAmelCase_						=				jnp.zeros(lowerCAmelCase    ,	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(lowerCAmelCase    ,	dtype=jnp.floataa     )

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

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



					def    A__		(			self     ):
							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(lowerCAmelCase    ,	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(lowerCAmelCase    ,	lowerCAmelCase     ):
									UpperCAmelCase_						=				(only_cross_attention,) * len(self.down_block_types     )

							if isinstance(lowerCAmelCase    ,	lowerCAmelCase     ):
									UpperCAmelCase_						=				(num_attention_heads,) * len(self.down_block_types     )

							# down
							UpperCAmelCase_						=				[]
							UpperCAmelCase_						=				[]

							UpperCAmelCase_						=				block_out_channels[0]

							UpperCAmelCase_						=				nn.Conv(
							    lowerCAmelCase    ,	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(lowerCAmelCase     )

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

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

									down_blocks.append(lowerCAmelCase     )

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

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

							UpperCAmelCase_						=				down_blocks
							UpperCAmelCase_						=				controlnet_down_blocks

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

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



					def __call__(			self    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase = 1.0    ,	lowerCAmelCase = True    ,	lowerCAmelCase = False    ,	):
							UpperCAmelCase_						=				self.controlnet_conditioning_channel_order
							if channel_order == "bgr":
									UpperCAmelCase_						=				jnp.flip(lowerCAmelCase    ,	axis=1     )

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

							UpperCAmelCase_						=				self.time_proj(lowerCAmelCase     )
							UpperCAmelCase_						=				self.time_embedding(lowerCAmelCase     )

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

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

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

							# 4. mid
							UpperCAmelCase_						=				self.mid_block(lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	deterministic=not train     )

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

							UpperCAmelCase_						=				controlnet_down_block_res_samples

							UpperCAmelCase_						=				self.controlnet_mid_block(lowerCAmelCase     )

							# 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=lowerCAmelCase    ,	mid_block_res_sample=lowerCAmelCase     )






import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ....tokenization_utils_fast import PreTrainedTokenizerFast
from ....utils import logging
from .tokenization_retribert import RetriBertTokenizer


SCREAMING_SNAKE_CASE     				=						logging.get_logger(__name__)

SCREAMING_SNAKE_CASE     				=						{"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"}

SCREAMING_SNAKE_CASE     				=						{
    "vocab_file": {
        "yjernite/retribert-base-uncased": (
            "https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/vocab.txt"
        ),
    },
    "tokenizer_file": {
        "yjernite/retribert-base-uncased": (
            "https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/tokenizer.json"
        ),
    },
}

SCREAMING_SNAKE_CASE     				=						{
    "yjernite/retribert-base-uncased": 512,
}


SCREAMING_SNAKE_CASE     				=						{
    "yjernite/retribert-base-uncased": {"do_lower_case": True},
}



class 	lowerCamelCase      (						lowercase__  ):







					'''simple docstring'''







					lowerCAmelCase_				:    List[str]									=  VOCAB_FILES_NAMES
					lowerCAmelCase_				:    int									=  PRETRAINED_VOCAB_FILES_MAP
					lowerCAmelCase_				:    Any									=  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
					lowerCAmelCase_				:    Dict									=  PRETRAINED_INIT_CONFIGURATION
					lowerCAmelCase_				:    List[str]									=  RetriBertTokenizer
					lowerCAmelCase_				:    Union[str, Any]									=  ['input_ids', 'attention_mask']



					def __init__(			self    ,	lowerCAmelCase=None    ,	lowerCAmelCase=None    ,	lowerCAmelCase=True    ,	lowerCAmelCase="[UNK]"    ,	lowerCAmelCase="[SEP]"    ,	lowerCAmelCase="[PAD]"    ,	lowerCAmelCase="[CLS]"    ,	lowerCAmelCase="[MASK]"    ,	lowerCAmelCase=True    ,	lowerCAmelCase=None    ,	**lowerCAmelCase    ,	):
							super().__init__(
							    lowerCAmelCase    ,	tokenizer_file=lowerCAmelCase    ,	do_lower_case=lowerCAmelCase    ,	unk_token=lowerCAmelCase    ,	sep_token=lowerCAmelCase    ,	pad_token=lowerCAmelCase    ,	cls_token=lowerCAmelCase    ,	mask_token=lowerCAmelCase    ,	tokenize_chinese_chars=lowerCAmelCase    ,	strip_accents=lowerCAmelCase    ,	**lowerCAmelCase    ,	)

							UpperCAmelCase_						=				json.loads(self.backend_tokenizer.normalizer.__getstate__()     )
							if (
							    normalizer_state.get("lowercase"    ,	lowerCAmelCase     ) != do_lower_case
							    or normalizer_state.get("strip_accents"    ,	lowerCAmelCase     ) != strip_accents
							    or normalizer_state.get("handle_chinese_chars"    ,	lowerCAmelCase     ) != tokenize_chinese_chars
							):
									UpperCAmelCase_						=				getattr(lowerCAmelCase    ,	normalizer_state.pop("type"     )     )
									UpperCAmelCase_						=				do_lower_case
									UpperCAmelCase_						=				strip_accents
									UpperCAmelCase_						=				tokenize_chinese_chars
									UpperCAmelCase_						=				normalizer_class(**lowerCAmelCase     )

							UpperCAmelCase_						=				do_lower_case



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase=None     ):
							UpperCAmelCase_						=				[self.cls_token_id] + token_ids_a + [self.sep_token_id]

							if token_ids_a:
									output += token_ids_a + [self.sep_token_id]

							return output



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase = None     ):
							UpperCAmelCase_						=				[self.sep_token_id]
							UpperCAmelCase_						=				[self.cls_token_id]
							if token_ids_a is None:
									return len(cls + token_ids_a + sep     ) * [0]
							return len(cls + token_ids_a + sep     ) * [0] + len(token_ids_a + sep     ) * [1]



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase = None     ):
							UpperCAmelCase_						=				self._tokenizer.model.save(lowerCAmelCase    ,	name=lowerCAmelCase     )
							return tuple(lowerCAmelCase     )

