




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

						def __init__(		self   :int    , __snake_case   :Dict    , __snake_case   :Optional[Any]=13    , __snake_case   :Optional[int]=32    , __snake_case   :Any=3    , __snake_case   :Union[str, Any]=4    , __snake_case   :Tuple=[10, 20, 30, 40]    , __snake_case   :Dict=[2, 2, 3, 2]    , __snake_case   :Any=True    , __snake_case   :Optional[Any]=True    , __snake_case   :Any=37    , __snake_case   :List[str]="gelu"    , __snake_case   :str=10    , __snake_case   :Tuple=0.02    , __snake_case   :Any=["stage2", "stage3", "stage4"]    , __snake_case   :str=3    , __snake_case   :Any=None    , ):
										'''simple docstring'''

										__magic_name__	:	str								=parent
										__magic_name__	:	int								=batch_size
										__magic_name__	:	Any								=image_size
										__magic_name__	:	Optional[int]								=num_channels
										__magic_name__	:	Optional[int]								=num_stages
										__magic_name__	:	Any								=hidden_sizes
										__magic_name__	:	Any								=depths
										__magic_name__	:	Union[str, Any]								=is_training
										__magic_name__	:	Optional[int]								=use_labels
										__magic_name__	:	Optional[int]								=intermediate_size
										__magic_name__	:	Any								=hidden_act
										__magic_name__	:	Any								=type_sequence_label_size
										__magic_name__	:	str								=initializer_range
										__magic_name__	:	Union[str, Any]								=out_features
										__magic_name__	:	int								=num_labels
										__magic_name__	:	List[str]								=scope
										__magic_name__	:	Any								=num_stages

						def     A__						(		self   :int ):
										'''simple docstring'''

										__magic_name__	:	Dict								=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

										__magic_name__	:	str								=None
										if self.use_labels:
														__magic_name__	:	Any								=ids_tensor([self.batch_size]    , self.type_sequence_label_size )

										__magic_name__	:	int								=self.get_config()

										return config, pixel_values, labels

						def     A__						(		self   :Dict ):
										'''simple docstring'''

										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     A__						(		self   :Optional[Any] ):
										'''simple docstring'''

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

						def     A__						(		self   :str    , __snake_case   :Tuple    , __snake_case   :str    , __snake_case   :Dict ):
										'''simple docstring'''

										__magic_name__	:	Any								=UperNetForSemanticSegmentation(config=lowercase_ )
										model.to(lowercase_ )
										model.eval()
										__magic_name__	:	Optional[int]								=model(lowercase_ )
										self.parent.assertEqual(
										    result.logits.shape    , (self.batch_size, self.num_labels, self.image_size, self.image_size) )


						def     A__						(		self   :Dict ):
										'''simple docstring'''

										__magic_name__	:	Tuple								=self.prepare_config_and_inputs()
										(
										    __magic_name__ 
										)	:	str								=config_and_inputs
										__magic_name__	:	str								={"pixel_values": pixel_values}
										return config, inputs_dict




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

						def     A__						(		self   :List[Any] ):
										'''simple docstring'''

										__magic_name__	:	List[Any]								=UperNetModelTester(self )
										__magic_name__	:	Dict								=ConfigTester(self    , config_class=lowercase_    , has_text_modality=lowercase_    , hidden_size=37 )

						def     A__						(		self   :List[str] ):
										'''simple docstring'''

										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     A__						(		self   :Union[str, Any] ):
										'''simple docstring'''

										return

						def     A__						(		self   :str ):
										'''simple docstring'''

										__magic_name__	:	List[Any]								=self.model_tester.prepare_config_and_inputs_for_common()

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

														__magic_name__	:	Union[str, Any]								=["pixel_values"]
														self.assertListEqual(arg_names[:1]    , lowercase_ )

						def     A__						(		self   :int ):
										'''simple docstring'''

										__magic_name__	:	List[str]								=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_semantic_segmentation(*lowercase_ )

						@unittest.skip(reason="""UperNet does not use inputs_embeds""" )
						def     A__						(		self   :Union[str, Any] ):
										'''simple docstring'''

										pass

						@unittest.skip(reason="""UperNet does not support input and output embeddings""" )
						def     A__						(		self   :Union[str, Any] ):
										'''simple docstring'''

										pass

						@unittest.skip(reason="""UperNet does not have a base model""" )
						def     A__						(		self   :int ):
										'''simple docstring'''

										pass

						@unittest.skip(reason="""UperNet does not have a base model""" )
						def     A__						(		self   :int ):
										'''simple docstring'''

										pass

						@require_torch_multi_gpu
						@unittest.skip(reason="""UperNet has some layers using `add_module` which doesn't work well with `nn.DataParallel`""" )
						def     A__						(		self   :Tuple ):
										'''simple docstring'''

										pass

						@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" )
						def     A__						(		self   :Any ):
										'''simple docstring'''

										pass

						def     A__						(		self   :List[Any] ):
										'''simple docstring'''

										def check_hidden_states_output(__snake_case   :Optional[Any]    , __snake_case   :Union[str, Any]    , __snake_case   :Tuple ):
														__magic_name__	:	Optional[Any]								=model_class(lowercase_ )
														model.to(lowercase_ )
														model.eval()

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

														__magic_name__	:	List[Any]								=outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

														__magic_name__	:	Optional[int]								=self.model_tester.num_stages
														self.assertEqual(len(lowercase_ )    , 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]    , )

										__magic_name__	:	List[str]								=self.model_tester.prepare_config_and_inputs_for_common()

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

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

														check_hidden_states_output(lowercase_    , lowercase_    , lowercase_ )

						def     A__						(		self   :str ):
										'''simple docstring'''

										__magic_name__	:	Any								=self.model_tester.prepare_config_and_inputs_for_common()

										__magic_name__	:	Tuple								=_config_zero_init(lowercase_ )
										__magic_name__	:	Optional[Any]								=_config_zero_init(configs_no_init.backbone_config )
										for model_class in self.all_model_classes:
														__magic_name__	:	str								=model_class(config=lowercase_ )
														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     A__						(		self   :Optional[Any] ):
										'''simple docstring'''

										pass


						@slow
						def     A__						(		self   :Union[str, Any] ):
										'''simple docstring'''

										for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														__magic_name__	:	List[str]								=UperNetForSemanticSegmentation.from_pretrained(lowercase_ )
														self.assertIsNotNone(lowercase_ )




def 		lowerCAmelCase_   (	):
				__magic_name__	:	int								=hf_hub_download(
				    repo_id="""hf-internal-testing/fixtures_ade20k"""	,     repo_type="""dataset"""	,     filename="""ADE_val_00000001.jpg"""   )
				__magic_name__	:	str								=Image.open(__SCREAMING_SNAKE_CASE   ).convert("""RGB"""   )
				return image




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

						def     A__						(		self   :Optional[Any] ):
										'''simple docstring'''

										__magic_name__	:	Tuple								=AutoImageProcessor.from_pretrained("""openmmlab/upernet-swin-tiny""" )
										__magic_name__	:	Optional[int]								=UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-swin-tiny""" ).to(lowercase_ )

										__magic_name__	:	List[Any]								=prepare_img()
										__magic_name__	:	Optional[int]								=processor(images=lowercase_    , return_tensors="""pt""" ).to(lowercase_ )

										with torch.no_grad():
														__magic_name__	:	Dict								=model(**lowercase_ )

										__magic_name__	:	List[Any]								=torch.Size((1, model.config.num_labels, 5_12, 5_12) )
										self.assertEqual(outputs.logits.shape    , lowercase_ )

										__magic_name__	:	str								=torch.tensor(
										    [[-7.5958, -7.5958, -7.4302], [-7.5958, -7.5958, -7.4302], [-7.4797, -7.4797, -7.3068]] ).to(lowercase_ )
										self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]    , lowercase_    , atol=1E-4 ) )


						def     A__						(		self   :Dict ):
										'''simple docstring'''

										__magic_name__	:	int								=AutoImageProcessor.from_pretrained("""openmmlab/upernet-convnext-tiny""" )
										__magic_name__	:	Tuple								=UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-convnext-tiny""" ).to(lowercase_ )

										__magic_name__	:	Optional[Any]								=prepare_img()
										__magic_name__	:	Optional[int]								=processor(images=lowercase_    , return_tensors="""pt""" ).to(lowercase_ )

										with torch.no_grad():
														__magic_name__	:	Union[str, Any]								=model(**lowercase_ )

										__magic_name__	:	Any								=torch.Size((1, model.config.num_labels, 5_12, 5_12) )
										self.assertEqual(outputs.logits.shape    , lowercase_ )

										__magic_name__	:	List[str]								=torch.tensor(
										    [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]] ).to(lowercase_ )
										self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]    , lowercase_    , atol=1E-4 ) )










import json
import os
import unittest

from transformers.models.blenderbot_small.tokenization_blenderbot_small import (
    VOCAB_FILES_NAMES,
    BlenderbotSmallTokenizer,
)

from ...test_tokenization_common import TokenizerTesterMixin




class 		__A      (			UpperCamelCase__					,						unittest.TestCase			):
						UpperCamelCase     						=					BlenderbotSmallTokenizer
						UpperCamelCase     						=					False

						def     A__						(		self   :Dict ):
										'''simple docstring'''

										super().setUp()

										__magic_name__	:	Tuple								=["""__start__""", """adapt""", """act""", """ap@@""", """te""", """__end__""", """__unk__"""]
										__magic_name__	:	Optional[Any]								=dict(zip(__snake_case    , range(len(__snake_case ) ) ) )

										__magic_name__	:	List[Any]								=["""#version: 0.2""", """a p""", """t e</w>""", """ap t</w>""", """a d""", """ad apt</w>""", """a c""", """ac t</w>""", """"""]
										__magic_name__	:	int								={"""unk_token""": """__unk__""", """bos_token""": """__start__""", """eos_token""": """__end__"""}

										__magic_name__	:	Dict								=os.path.join(self.tmpdirname    , VOCAB_FILES_NAMES["""vocab_file"""] )
										__magic_name__	:	int								=os.path.join(self.tmpdirname    , VOCAB_FILES_NAMES["""merges_file"""] )
										with open(self.vocab_file    , """w"""    , encoding="""utf-8""" ) as fp:
														fp.write(json.dumps(__snake_case ) + """\n""" )
										with open(self.merges_file    , """w"""    , encoding="""utf-8""" ) as fp:
														fp.write("""\n""".join(__snake_case ) )

						def     A__						(		self   :Union[str, Any]    , **__snake_case   :Optional[int] ):
										'''simple docstring'''

										kwargs.update(self.special_tokens_map )
										return BlenderbotSmallTokenizer.from_pretrained(self.tmpdirname    , **__snake_case )

						def     A__						(		self   :str    , __snake_case   :List[Any] ):
										'''simple docstring'''

										__magic_name__	:	Any								="""adapt act apte"""
										__magic_name__	:	str								="""adapt act apte"""
										return input_text, output_text

						def     A__						(		self   :List[str] ):
										'''simple docstring'''

										__magic_name__	:	Tuple								=BlenderbotSmallTokenizer(self.vocab_file    , self.merges_file    , **self.special_tokens_map )
										__magic_name__	:	List[str]								="""adapt act apte"""
										__magic_name__	:	Optional[int]								=["""adapt""", """act""", """ap@@""", """te"""]
										__magic_name__	:	List[Any]								=tokenizer.tokenize(__snake_case )
										self.assertListEqual(__snake_case    , __snake_case )

										__magic_name__	:	int								=[tokenizer.bos_token] + tokens + [tokenizer.eos_token]

										__magic_name__	:	Any								=[0, 1, 2, 3, 4, 5]
										self.assertListEqual(tokenizer.convert_tokens_to_ids(__snake_case )    , __snake_case )

						def     A__						(		self   :Optional[int] ):
										'''simple docstring'''

										__magic_name__	:	Any								=BlenderbotSmallTokenizer.from_pretrained("""facebook/blenderbot-90M""" )
										assert tok("""sam""" ).input_ids == [13_84]
										__magic_name__	:	Tuple								="""I am a small frog."""
										__magic_name__	:	Tuple								=tok([src_text]    , padding=__snake_case    , truncation=__snake_case )["""input_ids"""]
										__magic_name__	:	Optional[Any]								=tok.batch_decode(__snake_case    , skip_special_tokens=__snake_case    , clean_up_tokenization_spaces=__snake_case )[0]
										assert src_text != decoded  # I wish it did!
										assert decoded == "i am a small frog ."


						def     A__						(		self   :Optional[Any] ):
										'''simple docstring'''

										__magic_name__	:	List[Any]								=BlenderbotSmallTokenizer.from_pretrained("""facebook/blenderbot-90M""" )
										__magic_name__	:	int								="""I am a small frog ."""
										__magic_name__	:	List[Any]								="""."""
										__magic_name__	:	List[str]								=tok(__snake_case )["""input_ids"""]
										__magic_name__	:	List[str]								=tok(__snake_case )["""input_ids"""]

										assert encoded[-1] == encoded_dot[0]




