





import math

def       A__(		__lowerCAmelCase ):

				if not isinstance(__lowerCAmelCase	,			__lowerCAmelCase ):
								_snake_case :			int  						=   F'''Input value of [number={number}] must be an integer'''
								raise TypeError(__lowerCAmelCase )

				if number < 1:
								_snake_case :			List[str]  						=   F'''Input value of [number={number}] must be > 0'''
								raise ValueError(__lowerCAmelCase )
				elif number == 1:
								return 3
				elif number == 2:
								return 5
				else:
								_snake_case :			Union[str, Any]  						=   int(math.log(number // 3	,			2 ) ) + 2

								_snake_case :			List[Any]  						=   [3, 5]
								_snake_case :			Dict  						=   2
								_snake_case :			Union[str, Any]  						=   3
								for block in range(1	,			__lowerCAmelCase ):
												for _ in range(__lowerCAmelCase ):
																proth_list.append(2 ** (block + 1) + proth_list[proth_index - 1] )
																proth_index += 1
												increment *= 2

				return proth_list[number - 1]


if __name__ == "__main__":
		import doctest

		doctest.testmod()

		for number in range(11):
				lowercase_		:				Tuple				=						0
				try:
						lowercase_		:				Any				=						proth(number)
				except ValueError:
						print(F'''ValueError: there is no {number}th Proth number''')
						continue

				print(F'''The {number}th Proth number: {value}''')













import os
from pathlib import Path

import numpy as np
import pytest
from pack_dataset import pack_data_dir
from parameterized import parameterized
from save_len_file import save_len_file
from torch.utils.data import DataLoader

from transformers import AutoTokenizer
from transformers.models.mbart.modeling_mbart import shift_tokens_right
from transformers.testing_utils import TestCasePlus, slow
from utils import FAIRSEQ_AVAILABLE, DistributedSortishSampler, LegacySeqaSeqDataset, SeqaSeqDataset


lowercase_		:				Dict				=						'''bert-base-cased'''
lowercase_		:				Any				=						'''google/pegasus-xsum'''
lowercase_		:				str				=						[''' Sam ate lunch today.''', '''Sams lunch ingredients.''']
lowercase_		:				Tuple				=						['''A very interesting story about what I ate for lunch.''', '''Avocado, celery, turkey, coffee''']
lowercase_		:				Any				=						'''patrickvonplaten/t5-tiny-random'''
lowercase_		:				List[Any]				=						'''sshleifer/bart-tiny-random'''
lowercase_		:				Dict				=						'''sshleifer/tiny-mbart'''
lowercase_		:				str				=						'''sshleifer/tiny-marian-en-de'''

def       A__(		__lowerCAmelCase	,			__lowerCAmelCase ):
				_snake_case :			str  						=   '\n'.join(__lowerCAmelCase )
				Path(__lowerCAmelCase ).open('w' ).writelines(__lowerCAmelCase )

def       A__(		__lowerCAmelCase ):
				for split in ["train", "val", "test"]:
								_dump_articles(os.path.join(__lowerCAmelCase	,			F'''{split}.source''' )	,			__lowerCAmelCase )
								_dump_articles(os.path.join(__lowerCAmelCase	,			F'''{split}.target''' )	,			__lowerCAmelCase )
				return tmp_dir


class 			lowercase				(							a_     ):



						"""simple docstring"""

						@parameterized.expand(
						    [
						        MBART_TINY,
						        MARIAN_TINY,
						        T5_TINY,
						        BART_TINY,
						        PEGASUS_XSUM,
						    ] ,				)
						@slow
						def 		__UpperCAmelCase       (    self			:		Optional[int] ,				lowerCamelCase_			:		int					):





										'''simple docstring'''







										_snake_case :			Optional[Any]  						=   AutoTokenizer.from_pretrained(lowerCamelCase_					)
										_snake_case :			Dict  						=   make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir()					)
										_snake_case :			Optional[int]  						=   max(len(tokenizer.encode(lowerCamelCase_					)					) for a in ARTICLES					)
										_snake_case :			Any  						=   max(len(tokenizer.encode(lowerCamelCase_					)					) for a in SUMMARIES					)
										_snake_case :			Dict  						=   4
										_snake_case :			Any  						=   8
										assert max_len_target > max_src_len  # Will be truncated
										assert max_len_source > max_src_len  # Will be truncated
										_snake_case						,			_snake_case :			Optional[Any]  						=   'ro_RO', 'de_DE'  # ignored for all but mbart, but never causes error.
										_snake_case :			int  						=   SeqaSeqDataset(
										    lowerCamelCase_ ,				data_dir=lowerCamelCase_ ,				type_path='train' ,				max_source_length=lowerCamelCase_ ,				max_target_length=lowerCamelCase_ ,				src_lang=lowerCamelCase_ ,				tgt_lang=lowerCamelCase_ ,				)
										_snake_case :			List[str]  						=   DataLoader(lowerCamelCase_ ,				batch_size=2 ,				collate_fn=train_dataset.collate_fn					)
										for batch in dataloader:
														assert isinstance(lowerCamelCase_ ,				lowerCamelCase_					)
														assert batch["attention_mask"].shape == batch["input_ids"].shape
														# show that articles were trimmed.
														assert batch["input_ids"].shape[1] == max_src_len
														# show that targets are the same len
														assert batch["labels"].shape[1] == max_tgt_len
														if tok_name != MBART_TINY:
																		continue
														# check language codes in correct place
														_snake_case :			List[Any]  						=   shift_tokens_right(batch['labels'] ,				tokenizer.pad_token_id					)
														assert batch["decoder_input_ids"][0, 0].item() == tokenizer.lang_code_to_id[tgt_lang]
														assert batch["decoder_input_ids"][0, -1].item() == tokenizer.eos_token_id
														assert batch["input_ids"][0, -2].item() == tokenizer.eos_token_id
														assert batch["input_ids"][0, -1].item() == tokenizer.lang_code_to_id[src_lang]

														break  # No need to test every batch







						@parameterized.expand([BART_TINY, BERT_BASE_CASED]					)
						def 		__UpperCAmelCase       (    self			:		Any ,				lowerCamelCase_			:		str					):





										'''simple docstring'''







										_snake_case :			List[Any]  						=   AutoTokenizer.from_pretrained(lowerCamelCase_					)
										_snake_case :			List[Any]  						=   make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir()					)
										_snake_case :			Dict  						=   max(len(tokenizer.encode(lowerCamelCase_					)					) for a in ARTICLES					)
										_snake_case :			Union[str, Any]  						=   max(len(tokenizer.encode(lowerCamelCase_					)					) for a in SUMMARIES					)
										_snake_case :			Union[str, Any]  						=   4
										_snake_case :			Optional[int]  						=   LegacySeqaSeqDataset(
										    lowerCamelCase_ ,				data_dir=lowerCamelCase_ ,				type_path='train' ,				max_source_length=20 ,				max_target_length=lowerCamelCase_ ,				)
										_snake_case :			Dict  						=   DataLoader(lowerCamelCase_ ,				batch_size=2 ,				collate_fn=train_dataset.collate_fn					)
										for batch in dataloader:
														assert batch["attention_mask"].shape == batch["input_ids"].shape
														# show that articles were trimmed.
														assert batch["input_ids"].shape[1] == max_len_source
														assert 20 >= batch["input_ids"].shape[1]  # trimmed significantly
														# show that targets were truncated
														assert batch["labels"].shape[1] == trunc_target  # Truncated
														assert max_len_target > trunc_target  # Truncated
														break  # No need to test every batch







						def 		__UpperCAmelCase       (    self			:		Dict					):





										'''simple docstring'''







										_snake_case :			int  						=   AutoTokenizer.from_pretrained('facebook/mbart-large-cc25'					)

										_snake_case :			List[str]  						=   Path(make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir()					)					)
										_snake_case :			Any  						=   tmp_dir.joinpath('train.source'					).open().readlines()
										_snake_case :			Optional[Any]  						=   Path(make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir()					)					)
										pack_data_dir(lowerCamelCase_ ,				lowerCamelCase_ ,				1_28 ,				lowerCamelCase_					)
										_snake_case :			Tuple  						=   {x.name for x in tmp_dir.iterdir()}
										_snake_case :			Dict  						=   {x.name for x in save_dir.iterdir()}
										_snake_case :			str  						=   save_dir.joinpath('train.source'					).open().readlines()
										# orig: [' Sam ate lunch today.\n', 'Sams lunch ingredients.']
										# desired_packed: [' Sam ate lunch today.\n Sams lunch ingredients.']
										assert len(lowerCamelCase_					) < len(lowerCamelCase_					)
										assert len(lowerCamelCase_					) == 1
										assert len(packed_examples[0]					) == sum(len(lowerCamelCase_					) for x in orig_examples					)
										assert orig_paths == new_paths







						@pytest.mark.skipif(not FAIRSEQ_AVAILABLE ,				reason='This test requires fairseq'					)
						def 		__UpperCAmelCase       (    self			:		List[str]					):





										'''simple docstring'''







										if not FAIRSEQ_AVAILABLE:
														return
										_snake_case						,			_snake_case						,			_snake_case :			int  						=   self._get_dataset(max_len=64					)
										_snake_case :			List[str]  						=   64
										_snake_case :			str  						=   ds.make_dynamic_sampler(lowerCamelCase_ ,				required_batch_size_multiple=lowerCamelCase_					)
										_snake_case :			Optional[Any]  						=   [len(lowerCamelCase_					) for x in batch_sampler]
										assert len(set(lowerCamelCase_					)					) > 1  # it's not dynamic batch size if every batch is the same length
										assert sum(lowerCamelCase_					) == len(lowerCamelCase_					)  # no dropped or added examples
										_snake_case :			Union[str, Any]  						=   DataLoader(lowerCamelCase_ ,				batch_sampler=lowerCamelCase_ ,				collate_fn=ds.collate_fn ,				num_workers=2					)
										_snake_case :			List[Any]  						=   []
										_snake_case :			List[Any]  						=   []
										for batch in data_loader:
														_snake_case :			Any  						=   batch['input_ids'].shape
														_snake_case :			str  						=   src_shape[0]
														assert bs % required_batch_size_multiple == 0 or bs < required_batch_size_multiple
														_snake_case :			int  						=   np.product(batch['input_ids'].shape					)
														num_src_per_batch.append(lowerCamelCase_					)
														if num_src_tokens > (max_tokens * 1.1):
																		failures.append(lowerCamelCase_					)
										assert num_src_per_batch[0] == max(lowerCamelCase_					)
										if failures:
														raise AssertionError(f'''too many tokens in {len(lowerCamelCase_					)} batches'''					)







						def 		__UpperCAmelCase       (    self			:		Optional[int]					):





										'''simple docstring'''







										_snake_case						,			_snake_case						,			_snake_case :			str  						=   self._get_dataset(max_len=5_12					)
										_snake_case :			Optional[Any]  						=   2
										_snake_case :			Dict  						=   ds.make_sortish_sampler(lowerCamelCase_ ,				shuffle=lowerCamelCase_					)

										_snake_case :			int  						=   DataLoader(lowerCamelCase_ ,				batch_size=lowerCamelCase_ ,				collate_fn=ds.collate_fn ,				num_workers=2					)
										_snake_case :			str  						=   DataLoader(lowerCamelCase_ ,				batch_size=lowerCamelCase_ ,				collate_fn=ds.collate_fn ,				num_workers=2 ,				sampler=lowerCamelCase_					)

										_snake_case :			Tuple  						=   tokenizer.pad_token_id

										def count_pad_tokens(lowerCamelCase_			:		List[str] ,				lowerCamelCase_			:		Any="input_ids"					):
														return [batch[k].eq(lowerCamelCase_					).sum().item() for batch in data_loader]

										assert sum(count_pad_tokens(lowerCamelCase_ ,				k='labels'					)					) < sum(count_pad_tokens(lowerCamelCase_ ,				k='labels'					)					)
										assert sum(count_pad_tokens(lowerCamelCase_					)					) < sum(count_pad_tokens(lowerCamelCase_					)					)
										assert len(lowerCamelCase_					) == len(lowerCamelCase_					)







						def 		__UpperCAmelCase       (    self			:		Tuple ,				lowerCamelCase_			:		Tuple=10_00 ,				lowerCamelCase_			:		Tuple=1_28					):





										'''simple docstring'''







										if os.getenv('USE_REAL_DATA' ,				lowerCamelCase_					):
														_snake_case :			Dict  						=   'examples/seq2seq/wmt_en_ro'
														_snake_case :			List[Any]  						=   max_len * 2 * 64
														if not Path(lowerCamelCase_					).joinpath('train.len'					).exists():
																		save_len_file(lowerCamelCase_ ,				lowerCamelCase_					)
										else:
														_snake_case :			Union[str, Any]  						=   'examples/seq2seq/test_data/wmt_en_ro'
														_snake_case :			List[Any]  						=   max_len * 4
														save_len_file(lowerCamelCase_ ,				lowerCamelCase_					)

										_snake_case :			Union[str, Any]  						=   AutoTokenizer.from_pretrained(lowerCamelCase_					)
										_snake_case :			str  						=   SeqaSeqDataset(
										    lowerCamelCase_ ,				data_dir=lowerCamelCase_ ,				type_path='train' ,				max_source_length=lowerCamelCase_ ,				max_target_length=lowerCamelCase_ ,				n_obs=lowerCamelCase_ ,				)
										return ds, max_tokens, tokenizer







						def 		__UpperCAmelCase       (    self			:		Union[str, Any]					):





										'''simple docstring'''







										_snake_case						,			_snake_case						,			_snake_case :			Any  						=   self._get_dataset()
										_snake_case :			List[str]  						=   set(DistributedSortishSampler(lowerCamelCase_ ,				2_56 ,				num_replicas=2 ,				rank=0 ,				add_extra_examples=lowerCamelCase_					)					)
										_snake_case :			Optional[Any]  						=   set(DistributedSortishSampler(lowerCamelCase_ ,				2_56 ,				num_replicas=2 ,				rank=1 ,				add_extra_examples=lowerCamelCase_					)					)
										assert idsa.intersection(lowerCamelCase_					) == set()







						@parameterized.expand(
						    [
						        MBART_TINY,
						        MARIAN_TINY,
						        T5_TINY,
						        BART_TINY,
						        PEGASUS_XSUM,
						    ] ,				)
						def 		__UpperCAmelCase       (    self			:		Dict ,				lowerCamelCase_			:		Optional[int]					):





										'''simple docstring'''







										_snake_case :			List[str]  						=   AutoTokenizer.from_pretrained(lowerCamelCase_ ,				use_fast=lowerCamelCase_					)
										if tok_name == MBART_TINY:
														_snake_case :			int  						=   SeqaSeqDataset(
														    lowerCamelCase_ ,				data_dir=make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir()					) ,				type_path='train' ,				max_source_length=4 ,				max_target_length=8 ,				src_lang='EN' ,				tgt_lang='FR' ,				)
														_snake_case :			Optional[Any]  						=   train_dataset.dataset_kwargs
														assert "src_lang" in kwargs and "tgt_lang" in kwargs
										else:
														_snake_case :			Tuple  						=   SeqaSeqDataset(
														    lowerCamelCase_ ,				data_dir=make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir()					) ,				type_path='train' ,				max_source_length=4 ,				max_target_length=8 ,				)
														_snake_case :			List[Any]  						=   train_dataset.dataset_kwargs
														assert "add_prefix_space" not in kwargs if tok_name != BART_TINY else "add_prefix_space" in kwargs
														assert len(lowerCamelCase_					) == 1 if tok_name == BART_TINY else len(lowerCamelCase_					) == 0






