code: 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=512      ,      lowerCAmelCase  :   List[str]=16      ,      lowerCAmelCase  :   Tuple=2      ,      lowerCAmelCase  :   Optional[Any]=0.02      ,      lowerCAmelCase  :   Tuple=3      ,      lowerCAmelCase  :   List[Any]=4      ,      lowerCAmelCase  :   Optional[Any]=None      ,      )	-> int:
        a__     		=     parent
        a__     		=     batch_size
        a__     		=     seq_length
        a__     		=     is_training
        a__     		=     use_input_mask
        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 						__lowerCamelCase (					self  :   Optional[Any]   )	-> Tuple:
        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__     		=     self.get_config()
  
        return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
  
  
  
  
      def 						__lowerCamelCase (					self  :   List[str]   )	-> Dict:
        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__     		=     self.get_config()
  
        return config, input_ids, input_mask
  
  
  
  
      def 						__lowerCamelCase (					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 						__lowerCamelCase (					self  :   List[Any]      ,      lowerCAmelCase  :   int      ,      lowerCAmelCase  :   Dict      ,      lowerCAmelCase  :   str      ,      lowerCAmelCase  :   Optional[Any]      ,      lowerCAmelCase  :   Union[str, Any]      ,      lowerCAmelCase  :   str   )	-> Union[str, Any]:
        a__     		=     ErnieMModel(config=lowerCAmelCase   )
        model.to(lowerCAmelCase   )
        model.eval()
        a__     		=     model(lowerCAmelCase      ,      return_dict=lowerCAmelCase   )
        self.parent.assertEqual(result.last_hidden_state.shape      ,      (self.batch_size, self.seq_length, self.hidden_size)   )
  
  
  
  
      def 						__lowerCamelCase (					self  :   Dict      ,      lowerCAmelCase  :   Optional[Any]      ,      lowerCAmelCase  :   Optional[Any]      ,      lowerCAmelCase  :   Dict      ,      lowerCAmelCase  :   Tuple      ,      lowerCAmelCase  :   Optional[int]      ,      lowerCAmelCase  :   str   )	-> Dict:
        a__     		=     ErnieMForQuestionAnswering(config=lowerCAmelCase   )
        model.to(lowerCAmelCase   )
        model.eval()
        a__     		=     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 						__lowerCamelCase (					self  :   Union[str, Any]      ,      lowerCAmelCase  :   str      ,      lowerCAmelCase  :   List[Any]      ,      lowerCAmelCase  :   str      ,      lowerCAmelCase  :   Tuple      ,      lowerCAmelCase  :   List[Any]      ,      lowerCAmelCase  :   Optional[int]   )	-> str:
        a__     		=     ErnieMForInformationExtraction(config=lowerCAmelCase   )
        model.to(lowerCAmelCase   )
        model.eval()
        a__     		=     torch.ones_like(lowerCAmelCase      ,      dtype=torch.floataa   )
        a__     		=     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 						__lowerCamelCase (					self  :   Any      ,      lowerCAmelCase  :   List[str]      ,      lowerCAmelCase  :   Dict      ,      lowerCAmelCase  :   Optional[Any]      ,      lowerCAmelCase  :   Any      ,      lowerCAmelCase  :   Optional[Any]      ,      lowerCAmelCase  :   List[str]   )	-> int:
        a__     		=     self.num_labels
        a__     		=     ErnieMForSequenceClassification(lowerCAmelCase   )
        model.to(lowerCAmelCase   )
        model.eval()
        a__     		=     model(lowerCAmelCase      ,      attention_mask=lowerCAmelCase      ,      labels=lowerCAmelCase   )
        self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.num_labels)   )
  
  
  
  
      def 						__lowerCamelCase (					self  :   str      ,      lowerCAmelCase  :   Tuple      ,      lowerCAmelCase  :   Optional[int]      ,      lowerCAmelCase  :   Union[str, Any]      ,      lowerCAmelCase  :   Any      ,      lowerCAmelCase  :   Dict      ,      lowerCAmelCase  :   List[str]   )	-> str:
        a__     		=     self.num_labels
        a__     		=     ErnieMForTokenClassification(config=lowerCAmelCase   )
        model.to(lowerCAmelCase   )
        model.eval()
        input_ids.to(lowerCAmelCase   )
        input_mask.to(lowerCAmelCase   )
        token_labels.to(lowerCAmelCase   )
  
        a__     		=     model(lowerCAmelCase      ,      attention_mask=lowerCAmelCase      ,      labels=lowerCAmelCase   )
  
        self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length, self.num_labels)   )
  
  
  
  
      def 						__lowerCamelCase (					self  :   List[str]      ,      lowerCAmelCase  :   Union[str, Any]      ,      lowerCAmelCase  :   Tuple      ,      lowerCAmelCase  :   List[Any]      ,      lowerCAmelCase  :   int      ,      lowerCAmelCase  :   Optional[Any]      ,      lowerCAmelCase  :   Dict   )	-> Optional[int]:
        a__     		=     self.num_choices
        a__     		=     ErnieMForMultipleChoice(config=lowerCAmelCase   )
        model.to(lowerCAmelCase   )
        model.eval()
        a__     		=     input_ids.unsqueeze(1   ).expand(-1      ,      self.num_choices      ,      -1   ).contiguous()
        a__     		=     input_mask.unsqueeze(1   ).expand(-1      ,      self.num_choices      ,      -1   ).contiguous()
        a__     		=     model(
            lowerCAmelCase      ,      attention_mask=lowerCAmelCase      ,      labels=lowerCAmelCase      ,      )
        self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.num_choices)   )
  
  
  
  
      def 						__lowerCamelCase (					self  :   Union[str, Any]   )	-> int:
        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_torch
  class 					UpperCAmelCase_				(							_a     ,     _a     ,     unittest.TestCase    ):
      UpperCAmelCase_			:       Optional[Any]						 =							(
          (
              ErnieMModel,
              ErnieMForMultipleChoice,
              ErnieMForQuestionAnswering,
              ErnieMForSequenceClassification,
              ErnieMForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      UpperCAmelCase_			:       Union[str, Any]						 =							()
      UpperCAmelCase_			:       Optional[int]						 =							(
          {
              'feature-extraction': ErnieMModel,
              'question-answering': ErnieMForQuestionAnswering,
              'text-classification': ErnieMForSequenceClassification,
              'token-classification': ErnieMForTokenClassification,
              'zero-shot': ErnieMForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      UpperCAmelCase_			:       List[Any]						 =							False
  
  
  
  
      def 						__lowerCamelCase (					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 						__lowerCamelCase (					self  :   List[Any]   )	-> Dict:
        a__     		=     ErnieMModelTester(self   )
        a__     		=     ConfigTester(self      ,      config_class=lowerCAmelCase      ,      hidden_size=37   )
  
  
  
  
      def 						__lowerCamelCase (					self  :   List[Any]   )	-> int:
        self.config_tester.run_common_tests()
  
  
  
  
      def 						__lowerCamelCase (					self  :   Optional[Any]   )	-> Optional[int]:
        a__     		=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase   )
  
  
  
  
      def 						__lowerCamelCase (					self  :   Union[str, Any]   )	-> Dict:
        a__     		=     self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
          a__     		=     type
          self.model_tester.create_and_check_model(*lowerCAmelCase   )
  
  
  
  
      def 						__lowerCamelCase (					self  :   List[Any]   )	-> Dict:
        a__     		=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*lowerCAmelCase   )
  
  
  
  
      def 						__lowerCamelCase (					self  :   Union[str, Any]   )	-> Any:
        a__     		=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase   )
  
  
  
  
      def 						__lowerCamelCase (					self  :   Tuple   )	-> Optional[Any]:
        a__     		=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase   )
  
  
  
  
      def 						__lowerCamelCase (					self  :   Union[str, Any]   )	-> List[str]:
        a__     		=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_information_extraction(*lowerCAmelCase   )
  
  
  
  
      def 						__lowerCamelCase (					self  :   List[str]   )	-> List[Any]:
        a__     		=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase   )
  
  
  
  
      @slow
      def 						__lowerCamelCase (					self  :   Optional[int]   )	-> str:
        for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          a__     		=     ErnieMModel.from_pretrained(lowerCAmelCase   )
          self.assertIsNotNone(lowerCAmelCase   )
  
  
  
  
  
  
  
  @require_torch
  class 					UpperCAmelCase_				(							unittest.TestCase    ):
  
  
  
  
      @slow
      def 						__lowerCamelCase (					self  :   List[str]   )	-> Union[str, Any]:
        a__     		=     ErnieMModel.from_pretrained("susnato/ernie-m-base_pytorch"   )
        model.eval()
        a__     		=     torch.tensor([[0, 1, 2, 3, 4, 5]]   )
        a__     		=     model(lowerCAmelCase   )[0]
  
        # TODO Replace vocab size
        a__     		=     768
  
        a__     		=     torch.Size((1, 6, hidden_size)   )
        self.assertEqual(output.shape      ,      lowerCAmelCase   )
  
        a__     		=     torch.tensor(
            [[[-0.0_012, 0.1_245, -0.0_214], [-0.0_742, 0.0_244, -0.0_771], [-0.0_333, 0.1_164, -0.1_554]]]   )
  
        self.assertTrue(torch.allclose(output[:, :3, :3]      ,      lowerCAmelCase      ,      atol=1e-3   )   )
  
  
style_context: from unittest.mock import patch
  
  import datasets
  from datasets import Dataset
  def 				__lowercase	(						)				->Union[str, Any]:
  
    '''simple docstring'''
  
  
  
  
    a__     		=     Dataset.from_dict({"col_1": [3, 2, 0, 1]}					)
  
    with patch("tqdm.auto.tqdm"					) as mock_tqdm:
      datasets.disable_progress_bar()
      dset.map(lambda lowerCamelCase__					: {"col_2": x["col_1"] + 1}					)
      mock_tqdm.assert_not_called()
  
      mock_tqdm.reset_mock()
  
      datasets.enable_progress_bar()
      dset.map(lambda lowerCamelCase__					: {"col_2": x["col_1"] + 1}					)
      mock_tqdm.assert_called()
  
  