


import argparse
import glob
import inspect
import os
import re
import unittest

from get_test_info import get_test_classes

from tests.test_pipeline_mixin import pipeline_test_mapping


__lowerCAmelCase									=				{}
for task, _ in pipeline_test_mapping.items():
 __lowerCAmelCase									=				{'''pt''': None, '''tf''': None}


# DO **NOT** add item to this set (unless the reason is approved)
__lowerCAmelCase									=				{
    '''tests/models/esm/test_modeling_esmfold.py''',  # The pipeline test mapping is added to `test_modeling_esm.py`
}




def   snake_case_	(    snake_case	)    ->							Union[str, Any]:

       if "ModelTesterMixin" in [x.__name__ for x in test_class.__bases__]:
              return "pt"
       elif "TFModelTesterMixin" in [x.__name__ for x in test_class.__bases__]:
              return "tf"
       elif "FlaxModelTesterMixin" in [x.__name__ for x in test_class.__bases__]:
              return "flax"
       else:
              return None




def   snake_case_	(    snake_case     ,				snake_case	)    ->							List[str]:
       # Use the cached results
       if PIPELINE_TEST_MAPPING[task].get(snake_case     ,				snake_case	) is not None:
              return PIPELINE_TEST_MAPPING[task][framework]

       lowercase__:  Optional[Any]         =  pipeline_test_mapping[task]['test']
       lowercase__:  Dict         =  None

       if framework == "pt":
              lowercase__:  Dict         =  getattr(snake_case     ,				'model_mapping'     ,				snake_case	)
       elif framework == "tf":
              lowercase__:  int         =  getattr(snake_case     ,				'tf_model_mapping'     ,				snake_case	)

       if mapping is not None:
              lowercase__:  Optional[int]         =  dict(mapping.items()	)

       # cache the results
       lowercase__:  Tuple         =  mapping
       return mapping




def   snake_case_	(    snake_case     ,				snake_case	)    ->							List[str]:
       lowercase__:  Dict         =  get_framework(snake_case	)
       if framework is None:
              return None
       lowercase__:  Tuple         =  get_mapping_for_task(snake_case     ,				snake_case	)
       if mapping is None:
              return None

       lowercase__:  Dict         =  list({model_class.config_class for model_class in test_class.all_model_classes}	)
       if len(snake_case	) != 1:
              raise ValueError('There should be exactly one configuration class from `test_class.all_model_classes`.'	)

       # This could be a list/tuple of model classes, but it's rare.
       lowercase__:  Any         =  mapping.get(config_classes[0]     ,				snake_case	)
       if isinstance(snake_case     ,				(tuple, list)	):
              lowercase__:  int         =  sorted(snake_case     ,				key=lambda snake_case	: x.__name__	)

       return model_class




def   snake_case_	(    snake_case	)    ->							Dict:
       lowercase__:  Union[str, Any]         =  [(task, get_model_for_pipeline_test(snake_case     ,				snake_case	)) for task in pipeline_test_mapping]
       lowercase__:  Dict         =  sorted([(task, model) for task, model in mapping if model is not None]     ,				key=lambda snake_case	: x[0]	)

       return dict(snake_case	)




def   snake_case_	(    snake_case	)    ->							Optional[Any]:
       lowercase__:  str         =  get_framework(snake_case	)
       if framework == "pt":
              lowercase__:  Tuple         =  'torch'
       lowercase__:  Optional[int]         =  '{}'

       lowercase__:  int         =  get_pipeline_model_mapping(snake_case	)
       if len(snake_case	) == 0:
              return ""

       lowercase__:  Tuple         =  []
       for task, model_classes in mapping.items():
              if isinstance(snake_case     ,				(tuple, list)	):
                     # A list/tuple of model classes
                     lowercase__:  List[Any]         =  '(' + ', '.join([x.__name__ for x in model_classes]	) + ')'
              else:
                     # A single model class
                     lowercase__:  Optional[int]         =  model_classes.__name__
              texts.append(f'"{task}": {value}'	)
       lowercase__:  Any         =  '{' + ', '.join(snake_case	) + '}'
       lowercase__:  Union[str, Any]         =  f'pipeline_model_mapping = {text} if is_{framework}_available() else {default_value}'

       return text




def   snake_case_	(    snake_case	)    ->							List[Any]:
       lowercase__:  Union[str, Any]         =  {'ModelTesterMixin', 'TFModelTesterMixin', 'FlaxModelTesterMixin'}
       if not issubclass(snake_case     ,				unittest.TestCase	):
              return False
       return len(base_class_names.intersection([x.__name__ for x in test_class.__bases__]	)	) > 0




def   snake_case_	(    snake_case	)    ->							List[Any]:
       lowercase__:  Tuple         =  [x for x in get_test_classes(snake_case	) if is_valid_test_class(snake_case	)]

       lowercase__:  Dict         =  None
       for test_class in test_classes:
              # If a test class has defined `pipeline_model_mapping`, let's take it
              if getattr(snake_case     ,				'pipeline_model_mapping'     ,				snake_case	) is not None:
                     lowercase__:  Tuple         =  test_class
                     break
    # Take the test class with the shortest name (just a heuristic)
       if target_test_class is None and len(snake_case	) > 0:
              lowercase__:  Dict         =  sorted(snake_case     ,				key=lambda snake_case	: (len(x.__name__	), x.__name__)	)[0]

       return target_test_class




def   snake_case_	(    snake_case     ,				snake_case     ,				snake_case	)    ->							Tuple:
       lowercase__:  Optional[int]         =  start_idx
       for idx, line in enumerate(lines[start_idx:]	):
              lowercase__:  List[str]         =  len(snake_case	) - len(line.lstrip()	)
              if idx == 0 or indent > indent_level or (indent == indent_level and line.strip() == ")"):
                     lowercase__:  List[Any]         =  start_idx + idx
              elif idx > 0 and indent <= indent_level:
                     # Outside the definition block of `pipeline_model_mapping`
                     break

       return end_idx




def   snake_case_	(    snake_case     ,				snake_case=False	)    ->							Union[str, Any]:
       if getattr(snake_case     ,				'pipeline_model_mapping'     ,				snake_case	) is not None:
              if not overwrite:
                     return "", -1

       lowercase__:  Union[str, Any]         =  get_pipeline_model_mapping_string(snake_case	)
       if len(snake_case	) == 0:
              return "", -1
       lowercase__:  List[str]         =  line_to_add + '\n'

       # The code defined the class `test_class`
       lowercase__     ,		lowercase__:  int         =  inspect.getsourcelines(snake_case	)
       # `inspect` gives the code for an object, including decorator(s) if any.
       # We (only) need the exact line of the class definition.
       for idx, line in enumerate(snake_case	):
              if line.lstrip().startswith('class '	):
                     lowercase__:  Dict         =  class_lines[idx:]
                     class_start_line_no += idx
                     break
       lowercase__:  List[str]         =  class_start_line_no + len(snake_case	) - 1

       # The index in `class_lines` that starts the definition of `all_model_classes`, `all_generative_model_classes` or
       # `pipeline_model_mapping`. This assumes they are defined in such order, and we take the start index of the last
       # block that appears in a `test_class`.
       lowercase__:  Any         =  None
       # The indent level of the line at `class_lines[start_idx]` (if defined)
       lowercase__:  Optional[int]         =  0
       # To record if `pipeline_model_mapping` is found in `test_class`.
       lowercase__:  Union[str, Any]         =  None
       for idx, line in enumerate(snake_case	):
              if line.strip().startswith('all_model_classes = '	):
                     lowercase__:  Any         =  len(snake_case	) - len(line.lstrip()	)
                     lowercase__:  List[Any]         =  idx
              elif line.strip().startswith('all_generative_model_classes = '	):
                     lowercase__:  Tuple         =  len(snake_case	) - len(line.lstrip()	)
                     lowercase__:  int         =  idx
              elif line.strip().startswith('pipeline_model_mapping = '	):
                     lowercase__:  Tuple         =  len(snake_case	) - len(line.lstrip()	)
                     lowercase__:  str         =  idx
                     lowercase__:  Optional[Any]         =  line
                     break

       if start_idx is None:
              return "", -1
       # Find the ending index (inclusive) of the above found block.
       lowercase__:  str         =  find_block_ending(snake_case     ,				snake_case     ,				snake_case	)

       # Extract `is_xxx_available()` from existing blocks: some models require specific libraries like `timm` and use
       # `is_timm_available()` instead of `is_torch_available()`.
       # Keep leading and trailing whitespaces
       lowercase__:  List[str]         =  re.compile(R'\s(is_\S+?_available\(\))\s'	)
       for line in class_lines[start_idx : end_idx + 1]:
              lowercase__:  int         =  r.search(snake_case	)
              if backend_condition is not None:
                     # replace the leading and trailing whitespaces to the space character " ".
                     lowercase__:  Dict         =  ' ' + backend_condition[0][1:-1] + ' '
                     lowercase__:  Optional[int]         =  r.sub(snake_case     ,				snake_case	)
                     break

       if def_line is None:
              # `pipeline_model_mapping` is not defined. The target index is set to the ending index (inclusive) of
              # `all_model_classes` or `all_generative_model_classes`.
              lowercase__:  int         =  end_idx
       else:
              # `pipeline_model_mapping` is defined. The target index is set to be one **BEFORE** its start index.
              lowercase__:  List[str]         =  start_idx - 1
              # mark the lines of the currently existing `pipeline_model_mapping` to be removed.
              for idx in range(snake_case     ,				end_idx + 1	):
                     # These lines are going to be removed before writing to the test file.
                     lowercase__:  Optional[Any]         =  None  # noqa

    # Make sure the test class is a subclass of `PipelineTesterMixin`.
       lowercase__:  List[Any]         =  [x.__name__ for x in test_class.__bases__]
       if "PipelineTesterMixin" not in parent_classes:
              # Put `PipelineTesterMixin` just before `unittest.TestCase`
              lowercase__:  str         =  [x for x in parent_classes if x != 'TestCase'] + ['PipelineTesterMixin']
              if "TestCase" in parent_classes:
                     # Here we **assume** the original string is always with `unittest.TestCase`.
                     _parent_classes.append('unittest.TestCase'	)
              lowercase__:  List[Any]         =  ', '.join(_parent_classes	)
              for idx, line in enumerate(snake_case	):
                     # Find the ending of the declaration of `test_class`
                     if line.strip().endswith('):'	):
                            # mark the lines of the declaration of `test_class` to be removed
                            for _idx in range(idx + 1	):
                                   lowercase__:  Optional[Any]         =  None  # noqa
                            break
        # Add the new, one-line, class declaration for `test_class`
              lowercase__:  Any         =  f'class {test_class.__name__}({parent_classes}):\n'

       # Add indentation
       lowercase__:  Optional[int]         =  ' ' * indent_level + line_to_add
       # Insert `pipeline_model_mapping` to `class_lines`.
       # (The line at `target_idx` should be kept by definition!)
       lowercase__:  int         =  class_lines[: target_idx + 1] + [line_to_add] + class_lines[target_idx + 1 :]
       # Remove the lines that are marked to be removed
       lowercase__:  str         =  [x for x in class_lines if x is not None]

       # Move from test class to module (in order to write to the test file)
       lowercase__:  Any         =  inspect.getsourcelines(inspect.getmodule(snake_case	)	)[0]
       # Be careful with the 1-off between line numbers and array indices
       lowercase__:  int         =  module_lines[: class_start_line_no - 1] + class_lines + module_lines[class_end_line_no:]
       lowercase__:  Optional[Any]         =  ''.join(snake_case	)

       lowercase__:  Any         =  inspect.getsourcefile(snake_case	)
       with open(snake_case     ,				'w'     ,				encoding='UTF-8'     ,				newline='\n'	) as fp:
              fp.write(snake_case	)

       return line_to_add




def   snake_case_	(    snake_case     ,				snake_case=False	)    ->							Tuple:
       lowercase__:  Optional[Any]         =  find_test_class(snake_case	)
       if test_class:
              add_pipeline_model_mapping(snake_case     ,				overwrite=snake_case	)


if __name__ == "__main__":
 __lowerCAmelCase									=				argparse.ArgumentParser()
 parser.add_argument(
     '''--test_file''', type=str, help='''A path to the test file, starting with the repository\'s `tests` directory.'''
 )
 parser.add_argument(
     '''--all''',
     action='''store_true''',
     help='''If to check and modify all test files.''',
 )
 parser.add_argument(
     '''--overwrite''',
     action='''store_true''',
     help='''If to overwrite a test class if it has already defined `pipeline_model_mapping`.''',
 )
 __lowerCAmelCase									=				parser.parse_args()

 if not args.all and not args.test_file:
  raise ValueError('''Please specify either `test_file` or pass `--all` to check/modify all test files.''')
 elif args.all and args.test_file:
  raise ValueError('''Only one of `--test_file` and `--all` could be specified.''')

 __lowerCAmelCase									=				[]
 if args.test_file:
  __lowerCAmelCase									=				[args.test_file]
 else:
  __lowerCAmelCase									=				os.path.join('''tests''', '''models''', '''**''', '''test_modeling_*.py''')
  for test_file in glob.glob(pattern):
   # `Flax` is not concerned at this moment
   if not test_file.startswith('''test_modeling_flax_'''):
    test_files.append(test_file)

 for test_file in test_files:
  if test_file in TEST_FILE_TO_IGNORE:
   print(F'''[SKIPPED] {test_file} is skipped as it is in `TEST_FILE_TO_IGNORE` in the file {__file__}.''')
   continue
  add_pipeline_model_mapping_to_test_file(test_file, overwrite=args.overwrite)






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     __a     :
 def __init__(  self							,					lowerCAmelCase__							,					lowerCAmelCase__=13							,					lowerCAmelCase__=7							,					lowerCAmelCase__=True							,					lowerCAmelCase__=True							,					lowerCAmelCase__=True							,					lowerCAmelCase__=99							,					lowerCAmelCase__=32							,					lowerCAmelCase__=5							,					lowerCAmelCase__=4							,					lowerCAmelCase__=37							,					lowerCAmelCase__="gelu"							,					lowerCAmelCase__=0.1							,					lowerCAmelCase__=0.1							,					lowerCAmelCase__=512							,					lowerCAmelCase__=16							,					lowerCAmelCase__=2							,					lowerCAmelCase__=0.0_2							,					lowerCAmelCase__=3							,					lowerCAmelCase__=4							,					lowerCAmelCase__=None							,					)   ->							List[Any]:




        '''simple docstring'''
        lowercase__:  Tuple         =  parent
        lowercase__:  Optional[int]         =  batch_size
        lowercase__:  Optional[Any]         =  seq_length
        lowercase__:  Dict         =  is_training
        lowercase__:  List[str]         =  use_input_mask
        lowercase__:  Tuple         =  use_labels
        lowercase__:  List[str]         =  vocab_size
        lowercase__:  Any         =  hidden_size
        lowercase__:  Optional[int]         =  num_hidden_layers
        lowercase__:  str         =  num_attention_heads
        lowercase__:  Any         =  intermediate_size
        lowercase__:  List[str]         =  hidden_act
        lowercase__:  str         =  hidden_dropout_prob
        lowercase__:  Optional[Any]         =  attention_probs_dropout_prob
        lowercase__:  Optional[Any]         =  max_position_embeddings
        lowercase__:  List[str]         =  type_vocab_size
        lowercase__:  List[Any]         =  type_sequence_label_size
        lowercase__:  Any         =  initializer_range
        lowercase__:  str         =  num_labels
        lowercase__:  Tuple         =  num_choices
        lowercase__:  str         =  scope
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							str:




        '''simple docstring'''
        lowercase__:  int         =  ids_tensor([self.batch_size, self.seq_length]							,					self.vocab_size					)

        lowercase__:  str         =  None
        if self.use_input_mask:
               lowercase__:  Dict         =  random_attention_mask([self.batch_size, self.seq_length]					)

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

        lowercase__:  int         =  self.get_config()

        return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Dict:




        '''simple docstring'''
        lowercase__:  Tuple         =  ids_tensor([self.batch_size, self.seq_length]							,					self.vocab_size					)

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

        return config, input_ids, input_mask
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Tuple:




        '''simple docstring'''
        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 						SCREAMING_SNAKE_CASE__  (  self							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__					)   ->							str:




        '''simple docstring'''
        lowercase__:  str         =  ErnieMModel(config=lowerCAmelCase__					)
        model.to(lowerCAmelCase__					)
        model.eval()
        lowercase__:  List[Any]         =  model(lowerCAmelCase__							,					return_dict=lowerCAmelCase__					)
        self.parent.assertEqual(result.last_hidden_state.shape							,					(self.batch_size, self.seq_length, self.hidden_size)					)
 def 						SCREAMING_SNAKE_CASE__  (  self							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__					)   ->							Optional[Any]:




        '''simple docstring'''
        lowercase__:  Optional[Any]         =  ErnieMForQuestionAnswering(config=lowerCAmelCase__					)
        model.to(lowerCAmelCase__					)
        model.eval()
        lowercase__:  List[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 						SCREAMING_SNAKE_CASE__  (  self							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__					)   ->							List[Any]:




        '''simple docstring'''
        lowercase__:  Dict         =  ErnieMForInformationExtraction(config=lowerCAmelCase__					)
        model.to(lowerCAmelCase__					)
        model.eval()
        lowercase__:  Any         =  torch.ones_like(lowerCAmelCase__							,					dtype=torch.floataa					)
        lowercase__:  Tuple         =  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 						SCREAMING_SNAKE_CASE__  (  self							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__					)   ->							Tuple:




        '''simple docstring'''
        lowercase__:  Tuple         =  self.num_labels
        lowercase__:  str         =  ErnieMForSequenceClassification(lowerCAmelCase__					)
        model.to(lowerCAmelCase__					)
        model.eval()
        lowercase__:  Optional[int]         =  model(lowerCAmelCase__							,					attention_mask=lowerCAmelCase__							,					labels=lowerCAmelCase__					)
        self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.num_labels)					)
 def 						SCREAMING_SNAKE_CASE__  (  self							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__					)   ->							List[Any]:




        '''simple docstring'''
        lowercase__:  Union[str, Any]         =  self.num_labels
        lowercase__:  Dict         =  ErnieMForTokenClassification(config=lowerCAmelCase__					)
        model.to(lowerCAmelCase__					)
        model.eval()
        input_ids.to(lowerCAmelCase__					)
        input_mask.to(lowerCAmelCase__					)
        token_labels.to(lowerCAmelCase__					)

        lowercase__:  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 						SCREAMING_SNAKE_CASE__  (  self							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__					)   ->							Optional[int]:




        '''simple docstring'''
        lowercase__:  Dict         =  self.num_choices
        lowercase__:  Any         =  ErnieMForMultipleChoice(config=lowerCAmelCase__					)
        model.to(lowerCAmelCase__					)
        model.eval()
        lowercase__:  Optional[int]         =  input_ids.unsqueeze(1					).expand(-1							,					self.num_choices							,					-1					).contiguous()
        lowercase__:  Any         =  input_mask.unsqueeze(1					).expand(-1							,					self.num_choices							,					-1					).contiguous()
        lowercase__:  Union[str, Any]         =  model(
            lowerCAmelCase__							,					attention_mask=lowerCAmelCase__							,					labels=lowerCAmelCase__							,					)
        self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.num_choices)					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Optional[int]:




        '''simple docstring'''
        lowercase__:  int         =  self.prepare_config_and_inputs()
        (
            (
            lowercase__
        )     ,		(
            lowercase__
        )     ,		(
            lowercase__
        )     ,		(
            lowercase__
        )     ,		(
            lowercase__
        )     ,		(
            lowercase__
        )     ,		
        ):  Optional[Any]         =  config_and_inputs
        lowercase__:  int         =  {'input_ids': input_ids, 'attention_mask': input_mask}
        return config, inputs_dict





@require_torch
class     __a     (       __UpperCamelCase			,			__UpperCamelCase			,			unittest.TestCase ):
 __lowercase					:				List[str]  	=					(
     (
         ErnieMModel,
         ErnieMForMultipleChoice,
         ErnieMForQuestionAnswering,
         ErnieMForSequenceClassification,
         ErnieMForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 __lowercase					:				List[str]  	=					()
 __lowercase					:				Optional[int]  	=					(
     {
         'feature-extraction': ErnieMModel,
         'question-answering': ErnieMForQuestionAnswering,
         'text-classification': ErnieMForSequenceClassification,
         'token-classification': ErnieMForTokenClassification,
         'zero-shot': ErnieMForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 __lowercase					:				Optional[Any]  	=					False
 def 						SCREAMING_SNAKE_CASE__  (  self							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__					)   ->							int:




        '''simple docstring'''
        if pipeline_test_casse_name == "QAPipelineTests":
               return True

        return False
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							List[Any]:




        '''simple docstring'''
        lowercase__:  Any         =  ErnieMModelTester(self					)
        lowercase__:  List[str]         =  ConfigTester(self							,					config_class=lowerCAmelCase__							,					hidden_size=37					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Tuple:




        '''simple docstring'''
        self.config_tester.run_common_tests()
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							str:




        '''simple docstring'''
        lowercase__:  Union[str, Any]         =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase__					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Tuple:




        '''simple docstring'''
        lowercase__:  int         =  self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
               lowercase__:  str         =  type
               self.model_tester.create_and_check_model(*lowerCAmelCase__					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Tuple:




        '''simple docstring'''
        lowercase__:  Optional[Any]         =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*lowerCAmelCase__					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Dict:




        '''simple docstring'''
        lowercase__:  Tuple         =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase__					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Optional[Any]:




        '''simple docstring'''
        lowercase__:  Any         =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase__					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							List[str]:




        '''simple docstring'''
        lowercase__:  Optional[int]         =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_information_extraction(*lowerCAmelCase__					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Optional[Any]:




        '''simple docstring'''
        lowercase__:  Optional[int]         =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase__					)
 @slow
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Any:




        '''simple docstring'''
        for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowercase__:  Tuple         =  ErnieMModel.from_pretrained(lowerCAmelCase__					)
               self.assertIsNotNone(lowerCAmelCase__					)





@require_torch
class     __a     (       unittest.TestCase ):
 @slow
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							List[Any]:




        '''simple docstring'''
        lowercase__:  Optional[Any]         =  ErnieMModel.from_pretrained('susnato/ernie-m-base_pytorch'					)
        model.eval()
        lowercase__:  Union[str, Any]         =  torch.tensor([[0, 1, 2, 3, 4, 5]]					)
        lowercase__:  str         =  model(lowerCAmelCase__					)[0]

        # TODO Replace vocab size
        lowercase__:  Union[str, Any]         =  768

        lowercase__:  Any         =  torch.Size((1, 6, hidden_size)					)
        self.assertEqual(output.shape							,					lowerCAmelCase__					)

        lowercase__:  str         =  torch.tensor(
            [[[-0.0_0_1_2, 0.1_2_4_5, -0.0_2_1_4], [-0.0_7_4_2, 0.0_2_4_4, -0.0_7_7_1], [-0.0_3_3_3, 0.1_1_6_4, -0.1_5_5_4]]]					)

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


