






import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from transformers import BatchEncoding, MarianTokenizer
from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow
from transformers.utils import is_sentencepiece_available, is_tf_available, is_torch_available


if is_sentencepiece_available():
 from transformers.models.marian.tokenization_marian import VOCAB_FILES_NAMES, save_json

from ...test_tokenization_common import TokenizerTesterMixin


_a    :   int        =			get_tests_dir("""fixtures/test_sentencepiece.model""")

_a    :   Optional[Any]        =			{"target_lang": "fi", "source_lang": "en"}
_a    :   Any        =			">>zh<<"
_a    :   Optional[int]        =			"Helsinki-NLP/"

if is_torch_available():
 _a    :   Optional[Any]        =			"pt"
elif is_tf_available():
 _a    :   Tuple        =			"tf"
else:
 _a    :   int        =			"jax"







@require_sentencepiece
class 	_UpperCAmelCase  (  _A				,							unittest.TestCase		):




      """simple docstring"""



      A          =    MarianTokenizer
      A          =    False
      A          =    True
      def 			snake_case_      (   self		):



          '''simple docstring'''
          super().setUp()
          lowerCAmelCase__   :List[str]      							=				["</s>", "<unk>", "▁This", "▁is", "▁a", "▁t", "est", "\u0120", "<pad>"]
          lowerCAmelCase__   :Union[str, Any]      							=				dict(zip(UpperCAmelCase__    ,	range(len(UpperCAmelCase__		)		)		)		)
          lowerCAmelCase__   :List[Any]      							=				Path(self.tmpdirname		)
          save_json(UpperCAmelCase__    ,	save_dir / VOCAB_FILES_NAMES["vocab"]		)
          save_json(UpperCAmelCase__    ,	save_dir / VOCAB_FILES_NAMES["tokenizer_config_file"]		)
          if not (save_dir / VOCAB_FILES_NAMES["source_spm"]).exists():
              copyfile(UpperCAmelCase__    ,	save_dir / VOCAB_FILES_NAMES["source_spm"]		)
              copyfile(UpperCAmelCase__    ,	save_dir / VOCAB_FILES_NAMES["target_spm"]		)

          lowerCAmelCase__   :Optional[Any]      							=				MarianTokenizer.from_pretrained(self.tmpdirname		)
          tokenizer.save_pretrained(self.tmpdirname		)
      def 			snake_case_      (   self    ,	**_lowerCAmelCase		):



          '''simple docstring'''
          return MarianTokenizer.from_pretrained(self.tmpdirname    ,	**UpperCAmelCase__		)
      def 			snake_case_      (   self    ,	_lowerCAmelCase		):



          '''simple docstring'''
          return (
              "This is a test",
              "This is a test",
          )
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :List[str]      							=				"</s>"
          lowerCAmelCase__   :Union[str, Any]      							=				0

          self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCAmelCase__		)    ,	UpperCAmelCase__		)
          self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCAmelCase__		)    ,	UpperCAmelCase__		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :str      							=				list(self.get_tokenizer().get_vocab().keys()		)

          self.assertEqual(vocab_keys[0]    ,	"</s>"		)
          self.assertEqual(vocab_keys[1]    ,	"<unk>"		)
          self.assertEqual(vocab_keys[-1]    ,	"<pad>"		)
          self.assertEqual(len(UpperCAmelCase__		)    ,	9		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          self.assertEqual(self.get_tokenizer().vocab_size    ,	9		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Dict      							=				MarianTokenizer.from_pretrained(f'''{ORG_NAME}opus-mt-en-de'''		)
          lowerCAmelCase__   :Tuple      							=				en_de_tokenizer(["I am a small frog"]    ,	return_tensors=UpperCAmelCase__		)
          self.assertIsInstance(UpperCAmelCase__    ,	UpperCAmelCase__		)
          lowerCAmelCase__   :List[Any]      							=				[38, 121, 14, 697, 38_848, 0]
          self.assertListEqual(UpperCAmelCase__    ,	batch.input_ids[0]		)

          lowerCAmelCase__   :Union[str, Any]      							=				tempfile.mkdtemp()
          en_de_tokenizer.save_pretrained(UpperCAmelCase__		)
          lowerCAmelCase__   :Optional[int]      							=				[x.name for x in Path(UpperCAmelCase__		).glob("*"		)]
          self.assertIn("source.spm"    ,	UpperCAmelCase__		)
          MarianTokenizer.from_pretrained(UpperCAmelCase__		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :List[str]      							=				self.get_tokenizer()

          lowerCAmelCase__   :Optional[int]      							=				tok(
              ["I am a small frog" * 1_000, "I am a small frog"]    ,	padding=UpperCAmelCase__    ,	truncation=UpperCAmelCase__    ,	return_tensors=UpperCAmelCase__		)
          self.assertIsInstance(UpperCAmelCase__    ,	UpperCAmelCase__		)
          self.assertEqual(batch.input_ids.shape    ,	(2, 512)		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Union[str, Any]      							=				self.get_tokenizer()
          lowerCAmelCase__   :Dict      							=				tok(["I am a tiny frog", "I am a small frog"]    ,	padding=UpperCAmelCase__    ,	return_tensors=UpperCAmelCase__		)
          self.assertIsInstance(UpperCAmelCase__    ,	UpperCAmelCase__		)
          self.assertEqual(batch_smaller.input_ids.shape    ,	(2, 10)		)
      @slow
      def 			snake_case_      (   self		):



          '''simple docstring'''
          # fmt: off
          lowerCAmelCase__   :Optional[int]      							=				{"input_ids": [[43_495, 462, 20, 42_164, 1_369, 52, 464, 132, 1_703, 492, 13, 7_491, 38_999, 6, 8, 464, 132, 1_703, 492, 13, 4_669, 37_867, 13, 7_525, 27, 1_593, 988, 13, 33_972, 7_029, 6, 20, 8_251, 383, 2, 270, 5_866, 3_788, 2, 2_353, 8_251, 12_338, 2, 13_958, 387, 2, 3_629, 6_953, 188, 2_900, 2, 13_958, 8_011, 11_501, 23, 8_460, 4_073, 34_009, 20, 435, 11_439, 27, 8, 8_460, 4_073, 6_004, 20, 9_988, 375, 27, 33, 266, 1_945, 1_076, 1_350, 37_867, 3_288, 5, 577, 1_076, 4_374, 8, 5_082, 5, 26_453, 257, 556, 403, 2, 242, 132, 383, 316, 492, 8, 10_767, 6, 316, 304, 4_239, 3, 0], [148, 15_722, 19, 1_839, 12, 1_350, 13, 22_327, 5_082, 5_418, 47_567, 35_938, 59, 318, 19_552, 108, 2_183, 54, 14_976, 4_835, 32, 547, 1_114, 8, 315, 2_417, 5, 92, 19_088, 3, 0, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100], [36, 6_395, 12_570, 39_147, 11_597, 6, 266, 4, 45_405, 7_296, 3, 0, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100, 58_100]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
          # fmt: on

          self.tokenizer_integration_test_util(
              expected_encoding=UpperCAmelCase__    ,	model_name="Helsinki-NLP/opus-mt-en-de"    ,	revision="1a8c2263da11e68e50938f97e10cd57820bd504c"    ,	decode_kwargs={"use_source_tokenizer": True}    ,	)



      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[Any]      							=				MarianTokenizer.from_pretrained("hf-internal-testing/test-marian-two-vocabs"		)

          lowerCAmelCase__   :Optional[Any]      							=				"Tämä on testi"
          lowerCAmelCase__   :List[Any]      							=				"This is a test"

          lowerCAmelCase__   :Tuple      							=				[76, 7, 2_047, 2]
          lowerCAmelCase__   :str      							=				[69, 12, 11, 940, 2]

          lowerCAmelCase__   :int      							=				tokenizer(UpperCAmelCase__		).input_ids
          self.assertListEqual(UpperCAmelCase__    ,	UpperCAmelCase__		)

          lowerCAmelCase__   :List[Any]      							=				tokenizer(text_target=UpperCAmelCase__		).input_ids
          self.assertListEqual(UpperCAmelCase__    ,	UpperCAmelCase__		)

          lowerCAmelCase__   :Optional[int]      							=				tokenizer.decode(UpperCAmelCase__    ,	skip_special_tokens=UpperCAmelCase__		)
          self.assertEqual(UpperCAmelCase__    ,	UpperCAmelCase__		)













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    ,	_lowerCAmelCase    ,	_lowerCAmelCase=13    ,	_lowerCAmelCase=32    ,	_lowerCAmelCase=3    ,	_lowerCAmelCase=4    ,	_lowerCAmelCase=[10, 20, 30, 40]    ,	_lowerCAmelCase=[2, 2, 3, 2]    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=37    ,	_lowerCAmelCase="gelu"    ,	_lowerCAmelCase=10    ,	_lowerCAmelCase=0.02    ,	_lowerCAmelCase=["stage2", "stage3", "stage4"]    ,	_lowerCAmelCase=3    ,	_lowerCAmelCase=None    ,	):



          '''simple docstring'''
          lowerCAmelCase__   :str      							=				parent
          lowerCAmelCase__   :Dict      							=				batch_size
          lowerCAmelCase__   :str      							=				image_size
          lowerCAmelCase__   :int      							=				num_channels
          lowerCAmelCase__   :Tuple      							=				num_stages
          lowerCAmelCase__   :Optional[int]      							=				hidden_sizes
          lowerCAmelCase__   :Tuple      							=				depths
          lowerCAmelCase__   :Tuple      							=				is_training
          lowerCAmelCase__   :str      							=				use_labels
          lowerCAmelCase__   :int      							=				intermediate_size
          lowerCAmelCase__   :List[str]      							=				hidden_act
          lowerCAmelCase__   :Dict      							=				type_sequence_label_size
          lowerCAmelCase__   :Any      							=				initializer_range
          lowerCAmelCase__   :List[str]      							=				out_features
          lowerCAmelCase__   :List[Any]      							=				num_labels
          lowerCAmelCase__   :str      							=				scope
          lowerCAmelCase__   :Dict      							=				num_stages
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :int      							=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

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

          return config, pixel_values, labels
      def 			snake_case_      (   self		):



          '''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 			snake_case_      (   self		):



          '''simple docstring'''
          return UperNetConfig(
              backbone_config=self.get_backbone_config()    ,	hidden_size=512    ,	pool_scales=[1, 2, 3, 6]    ,	use_auxiliary_head=_lowerCAmelCase    ,	auxiliary_loss_weight=0.4    ,	auxiliary_in_channels=40    ,	auxiliary_channels=256    ,	auxiliary_num_convs=1    ,	auxiliary_concat_input=_lowerCAmelCase    ,	loss_ignore_index=255    ,	num_labels=self.num_labels    ,	)
      def 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :Dict      							=				UperNetForSemanticSegmentation(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          lowerCAmelCase__   :List[Any]      							=				model(_lowerCAmelCase		)
          self.parent.assertEqual(
              result.logits.shape    ,	(self.batch_size, self.num_labels, self.image_size, self.image_size)		)



      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[int]      							=				self.prepare_config_and_inputs()
          (
              (
              lowerCAmelCase__
          )							,(
              lowerCAmelCase__
          )							,(
              lowerCAmelCase__
          )							,
          )   :Dict      							=				config_and_inputs
          lowerCAmelCase__   :Any      							=				{"pixel_values": pixel_values}
          return config, inputs_dict







@require_torch
class 	_UpperCAmelCase  (  _A				,							_A				,							unittest.TestCase		):




      """simple docstring"""



      A          =    (UperNetForSemanticSegmentation,) if is_torch_available() else ()
      A          =    {'''image-segmentation''': UperNetForSemanticSegmentation} if is_torch_available() else {}
      A          =    False
      A          =    False
      A          =    False
      A          =    False
      A          =    False
      A          =    False
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Tuple      							=				UperNetModelTester(self		)
          lowerCAmelCase__   :Any      							=				ConfigTester(self    ,	config_class=_lowerCAmelCase    ,	has_text_modality=_lowerCAmelCase    ,	hidden_size=37		)
      def 			snake_case_      (   self		):



          '''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 			snake_case_      (   self		):



          '''simple docstring'''
          return
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__							,lowerCAmelCase__   :str      							=				self.model_tester.prepare_config_and_inputs_for_common()

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

              lowerCAmelCase__   :Union[str, Any]      							=				["pixel_values"]
              self.assertListEqual(arg_names[:1]    ,	_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :int      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_semantic_segmentation(*_lowerCAmelCase		)
      @unittest.skip(reason="UperNet does not use inputs_embeds"		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          pass
      @unittest.skip(reason="UperNet does not support input and output embeddings"		)
      def 			snake_case_      (   self		):



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



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



          '''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 			snake_case_      (   self		):



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



          '''simple docstring'''
          pass
      def 			snake_case_      (   self		):



          '''simple docstring'''
          def check_hidden_states_output(_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):
              lowerCAmelCase__   :int      							=				model_class(_lowerCAmelCase		)
              model.to(_lowerCAmelCase		)
              model.eval()

              with torch.no_grad():
                  lowerCAmelCase__   :Dict      							=				model(**self._prepare_for_class(_lowerCAmelCase    ,	_lowerCAmelCase		)		)

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

              lowerCAmelCase__   :Tuple      							=				self.model_tester.num_stages
              self.assertEqual(len(_lowerCAmelCase		)    ,	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__   :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(_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		)

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

              check_hidden_states_output(_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__							,lowerCAmelCase__   :Dict      							=				self.model_tester.prepare_config_and_inputs_for_common()

          lowerCAmelCase__   :str      							=				_config_zero_init(_lowerCAmelCase		)
          lowerCAmelCase__   :List[Any]      							=				_config_zero_init(configs_no_init.backbone_config		)
          for model_class in self.all_model_classes:
              lowerCAmelCase__   :List[Any]      							=				model_class(config=_lowerCAmelCase		)
              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 			snake_case_      (   self		):



          '''simple docstring'''
          pass



      @slow
      def 			snake_case_      (   self		):



          '''simple docstring'''
          for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              lowerCAmelCase__   :List[str]      							=				UperNetForSemanticSegmentation.from_pretrained(_lowerCAmelCase		)
              self.assertIsNotNone(_lowerCAmelCase		)





def 				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(UpperCAmelCase       ).convert("RGB"       )
    return image







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




      """simple docstring"""



      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[Any]      							=				AutoImageProcessor.from_pretrained("openmmlab/upernet-swin-tiny"		)
          lowerCAmelCase__   :Dict      							=				UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-swin-tiny"		).to(_lowerCAmelCase		)

          lowerCAmelCase__   :str      							=				prepare_img()
          lowerCAmelCase__   :str      							=				processor(images=_lowerCAmelCase    ,	return_tensors="pt"		).to(_lowerCAmelCase		)

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

          lowerCAmelCase__   :str      							=				torch.Size((1, model.config.num_labels, 512, 512)		)
          self.assertEqual(outputs.logits.shape    ,	_lowerCAmelCase		)

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



      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Any      							=				AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-tiny"		)
          lowerCAmelCase__   :Optional[int]      							=				UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-tiny"		).to(_lowerCAmelCase		)

          lowerCAmelCase__   :List[Any]      							=				prepare_img()
          lowerCAmelCase__   :Tuple      							=				processor(images=_lowerCAmelCase    ,	return_tensors="pt"		).to(_lowerCAmelCase		)

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

          lowerCAmelCase__   :str      							=				torch.Size((1, model.config.num_labels, 512, 512)		)
          self.assertEqual(outputs.logits.shape    ,	_lowerCAmelCase		)

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



