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



   def __init__(				self   :   Dict			,	A__   :   List[Any]			,	A__   :   Dict=13			,	A__   :   Optional[Any]=7			,	A__   :   Optional[int]=True			,	A__   :   Optional[int]=True			,	A__   :   List[Any]=True			,	A__   :   List[str]=99			,	A__   :   Optional[Any]=32			,	A__   :   Union[str, Any]=5			,	A__   :   List[str]=4			,	A__   :   List[str]=37			,	A__   :   Optional[Any]="gelu"			,	A__   :   Union[str, Any]=0.1			,	A__   :   Tuple=0.1			,	A__   :   str=5_12			,	A__   :   Any=16			,	A__   :   Tuple=2			,	A__   :   List[str]=0.02			,	A__   :   int=3			,	A__   :   Union[str, Any]=4			,	A__   :   int=None			,	)							->     Dict:




         '''simple docstring'''




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



   def 					UpperCAmelCase__       (				self   :   Any   )							->     Optional[Any]:




         '''simple docstring'''




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

         snake_case_     :			Optional[int]       =       None
         if self.use_input_mask:
               snake_case_     :			Union[str, Any]       =       random_attention_mask([self.batch_size, self.seq_length]   )

         snake_case_     :			str       =       None
         snake_case_     :			Union[str, Any]       =       None
         snake_case_     :			List[str]       =       None
         if self.use_labels:
               snake_case_     :			Optional[int]       =       ids_tensor([self.batch_size]			,	self.type_sequence_label_size   )
               snake_case_     :			List[str]       =       ids_tensor([self.batch_size, self.seq_length]			,	self.num_labels   )
               snake_case_     :			List[Any]       =       ids_tensor([self.batch_size]			,	self.num_choices   )

         snake_case_     :			Tuple       =       self.get_config()

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



   def 					UpperCAmelCase__       (				self   :   Any   )							->     Optional[int]:




         '''simple docstring'''




         snake_case_     :			int       =       ids_tensor([self.batch_size, self.seq_length]			,	self.vocab_size   )

         snake_case_     :			List[str]       =       None
         if self.use_input_mask:
               snake_case_     :			Optional[int]       =       random_attention_mask([self.batch_size, self.seq_length]   )
         snake_case_     :			int       =       self.get_config()

         return config, input_ids, input_mask



   def 					UpperCAmelCase__       (				self   :   List[str]   )							->     Any:




         '''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   :   Optional[Any]			,	A__   :   Union[str, Any]			,	A__   :   List[str]			,	A__   :   Dict			,	A__   :   Optional[int]			,	A__   :   Optional[Any]			,	A__   :   Tuple   )							->     str:




         '''simple docstring'''




         snake_case_     :			int       =       ErnieMModel(config=A__   )
         model.to(A__   )
         model.eval()
         snake_case_     :			Union[str, Any]       =       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   :   Optional[Any]			,	A__   :   Optional[int]			,	A__   :   Union[str, Any]			,	A__   :   Dict			,	A__   :   Any			,	A__   :   List[str]			,	A__   :   List[str]   )							->     Optional[int]:




         '''simple docstring'''




         snake_case_     :			int       =       ErnieMForQuestionAnswering(config=A__   )
         model.to(A__   )
         model.eval()
         snake_case_     :			List[Any]       =       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   :   List[Any]			,	A__   :   Tuple			,	A__   :   str			,	A__   :   Union[str, Any]			,	A__   :   int			,	A__   :   List[str]			,	A__   :   Tuple   )							->     int:




         '''simple docstring'''




         snake_case_     :			List[Any]       =       ErnieMForInformationExtraction(config=A__   )
         model.to(A__   )
         model.eval()
         snake_case_     :			Any       =       torch.ones_like(A__			,	dtype=torch.floataa   )
         snake_case_     :			Optional[int]       =       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   :   Optional[Any]			,	A__   :   Any			,	A__   :   Any			,	A__   :   Tuple			,	A__   :   Optional[Any]			,	A__   :   Tuple			,	A__   :   Dict   )							->     int:




         '''simple docstring'''




         snake_case_     :			int       =       self.num_labels
         snake_case_     :			int       =       ErnieMForSequenceClassification(A__   )
         model.to(A__   )
         model.eval()
         snake_case_     :			Dict       =       model(A__			,	attention_mask=A__			,	labels=A__   )
         self.parent.assertEqual(result.logits.shape			,	(self.batch_size, self.num_labels)   )



   def 					UpperCAmelCase__       (				self   :   List[str]			,	A__   :   int			,	A__   :   Optional[Any]			,	A__   :   str			,	A__   :   List[str]			,	A__   :   Tuple			,	A__   :   Union[str, Any]   )							->     Dict:




         '''simple docstring'''




         snake_case_     :			Any       =       self.num_labels
         snake_case_     :			Tuple       =       ErnieMForTokenClassification(config=A__   )
         model.to(A__   )
         model.eval()
         input_ids.to(A__   )
         input_mask.to(A__   )
         token_labels.to(A__   )

         snake_case_     :			List[str]       =       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   :   Any			,	A__   :   List[Any]			,	A__   :   Union[str, Any]			,	A__   :   int			,	A__   :   Any			,	A__   :   Dict			,	A__   :   Tuple   )							->     Union[str, Any]:




         '''simple docstring'''




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



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




         '''simple docstring'''




         snake_case_     :			Any       =       self.prepare_config_and_inputs()
         (
             (
             snake_case_
         )  ,(
             snake_case_
         )  ,(
             snake_case_
         )  ,(
             snake_case_
         )  ,(
             snake_case_
         )  ,(
             snake_case_
         )  ,
         )     :			str       =       config_and_inputs
         snake_case_     :			Union[str, Any]       =       {"input_ids": input_ids, "attention_mask": input_mask}
         return config, inputs_dict



@require_torch
class 					snake_case__       (				_UpperCamelCase					,						_UpperCamelCase					,						unittest.TestCase							):
   _SCREAMING_SNAKE_CASE	: Optional[Any]						    =      (
       (
           ErnieMModel,
           ErnieMForMultipleChoice,
           ErnieMForQuestionAnswering,
           ErnieMForSequenceClassification,
           ErnieMForTokenClassification,
       )
       if is_torch_available()
       else ()
   )
   _SCREAMING_SNAKE_CASE	: Optional[Any]						    =      ()
   _SCREAMING_SNAKE_CASE	: str						    =      (
       {
           "feature-extraction": ErnieMModel,
           "question-answering": ErnieMForQuestionAnswering,
           "text-classification": ErnieMForSequenceClassification,
           "token-classification": ErnieMForTokenClassification,
           "zero-shot": ErnieMForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   _SCREAMING_SNAKE_CASE	: Optional[int]						    =      False



   def 					UpperCAmelCase__       (				self   :   str			,	A__   :   Union[str, Any]			,	A__   :   int			,	A__   :   Optional[Any]			,	A__   :   str			,	A__   :   Dict   )							->     Union[str, Any]:




         '''simple docstring'''




         if pipeline_test_casse_name == "QAPipelineTests":
               return True

         return False



   def 					UpperCAmelCase__       (				self   :   Dict   )							->     Optional[int]:




         '''simple docstring'''




         snake_case_     :			List[str]       =       ErnieMModelTester(self   )
         snake_case_     :			int       =       ConfigTester(self			,	config_class=A__			,	hidden_size=37   )



   def 					UpperCAmelCase__       (				self   :   Optional[Any]   )							->     str:




         '''simple docstring'''




         self.config_tester.run_common_tests()



   def 					UpperCAmelCase__       (				self   :   Any   )							->     Optional[Any]:




         '''simple docstring'''




         snake_case_     :			Any       =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*A__   )



   def 					UpperCAmelCase__       (				self   :   Tuple   )							->     Optional[int]:




         '''simple docstring'''




         snake_case_     :			Union[str, Any]       =       self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
               snake_case_     :			Any       =       type
               self.model_tester.create_and_check_model(*A__   )



   def 					UpperCAmelCase__       (				self   :   Optional[int]   )							->     List[str]:




         '''simple docstring'''




         snake_case_     :			List[Any]       =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_multiple_choice(*A__   )



   def 					UpperCAmelCase__       (				self   :   List[Any]   )							->     Tuple:




         '''simple docstring'''




         snake_case_     :			Optional[int]       =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*A__   )



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




         '''simple docstring'''




         snake_case_     :			Union[str, Any]       =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*A__   )



   def 					UpperCAmelCase__       (				self   :   int   )							->     Dict:




         '''simple docstring'''




         snake_case_     :			Any       =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_information_extraction(*A__   )



   def 					UpperCAmelCase__       (				self   :   Optional[int]   )							->     List[Any]:




         '''simple docstring'''




         snake_case_     :			Dict       =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*A__   )



   @slow
   def 					UpperCAmelCase__       (				self   :   List[str]   )							->     int:




         '''simple docstring'''




         for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               snake_case_     :			List[Any]       =       ErnieMModel.from_pretrained(A__   )
               self.assertIsNotNone(A__   )



@require_torch
class 					snake_case__       (				unittest.TestCase							):



   @slow
   def 					UpperCAmelCase__       (				self   :   Any   )							->     Optional[Any]:




         '''simple docstring'''




         snake_case_     :			Dict       =       ErnieMModel.from_pretrained("susnato/ernie-m-base_pytorch"   )
         model.eval()
         snake_case_     :			Union[str, Any]       =       torch.tensor([[0, 1, 2, 3, 4, 5]]   )
         snake_case_     :			Union[str, Any]       =       model(A__   )[0]

         # TODO Replace vocab size
         snake_case_     :			Tuple       =       7_68

         snake_case_     :			int       =       torch.Size((1, 6, hidden_size)   )
         self.assertEqual(output.shape			,	A__   )

         snake_case_     :			Tuple       =       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   )   )




import math



class 					snake_case__       :



   def __init__(				self   :   int			,	A__   :   str   )							->     int:




         '''simple docstring'''




         snake_case_     :			Tuple       =       len(A__   )
         snake_case_     :			Union[str, Any]       =       [0] * (
             4 * self.N
         )  # approximate the overall size of segment tree with array N
         self.build(1			,	0			,	self.N - 1   )



   def 					UpperCAmelCase__       (				self   :   Optional[Any]			,	A__   :   Tuple   )							->     Optional[int]:




         '''simple docstring'''




         return idx * 2



   def 					UpperCAmelCase__       (				self   :   Union[str, Any]			,	A__   :   List[str]   )							->     Dict:




         '''simple docstring'''




         return idx * 2 + 1



   def 					UpperCAmelCase__       (				self   :   str			,	A__   :   Any			,	A__   :   Optional[int]			,	A__   :   Tuple   )							->     List[str]:  # noqa: E741




         '''simple docstring'''




         if l == r:
               snake_case_     :			str       =       A[l]
         else:
               snake_case_     :			List[str]       =       (l + r) // 2
               self.build(self.left(A__   )			,	A__			,	A__   )
               self.build(self.right(A__   )			,	mid + 1			,	A__   )
               snake_case_     :			Union[str, Any]       =       max(self.st[self.left(A__   )]			,	self.st[self.right(A__   )]   )



   def 					UpperCAmelCase__       (				self   :   Optional[Any]			,	A__   :   List[str]			,	A__   :   Union[str, Any]			,	A__   :   str   )							->     int:




         '''simple docstring'''




         return self.update_recursive(1			,	0			,	self.N - 1			,	a - 1			,	b - 1			,	A__   )



   def 					UpperCAmelCase__       (				self   :   Optional[int]			,	A__   :   Optional[Any]			,	A__   :   List[str]			,	A__   :   Optional[Any]			,	A__   :   Union[str, Any]			,	A__   :   List[Any]			,	A__   :   str   )							->     Any:  # noqa: E741




         '''simple docstring'''




         if r < a or l > b:
               return True
         if l == r:
               snake_case_     :			List[str]       =       val
               return True
         snake_case_     :			Dict       =       (l + r) // 2
         self.update_recursive(self.left(A__   )			,	A__			,	A__			,	A__			,	A__			,	A__   )
         self.update_recursive(self.right(A__   )			,	mid + 1			,	A__			,	A__			,	A__			,	A__   )
         snake_case_     :			Tuple       =       max(self.st[self.left(A__   )]			,	self.st[self.right(A__   )]   )
         return True



   def 					UpperCAmelCase__       (				self   :   Optional[int]			,	A__   :   List[Any]			,	A__   :   Optional[Any]   )							->     str:




         '''simple docstring'''




         return self.query_recursive(1			,	0			,	self.N - 1			,	a - 1			,	b - 1   )



   def 					UpperCAmelCase__       (				self   :   Union[str, Any]			,	A__   :   Any			,	A__   :   int			,	A__   :   Tuple			,	A__   :   Tuple			,	A__   :   Dict   )							->     str:  # noqa: E741




         '''simple docstring'''




         if r < a or l > b:
               return -math.inf
         if l >= a and r <= b:
               return self.st[idx]
         snake_case_     :			Dict       =       (l + r) // 2
         snake_case_     :			Optional[Any]       =       self.query_recursive(self.left(A__   )			,	A__			,	A__			,	A__			,	A__   )
         snake_case_     :			Any       =       self.query_recursive(self.right(A__   )			,	mid + 1			,	A__			,	A__			,	A__   )
         return max(A__			,	A__   )



   def 					UpperCAmelCase__       (				self   :   Dict   )							->     Optional[Any]:




         '''simple docstring'''




         snake_case_     :			int       =       []
         for i in range(1			,	N + 1   ):
               show_list += [self.query(A__			,	A__   )]
         print(A__   )


if __name__ == "__main__":
     UpperCAmelCase							=       [1, 2, -4, 7, 3, -5, 6, 1_1, -2_0, 9, 1_4, 1_5, 5, 2, -8]
     UpperCAmelCase							=       1_5
     UpperCAmelCase							=       SegmentTree(A)
     print(segt.query(4, 6))
     print(segt.query(7, 1_1))
     print(segt.query(7, 1_2))
     segt.update(1, 3, 1_1_1)
     print(segt.query(1, 1_5))
     segt.update(7, 8, 2_3_5)
     segt.show_data()



