


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,
    FlaxCrossAttnUpBlockaD,
    FlaxDownBlockaD,
    FlaxUNetMidBlockaDCrossAttn,
    FlaxUpBlockaD,
)






@flax.struct.dataclass
class    UpperCAmelCase__	( A_		):



    """simple docstring"""






    UpperCAmelCase__						:		jnp.ndarray






@flax_register_to_config
class    UpperCAmelCase__	( nn.Module       ,  A_       ,  A_		):



    """simple docstring"""






    UpperCAmelCase__						:		int              =  3_2
    UpperCAmelCase__						:		int              =  4
    UpperCAmelCase__						:		int              =  4
    UpperCAmelCase__						:		Tuple[str]              =  (
        "CrossAttnDownBlock2D",
        "CrossAttnDownBlock2D",
        "CrossAttnDownBlock2D",
        "DownBlock2D",
    )
    UpperCAmelCase__						:		Tuple[str]              =  ("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D")
    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__						:		bool              =  False






    def _a (      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							,				__UpperCamelCase   							=jax.random.split(A_   )
         __UpperCamelCase   							={'params': params_rng, 'dropout': dropout_rng}

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






    def _a (      self   )  ->	int:
         __UpperCamelCase   							=self.block_out_channels
         __UpperCamelCase   							=block_out_channels[0] * 4

         if self.num_attention_heads is not None:
              raise ValueError(
                  'At the moment it is not possible to define the number of attention heads via `num_attention_heads` because of a naming issue as described in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131. Passing `num_attention_heads` will only be supported in diffusers v0.19.'   )

         # 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   							=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   							=block_out_channels[0]
         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] ,       use_memory_efficient_attention=self.use_memory_efficient_attention ,       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_   )
         __UpperCamelCase   							=down_blocks

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

         # up
         __UpperCamelCase   							=[]
         __UpperCamelCase   							=list(reversed(A_   )   )
         __UpperCamelCase   							=list(reversed(A_   )   )
         __UpperCamelCase   							=list(reversed(A_   )   )
         __UpperCamelCase   							=reversed_block_out_channels[0]
         for i, up_block_type in enumerate(self.up_block_types   ):
              __UpperCamelCase   							=output_channel
              __UpperCamelCase   							=reversed_block_out_channels[i]
              __UpperCamelCase   							=reversed_block_out_channels[min(i + 1 ,       len(A_   ) - 1   )]

              __UpperCamelCase   							=i == len(A_   ) - 1

              if up_block_type == "CrossAttnUpBlock2D":
                   __UpperCamelCase   							=FlaxCrossAttnUpBlockaD(
                       in_channels=A_ ,       out_channels=A_ ,       prev_output_channel=A_ ,       num_layers=self.layers_per_block + 1 ,       num_attention_heads=reversed_num_attention_heads[i] ,       add_upsample=not is_final_block ,       dropout=self.dropout ,       use_linear_projection=self.use_linear_projection ,       only_cross_attention=only_cross_attention[i] ,       use_memory_efficient_attention=self.use_memory_efficient_attention ,       dtype=self.dtype ,       )
              else:
                   __UpperCamelCase   							=FlaxUpBlockaD(
                       in_channels=A_ ,       out_channels=A_ ,       prev_output_channel=A_ ,       num_layers=self.layers_per_block + 1 ,       add_upsample=not is_final_block ,       dropout=self.dropout ,       dtype=self.dtype ,       )

              up_blocks.append(A_   )
              __UpperCamelCase   							=output_channel
         __UpperCamelCase   							=up_blocks

         # out
         __UpperCamelCase   							=nn.GroupNorm(num_groups=32 ,       epsilon=1E-5   )
         __UpperCamelCase   							=nn.Conv(
             self.out_channels ,       kernel_size=(3, 3) ,       strides=(1, 1) ,       padding=((1, 1), (1, 1)) ,       dtype=self.dtype ,       )






    def __call__(      self ,       A_ ,       A_ ,       A_ ,       A_=None ,       A_=None ,       A_ = True ,       A_ = False ,       )  ->	Union[FlaxUNetaDConditionOutput, Tuple]:
         # 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_   )

         # 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

         if down_block_additional_residuals is not None:
              __UpperCamelCase   							=()

              for down_block_res_sample, down_block_additional_residual in zip(
                  A_ ,       A_   ):
                   down_block_res_sample += down_block_additional_residual
                   new_down_block_res_samples += (down_block_res_sample,)

              __UpperCamelCase   							=new_down_block_res_samples

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

         if mid_block_additional_residual is not None:
              sample += mid_block_additional_residual

         # 5. up
         for up_block in self.up_blocks:
              __UpperCamelCase   							=down_block_res_samples[-(self.layers_per_block + 1) :]
              __UpperCamelCase   							=down_block_res_samples[: -(self.layers_per_block + 1)]
              if isinstance(A_ ,       A_   ):
                   __UpperCamelCase   							=up_block(
                       A_ ,       temb=A_ ,       encoder_hidden_states=A_ ,       res_hidden_states_tuple=A_ ,       deterministic=not train ,       )
              else:
                   __UpperCamelCase   							=up_block(A_ ,       temb=A_ ,       res_hidden_states_tuple=A_ ,       deterministic=not train   )

        # 6. post-process
         __UpperCamelCase   							=self.conv_norm_out(A_   )
         __UpperCamelCase   							=nn.silu(A_   )
         __UpperCamelCase   							=self.conv_out(A_   )
         __UpperCamelCase   							=jnp.transpose(A_ ,       (0, 3, 1, 2)   )

         if not return_dict:
              return (sample,)

         return FlaxUNetaDConditionOutput(sample=A_   )









import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_bert import BertTokenizer


_A			=						logging.get_logger(__name__)

_A			=						{'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

_A			=						{
    'vocab_file': {
        'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/vocab.txt',
        'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/vocab.txt',
        'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/vocab.txt',
        'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/vocab.txt',
        'bert-base-multilingual-uncased': (
            'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/vocab.txt'
        ),
        'bert-base-multilingual-cased': 'https://huggingface.co/bert-base-multilingual-cased/resolve/main/vocab.txt',
        'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/vocab.txt',
        'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/vocab.txt',
        'bert-large-uncased-whole-word-masking': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/vocab.txt'
        ),
        'bert-large-cased-whole-word-masking': (
            'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/vocab.txt'
        ),
        'bert-large-uncased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt'
        ),
        'bert-large-cased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt'
        ),
        'bert-base-cased-finetuned-mrpc': (
            'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/vocab.txt'
        ),
        'bert-base-german-dbmdz-cased': 'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/vocab.txt',
        'bert-base-german-dbmdz-uncased': (
            'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/vocab.txt'
        ),
        'TurkuNLP/bert-base-finnish-cased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/vocab.txt'
        ),
        'TurkuNLP/bert-base-finnish-uncased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/vocab.txt'
        ),
        'wietsedv/bert-base-dutch-cased': (
            'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/vocab.txt'
        ),
    },
    'tokenizer_file': {
        'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/tokenizer.json',
        'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/tokenizer.json',
        'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/tokenizer.json',
        'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/tokenizer.json',
        'bert-base-multilingual-uncased': (
            'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/tokenizer.json'
        ),
        'bert-base-multilingual-cased': (
            'https://huggingface.co/bert-base-multilingual-cased/resolve/main/tokenizer.json'
        ),
        'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/tokenizer.json',
        'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/tokenizer.json',
        'bert-large-uncased-whole-word-masking': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/tokenizer.json'
        ),
        'bert-large-cased-whole-word-masking': (
            'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/tokenizer.json'
        ),
        'bert-large-uncased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json'
        ),
        'bert-large-cased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json'
        ),
        'bert-base-cased-finetuned-mrpc': (
            'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/tokenizer.json'
        ),
        'bert-base-german-dbmdz-cased': (
            'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/tokenizer.json'
        ),
        'bert-base-german-dbmdz-uncased': (
            'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/tokenizer.json'
        ),
        'TurkuNLP/bert-base-finnish-cased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/tokenizer.json'
        ),
        'TurkuNLP/bert-base-finnish-uncased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/tokenizer.json'
        ),
        'wietsedv/bert-base-dutch-cased': (
            'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/tokenizer.json'
        ),
    },
}

_A			=						{
    'bert-base-uncased': 512,
    'bert-large-uncased': 512,
    'bert-base-cased': 512,
    'bert-large-cased': 512,
    'bert-base-multilingual-uncased': 512,
    'bert-base-multilingual-cased': 512,
    'bert-base-chinese': 512,
    'bert-base-german-cased': 512,
    'bert-large-uncased-whole-word-masking': 512,
    'bert-large-cased-whole-word-masking': 512,
    'bert-large-uncased-whole-word-masking-finetuned-squad': 512,
    'bert-large-cased-whole-word-masking-finetuned-squad': 512,
    'bert-base-cased-finetuned-mrpc': 512,
    'bert-base-german-dbmdz-cased': 512,
    'bert-base-german-dbmdz-uncased': 512,
    'TurkuNLP/bert-base-finnish-cased-v1': 512,
    'TurkuNLP/bert-base-finnish-uncased-v1': 512,
    'wietsedv/bert-base-dutch-cased': 512,
}

_A			=						{
    'bert-base-uncased': {'do_lower_case': True},
    'bert-large-uncased': {'do_lower_case': True},
    'bert-base-cased': {'do_lower_case': False},
    'bert-large-cased': {'do_lower_case': False},
    'bert-base-multilingual-uncased': {'do_lower_case': True},
    'bert-base-multilingual-cased': {'do_lower_case': False},
    'bert-base-chinese': {'do_lower_case': False},
    'bert-base-german-cased': {'do_lower_case': False},
    'bert-large-uncased-whole-word-masking': {'do_lower_case': True},
    'bert-large-cased-whole-word-masking': {'do_lower_case': False},
    'bert-large-uncased-whole-word-masking-finetuned-squad': {'do_lower_case': True},
    'bert-large-cased-whole-word-masking-finetuned-squad': {'do_lower_case': False},
    'bert-base-cased-finetuned-mrpc': {'do_lower_case': False},
    'bert-base-german-dbmdz-cased': {'do_lower_case': False},
    'bert-base-german-dbmdz-uncased': {'do_lower_case': True},
    'TurkuNLP/bert-base-finnish-cased-v1': {'do_lower_case': False},
    'TurkuNLP/bert-base-finnish-uncased-v1': {'do_lower_case': True},
    'wietsedv/bert-base-dutch-cased': {'do_lower_case': False},
}






class    UpperCAmelCase__	( A_		):



    """simple docstring"""






    UpperCAmelCase__						:		Any              =  VOCAB_FILES_NAMES
    UpperCAmelCase__						:		List[str]              =  PRETRAINED_VOCAB_FILES_MAP
    UpperCAmelCase__						:		Optional[int]              =  PRETRAINED_INIT_CONFIGURATION
    UpperCAmelCase__						:		List[str]              =  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    UpperCAmelCase__						:		Dict              =  BertTokenizer






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

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

         __UpperCamelCase   							=do_lower_case






    def _a (      self ,       A_ ,       A_=None   )  ->	List[str]:
         __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 ,       A_ ,       A_ = None   )  ->	List[int]:
         __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 ,       A_ ,       A_ = None   )  ->	Tuple[str]:
         __UpperCamelCase   							=self._tokenizer.model.save(A_ ,       name=A_   )
         return tuple(A_   )





