
from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


lowerCamelCase__     :		Optional[Any]        = logging.get_logger(__name__)

lowerCamelCase__     :		str        = {
    """microsoft/focalnet-tiny""": """https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json""",
}



class    _snake_case (						UpperCAmelCase_	,    UpperCAmelCase_				):
 __lowerCAmelCase	:  List[Any]	   =      'focalnet'


 def __init__(       self       ,	SCREAMING_SNAKE_CASE_=2_24       ,	SCREAMING_SNAKE_CASE_=4       ,	SCREAMING_SNAKE_CASE_=3       ,	SCREAMING_SNAKE_CASE_=96       ,	SCREAMING_SNAKE_CASE_=False       ,	SCREAMING_SNAKE_CASE_=[1_92, 3_84, 7_68, 7_68]       ,	SCREAMING_SNAKE_CASE_=[2, 2, 6, 2]       ,	SCREAMING_SNAKE_CASE_=[2, 2, 2, 2]       ,	SCREAMING_SNAKE_CASE_=[3, 3, 3, 3]       ,	SCREAMING_SNAKE_CASE_="gelu"       ,	SCREAMING_SNAKE_CASE_=4.0       ,	SCREAMING_SNAKE_CASE_=0.0       ,	SCREAMING_SNAKE_CASE_=0.1       ,	SCREAMING_SNAKE_CASE_=False       ,	SCREAMING_SNAKE_CASE_=1E-4       ,	SCREAMING_SNAKE_CASE_=False       ,	SCREAMING_SNAKE_CASE_=False       ,	SCREAMING_SNAKE_CASE_=False       ,	SCREAMING_SNAKE_CASE_=0.0_2       ,	SCREAMING_SNAKE_CASE_=1E-5       ,	SCREAMING_SNAKE_CASE_=32       ,	SCREAMING_SNAKE_CASE_=None       ,	SCREAMING_SNAKE_CASE_=None       ,	**SCREAMING_SNAKE_CASE_       ,	):


   '''simple docstring'''




   super().__init__(**SCREAMING_SNAKE_CASE_)

   lowercase__	: Dict     		=   image_size
   lowercase__	: List[str]     		=   patch_size
   lowercase__	: str     		=   num_channels
   lowercase__	: str     		=   embed_dim
   lowercase__	: List[Any]     		=   use_conv_embed
   lowercase__	: Union[str, Any]     		=   hidden_sizes
   lowercase__	: Tuple     		=   depths
   lowercase__	: Union[str, Any]     		=   focal_levels
   lowercase__	: List[str]     		=   focal_windows
   lowercase__	: str     		=   hidden_act
   lowercase__	: str     		=   mlp_ratio
   lowercase__	: Optional[int]     		=   hidden_dropout_prob
   lowercase__	: Optional[int]     		=   drop_path_rate
   lowercase__	: Optional[Any]     		=   use_layerscale
   lowercase__	: str     		=   layerscale_value
   lowercase__	: Optional[Any]     		=   use_post_layernorm
   lowercase__	: Any     		=   use_post_layernorm_in_modulation
   lowercase__	: Optional[int]     		=   normalize_modulator
   lowercase__	: Optional[int]     		=   initializer_range
   lowercase__	: Tuple     		=   layer_norm_eps
   lowercase__	: str     		=   encoder_stride
   lowercase__	: int     		=   ["""stem"""] + [f'stage{idx}' for idx in range(1       ,	len(self.depths) + 1)]
   lowercase__		,     lowercase__	: List[str]     		=   get_aligned_output_features_output_indices(
       out_features=SCREAMING_SNAKE_CASE_       ,	out_indices=SCREAMING_SNAKE_CASE_       ,	stage_names=self.stage_names)








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 		UpperCamelCase						(   )		->						List[Any]:


  '''simple docstring'''






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



class    _snake_case (						nn.Module				):


 def __init__(       self):


   '''simple docstring'''




   super().__init__()
   lowercase__	: Optional[Any]     		=   nn.Linear(3       ,	4)
   lowercase__	: Union[str, Any]     		=   nn.BatchNormad(4)
   lowercase__	: str     		=   nn.Linear(4       ,	5)



 def   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




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



class    _snake_case (						unittest.TestCase				):


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: List[str]     		=   []

   @find_executable_batch_size(starting_batch_size=1_28)
   def mock_training_loop_function(SCREAMING_SNAKE_CASE_):
     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_       ,	[1_28, 64, 32, 16, 8])


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: int     		=   []

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

   lowercase__		,     lowercase__	: int     		=   mock_training_loop_function("""hello""")
   self.assertListEqual(SCREAMING_SNAKE_CASE_       ,	[1_28, 64, 32, 16, 8])
   self.assertListEqual([bs, arga]       ,	[8, """hello"""])


 def   lowercase__  (       self):


   '''simple docstring'''




   @find_executable_batch_size(starting_batch_size=0)
   def mock_training_loop_function(SCREAMING_SNAKE_CASE_):
     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   lowercase__  (       self):


   '''simple docstring'''




   @find_executable_batch_size(starting_batch_size=16)
   def mock_training_loop_function(SCREAMING_SNAKE_CASE_):
     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   lowercase__  (       self):


   '''simple docstring'''




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

   with self.assertRaises(SCREAMING_SNAKE_CASE_) 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   lowercase__  (       self):


   '''simple docstring'''




   @find_executable_batch_size(starting_batch_size=16)
   def mock_training_loop_function(SCREAMING_SNAKE_CASE_):
     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   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: str     		=   torch.cuda.memory_allocated()
   lowercase__	: str     		=   ModelForTest()
   model.cuda()
   self.assertGreater(torch.cuda.memory_allocated()       ,	SCREAMING_SNAKE_CASE_)
   lowercase__	: Tuple     		=   release_memory(SCREAMING_SNAKE_CASE_)
   self.assertEqual(torch.cuda.memory_allocated()       ,	SCREAMING_SNAKE_CASE_)






