




import unittest

from transformers import MPNetConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    MPNetForMaskedLM,
		    MPNetForMultipleChoice,
		    MPNetForQuestionAnswering,
		    MPNetForSequenceClassification,
		    MPNetForTokenClassification,
		    MPNetModel,
		)
class 		UpperCAmelCase   :







						'''simple docstring'''





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






							"""simple docstring"""






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






						def        snake_case__						(						self							:     List[str]						):






							"""simple docstring"""






							return MPNetConfig.from_pretrained("microsoft/mpnet-base"						)






						def        snake_case__						(						self							:     Optional[int]						):






							"""simple docstring"""






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

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

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

							snake_case_		    =     self.get_config()
							return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels






						def        snake_case__						(						self							:     List[str]						):






							"""simple docstring"""






							return 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       ,	)






						def        snake_case__						(						self							:     List[str]       ,	__lowercase							:     int       ,	__lowercase							:     Any       ,	__lowercase							:     Union[str, Any]       ,	__lowercase							:     str       ,	__lowercase							:     Dict       ,	__lowercase							:     Union[str, Any]						):






							"""simple docstring"""






							snake_case_		    =     MPNetModel(config=__lowercase						)
							model.to(__lowercase						)
							model.eval()
							snake_case_		    =     model(__lowercase       ,	__lowercase						)
							snake_case_		    =     model(__lowercase						)
							self.parent.assertEqual(result.last_hidden_state.shape       ,	(self.batch_size, self.seq_length, self.hidden_size)						)
							self.parent.assertEqual(result.pooler_output.shape       ,	(self.batch_size, self.hidden_size)						)






						def        snake_case__						(						self							:     Dict       ,	__lowercase							:     List[Any]       ,	__lowercase							:     int       ,	__lowercase							:     str       ,	__lowercase							:     List[str]       ,	__lowercase							:     Union[str, Any]       ,	__lowercase							:     List[str]						):






							"""simple docstring"""






							snake_case_		    =     MPNetForQuestionAnswering(config=__lowercase						)
							model.to(__lowercase						)
							model.eval()
							snake_case_		    =     model(
							    __lowercase       ,	attention_mask=__lowercase       ,	start_positions=__lowercase       ,	end_positions=__lowercase       ,	)
							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        snake_case__						(						self							:     List[str]       ,	__lowercase							:     Optional[int]       ,	__lowercase							:     Any       ,	__lowercase							:     Dict       ,	__lowercase							:     str       ,	__lowercase							:     Optional[int]       ,	__lowercase							:     Optional[Any]						):






							"""simple docstring"""






							snake_case_		    =     self.num_labels
							snake_case_		    =     MPNetForSequenceClassification(__lowercase						)
							model.to(__lowercase						)
							model.eval()
							snake_case_		    =     model(__lowercase       ,	attention_mask=__lowercase       ,	labels=__lowercase						)
							self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.num_labels)						)






						def        snake_case__						(						self							:     Any       ,	__lowercase							:     Any       ,	__lowercase							:     Optional[Any]       ,	__lowercase							:     Optional[Any]       ,	__lowercase							:     List[str]       ,	__lowercase							:     Optional[int]       ,	__lowercase							:     Dict						):






							"""simple docstring"""






							snake_case_		    =     self.num_choices
							snake_case_		    =     MPNetForMultipleChoice(config=__lowercase						)
							model.to(__lowercase						)
							model.eval()
							snake_case_		    =     input_ids.unsqueeze(1						).expand(-1       ,	self.num_choices       ,	-1						).contiguous()
							snake_case_		    =     input_mask.unsqueeze(1						).expand(-1       ,	self.num_choices       ,	-1						).contiguous()
							snake_case_		    =     model(
							    __lowercase       ,	attention_mask=__lowercase       ,	labels=__lowercase       ,	)
							self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.num_choices)						)






						def        snake_case__						(						self							:     Dict       ,	__lowercase							:     Optional[int]       ,	__lowercase							:     List[Any]       ,	__lowercase							:     str       ,	__lowercase							:     Optional[Any]       ,	__lowercase							:     int       ,	__lowercase							:     Union[str, Any]						):






							"""simple docstring"""






							snake_case_		    =     self.num_labels
							snake_case_		    =     MPNetForTokenClassification(config=__lowercase						)
							model.to(__lowercase						)
							model.eval()
							snake_case_		    =     model(__lowercase       ,	attention_mask=__lowercase       ,	labels=__lowercase						)
							self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.seq_length, self.num_labels)						)






						def        snake_case__						(						self							:     Optional[Any]						):






							"""simple docstring"""






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







@require_torch
class 		UpperCAmelCase   (				UpperCAmelCase__       ,   UpperCAmelCase__       ,   unittest.TestCase			):







						'''simple docstring'''





						lowerCAmelCase_     		=   (
						    (
						        MPNetForMaskedLM,
						        MPNetForMultipleChoice,
						        MPNetForQuestionAnswering,
						        MPNetForSequenceClassification,
						        MPNetForTokenClassification,
						        MPNetModel,
						    )
						    if is_torch_available()
						    else ()
						)
						lowerCAmelCase_     		=   (
						    {
						        '''feature-extraction''': MPNetModel,
						        '''fill-mask''': MPNetForMaskedLM,
						        '''question-answering''': MPNetForQuestionAnswering,
						        '''text-classification''': MPNetForSequenceClassification,
						        '''token-classification''': MPNetForTokenClassification,
						        '''zero-shot''': MPNetForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						lowerCAmelCase_     		=   False
						lowerCAmelCase_     		=   True






						def        snake_case__						(						self							:     List[Any]						):






							"""simple docstring"""






							snake_case_		    =     MPNetModelTester(self						)
							snake_case_		    =     ConfigTester(self       ,	config_class=__lowercase       ,	hidden_size=37						)






						def        snake_case__						(						self							:     List[str]						):






							"""simple docstring"""






							self.config_tester.run_common_tests()






						def        snake_case__						(						self							:     int						):






							"""simple docstring"""






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






						def        snake_case__						(						self							:     Any						):






							"""simple docstring"""






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






						def        snake_case__						(						self							:     Union[str, Any]						):






							"""simple docstring"""






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






						def        snake_case__						(						self							:     str						):






							"""simple docstring"""






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






						def        snake_case__						(						self							:     Any						):






							"""simple docstring"""






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







@require_torch
class 		UpperCAmelCase   (				unittest.TestCase			):







						'''simple docstring'''





						@slow
						def        snake_case__						(						self							:     Union[str, Any]						):






							"""simple docstring"""






							snake_case_		    =     MPNetModel.from_pretrained("microsoft/mpnet-base"						)
							snake_case_		    =     torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]						)
							snake_case_		    =     model(__lowercase						)[0]
							snake_case_		    =     torch.Size((1, 11, 7_68)						)
							self.assertEqual(output.shape       ,	__lowercase						)
							snake_case_		    =     torch.tensor(
							    [[[-0.0550, 0.1943, -0.0740], [-0.0562, 0.2211, -0.0579], [-0.0437, 0.3337, -0.0641]]]						)
							# compare the actual values for a slice.
							self.assertTrue(torch.allclose(output[:, :3, :3]       ,	__lowercase       ,	atol=1E-4						)						)






from collections import defaultdict
from typing import Optional

from ..image_utils import load_image
from ..utils import (
    add_end_docstrings,
    is_torch_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_torch_available():
		import torch

		from ..models.auto.modeling_auto import MODEL_FOR_MASK_GENERATION_MAPPING

lowercase__				:     Dict 		=						logging.get_logger(__name__)
@add_end_docstrings(UpperCAmelCase__			)
class 		UpperCAmelCase   (				UpperCAmelCase__			):







						'''simple docstring'''





						def __init__(						self							:     Optional[Any]       ,	**__lowercase							:     Union[str, Any]						):






							"""simple docstring"""






							super().__init__(**__lowercase						)
							requires_backends(self       ,	"vision"						)
							requires_backends(self       ,	"torch"						)

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

							self.check_model_type(__lowercase						)






						def        snake_case__						(						self							:     Optional[int]       ,	**__lowercase							:     Optional[Any]						):






							"""simple docstring"""






							snake_case_		    =     {}
							snake_case_		    =     {}
							snake_case_		    =     {}
							# preprocess args
							if "points_per_batch" in kwargs:
								snake_case_		    =     kwargs["points_per_batch"]
							if "points_per_crop" in kwargs:
								snake_case_		    =     kwargs["points_per_crop"]
							if "crops_n_layers" in kwargs:
								snake_case_		    =     kwargs["crops_n_layers"]
							if "crop_overlap_ratio" in kwargs:
								snake_case_		    =     kwargs["crop_overlap_ratio"]
							if "crop_n_points_downscale_factor" in kwargs:
								snake_case_		    =     kwargs["crop_n_points_downscale_factor"]
							# postprocess args
							if "pred_iou_thresh" in kwargs:
								snake_case_		    =     kwargs["pred_iou_thresh"]
							if "stability_score_offset" in kwargs:
								snake_case_		    =     kwargs["stability_score_offset"]
							if "mask_threshold" in kwargs:
								snake_case_		    =     kwargs["mask_threshold"]
							if "stability_score_thresh" in kwargs:
								snake_case_		    =     kwargs["stability_score_thresh"]
							if "crops_nms_thresh" in kwargs:
								snake_case_		    =     kwargs["crops_nms_thresh"]
							if "output_rle_mask" in kwargs:
								snake_case_		    =     kwargs["output_rle_mask"]
							if "output_bboxes_mask" in kwargs:
								snake_case_		    =     kwargs["output_bboxes_mask"]
							return preprocess_kwargs, forward_params, postprocess_kwargs






						def __call__(						self							:     Optional[int]       ,	__lowercase							:     List[str]       ,	*__lowercase							:     Optional[Any]       ,	__lowercase							:     Dict=None       ,	__lowercase							:     List[str]=None       ,	**__lowercase							:     Optional[Any]						):






							"""simple docstring"""






							return super().__call__(__lowercase       ,	*__lowercase       ,	num_workers=__lowercase       ,	batch_size=__lowercase       ,	**__lowercase						)






						def        snake_case__						(						self							:     str       ,	__lowercase							:     int       ,	__lowercase							:     List[str]=64       ,	__lowercase							:     int = 0       ,	__lowercase							:     float = 5_12 / 15_00       ,	__lowercase							:     Optional[int] = 32       ,	__lowercase							:     Optional[int] = 1       ,	):






							"""simple docstring"""






							snake_case_		    =     load_image(__lowercase						)
							snake_case_		    =     self.image_processor.size["longest_edge"]
							snake_case_    ,      snake_case_    ,      snake_case_    ,      snake_case_		    =     self.image_processor.generate_crop_boxes(
							    __lowercase       ,	__lowercase       ,	__lowercase       ,	__lowercase       ,	__lowercase       ,	__lowercase						)
							snake_case_		    =     self.image_processor(images=__lowercase       ,	return_tensors="pt"						)

							with self.device_placement():
								if self.framework == "pt":
									snake_case_		    =     self.get_inference_context()
									with inference_context():
										snake_case_		    =     self._ensure_tensor_on_device(__lowercase       ,	device=self.device						)
										snake_case_		    =     self.model.get_image_embeddings(model_inputs.pop("pixel_values"						)						)
										snake_case_		    =     image_embeddings

							snake_case_		    =     grid_points.shape[1]
							snake_case_		    =     points_per_batch if points_per_batch is not None else n_points

							if points_per_batch <= 0:
								raise ValueError(
								    "Cannot have points_per_batch<=0. Must be >=1 to returned batched outputs. "
								    "To return all points at once, set points_per_batch to None"						)

							for i in range(0       ,	__lowercase       ,	__lowercase						):
								snake_case_		    =     grid_points[:, i : i + points_per_batch, :, :]
								snake_case_		    =     input_labels[:, i : i + points_per_batch]
								snake_case_		    =     i == n_points - points_per_batch
								yield {
								    "input_points": batched_points,
								    "input_labels": labels,
								    "input_boxes": crop_boxes,
								    "is_last": is_last,
								    **model_inputs,
								}






						def        snake_case__						(						self							:     Tuple       ,	__lowercase							:     Union[str, Any]       ,	__lowercase							:     Union[str, Any]=0.88       ,	__lowercase							:     Union[str, Any]=0.95       ,	__lowercase							:     int=0       ,	__lowercase							:     int=1       ,	):






							"""simple docstring"""






							snake_case_		    =     model_inputs.pop("input_boxes"						)
							snake_case_		    =     model_inputs.pop("is_last"						)
							snake_case_		    =     model_inputs.pop("original_sizes"						).tolist()
							snake_case_		    =     model_inputs.pop("reshaped_input_sizes"						).tolist()

							snake_case_		    =     self.model(**__lowercase						)

							# post processing happens here in order to avoid CPU GPU copies of ALL the masks
							snake_case_		    =     model_outputs["pred_masks"]
							snake_case_		    =     self.image_processor.post_process_masks(
							    __lowercase       ,	__lowercase       ,	__lowercase       ,	__lowercase       ,	binarize=__lowercase						)
							snake_case_		    =     model_outputs["iou_scores"]
							snake_case_    ,      snake_case_    ,      snake_case_		    =     self.image_processor.filter_masks(
							    masks[0]       ,	iou_scores[0]       ,	original_sizes[0]       ,	input_boxes[0]       ,	__lowercase       ,	__lowercase       ,	__lowercase       ,	__lowercase       ,	)
							return {
							    "masks": masks,
							    "is_last": is_last,
							    "boxes": boxes,
							    "iou_scores": iou_scores,
							}






						def        snake_case__						(						self							:     str       ,	__lowercase							:     Any       ,	__lowercase							:     Optional[int]=False       ,	__lowercase							:     int=False       ,	__lowercase							:     List[str]=0.7       ,	):






							"""simple docstring"""






							snake_case_		    =     []
							snake_case_		    =     []
							snake_case_		    =     []
							for model_output in model_outputs:
								all_scores.append(model_output.pop("iou_scores"						)						)
								all_masks.extend(model_output.pop("masks"						)						)
								all_boxes.append(model_output.pop("boxes"						)						)

							snake_case_		    =     torch.cat(__lowercase						)
							snake_case_		    =     torch.cat(__lowercase						)
							snake_case_    ,      snake_case_    ,      snake_case_    ,      snake_case_		    =     self.image_processor.post_process_for_mask_generation(
							    __lowercase       ,	__lowercase       ,	__lowercase       ,	__lowercase						)

							snake_case_		    =     defaultdict(__lowercase						)
							for output in model_outputs:
								for k, v in output.items():
									extra[k].append(__lowercase						)

							snake_case_		    =     {}
							if output_rle_mask:
								snake_case_		    =     rle_mask

							if output_bboxes_mask:
								snake_case_		    =     bounding_boxes

							return {"masks": output_masks, "scores": iou_scores, **optional, **extra}
