code: 
  import json
  import os
  import pickle
  import subprocess
  from hashlib import mda
  from pathlib import Path
  from tempfile import gettempdir
  from textwrap import dedent
  from types import CodeType, FunctionType
  from unittest import TestCase
  from unittest.mock import patch
  
  import pytest
  from multiprocess import Pool
  from packaging import version
  
  import datasets
  from datasets.fingerprint import Hasher, fingerprint_transform
  from datasets.table import InMemoryTable
  
  from .utils import (
      require_regex,
      require_spacy,
      require_spacy_model,
      require_tiktoken,
      require_torch,
      require_transformers,
  )
  
  
  
  
  class      __lowercase	:
  
  
  
  
  
  
  	def __init__(self							:   str			,  snake_case							:   Any						)   -> Optional[int]:
  							_lowercase					:  Tuple    			=      foo
  
  
  
  
  
  
  	def __call__(self							:   List[str]						)   -> List[str]:
  							return self.foo
  
  
  
  
  class      __lowercase	(	datasets.Dataset					):
  
  
  
  
  
  
  	@fingerprint_transform(inplace=snake_case						)
  	def 			_a(self							:   List[str]			,  snake_case							:   Optional[Any]			,  *snake_case							:   str			,  **snake_case							:   int						)   -> Optional[int]:
  							return DatasetChild(self.data			,  fingerprint=snake_case						)
  
  
  
  
  
  
  	@fingerprint_transform(inplace=snake_case						)
  	def 			_a(self							:   int			,  snake_case							:   List[Any]			,  *snake_case							:   Optional[int]			,  **snake_case							:   List[str]						)   -> Optional[int]:
  							return DatasetChild(self.data			,  fingerprint=snake_case						)
  
  
  
  
  class      __lowercase	:
  
  
  
  
  
  
  	def __init__(self							:   Optional[Any]			,  snake_case							:   Dict						)   -> int:
  							_lowercase					:  str    			=      callable
  
  
  
  
  
  
  	def __call__(self							:   Union[str, Any]			,  *snake_case							:   str			,  **snake_case							:   Any						)   -> str:
  							if self.callable is not None:
  													return self.callable(*snake_case			,  **snake_case						)
  
  
  
  
  
  
  	def __getstate__(self							:   str						)   -> Tuple:
  							raise pickle.PicklingError()
  
  
  
  
  class      __lowercase	(	__snake_case					):
  
  
  
  
  
  
  	@require_transformers
  	@pytest.mark.integration
  	def 			_a(self							:   Optional[Any]						)   -> Optional[int]:
  							from transformers import AutoTokenizer
  
  							def encode(snake_case							:   List[str]						):
  													return tokenizer(snake_case						)
  
  							# TODO: add hash consistency tests across sessions
  							_lowercase					:  Optional[Any]    			=      AutoTokenizer.from_pretrained("bert-base-uncased"						)
  							_lowercase					:  Any    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							_lowercase					:  List[str]    			=      mda(datasets.utils.py_utils.dumps(lambda snake_case						: tokenizer(snake_case						)						)						).hexdigest()
  							_lowercase					:  Tuple    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							_lowercase					:  Any    			=      AutoTokenizer.from_pretrained("bert-base-cased"						)
  							_lowercase					:  str    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							_lowercase					:  Union[str, Any]    			=      mda(datasets.utils.py_utils.dumps(lambda snake_case						: tokenizer(snake_case						)						)						).hexdigest()
  							_lowercase					:  Tuple    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							_lowercase					:  List[Any]    			=      AutoTokenizer.from_pretrained("bert-base-uncased"						)
  							_lowercase					:  List[Any]    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							_lowercase					:  int    			=      mda(datasets.utils.py_utils.dumps(lambda snake_case						: tokenizer(snake_case						)						)						).hexdigest()
  							_lowercase					:  Optional[int]    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							self.assertEqual(snake_case			,  snake_case						)
  							self.assertNotEqual(snake_case			,  snake_case						)
  							self.assertEqual(snake_case			,  snake_case						)
  							self.assertNotEqual(snake_case			,  snake_case						)
  							self.assertEqual(snake_case			,  snake_case						)
  							self.assertNotEqual(snake_case			,  snake_case						)
  
  
  
  
  
  
  	@require_transformers
  	@pytest.mark.integration
  	def 			_a(self							:   List[Any]						)   -> int:
  							from transformers import AutoTokenizer
  
  							_lowercase					:  Tuple    			=      AutoTokenizer.from_pretrained("gpt2"						)
  							_lowercase					:  str    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							tokenizer("Hello world !"						)  # call once to change the tokenizer's cache
  							_lowercase					:  Tuple    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							self.assertEqual(snake_case			,  snake_case						)
  
  
  
  
  
  
  	@require_regex
  	def 			_a(self							:   str						)   -> List[Any]:
  							import regex
  
  							_lowercase					:  Tuple    			=      regex.Regex("foo"						)
  							_lowercase					:  List[str]    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							_lowercase					:  Dict    			=      regex.Regex("bar"						)
  							_lowercase					:  int    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							_lowercase					:  int    			=      regex.Regex("foo"						)
  							_lowercase					:  int    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							self.assertEqual(snake_case			,  snake_case						)
  							self.assertNotEqual(snake_case			,  snake_case						)
  
  
  
  
  class      __lowercase	(	__snake_case					):
  
  
  
  
  
  
  	def 			_a(self							:   Any						)   -> Dict:
  							def func():
  													return foo
  
  							_lowercase					:  Any    			=      [0]
  							_lowercase					:  Optional[Any]    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							_lowercase					:  List[Any]    			=      [1]
  							_lowercase					:  List[str]    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							_lowercase					:  Any    			=      [0]
  							_lowercase					:  Any    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							self.assertEqual(snake_case			,  snake_case						)
  							self.assertNotEqual(snake_case			,  snake_case						)
  
  
  
  
  
  
  	def 			_a(self							:   Optional[Any]						)   -> Union[str, Any]:
  							def func():
  													pass
  
  							_lowercase					:  List[str]    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  
  							def func():
  													pass
  
  							_lowercase					:  int    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							self.assertEqual(snake_case			,  snake_case						)
  
  
  
  
  
  
  	def 			_a(self							:   Optional[int]						)   -> List[Any]:
  							_lowercase					:  int    			=      mda(datasets.utils.py_utils.dumps(Foo([0]						)						)						).hexdigest()
  							_lowercase					:  Optional[int]    			=      mda(datasets.utils.py_utils.dumps(Foo([1]						)						)						).hexdigest()
  							_lowercase					:  Optional[Any]    			=      mda(datasets.utils.py_utils.dumps(Foo([0]						)						)						).hexdigest()
  							self.assertEqual(snake_case			,  snake_case						)
  							self.assertNotEqual(snake_case			,  snake_case						)
  
  
  
  
  
  
  	def 			_a(self							:   Any						)   -> Union[str, Any]:
  							_lowercase					:  Optional[Any]    			=      mda(datasets.utils.py_utils.dumps(Foo([0]						).__call__						)						).hexdigest()
  							_lowercase					:  int    			=      mda(datasets.utils.py_utils.dumps(Foo([1]						).__call__						)						).hexdigest()
  							_lowercase					:  Any    			=      mda(datasets.utils.py_utils.dumps(Foo([0]						).__call__						)						).hexdigest()
  							self.assertEqual(snake_case			,  snake_case						)
  							self.assertNotEqual(snake_case			,  snake_case						)
  
  
  
  
  
  
  	def 			_a(self							:   Any						)   -> Any:
  							_lowercase					:  Tuple    			=      (
  							    "co_argcount",
  							    "co_kwonlyargcount",
  							    "co_nlocals",
  							    "co_stacksize",
  							    "co_flags",
  							    "co_code",
  							    "co_consts",
  							    "co_names",
  							    "co_varnames",
  							    "co_filename",
  							    "co_name",
  							    "co_firstlineno",
  							    "co_lnotab",
  							    "co_freevars",
  							    "co_cellvars",
  							)
  
  							def _create_code_pyaa(*snake_case							:   List[Any]						):
  													if hasattr(args[-3]			,  "encode"						):
  																			_lowercase					:  Any    			=      list(snake_case						)
  																			_lowercase					:  str    			=      args[-3].encode()  # co_lnotab
  																			_lowercase					:  List[str]    			=      args[-10].encode()  # co_code
  													if hasattr(snake_case			,  "co_posonlyargcount"						):
  																			if len(snake_case						) == 16:
  																									return CodeType(*snake_case						)
  																			elif len(snake_case						) == 15:
  																									return CodeType(args[0]			,  0			,  *args[1:]						)
  																			return CodeType(args[0]			,  0			,  0			,  *args[1:]						)
  													elif hasattr(snake_case			,  "co_kwonlyargcount"						):
  																			if len(snake_case						) == 16:
  																									return CodeType(args[0]			,  *args[2:]						)
  																			elif len(snake_case						) == 15:
  																									return CodeType(*snake_case						)
  																			return CodeType(args[0]			,  0			,  *args[1:]						)
  													if len(snake_case						) == 16:
  																			return CodeType(args[0]			,  *args[3:]						)
  													elif len(snake_case						) == 15:
  																			return CodeType(args[0]			,  *args[2:]						)
  													return CodeType(*snake_case						)
  
  							def create_ipython_func(snake_case							:   Dict			,  snake_case							:   str						):
  													def func():
  																			return returned_obj
  
  													_lowercase					:  Tuple    			=      func.__code__
  													# Use _create_code from dill in order to make it work for different python versions
  													if datasets.config.PY_VERSION < version.parse("3.8"						):
  																			_lowercase					:  Tuple    			=      _create_code_pyaa(
  																			    *[getattr(snake_case			,  snake_case						) if k != "co_filename" else co_filename for k in code_args_pyaa]						)
  													else:
  																			_lowercase					:  Any    			=      code.replace(co_filename=snake_case						)
  													return FunctionType(snake_case			,  func.__globals__			,  func.__name__			,  func.__defaults__			,  func.__closure__						)
  
  							_lowercase					:  Union[str, Any]    			=      "<ipython-input-2-e0383a102aae>", [0]
  							_lowercase					:  Optional[int]    			=      mda(datasets.utils.py_utils.dumps(create_ipython_func(snake_case			,  snake_case						)						)						).hexdigest()
  							_lowercase					:  Tuple    			=      "<ipython-input-2-e0383a102aae>", [1]
  							_lowercase					:  Dict    			=      mda(datasets.utils.py_utils.dumps(create_ipython_func(snake_case			,  snake_case						)						)						).hexdigest()
  							_lowercase					:  List[str]    			=      "<ipython-input-5-713f6613acf3>", [0]
  							_lowercase					:  Dict    			=      mda(datasets.utils.py_utils.dumps(create_ipython_func(snake_case			,  snake_case						)						)						).hexdigest()
  							self.assertEqual(snake_case			,  snake_case						)
  							self.assertNotEqual(snake_case			,  snake_case						)
  
  							_lowercase					:  Dict    			=      os.path.join(gettempdir()			,  "ipykernel_12345"			,  "321456789.py"						), [0]
  							_lowercase					:  int    			=      mda(datasets.utils.py_utils.dumps(create_ipython_func(snake_case			,  snake_case						)						)						).hexdigest()
  							_lowercase					:  Union[str, Any]    			=      os.path.join(gettempdir()			,  "ipykernel_12345"			,  "321456789.py"						), [1]
  							_lowercase					:  int    			=      mda(datasets.utils.py_utils.dumps(create_ipython_func(snake_case			,  snake_case						)						)						).hexdigest()
  							_lowercase					:  List[str]    			=      os.path.join(gettempdir()			,  "ipykernel_12345"			,  "654123987.py"						), [0]
  							_lowercase					:  Union[str, Any]    			=      mda(datasets.utils.py_utils.dumps(create_ipython_func(snake_case			,  snake_case						)						)						).hexdigest()
  							self.assertEqual(snake_case			,  snake_case						)
  							self.assertNotEqual(snake_case			,  snake_case						)
  
  
  
  
  
  
  	def 			_a(self							:   str						)   -> str:
  							_lowercase					:  Optional[int]    			=      [0], [1]
  
  							def func():
  													return foo, bar
  
  							_lowercase					:  Dict    			=      "__main__"
  
  							def globalvars_mocka_side_effect(snake_case							:   Optional[int]			,  *snake_case							:   str			,  **snake_case							:   Optional[int]						):
  													return {"foo": foo, "bar": bar}
  
  							def globalvars_mocka_side_effect(snake_case							:   int			,  *snake_case							:   List[Any]			,  **snake_case							:   str						):
  													return {"bar": bar, "foo": foo}
  
  							with patch("dill.detect.globalvars"			,  side_effect=snake_case						) as globalvars_mocka:
  													_lowercase					:  List[str]    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  													self.assertGreater(globalvars_mocka.call_count			,  0						)
  							with patch("dill.detect.globalvars"			,  side_effect=snake_case						) as globalvars_mocka:
  													_lowercase					:  str    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  													self.assertGreater(globalvars_mocka.call_count			,  0						)
  							self.assertEqual(snake_case			,  snake_case						)
  
  
  
  
  class      __lowercase	(	__snake_case					):
  
  
  
  
  
  
  	def 			_a(self							:   Any						)   -> Tuple:
  							_lowercase					:  int    			=      Hasher.hash("hello"						)
  							_lowercase					:  Optional[int]    			=      Hasher.hash("hello"						)
  							_lowercase					:  Dict    			=      Hasher.hash("there"						)
  							self.assertEqual(snake_case			,  snake_case						)
  							self.assertNotEqual(snake_case			,  snake_case						)
  
  
  
  
  
  
  	def 			_a(self							:   Dict						)   -> int:
  							_lowercase					:  Union[str, Any]    			=      Hasher.hash(Foo("hello"						)						)
  							_lowercase					:  List[Any]    			=      Hasher.hash(Foo("hello"						)						)
  							_lowercase					:  Dict    			=      Hasher.hash(Foo("there"						)						)
  							self.assertEqual(snake_case			,  snake_case						)
  							self.assertNotEqual(snake_case			,  snake_case						)
  
  
  
  
  
  
  	def 			_a(self							:   int						)   -> Optional[int]:
  							_lowercase					:  int    			=      Hasher()
  							for x in ["hello", Foo("hello"						)]:
  													hasher.update(snake_case						)
  							_lowercase					:  Union[str, Any]    			=      hasher.hexdigest()
  							_lowercase					:  Any    			=      Hasher()
  							for x in ["hello", Foo("hello"						)]:
  													hasher.update(snake_case						)
  							_lowercase					:  Union[str, Any]    			=      hasher.hexdigest()
  							_lowercase					:  Optional[Any]    			=      Hasher()
  							for x in ["there", Foo("there"						)]:
  													hasher.update(snake_case						)
  							_lowercase					:  List[str]    			=      hasher.hexdigest()
  							self.assertEqual(snake_case			,  snake_case						)
  							self.assertNotEqual(snake_case			,  snake_case						)
  
  
  
  
  
  
  	def 			_a(self							:   str						)   -> Optional[int]:
  							with self.assertRaises(pickle.PicklingError						):
  													Hasher.hash(UnpicklableCallable(Foo("hello"						)						)						)
  
  
  
  
  
  
  	def 			_a(self							:   Optional[Any]						)   -> Any:
  							_lowercase					:  int    			=      "abc"
  							_lowercase					:  Tuple    			=      [string, string]  # two strings have the same ids
  							_lowercase					:  Any    			=      [string, string]
  							_lowercase					:  Optional[Any]    			=      json.loads(F"""[\"{string}\", \"{string}\"]"""						)  # two strings have different ids
  							self.assertIs(obja[0]			,  snake_case						)
  							self.assertIs(obja[0]			,  obja[1]						)
  							self.assertIs(obja[0]			,  snake_case						)
  							self.assertIs(obja[0]			,  obja[1]						)
  							self.assertIsNot(obja[0]			,  snake_case						)
  							self.assertIsNot(obja[0]			,  obja[1]						)
  							_lowercase					:  Optional[int]    			=      Hasher.hash(snake_case						)
  							_lowercase					:  List[str]    			=      Hasher.hash(snake_case						)
  							_lowercase					:  Optional[int]    			=      Hasher.hash(snake_case						)
  							self.assertEqual(snake_case			,  snake_case						)
  							self.assertEqual(snake_case			,  snake_case						)
  
  
  
  
  
  
  	@require_tiktoken
  	def 			_a(self							:   Any						)   -> Dict:
  							import tiktoken
  
  							_lowercase					:  Dict    			=      tiktoken.get_encoding("gpt2"						)
  							_lowercase					:  List[str]    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							_lowercase					:  Optional[int]    			=      tiktoken.get_encoding("r50k_base"						)
  							_lowercase					:  Union[str, Any]    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							_lowercase					:  Optional[int]    			=      tiktoken.get_encoding("gpt2"						)
  							_lowercase					:  Union[str, Any]    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							self.assertEqual(snake_case			,  snake_case						)
  							self.assertNotEqual(snake_case			,  snake_case						)
  
  
  
  
  
  
  	@require_torch
  	def 			_a(self							:   Optional[Any]						)   -> List[Any]:
  							import torch
  
  							_lowercase					:  Optional[Any]    			=      torch.tensor([1.0]						)
  							_lowercase					:  str    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							_lowercase					:  Optional[Any]    			=      torch.tensor([2.0]						)
  							_lowercase					:  List[str]    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							_lowercase					:  List[str]    			=      torch.tensor([1.0]						)
  							_lowercase					:  Union[str, Any]    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							self.assertEqual(snake_case			,  snake_case						)
  							self.assertNotEqual(snake_case			,  snake_case						)
  
  
  
  
  
  
  	@require_spacy
  	@require_spacy_model("en_core_web_sm"						)
  	@require_spacy_model("fr_core_news_sm"						)
  	@pytest.mark.integration
  	def 			_a(self							:   int						)   -> Optional[Any]:
  							import spacy
  
  							_lowercase					:  str    			=      spacy.load("en_core_web_sm"						)
  							_lowercase					:  Tuple    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							_lowercase					:  Dict    			=      spacy.load("fr_core_news_sm"						)
  							_lowercase					:  int    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							_lowercase					:  List[Any]    			=      spacy.load("en_core_web_sm"						)
  							_lowercase					:  int    			=      mda(datasets.utils.py_utils.dumps(snake_case						)						).hexdigest()
  							self.assertEqual(snake_case			,  snake_case						)
  							self.assertNotEqual(snake_case			,  snake_case						)
  
  
  
  @pytest.mark.integration
  def 		UpperCamelCase    (					_UpperCAmelCase     :      Path     )      -> str:
  
  
  
  
  
  
  						'''simple docstring'''
  
  						_lowercase					:  str    			=      tmp_path / "dir1"
  						_lowercase					:  Union[str, Any]    			=      tmp_path / "dir2"
  						dira.mkdir()
  						dira.mkdir()
  						_lowercase					:  Union[str, Any]    			=      "script.py"
  						_lowercase					:  Optional[Any]    			=      dedent(
  						    "\n    from datasets.fingerprint import Hasher\n    def foo():\n        pass\n    print(Hasher.hash(foo))\n    "     )
  						_lowercase					:  Tuple    			=      dira / script_filename
  						_lowercase					:  Dict    			=      dira / script_filename
  						with script_patha.open("w"     ) as f:
  												f.write(_UpperCAmelCase     )
  						with script_patha.open("w"     ) as f:
  												f.write(_UpperCAmelCase     )
  						_lowercase					:  Optional[int]    			=      subprocess.check_output(["python", str(_UpperCAmelCase     )]     )
  						_lowercase					:  Tuple    			=      subprocess.check_output(["python", str(_UpperCAmelCase     )]     )
  						assert fingerprinta == fingerprinta
  
  
  
  def 		UpperCamelCase    (					)      -> Optional[Any]:
  
  
  
  
  
  
  						'''simple docstring'''
  
  						_lowercase					:  str    			=      {"a": [0, 1, 2]}
  						_lowercase					:  Any    			=      DatasetChild(InMemoryTable.from_pydict(_UpperCAmelCase     )     )
  						_lowercase					:  List[str]    			=      dataset.funca()._fingerprint
  						assert expected_fingerprint == dataset.funca()._fingerprint
  						assert expected_fingerprint != dataset.funca()._fingerprint
  
  						with Pool(2     ) as p:
  												assert expected_fingerprint == p.apply_async(dataset.funca     ).get()._fingerprint
  												assert expected_fingerprint != p.apply_async(dataset.funca     ).get()._fingerprint
  
  
  
  def 		UpperCamelCase    (					)      -> int:
  
  
  
  
  
  
  						'''simple docstring'''
  
  						_lowercase					:  str    			=      {"a": [0, 1, 2]}
  
  
  
  
  						class      __lowercase	(	datasets.Dataset					):
  
  
  
  
  
  
  							@fingerprint_transform(inplace=snake_case						)
  							def 			_a(self							:   List[str]			,  snake_case							:   Dict						)   -> Optional[Any]:
  													return DummyDatasetChild(self.data			,  fingerprint=snake_case						)
  
  
  						_lowercase					:  Dict    			=      DummyDatasetChild(InMemoryTable.from_pydict(_UpperCAmelCase     )     ).func()
  
  
  
  
  						class      __lowercase	(	datasets.Dataset					):
  
  
  
  
  
  
  							@fingerprint_transform(inplace=snake_case			,  version="1.0.0"						)
  							def 			_a(self							:   Dict			,  snake_case							:   Optional[int]						)   -> List[Any]:
  													return DummyDatasetChild(self.data			,  fingerprint=snake_case						)
  
  
  						_lowercase					:  Optional[Any]    			=      DummyDatasetChild(InMemoryTable.from_pydict(_UpperCAmelCase     )     ).func()
  
  
  
  
  						class      __lowercase	(	datasets.Dataset					):
  
  
  
  
  
  
  							@fingerprint_transform(inplace=snake_case			,  version="2.0.0"						)
  							def 			_a(self							:   str			,  snake_case							:   Dict						)   -> Optional[int]:
  													return DummyDatasetChild(self.data			,  fingerprint=snake_case						)
  
  
  						_lowercase					:  List[Any]    			=      DummyDatasetChild(InMemoryTable.from_pydict(_UpperCAmelCase     )     ).func()
  
  						assert len({fingeprint_no_version, fingeprint_a, fingeprint_a}     ) == 3
  
  
  
  def 		UpperCamelCase    (					)      -> List[Any]:
  
  
  
  
  
  
  						'''simple docstring'''
  
  						_lowercase					:  Dict    			=      Hasher()
  						hasher.update(lambda _UpperCAmelCase     : x     )
  
  
  
  
  
  
style_context: 
  import unittest
  
  from transformers import ElectraConfig, is_torch_available
  from transformers.models.auto import get_values
  from transformers.testing_utils import require_torch, slow, torch_device
  
  from ...test_configuration_common import ConfigTester
  from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
  from ...test_pipeline_mixin import PipelineTesterMixin
  
  
  if is_torch_available():
  							import torch
  
  							from transformers import (
  							    MODEL_FOR_PRETRAINING_MAPPING,
  							    ElectraForCausalLM,
  							    ElectraForMaskedLM,
  							    ElectraForMultipleChoice,
  							    ElectraForPreTraining,
  							    ElectraForQuestionAnswering,
  							    ElectraForSequenceClassification,
  							    ElectraForTokenClassification,
  							    ElectraModel,
  							)
  							from transformers.models.electra.modeling_electra import ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST
  
  
  
  
  class      __lowercase	:
  
  
  
  
  
  
  	def __init__(self							:   List[Any]			,  snake_case							:   Dict			,  snake_case							:   Optional[Any]=13			,  snake_case							:   Optional[Any]=7			,  snake_case							:   Optional[int]=True			,  snake_case							:   str=True			,  snake_case							:   Dict=True			,  snake_case							:   str=True			,  snake_case							:   int=99			,  snake_case							:   int=32			,  snake_case							:   Union[str, Any]=5			,  snake_case							:   Any=4			,  snake_case							:   Tuple=37			,  snake_case							:   Tuple="gelu"			,  snake_case							:   Tuple=0.1			,  snake_case							:   Optional[int]=0.1			,  snake_case							:   Optional[Any]=512			,  snake_case							:   Dict=16			,  snake_case							:   Optional[int]=2			,  snake_case							:   int=0.02			,  snake_case							:   int=3			,  snake_case							:   str=4			,  snake_case							:   Optional[Any]=None			,  )   -> List[Any]:
  							_lowercase					:  Dict    			=      parent
  							_lowercase					:  List[str]    			=      batch_size
  							_lowercase					:  int    			=      seq_length
  							_lowercase					:  Optional[Any]    			=      is_training
  							_lowercase					:  Tuple    			=      use_input_mask
  							_lowercase					:  Optional[int]    			=      use_token_type_ids
  							_lowercase					:  Optional[int]    			=      use_labels
  							_lowercase					:  List[Any]    			=      vocab_size
  							_lowercase					:  Union[str, Any]    			=      hidden_size
  							_lowercase					:  Dict    			=      num_hidden_layers
  							_lowercase					:  str    			=      num_attention_heads
  							_lowercase					:  int    			=      intermediate_size
  							_lowercase					:  Union[str, Any]    			=      hidden_act
  							_lowercase					:  Tuple    			=      hidden_dropout_prob
  							_lowercase					:  Union[str, Any]    			=      attention_probs_dropout_prob
  							_lowercase					:  Optional[Any]    			=      max_position_embeddings
  							_lowercase					:  str    			=      type_vocab_size
  							_lowercase					:  Dict    			=      type_sequence_label_size
  							_lowercase					:  List[Any]    			=      initializer_range
  							_lowercase					:  List[str]    			=      num_labels
  							_lowercase					:  Any    			=      num_choices
  							_lowercase					:  Any    			=      scope
  
  
  
  
  
  
  	def 			_a(self							:   Tuple						)   -> Tuple:
  							_lowercase					:  List[str]    			=      ids_tensor([self.batch_size, self.seq_length]			,  self.vocab_size						)
  
  							_lowercase					:  Dict    			=      None
  							if self.use_input_mask:
  													_lowercase					:  List[str]    			=      random_attention_mask([self.batch_size, self.seq_length]						)
  
  							_lowercase					:  Optional[Any]    			=      None
  							if self.use_token_type_ids:
  													_lowercase					:  Tuple    			=      ids_tensor([self.batch_size, self.seq_length]			,  self.type_vocab_size						)
  
  							_lowercase					:  str    			=      None
  							_lowercase					:  Optional[int]    			=      None
  							_lowercase					:  int    			=      None
  							if self.use_labels:
  													_lowercase					:  str    			=      ids_tensor([self.batch_size]			,  self.type_sequence_label_size						)
  													_lowercase					:  Any    			=      ids_tensor([self.batch_size, self.seq_length]			,  self.num_labels						)
  													_lowercase					:  Any    			=      ids_tensor([self.batch_size]			,  self.num_choices						)
  													_lowercase					:  Optional[Any]    			=      ids_tensor([self.batch_size, self.seq_length]			,  1						)
  
  							_lowercase					:  Optional[Any]    			=      self.get_config()
  
  							return (
  							    config,
  							    input_ids,
  							    token_type_ids,
  							    input_mask,
  							    sequence_labels,
  							    token_labels,
  							    choice_labels,
  							    fake_token_labels,
  							)
  
  
  
  
  
  
  	def 			_a(self							:   int						)   -> Optional[Any]:
  							return ElectraConfig(
  							    vocab_size=self.vocab_size			,  hidden_size=self.hidden_size			,  num_hidden_layers=self.num_hidden_layers			,  num_attention_heads=self.num_attention_heads			,  intermediate_size=self.intermediate_size			,  hidden_act=self.hidden_act			,  hidden_dropout_prob=self.hidden_dropout_prob			,  attention_probs_dropout_prob=self.attention_probs_dropout_prob			,  max_position_embeddings=self.max_position_embeddings			,  type_vocab_size=self.type_vocab_size			,  is_decoder=snake_case			,  initializer_range=self.initializer_range			,  )
  
  
  
  
  
  
  	def 			_a(self							:   List[str]						)   -> Optional[Any]:
  							(
  							    (
  							    _lowercase
  							)			,						(
  							    _lowercase
  							)			,						(
  							    _lowercase
  							)			,						(
  							    _lowercase
  							)			,						(
  							    _lowercase
  							)			,						(
  							    _lowercase
  							)			,						(
  							    _lowercase
  							)			,						(
  							    _lowercase
  							)			,						
  							)					:  Any    			=      self.prepare_config_and_inputs()
  
  							_lowercase					:  Union[str, Any]    			=      True
  							_lowercase					:  Dict    			=      floats_tensor([self.batch_size, self.seq_length, self.hidden_size]						)
  							_lowercase					:  Tuple    			=      ids_tensor([self.batch_size, self.seq_length]			,  vocab_size=2						)
  
  							return (
  							    config,
  							    input_ids,
  							    token_type_ids,
  							    input_mask,
  							    sequence_labels,
  							    token_labels,
  							    choice_labels,
  							    encoder_hidden_states,
  							    encoder_attention_mask,
  							)
  
  
  
  
  
  
  	def 			_a(self							:   Optional[Any]			,  snake_case							:   Optional[int]			,  snake_case							:   Dict			,  snake_case							:   Dict			,  snake_case							:   Union[str, Any]			,  snake_case							:   str			,  snake_case							:   List[Any]			,  snake_case							:   Optional[int]			,  snake_case							:   Tuple			,  )   -> str:
  							_lowercase					:  Tuple    			=      ElectraModel(config=snake_case						)
  							model.to(snake_case						)
  							model.eval()
  							_lowercase					:  str    			=      model(snake_case			,  attention_mask=snake_case			,  token_type_ids=snake_case						)
  							_lowercase					:  Optional[Any]    			=      model(snake_case			,  token_type_ids=snake_case						)
  							_lowercase					:  Optional[int]    			=      model(snake_case						)
  							self.parent.assertEqual(result.last_hidden_state.shape			,  (self.batch_size, self.seq_length, self.hidden_size)						)
  
  
  
  
  
  
  	def 			_a(self							:   List[str]			,  snake_case							:   int			,  snake_case							:   Dict			,  snake_case							:   Any			,  snake_case							:   int			,  snake_case							:   Optional[Any]			,  snake_case							:   Optional[int]			,  snake_case							:   str			,  snake_case							:   Optional[Any]			,  snake_case							:   Optional[int]			,  )   -> Dict:
  							_lowercase					:  Tuple    			=      True
  							_lowercase					:  Optional[int]    			=      ElectraModel(snake_case						)
  							model.to(snake_case						)
  							model.eval()
  							_lowercase					:  int    			=      model(
  							    snake_case			,  attention_mask=snake_case			,  token_type_ids=snake_case			,  encoder_hidden_states=snake_case			,  encoder_attention_mask=snake_case			,  )
  							_lowercase					:  Optional[Any]    			=      model(
  							    snake_case			,  attention_mask=snake_case			,  token_type_ids=snake_case			,  encoder_hidden_states=snake_case			,  )
  							_lowercase					:  int    			=      model(snake_case			,  attention_mask=snake_case			,  token_type_ids=snake_case						)
  							self.parent.assertEqual(result.last_hidden_state.shape			,  (self.batch_size, self.seq_length, self.hidden_size)						)
  
  
  
  
  
  
  	def 			_a(self							:   Tuple			,  snake_case							:   int			,  snake_case							:   Tuple			,  snake_case							:   List[Any]			,  snake_case							:   Union[str, Any]			,  snake_case							:   str			,  snake_case							:   Dict			,  snake_case							:   Dict			,  snake_case							:   Optional[Any]			,  )   -> Any:
  							_lowercase					:  Dict    			=      ElectraForMaskedLM(config=snake_case						)
  							model.to(snake_case						)
  							model.eval()
  							_lowercase					:  Optional[int]    			=      model(snake_case			,  attention_mask=snake_case			,  token_type_ids=snake_case			,  labels=snake_case						)
  							self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.seq_length, self.vocab_size)						)
  
  
  
  
  
  
  	def 			_a(self							:   Optional[Any]			,  snake_case							:   Dict			,  snake_case							:   Optional[int]			,  snake_case							:   Any			,  snake_case							:   List[Any]			,  snake_case							:   Optional[Any]			,  snake_case							:   List[str]			,  snake_case							:   Dict			,  snake_case							:   str			,  snake_case							:   str			,  )   -> Any:
  							_lowercase					:  int    			=      ElectraForCausalLM(config=snake_case						)
  							model.to(snake_case						)
  							model.eval()
  							_lowercase					:  Union[str, Any]    			=      model(snake_case			,  attention_mask=snake_case			,  token_type_ids=snake_case			,  labels=snake_case						)
  							self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.seq_length, self.vocab_size)						)
  
  
  
  
  
  
  	def 			_a(self							:   Dict			,  snake_case							:   Union[str, Any]			,  snake_case							:   List[str]			,  snake_case							:   Any			,  snake_case							:   Dict			,  snake_case							:   Tuple			,  snake_case							:   Union[str, Any]			,  snake_case							:   Dict			,  snake_case							:   Optional[int]			,  )   -> Tuple:
  							_lowercase					:  List[Any]    			=      self.num_labels
  							_lowercase					:  Dict    			=      ElectraForTokenClassification(config=snake_case						)
  							model.to(snake_case						)
  							model.eval()
  							_lowercase					:  Tuple    			=      model(snake_case			,  attention_mask=snake_case			,  token_type_ids=snake_case			,  labels=snake_case						)
  							self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.seq_length, self.num_labels)						)
  
  
  
  
  
  
  	def 			_a(self							:   str			,  snake_case							:   Union[str, Any]			,  snake_case							:   Optional[Any]			,  snake_case							:   Dict			,  snake_case							:   Dict			,  snake_case							:   str			,  snake_case							:   int			,  snake_case							:   int			,  snake_case							:   Union[str, Any]			,  )   -> Union[str, Any]:
  							_lowercase					:  List[Any]    			=      self.num_labels
  							_lowercase					:  int    			=      ElectraForPreTraining(config=snake_case						)
  							model.to(snake_case						)
  							model.eval()
  							_lowercase					:  List[str]    			=      model(snake_case			,  attention_mask=snake_case			,  token_type_ids=snake_case			,  labels=snake_case						)
  							self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.seq_length)						)
  
  
  
  
  
  
  	def 			_a(self							:   List[Any]			,  snake_case							:   Optional[Any]			,  snake_case							:   Tuple			,  snake_case							:   Optional[int]			,  snake_case							:   str			,  snake_case							:   Tuple			,  snake_case							:   Union[str, Any]			,  snake_case							:   Union[str, Any]			,  snake_case							:   Dict			,  )   -> List[Any]:
  							_lowercase					:  List[str]    			=      self.num_labels
  							_lowercase					:  Tuple    			=      ElectraForSequenceClassification(snake_case						)
  							model.to(snake_case						)
  							model.eval()
  							_lowercase					:  Any    			=      model(snake_case			,  attention_mask=snake_case			,  token_type_ids=snake_case			,  labels=snake_case						)
  							self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.num_labels)						)
  
  
  
  
  
  
  	def 			_a(self							:   Tuple			,  snake_case							:   Optional[int]			,  snake_case							:   List[Any]			,  snake_case							:   List[str]			,  snake_case							:   Any			,  snake_case							:   Optional[int]			,  snake_case							:   Tuple			,  snake_case							:   Union[str, Any]			,  snake_case							:   str			,  )   -> str:
  							_lowercase					:  int    			=      ElectraForQuestionAnswering(config=snake_case						)
  							model.to(snake_case						)
  							model.eval()
  							_lowercase					:  Tuple    			=      model(
  							    snake_case			,  attention_mask=snake_case			,  token_type_ids=snake_case			,  start_positions=snake_case			,  end_positions=snake_case			,  )
  							self.parent.assertEqual(result.start_logits.shape			,  (self.batch_size, self.seq_length)						)
  							self.parent.assertEqual(result.end_logits.shape			,  (self.batch_size, self.seq_length)						)
  
  
  
  
  
  
  	def 			_a(self							:   Dict			,  snake_case							:   str			,  snake_case							:   Dict			,  snake_case							:   Any			,  snake_case							:   Tuple			,  snake_case							:   str			,  snake_case							:   Optional[Any]			,  snake_case							:   str			,  snake_case							:   Tuple			,  )   -> Union[str, Any]:
  							_lowercase					:  List[Any]    			=      self.num_choices
  							_lowercase					:  Optional[Any]    			=      ElectraForMultipleChoice(config=snake_case						)
  							model.to(snake_case						)
  							model.eval()
  							_lowercase					:  str    			=      input_ids.unsqueeze(1						).expand(-1			,  self.num_choices			,  -1						).contiguous()
  							_lowercase					:  Dict    			=      token_type_ids.unsqueeze(1						).expand(-1			,  self.num_choices			,  -1						).contiguous()
  							_lowercase					:  Optional[Any]    			=      input_mask.unsqueeze(1						).expand(-1			,  self.num_choices			,  -1						).contiguous()
  							_lowercase					:  Optional[int]    			=      model(
  							    snake_case			,  attention_mask=snake_case			,  token_type_ids=snake_case			,  labels=snake_case			,  )
  							self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.num_choices)						)
  
  
  
  
  
  
  	def 			_a(self							:   Optional[Any]						)   -> Union[str, Any]:
  							_lowercase					:  Any    			=      self.prepare_config_and_inputs()
  							(
  							    (
  							    _lowercase
  							)			,						(
  							    _lowercase
  							)			,						(
  							    _lowercase
  							)			,						(
  							    _lowercase
  							)			,						(
  							    _lowercase
  							)			,						(
  							    _lowercase
  							)			,						(
  							    _lowercase
  							)			,						(
  							    _lowercase
  							)			,						
  							)					:  Optional[int]    			=      config_and_inputs
  							_lowercase					:  Optional[Any]    			=      {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
  							return config, inputs_dict
  
  
  
  
  @require_torch
  class      __lowercase	(	__snake_case      ,   __snake_case      ,   unittest.TestCase					):
  	_A   			=      (
  	    (
  	        ElectraModel,
  	        ElectraForPreTraining,
  	        ElectraForMaskedLM,
  	        ElectraForCausalLM,
  	        ElectraForMultipleChoice,
  	        ElectraForTokenClassification,
  	        ElectraForSequenceClassification,
  	        ElectraForQuestionAnswering,
  	    )
  	    if is_torch_available()
  	    else ()
  	)
  	_A   			=      (
  	    {
  	        "feature-extraction": ElectraModel,
  	        "fill-mask": ElectraForMaskedLM,
  	        "question-answering": ElectraForQuestionAnswering,
  	        "text-classification": ElectraForSequenceClassification,
  	        "text-generation": ElectraForCausalLM,
  	        "token-classification": ElectraForTokenClassification,
  	        "zero-shot": ElectraForSequenceClassification,
  	    }
  	    if is_torch_available()
  	    else {}
  	)
  	_A   			=      True
  
  
  
  
  
  
  	def 			_a(self							:   Union[str, Any]			,  snake_case							:   Any			,  snake_case							:   List[Any]			,  snake_case							:   Any=False						)   -> Optional[int]:
  							_lowercase					:  Any    			=      super()._prepare_for_class(snake_case			,  snake_case			,  return_labels=snake_case						)
  
  							if return_labels:
  													if model_class in get_values(snake_case						):
  																			_lowercase					:  List[str]    			=      torch.zeros(
  																			    (self.model_tester.batch_size, self.model_tester.seq_length)			,  dtype=torch.long			,  device=snake_case						)
  							return inputs_dict
  
  
  
  
  
  
  	def 			_a(self							:   List[str]						)   -> List[Any]:
  							_lowercase					:  Tuple    			=      ElectraModelTester(self						)
  							_lowercase					:  int    			=      ConfigTester(self			,  config_class=snake_case			,  hidden_size=37						)
  
  
  
  
  
  
  	def 			_a(self							:   int						)   -> Any:
  							self.config_tester.run_common_tests()
  
  
  
  
  
  
  	def 			_a(self							:   Optional[Any]						)   -> str:
  							_lowercase					:  List[str]    			=      self.model_tester.prepare_config_and_inputs()
  							self.model_tester.create_and_check_electra_model(*snake_case						)
  
  
  
  
  
  
  	def 			_a(self							:   List[Any]						)   -> str:
  							_lowercase					:  Union[str, Any]    			=      self.model_tester.prepare_config_and_inputs_for_decoder()
  							self.model_tester.create_and_check_electra_model_as_decoder(*snake_case						)
  
  
  
  
  
  
  	def 			_a(self							:   Tuple						)   -> Dict:
  							_lowercase					:  str    			=      self.model_tester.prepare_config_and_inputs()
  							for type in ["absolute", "relative_key", "relative_key_query"]:
  													_lowercase					:  List[Any]    			=      type
  													self.model_tester.create_and_check_electra_model(*snake_case						)
  
  
  
  
  
  
  	def 			_a(self							:   Optional[int]						)   -> str:
  							_lowercase					:  int    			=      self.model_tester.prepare_config_and_inputs()
  							self.model_tester.create_and_check_electra_for_masked_lm(*snake_case						)
  
  
  
  
  
  
  	def 			_a(self							:   str						)   -> Dict:
  							_lowercase					:  Optional[Any]    			=      self.model_tester.prepare_config_and_inputs()
  							self.model_tester.create_and_check_electra_for_token_classification(*snake_case						)
  
  
  
  
  
  
  	def 			_a(self							:   Dict						)   -> Optional[Any]:
  							_lowercase					:  Any    			=      self.model_tester.prepare_config_and_inputs()
  							self.model_tester.create_and_check_electra_for_pretraining(*snake_case						)
  
  
  
  
  
  
  	def 			_a(self							:   Union[str, Any]						)   -> Optional[int]:
  							_lowercase					:  Dict    			=      self.model_tester.prepare_config_and_inputs()
  							self.model_tester.create_and_check_electra_for_sequence_classification(*snake_case						)
  
  
  
  
  
  
  	def 			_a(self							:   Any						)   -> Dict:
  							_lowercase					:  Any    			=      self.model_tester.prepare_config_and_inputs()
  							self.model_tester.create_and_check_electra_for_question_answering(*snake_case						)
  
  
  
  
  
  
  	def 			_a(self							:   Tuple						)   -> Tuple:
  							_lowercase					:  Union[str, Any]    			=      self.model_tester.prepare_config_and_inputs()
  							self.model_tester.create_and_check_electra_for_multiple_choice(*snake_case						)
  
  
  
  
  
  
  	@slow
  	def 			_a(self							:   List[str]						)   -> List[Any]:
  							for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
  													_lowercase					:  Optional[int]    			=      ElectraModel.from_pretrained(snake_case						)
  													self.assertIsNotNone(snake_case						)
  
  
  
  
  
  
  	def 			_a(self							:   List[Any]						)   -> List[Any]:
  							_lowercase					:  int    			=      self.model_tester.prepare_config_and_inputs_for_decoder()
  							self.model_tester.create_and_check_electra_for_causal_lm(*snake_case						)
  
  
  
  
  @require_torch
  class      __lowercase	(	unittest.TestCase					):
  
  
  
  
  
  
  	@slow
  	def 			_a(self							:   str						)   -> Optional[int]:
  							_lowercase					:  int    			=      ElectraModel.from_pretrained("google/electra-small-discriminator"						)
  							_lowercase					:  Dict    			=      torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]						)
  							_lowercase					:  Union[str, Any]    			=      torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
  							_lowercase					:  Optional[Any]    			=      model(snake_case			,  attention_mask=snake_case						)[0]
  							_lowercase					:  Union[str, Any]    			=      torch.Size((1, 11, 256)						)
  							self.assertEqual(output.shape			,  snake_case						)
  							_lowercase					:  List[Any]    			=      torch.tensor(
  							    [[[0.44_71, 0.68_21, -0.32_65], [0.46_27, 0.52_55, -0.36_68], [0.45_32, 0.33_13, -0.43_44]]]						)
  
  							self.assertTrue(torch.allclose(output[:, 1:4, 1:4]			,  snake_case			,  atol=1e-4						)						)
  
  
  
  
  