

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






					"""simple docstring"""



					def __init__(       self			:				str   ,lowercase_			:				Optional[Any]   ,lowercase_			:				List[str]=1_3   ,lowercase_			:				Tuple=3_2   ,lowercase_			:				List[Any]=3   ,lowercase_			:				int=4   ,lowercase_			:				List[Any]=[1_0, 2_0, 3_0, 4_0]   ,lowercase_			:				Optional[int]=[2, 2, 3, 2]   ,lowercase_			:				int=True   ,lowercase_			:				Dict=True   ,lowercase_			:				Tuple=3_7   ,lowercase_			:				int="gelu"   ,lowercase_			:				Union[str, Any]=1_0   ,lowercase_			:				Tuple=0.02   ,lowercase_			:				Dict=["stage2", "stage3", "stage4"]   ,lowercase_			:				Tuple=3   ,lowercase_			:				Optional[int]=None   ,):
						lowerCAmelCase__	:						Any  				=	parent
						lowerCAmelCase__	:						List[str]  				=	batch_size
						lowerCAmelCase__	:						Union[str, Any]  				=	image_size
						lowerCAmelCase__	:						List[str]  				=	num_channels
						lowerCAmelCase__	:						Any  				=	num_stages
						lowerCAmelCase__	:						Union[str, Any]  				=	hidden_sizes
						lowerCAmelCase__	:						Optional[int]  				=	depths
						lowerCAmelCase__	:						List[Any]  				=	is_training
						lowerCAmelCase__	:						Optional[Any]  				=	use_labels
						lowerCAmelCase__	:						List[Any]  				=	intermediate_size
						lowerCAmelCase__	:						str  				=	hidden_act
						lowerCAmelCase__	:						Union[str, Any]  				=	type_sequence_label_size
						lowerCAmelCase__	:						Any  				=	initializer_range
						lowerCAmelCase__	:						Optional[int]  				=	out_features
						lowerCAmelCase__	:						Tuple  				=	num_labels
						lowerCAmelCase__	:						Tuple  				=	scope
						lowerCAmelCase__	:						Tuple  				=	num_stages







					def        __lowerCAmelCase (       self			:				List[Any]		):
						lowerCAmelCase__	:						Any  				=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

						lowerCAmelCase__	:						List[Any]  				=	None
						if self.use_labels:
							lowerCAmelCase__	:						Dict  				=	ids_tensor([self.batch_size]   ,self.type_sequence_label_size		)

						lowerCAmelCase__	:						Union[str, Any]  				=	self.get_config()

						return config, pixel_values, labels







					def        __lowerCAmelCase (       self			:				Tuple		):
						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        __lowerCAmelCase (       self			:				Dict		):
						return UperNetConfig(
						    backbone_config=self.get_backbone_config()   ,hidden_size=5_1_2   ,pool_scales=[1, 2, 3, 6]   ,use_auxiliary_head=lowercase_   ,auxiliary_loss_weight=0.4   ,auxiliary_in_channels=4_0   ,auxiliary_channels=2_5_6   ,auxiliary_num_convs=1   ,auxiliary_concat_input=lowercase_   ,loss_ignore_index=2_5_5   ,num_labels=self.num_labels   ,)







					def        __lowerCAmelCase (       self			:				int   ,lowercase_			:				Tuple   ,lowercase_			:				Tuple   ,lowercase_			:				Optional[int]		):
						lowerCAmelCase__	:						Tuple  				=	UperNetForSemanticSegmentation(config=lowercase_		)
						model.to(lowercase_		)
						model.eval()
						lowerCAmelCase__	:						Dict  				=	model(lowercase_		)
						self.parent.assertEqual(
						    result.logits.shape   ,(self.batch_size, self.num_labels, self.image_size, self.image_size)		)







					def        __lowerCAmelCase (       self			:				Any		):
						lowerCAmelCase__	:						Optional[int]  				=	self.prepare_config_and_inputs()
						(
						    (
						    lowerCAmelCase__
						)				,(
						    lowerCAmelCase__
						)				,(
						    lowerCAmelCase__
						)				,
						)	:						Any  				=	config_and_inputs
						lowerCAmelCase__	:						Any  				=	{'''pixel_values''': pixel_values}
						return config, inputs_dict







@require_torch
class 				SCREAMING_SNAKE_CASE					(		a_    ,							a_    ,							unittest.TestCase       ):






					"""simple docstring"""



					lowercase__				 =       (UperNetForSemanticSegmentation,) if is_torch_available() else ()
					lowercase__				 =       {"image-segmentation": UperNetForSemanticSegmentation} if is_torch_available() else {}
					lowercase__				 =       False
					lowercase__				 =       False
					lowercase__				 =       False
					lowercase__				 =       False
					lowercase__				 =       False
					lowercase__				 =       False







					def        __lowerCAmelCase (       self			:				str		):
						lowerCAmelCase__	:						str  				=	UperNetModelTester(self		)
						lowerCAmelCase__	:						Any  				=	ConfigTester(self   ,config_class=lowercase_   ,has_text_modality=lowercase_   ,hidden_size=3_7		)







					def        __lowerCAmelCase (       self			:				Dict		):
						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        __lowerCAmelCase (       self			:				Any		):
						return







					def        __lowerCAmelCase (       self			:				Any		):
						lowerCAmelCase__				,lowerCAmelCase__	:						List[Any]  				=	self.model_tester.prepare_config_and_inputs_for_common()

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

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







					def        __lowerCAmelCase (       self			:				Any		):
						lowerCAmelCase__	:						List[Any]  				=	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        __lowerCAmelCase (       self			:				Optional[Any]		):
						pass







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







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







					@unittest.skip(reason='''UperNet does not have a base model'''		)
					def        __lowerCAmelCase (       self			:				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        __lowerCAmelCase (       self			:				List[Any]		):
						pass







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







					def        __lowerCAmelCase (       self			:				Optional[Any]		):
						def check_hidden_states_output(lowercase_			:				Union[str, Any]   ,lowercase_			:				List[Any]   ,lowercase_			:				str		):
							lowerCAmelCase__	:						Optional[Any]  				=	model_class(lowercase_		)
							model.to(lowercase_		)
							model.eval()

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

							lowerCAmelCase__	:						Dict  				=	outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

							lowerCAmelCase__	:						Union[str, Any]  				=	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]   ,)

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

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

							# check that output_hidden_states also work using config
							del inputs_dict["output_hidden_states"]
							lowerCAmelCase__	:						Union[str, Any]  				=	True

							check_hidden_states_output(lowercase_   ,lowercase_   ,lowercase_		)







					def        __lowerCAmelCase (       self			:				Union[str, Any]		):
						lowerCAmelCase__				,lowerCAmelCase__	:						str  				=	self.model_tester.prepare_config_and_inputs_for_common()

						lowerCAmelCase__	:						Dict  				=	_config_zero_init(lowercase_		)
						lowerCAmelCase__	:						str  				=	_config_zero_init(configs_no_init.backbone_config		)
						for model_class in self.all_model_classes:
							lowerCAmelCase__	:						Any  				=	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        __lowerCAmelCase (       self			:				str		):
						pass







					@slow
					def        __lowerCAmelCase (       self			:				Any		):
						for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							lowerCAmelCase__	:						Union[str, Any]  				=	UperNetForSemanticSegmentation.from_pretrained(lowercase_		)
							self.assertIsNotNone(lowercase_		)

def   __SCREAMING_SNAKE_CASE  (      ):
	lowerCAmelCase__	:						int  				=	hf_hub_download(
	    repo_id='''hf-internal-testing/fixtures_ade20k'''						,     repo_type='''dataset'''						,     filename='''ADE_val_00000001.jpg''' )
	lowerCAmelCase__	:						Union[str, Any]  				=	Image.open(A_ ).convert('''RGB''' )
	return image







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






					"""simple docstring"""



					def        __lowerCAmelCase (       self			:				Union[str, Any]		):
						lowerCAmelCase__	:						Any  				=	AutoImageProcessor.from_pretrained('''openmmlab/upernet-swin-tiny'''		)
						lowerCAmelCase__	:						Tuple  				=	UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-swin-tiny'''		).to(lowercase_		)

						lowerCAmelCase__	:						str  				=	prepare_img()
						lowerCAmelCase__	:						Optional[int]  				=	processor(images=lowercase_   ,return_tensors='''pt'''		).to(lowercase_		)

						with torch.no_grad():
							lowerCAmelCase__	:						int  				=	model(**lowercase_		)

						lowerCAmelCase__	:						Tuple  				=	torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2)		)
						self.assertEqual(outputs.logits.shape   ,lowercase_		)

						lowerCAmelCase__	:						Tuple  				=	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        __lowerCAmelCase (       self			:				Optional[Any]		):
						lowerCAmelCase__	:						Any  				=	AutoImageProcessor.from_pretrained('''openmmlab/upernet-convnext-tiny'''		)
						lowerCAmelCase__	:						List[str]  				=	UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-convnext-tiny'''		).to(lowercase_		)

						lowerCAmelCase__	:						Dict  				=	prepare_img()
						lowerCAmelCase__	:						Tuple  				=	processor(images=lowercase_   ,return_tensors='''pt'''		).to(lowercase_		)

						with torch.no_grad():
							lowerCAmelCase__	:						List[Any]  				=	model(**lowercase_		)

						lowerCAmelCase__	:						Union[str, Any]  				=	torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2)		)
						self.assertEqual(outputs.logits.shape   ,lowercase_		)

						lowerCAmelCase__	:						Optional[Any]  				=	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		)		)







"""simple docstring"""





# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os

from accelerate.test_utils import execute_subprocess_async

def   __SCREAMING_SNAKE_CASE  (      A_=None ):
	if subparsers is not None:
		lowerCAmelCase__	:						Optional[Any]  				=	subparsers.add_parser('''test''' )
	else:
		lowerCAmelCase__	:						List[str]  				=	argparse.ArgumentParser('''Accelerate test command''' )

	parser.add_argument(
	    '''--config_file'''						,     default=A_						,     help=(
	        '''The path to use to store the config file. Will default to a file named default_config.yaml in the cache '''
	        '''location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have '''
	        '''such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed '''
	        '''with \'huggingface\'.'''
	    )						,     )

	if subparsers is not None:
		parser.set_defaults(func=A_ )
	return parser

def   __SCREAMING_SNAKE_CASE  (      A_ ):
	lowerCAmelCase__	:						Optional[int]  				=	os.path.sep.join(__file__.split(os.path.sep )[:-2] + ['''test_utils''', '''scripts''', '''test_script.py'''] )

	if args.config_file is None:
		lowerCAmelCase__	:						Optional[Any]  				=	script_name
	else:
		lowerCAmelCase__	:						Any  				=	f'--config_file={args.config_file} {script_name}'

	lowerCAmelCase__	:						List[Any]  				=	['''accelerate-launch'''] + test_args.split()
	lowerCAmelCase__	:						int  				=	execute_subprocess_async(A_						,     env=os.environ.copy() )
	if result.returncode == 0:
		print('''Test is a success! You are ready for your distributed training!''' )

def   __SCREAMING_SNAKE_CASE  (      ):
	lowerCAmelCase__	:						Any  				=	test_command_parser()
	lowerCAmelCase__	:						List[Any]  				=	parser.parse_args()
	test_command(A_ )


if __name__ == "__main__":
							main()




