

import gc
import unittest

from transformers import CTRLConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
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 (
         CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
         CTRLForSequenceClassification,
         CTRLLMHeadModel,
         CTRLModel,
     )


class 			A						:







    """simple docstring"""





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





        '''simple docstring'''







        A__   				=       parent
        A__   				=       batch_size
        A__   				=       seq_length
        A__   				=       is_training
        A__   				=       use_token_type_ids
        A__   				=       use_input_mask
        A__   				=       use_labels
        A__   				=       use_mc_token_ids
        A__   				=       vocab_size
        A__   				=       hidden_size
        A__   				=       num_hidden_layers
        A__   				=       num_attention_heads
        A__   				=       intermediate_size
        A__   				=       hidden_act
        A__   				=       hidden_dropout_prob
        A__   				=       attention_probs_dropout_prob
        A__   				=       max_position_embeddings
        A__   				=       type_vocab_size
        A__   				=       type_sequence_label_size
        A__   				=       initializer_range
        A__   				=       num_labels
        A__   				=       num_choices
        A__   				=       scope
        A__   				=       self.vocab_size - 1


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





        '''simple docstring'''







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

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

        A__   				=       None
        if self.use_token_type_ids:
            A__   				=       ids_tensor([self.batch_size, self.seq_length],self.type_vocab_size		)

        A__   				=       None
        if self.use_mc_token_ids:
            A__   				=       ids_tensor([self.batch_size, self.num_choices],self.seq_length		)

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

        A__   				=       self.get_config()

        A__   				=       ids_tensor([self.num_hidden_layers, self.num_attention_heads],2		)

        return (
            config,
            input_ids,
            input_mask,
            head_mask,
            token_type_ids,
            mc_token_ids,
            sequence_labels,
            token_labels,
            choice_labels,
        )


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





        '''simple docstring'''







        return CTRLConfig(
            vocab_size=self.vocab_size,n_embd=self.hidden_size,n_layer=self.num_hidden_layers,n_head=self.num_attention_heads,n_positions=self.max_position_embeddings,pad_token_id=self.pad_token_id,)


    def 						snake_case__					(				self			:	Union[str, Any],lowercase_			:	int,lowercase_			:	Tuple,lowercase_			:	List[Any],lowercase_			:	Union[str, Any],lowercase_			:	Union[str, Any],*lowercase_			:	List[Any]		)->   Tuple:





        '''simple docstring'''







        A__   				=       CTRLModel(config=lowercase_		)
        model.to(lowercase_		)
        model.eval()

        model(lowercase_,token_type_ids=lowercase_,head_mask=lowercase_		)
        model(lowercase_,token_type_ids=lowercase_		)
        A__   				=       model(lowercase_		)
        self.parent.assertEqual(result.last_hidden_state.shape,(self.batch_size, self.seq_length, self.hidden_size)		)
        self.parent.assertEqual(len(result.past_key_values		),config.n_layer		)


    def 						snake_case__					(				self			:	Optional[int],lowercase_			:	Optional[int],lowercase_			:	Dict,lowercase_			:	Optional[int],lowercase_			:	Optional[int],lowercase_			:	Optional[int],*lowercase_			:	Dict		)->   str:





        '''simple docstring'''







        A__   				=       CTRLLMHeadModel(lowercase_		)
        model.to(lowercase_		)
        model.eval()

        A__   				=       model(lowercase_,token_type_ids=lowercase_,labels=lowercase_		)
        self.parent.assertEqual(result.loss.shape,()		)
        self.parent.assertEqual(result.logits.shape,(self.batch_size, self.seq_length, self.vocab_size)		)


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





        '''simple docstring'''







        A__   				=       self.prepare_config_and_inputs()

        (
            (
            A__
        )				,						(
            A__
        )				,						(
            A__
        )				,						(
            A__
        )				,						(
            A__
        )				,						(
            A__
        )				,						(
            A__
        )				,						(
            A__
        )				,						(
            A__
        )				,						
        )   				=       config_and_inputs

        A__   				=       {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'head_mask': head_mask}

        return config, inputs_dict


    def 						snake_case__					(				self			:	str,lowercase_			:	int,lowercase_			:	Optional[Any],lowercase_			:	Optional[Any],lowercase_			:	List[str],*lowercase_			:	List[Any]		)->   Tuple:





        '''simple docstring'''







        A__   				=       self.num_labels
        A__   				=       CTRLForSequenceClassification(lowercase_		)
        model.to(lowercase_		)
        model.eval()
        A__   				=       ids_tensor([self.batch_size],self.type_sequence_label_size		)
        A__   				=       model(lowercase_,token_type_ids=lowercase_,labels=lowercase_		)
        self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_labels)		)




@require_torch
class 			A						(							_UpperCAmelCase   ,							_UpperCAmelCase   ,							_UpperCAmelCase   ,							unittest.TestCase			):







    """simple docstring"""





    lowerCamelCase		     =							(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
    lowerCamelCase		     =							(CTRLLMHeadModel,) if is_torch_available() else ()
    lowerCamelCase		     =							(
        {
            'feature-extraction': CTRLModel,
            'text-classification': CTRLForSequenceClassification,
            'text-generation': CTRLLMHeadModel,
            'zero-shot': CTRLForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    lowerCamelCase		     =							True
    lowerCamelCase		     =							False
    lowerCamelCase		     =							False


    def 						snake_case__					(				self			:	Any,lowercase_			:	int,lowercase_			:	Any,lowercase_			:	str,lowercase_			:	Any,lowercase_			:	Optional[Any]		)->   Optional[int]:





        '''simple docstring'''







        if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
            # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
            # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny
            # config could not be created.
            return True

        return False


    def 						snake_case__					(				self			:	Dict		)->   str:





        '''simple docstring'''







        A__   				=       CTRLModelTester(self		)
        A__   				=       ConfigTester(self,config_class=lowercase_,n_embd=3_7		)


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





        '''simple docstring'''







        super().tearDown()
        # clean-up as much as possible GPU memory occupied by PyTorch
        gc.collect()
        torch.cuda.empty_cache()


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





        '''simple docstring'''







        self.config_tester.run_common_tests()


    def 						snake_case__					(				self			:	Optional[int]		)->   Optional[int]:





        '''simple docstring'''







        A__   				=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_ctrl_model(*lowercase_		)


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





        '''simple docstring'''







        A__   				=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_lm_head_model(*lowercase_		)


    @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'		)
    def 						snake_case__					(				self			:	Optional[Any]		)->   List[Any]:





        '''simple docstring'''







        pass


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





        '''simple docstring'''







        for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            A__   				=       CTRLModel.from_pretrained(lowercase_		)
            self.assertIsNotNone(lowercase_		)


    @unittest.skip('The model doesn\'t support left padding'		)  # and it's not used enough to be worth fixing :)
    def 						snake_case__					(				self			:	int		)->   Optional[Any]:





        '''simple docstring'''







        pass




@require_torch
class 			A						(							unittest.TestCase			):







    """simple docstring"""





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





        '''simple docstring'''







        super().tearDown()
        # clean-up as much as possible GPU memory occupied by PyTorch
        gc.collect()
        torch.cuda.empty_cache()


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





        '''simple docstring'''







        A__   				=       CTRLLMHeadModel.from_pretrained('ctrl'		)
        model.to(lowercase_		)
        A__   				=       torch.tensor(
            [[1_1_8_5_9, 0, 1_6_1_1, 8]],dtype=torch.long,device=lowercase_		)  # Legal the president is
        A__   				=       [
            1_1_8_5_9,
            0,
            1_6_1_1,
            8,
            5,
            1_5_0,
            2_6_4_4_9,
            2,
            1_9,
            3_4_8,
            4_6_9,
            3,
            2_5_9_5,
            4_8,
            2_0_7_4_0,
            2_4_6_5_3_3,
            2_4_6_5_3_3,
            1_9,
            3_0,
            5,
        ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

        A__   				=       model.generate(lowercase_,do_sample=lowercase_		)
        self.assertListEqual(output_ids[0].tolist(),lowercase_		)




import datasets

from .evaluate import evaluate


lowercase_			=  "\\n@inproceedings{Rajpurkar2016SQuAD10,\n  title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},\n  author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},\n  booktitle={EMNLP},\n  year={2016}\n}\n"

lowercase_			=  "\nThis metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD).\n\nStanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by\ncrowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span,\nfrom the corresponding reading passage, or the question might be unanswerable.\n"

lowercase_			=  "\nComputes SQuAD scores (F1 and EM).\nArgs:\n    predictions: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair as given in the references (see below)\n        - 'prediction_text': the text of the answer\n    references: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair (see above),\n        - 'answers': a Dict in the SQuAD dataset format\n            {\n                'text': list of possible texts for the answer, as a list of strings\n                'answer_start': list of start positions for the answer, as a list of ints\n            }\n            Note that answer_start values are not taken into account to compute the metric.\nReturns:\n    'exact_match': Exact match (the normalized answer exactly match the gold answer)\n    'f1': The F-score of predicted tokens versus the gold answer\nExamples:\n\n    >>> predictions = [{'prediction_text': '1976', 'id': '56e10a3be3433e1400422b22'}]\n    >>> references = [{'answers': {'answer_start': [97], 'text': ['1976']}, 'id': '56e10a3be3433e1400422b22'}]\n    >>> squad_metric = datasets.load_metric(\"squad\")\n    >>> results = squad_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'exact_match': 100.0, 'f1': 100.0}\n"


@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION   ,							_KWARGS_DESCRIPTION			)
class 			A						(							datasets.Metric			):







    """simple docstring"""





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





        '''simple docstring'''







        return datasets.MetricInfo(
            description=_DESCRIPTION,citation=_CITATION,inputs_description=_KWARGS_DESCRIPTION,features=datasets.Features(
                {
                    'predictions': {'id': datasets.Value('string'		), 'prediction_text': datasets.Value('string'		)},
                    'references': {
                        'id': datasets.Value('string'		),
                        'answers': datasets.features.Sequence(
                            {
                                'text': datasets.Value('string'		),
                                'answer_start': datasets.Value('int32'		),
                            }		),
                    },
                }		),codebase_urls=['https://rajpurkar.github.io/SQuAD-explorer/'],reference_urls=['https://rajpurkar.github.io/SQuAD-explorer/'],)


    def 						snake_case__					(				self			:	str,lowercase_			:	List[Any],lowercase_			:	Dict		)->   List[str]:





        '''simple docstring'''







        A__   				=       {prediction['id']: prediction['prediction_text'] for prediction in predictions}
        A__   				=       [
            {
                'paragraphs': [
                    {
                        'qas': [
                            {
                                'answers': [{'text': answer_text} for answer_text in ref['answers']['text']],
                                'id': ref['id'],
                            }
                            for ref in references
                        ]
                    }
                ]
            }
        ]
        A__   				=       evaluate(dataset=lowercase_,predictions=lowercase_		)
        return score

