
'''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     a :
				_snake_case		     =			42




class     a (					unittest.TestCase     ):




				def 						__snake_case				(				self						:					int   ):
										snake_case							:  int			=     get_tests_dir('''fixtures/test_sentencepiece.model'''   )
										snake_case							:  str			=     get_tests_dir('''fixtures/test_sentencepiece_with_bytefallback.model'''   )

										snake_case							:  Tuple			=     FakeOriginalTokenizer(vocab_file=SCREAMING_SNAKE_CASE_   )

										with warnings.catch_warnings(record=SCREAMING_SNAKE_CASE_   ) as w:
																snake_case							:  Union[str, Any]			=     SpmConverter(SCREAMING_SNAKE_CASE_   )
										self.assertEqual(len(SCREAMING_SNAKE_CASE_   ),     0   )

										snake_case							:  Optional[Any]			=     FakeOriginalTokenizer(vocab_file=SCREAMING_SNAKE_CASE_   )

										with warnings.catch_warnings(record=SCREAMING_SNAKE_CASE_   ) as w:
																snake_case							:  int			=     SpmConverter(SCREAMING_SNAKE_CASE_   )
										self.assertEqual(len(SCREAMING_SNAKE_CASE_   ),     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   ),     )









'''simple docstring'''


import os
import pickle
import unittest
from collections import UserDict, namedtuple

import torch

from accelerate.test_utils.testing import require_cuda, require_torch_min_version
from accelerate.test_utils.training import RegressionModel
from accelerate.utils import (
    convert_outputs_to_fpaa,
    extract_model_from_parallel,
    find_device,
    listify,
    patch_environment,
    recursively_apply,
    send_to_device,
)


lowerCamelCase_					:			List[Any]     						=				namedtuple('''ExampleNamedTuple''', '''a b c''')
class  _SCREAMING_SNAKE_CASE				(		unittest.TestCase						):


			'''simple docstring'''





			def 					A   ( self				:    List[Any]      )				->     str:

									'''simple docstring'''







									UpperCamelCase__       =			torch.randn(5			,   2      )
									UpperCamelCase__       =			torch.device("""cuda"""      ) if torch.cuda.is_available() else torch.device("""cpu"""      )

									UpperCamelCase__       =			send_to_device(lowercase			,   lowercase      )
									self.assertTrue(torch.equal(resulta.cpu()			,   lowercase      )      )

									UpperCamelCase__       =			send_to_device((tensor, [tensor, tensor], 1)			,   lowercase      )
									self.assertIsInstance(lowercase			,   lowercase      )
									self.assertTrue(torch.equal(resulta[0].cpu()			,   lowercase      )      )
									self.assertIsInstance(resulta[1]			,   lowercase      )
									self.assertTrue(torch.equal(resulta[1][0].cpu()			,   lowercase      )      )
									self.assertTrue(torch.equal(resulta[1][1].cpu()			,   lowercase      )      )
									self.assertEqual(resulta[2]			,   1      )

									UpperCamelCase__       =			send_to_device({"""a""": tensor, """b""": [tensor, tensor], """c""": 1}			,   lowercase      )
									self.assertIsInstance(lowercase			,   lowercase      )
									self.assertTrue(torch.equal(resulta["""a"""].cpu()			,   lowercase      )      )
									self.assertIsInstance(resulta["""b"""]			,   lowercase      )
									self.assertTrue(torch.equal(resulta["""b"""][0].cpu()			,   lowercase      )      )
									self.assertTrue(torch.equal(resulta["""b"""][1].cpu()			,   lowercase      )      )
									self.assertEqual(resulta["""c"""]			,   1      )

									UpperCamelCase__       =			send_to_device(ExampleNamedTuple(a=lowercase			,   b=[tensor, tensor]			,   c=1      )			,   lowercase      )
									self.assertIsInstance(lowercase			,   lowercase      )
									self.assertTrue(torch.equal(resulta.a.cpu()			,   lowercase      )      )
									self.assertIsInstance(resulta.b			,   lowercase      )
									self.assertTrue(torch.equal(resulta.b[0].cpu()			,   lowercase      )      )
									self.assertTrue(torch.equal(resulta.b[1].cpu()			,   lowercase      )      )
									self.assertEqual(resulta.c			,   1      )

									UpperCamelCase__       =			send_to_device(UserDict({"""a""": tensor, """b""": [tensor, tensor], """c""": 1}      )			,   lowercase      )
									self.assertIsInstance(lowercase			,   lowercase      )
									self.assertTrue(torch.equal(resulta["""a"""].cpu()			,   lowercase      )      )
									self.assertIsInstance(resulta["""b"""]			,   lowercase      )
									self.assertTrue(torch.equal(resulta["""b"""][0].cpu()			,   lowercase      )      )
									self.assertTrue(torch.equal(resulta["""b"""][1].cpu()			,   lowercase      )      )
									self.assertEqual(resulta["""c"""]			,   1      )






			def 					A   ( self				:    List[Any]      )				->     List[str]:

									'''simple docstring'''







									with self.assertRaises(lowercase      ) as cm:
															UpperCamelCase__       =			recursively_apply(torch.tensor			,   (torch.tensor(1      ), 1)			,   error_on_other_type=lowercase      )
									self.assertEqual(
									    str(cm.exception      )			,   """Unsupported types (<class 'int'>) passed to `tensor`. Only nested list/tuple/dicts of objects that are valid for `is_torch_tensor` should be passed."""			,   )






			def 					A   ( self				:    Tuple      )				->     Tuple:

									'''simple docstring'''







									UpperCamelCase__       =			torch.tensor([1, 2, 3, 4, 5]      )
									self.assertEqual(listify(lowercase      )			,   [1, 2, 3, 4, 5]      )

									UpperCamelCase__       =			torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]      )
									self.assertEqual(listify(lowercase      )			,   [[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]      )

									UpperCamelCase__       =			torch.tensor([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]], [[1_1, 1_2, 1_3, 1_4, 1_5], [1_6, 1_7, 1_8, 1_9, 2_0]]]      )
									self.assertEqual(
									    listify(lowercase      )			,   [[[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]], [[1_1, 1_2, 1_3, 1_4, 1_5], [1_6, 1_7, 1_8, 1_9, 2_0]]]      )






			def 					A   ( self				:    Union[str, Any]      )				->     List[str]:

									'''simple docstring'''







									with patch_environment(aa=1			,   BB=2      ):
															self.assertEqual(os.environ.get("""AA"""      )			,   """1"""      )
															self.assertEqual(os.environ.get("""BB"""      )			,   """2"""      )

									self.assertNotIn("""AA"""			,   os.environ      )
									self.assertNotIn("""BB"""			,   os.environ      )






			def 					A   ( self				:    Optional[int]      )				->     int:

									'''simple docstring'''







									UpperCamelCase__       =			RegressionModel()
									UpperCamelCase__       =			model.forward
									UpperCamelCase__       =			convert_outputs_to_fpaa(model.forward      )
									UpperCamelCase__       =			extract_model_from_parallel(lowercase			,   keep_fpaa_wrapper=lowercase      )
									UpperCamelCase__       =			pickle.dumps(lowercase      )






			@require_cuda
			def 					A   ( self				:    int      )				->     Optional[int]:

									'''simple docstring'''







									UpperCamelCase__       =			RegressionModel()
									UpperCamelCase__       =			model.forward
									UpperCamelCase__       =			torch.cuda.amp.autocast(dtype=torch.floataa      )(model.forward      )
									UpperCamelCase__       =			convert_outputs_to_fpaa(model.forward      )
									UpperCamelCase__       =			extract_model_from_parallel(lowercase			,   keep_fpaa_wrapper=lowercase      )
									UpperCamelCase__       =			pickle.dumps(lowercase      )






			@require_cuda
			@require_torch_min_version(version="""2.0"""      )
			def 					A   ( self				:    Any      )				->     int:

									'''simple docstring'''







									UpperCamelCase__       =			RegressionModel()
									UpperCamelCase__       =			model.forward
									UpperCamelCase__       =			torch.cuda.amp.autocast(dtype=torch.floataa      )(model.forward      )
									UpperCamelCase__       =			convert_outputs_to_fpaa(model.forward      )
									UpperCamelCase__       =			torch.compile(model.forward			,   backend="""inductor"""      )
									UpperCamelCase__       =			torch.randn(4			,   1_0      ).cuda()
									UpperCamelCase__       =			model(lowercase      )






			def 					A   ( self				:    Union[str, Any]      )				->     Union[str, Any]:

									'''simple docstring'''







									UpperCamelCase__       =			RegressionModel()
									# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
									UpperCamelCase__       =			torch.nn.parallel.DataParallel(lowercase      )
									UpperCamelCase__       =			extract_model_from_parallel(lowercase      )

									self.assertEqual(lowercase			,   lowercase      )






			@require_torch_min_version(version="""2.0"""      )
			def 					A   ( self				:    Tuple      )				->     Tuple:

									'''simple docstring'''







									UpperCamelCase__       =			RegressionModel()
									UpperCamelCase__       =			torch.compile(lowercase      )

									# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
									UpperCamelCase__       =			torch.nn.parallel.DataParallel(lowercase      )
									UpperCamelCase__       =			torch.compile(lowercase      )
									UpperCamelCase__       =			extract_model_from_parallel(lowercase      )

									self.assertEqual(compiled_model._orig_mod			,   compiled_model_unwrapped._orig_mod      )







			def 					A   ( self				:    Union[str, Any]      )				->     Tuple:

									'''simple docstring'''







									self.assertEqual(find_device([1, """a""", torch.tensor([1, 2, 3]      )]      )			,   torch.device("""cpu"""      )      )
									self.assertEqual(find_device({"""a""": 1, """b""": torch.tensor([1, 2, 3]      )}      )			,   torch.device("""cpu"""      )      )
									self.assertIsNone(find_device([1, """a"""]      )      )
