
import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


_SCREAMING_SNAKE_CASE    :      Any          =			'▁'

_SCREAMING_SNAKE_CASE    :      Any          =			{'vocab_file': 'spiece.model'}

_SCREAMING_SNAKE_CASE    :      str          =			{
    'vocab_file': {'google/pegasus-xsum': 'https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model'}
}

_SCREAMING_SNAKE_CASE    :      Optional[int]          =			{
    'google/pegasus-xsum': 5_1_2,
}


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







class  UpperCamelCase__		(    SCREAMING_SNAKE_CASE_   ):







	'''simple docstring'''
	_lowerCAmelCase							       =				VOCAB_FILES_NAMES

	_lowerCAmelCase							       =				VOCAB_FILES_NAMES
	_lowerCAmelCase							       =				PRETRAINED_VOCAB_FILES_MAP
	_lowerCAmelCase							       =				PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
	_lowerCAmelCase							       =				["input_ids", "attention_mask"]





	def __init__(			self				,       UpperCamelCase__				,       UpperCamelCase__="<pad>"				,       UpperCamelCase__="</s>"				,       UpperCamelCase__="<unk>"				,       UpperCamelCase__="<mask_2>"				,       UpperCamelCase__="<mask_1>"				,       UpperCamelCase__=None				,       UpperCamelCase__=103				,       UpperCamelCase__ = None				,       **UpperCamelCase__				,       ):
					A__  :	Optional[Any]         =							offset
					if additional_special_tokens is not None:
									if not isinstance(UpperCamelCase__				,       UpperCamelCase__		):
													raise TypeError(
													    F"additional_special_tokens should be of type {type(UpperCamelCase__		)}, but is"
													    F" {type(UpperCamelCase__		)}"		)

									A__  :	Any         =							(
									    ([mask_token_sent] + additional_special_tokens)
									    if mask_token_sent not in additional_special_tokens and mask_token_sent is not None
									    else additional_special_tokens
									)
									# fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken
									additional_special_tokens_extended += [
									    F"<unk_{i}>" for i in range(len(UpperCamelCase__		)				,       self.offset - 1		)
									]

									if len(set(UpperCamelCase__		)		) != len(UpperCamelCase__		):
													raise ValueError(
													    '''Please make sure that the provided additional_special_tokens do not contain an incorrectly'''
													    F" shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}."		)
									A__  :	Optional[Any]         =							additional_special_tokens_extended
					else:
									A__  :	Dict         =							[mask_token_sent] if mask_token_sent is not None else []
									additional_special_tokens += [F"<unk_{i}>" for i in range(2				,       self.offset		)]

					A__  :	Optional[int]         =							{} if sp_model_kwargs is None else sp_model_kwargs

					super().__init__(
					    eos_token=UpperCamelCase__				,       unk_token=UpperCamelCase__				,       mask_token=UpperCamelCase__				,       pad_token=UpperCamelCase__				,       mask_token_sent=UpperCamelCase__				,       offset=UpperCamelCase__				,       additional_special_tokens=UpperCamelCase__				,       sp_model_kwargs=self.sp_model_kwargs				,       **UpperCamelCase__				,       )
					A__  :	List[Any]         =							mask_token_sent
					A__  :	Any         =							vocab_file
					A__  :	str         =							spm.SentencePieceProcessor(**self.sp_model_kwargs		)
					self.sp_model.Load(UpperCamelCase__		)

					# add special tokens to encoder dict
					A__  :	Dict[int, str]         =							{
					    0: self.pad_token,
					    1: self.eos_token,
					}

					if self.mask_token_sent is not None:
									self.encoder.update(
									    {
									        2: self.mask_token_sent,
									        3: self.mask_token,
									    }		)

					if self.offset > 0:
									# entries 2-104 are only used for pretraining and called <mask_1>, <mask_2>, unk_2, ...unk_102
									# mask_token_sent is already added to list -> so start at 1
									self.encoder.update({i + 3: additional_special_tokens[i] for i in range(1				,       self.offset - 1		)}		)

					A__  :	Dict[str, int]         =							{v: k for k, v in self.encoder.items()}





	@property
	def   __snake_case				(			self		):
					return len(self.sp_model		) + self.offset





	def   __snake_case				(			self		):
					A__  :	List[str]         =							{self.convert_ids_to_tokens(UpperCamelCase__		): i for i in range(self.vocab_size		)}
					vocab.update(self.added_tokens_encoder		)
					return vocab





	def __getstate__(			self		):
					A__  :	List[str]         =							self.__dict__.copy()
					A__  :	str         =							None
					return state





	def __setstate__(			self				,       UpperCamelCase__		):
					A__  :	Dict         =							d

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

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





	def   __snake_case				(			self				,       UpperCamelCase__		):
					return self.sp_model.encode(UpperCamelCase__				,       out_type=UpperCamelCase__		)





	def   __snake_case				(			self				,       UpperCamelCase__		):
					if token in self.decoder:
									return self.decoder[token]
					elif token in self.added_tokens_decoder:
									return self.added_tokens_decoder[token]
					A__  :	str         =							self.sp_model.piece_to_id(UpperCamelCase__		)
					return sp_id + self.offset





	def   __snake_case				(			self				,       UpperCamelCase__		):
					if index in self.encoder:
									return self.encoder[index]
					elif index in self.added_tokens_encoder:
									return self.added_tokens_encoder[index]
					else:
									A__  :	Dict         =							self.sp_model.IdToPiece(index - self.offset		)
					return token





	def   __snake_case				(			self				,       UpperCamelCase__		):
					A__  :	Optional[int]         =							[]
					A__  :	Union[str, Any]         =							''''''
					for token in tokens:
									# make sure that special tokens are not decoded using sentencepiece model
									if token in self.all_special_tokens:
													out_string += self.sp_model.decode(UpperCamelCase__		) + token
													A__  :	int         =							[]
									else:
													current_sub_tokens.append(UpperCamelCase__		)
					out_string += self.sp_model.decode(UpperCamelCase__		)
					return out_string.strip()





	def   __snake_case				(			self				,       UpperCamelCase__=False		):
					return 1





	def   __snake_case				(			self				,       UpperCamelCase__		):
					A__  :	Any         =							set(self.all_special_ids		)  # call it once instead of inside list comp
					all_special_ids.remove(self.unk_token_id		)  # <unk> is only sometimes special

					return [1 if x in all_special_ids else 0 for x in seq]





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__ = None				,       UpperCamelCase__ = False		):
					if already_has_special_tokens:
									return self._special_token_mask(UpperCamelCase__		)
					elif token_ids_a is None:
									return self._special_token_mask(UpperCamelCase__		) + [1]
					else:
									return self._special_token_mask(token_ids_a + token_ids_a		) + [1]





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__=None		):
					if token_ids_a is None:
									return token_ids_a + [self.eos_token_id]
					# We don't expect to process pairs, but leave the pair logic for API consistency
					return token_ids_a + token_ids_a + [self.eos_token_id]





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__ = None		):
					if not os.path.isdir(UpperCamelCase__		):
									logger.error(F"Vocabulary path ({save_directory}) should be a directory"		)
									return
					A__  :	Dict         =							os.path.join(
					    UpperCamelCase__				,       (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']		)

					if os.path.abspath(self.vocab_file		) != os.path.abspath(UpperCamelCase__		) and os.path.isfile(self.vocab_file		):
									copyfile(self.vocab_file				,       UpperCamelCase__		)
					elif not os.path.isfile(self.vocab_file		):
									with open(UpperCamelCase__				,       '''wb'''		) as fi:
													A__  :	List[Any]         =							self.sp_model.serialized_model_proto()
													fi.write(UpperCamelCase__		)

					return (out_vocab_file,)

import copy

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


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

_SCREAMING_SNAKE_CASE    :      int          =			{
    'SenseTime/deformable-detr': 'https://huggingface.co/sensetime/deformable-detr/resolve/main/config.json',
    # See all Deformable DETR models at https://huggingface.co/models?filter=deformable-detr
}







class  UpperCamelCase__		(    SCREAMING_SNAKE_CASE_   ):







	'''simple docstring'''
	_lowerCAmelCase							       =				"deformable_detr"
	_lowerCAmelCase							       =				{
	    "hidden_size": "d_model",
	    "num_attention_heads": "encoder_attention_heads",
	}





	def __init__(			self				,       UpperCamelCase__=True				,       UpperCamelCase__=None				,       UpperCamelCase__=3				,       UpperCamelCase__=300				,       UpperCamelCase__=1024				,       UpperCamelCase__=6				,       UpperCamelCase__=1024				,       UpperCamelCase__=8				,       UpperCamelCase__=6				,       UpperCamelCase__=1024				,       UpperCamelCase__=8				,       UpperCamelCase__=0.0				,       UpperCamelCase__=True				,       UpperCamelCase__="relu"				,       UpperCamelCase__=256				,       UpperCamelCase__=0.1				,       UpperCamelCase__=0.0				,       UpperCamelCase__=0.0				,       UpperCamelCase__=0.0_2				,       UpperCamelCase__=1.0				,       UpperCamelCase__=True				,       UpperCamelCase__=False				,       UpperCamelCase__="sine"				,       UpperCamelCase__="resnet50"				,       UpperCamelCase__=True				,       UpperCamelCase__=False				,       UpperCamelCase__=4				,       UpperCamelCase__=4				,       UpperCamelCase__=4				,       UpperCamelCase__=False				,       UpperCamelCase__=300				,       UpperCamelCase__=False				,       UpperCamelCase__=1				,       UpperCamelCase__=5				,       UpperCamelCase__=2				,       UpperCamelCase__=1				,       UpperCamelCase__=1				,       UpperCamelCase__=5				,       UpperCamelCase__=2				,       UpperCamelCase__=0.1				,       UpperCamelCase__=0.2_5				,       UpperCamelCase__=False				,       **UpperCamelCase__				,       ):
					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.'''		)
													A__  :	int         =							CONFIG_MAPPING['''resnet'''](out_features=['''stage4''']		)
									elif isinstance(UpperCamelCase__				,       UpperCamelCase__		):
													A__  :	Union[str, Any]         =							backbone_config.get('''model_type'''		)
													A__  :	Union[str, Any]         =							CONFIG_MAPPING[backbone_model_type]
													A__  :	Optional[int]         =							config_class.from_dict(UpperCamelCase__		)
					A__  :	Tuple         =							use_timm_backbone
					A__  :	int         =							backbone_config
					A__  :	List[Any]         =							num_channels
					A__  :	List[Any]         =							num_queries
					A__  :	str         =							max_position_embeddings
					A__  :	Tuple         =							d_model
					A__  :	int         =							encoder_ffn_dim
					A__  :	Union[str, Any]         =							encoder_layers
					A__  :	Optional[Any]         =							encoder_attention_heads
					A__  :	List[Any]         =							decoder_ffn_dim
					A__  :	Tuple         =							decoder_layers
					A__  :	Optional[Any]         =							decoder_attention_heads
					A__  :	List[str]         =							dropout
					A__  :	str         =							attention_dropout
					A__  :	List[Any]         =							activation_dropout
					A__  :	Any         =							activation_function
					A__  :	Optional[Any]         =							init_std
					A__  :	Union[str, Any]         =							init_xavier_std
					A__  :	Union[str, Any]         =							encoder_layerdrop
					A__  :	Optional[int]         =							auxiliary_loss
					A__  :	str         =							position_embedding_type
					A__  :	List[Any]         =							backbone
					A__  :	Optional[Any]         =							use_pretrained_backbone
					A__  :	Any         =							dilation
					# deformable attributes
					A__  :	List[Any]         =							num_feature_levels
					A__  :	List[str]         =							encoder_n_points
					A__  :	int         =							decoder_n_points
					A__  :	List[Any]         =							two_stage
					A__  :	Dict         =							two_stage_num_proposals
					A__  :	Optional[int]         =							with_box_refine
					if two_stage is True and with_box_refine is False:
									raise ValueError('''If two_stage is True, with_box_refine must be True.'''		)
					# Hungarian matcher
					A__  :	List[str]         =							class_cost
					A__  :	List[Any]         =							bbox_cost
					A__  :	Any         =							giou_cost
					# Loss coefficients
					A__  :	List[str]         =							mask_loss_coefficient
					A__  :	Union[str, Any]         =							dice_loss_coefficient
					A__  :	List[Any]         =							bbox_loss_coefficient
					A__  :	Tuple         =							giou_loss_coefficient
					A__  :	Optional[Any]         =							eos_coefficient
					A__  :	List[Any]         =							focal_alpha
					A__  :	List[str]         =							disable_custom_kernels
					super().__init__(is_encoder_decoder=UpperCamelCase__				,       **UpperCamelCase__		)





	@property
	def   __snake_case				(			self		):
					return self.encoder_attention_heads





	@property
	def   __snake_case				(			self		):
					return self.d_model





	def   __snake_case				(			self		):
					A__  :	List[str]         =							copy.deepcopy(self.__dict__		)
					if self.backbone_config is not None:
									A__  :	Tuple         =							self.backbone_config.to_dict()
					A__  :	Optional[int]         =							self.__class__.model_type
					return output