


import os
import pickle
import unittest
from collections import UserDict, namedtuple

import torch

from accelerate.test_utils.testing import require_cuda, require_torch_min_version
from accelerate.test_utils.training import RegressionModel
from accelerate.utils import (
    convert_outputs_to_fpaa,
    extract_model_from_parallel,
    find_device,
    listify,
    patch_environment,
    recursively_apply,
    send_to_device,
)


__UpperCamelCase							:							Any    							=					namedtuple("ExampleNamedTuple", "a b c")

class    __magic_name__						(  unittest.TestCase):


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





      '''simple docstring'''






      UpperCamelCase__       :     List[Any] 							= torch.randn(5				,						2      )
      UpperCamelCase__       :     Optional[int] 							= torch.device('''cuda'''      ) if torch.cuda.is_available() else torch.device('''cpu'''      )

      UpperCamelCase__       :     Any 							= send_to_device(_SCREAMING_SNAKE_CASE				,						_SCREAMING_SNAKE_CASE      )
      self.assertTrue(torch.equal(resulta.cpu()				,						_SCREAMING_SNAKE_CASE      )      )

      UpperCamelCase__       :     Union[str, Any] 							= send_to_device((tensor, [tensor, tensor], 1)				,						_SCREAMING_SNAKE_CASE      )
      self.assertIsInstance(_SCREAMING_SNAKE_CASE				,						_SCREAMING_SNAKE_CASE      )
      self.assertTrue(torch.equal(resulta[0].cpu()				,						_SCREAMING_SNAKE_CASE      )      )
      self.assertIsInstance(resulta[1]				,						_SCREAMING_SNAKE_CASE      )
      self.assertTrue(torch.equal(resulta[1][0].cpu()				,						_SCREAMING_SNAKE_CASE      )      )
      self.assertTrue(torch.equal(resulta[1][1].cpu()				,						_SCREAMING_SNAKE_CASE      )      )
      self.assertEqual(resulta[2]				,						1      )

      UpperCamelCase__       :     int 							= send_to_device({'''a''': tensor, '''b''': [tensor, tensor], '''c''': 1}				,						_SCREAMING_SNAKE_CASE      )
      self.assertIsInstance(_SCREAMING_SNAKE_CASE				,						_SCREAMING_SNAKE_CASE      )
      self.assertTrue(torch.equal(resulta['''a'''].cpu()				,						_SCREAMING_SNAKE_CASE      )      )
      self.assertIsInstance(resulta['''b''']				,						_SCREAMING_SNAKE_CASE      )
      self.assertTrue(torch.equal(resulta['''b'''][0].cpu()				,						_SCREAMING_SNAKE_CASE      )      )
      self.assertTrue(torch.equal(resulta['''b'''][1].cpu()				,						_SCREAMING_SNAKE_CASE      )      )
      self.assertEqual(resulta['''c''']				,						1      )

      UpperCamelCase__       :     List[str] 							= send_to_device(ExampleNamedTuple(a=_SCREAMING_SNAKE_CASE				,						b=[tensor, tensor]				,						c=1      )				,						_SCREAMING_SNAKE_CASE      )
      self.assertIsInstance(_SCREAMING_SNAKE_CASE				,						_SCREAMING_SNAKE_CASE      )
      self.assertTrue(torch.equal(resulta.a.cpu()				,						_SCREAMING_SNAKE_CASE      )      )
      self.assertIsInstance(resulta.b				,						_SCREAMING_SNAKE_CASE      )
      self.assertTrue(torch.equal(resulta.b[0].cpu()				,						_SCREAMING_SNAKE_CASE      )      )
      self.assertTrue(torch.equal(resulta.b[1].cpu()				,						_SCREAMING_SNAKE_CASE      )      )
      self.assertEqual(resulta.c				,						1      )

      UpperCamelCase__       :     int 							= send_to_device(UserDict({'''a''': tensor, '''b''': [tensor, tensor], '''c''': 1}      )				,						_SCREAMING_SNAKE_CASE      )
      self.assertIsInstance(_SCREAMING_SNAKE_CASE				,						_SCREAMING_SNAKE_CASE      )
      self.assertTrue(torch.equal(resulta['''a'''].cpu()				,						_SCREAMING_SNAKE_CASE      )      )
      self.assertIsInstance(resulta['''b''']				,						_SCREAMING_SNAKE_CASE      )
      self.assertTrue(torch.equal(resulta['''b'''][0].cpu()				,						_SCREAMING_SNAKE_CASE      )      )
      self.assertTrue(torch.equal(resulta['''b'''][1].cpu()				,						_SCREAMING_SNAKE_CASE      )      )
      self.assertEqual(resulta['''c''']				,						1      )


     def   UpperCAmelCase__						(   self				:  Optional[int]      )	->   Dict:





      '''simple docstring'''






      with self.assertRaises(_SCREAMING_SNAKE_CASE      ) as cm:
       UpperCamelCase__       :     Optional[Any] 							= recursively_apply(torch.tensor				,						(torch.tensor(1      ), 1)				,						error_on_other_type=_SCREAMING_SNAKE_CASE      )
      self.assertEqual(
          str(cm.exception      )				,						'''Unsupported types (<class \'int\'>) passed to `tensor`. Only nested list/tuple/dicts of objects that are valid for `is_torch_tensor` should be passed.'''				,						)


     def   UpperCAmelCase__						(   self				:  str      )	->   Tuple:





      '''simple docstring'''






      UpperCamelCase__       :     Union[str, Any] 							= torch.tensor([1, 2, 3, 4, 5]      )
      self.assertEqual(listify(_SCREAMING_SNAKE_CASE      )				,						[1, 2, 3, 4, 5]      )

      UpperCamelCase__       :     str 							= torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]      )
      self.assertEqual(listify(_SCREAMING_SNAKE_CASE      )				,						[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]      )

      UpperCamelCase__       :     Tuple 							= torch.tensor([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]      )
      self.assertEqual(
          listify(_SCREAMING_SNAKE_CASE      )				,						[[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]      )


     def   UpperCAmelCase__						(   self				:  Dict      )	->   Any:





      '''simple docstring'''






      with patch_environment(aa=1				,						BB=2      ):
       self.assertEqual(os.environ.get('''AA'''      )				,						'''1'''      )
       self.assertEqual(os.environ.get('''BB'''      )				,						'''2'''      )

      self.assertNotIn('''AA'''				,						os.environ      )
      self.assertNotIn('''BB'''				,						os.environ      )


     def   UpperCAmelCase__						(   self				:  List[str]      )	->   Dict:





      '''simple docstring'''






      UpperCamelCase__       :     int 							= RegressionModel()
      UpperCamelCase__       :     List[str] 							= model.forward
      UpperCamelCase__       :     Any 							= convert_outputs_to_fpaa(model.forward      )
      UpperCamelCase__       :     Optional[Any] 							= extract_model_from_parallel(_SCREAMING_SNAKE_CASE				,						keep_fpaa_wrapper=_SCREAMING_SNAKE_CASE      )
      UpperCamelCase__       :     Dict 							= pickle.dumps(_SCREAMING_SNAKE_CASE      )


     @require_cuda
     def   UpperCAmelCase__						(   self				:  Any      )	->   Optional[Any]:





      '''simple docstring'''






      UpperCamelCase__       :     Tuple 							= RegressionModel()
      UpperCamelCase__       :     Union[str, Any] 							= model.forward
      UpperCamelCase__       :     int 							= torch.cuda.amp.autocast(dtype=torch.floataa      )(model.forward      )
      UpperCamelCase__       :     Union[str, Any] 							= convert_outputs_to_fpaa(model.forward      )
      UpperCamelCase__       :     str 							= extract_model_from_parallel(_SCREAMING_SNAKE_CASE				,						keep_fpaa_wrapper=_SCREAMING_SNAKE_CASE      )
      UpperCamelCase__       :     Optional[int] 							= pickle.dumps(_SCREAMING_SNAKE_CASE      )


     @require_cuda
     @require_torch_min_version(version='''2.0'''      )
     def   UpperCAmelCase__						(   self				:  Any      )	->   List[str]:





      '''simple docstring'''






      UpperCamelCase__       :     int 							= RegressionModel()
      UpperCamelCase__       :     Union[str, Any] 							= model.forward
      UpperCamelCase__       :     List[str] 							= torch.cuda.amp.autocast(dtype=torch.floataa      )(model.forward      )
      UpperCamelCase__       :     Optional[Any] 							= convert_outputs_to_fpaa(model.forward      )
      UpperCamelCase__       :     Optional[Any] 							= torch.compile(model.forward				,						backend='''inductor'''      )
      UpperCamelCase__       :     Optional[int] 							= torch.randn(4				,						10      ).cuda()
      UpperCamelCase__       :     Union[str, Any] 							= model(_SCREAMING_SNAKE_CASE      )


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





      '''simple docstring'''






      UpperCamelCase__       :     Dict 							= RegressionModel()
      # could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
      UpperCamelCase__       :     str 							= torch.nn.parallel.DataParallel(_SCREAMING_SNAKE_CASE      )
      UpperCamelCase__       :     List[str] 							= extract_model_from_parallel(_SCREAMING_SNAKE_CASE      )

      self.assertEqual(_SCREAMING_SNAKE_CASE				,						_SCREAMING_SNAKE_CASE      )


     @require_torch_min_version(version='''2.0'''      )
     def   UpperCAmelCase__						(   self				:  Union[str, Any]      )	->   List[Any]:





      '''simple docstring'''






      UpperCamelCase__       :     Tuple 							= RegressionModel()
      UpperCamelCase__       :     Optional[int] 							= torch.compile(_SCREAMING_SNAKE_CASE      )

      # could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
      UpperCamelCase__       :     Tuple 							= torch.nn.parallel.DataParallel(_SCREAMING_SNAKE_CASE      )
      UpperCamelCase__       :     List[Any] 							= torch.compile(_SCREAMING_SNAKE_CASE      )
      UpperCamelCase__       :     Dict 							= extract_model_from_parallel(_SCREAMING_SNAKE_CASE      )

      self.assertEqual(compiled_model._orig_mod				,						compiled_model_unwrapped._orig_mod      )


     def   UpperCAmelCase__						(   self				:  Any      )	->   Any:





      '''simple docstring'''






      self.assertEqual(find_device([1, '''a''', torch.tensor([1, 2, 3]      )]      )				,						torch.device('''cpu'''      )      )
      self.assertEqual(find_device({'''a''': 1, '''b''': torch.tensor([1, 2, 3]      )}      )				,						torch.device('''cpu'''      )      )
      self.assertIsNone(find_device([1, '''a''']      )      )





from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__A	:   List[Any]	=					logging.get_logger(__name__)

__A	:   Tuple	=					{
    'google/electra-small-generator': 'https://huggingface.co/google/electra-small-generator/resolve/main/config.json',
    'google/electra-base-generator': 'https://huggingface.co/google/electra-base-generator/resolve/main/config.json',
    'google/electra-large-generator': 'https://huggingface.co/google/electra-large-generator/resolve/main/config.json',
    'google/electra-small-discriminator': (
        'https://huggingface.co/google/electra-small-discriminator/resolve/main/config.json'
    ),
    'google/electra-base-discriminator': (
        'https://huggingface.co/google/electra-base-discriminator/resolve/main/config.json'
    ),
    'google/electra-large-discriminator': (
        'https://huggingface.co/google/electra-large-discriminator/resolve/main/config.json'
    ),
}


class 			_SCREAMING_SNAKE_CASE      (  lowerCAmelCase__):
						_UpperCamelCase:Tuple	=		"electra"







						def __init__(			self     ,						_SCREAMING_SNAKE_CASE=3_0522     ,						_SCREAMING_SNAKE_CASE=128     ,						_SCREAMING_SNAKE_CASE=256     ,						_SCREAMING_SNAKE_CASE=12     ,						_SCREAMING_SNAKE_CASE=4     ,						_SCREAMING_SNAKE_CASE=1024     ,						_SCREAMING_SNAKE_CASE="gelu"     ,						_SCREAMING_SNAKE_CASE=0.1     ,						_SCREAMING_SNAKE_CASE=0.1     ,						_SCREAMING_SNAKE_CASE=512     ,						_SCREAMING_SNAKE_CASE=2     ,						_SCREAMING_SNAKE_CASE=0.0_2     ,						_SCREAMING_SNAKE_CASE=1E-12     ,						_SCREAMING_SNAKE_CASE="first"     ,						_SCREAMING_SNAKE_CASE=True     ,						_SCREAMING_SNAKE_CASE="gelu"     ,						_SCREAMING_SNAKE_CASE=0.1     ,						_SCREAMING_SNAKE_CASE=0     ,						_SCREAMING_SNAKE_CASE="absolute"     ,						_SCREAMING_SNAKE_CASE=True     ,						_SCREAMING_SNAKE_CASE=None     ,						**_SCREAMING_SNAKE_CASE     ,						)->							Optional[int]:
													super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE     ,						**_SCREAMING_SNAKE_CASE							)

													lowerCamelCase_								=vocab_size
													lowerCamelCase_								=embedding_size
													lowerCamelCase_								=hidden_size
													lowerCamelCase_								=num_hidden_layers
													lowerCamelCase_								=num_attention_heads
													lowerCamelCase_								=intermediate_size
													lowerCamelCase_								=hidden_act
													lowerCamelCase_								=hidden_dropout_prob
													lowerCamelCase_								=attention_probs_dropout_prob
													lowerCamelCase_								=max_position_embeddings
													lowerCamelCase_								=type_vocab_size
													lowerCamelCase_								=initializer_range
													lowerCamelCase_								=layer_norm_eps

													lowerCamelCase_								=summary_type
													lowerCamelCase_								=summary_use_proj
													lowerCamelCase_								=summary_activation
													lowerCamelCase_								=summary_last_dropout
													lowerCamelCase_								=position_embedding_type
													lowerCamelCase_								=use_cache
													lowerCamelCase_								=classifier_dropout


class 			_SCREAMING_SNAKE_CASE      (  lowerCAmelCase__):







						@property
						def        _snake_case				(			self							)->							Mapping[str, Mapping[int, str]]:
													if self.task == "multiple-choice":
																				lowerCamelCase_								={0: """batch""", 1: """choice""", 2: """sequence"""}
													else:
																				lowerCamelCase_								={0: """batch""", 1: """sequence"""}
													return OrderedDict(
													    [
													        ("""input_ids""", dynamic_axis),
													        ("""attention_mask""", dynamic_axis),
													        ("""token_type_ids""", dynamic_axis),
													    ]							)
