


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


_SCREAMING_SNAKE_CASE :					Optional[Any]			=	namedtuple("""ExampleNamedTuple""", """a b c""")

class  UpperCamelCase__ ( unittest.TestCase			):






      def 	__lowercase(     self					:   List[Any]   )  -> Tuple:
           UpperCamelCase__							:  Tuple								=     torch.randn(5,		2   )
           UpperCamelCase__							:  List[str]								=     torch.device('''cuda'''   ) if torch.cuda.is_available() else torch.device('''cpu'''   )

           UpperCamelCase__							:  List[Any]								=     send_to_device(lowerCamelCase_,		lowerCamelCase_   )
           self.assertTrue(torch.equal(resulta.cpu(),		lowerCamelCase_   )   )

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

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

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

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






      def 	__lowercase(     self					:   List[str]   )  -> List[str]:

           with self.assertRaises(lowerCamelCase_   ) as cm:
                UpperCamelCase__							:  int								=     recursively_apply(torch.tensor,		(torch.tensor(1   ), 1),		error_on_other_type=lowerCamelCase_   )
           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 	__lowercase(     self					:   Optional[int]   )  -> int:
           UpperCamelCase__							:  List[str]								=     torch.tensor([1, 2, 3, 4, 5]   )
           self.assertEqual(listify(lowerCamelCase_   ),		[1, 2, 3, 4, 5]   )

           UpperCamelCase__							:  Optional[int]								=     torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]   )
           self.assertEqual(listify(lowerCamelCase_   ),		[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]   )

           UpperCamelCase__							:  Union[str, Any]								=     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(lowerCamelCase_   ),		[[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]   )






      def 	__lowercase(     self					:   str   )  -> Optional[Any]:

           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 	__lowercase(     self					:   Optional[int]   )  -> Tuple:
           UpperCamelCase__							:  Dict								=     RegressionModel()
           UpperCamelCase__							:  str								=     model.forward
           UpperCamelCase__							:  List[str]								=     convert_outputs_to_fpaa(model.forward   )
           UpperCamelCase__							:  Union[str, Any]								=     extract_model_from_parallel(lowerCamelCase_,		keep_fpaa_wrapper=lowerCamelCase_   )
           UpperCamelCase__							:  int								=     pickle.dumps(lowerCamelCase_   )






      @require_cuda
      def 	__lowercase(     self					:   Any   )  -> str:
           UpperCamelCase__							:  Tuple								=     RegressionModel()
           UpperCamelCase__							:  Optional[int]								=     model.forward
           UpperCamelCase__							:  Optional[int]								=     torch.cuda.amp.autocast(dtype=torch.floataa   )(model.forward   )
           UpperCamelCase__							:  Tuple								=     convert_outputs_to_fpaa(model.forward   )
           UpperCamelCase__							:  int								=     extract_model_from_parallel(lowerCamelCase_,		keep_fpaa_wrapper=lowerCamelCase_   )
           UpperCamelCase__							:  Union[str, Any]								=     pickle.dumps(lowerCamelCase_   )






      @require_cuda
      @require_torch_min_version(version='''2.0'''   )
      def 	__lowercase(     self					:   List[Any]   )  -> List[Any]:
           UpperCamelCase__							:  Dict								=     RegressionModel()
           UpperCamelCase__							:  List[Any]								=     model.forward
           UpperCamelCase__							:  Optional[int]								=     torch.cuda.amp.autocast(dtype=torch.floataa   )(model.forward   )
           UpperCamelCase__							:  List[Any]								=     convert_outputs_to_fpaa(model.forward   )
           UpperCamelCase__							:  str								=     torch.compile(model.forward,		backend='''inductor'''   )
           UpperCamelCase__							:  str								=     torch.randn(4,		10   ).cuda()
           UpperCamelCase__							:  str								=     model(lowerCamelCase_   )






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

           self.assertEqual(lowerCamelCase_,		lowerCamelCase_   )






      @require_torch_min_version(version='''2.0'''   )
      def 	__lowercase(     self					:   List[Any]   )  -> str:
           UpperCamelCase__							:  Optional[int]								=     RegressionModel()
           UpperCamelCase__							:  str								=     torch.compile(lowerCamelCase_   )

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

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






      def 	__lowercase(     self					:   List[str]   )  -> List[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

from apache_beam.io.filesystems import FileSystems
from apache_beam.pipeline import Pipeline

from .logging import get_logger


_SCREAMING_SNAKE_CASE :					Optional[int]			=	2 << 20  # 2mb
_SCREAMING_SNAKE_CASE :					Optional[Any]			=	get_logger(__name__)

class  UpperCamelCase__ ( __lowerCamelCase			):






      def 	__lowercase(     self					:   List[str]   )  -> List[Any]:
           UpperCamelCase__							:  int								=     self._options.get_all_options().get('''runner'''   )
           return runner in [None, "DirectRunner", "PortableRunner"]







def 						_lowercase       (     __lowerCamelCase			:					List[str]		,__lowerCamelCase			:					Any		,__lowerCamelCase			:					List[str]=False			)  ->   Optional[int]:





     '''simple docstring'''





     UpperCamelCase__							:  Any								=     FileSystems
     if fs.exists(__lowerCamelCase			):
          if force_upload:
               logger.info(F'Remote path already exist: {remote_file_path}. Overwriting it as force_upload=True.'			)
          else:
               logger.info(F'Remote path already exist: {remote_file_path}. Skipping it as force_upload=False.'			)
               return
     with fs.create(__lowerCamelCase			) as remote_file:
          with open(__lowerCamelCase		,'''rb'''			) as local_file:
               UpperCamelCase__							:  List[Any]								=     local_file.read(__lowerCamelCase			)
               while chunk:
                    remote_file.write(__lowerCamelCase			)
                    UpperCamelCase__							:  Union[str, Any]								=     local_file.read(__lowerCamelCase			)







def 						_lowercase       (     __lowerCamelCase			:					Any		,__lowerCamelCase			:					int		,__lowerCamelCase			:					str=False			)  ->   Tuple:





     '''simple docstring'''





     UpperCamelCase__							:  Optional[int]								=     FileSystems
     if os.path.exists(__lowerCamelCase			):
          if force_download:
               logger.info(F'Local path already exist: {remote_file_path}. Overwriting it as force_upload=True.'			)
          else:
               logger.info(F'Local path already exist: {remote_file_path}. Skipping it as force_upload=False.'			)
               return
     with fs.open(__lowerCamelCase			) as remote_file:
          with open(__lowerCamelCase		,'''wb'''			) as local_file:
               UpperCamelCase__							:  Any								=     remote_file.read(__lowerCamelCase			)
               while chunk:
                    local_file.write(__lowerCamelCase			)
                    UpperCamelCase__							:  Dict								=     remote_file.read(__lowerCamelCase			)




