






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,
)


__UpperCAmelCase										=		namedtuple('ExampleNamedTuple', 'a b c')



class 						lowerCamelCase   (unittest.TestCase    ):







		'''simple docstring'''



		def       __UpperCAmelCase    (							self     )			->							Union[str, Any]:
							UpperCAmelCase_					:   Optional[Any]   =		torch.randn(5       ,	2     )
							UpperCAmelCase_					:   List[str]   =		torch.device('cuda'     ) if torch.cuda.is_available() else torch.device('cpu'     )

							UpperCAmelCase_					:   Optional[int]   =		send_to_device(_UpperCamelCase       ,	_UpperCamelCase     )
							self.assertTrue(torch.equal(resulta.cpu()       ,	_UpperCamelCase     )     )

							UpperCAmelCase_					:   Dict   =		send_to_device((tensor, [tensor, tensor], 1)       ,	_UpperCamelCase     )
							self.assertIsInstance(_UpperCamelCase       ,	_UpperCamelCase     )
							self.assertTrue(torch.equal(resulta[0].cpu()       ,	_UpperCamelCase     )     )
							self.assertIsInstance(resulta[1]       ,	_UpperCamelCase     )
							self.assertTrue(torch.equal(resulta[1][0].cpu()       ,	_UpperCamelCase     )     )
							self.assertTrue(torch.equal(resulta[1][1].cpu()       ,	_UpperCamelCase     )     )
							self.assertEqual(resulta[2]       ,	1     )

							UpperCAmelCase_					:   Optional[Any]   =		send_to_device({'a': tensor, 'b': [tensor, tensor], 'c': 1}       ,	_UpperCamelCase     )
							self.assertIsInstance(_UpperCamelCase       ,	_UpperCamelCase     )
							self.assertTrue(torch.equal(resulta['a'].cpu()       ,	_UpperCamelCase     )     )
							self.assertIsInstance(resulta['b']       ,	_UpperCamelCase     )
							self.assertTrue(torch.equal(resulta['b'][0].cpu()       ,	_UpperCamelCase     )     )
							self.assertTrue(torch.equal(resulta['b'][1].cpu()       ,	_UpperCamelCase     )     )
							self.assertEqual(resulta['c']       ,	1     )

							UpperCAmelCase_					:   Optional[int]   =		send_to_device(ExampleNamedTuple(a=_UpperCamelCase       ,	b=[tensor, tensor]       ,	c=1     )       ,	_UpperCamelCase     )
							self.assertIsInstance(_UpperCamelCase       ,	_UpperCamelCase     )
							self.assertTrue(torch.equal(resulta.a.cpu()       ,	_UpperCamelCase     )     )
							self.assertIsInstance(resulta.b       ,	_UpperCamelCase     )
							self.assertTrue(torch.equal(resulta.b[0].cpu()       ,	_UpperCamelCase     )     )
							self.assertTrue(torch.equal(resulta.b[1].cpu()       ,	_UpperCamelCase     )     )
							self.assertEqual(resulta.c       ,	1     )

							UpperCAmelCase_					:   List[Any]   =		send_to_device(UserDict({'a': tensor, 'b': [tensor, tensor], 'c': 1}     )       ,	_UpperCamelCase     )
							self.assertIsInstance(_UpperCamelCase       ,	_UpperCamelCase     )
							self.assertTrue(torch.equal(resulta['a'].cpu()       ,	_UpperCamelCase     )     )
							self.assertIsInstance(resulta['b']       ,	_UpperCamelCase     )
							self.assertTrue(torch.equal(resulta['b'][0].cpu()       ,	_UpperCamelCase     )     )
							self.assertTrue(torch.equal(resulta['b'][1].cpu()       ,	_UpperCamelCase     )     )
							self.assertEqual(resulta['c']       ,	1     )



		def       __UpperCAmelCase    (							self     )			->							Optional[Any]:
							with self.assertRaises(_UpperCamelCase     ) as cm:
												UpperCAmelCase_					:   Union[str, Any]   =		recursively_apply(torch.tensor       ,	(torch.tensor(1     ), 1)       ,	error_on_other_type=_UpperCamelCase     )
							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       __UpperCAmelCase    (							self     )			->							str:
							UpperCAmelCase_					:   Union[str, Any]   =		torch.tensor([1, 2, 3, 4, 5]     )
							self.assertEqual(listify(_UpperCamelCase     )       ,	[1, 2, 3, 4, 5]     )

							UpperCAmelCase_					:   Any   =		torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]     )
							self.assertEqual(listify(_UpperCamelCase     )       ,	[[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]     )

							UpperCAmelCase_					:   int   =		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(_UpperCamelCase     )       ,	[[[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       __UpperCAmelCase    (							self     )			->							Optional[int]:
							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       __UpperCAmelCase    (							self     )			->							Optional[int]:
							UpperCAmelCase_					:   List[Any]   =		RegressionModel()
							UpperCAmelCase_					:   List[str]   =		model.forward
							UpperCAmelCase_					:   Optional[int]   =		convert_outputs_to_fpaa(model.forward     )
							UpperCAmelCase_					:   Tuple   =		extract_model_from_parallel(_UpperCamelCase       ,	keep_fpaa_wrapper=_UpperCamelCase     )
							UpperCAmelCase_					:   Optional[Any]   =		pickle.dumps(_UpperCamelCase     )



		@require_cuda
		def       __UpperCAmelCase    (							self     )			->							Dict:
							UpperCAmelCase_					:   int   =		RegressionModel()
							UpperCAmelCase_					:   Optional[int]   =		model.forward
							UpperCAmelCase_					:   int   =		torch.cuda.amp.autocast(dtype=torch.floataa     )(model.forward     )
							UpperCAmelCase_					:   Tuple   =		convert_outputs_to_fpaa(model.forward     )
							UpperCAmelCase_					:   List[Any]   =		extract_model_from_parallel(_UpperCamelCase       ,	keep_fpaa_wrapper=_UpperCamelCase     )
							UpperCAmelCase_					:   Tuple   =		pickle.dumps(_UpperCamelCase     )



		@require_cuda
		@require_torch_min_version(version='2.0'     )
		def       __UpperCAmelCase    (							self     )			->							Optional[int]:
							UpperCAmelCase_					:   Union[str, Any]   =		RegressionModel()
							UpperCAmelCase_					:   Any   =		model.forward
							UpperCAmelCase_					:   str   =		torch.cuda.amp.autocast(dtype=torch.floataa     )(model.forward     )
							UpperCAmelCase_					:   Any   =		convert_outputs_to_fpaa(model.forward     )
							UpperCAmelCase_					:   int   =		torch.compile(model.forward       ,	backend='inductor'     )
							UpperCAmelCase_					:   Tuple   =		torch.randn(4       ,	1_0     ).cuda()
							UpperCAmelCase_					:   Optional[int]   =		model(_UpperCamelCase     )



		def       __UpperCAmelCase    (							self     )			->							Tuple:
							UpperCAmelCase_					:   List[Any]   =		RegressionModel()
							# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
							UpperCAmelCase_					:   Tuple   =		torch.nn.parallel.DataParallel(_UpperCamelCase     )
							UpperCAmelCase_					:   Tuple   =		extract_model_from_parallel(_UpperCamelCase     )

							self.assertEqual(_UpperCamelCase       ,	_UpperCamelCase     )



		@require_torch_min_version(version='2.0'     )
		def       __UpperCAmelCase    (							self     )			->							Optional[int]:
							UpperCAmelCase_					:   Tuple   =		RegressionModel()
							UpperCAmelCase_					:   int   =		torch.compile(_UpperCamelCase     )

							# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
							UpperCAmelCase_					:   List[str]   =		torch.nn.parallel.DataParallel(_UpperCamelCase     )
							UpperCAmelCase_					:   Optional[Any]   =		torch.compile(_UpperCamelCase     )
							UpperCAmelCase_					:   Any   =		extract_model_from_parallel(_UpperCamelCase     )

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



		def       __UpperCAmelCase    (							self     )			->							str:
							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']     )     )














import os
import unittest

from transformers.models.bertweet.tokenization_bertweet import VOCAB_FILES_NAMES, BertweetTokenizer

from ...test_tokenization_common import TokenizerTesterMixin



class 						lowerCamelCase   (_snake_case				,						unittest.TestCase    ):







		'''simple docstring'''



		_snake_case					:					Tuple 		=						BertweetTokenizer
		_snake_case					:					List[str] 		=						False



		def       __UpperCAmelCase    (							self     )			->							Optional[int]:
							super().setUp()

							# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
							UpperCAmelCase_					:   Optional[int]   =		['I', 'm', 'V@@', 'R@@', 'r', 'e@@']
							UpperCAmelCase_					:   Dict   =		dict(zip(_UpperCamelCase       ,	range(len(_UpperCamelCase     )     )     )     )
							UpperCAmelCase_					:   List[Any]   =		['#version: 0.2', 'a m</w>']
							UpperCAmelCase_					:   Union[str, Any]   =		{'unk_token': '<unk>'}

							UpperCAmelCase_					:   Optional[Any]   =		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'       ,	encoding='utf-8'     ) as fp:
												for token in vocab_tokens:
																	fp.write(f"{token} {vocab_tokens[token]}\n"     )
							with open(self.merges_file       ,	'w'       ,	encoding='utf-8'     ) as fp:
												fp.write('\n'.join(_UpperCamelCase     )     )



		def       __UpperCAmelCase    (							self       ,	**_UpperCamelCase     )			->							List[Any]:
							kwargs.update(self.special_tokens_map     )
							return BertweetTokenizer.from_pretrained(self.tmpdirname       ,	**_UpperCamelCase     )



		def       __UpperCAmelCase    (							self       ,	_UpperCamelCase     )			->							List[Any]:
							UpperCAmelCase_					:   Any   =		'I am VinAI Research'
							UpperCAmelCase_					:   Any   =		'I <unk> m V<unk> <unk> <unk> I Re<unk> e<unk> <unk> <unk> <unk>'
							return input_text, output_text



		def       __UpperCAmelCase    (							self     )			->							Tuple:
							UpperCAmelCase_					:   Union[str, Any]   =		BertweetTokenizer(self.vocab_file       ,	self.merges_file       ,	**self.special_tokens_map     )
							UpperCAmelCase_					:   Optional[Any]   =		'I am VinAI Research'
							UpperCAmelCase_					:   str   =		'I a@@ m V@@ i@@ n@@ A@@ I R@@ e@@ s@@ e@@ a@@ r@@ c@@ h'.split()
							UpperCAmelCase_					:   Any   =		tokenizer.tokenize(_UpperCamelCase     )
							self.assertListEqual(_UpperCamelCase       ,	_UpperCamelCase     )

							UpperCAmelCase_					:   Optional[int]   =		tokens + [tokenizer.unk_token]

							UpperCAmelCase_					:   List[str]   =		[4, 3, 5, 6, 3, 3, 3, 4, 7, 9, 3, 9, 3, 3, 3, 3, 3]
							self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCamelCase     )       ,	_UpperCamelCase     )






