'''simple docstring'''




import collections.abc
from typing import Optional, Tuple, Union

import torch
import torch.utils.checkpoint
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_poolformer import PoolFormerConfig


_A								=   logging.get_logger(__name__)

# General docstring
_A								=   'PoolFormerConfig'

# Base docstring
_A								=   'sail/poolformer_s12'
_A								=   [1, 5_1_2, 7, 7]

# Image classification docstring
_A								=   'sail/poolformer_s12'
_A								=   'tabby, tabby cat'

_A								=   [
    'sail/poolformer_s12',
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
]

def _UpperCamelCase   (       SCREAMING_SNAKE_CASE_  ,       SCREAMING_SNAKE_CASE_ = 0.0  ,       SCREAMING_SNAKE_CASE_ = False							):
     if drop_prob == 0.0 or not training:
          return input
     lowercase_					:       str     				=					1 - drop_prob
     lowercase_					:       int     				=					(input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
     lowercase_					:       List[str]     				=					keep_prob + torch.rand(SCREAMING_SNAKE_CASE_  ,       dtype=input.dtype  ,       device=input.device							)
     random_tensor.floor_()  # binarize
     lowercase_					:       Optional[int]     				=					input.div(SCREAMING_SNAKE_CASE_							) * random_tensor
     return output


class       UpperCAmelCase__  (       nn.Module			):






   """simple docstring"""
   def __init__(self       ,				_a = None				)    ->     None:
        super().__init__()
        lowercase_					:       Tuple     				=					drop_prob
   def 		_lowerCamelCase (self       ,				_a				)    ->     torch.Tensor:
        return drop_path(_a       ,				self.drop_prob       ,				self.training				)





   def 		_lowerCamelCase (self				)    ->     str:
        return "p={}".format(self.drop_prob				)



class       UpperCAmelCase__  (       nn.Module			):






   """simple docstring"""
   def __init__(self       ,				_a       ,				_a       ,				_a       ,				_a       ,				_a       ,				_a=None				)    ->     int:
        super().__init__()
        lowercase_					:       Union[str, Any]     				=					patch_size if isinstance(_a       ,				collections.abc.Iterable				) else (patch_size, patch_size)
        lowercase_					:       Optional[int]     				=					stride if isinstance(_a       ,				collections.abc.Iterable				) else (stride, stride)
        lowercase_					:       List[str]     				=					padding if isinstance(_a       ,				collections.abc.Iterable				) else (padding, padding)

        lowercase_					:       List[str]     				=					nn.Convad(_a       ,				_a       ,				kernel_size=_a       ,				stride=_a       ,				padding=_a				)
        lowercase_					:       Optional[Any]     				=					norm_layer(_a				) if norm_layer else nn.Identity()





   def 		_lowerCamelCase (self       ,				_a				)    ->     Optional[int]:
        lowercase_					:       List[str]     				=					self.projection(_a				)
        lowercase_					:       Dict     				=					self.norm(_a				)
        return embeddings



class       UpperCAmelCase__  (       nn.GroupNorm			):






   """simple docstring"""
   def __init__(self       ,				_a       ,				**_a				)    ->     List[str]:
        super().__init__(1       ,				_a       ,				**_a				)



class       UpperCAmelCase__  (       nn.Module			):






   """simple docstring"""
   def __init__(self       ,				_a				)    ->     List[Any]:
        super().__init__()
        lowercase_					:       List[str]     				=					nn.AvgPoolad(_a       ,				stride=1       ,				padding=pool_size // 2       ,				count_include_pad=_a				)





   def 		_lowerCamelCase (self       ,				_a				)    ->     Optional[int]:
        return self.pool(_a				) - hidden_states



class       UpperCAmelCase__  (       nn.Module			):






   """simple docstring"""
   def __init__(self       ,				_a       ,				_a       ,				_a       ,				_a				)    ->     Dict:
        super().__init__()
        lowercase_					:       Optional[Any]     				=					nn.Convad(_a       ,				_a       ,				1				)
        lowercase_					:       Optional[int]     				=					nn.Convad(_a       ,				_a       ,				1				)
        lowercase_					:       Union[str, Any]     				=					PoolFormerDropPath(_a				)
        if isinstance(config.hidden_act       ,				_a				):
             lowercase_					:       Optional[int]     				=					ACTaFN[config.hidden_act]
        else:
             lowercase_					:       List[str]     				=					config.hidden_act





   def 		_lowerCamelCase (self       ,				_a				)    ->     Optional[Any]:
        lowercase_					:       str     				=					self.conva(_a				)
        lowercase_					:       Optional[int]     				=					self.act_fn(_a				)
        lowercase_					:       List[str]     				=					self.drop(_a				)
        lowercase_					:       List[str]     				=					self.conva(_a				)
        lowercase_					:       Dict     				=					self.drop(_a				)

        return hidden_states



class       UpperCAmelCase__  (       nn.Module			):






   """simple docstring"""
   def __init__(self       ,				_a       ,				_a       ,				_a       ,				_a       ,				_a       ,				_a				)    ->     int:
        super().__init__()
        lowercase_					:       Optional[int]     				=					PoolFormerPooling(_a				)
        lowercase_					:       int     				=					PoolFormerOutput(_a       ,				_a       ,				_a       ,				_a				)
        lowercase_					:       Dict     				=					PoolFormerGroupNorm(_a				)
        lowercase_					:       List[str]     				=					PoolFormerGroupNorm(_a				)

        # Useful for training neural nets
        lowercase_					:       Optional[int]     				=					PoolFormerDropPath(_a				) if drop_path > 0.0 else nn.Identity()
        lowercase_					:       str     				=					config.use_layer_scale
        if config.use_layer_scale:
             lowercase_					:       Optional[int]     				=					nn.Parameter(
                 config.layer_scale_init_value * torch.ones((_a)				)       ,				requires_grad=_a				)
             lowercase_					:       Dict     				=					nn.Parameter(
                 config.layer_scale_init_value * torch.ones((_a)				)       ,				requires_grad=_a				)





   def 		_lowerCamelCase (self       ,				_a				)    ->     List[Any]:
        if self.use_layer_scale:
             lowercase_					:       List[Any]     				=					self.pooling(self.before_norm(_a				)				)
             lowercase_					:       Tuple     				=					self.layer_scale_a.unsqueeze(-1				).unsqueeze(-1				) * pooling_output
             # First residual connection
             lowercase_					:       Optional[int]     				=					hidden_states + self.drop_path(_a				)
             lowercase_					:       int     				=					()

             lowercase_					:       Optional[int]     				=					self.output(self.after_norm(_a				)				)
             lowercase_					:       Dict     				=					self.layer_scale_a.unsqueeze(-1				).unsqueeze(-1				) * layer_output
             # Second residual connection
             lowercase_					:       Tuple     				=					hidden_states + self.drop_path(_a				)

             lowercase_					:       Dict     				=					(output,) + outputs
             return outputs

        else:
             lowercase_					:       Dict     				=					self.drop_path(self.pooling(self.before_norm(_a				)				)				)
             # First residual connection
             lowercase_					:       Tuple     				=					pooling_output + hidden_states
             lowercase_					:       Union[str, Any]     				=					()

             # Second residual connection inside the PoolFormerOutput block
             lowercase_					:       Any     				=					self.drop_path(self.output(self.after_norm(_a				)				)				)
             lowercase_					:       Optional[Any]     				=					hidden_states + layer_output

             lowercase_					:       int     				=					(output,) + outputs
             return outputs



class       UpperCAmelCase__  (       nn.Module			):






   """simple docstring"""
   def __init__(self       ,				_a				)    ->     Tuple:
        super().__init__()
        lowercase_					:       List[str]     				=					config
        # stochastic depth decay rule
        lowercase_					:       List[str]     				=					[x.item() for x in torch.linspace(0       ,				config.drop_path_rate       ,				sum(config.depths				)				)]

        # patch embeddings
        lowercase_					:       Optional[int]     				=					[]
        for i in range(config.num_encoder_blocks				):
             embeddings.append(
                 PoolFormerEmbeddings(
                     patch_size=config.patch_sizes[i]       ,				stride=config.strides[i]       ,				padding=config.padding[i]       ,				num_channels=config.num_channels if i == 0 else config.hidden_sizes[i - 1]       ,				hidden_size=config.hidden_sizes[i]       ,				)				)
        lowercase_					:       Union[str, Any]     				=					nn.ModuleList(_a				)

        # Transformer blocks
        lowercase_					:       Union[str, Any]     				=					[]
        lowercase_					:       str     				=					0
        for i in range(config.num_encoder_blocks				):
             # each block consists of layers
             lowercase_					:       Optional[int]     				=					[]
             if i != 0:
                  cur += config.depths[i - 1]
             for j in range(config.depths[i]				):
                  layers.append(
                      PoolFormerLayer(
                          _a       ,				num_channels=config.hidden_sizes[i]       ,				pool_size=config.pool_size       ,				hidden_size=config.hidden_sizes[i]       ,				intermediate_size=int(config.hidden_sizes[i] * config.mlp_ratio				)       ,				drop_path=dpr[cur + j]       ,				)				)
             blocks.append(nn.ModuleList(_a				)				)

        lowercase_					:       Union[str, Any]     				=					nn.ModuleList(_a				)





   def 		_lowerCamelCase (self       ,				_a       ,				_a=False       ,				_a=True				)    ->     List[Any]:
        lowercase_					:       Optional[Any]     				=					() if output_hidden_states else None

        lowercase_					:       Optional[int]     				=					pixel_values
        for idx, layers in enumerate(zip(self.patch_embeddings       ,				self.block				)				):
             lowercase_				,lowercase_					:       List[str]     				=					layers
             # Get patch embeddings from hidden_states
             lowercase_					:       str     				=					embedding_layer(_a				)
             # Send the embeddings through the blocks
             for _, blk in enumerate(_a				):
                  lowercase_					:       Optional[Any]     				=					blk(_a				)
                  lowercase_					:       Dict     				=					layer_outputs[0]

             if output_hidden_states:
                  lowercase_					:       int     				=					all_hidden_states + (hidden_states,)

        if not return_dict:
             return tuple(v for v in [hidden_states, all_hidden_states] if v is not None				)

        return BaseModelOutputWithNoAttention(last_hidden_state=_a       ,				hidden_states=_a				)



class       UpperCAmelCase__  (       _snake_case			):






   """simple docstring"""
   A							:       Tuple     =    PoolFormerConfig
   A							:       Tuple     =    '''poolformer'''
   A							:       List[str]     =    '''pixel_values'''
   A							:       List[Any]     =    True
   def 		_lowerCamelCase (self       ,				_a				)    ->     int:
        if isinstance(_a       ,				(nn.Linear, nn.Convad)				):
             module.weight.data.normal_(mean=0.0       ,				std=self.config.initializer_range				)
             if module.bias is not None:
                  module.bias.data.zero_()
        elif isinstance(_a       ,				nn.LayerNorm				):
             module.bias.data.zero_()
             module.weight.data.fill_(1.0				)





   def 		_lowerCamelCase (self       ,				_a       ,				_a=False				)    ->     Union[str, Any]:
        if isinstance(_a       ,				_a				):
             lowercase_					:       Optional[int]     				=					value



_A								=   R'\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\n    Parameters:\n        config ([`PoolFormerConfig`]): 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'

_A								=   R'\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`PoolFormerImageProcessor.__call__`] for details.\n'


@add_start_docstrings(
    '''The bare PoolFormer Model transformer outputting raw hidden-states without any specific head on top.'''					,     _snake_case					,     )
class       UpperCAmelCase__  (       _snake_case			):






   """simple docstring"""
   def __init__(self       ,				_a				)    ->     Any:
        super().__init__(_a				)
        lowercase_					:       int     				=					config

        lowercase_					:       Any     				=					PoolFormerEncoder(_a				)

        # Initialize weights and apply final processing
        self.post_init()
   def 		_lowerCamelCase (self				)    ->     List[Any]:
        return self.embeddings.patch_embeddings





   @add_start_docstrings_to_model_forward(_a				)
   @add_code_sample_docstrings(
       checkpoint=_CHECKPOINT_FOR_DOC       ,				output_type=_a       ,				config_class=_CONFIG_FOR_DOC       ,				modality='vision'       ,				expected_output=_EXPECTED_OUTPUT_SHAPE       ,				)
   def 		_lowerCamelCase (self       ,				_a = None       ,				_a = None       ,				_a = None       ,				)    ->     Union[Tuple, BaseModelOutputWithNoAttention]:
        lowercase_					:       Tuple     				=					(
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        lowercase_					:       List[str]     				=					return_dict if return_dict is not None else self.config.use_return_dict

        if pixel_values is None:
             raise ValueError('You have to specify pixel_values'				)

        lowercase_					:       List[Any]     				=					self.encoder(
            _a       ,				output_hidden_states=_a       ,				return_dict=_a       ,				)
        lowercase_					:       Optional[Any]     				=					encoder_outputs[0]

        if not return_dict:
             return (sequence_output, None) + encoder_outputs[1:]

        return BaseModelOutputWithNoAttention(
            last_hidden_state=_a       ,				hidden_states=encoder_outputs.hidden_states       ,				)



class       UpperCAmelCase__  (       nn.Module			):






   """simple docstring"""
   def __init__(self       ,				_a				)    ->     Optional[int]:
        super().__init__()
        lowercase_					:       List[Any]     				=					nn.Linear(config.hidden_size       ,				config.hidden_size				)





   def 		_lowerCamelCase (self       ,				_a				)    ->     Optional[Any]:
        lowercase_					:       Optional[int]     				=					self.dense(_a				)
        return output



@add_start_docstrings(
    '''
    PoolFormer Model transformer with an image classification head on top
    '''					,     _snake_case					,     )
class       UpperCAmelCase__  (       _snake_case			):






   """simple docstring"""
   def __init__(self       ,				_a				)    ->     List[str]:
        super().__init__(_a				)
        lowercase_					:       List[Any]     				=					config.num_labels
        lowercase_					:       Dict     				=					PoolFormerModel(_a				)

        # Final norm
        lowercase_					:       int     				=					PoolFormerGroupNorm(config.hidden_sizes[-1]				)
        # Classifier head
        lowercase_					:       Tuple     				=					(
            nn.Linear(config.hidden_sizes[-1]       ,				config.num_labels				) if config.num_labels > 0 else nn.Identity()
        )

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





   @add_start_docstrings_to_model_forward(_a				)
   @add_code_sample_docstrings(
       checkpoint=_IMAGE_CLASS_CHECKPOINT       ,				output_type=_a       ,				config_class=_CONFIG_FOR_DOC       ,				expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT       ,				)
   def 		_lowerCamelCase (self       ,				_a = None       ,				_a = None       ,				_a = None       ,				_a = None       ,				)    ->     Union[Tuple, ImageClassifierOutputWithNoAttention]:
        lowercase_					:       List[Any]     				=					return_dict if return_dict is not None else self.config.use_return_dict

        lowercase_					:       Union[str, Any]     				=					self.poolformer(
            _a       ,				output_hidden_states=_a       ,				return_dict=_a       ,				)

        lowercase_					:       Tuple     				=					outputs[0]

        lowercase_					:       List[str]     				=					self.classifier(self.norm(_a				).mean([-2, -1]				)				)

        lowercase_					:       Any     				=					None
        if labels is not None:
             if self.config.problem_type is None:
                  if self.num_labels == 1:
                       lowercase_					:       Optional[int]     				=					'regression'
                  elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
                       lowercase_					:       Tuple     				=					'single_label_classification'
                  else:
                       lowercase_					:       Optional[Any]     				=					'multi_label_classification'

             if self.config.problem_type == "regression":
                  lowercase_					:       str     				=					MSELoss()
                  if self.num_labels == 1:
                       lowercase_					:       Optional[Any]     				=					loss_fct(logits.squeeze()       ,				labels.squeeze()				)
                  else:
                       lowercase_					:       Any     				=					loss_fct(_a       ,				_a				)
             elif self.config.problem_type == "single_label_classification":
                  lowercase_					:       Tuple     				=					CrossEntropyLoss()
                  lowercase_					:       str     				=					loss_fct(logits.view(-1       ,				self.num_labels				)       ,				labels.view(-1				)				)
             elif self.config.problem_type == "multi_label_classification":
                  lowercase_					:       Dict     				=					BCEWithLogitsLoss()
                  lowercase_					:       List[str]     				=					loss_fct(_a       ,				_a				)

        if not return_dict:
             lowercase_					:       List[str]     				=					(logits,) + outputs[2:]
             return ((loss,) + output) if loss is not None else output

        return ImageClassifierOutputWithNoAttention(loss=_a       ,				logits=_a       ,				hidden_states=outputs.hidden_states				)





'''simple docstring'''




import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

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


if is_torch_available():
 import torch

 from transformers import (
     MODEL_MAPPING,
     SegformerForImageClassification,
     SegformerForSemanticSegmentation,
     SegformerModel,
 )
 from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import SegformerImageProcessor


class       UpperCAmelCase__  (       _snake_case			):






   """simple docstring"""
   def 		_lowerCamelCase (self				)    ->     Any:
        lowercase_					:       Optional[Any]     				=					self.config_class(**self.inputs_dict				)
        self.parent.assertTrue(hasattr(_a       ,				'hidden_sizes'				)				)
        self.parent.assertTrue(hasattr(_a       ,				'num_attention_heads'				)				)
        self.parent.assertTrue(hasattr(_a       ,				'num_encoder_blocks'				)				)



class       UpperCAmelCase__  :






   """simple docstring"""
   def __init__(self       ,				_a       ,				_a=13       ,				_a=64       ,				_a=3       ,				_a=4       ,				_a=[2, 2, 2, 2]       ,				_a=[8, 4, 2, 1]       ,				_a=[16, 32, 64, 128]       ,				_a=[1, 4, 8, 16]       ,				_a=[1, 2, 4, 8]       ,				_a=True       ,				_a=True       ,				_a="gelu"       ,				_a=0.1       ,				_a=0.1       ,				_a=0.02       ,				_a=3       ,				_a=None       ,				)    ->     List[Any]:
        lowercase_					:       Dict     				=					parent
        lowercase_					:       int     				=					batch_size
        lowercase_					:       Any     				=					image_size
        lowercase_					:       List[str]     				=					num_channels
        lowercase_					:       str     				=					num_encoder_blocks
        lowercase_					:       Tuple     				=					sr_ratios
        lowercase_					:       Tuple     				=					depths
        lowercase_					:       Dict     				=					hidden_sizes
        lowercase_					:       Optional[int]     				=					downsampling_rates
        lowercase_					:       List[str]     				=					num_attention_heads
        lowercase_					:       List[Any]     				=					is_training
        lowercase_					:       Dict     				=					use_labels
        lowercase_					:       Optional[Any]     				=					hidden_act
        lowercase_					:       Any     				=					hidden_dropout_prob
        lowercase_					:       int     				=					attention_probs_dropout_prob
        lowercase_					:       Any     				=					initializer_range
        lowercase_					:       List[str]     				=					num_labels
        lowercase_					:       str     				=					scope
   def 		_lowerCamelCase (self				)    ->     str:
        lowercase_					:       Union[str, Any]     				=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

        lowercase_					:       str     				=					None
        if self.use_labels:
             lowercase_					:       Optional[Any]     				=					ids_tensor([self.batch_size, self.image_size, self.image_size]       ,				self.num_labels				)

        lowercase_					:       Tuple     				=					self.get_config()
        return config, pixel_values, labels
   def 		_lowerCamelCase (self				)    ->     List[Any]:
        return SegformerConfig(
            image_size=self.image_size       ,				num_channels=self.num_channels       ,				num_encoder_blocks=self.num_encoder_blocks       ,				depths=self.depths       ,				hidden_sizes=self.hidden_sizes       ,				num_attention_heads=self.num_attention_heads       ,				hidden_act=self.hidden_act       ,				hidden_dropout_prob=self.hidden_dropout_prob       ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,				initializer_range=self.initializer_range       ,				)
   def 		_lowerCamelCase (self       ,				_a       ,				_a       ,				_a				)    ->     Dict:
        lowercase_					:       Union[str, Any]     				=					SegformerModel(config=_a				)
        model.to(_a				)
        model.eval()
        lowercase_					:       int     				=					model(_a				)
        lowercase_					:       Any     				=					self.image_size // (self.downsampling_rates[-1] * 2)
        self.parent.assertEqual(
            result.last_hidden_state.shape       ,				(self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)				)
   def 		_lowerCamelCase (self       ,				_a       ,				_a       ,				_a				)    ->     Tuple:
        lowercase_					:       Optional[Any]     				=					self.num_labels
        lowercase_					:       Union[str, Any]     				=					SegformerForSemanticSegmentation(_a				)
        model.to(_a				)
        model.eval()
        lowercase_					:       Tuple     				=					model(_a				)
        self.parent.assertEqual(
            result.logits.shape       ,				(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)				)
        lowercase_					:       str     				=					model(_a       ,				labels=_a				)
        self.parent.assertEqual(
            result.logits.shape       ,				(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)				)
        self.parent.assertGreater(result.loss       ,				0.0				)
   def 		_lowerCamelCase (self       ,				_a       ,				_a       ,				_a				)    ->     int:
        lowercase_					:       Dict     				=					1
        lowercase_					:       Dict     				=					SegformerForSemanticSegmentation(config=_a				)
        model.to(_a				)
        model.eval()
        lowercase_					:       List[str]     				=					torch.randint(0       ,				1       ,				(self.batch_size, self.image_size, self.image_size)				).to(_a				)
        lowercase_					:       Any     				=					model(_a       ,				labels=_a				)
        self.parent.assertGreater(result.loss       ,				0.0				)





   def 		_lowerCamelCase (self				)    ->     str:
        lowercase_					:       Any     				=					self.prepare_config_and_inputs()
        lowercase_				,lowercase_				,lowercase_					:       Dict     				=					config_and_inputs
        lowercase_					:       int     				=					{'pixel_values': pixel_values}
        return config, inputs_dict



@require_torch
class       UpperCAmelCase__  (       _snake_case					,     _snake_case					,     unittest.TestCase			):






   """simple docstring"""
   A							:       int     =    (
       (
           SegformerModel,
           SegformerForSemanticSegmentation,
           SegformerForImageClassification,
       )
       if is_torch_available()
       else ()
   )
   A							:       Tuple     =    (
       {
           '''feature-extraction''': SegformerModel,
           '''image-classification''': SegformerForImageClassification,
           '''image-segmentation''': SegformerForSemanticSegmentation,
       }
       if is_torch_available()
       else {}
   )

   A							:       str     =    True
   A							:       int     =    False
   A							:       int     =    False
   A							:       Optional[Any]     =    False
   def 		_lowerCamelCase (self				)    ->     int:
        lowercase_					:       List[str]     				=					SegformerModelTester(self				)
        lowercase_					:       str     				=					SegformerConfigTester(self       ,				config_class=_a				)
   def 		_lowerCamelCase (self				)    ->     str:
        self.config_tester.run_common_tests()
   def 		_lowerCamelCase (self				)    ->     Dict:
        lowercase_					:       Dict     				=					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_a				)
   def 		_lowerCamelCase (self				)    ->     Dict:
        lowercase_					:       Any     				=					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_binary_image_segmentation(*_a				)
   def 		_lowerCamelCase (self				)    ->     Union[str, Any]:
        lowercase_					:       int     				=					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_segmentation(*_a				)
   @unittest.skip('SegFormer does not use inputs_embeds'				)
   def 		_lowerCamelCase (self				)    ->     Any:
        pass
   @unittest.skip('SegFormer does not have get_input_embeddings method and get_output_embeddings methods'				)
   def 		_lowerCamelCase (self				)    ->     Optional[int]:
        pass
   def 		_lowerCamelCase (self				)    ->     Dict:
        lowercase_				,lowercase_					:       Optional[int]     				=					self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             lowercase_					:       int     				=					model_class(_a				)
             lowercase_					:       Optional[int]     				=					inspect.signature(model.forward				)
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             lowercase_					:       str     				=					[*signature.parameters.keys()]

             lowercase_					:       Dict     				=					['pixel_values']
             self.assertListEqual(arg_names[:1]       ,				_a				)
   def 		_lowerCamelCase (self				)    ->     Tuple:
        lowercase_				,lowercase_					:       Dict     				=					self.model_tester.prepare_config_and_inputs_for_common()
        lowercase_					:       Optional[Any]     				=					True

        for model_class in self.all_model_classes:
             lowercase_					:       Any     				=					True
             lowercase_					:       int     				=					False
             lowercase_					:       int     				=					True
             lowercase_					:       Tuple     				=					model_class(_a				)
             model.to(_a				)
             model.eval()
             with torch.no_grad():
                  lowercase_					:       int     				=					model(**self._prepare_for_class(_a       ,				_a				)				)
             lowercase_					:       Union[str, Any]     				=					outputs.attentions

             lowercase_					:       Tuple     				=					sum(self.model_tester.depths				)
             self.assertEqual(len(_a				)       ,				_a				)

             # check that output_attentions also work using config
             del inputs_dict["output_attentions"]
             lowercase_					:       List[str]     				=					True
             lowercase_					:       Dict     				=					model_class(_a				)
             model.to(_a				)
             model.eval()
             with torch.no_grad():
                  lowercase_					:       Optional[Any]     				=					model(**self._prepare_for_class(_a       ,				_a				)				)
             lowercase_					:       List[Any]     				=					outputs.attentions

             self.assertEqual(len(_a				)       ,				_a				)

             # verify the first attentions (first block, first layer)
             lowercase_					:       Any     				=					(self.model_tester.image_size // 4) ** 2
             lowercase_					:       Dict     				=					(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
             self.assertListEqual(
                 list(attentions[0].shape[-3:]				)       ,				[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]       ,				)

             # verify the last attentions (last block, last layer)
             lowercase_					:       Optional[int]     				=					(self.model_tester.image_size // 32) ** 2
             lowercase_					:       Optional[int]     				=					(self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
             self.assertListEqual(
                 list(attentions[-1].shape[-3:]				)       ,				[self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]       ,				)
             lowercase_					:       Optional[Any]     				=					len(_a				)

             # Check attention is always last and order is fine
             lowercase_					:       List[Any]     				=					True
             lowercase_					:       Union[str, Any]     				=					True
             lowercase_					:       Optional[int]     				=					model_class(_a				)
             model.to(_a				)
             model.eval()
             with torch.no_grad():
                  lowercase_					:       Any     				=					model(**self._prepare_for_class(_a       ,				_a				)				)

             self.assertEqual(out_len + 1       ,				len(_a				)				)

             lowercase_					:       List[Any]     				=					outputs.attentions

             self.assertEqual(len(_a				)       ,				_a				)
             # verify the first attentions (first block, first layer)
             lowercase_					:       Any     				=					(self.model_tester.image_size // 4) ** 2
             lowercase_					:       List[str]     				=					(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
             self.assertListEqual(
                 list(self_attentions[0].shape[-3:]				)       ,				[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]       ,				)
   def 		_lowerCamelCase (self				)    ->     List[str]:
        def check_hidden_states_output(_a       ,				_a       ,				_a				):
             lowercase_					:       Dict     				=					model_class(_a				)
             model.to(_a				)
             model.eval()

             with torch.no_grad():
                  lowercase_					:       List[Any]     				=					model(**self._prepare_for_class(_a       ,				_a				)				)

             lowercase_					:       Any     				=					outputs.hidden_states

             lowercase_					:       str     				=					self.model_tester.num_encoder_blocks
             self.assertEqual(len(_a				)       ,				_a				)

             # verify the first hidden states (first block)
             self.assertListEqual(
                 list(hidden_states[0].shape[-3:]				)       ,				[
                     self.model_tester.hidden_sizes[0],
                     self.model_tester.image_size // 4,
                     self.model_tester.image_size // 4,
                 ]       ,				)

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

        for model_class in self.all_model_classes:
             lowercase_					:       str     				=					True
             check_hidden_states_output(_a       ,				_a       ,				_a				)

             # check that output_hidden_states also work using config
             del inputs_dict["output_hidden_states"]
             lowercase_					:       Optional[Any]     				=					True

             check_hidden_states_output(_a       ,				_a       ,				_a				)
   def 		_lowerCamelCase (self				)    ->     Dict:
        if not self.model_tester.is_training:
             return

        lowercase_				,lowercase_					:       Optional[Any]     				=					self.model_tester.prepare_config_and_inputs_for_common()
        lowercase_					:       List[Any]     				=					True

        for model_class in self.all_model_classes:
             if model_class in get_values(_a				):
                  continue

             lowercase_					:       Optional[int]     				=					model_class(_a				)
             model.to(_a				)
             model.train()
             lowercase_					:       List[Any]     				=					self._prepare_for_class(_a       ,				_a       ,				return_labels=_a				)
             lowercase_					:       str     				=					model(**_a				).loss
             loss.backward()
   @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'				)
   def 		_lowerCamelCase (self				)    ->     str:
        pass





   @slow
   def 		_lowerCamelCase (self				)    ->     Any:
        for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowercase_					:       List[Any]     				=					SegformerModel.from_pretrained(_a				)
             self.assertIsNotNone(_a				)



def _UpperCamelCase   (       ):
     lowercase_					:       int     				=					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'							)
     return image






@require_torch
class       UpperCAmelCase__  (       unittest.TestCase			):






   """simple docstring"""
   @slow
   def 		_lowerCamelCase (self				)    ->     Union[str, Any]:
        # only resize + normalize
        lowercase_					:       Union[str, Any]     				=					SegformerImageProcessor(
            image_scale=(512, 512)       ,				keep_ratio=_a       ,				align=_a       ,				do_random_crop=_a				)
        lowercase_					:       List[str]     				=					SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512'				).to(
            _a				)

        lowercase_					:       int     				=					prepare_img()
        lowercase_					:       int     				=					image_processor(images=_a       ,				return_tensors='pt'				)
        lowercase_					:       int     				=					encoded_inputs.pixel_values.to(_a				)

        with torch.no_grad():
             lowercase_					:       List[Any]     				=					model(_a				)

        lowercase_					:       Union[str, Any]     				=					torch.Size((1, model.config.num_labels, 128, 128)				)
        self.assertEqual(outputs.logits.shape       ,				_a				)

        lowercase_					:       int     				=					torch.tensor(
            [
                [[-4.63_10, -5.52_32, -6.23_56], [-5.19_21, -6.14_44, -6.59_96], [-5.44_24, -6.27_90, -6.75_74]],
                [[-12.13_91, -13.31_22, -13.95_54], [-12.87_32, -13.93_52, -14.35_63], [-12.94_38, -13.82_26, -14.25_13]],
                [[-12.51_34, -13.46_86, -14.49_15], [-12.86_69, -14.43_43, -14.77_58], [-13.25_23, -14.58_19, -15.06_94]],
            ]				).to(_a				)
        self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]       ,				_a       ,				atol=1e-4				)				)
   @slow
   def 		_lowerCamelCase (self				)    ->     Dict:
        # only resize + normalize
        lowercase_					:       Optional[Any]     				=					SegformerImageProcessor(
            image_scale=(512, 512)       ,				keep_ratio=_a       ,				align=_a       ,				do_random_crop=_a				)
        lowercase_					:       Union[str, Any]     				=					SegformerForSemanticSegmentation.from_pretrained(
            'nvidia/segformer-b1-finetuned-cityscapes-1024-1024'				).to(_a				)

        lowercase_					:       Optional[Any]     				=					prepare_img()
        lowercase_					:       Tuple     				=					image_processor(images=_a       ,				return_tensors='pt'				)
        lowercase_					:       Any     				=					encoded_inputs.pixel_values.to(_a				)

        with torch.no_grad():
             lowercase_					:       Union[str, Any]     				=					model(_a				)

        lowercase_					:       str     				=					torch.Size((1, model.config.num_labels, 128, 128)				)
        self.assertEqual(outputs.logits.shape       ,				_a				)

        lowercase_					:       List[Any]     				=					torch.tensor(
            [
                [[-13.57_48, -13.91_11, -12.65_00], [-14.35_00, -15.36_83, -14.23_28], [-14.75_32, -16.04_24, -15.60_87]],
                [[-17.16_51, -15.87_25, -12.96_53], [-17.25_80, -17.37_18, -14.82_23], [-16.60_58, -16.87_83, -16.74_52]],
                [[-3.64_56, -3.02_09, -1.42_03], [-3.07_97, -3.19_59, -2.00_00], [-1.87_57, -1.92_17, -1.69_97]],
            ]				).to(_a				)
        self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]       ,				_a       ,				atol=1e-1				)				)





   @slow
   def 		_lowerCamelCase (self				)    ->     Optional[Any]:
        # only resize + normalize
        lowercase_					:       List[Any]     				=					SegformerImageProcessor(
            image_scale=(512, 512)       ,				keep_ratio=_a       ,				align=_a       ,				do_random_crop=_a				)
        lowercase_					:       List[Any]     				=					SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512'				).to(
            _a				)

        lowercase_					:       List[str]     				=					prepare_img()
        lowercase_					:       List[str]     				=					image_processor(images=_a       ,				return_tensors='pt'				)
        lowercase_					:       Optional[Any]     				=					encoded_inputs.pixel_values.to(_a				)

        with torch.no_grad():
             lowercase_					:       Dict     				=					model(_a				)

        lowercase_					:       Optional[int]     				=					outputs.logits.detach().cpu()

        lowercase_					:       Tuple     				=					image_processor.post_process_semantic_segmentation(outputs=_a       ,				target_sizes=[(500, 300)]				)
        lowercase_					:       Any     				=					torch.Size((500, 300)				)
        self.assertEqual(segmentation[0].shape       ,				_a				)

        lowercase_					:       List[str]     				=					image_processor.post_process_semantic_segmentation(outputs=_a				)
        lowercase_					:       Optional[int]     				=					torch.Size((128, 128)				)
        self.assertEqual(segmentation[0].shape       ,				_a				)




