



'''simple docstring'''







import unittest
from functools import partial

import torch

from accelerate import Accelerator, debug_launcher
from accelerate.state import AcceleratorState, GradientState
from accelerate.test_utils import require_cpu, require_huggingface_suite
from accelerate.utils import GradientAccumulationPlugin







def  SCREAMING_SNAKE_CASE__ (				SCREAMING_SNAKE_CASE=2 ,		SCREAMING_SNAKE_CASE=True ,		SCREAMING_SNAKE_CASE=False     ):
		SCREAMING_SNAKE_CASE_						:Optional[int]										=      Accelerator(step_scheduler_with_optimizer=SCREAMING_SNAKE_CASE ,		split_batches=SCREAMING_SNAKE_CASE     )
		SCREAMING_SNAKE_CASE_						:Any										=      torch.nn.Linear(2 ,		4     )
		SCREAMING_SNAKE_CASE_						:int										=      torch.optim.AdamW(model.parameters() ,		lr=1.0     )
		SCREAMING_SNAKE_CASE_						:str										=      torch.optim.lr_scheduler.OneCycleLR(SCREAMING_SNAKE_CASE ,		max_lr=0.0_1 ,		steps_per_epoch=2 ,		epochs=1     )
		SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						:int										=      accelerator.prepare(SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE     )

		# Optimizer has stepped
		scheduler.step()
		if step_scheduler_with_optimizer or (num_processes == 1):
				assert (
				    scheduler.scheduler.last_epoch == num_processes
				), F'Last Epoch ({scheduler.scheduler.last_epoch}) != Num Processes ({num_processes})'
		else:
				assert (
				    scheduler.scheduler.last_epoch != num_processes
				), F'Last Epoch ({scheduler.scheduler.last_epoch}) == Num Processes ({num_processes})'







def  SCREAMING_SNAKE_CASE__ (				SCREAMING_SNAKE_CASE=2 ,		SCREAMING_SNAKE_CASE=True ,		SCREAMING_SNAKE_CASE=False     ):
		SCREAMING_SNAKE_CASE_						:List[str]										=      Accelerator(step_scheduler_with_optimizer=SCREAMING_SNAKE_CASE ,		split_batches=SCREAMING_SNAKE_CASE     )
		SCREAMING_SNAKE_CASE_						:Optional[Any]										=      torch.nn.Linear(2 ,		4     )
		SCREAMING_SNAKE_CASE_						:List[Any]										=      torch.optim.AdamW(model.parameters() ,		lr=1.0     )
		SCREAMING_SNAKE_CASE_						:Optional[int]										=      torch.optim.lr_scheduler.LambdaLR(SCREAMING_SNAKE_CASE ,		lr_lambda=lambda SCREAMING_SNAKE_CASE     : 1 - n / 10     )
		SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						:Tuple										=      accelerator.prepare(SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE     )

		# Optimizer has stepped
		SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      False
		scheduler.step()
		SCREAMING_SNAKE_CASE_						:str										=      1 - (num_processes if (step_scheduler_with_optimizer and not split_batches) else 1) / 10
		assert (
		    scheduler.get_last_lr()[0] == expected_lr
		), F'Wrong lr found at first step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}'

		# Optimizer has not stepped
		SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      True
		scheduler.step()
		if not step_scheduler_with_optimizer:
				SCREAMING_SNAKE_CASE_						:Tuple										=      1 - 2 / 10
		assert (
		    scheduler.get_last_lr()[0] == expected_lr
		), F'Wrong lr found at second step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}'







def  SCREAMING_SNAKE_CASE__ (				SCREAMING_SNAKE_CASE = 2     ):
		from transformers import get_linear_schedule_with_warmup

		SCREAMING_SNAKE_CASE_						:Any										=      [1, 2, 4]
		for num_steps in steps:
				SCREAMING_SNAKE_CASE_						:Any										=      GradientAccumulationPlugin(num_steps=SCREAMING_SNAKE_CASE ,		adjust_scheduler=num_steps > 1     )
				SCREAMING_SNAKE_CASE_						:str										=      Accelerator(gradient_accumulation_plugin=SCREAMING_SNAKE_CASE     )
				SCREAMING_SNAKE_CASE_						:int										=      torch.nn.Linear(2 ,		4     )
				SCREAMING_SNAKE_CASE_						:Any										=      torch.optim.AdamW(model.parameters() ,		lr=1_0.0     )
				SCREAMING_SNAKE_CASE_						:str										=      get_linear_schedule_with_warmup(optimizer=SCREAMING_SNAKE_CASE ,		num_warmup_steps=0 ,		num_training_steps=20     )

				SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      accelerator.prepare(SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE     )

				for i in range(10 * num_steps     ):
						with accelerator.accumulate(SCREAMING_SNAKE_CASE     ):
								optimizer.step()
								scheduler.step()

						if i == (10 * num_steps - 2):
								assert (
								    scheduler.get_last_lr()[0] != 0
								), F'Wrong lr found at second-to-last step, expected non-zero, got {scheduler.get_last_lr()[0]}. num_steps: {num_steps}'
				assert (
				    scheduler.get_last_lr()[0] == 0
				), F'Wrong lr found at last step, expected 0, got {scheduler.get_last_lr()[0]}'
				GradientState._reset_state()





@require_cpu
class 				__lowerCAmelCase(							unittest.TestCase      ):






			def 				_lowercase	(   self  :							Tuple	):




					"""simple docstring"""







					debug_launcher(partial(SCREAMING_SNAKE_CASE      ,       num_processes=1	)      ,       num_processes=1	)
					debug_launcher(partial(SCREAMING_SNAKE_CASE      ,       num_processes=1      ,       split_batches=SCREAMING_SNAKE_CASE	)      ,       num_processes=1	)






			def 				_lowercase	(   self  :							Optional[int]	):




					"""simple docstring"""







					debug_launcher(partial(SCREAMING_SNAKE_CASE      ,       num_processes=1	)      ,       num_processes=1	)
					debug_launcher(partial(SCREAMING_SNAKE_CASE      ,       num_processes=1      ,       split_batches=SCREAMING_SNAKE_CASE	)      ,       num_processes=1	)






			def 				_lowercase	(   self  :							str	):




					"""simple docstring"""







					debug_launcher(partial(SCREAMING_SNAKE_CASE      ,       num_processes=1      ,       step_scheduler_with_optimizer=SCREAMING_SNAKE_CASE	)      ,       num_processes=1	)






			def 				_lowercase	(   self  :							int	):




					"""simple docstring"""







					debug_launcher(partial(SCREAMING_SNAKE_CASE      ,       num_processes=1      ,       step_scheduler_with_optimizer=SCREAMING_SNAKE_CASE	)      ,       num_processes=1	)






			def 				_lowercase	(   self  :							List[str]	):




					"""simple docstring"""







					AcceleratorState._reset_state(SCREAMING_SNAKE_CASE	)
					debug_launcher(SCREAMING_SNAKE_CASE	)
					debug_launcher(partial(SCREAMING_SNAKE_CASE      ,       num_processes=1      ,       split_batches=SCREAMING_SNAKE_CASE	)      ,       num_processes=1	)






			def 				_lowercase	(   self  :							Any	):




					"""simple docstring"""







					AcceleratorState._reset_state(SCREAMING_SNAKE_CASE	)
					debug_launcher(SCREAMING_SNAKE_CASE	)
					debug_launcher(partial(SCREAMING_SNAKE_CASE      ,       num_processes=1      ,       split_batches=SCREAMING_SNAKE_CASE	)      ,       num_processes=1	)






			def 				_lowercase	(   self  :							Optional[int]	):




					"""simple docstring"""







					AcceleratorState._reset_state(SCREAMING_SNAKE_CASE	)
					debug_launcher(partial(SCREAMING_SNAKE_CASE      ,       step_scheduler_with_optimizer=SCREAMING_SNAKE_CASE	)	)






			def 				_lowercase	(   self  :							Dict	):




					"""simple docstring"""







					AcceleratorState._reset_state(SCREAMING_SNAKE_CASE	)
					debug_launcher(partial(SCREAMING_SNAKE_CASE      ,       step_scheduler_with_optimizer=SCREAMING_SNAKE_CASE	)	)






			@require_huggingface_suite
			def 				_lowercase	(   self  :							Union[str, Any]	):




					"""simple docstring"""







					AcceleratorState._reset_state(SCREAMING_SNAKE_CASE	)
					debug_launcher(partial(SCREAMING_SNAKE_CASE      ,       num_processes=1	)	)
					debug_launcher(SCREAMING_SNAKE_CASE	)





import json
import os
import unittest

from transformers import HerbertTokenizer, HerbertTokenizerFast
from transformers.models.herbert.tokenization_herbert import VOCAB_FILES_NAMES
from transformers.testing_utils import get_tests_dir, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin

@require_tokenizers
class 						UpperCAmelCase__     (     __UpperCamelCase		,unittest.TestCase  ):




			'''simple docstring'''






			UpperCamelCase     		=			HerbertTokenizer
			UpperCamelCase     		=			HerbertTokenizerFast
			UpperCamelCase     		=			True




			def 					snake_case__	(  self				:					Tuple					):
					'''simple docstring'''





					super().setUp()

					# Use a simpler test file without japanese/chinese characters
					with open(F'{get_tests_dir()}/fixtures/sample_text_no_unicode.txt'			,			encoding='''utf-8'''					) as f_data:
							__UpperCAmelCase       :	List[str]						=      f_data.read().replace('''\n\n'''			,			'''\n'''					).strip()

					__UpperCAmelCase       :	Dict						=      [
					    '''<s>''',
					    '''</s>''',
					    '''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>''',
					    ''',</w>''',
					    '''<unk>''',
					]
					__UpperCAmelCase       :	Tuple						=      dict(zip(a_			,			range(len(a_					)					)					)					)
					__UpperCAmelCase       :	Optional[int]						=      ['''l o 123''', '''lo w 1456''', '''e r</w> 1789''', '''''']

					__UpperCAmelCase       :	str						=      os.path.join(self.tmpdirname			,			VOCAB_FILES_NAMES['''vocab_file''']					)
					__UpperCAmelCase       :	List[str]						=      os.path.join(self.tmpdirname			,			VOCAB_FILES_NAMES['''merges_file''']					)
					with open(self.vocab_file			,			'''w'''					) as fp:
							fp.write(json.dumps(a_					)					)
					with open(self.merges_file			,			'''w'''					) as fp:
							fp.write('''\n'''.join(a_					)					)




			def 					snake_case__	(  self				:					Union[str, Any]			,			a_				:					Optional[Any]					):
					'''simple docstring'''





					__UpperCAmelCase       :	List[str]						=      '''lower newer'''
					__UpperCAmelCase       :	List[Any]						=      '''lower newer'''
					return input_text, output_text




			def 					snake_case__	(  self				:					int					):
					'''simple docstring'''





					__UpperCAmelCase       :	Any						=      self.tokenizer_class(vocab_file=self.vocab_file			,			merges_file=self.merges_file					)

					__UpperCAmelCase       :	Optional[int]						=      '''lower'''
					__UpperCAmelCase       :	Any						=      ['''low''', '''er</w>''']
					__UpperCAmelCase       :	Any						=      tokenizer.tokenize(a_					)
					self.assertListEqual(a_			,			a_					)

					__UpperCAmelCase       :	int						=      tokens + ['''<unk>''']
					__UpperCAmelCase       :	Optional[Any]						=      [16, 17, 23]
					self.assertListEqual(tokenizer.convert_tokens_to_ids(a_					)			,			a_					)




			def 					snake_case__	(  self				:					List[Any]					):
					'''simple docstring'''





					if not self.test_rust_tokenizer:
							return

					__UpperCAmelCase       :	str						=      self.get_tokenizer()
					__UpperCAmelCase       :	Optional[int]						=      self.get_rust_tokenizer()

					__UpperCAmelCase       :	Tuple						=      '''lower,newer'''

					__UpperCAmelCase       :	Optional[int]						=      tokenizer.tokenize(a_					)
					__UpperCAmelCase       :	Tuple						=      rust_tokenizer.tokenize(a_					)
					self.assertListEqual(a_			,			a_					)

					__UpperCAmelCase       :	Optional[Any]						=      tokenizer.encode(a_			,			add_special_tokens=a_					)
					__UpperCAmelCase       :	Union[str, Any]						=      rust_tokenizer.encode(a_			,			add_special_tokens=a_					)
					self.assertListEqual(a_			,			a_					)

					__UpperCAmelCase       :	Tuple						=      self.get_rust_tokenizer()
					__UpperCAmelCase       :	Union[str, Any]						=      tokenizer.encode(a_					)
					__UpperCAmelCase       :	str						=      rust_tokenizer.encode(a_					)
					self.assertListEqual(a_			,			a_					)




			@slow
			def 					snake_case__	(  self				:					Tuple					):
					'''simple docstring'''





					__UpperCAmelCase       :	List[Any]						=      self.tokenizer_class.from_pretrained('''allegro/herbert-base-cased'''					)

					__UpperCAmelCase       :	Tuple						=      tokenizer.encode('''konstruowanie sekwencji'''			,			add_special_tokens=a_					)
					__UpperCAmelCase       :	int						=      tokenizer.encode('''konstruowanie wielu sekwencji'''			,			add_special_tokens=a_					)

					__UpperCAmelCase       :	Union[str, Any]						=      tokenizer.build_inputs_with_special_tokens(a_					)
					__UpperCAmelCase       :	Any						=      tokenizer.build_inputs_with_special_tokens(a_			,			a_					)

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





