
import math
from collections import OrderedDict

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

from .utils import logging


__magic_name__           =					logging.get_logger(__name__)




class    _SCREAMING_SNAKE_CASE					( nn.Module	):
      def __init__(     self	):
          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 A_     (     self						,		lowerCamelCase	):
          return nn.functional.gelu(lowerCamelCase						,		approximate="tanh"	)





class    _SCREAMING_SNAKE_CASE					( nn.Module	):
      def A_     (     self						,		lowerCamelCase	):
          return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi	) * (input + 0.0_4_4_7_1_5 * torch.pow(lowerCamelCase						,		3.0	))	))





class    _SCREAMING_SNAKE_CASE					( nn.Module	):
      def __init__(     self						,		lowerCamelCase = False	):
          super().__init__()
          if use_gelu_python:
              snake_case__  =			self._gelu_python
          else:
              snake_case__  =			nn.functional.gelu
      def A_     (     self						,		lowerCamelCase	):
          return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0	)	))



      def A_     (     self						,		lowerCamelCase	):
          return self.act(lowerCamelCase	)





class    _SCREAMING_SNAKE_CASE					( nn.Module	):
      def A_     (     self						,		lowerCamelCase	):
          return 0.5 * input * (1.0 + torch.tanh(input * 0.7_9_7_8_8_4_5_6_0_8 * (1.0 + 0.0_4_4_7_1_5 * input * input)	))





class    _SCREAMING_SNAKE_CASE					( nn.Module	):
      def A_     (     self						,		lowerCamelCase	):
          return input * torch.sigmoid(1.7_0_2 * input	)





class    _SCREAMING_SNAKE_CASE					( nn.Module	):
      def __init__(     self						,		lowerCamelCase						,		lowerCamelCase	):
          if min > max:
              raise ValueError(F"""min should be < max (got min: {min}, max: {max})"""	)

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



      def A_     (     self						,		lowerCamelCase	):
          return torch.clip(gelu(lowerCamelCase	)						,		self.min						,		self.max	)





class    _SCREAMING_SNAKE_CASE					( nn.Module	):
      def __init__(     self	):
          super().__init__()
          snake_case__  =			math.sqrt(2 / math.pi	)



      def A_     (     self						,		lowerCamelCase	):
          return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.0_4_4_7_1_5 * torch.pow(lowerCamelCase						,		3	))	))





class    _SCREAMING_SNAKE_CASE					( nn.Module	):
      def A_     (     self						,		lowerCamelCase	):
          return nn.functional.silu(lowerCamelCase	)





class    _SCREAMING_SNAKE_CASE					( nn.Module	):
      def __init__(     self	):
          super().__init__()
          if version.parse(torch.__version__	) < version.parse("1.9.0"	):
              snake_case__  =			self._mish_python
          else:
              snake_case__  =			nn.functional.mish
      def A_     (     self						,		lowerCamelCase	):
          return input * torch.tanh(nn.functional.softplus(lowerCamelCase	)	)



      def A_     (     self						,		lowerCamelCase	):
          return self.act(lowerCamelCase	)





class    _SCREAMING_SNAKE_CASE					( nn.Module	):
      def A_     (     self						,		lowerCamelCase	):
          return input





class    _SCREAMING_SNAKE_CASE					( nn.Module	):
      def A_     (     self						,		lowerCamelCase						,		lowerCamelCase=0.7_0_7_1_0_7						,		lowerCamelCase=0.2_8_2_0_9_5	):
          snake_case__  =			(input - mu).div(sigma * math.sqrt(2.0	)	)
          return 0.5 * (1.0 + torch.erf(lowerCamelCase	))





class    _SCREAMING_SNAKE_CASE					( nn.Module	):
      def A_     (     self						,		lowerCamelCase	):
          snake_case__  =			nn.functional.relu(lowerCamelCase	)
          snake_case__  =			torch.square(lowerCamelCase	)
          return squared





class    _SCREAMING_SNAKE_CASE					( __UpperCamelCase	):
      def __getitem__(     self						,		lowerCamelCase	):
          snake_case__  =			super().__getitem__(lowerCamelCase	)
          snake_case__  ,   snake_case__  =			content if isinstance(lowerCamelCase						,		lowerCamelCase	) else (content, {})
          return cls(**lowerCamelCase	)





__magic_name__           =					{
    '''gelu''': GELUActivation,
    '''gelu_10''': (ClippedGELUActivation, {'''min''': -10, '''max''': 10}),
    '''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,
}
__magic_name__           =					ClassInstantier(ACTaCLS)





def 		SCREAMING_SNAKE_CASE__      ( __lowerCAmelCase							):
    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
__magic_name__           =					get_activation('''gelu_python''')
__magic_name__           =					get_activation('''gelu_new''')
__magic_name__           =					get_activation('''gelu''')
__magic_name__           =					get_activation('''gelu_fast''')
__magic_name__           =					get_activation('''quick_gelu''')
__magic_name__           =					get_activation('''silu''')
__magic_name__           =					get_activation('''mish''')





__magic_name__           =					get_activation('''linear''')





import os
import tempfile
import unittest
from typing import List

from transformers.models.esm.tokenization_esm import VOCAB_FILES_NAMES, EsmTokenizer
from transformers.testing_utils import require_tokenizers
from transformers.tokenization_utils import PreTrainedTokenizer
from transformers.tokenization_utils_base import PreTrainedTokenizerBase




@require_tokenizers
class    _SCREAMING_SNAKE_CASE					( unittest.TestCase	):
      _A  :						Optional[Any]      		=       EsmTokenizer
      def A_     (     self	):
          super().setUp()
          snake_case__  =			tempfile.mkdtemp()
          # fmt: off
          snake_case__  =			["<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>"]  # noqa: E501
          # fmt: on
          snake_case__  =			os.path.join(self.tmpdirname						,		VOCAB_FILES_NAMES["vocab_file"]	)
          with open(self.vocab_file						,		"w"						,		encoding="utf-8"	) as vocab_writer:
              vocab_writer.write("".join([x + "\n" for x in vocab_tokens]	)	)
      def A_     (     self						,		**lowerCamelCase	):
          return [self.get_tokenizer(**lowerCamelCase	)]
      def A_     (     self						,		**lowerCamelCase	):
          return self.tokenizer_class.from_pretrained(self.tmpdirname						,		**lowerCamelCase	)
      def A_     (     self	):
          snake_case__  =			self.tokenizer_class(self.vocab_file	)

          snake_case__  =			tokenizer.tokenize("LAGVS"	)
          self.assertListEqual(lowerCamelCase						,		["L", "A", "G", "V", "S"]	)
          self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase	)						,		[4, 5, 6, 7, 8]	)
      def A_     (     self	):
          snake_case__  =			self.tokenizer_class(self.vocab_file	)

          snake_case__  =			"LAGVS"
          self.assertListEqual(tokenizer.encode(lowerCamelCase	)						,		[0, 4, 5, 6, 7, 8, 2]	)
      def A_     (     self	):
          snake_case__  =			self.tokenizer_class(self.vocab_file	)

          snake_case__  =			["LAGVS", "WCB"]
          snake_case__  =			tokenizer(lowerCamelCase						,		padding=lowerCamelCase	)["input_ids"]

          self.assertListEqual(lowerCamelCase						,		[[0, 4, 5, 6, 7, 8, 2], [0, 22, 23, 25, 2]]	)
      def A_     (     self	):
          snake_case__  =			self.tokenizer_class(self.vocab_file	)

          snake_case__  =			["LAGVS", "WCB"]
          snake_case__  =			tokenizer(lowerCamelCase						,		padding=lowerCamelCase	)["input_ids"]

          self.assertListEqual(lowerCamelCase						,		[[0, 4, 5, 6, 7, 8, 2], [0, 22, 23, 25, 2, 1, 1]]	)



      def A_     (     self	):
          snake_case__  =			self.get_tokenizers(fast=lowerCamelCase	)
          for tokenizer in tokenizers:
              with self.subTest(F"""{tokenizer.__class__.__name__}"""	):
                  snake_case__  =			"<unk>"
                  snake_case__  =			"<mask>"

                  snake_case__  =			tokenizer.tokenize(lowerCamelCase	)
                  snake_case__  =			tokenizer.tokenize(lowerCamelCase	)

                  self.assertEqual(len(lowerCamelCase	)						,		1	)
                  self.assertEqual(len(lowerCamelCase	)						,		1	)
                  self.assertEqual(token_a[0]						,		lowerCamelCase	)
                  self.assertEqual(token_a[0]						,		lowerCamelCase	)



