






'''simple docstring'''







import copy
import tempfile
import unittest

from transformers import MaMaaaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device
from transformers.utils import cached_property

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaTokenizer
			from transformers.models.mam_aaa.modeling_mam_aaa import MaMaaaDecoder, MaMaaaEncoder





def 				SCREAMING_SNAKE_CASE_ (UpperCamelCase       ,    UpperCamelCase       ,    UpperCamelCase       ,    UpperCamelCase=None       ,    UpperCamelCase=None       ,    UpperCamelCase=None       ,    UpperCamelCase=None       ,    UpperCamelCase=None       ,    )   -> List[Any]:
							if attention_mask is None:
														lowerCamelCase__      :			Optional[Any] 						= input_ids.ne(config.pad_token_id   )
							if decoder_attention_mask is None:
														lowerCamelCase__      :			Union[str, Any] 						= decoder_input_ids.ne(config.pad_token_id   )
							if head_mask is None:
														lowerCamelCase__      :			Tuple 						= torch.ones(config.encoder_layers       ,    config.encoder_attention_heads       ,    device=UpperCamelCase   )
							if decoder_head_mask is None:
														lowerCamelCase__      :			int 						= torch.ones(config.decoder_layers       ,    config.decoder_attention_heads       ,    device=UpperCamelCase   )
							if cross_attn_head_mask is None:
														lowerCamelCase__      :			str 						= torch.ones(config.decoder_layers       ,    config.decoder_attention_heads       ,    device=UpperCamelCase   )
							return {
							    "input_ids": input_ids,
							    "decoder_input_ids": decoder_input_ids,
							    "attention_mask": attention_mask,
							    "decoder_attention_mask": attention_mask,
							    "head_mask": head_mask,
							    "decoder_head_mask": decoder_head_mask,
							    "cross_attn_head_mask": cross_attn_head_mask,
							}

class _lowercase			:




							def __init__(							self:       List[str]    ,							UpperCamelCase__:       Dict    ,							UpperCamelCase__:       int=13    ,							UpperCamelCase__:       List[str]=7    ,							UpperCamelCase__:       List[str]=True    ,							UpperCamelCase__:       Optional[Any]=False    ,							UpperCamelCase__:       Optional[Any]=99    ,							UpperCamelCase__:       Optional[Any]=16    ,							UpperCamelCase__:       List[Any]=2    ,							UpperCamelCase__:       List[str]=4    ,							UpperCamelCase__:       List[Any]=4    ,							UpperCamelCase__:       Any="relu"    ,							UpperCamelCase__:       List[Any]=0.1    ,							UpperCamelCase__:       int=0.1    ,							UpperCamelCase__:       List[str]=0.0    ,							UpperCamelCase__:       Optional[Any]=0.0    ,							UpperCamelCase__:       int=20    ,							UpperCamelCase__:       Dict=2    ,							UpperCamelCase__:       Optional[Any]=1    ,							UpperCamelCase__:       List[Any]=0    ,							):
														lowerCamelCase__      :			Tuple 						= parent
														lowerCamelCase__      :			Any 						= batch_size
														lowerCamelCase__      :			Dict 						= seq_length
														lowerCamelCase__      :			Dict 						= is_training
														lowerCamelCase__      :			Any 						= use_labels
														lowerCamelCase__      :			Union[str, Any] 						= vocab_size
														lowerCamelCase__      :			Any 						= hidden_size
														lowerCamelCase__      :			List[Any] 						= num_hidden_layers
														lowerCamelCase__      :			int 						= num_attention_heads
														lowerCamelCase__      :			List[Any] 						= intermediate_size
														lowerCamelCase__      :			Dict 						= hidden_act
														lowerCamelCase__      :			List[Any] 						= hidden_dropout_prob
														lowerCamelCase__      :			List[str] 						= attention_probs_dropout_prob
														lowerCamelCase__      :			Dict 						= encoder_layerdrop
														lowerCamelCase__      :			Optional[int] 						= decoder_layerdrop
														lowerCamelCase__      :			Union[str, Any] 						= max_position_embeddings
														lowerCamelCase__      :			Union[str, Any] 						= eos_token_id
														lowerCamelCase__      :			List[str] 						= pad_token_id
														lowerCamelCase__      :			Tuple 						= bos_token_id




							def 	lowerCamelCase_       (							self:       List[Any]  ):
														lowerCamelCase__      :			Dict 						= ids_tensor([self.batch_size, self.seq_length]    ,							self.vocab_size  )
														lowerCamelCase__      :			Tuple 						= self.eos_token_id  # Eos Token
														lowerCamelCase__      :			int 						= ids_tensor([self.batch_size, self.seq_length]    ,							self.vocab_size  )

														# we need to clamp the input ids here to avoid having pad token in between
														# this is because for M2M100 the position_ids are prepared such that
														# all pad tokens have pos id = 2 and rest are between 2..seq_length
														# and the seq_length here is seq_length - num_pad_tokens
														# but when using past, there is no way of knowing if the past input ids had
														# pad tokens in them, which results in incorrect seq_lenth and which in turn results in
														# position_ids being off by num_pad_tokens in past input
														lowerCamelCase__      :			Dict 						= input_ids.clamp(self.pad_token_id + 1  )
														lowerCamelCase__      :			Tuple 						= decoder_input_ids.clamp(self.pad_token_id + 1  )

														lowerCamelCase__      :			str 						= self.get_config()
														lowerCamelCase__      :			Dict 						= prepare_mam_aaa_inputs_dict(UpperCamelCase__    ,							UpperCamelCase__    ,							UpperCamelCase__  )
														return config, inputs_dict




							def 	lowerCamelCase_       (							self:       Optional[int]  ):
														return MaMaaaConfig(
														    vocab_size=self.vocab_size    ,							d_model=self.hidden_size    ,							encoder_layers=self.num_hidden_layers    ,							decoder_layers=self.num_hidden_layers    ,							encoder_attention_heads=self.num_attention_heads    ,							decoder_attention_heads=self.num_attention_heads    ,							encoder_ffn_dim=self.intermediate_size    ,							decoder_ffn_dim=self.intermediate_size    ,							dropout=self.hidden_dropout_prob    ,							attention_dropout=self.attention_probs_dropout_prob    ,							encoder_layerdrop=self.encoder_layerdrop    ,							decoder_layerdrop=self.decoder_layerdrop    ,							max_position_embeddings=self.max_position_embeddings    ,							eos_token_id=self.eos_token_id    ,							bos_token_id=self.bos_token_id    ,							pad_token_id=self.pad_token_id    ,							)




							def 	lowerCamelCase_       (							self:       List[str]  ):
														lowerCamelCase__						,	lowerCamelCase__      :			Union[str, Any] 						= self.prepare_config_and_inputs()
														return config, inputs_dict




							def 	lowerCamelCase_       (							self:       int    ,							UpperCamelCase__:       Any    ,							UpperCamelCase__:       Optional[Any]  ):
														lowerCamelCase__      :			List[str] 						= MaMaaaModel(config=UpperCamelCase__  ).get_decoder().to(UpperCamelCase__  ).eval()
														lowerCamelCase__      :			List[Any] 						= inputs_dict["""input_ids"""]
														lowerCamelCase__      :			Union[str, Any] 						= inputs_dict["""attention_mask"""]
														lowerCamelCase__      :			Optional[Any] 						= inputs_dict["""head_mask"""]

														# first forward pass
														lowerCamelCase__      :			Dict 						= model(UpperCamelCase__    ,							attention_mask=UpperCamelCase__    ,							head_mask=UpperCamelCase__    ,							use_cache=UpperCamelCase__  )

														lowerCamelCase__						,	lowerCamelCase__      :			List[Any] 						= outputs.to_tuple()

														# create hypothetical multiple next token and extent to next_input_ids
														lowerCamelCase__      :			Dict 						= ids_tensor((self.batch_size, 3)    ,							config.vocab_size  )
														lowerCamelCase__      :			Dict 						= ids_tensor((self.batch_size, 3)    ,							2  )

														# append to next input_ids and
														lowerCamelCase__      :			Optional[int] 						= torch.cat([input_ids, next_tokens]    ,							dim=-1  )
														lowerCamelCase__      :			int 						= torch.cat([attention_mask, next_attn_mask]    ,							dim=-1  )

														lowerCamelCase__      :			int 						= model(UpperCamelCase__    ,							attention_mask=UpperCamelCase__  )["""last_hidden_state"""]
														lowerCamelCase__      :			List[Any] 						= model(UpperCamelCase__    ,							attention_mask=UpperCamelCase__    ,							past_key_values=UpperCamelCase__  )[
														    """last_hidden_state"""
														]

														# select random slice
														lowerCamelCase__      :			List[Any] 						= ids_tensor((1,)    ,							output_from_past.shape[-1]  ).item()
														lowerCamelCase__      :			int 						= output_from_no_past[:, -3:, random_slice_idx].detach()
														lowerCamelCase__      :			Any 						= output_from_past[:, :, random_slice_idx].detach()

														self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]  )

														# test that outputs are equal for slice
														self.parent.assertTrue(torch.allclose(UpperCamelCase__    ,							UpperCamelCase__    ,							atol=1e-2  )  )




							def 	lowerCamelCase_       (							self:       Optional[int]    ,							UpperCamelCase__:       Union[str, Any]    ,							UpperCamelCase__:       Tuple  ):
														lowerCamelCase__      :			Optional[Any] 						= MaMaaaModel(config=UpperCamelCase__  ).to(UpperCamelCase__  ).eval()
														lowerCamelCase__      :			Tuple 						= model(**UpperCamelCase__  )

														lowerCamelCase__      :			List[str] 						= outputs.encoder_last_hidden_state
														lowerCamelCase__      :			Union[str, Any] 						= outputs.last_hidden_state

														with tempfile.TemporaryDirectory() as tmpdirname:
																					lowerCamelCase__      :			Optional[int] 						= model.get_encoder()
																					encoder.save_pretrained(UpperCamelCase__  )
																					lowerCamelCase__      :			Tuple 						= MaMaaaEncoder.from_pretrained(UpperCamelCase__  ).to(UpperCamelCase__  )

														lowerCamelCase__      :			Dict 						= encoder(inputs_dict["""input_ids"""]    ,							attention_mask=inputs_dict["""attention_mask"""]  )[
														    0
														]

														self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1e-3  )

														with tempfile.TemporaryDirectory() as tmpdirname:
																					lowerCamelCase__      :			str 						= model.get_decoder()
																					decoder.save_pretrained(UpperCamelCase__  )
																					lowerCamelCase__      :			List[str] 						= MaMaaaDecoder.from_pretrained(UpperCamelCase__  ).to(UpperCamelCase__  )

														lowerCamelCase__      :			Dict 						= decoder(
														    input_ids=inputs_dict["""decoder_input_ids"""]    ,							attention_mask=inputs_dict["""decoder_attention_mask"""]    ,							encoder_hidden_states=UpperCamelCase__    ,							encoder_attention_mask=inputs_dict["""attention_mask"""]    ,							)[0]

														self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1e-3  )



@require_torch
class _lowercase			(       _lowercase       ,   _lowercase       ,   _lowercase       ,   unittest.TestCase	):
							a          = (
							    (
							        MaMaaaModel,
							        MaMaaaForConditionalGeneration,
							    )
							    if is_torch_available()
							    else ()
							)
							a          = (MaMaaaForConditionalGeneration,) if is_torch_available() else ()
							a          = (
							    {
							        """conversational""": MaMaaaForConditionalGeneration,
							        """feature-extraction""": MaMaaaModel,
							        """summarization""": MaMaaaForConditionalGeneration,
							        """text2text-generation""": MaMaaaForConditionalGeneration,
							        """translation""": MaMaaaForConditionalGeneration,
							    }
							    if is_torch_available()
							    else {}
							)
							a          = True
							a          = True
							a          = False
							a          = False




							def 	lowerCamelCase_       (							self:       Any    ,							UpperCamelCase__:       Any    ,							UpperCamelCase__:       str    ,							UpperCamelCase__:       Dict    ,							UpperCamelCase__:       List[str]    ,							UpperCamelCase__:       int  ):
														if pipeline_test_casse_name == "TranslationPipelineTests":
																					# Get `ValueError: Translation requires a `src_lang` and a `tgt_lang` for this model`.
																					# `M2M100Config` was never used in pipeline tests: cannot create a simple tokenizer.
																					return True

														return False




							def 	lowerCamelCase_       (							self:       List[Any]  ):
														lowerCamelCase__      :			Tuple 						= MaMaaaModelTester(self  )
														lowerCamelCase__      :			Optional[int] 						= ConfigTester(self    ,							config_class=UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Tuple  ):
														self.config_tester.run_common_tests()




							def 	lowerCamelCase_       (							self:       str  ):
														lowerCamelCase__						,	lowerCamelCase__      :			Dict 						= self.model_tester.prepare_config_and_inputs()
														for model_class in self.all_model_classes:
																					lowerCamelCase__      :			Union[str, Any] 						= model_class(UpperCamelCase__  )

																					with tempfile.TemporaryDirectory() as tmpdirname:
																												model.save_pretrained(UpperCamelCase__  )
																												lowerCamelCase__						,	lowerCamelCase__      :			Tuple 						= model_class.from_pretrained(UpperCamelCase__    ,							output_loading_info=UpperCamelCase__  )
																					self.assertEqual(info["""missing_keys"""]    ,							[]  )




							def 	lowerCamelCase_       (							self:       Tuple  ):
														lowerCamelCase__      :			int 						= self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_decoder_model_past_large_inputs(*UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       str  ):
														lowerCamelCase__      :			Union[str, Any] 						= self.model_tester.prepare_config_and_inputs_for_common()
														self.model_tester.check_encoder_decoder_model_standalone(*UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Optional[int]  ):
														lowerCamelCase__						,	lowerCamelCase__      :			Union[str, Any] 						= self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in (MaMaaaModel, MaMaaaForConditionalGeneration):
																					lowerCamelCase__      :			Any 						= model_class(UpperCamelCase__  )
																					model.to(UpperCamelCase__  )
																					model.eval()

																					lowerCamelCase__      :			str 						= copy.deepcopy(self._prepare_for_class(UpperCamelCase__    ,							UpperCamelCase__  )  )

																					if not self.is_encoder_decoder:
																												lowerCamelCase__      :			List[Any] 						= inputs["""input_ids"""]
																												del inputs["input_ids"]
																					else:
																												lowerCamelCase__      :			Tuple 						= inputs["""input_ids"""]
																												lowerCamelCase__      :			int 						= inputs.get("""decoder_input_ids"""    ,							UpperCamelCase__  )
																												del inputs["input_ids"]
																												inputs.pop("""decoder_input_ids"""    ,							UpperCamelCase__  )

																					lowerCamelCase__      :			Dict 						= model.get_input_embeddings()
																					if not self.is_encoder_decoder:
																												lowerCamelCase__      :			str 						= wte(UpperCamelCase__  )
																					else:
																												lowerCamelCase__      :			List[Any] 						= wte(UpperCamelCase__  )
																												lowerCamelCase__      :			Tuple 						= wte(UpperCamelCase__  )

																					with torch.no_grad():
																												model(**UpperCamelCase__  )[0]




							def 	lowerCamelCase_       (							self:       Optional[int]  ):
														lowerCamelCase__						,	lowerCamelCase__      :			List[str] 						= self.model_tester.prepare_config_and_inputs()
														lowerCamelCase__      :			Optional[int] 						= input_dict["""input_ids"""]
														lowerCamelCase__      :			Tuple 						= input_ids.ne(1  ).to(UpperCamelCase__  )
														lowerCamelCase__      :			List[Any] 						= MaMaaaForConditionalGeneration(UpperCamelCase__  ).eval().to(UpperCamelCase__  )
														if torch_device == "cuda":
																					model.half()
														model.generate(UpperCamelCase__    ,							attention_mask=UpperCamelCase__  )
														model.generate(num_beams=4    ,							do_sample=UpperCamelCase__    ,							early_stopping=UpperCamelCase__    ,							num_return_sequences=3  )





def 				SCREAMING_SNAKE_CASE_ (UpperCamelCase   )   -> Optional[int]:
							return torch.tensor(UpperCamelCase       ,    dtype=torch.long       ,    device=UpperCamelCase   )


_A			:    List[str]					  =1e-4



@require_torch
@require_sentencepiece
@require_tokenizers
@slow
class _lowercase			(       unittest.TestCase	):




							@cached_property
							def 	lowerCamelCase_       (							self:       Optional[int]  ):
														return MaMaaaTokenizer.from_pretrained("""facebook/m2m100_418M"""  )




							def 	lowerCamelCase_       (							self:       Tuple  ):
														lowerCamelCase__      :			Union[str, Any] 						= MaMaaaModel.from_pretrained("""facebook/m2m100_418M"""  ).to(UpperCamelCase__  )
														lowerCamelCase__      :			List[str] 						= _long_tensor([[128_028, 98, 12, 30_527, 2_732, 159, 7_755, 61_904, 39_144, 38, 2]]  )
														lowerCamelCase__      :			Union[str, Any] 						= _long_tensor([[2, 128_028, 98, 12, 30_527, 2_732, 159, 7_755, 61_904, 39_144, 38]]  )
														lowerCamelCase__      :			List[str] 						= prepare_mam_aaa_inputs_dict(model.config    ,							UpperCamelCase__    ,							UpperCamelCase__  )
														with torch.no_grad():
																					lowerCamelCase__      :			Union[str, Any] 						= model(**UpperCamelCase__  )[0]
														lowerCamelCase__      :			List[str] 						= torch.Size((1, 11, 1_024)  )
														self.assertEqual(output.shape    ,							UpperCamelCase__  )
														# change to expected output here
														lowerCamelCase__      :			Any 						= torch.tensor(
														    [[-0.7_780, -0.1_676, 0.1_038], [-6.7_556, -1.3_992, 0.0_567], [-7.5_383, -0.5_920, -0.2_779]]    ,							device=UpperCamelCase__  )
														self.assertTrue(torch.allclose(output[:, :3, :3]    ,							UpperCamelCase__    ,							atol=UpperCamelCase__  )  )




							def 	lowerCamelCase_       (							self:       Any  ):
														lowerCamelCase__      :			str 						= MaMaaaForConditionalGeneration.from_pretrained("""facebook/m2m100_418M"""  ).to(UpperCamelCase__  )

														# change to intended input
														lowerCamelCase__      :			int 						= _long_tensor([[128_028, 98, 12, 30_527, 2_732, 159, 7_755, 61_904, 39_144, 38, 2]]  )
														lowerCamelCase__      :			List[str] 						= _long_tensor([[2, 128_028, 98, 12, 30_527, 2_732, 159, 7_755, 61_904, 39_144, 38]]  )
														lowerCamelCase__      :			str 						= prepare_mam_aaa_inputs_dict(model.config    ,							UpperCamelCase__    ,							UpperCamelCase__  )
														with torch.no_grad():
																					lowerCamelCase__      :			List[str] 						= model(**UpperCamelCase__  )[0]
														lowerCamelCase__      :			Union[str, Any] 						= torch.Size((1, 11, model.config.vocab_size)  )
														self.assertEqual(output.shape    ,							UpperCamelCase__  )
														# change to expected output here
														lowerCamelCase__      :			int 						= torch.tensor(
														    [[-1.0_448, -1.0_411, 3.7_992], [-3.2_191, -3.2_386, -1.3_451], [-3.6_210, -3.5_993, 0.4_925]]    ,							device=UpperCamelCase__  )
														self.assertTrue(torch.allclose(output[:, :3, :3]    ,							UpperCamelCase__    ,							atol=UpperCamelCase__  )  )




							def 	lowerCamelCase_       (							self:       int  ):
														lowerCamelCase__      :			Union[str, Any] 						= MaMaaaForConditionalGeneration.from_pretrained("""facebook/m2m100_418M"""  ).to(UpperCamelCase__  )
														lowerCamelCase__      :			Dict 						= MaMaaaTokenizer.from_pretrained("""facebook/m2m100_418M"""    ,							src_lang="""fr"""    ,							tgt_lang="""en"""  )

														lowerCamelCase__      :			Any 						= [
														    """L'affaire NSA souligne l'absence totale de débat sur le renseignement""",
														    """Selon moi, il y a deux niveaux de réponse de la part du gouvernement français.""",
														    """Lorsque François Hollande téléphone à Barack Obama ou quand le ministre des affaires étrangères Laurent"""
														    """ Fabius convoque l'ambassadeur des Etats-Unis, ils réagissent à une vraie découverte, qui est celle de"""
														    """ l'ampleur de la surveillance américaine sur l'ensemble des communications en France.""",
														]

														# The below article tests that we don't add any hypotheses outside of the top n_beams
														lowerCamelCase__      :			Dict 						= tokenizer(UpperCamelCase__    ,							padding=UpperCamelCase__    ,							return_tensors="""pt"""  )

														lowerCamelCase__      :			Union[str, Any] 						= model.generate(
														    input_ids=dct["""input_ids"""].to(UpperCamelCase__  )    ,							attention_mask=dct["""attention_mask"""].to(UpperCamelCase__  )    ,							num_beams=5    ,							forced_bos_token_id=tokenizer.get_lang_id("""en"""  )    ,							)

														lowerCamelCase__      :			Union[str, Any] 						= [
														    """The NSA case highlights the total absence of intelligence debate""",
														    """I think there are two levels of response from the French government.""",
														    """When François Hollande calls Barack Obama or when Foreign Minister Laurent Fabius calls the U.S."""
														    """ Ambassador, they respond to a real discovery, which is that of the scale of U.S. surveillance on all"""
														    """ communications in France.""",
														]

														lowerCamelCase__      :			Any 						= tokenizer.batch_decode(
														    hypotheses_batch.tolist()    ,							clean_up_tokenization_spaces=UpperCamelCase__    ,							skip_special_tokens=UpperCamelCase__  )
														assert generated == expected_en









'''simple docstring'''







import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from transformers import BatchEncoding, MarianTokenizer
from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow
from transformers.utils import is_sentencepiece_available, is_tf_available, is_torch_available


if is_sentencepiece_available():
			from transformers.models.marian.tokenization_marian import VOCAB_FILES_NAMES, save_json

from ...test_tokenization_common import TokenizerTesterMixin


_A			:    int					  =get_tests_dir('''fixtures/test_sentencepiece.model''')

_A			:    Tuple					  ={'''target_lang''': '''fi''', '''source_lang''': '''en'''}
_A			:    int					  ='''>>zh<<'''
_A			:    Dict					  ='''Helsinki-NLP/'''

if is_torch_available():
			_A			:    List[Any]					  ='''pt'''
elif is_tf_available():
			_A			:    Optional[int]					  ='''tf'''
else:
			_A			:    Dict					  ='''jax'''

@require_sentencepiece
class _lowercase			(       _lowercase       ,   unittest.TestCase	):
							a          = MarianTokenizer
							a          = False
							a          = True




							def 	lowerCamelCase_       (							self:       List[str]  ):
														super().setUp()
														lowerCamelCase__      :			List[Any] 						= ["""</s>""", """<unk>""", """▁This""", """▁is""", """▁a""", """▁t""", """est""", """\u0120""", """<pad>"""]
														lowerCamelCase__      :			Optional[Any] 						= dict(zip(UpperCamelCase__    ,							range(len(UpperCamelCase__  )  )  )  )
														lowerCamelCase__      :			Optional[int] 						= Path(self.tmpdirname  )
														save_json(UpperCamelCase__    ,							save_dir / VOCAB_FILES_NAMES["""vocab"""]  )
														save_json(UpperCamelCase__    ,							save_dir / VOCAB_FILES_NAMES["""tokenizer_config_file"""]  )
														if not (save_dir / VOCAB_FILES_NAMES["source_spm"]).exists():
																					copyfile(UpperCamelCase__    ,							save_dir / VOCAB_FILES_NAMES["""source_spm"""]  )
																					copyfile(UpperCamelCase__    ,							save_dir / VOCAB_FILES_NAMES["""target_spm"""]  )

														lowerCamelCase__      :			Dict 						= MarianTokenizer.from_pretrained(self.tmpdirname  )
														tokenizer.save_pretrained(self.tmpdirname  )




							def 	lowerCamelCase_       (							self:       Optional[Any]    ,							**UpperCamelCase__:       Any  ):
														return MarianTokenizer.from_pretrained(self.tmpdirname    ,							**UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       str    ,							UpperCamelCase__:       List[str]  ):
														return (
														    "This is a test",
														    "This is a test",
														)




							def 	lowerCamelCase_       (							self:       Optional[Any]  ):
														lowerCamelCase__      :			Any 						= """</s>"""
														lowerCamelCase__      :			List[Any] 						= 0

														self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCamelCase__  )    ,							UpperCamelCase__  )
														self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCamelCase__  )    ,							UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Union[str, Any]  ):
														lowerCamelCase__      :			int 						= list(self.get_tokenizer().get_vocab().keys()  )

														self.assertEqual(vocab_keys[0]    ,							"""</s>"""  )
														self.assertEqual(vocab_keys[1]    ,							"""<unk>"""  )
														self.assertEqual(vocab_keys[-1]    ,							"""<pad>"""  )
														self.assertEqual(len(UpperCamelCase__  )    ,							9  )




							def 	lowerCamelCase_       (							self:       int  ):
														self.assertEqual(self.get_tokenizer().vocab_size    ,							9  )




							def 	lowerCamelCase_       (							self:       int  ):
														lowerCamelCase__      :			List[Any] 						= MarianTokenizer.from_pretrained(F'''{ORG_NAME}opus-mt-en-de'''  )
														lowerCamelCase__      :			Optional[int] 						= en_de_tokenizer(["""I am a small frog"""]    ,							return_tensors=UpperCamelCase__  )
														self.assertIsInstance(UpperCamelCase__    ,							UpperCamelCase__  )
														lowerCamelCase__      :			Optional[Any] 						= [38, 121, 14, 697, 38_848, 0]
														self.assertListEqual(UpperCamelCase__    ,							batch.input_ids[0]  )

														lowerCamelCase__      :			List[str] 						= tempfile.mkdtemp()
														en_de_tokenizer.save_pretrained(UpperCamelCase__  )
														lowerCamelCase__      :			Tuple 						= [x.name for x in Path(UpperCamelCase__  ).glob("""*"""  )]
														self.assertIn("""source.spm"""    ,							UpperCamelCase__  )
														MarianTokenizer.from_pretrained(UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Tuple  ):
														lowerCamelCase__      :			List[Any] 						= self.get_tokenizer()

														lowerCamelCase__      :			Any 						= tok(
														    ["""I am a small frog""" * 1_000, """I am a small frog"""]    ,							padding=UpperCamelCase__    ,							truncation=UpperCamelCase__    ,							return_tensors=UpperCamelCase__  )
														self.assertIsInstance(UpperCamelCase__    ,							UpperCamelCase__  )
														self.assertEqual(batch.input_ids.shape    ,							(2, 512)  )




							def 	lowerCamelCase_       (							self:       Union[str, Any]  ):
														lowerCamelCase__      :			str 						= self.get_tokenizer()
														lowerCamelCase__      :			Dict 						= tok(["""I am a tiny frog""", """I am a small frog"""]    ,							padding=UpperCamelCase__    ,							return_tensors=UpperCamelCase__  )
														self.assertIsInstance(UpperCamelCase__    ,							UpperCamelCase__  )
														self.assertEqual(batch_smaller.input_ids.shape    ,							(2, 10)  )




							@slow
							def 	lowerCamelCase_       (							self:       List[str]  ):
														# fmt: off
														lowerCamelCase__      :			int 						= {"""input_ids""": [[43_495, 462, 20, 42_164, 1_369, 52, 464, 132, 1_703, 492, 13, 7_491, 38_999, 6, 8, 464, 132, 1_703, 492, 13, 4_669, 37_867, 13, 7_525, 27, 1_593, 988, 13, 33_972, 7_029, 6, 20, 8_251, 383, 2, 270, 5_866, 3_788, 2, 2_353, 8_251, 12_338, 2, 13_958, 387, 2, 3_629, 6_953, 188, 2_900, 2, 13_958, 8_011, 11_501, 23, 8_460, 4_073, 34_009, 20, 435, 11_439, 27, 8, 8_460, 4_073, 6_004, 20, 9_988, 375, 27, 33, 266, 1_945, 1_076, 1_350, 37_867, 3_288, 5, 577, 1_076, 4_374, 8, 5_082, 5, 26_453, 257, 556, 403, 2, 242, 132, 383, 316, 492, 8, 10_767, 6, 316, 304, 4_239, 3, 0], [148, 15_722, 19, 1_839, 12, 1_350, 13, 22_327, 5_082, 5_418, 47_567, 35_938, 59, 318, 19_552, 108, 2_183, 54, 14_976, 4_835, 32, 547, 1_114, 8, 315, 2_417, 5, 92, 19_088, 3, 0, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100], [36, 6_395, 12_570, 39_147, 11_597, 6, 266, 4, 45_405, 7_296, 3, 0, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
														# fmt: on

														self.tokenizer_integration_test_util(
														    expected_encoding=UpperCamelCase__    ,							model_name="""Helsinki-NLP/opus-mt-en-de"""    ,							revision="""1a8c2263da11e68e50938f97e10cd57820bd504c"""    ,							decode_kwargs={"""use_source_tokenizer""": True}    ,							)




							def 	lowerCamelCase_       (							self:       List[str]  ):
														lowerCamelCase__      :			Union[str, Any] 						= MarianTokenizer.from_pretrained("""hf-internal-testing/test-marian-two-vocabs"""  )

														lowerCamelCase__      :			str 						= """Tämä on testi"""
														lowerCamelCase__      :			Any 						= """This is a test"""

														lowerCamelCase__      :			int 						= [76, 7, 2_047, 2]
														lowerCamelCase__      :			List[str] 						= [69, 12, 11, 940, 2]

														lowerCamelCase__      :			Tuple 						= tokenizer(UpperCamelCase__  ).input_ids
														self.assertListEqual(UpperCamelCase__    ,							UpperCamelCase__  )

														lowerCamelCase__      :			Union[str, Any] 						= tokenizer(text_target=UpperCamelCase__  ).input_ids
														self.assertListEqual(UpperCamelCase__    ,							UpperCamelCase__  )

														lowerCamelCase__      :			Tuple 						= tokenizer.decode(UpperCamelCase__    ,							skip_special_tokens=UpperCamelCase__  )
														self.assertEqual(UpperCamelCase__    ,							UpperCamelCase__  )

