




'''simple docstring'''







import torch

from accelerate import Accelerator, DistributedType





class   lowerCamelCase_							:




					'''simple docstring'''






					def __enter__(					self				):
							if self.enabled:
									_snake_case			 = self.model.no_sync()
									self.model_sync_obj.__enter__()

							return self






					def __exit__(					self,						__UpperCamelCase,						__UpperCamelCase,						__UpperCamelCase				):
							if self.enabled:
									# Average all models on exit
									self._sync_and_avg_model_params()
									self.model_sync_obj.__exit__(__SCREAMING_SNAKE_CASE,						__SCREAMING_SNAKE_CASE,						__SCREAMING_SNAKE_CASE				)






					def __init__(					self,						__UpperCamelCase,						__UpperCamelCase,						__UpperCamelCase,						__UpperCamelCase = True				):
							if accelerator.distributed_type not in [
							    DistributedType.NO,
							    DistributedType.MULTI_CPU,
							    DistributedType.MULTI_GPU,
							]:
									raise NotImplementedError("LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)"				)
							_snake_case			 = enabled and accelerator.distributed_type != DistributedType.NO
							_snake_case			 = 0
							if self.enabled:
									_snake_case			 = accelerator
									_snake_case			 = model
									_snake_case			 = local_sgd_steps






					def        __SCREAMING_SNAKE_CASE			(					self				):
							self.num_steps += 1
							if not self.enabled:
									return

							if self.num_steps % self.local_sgd_steps == 0:
									self._sync_and_avg_model_params()






					def        __SCREAMING_SNAKE_CASE			(					self				):
							self.accelerator.wait_for_everyone()
							with self.accelerator.autocast():
									for param in self.model.parameters():
											_snake_case			 = self.accelerator.reduce(param.data,						reduction="mean"				)











'''simple docstring'''







import unittest
import warnings
from dataclasses import dataclass

from transformers.convert_slow_tokenizer import SpmConverter
from transformers.testing_utils import get_tests_dir





@dataclass
class   lowerCamelCase_							:




					'''simple docstring'''






					lowercase__					=				42





class   lowerCamelCase_							( unittest.TestCase							):




					'''simple docstring'''






					def        __SCREAMING_SNAKE_CASE			(					self				):
							_snake_case			 = get_tests_dir("fixtures/test_sentencepiece.model"				)
							_snake_case			 = get_tests_dir("fixtures/test_sentencepiece_with_bytefallback.model"				)

							_snake_case			 = FakeOriginalTokenizer(vocab_file=__UpperCamelCase				)

							with warnings.catch_warnings(record=__UpperCamelCase				) as w:
									_snake_case			 = SpmConverter(__UpperCamelCase				)
							self.assertEqual(len(__UpperCamelCase				),						0				)

							_snake_case			 = FakeOriginalTokenizer(vocab_file=__UpperCamelCase				)

							with warnings.catch_warnings(record=__UpperCamelCase				) as w:
									_snake_case			 = SpmConverter(__UpperCamelCase				)
							self.assertEqual(len(__UpperCamelCase				),						1				)

							self.assertIn(
							    "The sentencepiece tokenizer that you are converting to a fast tokenizer uses the byte fallback option"
							    " which is not implemented in the fast tokenizers.",						str(w[0].message				),						)





