




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


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


class       _a  (     unittest.TestCase			):
       def    A				(     self						:     List[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						:     Dict    ):



           '''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						:     List[Any]    ):



           '''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, 10]]    )
           self.assertEqual(listify(lowercase    )       ,    [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]    )

           UpperCAmelCase      	=		torch.tensor([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]    )
           self.assertEqual(
               listify(lowercase    )       ,    [[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]    )
       def    A				(     self						:     Optional[int]    ):



           '''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						:     Any    ):



           '''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						:     Union[str, Any]    ):



           '''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    ):



           '''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       ,    10    ).cuda()
           UpperCAmelCase      	=		model(lowercase    )
       def    A				(     self						:     Optional[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						:     List[str]    ):



           '''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						:     Any    ):



           '''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''']    )    )










'''simple docstring'''







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       _a  (     __a							,							unittest.TestCase			):
       __a    :					str           = HerbertTokenizer
       __a    :					Optional[Any]           = HerbertTokenizerFast
       __a    :					Any           = True
       def    A				(     self						:     Any    ):



           '''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      	=		f_data.read().replace('''\n\n'''       ,    '''\n'''    ).strip()

           UpperCAmelCase      	=		[
               '''<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      	=		dict(zip(lowercase       ,    range(len(lowercase    )    )    )    )
           UpperCAmelCase      	=		['''l o 123''', '''lo w 1456''', '''e r</w> 1789''', '''''']

           UpperCAmelCase      	=		os.path.join(self.tmpdirname       ,    VOCAB_FILES_NAMES['''vocab_file''']    )
           UpperCAmelCase      	=		os.path.join(self.tmpdirname       ,    VOCAB_FILES_NAMES['''merges_file''']    )
           with open(self.vocab_file       ,    '''w'''    ) as fp:
               fp.write(json.dumps(lowercase    )    )
           with open(self.merges_file       ,    '''w'''    ) as fp:
               fp.write('''\n'''.join(lowercase    )    )
       def    A				(     self						:     List[Any]       ,    lowercase						:     Optional[Any]    ):



           '''simple docstring'''

           UpperCAmelCase      	=		'''lower newer'''
           UpperCAmelCase      	=		'''lower newer'''
           return input_text, output_text
       def    A				(     self						:     int    ):



           '''simple docstring'''

           UpperCAmelCase      	=		self.tokenizer_class(vocab_file=self.vocab_file       ,    merges_file=self.merges_file    )

           UpperCAmelCase      	=		'''lower'''
           UpperCAmelCase      	=		['''low''', '''er</w>''']
           UpperCAmelCase      	=		tokenizer.tokenize(lowercase    )
           self.assertListEqual(lowercase       ,    lowercase    )

           UpperCAmelCase      	=		tokens + ['''<unk>''']
           UpperCAmelCase      	=		[16, 17, 23]
           self.assertListEqual(tokenizer.convert_tokens_to_ids(lowercase    )       ,    lowercase    )
       def    A				(     self						:     str    ):



           '''simple docstring'''

           if not self.test_rust_tokenizer:
               return

           UpperCAmelCase      	=		self.get_tokenizer()
           UpperCAmelCase      	=		self.get_rust_tokenizer()

           UpperCAmelCase      	=		'''lower,newer'''

           UpperCAmelCase      	=		tokenizer.tokenize(lowercase    )
           UpperCAmelCase      	=		rust_tokenizer.tokenize(lowercase    )
           self.assertListEqual(lowercase       ,    lowercase    )

           UpperCAmelCase      	=		tokenizer.encode(lowercase       ,    add_special_tokens=lowercase    )
           UpperCAmelCase      	=		rust_tokenizer.encode(lowercase       ,    add_special_tokens=lowercase    )
           self.assertListEqual(lowercase       ,    lowercase    )

           UpperCAmelCase      	=		self.get_rust_tokenizer()
           UpperCAmelCase      	=		tokenizer.encode(lowercase    )
           UpperCAmelCase      	=		rust_tokenizer.encode(lowercase    )
           self.assertListEqual(lowercase       ,    lowercase    )







       @slow
       def    A				(     self						:     int    ):



           '''simple docstring'''

           UpperCAmelCase      	=		self.tokenizer_class.from_pretrained('''allegro/herbert-base-cased'''    )

           UpperCAmelCase      	=		tokenizer.encode('''konstruowanie sekwencji'''       ,    add_special_tokens=lowercase    )
           UpperCAmelCase      	=		tokenizer.encode('''konstruowanie wielu sekwencji'''       ,    add_special_tokens=lowercase    )

           UpperCAmelCase      	=		tokenizer.build_inputs_with_special_tokens(lowercase    )
           UpperCAmelCase      	=		tokenizer.build_inputs_with_special_tokens(lowercase       ,    lowercase    )

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




