
'''simple docstring'''

from __future__ import annotations

import unittest

from transformers import BlenderbotSmallConfig, BlenderbotSmallTokenizer, is_tf_available
from transformers.testing_utils import require_tf, require_tokenizers, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
		import tensorflow as tf

		from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel






@require_tf
class  A  :
	snake_case__   :str   =  BlenderbotSmallConfig
	snake_case__   :Dict   =  {}
	snake_case__   :Union[str, Any]   =  'gelu'





	def __init__(       self      :							Any   ,				__magic_name__      :							int   ,				__magic_name__      :							int=13   ,				__magic_name__      :							int=7   ,				__magic_name__      :							Any=True   ,				__magic_name__      :							Tuple=False   ,				__magic_name__      :							Union[str, Any]=99   ,				__magic_name__      :							Union[str, Any]=32   ,				__magic_name__      :							Tuple=2   ,				__magic_name__      :							List[str]=4   ,				__magic_name__      :							List[Any]=37   ,				__magic_name__      :							Optional[Any]=0.1   ,				__magic_name__      :							Optional[Any]=0.1   ,				__magic_name__      :							Union[str, Any]=20   ,				__magic_name__      :							Tuple=2   ,				__magic_name__      :							List[str]=1   ,				__magic_name__      :							Union[str, Any]=0   ,				):


				"""simple docstring"""

				lowerCAmelCase__										=				parent
				lowerCAmelCase__										=				batch_size
				lowerCAmelCase__										=				seq_length
				lowerCAmelCase__										=				is_training
				lowerCAmelCase__										=				use_labels
				lowerCAmelCase__										=				vocab_size
				lowerCAmelCase__										=				hidden_size
				lowerCAmelCase__										=				num_hidden_layers
				lowerCAmelCase__										=				num_attention_heads
				lowerCAmelCase__										=				intermediate_size

				lowerCAmelCase__										=				hidden_dropout_prob
				lowerCAmelCase__										=				attention_probs_dropout_prob
				lowerCAmelCase__										=				max_position_embeddings
				lowerCAmelCase__										=				eos_token_id
				lowerCAmelCase__										=				pad_token_id
				lowerCAmelCase__										=				bos_token_id





	def 				__SCREAMING_SNAKE_CASE	(       self      :							List[Any]				):


				"""simple docstring"""

				lowerCAmelCase__										=				ids_tensor([self.batch_size, self.seq_length - 1]   ,				self.vocab_size				)
				lowerCAmelCase__										=				tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size				)   ,				1				)
				lowerCAmelCase__										=				tf.concat([input_ids, eos_tensor]   ,				axis=1				)

				lowerCAmelCase__										=				ids_tensor([self.batch_size, self.seq_length]   ,				self.vocab_size				)

				lowerCAmelCase__										=				self.config_cls(
				    vocab_size=self.vocab_size   ,				d_model=self.hidden_size   ,				encoder_layers=self.num_hidden_layers   ,				decoder_layers=self.num_hidden_layers   ,				encoder_attention_heads=self.num_attention_heads   ,				decoder_attention_heads=self.num_attention_heads   ,				encoder_ffn_dim=self.intermediate_size   ,				decoder_ffn_dim=self.intermediate_size   ,				dropout=self.hidden_dropout_prob   ,				attention_dropout=self.attention_probs_dropout_prob   ,				max_position_embeddings=self.max_position_embeddings   ,				eos_token_ids=[2]   ,				bos_token_id=self.bos_token_id   ,				pad_token_id=self.pad_token_id   ,				decoder_start_token_id=self.pad_token_id   ,				**self.config_updates   ,				)
				lowerCAmelCase__										=				prepare_blenderbot_small_inputs_dict(__magic_name__   ,				__magic_name__   ,				__magic_name__				)
				return config, inputs_dict







	def 				__SCREAMING_SNAKE_CASE	(       self      :							Optional[int]   ,				__magic_name__      :							Dict   ,				__magic_name__      :							str				):


				"""simple docstring"""

				lowerCAmelCase__										=				TFBlenderbotSmallModel(config=__magic_name__				).get_decoder()
				lowerCAmelCase__										=				inputs_dict["input_ids"]

				lowerCAmelCase__										=				input_ids[:1, :]
				lowerCAmelCase__										=				inputs_dict["attention_mask"][:1, :]
				lowerCAmelCase__										=				inputs_dict["head_mask"]
				lowerCAmelCase__										=				1

				# first forward pass
				lowerCAmelCase__										=				model(__magic_name__   ,				attention_mask=__magic_name__   ,				head_mask=__magic_name__   ,				use_cache=__magic_name__				)

				lowerCAmelCase__						,lowerCAmelCase__										=				outputs.to_tuple()

				# create hypothetical next token and extent to next_input_ids
				lowerCAmelCase__										=				ids_tensor((self.batch_size, 3)   ,				config.vocab_size				)
				lowerCAmelCase__										=				tf.cast(ids_tensor((self.batch_size, 3)   ,				2				)   ,				tf.inta				)

				# append to next input_ids and
				lowerCAmelCase__										=				tf.concat([input_ids, next_tokens]   ,				axis=-1				)
				lowerCAmelCase__										=				tf.concat([attention_mask, next_attn_mask]   ,				axis=-1				)

				lowerCAmelCase__										=				model(__magic_name__   ,				attention_mask=__magic_name__				)[0]
				lowerCAmelCase__										=				model(__magic_name__   ,				attention_mask=__magic_name__   ,				past_key_values=__magic_name__				)[0]

				self.parent.assertEqual(next_tokens.shape[1]   ,				output_from_past.shape[1]				)

				# select random slice
				lowerCAmelCase__										=				int(ids_tensor((1,)   ,				output_from_past.shape[-1]				)				)
				lowerCAmelCase__										=				output_from_no_past[:, -3:, random_slice_idx]
				lowerCAmelCase__										=				output_from_past[:, :, random_slice_idx]

				# test that outputs are equal for slice
				tf.debugging.assert_near(__magic_name__   ,				__magic_name__   ,				rtol=1E-3				)







def 	A  (   UpperCamelCase_       :  Optional[Any]    ,						UpperCamelCase_       :  Union[str, Any]    ,						UpperCamelCase_       :  List[str]    ,						UpperCamelCase_       :  str=None    ,						UpperCamelCase_       :  List[str]=None    ,						UpperCamelCase_       :  Dict=None    ,						UpperCamelCase_       :  List[str]=None    ,						UpperCamelCase_       :  List[str]=None    ,						)       ->							List[Any]:







			'''simple docstring'''



			if attention_mask is None:
						lowerCAmelCase__										=				tf.cast(tf.math.not_equal(UpperCamelCase_    ,						config.pad_token_id   )    ,						tf.inta   )
			if decoder_attention_mask is None:
						lowerCAmelCase__										=				tf.concat(
						    [
						        tf.ones(decoder_input_ids[:, :1].shape    ,						dtype=tf.inta   ),
						        tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]    ,						config.pad_token_id   )    ,						tf.inta   ),
						    ]    ,						axis=-1    ,						)
			if head_mask is None:
						lowerCAmelCase__										=				tf.ones((config.encoder_layers, config.encoder_attention_heads)   )
			if decoder_head_mask is None:
						lowerCAmelCase__										=				tf.ones((config.decoder_layers, config.decoder_attention_heads)   )
			if cross_attn_head_mask is None:
						lowerCAmelCase__										=				tf.ones((config.decoder_layers, config.decoder_attention_heads)   )
			return {
			    "input_ids": input_ids,
			    "decoder_input_ids": decoder_input_ids,
			    "attention_mask": attention_mask,
			    "decoder_attention_mask": decoder_attention_mask,
			    "head_mask": head_mask,
			    "decoder_head_mask": decoder_head_mask,
			    "cross_attn_head_mask": cross_attn_head_mask,
			}






@require_tf
class  A  (						SCREAMING_SNAKE_CASE__	,   SCREAMING_SNAKE_CASE__	,   unittest.TestCase     ):
	snake_case__   :List[Any]   =  (
	    (TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel) if is_tf_available() else ()
	)
	snake_case__   :Dict   =  (TFBlenderbotSmallForConditionalGeneration,) if is_tf_available() else ()
	snake_case__   :int   =  (
	    {
	        'conversational': TFBlenderbotSmallForConditionalGeneration,
	        'feature-extraction': TFBlenderbotSmallModel,
	        'summarization': TFBlenderbotSmallForConditionalGeneration,
	        'text2text-generation': TFBlenderbotSmallForConditionalGeneration,
	        'translation': TFBlenderbotSmallForConditionalGeneration,
	    }
	    if is_tf_available()
	    else {}
	)
	snake_case__   :Optional[Any]   =  True
	snake_case__   :Any   =  False
	snake_case__   :int   =  False





	def 				__SCREAMING_SNAKE_CASE	(       self      :							Dict				):


				"""simple docstring"""

				lowerCAmelCase__										=				TFBlenderbotSmallModelTester(self				)
				lowerCAmelCase__										=				ConfigTester(self   ,				config_class=__magic_name__				)





	def 				__SCREAMING_SNAKE_CASE	(       self      :							Tuple				):


				"""simple docstring"""

				self.config_tester.run_common_tests()







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


				"""simple docstring"""

				lowerCAmelCase__										=				self.model_tester.prepare_config_and_inputs_for_common()
				self.model_tester.check_decoder_model_past_large_inputs(*__magic_name__				)






@require_tokenizers
@require_tf
class  A  (						unittest.TestCase     ):
	snake_case__   :Any   =  [
	    'Social anxiety\nWow, I am never shy. Do you have anxiety?\nYes. I end up sweating and blushing and feel like '
	    '  i\'m going to throw up.\nand why is that?'
	]
	snake_case__   :Dict   =  'facebook/blenderbot_small-90M'





	@cached_property
	def 				__SCREAMING_SNAKE_CASE	(       self      :							List[Any]				):


				"""simple docstring"""

				return BlenderbotSmallTokenizer.from_pretrained("facebook/blenderbot-90M"				)





	@cached_property
	def 				__SCREAMING_SNAKE_CASE	(       self      :							List[str]				):


				"""simple docstring"""

				lowerCAmelCase__										=				TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name				)
				return model







	@slow
	def 				__SCREAMING_SNAKE_CASE	(       self      :							Tuple				):


				"""simple docstring"""

				lowerCAmelCase__										=				self.tokenizer(self.src_text   ,				return_tensors="tf"				)
				lowerCAmelCase__										=				self.model.generate(
				    model_inputs.input_ids   ,				attention_mask=model_inputs.attention_mask   ,				num_beams=2   ,				use_cache=__magic_name__   ,				)
				lowerCAmelCase__										=				self.tokenizer.batch_decode(generated_ids.numpy()   ,				skip_special_tokens=__magic_name__				)[0]
				assert generated_words in (
				    "i don't know. i just feel like i'm going to throw up. it's not fun.",
				    "i'm not sure. i just feel like i've been feeling like i have to be in a certain place",
				    "i'm not sure. i just feel like i've been in a bad situation.",
				)








'''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        A__   (   _snake_case      ):



    def        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           A_						       =   self.config_class(**self.inputs_dict							)
           self.parent.assertTrue(hasattr(UpperCamelCase__		,					"""hidden_sizes"""							)							)
           self.parent.assertTrue(hasattr(UpperCamelCase__		,					"""num_attention_heads"""							)							)
           self.parent.assertTrue(hasattr(UpperCamelCase__		,					"""num_encoder_blocks"""							)							)


class        A__   :



    def __init__(       self		,					UpperCamelCase__		,					UpperCamelCase__=13		,					UpperCamelCase__=64		,					UpperCamelCase__=3		,					UpperCamelCase__=4		,					UpperCamelCase__=[2, 2, 2, 2]		,					UpperCamelCase__=[8, 4, 2, 1]		,					UpperCamelCase__=[16, 32, 64, 128]		,					UpperCamelCase__=[1, 4, 8, 16]		,					UpperCamelCase__=[1, 2, 4, 8]		,					UpperCamelCase__=True		,					UpperCamelCase__=True		,					UpperCamelCase__="gelu"		,					UpperCamelCase__=0.1		,					UpperCamelCase__=0.1		,					UpperCamelCase__=0.02		,					UpperCamelCase__=3		,					UpperCamelCase__=None		,					)    ->   Tuple:





           '''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        snake_case_      (       self							)    ->   Any:





           '''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        snake_case_      (       self							)    ->   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        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   Optional[Any]:





           '''simple docstring'''
           A_						       =   SegformerModel(config=UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   model(UpperCamelCase__							)
           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        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   Any:





           '''simple docstring'''
           A_						       =   self.num_labels
           A_						       =   SegformerForSemanticSegmentation(UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   model(UpperCamelCase__							)
           self.parent.assertEqual(
               result.logits.shape		,					(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)							)
           A_						       =   model(UpperCamelCase__		,					labels=UpperCamelCase__							)
           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        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   Dict:





           '''simple docstring'''
           A_						       =   1
           A_						       =   SegformerForSemanticSegmentation(config=UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   torch.randint(0		,					1		,					(self.batch_size, self.image_size, self.image_size)							).to(UpperCamelCase__							)
           A_						       =   model(UpperCamelCase__		,					labels=UpperCamelCase__							)
           self.parent.assertGreater(result.loss		,					0.0							)







    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           A_						       =   self.prepare_config_and_inputs()
           A_       ,      A_       ,      A_						       =   config_and_inputs
           A_						       =   {"""pixel_values""": pixel_values}
           return config, inputs_dict


@require_torch
class        A__   (   _snake_case    ,    _snake_case    ,    unittest.TestCase      ):
    lowercase						       =		(
        (
            SegformerModel,
            SegformerForSemanticSegmentation,
            SegformerForImageClassification,
        )
        if is_torch_available()
        else ()
    )
    lowercase						       =		(
        {
            "feature-extraction": SegformerModel,
            "image-classification": SegformerForImageClassification,
            "image-segmentation": SegformerForSemanticSegmentation,
        }
        if is_torch_available()
        else {}
    )

    lowercase						       =		True
    lowercase						       =		False
    lowercase						       =		False
    lowercase						       =		False



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





           '''simple docstring'''
           A_						       =   SegformerModelTester(self							)
           A_						       =   SegformerConfigTester(self		,					config_class=UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           self.config_tester.run_common_tests()



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





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_binary_image_segmentation(*UpperCamelCase__							)



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





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_segmentation(*UpperCamelCase__							)



    @unittest.skip("""SegFormer does not use inputs_embeds"""							)
    def        snake_case_      (       self							)    ->   List[Any]:





           '''simple docstring'''
           pass



    @unittest.skip("""SegFormer does not have get_input_embeddings method and get_output_embeddings methods"""							)
    def        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           pass



    def        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           A_       ,      A_						       =   self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                  A_						       =   model_class(UpperCamelCase__							)
                  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]		,					UpperCamelCase__							)



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





           '''simple docstring'''
           A_       ,      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(UpperCamelCase__							)
                  model.to(UpperCamelCase__							)
                  model.eval()
                  with torch.no_grad():
                         A_						       =   model(**self._prepare_for_class(UpperCamelCase__		,					UpperCamelCase__							)							)
                  A_						       =   outputs.attentions

                  A_						       =   sum(self.model_tester.depths							)
                  self.assertEqual(len(UpperCamelCase__							)		,					UpperCamelCase__							)

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

                  self.assertEqual(len(UpperCamelCase__							)		,					UpperCamelCase__							)

                  # 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(UpperCamelCase__							)

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

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

                  A_						       =   outputs.attentions

                  self.assertEqual(len(UpperCamelCase__							)		,					UpperCamelCase__							)
                  # 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        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           def check_hidden_states_output(UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							):
                  A_						       =   model_class(UpperCamelCase__							)
                  model.to(UpperCamelCase__							)
                  model.eval()

                  with torch.no_grad():
                         A_						       =   model(**self._prepare_for_class(UpperCamelCase__		,					UpperCamelCase__							)							)

                  A_						       =   outputs.hidden_states

                  A_						       =   self.model_tester.num_encoder_blocks
                  self.assertEqual(len(UpperCamelCase__							)		,					UpperCamelCase__							)

                  # 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_       ,      A_						       =   self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                  A_						       =   True
                  check_hidden_states_output(UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)

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

                  check_hidden_states_output(UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)



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





           '''simple docstring'''
           if not self.model_tester.is_training:
                  return

           A_       ,      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(UpperCamelCase__							):
                         continue

                  A_						       =   model_class(UpperCamelCase__							)
                  model.to(UpperCamelCase__							)
                  model.train()
                  A_						       =   self._prepare_for_class(UpperCamelCase__		,					UpperCamelCase__		,					return_labels=UpperCamelCase__							)
                  A_						       =   model(**UpperCamelCase__							).loss
                  loss.backward()



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





           '''simple docstring'''
           pass







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





           '''simple docstring'''
           for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  A_						       =   SegformerModel.from_pretrained(UpperCamelCase__							)
                  self.assertIsNotNone(UpperCamelCase__							)

def    UpperCAmelCase__     (   )      ->      Any:
       A_						       =   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
       return image







@require_torch
class        A__   (   unittest.TestCase      ):



    @slow
    def        snake_case_      (       self							)    ->   Any:





           '''simple docstring'''
           # only resize + normalize
           A_						       =   SegformerImageProcessor(
               image_scale=(512, 512)		,					keep_ratio=UpperCamelCase__		,					align=UpperCamelCase__		,					do_random_crop=UpperCamelCase__							)
           A_						       =   SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""							).to(
               UpperCamelCase__							)

           A_						       =   prepare_img()
           A_						       =   image_processor(images=UpperCamelCase__		,					return_tensors="""pt"""							)
           A_						       =   encoded_inputs.pixel_values.to(UpperCamelCase__							)

           with torch.no_grad():
                  A_						       =   model(UpperCamelCase__							)

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

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



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





           '''simple docstring'''
           # only resize + normalize
           A_						       =   SegformerImageProcessor(
               image_scale=(512, 512)		,					keep_ratio=UpperCamelCase__		,					align=UpperCamelCase__		,					do_random_crop=UpperCamelCase__							)
           A_						       =   SegformerForSemanticSegmentation.from_pretrained(
               """nvidia/segformer-b1-finetuned-cityscapes-1024-1024"""							).to(UpperCamelCase__							)

           A_						       =   prepare_img()
           A_						       =   image_processor(images=UpperCamelCase__		,					return_tensors="""pt"""							)
           A_						       =   encoded_inputs.pixel_values.to(UpperCamelCase__							)

           with torch.no_grad():
                  A_						       =   model(UpperCamelCase__							)

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

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







    @slow
    def        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           # only resize + normalize
           A_						       =   SegformerImageProcessor(
               image_scale=(512, 512)		,					keep_ratio=UpperCamelCase__		,					align=UpperCamelCase__		,					do_random_crop=UpperCamelCase__							)
           A_						       =   SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""							).to(
               UpperCamelCase__							)

           A_						       =   prepare_img()
           A_						       =   image_processor(images=UpperCamelCase__		,					return_tensors="""pt"""							)
           A_						       =   encoded_inputs.pixel_values.to(UpperCamelCase__							)

           with torch.no_grad():
                  A_						       =   model(UpperCamelCase__							)

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

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

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