




'''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 			_UpperCamelCase      (				lowerCAmelCase		):
      UpperCAmelCase_        =		42
      UpperCAmelCase_        =		42



class 			_UpperCamelCase      (				nn.Module		):
      UpperCAmelCase_        =		42
      UpperCAmelCase_        =		(16, 32, 96, 2_56)
      UpperCAmelCase_        =		jnp.floataa
      def 	UpperCAmelCase_						(			self   :Any      )     ->			Dict:
       UpperCAmelCase__							 =						nn.Conv(
           self.block_out_channels[0] ,      kernel_size=(3, 3) ,      padding=((1, 1), (1, 1)) ,      dtype=self.dtype ,      )

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

       UpperCAmelCase__							 =						nn.Conv(
           self.conditioning_embedding_channels ,      kernel_size=(3, 3) ,      padding=((1, 1), (1, 1)) ,      kernel_init=nn.initializers.zeros_init() ,      bias_init=nn.initializers.zeros_init() ,      dtype=self.dtype ,      )







      def __call__(			self   :str ,      lowerCamelCase   :Union[str, Any]      )     ->			Optional[int]:
       UpperCAmelCase__							 =						self.conv_in(lowerCamelCase      )
       UpperCAmelCase__							 =						nn.silu(lowerCamelCase      )

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

       UpperCAmelCase__							 =						self.conv_out(lowerCamelCase      )

       return embedding



@flax_register_to_config
class 			_UpperCamelCase      (				nn.Module	,       lowerCAmelCase	,       lowerCAmelCase		):
      UpperCAmelCase_        =		32
      UpperCAmelCase_        =		4
      UpperCAmelCase_        =		(
          """CrossAttnDownBlock2D""",
          """CrossAttnDownBlock2D""",
          """CrossAttnDownBlock2D""",
          """DownBlock2D""",
      )
      UpperCAmelCase_        =		False
      UpperCAmelCase_        =		(3_20, 6_40, 12_80, 12_80)
      UpperCAmelCase_        =		2
      UpperCAmelCase_        =		8
      UpperCAmelCase_        =		None
      UpperCAmelCase_        =		12_80
      UpperCAmelCase_        =		0.0
      UpperCAmelCase_        =		False
      UpperCAmelCase_        =		jnp.floataa
      UpperCAmelCase_        =		True
      UpperCAmelCase_        =		0
      UpperCAmelCase_        =		"""rgb"""
      UpperCAmelCase_        =		(16, 32, 96, 2_56)
      def 	UpperCAmelCase_						(			self   :str ,      lowerCamelCase   :jax.random.KeyArray      )     ->			FrozenDict:
       # init input tensors
       UpperCAmelCase__							 =						(1, self.in_channels, self.sample_size, self.sample_size)
       UpperCAmelCase__							 =						jnp.zeros(lowerCamelCase ,      dtype=jnp.floataa      )
       UpperCAmelCase__							 =						jnp.ones((1,) ,      dtype=jnp.intaa      )
       UpperCAmelCase__							 =						jnp.zeros((1, 1, self.cross_attention_dim) ,      dtype=jnp.floataa      )
       UpperCAmelCase__							 =						(1, 3, self.sample_size * 8, self.sample_size * 8)
       UpperCAmelCase__							 =						jnp.zeros(lowerCamelCase ,      dtype=jnp.floataa      )

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

       return self.init(lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase      )["params"]
      def 	UpperCAmelCase_						(			self   :Optional[Any]      )     ->			Optional[int]:
       UpperCAmelCase__							 =						self.block_out_channels
       UpperCAmelCase__							 =						block_out_channels[0] * 4

       # If `num_attention_heads` is not defined (which is the case for most models)
       # it will default to `attention_head_dim`. This looks weird upon first reading it and it is.
       # The reason for this behavior is to correct for incorrectly named variables that were introduced
       # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131
       # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking
       # which is why we correct for the naming here.
       UpperCAmelCase__							 =						self.num_attention_heads or self.attention_head_dim

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

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

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

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

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

       # down
       UpperCAmelCase__							 =						[]
       UpperCAmelCase__							 =						[]

       UpperCAmelCase__							 =						block_out_channels[0]

       UpperCAmelCase__							 =						nn.Conv(
           lowerCamelCase ,      kernel_size=(1, 1) ,      padding="VALID" ,      kernel_init=nn.initializers.zeros_init() ,      bias_init=nn.initializers.zeros_init() ,      dtype=self.dtype ,      )
       controlnet_down_blocks.append(lowerCamelCase      )

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

        if down_block_type == "CrossAttnDownBlock2D":
         UpperCAmelCase__							 =						FlaxCrossAttnDownBlockaD(
             in_channels=lowerCamelCase ,      out_channels=lowerCamelCase ,      dropout=self.dropout ,      num_layers=self.layers_per_block ,      num_attention_heads=num_attention_heads[i] ,      add_downsample=not is_final_block ,      use_linear_projection=self.use_linear_projection ,      only_cross_attention=only_cross_attention[i] ,      dtype=self.dtype ,      )
        else:
         UpperCAmelCase__							 =						FlaxDownBlockaD(
             in_channels=lowerCamelCase ,      out_channels=lowerCamelCase ,      dropout=self.dropout ,      num_layers=self.layers_per_block ,      add_downsample=not is_final_block ,      dtype=self.dtype ,      )

        down_blocks.append(lowerCamelCase      )

        for _ in range(self.layers_per_block      ):
         UpperCAmelCase__							 =						nn.Conv(
             lowerCamelCase ,      kernel_size=(1, 1) ,      padding="VALID" ,      kernel_init=nn.initializers.zeros_init() ,      bias_init=nn.initializers.zeros_init() ,      dtype=self.dtype ,      )
         controlnet_down_blocks.append(lowerCamelCase      )

        if not is_final_block:
         UpperCAmelCase__							 =						nn.Conv(
             lowerCamelCase ,      kernel_size=(1, 1) ,      padding="VALID" ,      kernel_init=nn.initializers.zeros_init() ,      bias_init=nn.initializers.zeros_init() ,      dtype=self.dtype ,      )
         controlnet_down_blocks.append(lowerCamelCase      )

       UpperCAmelCase__							 =						down_blocks
       UpperCAmelCase__							 =						controlnet_down_blocks

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

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







      def __call__(			self   :int ,      lowerCamelCase   :Any ,      lowerCamelCase   :Union[str, Any] ,      lowerCamelCase   :Any ,      lowerCamelCase   :Optional[Any] ,      lowerCamelCase   :float = 1.0 ,      lowerCamelCase   :bool = True ,      lowerCamelCase   :bool = False ,      )     ->			Union[FlaxControlNetOutput, Tuple]:
       UpperCAmelCase__							 =						self.controlnet_conditioning_channel_order
       if channel_order == "bgr":
        UpperCAmelCase__							 =						jnp.flip(lowerCamelCase ,      axis=1      )

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

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

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

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

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

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

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

       UpperCAmelCase__							 =						controlnet_down_block_res_samples

       UpperCAmelCase__							 =						self.controlnet_mid_block(lowerCamelCase      )

       # 6. scaling
       UpperCAmelCase__							 =						[sample * conditioning_scale for sample in down_block_res_samples]
       mid_block_res_sample *= conditioning_scale

       if not return_dict:
        return (down_block_res_samples, mid_block_res_sample)

       return FlaxControlNetOutput(
           down_block_res_samples=lowerCamelCase ,      mid_block_res_sample=lowerCamelCase      )











import json
from typing import TYPE_CHECKING, List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_base import BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging


if TYPE_CHECKING:
  from transformers.pipelines.conversational import Conversation


_lowerCAmelCase						:   str		=   logging.get_logger(__name__)

_lowerCAmelCase						:   List[Any]		=   {"tokenizer_file": "tokenizer.json"}

_lowerCAmelCase						:   Tuple		=   {
    "tokenizer_file": {
        "bigscience/tokenizer": "https://huggingface.co/bigscience/tokenizer/blob/main/tokenizer.json",
        "bigscience/bloom-560m": "https://huggingface.co/bigscience/bloom-560m/blob/main/tokenizer.json",
        "bigscience/bloom-1b1": "https://huggingface.co/bigscience/bloom-1b1/blob/main/tokenizer.json",
        "bigscience/bloom-1b7": "https://huggingface.co/bigscience/bloom-1b7/blob/main/tokenizer.json",
        "bigscience/bloom-3b": "https://huggingface.co/bigscience/bloom-3b/blob/main/tokenizer.json",
        "bigscience/bloom-7b1": "https://huggingface.co/bigscience/bloom-7b1/blob/main/tokenizer.json",
        "bigscience/bloom": "https://huggingface.co/bigscience/bloom/blob/main/tokenizer.json",
    },
}
class 			_UpperCamelCase      (				lowerCAmelCase		):
      UpperCAmelCase_        =		VOCAB_FILES_NAMES
      UpperCAmelCase_        =		PRETRAINED_VOCAB_FILES_MAP
      UpperCAmelCase_        =		["""input_ids""", """attention_mask"""]
      UpperCAmelCase_        =		None
      def __init__(			self   :List[Any] ,      lowerCamelCase   :Union[str, Any]=None ,      lowerCamelCase   :Optional[Any]=None ,      lowerCamelCase   :List[Any]=None ,      lowerCamelCase   :Union[str, Any]="<unk>" ,      lowerCamelCase   :Union[str, Any]="<s>" ,      lowerCamelCase   :Union[str, Any]="</s>" ,      lowerCamelCase   :str="<pad>" ,      lowerCamelCase   :Any=False ,      lowerCamelCase   :Optional[Any]=False ,      **lowerCamelCase   :Optional[Any] ,      )     ->			Any:
       super().__init__(
           lowerCamelCase ,      lowerCamelCase ,      tokenizer_file=lowerCamelCase ,      unk_token=lowerCamelCase ,      bos_token=lowerCamelCase ,      eos_token=lowerCamelCase ,      pad_token=lowerCamelCase ,      add_prefix_space=lowerCamelCase ,      clean_up_tokenization_spaces=lowerCamelCase ,      **lowerCamelCase ,      )
       UpperCAmelCase__							 =						json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()      )
       if pre_tok_state.get("add_prefix_space" ,      lowerCamelCase      ) != add_prefix_space:
        UpperCAmelCase__							 =						getattr(lowerCamelCase ,      pre_tok_state.pop("type"      )      )
        UpperCAmelCase__							 =						add_prefix_space
        UpperCAmelCase__							 =						pre_tok_class(**lowerCamelCase      )

       UpperCAmelCase__							 =						add_prefix_space
      def 	UpperCAmelCase_						(			self   :int ,      *lowerCamelCase   :Any ,      **lowerCamelCase   :int      )     ->			BatchEncoding:
       UpperCAmelCase__							 =						kwargs.get("is_split_into_words" ,      lowerCamelCase      )
       if not (self.add_prefix_space or not is_split_into_words):
        raise Exception(
            f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with'''
            " pretokenized inputs."      )

       return super()._batch_encode_plus(*lowerCamelCase ,      **lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :Any ,      *lowerCamelCase   :List[Any] ,      **lowerCamelCase   :Union[str, Any]      )     ->			BatchEncoding:
       UpperCAmelCase__							 =						kwargs.get("is_split_into_words" ,      lowerCamelCase      )

       if not (self.add_prefix_space or not is_split_into_words):
        raise Exception(
            f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with'''
            " pretokenized inputs."      )

       return super()._encode_plus(*lowerCamelCase ,      **lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :Tuple ,      lowerCamelCase   :str ,      lowerCamelCase   :Optional[str] = None      )     ->			Tuple[str]:
       UpperCAmelCase__							 =						self._tokenizer.model.save(lowerCamelCase ,      name=lowerCamelCase      )
       return tuple(lowerCamelCase      )







      def 	UpperCAmelCase_						(			self   :List[str] ,      lowerCamelCase   :"Conversation"      )     ->			List[int]:
       UpperCAmelCase__							 =						[]
       for is_user, text in conversation.iter_texts():
        input_ids.extend(self.encode(lowerCamelCase ,      add_special_tokens=lowerCamelCase      ) + [self.eos_token_id]      )

       if len(lowerCamelCase      ) > self.model_max_length:
        UpperCAmelCase__							 =						input_ids[-self.model_max_length :]
       return input_ids





