



'''simple docstring'''




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


a   :			str					  =    logging.get_logger(__name__)

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

class 			UpperCamelCase_ (							__magic_name__      ,     __magic_name__			):
	lowercase          = 'focalnet'





	def __init__(  self		,		A=224		,		A=4		,		A=3		,		A=96		,		A=False		,		A=[192, 384, 768, 768]		,		A=[2, 2, 6, 2]		,		A=[2, 2, 2, 2]		,		A=[3, 3, 3, 3]		,		A="gelu"		,		A=4.0		,		A=0.0		,		A=0.1		,		A=False		,		A=1e-4		,		A=False		,		A=False		,		A=False		,		A=0.0_2		,		A=1e-5		,		A=32		,		A=None		,		A=None		,		**A		,		)			->			List[str]:
								super().__init__(**A    )

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






'''simple docstring'''




# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import torch

from accelerate import PartialState
from accelerate.utils.operations import broadcast, gather, gather_object, pad_across_processes, reduce



def       __lowerCamelCase  (					_lowercase		)      ->			Tuple:
							return (torch.arange(state.num_processes		) + 1.0 + (state.num_processes * state.process_index)).to(state.device		)



def       __lowerCamelCase  (					_lowercase		)      ->			List[Any]:
							UpperCAmelCase     :		Any							     =		create_tensor(_lowercase		)
							UpperCAmelCase     :		Union[str, Any]							     =		gather(_lowercase		)
							assert gathered_tensor.tolist() == list(range(1	,				state.num_processes**2 + 1		)		)



def       __lowerCamelCase  (					_lowercase		)      ->			Optional[int]:
							UpperCAmelCase     :		Any							     =		[state.process_index]
							UpperCAmelCase     :		Union[str, Any]							     =		gather_object(_lowercase		)
							assert len(_lowercase		) == state.num_processes, F'''{gathered_obj}, {len(_lowercase		)} != {state.num_processes}'''
							assert gathered_obj == list(range(state.num_processes		)		), F'''{gathered_obj} != {list(range(state.num_processes		)		)}'''



def       __lowerCamelCase  (					_lowercase		)      ->			List[Any]:
							UpperCAmelCase     :		Optional[int]							     =		create_tensor(_lowercase		)
							UpperCAmelCase     :		List[str]							     =		broadcast(_lowercase		)
							assert broadcasted_tensor.shape == torch.Size([state.num_processes]		)
							assert broadcasted_tensor.tolist() == list(range(1	,				state.num_processes + 1		)		)



def       __lowerCamelCase  (					_lowercase		)      ->			Tuple:
							# We need to pad the tensor with one more element if we are the main process
							# to ensure that we can pad
							if state.is_main_process:
														UpperCAmelCase     :		Optional[Any]							     =		torch.arange(state.num_processes + 1		).to(state.device		)
							else:
														UpperCAmelCase     :		Tuple							     =		torch.arange(state.num_processes		).to(state.device		)
							UpperCAmelCase     :		Optional[Any]							     =		pad_across_processes(_lowercase		)
							assert padded_tensor.shape == torch.Size([state.num_processes + 1]		)
							if not state.is_main_process:
														assert padded_tensor.tolist() == list(range(0	,				state.num_processes		)		) + [0]



def       __lowerCamelCase  (					_lowercase		)      ->			Dict:
							# For now runs on only two processes
							if state.num_processes != 2:
														return
							UpperCAmelCase     :		Optional[Any]							     =		create_tensor(_lowercase		)
							UpperCAmelCase     :		Optional[Any]							     =		reduce(_lowercase	,				"""sum"""		)
							UpperCAmelCase     :		Optional[Any]							     =		torch.tensor([4.0, 6]		).to(state.device		)
							assert torch.allclose(_lowercase	,				_lowercase		), F'''{reduced_tensor} != {truth_tensor}'''



def       __lowerCamelCase  (					_lowercase		)      ->			Optional[Any]:
							# For now runs on only two processes
							if state.num_processes != 2:
														return
							UpperCAmelCase     :		Tuple							     =		create_tensor(_lowercase		)
							UpperCAmelCase     :		Optional[int]							     =		reduce(_lowercase	,				"""mean"""		)
							UpperCAmelCase     :		str							     =		torch.tensor([2.0, 3]		).to(state.device		)
							assert torch.allclose(_lowercase	,				_lowercase		), F'''{reduced_tensor} != {truth_tensor}'''



def       __lowerCamelCase  (					_lowercase		)      ->			Optional[int]:
							# For xla_spawn (TPUs)
							main()



def       __lowerCamelCase  (					)      ->			int:
							UpperCAmelCase     :		List[Any]							     =		PartialState()
							state.print(F'''State: {state}'''		)
							state.print("""testing gather"""		)
							test_gather(_lowercase		)
							state.print("""testing gather_object"""		)
							test_gather_object(_lowercase		)
							state.print("""testing broadcast"""		)
							test_broadcast(_lowercase		)
							state.print("""testing pad_across_processes"""		)
							test_pad_across_processes(_lowercase		)
							state.print("""testing reduce_sum"""		)
							test_reduce_sum(_lowercase		)
							state.print("""testing reduce_mean"""		)
							test_reduce_mean(_lowercase		)


if __name__ == "__main__":
							main()

