




"""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_   (			UpperCAmelCase__					):

    '''simple docstring'''






    def 	SCREAMING_SNAKE_CASE							(      self				:     Any)						->List[Any]:





           '''simple docstring'''

           A__							=   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				:     Optional[int] ,			UpperCAmelCase__				:     Any ,			UpperCAmelCase__				:     str=13 ,			UpperCAmelCase__				:     List[str]=64 ,			UpperCAmelCase__				:     List[Any]=3 ,			UpperCAmelCase__				:     Any=4 ,			UpperCAmelCase__				:     List[str]=[2, 2, 2, 2] ,			UpperCAmelCase__				:     Tuple=[8, 4, 2, 1] ,			UpperCAmelCase__				:     List[str]=[16, 32, 64, 128] ,			UpperCAmelCase__				:     int=[1, 4, 8, 16] ,			UpperCAmelCase__				:     List[str]=[1, 2, 4, 8] ,			UpperCAmelCase__				:     Dict=True ,			UpperCAmelCase__				:     Any=True ,			UpperCAmelCase__				:     List[str]="gelu" ,			UpperCAmelCase__				:     Optional[int]=0.1 ,			UpperCAmelCase__				:     Union[str, Any]=0.1 ,			UpperCAmelCase__				:     List[Any]=0.02 ,			UpperCAmelCase__				:     str=3 ,			UpperCAmelCase__				:     int=None ,			)						->List[Any]:





           '''simple docstring'''

           A__							=   parent
           A__							=   batch_size
           A__							=   image_size
           A__							=   num_channels
           A__							=   num_encoder_blocks
           A__							=   sr_ratios
           A__							=   depths
           A__							=   hidden_sizes
           A__							=   downsampling_rates
           A__							=   num_attention_heads
           A__							=   is_training
           A__							=   use_labels
           A__							=   hidden_act
           A__							=   hidden_dropout_prob
           A__							=   attention_probs_dropout_prob
           A__							=   initializer_range
           A__							=   num_labels
           A__							=   scope


    def 	SCREAMING_SNAKE_CASE							(      self				:     List[Any])						->Tuple:





           '''simple docstring'''

           A__							=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

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


    def 	SCREAMING_SNAKE_CASE							(      self				:     Any)						->Optional[int]:





           '''simple docstring'''

           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 	SCREAMING_SNAKE_CASE							(      self				:     List[str] ,			UpperCAmelCase__				:     Union[str, Any] ,			UpperCAmelCase__				:     List[Any] ,			UpperCAmelCase__				:     Union[str, Any])						->int:





           '''simple docstring'''

           A__							=   SegformerModel(config=_A)
           model.to(_A)
           model.eval()
           A__							=   model(_A)
           A__							=   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 	SCREAMING_SNAKE_CASE							(      self				:     Tuple ,			UpperCAmelCase__				:     Dict ,			UpperCAmelCase__				:     Any ,			UpperCAmelCase__				:     int)						->str:





           '''simple docstring'''

           A__							=   self.num_labels
           A__							=   SegformerForSemanticSegmentation(_A)
           model.to(_A)
           model.eval()
           A__							=   model(_A)
           self.parent.assertEqual(
               result.logits.shape ,			(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4))
           A__							=   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 	SCREAMING_SNAKE_CASE							(      self				:     Optional[Any] ,			UpperCAmelCase__				:     Dict ,			UpperCAmelCase__				:     Optional[int] ,			UpperCAmelCase__				:     str)						->List[Any]:





           '''simple docstring'''

           A__							=   1
           A__							=   SegformerForSemanticSegmentation(config=_A)
           model.to(_A)
           model.eval()
           A__							=   torch.randint(0 ,			1 ,			(self.batch_size, self.image_size, self.image_size)).to(_A)
           A__							=   model(_A ,			labels=_A)
           self.parent.assertGreater(result.loss ,			0.0)





    def 	SCREAMING_SNAKE_CASE							(      self				:     Any)						->Optional[int]:





           '''simple docstring'''

           A__							=   self.prepare_config_and_inputs()
           A__							=   config_and_inputs
           A__							=   {'pixel_values': pixel_values}
           return config, inputs_dict


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

    '''simple docstring'''






    UpperCAmelCase__							  =				(
        (
            SegformerModel,
            SegformerForSemanticSegmentation,
            SegformerForImageClassification,
        )
        if is_torch_available()
        else ()
    )
    UpperCAmelCase__							  =				(
        {
            """feature-extraction""": SegformerModel,
            """image-classification""": SegformerForImageClassification,
            """image-segmentation""": SegformerForSemanticSegmentation,
        }
        if is_torch_available()
        else {}
    )

    UpperCAmelCase__							  =				True
    UpperCAmelCase__							  =				False
    UpperCAmelCase__							  =				False
    UpperCAmelCase__							  =				False


    def 	SCREAMING_SNAKE_CASE							(      self				:     str)						->Union[str, Any]:





           '''simple docstring'''

           A__							=   SegformerModelTester(self)
           A__							=   SegformerConfigTester(self ,			config_class=_A)


    def 	SCREAMING_SNAKE_CASE							(      self				:     List[str])						->Optional[int]:





           '''simple docstring'''

           self.config_tester.run_common_tests()


    def 	SCREAMING_SNAKE_CASE							(      self				:     List[Any])						->List[str]:





           '''simple docstring'''

           A__							=   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*_A)


    def 	SCREAMING_SNAKE_CASE							(      self				:     Optional[int])						->str:





           '''simple docstring'''

           A__							=   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_binary_image_segmentation(*_A)


    def 	SCREAMING_SNAKE_CASE							(      self				:     int)						->Union[str, Any]:





           '''simple docstring'''

           A__							=   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 	SCREAMING_SNAKE_CASE							(      self				:     Union[str, Any])						->str:





           '''simple docstring'''

           pass


    @unittest.skip('''SegFormer does not have get_input_embeddings method and get_output_embeddings methods''')
    def 	SCREAMING_SNAKE_CASE							(      self				:     int)						->str:





           '''simple docstring'''

           pass


    def 	SCREAMING_SNAKE_CASE							(      self				:     List[str])						->Any:





           '''simple docstring'''

           A__							=   self.model_tester.prepare_config_and_inputs_for_common()

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

                  A__							=   ['pixel_values']
                  self.assertListEqual(arg_names[:1] ,			_A)


    def 	SCREAMING_SNAKE_CASE							(      self				:     List[str])						->List[Any]:





           '''simple docstring'''

           A__							=   self.model_tester.prepare_config_and_inputs_for_common()
           A__							=   True

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

                  A__							=   sum(self.model_tester.depths)
                  self.assertEqual(len(_A) ,			_A)

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

                  self.assertEqual(len(_A) ,			_A)

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

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

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

                  A__							=   outputs.attentions

                  self.assertEqual(len(_A) ,			_A)
                  # verify the first attentions (first block, first layer)
                  A__							=   (self.model_tester.image_size // 4) ** 2
                  A__							=   (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 	SCREAMING_SNAKE_CASE							(      self				:     str)						->List[str]:





           '''simple docstring'''

           def check_hidden_states_output(UpperCAmelCase__				:     Union[str, Any] ,			UpperCAmelCase__				:     List[str] ,			UpperCAmelCase__				:     Tuple):
                  A__							=   model_class(_A)
                  model.to(_A)
                  model.eval()

                  with torch.no_grad():
                         A__							=   model(**self._prepare_for_class(_A ,			_A))

                  A__							=   outputs.hidden_states

                  A__							=   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,
                      ] ,			)

           A__							=   self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                  A__							=   True
                  check_hidden_states_output(_A ,			_A ,			_A)

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

                  check_hidden_states_output(_A ,			_A ,			_A)


    def 	SCREAMING_SNAKE_CASE							(      self				:     Optional[int])						->int:





           '''simple docstring'''

           if not self.model_tester.is_training:
                  return

           A__							=   self.model_tester.prepare_config_and_inputs_for_common()
           A__							=   True

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

                  A__							=   model_class(_A)
                  model.to(_A)
                  model.train()
                  A__							=   self._prepare_for_class(_A ,			_A ,			return_labels=_A)
                  A__							=   model(**_A).loss
                  loss.backward()


    @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''')
    def 	SCREAMING_SNAKE_CASE							(      self				:     Tuple)						->Dict:





           '''simple docstring'''

           pass





    @slow
    def 	SCREAMING_SNAKE_CASE							(      self				:     Any)						->List[str]:





           '''simple docstring'''

           for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  A__							=   SegformerModel.from_pretrained(_A)
                  self.assertIsNotNone(_A)







def SCREAMING_SNAKE_CASE    (	)       ->	Union[str, Any]:



       """simple docstring"""

       A__							=   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
       return image


@require_torch
class     UpperCamelCase_   (			unittest.TestCase					):

    '''simple docstring'''






    @slow
    def 	SCREAMING_SNAKE_CASE							(      self				:     Union[str, Any])						->Any:





           '''simple docstring'''

           A__							=   SegformerImageProcessor(
               image_scale=(512, 512) ,			keep_ratio=_A ,			align=_A ,			do_random_crop=_A)
           A__							=   SegformerForSemanticSegmentation.from_pretrained('''nvidia/segformer-b0-finetuned-ade-512-512''').to(
               _A)

           A__							=   prepare_img()
           A__							=   image_processor(images=_A ,			return_tensors='''pt''')
           A__							=   encoded_inputs.pixel_values.to(_A)

           with torch.no_grad():
                  A__							=   model(_A)

           A__							=   torch.Size((1, model.config.num_labels, 128, 128))
           self.assertEqual(outputs.logits.shape ,			_A)

           A__							=   torch.tensor(
               [
                   [[-4.6310, -5.5232, -6.2356], [-5.1921, -6.1444, -6.5996], [-5.4424, -6.2790, -6.7574]],
                   [[-12.1391, -13.3122, -13.9554], [-12.8732, -13.9352, -14.3563], [-12.9438, -13.8226, -14.2513]],
                   [[-12.5134, -13.4686, -14.4915], [-12.8669, -14.4343, -14.7758], [-13.2523, -14.5819, -15.0694]],
               ]).to(_A)
           self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] ,			_A ,			atol=1e-4))


    @slow
    def 	SCREAMING_SNAKE_CASE							(      self				:     Any)						->Optional[int]:





           '''simple docstring'''

           A__							=   SegformerImageProcessor(
               image_scale=(512, 512) ,			keep_ratio=_A ,			align=_A ,			do_random_crop=_A)
           A__							=   SegformerForSemanticSegmentation.from_pretrained(
               '''nvidia/segformer-b1-finetuned-cityscapes-1024-1024''').to(_A)

           A__							=   prepare_img()
           A__							=   image_processor(images=_A ,			return_tensors='''pt''')
           A__							=   encoded_inputs.pixel_values.to(_A)

           with torch.no_grad():
                  A__							=   model(_A)

           A__							=   torch.Size((1, model.config.num_labels, 128, 128))
           self.assertEqual(outputs.logits.shape ,			_A)

           A__							=   torch.tensor(
               [
                   [[-13.5748, -13.9111, -12.6500], [-14.3500, -15.3683, -14.2328], [-14.7532, -16.0424, -15.6087]],
                   [[-17.1651, -15.8725, -12.9653], [-17.2580, -17.3718, -14.8223], [-16.6058, -16.8783, -16.7452]],
                   [[-3.6456, -3.0209, -1.4203], [-3.0797, -3.1959, -2.0000], [-1.8757, -1.9217, -1.6997]],
               ]).to(_A)
           self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] ,			_A ,			atol=1e-1))





    @slow
    def 	SCREAMING_SNAKE_CASE							(      self				:     Optional[int])						->Union[str, Any]:





           '''simple docstring'''

           A__							=   SegformerImageProcessor(
               image_scale=(512, 512) ,			keep_ratio=_A ,			align=_A ,			do_random_crop=_A)
           A__							=   SegformerForSemanticSegmentation.from_pretrained('''nvidia/segformer-b0-finetuned-ade-512-512''').to(
               _A)

           A__							=   prepare_img()
           A__							=   image_processor(images=_A ,			return_tensors='''pt''')
           A__							=   encoded_inputs.pixel_values.to(_A)

           with torch.no_grad():
                  A__							=   model(_A)

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

           A__							=   image_processor.post_process_semantic_segmentation(outputs=_A ,			target_sizes=[(500, 300)])
           A__							=   torch.Size((500, 300))
           self.assertEqual(segmentation[0].shape ,			_A)

           A__							=   image_processor.post_process_semantic_segmentation(outputs=_A)
           A__							=   torch.Size((128, 128))
           self.assertEqual(segmentation[0].shape ,			_A)








from __future__ import annotations

from collections import Counter
from random import random


class     UpperCamelCase_   :

    '''simple docstring'''






    def __init__(      self				:     Any)						->Optional[Any]:





           '''simple docstring'''

           A__							=   {}


    def 	SCREAMING_SNAKE_CASE							(      self				:     Optional[int] ,			UpperCAmelCase__				:     str)						->None:





           '''simple docstring'''

           A__							=   {}


    def 	SCREAMING_SNAKE_CASE							(      self				:     List[str] ,			UpperCAmelCase__				:     str ,			UpperCAmelCase__				:     str ,			UpperCAmelCase__				:     float)						->None:





           '''simple docstring'''

           if nodea not in self.connections:
                  self.add_node(UpperCAmelCase__)
           if nodea not in self.connections:
                  self.add_node(UpperCAmelCase__)
           A__							=   probability


    def 	SCREAMING_SNAKE_CASE							(      self				:     Optional[int])						->list[str]:





           '''simple docstring'''

           return list(self.connections)





    def 	SCREAMING_SNAKE_CASE							(      self				:     Any ,			UpperCAmelCase__				:     str)						->str:





           '''simple docstring'''

           A__							=   0
           A__							=   random()

           for dest in self.connections[node]:
                  current_probability += self.connections[node][dest]
                  if current_probability > random_value:
                         return dest
           return ""







def SCREAMING_SNAKE_CASE    (	lowercase_			,    lowercase_			,    lowercase_						)       ->	dict[str, int]:



       """simple docstring"""

       A__							=   MarkovChainGraphUndirectedUnweighted()

       for nodea, nodea, probability in transitions:
              graph.add_transition_probability(lowercase_			,    lowercase_			,    lowercase_						)

       A__							=   Counter(graph.get_nodes()						)
       A__							=   start

       for _ in range(lowercase_						):
              A__							=   graph.transition(lowercase_						)
              visited[node] += 1

       return visited


if __name__ == "__main__":
  import doctest

  doctest.testmod()


