




'''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 :    str			   =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 unittest

from transformers import is_torch_available, load_tool

from .test_tools_common import ToolTesterMixin


if is_torch_available():
   import torch


class       _a  (     unittest.TestCase							,							__a			):
       def    A				(     self						:     str    ):



           '''simple docstring'''

           UpperCAmelCase      	=		load_tool('''speech-to-text'''    )
           self.tool.setup()
       def    A				(     self						:     Dict    ):



           '''simple docstring'''

           UpperCAmelCase      	=		self.tool(torch.ones(3_000    )    )
           self.assertEqual(lowercase       ,    ''' you'''    )







       def    A				(     self						:     Dict    ):



           '''simple docstring'''

           UpperCAmelCase      	=		self.tool(audio=torch.ones(3_000    )    )
           self.assertEqual(lowercase       ,    ''' you'''    )




