





"""simple docstring"""




import json
import os
from functools import lru_cache
from typing import TYPE_CHECKING, List, Optional, Tuple, Union

import numpy as np
import regex as re

from ...utils import is_tf_available, is_torch_available, logging


if TYPE_CHECKING:
   if is_torch_available():
      import torch
   if is_tf_available():
      import tensorflow as tf

from ...tokenization_utils import AddedToken, PreTrainedTokenizer


lowerCamelCase_	:		List[str]         =   logging.get_logger(__name__)

lowerCamelCase_	:		Optional[int]         =   {
    'vocab_file': 'vocab.json',
    'merges_file': 'merges.txt',
}

lowerCamelCase_	:		Optional[Any]         =   {
    'vocab_file': {
        'Salesforce/codegen-350M-mono': 'https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/vocab.json',
    },
    'merges_file': {
        'Salesforce/codegen-350M-mono': 'https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/merges.txt',
    },
}

lowerCamelCase_	:		str         =   {
    'Salesforce/codegen-350M-mono': 20_48,
}





@lru_cache()
def 		UpperCAmelCase__						(	):






  """simple docstring"""





  A_    :			str									=    (
      list(range(ord('!'   )					,   ord('~'   ) + 1   )   ) + list(range(ord('¡'   )					,   ord('¬'   ) + 1   )   ) + list(range(ord('®'   )					,   ord('ÿ'   ) + 1   )   )
  )
  A_    :			Any									=    bs[:]
  A_    :			Any									=    0
  for b in range(2**8   ):
    if b not in bs:
      bs.append(_UpperCAmelCase   )
      cs.append(2**8 + n   )
      n += 1
  A_    :			Dict									=    [chr(_UpperCAmelCase   ) for n in cs]
  return dict(zip(_UpperCAmelCase					,   _UpperCAmelCase   )   )





def 		UpperCAmelCase__						(	_UpperCAmelCase   ):






  """simple docstring"""





  A_    :			Tuple									=    set()
  A_    :			Any									=    word[0]
  for char in word[1:]:
    pairs.add((prev_char, char)   )
    A_    :			List[Any]									=    char
  return pairs





class 					_UpperCAmelCase    (  UpperCAmelCase__						):



      '''simple docstring'''
      lowercase_ :					Any             =  VOCAB_FILES_NAMES
      lowercase_ :					List[str]             =  PRETRAINED_VOCAB_FILES_MAP
      lowercase_ :					str             =  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      lowercase_ :					int             =  ["""input_ids""", """attention_mask"""]

      def __init__(	self   , snake_case_   , snake_case_   , snake_case_="replace"   , snake_case_="<|endoftext|>"   , snake_case_="<|endoftext|>"   , snake_case_="<|endoftext|>"   , snake_case_=None   , snake_case_=False   , snake_case_=False   , **snake_case_   , ):

        """simple docstring"""

        A_    :			Optional[int]									=    AddedToken(snake_case_   , lstrip=snake_case_   , rstrip=snake_case_    ) if isinstance(snake_case_   , snake_case_    ) else bos_token
        A_    :			List[Any]									=    AddedToken(snake_case_   , lstrip=snake_case_   , rstrip=snake_case_    ) if isinstance(snake_case_   , snake_case_    ) else eos_token
        A_    :			str									=    AddedToken(snake_case_   , lstrip=snake_case_   , rstrip=snake_case_    ) if isinstance(snake_case_   , snake_case_    ) else unk_token
        A_    :			Tuple									=    AddedToken(snake_case_   , lstrip=snake_case_   , rstrip=snake_case_    ) if isinstance(snake_case_   , snake_case_    ) else pad_token
        super().__init__(
            errors=snake_case_   , unk_token=snake_case_   , bos_token=snake_case_   , eos_token=snake_case_   , pad_token=snake_case_   , add_prefix_space=snake_case_   , add_bos_token=snake_case_   , **snake_case_   , )
        A_    :			str									=    add_bos_token

        with open(snake_case_   , encoding='utf-8'    ) as vocab_handle:
          A_    :			Union[str, Any]									=    json.load(snake_case_    )
        A_    :			Union[str, Any]									=    {v: k for k, v in self.encoder.items()}
        A_    :			Any									=    errors  # how to handle errors in decoding
        A_    :			List[Any]									=    bytes_to_unicode()
        A_    :			Union[str, Any]									=    {v: k for k, v in self.byte_encoder.items()}
        with open(snake_case_   , encoding='utf-8'    ) as merges_handle:
          A_    :			Any									=    merges_handle.read().split('\n'    )[1:-1]
        A_    :			Dict									=    [tuple(merge.split()    ) for merge in bpe_merges]
        A_    :			List[Any]									=    dict(zip(snake_case_   , range(len(snake_case_    )    )    )    )
        A_    :			Dict									=    {}
        A_    :			int									=    add_prefix_space

        # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
        A_    :			List[Any]									=    re.compile(R'\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+'    )

      @property
      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        return len(self.encoder    )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        return dict(self.encoder   , **self.added_tokens_encoder    )

      def    lowerCamelCase_     (	self   , snake_case_    ):

        """simple docstring"""

        if token in self.cache:
          return self.cache[token]
        A_    :			Any									=    tuple(snake_case_    )
        A_    :			Dict									=    get_pairs(snake_case_    )

        if not pairs:
          return token

        while True:
          A_    :			str									=    min(snake_case_   , key=lambda snake_case_    : self.bpe_ranks.get(snake_case_   , float('inf'    )    )    )
          if bigram not in self.bpe_ranks:
            break
          A_		,			A_    :			List[str]									=    bigram
          A_    :			int									=    []
          A_    :			Union[str, Any]									=    0
          while i < len(snake_case_    ):
            try:
              A_    :			Optional[int]									=    word.index(snake_case_   , snake_case_    )
            except ValueError:
              new_word.extend(word[i:]    )
              break
            else:
              new_word.extend(word[i:j]    )
              A_    :			Tuple									=    j

            if word[i] == first and i < len(snake_case_    ) - 1 and word[i + 1] == second:
              new_word.append(first + second    )
              i += 2
            else:
              new_word.append(word[i]    )
              i += 1
          A_    :			str									=    tuple(snake_case_    )
          A_    :			Dict									=    new_word
          if len(snake_case_    ) == 1:
            break
          else:
            A_    :			Tuple									=    get_pairs(snake_case_    )
        A_    :			Any									=    ' '.join(snake_case_    )
        A_    :			Any									=    word
        return word

      def    lowerCamelCase_     (	self   , snake_case_   , snake_case_=None    ):

        """simple docstring"""

        if self.add_bos_token:
          A_    :			Optional[Any]									=    [self.bos_token_id]
        else:
          A_    :			List[str]									=    []

        A_    :			Optional[int]									=    bos_token_ids + token_ids_a

        if token_ids_a is None:
          return output

        return output + bos_token_ids + token_ids_a

      def    lowerCamelCase_     (	self   , snake_case_    ):

        """simple docstring"""

        A_    :			List[str]									=    []
        for token in re.findall(self.pat   , snake_case_    ):
          A_    :			str									=    ''.join(
              self.byte_encoder[b] for b in token.encode('utf-8'    )    )  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
          bpe_tokens.extend(bpe_token for bpe_token in self.bpe(snake_case_    ).split(' '    )    )
        return bpe_tokens

      def    lowerCamelCase_     (	self   , snake_case_    ):

        """simple docstring"""

        return self.encoder.get(snake_case_   , self.encoder.get(self.unk_token    )    )

      def    lowerCamelCase_     (	self   , snake_case_    ):

        """simple docstring"""

        return self.decoder.get(snake_case_    )

      def    lowerCamelCase_     (	self   , snake_case_    ):

        """simple docstring"""

        A_    :			Union[str, Any]									=    ''.join(snake_case_    )
        A_    :			Tuple									=    bytearray([self.byte_decoder[c] for c in text]    ).decode('utf-8'   , errors=self.errors    )
        return text

      def    lowerCamelCase_     (	self   , snake_case_   , snake_case_ = None    ):

        """simple docstring"""

        if not os.path.isdir(snake_case_    ):
          logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""    )
          return
        A_    :			int									=    os.path.join(
            snake_case_   , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']    )
        A_    :			Tuple									=    os.path.join(
            snake_case_   , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file']    )

        with open(snake_case_   , 'w'   , encoding='utf-8'    ) as f:
          f.write(json.dumps(self.encoder   , indent=2   , sort_keys=snake_case_   , ensure_ascii=snake_case_    ) + '\n'    )

        A_    :			Optional[int]									=    0
        with open(snake_case_   , 'w'   , encoding='utf-8'    ) as writer:
          writer.write('#version: 0.2\n'    )
          for bpe_tokens, token_index in sorted(self.bpe_ranks.items()   , key=lambda snake_case_    : kv[1]    ):
            if index != token_index:
              logger.warning(
                  F"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive."""
                  ' Please check that the tokenizer is not corrupted!'    )
              A_    :			Dict									=    token_index
            writer.write(' '.join(snake_case_    ) + '\n'    )
            index += 1

        return vocab_file, merge_file

      def    lowerCamelCase_     (	self   , snake_case_   , snake_case_=False   , **snake_case_    ):

        """simple docstring"""

        A_    :			Optional[int]									=    kwargs.pop('add_prefix_space'   , self.add_prefix_space    )
        if is_split_into_words or add_prefix_space:
          A_    :			Any									=    ' ' + text
        return (text, kwargs)

      def    lowerCamelCase_     (	self   , snake_case_   , snake_case_ = False   , snake_case_ = None   , snake_case_ = None   , **snake_case_   , ):

        """simple docstring"""

        A_    :			Dict									=    super()._decode(
            token_ids=snake_case_   , skip_special_tokens=snake_case_   , clean_up_tokenization_spaces=snake_case_   , **snake_case_   , )

        if truncate_before_pattern is not None and len(snake_case_    ) > 0:
          A_    :			List[str]									=    self.truncate(snake_case_   , snake_case_    )

        return decoded_text

      def    lowerCamelCase_     (	self   , snake_case_   , snake_case_    ):

        """simple docstring"""

        def find_re(snake_case_   , snake_case_   , snake_case_    ):
          A_    :			List[str]									=    pattern.search(snake_case_   , snake_case_    )
          return m.start() if m else -1

        A_    :			int									=    [re.compile(snake_case_   , re.MULTILINE    ) for pattern in truncate_before_pattern]

        A_    :			Optional[Any]									=    list(re.finditer('^print'   , snake_case_   , re.MULTILINE    )    )

        if len(snake_case_    ) > 1:
          A_    :			Tuple									=    completion[: prints[1].start()]

        A_    :			Any									=    list(re.finditer('^def'   , snake_case_   , re.MULTILINE    )    )

        if len(snake_case_    ) > 1:
          A_    :			int									=    completion[: defs[1].start()]

        A_    :			List[str]									=    0

        A_    :			Tuple									=    [
            pos for pos in [find_re(snake_case_   , snake_case_   , snake_case_    ) for terminal in terminals] if pos != -1
        ]

        if len(snake_case_    ) > 0:
          return completion[: min(snake_case_    )]
        else:
          return completion



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






      '''simple docstring'''







      def __init__(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE=13   ,			__SCREAMING_SNAKE_CASE=7   ,			__SCREAMING_SNAKE_CASE=True   ,			__SCREAMING_SNAKE_CASE=True   ,			__SCREAMING_SNAKE_CASE=True   ,			__SCREAMING_SNAKE_CASE=99   ,			__SCREAMING_SNAKE_CASE=32   ,			__SCREAMING_SNAKE_CASE=5   ,			__SCREAMING_SNAKE_CASE=4   ,			__SCREAMING_SNAKE_CASE=37   ,			__SCREAMING_SNAKE_CASE="gelu"   ,			__SCREAMING_SNAKE_CASE=0.1   ,			__SCREAMING_SNAKE_CASE=0.1   ,			__SCREAMING_SNAKE_CASE=5_12   ,			__SCREAMING_SNAKE_CASE=16   ,			__SCREAMING_SNAKE_CASE=2   ,			__SCREAMING_SNAKE_CASE=0.02   ,			__SCREAMING_SNAKE_CASE=3   ,			__SCREAMING_SNAKE_CASE=4   ,			__SCREAMING_SNAKE_CASE=None   ,			)					->						Any:







         """simple docstring"""







         UpperCamelCase__ :			Optional[Any] 						=parent
         UpperCamelCase__ :			Tuple 						=batch_size
         UpperCamelCase__ :			Union[str, Any] 						=seq_length
         UpperCamelCase__ :			Union[str, Any] 						=is_training
         UpperCamelCase__ :			Tuple 						=use_input_mask
         UpperCamelCase__ :			Union[str, Any] 						=use_labels
         UpperCamelCase__ :			Union[str, Any] 						=vocab_size
         UpperCamelCase__ :			List[Any] 						=hidden_size
         UpperCamelCase__ :			Dict 						=num_hidden_layers
         UpperCamelCase__ :			Any 						=num_attention_heads
         UpperCamelCase__ :			List[str] 						=intermediate_size
         UpperCamelCase__ :			List[Any] 						=hidden_act
         UpperCamelCase__ :			Tuple 						=hidden_dropout_prob
         UpperCamelCase__ :			Any 						=attention_probs_dropout_prob
         UpperCamelCase__ :			Tuple 						=max_position_embeddings
         UpperCamelCase__ :			Dict 						=type_vocab_size
         UpperCamelCase__ :			Tuple 						=type_sequence_label_size
         UpperCamelCase__ :			Tuple 						=initializer_range
         UpperCamelCase__ :			List[str] 						=num_labels
         UpperCamelCase__ :			Any 						=num_choices
         UpperCamelCase__ :			Tuple 						=scope




      def    UpperCAmelCase							(		self)					->						int:







         """simple docstring"""







         UpperCamelCase__ :			List[str] 						=ids_tensor([self.batch_size, self.seq_length]   ,			self.vocab_size)

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

         UpperCamelCase__ :			List[str] 						=None
         UpperCamelCase__ :			int 						=None
         UpperCamelCase__ :			Optional[Any] 						=None
         if self.use_labels:
            UpperCamelCase__ :			Dict 						=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__ :			Tuple 						=self.get_config()

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




      def    UpperCAmelCase							(		self)					->						str:







         """simple docstring"""







         UpperCamelCase__ :			Tuple 						=ids_tensor([self.batch_size, self.seq_length]   ,			self.vocab_size)

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

         return config, input_ids, input_mask




      def    UpperCAmelCase							(		self)					->						Optional[int]:







         """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    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						str:







         """simple docstring"""







         UpperCamelCase__ :			Tuple 						=ErnieMModel(config=__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()
         UpperCamelCase__ :			int 						=model(__SCREAMING_SNAKE_CASE   ,			return_dict=__SCREAMING_SNAKE_CASE)
         self.parent.assertEqual(result.last_hidden_state.shape   ,			(self.batch_size, self.seq_length, self.hidden_size))




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						List[Any]:







         """simple docstring"""







         UpperCamelCase__ :			str 						=ErnieMForQuestionAnswering(config=__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()
         UpperCamelCase__ :			Dict 						=model(
             __SCREAMING_SNAKE_CASE   ,			attention_mask=__SCREAMING_SNAKE_CASE   ,			start_positions=__SCREAMING_SNAKE_CASE   ,			end_positions=__SCREAMING_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    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Dict:







         """simple docstring"""







         UpperCamelCase__ :			Any 						=ErnieMForInformationExtraction(config=__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()
         UpperCamelCase__ :			int 						=torch.ones_like(__SCREAMING_SNAKE_CASE   ,			dtype=torch.floataa)
         UpperCamelCase__ :			Union[str, Any] 						=model(
             __SCREAMING_SNAKE_CASE   ,			attention_mask=__SCREAMING_SNAKE_CASE   ,			start_positions=__SCREAMING_SNAKE_CASE   ,			end_positions=__SCREAMING_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    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						List[str]:







         """simple docstring"""







         UpperCamelCase__ :			List[Any] 						=self.num_labels
         UpperCamelCase__ :			List[Any] 						=ErnieMForSequenceClassification(__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()
         UpperCamelCase__ :			Union[str, Any] 						=model(__SCREAMING_SNAKE_CASE   ,			attention_mask=__SCREAMING_SNAKE_CASE   ,			labels=__SCREAMING_SNAKE_CASE)
         self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.num_labels))




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Tuple:







         """simple docstring"""







         UpperCamelCase__ :			List[str] 						=self.num_labels
         UpperCamelCase__ :			int 						=ErnieMForTokenClassification(config=__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()
         input_ids.to(__SCREAMING_SNAKE_CASE)
         input_mask.to(__SCREAMING_SNAKE_CASE)
         token_labels.to(__SCREAMING_SNAKE_CASE)

         UpperCamelCase__ :			Tuple 						=model(__SCREAMING_SNAKE_CASE   ,			attention_mask=__SCREAMING_SNAKE_CASE   ,			labels=__SCREAMING_SNAKE_CASE)

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




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Optional[Any]:







         """simple docstring"""







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




      def    UpperCAmelCase							(		self)					->						Union[str, Any]:







         """simple docstring"""







         UpperCamelCase__ :			Tuple 						=self.prepare_config_and_inputs()
         (
             (
             UpperCamelCase__
         )						,	(
             UpperCamelCase__
         )						,	(
             UpperCamelCase__
         )						,	(
             UpperCamelCase__
         )						,	(
             UpperCamelCase__
         )						,	(
             UpperCamelCase__
         )						,	
         ) :			int 						=config_and_inputs
         UpperCamelCase__ :			Tuple 						={"input_ids": input_ids, "attention_mask": input_mask}
         return config, inputs_dict




@require_torch
class 							lowercase__(						snake_case__			,							snake_case__			,							unittest.TestCase			):






      '''simple docstring'''







      snake_case__       =					(
          (
              ErnieMModel,
              ErnieMForMultipleChoice,
              ErnieMForQuestionAnswering,
              ErnieMForSequenceClassification,
              ErnieMForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      snake_case__       =					()
      snake_case__       =					(
          {
              '''feature-extraction''': ErnieMModel,
              '''question-answering''': ErnieMForQuestionAnswering,
              '''text-classification''': ErnieMForSequenceClassification,
              '''token-classification''': ErnieMForTokenClassification,
              '''zero-shot''': ErnieMForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      snake_case__       =					False




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Optional[Any]:







         """simple docstring"""







         if pipeline_test_casse_name == "QAPipelineTests":
            return True

         return False




      def    UpperCAmelCase							(		self)					->						Optional[int]:







         """simple docstring"""







         UpperCamelCase__ :			Dict 						=ErnieMModelTester(self)
         UpperCamelCase__ :			int 						=ConfigTester(self   ,			config_class=__SCREAMING_SNAKE_CASE   ,			hidden_size=37)




      def    UpperCAmelCase							(		self)					->						List[Any]:







         """simple docstring"""







         self.config_tester.run_common_tests()




      def    UpperCAmelCase							(		self)					->						List[str]:







         """simple docstring"""







         UpperCamelCase__ :			Any 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE)




      def    UpperCAmelCase							(		self)					->						Union[str, Any]:







         """simple docstring"""







         UpperCamelCase__ :			int 						=self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
            UpperCamelCase__ :			List[str] 						=type
            self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE)




      def    UpperCAmelCase							(		self)					->						List[str]:







         """simple docstring"""







         UpperCamelCase__ :			Optional[Any] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_multiple_choice(*__SCREAMING_SNAKE_CASE)




      def    UpperCAmelCase							(		self)					->						Tuple:







         """simple docstring"""







         UpperCamelCase__ :			Optional[Any] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE)




      def    UpperCAmelCase							(		self)					->						Any:







         """simple docstring"""







         UpperCamelCase__ :			Optional[int] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE)




      def    UpperCAmelCase							(		self)					->						List[str]:







         """simple docstring"""







         UpperCamelCase__ :			Union[str, Any] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_information_extraction(*__SCREAMING_SNAKE_CASE)




      def    UpperCAmelCase							(		self)					->						Union[str, Any]:







         """simple docstring"""







         UpperCamelCase__ :			Optional[Any] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE)




      @slow
      def    UpperCAmelCase							(		self)					->						Tuple:







         """simple docstring"""







         for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCamelCase__ :			Optional[Any] 						=ErnieMModel.from_pretrained(__SCREAMING_SNAKE_CASE)
            self.assertIsNotNone(__SCREAMING_SNAKE_CASE)




@require_torch
class 							lowercase__(						unittest.TestCase			):






      '''simple docstring'''







      @slow
      def    UpperCAmelCase							(		self)					->						Any:







         """simple docstring"""







         UpperCamelCase__ :			Optional[int] 						=ErnieMModel.from_pretrained("susnato/ernie-m-base_pytorch")
         model.eval()
         UpperCamelCase__ :			int 						=torch.tensor([[0, 1, 2, 3, 4, 5]])
         UpperCamelCase__ :			List[Any] 						=model(__SCREAMING_SNAKE_CASE)[0]

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

         UpperCamelCase__ :			Dict 						=torch.Size((1, 6, hidden_size))
         self.assertEqual(output.shape   ,			__SCREAMING_SNAKE_CASE)

         UpperCamelCase__ :			Union[str, Any] 						=torch.tensor(
             [[[-0.00_12, 0.12_45, -0.02_14], [-0.07_42, 0.02_44, -0.07_71], [-0.03_33, 0.11_64, -0.15_54]]])

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

