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			:




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



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				parent
												SCREAMING_SNAKE_CASE				=				batch_size
												SCREAMING_SNAKE_CASE				=				seq_length
												SCREAMING_SNAKE_CASE				=				is_training
												SCREAMING_SNAKE_CASE				=				use_input_mask
												SCREAMING_SNAKE_CASE				=				use_labels
												SCREAMING_SNAKE_CASE				=				vocab_size
												SCREAMING_SNAKE_CASE				=				hidden_size
												SCREAMING_SNAKE_CASE				=				num_hidden_layers
												SCREAMING_SNAKE_CASE				=				num_attention_heads
												SCREAMING_SNAKE_CASE				=				intermediate_size
												SCREAMING_SNAKE_CASE				=				hidden_act
												SCREAMING_SNAKE_CASE				=				hidden_dropout_prob
												SCREAMING_SNAKE_CASE				=				attention_probs_dropout_prob
												SCREAMING_SNAKE_CASE				=				max_position_embeddings
												SCREAMING_SNAKE_CASE				=				type_vocab_size
												SCREAMING_SNAKE_CASE				=				type_sequence_label_size
												SCREAMING_SNAKE_CASE				=				initializer_range
												SCREAMING_SNAKE_CASE				=				num_labels
												SCREAMING_SNAKE_CASE				=				num_choices
												SCREAMING_SNAKE_CASE				=				scope




							def 		__snake_case(  self      :    int					)    -> Dict:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				ids_tensor([self.batch_size, self.seq_length]		,							self.vocab_size					)

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

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

												SCREAMING_SNAKE_CASE				=				self.get_config()

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




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



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				ids_tensor([self.batch_size, self.seq_length]		,							self.vocab_size					)

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

												return config, input_ids, input_mask




							def 		__snake_case(  self      :    str					)    -> 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 		__snake_case(  self      :    int		,							_UpperCamelCase      :    Dict		,							_UpperCamelCase      :    Any		,							_UpperCamelCase      :    List[Any]		,							_UpperCamelCase      :    List[Any]		,							_UpperCamelCase      :    Optional[Any]		,							_UpperCamelCase      :    List[str]					)    -> Optional[Any]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				ErnieMModel(config=_UpperCamelCase					)
												model.to(_UpperCamelCase					)
												model.eval()
												SCREAMING_SNAKE_CASE				=				model(_UpperCamelCase		,							return_dict=_UpperCamelCase					)
												self.parent.assertEqual(result.last_hidden_state.shape		,							(self.batch_size, self.seq_length, self.hidden_size)					)




							def 		__snake_case(  self      :    Tuple		,							_UpperCamelCase      :    Optional[Any]		,							_UpperCamelCase      :    str		,							_UpperCamelCase      :    List[Any]		,							_UpperCamelCase      :    List[Any]		,							_UpperCamelCase      :    Union[str, Any]		,							_UpperCamelCase      :    Dict					)    -> Union[str, Any]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				ErnieMForQuestionAnswering(config=_UpperCamelCase					)
												model.to(_UpperCamelCase					)
												model.eval()
												SCREAMING_SNAKE_CASE				=				model(
												    _UpperCamelCase		,							attention_mask=_UpperCamelCase		,							start_positions=_UpperCamelCase		,							end_positions=_UpperCamelCase		,							)
												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 		__snake_case(  self      :    int		,							_UpperCamelCase      :    Optional[Any]		,							_UpperCamelCase      :    List[str]		,							_UpperCamelCase      :    Tuple		,							_UpperCamelCase      :    int		,							_UpperCamelCase      :    List[Any]		,							_UpperCamelCase      :    Tuple					)    -> Union[str, Any]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				ErnieMForInformationExtraction(config=_UpperCamelCase					)
												model.to(_UpperCamelCase					)
												model.eval()
												SCREAMING_SNAKE_CASE				=				torch.ones_like(_UpperCamelCase		,							dtype=torch.floataa					)
												SCREAMING_SNAKE_CASE				=				model(
												    _UpperCamelCase		,							attention_mask=_UpperCamelCase		,							start_positions=_UpperCamelCase		,							end_positions=_UpperCamelCase		,							)
												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 		__snake_case(  self      :    List[Any]		,							_UpperCamelCase      :    int		,							_UpperCamelCase      :    Dict		,							_UpperCamelCase      :    Dict		,							_UpperCamelCase      :    str		,							_UpperCamelCase      :    Optional[int]		,							_UpperCamelCase      :    Any					)    -> Dict:



												'''simple docstring'''

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




							def 		__snake_case(  self      :    int		,							_UpperCamelCase      :    Optional[Any]		,							_UpperCamelCase      :    int		,							_UpperCamelCase      :    List[str]		,							_UpperCamelCase      :    Optional[int]		,							_UpperCamelCase      :    Dict		,							_UpperCamelCase      :    Optional[Any]					)    -> Tuple:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				self.num_labels
												SCREAMING_SNAKE_CASE				=				ErnieMForTokenClassification(config=_UpperCamelCase					)
												model.to(_UpperCamelCase					)
												model.eval()
												input_ids.to(_UpperCamelCase					)
												input_mask.to(_UpperCamelCase					)
												token_labels.to(_UpperCamelCase					)

												SCREAMING_SNAKE_CASE				=				model(_UpperCamelCase		,							attention_mask=_UpperCamelCase		,							labels=_UpperCamelCase					)

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




							def 		__snake_case(  self      :    List[str]		,							_UpperCamelCase      :    Tuple		,							_UpperCamelCase      :    Optional[int]		,							_UpperCamelCase      :    Optional[int]		,							_UpperCamelCase      :    List[Any]		,							_UpperCamelCase      :    Optional[Any]		,							_UpperCamelCase      :    List[str]					)    -> Tuple:



												'''simple docstring'''

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





							def 		__snake_case(  self      :    Any					)    -> int:



												'''simple docstring'''

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



@require_torch
class   lowercase			(   a ,				a ,				unittest.TestCase   ):
							lowercase__  :				List[Any]									=							(
							    (
							        ErnieMModel,
							        ErnieMForMultipleChoice,
							        ErnieMForQuestionAnswering,
							        ErnieMForSequenceClassification,
							        ErnieMForTokenClassification,
							    )
							    if is_torch_available()
							    else ()
							)
							lowercase__  :				Any									=							()
							lowercase__  :				List[str]									=							(
							    {
							        """feature-extraction""": ErnieMModel,
							        """question-answering""": ErnieMForQuestionAnswering,
							        """text-classification""": ErnieMForSequenceClassification,
							        """token-classification""": ErnieMForTokenClassification,
							        """zero-shot""": ErnieMForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							lowercase__  :				Tuple									=							False




							def 		__snake_case(  self      :    str		,							_UpperCamelCase      :    Optional[Any]		,							_UpperCamelCase      :    Tuple		,							_UpperCamelCase      :    List[str]		,							_UpperCamelCase      :    Tuple		,							_UpperCamelCase      :    List[Any]					)    -> str:



												'''simple docstring'''

												if pipeline_test_casse_name == "QAPipelineTests":
																	return True

												return False




							def 		__snake_case(  self      :    Dict					)    -> Dict:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				ErnieMModelTester(self					)
												SCREAMING_SNAKE_CASE				=				ConfigTester(self		,							config_class=_UpperCamelCase		,							hidden_size=37					)




							def 		__snake_case(  self      :    Dict					)    -> List[Any]:



												'''simple docstring'''

												self.config_tester.run_common_tests()




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



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*_UpperCamelCase					)




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



												'''simple docstring'''

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




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



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_multiple_choice(*_UpperCamelCase					)




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



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_question_answering(*_UpperCamelCase					)




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



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_sequence_classification(*_UpperCamelCase					)




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



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_information_extraction(*_UpperCamelCase					)




							def 		__snake_case(  self      :    int					)    -> Any:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_token_classification(*_UpperCamelCase					)





							@slow
							def 		__snake_case(  self      :    Tuple					)    -> Dict:



												'''simple docstring'''

												for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	SCREAMING_SNAKE_CASE				=				ErnieMModel.from_pretrained(_UpperCamelCase					)
																	self.assertIsNotNone(_UpperCamelCase					)



@require_torch
class   lowercase			(   unittest.TestCase   ):




							@slow
							def 		__snake_case(  self      :    str					)    -> Optional[int]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				ErnieMModel.from_pretrained("susnato/ernie-m-base_pytorch"					)
												model.eval()
												SCREAMING_SNAKE_CASE				=				torch.tensor([[0, 1, 2, 3, 4, 5]]					)
												SCREAMING_SNAKE_CASE				=				model(_UpperCamelCase					)[0]

												# TODO Replace vocab size
												SCREAMING_SNAKE_CASE				=				768

												SCREAMING_SNAKE_CASE				=				torch.Size((1, 6, hidden_size)					)
												self.assertEqual(output.shape		,							_UpperCamelCase					)

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





import json
import os
from dataclasses import dataclass

import numpy as np
import pyarrow as pa

import datasets
from utils import get_duration


_lowerCamelCase    :      int	 =						10_00_00_00_00_00
_lowerCamelCase    :      Union[str, Any]	 =						1_00_00

_lowerCamelCase ,  _lowerCamelCase    :      List[str]	 =						os.path.split(__file__)
_lowerCamelCase    :      Any	 =						os.path.join(RESULTS_BASEPATH, '''results''', RESULTS_FILENAME.replace('''.py''', '''.json'''))





def 		__lowerCamelCase							(UpperCAmelCase__   :							int					,       UpperCAmelCase__   :							int     ):
					SCREAMING_SNAKE_CASE				=				pa.Table.from_pydict({"col": [0] * chunk_size}     )
					SCREAMING_SNAKE_CASE				=				pa.concat_tables([table] * (num_examples // chunk_size)     )
					return datasets.Dataset(UpperCAmelCase__					,       fingerprint="table_100B"     )


@dataclass
class   lowercase			:
							lowercase__  :				int
							lowercase__  :				int
							lowercase__  :				int
							lowercase__  :				int




							def 		__snake_case(  self      :    Tuple					)    -> Dict:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				np.random.default_rng(self.seed					)
												SCREAMING_SNAKE_CASE				=				rng.integers(low=self.low		,							high=self.high		,							size=self.size					).tolist()




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



												'''simple docstring'''

												return iter(self._sampled_values					)





							def __len__(  self      :    Any					)    -> int:



												'''simple docstring'''

												return self.size





@get_duration
def 		__lowerCamelCase							(UpperCAmelCase__   :							datasets.Dataset     ):
					SCREAMING_SNAKE_CASE				=				dataset[0]





@get_duration
def 		__lowerCamelCase							(UpperCAmelCase__   :							datasets.Dataset     ):
					SCREAMING_SNAKE_CASE				=				dataset[-1]





@get_duration
def 		__lowerCamelCase							(UpperCAmelCase__   :							datasets.Dataset     ):
					SCREAMING_SNAKE_CASE				=				dataset[range(len(UpperCAmelCase__     ) // 2					,       len(UpperCAmelCase__     ) // 2 + 1_0_2_4     )]





@get_duration
def 		__lowerCamelCase							(UpperCAmelCase__   :							datasets.Dataset     ):
					SCREAMING_SNAKE_CASE				=				dataset[RandIter(0					,       len(UpperCAmelCase__     )					,       1_0_2_4					,       seed=4_2     )]





def 		__lowerCamelCase							():
					SCREAMING_SNAKE_CASE				=				{"num examples": SPEED_TEST_N_EXAMPLES}
					SCREAMING_SNAKE_CASE				=				(get_first_row, get_last_row, get_batch_of_aaaa_rows, get_batch_of_aaaa_random_rows)
					print("generating dataset"     )
					SCREAMING_SNAKE_CASE				=				generate_100B_dataset(num_examples=UpperCAmelCase__					,       chunk_size=UpperCAmelCase__     )
					print("Functions"     )
					for func in functions:
										print(func.__name__     )
										SCREAMING_SNAKE_CASE				=				func(UpperCAmelCase__     )

					with open(UpperCAmelCase__					,       "wb"     ) as f:
										f.write(json.dumps(UpperCAmelCase__     ).encode("utf-8"     )     )


if __name__ == "__main__":  # useful to run the profiler
							benchmark_table_100B()




