



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





     '''simple docstring'''



     def __init__(      self ,			A ,			A=1_3 ,			A=7 ,			A=True ,			A=True ,			A=False ,			A=True ,			A=9_9 ,			A=6_4 ,			A=2 ,			A=4 ,			A=6_4 ,			A="gelu" ,			A=0.1 ,			A=0.1 ,			A=5_1_2 ,			A=1_6 ,			A=2 ,			A=0.02 ,			A=3 ,			A=4 ,			A=None ,			)							->      Union[str, Any]:
         _UpperCAmelCase							:						Any       							=					parent
         _UpperCAmelCase							:						Any       							=					batch_size
         _UpperCAmelCase							:						List[str]       							=					seq_length
         _UpperCAmelCase							:						Any       							=					is_training
         _UpperCAmelCase							:						str       							=					use_input_mask
         _UpperCAmelCase							:						Any       							=					use_token_type_ids
         _UpperCAmelCase							:						Any       							=					use_labels
         _UpperCAmelCase							:						str       							=					vocab_size
         _UpperCAmelCase							:						Dict       							=					hidden_size
         _UpperCAmelCase							:						str       							=					num_hidden_layers
         _UpperCAmelCase							:						List[Any]       							=					num_attention_heads
         _UpperCAmelCase							:						Union[str, Any]       							=					intermediate_size
         _UpperCAmelCase							:						str       							=					hidden_act
         _UpperCAmelCase							:						Union[str, Any]       							=					hidden_dropout_prob
         _UpperCAmelCase							:						Dict       							=					attention_probs_dropout_prob
         _UpperCAmelCase							:						List[str]       							=					max_position_embeddings
         _UpperCAmelCase							:						Dict       							=					type_vocab_size
         _UpperCAmelCase							:						List[Any]       							=					type_sequence_label_size
         _UpperCAmelCase							:						Union[str, Any]       							=					initializer_range
         _UpperCAmelCase							:						List[str]       							=					num_labels
         _UpperCAmelCase							:						Any       							=					num_choices
         _UpperCAmelCase							:						Optional[int]       							=					scope





     def 				__lowerCAmelCase			(      self	)							->      Union[str, Any]:
         _UpperCAmelCase							:						str       							=					ids_tensor([self.batch_size, self.seq_length] ,			self.vocab_size	)

         _UpperCAmelCase							:						Union[str, Any]       							=					None
         if self.use_input_mask:
             _UpperCAmelCase							:						Union[str, Any]       							=					random_attention_mask([self.batch_size, self.seq_length]	)

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

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





     def 				__lowerCAmelCase			(      self ,			A ,			A ,			A ,			A ,			A ,			A	)							->      str:
         _UpperCAmelCase							:						int       							=					TFMPNetForMaskedLM(config=A	)
         _UpperCAmelCase							:						Tuple       							=					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
         _UpperCAmelCase							:						List[str]       							=					model(A	)
         self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.seq_length, self.vocab_size)	)





     def 				__lowerCAmelCase			(      self ,			A ,			A ,			A ,			A ,			A ,			A	)							->      Dict:
         _UpperCAmelCase							:						List[Any]       							=					TFMPNetForQuestionAnswering(config=A	)
         _UpperCAmelCase							:						int       							=					{
             '''input_ids''': input_ids,
             '''attention_mask''': input_mask,
         }
         _UpperCAmelCase							:						int       							=					model(A	)
         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 				__lowerCAmelCase			(      self ,			A ,			A ,			A ,			A ,			A ,			A	)							->      Dict:
         _UpperCAmelCase							:						Optional[Any]       							=					self.num_labels
         _UpperCAmelCase							:						List[str]       							=					TFMPNetForSequenceClassification(A	)
         _UpperCAmelCase							:						str       							=					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
         _UpperCAmelCase							:						Dict       							=					model(A	)
         self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.num_labels)	)





     def 				__lowerCAmelCase			(      self ,			A ,			A ,			A ,			A ,			A ,			A	)							->      Optional[Any]:
         _UpperCAmelCase							:						int       							=					self.num_choices
         _UpperCAmelCase							:						Dict       							=					TFMPNetForMultipleChoice(A	)
         _UpperCAmelCase							:						str       							=					tf.tile(tf.expand_dims(A ,			1	) ,			(1, self.num_choices, 1)	)
         _UpperCAmelCase							:						Union[str, Any]       							=					tf.tile(tf.expand_dims(A ,			1	) ,			(1, self.num_choices, 1)	)
         _UpperCAmelCase							:						Tuple       							=					{
             '''input_ids''': multiple_choice_inputs_ids,
             '''attention_mask''': multiple_choice_input_mask,
         }
         _UpperCAmelCase							:						List[Any]       							=					model(A	)
         self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.num_choices)	)





     def 				__lowerCAmelCase			(      self ,			A ,			A ,			A ,			A ,			A ,			A	)							->      List[str]:
         _UpperCAmelCase							:						Dict       							=					self.num_labels
         _UpperCAmelCase							:						List[str]       							=					TFMPNetForTokenClassification(A	)
         _UpperCAmelCase							:						Union[str, Any]       							=					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
         _UpperCAmelCase							:						List[Any]       							=					model(A	)
         self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.seq_length, self.num_labels)	)





     def 				__lowerCAmelCase			(      self	)							->      Union[str, Any]:
         _UpperCAmelCase							:						List[str]       							=					self.prepare_config_and_inputs()
         ((_UpperCAmelCase)							,       (_UpperCAmelCase)							,       (_UpperCAmelCase)							,       (_UpperCAmelCase)							,       (_UpperCAmelCase)							,       (_UpperCAmelCase))							:						int       							=					config_and_inputs
         _UpperCAmelCase							:						int       							=					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
         return config, inputs_dict



@require_tf
class 							_UpperCAmelCase							(				a	,a	,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 				__lowerCAmelCase			(      self	)							->      Tuple:
         _UpperCAmelCase							:						List[str]       							=					TFMPNetModelTester(self	)
         _UpperCAmelCase							:						Optional[Any]       							=					ConfigTester(self ,			config_class=A ,			hidden_size=3_7	)





     def 				__lowerCAmelCase			(      self	)							->      Union[str, Any]:
         self.config_tester.run_common_tests()





     def 				__lowerCAmelCase			(      self	)							->      Optional[int]:
         _UpperCAmelCase							:						int       							=					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_model(*A	)





     def 				__lowerCAmelCase			(      self	)							->      str:
         _UpperCAmelCase							:						int       							=					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_masked_lm(*A	)





     def 				__lowerCAmelCase			(      self	)							->      Dict:
         _UpperCAmelCase							:						Optional[Any]       							=					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_question_answering(*A	)





     def 				__lowerCAmelCase			(      self	)							->      str:
         _UpperCAmelCase							:						Optional[int]       							=					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_sequence_classification(*A	)





     def 				__lowerCAmelCase			(      self	)							->      Any:
         _UpperCAmelCase							:						Optional[int]       							=					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_multiple_choice(*A	)





     def 				__lowerCAmelCase			(      self	)							->      List[Any]:
         _UpperCAmelCase							:						Union[str, Any]       							=					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_token_classification(*A	)





     @slow
     def 				__lowerCAmelCase			(      self	)							->      int:
         for model_name in ["microsoft/mpnet-base"]:
             _UpperCAmelCase							:						List[Any]       							=					TFMPNetModel.from_pretrained(A	)
             self.assertIsNotNone(A	)



@require_tf
class 							_UpperCAmelCase							(				unittest.TestCase   ):





     '''simple docstring'''



     @slow
     def 				__lowerCAmelCase			(      self	)							->      Dict:
         _UpperCAmelCase							:						Any       							=					TFMPNetModel.from_pretrained('''microsoft/mpnet-base'''	)
         _UpperCAmelCase							:						str       							=					tf.constant([[0, 1, 2, 3, 4, 5]]	)
         _UpperCAmelCase							:						int       							=					model(A	)[0]

         _UpperCAmelCase							:						Optional[int]       							=					[1, 6, 7_6_8]
         self.assertEqual(output.shape ,			A	)

         _UpperCAmelCase							:						List[Any]       							=					tf.constant(
             [
                 [
                     [-0.1_067_172, 0.08_216_473, 0.0_024_543],
                     [-0.03_465_879, 0.8_354_118, -0.03_252_288],
                     [-0.06_569_476, -0.12_424_111, -0.0_494_436],
                 ]
             ]	)
         tf.debugging.assert_near(output[:, :3, :3] ,			A ,			atol=1E-4	)







"""simple docstring"""
import os

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

from .logging import get_logger


_lowerCAmelCase					:Optional[int]  =      2 << 20  # 2mb
_lowerCAmelCase					:Union[str, Any]  =      get_logger(__name__)
class 							_UpperCAmelCase							(				a   ):





     '''simple docstring'''



     def 				__lowerCAmelCase			(      self	)							->      Any:
         _UpperCAmelCase							:						str       							=					self._options.get_all_options().get('''runner'''	)
         return runner in [None, "DirectRunner", "PortableRunner"]





def 				lowerCamelCase_		(UpperCamelCase__	:	List[str]  , UpperCamelCase__	:	str  , UpperCamelCase__	:	Dict=False							):
    _UpperCAmelCase							:						Union[str, Any]       							=					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							:						Optional[Any]       							=					local_file.read(UpperCamelCase__							)
            while chunk:
                remote_file.write(UpperCamelCase__							)
                _UpperCAmelCase							:						List[Any]       							=					local_file.read(UpperCamelCase__							)





def 				lowerCamelCase_		(UpperCamelCase__	:	int  , UpperCamelCase__	:	Dict  , UpperCamelCase__	:	str=False							):
    _UpperCAmelCase							:						int       							=					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							:						List[str]       							=					remote_file.read(UpperCamelCase__							)
            while chunk:
                local_file.write(UpperCamelCase__							)
                _UpperCAmelCase							:						List[Any]       							=					remote_file.read(UpperCamelCase__							)


