
"""simple docstring"""





import logging
import os
import sys
from dataclasses import dataclass, field
from typing import Optional

from seqaseq_trainer import SeqaSeqTrainer
from seqaseq_training_args import SeqaSeqTrainingArguments

import transformers
from transformers import (
    AutoConfig,
    AutoModelForSeqaSeqLM,
    AutoTokenizer,
    HfArgumentParser,
    MBartTokenizer,
    MBartTokenizerFast,
    set_seed,
)
from transformers.trainer_utils import EvaluationStrategy, is_main_process
from transformers.training_args import ParallelMode
from utils import (
    SeqaSeqDataCollator,
    SeqaSeqDataset,
    assert_all_frozen,
    build_compute_metrics_fn,
    check_output_dir,
    freeze_embeds,
    freeze_params,
    lmap,
    save_json,
    use_task_specific_params,
    write_txt_file,
)


_a								=						logging.getLogger(__name__)






@dataclass
class        _UpperCAmelCase:
		lowercase__         = field(
		    metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'}      )
		lowercase__         = field(
		    default=lowerCamelCase ,	metadata={'help': 'Pretrained config name or path if not the same as model_name'}      )
		lowercase__         = field(
		    default=lowerCamelCase ,	metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'}      )
		lowercase__         = field(
		    default=lowerCamelCase ,	metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} ,	)
		lowercase__         = field(default=lowerCamelCase ,	metadata={'help': 'Whether tp freeze the encoder.'}      )
		lowercase__         = field(default=lowerCamelCase ,	metadata={'help': 'Whether  to freeze the embeddings.'}      )






@dataclass
class        _UpperCAmelCase:
		lowercase__         = field(
		    metadata={'help': 'The input data dir. Should contain the .tsv files (or other data files) for the task.'}      )
		lowercase__         = field(
		    default='summarization' ,	metadata={'help': 'Task name, summarization (or summarization_{dataset} for pegasus) or translation'} ,	)
		lowercase__         = field(
		    default=10_24 ,	metadata={
		        'help': (
		            'The maximum total input sequence length after tokenization. Sequences longer '
		            'than this will be truncated, sequences shorter will be padded.'
		        )
		    } ,	)
		lowercase__         = field(
		    default=1_28 ,	metadata={
		        'help': (
		            'The maximum total sequence length for target text after tokenization. Sequences longer '
		            'than this will be truncated, sequences shorter will be padded.'
		        )
		    } ,	)
		lowercase__         = field(
		    default=1_42 ,	metadata={
		        'help': (
		            'The maximum total sequence length for validation target text after tokenization. Sequences longer '
		            'than this will be truncated, sequences shorter will be padded. '
		            'This argument is also used to override the ``max_length`` param of ``model.generate``, which is used '
		            'during ``evaluate`` and ``predict``.'
		        )
		    } ,	)
		lowercase__         = field(
		    default=1_42 ,	metadata={
		        'help': (
		            'The maximum total sequence length for test target text after tokenization. Sequences longer '
		            'than this will be truncated, sequences shorter will be padded.'
		        )
		    } ,	)
		lowercase__         = field(default=-1 ,	metadata={'help': '# training examples. -1 means use all.'}      )
		lowercase__         = field(default=-1 ,	metadata={'help': '# validation examples. -1 means use all.'}      )
		lowercase__         = field(default=-1 ,	metadata={'help': '# test examples. -1 means use all.'}      )
		lowercase__         = field(default=lowerCamelCase ,	metadata={'help': 'Source language id for translation.'}      )
		lowercase__         = field(default=lowerCamelCase ,	metadata={'help': 'Target language id for translation.'}      )
		lowercase__         = field(default=lowerCamelCase ,	metadata={'help': '# num_beams to use for evaluation.'}      )
		lowercase__         = field(
		    default=lowerCamelCase ,	metadata={'help': 'If only pad tokens should be ignored. This assumes that `config.pad_token_id` is defined.'} ,	)





def  lowerCamelCase__						(     __snake_case,	__snake_case,	__snake_case      )				->						int:


					"""simple docstring"""





					logger.info(F'''***** {split} metrics *****'''      )
					for key in sorted(metrics.keys()      ):
										logger.info(F'''  {key} = {metrics[key]}'''      )
					save_json(__snake_case,	os.path.join(__snake_case,	F'''{split}_results.json'''      )      )



def  lowerCamelCase__						(     )				->						Optional[Any]:


					"""simple docstring"""





					_UpperCamelCase									=    HfArgumentParser((ModelArguments, DataTrainingArguments, SeqaSeqTrainingArguments)      )

					if len(sys.argv      ) == 2 and sys.argv[1].endswith('''.json'''      ):
										# If we pass only one argument to the script and it's the path to a json file,
										# let's parse it to get our arguments.
										_UpperCamelCase					,    _UpperCamelCase					,    _UpperCamelCase									=    parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]      )      )
					else:
										_UpperCamelCase					,    _UpperCamelCase					,    _UpperCamelCase									=    parser.parse_args_into_dataclasses()

					check_output_dir(__snake_case      )

					# Setup logging
					logging.basicConfig(
					    format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''',	datefmt='''%m/%d/%Y %H:%M:%S''',	level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN,	)
					logger.warning(
					    '''Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s''',	training_args.local_rank,	training_args.device,	training_args.n_gpu,	bool(training_args.parallel_mode == ParallelMode.DISTRIBUTED      ),	training_args.fpaa,	)
					transformers.utils.logging.enable_default_handler()
					transformers.utils.logging.enable_explicit_format()
					# Set the verbosity to info of the Transformers logger (on main process only):
					if is_main_process(training_args.local_rank      ):
										transformers.utils.logging.set_verbosity_info()
					logger.info('''Training/evaluation parameters %s''',	__snake_case      )

					# Set seed
					set_seed(training_args.seed      )

					# Load pretrained model and tokenizer
					#
					# Distributed training:
					# The .from_pretrained methods guarantee that only one local process can concurrently
					# download model & vocab.

					_UpperCamelCase									=    AutoConfig.from_pretrained(
					    model_args.config_name if model_args.config_name else model_args.model_name_or_path,	cache_dir=model_args.cache_dir,	)

					_UpperCamelCase									=    ('''encoder_layerdrop''', '''decoder_layerdrop''', '''dropout''', '''attention_dropout''')
					for p in extra_model_params:
										if getattr(__snake_case,	__snake_case,	__snake_case      ):
															assert hasattr(__snake_case,	__snake_case      ), F'''({config.__class__.__name__}) doesn\'t have a `{p}` attribute'''
															setattr(__snake_case,	__snake_case,	getattr(__snake_case,	__snake_case      )      )

					_UpperCamelCase									=    AutoTokenizer.from_pretrained(
					    model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path,	cache_dir=model_args.cache_dir,	)
					_UpperCamelCase									=    AutoModelForSeqaSeqLM.from_pretrained(
					    model_args.model_name_or_path,	from_tf='''.ckpt''' in model_args.model_name_or_path,	config=__snake_case,	cache_dir=model_args.cache_dir,	)

					# use task specific params
					use_task_specific_params(__snake_case,	data_args.task      )

					# set num_beams for evaluation
					if data_args.eval_beams is None:
										_UpperCamelCase									=    model.config.num_beams

					# set decoder_start_token_id for MBart
					if model.config.decoder_start_token_id is None and isinstance(__snake_case,	(MBartTokenizer, MBartTokenizerFast)      ):
										assert (
										    data_args.tgt_lang is not None and data_args.src_lang is not None
										), "mBart requires --tgt_lang and --src_lang"
										if isinstance(__snake_case,	__snake_case      ):
															_UpperCamelCase									=    tokenizer.lang_code_to_id[data_args.tgt_lang]
										else:
															_UpperCamelCase									=    tokenizer.convert_tokens_to_ids(data_args.tgt_lang      )

					if model_args.freeze_embeds:
										freeze_embeds(__snake_case      )
					if model_args.freeze_encoder:
										freeze_params(model.get_encoder()      )
										assert_all_frozen(model.get_encoder()      )

					_UpperCamelCase									=    SeqaSeqDataset

					# Get datasets
					_UpperCamelCase									=    (
					    dataset_class(
					        __snake_case,	type_path='''train''',	data_dir=data_args.data_dir,	n_obs=data_args.n_train,	max_target_length=data_args.max_target_length,	max_source_length=data_args.max_source_length,	prefix=model.config.prefix or '''''',	)
					    if training_args.do_train
					    else None
					)
					_UpperCamelCase									=    (
					    dataset_class(
					        __snake_case,	type_path='''val''',	data_dir=data_args.data_dir,	n_obs=data_args.n_val,	max_target_length=data_args.val_max_target_length,	max_source_length=data_args.max_source_length,	prefix=model.config.prefix or '''''',	)
					    if training_args.do_eval or training_args.evaluation_strategy != EvaluationStrategy.NO
					    else None
					)
					_UpperCamelCase									=    (
					    dataset_class(
					        __snake_case,	type_path='''test''',	data_dir=data_args.data_dir,	n_obs=data_args.n_test,	max_target_length=data_args.test_max_target_length,	max_source_length=data_args.max_source_length,	prefix=model.config.prefix or '''''',	)
					    if training_args.do_predict
					    else None
					)

					# Initialize our Trainer
					_UpperCamelCase									=    (
					    build_compute_metrics_fn(data_args.task,	__snake_case      ) if training_args.predict_with_generate else None
					)
					_UpperCamelCase									=    SeqaSeqTrainer(
					    model=__snake_case,	args=__snake_case,	data_args=__snake_case,	train_dataset=__snake_case,	eval_dataset=__snake_case,	data_collator=SeqaSeqDataCollator(
					        __snake_case,	__snake_case,	model.config.decoder_start_token_id,	training_args.tpu_num_cores      ),	compute_metrics=__snake_case,	tokenizer=__snake_case,	)

					_UpperCamelCase									=    {}
					# Training
					if training_args.do_train:
										logger.info('''*** Train ***'''      )

										_UpperCamelCase									=    trainer.train(
										    model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path      ) else None      )
										_UpperCamelCase									=    train_result.metrics
										_UpperCamelCase									=    data_args.n_train

										trainer.save_model()  # this also saves the tokenizer

										if trainer.is_world_process_zero():
															handle_metrics('''train''',	__snake_case,	training_args.output_dir      )
															all_metrics.update(__snake_case      )

															# Need to save the state, since Trainer.save_model saves only the tokenizer with the model
															trainer.state.save_to_json(os.path.join(training_args.output_dir,	'''trainer_state.json'''      )      )

															# For convenience, we also re-save the tokenizer to the same directory,
															# so that you can share your model easily on huggingface.co/models =)
															tokenizer.save_pretrained(training_args.output_dir      )

    # Evaluation
					if training_args.do_eval:
										logger.info('''*** Evaluate ***'''      )

										_UpperCamelCase									=    trainer.evaluate(metric_key_prefix='''val'''      )
										_UpperCamelCase									=    data_args.n_val
										_UpperCamelCase									=    round(metrics['''val_loss'''],	4      )

										if trainer.is_world_process_zero():
															handle_metrics('''val''',	__snake_case,	training_args.output_dir      )
															all_metrics.update(__snake_case      )

					if training_args.do_predict:
										logger.info('''*** Predict ***'''      )

										_UpperCamelCase									=    trainer.predict(test_dataset=__snake_case,	metric_key_prefix='''test'''      )
										_UpperCamelCase									=    test_output.metrics
										_UpperCamelCase									=    data_args.n_test

										if trainer.is_world_process_zero():
															_UpperCamelCase									=    round(metrics['''test_loss'''],	4      )
															handle_metrics('''test''',	__snake_case,	training_args.output_dir      )
															all_metrics.update(__snake_case      )

															if training_args.predict_with_generate:
																				_UpperCamelCase									=    tokenizer.batch_decode(
																				    test_output.predictions,	skip_special_tokens=__snake_case,	clean_up_tokenization_spaces=__snake_case      )
																				_UpperCamelCase									=    lmap(str.strip,	__snake_case      )
																				write_txt_file(__snake_case,	os.path.join(training_args.output_dir,	'''test_generations.txt'''      )      )

					if trainer.is_world_process_zero():
										save_json(__snake_case,	os.path.join(training_args.output_dir,	'''all_results.json'''      )      )

					return all_metrics



def  lowerCamelCase__						(     __snake_case      )				->						List[str]:


					"""simple docstring"""





					main()


if __name__ == "__main__":
				main()








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



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






							'''simple docstring'''
							_UpperCamelCase									=    parent
							_UpperCamelCase									=    batch_size
							_UpperCamelCase									=    image_size
							_UpperCamelCase									=    num_channels
							_UpperCamelCase									=    num_stages
							_UpperCamelCase									=    hidden_sizes
							_UpperCamelCase									=    depths
							_UpperCamelCase									=    is_training
							_UpperCamelCase									=    use_labels
							_UpperCamelCase									=    intermediate_size
							_UpperCamelCase									=    hidden_act
							_UpperCamelCase									=    type_sequence_label_size
							_UpperCamelCase									=    initializer_range
							_UpperCamelCase									=    out_features
							_UpperCamelCase									=    num_labels
							_UpperCamelCase									=    scope
							_UpperCamelCase									=    num_stages



		def 		UpperCAmelCase				(   self)			->			List[str]:






							'''simple docstring'''
							_UpperCamelCase									=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

							_UpperCamelCase									=    None
							if self.use_labels:
												_UpperCamelCase									=    ids_tensor([self.batch_size]							,						self.type_sequence_label_size)

							_UpperCamelCase									=    self.get_config()

							return config, pixel_values, labels



		def 		UpperCAmelCase				(   self)			->			Any:






							'''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 		UpperCAmelCase				(   self)			->			Optional[int]:






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






							'''simple docstring'''
							_UpperCamelCase									=    UperNetForSemanticSegmentation(config=__a)
							model.to(__a)
							model.eval()
							_UpperCamelCase									=    model(__a)
							self.parent.assertEqual(
							    result.logits.shape							,						(self.batch_size, self.num_labels, self.image_size, self.image_size))




		def 		UpperCAmelCase				(   self)			->			Dict:






							'''simple docstring'''
							_UpperCamelCase									=    self.prepare_config_and_inputs()
							(
							    (
							    _UpperCamelCase
							)					,    (
							    _UpperCamelCase
							)					,    (
							    _UpperCamelCase
							)					,    
							)									=    config_and_inputs
							_UpperCamelCase									=    {'''pixel_values''': pixel_values}
							return config, inputs_dict






@require_torch
class        _UpperCAmelCase(       lowerCamelCase ,	lowerCamelCase ,	unittest.TestCase      ):
		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 		UpperCAmelCase				(   self)			->			Union[str, Any]:






							'''simple docstring'''
							_UpperCamelCase									=    UperNetModelTester(self)
							_UpperCamelCase									=    ConfigTester(self							,						config_class=__a							,						has_text_modality=__a							,						hidden_size=37)



		def 		UpperCAmelCase				(   self)			->			Dict:






							'''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 		UpperCAmelCase				(   self)			->			List[Any]:






							'''simple docstring'''
							return



		def 		UpperCAmelCase				(   self)			->			str:






							'''simple docstring'''
							_UpperCamelCase					,    _UpperCamelCase									=    self.model_tester.prepare_config_and_inputs_for_common()

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

												_UpperCamelCase									=    ['''pixel_values''']
												self.assertListEqual(arg_names[:1]							,						__a)



		def 		UpperCAmelCase				(   self)			->			Optional[int]:






							'''simple docstring'''
							_UpperCamelCase									=    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)			->			Any:






							'''simple docstring'''
							pass



		@unittest.skip(reason='''UperNet does not support input and output embeddings''')
		def 		UpperCAmelCase				(   self)			->			List[Any]:






							'''simple docstring'''
							pass



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






							'''simple docstring'''
							pass



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






							'''simple docstring'''
							pass



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






							'''simple docstring'''
							pass



		def 		UpperCAmelCase				(   self)			->			List[Any]:






							'''simple docstring'''
							def check_hidden_states_output(__a							,						__a							,						__a):
												_UpperCamelCase									=    model_class(__a)
												model.to(__a)
												model.eval()

												with torch.no_grad():
																	_UpperCamelCase									=    model(**self._prepare_for_class(__a							,						__a))

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

												_UpperCamelCase									=    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]							,						)

							_UpperCamelCase					,    _UpperCamelCase									=    self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
												_UpperCamelCase									=    True
												check_hidden_states_output(__a							,						__a							,						__a)

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

												check_hidden_states_output(__a							,						__a							,						__a)



		def 		UpperCAmelCase				(   self)			->			Optional[Any]:






							'''simple docstring'''
							_UpperCamelCase					,    _UpperCamelCase									=    self.model_tester.prepare_config_and_inputs_for_common()

							_UpperCamelCase									=    _config_zero_init(__a)
							_UpperCamelCase									=    _config_zero_init(configs_no_init.backbone_config)
							for model_class in self.all_model_classes:
												_UpperCamelCase									=    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)			->			Optional[int]:






							'''simple docstring'''
							pass




		@slow
		def 		UpperCAmelCase				(   self)			->			Optional[Any]:






							'''simple docstring'''
							for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												_UpperCamelCase									=    UperNetForSemanticSegmentation.from_pretrained(__a)
												self.assertIsNotNone(__a)





def  lowerCamelCase__						(     )				->						int:


					"""simple docstring"""





					_UpperCamelCase									=    hf_hub_download(
					    repo_id='''hf-internal-testing/fixtures_ade20k''',	repo_type='''dataset''',	filename='''ADE_val_00000001.jpg'''      )
					_UpperCamelCase									=    Image.open(__snake_case      ).convert('''RGB'''      )
					return image






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



		def 		UpperCAmelCase				(   self)			->			Optional[int]:






							'''simple docstring'''
							_UpperCamelCase									=    AutoImageProcessor.from_pretrained('''openmmlab/upernet-swin-tiny''')
							_UpperCamelCase									=    UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-swin-tiny''').to(__a)

							_UpperCamelCase									=    prepare_img()
							_UpperCamelCase									=    processor(images=__a							,						return_tensors='''pt''').to(__a)

							with torch.no_grad():
												_UpperCamelCase									=    model(**__a)

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

							_UpperCamelCase									=    torch.tensor(
							    [[-7.5958, -7.5958, -7.4302], [-7.5958, -7.5958, -7.4302], [-7.4797, -7.4797, -7.3068]]).to(__a)
							self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]							,						__a							,						atol=1e-4))




		def 		UpperCAmelCase				(   self)			->			List[Any]:






							'''simple docstring'''
							_UpperCamelCase									=    AutoImageProcessor.from_pretrained('''openmmlab/upernet-convnext-tiny''')
							_UpperCamelCase									=    UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-convnext-tiny''').to(__a)

							_UpperCamelCase									=    prepare_img()
							_UpperCamelCase									=    processor(images=__a							,						return_tensors='''pt''').to(__a)

							with torch.no_grad():
												_UpperCamelCase									=    model(**__a)

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

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






