
"""simple docstring"""




import os
import unicodedata
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__   :					Union[str, Any]												=				logging.get_logger(__name__)
UpperCAmelCase__   :					List[Any]												=				{'vocab_file': 'spiece.model'}

UpperCAmelCase__   :					Optional[Any]												=				{
    'vocab_file': {
        'albert-base-v1': 'https://huggingface.co/albert-base-v1/resolve/main/spiece.model',
        'albert-large-v1': 'https://huggingface.co/albert-large-v1/resolve/main/spiece.model',
        'albert-xlarge-v1': 'https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model',
        'albert-xxlarge-v1': 'https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model',
        'albert-base-v2': 'https://huggingface.co/albert-base-v2/resolve/main/spiece.model',
        'albert-large-v2': 'https://huggingface.co/albert-large-v2/resolve/main/spiece.model',
        'albert-xlarge-v2': 'https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model',
        'albert-xxlarge-v2': 'https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model',
    }
}

UpperCAmelCase__   :					List[str]												=				{
    'albert-base-v1': 5_1_2,
    'albert-large-v1': 5_1_2,
    'albert-xlarge-v1': 5_1_2,
    'albert-xxlarge-v1': 5_1_2,
    'albert-base-v2': 5_1_2,
    'albert-large-v2': 5_1_2,
    'albert-xlarge-v2': 5_1_2,
    'albert-xxlarge-v2': 5_1_2,
}

UpperCAmelCase__   :					Dict												=				'▁'



class 			lowerCAmelCase_		(__SCREAMING_SNAKE_CASE						):


				"""simple docstring"""



				__UpperCamelCase   :		Optional[Any]       =     VOCAB_FILES_NAMES
				__UpperCamelCase   :		int       =     PRETRAINED_VOCAB_FILES_MAP
				__UpperCamelCase   :		Dict       =     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES



				def __init__(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__="[CLS]"     , SCREAMING_SNAKE_CASE__="[SEP]"     , SCREAMING_SNAKE_CASE__="<unk>"     , SCREAMING_SNAKE_CASE__="[SEP]"     , SCREAMING_SNAKE_CASE__="<pad>"     , SCREAMING_SNAKE_CASE__="[CLS]"     , SCREAMING_SNAKE_CASE__="[MASK]"     , SCREAMING_SNAKE_CASE__ = None     , **SCREAMING_SNAKE_CASE__     , )      ->	List[str]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[int]     =     (
											    AddedToken(_a     , lstrip=_a     , rstrip=_a     , normalized=_a					)
											    if isinstance(_a     , _a					)
											    else mask_token
											)

											SCREAMING_SNAKE_CASE__		: List[Any]     =     {} if sp_model_kwargs is None else sp_model_kwargs

											super().__init__(
											    do_lower_case=_a     , remove_space=_a     , keep_accents=_a     , bos_token=_a     , eos_token=_a     , unk_token=_a     , sep_token=_a     , pad_token=_a     , cls_token=_a     , mask_token=_a     , sp_model_kwargs=self.sp_model_kwargs     , **_a     , )

											SCREAMING_SNAKE_CASE__		: Any     =     do_lower_case
											SCREAMING_SNAKE_CASE__		: Dict     =     remove_space
											SCREAMING_SNAKE_CASE__		: Dict     =     keep_accents
											SCREAMING_SNAKE_CASE__		: Dict     =     vocab_file

											SCREAMING_SNAKE_CASE__		: Optional[int]     =     spm.SentencePieceProcessor(**self.sp_model_kwargs					)
											self.sp_model.Load(_a					)



				@property
				def 			__magic_name__					(self					)      ->	Dict:



											"""simple docstring"""




											return len(self.sp_model					)



				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: str     =     {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					)      ->	List[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: str     =     self.__dict__.copy()
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     None
											return state



				def __setstate__(self     , SCREAMING_SNAKE_CASE__					)      ->	List[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Tuple     =     d

											# for backward compatibility
											if not hasattr(self     , """sp_model_kwargs"""					):
																		SCREAMING_SNAKE_CASE__		: Dict     =     {}

											SCREAMING_SNAKE_CASE__		: Dict     =     spm.SentencePieceProcessor(**self.sp_model_kwargs					)
											self.sp_model.Load(self.vocab_file					)



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__					)      ->	int:



											"""simple docstring"""




											if self.remove_space:
																		SCREAMING_SNAKE_CASE__		: List[Any]     =     """ """.join(inputs.strip().split()					)
											else:
																		SCREAMING_SNAKE_CASE__		: List[Any]     =     inputs
											SCREAMING_SNAKE_CASE__		: Tuple     =     outputs.replace("""``"""     , """\""""					).replace("""\'\'"""     , """\""""					)

											if not self.keep_accents:
																		SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     unicodedata.normalize("""NFKD"""     , _a					)
																		SCREAMING_SNAKE_CASE__		: List[str]     =     """""".join([c for c in outputs if not unicodedata.combining(_a					)]					)
											if self.do_lower_case:
																		SCREAMING_SNAKE_CASE__		: Dict     =     outputs.lower()

											return outputs



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__					)      ->	List[str]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[Any]     =     self.preprocess_text(_a					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     self.sp_model.encode(_a     , out_type=_a					)
											SCREAMING_SNAKE_CASE__		: int     =     []
											for piece in pieces:
																		if len(_a					) > 1 and piece[-1] == str(""","""					) and piece[-2].isdigit():
																									SCREAMING_SNAKE_CASE__		: Optional[int]     =     self.sp_model.EncodeAsPieces(piece[:-1].replace(_a     , """"""					)					)
																									if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
																																if len(cur_pieces[0]					) == 1:
																																							SCREAMING_SNAKE_CASE__		: List[str]     =     cur_pieces[1:]
																																else:
																																							SCREAMING_SNAKE_CASE__		: List[str]     =     cur_pieces[0][1:]
																									cur_pieces.append(piece[-1]					)
																									new_pieces.extend(_a					)
																		else:
																									new_pieces.append(_a					)

											return new_pieces



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__					)      ->	List[Any]:



											"""simple docstring"""




											return self.sp_model.PieceToId(_a					)



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__					)      ->	Dict:



											"""simple docstring"""




											return self.sp_model.IdToPiece(_a					)



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__					)      ->	Union[str, Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Dict     =     []
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     """"""
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     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
																									SCREAMING_SNAKE_CASE__		: str     =     True
																									SCREAMING_SNAKE_CASE__		: Optional[int]     =     []
																		else:
																									current_sub_tokens.append(_a					)
																									SCREAMING_SNAKE_CASE__		: str     =     False
											out_string += self.sp_model.decode(_a					)
											return out_string.strip()



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__ = None					)      ->	int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Dict     =     [self.sep_token_id]
											SCREAMING_SNAKE_CASE__		: Dict     =     [self.cls_token_id]
											if token_ids_a is None:
																		return cls + token_ids_a + sep
											return cls + token_ids_a + sep + token_ids_a + sep



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__ = None     , SCREAMING_SNAKE_CASE__ = False					)      ->	str:



											"""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 not None:
																		return [1] + ([0] * len(_a					)) + [1] + ([0] * len(_a					)) + [1]
											return [1] + ([0] * len(_a					)) + [1]



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__ = None					)      ->	Union[str, Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[str]     =     [self.sep_token_id]
											SCREAMING_SNAKE_CASE__		: List[Any]     =     [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 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__ = None					)      ->	Optional[Any]:



											"""simple docstring"""




											if not os.path.isdir(_a					):
																		logger.error(F'''Vocabulary path ({save_directory}) should be a directory'''					)
																		return
											SCREAMING_SNAKE_CASE__		: Tuple     =     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:
																									SCREAMING_SNAKE_CASE__		: Any     =     self.sp_model.serialized_model_proto()
																									fi.write(_a					)

											return (out_vocab_file,)






"""simple docstring"""




import copy
from collections import OrderedDict
from typing import Dict, Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


UpperCAmelCase__   :					Optional[Any]												=				logging.get_logger(__name__)

UpperCAmelCase__   :					str												=				{
    'facebook/detr-resnet-50': 'https://huggingface.co/facebook/detr-resnet-50/resolve/main/config.json',
    # See all DETR models at https://huggingface.co/models?filter=detr
}



class 			lowerCAmelCase_		(a__						):


				"""simple docstring"""



				__UpperCamelCase   :		List[Any]       =     '''detr'''
				__UpperCamelCase   :		List[Any]       =     ['''past_key_values''']
				__UpperCamelCase   :		List[Any]       =     {
				    '''hidden_size''': '''d_model''',
				    '''num_attention_heads''': '''encoder_attention_heads''',
				}



				def __init__(self     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=None     , SCREAMING_SNAKE_CASE__=3     , SCREAMING_SNAKE_CASE__=1_00     , SCREAMING_SNAKE_CASE__=6     , SCREAMING_SNAKE_CASE__=20_48     , SCREAMING_SNAKE_CASE__=8     , SCREAMING_SNAKE_CASE__=6     , SCREAMING_SNAKE_CASE__=20_48     , SCREAMING_SNAKE_CASE__=8     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__="relu"     , SCREAMING_SNAKE_CASE__=2_56     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.02     , SCREAMING_SNAKE_CASE__=1.0     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__="sine"     , SCREAMING_SNAKE_CASE__="resnet50"     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=1     , SCREAMING_SNAKE_CASE__=5     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=1     , SCREAMING_SNAKE_CASE__=1     , SCREAMING_SNAKE_CASE__=5     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=0.1     , **SCREAMING_SNAKE_CASE__     , )      ->	Optional[Any]:



											"""simple docstring"""




											if backbone_config is not None and use_timm_backbone:
																		raise ValueError("""You can't specify both `backbone_config` and `use_timm_backbone`."""					)

											if not use_timm_backbone:
																		if backbone_config is None:
																									logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""					)
																									SCREAMING_SNAKE_CASE__		: Any     =     CONFIG_MAPPING["""resnet"""](out_features=["""stage4"""]					)
																		elif isinstance(SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					):
																									SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     backbone_config.get("""model_type"""					)
																									SCREAMING_SNAKE_CASE__		: int     =     CONFIG_MAPPING[backbone_model_type]
																									SCREAMING_SNAKE_CASE__		: str     =     config_class.from_dict(SCREAMING_SNAKE_CASE__					)
																		# set timm attributes to None
																		SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__		: List[str]     =     None, None, None

											SCREAMING_SNAKE_CASE__		: Optional[int]     =     use_timm_backbone
											SCREAMING_SNAKE_CASE__		: Tuple     =     backbone_config
											SCREAMING_SNAKE_CASE__		: List[Any]     =     num_channels
											SCREAMING_SNAKE_CASE__		: Tuple     =     num_queries
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     d_model
											SCREAMING_SNAKE_CASE__		: str     =     encoder_ffn_dim
											SCREAMING_SNAKE_CASE__		: str     =     encoder_layers
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     encoder_attention_heads
											SCREAMING_SNAKE_CASE__		: Any     =     decoder_ffn_dim
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     decoder_layers
											SCREAMING_SNAKE_CASE__		: Dict     =     decoder_attention_heads
											SCREAMING_SNAKE_CASE__		: List[str]     =     dropout
											SCREAMING_SNAKE_CASE__		: Tuple     =     attention_dropout
											SCREAMING_SNAKE_CASE__		: Tuple     =     activation_dropout
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     activation_function
											SCREAMING_SNAKE_CASE__		: Any     =     init_std
											SCREAMING_SNAKE_CASE__		: Dict     =     init_xavier_std
											SCREAMING_SNAKE_CASE__		: Any     =     encoder_layerdrop
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     decoder_layerdrop
											SCREAMING_SNAKE_CASE__		: Dict     =     encoder_layers
											SCREAMING_SNAKE_CASE__		: List[str]     =     auxiliary_loss
											SCREAMING_SNAKE_CASE__		: List[Any]     =     position_embedding_type
											SCREAMING_SNAKE_CASE__		: List[str]     =     backbone
											SCREAMING_SNAKE_CASE__		: Dict     =     use_pretrained_backbone
											SCREAMING_SNAKE_CASE__		: Any     =     dilation
											# Hungarian matcher
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     class_cost
											SCREAMING_SNAKE_CASE__		: Tuple     =     bbox_cost
											SCREAMING_SNAKE_CASE__		: List[Any]     =     giou_cost
											# Loss coefficients
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     mask_loss_coefficient
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     dice_loss_coefficient
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     bbox_loss_coefficient
											SCREAMING_SNAKE_CASE__		: Any     =     giou_loss_coefficient
											SCREAMING_SNAKE_CASE__		: List[str]     =     eos_coefficient
											super().__init__(is_encoder_decoder=SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)



				@property
				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											return self.encoder_attention_heads



				@property
				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											return self.d_model



				@classmethod
				def 			__magic_name__					(cls     , SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)      ->	Tuple:



											"""simple docstring"""




											return cls(backbone_config=SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)



				def 			__magic_name__					(self					)      ->	Dict[str, any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Dict     =     copy.deepcopy(self.__dict__					)
											if output["backbone_config"] is not None:
																		SCREAMING_SNAKE_CASE__		: Any     =     self.backbone_config.to_dict()
											SCREAMING_SNAKE_CASE__		: Dict     =     self.__class__.model_type
											return output






class 			lowerCAmelCase_		(a__						):


				"""simple docstring"""



				__UpperCamelCase   :		Any       =     version.parse('''1.11'''						)



				@property
				def 			__magic_name__					(self					)      ->	Mapping[str, Mapping[int, str]]:



											"""simple docstring"""




											return OrderedDict(
											    [
											        ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
											        ("""pixel_mask""", {0: """batch"""}),
											    ]					)



				@property
				def 			__magic_name__					(self					)      ->	float:



											"""simple docstring"""




											return 1E-5



				@property
				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											return 12




