


'''simple docstring'''






import os
import unicodedata
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import SPIECE_UNDERLINE, logging


lowercase							=logging.get_logger(__name__)

lowercase							={'vocab_file': 'spiece.model'}

lowercase							={
    'vocab_file': {
        'TsinghuaAI/CPM-Generate': 'https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/spiece.model',
    }
}






class  __magic_name__							(		lowerCAmelCase    ):






       def __init__(      self  ,							snake_case  ,							snake_case=False  ,							snake_case=True  ,							snake_case=False  ,							snake_case="<s>"  ,							snake_case="</s>"  ,							snake_case="<unk>"  ,							snake_case="<sep>"  ,							snake_case="<pad>"  ,							snake_case="<cls>"  ,							snake_case="<mask>"  ,							snake_case=["<eop>", "<eod>"]  ,							snake_case = None  ,							**snake_case  ,							)						->  None:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[int]							   =AddedToken(snake_case  ,							lstrip=snake_case  ,							rstrip=snake_case) if isinstance(snake_case  ,							snake_case) else mask_token

          _UpperCAmelCase       :       List[Any]							   ={} if sp_model_kwargs is None else sp_model_kwargs

          super().__init__(
              do_lower_case=snake_case  ,							remove_space=snake_case  ,							keep_accents=snake_case  ,							bos_token=snake_case  ,							eos_token=snake_case  ,							unk_token=snake_case  ,							sep_token=snake_case  ,							pad_token=snake_case  ,							cls_token=snake_case  ,							mask_token=snake_case  ,							additional_special_tokens=snake_case  ,							sp_model_kwargs=self.sp_model_kwargs  ,							**snake_case  ,							)

          _UpperCAmelCase       :       int							   =3

          _UpperCAmelCase       :       List[str]							   =do_lower_case
          _UpperCAmelCase       :       Union[str, Any]							   =remove_space
          _UpperCAmelCase       :       Optional[int]							   =keep_accents
          _UpperCAmelCase       :       Tuple							   =vocab_file

          _UpperCAmelCase       :       Dict							   =spm.SentencePieceProcessor(**self.sp_model_kwargs)
          self.sp_model.Load(snake_case)

          try:
             import jieba
          except ModuleNotFoundError as error:
             raise error.__class__(
                 'You need to install jieba to use CpmTokenizer or CpmTokenizerFast. '
                 'See https://pypi.org/project/jieba/ for installation.')
          _UpperCAmelCase       :       List[str]							   =jieba
          _UpperCAmelCase       :       Any							   =str.maketrans(' \n'  ,							'\u2582\u2583')






       @property
       # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.vocab_size
       def       lowerCAmelCase		(      self)						->  int:




          '''simple docstring'''
          return len(self.sp_model)






       def       lowerCAmelCase		(      self)						->  int:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[int]							   ={self.convert_ids_to_tokens(snake_case): i for i in range(self.vocab_size)}
          vocab.update(self.added_tokens_encoder)
          return vocab






       def __getstate__(      self)						->  int:




          '''simple docstring'''
          _UpperCAmelCase       :       Union[str, Any]							   =self.__dict__.copy()
          _UpperCAmelCase       :       Union[str, Any]							   =None
          return state






       def __setstate__(      self  ,							snake_case)						->  Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase       :       int							   =d

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

          _UpperCAmelCase       :       Any							   =spm.SentencePieceProcessor(**self.sp_model_kwargs)
          self.sp_model.Load(self.vocab_file)






       def       lowerCAmelCase		(      self  ,							snake_case)						->  List[str]:




          '''simple docstring'''
          if self.remove_space:
             _UpperCAmelCase       :       Dict							   =' '.join(inputs.strip().split())
          else:
             _UpperCAmelCase       :       int							   =inputs
          _UpperCAmelCase       :       Union[str, Any]							   =outputs.replace('``'  ,							'"').replace('\'\''  ,							'"')

          if not self.keep_accents:
             _UpperCAmelCase       :       Tuple							   =unicodedata.normalize('NFKD'  ,							snake_case)
             _UpperCAmelCase       :       Any							   =''.join([c for c in outputs if not unicodedata.combining(snake_case)])
          if self.do_lower_case:
             _UpperCAmelCase       :       str							   =outputs.lower()

          return outputs






       def       lowerCAmelCase		(      self  ,							snake_case)						->  List[str]:




          '''simple docstring'''
          _UpperCAmelCase       :       Union[str, Any]							   =self.preprocess_text(snake_case)
          _UpperCAmelCase       :       Optional[int]							   =self.sp_model.encode(snake_case  ,							out_type=snake_case)
          _UpperCAmelCase       :       Optional[int]							   =[]
          for piece in pieces:
             if len(snake_case) > 1 and piece[-1] == str(',') and piece[-2].isdigit():
                _UpperCAmelCase       :       Dict							   =self.sp_model.EncodeAsPieces(piece[:-1].replace(snake_case  ,							''))
                if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
                   if len(cur_pieces[0]) == 1:
                      _UpperCAmelCase       :       Union[str, Any]							   =cur_pieces[1:]
                   else:
                      _UpperCAmelCase       :       Optional[int]							   =cur_pieces[0][1:]
                cur_pieces.append(piece[-1])
                new_pieces.extend(snake_case)
             else:
                new_pieces.append(snake_case)

          return new_pieces






       def       lowerCAmelCase		(      self  ,							snake_case)						->  List[Any]:




          '''simple docstring'''
          return self.sp_model.PieceToId(snake_case)






       def       lowerCAmelCase		(      self  ,							snake_case)						->  Union[str, Any]:




          '''simple docstring'''
          return self.sp_model.IdToPiece(snake_case)






       def       lowerCAmelCase		(      self  ,							snake_case)						->  Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[Any]							   =''.join(snake_case).replace(snake_case  ,							' ').strip()
          return out_string






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case = None)						->  List[int]:




          '''simple docstring'''
          _UpperCAmelCase       :       Tuple							   =[self.sep_token_id]
          _UpperCAmelCase       :       Tuple							   =[self.cls_token_id]
          if token_ids_a is None:
             return token_ids_a + sep + cls
          return token_ids_a + sep + token_ids_a + sep + cls






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case = None  ,							snake_case = False)						->  List[int]:




          '''simple docstring'''

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

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






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case = None)						->  List[int]:




          '''simple docstring'''
          _UpperCAmelCase       :       Any							   =[self.sep_token_id]
          _UpperCAmelCase       :       Dict							   =[2]

          if token_ids_a is None:
             return len(token_ids_a + sep) * [0] + cls_segment_id
          return len(token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1] + cls_segment_id






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case = None)						->  Tuple[str]:




          '''simple docstring'''
          if not os.path.isdir(snake_case):
             logger.error(f"Vocabulary path ({save_directory}) should be a directory")
             return
          _UpperCAmelCase       :       Optional[Any]							   =os.path.join(
              snake_case  ,							(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'])

          if os.path.abspath(self.vocab_file) != os.path.abspath(snake_case) and os.path.isfile(self.vocab_file):
             copyfile(self.vocab_file  ,							snake_case)
          elif not os.path.isfile(self.vocab_file):
             with open(snake_case  ,							'wb') as fi:
                _UpperCAmelCase       :       str							   =self.sp_model.serialized_model_proto()
                fi.write(snake_case)

          return (out_vocab_file,)






       def       lowerCAmelCase		(      self  ,							*snake_case  ,							**snake_case)						->  List[str]:




          '''simple docstring'''
          _UpperCAmelCase       :       Any							   =super()._decode(*snake_case  ,							**snake_case)
          _UpperCAmelCase       :       Optional[int]							   =text.replace(' '  ,							'').replace('\u2582'  ,							' ').replace('\u2583'  ,							'\n')
          return text





'''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  __magic_name__							(		lowerCAmelCase    ):
       UpperCAmelCase										=42
       UpperCAmelCase										=42






class  __magic_name__							(		nn.Module    ):
       UpperCAmelCase										=42
       UpperCAmelCase										=(1_6, 3_2, 9_6, 2_5_6)
       UpperCAmelCase										=jnp.floataa






       def       lowerCAmelCase		(      self)						->  Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase       :       str							   =nn.Conv(
              self.block_out_channels[0]  ,							kernel_size=(3, 3)  ,							padding=((1, 1), (1, 1))  ,							dtype=self.dtype  ,							)

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

          _UpperCAmelCase       :       Tuple							   =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  ,							snake_case)						->  List[str]:




          '''simple docstring'''
          _UpperCAmelCase       :       int							   =self.conv_in(snake_case)
          _UpperCAmelCase       :       Any							   =nn.silu(snake_case)

          for block in self.blocks:
             _UpperCAmelCase       :       Optional[Any]							   =block(snake_case)
             _UpperCAmelCase       :       Union[str, Any]							   =nn.silu(snake_case)

          _UpperCAmelCase       :       str							   =self.conv_out(snake_case)

          return embedding






@flax_register_to_config
class  __magic_name__							(		nn.Module ,lowerCAmelCase ,lowerCAmelCase    ):
       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       lowerCAmelCase		(      self  ,							snake_case)						->  FrozenDict:




          '''simple docstring'''
          # init input tensors
          _UpperCAmelCase       :       Any							   =(1, self.in_channels, self.sample_size, self.sample_size)
          _UpperCAmelCase       :       Optional[Any]							   =jnp.zeros(snake_case  ,							dtype=jnp.floataa)
          _UpperCAmelCase       :       Optional[int]							   =jnp.ones((1,)  ,							dtype=jnp.intaa)
          _UpperCAmelCase       :       str							   =jnp.zeros((1, 1, self.cross_attention_dim)  ,							dtype=jnp.floataa)
          _UpperCAmelCase       :       Optional[Any]							   =(1, 3, self.sample_size * 8, self.sample_size * 8)
          _UpperCAmelCase       :       int							   =jnp.zeros(snake_case  ,							dtype=jnp.floataa)

          _UpperCAmelCase   ,	_UpperCAmelCase       :       List[Any]							   =jax.random.split(snake_case)
          _UpperCAmelCase       :       str							   ={'params': params_rng, 'dropout': dropout_rng}

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






       def       lowerCAmelCase		(      self)						->  Tuple:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[Any]							   =self.block_out_channels
          _UpperCAmelCase       :       Tuple							   =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[Any]							   =self.num_attention_heads or self.attention_head_dim

          # input
          _UpperCAmelCase       :       Tuple							   =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       :       str							   =FlaxTimestepEmbedding(snake_case  ,							dtype=self.dtype)

          _UpperCAmelCase       :       Optional[Any]							   =FlaxControlNetConditioningEmbedding(
              conditioning_embedding_channels=block_out_channels[0]  ,							block_out_channels=self.conditioning_embedding_out_channels  ,							)

          _UpperCAmelCase       :       Optional[int]							   =self.only_cross_attention
          if isinstance(snake_case  ,							snake_case):
             _UpperCAmelCase       :       Dict							   =(only_cross_attention,) * len(self.down_block_types)

          if isinstance(snake_case  ,							snake_case):
             _UpperCAmelCase       :       Optional[Any]							   =(num_attention_heads,) * len(self.down_block_types)

          # down
          _UpperCAmelCase       :       int							   =[]
          _UpperCAmelCase       :       Optional[int]							   =[]

          _UpperCAmelCase       :       List[str]							   =block_out_channels[0]

          _UpperCAmelCase       :       int							   =nn.Conv(
              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(snake_case)

          for i, down_block_type in enumerate(self.down_block_types):
             _UpperCAmelCase       :       Tuple							   =output_channel
             _UpperCAmelCase       :       Dict							   =block_out_channels[i]
             _UpperCAmelCase       :       str							   =i == len(snake_case) - 1

             if down_block_type == "CrossAttnDownBlock2D":
                _UpperCAmelCase       :       Tuple							   =FlaxCrossAttnDownBlockaD(
                    in_channels=snake_case  ,							out_channels=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:
                _UpperCAmelCase       :       Optional[Any]							   =FlaxDownBlockaD(
                    in_channels=snake_case  ,							out_channels=snake_case  ,							dropout=self.dropout  ,							num_layers=self.layers_per_block  ,							add_downsample=not is_final_block  ,							dtype=self.dtype  ,							)

             down_blocks.append(snake_case)

             for _ in range(self.layers_per_block):
                _UpperCAmelCase       :       Tuple							   =nn.Conv(
                    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(snake_case)

             if not is_final_block:
                _UpperCAmelCase       :       List[str]							   =nn.Conv(
                    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(snake_case)

          _UpperCAmelCase       :       List[Any]							   =down_blocks
          _UpperCAmelCase       :       Optional[Any]							   =controlnet_down_blocks

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

          _UpperCAmelCase       :       Optional[int]							   =nn.Conv(
              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  ,							snake_case  ,							snake_case  ,							snake_case  ,							snake_case  ,							snake_case = 1.0  ,							snake_case = True  ,							snake_case = False  ,							)						->  Union[FlaxControlNetOutput, Tuple]:




          '''simple docstring'''
          _UpperCAmelCase       :       Union[str, Any]							   =self.controlnet_conditioning_channel_order
          if channel_order == "bgr":
             _UpperCAmelCase       :       Optional[int]							   =jnp.flip(snake_case  ,							axis=1)

          # 1. time
          if not isinstance(snake_case  ,							jnp.ndarray):
             _UpperCAmelCase       :       Optional[int]							   =jnp.array([timesteps]  ,							dtype=jnp.intaa)
          elif isinstance(snake_case  ,							jnp.ndarray) and len(timesteps.shape) == 0:
             _UpperCAmelCase       :       str							   =timesteps.astype(dtype=jnp.floataa)
             _UpperCAmelCase       :       Dict							   =jnp.expand_dims(snake_case  ,							0)

          _UpperCAmelCase       :       int							   =self.time_proj(snake_case)
          _UpperCAmelCase       :       Any							   =self.time_embedding(snake_case)

          # 2. pre-process
          _UpperCAmelCase       :       str							   =jnp.transpose(snake_case  ,							(0, 2, 3, 1))
          _UpperCAmelCase       :       Any							   =self.conv_in(snake_case)

          _UpperCAmelCase       :       List[str]							   =jnp.transpose(snake_case  ,							(0, 2, 3, 1))
          _UpperCAmelCase       :       Optional[int]							   =self.controlnet_cond_embedding(snake_case)
          sample += controlnet_cond

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

          # 4. mid
          _UpperCAmelCase       :       List[Any]							   =self.mid_block(snake_case  ,							snake_case  ,							snake_case  ,							deterministic=not train)

          # 5. contronet blocks
          _UpperCAmelCase       :       Union[str, Any]							   =()
          for down_block_res_sample, controlnet_block in zip(snake_case  ,							self.controlnet_down_blocks):
             _UpperCAmelCase       :       List[str]							   =controlnet_block(snake_case)
             controlnet_down_block_res_samples += (down_block_res_sample,)

          _UpperCAmelCase       :       Optional[int]							   =controlnet_down_block_res_samples

          _UpperCAmelCase       :       List[str]							   =self.controlnet_mid_block(snake_case)

          # 6. scaling
          _UpperCAmelCase       :       Tuple							   =[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=snake_case  ,							mid_block_res_sample=snake_case)

