
"""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  _snake_case	:






							def __init__(       self    :      Optional[int]    ,			snake_case__    :      Optional[int]    ,			snake_case__    :      int=1_3    ,			snake_case__    :      int=7    ,			snake_case__    :      int=True    ,			snake_case__    :      Optional[Any]=True    ,			snake_case__    :      Tuple=True    ,			snake_case__    :      Dict=9_9    ,			snake_case__    :      Dict=3_2    ,			snake_case__    :      Tuple=5    ,			snake_case__    :      Dict=4    ,			snake_case__    :      Dict=3_7    ,			snake_case__    :      List[Any]="gelu"    ,			snake_case__    :      Union[str, Any]=0.1    ,			snake_case__    :      Optional[Any]=0.1    ,			snake_case__    :      List[Any]=5_1_2    ,			snake_case__    :      List[Any]=1_6    ,			snake_case__    :      Dict=2    ,			snake_case__    :      Union[str, Any]=0.02    ,			snake_case__    :      Optional[int]=3    ,			snake_case__    :      int=4    ,			snake_case__    :      List[Any]=None    ,			):






												"""simple docstring"""



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






							def 				__lowercase	(       self    :      List[Any]     ):






												"""simple docstring"""



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

												__a:    Dict		     =       None
												if self.use_input_mask:
																	__a:    List[Any]		     =       random_attention_mask([self.batch_size, self.seq_length]     )

												__a:    Dict		     =       None
												__a:    List[Any]		     =       None
												__a:    Optional[int]		     =       None
												if self.use_labels:
																	__a:    Any		     =       ids_tensor([self.batch_size]    ,			self.type_sequence_label_size     )
																	__a:    Optional[Any]		     =       ids_tensor([self.batch_size, self.seq_length]    ,			self.num_labels     )
																	__a:    Dict		     =       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 				__lowercase	(       self    :      Optional[int]     ):






												"""simple docstring"""



												__a:    Union[str, Any]		     =       ids_tensor([self.batch_size, self.seq_length]    ,			self.vocab_size     )

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

												return config, input_ids, input_mask






							def 				__lowercase	(       self    :      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 				__lowercase	(       self    :      int    ,			snake_case__    :      Optional[Any]    ,			snake_case__    :      str    ,			snake_case__    :      Union[str, Any]    ,			snake_case__    :      Tuple    ,			snake_case__    :      Optional[int]    ,			snake_case__    :      List[str]     ):






												"""simple docstring"""



												__a:    Dict		     =       ErnieMModel(config=snake_case__     )
												model.to(snake_case__     )
												model.eval()
												__a:    int		     =       model(snake_case__    ,			return_dict=snake_case__     )
												self.parent.assertEqual(result.last_hidden_state.shape    ,			(self.batch_size, self.seq_length, self.hidden_size)     )






							def 				__lowercase	(       self    :      Any    ,			snake_case__    :      Dict    ,			snake_case__    :      Optional[Any]    ,			snake_case__    :      Dict    ,			snake_case__    :      List[Any]    ,			snake_case__    :      Tuple    ,			snake_case__    :      int     ):






												"""simple docstring"""



												__a:    Union[str, Any]		     =       ErnieMForQuestionAnswering(config=snake_case__     )
												model.to(snake_case__     )
												model.eval()
												__a:    Optional[Any]		     =       model(
												    snake_case__    ,			attention_mask=snake_case__    ,			start_positions=snake_case__    ,			end_positions=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 				__lowercase	(       self    :      str    ,			snake_case__    :      Optional[Any]    ,			snake_case__    :      List[Any]    ,			snake_case__    :      List[Any]    ,			snake_case__    :      Any    ,			snake_case__    :      Union[str, Any]    ,			snake_case__    :      List[Any]     ):






												"""simple docstring"""



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






												"""simple docstring"""



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






							def 				__lowercase	(       self    :      str    ,			snake_case__    :      Optional[Any]    ,			snake_case__    :      Union[str, Any]    ,			snake_case__    :      List[Any]    ,			snake_case__    :      List[str]    ,			snake_case__    :      Dict    ,			snake_case__    :      Optional[Any]     ):






												"""simple docstring"""



												__a:    List[Any]		     =       self.num_labels
												__a:    Union[str, Any]		     =       ErnieMForTokenClassification(config=snake_case__     )
												model.to(snake_case__     )
												model.eval()
												input_ids.to(snake_case__     )
												input_mask.to(snake_case__     )
												token_labels.to(snake_case__     )

												__a:    Union[str, Any]		     =       model(snake_case__    ,			attention_mask=snake_case__    ,			labels=snake_case__     )

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






							def 				__lowercase	(       self    :      Any    ,			snake_case__    :      List[Any]    ,			snake_case__    :      str    ,			snake_case__    :      str    ,			snake_case__    :      Union[str, Any]    ,			snake_case__    :      Optional[int]    ,			snake_case__    :      List[Any]     ):






												"""simple docstring"""



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






							def 				__lowercase	(       self    :      int     ):






												"""simple docstring"""



												__a:    List[str]		     =       self.prepare_config_and_inputs()
												(
												    (
												    __a
												)     ,					(
												    __a
												)     ,					(
												    __a
												)     ,					(
												    __a
												)     ,					(
												    __a
												)     ,					(
												    __a
												)     ,					
												):    int		     =       config_and_inputs
												__a:    int		     =       {"""input_ids""": input_ids, """attention_mask""": input_mask}
												return config, inputs_dict






@require_torch
class  _snake_case	(    _lowercase			,					_lowercase			,					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 				__lowercase	(       self    :      Dict    ,			snake_case__    :      str    ,			snake_case__    :      Any    ,			snake_case__    :      Dict    ,			snake_case__    :      Union[str, Any]    ,			snake_case__    :      List[Any]     ):






												"""simple docstring"""



												if pipeline_test_casse_name == "QAPipelineTests":
																	return True

												return False






							def 				__lowercase	(       self    :      List[str]     ):






												"""simple docstring"""



												__a:    Union[str, Any]		     =       ErnieMModelTester(self     )
												__a:    Any		     =       ConfigTester(self    ,			config_class=snake_case__    ,			hidden_size=3_7     )






							def 				__lowercase	(       self    :      Any     ):






												"""simple docstring"""



												self.config_tester.run_common_tests()






							def 				__lowercase	(       self    :      Optional[Any]     ):






												"""simple docstring"""



												__a:    Dict		     =       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*snake_case__     )






							def 				__lowercase	(       self    :      str     ):






												"""simple docstring"""



												__a:    Dict		     =       self.model_tester.prepare_config_and_inputs()
												for type in ["absolute", "relative_key", "relative_key_query"]:
																	__a:    int		     =       type
																	self.model_tester.create_and_check_model(*snake_case__     )






							def 				__lowercase	(       self    :      str     ):






												"""simple docstring"""



												__a:    Dict		     =       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_multiple_choice(*snake_case__     )






							def 				__lowercase	(       self    :      int     ):






												"""simple docstring"""



												__a:    Union[str, Any]		     =       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_question_answering(*snake_case__     )






							def 				__lowercase	(       self    :      Dict     ):






												"""simple docstring"""



												__a:    int		     =       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_sequence_classification(*snake_case__     )






							def 				__lowercase	(       self    :      str     ):






												"""simple docstring"""



												__a:    Tuple		     =       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_information_extraction(*snake_case__     )






							def 				__lowercase	(       self    :      Dict     ):






												"""simple docstring"""



												__a:    Any		     =       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_token_classification(*snake_case__     )






							@slow
							def 				__lowercase	(       self    :      Any     ):






												"""simple docstring"""



												for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	__a:    Tuple		     =       ErnieMModel.from_pretrained(snake_case__     )
																	self.assertIsNotNone(snake_case__     )






@require_torch
class  _snake_case	(    unittest.TestCase							):






							@slow
							def 				__lowercase	(       self    :      Tuple     ):






												"""simple docstring"""



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

												# TODO Replace vocab size
												__a:    Union[str, Any]		     =       7_6_8

												__a:    List[Any]		     =       torch.Size((1, 6, hidden_size)     )
												self.assertEqual(output.shape    ,			snake_case__     )

												__a:    str		     =       torch.tensor(
												    [[[-0.0_012, 0.1_245, -0.0_214], [-0.0_742, 0.0_244, -0.0_771], [-0.0_333, 0.1_164, -0.1_554]]]     )

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


"""simple docstring"""
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


lowercase		:    Tuple			=       {
    'configuration_codegen': ['CODEGEN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'CodeGenConfig', 'CodeGenOnnxConfig'],
    'tokenization_codegen': ['CodeGenTokenizer'],
}

try:
			if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			lowercase		:    int			=       ['CodeGenTokenizerFast']

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			lowercase		:    Optional[int]			=       [
			    'CODEGEN_PRETRAINED_MODEL_ARCHIVE_LIST',
			    'CodeGenForCausalLM',
			    'CodeGenModel',
			    'CodeGenPreTrainedModel',
			]

if TYPE_CHECKING:
			from .configuration_codegen import CODEGEN_PRETRAINED_CONFIG_ARCHIVE_MAP, CodeGenConfig, CodeGenOnnxConfig
			from .tokenization_codegen import CodeGenTokenizer

			try:
						if not is_tokenizers_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .tokenization_codegen_fast import CodeGenTokenizerFast

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_codegen import (
						    CODEGEN_PRETRAINED_MODEL_ARCHIVE_LIST,
						    CodeGenForCausalLM,
						    CodeGenModel,
						    CodeGenPreTrainedModel,
						)

else:
			import sys

			lowercase		:    Optional[Any]			=       _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
