'''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


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

# General docstring
snake_case__					:      List[str]				      =  '''UperNetConfig'''






class 					__SCREAMING_SNAKE_CASE						(	nn.Module	):







  '''simple docstring'''






  def __init__(			self				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_ = 0				,		snake_case_ = False				,		snake_case_ = 1				,		):




       '''simple docstring'''

       super().__init__()
       UpperCAmelCase_							:       Dict       =				nn.Convad(
           in_channels=snake_case_				,		out_channels=snake_case_				,		kernel_size=snake_case_				,		padding=snake_case_				,		bias=snake_case_				,		dilation=snake_case_				,		)
       UpperCAmelCase_							:       Union[str, Any]       =				nn.BatchNormad(snake_case_						)
       UpperCAmelCase_							:       Union[str, Any]       =				nn.ReLU()






  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[Any]       =				self.conv(snake_case_						)
       UpperCAmelCase_							:       Tuple       =				self.batch_norm(snake_case_						)
       UpperCAmelCase_							:       str       =				self.activation(snake_case_						)

       return output







class 					__SCREAMING_SNAKE_CASE						(	nn.Module	):







  '''simple docstring'''






  def __init__(			self				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       super().__init__()
       UpperCAmelCase_							:       Tuple       =				[
           nn.AdaptiveAvgPoolad(snake_case_						),
           UperNetConvModule(snake_case_				,		snake_case_				,		kernel_size=1						),
       ]
       for i, layer in enumerate(self.layers						):
            self.add_module(str(snake_case_						)				,		snake_case_						)






  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       str       =				input
       for layer in self.layers:
            UpperCAmelCase_							:       Union[str, Any]       =				layer(snake_case_						)
       return hidden_state







class 					__SCREAMING_SNAKE_CASE						(	nn.Module	):







  '''simple docstring'''






  def __init__(			self				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       super().__init__()
       UpperCAmelCase_							:       Any       =				pool_scales
       UpperCAmelCase_							:       str       =				align_corners
       UpperCAmelCase_							:       List[Any]       =				in_channels
       UpperCAmelCase_							:       Optional[int]       =				channels
       UpperCAmelCase_							:       List[Any]       =				[]
       for i, pool_scale in enumerate(snake_case_						):
            UpperCAmelCase_							:       List[Any]       =				UperNetPyramidPoolingBlock(pool_scale=snake_case_				,		in_channels=snake_case_				,		channels=snake_case_						)
            self.blocks.append(snake_case_						)
            self.add_module(str(snake_case_						)				,		snake_case_						)






  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Union[str, Any]       =				[]
       for ppm in self.blocks:
            UpperCAmelCase_							:       Union[str, Any]       =				ppm(snake_case_						)
            UpperCAmelCase_							:       Dict       =				nn.functional.interpolate(
                snake_case_				,		size=x.size()[2:]				,		mode='bilinear'				,		align_corners=self.align_corners						)
            ppm_outs.append(snake_case_						)
       return ppm_outs







class 					__SCREAMING_SNAKE_CASE						(	nn.Module	):







  '''simple docstring'''






  def __init__(			self				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       super().__init__()

       UpperCAmelCase_							:       List[str]       =				config
       UpperCAmelCase_							:       List[Any]       =				config.pool_scales  # e.g. (1, 2, 3, 6)
       UpperCAmelCase_							:       Dict       =				in_channels
       UpperCAmelCase_							:       int       =				config.hidden_size
       UpperCAmelCase_							:       Union[str, Any]       =				False
       UpperCAmelCase_							:       int       =				nn.Convad(self.channels				,		config.num_labels				,		kernel_size=1						)

       # PSP Module
       UpperCAmelCase_							:       int       =				UperNetPyramidPoolingModule(
           self.pool_scales				,		self.in_channels[-1]				,		self.channels				,		align_corners=self.align_corners				,		)
       UpperCAmelCase_							:       Union[str, Any]       =				UperNetConvModule(
           self.in_channels[-1] + len(self.pool_scales						) * self.channels				,		self.channels				,		kernel_size=3				,		padding=1				,		)
       # FPN Module
       UpperCAmelCase_							:       Tuple       =				nn.ModuleList()
       UpperCAmelCase_							:       Dict       =				nn.ModuleList()
       for in_channels in self.in_channels[:-1]:  # skip the top layer
            UpperCAmelCase_							:       Optional[Any]       =				UperNetConvModule(snake_case_				,		self.channels				,		kernel_size=1						)
            UpperCAmelCase_							:       str       =				UperNetConvModule(self.channels				,		self.channels				,		kernel_size=3				,		padding=1						)
            self.lateral_convs.append(snake_case_						)
            self.fpn_convs.append(snake_case_						)

       UpperCAmelCase_							:       int       =				UperNetConvModule(
           len(self.in_channels						) * self.channels				,		self.channels				,		kernel_size=3				,		padding=1				,		)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       self.apply(self._init_weights						)


  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       if isinstance(snake_case_				,		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 _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Optional[int]       =				inputs[-1]
       UpperCAmelCase_							:       Optional[Any]       =				[x]
       psp_outs.extend(self.psp_modules(snake_case_						)						)
       UpperCAmelCase_							:       Dict       =				torch.cat(snake_case_				,		dim=1						)
       UpperCAmelCase_							:       Dict       =				self.bottleneck(snake_case_						)

       return output






  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Union[str, Any]       =				[lateral_conv(encoder_hidden_states[i]						) for i, lateral_conv in enumerate(self.lateral_convs						)]

       laterals.append(self.psp_forward(snake_case_						)						)

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

       # build outputs
       UpperCAmelCase_							:       Dict       =				[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						):
            UpperCAmelCase_							:       Any       =				nn.functional.interpolate(
                fpn_outs[i]				,		size=fpn_outs[0].shape[2:]				,		mode='bilinear'				,		align_corners=self.align_corners						)
       UpperCAmelCase_							:       Optional[Any]       =				torch.cat(snake_case_				,		dim=1						)
       UpperCAmelCase_							:       int       =				self.fpn_bottleneck(snake_case_						)
       UpperCAmelCase_							:       List[str]       =				self.classifier(snake_case_						)

       return output







class 					__SCREAMING_SNAKE_CASE						(	nn.Module	):







  '''simple docstring'''






  def __init__(			self				,		snake_case_				,		snake_case_ = 2				,		snake_case_ = 3				,		snake_case_ = 1						):




       '''simple docstring'''

       super().__init__()

       UpperCAmelCase_							:       List[str]       =				config
       UpperCAmelCase_							:       Dict       =				config.auxiliary_in_channels
       UpperCAmelCase_							:       Union[str, Any]       =				config.auxiliary_channels
       UpperCAmelCase_							:       List[Any]       =				config.auxiliary_num_convs
       UpperCAmelCase_							:       Union[str, Any]       =				config.auxiliary_concat_input
       UpperCAmelCase_							:       Union[str, Any]       =				in_index

       UpperCAmelCase_							:       List[str]       =				(kernel_size // 2) * dilation
       UpperCAmelCase_							:       Dict       =				[]
       convs.append(
           UperNetConvModule(
               self.in_channels				,		self.channels				,		kernel_size=snake_case_				,		padding=snake_case_				,		dilation=snake_case_						)						)
       for i in range(self.num_convs - 1						):
            convs.append(
                UperNetConvModule(
                    self.channels				,		self.channels				,		kernel_size=snake_case_				,		padding=snake_case_				,		dilation=snake_case_						)						)
       if self.num_convs == 0:
            UpperCAmelCase_							:       List[Any]       =				nn.Identity()
       else:
            UpperCAmelCase_							:       Any       =				nn.Sequential(*snake_case_						)
       if self.concat_input:
            UpperCAmelCase_							:       Tuple       =				UperNetConvModule(
                self.in_channels + self.channels				,		self.channels				,		kernel_size=snake_case_				,		padding=kernel_size // 2						)

       UpperCAmelCase_							:       List[str]       =				nn.Convad(self.channels				,		config.num_labels				,		kernel_size=1						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       self.apply(self._init_weights						)


  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       if isinstance(snake_case_				,		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 _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Union[str, Any]       =				encoder_hidden_states[self.in_index]
       UpperCAmelCase_							:       Union[str, Any]       =				self.convs(snake_case_						)
       if self.concat_input:
            UpperCAmelCase_							:       List[str]       =				self.conv_cat(torch.cat([hidden_states, output]				,		dim=1						)						)
       UpperCAmelCase_							:       Optional[Any]       =				self.classifier(snake_case_						)
       return output







class 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_	):







  '''simple docstring'''






  lowerCamelCase_		:List[Any]						   =		UperNetConfig
  lowerCamelCase_		:int						   =		'''pixel_values'''
  lowerCamelCase_		:Optional[Any]						   =		True


  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       if isinstance(snake_case_				,		snake_case_						):
            module.backbone.init_weights()
            module.decode_head.init_weights()
            module.auxiliary_head.init_weights()


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       self.backbone.init_weights()
       self.decode_head.init_weights()
       self.auxiliary_head.init_weights()






  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_=False						):




       '''simple docstring'''

       if isinstance(snake_case_				,		snake_case_						):
            UpperCAmelCase_							:       Tuple       =				value







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

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






@add_start_docstrings(
    '''UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.'''						,     lowerCamelCase_						,     )
class 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_	):







  '''simple docstring'''






  def __init__(			self				,		snake_case_						):




       '''simple docstring'''

       super().__init__(snake_case_						)

       UpperCAmelCase_							:       List[str]       =				AutoBackbone.from_config(config.backbone_config						)

       # Semantic segmentation head(s)
       UpperCAmelCase_							:       Optional[Any]       =				UperNetHead(snake_case_				,		in_channels=self.backbone.channels						)
       UpperCAmelCase_							:       int       =				UperNetFCNHead(snake_case_						) 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=snake_case_				,		config_class=_CONFIG_FOR_DOC						)
  def _UpperCamelCase					(			self				,		snake_case_ = None				,		snake_case_ = None				,		snake_case_ = None				,		snake_case_ = None				,		snake_case_ = None				,		):




       '''simple docstring'''

       UpperCAmelCase_							:       Dict       =				return_dict if return_dict is not None else self.config.use_return_dict
       UpperCAmelCase_							:       List[str]       =				(
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )
       UpperCAmelCase_							:       Tuple       =				output_attentions if output_attentions is not None else self.config.output_attentions

       UpperCAmelCase_							:       List[Any]       =				self.backbone.forward_with_filtered_kwargs(
           snake_case_				,		output_hidden_states=snake_case_				,		output_attentions=snake_case_						)
       UpperCAmelCase_							:       str       =				outputs.feature_maps

       UpperCAmelCase_							:       Union[str, Any]       =				self.decode_head(snake_case_						)
       UpperCAmelCase_							:       Dict       =				nn.functional.interpolate(snake_case_				,		size=pixel_values.shape[2:]				,		mode='bilinear'				,		align_corners=snake_case_						)

       UpperCAmelCase_							:       List[str]       =				None
       if self.auxiliary_head is not None:
            UpperCAmelCase_							:       Optional[int]       =				self.auxiliary_head(snake_case_						)
            UpperCAmelCase_							:       Tuple       =				nn.functional.interpolate(
                snake_case_				,		size=pixel_values.shape[2:]				,		mode='bilinear'				,		align_corners=snake_case_						)

       UpperCAmelCase_							:       Union[str, Any]       =				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
                 UpperCAmelCase_							:       List[str]       =				CrossEntropyLoss(ignore_index=self.config.loss_ignore_index						)
                 UpperCAmelCase_							:       List[str]       =				loss_fct(snake_case_				,		snake_case_						)
                 UpperCAmelCase_							:       Optional[int]       =				loss_fct(snake_case_				,		snake_case_						)
                 UpperCAmelCase_							:       List[Any]       =				main_loss + self.config.auxiliary_loss_weight * auxiliary_loss

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

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







'''simple docstring'''


import json
import os
import subprocess
import unittest
from ast import literal_eval

import pytest
from parameterized import parameterized_class

from . import is_sagemaker_available


if is_sagemaker_available():
 from sagemaker import Session, TrainingJobAnalytics
 from sagemaker.huggingface import HuggingFace






@pytest.mark.skipif(
    literal_eval(os.getenv('''TEST_SAGEMAKER'''						,     '''False'''	)	) is not True						,     reason='''Skipping test because should only be run when releasing minor transformers version'''						,     )
@pytest.mark.usefixtures('''sm_env'''	)
@parameterized_class(
    [
        {
            '''framework''': '''pytorch''',
            '''script''': '''run_glue.py''',
            '''model_name_or_path''': '''distilbert-base-cased''',
            '''instance_type''': '''ml.g4dn.xlarge''',
            '''results''': {'''train_runtime''': 650, '''eval_accuracy''': 0.6, '''eval_loss''': 0.9},
        },
        {
            '''framework''': '''tensorflow''',
            '''script''': '''run_tf.py''',
            '''model_name_or_path''': '''distilbert-base-cased''',
            '''instance_type''': '''ml.g4dn.xlarge''',
            '''results''': {'''train_runtime''': 600, '''eval_accuracy''': 0.3, '''eval_loss''': 0.9},
        },
    ]	)
class 					__SCREAMING_SNAKE_CASE						(	unittest.TestCase	):







  '''simple docstring'''






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       if self.framework == "pytorch":
            subprocess.run(
                F'''cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py'''.split()				,		encoding='utf-8'				,		check=snake_case_				,		)
       assert hasattr(self				,		'env'						)


  def _UpperCamelCase					(			self				,		snake_case_=1						):




       '''simple docstring'''

       return HuggingFace(
           entry_point=self.script				,		source_dir=self.env.test_path				,		role=self.env.role				,		image_uri=self.env.image_uri				,		base_job_name=F'''{self.env.base_job_name}-single'''				,		instance_count=snake_case_				,		instance_type=self.instance_type				,		debugger_hook_config=snake_case_				,		hyperparameters={**self.env.hyperparameters, 'model_name_or_path': self.model_name_or_path}				,		metric_definitions=self.env.metric_definitions				,		py_version='py36'				,		)


  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       TrainingJobAnalytics(snake_case_						).export_csv(F'''{self.env.test_path}/{job_name}_metrics.csv'''						)






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Union[str, Any]       =				self.create_estimator()

       # run training
       estimator.fit()

       # result dataframe
       UpperCAmelCase_							:       int       =				TrainingJobAnalytics(estimator.latest_training_job.name						).dataframe()

       # extract kpis
       UpperCAmelCase_							:       Dict       =				list(result_metrics_df[result_metrics_df.metric_name == 'eval_accuracy']['value']						)
       UpperCAmelCase_							:       List[str]       =				list(result_metrics_df[result_metrics_df.metric_name == 'eval_loss']['value']						)
       # get train time from SageMaker job, this includes starting, preprocessing, stopping
       UpperCAmelCase_							:       int       =				(
           Session().describe_training_job(estimator.latest_training_job.name						).get('TrainingTimeInSeconds'				,		9_9_9_9_9_9						)
       )

       # assert kpis
       assert train_runtime <= self.results["train_runtime"]
       assert all(t >= self.results['eval_accuracy'] for t in eval_accuracy						)
       assert all(t <= self.results['eval_loss'] for t in eval_loss						)

       # dump tests result into json file to share in PR
       with open(F'''{estimator.latest_training_job.name}.json'''				,		'w'						) as outfile:
            json.dump({'train_time': train_runtime, 'eval_accuracy': eval_accuracy, 'eval_loss': eval_loss}				,		snake_case_						)






