


import json
from typing import Dict, List, Optional, Tuple, Union

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import PaddingStrategy, logging
from .tokenization_led import LEDTokenizer


UpperCAmelCase_				:			Union[str, Any]   =	logging.get_logger(__name__)


UpperCAmelCase_				:			str   =	{'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'}

UpperCAmelCase_				:			Any   =	{
    'vocab_file': {
        'allenai/led-base-16384': 'https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json',
    },
    'merges_file': {
        'allenai/led-base-16384': 'https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt',
    },
    'tokenizer_file': {
        'allenai/led-base-16384': 'https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json',
    },
}

UpperCAmelCase_				:			Tuple   =	{
    'allenai/led-base-16384': 1_6384,
}
class 			SCREAMING_SNAKE_CASE__  (    lowercase__      ):
      snake_case__    :					Optional[Any]					       =						VOCAB_FILES_NAMES
      snake_case__    :					Optional[Any]					       =						PRETRAINED_VOCAB_FILES_MAP
      snake_case__    :					Dict					       =						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      snake_case__    :					List[str]					       =						LEDTokenizer
      snake_case__    :					Dict					       =						['''input_ids''', '''attention_mask''']
      def __init__(	self    :	Optional[Any]					,							SCREAMING_SNAKE_CASE__    :	str=None					,							SCREAMING_SNAKE_CASE__    :	List[str]=None					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=None					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]="replace"					,							SCREAMING_SNAKE_CASE__    :	List[Any]="<s>"					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]="</s>"					,							SCREAMING_SNAKE_CASE__    :	Optional[int]="</s>"					,							SCREAMING_SNAKE_CASE__    :	Tuple="<s>"					,							SCREAMING_SNAKE_CASE__    :	List[str]="<unk>"					,							SCREAMING_SNAKE_CASE__    :	Dict="<pad>"					,							SCREAMING_SNAKE_CASE__    :	List[Any]="<mask>"					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]=False					,							SCREAMING_SNAKE_CASE__    :	Any=True					,							**SCREAMING_SNAKE_CASE__    :	Tuple					,							)		->      Dict:
             super().__init__(
                 SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					,							tokenizer_file=SCREAMING_SNAKE_CASE__					,							errors=SCREAMING_SNAKE_CASE__					,							bos_token=SCREAMING_SNAKE_CASE__					,							eos_token=SCREAMING_SNAKE_CASE__					,							sep_token=SCREAMING_SNAKE_CASE__					,							cls_token=SCREAMING_SNAKE_CASE__					,							unk_token=SCREAMING_SNAKE_CASE__					,							pad_token=SCREAMING_SNAKE_CASE__					,							mask_token=SCREAMING_SNAKE_CASE__					,							add_prefix_space=SCREAMING_SNAKE_CASE__					,							trim_offsets=SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					,							)

             a_ :		Union[str, Any]							=  json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()					)
             if pre_tok_state.get('add_prefix_space'					,							SCREAMING_SNAKE_CASE__					) != add_prefix_space:
                    a_ :		Dict							=  getattr(SCREAMING_SNAKE_CASE__					,							pre_tok_state.pop('type'					)					)
                    a_ :		str							=  add_prefix_space
                    a_ :		Optional[Any]							=  pre_tok_class(**SCREAMING_SNAKE_CASE__					)

             a_ :		str							=  add_prefix_space

             # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__`
             a_ :		Optional[Any]							=  'post_processor'
             a_ :		Union[str, Any]							=  getattr(self.backend_tokenizer					,							SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)
             if tokenizer_component_instance:
                    a_ :		Optional[Any]							=  json.loads(tokenizer_component_instance.__getstate__()					)

                    # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
                    if "sep" in state:
                           a_ :		Tuple							=  tuple(state['sep']					)
                    if "cls" in state:
                           a_ :		Optional[Any]							=  tuple(state['cls']					)

                    a_ :		Optional[int]							=  False

                    if state.get('add_prefix_space'					,							SCREAMING_SNAKE_CASE__					) != add_prefix_space:
                           a_ :		str							=  add_prefix_space
                           a_ :		Union[str, Any]							=  True

                    if state.get('trim_offsets'					,							SCREAMING_SNAKE_CASE__					) != trim_offsets:
                           a_ :		Any							=  trim_offsets
                           a_ :		Tuple							=  True

                    if changes_to_apply:
                           a_ :		Optional[int]							=  getattr(SCREAMING_SNAKE_CASE__					,							state.pop('type'					)					)
                           a_ :		Any							=  component_class(**SCREAMING_SNAKE_CASE__					)
                           setattr(self.backend_tokenizer					,							SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)
      @property
      # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED
      def 	SCREAMING_SNAKE_CASE							(	self    :	Optional[int]					)		->      str:
             if self._mask_token is None:
                    if self.verbose:
                           logger.error('Using mask_token, but it is not set yet.'					)
                    return None
             return str(self._mask_token					)
      @mask_token.setter
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[str]					,							SCREAMING_SNAKE_CASE__    :	str					)		->      str:
             a_ :		List[Any]							=  AddedToken(SCREAMING_SNAKE_CASE__					,							lstrip=SCREAMING_SNAKE_CASE__					,							rstrip=SCREAMING_SNAKE_CASE__					) if isinstance(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					) else value
             a_ :		Optional[int]							=  value
      def 	SCREAMING_SNAKE_CASE							(	self    :	Union[str, Any]					,							*SCREAMING_SNAKE_CASE__    :	List[Any]					,							**SCREAMING_SNAKE_CASE__    :	Optional[Any]					)		->      BatchEncoding:
             a_ :		List[str]							=  kwargs.get('is_split_into_words'					,							SCREAMING_SNAKE_CASE__					)

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

             return super()._batch_encode_plus(*SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[Any]					,							*SCREAMING_SNAKE_CASE__    :	Any					,							**SCREAMING_SNAKE_CASE__    :	List[str]					)		->      BatchEncoding:
             a_ :		int							=  kwargs.get('is_split_into_words'					,							SCREAMING_SNAKE_CASE__					)

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

             return super()._encode_plus(*SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[Any]					,							SCREAMING_SNAKE_CASE__    :	str					,							SCREAMING_SNAKE_CASE__    :	Optional[str] = None					)		->      Tuple[str]:
             a_ :		Any							=  self._tokenizer.model.save(SCREAMING_SNAKE_CASE__					,							name=SCREAMING_SNAKE_CASE__					)
             return tuple(SCREAMING_SNAKE_CASE__					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	Dict					,							SCREAMING_SNAKE_CASE__    :	Any					,							SCREAMING_SNAKE_CASE__    :	str=None					)		->      List[Any]:
             a_ :		Any							=  [self.bos_token_id] + token_ids_a + [self.eos_token_id]
             if token_ids_a is None:
                    return output

             return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]
      def 	SCREAMING_SNAKE_CASE							(	self    :	Union[str, Any]					,							SCREAMING_SNAKE_CASE__    :	List[int]					,							SCREAMING_SNAKE_CASE__    :	Optional[List[int]] = None					)		->      List[int]:
             a_ :		Tuple							=  [self.sep_token_id]
             a_ :		str							=  [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 + sep + token_ids_a + sep					) * [0]

      def 	SCREAMING_SNAKE_CASE							(	self    :	Tuple					,							SCREAMING_SNAKE_CASE__    :	Union[Dict[str, EncodedInput], BatchEncoding]					,							SCREAMING_SNAKE_CASE__    :	Optional[int] = None					,							SCREAMING_SNAKE_CASE__    :	PaddingStrategy = PaddingStrategy.DO_NOT_PAD					,							SCREAMING_SNAKE_CASE__    :	Optional[int] = None					,							SCREAMING_SNAKE_CASE__    :	Optional[bool] = None					,							)		->      dict:
             a_ :		Any							=  super()._pad(
                 encoded_inputs=SCREAMING_SNAKE_CASE__					,							max_length=SCREAMING_SNAKE_CASE__					,							padding_strategy=SCREAMING_SNAKE_CASE__					,							pad_to_multiple_of=SCREAMING_SNAKE_CASE__					,							return_attention_mask=SCREAMING_SNAKE_CASE__					,							)

             # Load from model defaults
             if return_attention_mask is None:
                    a_ :		Tuple							=  'attention_mask' in self.model_input_names

             if return_attention_mask and "global_attention_mask" in encoded_inputs:
                    a_ :		Tuple							=  encoded_inputs[self.model_input_names[0]]
                    # `global_attention_mask` need to have the same length as other (sequential) inputs.
                    a_ :		List[str]							=  len(encoded_inputs['global_attention_mask']					) != len(SCREAMING_SNAKE_CASE__					)

                    if needs_to_be_padded:
                           a_ :		Any							=  len(SCREAMING_SNAKE_CASE__					) - len(encoded_inputs['global_attention_mask']					)

                           if self.padding_side == "right":
                                  # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend`
                                  a_ :		List[Any]							=  (
                                      encoded_inputs['global_attention_mask'] + [-1] * difference
                                  )
                           elif self.padding_side == "left":
                                  a_ :		Union[str, Any]							=  [-1] * difference + encoded_inputs[
                                      'global_attention_mask'
                                  ]
                           else:
                                  raise ValueError('Invalid padding strategy:' + str(self.padding_side					)					)

             return encoded_inputs






import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
from ...utils import logging
from ..auto import CONFIG_MAPPING


UpperCAmelCase_				:			str   =	logging.get_logger(__name__)

UpperCAmelCase_				:			str   =	{
    'Salesforce/instruct-blip-flan-t5': 'https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json',
}
class 			SCREAMING_SNAKE_CASE__  (    lowercase__      ):
      snake_case__    :					int					       =						'''instructblip_vision_model'''
      def __init__(	self    :	Optional[int]					,							SCREAMING_SNAKE_CASE__    :	List[Any]=1_4_0_8					,							SCREAMING_SNAKE_CASE__    :	int=6_1_4_4					,							SCREAMING_SNAKE_CASE__    :	Dict=3_9					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=1_6					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=2_2_4					,							SCREAMING_SNAKE_CASE__    :	Optional[int]=1_4					,							SCREAMING_SNAKE_CASE__    :	List[Any]="gelu"					,							SCREAMING_SNAKE_CASE__    :	List[str]=1E-6					,							SCREAMING_SNAKE_CASE__    :	Tuple=0.0					,							SCREAMING_SNAKE_CASE__    :	List[str]=1E-10					,							SCREAMING_SNAKE_CASE__    :	str=True					,							**SCREAMING_SNAKE_CASE__    :	Dict					,							)		->      int:
             super().__init__(**SCREAMING_SNAKE_CASE__					)

             a_ :		List[str]							=  hidden_size
             a_ :		Any							=  intermediate_size
             a_ :		str							=  num_hidden_layers
             a_ :		Dict							=  num_attention_heads
             a_ :		str							=  patch_size
             a_ :		Any							=  image_size
             a_ :		Dict							=  initializer_range
             a_ :		List[Any]							=  attention_dropout
             a_ :		Union[str, Any]							=  layer_norm_eps
             a_ :		Optional[Any]							=  hidden_act
             a_ :		Optional[Any]							=  qkv_bias

      @classmethod
      def 	SCREAMING_SNAKE_CASE							(	cls    :	Any					,							SCREAMING_SNAKE_CASE__    :	Union[str, os.PathLike]					,							**SCREAMING_SNAKE_CASE__    :	List[str]					)		->      "PretrainedConfig":
             cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE__					)

             a_							,		a_ :		Dict							=  cls.get_config_dict(SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)

             # get the vision config dict if we are loading from InstructBlipConfig
             if config_dict.get('model_type'					) == "instructblip":
                    a_ :		List[Any]							=  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(SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)
class 			SCREAMING_SNAKE_CASE__  (    lowercase__      ):
      snake_case__    :					List[Any]					       =						'''instructblip_qformer'''
      def __init__(	self    :	Optional[int]					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=3_0_5_2_2					,							SCREAMING_SNAKE_CASE__    :	str=7_6_8					,							SCREAMING_SNAKE_CASE__    :	List[str]=1_2					,							SCREAMING_SNAKE_CASE__    :	Any=1_2					,							SCREAMING_SNAKE_CASE__    :	Optional[int]=3_0_7_2					,							SCREAMING_SNAKE_CASE__    :	List[str]="gelu"					,							SCREAMING_SNAKE_CASE__    :	Dict=0.1					,							SCREAMING_SNAKE_CASE__    :	int=0.1					,							SCREAMING_SNAKE_CASE__    :	Optional[int]=5_1_2					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]=0.02					,							SCREAMING_SNAKE_CASE__    :	int=1E-12					,							SCREAMING_SNAKE_CASE__    :	Dict=0					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]="absolute"					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=2					,							SCREAMING_SNAKE_CASE__    :	str=1_4_0_8					,							**SCREAMING_SNAKE_CASE__    :	Dict					,							)		->      Optional[Any]:
             super().__init__(pad_token_id=SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)

             a_ :		Union[str, Any]							=  vocab_size
             a_ :		List[str]							=  hidden_size
             a_ :		Any							=  num_hidden_layers
             a_ :		Dict							=  num_attention_heads
             a_ :		Optional[int]							=  hidden_act
             a_ :		List[str]							=  intermediate_size
             a_ :		Tuple							=  hidden_dropout_prob
             a_ :		Union[str, Any]							=  attention_probs_dropout_prob
             a_ :		List[str]							=  max_position_embeddings
             a_ :		List[str]							=  initializer_range
             a_ :		Any							=  layer_norm_eps
             a_ :		Tuple							=  position_embedding_type
             a_ :		List[str]							=  cross_attention_frequency
             a_ :		Union[str, Any]							=  encoder_hidden_size

      @classmethod
      def 	SCREAMING_SNAKE_CASE							(	cls    :	str					,							SCREAMING_SNAKE_CASE__    :	Union[str, os.PathLike]					,							**SCREAMING_SNAKE_CASE__    :	Union[str, Any]					)		->      "PretrainedConfig":
             cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE__					)

             a_							,		a_ :		Union[str, Any]							=  cls.get_config_dict(SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)

             # get the qformer config dict if we are loading from InstructBlipConfig
             if config_dict.get('model_type'					) == "instructblip":
                    a_ :		Dict							=  config_dict['qformer_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(SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)
class 			SCREAMING_SNAKE_CASE__  (    lowercase__      ):
      snake_case__    :					Union[str, Any]					       =						'''instructblip'''
      snake_case__    :					List[str]					       =						True
      def __init__(	self    :	Tuple					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]=None					,							SCREAMING_SNAKE_CASE__    :	Dict=None					,							SCREAMING_SNAKE_CASE__    :	List[Any]=None					,							SCREAMING_SNAKE_CASE__    :	int=3_2					,							**SCREAMING_SNAKE_CASE__    :	Union[str, Any]					)		->      Tuple:
             super().__init__(**SCREAMING_SNAKE_CASE__					)

             if vision_config is None:
                    a_ :		Dict							=  {}
                    logger.info('vision_config is None. initializing the InstructBlipVisionConfig with default values.'					)

             if qformer_config is None:
                    a_ :		List[str]							=  {}
                    logger.info('qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.'					)

             if text_config is None:
                    a_ :		Any							=  {}
                    logger.info('text_config is None. Initializing the text config with default values (`OPTConfig`).'					)

             a_ :		int							=  InstructBlipVisionConfig(**SCREAMING_SNAKE_CASE__					)
             a_ :		int							=  InstructBlipQFormerConfig(**SCREAMING_SNAKE_CASE__					)
             a_ :		Optional[Any]							=  text_config['model_type'] if 'model_type' in text_config else 'opt'
             a_ :		List[Any]							=  CONFIG_MAPPING[text_model_type](**SCREAMING_SNAKE_CASE__					)

             a_ :		Tuple							=  self.text_config.tie_word_embeddings
             a_ :		List[Any]							=  self.text_config.is_encoder_decoder

             a_ :		Optional[Any]							=  num_query_tokens
             a_ :		int							=  self.vision_config.hidden_size
             a_ :		List[str]							=  self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
             a_ :		Optional[int]							=  1.0
             a_ :		Any							=  0.02
      @classmethod
      def 	SCREAMING_SNAKE_CASE							(	cls    :	List[Any]					,							SCREAMING_SNAKE_CASE__    :	InstructBlipVisionConfig					,							SCREAMING_SNAKE_CASE__    :	InstructBlipQFormerConfig					,							SCREAMING_SNAKE_CASE__    :	PretrainedConfig					,							**SCREAMING_SNAKE_CASE__    :	List[str]					,							)		->      Any:
             return cls(
                 vision_config=vision_config.to_dict()					,							qformer_config=qformer_config.to_dict()					,							text_config=text_config.to_dict()					,							**SCREAMING_SNAKE_CASE__					,							)

      def 	SCREAMING_SNAKE_CASE							(	self    :	Optional[int]					)		->      Union[str, Any]:
             a_ :		List[Any]							=  copy.deepcopy(self.__dict__					)
             a_ :		Optional[int]							=  self.vision_config.to_dict()
             a_ :		Optional[Any]							=  self.qformer_config.to_dict()
             a_ :		Union[str, Any]							=  self.text_config.to_dict()
             a_ :		Optional[Any]							=  self.__class__.model_type
             return output


