



from copy import deepcopy
from unittest.case import TestCase

import pytest

from datasets.arrow_dataset import Dataset
from datasets.features import Audio, ClassLabel, Features, Image, Sequence, Value
from datasets.info import DatasetInfo
from datasets.tasks import (
    AudioClassification,
    AutomaticSpeechRecognition,
    ImageClassification,
    LanguageModeling,
    QuestionAnsweringExtractive,
    Summarization,
    TextClassification,
    task_template_from_dict,
)
from datasets.utils.py_utils import asdict


A__  :  Tuple       = {
    '''id''': '''5733be284776f41900661182''',
    '''title''': '''University_of_Notre_Dame''',
    '''context''': '''Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.''',
    '''question''': '''To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?''',
    '''answers''': {'''text''': ['''Saint Bernadette Soubirous'''], '''answer_start''': [515]},
}


@pytest.mark.parametrize(
    '''task_cls'''					,[
        AudioClassification,
        AutomaticSpeechRecognition,
        ImageClassification,
        LanguageModeling,
        QuestionAnsweringExtractive,
        Summarization,
        TextClassification,
    ]					,)
def       UpperCamelCase(  __UpperCamelCase							:		int			):
		lowerCAmelCase_      :					Any     =			task_cls()
		lowerCAmelCase_      :					Optional[Any]     =			asdict(__UpperCamelCase			)
		lowerCAmelCase_      :					Union[str, Any]     =			task_template_from_dict(__UpperCamelCase			)
		assert task == reloaded



class     __snake_case						:




							def        UpperCAmelCase__      (	self					:	Optional[Any]):
									lowerCAmelCase_      :					Dict     =			LanguageModeling(text_column='''input_text''')
									assert {"input_text": "text"} == task.column_mapping







							def        UpperCAmelCase__      (	self					:	List[str]):
									lowerCAmelCase_      :					List[Any]     =			Features({'''text''': Value('''string''')})
									lowerCAmelCase_      :					Optional[Any]     =			{'''text_column''': '''input_text'''}
									lowerCAmelCase_      :					str     =			LanguageModeling.from_dict(A_)
									assert "language-modeling" == task.task
									assert input_schema == task.input_schema





class     __snake_case						(					UpperCamelCase_						):




							def        UpperCAmelCase__      (	self					:	Union[str, Any]):
									lowerCAmelCase_      :					Optional[Any]     =			sorted(['''pos''', '''neg'''])




							def        UpperCAmelCase__      (	self					:	List[str]):
									lowerCAmelCase_      :					List[Any]     =			TextClassification(text_column='''input_text'''    ,    label_column='''input_label''')
									self.assertDictEqual({'''input_text''': '''text''', '''input_label''': '''labels'''}    ,    task.column_mapping)




							def        UpperCAmelCase__      (	self					:	Union[str, Any]):
									lowerCAmelCase_      :					Dict     =			Features({'''text''': Value('''string''')})
									# Labels are cast to tuple during `TextClassification.__post_init__`, so we do the same here
									lowerCAmelCase_      :					Union[str, Any]     =			Features({'''labels''': ClassLabel})
									lowerCAmelCase_      :					Optional[int]     =			{'''text_column''': '''input_text''', '''label_column''': '''input_labels'''}
									lowerCAmelCase_      :					Tuple     =			TextClassification.from_dict(A_)
									self.assertEqual('''text-classification'''    ,    task.task)
									self.assertEqual(A_    ,    task.input_schema)
									self.assertEqual(A_    ,    task.label_schema)







							def        UpperCAmelCase__      (	self					:	int):
									lowerCAmelCase_      :					Any     =			TextClassification(text_column='''input_text'''    ,    label_column='''input_label''')
									self.assertEqual(task.label_schema['''labels''']    ,    A_)
									lowerCAmelCase_      :					str     =			task.align_with_features(Features({'''input_label''': ClassLabel(names=self.labels)}))
									self.assertEqual(task.label_schema['''labels''']    ,    ClassLabel(names=self.labels))





class     __snake_case						(					UpperCamelCase_						):




							def        UpperCAmelCase__      (	self					:	Union[str, Any]):
									lowerCAmelCase_      :					Any     =			QuestionAnsweringExtractive(
									    context_column='''input_context'''    ,    question_column='''input_question'''    ,    answers_column='''input_answers''')
									self.assertDictEqual(
									    {'''input_context''': '''context''', '''input_question''': '''question''', '''input_answers''': '''answers'''}    ,    task.column_mapping)







							def        UpperCAmelCase__      (	self					:	int):
									lowerCAmelCase_      :					Optional[int]     =			Features({'''question''': Value('''string'''), '''context''': Value('''string''')})
									lowerCAmelCase_      :					Tuple     =			Features(
									    {
									        '''answers''': Sequence(
									            {
									                '''text''': Value('''string'''),
									                '''answer_start''': Value('''int32'''),
									            })
									    })
									lowerCAmelCase_      :					Dict     =			{
									    '''context_column''': '''input_input_context''',
									    '''question_column''': '''input_question''',
									    '''answers_column''': '''input_answers''',
									}
									lowerCAmelCase_      :					Dict     =			QuestionAnsweringExtractive.from_dict(A_)
									self.assertEqual('''question-answering-extractive'''    ,    task.task)
									self.assertEqual(A_    ,    task.input_schema)
									self.assertEqual(A_    ,    task.label_schema)





class     __snake_case						(					UpperCamelCase_						):




							def        UpperCAmelCase__      (	self					:	Optional[int]):
									lowerCAmelCase_      :					str     =			Summarization(text_column='''input_text'''    ,    summary_column='''input_summary''')
									self.assertDictEqual({'''input_text''': '''text''', '''input_summary''': '''summary'''}    ,    task.column_mapping)







							def        UpperCAmelCase__      (	self					:	Any):
									lowerCAmelCase_      :					Any     =			Features({'''text''': Value('''string''')})
									lowerCAmelCase_      :					Optional[Any]     =			Features({'''summary''': Value('''string''')})
									lowerCAmelCase_      :					Dict     =			{'''text_column''': '''input_text''', '''summary_column''': '''input_summary'''}
									lowerCAmelCase_      :					Union[str, Any]     =			Summarization.from_dict(A_)
									self.assertEqual('''summarization'''    ,    task.task)
									self.assertEqual(A_    ,    task.input_schema)
									self.assertEqual(A_    ,    task.label_schema)





class     __snake_case						(					UpperCamelCase_						):




							def        UpperCAmelCase__      (	self					:	Optional[Any]):
									lowerCAmelCase_      :					str     =			AutomaticSpeechRecognition(audio_column='''input_audio'''    ,    transcription_column='''input_transcription''')
									self.assertDictEqual({'''input_audio''': '''audio''', '''input_transcription''': '''transcription'''}    ,    task.column_mapping)







							def        UpperCAmelCase__      (	self					:	Dict):
									lowerCAmelCase_      :					Optional[Any]     =			Features({'''audio''': Audio()})
									lowerCAmelCase_      :					Tuple     =			Features({'''transcription''': Value('''string''')})
									lowerCAmelCase_      :					Optional[int]     =			{
									    '''audio_column''': '''input_audio''',
									    '''transcription_column''': '''input_transcription''',
									}
									lowerCAmelCase_      :					Optional[Any]     =			AutomaticSpeechRecognition.from_dict(A_)
									self.assertEqual('''automatic-speech-recognition'''    ,    task.task)
									self.assertEqual(A_    ,    task.input_schema)
									self.assertEqual(A_    ,    task.label_schema)





class     __snake_case						(					UpperCamelCase_						):




							def        UpperCAmelCase__      (	self					:	List[str]):
									lowerCAmelCase_      :					int     =			sorted(['''pos''', '''neg'''])




							def        UpperCAmelCase__      (	self					:	int):
									lowerCAmelCase_      :					List[Any]     =			AudioClassification(audio_column='''input_audio'''    ,    label_column='''input_label''')
									self.assertDictEqual({'''input_audio''': '''audio''', '''input_label''': '''labels'''}    ,    task.column_mapping)




							def        UpperCAmelCase__      (	self					:	Optional[Any]):
									lowerCAmelCase_      :					Dict     =			Features({'''audio''': Audio()})
									lowerCAmelCase_      :					Dict     =			Features({'''labels''': ClassLabel})
									lowerCAmelCase_      :					Union[str, Any]     =			{
									    '''audio_column''': '''input_image''',
									    '''label_column''': '''input_label''',
									}
									lowerCAmelCase_      :					List[Any]     =			AudioClassification.from_dict(A_)
									self.assertEqual('''audio-classification'''    ,    task.task)
									self.assertEqual(A_    ,    task.input_schema)
									self.assertEqual(A_    ,    task.label_schema)







							def        UpperCAmelCase__      (	self					:	Dict):
									lowerCAmelCase_      :					int     =			AudioClassification(audio_column='''input_audio'''    ,    label_column='''input_label''')
									self.assertEqual(task.label_schema['''labels''']    ,    A_)
									lowerCAmelCase_      :					Optional[Any]     =			task.align_with_features(Features({'''input_label''': ClassLabel(names=self.labels)}))
									self.assertEqual(task.label_schema['''labels''']    ,    ClassLabel(names=self.labels))





class     __snake_case						(					UpperCamelCase_						):




							def        UpperCAmelCase__      (	self					:	Tuple):
									lowerCAmelCase_      :					Optional[int]     =			sorted(['''pos''', '''neg'''])




							def        UpperCAmelCase__      (	self					:	Optional[Any]):
									lowerCAmelCase_      :					int     =			ImageClassification(image_column='''input_image'''    ,    label_column='''input_label''')
									self.assertDictEqual({'''input_image''': '''image''', '''input_label''': '''labels'''}    ,    task.column_mapping)




							def        UpperCAmelCase__      (	self					:	int):
									lowerCAmelCase_      :					Tuple     =			Features({'''image''': Image()})
									lowerCAmelCase_      :					str     =			Features({'''labels''': ClassLabel})
									lowerCAmelCase_      :					str     =			{
									    '''image_column''': '''input_image''',
									    '''label_column''': '''input_label''',
									}
									lowerCAmelCase_      :					Any     =			ImageClassification.from_dict(A_)
									self.assertEqual('''image-classification'''    ,    task.task)
									self.assertEqual(A_    ,    task.input_schema)
									self.assertEqual(A_    ,    task.label_schema)







							def        UpperCAmelCase__      (	self					:	Any):
									lowerCAmelCase_      :					Dict     =			ImageClassification(image_column='''input_image'''    ,    label_column='''input_label''')
									self.assertEqual(task.label_schema['''labels''']    ,    A_)
									lowerCAmelCase_      :					int     =			task.align_with_features(Features({'''input_label''': ClassLabel(names=self.labels)}))
									self.assertEqual(task.label_schema['''labels''']    ,    ClassLabel(names=self.labels))





class     __snake_case						(					UpperCamelCase_						):




							def        UpperCAmelCase__      (	self					:	List[Any]):
									lowerCAmelCase_      :					Any     =			DatasetInfo(task_templates=QuestionAnsweringExtractive())
									lowerCAmelCase_      :					Optional[int]     =			Dataset.from_dict({k: [v] for k, v in SAMPLE_QUESTION_ANSWERING_EXTRACTIVE.items()}    ,    info=A_)
									assert isinstance(dataset.info.task_templates    ,    A_)
									assert len(dataset.info.task_templates) == 1

									def keep_task(A_					:	Optional[int]):
											return x

									def dont_keep_task(A_					:	Tuple):
											lowerCAmelCase_      :					Optional[int]     =			deepcopy(A_)
											lowerCAmelCase_      :					Tuple     =			0
											return out

									lowerCAmelCase_      :					List[str]     =			dataset.map(A_)
									assert mapped_dataset.info.task_templates == dataset.info.task_templates
									# reload from cache
									lowerCAmelCase_      :					Any     =			dataset.map(A_)
									assert mapped_dataset.info.task_templates == dataset.info.task_templates

									lowerCAmelCase_      :					Tuple     =			dataset.map(A_)
									assert mapped_dataset.info.task_templates == []
									# reload from cache
									lowerCAmelCase_      :					List[str]     =			dataset.map(A_)
									assert mapped_dataset.info.task_templates == []







							def        UpperCAmelCase__      (	self					:	Dict):
									lowerCAmelCase_      :					Union[str, Any]     =			Features({'''text''': Value('''string'''), '''label''': ClassLabel(names=('''pos''', '''neg'''))})
									lowerCAmelCase_      :					Optional[Any]     =			TextClassification(text_column='''text'''    ,    label_column='''label''')
									lowerCAmelCase_      :					Tuple     =			DatasetInfo(features=A_    ,    task_templates=A_)
									lowerCAmelCase_      :					List[Any]     =			Dataset.from_dict({'''text''': ['''A sentence.'''], '''label''': ['''pos''']}    ,    info=A_)

									def process(A_					:	Any):
											return example

									lowerCAmelCase_      :					List[Any]     =			dataset.remove_columns('''label''')
									lowerCAmelCase_      :					Optional[Any]     =			modified_dataset.map(A_)
									assert mapped_dataset.info.task_templates == []










from ...configuration_utils import PretrainedConfig
from ...utils import logging


A__  :  Tuple       = logging.get_logger(__name__)

A__  :  Tuple       = {
    '''facebook/opt-125m''': '''https://huggingface.co/facebook/opt-125m/blob/main/config.json''',
    '''facebook/opt-350m''': '''https://huggingface.co/facebook/opt-350m/blob/main/config.json''',
    '''facebook/opt-1.3b''': '''https://huggingface.co/facebook/opt-1.3b/blob/main/config.json''',
    '''facebook/opt-2.7b''': '''https://huggingface.co/facebook/opt-2.7b/blob/main/config.json''',
    '''facebook/opt-6.7b''': '''https://huggingface.co/facebook/opt-6.7b/blob/main/config.json''',
    '''facebook/opt-13b''': '''https://huggingface.co/facebook/opt-13b/blob/main/config.json''',
}



class     __snake_case						(					UpperCamelCase_						):
							_a      				=			'''opt'''
							_a      				=			['''past_key_values''']




							def __init__(	self					:	Optional[int]    ,    A_					:	Any=5_0_2_7_2    ,    A_					:	Tuple=7_6_8    ,    A_					:	Optional[Any]=1_2    ,    A_					:	str=3_0_7_2    ,    A_					:	Dict=2_0_4_8    ,    A_					:	List[Any]=True    ,    A_					:	Optional[int]=False    ,    A_					:	Optional[int]=None    ,    A_					:	Dict=0.1    ,    A_					:	str=0.0    ,    A_					:	Union[str, Any]=1_2    ,    A_					:	List[str]="relu"    ,    A_					:	Dict=0.0    ,    A_					:	Union[str, Any]=0.02    ,    A_					:	Optional[Any]=True    ,    A_					:	Optional[Any]=1    ,    A_					:	List[Any]=2    ,    A_					:	Dict=2    ,    A_					:	Optional[int]=True    ,    A_					:	Optional[int]=True    ,    **A_					:	List[Any]    ,    ):
									super().__init__(
									    pad_token_id=A_    ,    bos_token_id=A_    ,    eos_token_id=A_    ,    **A_    ,    )
									lowerCAmelCase_      :					List[Any]     =			vocab_size
									lowerCAmelCase_      :					Union[str, Any]     =			max_position_embeddings
									lowerCAmelCase_      :					Optional[int]     =			num_attention_heads
									lowerCAmelCase_      :					int     =			word_embed_proj_dim if word_embed_proj_dim is not None else hidden_size
									lowerCAmelCase_      :					List[Any]     =			ffn_dim
									lowerCAmelCase_      :					Any     =			hidden_size
									lowerCAmelCase_      :					Union[str, Any]     =			num_hidden_layers
									lowerCAmelCase_      :					str     =			dropout
									lowerCAmelCase_      :					List[str]     =			attention_dropout
									lowerCAmelCase_      :					Any     =			activation_function
									lowerCAmelCase_      :					Union[str, Any]     =			init_std
									lowerCAmelCase_      :					int     =			layerdrop
									lowerCAmelCase_      :					Optional[int]     =			use_cache
									lowerCAmelCase_      :					int     =			do_layer_norm_before
									# We keep these variables at `True` for backward compatibility.
									lowerCAmelCase_      :					Optional[Any]     =			enable_bias
									lowerCAmelCase_      :					Optional[int]     =			layer_norm_elementwise_affine

									# Note that the only purpose of `_remove_final_layer_norm` is to keep backward compatibility
									# with checkpoints that have been fine-tuned before transformers v4.20.1
									# see https://github.com/facebookresearch/metaseq/pull/164
									lowerCAmelCase_      :					Optional[Any]     =			_remove_final_layer_norm





