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

import sentencepiece as spm

from ...tokenization_utils import AddedToken, BatchEncoding, PreTrainedTokenizer
from ...utils import logging


__a  :     Any       =  logging.get_logger(__name__)

__a  :     Tuple       =  """▁"""

__a  :     Any       =  {"""vocab_file""": """sentencepiece.bpe.model"""}

__a  :     List[str]       =  {
    """vocab_file""": {
        """facebook/mbart-large-en-ro""": (
            """https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/sentencepiece.bpe.model"""
        ),
        """facebook/mbart-large-cc25""": (
            """https://huggingface.co/facebook/mbart-large-cc25/resolve/main/sentencepiece.bpe.model"""
        ),
    }
}

__a  :     Tuple       =  {
    """facebook/mbart-large-en-ro""": 1_0_2_4,
    """facebook/mbart-large-cc25""": 1_0_2_4,
}

# fmt: off
__a  :     str       =  ["""ar_AR""", """cs_CZ""", """de_DE""", """en_XX""", """es_XX""", """et_EE""", """fi_FI""", """fr_XX""", """gu_IN""", """hi_IN""", """it_IT""", """ja_XX""", """kk_KZ""", """ko_KR""", """lt_LT""", """lv_LV""", """my_MM""", """ne_NP""", """nl_XX""", """ro_RO""", """ru_RU""", """si_LK""", """tr_TR""", """vi_VN""", """zh_CN"""]




class    _UpperCamelCase      (						_UpperCAmelCase ):


			"""simple docstring"""






			__a  : int			 =						VOCAB_FILES_NAMES
			__a  : Optional[Any]			 =						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			__a  : int			 =						PRETRAINED_VOCAB_FILES_MAP
			__a  : Optional[Any]			 =						['''input_ids''', '''attention_mask''']

			__a  : List[int]			 =						[]
			__a  : List[int]			 =						[]





			def __init__(   self      ,   lowerCAmelCase__      ,   lowerCAmelCase__="<s>"      ,   lowerCAmelCase__="</s>"      ,   lowerCAmelCase__="</s>"      ,   lowerCAmelCase__="<s>"      ,   lowerCAmelCase__="<unk>"      ,   lowerCAmelCase__="<pad>"      ,   lowerCAmelCase__="<mask>"      ,   lowerCAmelCase__=None      ,   lowerCAmelCase__=None      ,   lowerCAmelCase__=None      ,   lowerCAmelCase__ = None      ,   lowerCAmelCase__=None      ,   **lowerCAmelCase__      ,   )     ->					Optional[Any]:

							'''simple docstring'''


							__lowercase 			=							AddedToken(lowerCAmelCase__      ,   lstrip=lowerCAmelCase__      ,   rstrip=lowerCAmelCase__						) if isinstance(lowerCAmelCase__      ,   lowerCAmelCase__						) else mask_token

							__lowercase 			=							{} if sp_model_kwargs is None else sp_model_kwargs

							super().__init__(
							    bos_token=lowerCAmelCase__      ,   eos_token=lowerCAmelCase__      ,   unk_token=lowerCAmelCase__      ,   sep_token=lowerCAmelCase__      ,   cls_token=lowerCAmelCase__      ,   pad_token=lowerCAmelCase__      ,   mask_token=lowerCAmelCase__      ,   tokenizer_file=lowerCAmelCase__      ,   src_lang=lowerCAmelCase__      ,   tgt_lang=lowerCAmelCase__      ,   additional_special_tokens=lowerCAmelCase__      ,   sp_model_kwargs=self.sp_model_kwargs      ,   **lowerCAmelCase__      ,   )

							__lowercase 			=							spm.SentencePieceProcessor(**self.sp_model_kwargs						)
							self.sp_model.Load(str(lowerCAmelCase__						)						)
							__lowercase 			=							vocab_file

							# Original fairseq vocab and spm vocab must be "aligned":
							# Vocab    |    0    |    1    |   2    |    3    |  4  |  5  |  6  |   7   |   8   |  9
							# -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ----
							# fairseq  | '<s>'   | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's'   | '▁de' | '-'
							# spm      | '<unk>' | '<s>'   | '</s>' | ','     | '.' | '▁' | 's' | '▁de' | '-'   | '▁a'

							# Mimic fairseq token-to-id alignment for the first 4 token
							__lowercase 			=							{'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3}

							# The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab
							__lowercase 			=							1

							__lowercase 			=							len(self.sp_model						)
							__lowercase 			=							{
							    code: self.sp_model_size + i + self.fairseq_offset for i, code in enumerate(lowerCAmelCase__						)
							}
							__lowercase 			=							{v: k for k, v in self.lang_code_to_id.items()}
							__lowercase 			=							len(self.sp_model						) + len(self.lang_code_to_id						) + self.fairseq_offset

							self.fairseq_tokens_to_ids.update(self.lang_code_to_id						)
							__lowercase 			=							{v: k for k, v in self.fairseq_tokens_to_ids.items()}
							__lowercase 			=							list(self.lang_code_to_id.keys()						)

							if additional_special_tokens is not None:
											# Only add those special tokens if they are not already there.
											self._additional_special_tokens.extend(
											    [t for t in additional_special_tokens if t not in self._additional_special_tokens]						)

							__lowercase 			=							src_lang if src_lang is not None else '''en_XX'''
							__lowercase 			=							self.lang_code_to_id[self._src_lang]
							__lowercase 			=							tgt_lang
							self.set_src_lang_special_tokens(self._src_lang						)





			def __getstate__(   self						)     ->					Optional[int]:

							'''simple docstring'''


							__lowercase 			=							self.__dict__.copy()
							__lowercase 			=							None
							__lowercase 			=							self.sp_model.serialized_model_proto()
							return state





			def __setstate__(   self      ,   lowerCAmelCase__						)     ->					int:

							'''simple docstring'''


							__lowercase 			=							d

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

							__lowercase 			=							spm.SentencePieceProcessor(**self.sp_model_kwargs						)
							self.sp_model.LoadFromSerializedProto(self.sp_model_proto						)





			@property
			def        _SCREAMING_SNAKE_CASE			(   self						)     ->					Union[str, Any]:

							'''simple docstring'''


							return len(self.sp_model						) + len(self.lang_code_to_id						) + self.fairseq_offset + 1  # Plus 1 for the mask token





			@property
			def        _SCREAMING_SNAKE_CASE			(   self						)     ->					str:

							'''simple docstring'''


							return self._src_lang





			@src_lang.setter
			def        _SCREAMING_SNAKE_CASE			(   self      ,   lowerCAmelCase__						)     ->					None:

							'''simple docstring'''


							__lowercase 			=							new_src_lang
							self.set_src_lang_special_tokens(self._src_lang						)





			def        _SCREAMING_SNAKE_CASE			(   self      ,   lowerCAmelCase__      ,   lowerCAmelCase__ = None      ,   lowerCAmelCase__ = False						)     ->					List[int]:

							'''simple docstring'''


							if already_has_special_tokens:
											return super().get_special_tokens_mask(
											    token_ids_a=lowerCAmelCase__      ,   token_ids_a=lowerCAmelCase__      ,   already_has_special_tokens=lowerCAmelCase__						)

							__lowercase 			=							[1] * len(self.prefix_tokens						)
							__lowercase 			=							[1] * len(self.suffix_tokens						)
							if token_ids_a is None:
											return prefix_ones + ([0] * len(lowerCAmelCase__						)) + suffix_ones
							return prefix_ones + ([0] * len(lowerCAmelCase__						)) + ([0] * len(lowerCAmelCase__						)) + suffix_ones





			def        _SCREAMING_SNAKE_CASE			(   self      ,   lowerCAmelCase__      ,   lowerCAmelCase__ = None						)     ->					List[int]:

							'''simple docstring'''


							if token_ids_a is None:
											return self.prefix_tokens + token_ids_a + self.suffix_tokens
							# We don't expect to process pairs, but leave the pair logic for API consistency
							return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens





			def        _SCREAMING_SNAKE_CASE			(   self      ,   lowerCAmelCase__      ,   lowerCAmelCase__ = None						)     ->					List[int]:

							'''simple docstring'''


							__lowercase 			=							[self.sep_token_id]
							__lowercase 			=							[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      ,   lowerCAmelCase__      ,   lowerCAmelCase__      ,   lowerCAmelCase__      ,   lowerCAmelCase__      ,   **lowerCAmelCase__						)     ->					str:

							'''simple docstring'''


							if src_lang is None or tgt_lang is None:
											raise ValueError('''Translation requires a `src_lang` and a `tgt_lang` for this model'''						)
							__lowercase 			=							src_lang
							__lowercase 			=							self(lowerCAmelCase__      ,   add_special_tokens=lowerCAmelCase__      ,   return_tensors=lowerCAmelCase__      ,   **lowerCAmelCase__						)
							__lowercase 			=							self.convert_tokens_to_ids(lowerCAmelCase__						)
							__lowercase 			=							tgt_lang_id
							return inputs





			def        _SCREAMING_SNAKE_CASE			(   self						)     ->					Optional[int]:

							'''simple docstring'''


							__lowercase 			=							{self.convert_ids_to_tokens(lowerCAmelCase__						): i for i in range(self.vocab_size						)}
							vocab.update(self.added_tokens_encoder						)
							return vocab





			def        _SCREAMING_SNAKE_CASE			(   self      ,   lowerCAmelCase__						)     ->					List[str]:

							'''simple docstring'''


							return self.sp_model.encode(lowerCAmelCase__      ,   out_type=lowerCAmelCase__						)





			def        _SCREAMING_SNAKE_CASE			(   self      ,   lowerCAmelCase__						)     ->					Optional[int]:

							'''simple docstring'''


							if token in self.fairseq_tokens_to_ids:
											return self.fairseq_tokens_to_ids[token]
							__lowercase 			=							self.sp_model.PieceToId(lowerCAmelCase__						)

							# Need to return unknown token if the SP model returned 0
							return spm_id + self.fairseq_offset if spm_id else self.unk_token_id





			def        _SCREAMING_SNAKE_CASE			(   self      ,   lowerCAmelCase__						)     ->					Dict:

							'''simple docstring'''


							if index in self.fairseq_ids_to_tokens:
											return self.fairseq_ids_to_tokens[index]
							return self.sp_model.IdToPiece(index - self.fairseq_offset						)





			def        _SCREAMING_SNAKE_CASE			(   self      ,   lowerCAmelCase__						)     ->					Dict:

							'''simple docstring'''


							__lowercase 			=							''''''.join(lowerCAmelCase__						).replace(lowerCAmelCase__      ,   ''' '''						).strip()
							return out_string





			def        _SCREAMING_SNAKE_CASE			(   self      ,   lowerCAmelCase__      ,   lowerCAmelCase__ = None						)     ->					Tuple[str]:

							'''simple docstring'''


							if not os.path.isdir(lowerCAmelCase__						):
											logger.error(F"Vocabulary path ({save_directory}) should be a directory"						)
											return
							__lowercase 			=							os.path.join(
							    lowerCAmelCase__      ,   (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']						)

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

							return (out_vocab_file,)





			def        _SCREAMING_SNAKE_CASE			(   self      ,   lowerCAmelCase__      ,   lowerCAmelCase__ = "en_XX"      ,   lowerCAmelCase__ = None      ,   lowerCAmelCase__ = "ro_RO"      ,   **lowerCAmelCase__      ,   )     ->					BatchEncoding:

							'''simple docstring'''


							__lowercase 			=							src_lang
							__lowercase 			=							tgt_lang
							return super().prepare_seqaseq_batch(lowerCAmelCase__      ,   lowerCAmelCase__      ,   **lowerCAmelCase__						)





			def        _SCREAMING_SNAKE_CASE			(   self						)     ->					List[str]:

							'''simple docstring'''


							return self.set_src_lang_special_tokens(self.src_lang						)





			def        _SCREAMING_SNAKE_CASE			(   self						)     ->					List[Any]:

							'''simple docstring'''


							return self.set_tgt_lang_special_tokens(self.tgt_lang						)





			def        _SCREAMING_SNAKE_CASE			(   self      ,   lowerCAmelCase__						)     ->					None:

							'''simple docstring'''


							__lowercase 			=							self.lang_code_to_id[src_lang]
							__lowercase 			=							[]
							__lowercase 			=							[self.eos_token_id, self.cur_lang_code]





			def        _SCREAMING_SNAKE_CASE			(   self      ,   lowerCAmelCase__						)     ->					None:

							'''simple docstring'''


							__lowercase 			=							self.lang_code_to_id[lang]
							__lowercase 			=							[]
							__lowercase 			=							[self.eos_token_id, self.cur_lang_code]
import copy

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


__a  :     int       =  logging.get_logger(__name__)

__a  :     str       =  {
    """ut/deta""": """https://huggingface.co/ut/deta/resolve/main/config.json""",
}




class    _UpperCamelCase      (						_UpperCAmelCase ):


			"""simple docstring"""






			__a  : Optional[int]			 =						'''deta'''
			__a  : Optional[int]			 =						{
			    '''hidden_size''': '''d_model''',
			    '''num_attention_heads''': '''encoder_attention_heads''',
			}





			def __init__(   self      ,   lowerCAmelCase__=None      ,   lowerCAmelCase__=9_00      ,   lowerCAmelCase__=20_48      ,   lowerCAmelCase__=6      ,   lowerCAmelCase__=20_48      ,   lowerCAmelCase__=8      ,   lowerCAmelCase__=6      ,   lowerCAmelCase__=10_24      ,   lowerCAmelCase__=8      ,   lowerCAmelCase__=0.0      ,   lowerCAmelCase__=True      ,   lowerCAmelCase__="relu"      ,   lowerCAmelCase__=2_56      ,   lowerCAmelCase__=0.1      ,   lowerCAmelCase__=0.0      ,   lowerCAmelCase__=0.0      ,   lowerCAmelCase__=0.02      ,   lowerCAmelCase__=1.0      ,   lowerCAmelCase__=True      ,   lowerCAmelCase__=False      ,   lowerCAmelCase__="sine"      ,   lowerCAmelCase__=5      ,   lowerCAmelCase__=4      ,   lowerCAmelCase__=4      ,   lowerCAmelCase__=True      ,   lowerCAmelCase__=3_00      ,   lowerCAmelCase__=True      ,   lowerCAmelCase__=True      ,   lowerCAmelCase__=1      ,   lowerCAmelCase__=5      ,   lowerCAmelCase__=2      ,   lowerCAmelCase__=1      ,   lowerCAmelCase__=1      ,   lowerCAmelCase__=5      ,   lowerCAmelCase__=2      ,   lowerCAmelCase__=0.1      ,   lowerCAmelCase__=0.25      ,   **lowerCAmelCase__      ,   )     ->					List[Any]:

							'''simple docstring'''


							if backbone_config is None:
											logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.'''						)
											__lowercase 			=							CONFIG_MAPPING['''resnet'''](out_features=['''stage2''', '''stage3''', '''stage4''']						)
							else:
											if isinstance(lowerCAmelCase__      ,   lowerCAmelCase__						):
															__lowercase 			=							backbone_config.pop('''model_type'''						)
															__lowercase 			=							CONFIG_MAPPING[backbone_model_type]
															__lowercase 			=							config_class.from_dict(lowerCAmelCase__						)

							__lowercase 			=							backbone_config
							__lowercase 			=							num_queries
							__lowercase 			=							max_position_embeddings
							__lowercase 			=							d_model
							__lowercase 			=							encoder_ffn_dim
							__lowercase 			=							encoder_layers
							__lowercase 			=							encoder_attention_heads
							__lowercase 			=							decoder_ffn_dim
							__lowercase 			=							decoder_layers
							__lowercase 			=							decoder_attention_heads
							__lowercase 			=							dropout
							__lowercase 			=							attention_dropout
							__lowercase 			=							activation_dropout
							__lowercase 			=							activation_function
							__lowercase 			=							init_std
							__lowercase 			=							init_xavier_std
							__lowercase 			=							encoder_layerdrop
							__lowercase 			=							auxiliary_loss
							__lowercase 			=							position_embedding_type
							# deformable attributes
							__lowercase 			=							num_feature_levels
							__lowercase 			=							encoder_n_points
							__lowercase 			=							decoder_n_points
							__lowercase 			=							two_stage
							__lowercase 			=							two_stage_num_proposals
							__lowercase 			=							with_box_refine
							__lowercase 			=							assign_first_stage
							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
							__lowercase 			=							class_cost
							__lowercase 			=							bbox_cost
							__lowercase 			=							giou_cost
							# Loss coefficients
							__lowercase 			=							mask_loss_coefficient
							__lowercase 			=							dice_loss_coefficient
							__lowercase 			=							bbox_loss_coefficient
							__lowercase 			=							giou_loss_coefficient
							__lowercase 			=							eos_coefficient
							__lowercase 			=							focal_alpha
							super().__init__(is_encoder_decoder=lowerCAmelCase__      ,   **lowerCAmelCase__						)





			@property
			def        _SCREAMING_SNAKE_CASE			(   self						)     ->					int:

							'''simple docstring'''


							return self.encoder_attention_heads





			@property
			def        _SCREAMING_SNAKE_CASE			(   self						)     ->					int:

							'''simple docstring'''


							return self.d_model





			def        _SCREAMING_SNAKE_CASE			(   self						)     ->					List[str]:

							'''simple docstring'''


							__lowercase 			=							copy.deepcopy(self.__dict__						)
							__lowercase 			=							self.backbone_config.to_dict()
							__lowercase 			=							self.__class__.model_type
							return output