



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  __lowerCAmelCase			(					unittest.TestCase	):




				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   torch.randn(5						,    2		)
									_lowerCAmelCase      =   torch.device("""cuda"""		) if torch.cuda.is_available() else torch.device("""cpu"""		)

									_lowerCAmelCase      =   send_to_device(_snake_case						,    _snake_case		)
									self.assertTrue(torch.equal(resulta.cpu()						,    _snake_case		)		)

									_lowerCAmelCase      =   send_to_device((tensor, [tensor, tensor], 1)						,    _snake_case		)
									self.assertIsInstance(_snake_case						,    _snake_case		)
									self.assertTrue(torch.equal(resulta[0].cpu()						,    _snake_case		)		)
									self.assertIsInstance(resulta[1]						,    _snake_case		)
									self.assertTrue(torch.equal(resulta[1][0].cpu()						,    _snake_case		)		)
									self.assertTrue(torch.equal(resulta[1][1].cpu()						,    _snake_case		)		)
									self.assertEqual(resulta[2]						,    1		)

									_lowerCAmelCase      =   send_to_device({"""a""": tensor, """b""": [tensor, tensor], """c""": 1}						,    _snake_case		)
									self.assertIsInstance(_snake_case						,    _snake_case		)
									self.assertTrue(torch.equal(resulta["""a"""].cpu()						,    _snake_case		)		)
									self.assertIsInstance(resulta["""b"""]						,    _snake_case		)
									self.assertTrue(torch.equal(resulta["""b"""][0].cpu()						,    _snake_case		)		)
									self.assertTrue(torch.equal(resulta["""b"""][1].cpu()						,    _snake_case		)		)
									self.assertEqual(resulta["""c"""]						,    1		)

									_lowerCAmelCase      =   send_to_device(ExampleNamedTuple(a=_snake_case						,    b=[tensor, tensor]						,    c=1		)						,    _snake_case		)
									self.assertIsInstance(_snake_case						,    _snake_case		)
									self.assertTrue(torch.equal(resulta.a.cpu()						,    _snake_case		)		)
									self.assertIsInstance(resulta.b						,    _snake_case		)
									self.assertTrue(torch.equal(resulta.b[0].cpu()						,    _snake_case		)		)
									self.assertTrue(torch.equal(resulta.b[1].cpu()						,    _snake_case		)		)
									self.assertEqual(resulta.c						,    1		)

									_lowerCAmelCase      =   send_to_device(UserDict({"""a""": tensor, """b""": [tensor, tensor], """c""": 1}		)						,    _snake_case		)
									self.assertIsInstance(_snake_case						,    _snake_case		)
									self.assertTrue(torch.equal(resulta["""a"""].cpu()						,    _snake_case		)		)
									self.assertIsInstance(resulta["""b"""]						,    _snake_case		)
									self.assertTrue(torch.equal(resulta["""b"""][0].cpu()						,    _snake_case		)		)
									self.assertTrue(torch.equal(resulta["""b"""][1].cpu()						,    _snake_case		)		)
									self.assertEqual(resulta["""c"""]						,    1		)




				def 							snake_case       (							self		):



									"""simple docstring"""





									with self.assertRaises(_snake_case		) as cm:
														_lowerCAmelCase      =   recursively_apply(torch.tensor						,    (torch.tensor(1		), 1)						,    error_on_other_type=_snake_case		)
									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 							snake_case       (							self		):



									"""simple docstring"""





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

									_lowerCAmelCase      =   torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]		)
									self.assertEqual(listify(_snake_case		)						,    [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]		)

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




				def 							snake_case       (							self		):



									"""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 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   RegressionModel()
									_lowerCAmelCase      =   model.forward
									_lowerCAmelCase      =   convert_outputs_to_fpaa(model.forward		)
									_lowerCAmelCase      =   extract_model_from_parallel(_snake_case						,    keep_fpaa_wrapper=_snake_case		)
									_lowerCAmelCase      =   pickle.dumps(_snake_case		)




				@require_cuda
				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   RegressionModel()
									_lowerCAmelCase      =   model.forward
									_lowerCAmelCase      =   torch.cuda.amp.autocast(dtype=torch.floataa		)(model.forward		)
									_lowerCAmelCase      =   convert_outputs_to_fpaa(model.forward		)
									_lowerCAmelCase      =   extract_model_from_parallel(_snake_case						,    keep_fpaa_wrapper=_snake_case		)
									_lowerCAmelCase      =   pickle.dumps(_snake_case		)




				@require_cuda
				@require_torch_min_version(version="""2.0"""		)
				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   RegressionModel()
									_lowerCAmelCase      =   model.forward
									_lowerCAmelCase      =   torch.cuda.amp.autocast(dtype=torch.floataa		)(model.forward		)
									_lowerCAmelCase      =   convert_outputs_to_fpaa(model.forward		)
									_lowerCAmelCase      =   torch.compile(model.forward						,    backend="""inductor"""		)
									_lowerCAmelCase      =   torch.randn(4						,    10		).cuda()
									_lowerCAmelCase      =   model(_snake_case		)




				def 							snake_case       (							self		):



									"""simple docstring"""





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

									self.assertEqual(_snake_case						,    _snake_case		)




				@require_torch_min_version(version="""2.0"""		)
				def 							snake_case       (							self		):



									"""simple docstring"""





									_lowerCAmelCase      =   RegressionModel()
									_lowerCAmelCase      =   torch.compile(_snake_case		)

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

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




				def 							snake_case       (							self		):



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








import os

from packaging import version

from .. import __version__
from .accelerate_utils import apply_forward_hook
from .constants import (
    CONFIG_NAME,
    DEPRECATED_REVISION_ARGS,
    DIFFUSERS_CACHE,
    DIFFUSERS_DYNAMIC_MODULE_NAME,
    FLAX_WEIGHTS_NAME,
    HF_MODULES_CACHE,
    HUGGINGFACE_CO_RESOLVE_ENDPOINT,
    ONNX_EXTERNAL_WEIGHTS_NAME,
    ONNX_WEIGHTS_NAME,
    SAFETENSORS_WEIGHTS_NAME,
    TEXT_ENCODER_ATTN_MODULE,
    WEIGHTS_NAME,
)
from .deprecation_utils import deprecate
from .doc_utils import replace_example_docstring
from .dynamic_modules_utils import get_class_from_dynamic_module
from .hub_utils import (
    HF_HUB_OFFLINE,
    _add_variant,
    _get_model_file,
    extract_commit_hash,
    http_user_agent,
)
from .import_utils import (
    BACKENDS_MAPPING,
    ENV_VARS_TRUE_AND_AUTO_VALUES,
    ENV_VARS_TRUE_VALUES,
    USE_JAX,
    USE_TF,
    USE_TORCH,
    DummyObject,
    OptionalDependencyNotAvailable,
    is_accelerate_available,
    is_accelerate_version,
    is_bsa_available,
    is_flax_available,
    is_ftfy_available,
    is_inflect_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_librosa_available,
    is_note_seq_available,
    is_omegaconf_available,
    is_onnx_available,
    is_safetensors_available,
    is_scipy_available,
    is_tensorboard_available,
    is_tf_available,
    is_torch_available,
    is_torch_version,
    is_torchsde_available,
    is_transformers_available,
    is_transformers_version,
    is_unidecode_available,
    is_wandb_available,
    is_xformers_available,
    requires_backends,
)
from .logging import get_logger
from .outputs import BaseOutput
from .pil_utils import PIL_INTERPOLATION, numpy_to_pil, pt_to_pil
from .torch_utils import is_compiled_module, randn_tensor


if is_torch_available():
	from .testing_utils import (
	    floats_tensor,
	    load_hf_numpy,
	    load_image,
	    load_numpy,
	    load_pt,
	    nightly,
	    parse_flag_from_env,
	    print_tensor_test,
	    require_torch_a,
	    require_torch_gpu,
	    skip_mps,
	    slow,
	    torch_all_close,
	    torch_device,
	)
	from .torch_utils import maybe_allow_in_graph

from .testing_utils import export_to_gif, export_to_video


A__     				=	get_logger(__name__)


def 		_UpperCAmelCase    (				snake_case      ):





					"""simple docstring"""

					if version.parse(snake_case      ) < version.parse(snake_case      ):
										if "dev" in min_version:
															_lowerCAmelCase      =   (
															    """This example requires a source install from HuggingFace diffusers (see """
															    """`https://huggingface.co/docs/diffusers/installation#install-from-source`),"""
															)
										else:
															_lowerCAmelCase      =   F'This example requires a minimum version of {min_version},'
										error_message += F' but the version found is {__version__}.\n'
										raise ImportError(snake_case      )



