
"""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 __magic_name__	(				)					->						str:

	'''simple docstring'''




	raise RuntimeError('''CUDA out of memory.'''			)



class 			lowercase					( nn.Module		):






			def __init__(self  :	Tuple			)       ->	Optional[int]:





				"""simple docstring"""


				super().__init__()
				lowerCAmelCase												=  nn.Linear(3      ,4			)
				lowerCAmelCase												=  nn.BatchNormad(4			)
				lowerCAmelCase												=  nn.Linear(4      ,5			)






			def 					UpperCAmelCase							(self  :	Optional[Any]      ,SCREAMING_SNAKE_CASE_  :	int			)       ->	str:





				"""simple docstring"""


				return self.lineara(self.batchnorm(self.lineara(SCREAMING_SNAKE_CASE_			)			)			)





class 			lowercase					( unittest.TestCase		):






			def 					UpperCAmelCase							(self  :	Any			)       ->	str:





				"""simple docstring"""


				lowerCAmelCase												=  []

				@find_executable_batch_size(starting_batch_size=128			)
				def mock_training_loop_function(SCREAMING_SNAKE_CASE_  :	Optional[Any]			):
					nonlocal batch_sizes
					batch_sizes.append(SCREAMING_SNAKE_CASE_			)
					if batch_size != 8:
						raise_fake_out_of_memory()

				mock_training_loop_function()
				self.assertListEqual(SCREAMING_SNAKE_CASE_      ,[128, 64, 32, 16, 8]			)






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





				"""simple docstring"""


				lowerCAmelCase												=  []

				@find_executable_batch_size(starting_batch_size=128			)
				def mock_training_loop_function(SCREAMING_SNAKE_CASE_  :	Tuple      ,SCREAMING_SNAKE_CASE_  :	Any			):
					nonlocal batch_sizes
					batch_sizes.append(SCREAMING_SNAKE_CASE_			)
					if batch_size != 8:
						raise_fake_out_of_memory()
					return batch_size, arga

				lowerCAmelCase							,       lowerCAmelCase												=  mock_training_loop_function('''hello'''			)
				self.assertListEqual(SCREAMING_SNAKE_CASE_      ,[128, 64, 32, 16, 8]			)
				self.assertListEqual([bs, arga]      ,[8, '''hello''']			)






			def 					UpperCAmelCase							(self  :	Any			)       ->	List[str]:





				"""simple docstring"""


				@find_executable_batch_size(starting_batch_size=0			)
				def mock_training_loop_function(SCREAMING_SNAKE_CASE_  :	Optional[int]			):
					pass

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






			def 					UpperCAmelCase							(self  :	Dict			)       ->	List[Any]:





				"""simple docstring"""


				@find_executable_batch_size(starting_batch_size=16			)
				def mock_training_loop_function(SCREAMING_SNAKE_CASE_  :	int			):
					if batch_size > 0:
						raise_fake_out_of_memory()
					pass

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






			def 					UpperCAmelCase							(self  :	int			)       ->	str:





				"""simple docstring"""


				@find_executable_batch_size(starting_batch_size=128			)
				def mock_training_loop_function(SCREAMING_SNAKE_CASE_  :	List[str]      ,SCREAMING_SNAKE_CASE_  :	Union[str, Any]      ,SCREAMING_SNAKE_CASE_  :	List[Any]			):
					if batch_size != 8:
						raise raise_fake_out_of_memory()

				with self.assertRaises(SCREAMING_SNAKE_CASE_			) as cm:
					mock_training_loop_function(128      ,'''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 					UpperCAmelCase							(self  :	Dict			)       ->	int:





				"""simple docstring"""


				@find_executable_batch_size(starting_batch_size=16			)
				def mock_training_loop_function(SCREAMING_SNAKE_CASE_  :	Optional[Any]			):
					raise ValueError('''Oops, we had an error!'''			)

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






			@require_cuda
			def 					UpperCAmelCase							(self  :	List[Any]			)       ->	int:





				"""simple docstring"""


				lowerCAmelCase												=  torch.cuda.memory_allocated()
				lowerCAmelCase												=  ModelForTest()
				model.cuda()
				self.assertGreater(torch.cuda.memory_allocated()      ,SCREAMING_SNAKE_CASE_			)
				lowerCAmelCase												=  release_memory(SCREAMING_SNAKE_CASE_			)
				self.assertEqual(torch.cuda.memory_allocated()      ,SCREAMING_SNAKE_CASE_			)




"""simple docstring"""






import warnings
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCAmelCase        =				logging.get_logger(__name__)

UpperCAmelCase        =				{
    """nvidia/segformer-b0-finetuned-ade-512-512""": (
        """https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json"""
    ),
    # See all SegFormer models at https://huggingface.co/models?filter=segformer
}



class 			lowercase					( lowercase__		):
			lowercase        =     '''segformer'''






			def __init__(self  :	str      ,SCREAMING_SNAKE_CASE_  :	Optional[Any]=3      ,SCREAMING_SNAKE_CASE_  :	List[str]=4      ,SCREAMING_SNAKE_CASE_  :	Union[str, Any]=[2, 2, 2, 2]      ,SCREAMING_SNAKE_CASE_  :	List[str]=[8, 4, 2, 1]      ,SCREAMING_SNAKE_CASE_  :	int=[32, 64, 160, 256]      ,SCREAMING_SNAKE_CASE_  :	Optional[Any]=[7, 3, 3, 3]      ,SCREAMING_SNAKE_CASE_  :	Union[str, Any]=[4, 2, 2, 2]      ,SCREAMING_SNAKE_CASE_  :	List[Any]=[1, 2, 5, 8]      ,SCREAMING_SNAKE_CASE_  :	Dict=[4, 4, 4, 4]      ,SCREAMING_SNAKE_CASE_  :	Optional[int]="gelu"      ,SCREAMING_SNAKE_CASE_  :	Optional[Any]=0.0      ,SCREAMING_SNAKE_CASE_  :	Union[str, Any]=0.0      ,SCREAMING_SNAKE_CASE_  :	List[str]=0.1      ,SCREAMING_SNAKE_CASE_  :	List[Any]=0.02      ,SCREAMING_SNAKE_CASE_  :	str=0.1      ,SCREAMING_SNAKE_CASE_  :	Dict=1e-6      ,SCREAMING_SNAKE_CASE_  :	List[str]=256      ,SCREAMING_SNAKE_CASE_  :	List[str]=255      ,**SCREAMING_SNAKE_CASE_  :	Tuple      ,)       ->	int:





				"""simple docstring"""


				super().__init__(**SCREAMING_SNAKE_CASE_			)

				if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False:
					warnings.warn(
					    '''Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be'''
					    ''' removed, as the behaviour will default to that of reshape_last_stage = True.'''      ,SCREAMING_SNAKE_CASE_      ,)

				lowerCAmelCase												=  num_channels
				lowerCAmelCase												=  num_encoder_blocks
				lowerCAmelCase												=  depths
				lowerCAmelCase												=  sr_ratios
				lowerCAmelCase												=  hidden_sizes
				lowerCAmelCase												=  patch_sizes
				lowerCAmelCase												=  strides
				lowerCAmelCase												=  mlp_ratios
				lowerCAmelCase												=  num_attention_heads
				lowerCAmelCase												=  hidden_act
				lowerCAmelCase												=  hidden_dropout_prob
				lowerCAmelCase												=  attention_probs_dropout_prob
				lowerCAmelCase												=  classifier_dropout_prob
				lowerCAmelCase												=  initializer_range
				lowerCAmelCase												=  drop_path_rate
				lowerCAmelCase												=  layer_norm_eps
				lowerCAmelCase												=  decoder_hidden_size
				lowerCAmelCase												=  kwargs.get('''reshape_last_stage'''      ,SCREAMING_SNAKE_CASE_			)
				lowerCAmelCase												=  semantic_loss_ignore_index





class 			lowercase					( lowercase__		):
			lowercase        =     version.parse('''1.11'''		)






			@property
			def 					UpperCAmelCase							(self  :	Union[str, Any]			)       ->	Mapping[str, Mapping[int, str]]:





				"""simple docstring"""


				return OrderedDict(
				    [
				        ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
				    ]			)






			@property
			def 					UpperCAmelCase							(self  :	str			)       ->	float:





				"""simple docstring"""


				return 1e-4






			@property
			def 					UpperCAmelCase							(self  :	Dict			)       ->	int:





				"""simple docstring"""


				return 12


