




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:



 '''simple docstring'''


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

       """simple docstring"""






       lowerCamelCase_							=   parent
       lowerCamelCase_							=   batch_size
       lowerCamelCase_							=   image_size
       lowerCamelCase_							=   num_channels
       lowerCamelCase_							=   num_stages
       lowerCamelCase_							=   hidden_sizes
       lowerCamelCase_							=   depths
       lowerCamelCase_							=   is_training
       lowerCamelCase_							=   use_labels
       lowerCamelCase_							=   intermediate_size
       lowerCamelCase_							=   hidden_act
       lowerCamelCase_							=   type_sequence_label_size
       lowerCamelCase_							=   initializer_range
       lowerCamelCase_							=   out_features
       lowerCamelCase_							=   num_labels
       lowerCamelCase_							=   scope
       lowerCamelCase_							=   num_stages

 def     a__			(    self :  Union[str, Any]  )       ->     str:

       """simple docstring"""






       lowerCamelCase_							=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

       lowerCamelCase_							=   self.get_config()

       return config, pixel_values, labels

 def     a__			(    self :  Optional[Any]  )       ->     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     a__			(    self :  str  )       ->     Union[str, Any]:

       """simple docstring"""






       return UperNetConfig(
           backbone_config=self.get_backbone_config()						,							hidden_size=512						,							pool_scales=[1, 2, 3, 6]						,							use_auxiliary_head=A_						,							auxiliary_loss_weight=0.4						,							auxiliary_in_channels=40						,							auxiliary_channels=256						,							auxiliary_num_convs=1						,							auxiliary_concat_input=A_						,							loss_ignore_index=255						,							num_labels=self.num_labels						,							)

 def     a__			(    self :  str						,							A_ :  Any						,							A_ :  str						,							A_ :  List[str]  )       ->     Optional[Any]:

       """simple docstring"""






       lowerCamelCase_							=   UperNetForSemanticSegmentation(config=A_  )
       model.to(A_  )
       model.eval()
       lowerCamelCase_							=   model(A_  )
       self.parent.assertEqual(
           result.logits.shape						,							(self.batch_size, self.num_labels, self.image_size, self.image_size)  )





 def     a__			(    self :  Union[str, Any]  )       ->     List[Any]:

       """simple docstring"""






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







@require_torch
class   A(						UpperCamelCase							,				UpperCamelCase							,				unittest.TestCase       ):



 '''simple docstring'''


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

       """simple docstring"""






       lowerCamelCase_							=   UperNetModelTester(self  )
       lowerCamelCase_							=   ConfigTester(self						,							config_class=A_						,							has_text_modality=A_						,							hidden_size=37  )

 def     a__			(    self :  Optional[Any]  )       ->     Optional[int]:

       """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 :  List[str]  )       ->     List[Any]:

       """simple docstring"""






       return

 def     a__			(    self :  Tuple  )       ->     Union[str, Any]:

       """simple docstring"""






       lowerCamelCase_					,    lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs_for_common()

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

             lowerCamelCase_							=   ['pixel_values']
             self.assertListEqual(arg_names[:1]						,							A_  )

 def     a__			(    self :  Union[str, Any]  )       ->     Dict:

       """simple docstring"""






       lowerCamelCase_							=   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     a__			(    self :  List[Any]  )       ->     str:

       """simple docstring"""






       pass

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

       """simple docstring"""






       pass

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

       """simple docstring"""






       pass

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

       """simple docstring"""






       pass

 @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'  )
 def     a__			(    self :  Union[str, Any]  )       ->     Union[str, Any]:

       """simple docstring"""






       pass

 def     a__			(    self :  List[Any]  )       ->     Optional[Any]:

       """simple docstring"""






       def check_hidden_states_output(A_ :  str						,							A_ :  List[Any]						,							A_ :  Optional[Any]  ):
             lowerCamelCase_							=   model_class(A_  )
             model.to(A_  )
             model.eval()

             with torch.no_grad():
                   lowerCamelCase_							=   model(**self._prepare_for_class(A_						,							A_  )  )

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

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

       lowerCamelCase_					,    lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             lowerCamelCase_							=   True
             check_hidden_states_output(A_						,							A_						,							A_  )

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

             check_hidden_states_output(A_						,							A_						,							A_  )

 def     a__			(    self :  Dict  )       ->     Any:

       """simple docstring"""






       lowerCamelCase_					,    lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs_for_common()

       lowerCamelCase_							=   _config_zero_init(A_  )
       lowerCamelCase_							=   _config_zero_init(configs_no_init.backbone_config  )
       for model_class in self.all_model_classes:
             lowerCamelCase_							=   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     a__			(    self :  List[Any]  )       ->     int:

       """simple docstring"""






       pass





 @slow
 def     a__			(    self :  int  )       ->     List[Any]:

       """simple docstring"""






       for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowerCamelCase_							=   UperNetForSemanticSegmentation.from_pretrained(A_  )
             self.assertIsNotNone(A_  )

def       _SCREAMING_SNAKE_CASE      (							):


      '''simple docstring'''



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







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



 '''simple docstring'''


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

       """simple docstring"""






       lowerCamelCase_							=   AutoImageProcessor.from_pretrained('openmmlab/upernet-swin-tiny'  )
       lowerCamelCase_							=   UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-swin-tiny'  ).to(A_  )

       lowerCamelCase_							=   prepare_img()
       lowerCamelCase_							=   processor(images=A_						,							return_tensors='pt'  ).to(A_  )

       with torch.no_grad():
             lowerCamelCase_							=   model(**A_  )

       lowerCamelCase_							=   torch.Size((1, model.config.num_labels, 512, 512)  )
       self.assertEqual(outputs.logits.shape						,							A_  )

       lowerCamelCase_							=   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     a__			(    self :  List[str]  )       ->     Optional[int]:

       """simple docstring"""






       lowerCamelCase_							=   AutoImageProcessor.from_pretrained('openmmlab/upernet-convnext-tiny'  )
       lowerCamelCase_							=   UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-convnext-tiny'  ).to(A_  )

       lowerCamelCase_							=   prepare_img()
       lowerCamelCase_							=   processor(images=A_						,							return_tensors='pt'  ).to(A_  )

       with torch.no_grad():
             lowerCamelCase_							=   model(**A_  )

       lowerCamelCase_							=   torch.Size((1, model.config.num_labels, 512, 512)  )
       self.assertEqual(outputs.logits.shape						,							A_  )

       lowerCamelCase_							=   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  )  )









import os
import time

import numpy as np
import onnxruntime as ort


lowerCamelCase    :		int			   =       "1"
lowerCamelCase    :		int			   =       "0"
lowerCamelCase    :		Union[str, Any]			   =       "1"

lowerCamelCase    :		List[Any]			   =       ort.SessionOptions()
lowerCamelCase    :		Optional[Any]			   =       ort.GraphOptimizationLevel.ORT_DISABLE_ALL
print("Create inference session...")
lowerCamelCase    :		Union[str, Any]			   =       ["TensorrtExecutionProvider", "CUDAExecutionProvider"]
lowerCamelCase    :		Tuple			   =       ort.InferenceSession("model.onnx", sess_options=sess_opt, providers=execution_provider)
lowerCamelCase    :		List[Any]			   =       ort.RunOptions()

lowerCamelCase    :		List[str]			   =       128
lowerCamelCase    :		List[Any]			   =       1
lowerCamelCase    :		Union[str, Any]			   =       np.ones((batch, sequence), dtype=np.intaa)
lowerCamelCase    :		Dict			   =       np.ones((batch, sequence), dtype=np.intaa)
lowerCamelCase    :		Optional[Any]			   =       np.ones((batch, sequence), dtype=np.intaa)

print("Warm up phase...")
sess.run(
    None,
    {
        sess.get_inputs()[0].name: input_ids,
        sess.get_inputs()[1].name: attention_mask,
        sess.get_inputs()[2].name: token_type_ids,
    },
    run_options=run_opt,
)

print("Start inference...")
lowerCamelCase    :		int			   =       time.time()
lowerCamelCase    :		Dict			   =       2_000
lowerCamelCase    :		Any			   =       {}
for iter in range(max_iters):
 lowerCamelCase    :		Union[str, Any]			   =       sess.run(
     None,
     {
         sess.get_inputs()[0].name: input_ids,
         sess.get_inputs()[1].name: attention_mask,
         sess.get_inputs()[2].name: token_type_ids,
     },
     run_options=run_opt,
 )
print("Average Inference Time = {:.3f} ms".format((time.time() - start_time) * 1_000 / max_iters))



