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


lowercase_      							=       logging.get_logger(__name__)

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





class   __UpperCamelCase      (				__SCREAMING_SNAKE_CASE      ,		__SCREAMING_SNAKE_CASE    ):






     """simple docstring"""







     lowerCAmelCase_									=       'focalnet'



     def __init__(							self				:				List[Any]			,		_A				:				List[Any]=224			,		_A				:				Optional[int]=4			,		_A				:				Union[str, Any]=3			,		_A				:				str=96			,		_A				:				Dict=False			,		_A				:				int=[192, 384, 768, 768]			,		_A				:				Optional[Any]=[2, 2, 6, 2]			,		_A				:				Union[str, Any]=[2, 2, 2, 2]			,		_A				:				Tuple=[3, 3, 3, 3]			,		_A				:				List[Any]="gelu"			,		_A				:				Dict=4.0			,		_A				:				str=0.0			,		_A				:				Optional[int]=0.1			,		_A				:				Optional[int]=False			,		_A				:				int=1e-4			,		_A				:				List[str]=False			,		_A				:				str=False			,		_A				:				int=False			,		_A				:				Any=0.02			,		_A				:				Optional[int]=1e-5			,		_A				:				int=32			,		_A				:				List[str]=None			,		_A				:				Tuple=None			,		**_A				:				Dict			,		):







       """simple docstring"""




       super().__init__(**_a      )

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




"""simple docstring"""




import inspect
import os
import unittest

import torch

import accelerate
from accelerate import Accelerator
from accelerate.test_utils import execute_subprocess_async, require_multi_gpu
from accelerate.utils import patch_environment
class 					__lowerCAmelCase					(   unittest.TestCase       ):


		'''simple docstring'''



		def 			__UpperCAmelCase       (  self      ):
				__a           =			inspect.getfile(accelerate.test_utils      )
				__a           =			os.path.sep.join(mod_file.split(os.path.sep      )[:-1] + ['''scripts''', '''test_script.py''']      )
				__a           =			os.path.sep.join(
				    mod_file.split(os.path.sep      )[:-1] + ['''scripts''', '''test_distributed_data_loop.py''']      )
				__a           =			os.path.sep.join(mod_file.split(os.path.sep      )[:-1] + ['''scripts''', '''test_ops.py''']      )

		@require_multi_gpu
		def 			__UpperCAmelCase       (  self      ):
				print(f'''Found {torch.cuda.device_count()} devices.'''      )
				__a           =			['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.test_file_path]
				with patch_environment(omp_num_threads=1      ):
						execute_subprocess_async(_a							, env=os.environ.copy()      )

		@require_multi_gpu
		def 			__UpperCAmelCase       (  self      ):
				print(f'''Found {torch.cuda.device_count()} devices.'''      )
				__a           =			['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.operation_file_path]
				print(f'''Command: {cmd}'''      )
				with patch_environment(omp_num_threads=1      ):
						execute_subprocess_async(_a							, env=os.environ.copy()      )

		@require_multi_gpu
		def 			__UpperCAmelCase       (  self      ):
				__a           =			['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__      )]
				with patch_environment(omp_num_threads=1      ):
						execute_subprocess_async(_a							, env=os.environ.copy()      )





		@require_multi_gpu
		def 			__UpperCAmelCase       (  self      ):
				print(f'''Found {torch.cuda.device_count()} devices, using 2 devices only'''      )
				__a           =			['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.data_loop_file_path]
				with patch_environment(omp_num_threads=1							, cuda_visible_devices='''0,1'''      ):
						execute_subprocess_async(_a							, env=os.environ.copy()      )


if __name__ == "__main__":
				lowercase_			     =  Accelerator()
				lowercase_			     =  (accelerator.state.process_index + 2, 1_0)
				lowercase_			     =  torch.randint(0, 1_0, shape).to(accelerator.device)

				lowercase_			     =  ""

				lowercase_			     =  accelerator.pad_across_processes(tensor)
				if tensora.shape[0] != accelerator.state.num_processes + 1:
								error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0."
				if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor):
								error_msg += "Tensors have different values."
				if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0):
								error_msg += "Padding was not done with the right value (0)."

				lowercase_			     =  accelerator.pad_across_processes(tensor, pad_first=True)
				if tensora.shape[0] != accelerator.state.num_processes + 1:
								error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0."
				lowercase_			     =  accelerator.state.num_processes - accelerator.state.process_index - 1
				if not torch.equal(tensora[index:], tensor):
								error_msg += "Tensors have different values."
				if not torch.all(tensora[:index] == 0):
								error_msg += "Padding was not done with the right value (0)."

				# Raise error at the end to make sure we don't stop at the first failure.
				if len(error_msg) > 0:
								raise ValueError(error_msg)






