




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


				def   A_ (		self    :					Optional[int]					):
									snake_case_									=				self.config_class(**self.inputs_dict					)
									self.parent.assertTrue(hasattr(lowercase_       ,				'''hidden_sizes'''					)					)
									self.parent.assertTrue(hasattr(lowercase_       ,				'''num_attention_heads'''					)					)
									self.parent.assertTrue(hasattr(lowercase_       ,				'''num_encoder_blocks'''					)					)







class 						a				:


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


				def   A_ (		self    :					str					):
									snake_case_									=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

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


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


				def   A_ (		self    :					List[str]					):
									snake_case_									=				self.prepare_config_and_inputs()
									snake_case_			,snake_case_			,snake_case_									=				config_and_inputs
									snake_case_									=				{'''pixel_values''': pixel_values}
									return config, inputs_dict







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

				snake_case_      =		True
				snake_case_      =		False
				snake_case_      =		False
				snake_case_      =		False


				def   A_ (		self    :					Any					):
									snake_case_									=				SegformerModelTester(self					)
									snake_case_									=				SegformerConfigTester(self       ,				config_class=lowercase_					)


				def   A_ (		self    :					Tuple					):
									self.config_tester.run_common_tests()


				def   A_ (		self    :					int					):
									snake_case_									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowercase_					)


				def   A_ (		self    :					int					):
									snake_case_									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_binary_image_segmentation(*lowercase_					)


				def   A_ (		self    :					Any					):
									snake_case_									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_segmentation(*lowercase_					)


				@unittest.skip('''SegFormer does not use inputs_embeds'''					)
				def   A_ (		self    :					str					):
									pass


				@unittest.skip('''SegFormer does not have get_input_embeddings method and get_output_embeddings methods'''					)
				def   A_ (		self    :					List[Any]					):
									pass


				def   A_ (		self    :					Optional[Any]					):
									snake_case_			,snake_case_									=				self.model_tester.prepare_config_and_inputs_for_common()

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

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


				def   A_ (		self    :					str					):
									snake_case_			,snake_case_									=				self.model_tester.prepare_config_and_inputs_for_common()
									snake_case_									=				True

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

														snake_case_									=				sum(self.model_tester.depths					)
														self.assertEqual(len(lowercase_					)       ,				lowercase_					)

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

														self.assertEqual(len(lowercase_					)       ,				lowercase_					)

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

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

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

														snake_case_									=				outputs.attentions

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

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

														snake_case_									=				outputs.hidden_states

														snake_case_									=				self.model_tester.num_encoder_blocks
														self.assertEqual(len(lowercase_					)       ,				lowercase_					)

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

									for model_class in self.all_model_classes:
														snake_case_									=				True
														check_hidden_states_output(lowercase_       ,				lowercase_       ,				lowercase_					)

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

														check_hidden_states_output(lowercase_       ,				lowercase_       ,				lowercase_					)


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

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

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

														snake_case_									=				model_class(lowercase_					)
														model.to(lowercase_					)
														model.train()
														snake_case_									=				self._prepare_for_class(lowercase_       ,				lowercase_       ,				return_labels=lowercase_					)
														snake_case_									=				model(**lowercase_					).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]					):
									pass


				@slow
				def   A_ (		self    :					Dict					):
									for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														snake_case_									=				SegformerModel.from_pretrained(lowercase_					)
														self.assertIsNotNone(lowercase_					)







def 		__magic_name__    (	)		->					Optional[int]:

					'''simple docstring'''

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






@require_torch
class 						a				(   unittest.TestCase     ):


				@slow
				def   A_ (		self    :					List[Any]					):
									# only resize + normalize
									snake_case_									=				SegformerImageProcessor(
									    image_scale=(512, 512)       ,				keep_ratio=lowercase_       ,				align=lowercase_       ,				do_random_crop=lowercase_					)
									snake_case_									=				SegformerForSemanticSegmentation.from_pretrained('''nvidia/segformer-b0-finetuned-ade-512-512'''					).to(
									    lowercase_					)

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

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

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

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


				@slow
				def   A_ (		self    :					List[str]					):
									# only resize + normalize
									snake_case_									=				SegformerImageProcessor(
									    image_scale=(512, 512)       ,				keep_ratio=lowercase_       ,				align=lowercase_       ,				do_random_crop=lowercase_					)
									snake_case_									=				SegformerForSemanticSegmentation.from_pretrained(
									    '''nvidia/segformer-b1-finetuned-cityscapes-1024-1024'''					).to(lowercase_					)

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

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

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

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


				@slow
				def   A_ (		self    :					str					):
									# only resize + normalize
									snake_case_									=				SegformerImageProcessor(
									    image_scale=(512, 512)       ,				keep_ratio=lowercase_       ,				align=lowercase_       ,				do_random_crop=lowercase_					)
									snake_case_									=				SegformerForSemanticSegmentation.from_pretrained('''nvidia/segformer-b0-finetuned-ade-512-512'''					).to(
									    lowercase_					)

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

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

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

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

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







'''simple docstring'''







import math
from typing import Optional

import numpy as np

from ...configuration_utils import PretrainedConfig
from ...utils import logging


A     =logging.get_logger(__name__)

A     ={
    'facebook/encodec_24khz': 'https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json',
    'facebook/encodec_48khz': 'https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json',
}


class 			_a  (	__a ):
       __a				:     Union[str, Any]   				=					"""encodec"""
       def __init__(	self						:	Tuple   ,				lowercase						:	List[str]=[1.5, 3.0, 6.0, 12.0, 24.0]   ,				lowercase						:	Any=24_000   ,				lowercase						:	str=1   ,				lowercase						:	Optional[int]=False   ,				lowercase						:	Optional[Any]=None   ,				lowercase						:	str=None   ,				lowercase						:	Tuple=128   ,				lowercase						:	Union[str, Any]=32   ,				lowercase						:	Union[str, Any]=1   ,				lowercase						:	Optional[Any]=[8, 5, 4, 2]   ,				lowercase						:	Any="weight_norm"   ,				lowercase						:	Tuple=7   ,				lowercase						:	int=7   ,				lowercase						:	Dict=3   ,				lowercase						:	List[Any]=2   ,				lowercase						:	str=True   ,				lowercase						:	List[str]="reflect"   ,				lowercase						:	List[Any]=2   ,				lowercase						:	Optional[Any]=2   ,				lowercase						:	int=1.0   ,				lowercase						:	Dict=1_024   ,				lowercase						:	str=None   ,				lowercase						:	Union[str, Any]=True   ,				**lowercase						:	Optional[int]   ,				):



           '''simple docstring'''

           UpperCAmelCase			       =		target_bandwidths
           UpperCAmelCase			       =		sampling_rate
           UpperCAmelCase			       =		audio_channels
           UpperCAmelCase			       =		normalize
           UpperCAmelCase			       =		chunk_length_s
           UpperCAmelCase			       =		overlap
           UpperCAmelCase			       =		hidden_size
           UpperCAmelCase			       =		num_filters
           UpperCAmelCase			       =		num_residual_layers
           UpperCAmelCase			       =		upsampling_ratios
           UpperCAmelCase			       =		norm_type
           UpperCAmelCase			       =		kernel_size
           UpperCAmelCase			       =		last_kernel_size
           UpperCAmelCase			       =		residual_kernel_size
           UpperCAmelCase			       =		dilation_growth_rate
           UpperCAmelCase			       =		use_causal_conv
           UpperCAmelCase			       =		pad_mode
           UpperCAmelCase			       =		compress
           UpperCAmelCase			       =		num_lstm_layers
           UpperCAmelCase			       =		trim_right_ratio
           UpperCAmelCase			       =		codebook_size
           UpperCAmelCase			       =		codebook_dim if codebook_dim is not None else hidden_size
           UpperCAmelCase			       =		use_conv_shortcut

           if self.norm_type not in ["weight_norm", "time_group_norm"]:
               raise ValueError(
                   f"self.norm_type must be one of `\"weight_norm\"`, `\"time_group_norm\"`), got {self.norm_type}"				)

           super().__init__(**lowercase				)
       @property
       def     A      (	self						:	Dict				):



           '''simple docstring'''

           if self.chunk_length_s is None:
               return None
           else:
               return int(self.chunk_length_s * self.sampling_rate				)
       @property
       def     A      (	self						:	Union[str, Any]				):



           '''simple docstring'''

           if self.chunk_length_s is None or self.overlap is None:
               return None
           else:
               return max(1   ,				int((1.0 - self.overlap) * self.chunk_length				)				)
       @property
       def     A      (	self						:	Any				):



           '''simple docstring'''

           UpperCAmelCase			       =		np.prod(self.upsampling_ratios				)
           return math.ceil(self.sampling_rate / hop_length				)







       @property
       def     A      (	self						:	Optional[int]				):



           '''simple docstring'''

           return int(1_000 * self.target_bandwidths[-1] // (self.frame_rate * 10)				)




