

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 						lowercase     :


		"""simple docstring"""




		def __init__(					self						,							__snake_case						,							__snake_case=13						,							__snake_case=7						,							__snake_case=True						,							__snake_case=True						,							__snake_case=False						,							__snake_case=True						,							__snake_case=99						,							__snake_case=64						,							__snake_case=2						,							__snake_case=4						,							__snake_case=64						,							__snake_case="gelu"						,							__snake_case=0.1						,							__snake_case=0.1						,							__snake_case=5_12						,							__snake_case=16						,							__snake_case=2						,							__snake_case=0.0_2						,							__snake_case=3						,							__snake_case=4						,							__snake_case=None						,							):
			_UpperCamelCase :		Any								=    parent
			_UpperCamelCase :		Tuple								=    batch_size
			_UpperCamelCase :		Optional[int]								=    seq_length
			_UpperCamelCase :		str								=    is_training
			_UpperCamelCase :		Tuple								=    use_input_mask
			_UpperCamelCase :		int								=    use_token_type_ids
			_UpperCamelCase :		Optional[int]								=    use_labels
			_UpperCamelCase :		Optional[Any]								=    vocab_size
			_UpperCamelCase :		Optional[int]								=    hidden_size
			_UpperCamelCase :		Any								=    num_hidden_layers
			_UpperCamelCase :		str								=    num_attention_heads
			_UpperCamelCase :		Union[str, Any]								=    intermediate_size
			_UpperCamelCase :		str								=    hidden_act
			_UpperCamelCase :		Dict								=    hidden_dropout_prob
			_UpperCamelCase :		str								=    attention_probs_dropout_prob
			_UpperCamelCase :		Dict								=    max_position_embeddings
			_UpperCamelCase :		Tuple								=    type_vocab_size
			_UpperCamelCase :		Tuple								=    type_sequence_label_size
			_UpperCamelCase :		List[Any]								=    initializer_range
			_UpperCamelCase :		Any								=    num_labels
			_UpperCamelCase :		List[str]								=    num_choices
			_UpperCamelCase :		Union[str, Any]								=    scope


		def 		A__       (					self):
			_UpperCamelCase :		Optional[int]								=    ids_tensor([self.batch_size, self.seq_length]						,							self.vocab_size)

			_UpperCamelCase :		Tuple								=    None
			if self.use_input_mask:
				_UpperCamelCase :		int								=    random_attention_mask([self.batch_size, self.seq_length])

			_UpperCamelCase :		Any								=    None
			_UpperCamelCase :		Optional[Any]								=    None
			_UpperCamelCase :		Optional[Any]								=    None
			if self.use_labels:
				_UpperCamelCase :		Union[str, Any]								=    ids_tensor([self.batch_size]						,							self.type_sequence_label_size)
				_UpperCamelCase :		List[Any]								=    ids_tensor([self.batch_size, self.seq_length]						,							self.num_labels)
				_UpperCamelCase :		Tuple								=    ids_tensor([self.batch_size]						,							self.num_choices)

			_UpperCamelCase :		Optional[int]								=    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 		A__       (					self						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case):
			_UpperCamelCase :		Optional[int]								=    TFMPNetModel(config=__snake_case)
			_UpperCamelCase :		Optional[int]								=    {'input_ids': input_ids, 'attention_mask': input_mask}
			_UpperCamelCase :		Union[str, Any]								=    model(__snake_case)
			_UpperCamelCase :		Optional[Any]								=    [input_ids, input_mask]
			_UpperCamelCase :		List[str]								=    model(__snake_case)
			self.parent.assertEqual(result.last_hidden_state.shape						,							(self.batch_size, self.seq_length, self.hidden_size))


		def 		A__       (					self						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case):
			_UpperCamelCase :		Optional[Any]								=    TFMPNetForMaskedLM(config=__snake_case)
			_UpperCamelCase :		Any								=    {'input_ids': input_ids, 'attention_mask': input_mask}
			_UpperCamelCase :		Tuple								=    model(__snake_case)
			self.parent.assertEqual(result.logits.shape						,							(self.batch_size, self.seq_length, self.vocab_size))


		def 		A__       (					self						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case):
			_UpperCamelCase :		Union[str, Any]								=    TFMPNetForQuestionAnswering(config=__snake_case)
			_UpperCamelCase :		str								=    {
			    'input_ids': input_ids,
			    'attention_mask': input_mask,
			}
			_UpperCamelCase :		int								=    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 		A__       (					self						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case):
			_UpperCamelCase :		Tuple								=    self.num_labels
			_UpperCamelCase :		Dict								=    TFMPNetForSequenceClassification(__snake_case)
			_UpperCamelCase :		Union[str, Any]								=    {'input_ids': input_ids, 'attention_mask': input_mask}
			_UpperCamelCase :		Any								=    model(__snake_case)
			self.parent.assertEqual(result.logits.shape						,							(self.batch_size, self.num_labels))


		def 		A__       (					self						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case):
			_UpperCamelCase :		Tuple								=    self.num_choices
			_UpperCamelCase :		List[Any]								=    TFMPNetForMultipleChoice(__snake_case)
			_UpperCamelCase :		List[str]								=    tf.tile(tf.expand_dims(__snake_case						,							1)						,							(1, self.num_choices, 1))
			_UpperCamelCase :		Union[str, Any]								=    tf.tile(tf.expand_dims(__snake_case						,							1)						,							(1, self.num_choices, 1))
			_UpperCamelCase :		Tuple								=    {
			    'input_ids': multiple_choice_inputs_ids,
			    'attention_mask': multiple_choice_input_mask,
			}
			_UpperCamelCase :		str								=    model(__snake_case)
			self.parent.assertEqual(result.logits.shape						,							(self.batch_size, self.num_choices))


		def 		A__       (					self						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case):
			_UpperCamelCase :		Any								=    self.num_labels
			_UpperCamelCase :		Any								=    TFMPNetForTokenClassification(__snake_case)
			_UpperCamelCase :		Dict								=    {'input_ids': input_ids, 'attention_mask': input_mask}
			_UpperCamelCase :		List[str]								=    model(__snake_case)
			self.parent.assertEqual(result.logits.shape						,							(self.batch_size, self.seq_length, self.num_labels))






		def 		A__       (					self):
			_UpperCamelCase :		int								=    self.prepare_config_and_inputs()
			((_UpperCamelCase)						,		(_UpperCamelCase)						,		(_UpperCamelCase)						,		(_UpperCamelCase)						,		(_UpperCamelCase)						,		(_UpperCamelCase)) :		int								=    config_and_inputs
			_UpperCamelCase :		Optional[int]								=    {'input_ids': input_ids, 'attention_mask': input_mask}
			return config, inputs_dict







@require_tf
class 						lowercase     (					_lowercase     ,				_lowercase     ,				unittest.TestCase				):


		"""simple docstring"""




		a__       							=	(
		    (
		        TFMPNetForMaskedLM,
		        TFMPNetForMultipleChoice,
		        TFMPNetForQuestionAnswering,
		        TFMPNetForSequenceClassification,
		        TFMPNetForTokenClassification,
		        TFMPNetModel,
		    )
		    if is_tf_available()
		    else ()
		)
		a__       							=	(
		    {
		        "feature-extraction": TFMPNetModel,
		        "fill-mask": TFMPNetForMaskedLM,
		        "question-answering": TFMPNetForQuestionAnswering,
		        "text-classification": TFMPNetForSequenceClassification,
		        "token-classification": TFMPNetForTokenClassification,
		        "zero-shot": TFMPNetForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)
		a__       							=	False
		a__       							=	False


		def 		A__       (					self):
			_UpperCamelCase :		Tuple								=    TFMPNetModelTester(self)
			_UpperCamelCase :		Optional[int]								=    ConfigTester(self						,							config_class=__snake_case						,							hidden_size=37)


		def 		A__       (					self):
			self.config_tester.run_common_tests()


		def 		A__       (					self):
			_UpperCamelCase :		Dict								=    self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_model(*__snake_case)


		def 		A__       (					self):
			_UpperCamelCase :		List[Any]								=    self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_for_masked_lm(*__snake_case)


		def 		A__       (					self):
			_UpperCamelCase :		Union[str, Any]								=    self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_for_question_answering(*__snake_case)


		def 		A__       (					self):
			_UpperCamelCase :		Optional[Any]								=    self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_for_sequence_classification(*__snake_case)


		def 		A__       (					self):
			_UpperCamelCase :		Optional[Any]								=    self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_for_multiple_choice(*__snake_case)


		def 		A__       (					self):
			_UpperCamelCase :		str								=    self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_for_token_classification(*__snake_case)






		@slow
		def 		A__       (					self):
			for model_name in ["microsoft/mpnet-base"]:
				_UpperCamelCase :		Optional[Any]								=    TFMPNetModel.from_pretrained(__snake_case)
				self.assertIsNotNone(__snake_case)







@require_tf
class 						lowercase     (					unittest.TestCase				):


		"""simple docstring"""




		@slow
		def 		A__       (					self):
			_UpperCamelCase :		Dict								=    TFMPNetModel.from_pretrained('microsoft/mpnet-base')
			_UpperCamelCase :		Dict								=    tf.constant([[0, 1, 2, 3, 4, 5]])
			_UpperCamelCase :		Optional[Any]								=    model(__snake_case)[0]

			_UpperCamelCase :		List[Any]								=    [1, 6, 7_68]
			self.assertEqual(output.shape						,							__snake_case)

			_UpperCamelCase :		Optional[int]								=    tf.constant(
			    [
			        [
			            [-0.1_0_6_7_1_7_2, 0.0_8_2_1_6_4_7_3, 0.0_0_2_4_5_4_3],
			            [-0.0_3_4_6_5_8_7_9, 0.8_3_5_4_1_1_8, -0.0_3_2_5_2_2_8_8],
			            [-0.0_6_5_6_9_4_7_6, -0.1_2_4_2_4_1_1_1, -0.0_4_9_4_4_3_6],
			        ]
			    ])
			tf.debugging.assert_near(output[:, :3, :3]						,							__snake_case						,							atol=1e-4)







import os

from apache_beam.io.filesystems import FileSystems
from apache_beam.pipeline import Pipeline

from .logging import get_logger


lowerCAmelCase__             =		2 << 2_0  # 2mb
lowerCAmelCase__             =		get_logger(__name__)







class 						lowercase     (					_lowercase				):


		"""simple docstring"""




		def 		A__       (					self):
			_UpperCamelCase :		Optional[Any]								=    self._options.get_all_options().get('runner')
			return runner in [None, "DirectRunner", "PortableRunner"]






def        lowerCamelCase_  (				UpperCAmelCase_				:  Any				,							UpperCAmelCase_				:  Optional[Any]				,							UpperCAmelCase_				:  str=False						)    ->					str:




	'''simple docstring'''




	_UpperCamelCase :		Optional[int]								=    FileSystems
	if fs.exists(UpperCAmelCase_						):
		if force_upload:
			logger.info(F'''Remote path already exist: {remote_file_path}. Overwriting it as force_upload=True.'''						)
		else:
			logger.info(F'''Remote path already exist: {remote_file_path}. Skipping it as force_upload=False.'''						)
			return
	with fs.create(UpperCAmelCase_						) as remote_file:
		with open(UpperCAmelCase_				,							'rb'						) as local_file:
			_UpperCamelCase :		List[str]								=    local_file.read(UpperCAmelCase_						)
			while chunk:
				remote_file.write(UpperCAmelCase_						)
				_UpperCamelCase :		List[str]								=    local_file.read(UpperCAmelCase_						)







def        lowerCamelCase_  (				UpperCAmelCase_				:  Any				,							UpperCAmelCase_				:  List[Any]				,							UpperCAmelCase_				:  Optional[int]=False						)    ->					Any:




	'''simple docstring'''




	_UpperCamelCase :		List[str]								=    FileSystems
	if os.path.exists(UpperCAmelCase_						):
		if force_download:
			logger.info(F'''Local path already exist: {remote_file_path}. Overwriting it as force_upload=True.'''						)
		else:
			logger.info(F'''Local path already exist: {remote_file_path}. Skipping it as force_upload=False.'''						)
			return
	with fs.open(UpperCAmelCase_						) as remote_file:
		with open(UpperCAmelCase_				,							'wb'						) as local_file:
			_UpperCamelCase :		Union[str, Any]								=    remote_file.read(UpperCAmelCase_						)
			while chunk:
				local_file.write(UpperCAmelCase_						)
				_UpperCamelCase :		List[str]								=    remote_file.read(UpperCAmelCase_						)




