
import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_UpperCAmelCase					: Optional[int]      =       logging.get_logger(__name__)

_UpperCAmelCase					: Optional[int]      =       {
    "Salesforce/blip-vqa-base": "https://huggingface.co/Salesforce/blip-vqa-base/resolve/main/config.json",
    "Salesforce/blip-vqa-capfit-large": (
        "https://huggingface.co/Salesforce/blip-vqa-base-capfit/resolve/main/config.json"
    ),
    "Salesforce/blip-image-captioning-base": (
        "https://huggingface.co/Salesforce/blip-image-captioning-base/resolve/main/config.json"
    ),
    "Salesforce/blip-image-captioning-large": (
        "https://huggingface.co/Salesforce/blip-image-captioning-large/resolve/main/config.json"
    ),
    "Salesforce/blip-itm-base-coco": "https://huggingface.co/Salesforce/blip-itm-base-coco/resolve/main/config.json",
    "Salesforce/blip-itm-large-coco": "https://huggingface.co/Salesforce/blip-itm-large-coco/resolve/main/config.json",
    "Salesforce/blip-itm-base-flikr": "https://huggingface.co/Salesforce/blip-itm-base-flikr/resolve/main/config.json",
    "Salesforce/blip-itm-large-flikr": (
        "https://huggingface.co/Salesforce/blip-itm-large-flikr/resolve/main/config.json"
    ),
}


class 				lowercase					(		_SCREAMING_SNAKE_CASE   ):
		__lowercase       :       Tuple	     =				"blip_text_model"



		def __init__( self				,      A_=30_524				,      A_=768				,      A_=768				,      A_=3_072				,      A_=768				,      A_=12				,      A_=8				,      A_=512				,      A_="gelu"				,      A_=1e-12				,      A_=0.0				,      A_=0.0				,      A_=0.02				,      A_=30_522				,      A_=2				,      A_=0				,      A_=102				,      A_=True				,      A_=True				,      **A_				,      )  ->					Any:
				"""simple docstring"""
				super().__init__(
				    pad_token_id=A_				,      bos_token_id=A_				,      eos_token_id=A_				,      sep_token_id=A_				,      **A_				,      )

				UpperCamelCase		=     vocab_size
				UpperCamelCase		=     hidden_size
				UpperCamelCase		=     encoder_hidden_size
				UpperCamelCase		=     intermediate_size
				UpperCamelCase		=     projection_dim
				UpperCamelCase		=     hidden_dropout_prob
				UpperCamelCase		=     num_hidden_layers
				UpperCamelCase		=     num_attention_heads
				UpperCamelCase		=     max_position_embeddings
				UpperCamelCase		=     layer_norm_eps
				UpperCamelCase		=     hidden_act
				UpperCamelCase		=     initializer_range
				UpperCamelCase		=     attention_probs_dropout_prob
				UpperCamelCase		=     is_decoder
				UpperCamelCase		=     use_cache



		@classmethod
		def __UpperCamelCase ( cls				,      A_				,      **A_       )  ->					"PretrainedConfig":
				"""simple docstring"""
				cls._set_token_in_kwargs(A_       )

				UpperCamelCase	,  UpperCamelCase		=     cls.get_config_dict(A_				,      **A_       )

				# get the text config dict if we are loading from BlipConfig
				if config_dict.get('model_type'       ) == "blip":
						UpperCamelCase		=     config_dict['text_config']

				if "model_type" in config_dict and hasattr(cls				,      'model_type'       ) and config_dict["model_type"] != cls.model_type:
						logger.warning(
						    F'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
						    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''       )

				return cls.from_dict(A_				,      **A_       )






class 				lowercase					(		_SCREAMING_SNAKE_CASE   ):
		__lowercase       :       Any	     =				"blip_vision_model"



		def __init__( self				,      A_=768				,      A_=3_072				,      A_=512				,      A_=12				,      A_=12				,      A_=384				,      A_=16				,      A_="gelu"				,      A_=1e-5				,      A_=0.0				,      A_=1e-10				,      **A_				,      )  ->					List[str]:
				"""simple docstring"""
				super().__init__(**A_       )

				UpperCamelCase		=     hidden_size
				UpperCamelCase		=     intermediate_size
				UpperCamelCase		=     projection_dim
				UpperCamelCase		=     num_hidden_layers
				UpperCamelCase		=     num_attention_heads
				UpperCamelCase		=     patch_size
				UpperCamelCase		=     image_size
				UpperCamelCase		=     initializer_range
				UpperCamelCase		=     attention_dropout
				UpperCamelCase		=     layer_norm_eps
				UpperCamelCase		=     hidden_act



		@classmethod
		def __UpperCamelCase ( cls				,      A_				,      **A_       )  ->					"PretrainedConfig":
				"""simple docstring"""
				cls._set_token_in_kwargs(A_       )

				UpperCamelCase	,  UpperCamelCase		=     cls.get_config_dict(A_				,      **A_       )

				# get the vision config dict if we are loading from BlipConfig
				if config_dict.get('model_type'       ) == "blip":
						UpperCamelCase		=     config_dict['vision_config']

				if "model_type" in config_dict and hasattr(cls				,      'model_type'       ) and config_dict["model_type"] != cls.model_type:
						logger.warning(
						    F'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
						    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''       )

				return cls.from_dict(A_				,      **A_       )






class 				lowercase					(		_SCREAMING_SNAKE_CASE   ):
		__lowercase       :       List[str]	     =				"blip"
		__lowercase       :       int	     =				True



		def __init__( self				,      A_=None				,      A_=None				,      A_=512				,      A_=2.6592				,      A_=256				,      **A_				,      )  ->					str:
				"""simple docstring"""
				super().__init__(**A_       )

				if text_config is None:
						UpperCamelCase		=     {}
						logger.info('`text_config` is `None`. Initializing the `BlipTextConfig` with default values.'       )

				if vision_config is None:
						UpperCamelCase		=     {}
						logger.info('`vision_config` is `None`. Initializing the `BlipVisionConfig` with default values.'       )

				UpperCamelCase		=     BlipTextConfig(**A_       )
				UpperCamelCase		=     BlipVisionConfig(**A_       )

				UpperCamelCase		=     self.vision_config.hidden_size

				UpperCamelCase		=     projection_dim
				UpperCamelCase		=     logit_scale_init_value
				UpperCamelCase		=     1.0
				UpperCamelCase		=     0.02
				UpperCamelCase		=     image_text_hidden_size



		@classmethod
		def __UpperCamelCase ( cls				,      A_				,      A_				,      **A_       )  ->					Optional[Any]:
				"""simple docstring"""
				return cls(text_config=text_config.to_dict()				,      vision_config=vision_config.to_dict()				,      **A_       )



		def __UpperCamelCase ( self       )  ->					int:
				"""simple docstring"""
				UpperCamelCase		=     copy.deepcopy(self.__dict__       )
				UpperCamelCase		=     self.text_config.to_dict()
				UpperCamelCase		=     self.vision_config.to_dict()
				UpperCamelCase		=     self.__class__.model_type
				return output


import os
import re
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 logging


_UpperCAmelCase					: List[Any]      =       logging.get_logger(__name__)

_UpperCAmelCase					: Dict      =       {"vocab_file": "spiece.model"}

_UpperCAmelCase					: Dict      =       {
    "vocab_file": {
        "google/bigbird-roberta-base": "https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model",
        "google/bigbird-roberta-large": (
            "https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model"
        ),
        "google/bigbird-base-trivia-itc": (
            "https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model"
        ),
    }
}

_UpperCAmelCase					: int      =       {
    "google/bigbird-roberta-base": 4_096,
    "google/bigbird-roberta-large": 4_096,
    "google/bigbird-base-trivia-itc": 4_096,
}


class 				lowercase					(		_SCREAMING_SNAKE_CASE   ):
		__lowercase       :       List[str]	     =				VOCAB_FILES_NAMES
		__lowercase       :       int	     =				PRETRAINED_VOCAB_FILES_MAP
		__lowercase       :       List[Any]	     =				PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		__lowercase       :       List[Any]	     =				["input_ids", "attention_mask"]
		__lowercase       :       List[int]	     =				[]



		def __init__( self				,      A_				,      A_="<unk>"				,      A_="<s>"				,      A_="</s>"				,      A_="<pad>"				,      A_="[SEP]"				,      A_="[MASK]"				,      A_="[CLS]"				,      A_ = None				,      **A_				,      )  ->					None:
				"""simple docstring"""
				UpperCamelCase		=     AddedToken(A_				,      lstrip=A_				,      rstrip=A_       ) if isinstance(A_				,      A_       ) else bos_token
				UpperCamelCase		=     AddedToken(A_				,      lstrip=A_				,      rstrip=A_       ) if isinstance(A_				,      A_       ) else eos_token
				UpperCamelCase		=     AddedToken(A_				,      lstrip=A_				,      rstrip=A_       ) if isinstance(A_				,      A_       ) else unk_token
				UpperCamelCase		=     AddedToken(A_				,      lstrip=A_				,      rstrip=A_       ) if isinstance(A_				,      A_       ) else pad_token
				UpperCamelCase		=     AddedToken(A_				,      lstrip=A_				,      rstrip=A_       ) if isinstance(A_				,      A_       ) else cls_token
				UpperCamelCase		=     AddedToken(A_				,      lstrip=A_				,      rstrip=A_       ) if isinstance(A_				,      A_       ) else sep_token

				# Mask token behave like a normal word, i.e. include the space before it
				UpperCamelCase		=     AddedToken(A_				,      lstrip=A_				,      rstrip=A_       ) if isinstance(A_				,      A_       ) else mask_token

				UpperCamelCase		=     {} if sp_model_kwargs is None else sp_model_kwargs

				super().__init__(
				    bos_token=A_				,      eos_token=A_				,      unk_token=A_				,      pad_token=A_				,      sep_token=A_				,      mask_token=A_				,      cls_token=A_				,      sp_model_kwargs=self.sp_model_kwargs				,      **A_				,      )

				UpperCamelCase		=     vocab_file

				UpperCamelCase		=     spm.SentencePieceProcessor(**self.sp_model_kwargs       )
				self.sp_model.Load(A_       )



		@property
		def __UpperCamelCase ( self       )  ->					Dict:
				"""simple docstring"""
				return self.sp_model.get_piece_size()



		def __UpperCamelCase ( self       )  ->					List[Any]:
				"""simple docstring"""
				UpperCamelCase		=     {self.convert_ids_to_tokens(A_       ): i for i in range(self.vocab_size       )}
				vocab.update(self.added_tokens_encoder       )
				return vocab



		def __getstate__( self       )  ->					Optional[int]:
				"""simple docstring"""
				UpperCamelCase		=     self.__dict__.copy()
				UpperCamelCase		=     None
				return state



		def __setstate__( self				,      A_       )  ->					Optional[int]:
				"""simple docstring"""
				UpperCamelCase		=     d

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

				UpperCamelCase		=     spm.SentencePieceProcessor(**self.sp_model_kwargs       )
				self.sp_model.Load(self.vocab_file       )



		def __UpperCamelCase ( self				,      A_       )  ->					List[str]:
				"""simple docstring"""
				return self.sp_model.encode(A_				,      out_type=A_       )



		def __UpperCamelCase ( self				,      A_       )  ->					Optional[Any]:
				"""simple docstring"""
				return self.sp_model.piece_to_id(A_       )



		def __UpperCamelCase ( self				,      A_       )  ->					Tuple:
				"""simple docstring"""
				UpperCamelCase		=     self.sp_model.IdToPiece(A_       )
				return token



		def __UpperCamelCase ( self				,      A_       )  ->					List[Any]:
				"""simple docstring"""
				UpperCamelCase		=     []
				UpperCamelCase		=     ''
				UpperCamelCase		=     False
				for token in tokens:
						# make sure that special tokens are not decoded using sentencepiece model
						if token in self.all_special_tokens:
								if not prev_is_special:
										out_string += " "
								out_string += self.sp_model.decode(A_       ) + token
								UpperCamelCase		=     True
								UpperCamelCase		=     []
						else:
								current_sub_tokens.append(A_       )
								UpperCamelCase		=     False
				out_string += self.sp_model.decode(A_       )
				return out_string.strip()



		def __UpperCamelCase ( self				,      A_				,      A_ = False				,      A_ = None				,      A_ = True				,      **A_				,      )  ->					str:
				"""simple docstring"""
				UpperCamelCase		=     kwargs.pop('use_source_tokenizer'				,      A_       )

				UpperCamelCase		=     self.convert_ids_to_tokens(A_				,      skip_special_tokens=A_       )

				# To avoid mixing byte-level and unicode for byte-level BPT
				# we need to build string separately for added tokens and byte-level tokens
				# cf. https://github.com/huggingface/transformers/issues/1133
				UpperCamelCase		=     []
				UpperCamelCase		=     []
				for token in filtered_tokens:
						if skip_special_tokens and token in self.all_special_ids:
								continue
						if token in self.added_tokens_encoder:
								if current_sub_text:
										sub_texts.append(self.convert_tokens_to_string(A_       )       )
										UpperCamelCase		=     []
								sub_texts.append(A_       )
						else:
								current_sub_text.append(A_       )
				if current_sub_text:
						sub_texts.append(self.convert_tokens_to_string(A_       )       )

				# Mimic the behavior of the Rust tokenizer:
				# No space before [MASK] and [SEP]
				if spaces_between_special_tokens:
						UpperCamelCase		=     re.sub(r' (\[(MASK|SEP)\])'				,      r'\1'				,      ' '.join(A_       )       )
				else:
						UpperCamelCase		=     ''.join(A_       )

				UpperCamelCase		=     (
				    clean_up_tokenization_spaces
				    if clean_up_tokenization_spaces is not None
				    else self.clean_up_tokenization_spaces
				)
				if clean_up_tokenization_spaces:
						UpperCamelCase		=     self.clean_up_tokenization(A_       )
						return clean_text
				else:
						return text



		def __UpperCamelCase ( self				,      A_				,      A_ = None       )  ->					Tuple[str]:
				"""simple docstring"""
				if not os.path.isdir(A_       ):
						logger.error(F'''Vocabulary path ({save_directory}) should be a directory'''       )
						return
				UpperCamelCase		=     os.path.join(
				    A_				,      (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']       )

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

				return (out_vocab_file,)



		def __UpperCamelCase ( self				,      A_				,      A_ = None       )  ->					List[int]:
				"""simple docstring"""
				if token_ids_a is None:
						return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
				UpperCamelCase		=     [self.cls_token_id]
				UpperCamelCase		=     [self.sep_token_id]
				return cls + token_ids_a + sep + token_ids_a + sep



		def __UpperCamelCase ( self				,      A_				,      A_ = None				,      A_ = False       )  ->					List[int]:
				"""simple docstring"""
				if already_has_special_tokens:
						return super().get_special_tokens_mask(
						    token_ids_a=A_				,      token_ids_a=A_				,      already_has_special_tokens=A_       )

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



		def __UpperCamelCase ( self				,      A_				,      A_ = None       )  ->					List[int]:
				"""simple docstring"""
				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]
