




"""simple docstring"""



from typing import Any, Dict, List, Union

import numpy as np

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
      from PIL import Image

      from ..image_utils import load_image

if is_torch_available():
      from ..models.auto.modeling_auto import (
          MODEL_FOR_IMAGE_SEGMENTATION_MAPPING,
          MODEL_FOR_INSTANCE_SEGMENTATION_MAPPING,
          MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING,
          MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING,
      )


SCREAMING_SNAKE_CASE							=			logging.get_logger(__name__)


SCREAMING_SNAKE_CASE							=			Dict[str, Any]
SCREAMING_SNAKE_CASE							=			List[Prediction]




@add_end_docstrings(A_				)
class       UpperCAmelCase_    (		A_				):




    def __init__(       self		:			Union[str, Any]      ,					*snake_case_		:			Optional[int]      ,					**snake_case_		:			Optional[int] ) ->				List[Any]:




     '''simple docstring'''

     super().__init__(*snake_case_      ,					**snake_case_ )

     if self.framework == "tf":
      raise ValueError(F"""The {self.__class__} is only available in PyTorch.""" )

     requires_backends(self      ,					"vision" )
     self.check_model_type(
         dict(
             MODEL_FOR_IMAGE_SEGMENTATION_MAPPING.items()
             + MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING.items()
             + MODEL_FOR_INSTANCE_SEGMENTATION_MAPPING.items()
             + MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING.items() ) )




    def 				__magic_name__				(       self		:			Optional[int]      ,					**snake_case_		:			Any ) ->				Any:




     '''simple docstring'''

     A__			=							{}
     A__			=							{}
     if "subtask" in kwargs:
      A__			=							kwargs["subtask"]
      A__			=							kwargs["subtask"]
     if "threshold" in kwargs:
      A__			=							kwargs["threshold"]
     if "mask_threshold" in kwargs:
      A__			=							kwargs["mask_threshold"]
     if "overlap_mask_area_threshold" in kwargs:
      A__			=							kwargs["overlap_mask_area_threshold"]

     return preprocess_kwargs, {}, postprocess_kwargs




    def __call__(       self		:			Union[str, Any]      ,					snake_case_		:			Union[str, Any]      ,					**snake_case_		:			str ) ->				Union[Predictions, List[Prediction]]:




     '''simple docstring'''

     return super().__call__(snake_case_      ,					**snake_case_ )




    def 				__magic_name__				(       self		:			List[str]      ,					snake_case_		:			int      ,					snake_case_		:			Union[str, Any]=None ) ->				Any:




     '''simple docstring'''

     A__			=							load_image(snake_case_ )
     A__			=							[(image.height, image.width)]
     if self.model.config.__class__.__name__ == "OneFormerConfig":
      if subtask is None:
       A__			=							{}
      else:
       A__			=							{"task_inputs": [subtask]}
      A__			=							self.image_processor(images=[image]      ,					return_tensors="pt"      ,					**snake_case_ )
      A__			=							self.tokenizer(
          inputs["task_inputs"]      ,					padding="max_length"      ,					max_length=self.model.config.task_seq_len      ,					return_tensors=self.framework      ,					)["input_ids"]
     else:
      A__			=							self.image_processor(images=[image]      ,					return_tensors="pt" )
     A__			=							target_size
     return inputs




    def 				__magic_name__				(       self		:			Optional[int]      ,					snake_case_		:			List[str] ) ->				int:




     '''simple docstring'''

     A__			=							model_inputs.pop("target_size" )
     A__			=							self.model(**snake_case_ )
     A__			=							target_size
     return model_outputs




    def 				__magic_name__				(       self		:			str      ,					snake_case_		:			Union[str, Any]      ,					snake_case_		:			List[str]=None      ,					snake_case_		:			Dict=0.9      ,					snake_case_		:			List[str]=0.5      ,					snake_case_		:			Optional[int]=0.5 ) ->				str:




     '''simple docstring'''

     A__			=							None
     if subtask in {"panoptic", None} and hasattr(self.image_processor      ,					"post_process_panoptic_segmentation" ):
      A__			=							self.image_processor.post_process_panoptic_segmentation
     elif subtask in {"instance", None} and hasattr(self.image_processor      ,					"post_process_instance_segmentation" ):
      A__			=							self.image_processor.post_process_instance_segmentation

     if fn is not None:
      A__			=							fn(
          snake_case_      ,					threshold=snake_case_      ,					mask_threshold=snake_case_      ,					overlap_mask_area_threshold=snake_case_      ,					target_sizes=model_outputs["target_size"]      ,					)[0]

      A__			=							[]
      A__			=							outputs["segmentation"]

      for segment in outputs["segments_info"]:
       A__			=							(segmentation == segment["id"]) * 255
       A__			=							Image.fromarray(mask.numpy().astype(np.uinta )      ,					mode="L" )
       A__			=							self.model.config.idalabel[segment["label_id"]]
       A__			=							segment["score"]
       annotation.append({"score": score, "label": label, "mask": mask} )

     elif subtask in {"semantic", None} and hasattr(self.image_processor      ,					"post_process_semantic_segmentation" ):
      A__			=							self.image_processor.post_process_semantic_segmentation(
          snake_case_      ,					target_sizes=model_outputs["target_size"] )[0]

      A__			=							[]
      A__			=							outputs.numpy()
      A__			=							np.unique(snake_case_ )

      for label in labels:
       A__			=							(segmentation == label) * 255
       A__			=							Image.fromarray(mask.astype(np.uinta )      ,					mode="L" )
       A__			=							self.model.config.idalabel[label]
       annotation.append({"score": None, "label": label, "mask": mask} )
     else:
      raise ValueError(F"""Subtask {subtask} is not supported for model {type(self.model )}""" )
     return annotation











"""simple docstring"""



from __future__ import annotations

import unittest

from transformers import MPNetConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
      import tensorflow as tf

      from transformers.models.mpnet.modeling_tf_mpnet import (
          TFMPNetForMaskedLM,
          TFMPNetForMultipleChoice,
          TFMPNetForQuestionAnswering,
          TFMPNetForSequenceClassification,
          TFMPNetForTokenClassification,
          TFMPNetModel,
      )




class       UpperCAmelCase_    :




    def __init__(       self		:			Dict      ,					snake_case_		:			List[str]      ,					snake_case_		:			List[Any]=13      ,					snake_case_		:			int=7      ,					snake_case_		:			List[Any]=True      ,					snake_case_		:			Tuple=True      ,					snake_case_		:			Tuple=False      ,					snake_case_		:			str=True      ,					snake_case_		:			str=99      ,					snake_case_		:			int=64      ,					snake_case_		:			Tuple=2      ,					snake_case_		:			Optional[int]=4      ,					snake_case_		:			List[Any]=64      ,					snake_case_		:			Any="gelu"      ,					snake_case_		:			List[Any]=0.1      ,					snake_case_		:			List[Any]=0.1      ,					snake_case_		:			Dict=512      ,					snake_case_		:			Dict=16      ,					snake_case_		:			Tuple=2      ,					snake_case_		:			Tuple=0.02      ,					snake_case_		:			Optional[Any]=3      ,					snake_case_		:			Dict=4      ,					snake_case_		:			List[str]=None      ,					) ->				Any:




     '''simple docstring'''

     A__			=							parent
     A__			=							batch_size
     A__			=							seq_length
     A__			=							is_training
     A__			=							use_input_mask
     A__			=							use_token_type_ids
     A__			=							use_labels
     A__			=							vocab_size
     A__			=							hidden_size
     A__			=							num_hidden_layers
     A__			=							num_attention_heads
     A__			=							intermediate_size
     A__			=							hidden_act
     A__			=							hidden_dropout_prob
     A__			=							attention_probs_dropout_prob
     A__			=							max_position_embeddings
     A__			=							type_vocab_size
     A__			=							type_sequence_label_size
     A__			=							initializer_range
     A__			=							num_labels
     A__			=							num_choices
     A__			=							scope




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




     '''simple docstring'''

     A__			=							ids_tensor([self.batch_size, self.seq_length]      ,					self.vocab_size )

     A__			=							None
     if self.use_input_mask:
      A__			=							random_attention_mask([self.batch_size, self.seq_length] )

     A__			=							None
     A__			=							None
     A__			=							None
     if self.use_labels:
      A__			=							ids_tensor([self.batch_size]      ,					self.type_sequence_label_size )
      A__			=							ids_tensor([self.batch_size, self.seq_length]      ,					self.num_labels )
      A__			=							ids_tensor([self.batch_size]      ,					self.num_choices )

     A__			=							MPNetConfig(
         vocab_size=self.vocab_size      ,					hidden_size=self.hidden_size      ,					num_hidden_layers=self.num_hidden_layers      ,					num_attention_heads=self.num_attention_heads      ,					intermediate_size=self.intermediate_size      ,					hidden_act=self.hidden_act      ,					hidden_dropout_prob=self.hidden_dropout_prob      ,					attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,					max_position_embeddings=self.max_position_embeddings      ,					initializer_range=self.initializer_range      ,					)
     return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels




    def 				__magic_name__				(       self		:			str      ,					snake_case_		:			int      ,					snake_case_		:			int      ,					snake_case_		:			Union[str, Any]      ,					snake_case_		:			Optional[Any]      ,					snake_case_		:			Any      ,					snake_case_		:			str ) ->				int:




     '''simple docstring'''

     A__			=							TFMPNetModel(config=snake_case_ )
     A__			=							{"input_ids": input_ids, "attention_mask": input_mask}
     A__			=							model(snake_case_ )
     A__			=							[input_ids, input_mask]
     A__			=							model(snake_case_ )
     self.parent.assertEqual(result.last_hidden_state.shape      ,					(self.batch_size, self.seq_length, self.hidden_size) )




    def 				__magic_name__				(       self		:			List[Any]      ,					snake_case_		:			Union[str, Any]      ,					snake_case_		:			List[str]      ,					snake_case_		:			Optional[int]      ,					snake_case_		:			Union[str, Any]      ,					snake_case_		:			str      ,					snake_case_		:			str ) ->				int:




     '''simple docstring'''

     A__			=							TFMPNetForMaskedLM(config=snake_case_ )
     A__			=							{"input_ids": input_ids, "attention_mask": input_mask}
     A__			=							model(snake_case_ )
     self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.seq_length, self.vocab_size) )




    def 				__magic_name__				(       self		:			List[Any]      ,					snake_case_		:			Union[str, Any]      ,					snake_case_		:			Tuple      ,					snake_case_		:			Any      ,					snake_case_		:			str      ,					snake_case_		:			Union[str, Any]      ,					snake_case_		:			Dict ) ->				Tuple:




     '''simple docstring'''

     A__			=							TFMPNetForQuestionAnswering(config=snake_case_ )
     A__			=							{
         "input_ids": input_ids,
         "attention_mask": input_mask,
     }
     A__			=							model(snake_case_ )
     self.parent.assertEqual(result.start_logits.shape      ,					(self.batch_size, self.seq_length) )
     self.parent.assertEqual(result.end_logits.shape      ,					(self.batch_size, self.seq_length) )




    def 				__magic_name__				(       self		:			List[Any]      ,					snake_case_		:			Union[str, Any]      ,					snake_case_		:			List[str]      ,					snake_case_		:			Dict      ,					snake_case_		:			Optional[int]      ,					snake_case_		:			Optional[int]      ,					snake_case_		:			Optional[int] ) ->				Tuple:




     '''simple docstring'''

     A__			=							self.num_labels
     A__			=							TFMPNetForSequenceClassification(snake_case_ )
     A__			=							{"input_ids": input_ids, "attention_mask": input_mask}
     A__			=							model(snake_case_ )
     self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.num_labels) )




    def 				__magic_name__				(       self		:			Dict      ,					snake_case_		:			Tuple      ,					snake_case_		:			int      ,					snake_case_		:			List[Any]      ,					snake_case_		:			Optional[int]      ,					snake_case_		:			str      ,					snake_case_		:			Dict ) ->				Optional[Any]:




     '''simple docstring'''

     A__			=							self.num_choices
     A__			=							TFMPNetForMultipleChoice(snake_case_ )
     A__			=							tf.tile(tf.expand_dims(snake_case_      ,					1 )      ,					(1, self.num_choices, 1) )
     A__			=							tf.tile(tf.expand_dims(snake_case_      ,					1 )      ,					(1, self.num_choices, 1) )
     A__			=							{
         "input_ids": multiple_choice_inputs_ids,
         "attention_mask": multiple_choice_input_mask,
     }
     A__			=							model(snake_case_ )
     self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.num_choices) )




    def 				__magic_name__				(       self		:			List[str]      ,					snake_case_		:			str      ,					snake_case_		:			Tuple      ,					snake_case_		:			Union[str, Any]      ,					snake_case_		:			List[str]      ,					snake_case_		:			Optional[Any]      ,					snake_case_		:			List[Any] ) ->				int:




     '''simple docstring'''

     A__			=							self.num_labels
     A__			=							TFMPNetForTokenClassification(snake_case_ )
     A__			=							{"input_ids": input_ids, "attention_mask": input_mask}
     A__			=							model(snake_case_ )
     self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.seq_length, self.num_labels) )




    def 				__magic_name__				(       self		:			Tuple ) ->				Dict:




     '''simple docstring'''

     A__			=							self.prepare_config_and_inputs()
     ((A__), (A__), (A__), (A__), (A__), (A__))			=							config_and_inputs
     A__			=							{"input_ids": input_ids, "attention_mask": input_mask}
     return config, inputs_dict






@require_tf
class       UpperCAmelCase_    (		A_,  A_,  unittest.TestCase				):
    lowercase__		   = (
        (
            TFMPNetForMaskedLM,
            TFMPNetForMultipleChoice,
            TFMPNetForQuestionAnswering,
            TFMPNetForSequenceClassification,
            TFMPNetForTokenClassification,
            TFMPNetModel,
        )
        if is_tf_available()
        else ()
    )
    lowercase__		   = (
        {
            '''feature-extraction''': TFMPNetModel,
            '''fill-mask''': TFMPNetForMaskedLM,
            '''question-answering''': TFMPNetForQuestionAnswering,
            '''text-classification''': TFMPNetForSequenceClassification,
            '''token-classification''': TFMPNetForTokenClassification,
            '''zero-shot''': TFMPNetForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )
    lowercase__		   = False
    lowercase__		   = False




    def 				__magic_name__				(       self		:			Any ) ->				List[str]:




     '''simple docstring'''

     A__			=							TFMPNetModelTester(self )
     A__			=							ConfigTester(self      ,					config_class=snake_case_      ,					hidden_size=37 )




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




     '''simple docstring'''

     self.config_tester.run_common_tests()




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




     '''simple docstring'''

     A__			=							self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mpnet_model(*snake_case_ )




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




     '''simple docstring'''

     A__			=							self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mpnet_for_masked_lm(*snake_case_ )




    def 				__magic_name__				(       self		:			Dict ) ->				Optional[Any]:




     '''simple docstring'''

     A__			=							self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mpnet_for_question_answering(*snake_case_ )




    def 				__magic_name__				(       self		:			int ) ->				List[Any]:




     '''simple docstring'''

     A__			=							self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mpnet_for_sequence_classification(*snake_case_ )




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




     '''simple docstring'''

     A__			=							self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mpnet_for_multiple_choice(*snake_case_ )




    def 				__magic_name__				(       self		:			Tuple ) ->				Any:




     '''simple docstring'''

     A__			=							self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mpnet_for_token_classification(*snake_case_ )




    @slow
    def 				__magic_name__				(       self		:			Dict ) ->				Tuple:




     '''simple docstring'''

     for model_name in ["microsoft/mpnet-base"]:
      A__			=							TFMPNetModel.from_pretrained(snake_case_ )
      self.assertIsNotNone(snake_case_ )






@require_tf
class       UpperCAmelCase_    (		unittest.TestCase				):




    @slow
    def 				__magic_name__				(       self		:			str ) ->				List[str]:




     '''simple docstring'''

     A__			=							TFMPNetModel.from_pretrained("microsoft/mpnet-base" )
     A__			=							tf.constant([[0, 1, 2, 3, 4, 5]] )
     A__			=							model(snake_case_ )[0]

     A__			=							[1, 6, 768]
     self.assertEqual(output.shape      ,					snake_case_ )

     A__			=							tf.constant(
         [
             [
                 [-0.106_7172, 0.0821_6473, 0.002_4543],
                 [-0.0346_5879, 0.835_4118, -0.0325_2288],
                 [-0.0656_9476, -0.1242_4111, -0.049_4436],
             ]
         ] )
     tf.debugging.assert_near(output[:, :3, :3]      ,					snake_case_      ,					atol=1e-4 )





