"""simple docstring"""







from __future__ import annotations

import unittest

from transformers import LEDConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

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 TFLEDForConditionalGeneration, TFLEDModel

@require_tf
class 		UpperCamelCase      :
				UpperCAmelCase    :				str   =		LEDConfig
				UpperCAmelCase    :				List[Any]   =		{}
				UpperCAmelCase    :				List[str]   =		"""gelu"""



				def __init__(self    :				List[str]     ,  _A    :				Any     ,  _A    :				List[str]=13     ,  _A    :				Dict=7     ,  _A    :				Optional[int]=True     ,  _A    :				Any=False     ,  _A    :				Dict=99     ,  _A    :				Optional[Any]=32     ,  _A    :				Union[str, Any]=2     ,  _A    :				Dict=4     ,  _A    :				str=37     ,  _A    :				List[Any]=0.1     ,  _A    :				str=0.1     ,  _A    :				List[str]=20     ,  _A    :				Union[str, Any]=2     ,  _A    :				Union[str, Any]=1     ,  _A    :				str=0     ,  _A    :				Optional[Any]=4     ,  )   ->     Any:
					__snake_case      :							Optional[int]						=			parent
					__snake_case      :							int						=			batch_size
					__snake_case      :							Any						=			seq_length
					__snake_case      :							Optional[int]						=			is_training
					__snake_case      :							Union[str, Any]						=			use_labels
					__snake_case      :							List[str]						=			vocab_size
					__snake_case      :							Union[str, Any]						=			hidden_size
					__snake_case      :							List[Any]						=			num_hidden_layers
					__snake_case      :							List[str]						=			num_attention_heads
					__snake_case      :							Any						=			intermediate_size

					__snake_case      :							Tuple						=			hidden_dropout_prob
					__snake_case      :							str						=			attention_probs_dropout_prob
					__snake_case      :							str						=			max_position_embeddings
					__snake_case      :							List[Any]						=			eos_token_id
					__snake_case      :							Optional[Any]						=			pad_token_id
					__snake_case      :							List[str]						=			bos_token_id
					__snake_case      :							Tuple						=			attention_window

					# `ModelTesterMixin.test_attention_outputs` is expecting attention tensors to be of size
					# [num_attention_heads, encoder_seq_length, encoder_key_length], but TFLongformerSelfAttention
					# returns attention of shape [num_attention_heads, encoder_seq_length, self.attention_window + 1]
					# because its local attention only attends to `self.attention_window` and one before and one after
					__snake_case      :							List[Any]						=			self.attention_window + 2

					# because of padding `encoder_seq_length`, is different from `seq_length`. Relevant for
					# the `test_attention_outputs` and `test_hidden_states_output` tests
					__snake_case      :							List[Any]						=			(
					    self.seq_length + (self.attention_window - self.seq_length % self.attention_window) % self.attention_window
					)



				def  _lowercase					(self    :				List[str])   ->     List[str]:
					__snake_case      :							Optional[Any]						=			ids_tensor([self.batch_size, self.seq_length - 1]     ,  self.vocab_size)
					__snake_case      :							Dict						=			tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size)     ,  1)
					__snake_case      :							List[str]						=			tf.concat([input_ids, eos_tensor]     ,  axis=1)

					__snake_case      :							Tuple						=			ids_tensor([self.batch_size, self.seq_length]     ,  self.vocab_size)

					__snake_case      :							Optional[int]						=			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     ,  attention_window=self.attention_window     ,  **self.config_updates     ,  )
					__snake_case      :							Optional[Any]						=			prepare_led_inputs_dict(_A     ,  _A     ,  _A)
					__snake_case      :							str						=			tf.concat(
					    [tf.zeros_like(_A)[:, :-1], tf.ones_like(_A)[:, -1:]]     ,  axis=-1     ,  )
					__snake_case      :							List[Any]						=			global_attention_mask
					return config, inputs_dict






				def  _lowercase					(self    :				Tuple     ,  _A    :				str     ,  _A    :				Union[str, Any])   ->     List[Any]:
					__snake_case      :							int						=			TFLEDModel(config=_A).get_decoder()
					__snake_case      :							Optional[int]						=			inputs_dict['input_ids']

					__snake_case      :							int						=			input_ids[:1, :]
					__snake_case      :							Dict						=			inputs_dict['attention_mask'][:1, :]
					__snake_case      :							Union[str, Any]						=			1

					# first forward pass
					__snake_case      :							Union[str, Any]						=			model(_A     ,  attention_mask=_A     ,  use_cache=_A)

					__snake_case		,    __snake_case      :							List[Any]						=			outputs.to_tuple()

					# create hypothetical next token and extent to next_input_ids
					__snake_case      :							Optional[Any]						=			ids_tensor((self.batch_size, 3)     ,  config.vocab_size)
					__snake_case      :							Tuple						=			tf.cast(ids_tensor((self.batch_size, 3)     ,  2)     ,  tf.inta)

					# append to next input_ids and
					__snake_case      :							List[Any]						=			tf.concat([input_ids, next_tokens]     ,  axis=-1)
					__snake_case      :							List[str]						=			tf.concat([attention_mask, next_attn_mask]     ,  axis=-1)

					__snake_case      :							int						=			model(_A     ,  attention_mask=_A)[0]
					__snake_case      :							Optional[int]						=			model(_A     ,  attention_mask=_A     ,  past_key_values=_A)[0]

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

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

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







def   __UpperCAmelCase    (   UpperCAmelCase_   :    List[Any]      ,							UpperCAmelCase_   :    str      ,							UpperCAmelCase_   :    List[str]      ,							UpperCAmelCase_   :    List[Any]=None      ,							UpperCAmelCase_   :    Any=None      ,							UpperCAmelCase_   :    Optional[int]=None      ,							UpperCAmelCase_   :    List[Any]=None      ,							)							->      Tuple:

	'''simple docstring'''





	if attention_mask is None:
		__snake_case      :							Dict						=			tf.cast(tf.math.not_equal(UpperCAmelCase_      ,							config.pad_token_id  )      ,							tf.inta  )
	if decoder_attention_mask is None:
		__snake_case      :							List[Any]						=			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:
		__snake_case      :							List[Any]						=			tf.ones((config.encoder_layers, config.encoder_attention_heads)  )
	if decoder_head_mask is None:
		__snake_case      :							str						=			tf.ones((config.decoder_layers, config.decoder_attention_heads)  )
	return {
	    "input_ids": input_ids,
	    "attention_mask": attention_mask,
	    "decoder_input_ids": decoder_input_ids,
	    "decoder_attention_mask": decoder_attention_mask,
	    "head_mask": head_mask,
	    "decoder_head_mask": decoder_head_mask,
	}

@require_tf
class 		UpperCamelCase      (   lowercase				,     lowercase				,     unittest.TestCase						):
				UpperCAmelCase    :				Optional[Any]   =		(TFLEDForConditionalGeneration, TFLEDModel) if is_tf_available() else ()
				UpperCAmelCase    :				List[Any]   =		(TFLEDForConditionalGeneration,) if is_tf_available() else ()
				UpperCAmelCase    :				Tuple   =		(
				    {
				        """conversational""": TFLEDForConditionalGeneration,
				        """feature-extraction""": TFLEDModel,
				        """summarization""": TFLEDForConditionalGeneration,
				        """text2text-generation""": TFLEDForConditionalGeneration,
				        """translation""": TFLEDForConditionalGeneration,
				    }
				    if is_tf_available()
				    else {}
				)
				UpperCAmelCase    :				Union[str, Any]   =		True
				UpperCAmelCase    :				int   =		False
				UpperCAmelCase    :				Dict   =		False
				UpperCAmelCase    :				Any   =		False



				def  _lowercase					(self    :				Optional[Any])   ->     Any:
					__snake_case      :							Tuple						=			TFLEDModelTester(self)
					__snake_case      :							Dict						=			ConfigTester(self     ,  config_class=_A)



				def  _lowercase					(self    :				Dict)   ->     Optional[Any]:
					self.config_tester.run_common_tests()



				def  _lowercase					(self    :				Dict)   ->     Tuple:
					__snake_case      :							Dict						=			self.model_tester.prepare_config_and_inputs_for_common()
					self.model_tester.check_decoder_model_past_large_inputs(*_A)



				def  _lowercase					(self    :				Dict)   ->     Tuple:
					__snake_case		,    __snake_case      :							List[str]						=			self.model_tester.prepare_config_and_inputs_for_common()
					__snake_case      :							Optional[int]						=			tf.zeros_like(inputs_dict['attention_mask'])
					__snake_case      :							List[Any]						=			2
					__snake_case      :							Any						=			tf.where(
					    tf.range(self.model_tester.seq_length)[None, :] < num_global_attn_indices     ,  1     ,  inputs_dict['global_attention_mask']     ,  )

					__snake_case      :							List[Any]						=			True
					__snake_case      :							List[Any]						=			self.model_tester.seq_length
					__snake_case      :							Tuple						=			self.model_tester.encoder_seq_length

					def check_decoder_attentions_output(_A    :				Any):
						__snake_case      :							int						=			outputs.decoder_attentions
						self.assertEqual(len(_A)     ,  self.model_tester.num_hidden_layers)
						self.assertListEqual(
						    list(decoder_attentions[0].shape[-3:])     ,  [self.model_tester.num_attention_heads, seq_length, seq_length]     ,  )

					def check_encoder_attentions_output(_A    :				str):
						__snake_case      :							Optional[Any]						=			[t.numpy() for t in outputs.encoder_attentions]
						__snake_case      :							Tuple						=			[t.numpy() for t in outputs.encoder_global_attentions]
						self.assertEqual(len(_A)     ,  self.model_tester.num_hidden_layers)
						self.assertEqual(len(_A)     ,  self.model_tester.num_hidden_layers)
						self.assertListEqual(
						    list(attentions[0].shape[-3:])     ,  [self.model_tester.num_attention_heads, seq_length, seq_length]     ,  )
						self.assertListEqual(
						    list(global_attentions[0].shape[-3:])     ,  [self.model_tester.num_attention_heads, encoder_seq_length, num_global_attn_indices]     ,  )

					for model_class in self.all_model_classes:
						__snake_case      :							Any						=			True
						__snake_case      :							Union[str, Any]						=			False
						__snake_case      :							int						=			False
						__snake_case      :							Union[str, Any]						=			model_class(_A)
						__snake_case      :							int						=			model(self._prepare_for_class(_A     ,  _A))
						__snake_case      :							Optional[Any]						=			len(_A)
						self.assertEqual(config.output_hidden_states     ,  _A)
						check_encoder_attentions_output(_A)

						if self.is_encoder_decoder:
							__snake_case      :							List[str]						=			model_class(_A)
							__snake_case      :							Optional[int]						=			model(self._prepare_for_class(_A     ,  _A))
							self.assertEqual(config.output_hidden_states     ,  _A)
							check_decoder_attentions_output(_A)

						# Check that output attentions can also be changed via the config
						del inputs_dict["output_attentions"]
						__snake_case      :							Tuple						=			True
						__snake_case      :							Optional[Any]						=			model_class(_A)
						__snake_case      :							Tuple						=			model(self._prepare_for_class(_A     ,  _A))
						self.assertEqual(config.output_hidden_states     ,  _A)
						check_encoder_attentions_output(_A)

						# Check attention is always last and order is fine
						__snake_case      :							List[Any]						=			True
						__snake_case      :							str						=			True
						__snake_case      :							Optional[int]						=			model_class(_A)
						__snake_case      :							Optional[Any]						=			model(self._prepare_for_class(_A     ,  _A))

						self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)     ,  len(_A))
						self.assertEqual(model.config.output_hidden_states     ,  _A)
						check_encoder_attentions_output(_A)



				@unittest.skip('LED keeps using potentially symbolic tensors in conditionals and breaks tracing.')
				def  _lowercase					(self    :				Tuple)   ->     List[Any]:
					pass






				def  _lowercase					(self    :				str)   ->     Optional[int]:
					# TODO: Head-masking not yet implement
					pass







def   __UpperCAmelCase    (   UpperCAmelCase_   :    Tuple  )							->      Optional[int]:

	'''simple docstring'''





	return tf.constant(UpperCAmelCase_      ,							dtype=tf.intaa  )


_a					:					Optional[int]=			1e-4

@slow
@require_tf
class 		UpperCamelCase      (   unittest.TestCase						):



				def  _lowercase					(self    :				Dict)   ->     Optional[Any]:
					__snake_case      :							Any						=			TFLEDForConditionalGeneration.from_pretrained('allenai/led-base-16384').led

					# change to intended input here
					__snake_case      :							List[str]						=			_long_tensor([5_12 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]])
					__snake_case      :							Tuple						=			_long_tensor([1_28 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]])
					__snake_case      :							Tuple						=			prepare_led_inputs_dict(model.config     ,  _A     ,  _A)
					__snake_case      :							List[str]						=			model(**_A)[0]
					__snake_case      :							Optional[int]						=			(1, 10_24, 7_68)
					self.assertEqual(output.shape     ,  _A)
					# change to expected output here
					__snake_case      :							Any						=			tf.convert_to_tensor(
					    [[2.3_050, 2.8_279, 0.6_531], [-1.8_457, -0.1_455, -3.5_661], [-1.0_186, 0.4_586, -2.2_043]]     ,  )
					tf.debugging.assert_near(output[:, :3, :3]     ,  _A     ,  atol=1E-3)






				def  _lowercase					(self    :				Optional[Any])   ->     List[str]:
					__snake_case      :							Union[str, Any]						=			TFLEDForConditionalGeneration.from_pretrained('allenai/led-base-16384')

					# change to intended input here
					__snake_case      :							str						=			_long_tensor([5_12 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]])
					__snake_case      :							Dict						=			_long_tensor([1_28 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]])
					__snake_case      :							List[Any]						=			prepare_led_inputs_dict(model.config     ,  _A     ,  _A)
					__snake_case      :							int						=			model(**_A)[0]
					__snake_case      :							Any						=			(1, 10_24, model.config.vocab_size)
					self.assertEqual(output.shape     ,  _A)
					# change to expected output here
					__snake_case      :							Dict						=			tf.convert_to_tensor(
					    [[33.6_507, 6.4_572, 16.8_089], [5.8_739, -2.4_238, 11.2_902], [-3.2_139, -4.3_149, 4.2_783]]     ,  )
					tf.debugging.assert_near(output[:, :3, :3]     ,  _A     ,  atol=1E-3     ,  rtol=1E-3)






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



				def  _lowercase					(self    :				Any)   ->     List[Any]:
					__snake_case      :							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      :



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



				def  _lowercase					(self    :				List[Any])   ->     Tuple:
					__snake_case      :							Optional[Any]						=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

					__snake_case      :							Dict						=			self.get_config()
					return config, pixel_values, labels



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






				def  _lowercase					(self    :				Any)   ->     Optional[int]:
					__snake_case      :							Optional[Any]						=			self.prepare_config_and_inputs()
					__snake_case		,    __snake_case		,    __snake_case      :							str						=			config_and_inputs
					__snake_case      :							List[str]						=			{'pixel_values': pixel_values}
					return config, inputs_dict







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

				UpperCAmelCase    :				Optional[int]   =		True
				UpperCAmelCase    :				Optional[int]   =		False
				UpperCAmelCase    :				List[Any]   =		False
				UpperCAmelCase    :				List[Any]   =		False



				def  _lowercase					(self    :				str)   ->     Union[str, Any]:
					__snake_case      :							Optional[int]						=			SegformerModelTester(self)
					__snake_case      :							Any						=			SegformerConfigTester(self     ,  config_class=_A)



				def  _lowercase					(self    :				List[str])   ->     Optional[int]:
					self.config_tester.run_common_tests()



				def  _lowercase					(self    :				List[Any])   ->     List[str]:
					__snake_case      :							int						=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*_A)



				def  _lowercase					(self    :				Optional[int])   ->     str:
					__snake_case      :							List[Any]						=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_binary_image_segmentation(*_A)



				def  _lowercase					(self    :				int)   ->     Union[str, Any]:
					__snake_case      :							Union[str, Any]						=			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  _lowercase					(self    :				Union[str, Any])   ->     str:
					pass



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



				def  _lowercase					(self    :				List[str])   ->     Any:
					__snake_case		,    __snake_case      :							Dict						=			self.model_tester.prepare_config_and_inputs_for_common()

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

						__snake_case      :							Optional[int]						=			['pixel_values']
						self.assertListEqual(arg_names[:1]     ,  _A)



				def  _lowercase					(self    :				List[str])   ->     List[Any]:
					__snake_case		,    __snake_case      :							Union[str, Any]						=			self.model_tester.prepare_config_and_inputs_for_common()
					__snake_case      :							Optional[int]						=			True

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

						__snake_case      :							int						=			sum(self.model_tester.depths)
						self.assertEqual(len(_A)     ,  _A)

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

						self.assertEqual(len(_A)     ,  _A)

						# verify the first attentions (first block, first layer)
						__snake_case      :							Optional[int]						=			(self.model_tester.image_size // 4) ** 2
						__snake_case      :							Tuple						=			(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)
						__snake_case      :							int						=			(self.model_tester.image_size // 32) ** 2
						__snake_case      :							Any						=			(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]     ,  )
						__snake_case      :							int						=			len(_A)

						# Check attention is always last and order is fine
						__snake_case      :							Any						=			True
						__snake_case      :							Tuple						=			True
						__snake_case      :							Optional[Any]						=			model_class(_A)
						model.to(_A)
						model.eval()
						with torch.no_grad():
							__snake_case      :							Any						=			model(**self._prepare_for_class(_A     ,  _A))

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

						__snake_case      :							List[Any]						=			outputs.attentions

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

						with torch.no_grad():
							__snake_case      :							Optional[int]						=			model(**self._prepare_for_class(_A     ,  _A))

						__snake_case      :							List[str]						=			outputs.hidden_states

						__snake_case      :							Tuple						=			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,
						    ]     ,  )

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

					for model_class in self.all_model_classes:
						__snake_case      :							List[str]						=			True
						check_hidden_states_output(_A     ,  _A     ,  _A)

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

						check_hidden_states_output(_A     ,  _A     ,  _A)



				def  _lowercase					(self    :				Optional[int])   ->     int:
					if not self.model_tester.is_training:
						return

					__snake_case		,    __snake_case      :							Dict						=			self.model_tester.prepare_config_and_inputs_for_common()
					__snake_case      :							int						=			True

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

						__snake_case      :							Tuple						=			model_class(_A)
						model.to(_A)
						model.train()
						__snake_case      :							str						=			self._prepare_for_class(_A     ,  _A     ,  return_labels=_A)
						__snake_case      :							Dict						=			model(**_A).loss
						loss.backward()



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






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







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

	'''simple docstring'''





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

@require_torch
class 		UpperCamelCase      (   unittest.TestCase						):



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

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

					with torch.no_grad():
						__snake_case      :							Any						=			model(_A)

					__snake_case      :							Optional[int]						=			torch.Size((1, model.config.num_labels, 1_28, 1_28))
					self.assertEqual(outputs.logits.shape     ,  _A)

					__snake_case      :							Tuple						=			torch.tensor(
					    [
					        [[-4.6_310, -5.5_232, -6.2_356], [-5.1_921, -6.1_444, -6.5_996], [-5.4_424, -6.2_790, -6.7_574]],
					        [[-12.1_391, -13.3_122, -13.9_554], [-12.8_732, -13.9_352, -14.3_563], [-12.9_438, -13.8_226, -14.2_513]],
					        [[-12.5_134, -13.4_686, -14.4_915], [-12.8_669, -14.4_343, -14.7_758], [-13.2_523, -14.5_819, -15.0_694]],
					    ]).to(_A)
					self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]     ,  _A     ,  atol=1E-4))



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

					__snake_case      :							str						=			prepare_img()
					__snake_case      :							Union[str, Any]						=			image_processor(images=_A     ,  return_tensors='pt')
					__snake_case      :							str						=			encoded_inputs.pixel_values.to(_A)

					with torch.no_grad():
						__snake_case      :							Any						=			model(_A)

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

					__snake_case      :							List[Any]						=			torch.tensor(
					    [
					        [[-13.5_748, -13.9_111, -12.6_500], [-14.3_500, -15.3_683, -14.2_328], [-14.7_532, -16.0_424, -15.6_087]],
					        [[-17.1_651, -15.8_725, -12.9_653], [-17.2_580, -17.3_718, -14.8_223], [-16.6_058, -16.8_783, -16.7_452]],
					        [[-3.6_456, -3.0_209, -1.4_203], [-3.0_797, -3.1_959, -2.0_000], [-1.8_757, -1.9_217, -1.6_997]],
					    ]).to(_A)
					self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]     ,  _A     ,  atol=1E-1))






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

					__snake_case      :							Optional[Any]						=			prepare_img()
					__snake_case      :							Optional[Any]						=			image_processor(images=_A     ,  return_tensors='pt')
					__snake_case      :							Union[str, Any]						=			encoded_inputs.pixel_values.to(_A)

					with torch.no_grad():
						__snake_case      :							Any						=			model(_A)

					__snake_case      :							Optional[Any]						=			outputs.logits.detach().cpu()

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

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





