






"""simple docstring"""


from typing import List, Optional, Tuple, Union

import torch
from torch import nn
from torch.nn import CrossEntropyLoss

from ... import AutoBackbone
from ...modeling_outputs import SemanticSegmenterOutput
from ...modeling_utils import PreTrainedModel
from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings
from ...utils.backbone_utils import BackboneMixin
from .configuration_upernet import UperNetConfig


lowercase_				     =     [
    'openmmlab/upernet-convnext-tiny',
    # See all UperNet models at https://huggingface.co/models?filter=upernet
]

# General docstring
lowercase_				     =     'UperNetConfig'


class 					snake_case		(							nn.Module						):


   '''simple docstring'''

   def __init__(	self :     List[str],       _lowerCamelCase :     int,       _lowerCamelCase :     int,       _lowerCamelCase :     Union[int, Tuple[int, int]],       _lowerCamelCase :     Union[int, Tuple[int, int], str] = 0,       _lowerCamelCase :     bool = False,       _lowerCamelCase :     Union[int, Tuple[int, int]] = 1,       ):

     '''simple docstring'''
     super().__init__()
     __A									= nn.Convad(
         in_channels=_lowerCamelCase,       out_channels=_lowerCamelCase,       kernel_size=_lowerCamelCase,       padding=_lowerCamelCase,       bias=_lowerCamelCase,       dilation=_lowerCamelCase,       )
     __A									= nn.BatchNormad(_lowerCamelCase  )
     __A									= nn.ReLU()






   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any],       _lowerCamelCase :     torch.Tensor  ):

     '''simple docstring'''
     __A									= self.conv(_lowerCamelCase  )
     __A									= self.batch_norm(_lowerCamelCase  )
     __A									= self.activation(_lowerCamelCase  )

     return output





class 					snake_case		(							nn.Module						):


   '''simple docstring'''

   def __init__(	self :     List[str],       _lowerCamelCase :     int,       _lowerCamelCase :     int,       _lowerCamelCase :     int  ):

     '''simple docstring'''
     super().__init__()
     __A									= [
         nn.AdaptiveAvgPoolad(_lowerCamelCase  ),
         UperNetConvModule(_lowerCamelCase,       _lowerCamelCase,       kernel_size=1  ),
     ]
     for i, layer in enumerate(self.layers  ):
       self.add_module(str(_lowerCamelCase  ),       _lowerCamelCase  )






   def 				_SCREAMING_SNAKE_CASE				(	self :     Union[str, Any],       _lowerCamelCase :     torch.Tensor  ):

     '''simple docstring'''
     __A									= input
     for layer in self.layers:
       __A									= layer(_lowerCamelCase  )
     return hidden_state





class 					snake_case		(							nn.Module						):


   '''simple docstring'''

   def __init__(	self :     List[str],       _lowerCamelCase :     Tuple[int, ...],       _lowerCamelCase :     int,       _lowerCamelCase :     int,       _lowerCamelCase :     bool  ):

     '''simple docstring'''
     super().__init__()
     __A									= pool_scales
     __A									= align_corners
     __A									= in_channels
     __A									= channels
     __A									= []
     for i, pool_scale in enumerate(_lowerCamelCase  ):
       __A									= UperNetPyramidPoolingBlock(pool_scale=_lowerCamelCase,       in_channels=_lowerCamelCase,       channels=_lowerCamelCase  )
       self.blocks.append(_lowerCamelCase  )
       self.add_module(str(_lowerCamelCase  ),       _lowerCamelCase  )






   def 				_SCREAMING_SNAKE_CASE				(	self :     List[Any],       _lowerCamelCase :     torch.Tensor  ):

     '''simple docstring'''
     __A									= []
     for ppm in self.blocks:
       __A									= ppm(_lowerCamelCase  )
       __A									= nn.functional.interpolate(
           _lowerCamelCase,       size=x.size()[2:],       mode='''bilinear''',       align_corners=self.align_corners  )
       ppm_outs.append(_lowerCamelCase  )
     return ppm_outs





class 					snake_case		(							nn.Module						):


   '''simple docstring'''

   def __init__(	self :     Any,       _lowerCamelCase :     List[str],       _lowerCamelCase :     Tuple  ):

     '''simple docstring'''
     super().__init__()

     __A									= config
     __A									= config.pool_scales  # e.g. (1, 2, 3, 6)
     __A									= in_channels
     __A									= config.hidden_size
     __A									= False
     __A									= nn.Convad(self.channels,       config.num_labels,       kernel_size=1  )

     # PSP Module
     __A									= UperNetPyramidPoolingModule(
         self.pool_scales,       self.in_channels[-1],       self.channels,       align_corners=self.align_corners,       )
     __A									= UperNetConvModule(
         self.in_channels[-1] + len(self.pool_scales  ) * self.channels,       self.channels,       kernel_size=3,       padding=1,       )
     # FPN Module
     __A									= nn.ModuleList()
     __A									= nn.ModuleList()
     for in_channels in self.in_channels[:-1]:  # skip the top layer
       __A									= UperNetConvModule(_lowerCamelCase,       self.channels,       kernel_size=1  )
       __A									= UperNetConvModule(self.channels,       self.channels,       kernel_size=3,       padding=1  )
       self.lateral_convs.append(_lowerCamelCase  )
       self.fpn_convs.append(_lowerCamelCase  )

     __A									= UperNetConvModule(
         len(self.in_channels  ) * self.channels,       self.channels,       kernel_size=3,       padding=1,       )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Union[str, Any]  ):

     '''simple docstring'''
     self.apply(self._init_weights  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[int],       _lowerCamelCase :     str  ):

     '''simple docstring'''
     if isinstance(_lowerCamelCase,       nn.Convad  ):
       module.weight.data.normal_(mean=0.0,       std=self.config.initializer_range  )
       if module.bias is not None:
         module.bias.data.zero_()


   def 				_SCREAMING_SNAKE_CASE				(	self :     Dict,       _lowerCamelCase :     Union[str, Any]  ):

     '''simple docstring'''
     __A									= inputs[-1]
     __A									= [x]
     psp_outs.extend(self.psp_modules(_lowerCamelCase  )  )
     __A									= torch.cat(_lowerCamelCase,       dim=1  )
     __A									= self.bottleneck(_lowerCamelCase  )

     return output






   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[int],       _lowerCamelCase :     torch.Tensor  ):

     '''simple docstring'''
     # build laterals
     __A									= [lateral_conv(encoder_hidden_states[i]  ) for i, lateral_conv in enumerate(self.lateral_convs  )]

     laterals.append(self.psp_forward(_lowerCamelCase  )  )

     # build top-down path
     __A									= len(_lowerCamelCase  )
     for i in range(used_backbone_levels - 1,       0,       -1  ):
       __A									= laterals[i - 1].shape[2:]
       __A									= laterals[i - 1] + nn.functional.interpolate(
           laterals[i],       size=_lowerCamelCase,       mode='''bilinear''',       align_corners=self.align_corners  )

     # build outputs
     __A									= [self.fpn_convs[i](laterals[i]  ) for i in range(used_backbone_levels - 1  )]
     # append psp feature
     fpn_outs.append(laterals[-1]  )

     for i in range(used_backbone_levels - 1,       0,       -1  ):
       __A									= nn.functional.interpolate(
           fpn_outs[i],       size=fpn_outs[0].shape[2:],       mode='''bilinear''',       align_corners=self.align_corners  )
     __A									= torch.cat(_lowerCamelCase,       dim=1  )
     __A									= self.fpn_bottleneck(_lowerCamelCase  )
     __A									= self.classifier(_lowerCamelCase  )

     return output





class 					snake_case		(							nn.Module						):


   '''simple docstring'''

   def __init__(	self :     Union[str, Any],       _lowerCamelCase :     Tuple,       _lowerCamelCase :     int = 2,       _lowerCamelCase :     int = 3,       _lowerCamelCase :     Union[int, Tuple[int, int]] = 1  ):

     '''simple docstring'''
     super().__init__()

     __A									= config
     __A									= config.auxiliary_in_channels
     __A									= config.auxiliary_channels
     __A									= config.auxiliary_num_convs
     __A									= config.auxiliary_concat_input
     __A									= in_index

     __A									= (kernel_size // 2) * dilation
     __A									= []
     convs.append(
         UperNetConvModule(
             self.in_channels,       self.channels,       kernel_size=_lowerCamelCase,       padding=_lowerCamelCase,       dilation=_lowerCamelCase  )  )
     for i in range(self.num_convs - 1  ):
       convs.append(
           UperNetConvModule(
               self.channels,       self.channels,       kernel_size=_lowerCamelCase,       padding=_lowerCamelCase,       dilation=_lowerCamelCase  )  )
     if self.num_convs == 0:
       __A									= nn.Identity()
     else:
       __A									= nn.Sequential(*_lowerCamelCase  )
     if self.concat_input:
       __A									= UperNetConvModule(
           self.in_channels + self.channels,       self.channels,       kernel_size=_lowerCamelCase,       padding=kernel_size // 2  )

     __A									= nn.Convad(self.channels,       config.num_labels,       kernel_size=1  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     List[str]  ):

     '''simple docstring'''
     self.apply(self._init_weights  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Dict,       _lowerCamelCase :     List[Any]  ):

     '''simple docstring'''
     if isinstance(_lowerCamelCase,       nn.Convad  ):
       module.weight.data.normal_(mean=0.0,       std=self.config.initializer_range  )
       if module.bias is not None:
         module.bias.data.zero_()






   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[int],       _lowerCamelCase :     torch.Tensor  ):

     '''simple docstring'''
     # just take the relevant feature maps
     __A									= encoder_hidden_states[self.in_index]
     __A									= self.convs(_lowerCamelCase  )
     if self.concat_input:
       __A									= self.conv_cat(torch.cat([hidden_states, output],       dim=1  )  )
     __A									= self.classifier(_lowerCamelCase  )
     return output





class 					snake_case		(							_lowerCAmelCase						):


   '''simple docstring'''

   A_						:	List[str]						 = UperNetConfig
   A_						:	Optional[Any]						 = "pixel_values"
   A_						:	str						 = True


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[int],       _lowerCamelCase :     int  ):

     '''simple docstring'''
     if isinstance(_lowerCamelCase,       _lowerCamelCase  ):
       module.backbone.init_weights()
       module.decode_head.init_weights()
       module.auxiliary_head.init_weights()


   def 				_SCREAMING_SNAKE_CASE				(	self :     int  ):

     '''simple docstring'''
     self.backbone.init_weights()
     self.decode_head.init_weights()
     self.auxiliary_head.init_weights()






   def 				_SCREAMING_SNAKE_CASE				(	self :     List[Any],       _lowerCamelCase :     Optional[int],       _lowerCamelCase :     Tuple=False  ):

     '''simple docstring'''
     if isinstance(_lowerCamelCase,       _lowerCamelCase  ):
       __A									= value





lowercase_				     =     R'\n    Parameters:\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n        config ([`UperNetConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n'

lowercase_				     =     R'\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n            [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n        output_attentions (`bool`, *optional*):\n            Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n            `attentions` under returned tensors for more detail.\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n            returned tensors for more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n'


@add_start_docstrings(
    "UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes."   ,	_lowerCAmelCase   ,	)
class 					snake_case		(							_lowerCAmelCase						):


   '''simple docstring'''

   def __init__(	self :     Optional[Any],       _lowerCamelCase :     str  ):

     '''simple docstring'''
     super().__init__(_lowerCamelCase  )

     __A									= AutoBackbone.from_config(config.backbone_config  )

     # Semantic segmentation head(s)
     __A									= UperNetHead(_lowerCamelCase,       in_channels=self.backbone.channels  )
     __A									= UperNetFCNHead(_lowerCamelCase  ) if config.use_auxiliary_head else None

     # Initialize weights and apply final processing
     self.post_init()






   @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format('''batch_size, sequence_length'''  )  )
   @replace_return_docstrings(output_type=_lowerCamelCase,       config_class=_CONFIG_FOR_DOC  )
   def 				_SCREAMING_SNAKE_CASE				(	self :     str,       _lowerCamelCase :     Optional[torch.Tensor] = None,       _lowerCamelCase :     Optional[bool] = None,       _lowerCamelCase :     Optional[bool] = None,       _lowerCamelCase :     Optional[torch.Tensor] = None,       _lowerCamelCase :     Optional[bool] = None,       ):

     '''simple docstring'''
     __A									= return_dict if return_dict is not None else self.config.use_return_dict
     __A									= (
         output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
     )
     __A									= output_attentions if output_attentions is not None else self.config.output_attentions

     __A									= self.backbone.forward_with_filtered_kwargs(
         _lowerCamelCase,       output_hidden_states=_lowerCamelCase,       output_attentions=_lowerCamelCase  )
     __A									= outputs.feature_maps

     __A									= self.decode_head(_lowerCamelCase  )
     __A									= nn.functional.interpolate(_lowerCamelCase,       size=pixel_values.shape[2:],       mode='''bilinear''',       align_corners=_lowerCamelCase  )

     __A									= None
     if self.auxiliary_head is not None:
       __A									= self.auxiliary_head(_lowerCamelCase  )
       __A									= nn.functional.interpolate(
           _lowerCamelCase,       size=pixel_values.shape[2:],       mode='''bilinear''',       align_corners=_lowerCamelCase  )

     __A									= None
     if labels is not None:
       if self.config.num_labels == 1:
         raise ValueError('''The number of labels should be greater than one'''  )
       else:
         # compute weighted loss
         __A									= CrossEntropyLoss(ignore_index=self.config.loss_ignore_index  )
         __A									= loss_fct(_lowerCamelCase,       _lowerCamelCase  )
         __A									= loss_fct(_lowerCamelCase,       _lowerCamelCase  )
         __A									= main_loss + self.config.auxiliary_loss_weight * auxiliary_loss

     if not return_dict:
       if output_hidden_states:
         __A									= (logits,) + outputs[1:]
       else:
         __A									= (logits,) + outputs[2:]
       return ((loss,) + output) if loss is not None else output

     return SemanticSegmenterOutput(
         loss=_lowerCamelCase,       logits=_lowerCamelCase,       hidden_states=outputs.hidden_states,       attentions=outputs.attentions,       )












"""simple docstring"""


import numpy as np
import torch
from torch.utils.data import DataLoader

from accelerate.utils.dataclasses import DistributedType


class 					snake_case		:


   '''simple docstring'''

   def __init__(	self :     Optional[int],       _lowerCamelCase :     Optional[int]=2,       _lowerCamelCase :     Optional[int]=3,       _lowerCamelCase :     int=64,       _lowerCamelCase :     List[str]=None  ):

     '''simple docstring'''
     __A									= np.random.default_rng(_lowerCamelCase  )
     __A									= length
     __A									= rng.normal(size=(length,)  ).astype(np.floataa  )
     __A									= a * self.x + b + rng.normal(scale=0.1,       size=(length,)  ).astype(np.floataa  )


   def __len__(	self :     str  ):

     '''simple docstring'''
     return self.length






   def __getitem__(	self :     Dict,       _lowerCamelCase :     Optional[int]  ):

     '''simple docstring'''
     return {"x": self.x[i], "y": self.y[i]}





class 					snake_case		(							torch.nn.Module						):


   '''simple docstring'''

   def __init__(	self :     Optional[Any],       _lowerCamelCase :     Tuple=0,       _lowerCamelCase :     Any=0,       _lowerCamelCase :     Optional[Any]=False  ):

     '''simple docstring'''
     super().__init__()
     __A									= torch.nn.Parameter(torch.tensor([2, 3]  ).float()  )
     __A									= torch.nn.Parameter(torch.tensor([2, 3]  ).float()  )
     __A									= True






   def 				_SCREAMING_SNAKE_CASE				(	self :     List[str],       _lowerCamelCase :     Optional[Any]=None  ):

     '''simple docstring'''
     if self.first_batch:
       print(f'Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}'  )
       __A									= False
     return x * self.a[0] + self.b[0]





class 					snake_case		(							torch.nn.Module						):


   '''simple docstring'''

   def __init__(	self :     str,       _lowerCamelCase :     Optional[Any]=0,       _lowerCamelCase :     Any=0,       _lowerCamelCase :     List[Any]=False  ):

     '''simple docstring'''
     super().__init__()
     __A									= torch.nn.Parameter(torch.tensor(_lowerCamelCase  ).float()  )
     __A									= torch.nn.Parameter(torch.tensor(_lowerCamelCase  ).float()  )
     __A									= True






   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any],       _lowerCamelCase :     List[str]=None  ):

     '''simple docstring'''
     if self.first_batch:
       print(f'Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}'  )
       __A									= False
     return x * self.a + self.b





def 					lowerCAmelCase				(    __UpperCamelCase		,       __UpperCamelCase = 1_6 ):




  """simple docstring"""







  from datasets import load_dataset
  from transformers import AutoTokenizer

  __A									= AutoTokenizer.from_pretrained('''bert-base-cased''' )
  __A									= {'''train''': '''tests/test_samples/MRPC/train.csv''', '''validation''': '''tests/test_samples/MRPC/dev.csv'''}
  __A									= load_dataset('''csv'''		,       data_files=__UpperCamelCase )
  __A									= datasets['''train'''].unique('''label''' )

  __A									= {v: i for i, v in enumerate(__UpperCamelCase )}

  def tokenize_function(__UpperCamelCase ):
    # max_length=None => use the model max length (it's actually the default)
    __A									= tokenizer(
        examples['''sentence1''']		,       examples['''sentence2''']		,       truncation=__UpperCamelCase		,       max_length=__UpperCamelCase		,       padding='''max_length''' )
    if "label" in examples:
      __A									= [label_to_id[l] for l in examples['''label''']]
    return outputs

  # Apply the method we just defined to all the examples in all the splits of the dataset
  __A									= datasets.map(
      __UpperCamelCase		,       batched=__UpperCamelCase		,       remove_columns=['''sentence1''', '''sentence2''', '''label''']		,       )

  def collate_fn(__UpperCamelCase ):
    # On TPU it's best to pad everything to the same length or training will be very slow.
    if accelerator.distributed_type == DistributedType.TPU:
      return tokenizer.pad(__UpperCamelCase		,       padding='''max_length'''		,       max_length=1_2_8		,       return_tensors='''pt''' )
    return tokenizer.pad(__UpperCamelCase		,       padding='''longest'''		,       return_tensors='''pt''' )

  # Instantiate dataloaders.
  __A									= DataLoader(tokenized_datasets['''train''']		,       shuffle=__UpperCamelCase		,       collate_fn=__UpperCamelCase		,       batch_size=2 )
  __A									= DataLoader(tokenized_datasets['''validation''']		,       shuffle=__UpperCamelCase		,       collate_fn=__UpperCamelCase		,       batch_size=1 )

  return train_dataloader, eval_dataloader




