

"""simple docstring"""





import collections
import os
from typing import List, Optional, Tuple

from transformers.utils import is_jieba_available, requires_backends


if is_jieba_available():
   import jieba

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


_a							=							logging.get_logger(__name__)

_a							=							{'vocab_file': 'vocab.txt'}

_a							=							{
    'vocab_file': {
        'openbmb/cpm-ant-10b': 'https://huggingface.co/openbmb/cpm-ant-10b/blob/main/vocab.txt',
    },
}

_a							=							{
    'openbmb/cpm-ant-10b': 10_24,
}




def   _A  (  UpperCamelCase_					:	Tuple)     ->						str:
   '''simple docstring'''


   __lowercase								=						collections.OrderedDict()
   with open(UpperCamelCase_,      "r",      encoding="utf-8") as reader:
      __lowercase								=						reader.readlines()
   for index, token in enumerate(UpperCamelCase_):
      __lowercase								=						token.rstrip("\n")
      __lowercase								=						index
   return vocab




class  _lowerCAmelCase  (     lowercase       ):







 """simple docstring"""







 def __init__(      self							:      int, UpperCAmelCase__							:      List[str], UpperCAmelCase__							:      Union[str, Any]="<unk>", UpperCAmelCase__							:      Optional[Any]=2_0_0     ):
    __lowercase								=						vocab
    __lowercase								=						unk_token
    __lowercase								=						max_input_chars_per_word



 def  _lowercase			(      self							:      List[Any], UpperCAmelCase__							:      List[Any]     ):
    __lowercase								=						list(UpperCAmelCase__     )
    if len(UpperCAmelCase__     ) > self.max_input_chars_per_word:
       return [self.unk_token]

    __lowercase								=						0
    __lowercase								=						[]
    while start < len(UpperCAmelCase__     ):
       __lowercase								=						len(UpperCAmelCase__     )
       __lowercase								=						None
       while start < end:
          __lowercase								=						"".join(chars[start:end]     )
          if substr in self.vocab:
             __lowercase								=						substr
             break
          end -= 1
       if cur_substr is None:
          sub_tokens.append(self.unk_token     )
          start += 1
       else:
          sub_tokens.append(UpperCAmelCase__     )
          __lowercase								=						end

    return sub_tokens







class  _lowerCAmelCase  (     lowercase       ):







 """simple docstring"""







 __UpperCAmelCase     : Tuple       		=     VOCAB_FILES_NAMES
 __UpperCAmelCase     : List[Any]       		=     PRETRAINED_VOCAB_FILES_MAP
 __UpperCAmelCase     : Tuple       		=     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 __UpperCAmelCase     : Any       		=     ["input_ids", "attention_mask"]
 __UpperCAmelCase     : str       		=     False


 def __init__(      self							:      str, UpperCAmelCase__							:      int, UpperCAmelCase__							:      List[Any]="<d>", UpperCAmelCase__							:      str="</d>", UpperCAmelCase__							:      Optional[Any]="<s>", UpperCAmelCase__							:      List[str]="</s>", UpperCAmelCase__							:      Any="<pad>", UpperCAmelCase__							:      Tuple="<unk>", UpperCAmelCase__							:      int="</n>", UpperCAmelCase__							:      Union[str, Any]="</_>", UpperCAmelCase__							:      Optional[int]="left", **UpperCAmelCase__							:      Tuple, ):
    requires_backends(self, ["jieba"]     )
    super().__init__(
        bod_token=UpperCAmelCase__, eod_token=UpperCAmelCase__, bos_token=UpperCAmelCase__, eos_token=UpperCAmelCase__, pad_token=UpperCAmelCase__, unk_token=UpperCAmelCase__, line_token=UpperCAmelCase__, space_token=UpperCAmelCase__, padding_side=UpperCAmelCase__, **UpperCAmelCase__, )
    __lowercase								=						bod_token
    __lowercase								=						eod_token
    __lowercase								=						load_vocab(UpperCAmelCase__     )
    __lowercase								=						self.encoder[space_token]
    __lowercase								=						self.encoder[line_token]

    del self.encoder[space_token]
    del self.encoder[line_token]

    __lowercase								=						collections.OrderedDict(sorted(self.encoder.items(), key=lambda UpperCAmelCase__     : x[1]     )     )
    __lowercase								=						{v: k for k, v in self.encoder.items()}

    __lowercase								=						WordpieceTokenizer(vocab=self.encoder, unk_token=self.unk_token     )


 @property
 def  _lowercase			(      self							:      Dict     ):
    return self.encoder[self.bod_token]


 @property
 def  _lowercase			(      self							:      Any     ):
    return self.encoder[self.eod_token]


 @property
 def  _lowercase			(      self							:      Any     ):
    return self.encoder["\n"]


 @property
 def  _lowercase			(      self							:      str     ):
    return len(self.encoder     )


 def  _lowercase			(      self							:      Tuple     ):
    return dict(self.encoder, **self.added_tokens_encoder     )


 def  _lowercase			(      self							:      Tuple, UpperCAmelCase__							:      Optional[Any]     ):
    __lowercase								=						[]
    for x in jieba.cut(UpperCAmelCase__, cut_all=UpperCAmelCase__     ):
       output_tokens.extend(self.wordpiece_tokenizer.tokenize(UpperCAmelCase__     )     )
    return output_tokens


 def  _lowercase			(      self							:      Optional[int], UpperCAmelCase__							:      Optional[int], **UpperCAmelCase__							:      List[str]     ):
    __lowercase								=						[i for i in token_ids if i >= 0]
    __lowercase								=						[
        x for x in token_ids if x != self.pad_token_id and x != self.eos_token_id and x != self.bos_token_id
    ]
    return super()._decode(UpperCAmelCase__, **UpperCAmelCase__     )


 def  _lowercase			(      self							:      List[Any], UpperCAmelCase__							:      int     ):
    return token in self.encoder


 def  _lowercase			(      self							:      Optional[int], UpperCAmelCase__							:      List[str]     ):
    return "".join(UpperCAmelCase__     )


 def  _lowercase			(      self							:      int, UpperCAmelCase__							:      Union[str, Any]     ):
    return self.encoder.get(UpperCAmelCase__, self.encoder.get(self.unk_token     )     )


 def  _lowercase			(      self							:      Optional[int], UpperCAmelCase__							:      List[str]     ):
    return self.decoder.get(UpperCAmelCase__, self.unk_token     )


 def  _lowercase			(      self							:      Optional[int], UpperCAmelCase__							:      str, UpperCAmelCase__							:      Optional[str] = None     ):
    if os.path.isdir(UpperCAmelCase__     ):
       __lowercase								=						os.path.join(
           UpperCAmelCase__, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]     )
    else:
       __lowercase								=						(filename_prefix + "-" if filename_prefix else "") + save_directory
    __lowercase								=						0
    if " " in self.encoder:
       __lowercase								=						self.encoder[" "]
       del self.encoder[" "]
    if "\n" in self.encoder:
       __lowercase								=						self.encoder["\n"]
       del self.encoder["\n"]
    __lowercase								=						collections.OrderedDict(sorted(self.encoder.items(), key=lambda UpperCAmelCase__     : x[1]     )     )
    with open(UpperCAmelCase__, "w", encoding="utf-8"     ) as writer:
       for token, token_index in self.encoder.items():
          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!"     )
             __lowercase								=						token_index
          writer.write(token + "\n"     )
          index += 1
    return (vocab_file,)


 def  _lowercase			(      self							:      Any, UpperCAmelCase__							:      List[int], UpperCAmelCase__							:      List[int] = None     ):
    if token_ids_a is None:
       return [self.bos_token_id] + token_ids_a
    return [self.bos_token_id] + token_ids_a + [self.bos_token_id] + token_ids_a



 def  _lowercase			(      self							:      Union[str, Any], UpperCAmelCase__							:      List[int], UpperCAmelCase__							:      Optional[List[int]] = None, UpperCAmelCase__							:      bool = False     ):

    if already_has_special_tokens:
       return super().get_special_tokens_mask(
           token_ids_a=UpperCAmelCase__, token_ids_a=UpperCAmelCase__, already_has_special_tokens=UpperCAmelCase__     )

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







"""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  _lowerCAmelCase  (     lowercase       ):







 """simple docstring"""







 __UpperCAmelCase     : jnp.ndarray
 __UpperCAmelCase     : jnp.ndarray







class  _lowerCAmelCase  (     nn.Module       ):







 """simple docstring"""







 __UpperCAmelCase     : int
 __UpperCAmelCase     : Tuple[int]       		=     (1_6, 3_2, 9_6, 2_5_6)
 __UpperCAmelCase     : jnp.dtype       		=     jnp.floataa


 def  _lowercase			(      self							:      Union[str, Any]     ):
    __lowercase								=						nn.Conv(
        self.block_out_channels[0], kernel_size=(3, 3), padding=((1, 1), (1, 1)), dtype=self.dtype, )

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

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

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

    __lowercase								=						self.conv_out(UpperCAmelCase__     )

    return embedding







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







 """simple docstring"""







 __UpperCAmelCase     : int       		=     3_2
 __UpperCAmelCase     : int       		=     4
 __UpperCAmelCase     : Tuple[str]       		=     (
     "CrossAttnDownBlock2D",
     "CrossAttnDownBlock2D",
     "CrossAttnDownBlock2D",
     "DownBlock2D",
 )
 __UpperCAmelCase     : Union[bool, Tuple[bool]]       		=     False
 __UpperCAmelCase     : Tuple[int]       		=     (3_2_0, 6_4_0, 1_2_8_0, 1_2_8_0)
 __UpperCAmelCase     : int       		=     2
 __UpperCAmelCase     : Union[int, Tuple[int]]       		=     8
 __UpperCAmelCase     : Optional[Union[int, Tuple[int]]]       		=     None
 __UpperCAmelCase     : int       		=     1_2_8_0
 __UpperCAmelCase     : float       		=     0.0
 __UpperCAmelCase     : bool       		=     False
 __UpperCAmelCase     : jnp.dtype       		=     jnp.floataa
 __UpperCAmelCase     : bool       		=     True
 __UpperCAmelCase     : int       		=     0
 __UpperCAmelCase     : str       		=     "rgb"
 __UpperCAmelCase     : Tuple[int]       		=     (1_6, 3_2, 9_6, 2_5_6)


 def  _lowercase			(      self							:      List[Any], UpperCAmelCase__							:      jax.random.KeyArray     ):
    # init input tensors
    __lowercase								=						(1, self.in_channels, self.sample_size, self.sample_size)
    __lowercase								=						jnp.zeros(UpperCAmelCase__, dtype=jnp.floataa     )
    __lowercase								=						jnp.ones((1,), dtype=jnp.intaa     )
    __lowercase								=						jnp.zeros((1, 1, self.cross_attention_dim), dtype=jnp.floataa     )
    __lowercase								=						(1, 3, self.sample_size * 8, self.sample_size * 8)
    __lowercase								=						jnp.zeros(UpperCAmelCase__, dtype=jnp.floataa     )

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

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


 def  _lowercase			(      self							:      Union[str, Any]     ):
    __lowercase								=						self.block_out_channels
    __lowercase								=						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.
    __lowercase								=						self.num_attention_heads or self.attention_head_dim

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

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

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

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

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

    # down
    __lowercase								=						[]
    __lowercase								=						[]

    __lowercase								=						block_out_channels[0]

    __lowercase								=						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     ):
       __lowercase								=						output_channel
       __lowercase								=						block_out_channels[i]
       __lowercase								=						i == len(UpperCAmelCase__     ) - 1

       if down_block_type == "CrossAttnDownBlock2D":
          __lowercase								=						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:
          __lowercase								=						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     ):
          __lowercase								=						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:
          __lowercase								=						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__     )

    __lowercase								=						down_blocks
    __lowercase								=						controlnet_down_blocks

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

    __lowercase								=						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							:      str, UpperCAmelCase__							:      Union[str, Any], UpperCAmelCase__							:      List[Any], UpperCAmelCase__							:      Tuple, UpperCAmelCase__							:      List[str], UpperCAmelCase__							:      float = 1.0, UpperCAmelCase__							:      bool = True, UpperCAmelCase__							:      bool = False, ):
    __lowercase								=						self.controlnet_conditioning_channel_order
    if channel_order == "bgr":
       __lowercase								=						jnp.flip(UpperCAmelCase__, axis=1     )

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

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

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

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

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

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

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

    __lowercase								=						controlnet_down_block_res_samples

    __lowercase								=						self.controlnet_mid_block(UpperCAmelCase__     )

    # 6. scaling
    __lowercase								=						[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__     )




