



"""simple docstring"""







import re
import tempfile
import unittest
from pathlib import Path

from datasets.utils.metadata import DatasetMetadata

def    __SCREAMING_SNAKE_CASE		( lowerCamelCase_:       str   ):
						"""simple docstring"""






						snake_case      : Union[str, Any]							=   min(re.search("^ +"    ,		lowerCamelCase_   ).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(lowerCamelCase_   )]   )


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

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


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

class 			_a     (   unittest.TestCase):



			def 							__lowercase				( self       :							Optional[Any]		)       ->				Tuple:
									with tempfile.TemporaryDirectory() as tmp_dir:
															snake_case      : Union[str, Any]							=   Path(_lowercase		) / "README.md"
															with open(_lowercase      ,  "w+"		) as readme_file:
																					readme_file.write(_lowercase		)
															snake_case      : Dict							=   DatasetMetadata.from_readme(_lowercase		)
															self.assertDictEqual(_lowercase      ,  {"language": ["zh", "en"], "task_ids": ["sentiment-classification"]}		)

															with open(_lowercase      ,  "w+"		) as readme_file:
																					readme_file.write(_lowercase		)
															snake_case      : List[Any]							=   DatasetMetadata.from_readme(_lowercase		)
															self.assertDictEqual(_lowercase      ,  {}		)

															with open(_lowercase      ,  "w+"		) as readme_file:
																					readme_file.write(_lowercase		)
															snake_case      : Dict							=   DatasetMetadata.from_readme(_lowercase		)
															self.assertEqual(_lowercase      ,  {}		)



			def 							__lowercase				( self       :							Tuple		)       ->				Dict:
									snake_case      : Tuple							=   _dedent(
									    "\\n            annotations_creators:\n            - found\n            language_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - question-answering\n            task_ids:\n            - open-domain-qa\n            "		)
									assert DatasetMetadata.from_yaml_string(_lowercase		)

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

									snake_case      : Optional[int]							=   _dedent(
									    "\\n            annotations_creators:\n            - found\n            language_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - text-classification\n            task_ids:\n            - multi-class-classification\n            paperswithcode_id:\n            - squad\n            configs:\n            - en\n            train-eval-index:\n            - config: en\n              task: text-classification\n              task_id: multi_class_classification\n              splits:\n                train_split: train\n                eval_split: test\n              col_mapping:\n                text: text\n                label: target\n              metrics:\n                - type: accuracy\n                  name: Accuracy\n            extra_gated_prompt: |\n              By clicking on “Access repository” below, you also agree to ImageNet Terms of Access:\n              [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:\n              1. Researcher shall use the Database only for non-commercial research and educational purposes.\n            extra_gated_fields:\n              Company: text\n              Country: text\n              I agree to use this model for non-commerical use ONLY: checkbox\n            "		)
									assert DatasetMetadata.from_yaml_string(_lowercase		)








"""simple docstring"""







import math
from collections import OrderedDict

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

from .utils import logging


A    =	logging.get_logger(__name__)

class 			_a     (   nn.Module):



			def __init__( self       :							Union[str, Any]		)       ->				str:
									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 							__lowercase				( self       :							str      ,  _lowercase       :							Tensor		)       ->				Tensor:
									return nn.functional.gelu(_lowercase      ,  approximate="tanh"		)



class 			_a     (   nn.Module):



			def 							__lowercase				( self       :							Tuple      ,  _lowercase       :							Tensor		)       ->				Tensor:
									return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi		) * (input + 0.044715 * torch.pow(_lowercase      ,  3.0		))		))



class 			_a     (   nn.Module):



			def __init__( self       :							Tuple      ,  _lowercase       :							bool = False		)       ->				Optional[Any]:
									super().__init__()
									if use_gelu_python:
															snake_case      : Tuple							=   self._gelu_python
									else:
															snake_case      : Any							=   nn.functional.gelu



			def 							__lowercase				( self       :							Any      ,  _lowercase       :							Tensor		)       ->				Tensor:
									return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0		)		))



			def 							__lowercase				( self       :							Optional[int]      ,  _lowercase       :							Tensor		)       ->				Tensor:
									return self.act(_lowercase		)



class 			_a     (   nn.Module):



			def 							__lowercase				( self       :							Union[str, Any]      ,  _lowercase       :							Tensor		)       ->				Tensor:
									return 0.5 * input * (1.0 + torch.tanh(input * 0.7978845608 * (1.0 + 0.044715 * input * input)		))



class 			_a     (   nn.Module):



			def 							__lowercase				( self       :							Dict      ,  _lowercase       :							Tensor		)       ->				Tensor:
									return input * torch.sigmoid(1.702 * input		)



class 			_a     (   nn.Module):



			def __init__( self       :							Union[str, Any]      ,  _lowercase       :							float      ,  _lowercase       :							float		)       ->				Union[str, Any]:
									if min > max:
															raise ValueError(F'''min should be < max (got min: {min}, max: {max})'''		)

									super().__init__()
									snake_case      : str							=   min
									snake_case      : Dict							=   max



			def 							__lowercase				( self       :							str      ,  _lowercase       :							Tensor		)       ->				Tensor:
									return torch.clip(gelu(_lowercase		)      ,  self.min      ,  self.max		)



class 			_a     (   nn.Module):



			def __init__( self       :							str		)       ->				Tuple:
									super().__init__()
									snake_case      : int							=   math.sqrt(2 / math.pi		)



			def 							__lowercase				( self       :							Union[str, Any]      ,  _lowercase       :							Tensor		)       ->				Tensor:
									return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.044715 * torch.pow(_lowercase      ,  3		))		))



class 			_a     (   nn.Module):



			def 							__lowercase				( self       :							int      ,  _lowercase       :							Tensor		)       ->				Tensor:
									return nn.functional.silu(_lowercase		)



class 			_a     (   nn.Module):



			def __init__( self       :							Union[str, Any]		)       ->				Optional[Any]:
									super().__init__()
									if version.parse(torch.__version__		) < version.parse("1.9.0"		):
															snake_case      : Union[str, Any]							=   self._mish_python
									else:
															snake_case      : Union[str, Any]							=   nn.functional.mish



			def 							__lowercase				( self       :							str      ,  _lowercase       :							Tensor		)       ->				Tensor:
									return input * torch.tanh(nn.functional.softplus(_lowercase		)		)



			def 							__lowercase				( self       :							Tuple      ,  _lowercase       :							Tensor		)       ->				Tensor:
									return self.act(_lowercase		)



class 			_a     (   nn.Module):



			def 							__lowercase				( self       :							List[Any]      ,  _lowercase       :							Tensor		)       ->				Tensor:
									return input



class 			_a     (   nn.Module):



			def 							__lowercase				( self       :							Optional[Any]      ,  _lowercase       :							Optional[Any]      ,  _lowercase       :							List[str]=0.707107      ,  _lowercase       :							Dict=0.282095		)       ->				int:
									snake_case      : Any							=   (input - mu).div(sigma * math.sqrt(2.0		)		)
									return 0.5 * (1.0 + torch.erf(_lowercase		))



class 			_a     (   nn.Module):



			def 							__lowercase				( self       :							str      ,  _lowercase       :							List[str]		)       ->				str:
									snake_case      : Optional[Any]							=   nn.functional.relu(_lowercase		)
									snake_case      : List[str]							=   torch.square(_lowercase		)
									return squared



class 			_a     (   SCREAMING_SNAKE_CASE__):



			def __getitem__( self       :							List[str]      ,  _lowercase       :							Any		)       ->				int:
									snake_case      : List[str]							=   super().__getitem__(_lowercase		)
									snake_case						, snake_case      : Optional[Any]							=   content if isinstance(_lowercase      ,  _lowercase		) else (content, {})
									return cls(**_lowercase		)



A    =	{
    '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    =	ClassInstantier(ACTaCLS)

def    __SCREAMING_SNAKE_CASE		( lowerCamelCase_:       Tuple   ):
						"""simple docstring"""






						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    =	get_activation('gelu_python')
A    =	get_activation('gelu_new')
A    =	get_activation('gelu')
A    =	get_activation('gelu_fast')
A    =	get_activation('quick_gelu')
A    =	get_activation('silu')
A    =	get_activation('mish')
A    =	get_activation('linear')



