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


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






class 		_lowercase (						unittest.TestCase      ):



						"""simple docstring"""




						def   UpperCAmelCase_ (				self							:    Optional[int]				)				->				Optional[int]:
													'''simple docstring'''



													__UpperCamelCase							=torch.randn(5     , 2				)
													__UpperCamelCase							=torch.device('''cuda'''				) if torch.cuda.is_available() else torch.device('''cpu'''				)

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

													__UpperCamelCase							=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							=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							=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							=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							:    Any				)				->				Optional[int]:
													'''simple docstring'''



													with self.assertRaises(UpperCamelCase__				) as cm:
																				__UpperCamelCase							=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							:    List[Any]				)				->				List[Any]:
													'''simple docstring'''



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

													__UpperCamelCase							=torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]				)
													self.assertEqual(listify(UpperCamelCase__				)     , [[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(UpperCamelCase__				)     , [[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]				)





						def   UpperCAmelCase_ (				self							:    Tuple				)				->				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   UpperCAmelCase_ (				self							:    List[Any]				)				->				Tuple:
													'''simple docstring'''



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





						@require_cuda
						def   UpperCAmelCase_ (				self							:    str				)				->				Dict:
													'''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(UpperCamelCase__     , keep_fpaa_wrapper=UpperCamelCase__				)
													__UpperCamelCase							=pickle.dumps(UpperCamelCase__				)





						@require_cuda
						@require_torch_min_version(version='''2.0'''				)
						def   UpperCAmelCase_ (				self							:    List[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     , 10				).cuda()
													__UpperCamelCase							=model(UpperCamelCase__				)





						def   UpperCAmelCase_ (				self							:    Any				)				->				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(UpperCamelCase__				)
													__UpperCamelCase							=extract_model_from_parallel(UpperCamelCase__				)

													self.assertEqual(UpperCamelCase__     , UpperCamelCase__				)





						@require_torch_min_version(version='''2.0'''				)
						def   UpperCAmelCase_ (				self							:    Optional[int]				)				->				Tuple:
													'''simple docstring'''



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

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

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





						def   UpperCAmelCase_ (				self							:    Dict				)				->				str:
													'''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"""

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__lowercase    =				{
    '''configuration_videomae''': ['''VIDEOMAE_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''VideoMAEConfig'''],
}

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__lowercase    =				[
				    '''VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST''',
				    '''VideoMAEForPreTraining''',
				    '''VideoMAEModel''',
				    '''VideoMAEPreTrainedModel''',
				    '''VideoMAEForVideoClassification''',
				]

try:
				if not is_vision_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__lowercase    =				['''VideoMAEFeatureExtractor''']
				__lowercase    =				['''VideoMAEImageProcessor''']

if TYPE_CHECKING:
				from .configuration_videomae import VIDEOMAE_PRETRAINED_CONFIG_ARCHIVE_MAP, VideoMAEConfig

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_videomae import (
								    VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST,
								    VideoMAEForPreTraining,
								    VideoMAEForVideoClassification,
								    VideoMAEModel,
								    VideoMAEPreTrainedModel,
								)

				try:
								if not is_vision_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .feature_extraction_videomae import VideoMAEFeatureExtractor
								from .image_processing_videomae import VideoMAEImageProcessor

else:
				import sys

				__lowercase    =				_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



