


"""simple docstring"""


import unittest

import torch
from torch import nn

from accelerate.test_utils import require_cuda
from accelerate.utils.memory import find_executable_batch_size, release_memory


def _lowerCamelCase			(	):

			'''simple docstring'''




			raise RuntimeError("CUDA out of memory."   )



class 						_UpperCamelCase						(     nn.Module	):
							'''simple docstring'''







							def __init__(  self			):
										super().__init__()
										__lowerCAmelCase       					=    nn.Linear(3					,     4			)
										__lowerCAmelCase       					=    nn.BatchNormad(4			)
										__lowerCAmelCase       					=    nn.Linear(4					,     5			)



							def     snake_case       (  self					,     __a			):
										return self.lineara(self.batchnorm(self.lineara(__a			)			)			)



class 						_UpperCamelCase						(     unittest.TestCase	):
							'''simple docstring'''







							def     snake_case       (  self			):
										__lowerCAmelCase       					=    []

										@find_executable_batch_size(starting_batch_size=1_28			)
										def mock_training_loop_function(__a			):
													nonlocal batch_sizes
													batch_sizes.append(__a			)
													if batch_size != 8:
																raise_fake_out_of_memory()

										mock_training_loop_function()
										self.assertListEqual(__a					,     [1_28, 64, 32, 16, 8]			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    []

										@find_executable_batch_size(starting_batch_size=1_28			)
										def mock_training_loop_function(__a					,     __a			):
													nonlocal batch_sizes
													batch_sizes.append(__a			)
													if batch_size != 8:
																raise_fake_out_of_memory()
													return batch_size, arga

										__lowerCAmelCase					,			__lowerCAmelCase       					=    mock_training_loop_function("hello"			)
										self.assertListEqual(__a					,     [1_28, 64, 32, 16, 8]			)
										self.assertListEqual([bs, arga]					,     [8, "hello"]			)

							def     snake_case       (  self			):
										@find_executable_batch_size(starting_batch_size=0			)
										def mock_training_loop_function(__a			):
													pass

										with self.assertRaises(__a			) as cm:
													mock_training_loop_function()
													self.assertIn("No executable batch size found, reached zero."					,     cm.exception.args[0]			)

							def     snake_case       (  self			):
										@find_executable_batch_size(starting_batch_size=16			)
										def mock_training_loop_function(__a			):
													if batch_size > 0:
																raise_fake_out_of_memory()
													pass

										with self.assertRaises(__a			) as cm:
													mock_training_loop_function()
													self.assertIn("No executable batch size found, reached zero."					,     cm.exception.args[0]			)

							def     snake_case       (  self			):
										@find_executable_batch_size(starting_batch_size=1_28			)
										def mock_training_loop_function(__a					,     __a					,     __a			):
													if batch_size != 8:
																raise raise_fake_out_of_memory()

										with self.assertRaises(__a			) as cm:
													mock_training_loop_function(1_28					,     "hello"					,     "world"			)
													self.assertIn("Batch size was passed into `f`"					,     cm.exception.args[0]			)
													self.assertIn("`f(arg1='hello', arg2='world')"					,     cm.exception.args[0]			)

							def     snake_case       (  self			):
										@find_executable_batch_size(starting_batch_size=16			)
										def mock_training_loop_function(__a			):
													raise ValueError("Oops, we had an error!"			)

										with self.assertRaises(__a			) as cm:
													mock_training_loop_function()
													self.assertIn("Oops, we had an error!"					,     cm.exception.args[0]			)



							@require_cuda
							def     snake_case       (  self			):
										__lowerCAmelCase       					=    torch.cuda.memory_allocated()
										__lowerCAmelCase       					=    ModelForTest()
										model.cuda()
										self.assertGreater(torch.cuda.memory_allocated()					,     __a			)
										__lowerCAmelCase       					=    release_memory(__a			)
										self.assertEqual(torch.cuda.memory_allocated()					,     __a			)








"""simple docstring"""


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


A  :     Union[str, Any]    					=    {
    "configuration_x_clip": [
        "XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "XCLIPConfig",
        "XCLIPTextConfig",
        "XCLIPVisionConfig",
    ],
    "processing_x_clip": ["XCLIPProcessor"],
}

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	A  :     Optional[int]    					=    [
	    "XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST",
	    "XCLIPModel",
	    "XCLIPPreTrainedModel",
	    "XCLIPTextModel",
	    "XCLIPVisionModel",
	]

if TYPE_CHECKING:
	from .configuration_x_clip import (
	    XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    XCLIPConfig,
	    XCLIPTextConfig,
	    XCLIPVisionConfig,
	)
	from .processing_x_clip import XCLIPProcessor

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_x_clip import (
		    XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
		    XCLIPModel,
		    XCLIPPreTrainedModel,
		    XCLIPTextModel,
		    XCLIPVisionModel,
		)

else:
	import sys

	A  :     Union[str, Any]    					=    _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)




