

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


A__						=							logging.get_logger(__name__)

A__						=							{"tokenizer_file": "tokenizer.json"}

A__						=							{
    "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				(					snake_case__			):
	_lowercase  :   str       				=  VOCAB_FILES_NAMES
	_lowercase  :   Tuple       				=  PRETRAINED_VOCAB_FILES_MAP
	_lowercase  :   str       				=  ["input_ids", "attention_mask"]
	_lowercase  :   Union[str, Any]       				=  None







	def __init__(   self: Any  , __UpperCamelCase: Any=None  , __UpperCamelCase: Tuple=None  , __UpperCamelCase: str=None  , __UpperCamelCase: List[str]="<unk>"  , __UpperCamelCase: str="<s>"  , __UpperCamelCase: Optional[int]="</s>"  , __UpperCamelCase: List[Any]="<pad>"  , __UpperCamelCase: int=False  , __UpperCamelCase: Union[str, Any]=False  , **__UpperCamelCase: int  , ):

						'''simple docstring'''





						super().__init__(
						    lowercase_  , lowercase_  , tokenizer_file=lowercase_  , unk_token=lowercase_  , bos_token=lowercase_  , eos_token=lowercase_  , pad_token=lowercase_  , add_prefix_space=lowercase_  , clean_up_tokenization_spaces=lowercase_  , **lowercase_  , )
						__magic_name__							=    json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()      )
						if pre_tok_state.get('add_prefix_space'  , lowercase_      ) != add_prefix_space:
											__magic_name__							=    getattr(lowercase_  , pre_tok_state.pop('type'      )      )
											__magic_name__							=    add_prefix_space
											__magic_name__							=    pre_tok_class(**lowercase_      )

						__magic_name__							=    add_prefix_space







	def 	_SCREAMING_SNAKE_CASE							(   self: int  , *__UpperCamelCase: Optional[Any]  , **__UpperCamelCase: Tuple      ):

						'''simple docstring'''





						__magic_name__							=    kwargs.get('is_split_into_words'  , lowercase_      )
						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(*lowercase_  , **lowercase_      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple  , *__UpperCamelCase: Optional[Any]  , **__UpperCamelCase: Optional[Any]      ):

						'''simple docstring'''





						__magic_name__							=    kwargs.get('is_split_into_words'  , lowercase_      )

						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(*lowercase_  , **lowercase_      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Optional[int]  , __UpperCamelCase: Tuple  , __UpperCamelCase: Optional[Any] = None      ):

						'''simple docstring'''





						__magic_name__							=    self._tokenizer.model.save(lowercase_  , name=lowercase_      )
						return tuple(lowercase_      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Optional[int]  , __UpperCamelCase: Union[str, Any]      ):

						'''simple docstring'''





						__magic_name__							=    []
						for is_user, text in conversation.iter_texts():
											input_ids.extend(self.encode(lowercase_  , add_special_tokens=lowercase_      ) + [self.eos_token_id]      )

						if len(lowercase_      ) > self.model_max_length:
											__magic_name__							=    input_ids[-self.model_max_length :]
						return input_ids







import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin

class 				__UpperCamelCase				(					SCREAMING_SNAKE_CASE  ,						SCREAMING_SNAKE_CASE			):







	@register_to_config
	def __init__(   self: Optional[Any]  , *,
	    __UpperCamelCase: int = 4  , __UpperCamelCase: int = 7_68  , __UpperCamelCase: int  , __UpperCamelCase: Any  , ):

						'''simple docstring'''





						super().__init__()

						__magic_name__							=    nn.Parameter(torch.zeros(__UpperCamelCase      )      )

						# parameters for additional clip time embeddings
						__magic_name__							=    nn.Linear(__UpperCamelCase  , __UpperCamelCase      )
						__magic_name__							=    nn.Linear(__UpperCamelCase  , __UpperCamelCase      )

						# parameters for encoder hidden states
						__magic_name__							=    clip_extra_context_tokens
						__magic_name__							=    nn.Linear(
						    __UpperCamelCase  , self.clip_extra_context_tokens * cross_attention_dim      )
						__magic_name__							=    nn.Linear(__UpperCamelCase  , __UpperCamelCase      )
						__magic_name__							=    nn.LayerNorm(__UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: List[str]  , *, __UpperCamelCase: Union[str, Any]  , __UpperCamelCase: str  , __UpperCamelCase: Optional[Any]  , __UpperCamelCase: Any      ):

						'''simple docstring'''





						if do_classifier_free_guidance:
											# Add the classifier free guidance embeddings to the image embeddings
											__magic_name__							=    image_embeddings.shape[0]
											__magic_name__							=    self.learned_classifier_free_guidance_embeddings.unsqueeze(0      )
											__magic_name__							=    classifier_free_guidance_embeddings.expand(
											    __UpperCamelCase  , -1      )
											__magic_name__							=    torch.cat([classifier_free_guidance_embeddings, image_embeddings]  , dim=0      )

						# The image embeddings batch size and the text embeddings batch size are equal
						assert image_embeddings.shape[0] == prompt_embeds.shape[0]

						__magic_name__							=    prompt_embeds.shape[0]

						# "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and
						# adding CLIP embeddings to the existing timestep embedding, ...
						__magic_name__							=    self.embedding_proj(__UpperCamelCase      )
						__magic_name__							=    self.clip_image_embeddings_project_to_time_embeddings(__UpperCamelCase      )
						__magic_name__							=    time_projected_image_embeddings + time_projected_prompt_embeds

						# ... and by projecting CLIP embeddings into four
						# extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder"
						__magic_name__							=    self.clip_extra_context_tokens_proj(__UpperCamelCase      )
						__magic_name__							=    clip_extra_context_tokens.reshape(__UpperCamelCase  , -1  , self.clip_extra_context_tokens      )
						__magic_name__							=    clip_extra_context_tokens.permute(0  , 2  , 1      )

						__magic_name__							=    self.encoder_hidden_states_proj(__UpperCamelCase      )
						__magic_name__							=    self.text_encoder_hidden_states_norm(__UpperCamelCase      )
						__magic_name__							=    torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]  , dim=1      )

						return text_encoder_hidden_states, additive_clip_time_embeddings




