"""simple docstring"""


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					:



  def __init__(		self   :    List[Any]						,  lowerCamelCase_   :    str     )      -> List[Any]:
      __a			      =						foo



  def __call__(		self   :    Union[str, Any]     )      -> Optional[int]:
      return self.foo






class 					a					(				datasets.Dataset							):



  @fingerprint_transform(inplace=lowerCamelCase_     )
  def        lowerCAmelCase_				(		self   :    Dict						,  lowerCamelCase_   :    Optional[int]						,  *lowerCamelCase_   :    Tuple						,  **lowerCamelCase_   :    str     )      -> Union[str, Any]:
      return DatasetChild(self.data						,  fingerprint=lowerCamelCase_     )



  @fingerprint_transform(inplace=lowerCamelCase_     )
  def        lowerCAmelCase_				(		self   :    int						,  lowerCamelCase_   :    Tuple						,  *lowerCamelCase_   :    Optional[int]						,  **lowerCamelCase_   :    Dict     )      -> int:
      return DatasetChild(self.data						,  fingerprint=lowerCamelCase_     )






class 					a					:



  def __init__(		self   :    Any						,  lowerCamelCase_   :    Any     )      -> Tuple:
      __a			      =						callable



  def __call__(		self   :    int						,  *lowerCamelCase_   :    Any						,  **lowerCamelCase_   :    int     )      -> int:
      if self.callable is not None:
          return self.callable(*lowerCamelCase_						,  **lowerCamelCase_     )



  def __getstate__(		self   :    Optional[Any]     )      -> Optional[int]:
      raise pickle.PicklingError()






class 					a					(				A_							):



  @require_transformers
  @pytest.mark.integration
  def        lowerCAmelCase_				(		self   :    Any     )      -> Optional[int]:
      from transformers import AutoTokenizer

      def encode(lowerCamelCase_   :    int     ):
          return tokenizer(lowerCamelCase_     )

      # TODO: add hash consistency tests across sessions
      __a			      =						AutoTokenizer.from_pretrained("""bert-base-uncased"""     )
      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
      __a			      =						mda(datasets.utils.py_utils.dumps(lambda lowerCamelCase_     : tokenizer(lowerCamelCase_     )     )     ).hexdigest()
      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
      __a			      =						AutoTokenizer.from_pretrained("""bert-base-cased"""     )
      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
      __a			      =						mda(datasets.utils.py_utils.dumps(lambda lowerCamelCase_     : tokenizer(lowerCamelCase_     )     )     ).hexdigest()
      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
      __a			      =						AutoTokenizer.from_pretrained("""bert-base-uncased"""     )
      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
      __a			      =						mda(datasets.utils.py_utils.dumps(lambda lowerCamelCase_     : tokenizer(lowerCamelCase_     )     )     ).hexdigest()
      __a			      =						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        lowerCAmelCase_				(		self   :    Optional[int]     )      -> Union[str, Any]:
      from transformers import AutoTokenizer

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



  @require_regex
  def        lowerCAmelCase_				(		self   :    Union[str, Any]     )      -> str:
      import regex

      __a			      =						regex.Regex("""foo"""     )
      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
      __a			      =						regex.Regex("""bar"""     )
      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
      __a			      =						regex.Regex("""foo"""     )
      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
      self.assertEqual(lowerCamelCase_						,  lowerCamelCase_     )
      self.assertNotEqual(lowerCamelCase_						,  lowerCamelCase_     )






class 					a					(				A_							):



  def        lowerCAmelCase_				(		self   :    List[str]     )      -> Dict:
      def func():
          return foo

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



  def        lowerCAmelCase_				(		self   :    str     )      -> Optional[Any]:
      def func():
          pass

      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()

      def func():
          pass

      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
      self.assertEqual(lowerCamelCase_						,  lowerCamelCase_     )



  def        lowerCAmelCase_				(		self   :    List[str]     )      -> List[str]:
      __a			      =						mda(datasets.utils.py_utils.dumps(Foo([0]     )     )     ).hexdigest()
      __a			      =						mda(datasets.utils.py_utils.dumps(Foo([1]     )     )     ).hexdigest()
      __a			      =						mda(datasets.utils.py_utils.dumps(Foo([0]     )     )     ).hexdigest()
      self.assertEqual(lowerCamelCase_						,  lowerCamelCase_     )
      self.assertNotEqual(lowerCamelCase_						,  lowerCamelCase_     )



  def        lowerCAmelCase_				(		self   :    List[str]     )      -> List[str]:
      __a			      =						mda(datasets.utils.py_utils.dumps(Foo([0]     ).__call__     )     ).hexdigest()
      __a			      =						mda(datasets.utils.py_utils.dumps(Foo([1]     ).__call__     )     ).hexdigest()
      __a			      =						mda(datasets.utils.py_utils.dumps(Foo([0]     ).__call__     )     ).hexdigest()
      self.assertEqual(lowerCamelCase_						,  lowerCamelCase_     )
      self.assertNotEqual(lowerCamelCase_						,  lowerCamelCase_     )



  def        lowerCAmelCase_				(		self   :    int     )      -> Union[str, Any]:
      __a			      =						(
          """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_   :    Optional[Any]     ):
          if hasattr(args[-3]						,  """encode"""     ):
              __a			      =						list(lowerCamelCase_     )
              __a			      =						args[-3].encode()  # co_lnotab
              __a			      =						args[-10].encode()  # co_code
          if hasattr(lowerCamelCase_						,  """co_posonlyargcount"""     ):
              if len(lowerCamelCase_     ) == 16:
                  return CodeType(*lowerCamelCase_     )
              elif len(lowerCamelCase_     ) == 15:
                  return CodeType(args[0]						,  0						,  *args[1:]     )
              return CodeType(args[0]						,  0						,  0						,  *args[1:]     )
          elif hasattr(lowerCamelCase_						,  """co_kwonlyargcount"""     ):
              if len(lowerCamelCase_     ) == 16:
                  return CodeType(args[0]						,  *args[2:]     )
              elif len(lowerCamelCase_     ) == 15:
                  return CodeType(*lowerCamelCase_     )
              return CodeType(args[0]						,  0						,  *args[1:]     )
          if len(lowerCamelCase_     ) == 16:
              return CodeType(args[0]						,  *args[3:]     )
          elif len(lowerCamelCase_     ) == 15:
              return CodeType(args[0]						,  *args[2:]     )
          return CodeType(*lowerCamelCase_     )

      def create_ipython_func(lowerCamelCase_   :    Dict						,  lowerCamelCase_   :    int     ):
          def func():
              return returned_obj

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

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

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



  def        lowerCAmelCase_				(		self   :    Union[str, Any]     )      -> Optional[Any]:
      __a      ,  __a			      =						[0], [1]

      def func():
          return foo, bar

      __a			      =						"""__main__"""

      def globalvars_mocka_side_effect(lowerCamelCase_   :    List[Any]						,  *lowerCamelCase_   :    List[Any]						,  **lowerCamelCase_   :    Dict     ):
          return {"foo": foo, "bar": bar}

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

      with patch("""dill.detect.globalvars"""						,  side_effect=lowerCamelCase_     ) as globalvars_mocka:
          __a			      =						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:
          __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
          self.assertGreater(globalvars_mocka.call_count						,  0     )
      self.assertEqual(lowerCamelCase_						,  lowerCamelCase_     )






class 					a					(				A_							):



  def        lowerCAmelCase_				(		self   :    List[Any]     )      -> Optional[Any]:
      __a			      =						Hasher.hash("""hello"""     )
      __a			      =						Hasher.hash("""hello"""     )
      __a			      =						Hasher.hash("""there"""     )
      self.assertEqual(lowerCamelCase_						,  lowerCamelCase_     )
      self.assertNotEqual(lowerCamelCase_						,  lowerCamelCase_     )



  def        lowerCAmelCase_				(		self   :    Tuple     )      -> List[str]:
      __a			      =						Hasher.hash(Foo("""hello"""     )     )
      __a			      =						Hasher.hash(Foo("""hello"""     )     )
      __a			      =						Hasher.hash(Foo("""there"""     )     )
      self.assertEqual(lowerCamelCase_						,  lowerCamelCase_     )
      self.assertNotEqual(lowerCamelCase_						,  lowerCamelCase_     )



  def        lowerCAmelCase_				(		self   :    Tuple     )      -> Tuple:
      __a			      =						Hasher()
      for x in ["hello", Foo("""hello"""     )]:
          hasher.update(lowerCamelCase_     )
      __a			      =						hasher.hexdigest()
      __a			      =						Hasher()
      for x in ["hello", Foo("""hello"""     )]:
          hasher.update(lowerCamelCase_     )
      __a			      =						hasher.hexdigest()
      __a			      =						Hasher()
      for x in ["there", Foo("""there"""     )]:
          hasher.update(lowerCamelCase_     )
      __a			      =						hasher.hexdigest()
      self.assertEqual(lowerCamelCase_						,  lowerCamelCase_     )
      self.assertNotEqual(lowerCamelCase_						,  lowerCamelCase_     )



  def        lowerCAmelCase_				(		self   :    Dict     )      -> List[Any]:
      with self.assertRaises(pickle.PicklingError     ):
          Hasher.hash(UnpicklableCallable(Foo("""hello"""     )     )     )



  def        lowerCAmelCase_				(		self   :    str     )      -> Any:
      __a			      =						"""abc"""
      __a			      =						[string, string]  # two strings have the same ids
      __a			      =						[string, string]
      __a			      =						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]     )
      __a			      =						Hasher.hash(lowerCamelCase_     )
      __a			      =						Hasher.hash(lowerCamelCase_     )
      __a			      =						Hasher.hash(lowerCamelCase_     )
      self.assertEqual(lowerCamelCase_						,  lowerCamelCase_     )
      self.assertEqual(lowerCamelCase_						,  lowerCamelCase_     )



  @require_tiktoken
  def        lowerCAmelCase_				(		self   :    Optional[Any]     )      -> List[Any]:
      import tiktoken

      __a			      =						tiktoken.get_encoding("""gpt2"""     )
      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
      __a			      =						tiktoken.get_encoding("""r50k_base"""     )
      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
      __a			      =						tiktoken.get_encoding("""gpt2"""     )
      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
      self.assertEqual(lowerCamelCase_						,  lowerCamelCase_     )
      self.assertNotEqual(lowerCamelCase_						,  lowerCamelCase_     )



  @require_torch
  def        lowerCAmelCase_				(		self   :    int     )      -> str:
      import torch

      __a			      =						torch.tensor([1.0]     )
      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
      __a			      =						torch.tensor([2.0]     )
      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
      __a			      =						torch.tensor([1.0]     )
      __a			      =						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        lowerCAmelCase_				(		self   :    int     )      -> Dict:
      import spacy

      __a			      =						spacy.load("""en_core_web_sm"""     )
      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
      __a			      =						spacy.load("""fr_core_news_sm"""     )
      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
      __a			      =						spacy.load("""en_core_web_sm"""     )
      __a			      =						mda(datasets.utils.py_utils.dumps(lowerCamelCase_     )     ).hexdigest()
      self.assertEqual(lowerCamelCase_						,  lowerCamelCase_     )
      self.assertNotEqual(lowerCamelCase_						,  lowerCamelCase_     )

@pytest.mark.integration
def 						UpperCamelCase				(	_lowerCAmelCase   :							Path						):
    __a			      =						tmp_path / """dir1"""
    __a			      =						tmp_path / """dir2"""
    dira.mkdir()
    dira.mkdir()
    __a			      =						"""script.py"""
    __a			      =						dedent(
        """
    from datasets.fingerprint import Hasher
    def foo():
        pass
    print(Hasher.hash(foo))
    """						)
    __a			      =						dira / script_filename
    __a			      =						dira / script_filename
    with script_patha.open("""w"""						) as f:
        f.write(_lowerCAmelCase						)
    with script_patha.open("""w"""						) as f:
        f.write(_lowerCAmelCase						)
    __a			      =						subprocess.check_output(["""python""", str(_lowerCAmelCase						)]						)
    __a			      =						subprocess.check_output(["""python""", str(_lowerCAmelCase						)]						)
    assert fingerprinta == fingerprinta

def 						UpperCamelCase				(	):
    __a			      =						{"""a""": [0, 1, 2]}
    __a			      =						DatasetChild(InMemoryTable.from_pydict(_lowerCAmelCase						)						)
    __a			      =						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				(	):
    __a			      =						{"""a""": [0, 1, 2]}






    class 					a					(				datasets.Dataset							):



      @fingerprint_transform(inplace=lowerCamelCase_     )
      def        lowerCAmelCase_				(		self   :    Any						,  lowerCamelCase_   :    List[Any]     )      -> Optional[int]:
          return DummyDatasetChild(self.data						,  fingerprint=lowerCamelCase_     )
    __a			      =						DummyDatasetChild(InMemoryTable.from_pydict(_lowerCAmelCase						)						).func()






    class 					a					(				datasets.Dataset							):



      @fingerprint_transform(inplace=lowerCamelCase_						,  version="""1.0.0"""     )
      def        lowerCAmelCase_				(		self   :    str						,  lowerCamelCase_   :    Union[str, Any]     )      -> Dict:
          return DummyDatasetChild(self.data						,  fingerprint=lowerCamelCase_     )
    __a			      =						DummyDatasetChild(InMemoryTable.from_pydict(_lowerCAmelCase						)						).func()






    class 					a					(				datasets.Dataset							):



      @fingerprint_transform(inplace=lowerCamelCase_						,  version="""2.0.0"""     )
      def        lowerCAmelCase_				(		self   :    int						,  lowerCamelCase_   :    List[str]     )      -> List[str]:
          return DummyDatasetChild(self.data						,  fingerprint=lowerCamelCase_     )
    __a			      =						DummyDatasetChild(InMemoryTable.from_pydict(_lowerCAmelCase						)						).func()

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






def 						UpperCamelCase				(	):
    # AttributeError: module 'dill._dill' has no attribute 'stack'
    __a			      =						Hasher()
    hasher.update(lambda _lowerCAmelCase						: x						)

"""simple docstring"""


import inspect
from typing import Callable, List, Optional, Set, Tuple, Union

import torch
from packaging import version
from safetensors.torch import storage_ptr, storage_size
from torch import nn

from .utils import logging


__A       			=      [nn.LayerNorm]

__A       			=      logging.get_logger(__name__)

__A       			=      version.parse(version.parse(torch.__version__).base_version)

__A       			=      parsed_torch_version_base >= version.parse("""2.0""")
__A       			=      parsed_torch_version_base >= version.parse("""1.12""")
__A       			=      parsed_torch_version_base >= version.parse("""1.11""")
__A       			=      parsed_torch_version_base < version.parse("""1.11""")

def 						UpperCamelCase				(	_lowerCAmelCase   :							Tuple				, _lowerCAmelCase   :							List[str]				, _lowerCAmelCase   :							Union[str, Any]				, _lowerCAmelCase   :							int				, self   :							Optional[Any]						):
    from torch import _softmax_backward_data

    if is_torch_less_than_1_11:
        return _softmax_backward_data(_lowerCAmelCase				, _lowerCAmelCase				, parent.dim				, self						)
    else:
        return _softmax_backward_data(_lowerCAmelCase				, _lowerCAmelCase				, parent.dim				, self.dtype						)

def 						UpperCamelCase				(	_lowerCAmelCase   :							nn.Linear				, _lowerCAmelCase   :							torch.LongTensor				, _lowerCAmelCase   :							int = 0						):
    __a			      =						index.to(layer.weight.device						)
    __a			      =						layer.weight.index_select(_lowerCAmelCase				, _lowerCAmelCase						).clone().detach()
    if layer.bias is not None:
        if dim == 1:
            __a			      =						layer.bias.clone().detach()
        else:
            __a			      =						layer.bias[index].clone().detach()
    __a			      =						list(layer.weight.size()						)
    __a			      =						len(_lowerCAmelCase						)
    __a			      =						nn.Linear(new_size[1]				, new_size[0]				, bias=layer.bias is not None						).to(layer.weight.device						)
    __a			      =						False
    new_layer.weight.copy_(W.contiguous()						)
    __a			      =						True
    if layer.bias is not None:
        __a			      =						False
        new_layer.bias.copy_(b.contiguous()						)
        __a			      =						True
    return new_layer






class 					a					(				nn.Module							):



  def __init__(		self   :    Optional[int]						,  lowerCamelCase_   :    Optional[Any]						,  lowerCamelCase_   :    List[Any]     )      -> List[str]:
      super().__init__()
      __a			      =						nf
      __a			      =						nn.Parameter(torch.empty(lowerCamelCase_						,  lowerCamelCase_     )     )
      __a			      =						nn.Parameter(torch.zeros(lowerCamelCase_     )     )
      nn.init.normal_(self.weight						,  std=0.02     )



  def        lowerCAmelCase_				(		self   :    int						,  lowerCamelCase_   :    Optional[Any]     )      -> Union[str, Any]:
      __a			      =						x.size()[:-1] + (self.nf,)
      __a			      =						torch.addmm(self.bias						,  x.view(-1						,  x.size(-1     )     )						,  self.weight     )
      __a			      =						x.view(lowerCamelCase_     )
      return x

def 						UpperCamelCase				(	_lowerCAmelCase   :							ConvaD				, _lowerCAmelCase   :							torch.LongTensor				, _lowerCAmelCase   :							int = 1						):
    __a			      =						index.to(layer.weight.device						)
    __a			      =						layer.weight.index_select(_lowerCAmelCase				, _lowerCAmelCase						).clone().detach()
    if dim == 0:
        __a			      =						layer.bias.clone().detach()
    else:
        __a			      =						layer.bias[index].clone().detach()
    __a			      =						list(layer.weight.size()						)
    __a			      =						len(_lowerCAmelCase						)
    __a			      =						ConvaD(new_size[1]				, new_size[0]						).to(layer.weight.device						)
    __a			      =						False
    new_layer.weight.copy_(W.contiguous()						)
    __a			      =						True
    __a			      =						False
    new_layer.bias.copy_(b.contiguous()						)
    __a			      =						True
    return new_layer

def 						UpperCamelCase				(	_lowerCAmelCase   :							Union[nn.Linear, ConvaD]				, _lowerCAmelCase   :							torch.LongTensor				, _lowerCAmelCase   :							Optional[int] = None						):
    if isinstance(_lowerCAmelCase				, nn.Linear						):
        return prune_linear_layer(_lowerCAmelCase				, _lowerCAmelCase				, dim=0 if dim is None else dim						)
    elif isinstance(_lowerCAmelCase				, _lowerCAmelCase						):
        return prune_convad_layer(_lowerCAmelCase				, _lowerCAmelCase				, dim=1 if dim is None else dim						)
    else:
        raise ValueError(f"""Can't prune layer of class {layer.__class__}"""						)

def 						UpperCamelCase				(	_lowerCAmelCase   :							Callable[..., torch.Tensor]				, _lowerCAmelCase   :							int				, _lowerCAmelCase   :							int				, *_lowerCAmelCase   :							List[Any]						):
    assert len(_lowerCAmelCase						) > 0, f"""{input_tensors} has to be a tuple/list of tensors"""

    # inspect.signature exist since python 3.5 and is a python method -> no problem with backward compatibility
    __a			      =						len(inspect.signature(_lowerCAmelCase						).parameters						)
    if num_args_in_forward_chunk_fn != len(_lowerCAmelCase						):
        raise ValueError(
            f"""forward_chunk_fn expects {num_args_in_forward_chunk_fn} arguments, but only {len(_lowerCAmelCase						)} input """
            """tensors are given"""						)

    if chunk_size > 0:
        __a			      =						input_tensors[0].shape[chunk_dim]
        for input_tensor in input_tensors:
            if input_tensor.shape[chunk_dim] != tensor_shape:
                raise ValueError(
                    f"""All input tenors have to be of the same shape: {tensor_shape}, """
                    f"""found shape {input_tensor.shape[chunk_dim]}"""						)

        if input_tensors[0].shape[chunk_dim] % chunk_size != 0:
            raise ValueError(
                f"""The dimension to be chunked {input_tensors[0].shape[chunk_dim]} has to be a multiple of the chunk """
                f"""size {chunk_size}"""						)

        __a			      =						input_tensors[0].shape[chunk_dim] // chunk_size

        # chunk input tensor into tuples
        __a			      =						tuple(input_tensor.chunk(_lowerCAmelCase				, dim=_lowerCAmelCase						) for input_tensor in input_tensors						)
        # apply forward fn to every tuple
        __a			      =						tuple(forward_fn(*_lowerCAmelCase						) for input_tensors_chunk in zip(*_lowerCAmelCase						)						)
        # concatenate output at same dimension
        return torch.cat(_lowerCAmelCase				, dim=_lowerCAmelCase						)

    return forward_fn(*_lowerCAmelCase						)

def 						UpperCamelCase				(	_lowerCAmelCase   :							List[int]				, _lowerCAmelCase   :							int				, _lowerCAmelCase   :							int				, _lowerCAmelCase   :							Set[int]						):
    __a			      =						torch.ones(_lowerCAmelCase				, _lowerCAmelCase						)
    __a			      =						set(_lowerCAmelCase						) - already_pruned_heads  # Convert to set and remove already pruned heads
    for head in heads:
        # Compute how many pruned heads are before the head and move the index accordingly
        __a			      =						head - sum(1 if h < head else 0 for h in already_pruned_heads						)
        __a			      =						0
    __a			      =						mask.view(-1						).contiguous().eq(1						)
    __a			      =						torch.arange(len(_lowerCAmelCase						)						)[mask].long()
    return heads, index

def 						UpperCamelCase				(	*_lowerCAmelCase   :							Union[torch.Tensor, List[torch.Tensor]]				, _lowerCAmelCase   :							Optional[str] = None						):
    return torch.meshgrid(*_lowerCAmelCase				, indexing=_lowerCAmelCase						)






def 						UpperCamelCase				(	_lowerCAmelCase   :							torch.Tensor						):
    return tensor.device, storage_ptr(_lowerCAmelCase						), storage_size(_lowerCAmelCase						)
