





'''simple docstring'''







import json
import os
import unittest

from transformers.models.fsmt.tokenization_fsmt import VOCAB_FILES_NAMES, FSMTTokenizer
from transformers.testing_utils import slow
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin


# using a different tiny model than the one used for default params defined in init to ensure proper testing
snake_case_  :							str            =	"stas/tiny-wmt19-en-ru"


class        __a   (lowerCamelCase							,		unittest.TestCase  ):
							__a       : Any          =							FSMTTokenizer
							__a       : List[Any]          =							False

							def      UpperCAmelCase__						(				self				:  str )       ->							Dict:







													"""simple docstring"""
													super().setUp()

													# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
													UpperCAmelCase_      :   Optional[int]      =	[
													    '''l''',
													    '''o''',
													    '''w''',
													    '''e''',
													    '''r''',
													    '''s''',
													    '''t''',
													    '''i''',
													    '''d''',
													    '''n''',
													    '''w</w>''',
													    '''r</w>''',
													    '''t</w>''',
													    '''lo''',
													    '''low''',
													    '''er</w>''',
													    '''low</w>''',
													    '''lowest</w>''',
													    '''newer</w>''',
													    '''wider</w>''',
													    '''<unk>''',
													]
													UpperCAmelCase_      :   Optional[int]      =	dict(zip(__magic_name__							,  range(len(__magic_name__ ) ) ) )
													UpperCAmelCase_      :   Any      =	['''l o 123''', '''lo w 1456''', '''e r</w> 1789''', '''''']

													UpperCAmelCase_      :   str      =	['''en''', '''ru''']
													UpperCAmelCase_      :   Tuple      =	{
													    '''langs''': self.langs,
													    '''src_vocab_size''': 10,
													    '''tgt_vocab_size''': 20,
													}

													UpperCAmelCase_      :   Tuple      =	os.path.join(self.tmpdirname							,  VOCAB_FILES_NAMES['''src_vocab_file'''] )
													UpperCAmelCase_      :   Any      =	os.path.join(self.tmpdirname							,  VOCAB_FILES_NAMES['''tgt_vocab_file'''] )
													UpperCAmelCase_      :   Optional[Any]      =	os.path.join(self.tmpdirname							,  '''tokenizer_config.json''' )
													UpperCAmelCase_      :   Union[str, Any]      =	os.path.join(self.tmpdirname							,  VOCAB_FILES_NAMES['''merges_file'''] )
													with open(self.src_vocab_file							,  '''w''' ) as fp:
																			fp.write(json.dumps(__magic_name__ ) )
													with open(self.tgt_vocab_file							,  '''w''' ) as fp:
																			fp.write(json.dumps(__magic_name__ ) )
													with open(self.merges_file							,  '''w''' ) as fp:
																			fp.write('''\n'''.join(__magic_name__ ) )
													with open(__magic_name__							,  '''w''' ) as fp:
																			fp.write(json.dumps(__magic_name__ ) )

							@cached_property
							def      UpperCAmelCase__						(				self				:  Union[str, Any] )       ->							int:







													"""simple docstring"""
													return FSMTTokenizer.from_pretrained('''facebook/wmt19-ru-en''' )

							@cached_property
							def      UpperCAmelCase__						(				self				:  str )       ->							str:







													"""simple docstring"""
													return FSMTTokenizer.from_pretrained('''facebook/wmt19-en-ru''' )

							def      UpperCAmelCase__						(				self				:  Optional[Any] )       ->							int:







													"""simple docstring"""
													UpperCAmelCase_      :   Optional[Any]      =	FSMTTokenizer.from_pretrained(__magic_name__ )
													self.assertListEqual([tokenizer.src_lang, tokenizer.tgt_lang]							,  ['''en''', '''ru'''] )
													self.assertEqual(tokenizer.src_vocab_size							,  21 )
													self.assertEqual(tokenizer.tgt_vocab_size							,  21 )

							def      UpperCAmelCase__						(				self				:  Dict )       ->							Any:







													"""simple docstring"""
													UpperCAmelCase_      :   List[Any]      =	FSMTTokenizer(self.langs							,  self.src_vocab_file							,  self.tgt_vocab_file							,  self.merges_file )

													UpperCAmelCase_      :   Tuple      =	'''lower'''
													UpperCAmelCase_      :   str      =	['''low''', '''er</w>''']
													UpperCAmelCase_      :   str      =	tokenizer.tokenize(__magic_name__ )
													self.assertListEqual(__magic_name__							,  __magic_name__ )

													UpperCAmelCase_      :   List[Any]      =	tokens + ['''<unk>''']
													UpperCAmelCase_      :   int      =	[14, 15, 20]
													self.assertListEqual(tokenizer.convert_tokens_to_ids(__magic_name__ )							,  __magic_name__ )

							@slow
							def      UpperCAmelCase__						(				self				:  Tuple )       ->							Union[str, Any]:







													"""simple docstring"""
													UpperCAmelCase_      :   Any      =	self.tokenizer_ru_en

													UpperCAmelCase_      :   List[Any]      =	tokenizer.encode('''sequence builders'''							,  add_special_tokens=__magic_name__ )
													UpperCAmelCase_      :   Dict      =	tokenizer.encode('''multi-sequence build'''							,  add_special_tokens=__magic_name__ )

													UpperCAmelCase_      :   Union[str, Any]      =	tokenizer.build_inputs_with_special_tokens(__magic_name__ )
													UpperCAmelCase_      :   List[Any]      =	tokenizer.build_inputs_with_special_tokens(__magic_name__							,  __magic_name__ )

													assert encoded_sentence == text + [2]
													assert encoded_pair == text + [2] + text_a + [2]

							@slow
							def      UpperCAmelCase__						(				self				:  Tuple )       ->							Any:







													"""simple docstring"""
													UpperCAmelCase_      :   Dict      =	self.tokenizer_en_ru
													UpperCAmelCase_      :   Tuple      =	self.tokenizer_ru_en

													UpperCAmelCase_      :   Dict      =	[
													    [
													        '''Here\'s a little song I wrote. Don\'t worry, be happy.''',
													        [24_70, 39, 11, 23_49, 72_22, 70, 59_79, 7, 84_50, 10_50, 1_31_60, 5, 26, 64_45, 7, 2],
													    ],
													    ['''This is it. No more. I\'m done!''', [1_32, 21, 37, 7, 14_34, 86, 7, 70, 64_76, 13_05, 4_27, 2]],
													]

													# if data needs to be recreated or added, run:
													# import torch
													# model = torch.hub.load("pytorch/fairseq", "transformer.wmt19.en-ru", checkpoint_file="model4.pt", tokenizer="moses", bpe="fastbpe")
													# for src_text, _ in targets: print(f"""[\n"{src_text}",\n {model.encode(src_text).tolist()}\n],""")

													for src_text, tgt_input_ids in targets:
																			UpperCAmelCase_      :   int      =	tokenizer_enc.encode(__magic_name__							,  return_tensors=__magic_name__ )
																			self.assertListEqual(__magic_name__							,  __magic_name__ )

																			# and decode backward, using the reversed languages model
																			UpperCAmelCase_      :   List[str]      =	tokenizer_dec.decode(__magic_name__							,  skip_special_tokens=__magic_name__ )
																			self.assertEqual(__magic_name__							,  __magic_name__ )

							@slow
							def      UpperCAmelCase__						(				self				:  Tuple )       ->							Tuple:







													"""simple docstring"""
													UpperCAmelCase_      :   List[Any]      =	FSMTTokenizer.from_pretrained('''facebook/wmt19-ru-en'''							,  do_lower_case=__magic_name__ )
													UpperCAmelCase_      :   Union[str, Any]      =	tokenizer.tokenize('''USA is United States of America''' )
													UpperCAmelCase_      :   Union[str, Any]      =	['''us''', '''a</w>''', '''is</w>''', '''un''', '''i''', '''ted</w>''', '''st''', '''ates</w>''', '''of</w>''', '''am''', '''er''', '''ica</w>''']
													self.assertListEqual(__magic_name__							,  __magic_name__ )

							@unittest.skip('''FSMTConfig.__init__  requires non-optional args''' )
							def      UpperCAmelCase__						(				self				:  int )       ->							Optional[Any]:







													"""simple docstring"""
													pass



							@unittest.skip('''FSMTConfig.__init__  requires non-optional args''' )
							def      UpperCAmelCase__						(				self				:  Optional[Any] )       ->							Union[str, Any]:







													"""simple docstring"""
													pass












'''simple docstring'''







from manim import *


class        __a   (lowerCamelCase  ):

							def      UpperCAmelCase__						(				self				:  str )       ->							Any:







													"""simple docstring"""
													UpperCAmelCase_      :   List[str]      =	Rectangle(height=0.5							,  width=0.5 )
													UpperCAmelCase_      :   int      =	Rectangle(height=0.4_6							,  width=0.4_6 ).set_stroke(width=0 )
													UpperCAmelCase_      :   str      =	Rectangle(height=0.2_5							,  width=0.2_5 )

													UpperCAmelCase_      :   Optional[Any]      =	[mem.copy() for i in range(6 )]
													UpperCAmelCase_      :   int      =	[mem.copy() for i in range(6 )]
													UpperCAmelCase_      :   List[str]      =	VGroup(*__magic_name__ ).arrange(__magic_name__							,  buff=0 )
													UpperCAmelCase_      :   Optional[Any]      =	VGroup(*__magic_name__ ).arrange(__magic_name__							,  buff=0 )
													UpperCAmelCase_      :   List[Any]      =	VGroup(__magic_name__							,  __magic_name__ ).arrange(__magic_name__							,  buff=0 )
													UpperCAmelCase_      :   int      =	Text('''CPU'''							,  font_size=24 )
													UpperCAmelCase_      :   Optional[Any]      =	Group(__magic_name__							,  __magic_name__ ).arrange(__magic_name__							,  buff=0.5							,  aligned_edge=__magic_name__ )
													cpu.move_to([-2.5, -0.5, 0] )
													self.add(__magic_name__ )

													UpperCAmelCase_      :   List[Any]      =	[mem.copy() for i in range(4 )]
													UpperCAmelCase_      :   List[str]      =	VGroup(*__magic_name__ ).arrange(__magic_name__							,  buff=0 )
													UpperCAmelCase_      :   str      =	Text('''GPU'''							,  font_size=24 )
													UpperCAmelCase_      :   Union[str, Any]      =	Group(__magic_name__							,  __magic_name__ ).arrange(__magic_name__							,  buff=0.5							,  aligned_edge=__magic_name__ )
													gpu.move_to([-1, -1, 0] )
													self.add(__magic_name__ )

													UpperCAmelCase_      :   Dict      =	[mem.copy() for i in range(6 )]
													UpperCAmelCase_      :   Optional[Any]      =	VGroup(*__magic_name__ ).arrange(__magic_name__							,  buff=0 )

													UpperCAmelCase_      :   Dict      =	Text('''Model'''							,  font_size=24 )
													UpperCAmelCase_      :   str      =	Group(__magic_name__							,  __magic_name__ ).arrange(__magic_name__							,  buff=0.5							,  aligned_edge=__magic_name__ )
													model.move_to([3, -1.0, 0] )
													self.add(__magic_name__ )

													UpperCAmelCase_      :   List[str]      =	[]
													UpperCAmelCase_      :   int      =	[]

													for i, rect in enumerate(__magic_name__ ):
																			rect.set_stroke(__magic_name__ )

																			UpperCAmelCase_      :   List[Any]      =	Rectangle(height=0.4_6 / 4							,  width=0.4_6 / 3 ).set_stroke(width=0.0 ).set_fill(__magic_name__							,  opacity=0.7 )

																			if i == 0:
																									cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT )							,  buff=0.0_2							,  direction=__magic_name__ )
																									cpu_target.set_x(cpu_target.get_x() + 0.1 )
																			elif i == 3:
																									cpu_target.next_to(model_cpu_arr[0]							,  direction=__magic_name__							,  buff=0.0 )
																			else:
																									cpu_target.next_to(model_cpu_arr[i - 1]							,  direction=__magic_name__							,  buff=0.0 )
																			self.add(__magic_name__ )
																			model_cpu_arr.append(__magic_name__ )

													self.add(*__magic_name__							,  *__magic_name__ )

													UpperCAmelCase_      :   Any      =	[meta_mem.copy() for i in range(6 )]
													UpperCAmelCase_      :   int      =	[meta_mem.copy() for i in range(6 )]
													UpperCAmelCase_      :   Optional[Any]      =	VGroup(*__magic_name__ ).arrange(__magic_name__							,  buff=0 )
													UpperCAmelCase_      :   Optional[int]      =	VGroup(*__magic_name__ ).arrange(__magic_name__							,  buff=0 )
													UpperCAmelCase_      :   Dict      =	VGroup(__magic_name__							,  __magic_name__ ).arrange(__magic_name__							,  buff=0 )
													UpperCAmelCase_      :   Optional[Any]      =	Text('''Disk'''							,  font_size=24 )
													UpperCAmelCase_      :   Any      =	Group(__magic_name__							,  __magic_name__ ).arrange(__magic_name__							,  buff=0.5							,  aligned_edge=__magic_name__ )
													disk.move_to([-4.0, -1.2_5, 0] )
													self.add(__magic_name__							,  __magic_name__ )

													UpperCAmelCase_      :   Optional[Any]      =	[]

													for i in range(6 ):
																			UpperCAmelCase_      :   Tuple      =	fill.copy().set_fill(__magic_name__							,  opacity=0.8 )
																			target.move_to(disk_left_col_base[i] ).scale(0.5 )
																			cpu_disk_arr.append(__magic_name__ )

													self.add(*__magic_name__ )

													UpperCAmelCase_      :   List[str]      =	Square(side_length=2.2 )
													key.move_to([-5, 2, 0] )

													UpperCAmelCase_      :   Union[str, Any]      =	MarkupText(
													    F"""<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model"""							,  font_size=18							,  )

													key_text.move_to([-5, 2.4, 0] )

													self.add(__magic_name__							,  __magic_name__ )

													UpperCAmelCase_      :   Union[str, Any]      =	MarkupText(
													    F"""<span fgcolor='{BLUE}'>●</span> Checkpoint"""							,  font_size=18							,  )

													blue_text.next_to(__magic_name__							,  DOWN * 2.4							,  aligned_edge=key_text.get_left() )
													self.add(__magic_name__ )

													UpperCAmelCase_      :   Dict      =	MarkupText(F"""The offloaded weights are all sent to the CPU."""							,  font_size=24 )
													step_a.move_to([2, 2, 0] )

													self.play(Write(__magic_name__							,  run_time=3 ) )

													for i in range(6 ):
																			UpperCAmelCase_      :   Dict      =	cpu_disk_arr[i]
																			UpperCAmelCase_      :   int      =	rect.copy().set_fill(__magic_name__							,  opacity=0.8 ).scale(2.0 )
																			cpa.generate_target()
																			cpa.target.move_to(model_base[i] )

																			if i == 0:
																									rect.set_fill(__magic_name__							,  opacity=0.8 )
																									rect.generate_target()
																									rect.target.move_to(cpu_left_col_base[0] ).scale(2.0 )

																									self.remove(
																									    *__magic_name__							,  *__magic_name__							,  )

																			else:
																									rect.generate_target()
																									rect.target.move_to(cpu_left_col_base[i] ).scale(2.0 )
																			self.play(MoveToTarget(__magic_name__ )							,  MoveToTarget(__magic_name__ )							,  model_base[i].animate.set_stroke(__magic_name__ ) )
													self.play(FadeOut(__magic_name__ ) )

													UpperCAmelCase_      :   int      =	MarkupText(
													    F"""Finally, hooks are added to each weight in the model\nto transfer the weights from CPU to GPU\n\t\tand back when needed."""							,  font_size=24							,  )
													step_a.move_to([2, 2, 0] )

													self.play(Write(__magic_name__							,  run_time=3 ) )

													UpperCAmelCase_      :   Union[str, Any]      =	[]
													UpperCAmelCase_      :   Tuple      =	[]
													for i in range(6 ):
																			UpperCAmelCase_      :   List[Any]      =	Arrow(start=__magic_name__							,  end=__magic_name__							,  color=__magic_name__							,  buff=0.5 )
																			a.next_to(model_base[i].get_left()							,  __magic_name__							,  buff=0.2 )
																			arrows.append(__magic_name__ )
																			animations.append(Write(__magic_name__ ) )
													self.play(*__magic_name__ )
													self.wait()





