
import re
import tempfile
import unittest
from pathlib import Path

from datasets.utils.metadata import DatasetMetadata


def 					__lowerCAmelCase    (    a__						) ->						str:
				__a    							=					min(re.search('''^ +'''		,					a__						).end() if t.startswith(''' '''						) else 0 for t in string.splitlines()						)
				return "\n".join([line[indent_level:] for line in string.splitlines() if indent_level < len(a__						)]						)


A  :			str     				=				'\\n---\nlanguage:\n- zh\n- en\ntask_ids:\n- sentiment-classification\n---\n# Begin of markdown\n\nSome cool dataset card\n'

A  :			List[str]     				=				'\\n---\n---\n# Begin of markdown\n\nSome cool dataset card\n'


A  :			Tuple     				=				'\\n# Begin of markdown\n\nSome cool dataset card\n'







class     __A(				unittest.TestCase    ):




				def        SCREAMING_SNAKE_CASE_	(  self       )					->   Dict:


								'''simple docstring'''




								with tempfile.TemporaryDirectory() as tmp_dir:
												__a    							=					Path(_snake_case       ) / '''README.md'''
												with open(_snake_case	,  '''w+'''       ) as readme_file:
																readme_file.write(_snake_case       )
												__a    							=					DatasetMetadata.from_readme(_snake_case       )
												self.assertDictEqual(_snake_case	,  {'''language''': ['''zh''', '''en'''], '''task_ids''': ['''sentiment-classification''']}       )

												with open(_snake_case	,  '''w+'''       ) as readme_file:
																readme_file.write(_snake_case       )
												__a    							=					DatasetMetadata.from_readme(_snake_case       )
												self.assertDictEqual(_snake_case	,  {}       )

												with open(_snake_case	,  '''w+'''       ) as readme_file:
																readme_file.write(_snake_case       )
												__a    							=					DatasetMetadata.from_readme(_snake_case       )
												self.assertEqual(_snake_case	,  {}       )




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


								'''simple docstring'''




								__a    							=					_dedent(
								    '''\
            annotations_creators:
            - found
            language_creators:
            - found
            language:
            - en
            license:
            - unknown
            multilinguality:
            - monolingual
            pretty_name: Test Dataset
            size_categories:
            - 10K<n<100K
            source_datasets:
            - extended|other-yahoo-webscope-l6
            task_categories:
            - question-answering
            task_ids:
            - open-domain-qa
            '''       )
								assert DatasetMetadata.from_yaml_string(_snake_case       )

								__a    							=					_dedent(
								    '''\
            annotations_creators:
            - found
            language:
            - en
            license:
            - unknown
            multilinguality:
            - monolingual
            pretty_name: Test Dataset
            size_categories:
            - 10K<n<100K
            source_datasets:
            - extended|other-yahoo-webscope-l6
            task_categories:
            - question-answering
            task_ids:
            - open-domain-qa
            task_ids:
            - open-domain-qa
            '''       )
								with self.assertRaises(_snake_case       ):
												DatasetMetadata.from_yaml_string(_snake_case       )

								__a    							=					_dedent(
								    '''\
            annotations_creators:
            - found
            language_creators:
            - found
            language:
            - en
            license:
            - unknown
            multilinguality:
            - monolingual
            pretty_name: Test Dataset
            size_categories:
            - 10K<n<100K
            source_datasets:
            - extended|other-yahoo-webscope-l6
            task_categories:
            - text-classification
            task_ids:
            - multi-class-classification
            paperswithcode_id:
            - squad
            configs:
            - en
            train-eval-index:
            - config: en
              task: text-classification
              task_id: multi_class_classification
              splits:
                train_split: train
                eval_split: test
              col_mapping:
                text: text
                label: target
              metrics:
                - type: accuracy
                  name: Accuracy
            extra_gated_prompt: |
              By clicking on “Access repository” below, you also agree to ImageNet Terms of Access:
              [RESEARCHER_FULLNAME] (the "Researcher") has requested permission to use the ImageNet database (the "Database") at Princeton University and Stanford University. In exchange for such permission, Researcher hereby agrees to the following terms and conditions:
              1. Researcher shall use the Database only for non-commercial research and educational purposes.
            extra_gated_fields:
              Company: text
              Country: text
              I agree to use this model for non-commerical use ONLY: checkbox
            '''       )
								assert DatasetMetadata.from_yaml_string(_snake_case       )

import math
from collections import OrderedDict

import torch
from packaging import version
from torch import Tensor, nn

from .utils import logging


A  :			List[str]     				=				logging.get_logger(__name__)







class     __A(				nn.Module    ):




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


								'''simple docstring'''




								super().__init__()
								if version.parse(torch.__version__       ) < version.parse('''1.12.0'''       ):
												raise ImportError(
												    F"""You are using torch=={torch.__version__}, but torch>=1.12.0 is required to use """
												    '''PytorchGELUTanh. Please upgrade torch.'''       )




				def        SCREAMING_SNAKE_CASE_	(  self	,  _snake_case       )					->   Tensor:


								'''simple docstring'''




								return nn.functional.gelu(_snake_case	,  approximate='''tanh'''       )







class     __A(				nn.Module    ):




				def        SCREAMING_SNAKE_CASE_	(  self	,  _snake_case       )					->   Tensor:


								'''simple docstring'''




								return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi       ) * (input + 0.04_4715 * torch.pow(_snake_case	,  3.0       ))       ))







class     __A(				nn.Module    ):




				def __init__(  self	,  _snake_case = False       )					->   Tuple:


								'''simple docstring'''




								super().__init__()
								if use_gelu_python:
												__a    							=					self._gelu_python
								else:
												__a    							=					nn.functional.gelu




				def        SCREAMING_SNAKE_CASE_	(  self	,  _snake_case       )					->   Tensor:


								'''simple docstring'''




								return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0       )       ))




				def        SCREAMING_SNAKE_CASE_	(  self	,  _snake_case       )					->   Tensor:


								'''simple docstring'''




								return self.act(_snake_case       )







class     __A(				nn.Module    ):




				def        SCREAMING_SNAKE_CASE_	(  self	,  _snake_case       )					->   Tensor:


								'''simple docstring'''




								return 0.5 * input * (1.0 + torch.tanh(input * 0.79_7884_5608 * (1.0 + 0.04_4715 * input * input)       ))







class     __A(				nn.Module    ):




				def        SCREAMING_SNAKE_CASE_	(  self	,  _snake_case       )					->   Tensor:


								'''simple docstring'''




								return input * torch.sigmoid(1.702 * input       )







class     __A(				nn.Module    ):




				def __init__(  self	,  _snake_case	,  _snake_case       )					->   Optional[int]:


								'''simple docstring'''




								if min > max:
												raise ValueError(F"""min should be < max (got min: {min}, max: {max})"""       )

								super().__init__()
								__a    							=					min
								__a    							=					max




				def        SCREAMING_SNAKE_CASE_	(  self	,  _snake_case       )					->   Tensor:


								'''simple docstring'''




								return torch.clip(gelu(_snake_case       )	,  self.min	,  self.max       )







class     __A(				nn.Module    ):




				def __init__(  self       )					->   str:


								'''simple docstring'''




								super().__init__()
								__a    							=					math.sqrt(2 / math.pi       )




				def        SCREAMING_SNAKE_CASE_	(  self	,  _snake_case       )					->   Tensor:


								'''simple docstring'''




								return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.04_4715 * torch.pow(_snake_case	,  3       ))       ))







class     __A(				nn.Module    ):




				def        SCREAMING_SNAKE_CASE_	(  self	,  _snake_case       )					->   Tensor:


								'''simple docstring'''




								return nn.functional.silu(_snake_case       )







class     __A(				nn.Module    ):




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


								'''simple docstring'''




								super().__init__()
								if version.parse(torch.__version__       ) < version.parse('''1.9.0'''       ):
												__a    							=					self._mish_python
								else:
												__a    							=					nn.functional.mish




				def        SCREAMING_SNAKE_CASE_	(  self	,  _snake_case       )					->   Tensor:


								'''simple docstring'''




								return input * torch.tanh(nn.functional.softplus(_snake_case       )       )




				def        SCREAMING_SNAKE_CASE_	(  self	,  _snake_case       )					->   Tensor:


								'''simple docstring'''




								return self.act(_snake_case       )







class     __A(				nn.Module    ):




				def        SCREAMING_SNAKE_CASE_	(  self	,  _snake_case       )					->   Tensor:


								'''simple docstring'''




								return input







class     __A(				nn.Module    ):




				def        SCREAMING_SNAKE_CASE_	(  self	,  _snake_case	,  _snake_case=0.70_7107	,  _snake_case=0.28_2095       )					->   int:


								'''simple docstring'''




								__a    							=					(input - mu).div(sigma * math.sqrt(2.0       )       )
								return 0.5 * (1.0 + torch.erf(_snake_case       ))







class     __A(				nn.Module    ):




				def        SCREAMING_SNAKE_CASE_	(  self	,  _snake_case       )					->   Any:


								'''simple docstring'''




								__a    							=					nn.functional.relu(_snake_case       )
								__a    							=					torch.square(_snake_case       )
								return squared







class     __A(				a    ):




				def __getitem__(  self	,  _snake_case       )					->   Dict:


								'''simple docstring'''




								__a    							=					super().__getitem__(_snake_case       )
								__a							,  __a    							=					content if isinstance(_snake_case	,  _snake_case       ) else (content, {})
								return cls(**_snake_case       )



A  :			str     				=				{
    'gelu': GELUActivation,
    'gelu_10': (ClippedGELUActivation, {'min': -1_0, 'max': 1_0}),
    'gelu_fast': FastGELUActivation,
    'gelu_new': NewGELUActivation,
    'gelu_python': (GELUActivation, {'use_gelu_python': True}),
    'gelu_pytorch_tanh': PytorchGELUTanh,
    'gelu_accurate': AccurateGELUActivation,
    'laplace': LaplaceActivation,
    'linear': LinearActivation,
    'mish': MishActivation,
    'quick_gelu': QuickGELUActivation,
    'relu': nn.ReLU,
    'relu2': ReLUSquaredActivation,
    'relu6': nn.ReLUa,
    'sigmoid': nn.Sigmoid,
    'silu': SiLUActivation,
    'swish': SiLUActivation,
    'tanh': nn.Tanh,
}
A  :			Any     				=				ClassInstantier(ACTaCLS)


def 					__lowerCAmelCase    (    a__						) ->						Optional[int]:
				if activation_string in ACTaFN:
								return ACTaFN[activation_string]
				else:
								raise KeyError(F"""function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()						)}"""						)


# For backwards compatibility with: from activations import gelu_python
A  :			Optional[Any]     				=				get_activation('gelu_python')
A  :			str     				=				get_activation('gelu_new')
A  :			Any     				=				get_activation('gelu')
A  :			int     				=				get_activation('gelu_fast')
A  :			Any     				=				get_activation('quick_gelu')
A  :			int     				=				get_activation('silu')
A  :			List[str]     				=				get_activation('mish')






A  :			Dict     				=				get_activation('linear')