


"""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 					a__			(				SCREAMING_SNAKE_CASE    :    Any				,      SCREAMING_SNAKE_CASE    :    Tuple=()				,      SCREAMING_SNAKE_CASE    :    Union[str, Any]=None				,      SCREAMING_SNAKE_CASE    :    Tuple="no"				,      SCREAMING_SNAKE_CASE    :    Dict="29500"				):


  '''simple docstring'''







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

  try:
    lowerCAmelCase     :     str								=							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"				,      SCREAMING_SNAKE_CASE				) 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:
      lowerCAmelCase     :     Optional[Any]								=							8

    lowerCAmelCase     :     Any								=							PrepareForLaunch(SCREAMING_SNAKE_CASE				,      distributed_type="TPU"				)
    print(f"""Launching a training on {num_processes} TPU cores."""				)
    xmp.spawn(SCREAMING_SNAKE_CASE				,      args=SCREAMING_SNAKE_CASE				,      nprocs=SCREAMING_SNAKE_CASE				,      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(*SCREAMING_SNAKE_CASE				)
  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=SCREAMING_SNAKE_CASE				,      master_addr="127.0.01"				,      master_port=SCREAMING_SNAKE_CASE				,      mixed_precision=SCREAMING_SNAKE_CASE				):
        lowerCAmelCase     :     Any								=							PrepareForLaunch(SCREAMING_SNAKE_CASE				,      distributed_type="MULTI_GPU"				)
        print(f"""Launching training on {num_processes} GPUs."""				)
        try:
          start_processes(SCREAMING_SNAKE_CASE				,      args=SCREAMING_SNAKE_CASE				,      nprocs=SCREAMING_SNAKE_CASE				,      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():
        lowerCAmelCase     :     Tuple								=							"1"
        print("Launching training on MPS."				)
      elif torch.cuda.is_available():
        print("Launching training on one GPU."				)
      else:
        print("Launching training on CPU."				)
      function(*SCREAMING_SNAKE_CASE				)





def 					a__			(				SCREAMING_SNAKE_CASE    :    Dict				,      SCREAMING_SNAKE_CASE    :    Dict=()				,      SCREAMING_SNAKE_CASE    :    Any=2				):


  '''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=SCREAMING_SNAKE_CASE				,      master_addr="127.0.01"				,      master_port="29500"				,      accelerate_mixed_precision="no"				,      accelerate_debug_rdv_file=tmp_file.name				,      accelerate_use_cpu="yes"				,      ):
      lowerCAmelCase     :     Optional[Any]								=							PrepareForLaunch(SCREAMING_SNAKE_CASE				,      debug=SCREAMING_SNAKE_CASE				)
      start_processes(SCREAMING_SNAKE_CASE				,      args=SCREAMING_SNAKE_CASE				,      nprocs=SCREAMING_SNAKE_CASE				,      start_method="fork"				)









"""simple docstring"""







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


lowerCAmelCase__      				=   logging.get_logger(__name__)

lowerCAmelCase__      				=   {
    '''facebook/data2vec-vision-base-ft''': (
        '''https://huggingface.co/facebook/data2vec-vision-base-ft/resolve/main/config.json'''
    ),
}


class       SCREAMING_SNAKE_CASE__				(		lowercase				):
       """simple docstring"""


       a  :				List[Any]   ="data2vec-vision"

       def __init__(						self		,     snake_case__=768		,     snake_case__=12		,     snake_case__=12		,     snake_case__=3_072		,     snake_case__="gelu"		,     snake_case__=0.0		,     snake_case__=0.0		,     snake_case__=0.02		,     snake_case__=1e-12		,     snake_case__=224		,     snake_case__=16		,     snake_case__=3		,     snake_case__=False		,     snake_case__=False		,     snake_case__=False		,     snake_case__=False		,     snake_case__=0.1		,     snake_case__=0.1		,     snake_case__=True		,     snake_case__=[3, 5, 7, 11]		,     snake_case__=[1, 2, 3, 6]		,     snake_case__=True		,     snake_case__=0.4		,     snake_case__=256		,     snake_case__=1		,     snake_case__=False		,     snake_case__=255		,     **snake_case__		,     ):






         """simple docstring"""





         super().__init__(**snake_case__						)

         lowerCAmelCase     :     Tuple								=							hidden_size
         lowerCAmelCase     :     List[Any]								=							num_hidden_layers
         lowerCAmelCase     :     Tuple								=							num_attention_heads
         lowerCAmelCase     :     Optional[int]								=							intermediate_size
         lowerCAmelCase     :     Optional[int]								=							hidden_act
         lowerCAmelCase     :     Dict								=							hidden_dropout_prob
         lowerCAmelCase     :     Optional[Any]								=							attention_probs_dropout_prob
         lowerCAmelCase     :     int								=							initializer_range
         lowerCAmelCase     :     Dict								=							layer_norm_eps

         lowerCAmelCase     :     Optional[int]								=							image_size
         lowerCAmelCase     :     Optional[Any]								=							patch_size
         lowerCAmelCase     :     Optional[Any]								=							num_channels
         lowerCAmelCase     :     Union[str, Any]								=							use_mask_token
         lowerCAmelCase     :     str								=							use_absolute_position_embeddings
         lowerCAmelCase     :     Any								=							use_relative_position_bias
         lowerCAmelCase     :     List[str]								=							use_shared_relative_position_bias
         lowerCAmelCase     :     str								=							layer_scale_init_value
         lowerCAmelCase     :     Union[str, Any]								=							drop_path_rate
         lowerCAmelCase     :     Any								=							use_mean_pooling
         # decode head attributes (semantic segmentation)
         lowerCAmelCase     :     Optional[int]								=							out_indices
         lowerCAmelCase     :     Union[str, Any]								=							pool_scales
         # auxiliary head attributes (semantic segmentation)
         lowerCAmelCase     :     str								=							use_auxiliary_head
         lowerCAmelCase     :     int								=							auxiliary_loss_weight
         lowerCAmelCase     :     Tuple								=							auxiliary_channels
         lowerCAmelCase     :     List[str]								=							auxiliary_num_convs
         lowerCAmelCase     :     Tuple								=							auxiliary_concat_input
         lowerCAmelCase     :     List[str]								=							semantic_loss_ignore_index


class       SCREAMING_SNAKE_CASE__				(		lowercase				):
       """simple docstring"""


       a  :				Union[str, Any]   =version.parse("1.11"				)

       @property
       def        lowercase__							(						self						):






         """simple docstring"""





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







       @property
       def        lowercase__							(						self						):






         """simple docstring"""





         return 1e-4





