





'''simple docstring'''

import math
from collections import defaultdict
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput

def     __magic_name__							(				__UpperCAmelCase				,						__UpperCAmelCase=0.9_9_9				,						__UpperCAmelCase="cosine"				,						)    ->       int:

   '''simple docstring'''







   if alpha_transform_type == "cosine":

      def alpha_bar_fn(__UpperCAmelCase   ):
         return math.cos((t + 0.0_0_8) / 1.0_0_8 * math.pi / 2   ) ** 2

   elif alpha_transform_type == "exp":

      def alpha_bar_fn(__UpperCAmelCase   ):
         return math.exp(t * -1_2.0   )

   else:
      raise ValueError(f"""Unsupported alpha_tranform_type: {alpha_transform_type}"""   )

   __SCREAMING_SNAKE_CASE   				=							[]
   for i in range(__UpperCAmelCase   ):
      __SCREAMING_SNAKE_CASE   				=							i / num_diffusion_timesteps
      __SCREAMING_SNAKE_CASE   				=							(i + 1) / num_diffusion_timesteps
      betas.append(min(1 - alpha_bar_fn(__UpperCAmelCase   ) / alpha_bar_fn(__UpperCAmelCase   )				,						__UpperCAmelCase   )   )
   return torch.tensor(__UpperCAmelCase				,						dtype=torch.floataa   )







class __a			( _snake_case, _snake_case      ):
   __UpperCamelCase :			Tuple						   =						[e.name for e in KarrasDiffusionSchedulers]
   __UpperCamelCase :			Union[str, Any]						   =						2





   @register_to_config
   def __init__(       self					:      Union[str, Any]					,lowerCamelCase					:      int = 1000					,lowerCamelCase					:      float = 0.00_085					,lowerCamelCase					:      float = 0.012					,lowerCamelCase					:      str = "linear"					,lowerCamelCase					:      Optional[Union[np.ndarray, List[float]]] = None					,lowerCamelCase					:      str = "epsilon"					,lowerCamelCase					:      str = "linspace"					,lowerCamelCase					:      int = 0					,):

      '''simple docstring'''

      if trained_betas is not None:
         __SCREAMING_SNAKE_CASE   				=							torch.tensor(lowerCamelCase					,dtype=torch.floataa     )
      elif beta_schedule == "linear":
         __SCREAMING_SNAKE_CASE   				=							torch.linspace(lowerCamelCase					,lowerCamelCase					,lowerCamelCase					,dtype=torch.floataa     )
      elif beta_schedule == "scaled_linear":
         # this schedule is very specific to the latent diffusion model.
         __SCREAMING_SNAKE_CASE   				=							(
             torch.linspace(beta_start**0.5					,beta_end**0.5					,lowerCamelCase					,dtype=torch.floataa     ) ** 2
         )
      elif beta_schedule == "squaredcos_cap_v2":
         # Glide cosine schedule
         __SCREAMING_SNAKE_CASE   				=							betas_for_alpha_bar(lowerCamelCase     )
      else:
         raise NotImplementedError(f"""{beta_schedule} does is not implemented for {self.__class__}"""     )

      __SCREAMING_SNAKE_CASE   				=							1.0 - self.betas
      __SCREAMING_SNAKE_CASE   				=							torch.cumprod(self.alphas					,dim=0     )

      #  set all values
      self.set_timesteps(lowerCamelCase					,lowerCamelCase					,lowerCamelCase     )





   def   UpperCAmelCase__  (       self					:      int					,lowerCamelCase					:      str					,lowerCamelCase					:      int=None     ):

      '''simple docstring'''

      if schedule_timesteps is None:
         __SCREAMING_SNAKE_CASE   				=							self.timesteps

      __SCREAMING_SNAKE_CASE   				=							(schedule_timesteps == timestep).nonzero()

      # The sigma index that is taken for the **very** first `step`
      # is always the second index (or the last index if there is only 1)
      # This way we can ensure we don't accidentally skip a sigma in
      # case we start in the middle of the denoising schedule (e.g. for image-to-image)
      if len(self._index_counter     ) == 0:
         __SCREAMING_SNAKE_CASE   				=							1 if len(lowerCamelCase     ) > 1 else 0
      else:
         __SCREAMING_SNAKE_CASE   				=							timestep.cpu().item() if torch.is_tensor(lowerCamelCase     ) else timestep
         __SCREAMING_SNAKE_CASE   				=							self._index_counter[timestep_int]

      return indices[pos].item()





   @property
   def   UpperCAmelCase__  (       self					:      List[Any]     ):

      '''simple docstring'''

      if self.config.timestep_spacing in ["linspace", "trailing"]:
         return self.sigmas.max()

      return (self.sigmas.max() ** 2 + 1) ** 0.5





   def   UpperCAmelCase__  (       self					:      Dict					,lowerCamelCase					:      torch.FloatTensor					,lowerCamelCase					:      Union[float, torch.FloatTensor]					,):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							self.index_for_timestep(lowerCamelCase     )

      if self.state_in_first_order:
         __SCREAMING_SNAKE_CASE   				=							self.sigmas[step_index]
      else:
         __SCREAMING_SNAKE_CASE   				=							self.sigmas_interpol[step_index]

      __SCREAMING_SNAKE_CASE   				=							sample / ((sigma**2 + 1) ** 0.5)
      return sample





   def   UpperCAmelCase__  (       self					:      int					,lowerCamelCase					:      int					,lowerCamelCase					:      Union[str, torch.device] = None					,lowerCamelCase					:      Optional[int] = None					,):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							num_inference_steps

      __SCREAMING_SNAKE_CASE   				=							num_train_timesteps or self.config.num_train_timesteps

      # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891
      if self.config.timestep_spacing == "linspace":
         __SCREAMING_SNAKE_CASE   				=							np.linspace(0					,num_train_timesteps - 1					,lowerCamelCase					,dtype=lowerCamelCase     )[::-1].copy()
      elif self.config.timestep_spacing == "leading":
         __SCREAMING_SNAKE_CASE   				=							num_train_timesteps // self.num_inference_steps
         # creates integer timesteps by multiplying by ratio
         # casting to int to avoid issues when num_inference_step is power of 3
         __SCREAMING_SNAKE_CASE   				=							(np.arange(0					,lowerCamelCase     ) * step_ratio).round()[::-1].copy().astype(lowerCamelCase     )
         timesteps += self.config.steps_offset
      elif self.config.timestep_spacing == "trailing":
         __SCREAMING_SNAKE_CASE   				=							num_train_timesteps / self.num_inference_steps
         # creates integer timesteps by multiplying by ratio
         # casting to int to avoid issues when num_inference_step is power of 3
         __SCREAMING_SNAKE_CASE   				=							(np.arange(lowerCamelCase					,0					,-step_ratio     )).round().copy().astype(lowerCamelCase     )
         timesteps -= 1
      else:
         raise ValueError(
             f"""{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'."""     )

      __SCREAMING_SNAKE_CASE   				=							np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5     )
      __SCREAMING_SNAKE_CASE   				=							torch.from_numpy(np.log(lowerCamelCase     )     ).to(lowerCamelCase     )

      __SCREAMING_SNAKE_CASE   				=							np.interp(lowerCamelCase					,np.arange(0					,len(lowerCamelCase     )     )					,lowerCamelCase     )
      __SCREAMING_SNAKE_CASE   				=							np.concatenate([sigmas, [0.0]]     ).astype(np.floataa     )
      __SCREAMING_SNAKE_CASE   				=							torch.from_numpy(lowerCamelCase     ).to(device=lowerCamelCase     )

      # interpolate sigmas
      __SCREAMING_SNAKE_CASE   				=							sigmas.log().lerp(sigmas.roll(1     ).log()					,0.5     ).exp()

      __SCREAMING_SNAKE_CASE   				=							torch.cat([sigmas[:1], sigmas[1:].repeat_interleave(2     ), sigmas[-1:]]     )
      __SCREAMING_SNAKE_CASE   				=							torch.cat(
          [sigmas_interpol[:1], sigmas_interpol[1:].repeat_interleave(2     ), sigmas_interpol[-1:]]     )

      if str(lowerCamelCase     ).startswith("""mps"""     ):
         # mps does not support float64
         __SCREAMING_SNAKE_CASE   				=							torch.from_numpy(lowerCamelCase     ).to(lowerCamelCase					,dtype=torch.floataa     )
      else:
         __SCREAMING_SNAKE_CASE   				=							torch.from_numpy(lowerCamelCase     ).to(lowerCamelCase     )

      # interpolate timesteps
      __SCREAMING_SNAKE_CASE   				=							self.sigma_to_t(lowerCamelCase     ).to(lowerCamelCase					,dtype=timesteps.dtype     )
      __SCREAMING_SNAKE_CASE   				=							torch.stack((timesteps_interpol[1:-1, None], timesteps[1:, None])					,dim=-1     ).flatten()

      __SCREAMING_SNAKE_CASE   				=							torch.cat([timesteps[:1], interleaved_timesteps]     )

      __SCREAMING_SNAKE_CASE   				=							None

      # for exp beta schedules, such as the one for `pipeline_shap_e.py`
      # we need an index counter
      __SCREAMING_SNAKE_CASE   				=							defaultdict(lowerCamelCase     )





   def   UpperCAmelCase__  (       self					:      Optional[Any]					,lowerCamelCase					:      Dict     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							sigma.log()

      # get distribution
      __SCREAMING_SNAKE_CASE   				=							log_sigma - self.log_sigmas[:, None]

      # get sigmas range
      __SCREAMING_SNAKE_CASE   				=							dists.ge(0     ).cumsum(dim=0     ).argmax(dim=0     ).clamp(max=self.log_sigmas.shape[0] - 2     )
      __SCREAMING_SNAKE_CASE   				=							low_idx + 1

      __SCREAMING_SNAKE_CASE   				=							self.log_sigmas[low_idx]
      __SCREAMING_SNAKE_CASE   				=							self.log_sigmas[high_idx]

      # interpolate sigmas
      __SCREAMING_SNAKE_CASE   				=							(low - log_sigma) / (low - high)
      __SCREAMING_SNAKE_CASE   				=							w.clamp(0					,1     )

      # transform interpolation to time range
      __SCREAMING_SNAKE_CASE   				=							(1 - w) * low_idx + w * high_idx
      __SCREAMING_SNAKE_CASE   				=							t.view(sigma.shape     )
      return t





   @property
   def   UpperCAmelCase__  (       self					:      Any     ):

      '''simple docstring'''

      return self.sample is None





   def   UpperCAmelCase__  (       self					:      List[str]					,lowerCamelCase					:      Union[torch.FloatTensor, np.ndarray]					,lowerCamelCase					:      Union[float, torch.FloatTensor]					,lowerCamelCase					:      Union[torch.FloatTensor, np.ndarray]					,lowerCamelCase					:      bool = True					,):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							self.index_for_timestep(lowerCamelCase     )

      # advance index counter by 1
      __SCREAMING_SNAKE_CASE   				=							timestep.cpu().item() if torch.is_tensor(lowerCamelCase     ) else timestep
      self._index_counter[timestep_int] += 1

      if self.state_in_first_order:
         __SCREAMING_SNAKE_CASE   				=							self.sigmas[step_index]
         __SCREAMING_SNAKE_CASE   				=							self.sigmas_interpol[step_index + 1]
         __SCREAMING_SNAKE_CASE   				=							self.sigmas[step_index + 1]
      else:
         # 2nd order / KDPM2's method
         __SCREAMING_SNAKE_CASE   				=							self.sigmas[step_index - 1]
         __SCREAMING_SNAKE_CASE   				=							self.sigmas_interpol[step_index]
         __SCREAMING_SNAKE_CASE   				=							self.sigmas[step_index]

      # currently only gamma=0 is supported. This usually works best anyways.
      # We can support gamma in the future but then need to scale the timestep before
      # passing it to the model which requires a change in API
      __SCREAMING_SNAKE_CASE   				=							0
      __SCREAMING_SNAKE_CASE   				=							sigma * (gamma + 1)  # Note: sigma_hat == sigma for now

      # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
      if self.config.prediction_type == "epsilon":
         __SCREAMING_SNAKE_CASE   				=							sigma_hat if self.state_in_first_order else sigma_interpol
         __SCREAMING_SNAKE_CASE   				=							sample - sigma_input * model_output
      elif self.config.prediction_type == "v_prediction":
         __SCREAMING_SNAKE_CASE   				=							sigma_hat if self.state_in_first_order else sigma_interpol
         __SCREAMING_SNAKE_CASE   				=							model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + (
             sample / (sigma_input**2 + 1)
         )
      elif self.config.prediction_type == "sample":
         raise NotImplementedError("""prediction_type not implemented yet: sample"""     )
      else:
         raise ValueError(
             f"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`"""     )

      if self.state_in_first_order:
         # 2. Convert to an ODE derivative for 1st order
         __SCREAMING_SNAKE_CASE   				=							(sample - pred_original_sample) / sigma_hat
         # 3. delta timestep
         __SCREAMING_SNAKE_CASE   				=							sigma_interpol - sigma_hat

         # store for 2nd order step
         __SCREAMING_SNAKE_CASE   				=							sample
      else:
         # DPM-Solver-2
         # 2. Convert to an ODE derivative for 2nd order
         __SCREAMING_SNAKE_CASE   				=							(sample - pred_original_sample) / sigma_interpol

         # 3. delta timestep
         __SCREAMING_SNAKE_CASE   				=							sigma_next - sigma_hat

         __SCREAMING_SNAKE_CASE   				=							self.sample
         __SCREAMING_SNAKE_CASE   				=							None

      __SCREAMING_SNAKE_CASE   				=							sample + derivative * dt

      if not return_dict:
         return (prev_sample,)

      return SchedulerOutput(prev_sample=lowerCamelCase     )





   def   UpperCAmelCase__  (       self					:      Optional[Any]					,lowerCamelCase					:      torch.FloatTensor					,lowerCamelCase					:      torch.FloatTensor					,lowerCamelCase					:      torch.FloatTensor					,):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							self.sigmas.to(device=original_samples.device					,dtype=original_samples.dtype     )
      if original_samples.device.type == "mps" and torch.is_floating_point(lowerCamelCase     ):
         # mps does not support float64
         __SCREAMING_SNAKE_CASE   				=							self.timesteps.to(original_samples.device					,dtype=torch.floataa     )
         __SCREAMING_SNAKE_CASE   				=							timesteps.to(original_samples.device					,dtype=torch.floataa     )
      else:
         __SCREAMING_SNAKE_CASE   				=							self.timesteps.to(original_samples.device     )
         __SCREAMING_SNAKE_CASE   				=							timesteps.to(original_samples.device     )

      __SCREAMING_SNAKE_CASE   				=							[self.index_for_timestep(lowerCamelCase					,lowerCamelCase     ) for t in timesteps]

      __SCREAMING_SNAKE_CASE   				=							sigmas[step_indices].flatten()
      while len(sigma.shape     ) < len(original_samples.shape     ):
         __SCREAMING_SNAKE_CASE   				=							sigma.unsqueeze(-1     )

      __SCREAMING_SNAKE_CASE   				=							original_samples + noise * sigma
      return noisy_samples





   def __len__(       self					:      List[Any]     ):

      '''simple docstring'''

      return self.config.num_train_timesteps









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 						lowerCamelCase_							(					lowerCAmelCase__					):



					'''simple docstring'''







					def A   (					self    ) ->      List[str]:







								'''simple docstring'''

								__lowercase			       = 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 						lowerCamelCase_							:



					'''simple docstring'''







					def __init__(					self			,							snake_case_			,							snake_case_=1_3			,							snake_case_=6_4			,							snake_case_=3			,							snake_case_=4			,							snake_case_=[2, 2, 2, 2]			,							snake_case_=[8, 4, 2, 1]			,							snake_case_=[1_6, 3_2, 6_4, 1_2_8]			,							snake_case_=[1, 4, 8, 1_6]			,							snake_case_=[1, 2, 4, 8]			,							snake_case_=True			,							snake_case_=True			,							snake_case_="gelu"			,							snake_case_=0.1			,							snake_case_=0.1			,							snake_case_=0.0_2			,							snake_case_=3			,							snake_case_=None			,							) ->      int:







								'''simple docstring'''

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






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







								'''simple docstring'''

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

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

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






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







								'''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 A   (					self			,							snake_case_			,							snake_case_			,							snake_case_    ) ->      Tuple:







								'''simple docstring'''

								__lowercase			       = SegformerModel(config=snake_case_    )
								model.to(snake_case_    )
								model.eval()
								__lowercase			       = model(snake_case_    )
								__lowercase			       = __lowercase			       = 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 A   (					self			,							snake_case_			,							snake_case_			,							snake_case_    ) ->      str:







								'''simple docstring'''

								__lowercase			       = self.num_labels
								__lowercase			       = SegformerForSemanticSegmentation(snake_case_    )
								model.to(snake_case_    )
								model.eval()
								__lowercase			       = model(snake_case_    )
								self.parent.assertEqual(
								    result.logits.shape			,							(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)    )
								__lowercase			       = 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 A   (					self			,							snake_case_			,							snake_case_			,							snake_case_    ) ->      Optional[int]:







								'''simple docstring'''

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






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







								'''simple docstring'''

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




@require_torch
class 						lowerCamelCase_							(					lowerCAmelCase__  ,  lowerCAmelCase__  ,  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 A   (					self    ) ->      Any:







								'''simple docstring'''

								__lowercase			       = SegformerModelTester(self    )
								__lowercase			       = SegformerConfigTester(self			,							config_class=snake_case_    )






					def A   (					self    ) ->      Dict:







								'''simple docstring'''

								self.config_tester.run_common_tests()






					def A   (					self    ) ->      Dict:







								'''simple docstring'''

								__lowercase			       = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*snake_case_    )






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







								'''simple docstring'''

								__lowercase			       = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_binary_image_segmentation(*snake_case_    )






					def A   (					self    ) ->      int:







								'''simple docstring'''

								__lowercase			       = 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 A   (					self    ) ->      List[Any]:







								'''simple docstring'''

								pass






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







								'''simple docstring'''

								pass






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







								'''simple docstring'''

								__lowercase       ,			__lowercase			       = self.model_tester.prepare_config_and_inputs_for_common()

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

											__lowercase			       = ['''pixel_values''']
											self.assertListEqual(arg_names[:1]			,							snake_case_    )






					def A   (					self    ) ->      List[str]:







								'''simple docstring'''

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

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

											__lowercase			       = 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"]
											__lowercase			       = True
											__lowercase			       = model_class(snake_case_    )
											model.to(snake_case_    )
											model.eval()
											with torch.no_grad():
														__lowercase			       = model(**self._prepare_for_class(snake_case_			,							snake_case_    )    )
											__lowercase			       = outputs.attentions

											self.assertEqual(len(snake_case_    )			,							snake_case_    )

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

											# verify the last attentions (last block, last layer)
											__lowercase			       = (self.model_tester.image_size // 3_2) ** 2
											__lowercase			       = (self.model_tester.image_size // (3_2 * self.model_tester.sr_ratios[-1])) ** 2
											self.assertListEqual(
											    list(attentions[-1].shape[-3:]    )			,							[self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]			,							)
											__lowercase			       = len(snake_case_    )

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

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

											__lowercase			       = outputs.attentions

											self.assertEqual(len(snake_case_    )			,							snake_case_    )
											# verify the first attentions (first block, first layer)
											__lowercase			       = (self.model_tester.image_size // 4) ** 2
											__lowercase			       = (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 A   (					self    ) ->      Dict:







								'''simple docstring'''

								def check_hidden_states_output(snake_case_			,							snake_case_			,							snake_case_    ):
											__lowercase			       = model_class(snake_case_    )
											model.to(snake_case_    )
											model.eval()

											with torch.no_grad():
														__lowercase			       = model(**self._prepare_for_class(snake_case_			,							snake_case_    )    )

											__lowercase			       = outputs.hidden_states

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

								__lowercase       ,			__lowercase			       = self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
											__lowercase			       = 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"]
											__lowercase			       = True

											check_hidden_states_output(snake_case_			,							snake_case_			,							snake_case_    )






					def A   (					self    ) ->      str:







								'''simple docstring'''

								if not self.model_tester.is_training:
											return

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

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

											__lowercase			       = model_class(snake_case_    )
											model.to(snake_case_    )
											model.train()
											__lowercase			       = self._prepare_for_class(snake_case_			,							snake_case_			,							return_labels=snake_case_    )
											__lowercase			       = model(**snake_case_    ).loss
											loss.backward()






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







								'''simple docstring'''

								pass






					@slow
					def A   (					self    ) ->      str:







								'''simple docstring'''

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




def 		lowercase_	(  ):


			'''simple docstring'''







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





@require_torch
class 						lowerCamelCase_							(					unittest.TestCase					):



					'''simple docstring'''







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







								'''simple docstring'''

								__lowercase			       = SegformerImageProcessor(
								    image_scale=(5_1_2, 5_1_2)			,							keep_ratio=snake_case_			,							align=snake_case_			,							do_random_crop=snake_case_    )
								__lowercase			       = SegformerForSemanticSegmentation.from_pretrained('''nvidia/segformer-b0-finetuned-ade-512-512'''    ).to(
								    snake_case_    )

								__lowercase			       = prepare_img()
								__lowercase			       = image_processor(images=snake_case_			,							return_tensors='''pt'''    )
								__lowercase			       = encoded_inputs.pixel_values.to(snake_case_    )

								with torch.no_grad():
											__lowercase			       = model(snake_case_    )

								__lowercase			       = torch.Size((1, model.config.num_labels, 1_2_8, 1_2_8)    )
								self.assertEqual(outputs.logits.shape			,							snake_case_    )

								__lowercase			       = torch.tensor(
								    [
								        [[-4.6_3_1_0, -5.5_2_3_2, -6.2_3_5_6], [-5.1_9_2_1, -6.1_4_4_4, -6.5_9_9_6], [-5.4_4_2_4, -6.2_7_9_0, -6.7_5_7_4]],
								        [[-1_2.1_3_9_1, -1_3.3_1_2_2, -1_3.9_5_5_4], [-1_2.8_7_3_2, -1_3.9_3_5_2, -1_4.3_5_6_3], [-1_2.9_4_3_8, -1_3.8_2_2_6, -1_4.2_5_1_3]],
								        [[-1_2.5_1_3_4, -1_3.4_6_8_6, -1_4.4_9_1_5], [-1_2.8_6_6_9, -1_4.4_3_4_3, -1_4.7_7_5_8], [-1_3.2_5_2_3, -1_4.5_8_1_9, -1_5.0_6_9_4]],
								    ]    ).to(snake_case_    )
								self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]			,							snake_case_			,							atol=1e-4    )    )






					@slow
					def A   (					self    ) ->      int:







								'''simple docstring'''

								__lowercase			       = SegformerImageProcessor(
								    image_scale=(5_1_2, 5_1_2)			,							keep_ratio=snake_case_			,							align=snake_case_			,							do_random_crop=snake_case_    )
								__lowercase			       = SegformerForSemanticSegmentation.from_pretrained(
								    '''nvidia/segformer-b1-finetuned-cityscapes-1024-1024'''    ).to(snake_case_    )

								__lowercase			       = prepare_img()
								__lowercase			       = image_processor(images=snake_case_			,							return_tensors='''pt'''    )
								__lowercase			       = encoded_inputs.pixel_values.to(snake_case_    )

								with torch.no_grad():
											__lowercase			       = model(snake_case_    )

								__lowercase			       = torch.Size((1, model.config.num_labels, 1_2_8, 1_2_8)    )
								self.assertEqual(outputs.logits.shape			,							snake_case_    )

								__lowercase			       = torch.tensor(
								    [
								        [[-1_3.5_7_4_8, -1_3.9_1_1_1, -1_2.6_5_0_0], [-1_4.3_5_0_0, -1_5.3_6_8_3, -1_4.2_3_2_8], [-1_4.7_5_3_2, -1_6.0_4_2_4, -1_5.6_0_8_7]],
								        [[-1_7.1_6_5_1, -1_5.8_7_2_5, -1_2.9_6_5_3], [-1_7.2_5_8_0, -1_7.3_7_1_8, -1_4.8_2_2_3], [-1_6.6_0_5_8, -1_6.8_7_8_3, -1_6.7_4_5_2]],
								        [[-3.6_4_5_6, -3.0_2_0_9, -1.4_2_0_3], [-3.0_7_9_7, -3.1_9_5_9, -2.0_0_0_0], [-1.8_7_5_7, -1.9_2_1_7, -1.6_9_9_7]],
								    ]    ).to(snake_case_    )
								self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]			,							snake_case_			,							atol=1e-1    )    )






					@slow
					def A   (					self    ) ->      int:







								'''simple docstring'''

								__lowercase			       = SegformerImageProcessor(
								    image_scale=(5_1_2, 5_1_2)			,							keep_ratio=snake_case_			,							align=snake_case_			,							do_random_crop=snake_case_    )
								__lowercase			       = SegformerForSemanticSegmentation.from_pretrained('''nvidia/segformer-b0-finetuned-ade-512-512'''    ).to(
								    snake_case_    )

								__lowercase			       = prepare_img()
								__lowercase			       = image_processor(images=snake_case_			,							return_tensors='''pt'''    )
								__lowercase			       = encoded_inputs.pixel_values.to(snake_case_    )

								with torch.no_grad():
											__lowercase			       = model(snake_case_    )

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

								__lowercase			       = image_processor.post_process_semantic_segmentation(outputs=snake_case_			,							target_sizes=[(5_0_0, 3_0_0)]    )
								__lowercase			       = torch.Size((5_0_0, 3_0_0)    )
								self.assertEqual(segmentation[0].shape			,							snake_case_    )

								__lowercase			       = image_processor.post_process_semantic_segmentation(outputs=snake_case_    )
								__lowercase			       = torch.Size((1_2_8, 1_2_8)    )
								self.assertEqual(segmentation[0].shape			,							snake_case_    )


