
'''simple docstring'''







import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
							import torch

							from transformers.activations import gelu_new, gelu_python, get_activation






@require_torch
class  lowerCAmelCase__   (  unittest.TestCase     ):






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     str													=       torch.tensor([-1_00, -1, -0.1, 0, 0.1, 1.0, 1_00]		)
											lowercase_	:     Union[str, Any]													=       get_activation('''gelu'''		)
											self.assertTrue(torch.allclose(gelu_python(__SCREAMING_SNAKE_CASE		)				,							torch_builtin(__SCREAMING_SNAKE_CASE		)		)		)
											self.assertFalse(torch.allclose(gelu_python(__SCREAMING_SNAKE_CASE		)				,							gelu_new(__SCREAMING_SNAKE_CASE		)		)		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Optional[Any]													=       torch.tensor([-1_00, -1, -0.1, 0, 0.1, 1.0, 1_00]		)
											lowercase_	:     Tuple													=       get_activation('''gelu'''		)
											lowercase_	:     Any													=       get_activation('''gelu_10'''		)

											lowercase_	:     List[str]													=       torch_builtin(__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Union[str, Any]													=       geluaa(__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Dict													=       torch.where(y_gelu_aa < 10.0				,							1				,							0		)

											self.assertTrue(torch.max(__SCREAMING_SNAKE_CASE		).item() == 10.0		)
											self.assertTrue(torch.allclose(y_gelu * clipped_mask				,							y_gelu_aa * clipped_mask		)		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											get_activation('''gelu'''		)
											get_activation('''gelu_10'''		)
											get_activation('''gelu_fast'''		)
											get_activation('''gelu_new'''		)
											get_activation('''gelu_python'''		)
											get_activation('''gelu_pytorch_tanh'''		)
											get_activation('''linear'''		)
											get_activation('''mish'''		)
											get_activation('''quick_gelu'''		)
											get_activation('''relu'''		)
											get_activation('''sigmoid'''		)
											get_activation('''silu'''		)
											get_activation('''swish'''		)
											get_activation('''tanh'''		)
											with self.assertRaises(__SCREAMING_SNAKE_CASE		):
																	get_activation('''bogus'''		)
											with self.assertRaises(__SCREAMING_SNAKE_CASE		):
																	get_activation(__SCREAMING_SNAKE_CASE		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     int													=       get_activation('''gelu'''		)
											lowercase_	:     Any													=       1
											lowercase_	:     str													=       get_activation('''gelu'''		)
											self.assertEqual(acta.a				,							1		)
											with self.assertRaises(__SCREAMING_SNAKE_CASE		):
																	lowercase_	:     Dict													=       acta.a










'''simple docstring'''

import argparse
import logging
import pickle
from collections import Counter


logging.basicConfig(
    format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""", datefmt="""%m/%d/%Y %H:%M:%S""", level=logging.INFO
)
lowercase_					= logging.getLogger(__name__)

if __name__ == "__main__":
      lowercase_					= argparse.ArgumentParser(
          description="""Token Counts for smoothing the masking probabilities in MLM (cf XLM/word2vec)"""
      )
      parser.add_argument(
          """--data_file""", type=str, default="""data/dump.bert-base-uncased.pickle""", help="""The binarized dataset."""
      )
      parser.add_argument(
          """--token_counts_dump""", type=str, default="""data/token_counts.bert-base-uncased.pickle""", help="""The dump file."""
      )
      parser.add_argument("""--vocab_size""", default=30_522, type=int)
      lowercase_					= parser.parse_args()

      logger.info(f"""Loading data from {args.data_file}""")
      with open(args.data_file, """rb""") as fp:
            lowercase_					= pickle.load(fp)

      logger.info("""Counting occurrences for MLM.""")
      lowercase_					= Counter()
      for tk_ids in data:
            counter.update(tk_ids)
      lowercase_					= [0] * args.vocab_size
      for k, v in counter.items():
            lowercase_					= v

      logger.info(f"""Dump to {args.token_counts_dump}""")
      with open(args.token_counts_dump, """wb""") as handle:
            pickle.dump(counts, handle, protocol=pickle.HIGHEST_PROTOCOL)



