
'''simple docstring'''

import unittest

from transformers import ElectraConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import (
         MODEL_FOR_PRETRAINING_MAPPING,
         ElectraForCausalLM,
         ElectraForMaskedLM,
         ElectraForMultipleChoice,
         ElectraForPreTraining,
         ElectraForQuestionAnswering,
         ElectraForSequenceClassification,
         ElectraForTokenClassification,
         ElectraModel,
     )
     from transformers.models.electra.modeling_electra import ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST


class 					a_					:




       def __init__(       self				,				snake_case_				,				snake_case_=1_3				,				snake_case_=7				,				snake_case_=True				,				snake_case_=True				,				snake_case_=True				,				snake_case_=True				,				snake_case_=9_9				,				snake_case_=3_2				,				snake_case_=5				,				snake_case_=4				,				snake_case_=3_7				,				snake_case_="gelu"				,				snake_case_=0.1				,				snake_case_=0.1				,				snake_case_=5_1_2				,				snake_case_=1_6				,				snake_case_=2				,				snake_case_=0.02				,				snake_case_=3				,				snake_case_=4				,				snake_case_=None				,				):
         _lowerCAmelCase    :					Dict							=   parent
         _lowerCAmelCase    :					List[str]							=   batch_size
         _lowerCAmelCase    :					Dict							=   seq_length
         _lowerCAmelCase    :					List[str]							=   is_training
         _lowerCAmelCase    :					List[Any]							=   use_input_mask
         _lowerCAmelCase    :					Optional[Any]							=   use_token_type_ids
         _lowerCAmelCase    :					Optional[int]							=   use_labels
         _lowerCAmelCase    :					List[str]							=   vocab_size
         _lowerCAmelCase    :					Any							=   hidden_size
         _lowerCAmelCase    :					List[Any]							=   num_hidden_layers
         _lowerCAmelCase    :					Any							=   num_attention_heads
         _lowerCAmelCase    :					Dict							=   intermediate_size
         _lowerCAmelCase    :					Optional[Any]							=   hidden_act
         _lowerCAmelCase    :					Optional[int]							=   hidden_dropout_prob
         _lowerCAmelCase    :					List[Any]							=   attention_probs_dropout_prob
         _lowerCAmelCase    :					Optional[Any]							=   max_position_embeddings
         _lowerCAmelCase    :					Optional[Any]							=   type_vocab_size
         _lowerCAmelCase    :					List[Any]							=   type_sequence_label_size
         _lowerCAmelCase    :					Tuple							=   initializer_range
         _lowerCAmelCase    :					List[str]							=   num_labels
         _lowerCAmelCase    :					Optional[Any]							=   num_choices
         _lowerCAmelCase    :					List[str]							=   scope




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					List[str]							=   ids_tensor([self.batch_size, self.seq_length]				,				self.vocab_size					)

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

         _lowerCAmelCase    :					List[Any]							=   None
         if self.use_token_type_ids:
           _lowerCAmelCase    :					Optional[Any]							=   ids_tensor([self.batch_size, self.seq_length]				,				self.type_vocab_size					)

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

         _lowerCAmelCase    :					Any							=   self.get_config()

         return (
             config,
             input_ids,
             token_type_ids,
             input_mask,
             sequence_labels,
             token_labels,
             choice_labels,
             fake_token_labels,
         )




       def 							__UpperCamelCase    (       self					):
         return ElectraConfig(
             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				,				is_decoder=snake_case_				,				initializer_range=self.initializer_range				,				)




       def 							__UpperCamelCase    (       self					):
         (
             (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   
         )    :					int							=   self.prepare_config_and_inputs()

         _lowerCAmelCase    :					str							=   True
         _lowerCAmelCase    :					List[str]							=   floats_tensor([self.batch_size, self.seq_length, self.hidden_size]					)
         _lowerCAmelCase    :					Optional[Any]							=   ids_tensor([self.batch_size, self.seq_length]				,				vocab_size=2					)

         return (
             config,
             input_ids,
             token_type_ids,
             input_mask,
             sequence_labels,
             token_labels,
             choice_labels,
             encoder_hidden_states,
             encoder_attention_mask,
         )




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				):
         _lowerCAmelCase    :					List[Any]							=   ElectraModel(config=snake_case_					)
         model.to(snake_case_					)
         model.eval()
         _lowerCAmelCase    :					List[Any]							=   model(snake_case_				,				attention_mask=snake_case_				,				token_type_ids=snake_case_					)
         _lowerCAmelCase    :					Optional[int]							=   model(snake_case_				,				token_type_ids=snake_case_					)
         _lowerCAmelCase    :					List[str]							=   model(snake_case_					)
         self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.seq_length, self.hidden_size)					)




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				):
         _lowerCAmelCase    :					Optional[int]							=   True
         _lowerCAmelCase    :					List[str]							=   ElectraModel(snake_case_					)
         model.to(snake_case_					)
         model.eval()
         _lowerCAmelCase    :					Tuple							=   model(
             snake_case_				,				attention_mask=snake_case_				,				token_type_ids=snake_case_				,				encoder_hidden_states=snake_case_				,				encoder_attention_mask=snake_case_				,				)
         _lowerCAmelCase    :					Dict							=   model(
             snake_case_				,				attention_mask=snake_case_				,				token_type_ids=snake_case_				,				encoder_hidden_states=snake_case_				,				)
         _lowerCAmelCase    :					Any							=   model(snake_case_				,				attention_mask=snake_case_				,				token_type_ids=snake_case_					)
         self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.seq_length, self.hidden_size)					)




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				):
         _lowerCAmelCase    :					Tuple							=   ElectraForMaskedLM(config=snake_case_					)
         model.to(snake_case_					)
         model.eval()
         _lowerCAmelCase    :					Optional[int]							=   model(snake_case_				,				attention_mask=snake_case_				,				token_type_ids=snake_case_				,				labels=snake_case_					)
         self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.seq_length, self.vocab_size)					)




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				):
         _lowerCAmelCase    :					Union[str, Any]							=   ElectraForCausalLM(config=snake_case_					)
         model.to(snake_case_					)
         model.eval()
         _lowerCAmelCase    :					Any							=   model(snake_case_				,				attention_mask=snake_case_				,				token_type_ids=snake_case_				,				labels=snake_case_					)
         self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.seq_length, self.vocab_size)					)




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				):
         _lowerCAmelCase    :					Dict							=   self.num_labels
         _lowerCAmelCase    :					int							=   ElectraForTokenClassification(config=snake_case_					)
         model.to(snake_case_					)
         model.eval()
         _lowerCAmelCase    :					Optional[Any]							=   model(snake_case_				,				attention_mask=snake_case_				,				token_type_ids=snake_case_				,				labels=snake_case_					)
         self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.seq_length, self.num_labels)					)




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				):
         _lowerCAmelCase    :					Tuple							=   self.num_labels
         _lowerCAmelCase    :					Optional[int]							=   ElectraForPreTraining(config=snake_case_					)
         model.to(snake_case_					)
         model.eval()
         _lowerCAmelCase    :					str							=   model(snake_case_				,				attention_mask=snake_case_				,				token_type_ids=snake_case_				,				labels=snake_case_					)
         self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.seq_length)					)




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				):
         _lowerCAmelCase    :					List[str]							=   self.num_labels
         _lowerCAmelCase    :					Union[str, Any]							=   ElectraForSequenceClassification(snake_case_					)
         model.to(snake_case_					)
         model.eval()
         _lowerCAmelCase    :					Dict							=   model(snake_case_				,				attention_mask=snake_case_				,				token_type_ids=snake_case_				,				labels=snake_case_					)
         self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_labels)					)




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				):
         _lowerCAmelCase    :					List[str]							=   ElectraForQuestionAnswering(config=snake_case_					)
         model.to(snake_case_					)
         model.eval()
         _lowerCAmelCase    :					Any							=   model(
             snake_case_				,				attention_mask=snake_case_				,				token_type_ids=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 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				):
         _lowerCAmelCase    :					Optional[Any]							=   self.num_choices
         _lowerCAmelCase    :					List[Any]							=   ElectraForMultipleChoice(config=snake_case_					)
         model.to(snake_case_					)
         model.eval()
         _lowerCAmelCase    :					Optional[int]							=   input_ids.unsqueeze(1					).expand(-1				,				self.num_choices				,				-1					).contiguous()
         _lowerCAmelCase    :					Any							=   token_type_ids.unsqueeze(1					).expand(-1				,				self.num_choices				,				-1					).contiguous()
         _lowerCAmelCase    :					Optional[Any]							=   input_mask.unsqueeze(1					).expand(-1				,				self.num_choices				,				-1					).contiguous()
         _lowerCAmelCase    :					Dict							=   model(
             snake_case_				,				attention_mask=snake_case_				,				token_type_ids=snake_case_				,				labels=snake_case_				,				)
         self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_choices)					)







       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					str							=   self.prepare_config_and_inputs()
         (
             (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   
         )    :					Dict							=   config_and_inputs
         _lowerCAmelCase    :					Dict							=   {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
         return config, inputs_dict



@require_torch
class 					a_					(_a							,     _a							,     unittest.TestCase      ):
       __lowerCAmelCase      :			Union[str, Any]      = (
           (
               ElectraModel,
               ElectraForPreTraining,
               ElectraForMaskedLM,
               ElectraForCausalLM,
               ElectraForMultipleChoice,
               ElectraForTokenClassification,
               ElectraForSequenceClassification,
               ElectraForQuestionAnswering,
           )
           if is_torch_available()
           else ()
       )
       __lowerCAmelCase      :			Dict      = (
           {
               """feature-extraction""": ElectraModel,
               """fill-mask""": ElectraForMaskedLM,
               """question-answering""": ElectraForQuestionAnswering,
               """text-classification""": ElectraForSequenceClassification,
               """text-generation""": ElectraForCausalLM,
               """token-classification""": ElectraForTokenClassification,
               """zero-shot""": ElectraForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       __lowerCAmelCase      :			Union[str, Any]      = True




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_				,				snake_case_=False					):
         _lowerCAmelCase    :					Tuple							=   super()._prepare_for_class(snake_case_				,				snake_case_				,				return_labels=snake_case_					)

         if return_labels:
           if model_class in get_values(snake_case_					):
             _lowerCAmelCase    :					List[Any]							=   torch.zeros(
                 (self.model_tester.batch_size, self.model_tester.seq_length)				,				dtype=torch.long				,				device=snake_case_					)
         return inputs_dict




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Tuple							=   ElectraModelTester(self					)
         _lowerCAmelCase    :					List[str]							=   ConfigTester(self				,				config_class=snake_case_				,				hidden_size=3_7					)




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




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Dict							=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_electra_model(*snake_case_					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					List[Any]							=   self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_electra_model_as_decoder(*snake_case_					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Dict							=   self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
           _lowerCAmelCase    :					str							=   type
           self.model_tester.create_and_check_electra_model(*snake_case_					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Union[str, Any]							=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_electra_for_masked_lm(*snake_case_					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Dict							=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_electra_for_token_classification(*snake_case_					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					List[str]							=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_electra_for_pretraining(*snake_case_					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					int							=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_electra_for_sequence_classification(*snake_case_					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Optional[Any]							=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_electra_for_question_answering(*snake_case_					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					List[Any]							=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_electra_for_multiple_choice(*snake_case_					)




       @slow
       def 							__UpperCamelCase    (       self					):
         for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _lowerCAmelCase    :					str							=   ElectraModel.from_pretrained(snake_case_					)
           self.assertIsNotNone(snake_case_					)







       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Any							=   self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_electra_for_causal_lm(*snake_case_					)



@require_torch
class 					a_					(unittest.TestCase      ):




       @slow
       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					List[Any]							=   ElectraModel.from_pretrained("""google/electra-small-discriminator"""					)
         _lowerCAmelCase    :					Union[str, Any]							=   torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]					)
         _lowerCAmelCase    :					Dict							=   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]					)
         _lowerCAmelCase    :					Optional[Any]							=   model(snake_case_				,				attention_mask=snake_case_					)[0]
         _lowerCAmelCase    :					List[str]							=   torch.Size((1, 1_1, 2_5_6)					)
         self.assertEqual(output.shape				,				snake_case_					)
         _lowerCAmelCase    :					Optional[int]							=   torch.tensor(
             [[[0.4471, 0.6821, -0.3265], [0.4627, 0.5255, -0.3668], [0.4532, 0.3313, -0.4344]]]					)

         self.assertTrue(torch.allclose(output[:, 1:4, 1:4]				,				snake_case_				,				atol=1E-4					)					)




'''simple docstring'''

import platform
from argparse import ArgumentParser

import huggingface_hub
import pandas
import pyarrow

from datasets import __version__ as version
from datasets.commands import BaseDatasetsCLICommand


def 		_UpperCAmelCase			(					_lowerCamelCase  :    List[Any]						)      ->  Optional[Any]:
  return EnvironmentCommand()



class 					a_					(_a      ):




       @staticmethod
       def 							__UpperCamelCase    (       snake_case_					):
         _lowerCAmelCase    :					Optional[Any]							=   parser.add_parser("""env"""				,				help="""Print relevant system environment info."""					)
         download_parser.set_defaults(func=snake_case_					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Optional[Any]							=   {
             """`datasets` version""": version,
             """Platform""": platform.platform(),
             """Python version""": platform.python_version(),
             """Huggingface_hub version""": huggingface_hub.__version__,
             """PyArrow version""": pyarrow.__version__,
             """Pandas version""": pandas.__version__,
         }

         print("""\nCopy-and-paste the text below in your GitHub issue.\n"""					)
         print(self.format_dict(snake_case_					)					)

         return info







       @staticmethod
       def 							__UpperCamelCase    (       snake_case_					):
         return "\n".join([f'- {prop}: {val}' for prop, val in d.items()]					) + "\n"


