






'''simple docstring'''







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 	SCREAMING_SNAKE_CASE					:






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






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			int:
							a_		:  List[Any]											=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

							a_		:  Optional[int]											=      None
							if self.use_labels:
											a_		:  Optional[Any]											=      ids_tensor([self.batch_size]  ,						self.type_sequence_label_size					)

							a_		:  Optional[Any]											=      self.get_config()

							return config, pixel_values, labels






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			Dict:
							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 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			Tuple:
							return UperNetConfig(
							    backbone_config=self.get_backbone_config()  ,						hidden_size=512  ,						pool_scales=[1, 2, 3, 6]  ,						use_auxiliary_head=__SCREAMING_SNAKE_CASE  ,						auxiliary_loss_weight=0.4  ,						auxiliary_in_channels=40  ,						auxiliary_channels=256  ,						auxiliary_num_convs=1  ,						auxiliary_concat_input=__SCREAMING_SNAKE_CASE  ,						loss_ignore_index=255  ,						num_labels=self.num_labels  ,						)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]					)			->			str:
							a_		:  str											=      UperNetForSemanticSegmentation(config=__SCREAMING_SNAKE_CASE					)
							model.to(__SCREAMING_SNAKE_CASE					)
							model.eval()
							a_		:  List[Any]											=      model(__SCREAMING_SNAKE_CASE					)
							self.parent.assertEqual(
							    result.logits.shape  ,						(self.batch_size, self.num_labels, self.image_size, self.image_size)					)







			def 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]					)			->			Union[str, Any]:
							a_		:  Any											=      self.prepare_config_and_inputs()
							(
							    (
							    a_
							)				,			(
							    a_
							)				,			(
							    a_
							)				,			
							)		:  Dict											=      config_and_inputs
							a_		:  Tuple											=      {'''pixel_values''': pixel_values}
							return config, inputs_dict





@require_torch
class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_	,   SCREAMING_SNAKE_CASE_	,   unittest.TestCase       ):
			snake_case__  =				(UperNetForSemanticSegmentation,) if is_torch_available() else ()
			snake_case__  =				{"image-segmentation": UperNetForSemanticSegmentation} if is_torch_available() else {}
			snake_case__  =				False
			snake_case__  =				False
			snake_case__  =				False
			snake_case__  =				False
			snake_case__  =				False
			snake_case__  =				False






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			str:
							a_		:  Union[str, Any]											=      UperNetModelTester(self					)
							a_		:  Dict											=      ConfigTester(self  ,						config_class=__SCREAMING_SNAKE_CASE  ,						has_text_modality=__SCREAMING_SNAKE_CASE  ,						hidden_size=37					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			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 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			Optional[int]:
							return






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			str:
							a_				,			a_		:  Optional[int]											=      self.model_tester.prepare_config_and_inputs_for_common()

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

											a_		:  Optional[Any]											=      ['''pixel_values''']
											self.assertListEqual(arg_names[:1]  ,						__SCREAMING_SNAKE_CASE					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			Optional[Any]:
							a_		:  Optional[Any]											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_semantic_segmentation(*__SCREAMING_SNAKE_CASE					)






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






			@unittest.skip(reason='''UperNet does not support input and output embeddings'''					)
			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			Dict:
							pass






			@unittest.skip(reason='''UperNet does not have a base model'''					)
			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			Tuple:
							pass






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






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






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






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]					)			->			str:
							def check_hidden_states_output(__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			List[Any]					):
											a_		:  int											=      model_class(__SCREAMING_SNAKE_CASE					)
											model.to(__SCREAMING_SNAKE_CASE					)
											model.eval()

											with torch.no_grad():
															a_		:  Any											=      model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)					)

											a_		:  List[str]											=      outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

											a_		:  List[Any]											=      self.model_tester.num_stages
											self.assertEqual(len(__SCREAMING_SNAKE_CASE					)  ,						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]  ,						)

							a_				,			a_		:  int											=      self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
											a_		:  Union[str, Any]											=      True
											check_hidden_states_output(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)

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

											check_hidden_states_output(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			int:
							a_				,			a_		:  Tuple											=      self.model_tester.prepare_config_and_inputs_for_common()

							a_		:  Tuple											=      _config_zero_init(__SCREAMING_SNAKE_CASE					)
							a_		:  Any											=      _config_zero_init(configs_no_init.backbone_config					)
							for model_class in self.all_model_classes:
											a_		:  Optional[Any]											=      model_class(config=__SCREAMING_SNAKE_CASE					)
											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 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			Tuple:
							pass







			@slow
			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			Dict:
							for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											a_		:  Optional[Any]											=      UperNetForSemanticSegmentation.from_pretrained(__SCREAMING_SNAKE_CASE					)
											self.assertIsNotNone(__SCREAMING_SNAKE_CASE					)





def 		_UpperCAmelCase   (							):
				a_		:  List[str]											=      hf_hub_download(
				    repo_id='''hf-internal-testing/fixtures_ade20k'''						,		repo_type='''dataset'''						,		filename='''ADE_val_00000001.jpg'''      )
				a_		:  Tuple											=      Image.open(__A      ).convert('''RGB'''      )
				return image





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






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]					)			->			str:
							a_		:  int											=      AutoImageProcessor.from_pretrained('''openmmlab/upernet-swin-tiny'''					)
							a_		:  List[Any]											=      UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-swin-tiny'''					).to(__SCREAMING_SNAKE_CASE					)

							a_		:  Union[str, Any]											=      prepare_img()
							a_		:  Union[str, Any]											=      processor(images=__SCREAMING_SNAKE_CASE  ,						return_tensors='''pt'''					).to(__SCREAMING_SNAKE_CASE					)

							with torch.no_grad():
											a_		:  Tuple											=      model(**__SCREAMING_SNAKE_CASE					)

							a_		:  Union[str, Any]											=      torch.Size((1, model.config.num_labels, 512, 512)					)
							self.assertEqual(outputs.logits.shape  ,						__SCREAMING_SNAKE_CASE					)

							a_		:  Union[str, Any]											=      torch.tensor(
							    [[-7.5958, -7.5958, -7.4302], [-7.5958, -7.5958, -7.4302], [-7.4797, -7.4797, -7.3068]]					).to(__SCREAMING_SNAKE_CASE					)
							self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]  ,						__SCREAMING_SNAKE_CASE  ,						atol=1e-4					)					)







			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			str:
							a_		:  int											=      AutoImageProcessor.from_pretrained('''openmmlab/upernet-convnext-tiny'''					)
							a_		:  Union[str, Any]											=      UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-convnext-tiny'''					).to(__SCREAMING_SNAKE_CASE					)

							a_		:  int											=      prepare_img()
							a_		:  str											=      processor(images=__SCREAMING_SNAKE_CASE  ,						return_tensors='''pt'''					).to(__SCREAMING_SNAKE_CASE					)

							with torch.no_grad():
											a_		:  Optional[int]											=      model(**__SCREAMING_SNAKE_CASE					)

							a_		:  List[Any]											=      torch.Size((1, model.config.num_labels, 512, 512)					)
							self.assertEqual(outputs.logits.shape  ,						__SCREAMING_SNAKE_CASE					)

							a_		:  str											=      torch.tensor(
							    [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]]					).to(__SCREAMING_SNAKE_CASE					)
							self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]  ,						__SCREAMING_SNAKE_CASE  ,						atol=1e-4					)					)












'''simple docstring'''







from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional

from packaging import version


if TYPE_CHECKING:
		from ... import PreTrainedTokenizer, TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import is_torch_available, logging


__lowerCAmelCase       		=      logging.get_logger(__name__)

__lowerCAmelCase       		=      {
    'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/resolve/main/config.json',
    'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/config.json',
    'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json',
    'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json',
    'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/config.json',
    'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json',
}





class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_       ):
			snake_case__  =				"bloom"
			snake_case__  =				["past_key_values"]
			snake_case__  =				{
			    "num_hidden_layers": "n_layer",
			    "num_attention_heads": "n_head",
			}






			def __init__( self      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			int=25_0880  ,						__SCREAMING_SNAKE_CASE      :			Dict=64  ,						__SCREAMING_SNAKE_CASE      :			Tuple=2  ,						__SCREAMING_SNAKE_CASE      :			int=8  ,						__SCREAMING_SNAKE_CASE      :			Any=1e-5  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=0.02  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]=True  ,						__SCREAMING_SNAKE_CASE      :			int=1  ,						__SCREAMING_SNAKE_CASE      :			Any=2  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=False  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=0.0  ,						__SCREAMING_SNAKE_CASE      :			str=0.0  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=1  ,						__SCREAMING_SNAKE_CASE      :			List[str]=False  ,						**__SCREAMING_SNAKE_CASE      :			str  ,						)			->			Any:
							a_		:  Optional[int]											=      vocab_size
							# Backward compatibility with n_embed kwarg
							a_		:  Any											=      kwargs.pop('''n_embed'''  ,						__SCREAMING_SNAKE_CASE					)
							a_		:  Optional[int]											=      hidden_size if n_embed is None else n_embed
							a_		:  int											=      n_layer
							a_		:  str											=      n_head
							a_		:  Optional[int]											=      layer_norm_epsilon
							a_		:  Dict											=      initializer_range
							a_		:  List[str]											=      use_cache
							a_		:  Dict											=      pretraining_tp
							a_		:  Optional[Any]											=      apply_residual_connection_post_layernorm
							a_		:  Optional[Any]											=      hidden_dropout
							a_		:  List[str]											=      attention_dropout

							a_		:  Dict											=      bos_token_id
							a_		:  Optional[int]											=      eos_token_id
							a_		:  Any											=      slow_but_exact

							super().__init__(bos_token_id=__SCREAMING_SNAKE_CASE  ,						eos_token_id=__SCREAMING_SNAKE_CASE  ,						**__SCREAMING_SNAKE_CASE					)





class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_       ):
			snake_case__  =				version.parse("1.12"       )






			def __init__( self      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			PretrainedConfig  ,						__SCREAMING_SNAKE_CASE      :			str = "default"  ,						__SCREAMING_SNAKE_CASE      :			List[PatchingSpec] = None  ,						__SCREAMING_SNAKE_CASE      :			bool = False  ,						)			->			Optional[Any]:
							super().__init__(__SCREAMING_SNAKE_CASE  ,						task=__SCREAMING_SNAKE_CASE  ,						patching_specs=__SCREAMING_SNAKE_CASE  ,						use_past=__SCREAMING_SNAKE_CASE					)
							if not getattr(self._config  ,						'''pad_token_id'''  ,						__SCREAMING_SNAKE_CASE					):
											# TODO: how to do that better?
											a_		:  Tuple											=      0






			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			Mapping[str, Mapping[int, str]]:
							a_		:  Optional[Any]											=      OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}}					)
							if self.use_past:
											# BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
											self.fill_with_past_key_values_(__SCREAMING_SNAKE_CASE  ,						direction='''inputs'''  ,						inverted_values_shape=__SCREAMING_SNAKE_CASE					)
											a_		:  Union[str, Any]											=      {0: '''batch''', 1: '''past_sequence + sequence'''}
							else:
											a_		:  Union[str, Any]											=      {0: '''batch''', 1: '''sequence'''}

							return common_inputs






			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			int:
							return self._config.n_layer






			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			int:
							return self._config.n_head






			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			float:
							return 1e-3






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			"PreTrainedTokenizer"  ,						__SCREAMING_SNAKE_CASE      :			int = -1  ,						__SCREAMING_SNAKE_CASE      :			int = -1  ,						__SCREAMING_SNAKE_CASE      :			bool = False  ,						__SCREAMING_SNAKE_CASE      :			Optional["TensorType"] = None  ,						)			->			Mapping[str, Any]:
							a_		:  Dict											=      super(__SCREAMING_SNAKE_CASE  ,						self					).generate_dummy_inputs(
							    __SCREAMING_SNAKE_CASE  ,						batch_size=__SCREAMING_SNAKE_CASE  ,						seq_length=__SCREAMING_SNAKE_CASE  ,						is_pair=__SCREAMING_SNAKE_CASE  ,						framework=__SCREAMING_SNAKE_CASE					)

							# We need to order the input in the way they appears in the forward()
							a_		:  Union[str, Any]											=      OrderedDict({'''input_ids''': common_inputs['''input_ids''']}					)

							# Need to add the past_keys
							if self.use_past:
											if not is_torch_available():
															raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''					)
											else:
															import torch

															a_				,			a_		:  Any											=      common_inputs['''input_ids'''].shape
															# Not using the same length for past_key_values
															a_		:  str											=      seqlen + 2
															a_		:  Any											=      self._config.hidden_size // self.num_attention_heads
															a_		:  Optional[int]											=      (
															    batch * self.num_attention_heads,
															    head_dim,
															    past_key_values_length,
															)
															a_		:  Any											=      (
															    batch * self.num_attention_heads,
															    past_key_values_length,
															    head_dim,
															)
															a_		:  List[str]											=      [
															    (torch.zeros(__SCREAMING_SNAKE_CASE					), torch.zeros(__SCREAMING_SNAKE_CASE					)) for _ in range(self.num_layers					)
															]

							a_		:  Union[str, Any]											=      common_inputs['''attention_mask''']
							if self.use_past:
											a_		:  Optional[int]											=      ordered_inputs['''attention_mask'''].dtype
											a_		:  List[Any]											=      torch.cat(
											    [ordered_inputs['''attention_mask'''], torch.ones(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						dtype=__SCREAMING_SNAKE_CASE					)]  ,						dim=1					)

							return ordered_inputs







			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]					)			->			int:
							return 13




