


import unittest

from transformers import ErnieMConfig, 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 (
       ErnieMForInformationExtraction,
       ErnieMForMultipleChoice,
       ErnieMForQuestionAnswering,
       ErnieMForSequenceClassification,
       ErnieMForTokenClassification,
       ErnieMModel,
   )
   from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST

class  UpperCamelCase__ :






      def __init__(     self					:   Tuple,		__lowerCamelCase					:   int,		__lowerCamelCase					:   int=13,		__lowerCamelCase					:   Optional[int]=7,		__lowerCamelCase					:   str=True,		__lowerCamelCase					:   str=True,		__lowerCamelCase					:   Tuple=True,		__lowerCamelCase					:   Union[str, Any]=99,		__lowerCamelCase					:   Optional[int]=32,		__lowerCamelCase					:   List[str]=5,		__lowerCamelCase					:   Tuple=4,		__lowerCamelCase					:   List[str]=37,		__lowerCamelCase					:   int="gelu",		__lowerCamelCase					:   Any=0.1,		__lowerCamelCase					:   Union[str, Any]=0.1,		__lowerCamelCase					:   Dict=5_12,		__lowerCamelCase					:   List[str]=16,		__lowerCamelCase					:   Tuple=2,		__lowerCamelCase					:   Optional[Any]=0.02,		__lowerCamelCase					:   Tuple=3,		__lowerCamelCase					:   List[Any]=4,		__lowerCamelCase					:   Optional[Any]=None,		)  -> int:
           UpperCamelCase__							:  Any								=     parent
           UpperCamelCase__							:  str								=     batch_size
           UpperCamelCase__							:  int								=     seq_length
           UpperCamelCase__							:  List[Any]								=     is_training
           UpperCamelCase__							:  List[str]								=     use_input_mask
           UpperCamelCase__							:  List[Any]								=     use_labels
           UpperCamelCase__							:  List[str]								=     vocab_size
           UpperCamelCase__							:  int								=     hidden_size
           UpperCamelCase__							:  Optional[Any]								=     num_hidden_layers
           UpperCamelCase__							:  str								=     num_attention_heads
           UpperCamelCase__							:  int								=     intermediate_size
           UpperCamelCase__							:  List[str]								=     hidden_act
           UpperCamelCase__							:  Optional[int]								=     hidden_dropout_prob
           UpperCamelCase__							:  Tuple								=     attention_probs_dropout_prob
           UpperCamelCase__							:  Optional[int]								=     max_position_embeddings
           UpperCamelCase__							:  Tuple								=     type_vocab_size
           UpperCamelCase__							:  Tuple								=     type_sequence_label_size
           UpperCamelCase__							:  Union[str, Any]								=     initializer_range
           UpperCamelCase__							:  List[Any]								=     num_labels
           UpperCamelCase__							:  Optional[int]								=     num_choices
           UpperCamelCase__							:  Tuple								=     scope






      def 	__lowercase(     self					:   Optional[Any]   )  -> Tuple:
           UpperCamelCase__							:  str								=     ids_tensor([self.batch_size, self.seq_length],		self.vocab_size   )

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

           UpperCamelCase__							:  List[Any]								=     None
           UpperCamelCase__							:  Optional[Any]								=     None
           UpperCamelCase__							:  Dict								=     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__							:  List[Any]								=     ids_tensor([self.batch_size],		self.num_choices   )

           UpperCamelCase__							:  List[Any]								=     self.get_config()

           return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels






      def 	__lowercase(     self					:   List[str]   )  -> Dict:
           UpperCamelCase__							:  int								=     ids_tensor([self.batch_size, self.seq_length],		self.vocab_size   )

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

           return config, input_ids, input_mask






      def 	__lowercase(     self					:   Any   )  -> Union[str, Any]:
           return ErnieMConfig(
               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,		type_vocab_size=self.type_vocab_size,		initializer_range=self.initializer_range,		)






      def 	__lowercase(     self					:   List[Any],		__lowerCamelCase					:   int,		__lowerCamelCase					:   Dict,		__lowerCamelCase					:   str,		__lowerCamelCase					:   Optional[Any],		__lowerCamelCase					:   Union[str, Any],		__lowerCamelCase					:   str   )  -> Union[str, Any]:
           UpperCamelCase__							:  int								=     ErnieMModel(config=__lowerCamelCase   )
           model.to(__lowerCamelCase   )
           model.eval()
           UpperCamelCase__							:  Any								=     model(__lowerCamelCase,		return_dict=__lowerCamelCase   )
           self.parent.assertEqual(result.last_hidden_state.shape,		(self.batch_size, self.seq_length, self.hidden_size)   )






      def 	__lowercase(     self					:   Dict,		__lowerCamelCase					:   Optional[Any],		__lowerCamelCase					:   Optional[Any],		__lowerCamelCase					:   Dict,		__lowerCamelCase					:   Tuple,		__lowerCamelCase					:   Optional[int],		__lowerCamelCase					:   str   )  -> Dict:
           UpperCamelCase__							:  Union[str, Any]								=     ErnieMForQuestionAnswering(config=__lowerCamelCase   )
           model.to(__lowerCamelCase   )
           model.eval()
           UpperCamelCase__							:  str								=     model(
               __lowerCamelCase,		attention_mask=__lowerCamelCase,		start_positions=__lowerCamelCase,		end_positions=__lowerCamelCase,		)
           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 	__lowercase(     self					:   Union[str, Any],		__lowerCamelCase					:   str,		__lowerCamelCase					:   List[Any],		__lowerCamelCase					:   str,		__lowerCamelCase					:   Tuple,		__lowerCamelCase					:   List[Any],		__lowerCamelCase					:   Optional[int]   )  -> str:
           UpperCamelCase__							:  List[Any]								=     ErnieMForInformationExtraction(config=__lowerCamelCase   )
           model.to(__lowerCamelCase   )
           model.eval()
           UpperCamelCase__							:  Union[str, Any]								=     torch.ones_like(__lowerCamelCase,		dtype=torch.floataa   )
           UpperCamelCase__							:  Dict								=     model(
               __lowerCamelCase,		attention_mask=__lowerCamelCase,		start_positions=__lowerCamelCase,		end_positions=__lowerCamelCase,		)
           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 	__lowercase(     self					:   Any,		__lowerCamelCase					:   List[str],		__lowerCamelCase					:   Dict,		__lowerCamelCase					:   Optional[Any],		__lowerCamelCase					:   Any,		__lowerCamelCase					:   Optional[Any],		__lowerCamelCase					:   List[str]   )  -> int:
           UpperCamelCase__							:  List[str]								=     self.num_labels
           UpperCamelCase__							:  Dict								=     ErnieMForSequenceClassification(__lowerCamelCase   )
           model.to(__lowerCamelCase   )
           model.eval()
           UpperCamelCase__							:  Any								=     model(__lowerCamelCase,		attention_mask=__lowerCamelCase,		labels=__lowerCamelCase   )
           self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.num_labels)   )






      def 	__lowercase(     self					:   str,		__lowerCamelCase					:   Tuple,		__lowerCamelCase					:   Optional[int],		__lowerCamelCase					:   Union[str, Any],		__lowerCamelCase					:   Any,		__lowerCamelCase					:   Dict,		__lowerCamelCase					:   List[str]   )  -> str:
           UpperCamelCase__							:  Any								=     self.num_labels
           UpperCamelCase__							:  List[Any]								=     ErnieMForTokenClassification(config=__lowerCamelCase   )
           model.to(__lowerCamelCase   )
           model.eval()
           input_ids.to(__lowerCamelCase   )
           input_mask.to(__lowerCamelCase   )
           token_labels.to(__lowerCamelCase   )

           UpperCamelCase__							:  Optional[Any]								=     model(__lowerCamelCase,		attention_mask=__lowerCamelCase,		labels=__lowerCamelCase   )

           self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.seq_length, self.num_labels)   )






      def 	__lowercase(     self					:   List[str],		__lowerCamelCase					:   Union[str, Any],		__lowerCamelCase					:   Tuple,		__lowerCamelCase					:   List[Any],		__lowerCamelCase					:   int,		__lowerCamelCase					:   Optional[Any],		__lowerCamelCase					:   Dict   )  -> Optional[int]:
           UpperCamelCase__							:  Optional[Any]								=     self.num_choices
           UpperCamelCase__							:  Any								=     ErnieMForMultipleChoice(config=__lowerCamelCase   )
           model.to(__lowerCamelCase   )
           model.eval()
           UpperCamelCase__							:  Optional[Any]								=     input_ids.unsqueeze(1   ).expand(-1,		self.num_choices,		-1   ).contiguous()
           UpperCamelCase__							:  Dict								=     input_mask.unsqueeze(1   ).expand(-1,		self.num_choices,		-1   ).contiguous()
           UpperCamelCase__							:  Dict								=     model(
               __lowerCamelCase,		attention_mask=__lowerCamelCase,		labels=__lowerCamelCase,		)
           self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.num_choices)   )






      def 	__lowercase(     self					:   Union[str, Any]   )  -> int:
           UpperCamelCase__							:  Tuple								=     self.prepare_config_and_inputs()
           (
               (
               UpperCamelCase__
           )	,(
               UpperCamelCase__
           )	,(
               UpperCamelCase__
           )	,(
               UpperCamelCase__
           )	,(
               UpperCamelCase__
           )	,(
               UpperCamelCase__
           )	,
           )							:  Any								=     config_and_inputs
           UpperCamelCase__							:  Optional[int]								=     {'''input_ids''': input_ids, '''attention_mask''': input_mask}
           return config, inputs_dict

@require_torch
class  UpperCamelCase__ ( _a							,						_a							,						unittest.TestCase			):
      a__   :     Optional[Any]								=						(
          (
              ErnieMModel,
              ErnieMForMultipleChoice,
              ErnieMForQuestionAnswering,
              ErnieMForSequenceClassification,
              ErnieMForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      a__   :     Union[str, Any]								=						()
      a__   :     Optional[int]								=						(
          {
              'feature-extraction': ErnieMModel,
              'question-answering': ErnieMForQuestionAnswering,
              'text-classification': ErnieMForSequenceClassification,
              'token-classification': ErnieMForTokenClassification,
              'zero-shot': ErnieMForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      a__   :     List[Any]								=						False






      def 	__lowercase(     self					:   List[Any],		__lowerCamelCase					:   List[str],		__lowerCamelCase					:   Optional[Any],		__lowerCamelCase					:   Any,		__lowerCamelCase					:   str,		__lowerCamelCase					:   str   )  -> Optional[Any]:
           if pipeline_test_casse_name == "QAPipelineTests":
                return True

           return False






      def 	__lowercase(     self					:   List[Any]   )  -> Dict:
           UpperCamelCase__							:  int								=     ErnieMModelTester(self   )
           UpperCamelCase__							:  List[str]								=     ConfigTester(self,		config_class=__lowerCamelCase,		hidden_size=37   )






      def 	__lowercase(     self					:   List[Any]   )  -> int:
           self.config_tester.run_common_tests()






      def 	__lowercase(     self					:   Optional[Any]   )  -> Optional[int]:
           UpperCamelCase__							:  str								=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__lowerCamelCase   )






      def 	__lowercase(     self					:   Union[str, Any]   )  -> Dict:
           UpperCamelCase__							:  Tuple								=     self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
                UpperCamelCase__							:  Union[str, Any]								=     type
                self.model_tester.create_and_check_model(*__lowerCamelCase   )






      def 	__lowercase(     self					:   List[Any]   )  -> Dict:
           UpperCamelCase__							:  Tuple								=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*__lowerCamelCase   )






      def 	__lowercase(     self					:   Union[str, Any]   )  -> Any:
           UpperCamelCase__							:  Optional[int]								=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase   )






      def 	__lowercase(     self					:   Tuple   )  -> Optional[Any]:
           UpperCamelCase__							:  Union[str, Any]								=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase   )






      def 	__lowercase(     self					:   Union[str, Any]   )  -> List[str]:
           UpperCamelCase__							:  List[Any]								=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_information_extraction(*__lowerCamelCase   )






      def 	__lowercase(     self					:   List[str]   )  -> List[Any]:
           UpperCamelCase__							:  List[str]								=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase   )






      @slow
      def 	__lowercase(     self					:   Optional[int]   )  -> str:
           for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCamelCase__							:  Optional[Any]								=     ErnieMModel.from_pretrained(__lowerCamelCase   )
                self.assertIsNotNone(__lowerCamelCase   )

@require_torch
class  UpperCamelCase__ ( unittest.TestCase			):






      @slow
      def 	__lowercase(     self					:   List[str]   )  -> Union[str, Any]:
           UpperCamelCase__							:  Any								=     ErnieMModel.from_pretrained('''susnato/ernie-m-base_pytorch'''   )
           model.eval()
           UpperCamelCase__							:  Optional[Any]								=     torch.tensor([[0, 1, 2, 3, 4, 5]]   )
           UpperCamelCase__							:  Dict								=     model(__lowerCamelCase   )[0]

           # TODO Replace vocab size
           UpperCamelCase__							:  Optional[Any]								=     7_68

           UpperCamelCase__							:  List[Any]								=     torch.Size((1, 6, hidden_size)   )
           self.assertEqual(output.shape,		__lowerCamelCase   )

           UpperCamelCase__							:  str								=     torch.tensor(
               [[[-0.0012, 0.1245, -0.0214], [-0.0742, 0.0244, -0.0771], [-0.0333, 0.1164, -0.1554]]]   )

           self.assertTrue(torch.allclose(output[:, :3, :3],		__lowerCamelCase,		atol=1e-3   )   )





import os

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

from .logging import get_logger


_SCREAMING_SNAKE_CASE :					Optional[int]			=	2 << 20  # 2mb
_SCREAMING_SNAKE_CASE :					Optional[Any]			=	get_logger(__name__)

class  UpperCamelCase__ ( __lowerCamelCase			):






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







def 						_lowercase       (     __lowerCamelCase			:					List[str]		,__lowerCamelCase			:					Any		,__lowerCamelCase			:					List[str]=False			)  ->   Optional[int]:





     '''simple docstring'''





     UpperCamelCase__							:  Any								=     FileSystems
     if fs.exists(__lowerCamelCase			):
          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(__lowerCamelCase			) as remote_file:
          with open(__lowerCamelCase		,'''rb'''			) as local_file:
               UpperCamelCase__							:  List[Any]								=     local_file.read(__lowerCamelCase			)
               while chunk:
                    remote_file.write(__lowerCamelCase			)
                    UpperCamelCase__							:  Union[str, Any]								=     local_file.read(__lowerCamelCase			)







def 						_lowercase       (     __lowerCamelCase			:					Any		,__lowerCamelCase			:					int		,__lowerCamelCase			:					str=False			)  ->   Tuple:





     '''simple docstring'''





     UpperCamelCase__							:  Optional[int]								=     FileSystems
     if os.path.exists(__lowerCamelCase			):
          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(__lowerCamelCase			) as remote_file:
          with open(__lowerCamelCase		,'''wb'''			) as local_file:
               UpperCamelCase__							:  Any								=     remote_file.read(__lowerCamelCase			)
               while chunk:
                    local_file.write(__lowerCamelCase			)
                    UpperCamelCase__							:  Dict								=     remote_file.read(__lowerCamelCase			)




