
"""simple docstring"""




import io
import os
import unicodedata
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


a		:       List[str]     						=       logging.get_logger(__name__)

a		:       Union[str, Any]     						=       '▁'

a		:       Optional[int]     						=       {'vocab_file': 'vocab.txt', 'sentencepiece_model_ckpt': 'sentencepiece.bpe.model'}

a		:       Dict     						=       {
    'sentencepiece_model_file': 'sentencepiece.bpe.model',
    'vocab_file': 'vocab.txt',
}

a		:       Any     						=       {
    'vocab_file': {
        'ernie-m-base': 'https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt',
        'ernie-m-large': 'https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt',
    },
    'sentencepiece_model_file': {
        'ernie-m-base': 'https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model',
        'ernie-m-large': 'https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model',
    },
}

a		:       str     						=       {
    'ernie-m-base': 5_14,
    'ernie-m-large': 5_14,
}

a		:       Tuple     						=       {
    'ernie-m-base': {'do_lower_case': False},
    'ernie-m-large': {'do_lower_case': False},
}







class     lowercase(_lowercase       ):
     __snake_case:	List[str]						=     ["input_ids"]

     __snake_case:	str						=     VOCAB_FILES_NAMES
     __snake_case:	Optional[int]						=     PRETRAINED_INIT_CONFIGURATION
     __snake_case:	Dict						=     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
     __snake_case:	Optional[int]						=     PRETRAINED_VOCAB_FILES_MAP
     __snake_case:	Tuple						=     RESOURCE_FILES_NAMES



     def __init__(							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE=None     ,				__SCREAMING_SNAKE_CASE=False     ,				__SCREAMING_SNAKE_CASE="utf8"     ,				__SCREAMING_SNAKE_CASE="[UNK]"     ,				__SCREAMING_SNAKE_CASE="[SEP]"     ,				__SCREAMING_SNAKE_CASE="[PAD]"     ,				__SCREAMING_SNAKE_CASE="[CLS]"     ,				__SCREAMING_SNAKE_CASE="[MASK]"     ,				__SCREAMING_SNAKE_CASE = None     ,				**__SCREAMING_SNAKE_CASE     ,				)		->     None:







         """simple docstring"""



         a__		=			{} if sp_model_kwargs is None else sp_model_kwargs
         super().__init__(
             do_lower_case=__SCREAMING_SNAKE_CASE     ,				unk_token=__SCREAMING_SNAKE_CASE     ,				sep_token=__SCREAMING_SNAKE_CASE     ,				pad_token=__SCREAMING_SNAKE_CASE     ,				cls_token=__SCREAMING_SNAKE_CASE     ,				mask_token=__SCREAMING_SNAKE_CASE     ,				vocab_file=__SCREAMING_SNAKE_CASE     ,				encoding=__SCREAMING_SNAKE_CASE     ,				sp_model_kwargs=self.sp_model_kwargs     ,				**__SCREAMING_SNAKE_CASE     ,				)
         a__		=			do_lower_case
         a__		=			sentencepiece_model_ckpt
         a__		=			spm.SentencePieceProcessor(**self.sp_model_kwargs   )
         self.sp_model.Load(__SCREAMING_SNAKE_CASE   )

         # to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning
         if vocab_file is not None:
             a__		=			self.load_vocab(filepath=__SCREAMING_SNAKE_CASE   )
         else:
             a__		=			{self.sp_model.id_to_piece(__SCREAMING_SNAKE_CASE   ): id for id in range(self.sp_model.get_piece_size()   )}
         a__		=			{v: k for k, v in self.vocab.items()}



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE   )		->     List[Any]:







         """simple docstring"""



         if text is None:
             return None

         a__		=			self.tokenize(__SCREAMING_SNAKE_CASE   )
         a__	,     a__		=			'', []

         for i, ch in enumerate(__SCREAMING_SNAKE_CASE   ):
             if ch in self.SP_CHAR_MAPPING:
                 a__		=			self.SP_CHAR_MAPPING.get(__SCREAMING_SNAKE_CASE   )
             else:
                 a__		=			unicodedata.normalize('NFKC'     ,				__SCREAMING_SNAKE_CASE   )
             if self.is_whitespace(__SCREAMING_SNAKE_CASE   ):
                 continue
             normalized_text += ch
             char_mapping.extend([i] * len(__SCREAMING_SNAKE_CASE   )   )

         a__	,     a__	,     a__		=			normalized_text, [], 0

         if self.do_lower_case:
             a__		=			text.lower()

         for token in split_tokens:
             if token[:1] == "▁":
                 a__		=			token[1:]
             a__		=			text[offset:].index(__SCREAMING_SNAKE_CASE   ) + offset
             a__		=			start + len(__SCREAMING_SNAKE_CASE   )

             token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1)   )
             a__		=			end
         return token_mapping



     @property
     def 			lowercase__  (							self   )		->     Optional[int]:







         """simple docstring"""



         return len(self.vocab   )



     def 			lowercase__  (							self   )		->     Any:







         """simple docstring"""



         return dict(self.vocab     ,				**self.added_tokens_encoder   )



     def __getstate__(							self   )		->     Any:







         """simple docstring"""



         a__		=			self.__dict__.copy()
         a__		=			None
         return state



     def __setstate__(							self     ,				__SCREAMING_SNAKE_CASE   )		->     Optional[Any]:







         """simple docstring"""



         a__		=			d

         # for backward compatibility
         if not hasattr(self     ,				'sp_model_kwargs'   ):
             a__		=			{}

         a__		=			spm.SentencePieceProcessor(**self.sp_model_kwargs   )
         self.sp_model.Load(self.sentencepiece_model_ckpt   )



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE   )		->     Dict:







         """simple docstring"""



         return "".join((self.SP_CHAR_MAPPING.get(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   ) for c in text)   )



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE=False     ,				__SCREAMING_SNAKE_CASE=6_4     ,				__SCREAMING_SNAKE_CASE=0.1   )		->     Union[str, Any]:







         """simple docstring"""



         if self.sp_model_kwargs.get('enable_sampling'   ) is True:
             a__		=			True
         if self.sp_model_kwargs.get('alpha'   ) is not None:
             a__		=			self.sp_model_kwargs.get('alpha'   )
         if self.sp_model_kwargs.get('nbest_size'   ) is not None:
             a__		=			self.sp_model_kwargs.get('nbest_size'   )

         if not enable_sampling:
             a__		=			self.sp_model.EncodeAsPieces(__SCREAMING_SNAKE_CASE   )
         else:
             a__		=			self.sp_model.SampleEncodeAsPieces(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )
         a__		=			[]
         for pi, piece in enumerate(__SCREAMING_SNAKE_CASE   ):
             if piece == SPIECE_UNDERLINE:
                 if not pieces[pi + 1].startswith(__SCREAMING_SNAKE_CASE   ) and pi != 0:
                     new_pieces.append(__SCREAMING_SNAKE_CASE   )
                     continue
                 else:
                     continue
             a__		=			0
             for i, chunk in enumerate(__SCREAMING_SNAKE_CASE   ):
                 if chunk == SPIECE_UNDERLINE:
                     continue
                 if self.is_ch_char(__SCREAMING_SNAKE_CASE   ) or self.is_punct(__SCREAMING_SNAKE_CASE   ):
                     if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
                         new_pieces.append(piece[lst_i:i]   )
                     new_pieces.append(__SCREAMING_SNAKE_CASE   )
                     a__		=			i + 1
                 elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit():
                     if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
                         new_pieces.append(piece[lst_i:i]   )
                     a__		=			i
                 elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit():
                     if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
                         new_pieces.append(piece[lst_i:i]   )
                     a__		=			i
             if len(__SCREAMING_SNAKE_CASE   ) > lst_i:
                 new_pieces.append(piece[lst_i:]   )
         return new_pieces



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE   )		->     List[Any]:







         """simple docstring"""



         a__		=			''.join(__SCREAMING_SNAKE_CASE   ).replace(__SCREAMING_SNAKE_CASE     ,				' '   ).strip()
         return out_string



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE   )		->     List[str]:







         """simple docstring"""



         a__		=			self.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE   )
         a__		=			''.join(__SCREAMING_SNAKE_CASE   ).replace(__SCREAMING_SNAKE_CASE     ,				' '   ).strip()
         return out_string



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE   )		->     Union[str, Any]:







         """simple docstring"""



         return self.vocab.get(__SCREAMING_SNAKE_CASE     ,				self.vocab.get(self.unk_token   )   )



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE   )		->     int:







         """simple docstring"""



         return self.reverse_vocab.get(__SCREAMING_SNAKE_CASE     ,				self.unk_token   )



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE=None   )		->     List[str]:







         """simple docstring"""



         if token_ids_a is None:
             return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
         a__		=			[self.cls_token_id]
         a__		=			[self.sep_token_id]
         return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE=None   )		->     List[Any]:







         """simple docstring"""



         if offset_mapping_a is None:
             return [(0, 0)] + offset_mapping_a + [(0, 0)]

         return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)]



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE=None     ,				__SCREAMING_SNAKE_CASE=False   )		->     Optional[int]:







         """simple docstring"""



         if already_has_special_tokens:
             if token_ids_a is not None:
                 raise ValueError(
                     'You should not supply a second sequence if the provided sequence of '
                     'ids is already formatted with special tokens for the model.'   )
             return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a]

         if token_ids_a is not None:
             return [1] + ([0] * len(__SCREAMING_SNAKE_CASE   )) + [1, 1] + ([0] * len(__SCREAMING_SNAKE_CASE   )) + [1]
         return [1] + ([0] * len(__SCREAMING_SNAKE_CASE   )) + [1]



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE = None   )		->     List[int]:







         """simple docstring"""



         if token_ids_a is None:
             # [CLS] X [SEP]
             return (len(__SCREAMING_SNAKE_CASE   ) + 2) * [0]

         # [CLS] A [SEP] [SEP] B [SEP]
         return [0] * (len(__SCREAMING_SNAKE_CASE   ) + 1) + [1] * (len(__SCREAMING_SNAKE_CASE   ) + 3)



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE   )		->     Union[str, Any]:







         """simple docstring"""



         if "\u4e00" <= char <= "\u9fff":
             return True
         return False



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE   )		->     Any:







         """simple docstring"""



         if ("a" <= char <= "z") or ("A" <= char <= "Z"):
             return True
         return False



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE   )		->     Optional[int]:







         """simple docstring"""



         if char in ",;:.?!~，；：。？！《》【】":
             return True
         return False



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE   )		->     Optional[int]:







         """simple docstring"""



         if char == " " or char == "\t" or char == "\n" or char == "\r":
             return True
         if len(__SCREAMING_SNAKE_CASE   ) == 1:
             a__		=			unicodedata.category(__SCREAMING_SNAKE_CASE   )
             if cat == "Zs":
                 return True
         return False



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE   )		->     int:







         """simple docstring"""



         a__		=			{}
         with io.open(__SCREAMING_SNAKE_CASE     ,				'r'     ,				encoding='utf-8'   ) as f:
             for index, line in enumerate(__SCREAMING_SNAKE_CASE   ):
                 a__		=			line.rstrip('\n'   )
                 a__		=			int(__SCREAMING_SNAKE_CASE   )

         return token_to_idx




     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE = None   )		->     Tuple[str]:







         """simple docstring"""



         a__		=			0
         if os.path.isdir(__SCREAMING_SNAKE_CASE   ):
             a__		=			os.path.join(
                 __SCREAMING_SNAKE_CASE     ,				(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']   )
         else:
             a__		=			(filename_prefix + '-' if filename_prefix else '') + save_directory
         with open(__SCREAMING_SNAKE_CASE     ,				'w'     ,				encoding='utf-8'   ) as writer:
             for token, token_index in sorted(self.vocab.items()     ,				key=lambda __SCREAMING_SNAKE_CASE   : kv[1]   ):
                 if index != token_index:
                     logger.warning(
                         f'Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.'
                         ' Please check that the vocabulary is not corrupted!'   )
                     a__		=			token_index
                 writer.write(token + '\n'   )
                 index += 1

         a__		=			os.path.join(__SCREAMING_SNAKE_CASE     ,				'sentencepiece.bpe.model'   )
         with open(__SCREAMING_SNAKE_CASE     ,				'wb'   ) as fi:
             a__		=			self.sp_model.serialized_model_proto()
             fi.write(__SCREAMING_SNAKE_CASE   )

         return (vocab_file,)




"""simple docstring"""




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       ):
     __snake_case:	jnp.ndarray
     __snake_case:	jnp.ndarray







class     lowercase(nn.Module       ):
     __snake_case:	int
     __snake_case:	Tuple[int]						=     (16, 32, 96, 256)
     __snake_case:	jnp.dtype						=     jnp.floataa



     def 			lowercase__  (							self   )		->     Dict:







         """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(
                 __SCREAMING_SNAKE_CASE     ,				kernel_size=(3, 3)     ,				padding=((1, 1), (1, 1))     ,				dtype=self.dtype     ,				)
             blocks.append(__SCREAMING_SNAKE_CASE   )
             a__		=			nn.Conv(
                 __SCREAMING_SNAKE_CASE     ,				kernel_size=(3, 3)     ,				strides=(2, 2)     ,				padding=((1, 1), (1, 1))     ,				dtype=self.dtype     ,				)
             blocks.append(__SCREAMING_SNAKE_CASE   )
         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     ,				__SCREAMING_SNAKE_CASE   )		->     Any:







         """simple docstring"""



         a__		=			self.conv_in(__SCREAMING_SNAKE_CASE   )
         a__		=			nn.silu(__SCREAMING_SNAKE_CASE   )

         for block in self.blocks:
             a__		=			block(__SCREAMING_SNAKE_CASE   )
             a__		=			nn.silu(__SCREAMING_SNAKE_CASE   )

         a__		=			self.conv_out(__SCREAMING_SNAKE_CASE   )

         return embedding







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



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE   )		->     FrozenDict:







         """simple docstring"""



         a__		=			(1, self.in_channels, self.sample_size, self.sample_size)
         a__		=			jnp.zeros(__SCREAMING_SNAKE_CASE     ,				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(__SCREAMING_SNAKE_CASE     ,				dtype=jnp.floataa   )

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

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



     def 			lowercase__  (							self   )		->     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(__SCREAMING_SNAKE_CASE     ,				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(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   ):
             a__		=			(only_cross_attention,) * len(self.down_block_types   )

         if isinstance(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   ):
             a__		=			(num_attention_heads,) * len(self.down_block_types   )

         # down
         a__		=			[]
         a__		=			[]

         a__		=			block_out_channels[0]

         a__		=			nn.Conv(
             __SCREAMING_SNAKE_CASE     ,				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(__SCREAMING_SNAKE_CASE   )

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

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

             down_blocks.append(__SCREAMING_SNAKE_CASE   )

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

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

         a__		=			down_blocks
         a__		=			controlnet_down_blocks

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

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




     def __call__(							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE = 1.0     ,				__SCREAMING_SNAKE_CASE = True     ,				__SCREAMING_SNAKE_CASE = False     ,				)		->     Union[FlaxControlNetOutput, Tuple]:







         """simple docstring"""



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

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

         a__		=			self.time_proj(__SCREAMING_SNAKE_CASE   )
         a__		=			self.time_embedding(__SCREAMING_SNAKE_CASE   )

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

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

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

         # 4. mid
         a__		=			self.mid_block(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				deterministic=not train   )

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

         a__		=			controlnet_down_block_res_samples

         a__		=			self.controlnet_mid_block(__SCREAMING_SNAKE_CASE   )

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


