


'''simple docstring'''





import json
import logging
import os
import socket

import git
import numpy as np
import torch


logging.basicConfig(
    format='''%(asctime)s - %(levelname)s - %(name)s - PID: %(process)d -  %(message)s''',
    datefmt='''%m/%d/%Y %H:%M:%S''',
    level=logging.INFO,
)
__lowerCamelCase												=						logging.getLogger(__name__)


def        a__     (					UpperCamelCase_						:      str						):
   UpperCAmelCase__	:Union[str, Any]      =      git.Repo(search_parent_directories=UpperCamelCase_						)
   UpperCAmelCase__	:List[Any]      =      {
       '''repo_id''': str(UpperCamelCase_						),
       '''repo_sha''': str(repo.head.object.hexsha						),
       '''repo_branch''': str(repo.active_branch						),
   }

   with open(os.path.join(UpperCamelCase_,   '''git_log.json'''						),   '''w'''						) as f:
      json.dump(UpperCamelCase_,   UpperCamelCase_,   indent=4						)


def        a__     (					UpperCamelCase_						:      Dict						):
   if params.n_gpu <= 0:
      UpperCAmelCase__	:Optional[int]      =      0
      UpperCAmelCase__	:Dict      =      -1
      UpperCAmelCase__	:Dict      =      True
      UpperCAmelCase__	:Tuple      =      False
      return

   assert torch.cuda.is_available()

   logger.info('''Initializing GPUs'''						)
   if params.n_gpu > 1:
      assert params.local_rank != -1

      UpperCAmelCase__	:Tuple      =      int(os.environ['''WORLD_SIZE''']						)
      UpperCAmelCase__	:Dict      =      int(os.environ['''N_GPU_NODE''']						)
      UpperCAmelCase__	:List[Any]      =      int(os.environ['''RANK''']						)

      # number of nodes / node ID
      UpperCAmelCase__	:Optional[int]      =      params.world_size // params.n_gpu_per_node
      UpperCAmelCase__	:Union[str, Any]      =      params.global_rank // params.n_gpu_per_node
      UpperCAmelCase__	:List[Any]      =      True

      assert params.n_nodes == int(os.environ['''N_NODES''']						)
      assert params.node_id == int(os.environ['''NODE_RANK''']						)

   # local job (single GPU)
   else:
      assert params.local_rank == -1

      UpperCAmelCase__	:List[str]      =      1
      UpperCAmelCase__	:int      =      0
      UpperCAmelCase__	:Any      =      0
      UpperCAmelCase__	:Optional[int]      =      0
      UpperCAmelCase__	:Optional[int]      =      1
      UpperCAmelCase__	:List[str]      =      1
      UpperCAmelCase__	:str      =      False

   # sanity checks
   assert params.n_nodes >= 1
   assert 0 <= params.node_id < params.n_nodes
   assert 0 <= params.local_rank <= params.global_rank < params.world_size
   assert params.world_size == params.n_nodes * params.n_gpu_per_node

   # define whether this is the master process / if we are in multi-node distributed mode
   UpperCAmelCase__	:Dict      =      params.node_id == 0 and params.local_rank == 0
   UpperCAmelCase__	:Dict      =      params.n_nodes > 1

   # summary
   UpperCAmelCase__	:Tuple      =      F'''--- Global rank: {params.global_rank} - '''
   logger.info(PREFIX + '''Number of nodes: %i''' % params.n_nodes						)
   logger.info(PREFIX + '''Node ID        : %i''' % params.node_id						)
   logger.info(PREFIX + '''Local rank     : %i''' % params.local_rank						)
   logger.info(PREFIX + '''World size     : %i''' % params.world_size						)
   logger.info(PREFIX + '''GPUs per node  : %i''' % params.n_gpu_per_node						)
   logger.info(PREFIX + '''Master         : %s''' % str(params.is_master						)						)
   logger.info(PREFIX + '''Multi-node     : %s''' % str(params.multi_node						)						)
   logger.info(PREFIX + '''Multi-GPU      : %s''' % str(params.multi_gpu						)						)
   logger.info(PREFIX + '''Hostname       : %s''' % socket.gethostname()						)

   # set GPU device
   torch.cuda.set_device(params.local_rank						)

   # initialize multi-GPU
   if params.multi_gpu:
      logger.info('''Initializing PyTorch distributed'''						)
      torch.distributed.init_process_group(
          init_method='''env://''',   backend='''nccl''',   )


def        a__     (					UpperCamelCase_						:      Tuple						):
   np.random.seed(args.seed						)
   torch.manual_seed(args.seed						)
   if args.n_gpu > 0:
      torch.cuda.manual_seed_all(args.seed						)







'''simple docstring'''





import warnings
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												=						{
    '''nvidia/segformer-b0-finetuned-ade-512-512''': (
        '''https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json'''
    ),
    # See all SegFormer models at https://huggingface.co/models?filter=segformer
}




class 				UpperCAmelCase			(			_snake_case						):
 UpperCAmelCase				=					"segformer"


 def __init__(      self							:	Dict			,       __lowerCamelCase							:	Dict=3			,       __lowerCamelCase							:	List[str]=4			,       __lowerCamelCase							:	List[Any]=[2, 2, 2, 2]			,       __lowerCamelCase							:	List[str]=[8, 4, 2, 1]			,       __lowerCamelCase							:	Optional[Any]=[3_2, 6_4, 1_6_0, 2_5_6]			,       __lowerCamelCase							:	Tuple=[7, 3, 3, 3]			,       __lowerCamelCase							:	Tuple=[4, 2, 2, 2]			,       __lowerCamelCase							:	Any=[1, 2, 5, 8]			,       __lowerCamelCase							:	Optional[Any]=[4, 4, 4, 4]			,       __lowerCamelCase							:	Union[str, Any]="gelu"			,       __lowerCamelCase							:	Dict=0.0			,       __lowerCamelCase							:	Union[str, Any]=0.0			,       __lowerCamelCase							:	Tuple=0.1			,       __lowerCamelCase							:	Optional[Any]=0.02			,       __lowerCamelCase							:	Optional[int]=0.1			,       __lowerCamelCase							:	str=1e-6			,       __lowerCamelCase							:	List[Any]=2_5_6			,       __lowerCamelCase							:	int=2_5_5			,       **__lowerCamelCase							:	str			,       ):
    super().__init__(**__lowerCamelCase )

    if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False:
       warnings.warn(
           '''Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be'''
           ''' removed, as the behaviour will default to that of reshape_last_stage = True.'''			,       __lowerCamelCase			,       )

    UpperCAmelCase__	:Dict      =      num_channels
    UpperCAmelCase__	:List[Any]      =      num_encoder_blocks
    UpperCAmelCase__	:List[str]      =      depths
    UpperCAmelCase__	:str      =      sr_ratios
    UpperCAmelCase__	:Optional[Any]      =      hidden_sizes
    UpperCAmelCase__	:List[str]      =      patch_sizes
    UpperCAmelCase__	:List[str]      =      strides
    UpperCAmelCase__	:List[str]      =      mlp_ratios
    UpperCAmelCase__	:Dict      =      num_attention_heads
    UpperCAmelCase__	:Tuple      =      hidden_act
    UpperCAmelCase__	:List[str]      =      hidden_dropout_prob
    UpperCAmelCase__	:List[Any]      =      attention_probs_dropout_prob
    UpperCAmelCase__	:int      =      classifier_dropout_prob
    UpperCAmelCase__	:str      =      initializer_range
    UpperCAmelCase__	:List[str]      =      drop_path_rate
    UpperCAmelCase__	:Dict      =      layer_norm_eps
    UpperCAmelCase__	:List[str]      =      decoder_hidden_size
    UpperCAmelCase__	:Optional[int]      =      kwargs.get('''reshape_last_stage'''			,       __lowerCamelCase )
    UpperCAmelCase__	:str      =      semantic_loss_ignore_index




class 				UpperCAmelCase			(			_snake_case						):
 UpperCAmelCase				=					version.parse("1.11"						)


 @property
 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[Any] ):
    return OrderedDict(
        [
            ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
        ] )


 @property
 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[int] ):
    return 1e-4





 @property
 def        __SCREAMING_SNAKE_CASE					(      self							:	List[str] ):
    return 1_2



