






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 					a_:



     """simple docstring"""



     def __init__(						self			:					int       ,       lowerCAmelCase__			:					Tuple)    ->					Tuple:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						foo





     def __call__(						self			:					Tuple)    ->					str:

      """simple docstring"""
      return self.foo

class 					a_(						datasets.Dataset					):



     """simple docstring"""



     @fingerprint_transform(inplace=lowerCAmelCase__)
     def   __UpperCamelCase		(						self			:					Tuple       ,       lowerCAmelCase__			:					str       ,       *lowerCAmelCase__			:					List[str]       ,       **lowerCAmelCase__			:					str)    ->					int:

      """simple docstring"""
      return DatasetChild(self.data       ,       fingerprint=lowerCAmelCase__)





     @fingerprint_transform(inplace=lowerCAmelCase__)
     def   __UpperCamelCase		(						self			:					int       ,       lowerCAmelCase__			:					Optional[Any]       ,       *lowerCAmelCase__			:					Any       ,       **lowerCAmelCase__			:					Dict)    ->					int:

      """simple docstring"""
      return DatasetChild(self.data       ,       fingerprint=lowerCAmelCase__)

class 					a_:



     """simple docstring"""



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

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						callable





     def __call__(						self			:					str       ,       *lowerCAmelCase__			:					List[str]       ,       **lowerCAmelCase__			:					List[Any])    ->					List[Any]:

      """simple docstring"""
      if self.callable is not None:
       return self.callable(*lowerCAmelCase__       ,       **lowerCAmelCase__)





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

      """simple docstring"""
      raise pickle.PicklingError()

class 					a_(						lowercase__					):



     """simple docstring"""



     @require_transformers
     @pytest.mark.integration
     def   __UpperCamelCase		(						self			:					Any)    ->					int:

      """simple docstring"""
      from transformers import AutoTokenizer

      def encode(lowerCAmelCase__			:					Optional[int]):
       return tokenizer(lowerCAmelCase__)

      # TODO: add hash consistency tests across sessions
      SCREAMING_SNAKE_CASE							  =						AutoTokenizer.from_pretrained('bert-base-uncased')
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lambda lowerCAmelCase__: tokenizer(lowerCAmelCase__))).hexdigest()
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      SCREAMING_SNAKE_CASE							  =						AutoTokenizer.from_pretrained('bert-base-cased')
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lambda lowerCAmelCase__: tokenizer(lowerCAmelCase__))).hexdigest()
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      SCREAMING_SNAKE_CASE							  =						AutoTokenizer.from_pretrained('bert-base-uncased')
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lambda lowerCAmelCase__: tokenizer(lowerCAmelCase__))).hexdigest()
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertNotEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertNotEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertNotEqual(lowerCAmelCase__       ,       lowerCAmelCase__)





     @require_transformers
     @pytest.mark.integration
     def   __UpperCamelCase		(						self			:					Tuple)    ->					int:

      """simple docstring"""
      from transformers import AutoTokenizer

      SCREAMING_SNAKE_CASE							  =						AutoTokenizer.from_pretrained('gpt2')
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      tokenizer('Hello world !')  # call once to change the tokenizer's cache
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)





     @require_regex
     def   __UpperCamelCase		(						self			:					List[Any])    ->					Dict:

      """simple docstring"""
      import regex

      SCREAMING_SNAKE_CASE							  =						regex.Regex('foo')
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      SCREAMING_SNAKE_CASE							  =						regex.Regex('bar')
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      SCREAMING_SNAKE_CASE							  =						regex.Regex('foo')
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertNotEqual(lowerCAmelCase__       ,       lowerCAmelCase__)

class 					a_(						lowercase__					):



     """simple docstring"""



     def   __UpperCamelCase		(						self			:					Optional[Any])    ->					Dict:

      """simple docstring"""
      def func():
       return foo

      SCREAMING_SNAKE_CASE							  =						[0]
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      SCREAMING_SNAKE_CASE							  =						[1]
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      SCREAMING_SNAKE_CASE							  =						[0]
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertNotEqual(lowerCAmelCase__       ,       lowerCAmelCase__)





     def   __UpperCamelCase		(						self			:					Tuple)    ->					Optional[int]:

      """simple docstring"""
      def func():
       pass

      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()

      def func():
       pass

      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)





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

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(Foo([0]))).hexdigest()
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(Foo([1]))).hexdigest()
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(Foo([0]))).hexdigest()
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertNotEqual(lowerCAmelCase__       ,       lowerCAmelCase__)





     def   __UpperCamelCase		(						self			:					Tuple)    ->					Any:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(Foo([0]).__call__)).hexdigest()
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(Foo([1]).__call__)).hexdigest()
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(Foo([0]).__call__)).hexdigest()
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertNotEqual(lowerCAmelCase__       ,       lowerCAmelCase__)





     def   __UpperCamelCase		(						self			:					Dict)    ->					int:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						(
          '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(*lowerCAmelCase__			:					int):
       if hasattr(args[-3]       ,       'encode'):
        SCREAMING_SNAKE_CASE							  =						list(lowerCAmelCase__)
        SCREAMING_SNAKE_CASE							  =						args[-3].encode()  # co_lnotab
        SCREAMING_SNAKE_CASE							  =						args[-1_0].encode()  # co_code
       if hasattr(lowerCAmelCase__       ,       'co_posonlyargcount'):
        if len(lowerCAmelCase__) == 1_6:
         return CodeType(*lowerCAmelCase__)
        elif len(lowerCAmelCase__) == 1_5:
         return CodeType(args[0]       ,       0       ,       *args[1:])
        return CodeType(args[0]       ,       0       ,       0       ,       *args[1:])
       elif hasattr(lowerCAmelCase__       ,       'co_kwonlyargcount'):
        if len(lowerCAmelCase__) == 1_6:
         return CodeType(args[0]       ,       *args[2:])
        elif len(lowerCAmelCase__) == 1_5:
         return CodeType(*lowerCAmelCase__)
        return CodeType(args[0]       ,       0       ,       *args[1:])
       if len(lowerCAmelCase__) == 1_6:
        return CodeType(args[0]       ,       *args[3:])
       elif len(lowerCAmelCase__) == 1_5:
        return CodeType(args[0]       ,       *args[2:])
       return CodeType(*lowerCAmelCase__)

      def create_ipython_func(lowerCAmelCase__			:					Tuple       ,       lowerCAmelCase__			:					Union[str, Any]):
       def func():
        return returned_obj

       SCREAMING_SNAKE_CASE							  =						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'):
        SCREAMING_SNAKE_CASE							  =						_create_code_pyaa(
            *[getattr(lowerCAmelCase__       ,       lowerCAmelCase__) if k != 'co_filename' else co_filename for k in code_args_pyaa])
       else:
        SCREAMING_SNAKE_CASE							  =						code.replace(co_filename=lowerCAmelCase__)
       return FunctionType(lowerCAmelCase__       ,       func.__globals__       ,       func.__name__       ,       func.__defaults__       ,       func.__closure__)

      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						'<ipython-input-2-e0383a102aae>', [0]
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(create_ipython_func(lowerCAmelCase__       ,       lowerCAmelCase__))).hexdigest()
      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						'<ipython-input-2-e0383a102aae>', [1]
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(create_ipython_func(lowerCAmelCase__       ,       lowerCAmelCase__))).hexdigest()
      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						'<ipython-input-5-713f6613acf3>', [0]
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(create_ipython_func(lowerCAmelCase__       ,       lowerCAmelCase__))).hexdigest()
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertNotEqual(lowerCAmelCase__       ,       lowerCAmelCase__)

      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						os.path.join(gettempdir()       ,       'ipykernel_12345'       ,       '321456789.py'), [0]
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(create_ipython_func(lowerCAmelCase__       ,       lowerCAmelCase__))).hexdigest()
      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						os.path.join(gettempdir()       ,       'ipykernel_12345'       ,       '321456789.py'), [1]
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(create_ipython_func(lowerCAmelCase__       ,       lowerCAmelCase__))).hexdigest()
      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						os.path.join(gettempdir()       ,       'ipykernel_12345'       ,       '654123987.py'), [0]
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(create_ipython_func(lowerCAmelCase__       ,       lowerCAmelCase__))).hexdigest()
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertNotEqual(lowerCAmelCase__       ,       lowerCAmelCase__)





     def   __UpperCamelCase		(						self			:					str)    ->					List[str]:

      """simple docstring"""
      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						[0], [1]

      def func():
       return foo, bar

      SCREAMING_SNAKE_CASE							  =						'__main__'

      def globalvars_mocka_side_effect(lowerCAmelCase__			:					Union[str, Any]       ,       *lowerCAmelCase__			:					Optional[Any]       ,       **lowerCAmelCase__			:					Optional[int]):
       return {"foo": foo, "bar": bar}

      def globalvars_mocka_side_effect(lowerCAmelCase__			:					Optional[int]       ,       *lowerCAmelCase__			:					Dict       ,       **lowerCAmelCase__			:					Union[str, Any]):
       return {"bar": bar, "foo": foo}

      with patch('dill.detect.globalvars'       ,       side_effect=lowerCAmelCase__) as globalvars_mocka:
       SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
       self.assertGreater(globalvars_mocka.call_count       ,       0)
      with patch('dill.detect.globalvars'       ,       side_effect=lowerCAmelCase__) as globalvars_mocka:
       SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
       self.assertGreater(globalvars_mocka.call_count       ,       0)
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)

class 					a_(						lowercase__					):



     """simple docstring"""



     def   __UpperCamelCase		(						self			:					Dict)    ->					Tuple:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						Hasher.hash('hello')
      SCREAMING_SNAKE_CASE							  =						Hasher.hash('hello')
      SCREAMING_SNAKE_CASE							  =						Hasher.hash('there')
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertNotEqual(lowerCAmelCase__       ,       lowerCAmelCase__)





     def   __UpperCamelCase		(						self			:					Dict)    ->					str:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						Hasher.hash(Foo('hello'))
      SCREAMING_SNAKE_CASE							  =						Hasher.hash(Foo('hello'))
      SCREAMING_SNAKE_CASE							  =						Hasher.hash(Foo('there'))
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertNotEqual(lowerCAmelCase__       ,       lowerCAmelCase__)





     def   __UpperCamelCase		(						self			:					Optional[int])    ->					List[str]:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						Hasher()
      for x in ["hello", Foo('hello')]:
       hasher.update(lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						hasher.hexdigest()
      SCREAMING_SNAKE_CASE							  =						Hasher()
      for x in ["hello", Foo('hello')]:
       hasher.update(lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						hasher.hexdigest()
      SCREAMING_SNAKE_CASE							  =						Hasher()
      for x in ["there", Foo('there')]:
       hasher.update(lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						hasher.hexdigest()
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertNotEqual(lowerCAmelCase__       ,       lowerCAmelCase__)





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

      """simple docstring"""
      with self.assertRaises(pickle.PicklingError):
       Hasher.hash(UnpicklableCallable(Foo('hello')))





     def   __UpperCamelCase		(						self			:					Any)    ->					List[str]:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						'abc'
      SCREAMING_SNAKE_CASE							  =						[string, string]  # two strings have the same ids
      SCREAMING_SNAKE_CASE							  =						[string, string]
      SCREAMING_SNAKE_CASE							  =						json.loads(f'''["{string}", "{string}"]''')  # two strings have different ids
      self.assertIs(obja[0]       ,       lowerCAmelCase__)
      self.assertIs(obja[0]       ,       obja[1])
      self.assertIs(obja[0]       ,       lowerCAmelCase__)
      self.assertIs(obja[0]       ,       obja[1])
      self.assertIsNot(obja[0]       ,       lowerCAmelCase__)
      self.assertIsNot(obja[0]       ,       obja[1])
      SCREAMING_SNAKE_CASE							  =						Hasher.hash(lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						Hasher.hash(lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						Hasher.hash(lowerCAmelCase__)
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)





     @require_tiktoken
     def   __UpperCamelCase		(						self			:					Any)    ->					Any:

      """simple docstring"""
      import tiktoken

      SCREAMING_SNAKE_CASE							  =						tiktoken.get_encoding('gpt2')
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      SCREAMING_SNAKE_CASE							  =						tiktoken.get_encoding('r50k_base')
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      SCREAMING_SNAKE_CASE							  =						tiktoken.get_encoding('gpt2')
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertNotEqual(lowerCAmelCase__       ,       lowerCAmelCase__)





     @require_torch
     def   __UpperCamelCase		(						self			:					Union[str, Any])    ->					Dict:

      """simple docstring"""
      import torch

      SCREAMING_SNAKE_CASE							  =						torch.tensor([1.0])
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      SCREAMING_SNAKE_CASE							  =						torch.tensor([2.0])
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      SCREAMING_SNAKE_CASE							  =						torch.tensor([1.0])
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertNotEqual(lowerCAmelCase__       ,       lowerCAmelCase__)





     @require_spacy
     @require_spacy_model('en_core_web_sm')
     @require_spacy_model('fr_core_news_sm')
     @pytest.mark.integration
     def   __UpperCamelCase		(						self			:					Any)    ->					Any:

      """simple docstring"""
      import spacy

      SCREAMING_SNAKE_CASE							  =						spacy.load('en_core_web_sm')
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      SCREAMING_SNAKE_CASE							  =						spacy.load('fr_core_news_sm')
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      SCREAMING_SNAKE_CASE							  =						spacy.load('en_core_web_sm')
      SCREAMING_SNAKE_CASE							  =						mda(datasets.utils.py_utils.dumps(lowerCAmelCase__)).hexdigest()
      self.assertEqual(lowerCAmelCase__       ,       lowerCAmelCase__)
      self.assertNotEqual(lowerCAmelCase__       ,       lowerCAmelCase__)





@pytest.mark.integration
def 		A_			(		lowercase_	)							->Optional[int]:







 """simple docstring"""

 SCREAMING_SNAKE_CASE							  =						tmp_path / 'dir1'
 SCREAMING_SNAKE_CASE							  =						tmp_path / 'dir2'
 dira.mkdir()
 dira.mkdir()
 SCREAMING_SNAKE_CASE							  =						'script.py'
 SCREAMING_SNAKE_CASE							  =						dedent(
     '\n    from datasets.fingerprint import Hasher\n    def foo():\n        pass\n    print(Hasher.hash(foo))\n    '	)
 SCREAMING_SNAKE_CASE							  =						dira / script_filename
 SCREAMING_SNAKE_CASE							  =						dira / script_filename
 with script_patha.open('w'	) as f:
  f.write(lowercase_	)
 with script_patha.open('w'	) as f:
  f.write(lowercase_	)
 SCREAMING_SNAKE_CASE							  =						subprocess.check_output(['python', str(lowercase_	)]	)
 SCREAMING_SNAKE_CASE							  =						subprocess.check_output(['python', str(lowercase_	)]	)
 assert fingerprinta == fingerprinta





def 		A_			(		)							->Any:







 """simple docstring"""

 SCREAMING_SNAKE_CASE							  =						{'a': [0, 1, 2]}
 SCREAMING_SNAKE_CASE							  =						DatasetChild(InMemoryTable.from_pydict(lowercase_	)	)
 SCREAMING_SNAKE_CASE							  =						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 		A_			(		)							->Optional[Any]:







 """simple docstring"""

 SCREAMING_SNAKE_CASE							  =						{'a': [0, 1, 2]}
 class 					a_(						datasets.Dataset					):



      """simple docstring"""



      @fingerprint_transform(inplace=lowerCAmelCase__)
      def   __UpperCamelCase		(						self			:					Union[str, Any]       ,       lowerCAmelCase__			:					List[str])    ->					Optional[Any]:

       """simple docstring"""
       return DummyDatasetChild(self.data       ,       fingerprint=lowerCAmelCase__)

 SCREAMING_SNAKE_CASE							  =						DummyDatasetChild(InMemoryTable.from_pydict(lowercase_	)	).func()
 class 					a_(						datasets.Dataset					):



      """simple docstring"""



      @fingerprint_transform(inplace=lowerCAmelCase__       ,       version='1.0.0')
      def   __UpperCamelCase		(						self			:					Any       ,       lowerCAmelCase__			:					str)    ->					int:

       """simple docstring"""
       return DummyDatasetChild(self.data       ,       fingerprint=lowerCAmelCase__)

 SCREAMING_SNAKE_CASE							  =						DummyDatasetChild(InMemoryTable.from_pydict(lowercase_	)	).func()
 class 					a_(						datasets.Dataset					):



      """simple docstring"""



      @fingerprint_transform(inplace=lowerCAmelCase__       ,       version='2.0.0')
      def   __UpperCamelCase		(						self			:					int       ,       lowerCAmelCase__			:					Optional[Any])    ->					Tuple:

       """simple docstring"""
       return DummyDatasetChild(self.data       ,       fingerprint=lowerCAmelCase__)

 SCREAMING_SNAKE_CASE							  =						DummyDatasetChild(InMemoryTable.from_pydict(lowercase_	)	).func()

 assert len({fingeprint_no_version, fingeprint_a, fingeprint_a}	) == 3





def 		A_			(		)							->Optional[Any]:







 """simple docstring"""

 SCREAMING_SNAKE_CASE							  =						Hasher()
 hasher.update(lambda lowercase_	: x	)












import random
from typing import List, Optional, Union

import numpy as np
import torch

from ..state import AcceleratorState
from .constants import CUDA_DISTRIBUTED_TYPES
from .dataclasses import DistributedType, RNGType
from .imports import is_npu_available, is_tpu_available, is_xpu_available


if is_tpu_available(check_device=False):
      import torch_xla.core.xla_model as xm





def 		A_			(		lowercase_					,       lowercase_ = False	)							->Union[str, Any]:







 """simple docstring"""

 if device_specific:
  seed += AcceleratorState().process_index
 random.seed(lowercase_	)
 np.random.seed(lowercase_	)
 torch.manual_seed(lowercase_	)
 if is_xpu_available():
  torch.xpu.manual_seed_all(lowercase_	)
 elif is_npu_available():
  torch.npu.manual_seed_all(lowercase_	)
 else:
  torch.cuda.manual_seed_all(lowercase_	)
 # ^^ safe to call this function even if cuda is not available
 if is_tpu_available():
  xm.set_rng_state(lowercase_	)





def 		A_			(		lowercase_ = None					,       lowercase_ = None	)							->Optional[Any]:







 """simple docstring"""

 if rng_type == RNGType.TORCH:
  SCREAMING_SNAKE_CASE							  =						torch.get_rng_state()
 elif rng_type == RNGType.CUDA:
  SCREAMING_SNAKE_CASE							  =						torch.cuda.get_rng_state()
 elif rng_type == RNGType.XLA:
  assert is_tpu_available(), "Can't synchronize XLA seeds on an environment without TPUs."
  SCREAMING_SNAKE_CASE							  =						torch.tensor(xm.get_rng_state()	)
 elif rng_type == RNGType.NPU:
  assert is_npu_available(), "Can't synchronize NPU seeds on an environment without NPUs."
  SCREAMING_SNAKE_CASE							  =						torch.npu.get_rng_state()
 elif rng_type == RNGType.XPU:
  assert is_xpu_available(), "Can't synchronize XPU seeds on an environment without XPUs."
  SCREAMING_SNAKE_CASE							  =						torch.xpu.get_rng_state()
 elif rng_type == RNGType.GENERATOR:
  assert generator is not None, "Need a generator to synchronize its seed."
  SCREAMING_SNAKE_CASE							  =						generator.get_state()

 # Broadcast the rng state from device 0 to other devices
 SCREAMING_SNAKE_CASE							  =						AcceleratorState()
 if state.distributed_type == DistributedType.TPU:
  SCREAMING_SNAKE_CASE							  =						rng_state.to(xm.xla_device()	)
  xm.collective_broadcast([rng_state]	)
  xm.mark_step()
  SCREAMING_SNAKE_CASE							  =						rng_state.cpu()
 elif (
     state.distributed_type in CUDA_DISTRIBUTED_TYPES
     or state.distributed_type == DistributedType.MULTI_NPU
     or state.distributed_type == DistributedType.MULTI_XPU
 ):
  SCREAMING_SNAKE_CASE							  =						rng_state.to(state.device	)
  torch.distributed.broadcast(lowercase_					,       0	)
  SCREAMING_SNAKE_CASE							  =						rng_state.cpu()
 elif state.distributed_type == DistributedType.MULTI_CPU:
  torch.distributed.broadcast(lowercase_					,       0	)

 # Set the broadcast rng state
 if rng_type == RNGType.TORCH:
  torch.set_rng_state(lowercase_	)
 elif rng_type == RNGType.CUDA:
  torch.cuda.set_rng_state(lowercase_	)
 elif rng_type == RNGType.NPU:
  torch.npu.set_rng_state(lowercase_	)
 elif rng_type == RNGType.XPU:
  torch.xpu.set_rng_state(lowercase_	)
 elif rng_type == RNGType.XLA:
  xm.set_rng_state(rng_state.item()	)
 elif rng_type == RNGType.GENERATOR:
  generator.set_state(lowercase_	)





def 		A_			(		lowercase_					,       lowercase_ = None	)							->str:







 """simple docstring"""

 for rng_type in rng_types:
  synchronize_rng_state(RNGType(lowercase_	)					,       generator=lowercase_	)




