





'''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_       ):




      def     lowercase__   (      self      :			int  )    ->				List[str]:
            _lowerCAmelCase												=							self.config_class(**self.inputs_dict  )
            self.parent.assertTrue(hasattr(__snake_case      ,      """hidden_sizes"""  )  )
            self.parent.assertTrue(hasattr(__snake_case      ,      """num_attention_heads"""  )  )
            self.parent.assertTrue(hasattr(__snake_case      ,      """num_encoder_blocks"""  )  )




class 			UpperCAmelCase					:




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




      def     lowercase__   (      self      :			List[str]  )    ->				Any:
            _lowerCAmelCase												=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

            _lowerCAmelCase												=							None
            if self.use_labels:
                  _lowerCAmelCase												=							ids_tensor([self.batch_size, self.image_size, self.image_size]      ,      self.num_labels  )

            _lowerCAmelCase												=							self.get_config()
            return config, pixel_values, labels




      def     lowercase__   (      self      :			List[Any]  )    ->				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     lowercase__   (      self      :			Union[str, Any]      ,      __snake_case      :			Union[str, Any]      ,      __snake_case      :			str      ,      __snake_case      :			Optional[Any]  )    ->				Optional[int]:
            _lowerCAmelCase												=							SegformerModel(config=__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase												=							model(__snake_case  )
            _lowerCAmelCase												=							_lowerCAmelCase												=							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     lowercase__   (      self      :			Optional[Any]      ,      __snake_case      :			int      ,      __snake_case      :			Dict      ,      __snake_case      :			Union[str, Any]  )    ->				Dict:
            _lowerCAmelCase												=							self.num_labels
            _lowerCAmelCase												=							SegformerForSemanticSegmentation(__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase												=							model(__snake_case  )
            self.parent.assertEqual(
                result.logits.shape      ,      (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)  )
            _lowerCAmelCase												=							model(__snake_case      ,      labels=__snake_case  )
            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     lowercase__   (      self      :			str      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			Dict      ,      __snake_case      :			List[str]  )    ->				Tuple:
            _lowerCAmelCase												=							1
            _lowerCAmelCase												=							SegformerForSemanticSegmentation(config=__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase												=							torch.randint(0      ,      1      ,      (self.batch_size, self.image_size, self.image_size)  ).to(__snake_case  )
            _lowerCAmelCase												=							model(__snake_case      ,      labels=__snake_case  )
            self.parent.assertGreater(result.loss      ,      0.0  )




      def     lowercase__   (      self      :			int  )    ->				int:
            _lowerCAmelCase												=							self.prepare_config_and_inputs()
            _lowerCAmelCase					,							_lowerCAmelCase					,							_lowerCAmelCase												=							config_and_inputs
            _lowerCAmelCase												=							{"""pixel_values""": pixel_values}
            return config, inputs_dict




@require_torch
class 			UpperCAmelCase					(      snake_case_							,		snake_case_							,		unittest.TestCase       ):
      _lowercase:      Dict											=       (
          (
              SegformerModel,
              SegformerForSemanticSegmentation,
              SegformerForImageClassification,
          )
          if is_torch_available()
          else ()
      )
      _lowercase:      List[str]											=       (
          {
              '''feature-extraction''': SegformerModel,
              '''image-classification''': SegformerForImageClassification,
              '''image-segmentation''': SegformerForSemanticSegmentation,
          }
          if is_torch_available()
          else {}
      )

      _lowercase:      int											=       True
      _lowercase:      Union[str, Any]											=       False
      _lowercase:      Dict											=       False
      _lowercase:      Optional[Any]											=       False




      def     lowercase__   (      self      :			int  )    ->				int:
            _lowerCAmelCase												=							SegformerModelTester(self  )
            _lowerCAmelCase												=							SegformerConfigTester(self      ,      config_class=__snake_case  )




      def     lowercase__   (      self      :			int  )    ->				str:
            self.config_tester.run_common_tests()




      def     lowercase__   (      self      :			Any  )    ->				List[Any]:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*__snake_case  )




      def     lowercase__   (      self      :			str  )    ->				List[Any]:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_binary_image_segmentation(*__snake_case  )




      def     lowercase__   (      self      :			Tuple  )    ->				Union[str, Any]:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_segmentation(*__snake_case  )




      @unittest.skip("""SegFormer does not use inputs_embeds"""  )
      def     lowercase__   (      self      :			Optional[int]  )    ->				Optional[int]:
            pass




      @unittest.skip("""SegFormer does not have get_input_embeddings method and get_output_embeddings methods"""  )
      def     lowercase__   (      self      :			Tuple  )    ->				str:
            pass




      def     lowercase__   (      self      :			Optional[int]  )    ->				Union[str, Any]:
            _lowerCAmelCase					,							_lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs_for_common()

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

                  _lowerCAmelCase												=							["""pixel_values"""]
                  self.assertListEqual(arg_names[:1]      ,      __snake_case  )




      def     lowercase__   (      self      :			Union[str, Any]  )    ->				Optional[Any]:
            _lowerCAmelCase					,							_lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs_for_common()
            _lowerCAmelCase												=							True

            for model_class in self.all_model_classes:
                  _lowerCAmelCase												=							True
                  _lowerCAmelCase												=							False
                  _lowerCAmelCase												=							True
                  _lowerCAmelCase												=							model_class(__snake_case  )
                  model.to(__snake_case  )
                  model.eval()
                  with torch.no_grad():
                        _lowerCAmelCase												=							model(**self._prepare_for_class(__snake_case      ,      __snake_case  )  )
                  _lowerCAmelCase												=							outputs.attentions

                  _lowerCAmelCase												=							sum(self.model_tester.depths  )
                  self.assertEqual(len(__snake_case  )      ,      __snake_case  )

                  # check that output_attentions also work using config
                  del inputs_dict["output_attentions"]
                  _lowerCAmelCase												=							True
                  _lowerCAmelCase												=							model_class(__snake_case  )
                  model.to(__snake_case  )
                  model.eval()
                  with torch.no_grad():
                        _lowerCAmelCase												=							model(**self._prepare_for_class(__snake_case      ,      __snake_case  )  )
                  _lowerCAmelCase												=							outputs.attentions

                  self.assertEqual(len(__snake_case  )      ,      __snake_case  )

                  # verify the first attentions (first block, first layer)
                  _lowerCAmelCase												=							(self.model_tester.image_size // 4) ** 2
                  _lowerCAmelCase												=							(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)
                  _lowerCAmelCase												=							(self.model_tester.image_size // 32) ** 2
                  _lowerCAmelCase												=							(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]      ,      )
                  _lowerCAmelCase												=							len(__snake_case  )

                  # Check attention is always last and order is fine
                  _lowerCAmelCase												=							True
                  _lowerCAmelCase												=							True
                  _lowerCAmelCase												=							model_class(__snake_case  )
                  model.to(__snake_case  )
                  model.eval()
                  with torch.no_grad():
                        _lowerCAmelCase												=							model(**self._prepare_for_class(__snake_case      ,      __snake_case  )  )

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

                  _lowerCAmelCase												=							outputs.attentions

                  self.assertEqual(len(__snake_case  )      ,      __snake_case  )
                  # verify the first attentions (first block, first layer)
                  _lowerCAmelCase												=							(self.model_tester.image_size // 4) ** 2
                  _lowerCAmelCase												=							(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     lowercase__   (      self      :			Dict  )    ->				int:
            def check_hidden_states_output(__snake_case      :			Union[str, Any]      ,      __snake_case      :			List[Any]      ,      __snake_case      :			str  ):
                  _lowerCAmelCase												=							model_class(__snake_case  )
                  model.to(__snake_case  )
                  model.eval()

                  with torch.no_grad():
                        _lowerCAmelCase												=							model(**self._prepare_for_class(__snake_case      ,      __snake_case  )  )

                  _lowerCAmelCase												=							outputs.hidden_states

                  _lowerCAmelCase												=							self.model_tester.num_encoder_blocks
                  self.assertEqual(len(__snake_case  )      ,      __snake_case  )

                  # 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,
                      ]      ,      )

            _lowerCAmelCase					,							_lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                  _lowerCAmelCase												=							True
                  check_hidden_states_output(__snake_case      ,      __snake_case      ,      __snake_case  )

                  # check that output_hidden_states also work using config
                  del inputs_dict["output_hidden_states"]
                  _lowerCAmelCase												=							True

                  check_hidden_states_output(__snake_case      ,      __snake_case      ,      __snake_case  )




      def     lowercase__   (      self      :			Union[str, Any]  )    ->				Union[str, Any]:
            if not self.model_tester.is_training:
                  return

            _lowerCAmelCase					,							_lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs_for_common()
            _lowerCAmelCase												=							True

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

                  _lowerCAmelCase												=							model_class(__snake_case  )
                  model.to(__snake_case  )
                  model.train()
                  _lowerCAmelCase												=							self._prepare_for_class(__snake_case      ,      __snake_case      ,      return_labels=__snake_case  )
                  _lowerCAmelCase												=							model(**__snake_case  ).loss
                  loss.backward()




      @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""  )
      def     lowercase__   (      self      :			Optional[Any]  )    ->				str:
            pass




      @slow
      def     lowercase__   (      self      :			Optional[Any]  )    ->				str:
            for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  _lowerCAmelCase												=							SegformerModel.from_pretrained(__snake_case  )
                  self.assertIsNotNone(__snake_case  )




def        UpperCamelCase__				(		):





      """simple docstring"""


      _lowerCAmelCase												=							Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  )
      return image





@require_torch
class 			UpperCAmelCase					(      unittest.TestCase       ):




      @slow
      def     lowercase__   (      self      :			Optional[int]  )    ->				Tuple:
            # only resize + normalize
            _lowerCAmelCase												=							SegformerImageProcessor(
                image_scale=(5_12, 5_12)      ,      keep_ratio=__snake_case      ,      align=__snake_case      ,      do_random_crop=__snake_case  )
            _lowerCAmelCase												=							SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""  ).to(
                __snake_case  )

            _lowerCAmelCase												=							prepare_img()
            _lowerCAmelCase												=							image_processor(images=__snake_case      ,      return_tensors="""pt"""  )
            _lowerCAmelCase												=							encoded_inputs.pixel_values.to(__snake_case  )

            with torch.no_grad():
                  _lowerCAmelCase												=							model(__snake_case  )

            _lowerCAmelCase												=							torch.Size((1, model.config.num_labels, 1_28, 1_28)  )
            self.assertEqual(outputs.logits.shape      ,      __snake_case  )

            _lowerCAmelCase												=							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(__snake_case  )
            self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]      ,      __snake_case      ,      atol=1E-4  )  )




      @slow
      def     lowercase__   (      self      :			int  )    ->				List[Any]:
            # only resize + normalize
            _lowerCAmelCase												=							SegformerImageProcessor(
                image_scale=(5_12, 5_12)      ,      keep_ratio=__snake_case      ,      align=__snake_case      ,      do_random_crop=__snake_case  )
            _lowerCAmelCase												=							SegformerForSemanticSegmentation.from_pretrained(
                """nvidia/segformer-b1-finetuned-cityscapes-1024-1024"""  ).to(__snake_case  )

            _lowerCAmelCase												=							prepare_img()
            _lowerCAmelCase												=							image_processor(images=__snake_case      ,      return_tensors="""pt"""  )
            _lowerCAmelCase												=							encoded_inputs.pixel_values.to(__snake_case  )

            with torch.no_grad():
                  _lowerCAmelCase												=							model(__snake_case  )

            _lowerCAmelCase												=							torch.Size((1, model.config.num_labels, 1_28, 1_28)  )
            self.assertEqual(outputs.logits.shape      ,      __snake_case  )

            _lowerCAmelCase												=							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(__snake_case  )
            self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]      ,      __snake_case      ,      atol=1E-1  )  )




      @slow
      def     lowercase__   (      self      :			int  )    ->				Union[str, Any]:
            # only resize + normalize
            _lowerCAmelCase												=							SegformerImageProcessor(
                image_scale=(5_12, 5_12)      ,      keep_ratio=__snake_case      ,      align=__snake_case      ,      do_random_crop=__snake_case  )
            _lowerCAmelCase												=							SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""  ).to(
                __snake_case  )

            _lowerCAmelCase												=							prepare_img()
            _lowerCAmelCase												=							image_processor(images=__snake_case      ,      return_tensors="""pt"""  )
            _lowerCAmelCase												=							encoded_inputs.pixel_values.to(__snake_case  )

            with torch.no_grad():
                  _lowerCAmelCase												=							model(__snake_case  )

            _lowerCAmelCase												=							outputs.logits.detach().cpu()

            _lowerCAmelCase												=							image_processor.post_process_semantic_segmentation(outputs=__snake_case      ,      target_sizes=[(5_00, 3_00)]  )
            _lowerCAmelCase												=							torch.Size((5_00, 3_00)  )
            self.assertEqual(segmentation[0].shape      ,      __snake_case  )

            _lowerCAmelCase												=							image_processor.post_process_semantic_segmentation(outputs=__snake_case  )
            _lowerCAmelCase												=							torch.Size((1_28, 1_28)  )
            self.assertEqual(segmentation[0].shape      ,      __snake_case  )













'''simple docstring'''






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available


A__    :       int              ={'''tokenization_herbert''': ['''HerbertTokenizer''']}

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A__    :       Dict              =['''HerbertTokenizerFast''']


if TYPE_CHECKING:
      from .tokenization_herbert import HerbertTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_herbert_fast import HerbertTokenizerFast

else:
      import sys

      A__    :       List[Any]              =_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)






