


'''simple docstring'''




import collections
import inspect
import unittest

from transformers import FocalNetConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch
		from torch import nn

		from transformers import (
		    FocalNetBackbone,
		    FocalNetForImageClassification,
		    FocalNetForMaskedImageModeling,
		    FocalNetModel,
		)
		from transformers.models.focalnet.modeling_focalnet import FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor







class    A_	:
							"""simple docstring"""




							def __init__(		self			:str       ,							lowerCAmelCase__			:Optional[Any]       ,							lowerCAmelCase__			:List[str]=13       ,							lowerCAmelCase__			:Any=32       ,							lowerCAmelCase__			:Tuple=2       ,							lowerCAmelCase__			:Tuple=3       ,							lowerCAmelCase__			:Dict=16       ,							lowerCAmelCase__			:Optional[int]=[32, 64, 128]       ,							lowerCAmelCase__			:int=[1, 2, 1]       ,							lowerCAmelCase__			:Union[str, Any]=[2, 2, 4]       ,							lowerCAmelCase__			:Optional[int]=2       ,							lowerCAmelCase__			:Tuple=2.0       ,							lowerCAmelCase__			:Optional[Any]=True       ,							lowerCAmelCase__			:List[str]=0.0       ,							lowerCAmelCase__			:List[Any]=0.0       ,							lowerCAmelCase__			:int=0.1       ,							lowerCAmelCase__			:Any="gelu"       ,							lowerCAmelCase__			:int=False       ,							lowerCAmelCase__			:str=True       ,							lowerCAmelCase__			:Optional[int]=0.0_2       ,							lowerCAmelCase__			:str=1E-5       ,							lowerCAmelCase__			:Optional[Any]=True       ,							lowerCAmelCase__			:Dict=None       ,							lowerCAmelCase__			:Any=True       ,							lowerCAmelCase__			:Union[str, Any]=10       ,							lowerCAmelCase__			:Dict=8       ,							lowerCAmelCase__			:Optional[int]=["stage1", "stage2"]       ,							lowerCAmelCase__			:Union[str, Any]=[1, 2]       ,							)				->  Optional[Any]:


										'''simple docstring'''




										snake_case_				:					Optional[Any]   =       parent
										snake_case_				:					Dict   =       batch_size
										snake_case_				:					str   =       image_size
										snake_case_				:					Dict   =       patch_size
										snake_case_				:					Union[str, Any]   =       num_channels
										snake_case_				:					Optional[int]   =       embed_dim
										snake_case_				:					List[Any]   =       hidden_sizes
										snake_case_				:					Optional[int]   =       depths
										snake_case_				:					Tuple   =       num_heads
										snake_case_				:					str   =       window_size
										snake_case_				:					List[Any]   =       mlp_ratio
										snake_case_				:					List[Any]   =       qkv_bias
										snake_case_				:					List[str]   =       hidden_dropout_prob
										snake_case_				:					Optional[Any]   =       attention_probs_dropout_prob
										snake_case_				:					Tuple   =       drop_path_rate
										snake_case_				:					Union[str, Any]   =       hidden_act
										snake_case_				:					Optional[int]   =       use_absolute_embeddings
										snake_case_				:					str   =       patch_norm
										snake_case_				:					Optional[int]   =       layer_norm_eps
										snake_case_				:					Union[str, Any]   =       initializer_range
										snake_case_				:					str   =       is_training
										snake_case_				:					Any   =       scope
										snake_case_				:					Optional[int]   =       use_labels
										snake_case_				:					Dict   =       type_sequence_label_size
										snake_case_				:					Optional[Any]   =       encoder_stride
										snake_case_				:					str   =       out_features
										snake_case_				:					List[str]   =       out_indices

							def 							_A	(		self			:int				)				->  Union[str, Any]:


										'''simple docstring'''




										snake_case_				:					Optional[Any]   =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

										snake_case_				:					List[str]   =       None
										if self.use_labels:
													snake_case_				:					int   =       ids_tensor([self.batch_size]       ,							self.type_sequence_label_size				)

										snake_case_				:					Dict   =       self.get_config()

										return config, pixel_values, labels

							def 							_A	(		self			:str				)				->  Optional[int]:


										'''simple docstring'''




										return FocalNetConfig(
										    image_size=self.image_size       ,							patch_size=self.patch_size       ,							num_channels=self.num_channels       ,							embed_dim=self.embed_dim       ,							hidden_sizes=self.hidden_sizes       ,							depths=self.depths       ,							num_heads=self.num_heads       ,							window_size=self.window_size       ,							mlp_ratio=self.mlp_ratio       ,							qkv_bias=self.qkv_bias       ,							hidden_dropout_prob=self.hidden_dropout_prob       ,							attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,							drop_path_rate=self.drop_path_rate       ,							hidden_act=self.hidden_act       ,							use_absolute_embeddings=self.use_absolute_embeddings       ,							path_norm=self.patch_norm       ,							layer_norm_eps=self.layer_norm_eps       ,							initializer_range=self.initializer_range       ,							encoder_stride=self.encoder_stride       ,							out_features=self.out_features       ,							out_indices=self.out_indices       ,							)

							def 							_A	(		self			:List[str]       ,							lowerCAmelCase__			:Dict       ,							lowerCAmelCase__			:Optional[int]       ,							lowerCAmelCase__			:Optional[Any]				)				->  Optional[Any]:


										'''simple docstring'''




										snake_case_				:					int   =       FocalNetModel(config=lowerCAmelCase__				)
										model.to(lowerCAmelCase__				)
										model.eval()
										snake_case_				:					int   =       model(lowerCAmelCase__				)

										snake_case_				:					Dict   =       ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths				) - 1))
										snake_case_				:					Optional[Any]   =       int(config.embed_dim * 2 ** (len(config.depths				) - 1)				)

										self.parent.assertEqual(result.last_hidden_state.shape       ,							(self.batch_size, expected_seq_len, expected_dim)				)

							def 							_A	(		self			:str       ,							lowerCAmelCase__			:List[Any]       ,							lowerCAmelCase__			:int       ,							lowerCAmelCase__			:Any				)				->  int:


										'''simple docstring'''




										snake_case_				:					int   =       FocalNetBackbone(config=lowerCAmelCase__				)
										model.to(lowerCAmelCase__				)
										model.eval()
										snake_case_				:					Optional[int]   =       model(lowerCAmelCase__				)

										# verify feature maps
										self.parent.assertEqual(len(result.feature_maps				)       ,							len(config.out_features				)				)
										self.parent.assertListEqual(list(result.feature_maps[0].shape				)       ,							[self.batch_size, self.image_size, 8, 8]				)

										# verify channels
										self.parent.assertEqual(len(model.channels				)       ,							len(config.out_features				)				)
										self.parent.assertListEqual(model.channels       ,							config.hidden_sizes[:-1]				)

										# verify backbone works with out_features=None
										snake_case_				:					str   =       None
										snake_case_				:					Optional[int]   =       FocalNetBackbone(config=lowerCAmelCase__				)
										model.to(lowerCAmelCase__				)
										model.eval()
										snake_case_				:					Any   =       model(lowerCAmelCase__				)

										# verify feature maps
										self.parent.assertEqual(len(result.feature_maps				)       ,							1				)
										self.parent.assertListEqual(list(result.feature_maps[0].shape				)       ,							[self.batch_size, self.image_size * 2, 4, 4]				)

										# verify channels
										self.parent.assertEqual(len(model.channels				)       ,							1				)
										self.parent.assertListEqual(model.channels       ,							[config.hidden_sizes[-1]]				)

							def 							_A	(		self			:List[str]       ,							lowerCAmelCase__			:int       ,							lowerCAmelCase__			:List[str]       ,							lowerCAmelCase__			:int				)				->  Tuple:


										'''simple docstring'''




										snake_case_				:					Any   =       FocalNetForMaskedImageModeling(config=lowerCAmelCase__				)
										model.to(lowerCAmelCase__				)
										model.eval()
										snake_case_				:					str   =       model(lowerCAmelCase__				)
										self.parent.assertEqual(
										    result.reconstruction.shape       ,							(self.batch_size, self.num_channels, self.image_size, self.image_size)				)

										# test greyscale images
										snake_case_				:					Optional[Any]   =       1
										snake_case_				:					int   =       FocalNetForMaskedImageModeling(lowerCAmelCase__				)
										model.to(lowerCAmelCase__				)
										model.eval()

										snake_case_				:					Dict   =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
										snake_case_				:					Tuple   =       model(lowerCAmelCase__				)
										self.parent.assertEqual(result.reconstruction.shape       ,							(self.batch_size, 1, self.image_size, self.image_size)				)

							def 							_A	(		self			:Optional[int]       ,							lowerCAmelCase__			:Dict       ,							lowerCAmelCase__			:Tuple       ,							lowerCAmelCase__			:List[Any]				)				->  List[str]:


										'''simple docstring'''




										snake_case_				:					Dict   =       self.type_sequence_label_size
										snake_case_				:					Any   =       FocalNetForImageClassification(lowerCAmelCase__				)
										model.to(lowerCAmelCase__				)
										model.eval()
										snake_case_				:					Optional[int]   =       model(lowerCAmelCase__       ,							labels=lowerCAmelCase__				)
										self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.type_sequence_label_size)				)

										# test greyscale images
										snake_case_				:					Tuple   =       1
										snake_case_				:					List[str]   =       FocalNetForImageClassification(lowerCAmelCase__				)
										model.to(lowerCAmelCase__				)
										model.eval()

										snake_case_				:					List[str]   =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
										snake_case_				:					int   =       model(lowerCAmelCase__				)
										self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.type_sequence_label_size)				)






							def 							_A	(		self			:List[Any]				)				->  int:


										'''simple docstring'''




										snake_case_				:					Union[str, Any]   =       self.prepare_config_and_inputs()

										snake_case_				:					List[str]   =       config_and_inputs
										snake_case_				:					Union[str, Any]   =       {"pixel_values": pixel_values}
										return config, inputs_dict







@require_torch
class    A_	(a_						,					a_						,					unittest.TestCase							):
							"""simple docstring"""




							a__   		=	(
							    (
							        FocalNetModel,
							        FocalNetForImageClassification,
							        FocalNetForMaskedImageModeling,
							        FocalNetBackbone,
							    )
							    if is_torch_available()
							    else ()
							)
							a__   		=	(
							    {'''feature-extraction''': FocalNetModel, '''image-classification''': FocalNetForImageClassification}
							    if is_torch_available()
							    else {}
							)
							a__   		=	False

							a__   		=	False
							a__   		=	False
							a__   		=	False
							a__   		=	False

							def 							_A	(		self			:Any				)				->  Any:


										'''simple docstring'''




										snake_case_				:					Union[str, Any]   =       FocalNetModelTester(self				)
										snake_case_				:					Dict   =       ConfigTester(self       ,							config_class=lowerCAmelCase__       ,							embed_dim=37       ,							has_text_modality=lowerCAmelCase__				)

							def 							_A	(		self			:Dict				)				->  Dict:


										'''simple docstring'''




										self.create_and_test_config_common_properties()
										self.config_tester.create_and_test_config_to_json_string()
										self.config_tester.create_and_test_config_to_json_file()
										self.config_tester.create_and_test_config_from_and_save_pretrained()
										self.config_tester.create_and_test_config_with_num_labels()
										self.config_tester.check_config_can_be_init_without_params()
										self.config_tester.check_config_arguments_init()

							def 							_A	(		self			:List[Any]				)				->  Any:


										'''simple docstring'''




										return

							def 							_A	(		self			:Optional[int]				)				->  List[Any]:


										'''simple docstring'''




										snake_case_				:					Optional[Any]   =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase__				)

							def 							_A	(		self			:List[Any]				)				->  Tuple:


										'''simple docstring'''




										snake_case_				:					List[Any]   =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_backbone(*lowerCAmelCase__				)

							def 							_A	(		self			:str				)				->  Tuple:


										'''simple docstring'''




										snake_case_				:					int   =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase__				)

							def 							_A	(		self			:Any				)				->  List[Any]:


										'''simple docstring'''




										snake_case_				:					Optional[Any]   =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__				)

							@unittest.skip(reason="FocalNet does not use inputs_embeds"				)
							def 							_A	(		self			:Tuple				)				->  Any:


										'''simple docstring'''




										pass

							@unittest.skip(reason="FocalNet does not use feedforward chunking"				)
							def 							_A	(		self			:Any				)				->  List[str]:


										'''simple docstring'''




										pass

							def 							_A	(		self			:Tuple				)				->  List[Any]:


										'''simple docstring'''




										snake_case_				:					List[Any]   =       self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes[:-1]:
													snake_case_				:					str   =       model_class(lowerCAmelCase__				)
													self.assertIsInstance(model.get_input_embeddings()       ,							(nn.Module)				)
													snake_case_				:					Any   =       model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(lowerCAmelCase__       ,							nn.Linear				)				)

							def 							_A	(		self			:List[Any]				)				->  Optional[Any]:


										'''simple docstring'''




										snake_case_				:					Optional[Any]   =       self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes[:-1]:
													snake_case_				:					int   =       model_class(lowerCAmelCase__				)
													snake_case_				:					Dict   =       inspect.signature(model.forward				)
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													snake_case_				:					Optional[int]   =       [*signature.parameters.keys()]

													snake_case_				:					List[str]   =       ["pixel_values"]
													self.assertListEqual(arg_names[:1]       ,							lowerCAmelCase__				)

							def 							_A	(		self			:List[Any]       ,							lowerCAmelCase__			:Dict       ,							lowerCAmelCase__			:Optional[Any]       ,							lowerCAmelCase__			:str       ,							lowerCAmelCase__			:str				)				->  List[Any]:


										'''simple docstring'''




										snake_case_				:					Optional[Any]   =       model_class(lowerCAmelCase__				)
										model.to(lowerCAmelCase__				)
										model.eval()

										with torch.no_grad():
													snake_case_				:					int   =       model(**self._prepare_for_class(lowerCAmelCase__       ,							lowerCAmelCase__				)				)

										snake_case_				:					int   =       outputs.hidden_states

										snake_case_				:					Dict   =       getattr(
										    self.model_tester       ,							"expected_num_hidden_layers"       ,							len(self.model_tester.depths				) + 1				)
										self.assertEqual(len(lowerCAmelCase__				)       ,							lowerCAmelCase__				)

										# FocalNet has a different seq_length
										snake_case_				:					Tuple   =       (
										    config.patch_size
										    if isinstance(config.patch_size       ,							collections.abc.Iterable				)
										    else (config.patch_size, config.patch_size)
										)

										snake_case_				:					List[Any]   =       (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

										self.assertListEqual(
										    list(hidden_states[0].shape[-2:]				)       ,							[num_patches, self.model_tester.embed_dim]       ,							)

										snake_case_				:					List[str]   =       outputs.reshaped_hidden_states
										self.assertEqual(len(lowerCAmelCase__				)       ,							lowerCAmelCase__				)

										snake_case_				:					Union[str, Any]   =       reshaped_hidden_states[0].shape
										snake_case_				:					Union[str, Any]   =       (
										    reshaped_hidden_states[0].view(lowerCAmelCase__       ,							lowerCAmelCase__       ,							height * width				).permute(0       ,							2       ,							1				)
										)
										self.assertListEqual(
										    list(reshaped_hidden_states.shape[-2:]				)       ,							[num_patches, self.model_tester.embed_dim]       ,							)

							def 							_A	(		self			:Optional[int]				)				->  Dict:


										'''simple docstring'''




										snake_case_				:					Tuple   =       self.model_tester.prepare_config_and_inputs_for_common()

										snake_case_				:					Optional[Any]   =       (
										    self.model_tester.image_size
										    if isinstance(self.model_tester.image_size       ,							collections.abc.Iterable				)
										    else (self.model_tester.image_size, self.model_tester.image_size)
										)

										for model_class in self.all_model_classes[:-1]:
													snake_case_				:					Tuple   =       True
													self.check_hidden_states_output(lowerCAmelCase__       ,							lowerCAmelCase__       ,							lowerCAmelCase__       ,							lowerCAmelCase__				)

													# check that output_hidden_states also work using config
													del inputs_dict["output_hidden_states"]
													snake_case_				:					Tuple   =       True

													self.check_hidden_states_output(lowerCAmelCase__       ,							lowerCAmelCase__       ,							lowerCAmelCase__       ,							lowerCAmelCase__				)

							def 							_A	(		self			:Optional[Any]				)				->  Dict:


										'''simple docstring'''




										snake_case_				:					Optional[Any]   =       self.model_tester.prepare_config_and_inputs_for_common()
										snake_case_				:					Optional[Any]   =       3

										snake_case_				:					Tuple   =       (
										    self.model_tester.image_size
										    if isinstance(self.model_tester.image_size       ,							collections.abc.Iterable				)
										    else (self.model_tester.image_size, self.model_tester.image_size)
										)
										snake_case_				:					Optional[Any]   =       (
										    config.patch_size
										    if isinstance(config.patch_size       ,							collections.abc.Iterable				)
										    else (config.patch_size, config.patch_size)
										)

										snake_case_				:					Union[str, Any]   =       image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
										snake_case_				:					List[Any]   =       image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

										for model_class in self.all_model_classes[:-1]:
													snake_case_				:					Union[str, Any]   =       True
													self.check_hidden_states_output(lowerCAmelCase__       ,							lowerCAmelCase__       ,							lowerCAmelCase__       ,							(padded_height, padded_width)				)

													# check that output_hidden_states also work using config
													del inputs_dict["output_hidden_states"]
													snake_case_				:					Union[str, Any]   =       True
													self.check_hidden_states_output(lowerCAmelCase__       ,							lowerCAmelCase__       ,							lowerCAmelCase__       ,							(padded_height, padded_width)				)

							@slow
							def 							_A	(		self			:Optional[Any]				)				->  str:


										'''simple docstring'''




										for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													snake_case_				:					Any   =       FocalNetModel.from_pretrained(lowerCAmelCase__				)
													self.assertIsNotNone(lowerCAmelCase__				)






							def 							_A	(		self			:Union[str, Any]				)				->  Tuple:


										'''simple docstring'''




										snake_case_				:					Tuple   =       self.model_tester.prepare_config_and_inputs_for_common()

										snake_case_				:					Tuple   =       _config_zero_init(lowerCAmelCase__				)
										for model_class in self.all_model_classes:
													snake_case_				:					str   =       model_class(config=lowerCAmelCase__				)
													for name, param in model.named_parameters():
																if "embeddings" not in name and param.requires_grad:
																			self.assertIn(
																			    ((param.data.mean() * 1E9).round() / 1E9).item()       ,							[0.0, 1.0]       ,							msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''       ,							)







@require_vision
@require_torch
class    A_	(unittest.TestCase							):
							"""simple docstring"""




							@cached_property
							def 							_A	(		self			:Tuple				)				->  int:


										'''simple docstring'''




										return AutoImageProcessor.from_pretrained("microsoft/focalnet-tiny"				) if is_vision_available() else None






							@slow
							def 							_A	(		self			:str				)				->  Union[str, Any]:


										'''simple docstring'''




										snake_case_				:					List[str]   =       FocalNetForImageClassification.from_pretrained("microsoft/focalnet-tiny"				).to(lowerCAmelCase__				)
										snake_case_				:					str   =       self.default_image_processor

										snake_case_				:					Dict   =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"				)
										snake_case_				:					Any   =       image_processor(images=lowerCAmelCase__       ,							return_tensors="pt"				).to(lowerCAmelCase__				)

										# forward pass
										with torch.no_grad():
													snake_case_				:					str   =       model(**lowerCAmelCase__				)

										# verify the logits
										snake_case_				:					List[str]   =       torch.Size((1, 1_000)				)
										self.assertEqual(outputs.logits.shape       ,							lowerCAmelCase__				)
										snake_case_				:					Tuple   =       torch.tensor([0.2_1_6_6, -0.4_3_6_8, 0.2_1_9_1]				).to(lowerCAmelCase__				)
										self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,							lowerCAmelCase__       ,							atol=1E-4				)				)
										self.assertTrue(outputs.logits.argmax(dim=-1				).item()       ,							281				)







@require_torch
class    A_	(a_						,					unittest.TestCase							):
							"""simple docstring"""




							a__   		=	(FocalNetBackbone,) if is_torch_available() else ()
							a__   		=	FocalNetConfig

							a__   		=	False

							def 							_A	(		self			:Any				)				->  List[Any]:


										'''simple docstring'''




										snake_case_				:					Any   =       FocalNetModelTester(self				)












'''simple docstring'''




import os
import sys
import tempfile

import torch

from .state import AcceleratorState
from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment

def 	__UpperCAmelCase       ( __magic_name__     ,__magic_name__=()     ,__magic_name__=None     ,__magic_name__="no"     ,__magic_name__="29500"						)->    Optional[int]:



			"""simple docstring"""



			snake_case_				:					str   =       False
			snake_case_				:					int   =       False
			if any(key.startswith("KAGGLE"						) for key in os.environ.keys()						):
						snake_case_				:					Any   =       True
			elif "IPython" in sys.modules:
						snake_case_				:					Union[str, Any]   =       "google.colab" in str(sys.modules["IPython"].get_ipython()						)

			try:
						snake_case_				:					Any   =       PrecisionType(mixed_precision.lower()						)
			except ValueError:
						raise ValueError(
						    F'''Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}.'''						)

			if (in_colab or in_kaggle) and (os.environ.get("TPU_NAME"     ,__magic_name__						) is not None):
						# TPU launch
						import torch_xla.distributed.xla_multiprocessing as xmp

						if len(AcceleratorState._shared_state						) > 0:
									raise ValueError(
									    "To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside "
									    "your training function. Restart your notebook and make sure no cells initializes an "
									    "`Accelerator`."						)
						if num_processes is None:
									snake_case_				:					Tuple   =       8

						snake_case_				:					Optional[int]   =       PrepareForLaunch(__magic_name__     ,distributed_type="TPU"						)
						print(F'''Launching a training on {num_processes} TPU cores.'''						)
						xmp.spawn(__magic_name__     ,args=__magic_name__     ,nprocs=__magic_name__     ,start_method="fork"						)
			elif in_colab:
						# No need for a distributed launch otherwise as it's either CPU or one GPU.
						if torch.cuda.is_available():
									print("Launching training on one GPU."						)
						else:
									print("Launching training on one CPU."						)
						function(*__magic_name__						)
			else:
						if num_processes is None:
									raise ValueError(
									    "You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call."						)

						if num_processes > 1:
									# Multi-GPU launch
									from torch.multiprocessing import start_processes
									from torch.multiprocessing.spawn import ProcessRaisedException

									if len(AcceleratorState._shared_state						) > 0:
												raise ValueError(
												    "To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized "
												    "inside your training function. Restart your notebook and make sure no cells initializes an "
												    "`Accelerator`."						)

									if torch.cuda.is_initialized():
												raise ValueError(
												    "To launch a multi-GPU training from your notebook, you need to avoid running any instruction "
												    "using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA "
												    "function."						)

									# torch.distributed will expect a few environment variable to be here. We set the ones common to each
									# process here (the other ones will be set be the launcher).
									with patch_environment(
									    world_size=__magic_name__     ,master_addr="127.0.01"     ,master_port=__magic_name__     ,mixed_precision=__magic_name__						):
												snake_case_				:					Optional[int]   =       PrepareForLaunch(__magic_name__     ,distributed_type="MULTI_GPU"						)
												print(F'''Launching training on {num_processes} GPUs.'''						)
												try:
															start_processes(__magic_name__     ,args=__magic_name__     ,nprocs=__magic_name__     ,start_method="fork"						)
												except ProcessRaisedException as e:
															if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]:
																		raise RuntimeError(
																		    "CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. "
																		    "This likely stems from an outside import causing issues once the `notebook_launcher()` is called. "
																		    "Please review your imports and test them when running the `notebook_launcher()` to identify "
																		    "which one is problematic."						) from e

						else:
									# No need for a distributed launch otherwise as it's either CPU, GPU or MPS.
									if is_mps_available():
												snake_case_				:					Any   =       "1"
												print("Launching training on MPS."						)
									elif torch.cuda.is_available():
												print("Launching training on one GPU."						)
									else:
												print("Launching training on CPU."						)
									function(*__magic_name__						)






def 	__UpperCAmelCase       ( __magic_name__     ,__magic_name__=()     ,__magic_name__=2						)->    Dict:



			"""simple docstring"""



			from torch.multiprocessing import start_processes

			with tempfile.NamedTemporaryFile() as tmp_file:
						# torch.distributed will expect a few environment variable to be here. We set the ones common to each
						# process here (the other ones will be set be the launcher).
						with patch_environment(
						    world_size=__magic_name__     ,master_addr="127.0.01"     ,master_port="29500"     ,accelerate_mixed_precision="no"     ,accelerate_debug_rdv_file=tmp_file.name     ,accelerate_use_cpu="yes"     ,):
									snake_case_				:					Any   =       PrepareForLaunch(__magic_name__     ,debug=__magic_name__						)
									start_processes(__magic_name__     ,args=__magic_name__     ,nprocs=__magic_name__     ,start_method="fork"						)




