






"""simple docstring"""







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






    def __init__( self	,A	,A=13	,A=7	,A=True	,A=True	,A=True	,A=99	,A=32	,A=5	,A=4	,A=37	,A="gelu"	,A=0.1	,A=0.1	,A=512	,A=16	,A=2	,A=0.02	,A=3	,A=4	,A=None	,):
          UpperCAmelCase							=       parent
          UpperCAmelCase							=       batch_size
          UpperCAmelCase							=       seq_length
          UpperCAmelCase							=       is_training
          UpperCAmelCase							=       use_input_mask
          UpperCAmelCase							=       use_labels
          UpperCAmelCase							=       vocab_size
          UpperCAmelCase							=       hidden_size
          UpperCAmelCase							=       num_hidden_layers
          UpperCAmelCase							=       num_attention_heads
          UpperCAmelCase							=       intermediate_size
          UpperCAmelCase							=       hidden_act
          UpperCAmelCase							=       hidden_dropout_prob
          UpperCAmelCase							=       attention_probs_dropout_prob
          UpperCAmelCase							=       max_position_embeddings
          UpperCAmelCase							=       type_vocab_size
          UpperCAmelCase							=       type_sequence_label_size
          UpperCAmelCase							=       initializer_range
          UpperCAmelCase							=       num_labels
          UpperCAmelCase							=       num_choices
          UpperCAmelCase							=       scope






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       ids_tensor([self.batch_size, self.seq_length]	,self.vocab_size					)

          UpperCAmelCase							=       None
          if self.use_input_mask:
                UpperCAmelCase							=       random_attention_mask([self.batch_size, self.seq_length]					)

          UpperCAmelCase							=       None
          UpperCAmelCase							=       None
          UpperCAmelCase							=       None
          if self.use_labels:
                UpperCAmelCase							=       ids_tensor([self.batch_size]	,self.type_sequence_label_size					)
                UpperCAmelCase							=       ids_tensor([self.batch_size, self.seq_length]	,self.num_labels					)
                UpperCAmelCase							=       ids_tensor([self.batch_size]	,self.num_choices					)

          UpperCAmelCase							=       self.get_config()

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






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       ids_tensor([self.batch_size, self.seq_length]	,self.vocab_size					)

          UpperCAmelCase							=       None
          if self.use_input_mask:
                UpperCAmelCase							=       random_attention_mask([self.batch_size, self.seq_length]					)
          UpperCAmelCase							=       self.get_config()

          return config, input_ids, input_mask






    def 				_UpperCamelCase							( self					):
          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	,A	,A	,A	,A	,A	,A					):
          UpperCAmelCase							=       ErnieMModel(config=A					)
          model.to(A					)
          model.eval()
          UpperCAmelCase							=       model(A	,return_dict=A					)
          self.parent.assertEqual(result.last_hidden_state.shape	,(self.batch_size, self.seq_length, self.hidden_size)					)






    def 				_UpperCamelCase							( self	,A	,A	,A	,A	,A	,A					):
          UpperCAmelCase							=       ErnieMForQuestionAnswering(config=A					)
          model.to(A					)
          model.eval()
          UpperCAmelCase							=       model(
              A	,attention_mask=A	,start_positions=A	,end_positions=A	,)
          self.parent.assertEqual(result.start_logits.shape	,(self.batch_size, self.seq_length)					)
          self.parent.assertEqual(result.end_logits.shape	,(self.batch_size, self.seq_length)					)






    def 				_UpperCamelCase							( self	,A	,A	,A	,A	,A	,A					):
          UpperCAmelCase							=       ErnieMForInformationExtraction(config=A					)
          model.to(A					)
          model.eval()
          UpperCAmelCase							=       torch.ones_like(A	,dtype=torch.floataa					)
          UpperCAmelCase							=       model(
              A	,attention_mask=A	,start_positions=A	,end_positions=A	,)
          self.parent.assertEqual(result.start_logits.shape	,(self.batch_size, self.seq_length)					)
          self.parent.assertEqual(result.end_logits.shape	,(self.batch_size, self.seq_length)					)






    def 				_UpperCamelCase							( self	,A	,A	,A	,A	,A	,A					):
          UpperCAmelCase							=       self.num_labels
          UpperCAmelCase							=       ErnieMForSequenceClassification(A					)
          model.to(A					)
          model.eval()
          UpperCAmelCase							=       model(A	,attention_mask=A	,labels=A					)
          self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.num_labels)					)






    def 				_UpperCamelCase							( self	,A	,A	,A	,A	,A	,A					):
          UpperCAmelCase							=       self.num_labels
          UpperCAmelCase							=       ErnieMForTokenClassification(config=A					)
          model.to(A					)
          model.eval()
          input_ids.to(A					)
          input_mask.to(A					)
          token_labels.to(A					)

          UpperCAmelCase							=       model(A	,attention_mask=A	,labels=A					)

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






    def 				_UpperCamelCase							( self	,A	,A	,A	,A	,A	,A					):
          UpperCAmelCase							=       self.num_choices
          UpperCAmelCase							=       ErnieMForMultipleChoice(config=A					)
          model.to(A					)
          model.eval()
          UpperCAmelCase							=       input_ids.unsqueeze(1					).expand(-1	,self.num_choices	,-1					).contiguous()
          UpperCAmelCase							=       input_mask.unsqueeze(1					).expand(-1	,self.num_choices	,-1					).contiguous()
          UpperCAmelCase							=       model(
              A	,attention_mask=A	,labels=A	,)
          self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.num_choices)					)






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       self.prepare_config_and_inputs()
          (
              (
              UpperCAmelCase
          )			,					(
              UpperCAmelCase
          )			,					(
              UpperCAmelCase
          )			,					(
              UpperCAmelCase
          )			,					(
              UpperCAmelCase
          )			,					(
              UpperCAmelCase
          )			,					
          )							=       config_and_inputs
          UpperCAmelCase							=       {"""input_ids""": input_ids, """attention_mask""": input_mask}
          return config, inputs_dict







@require_torch
class 					lowerCamelCase__					(       snake_case    ,     snake_case    ,     unittest.TestCase ):
    SCREAMING_SNAKE_CASE		=			(
        (
            ErnieMModel,
            ErnieMForMultipleChoice,
            ErnieMForQuestionAnswering,
            ErnieMForSequenceClassification,
            ErnieMForTokenClassification,
        )
        if is_torch_available()
        else ()
    )
    SCREAMING_SNAKE_CASE		=			()
    SCREAMING_SNAKE_CASE		=			(
        {
            '''feature-extraction''': ErnieMModel,
            '''question-answering''': ErnieMForQuestionAnswering,
            '''text-classification''': ErnieMForSequenceClassification,
            '''token-classification''': ErnieMForTokenClassification,
            '''zero-shot''': ErnieMForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    SCREAMING_SNAKE_CASE		=			False






    def 				_UpperCamelCase							( self	,A	,A	,A	,A	,A					):
          if pipeline_test_casse_name == "QAPipelineTests":
                return True

          return False






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       ErnieMModelTester(self					)
          UpperCAmelCase							=       ConfigTester(self	,config_class=A	,hidden_size=37					)






    def 				_UpperCamelCase							( self					):
          self.config_tester.run_common_tests()






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*A					)






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       self.model_tester.prepare_config_and_inputs()
          for type in ["absolute", "relative_key", "relative_key_query"]:
                UpperCAmelCase							=       type
                self.model_tester.create_and_check_model(*A					)






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*A					)






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*A					)






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*A					)






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_information_extraction(*A					)






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*A					)






    @slow
    def 				_UpperCamelCase							( self					):
          for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCAmelCase							=       ErnieMModel.from_pretrained(A					)
                self.assertIsNotNone(A					)







@require_torch
class 					lowerCamelCase__					(       unittest.TestCase ):






    @slow
    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       ErnieMModel.from_pretrained("""susnato/ernie-m-base_pytorch"""					)
          model.eval()
          UpperCAmelCase							=       torch.tensor([[0, 1, 2, 3, 4, 5]]					)
          UpperCAmelCase							=       model(A					)[0]

          # TODO Replace vocab size
          UpperCAmelCase							=       768

          UpperCAmelCase							=       torch.Size((1, 6, hidden_size)					)
          self.assertEqual(output.shape	,A					)

          UpperCAmelCase							=       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]	,A	,atol=1e-3					)					)











"""simple docstring"""







from __future__ import annotations

from typing import Generic, TypeVar

_UpperCamelCase     			=  TypeVar("""T""")







class 					lowerCamelCase__					(       Generic[T] ):






    def __init__( self	,A					):
          UpperCAmelCase							=       data  # Assign data
          UpperCAmelCase							=       None  # Initialize next as null
          UpperCAmelCase							=       None  # Initialize prev as null







class 					lowerCamelCase__					(       Generic[T] ):






    def __init__( self					):
          UpperCAmelCase							=       None






    def 				_UpperCamelCase							( self	,A					):
          if self.head is None:
                UpperCAmelCase							=       Node(A					)
          else:
                UpperCAmelCase							=       Node(A					)
                UpperCAmelCase							=       new_node
                UpperCAmelCase							=       self.head
                UpperCAmelCase							=       None
                UpperCAmelCase							=       new_node






    def 				_UpperCamelCase							( self					):
          if self.head is None:
                return None
          else:
                assert self.head is not None
                UpperCAmelCase							=       self.head.data
                UpperCAmelCase							=       self.head.next
                if self.head is not None:
                      UpperCAmelCase							=       None
                return temp






    def 				_UpperCamelCase							( self					):
          return self.head.data if self.head is not None else None






    def __len__( self					):
          UpperCAmelCase							=       self.head
          UpperCAmelCase							=       0
          while temp is not None:
                count += 1
                UpperCAmelCase							=       temp.next
          return count






    def 				_UpperCamelCase							( self					):
          return self.head is None






    def 				_UpperCamelCase							( self					):
          print("""stack elements are:"""					)
          UpperCAmelCase							=       self.head
          while temp is not None:
                print(temp.data	,end="""->"""					)
                UpperCAmelCase							=       temp.next


# Code execution starts here
if __name__ == "__main__":
  # Start with the empty stack
  _UpperCamelCase     			=  Stack()

  # Insert 4 at the beginning. So stack becomes 4->None
  print("""Stack operations using Doubly LinkedList""")
  stack.push(4)

  # Insert 5 at the beginning. So stack becomes 4->5->None
  stack.push(5)

  # Insert 6 at the beginning. So stack becomes 4->5->6->None
  stack.push(6)

  # Insert 7 at the beginning. So stack becomes 4->5->6->7->None
  stack.push(7)

  # Print the stack
  stack.print_stack()

  # Print the top element
  print("""\nTop element is """, stack.top())

  # Print the stack size
  print("""Size of the stack is """, len(stack))

  # pop the top element
  stack.pop()

  # pop the top element
  stack.pop()

  # two elements have now been popped off
  stack.print_stack()

  # Print True if the stack is empty else False
  print("""\nstack is empty:""", stack.is_empty())



