




'''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     UpperCamelCase_     :







						"""simple docstring"""


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





											"""simple docstring"""







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







						def       _a    (     self      :       List[str]    ):





											"""simple docstring"""







											A_ :     Any =							ids_tensor([self.batch_size, self.seq_length]							, self.vocab_size    )

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

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

											A_ :     List[Any] =							self.get_config()

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







						def       _a    (     self      :       Any    ):





											"""simple docstring"""







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

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

											return config, input_ids, input_mask







						def       _a    (     self      :       str    ):





											"""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       _a    (     self      :       Any							, _lowerCamelCase      :       Any							, _lowerCamelCase      :       Union[str, Any]							, _lowerCamelCase      :       Any							, _lowerCamelCase      :       Union[str, Any]							, _lowerCamelCase      :       List[str]							, _lowerCamelCase      :       Tuple    ):





											"""simple docstring"""







											A_ :     Dict =							ErnieMModel(config=_lowerCamelCase    )
											model.to(_lowerCamelCase    )
											model.eval()
											A_ :     Optional[Any] =							model(_lowerCamelCase							, return_dict=_lowerCamelCase    )
											self.parent.assertEqual(result.last_hidden_state.shape							, (self.batch_size, self.seq_length, self.hidden_size)    )







						def       _a    (     self      :       Any							, _lowerCamelCase      :       Dict							, _lowerCamelCase      :       str							, _lowerCamelCase      :       Any							, _lowerCamelCase      :       Any							, _lowerCamelCase      :       Any							, _lowerCamelCase      :       Optional[Any]    ):





											"""simple docstring"""







											A_ :     List[str] =							ErnieMForQuestionAnswering(config=_lowerCamelCase    )
											model.to(_lowerCamelCase    )
											model.eval()
											A_ :     Any =							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       _a    (     self      :       Union[str, Any]							, _lowerCamelCase      :       Tuple							, _lowerCamelCase      :       int							, _lowerCamelCase      :       Union[str, Any]							, _lowerCamelCase      :       Tuple							, _lowerCamelCase      :       int							, _lowerCamelCase      :       Dict    ):





											"""simple docstring"""







											A_ :     List[Any] =							ErnieMForInformationExtraction(config=_lowerCamelCase    )
											model.to(_lowerCamelCase    )
											model.eval()
											A_ :     Optional[Any] =							torch.ones_like(_lowerCamelCase							, dtype=torch.floataa    )
											A_ :     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       _a    (     self      :       Dict							, _lowerCamelCase      :       List[Any]							, _lowerCamelCase      :       Union[str, Any]							, _lowerCamelCase      :       Tuple							, _lowerCamelCase      :       Tuple							, _lowerCamelCase      :       Tuple							, _lowerCamelCase      :       Any    ):





											"""simple docstring"""







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







						def       _a    (     self      :       List[Any]							, _lowerCamelCase      :       str							, _lowerCamelCase      :       Optional[Any]							, _lowerCamelCase      :       str							, _lowerCamelCase      :       Optional[Any]							, _lowerCamelCase      :       str							, _lowerCamelCase      :       Union[str, Any]    ):





											"""simple docstring"""







											A_ :     Dict =							self.num_labels
											A_ :     int =							ErnieMForTokenClassification(config=_lowerCamelCase    )
											model.to(_lowerCamelCase    )
											model.eval()
											input_ids.to(_lowerCamelCase    )
											input_mask.to(_lowerCamelCase    )
											token_labels.to(_lowerCamelCase    )

											A_ :     Any =							model(_lowerCamelCase							, attention_mask=_lowerCamelCase							, labels=_lowerCamelCase    )

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







						def       _a    (     self      :       Tuple							, _lowerCamelCase      :       Dict							, _lowerCamelCase      :       Tuple							, _lowerCamelCase      :       Optional[int]							, _lowerCamelCase      :       List[str]							, _lowerCamelCase      :       int							, _lowerCamelCase      :       List[Any]    ):





											"""simple docstring"""







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







						def       _a    (     self      :       Any    ):





											"""simple docstring"""







											A_ :     Optional[int] =							self.prepare_config_and_inputs()
											(
											    (
											    A_
											) ,(
											    A_
											) ,(
											    A_
											) ,(
											    A_
											) ,(
											    A_
											) ,(
											    A_
											) ,
											) :     str =							config_and_inputs
											A_ :     int =							{'''input_ids''': input_ids, '''attention_mask''': input_mask}
											return config, inputs_dict






@require_torch
class     UpperCamelCase_     (a__,      a__,      unittest.TestCase     ):







						"""simple docstring"""


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







						def       _a    (     self      :       Optional[Any]							, _lowerCamelCase      :       Union[str, Any]							, _lowerCamelCase      :       Union[str, Any]							, _lowerCamelCase      :       Union[str, Any]							, _lowerCamelCase      :       Union[str, Any]							, _lowerCamelCase      :       int    ):





											"""simple docstring"""







											if pipeline_test_casse_name == "QAPipelineTests":
																return True

											return False







						def       _a    (     self      :       List[Any]    ):





											"""simple docstring"""







											A_ :     Dict =							ErnieMModelTester(self    )
											A_ :     int =							ConfigTester(self							, config_class=_lowerCamelCase							, hidden_size=37    )







						def       _a    (     self      :       List[str]    ):





											"""simple docstring"""







											self.config_tester.run_common_tests()







						def       _a    (     self      :       Dict    ):





											"""simple docstring"""







											A_ :     List[Any] =							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_lowerCamelCase    )







						def       _a    (     self      :       Union[str, Any]    ):





											"""simple docstring"""







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







						def       _a    (     self      :       str    ):





											"""simple docstring"""







											A_ :     Dict =							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_multiple_choice(*_lowerCamelCase    )







						def       _a    (     self      :       List[str]    ):





											"""simple docstring"""







											A_ :     int =							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_question_answering(*_lowerCamelCase    )







						def       _a    (     self      :       List[str]    ):





											"""simple docstring"""







											A_ :     Any =							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_sequence_classification(*_lowerCamelCase    )







						def       _a    (     self      :       List[Any]    ):





											"""simple docstring"""







											A_ :     Optional[Any] =							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_information_extraction(*_lowerCamelCase    )







						def       _a    (     self      :       Any    ):





											"""simple docstring"""







											A_ :     str =							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_token_classification(*_lowerCamelCase    )







						@slow
						def       _a    (     self      :       List[str]    ):





											"""simple docstring"""







											for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																A_ :     List[str] =							ErnieMModel.from_pretrained(_lowerCamelCase    )
																self.assertIsNotNone(_lowerCamelCase    )






@require_torch
class     UpperCamelCase_     (unittest.TestCase     ):







						"""simple docstring"""


						@slow
						def       _a    (     self      :       Dict    ):





											"""simple docstring"""







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

											# TODO Replace vocab size
											A_ :     Optional[int] =							768

											A_ :     str =							torch.Size((1, 6, hidden_size)    )
											self.assertEqual(output.shape							, _lowerCamelCase    )

											A_ :     str =							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]							, _lowerCamelCase							, atol=1E-3    )    )









'''simple docstring'''




from __future__ import annotations

snake_case__          =						[2, 1, 4, 5, 6, 0, 8, 9, 1, 2, 0, 6, 4, 2, 0, 6, 5, 3, 2, 7]






class     UpperCamelCase_     :







						"""simple docstring"""


						def __init__(     self      :       Dict							, _lowerCamelCase      :       int    ):





											"""simple docstring"""







											A_ :     int =							-1
											A_ :     int =							-1
											A_ :     list[int] =							[-1] * length
											A_ :     Node | None =							None
											A_ :     Node | None =							None







						def __repr__(     self      :       List[str]    ):





											"""simple docstring"""







											return f'Node(min_value={self.minn} max_value={self.maxx})'






def 						snake_case__    ( lowerCamelCase__				:   list[int]			)   ->   Node | None:
					A_ :     Optional[Any] =							Node(len(lowerCamelCase__			)			)
					A_ ,A_ :     str =							min(lowerCamelCase__			), max(lowerCamelCase__			)
					# Leaf node case where the node contains only one unique value
					if root.minn == root.maxx:
										return root
					A_ :     Optional[int] =							(root.minn + root.maxx) // 2

					A_ :     list[int] =							[]
					A_ :     list[int] =							[]

					for index, num in enumerate(lowerCamelCase__			):
										if num <= pivot:
															left_arr.append(lowerCamelCase__			)
										else:
															right_arr.append(lowerCamelCase__			)
										A_ :     Union[str, Any] =							len(lowerCamelCase__			)
					A_ :     Optional[int] =							build_tree(lowerCamelCase__			)
					A_ :     Optional[int] =							build_tree(lowerCamelCase__			)
					return root




def 						snake_case__    ( lowerCamelCase__				:   Node | None			,		lowerCamelCase__				:   int			,		lowerCamelCase__				:   int			)   ->   int:
					if index < 0 or node is None:
										return 0
					# Leaf node cases
					if node.minn == node.maxx:
										return index + 1 if node.minn == num else 0
					A_ :     List[Any] =							(node.minn + node.maxx) // 2
					if num <= pivot:
										# go the left subtree and map index to the left subtree
										return rank_till_index(node.left			,		lowerCamelCase__			,		node.map_left[index] - 1			)
					else:
										# go to the right subtree and map index to the right subtree
										return rank_till_index(node.right			,		lowerCamelCase__			,		index - node.map_left[index]			)




def 						snake_case__    ( lowerCamelCase__				:   Node | None			,		lowerCamelCase__				:   int			,		lowerCamelCase__				:   int			,		lowerCamelCase__				:   int			)   ->   int:
					if start > end:
										return 0
					A_ :     Union[str, Any] =							rank_till_index(lowerCamelCase__			,		lowerCamelCase__			,		lowerCamelCase__			)
					A_ :     Union[str, Any] =							rank_till_index(lowerCamelCase__			,		lowerCamelCase__			,		start - 1			)
					return rank_till_end - rank_before_start




def 						snake_case__    ( lowerCamelCase__				:   Node | None			,		lowerCamelCase__				:   int			,		lowerCamelCase__				:   int			,		lowerCamelCase__				:   int			)   ->   int:
					if index > (end - start) or start > end or node is None:
										return -1
					# Leaf node case
					if node.minn == node.maxx:
										return node.minn
					# Number of elements in the left subtree in interval [start, end]
					A_ :     List[str] =							node.map_left[end] - (
					    node.map_left[start - 1] if start else 0
					)
					if num_elements_in_left_tree > index:
										return quantile(
										    node.left			,		lowerCamelCase__			,		(node.map_left[start - 1] if start else 0)			,		node.map_left[end] - 1			,		)
					else:
										return quantile(
										    node.right			,		index - num_elements_in_left_tree			,		start - (node.map_left[start - 1] if start else 0)			,		end - node.map_left[end]			,		)




def 						snake_case__    ( lowerCamelCase__				:   Node | None			,		lowerCamelCase__				:   int			,		lowerCamelCase__				:   int			,		lowerCamelCase__				:   int			,		lowerCamelCase__				:   int			)   ->   int:
					if (
					    start > end
					    or node is None
					    or start_num > end_num
					    or node.minn > end_num
					    or node.maxx < start_num
					):
										return 0
					if start_num <= node.minn and node.maxx <= end_num:
										return end - start + 1
					A_ :     int =							range_counting(
					    node.left			,		(node.map_left[start - 1] if start else 0)			,		node.map_left[end] - 1			,		lowerCamelCase__			,		lowerCamelCase__			,		)
					A_ :     List[str] =							range_counting(
					    node.right			,		start - (node.map_left[start - 1] if start else 0)			,		end - node.map_left[end]			,		lowerCamelCase__			,		lowerCamelCase__			,		)
					return left + right


if __name__ == "__main__":
							import doctest

							doctest.testmod()



