






import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ConvNextConfig, UperNetConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import UperNetForSemanticSegmentation
	from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import AutoImageProcessor




class 					UpperCamelCase_ :



	'''simple docstring'''




	def __init__(			self			,     a			,     a=13			,     a=32			,     a=3			,     a=4			,     a=[10, 20, 30, 40]			,     a=[2, 2, 3, 2]			,     a=True			,     a=True			,     a=37			,     a="gelu"			,     a=10			,     a=0.02			,     a=["stage2", "stage3", "stage4"]			,     a=3			,     a=None			,     )			->       List[Any]:
								snake_case_           =				parent
								snake_case_           =				batch_size
								snake_case_           =				image_size
								snake_case_           =				num_channels
								snake_case_           =				num_stages
								snake_case_           =				hidden_sizes
								snake_case_           =				depths
								snake_case_           =				is_training
								snake_case_           =				use_labels
								snake_case_           =				intermediate_size
								snake_case_           =				hidden_act
								snake_case_           =				type_sequence_label_size
								snake_case_           =				initializer_range
								snake_case_           =				out_features
								snake_case_           =				num_labels
								snake_case_           =				scope
								snake_case_           =				num_stages




	def 				_UpperCamelCase     (			self   )			->       List[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.type_sequence_label_size   )

								snake_case_           =				self.get_config()

								return config, pixel_values, labels




	def 				_UpperCamelCase     (			self   )			->       Optional[int]:
								return ConvNextConfig(
								    num_channels=self.num_channels			,     num_stages=self.num_stages			,     hidden_sizes=self.hidden_sizes			,     depths=self.depths			,     is_training=self.is_training			,     intermediate_size=self.intermediate_size			,     hidden_act=self.hidden_act			,     out_features=self.out_features			,     )




	def 				_UpperCamelCase     (			self   )			->       Any:
								return UperNetConfig(
								    backbone_config=self.get_backbone_config()			,     hidden_size=5_12			,     pool_scales=[1, 2, 3, 6]			,     use_auxiliary_head=a			,     auxiliary_loss_weight=0.4			,     auxiliary_in_channels=40			,     auxiliary_channels=2_56			,     auxiliary_num_convs=1			,     auxiliary_concat_input=a			,     loss_ignore_index=2_55			,     num_labels=self.num_labels			,     )




	def 				_UpperCamelCase     (			self			,     a			,     a			,     a   )			->       Dict:
								snake_case_           =				UperNetForSemanticSegmentation(config=a   )
								model.to(a   )
								model.eval()
								snake_case_           =				model(a   )
								self.parent.assertEqual(
								    result.logits.shape			,     (self.batch_size, self.num_labels, self.image_size, self.image_size)   )






	def 				_UpperCamelCase     (			self   )			->       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 					UpperCamelCase_ (     snake_case_			,   snake_case_			,   unittest.TestCase							):



	'''simple docstring'''




	lowerCAmelCase						=					(UperNetForSemanticSegmentation,) if is_torch_available() else ()
	lowerCAmelCase						=					{'''image-segmentation''': UperNetForSemanticSegmentation} if is_torch_available() else {}
	lowerCAmelCase						=					False
	lowerCAmelCase						=					False
	lowerCAmelCase						=					False
	lowerCAmelCase						=					False
	lowerCAmelCase						=					False
	lowerCAmelCase						=					False




	def 				_UpperCamelCase     (			self   )			->       Dict:
								snake_case_           =				UperNetModelTester(self   )
								snake_case_           =				ConfigTester(self			,     config_class=a			,     has_text_modality=a			,     hidden_size=37   )




	def 				_UpperCamelCase     (			self   )			->       List[Any]:
								self.create_and_test_config_common_properties()
								self.config_tester.create_and_test_config_to_json_string()
								self.config_tester.create_and_test_config_to_json_file()
								self.config_tester.create_and_test_config_from_and_save_pretrained()
								self.config_tester.create_and_test_config_with_num_labels()
								self.config_tester.check_config_can_be_init_without_params()
								self.config_tester.check_config_arguments_init()




	def 				_UpperCamelCase     (			self   )			->       Tuple:
								return




	def 				_UpperCamelCase     (			self   )			->       int:
								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(a   )
															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]			,     a   )




	def 				_UpperCamelCase     (			self   )			->       Tuple:
								snake_case_           =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_semantic_segmentation(*a   )




	@unittest.skip(reason='UperNet does not use inputs_embeds'   )
	def 				_UpperCamelCase     (			self   )			->       Tuple:
								pass




	@unittest.skip(reason='UperNet does not support input and output embeddings'   )
	def 				_UpperCamelCase     (			self   )			->       str:
								pass




	@unittest.skip(reason='UperNet does not have a base model'   )
	def 				_UpperCamelCase     (			self   )			->       str:
								pass




	@unittest.skip(reason='UperNet does not have a base model'   )
	def 				_UpperCamelCase     (			self   )			->       Any:
								pass




	@require_torch_multi_gpu
	@unittest.skip(reason='UperNet has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'   )
	def 				_UpperCamelCase     (			self   )			->       str:
								pass




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




	def 				_UpperCamelCase     (			self   )			->       str:
								def check_hidden_states_output(a			,     a			,     a   ):
															snake_case_           =				model_class(a   )
															model.to(a   )
															model.eval()

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

															snake_case_           =				outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

															snake_case_           =				self.model_tester.num_stages
															self.assertEqual(len(a   )			,     expected_num_stages + 1   )

															# ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
															self.assertListEqual(
															    list(hidden_states[0].shape[-2:]   )			,     [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(a			,     a			,     a   )

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

															check_hidden_states_output(a			,     a			,     a   )




	def 				_UpperCamelCase     (			self   )			->       Union[str, Any]:
								snake_case_     ,						snake_case_           =				self.model_tester.prepare_config_and_inputs_for_common()

								snake_case_           =				_config_zero_init(a   )
								snake_case_           =				_config_zero_init(configs_no_init.backbone_config   )
								for model_class in self.all_model_classes:
															snake_case_           =				model_class(config=a   )
															for name, param in model.named_parameters():
																						if param.requires_grad:
																													self.assertIn(
																													    ((param.data.mean() * 1E9).round() / 1E9).item()			,     [0.0, 1.0]			,     msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''			,     )




	@unittest.skip(reason='UperNet does not have tied weights'   )
	def 				_UpperCamelCase     (			self   )			->       List[str]:
								pass






	@slow
	def 				_UpperCamelCase     (			self   )			->       int:
								for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case_           =				UperNetForSemanticSegmentation.from_pretrained(a   )
															self.assertIsNotNone(a   )






def 		__UpperCAmelCase     (	):
							snake_case_           =				hf_hub_download(
							    repo_id='hf-internal-testing/fixtures_ade20k'				,	repo_type='dataset'				,	filename='ADE_val_00000001.jpg')
							snake_case_           =				Image.open(a_).convert('RGB')
							return image




@require_torch
@require_vision
@slow
class 					UpperCamelCase_ (     unittest.TestCase							):



	'''simple docstring'''




	def 				_UpperCamelCase     (			self   )			->       Dict:
								snake_case_           =				AutoImageProcessor.from_pretrained('openmmlab/upernet-swin-tiny'   )
								snake_case_           =				UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-swin-tiny'   ).to(a   )

								snake_case_           =				prepare_img()
								snake_case_           =				processor(images=a			,     return_tensors='pt'   ).to(a   )

								with torch.no_grad():
															snake_case_           =				model(**a   )

								snake_case_           =				torch.Size((1, model.config.num_labels, 5_12, 5_12)   )
								self.assertEqual(outputs.logits.shape			,     a   )

								snake_case_           =				torch.tensor(
								    [[-7.5_958, -7.5_958, -7.4_302], [-7.5_958, -7.5_958, -7.4_302], [-7.4_797, -7.4_797, -7.3_068]]   ).to(a   )
								self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]			,     a			,     atol=1E-4   )   )






	def 				_UpperCamelCase     (			self   )			->       Dict:
								snake_case_           =				AutoImageProcessor.from_pretrained('openmmlab/upernet-convnext-tiny'   )
								snake_case_           =				UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-convnext-tiny'   ).to(a   )

								snake_case_           =				prepare_img()
								snake_case_           =				processor(images=a			,     return_tensors='pt'   ).to(a   )

								with torch.no_grad():
															snake_case_           =				model(**a   )

								snake_case_           =				torch.Size((1, model.config.num_labels, 5_12, 5_12)   )
								self.assertEqual(outputs.logits.shape			,     a   )

								snake_case_           =				torch.tensor(
								    [[-8.8_110, -8.8_110, -8.6_521], [-8.8_110, -8.8_110, -8.6_521], [-8.7_746, -8.7_746, -8.6_130]]   ).to(a   )
								self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]			,     a			,     atol=1E-4   )   )













import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


lowercase    				=   random.Random()

if is_torch_available():
	import torch





def 		__UpperCAmelCase     (	a_				,	a_=1.0				,	a_=None				,	a_=None):
							if rng is None:
														snake_case_           =				global_rng

							snake_case_           =				[]
							for batch_idx in range(shape[0]):
														values.append([])
														for _ in range(shape[1]):
																					values[-1].append(rng.random() * scale)

							return values




class 					UpperCamelCase_ (     unittest.TestCase							):



	'''simple docstring'''




	def __init__(			self			,     a			,     a=7			,     a=4_00			,     a=20_00			,     a=1			,     a=0.0			,     a=1_60_00			,     a=True			,     a=True			,     )			->       Tuple:
								snake_case_           =				parent
								snake_case_           =				batch_size
								snake_case_           =				min_seq_length
								snake_case_           =				max_seq_length
								snake_case_           =				(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
								snake_case_           =				feature_size
								snake_case_           =				padding_value
								snake_case_           =				sampling_rate
								snake_case_           =				return_attention_mask
								snake_case_           =				do_normalize




	def 				_UpperCamelCase     (			self   )			->       Union[str, Any]:
								return {
								    "feature_size": self.feature_size,
								    "padding_value": self.padding_value,
								    "sampling_rate": self.sampling_rate,
								    "return_attention_mask": self.return_attention_mask,
								    "do_normalize": self.do_normalize,
								}






	def 				_UpperCamelCase     (			self			,     a=False			,     a=False   )			->       Optional[int]:
								def _flatten(a   ):
															return list(itertools.chain(*a   )   )

								if equal_length:
															snake_case_           =				floats_list((self.batch_size, self.max_seq_length)   )
								else:
															# make sure that inputs increase in size
															snake_case_           =				[
															    _flatten(floats_list((x, self.feature_size)   )   )
															    for x in range(self.min_seq_length			,     self.max_seq_length			,     self.seq_length_diff   )
															]

								if numpify:
															snake_case_           =				[np.asarray(a   ) for x in speech_inputs]

								return speech_inputs






@require_torch
@require_torchaudio
class 					UpperCamelCase_ (     snake_case_			,   unittest.TestCase							):



	'''simple docstring'''




	lowerCAmelCase						=					ASTFeatureExtractor




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




	def 				_UpperCamelCase     (			self   )			->       Union[str, Any]:
								# Tests that all call wrap to encode_plus and batch_encode_plus
								snake_case_           =				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
								# create three inputs of length 800, 1000, and 1200
								snake_case_           =				[floats_list((1, x)   )[0] for x in range(8_00			,     14_00			,     2_00   )]
								snake_case_           =				[np.asarray(a   ) for speech_input in speech_inputs]

								# Test not batched input
								snake_case_           =				feat_extract(speech_inputs[0]			,     return_tensors='np'   ).input_values
								snake_case_           =				feat_extract(np_speech_inputs[0]			,     return_tensors='np'   ).input_values
								self.assertTrue(np.allclose(a			,     a			,     atol=1E-3   )   )

								# Test batched
								snake_case_           =				feat_extract(a			,     padding=a			,     return_tensors='np'   ).input_values
								snake_case_           =				feat_extract(a			,     padding=a			,     return_tensors='np'   ).input_values
								for enc_seq_a, enc_seq_a in zip(a			,     a   ):
															self.assertTrue(np.allclose(a			,     a			,     atol=1E-3   )   )

								# Test 2-D numpy arrays are batched.
								snake_case_           =				[floats_list((1, x)   )[0] for x in (8_00, 8_00, 8_00)]
								snake_case_           =				np.asarray(a   )
								snake_case_           =				feat_extract(a			,     return_tensors='np'   ).input_values
								snake_case_           =				feat_extract(a			,     return_tensors='np'   ).input_values
								for enc_seq_a, enc_seq_a in zip(a			,     a   ):
															self.assertTrue(np.allclose(a			,     a			,     atol=1E-3   )   )




	@require_torch
	def 				_UpperCamelCase     (			self   )			->       List[str]:
								import torch

								snake_case_           =				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()   )
								snake_case_           =				np.random.rand(1_00   ).astype(np.floataa   )
								snake_case_           =				np_speech_inputs.tolist()

								for inputs in [py_speech_inputs, np_speech_inputs]:
															snake_case_           =				feature_extractor.pad([{'input_values': inputs}]			,     return_tensors='np'   )
															self.assertTrue(np_processed.input_values.dtype == np.floataa   )
															snake_case_           =				feature_extractor.pad([{'input_values': inputs}]			,     return_tensors='pt'   )
															self.assertTrue(pt_processed.input_values.dtype == torch.floataa   )




	def 				_UpperCamelCase     (			self			,     a   )			->       Tuple:
								from datasets import load_dataset

								snake_case_           =				load_dataset('hf-internal-testing/librispeech_asr_dummy'			,     'clean'			,     split='validation'   )
								# automatic decoding with librispeech
								snake_case_           =				ds.sort('id'   ).select(range(a   )   )[:num_samples]['audio']

								return [x["array"] for x in speech_samples]






	@require_torch
	def 				_UpperCamelCase     (			self   )			->       Optional[int]:
								# fmt: off
								snake_case_           =				torch.tensor(
								    [-0.9_894, -1.2_776, -0.9_066, -1.2_776, -0.9_349, -1.2_609, -1.0_386, -1.2_776,
								     -1.1_561, -1.2_776, -1.2_052, -1.2_723, -1.2_190, -1.2_132, -1.2_776, -1.1_133,
								     -1.1_953, -1.1_343, -1.1_584, -1.2_203, -1.1_770, -1.2_474, -1.2_381, -1.1_936,
								     -0.9_270, -0.8_317, -0.8_049, -0.7_706, -0.7_565, -0.7_869]   )
								# fmt: on

								snake_case_           =				self._load_datasamples(1   )
								snake_case_           =				ASTFeatureExtractor()
								snake_case_           =				feature_extractor(a			,     return_tensors='pt'   ).input_values
								self.assertEquals(input_values.shape			,     (1, 10_24, 1_28)   )
								self.assertTrue(torch.allclose(input_values[0, 0, :30]			,     a			,     atol=1E-4   )   )





