



"""simple docstring"""






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


lowerCAmelCase__              =							logging.get_logger(__name__)

lowerCAmelCase__              =							'''▁'''

lowerCAmelCase__              =							{'''vocab_file''': '''sentencepiece.bpe.model'''}

lowerCAmelCase__              =							{
    '''vocab_file''': {
        '''facebook/mbart-large-50-one-to-many-mmt''': (
            '''https://huggingface.co/facebook/mbart-large-50-one-to-many-mmt/resolve/main/sentencepiece.bpe.model'''
        ),
    }
}

lowerCAmelCase__              =							{
    '''facebook/mbart-large-50-one-to-many-mmt''': 1024,
}

# fmt: off
lowerCAmelCase__              =							['''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''', '''af_ZA''', '''az_AZ''', '''bn_IN''', '''fa_IR''', '''he_IL''', '''hr_HR''', '''id_ID''', '''ka_GE''', '''km_KH''', '''mk_MK''', '''ml_IN''', '''mn_MN''', '''mr_IN''', '''pl_PL''', '''ps_AF''', '''pt_XX''', '''sv_SE''', '''sw_KE''', '''ta_IN''', '''te_IN''', '''th_TH''', '''tl_XX''', '''uk_UA''', '''ur_PK''', '''xh_ZA''', '''gl_ES''', '''sl_SI''']






class 			__snake_case			(      _lowercase):
  snake_case__							:  Optional[Any]         =		VOCAB_FILES_NAMES
  snake_case__							:  Optional[Any]         =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  snake_case__							:  Optional[Any]         =		PRETRAINED_VOCAB_FILES_MAP
  snake_case__							:  Optional[Any]         =		["input_ids", "attention_mask"]

  snake_case__							:  List[int]         =		[]
  snake_case__							:  List[int]         =		[]




  def __init__(					self	:		Dict		,   __lowerCAmelCase	:		Any		,   __lowerCAmelCase	:		Dict=None		,   __lowerCAmelCase	:		Optional[Any]=None		,   __lowerCAmelCase	:		Union[str, Any]="</s>"		,   __lowerCAmelCase	:		str="</s>"		,   __lowerCAmelCase	:		Union[str, Any]="<s>"		,   __lowerCAmelCase	:		Any="<unk>"		,   __lowerCAmelCase	:		List[Any]="<pad>"		,   __lowerCAmelCase	:		List[str]="<mask>"		,   __lowerCAmelCase	:		Optional[Dict[str, Any]] = None		,   **__lowerCAmelCase	:		Optional[int]		,   ):






         """simple docstring"""






         _lowerCamelCase		:							Optional[int]            =					AddedToken(__lowerCAmelCase		,   lstrip=__lowerCAmelCase		,   rstrip=__lowerCAmelCase						) if isinstance(__lowerCAmelCase		,   __lowerCAmelCase						) else mask_token

         _lowerCamelCase		:							Dict            =					{} if sp_model_kwargs is None else sp_model_kwargs

         _lowerCamelCase		:							List[Any]            =					kwargs.get('''additional_special_tokens'''		,   []						)
         kwargs["additional_special_tokens"] += [
             code for code in FAIRSEQ_LANGUAGE_CODES if code not in kwargs["additional_special_tokens"]
         ]

         super().__init__(
             src_lang=__lowerCAmelCase		,   tgt_lang=__lowerCAmelCase		,   eos_token=__lowerCAmelCase		,   unk_token=__lowerCAmelCase		,   sep_token=__lowerCAmelCase		,   cls_token=__lowerCAmelCase		,   pad_token=__lowerCAmelCase		,   mask_token=__lowerCAmelCase		,   sp_model_kwargs=self.sp_model_kwargs		,   **__lowerCAmelCase		,   )

         _lowerCamelCase		:							Optional[Any]            =					spm.SentencePieceProcessor(**self.sp_model_kwargs						)
         self.sp_model.Load(str(__lowerCAmelCase						)						)
         _lowerCamelCase		:							List[str]            =					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
         _lowerCamelCase		:							Optional[int]            =					{'''<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
         _lowerCamelCase		:							Tuple            =					1

         _lowerCamelCase		:							List[Any]            =					len(self.sp_model						)
         _lowerCamelCase		:							Any            =					{
             code: self.sp_model_size + i + self.fairseq_offset for i, code in enumerate(__lowerCAmelCase						)
         }
         _lowerCamelCase		:							List[str]            =					{v: k for k, v in self.lang_code_to_id.items()}
         _lowerCamelCase		:							Tuple            =					len(self.sp_model						) + len(self.lang_code_to_id						) + self.fairseq_offset

         self.fairseq_tokens_to_ids.update(self.lang_code_to_id						)
         _lowerCamelCase		:							Any            =					{v: k for k, v in self.fairseq_tokens_to_ids.items()}

         _lowerCamelCase		:							Tuple            =					src_lang if src_lang is not None else '''en_XX'''
         _lowerCamelCase		:							Optional[int]            =					self.lang_code_to_id[self._src_lang]
         _lowerCamelCase		:							List[Any]            =					tgt_lang
         self.set_src_lang_special_tokens(self._src_lang						)




  @property
  def 		SCREAMING_SNAKE_CASE							(					self	:		str						):






         """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	:		Dict						):






         """simple docstring"""






         return self._src_lang




  @src_lang.setter
  def 		SCREAMING_SNAKE_CASE							(					self	:		int		,   __lowerCAmelCase	:		str						):






         """simple docstring"""






         _lowerCamelCase		:							List[Any]            =					new_src_lang
         self.set_src_lang_special_tokens(self._src_lang						)




  def __getstate__(					self	:		List[str]						):






         """simple docstring"""






         _lowerCamelCase		:							List[str]            =					self.__dict__.copy()
         _lowerCamelCase		:							List[Any]            =					None
         return state




  def __setstate__(					self	:		Optional[int]		,   __lowerCAmelCase	:		Dict						):






         """simple docstring"""






         _lowerCamelCase		:							int            =					d

         # for backward compatibility
         if not hasattr(self		,   '''sp_model_kwargs'''						):
                _lowerCamelCase		:							List[Any]            =					{}

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




  def 		SCREAMING_SNAKE_CASE							(					self	:		List[Any]						):






         """simple docstring"""






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






         """simple docstring"""






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




  def 		SCREAMING_SNAKE_CASE							(					self	:		List[Any]		,   __lowerCAmelCase	:		str						):






         """simple docstring"""






         if token in self.fairseq_tokens_to_ids:
                return self.fairseq_tokens_to_ids[token]
         _lowerCamelCase		:							Dict            =					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	:		Optional[int]		,   __lowerCAmelCase	:		int						):






         """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	:		Any		,   __lowerCAmelCase	:		Optional[int]						):






         """simple docstring"""






         _lowerCamelCase		:							Dict            =					[]
         _lowerCamelCase		:							Any            =					''''''
         _lowerCamelCase		:							str            =					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(__lowerCAmelCase						) + token
                       _lowerCamelCase		:							Dict            =					True
                       _lowerCamelCase		:							str            =					[]
                else:
                       current_sub_tokens.append(__lowerCAmelCase						)
                       _lowerCamelCase		:							Union[str, Any]            =					False
         out_string += self.sp_model.decode(__lowerCAmelCase						)
         return out_string.strip()




  def 		SCREAMING_SNAKE_CASE							(					self	:		Dict		,   __lowerCAmelCase	:		str		,   __lowerCAmelCase	:		Optional[str] = None						):






         """simple docstring"""






         if not os.path.isdir(__lowerCAmelCase						):
                logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''						)
                return
         _lowerCamelCase		:							str            =					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:
                       _lowerCamelCase		:							Optional[int]            =					self.sp_model.serialized_model_proto()
                       fi.write(__lowerCAmelCase						)

         return (out_vocab_file,)




  def 		SCREAMING_SNAKE_CASE							(					self	:		Optional[Any]		,   __lowerCAmelCase	:		List[int]		,   __lowerCAmelCase	:		Optional[List[int]] = None		,   __lowerCAmelCase	:		bool = False						):






         """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						)

         _lowerCamelCase		:							int            =					[1] * len(self.prefix_tokens						)
         _lowerCamelCase		:							Optional[int]            =					[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	:		List[str]		,   __lowerCAmelCase	:		List[int]		,   __lowerCAmelCase	:		Optional[List[int]] = None						):






         """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	:		Optional[int]		,   __lowerCAmelCase	:		Tuple		,   __lowerCAmelCase	:		str		,   __lowerCAmelCase	:		Optional[str]		,   __lowerCAmelCase	:		Optional[str]		,   **__lowerCAmelCase	:		Tuple						):






         """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'''						)
         _lowerCamelCase		:							int            =					src_lang
         _lowerCamelCase		:							Tuple            =					self(__lowerCAmelCase		,   add_special_tokens=__lowerCAmelCase		,   return_tensors=__lowerCAmelCase		,   **__lowerCAmelCase						)
         _lowerCamelCase		:							int            =					self.convert_tokens_to_ids(__lowerCAmelCase						)
         _lowerCamelCase		:							Union[str, Any]            =					tgt_lang_id
         return inputs




  def 		SCREAMING_SNAKE_CASE							(					self	:		List[str]		,   __lowerCAmelCase	:		List[str]		,   __lowerCAmelCase	:		str = "en_XX"		,   __lowerCAmelCase	:		Optional[List[str]] = None		,   __lowerCAmelCase	:		str = "ro_RO"		,   **__lowerCAmelCase	:		List[str]		,   ):






         """simple docstring"""






         _lowerCamelCase		:							Optional[int]            =					src_lang
         _lowerCamelCase		:							Union[str, Any]            =					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	:		Union[str, Any]						):






         """simple docstring"""






         return self.set_tgt_lang_special_tokens(self.tgt_lang						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		Any		,   __lowerCAmelCase	:		str						):






         """simple docstring"""






         _lowerCamelCase		:							str            =					self.lang_code_to_id[src_lang]
         _lowerCamelCase		:							Tuple            =					[self.cur_lang_code_id]
         _lowerCamelCase		:							str            =					[self.eos_token_id]




  def 		SCREAMING_SNAKE_CASE							(					self	:		List[str]		,   __lowerCAmelCase	:		str						):






         """simple docstring"""






         _lowerCamelCase		:							Optional[int]            =					self.lang_code_to_id[tgt_lang]
         _lowerCamelCase		:							List[Any]            =					[self.cur_lang_code_id]
         _lowerCamelCase		:							Any            =					[self.eos_token_id]








import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
					import torch

					from transformers.activations import gelu_new, gelu_python, get_activation







@require_torch
class        __lowerCAmelCase					(			unittest.TestCase					):

			"""simple docstring"""







			def  lowercase_       ( self				)       -> int:



							'''simple docstring'''
							__lowerCamelCase									=       torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]				)
							__lowerCamelCase									=       get_activation('gelu'				)
							self.assertTrue(torch.allclose(gelu_python(lowerCamelCase__				)     ,		torch_builtin(lowerCamelCase__				)				)				)
							self.assertFalse(torch.allclose(gelu_python(lowerCamelCase__				)     ,		gelu_new(lowerCamelCase__				)				)				)


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



							'''simple docstring'''
							__lowerCamelCase									=       torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]				)
							__lowerCamelCase									=       get_activation('gelu'				)
							__lowerCamelCase									=       get_activation('gelu_10'				)

							__lowerCamelCase									=       torch_builtin(lowerCamelCase__				)
							__lowerCamelCase									=       geluaa(lowerCamelCase__				)

							__lowerCamelCase									=       torch.where(y_gelu_aa < 10.0     ,		1     ,		0				)

							self.assertTrue(torch.max(lowerCamelCase__				).item() == 10.0				)
							self.assertTrue(torch.allclose(y_gelu * clipped_mask     ,		y_gelu_aa * clipped_mask				)				)


			def  lowercase_       ( self				)       -> Any:



							'''simple docstring'''
							get_activation('gelu'				)
							get_activation('gelu_10'				)
							get_activation('gelu_fast'				)
							get_activation('gelu_new'				)
							get_activation('gelu_python'				)
							get_activation('gelu_pytorch_tanh'				)
							get_activation('linear'				)
							get_activation('mish'				)
							get_activation('quick_gelu'				)
							get_activation('relu'				)
							get_activation('sigmoid'				)
							get_activation('silu'				)
							get_activation('swish'				)
							get_activation('tanh'				)
							with self.assertRaises(lowerCamelCase__				):
											get_activation('bogus'				)
							with self.assertRaises(lowerCamelCase__				):
											get_activation(lowerCamelCase__				)






			def  lowercase_       ( self				)       -> Dict:



							'''simple docstring'''
							__lowerCamelCase									=       get_activation('gelu'				)
							__lowerCamelCase									=       1
							__lowerCamelCase									=       get_activation('gelu'				)
							self.assertEqual(acta.a     ,		1				)
							with self.assertRaises(lowerCamelCase__				):
											__lowerCamelCase									=       acta.a


