



'''simple docstring'''

from collections import OrderedDict
from typing import Mapping

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


__lowerCAmelCase						      =       logging.get_logger(__name__)

__lowerCAmelCase						      =       {
    '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/config.json''',
    '''distilbert-base-uncased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/config.json''',
    '''distilbert-base-cased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json''',
    '''distilbert-base-multilingual-cased''': (
        '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json'''
    ),
    '''distilbert-base-uncased-finetuned-sst-2-english''': (
        '''https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json'''
    ),
}

class      __magic_name__						(   _UpperCamelCase       ):
							lowerCAmelCase	:   Optional[int]           = 'distilbert'
							lowerCAmelCase	:   Optional[int]           = {
							    'hidden_size': 'dim',
							    'num_attention_heads': 'n_heads',
							    'num_hidden_layers': 'n_layers',
							}







							def __init__(							self :	str     ,_UpperCAmelCase :	Optional[Any]=30522     ,_UpperCAmelCase :	int=512     ,_UpperCAmelCase :	int=False     ,_UpperCAmelCase :	str=6     ,_UpperCAmelCase :	int=12     ,_UpperCAmelCase :	Union[str, Any]=768     ,_UpperCAmelCase :	Optional[Any]=4 * 768     ,_UpperCAmelCase :	int=0.1     ,_UpperCAmelCase :	Union[str, Any]=0.1     ,_UpperCAmelCase :	Tuple="gelu"     ,_UpperCAmelCase :	Any=0.02     ,_UpperCAmelCase :	List[Any]=0.1     ,_UpperCAmelCase :	Dict=0.2     ,_UpperCAmelCase :	Union[str, Any]=0     ,**_UpperCAmelCase :	List[Any]     ,):
									_a					:				Optional[Any]				=							vocab_size
									_a					:				Dict				=							max_position_embeddings
									_a					:				Optional[int]				=							sinusoidal_pos_embds
									_a					:				List[Any]				=							n_layers
									_a					:				Any				=							n_heads
									_a					:				int				=							dim
									_a					:				Optional[Any]				=							hidden_dim
									_a					:				str				=							dropout
									_a					:				Optional[Any]				=							attention_dropout
									_a					:				Any				=							activation
									_a					:				Tuple				=							initializer_range
									_a					:				Union[str, Any]				=							qa_dropout
									_a					:				List[Any]				=							seq_classif_dropout
									super().__init__(**_UpperCAmelCase     ,pad_token_id=_UpperCAmelCase		)

class      __magic_name__						(   _UpperCamelCase       ):







							@property
							def 							__lowercase      (							self :	Union[str, Any]		):
									if self.task == "multiple-choice":
											_a					:				Union[str, Any]				=							{0: 'batch', 1: 'choice', 2: 'sequence'}
									else:
											_a					:				List[str]				=							{0: 'batch', 1: 'sequence'}
									return OrderedDict(
									    [
									        ('input_ids', dynamic_axis),
									        ('attention_mask', dynamic_axis),
									    ]		)









'''simple docstring'''

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
							import tensorflow as tf
							from tensorflow.python.eager import context
							from tensorflow.python.framework import ops

							from transformers import GradientAccumulator, create_optimizer

@require_tf
class      __magic_name__						(   unittest.TestCase       ):







							def 							__lowercase      (							self :	int     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	str     ,_UpperCAmelCase :	Optional[Any]		):
									self.assertEqual(len(_UpperCAmelCase		)     ,len(_UpperCAmelCase		)		)
									for a, b in zip(_UpperCAmelCase     ,_UpperCAmelCase		):
											self.assertAlmostEqual(_UpperCAmelCase     ,_UpperCAmelCase     ,delta=_UpperCAmelCase		)







							def 							__lowercase      (							self :	int		):
									_a					:				int				=							GradientAccumulator()
									accumulator([tf.constant([1.0, 2.0]		)]		)
									accumulator([tf.constant([-2.0, 1.0]		)]		)
									accumulator([tf.constant([-1.0, 2.0]		)]		)
									with self.assertRaises(_UpperCAmelCase		):
											accumulator([tf.constant([1.0, 1.0]		), tf.constant([2.0, 2.0]		)]		)
									self.assertEqual(accumulator.step     ,3		)
									self.assertEqual(len(accumulator.gradients		)     ,1		)
									self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist()     ,[-2.0, 5.0]     ,tol=1E-2		)
									accumulator.reset()
									self.assertEqual(accumulator.step     ,0		)
									self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist()     ,[0.0, 0.0]     ,tol=1E-2		)







							def 							__lowercase      (							self :	Any		):
									_a					:				int				=							None
									ops.enable_eager_execution_internal()
									_a					:				Optional[int]				=							tf.config.list_physical_devices('CPU'		)
									if len(_UpperCAmelCase		) == 1:
											tf.config.set_logical_device_configuration(
											    physical_devices[0]     ,[tf.config.LogicalDeviceConfiguration(), tf.config.LogicalDeviceConfiguration()]		)
									_a					:				Tuple				=							tf.config.list_logical_devices(device_type='CPU'		)
									_a					:				List[str]				=							tf.distribute.MirroredStrategy(devices=devices[:2]		)

									with strategy.scope():
											_a					:				Tuple				=							GradientAccumulator()
											_a					:				List[Any]				=							tf.Variable([4.0, 3.0]		)
											_a	, _a					:				Dict				=							create_optimizer(5E-5     ,10     ,5		)
											_a					:				Tuple				=							tf.Variable([0.0, 0.0]     ,trainable=_UpperCAmelCase		)

									def accumulate_on_replica(_UpperCAmelCase :	str		):
											accumulator([gradient]		)

									def apply_on_replica():
											optimizer.apply_gradients(list(zip(accumulator.gradients     ,[variable]		)		)		)

									@tf.function
									def accumulate(_UpperCAmelCase :	Optional[int]     ,_UpperCAmelCase :	Optional[int]		):
											with strategy.scope():
													_a					:				Union[str, Any]				=							strategy.experimental_local_results(_UpperCAmelCase		)
													local_variables[0].assign(_UpperCAmelCase		)
													local_variables[1].assign(_UpperCAmelCase		)
													strategy.run(_UpperCAmelCase     ,args=(gradient_placeholder,)		)

									@tf.function
									def apply_grad():
											with strategy.scope():
													strategy.run(_UpperCAmelCase		)

									def _check_local_values(_UpperCAmelCase :	Tuple     ,_UpperCAmelCase :	List[str]		):
											_a					:				List[Any]				=							strategy.experimental_local_results(accumulator._gradients[0]		)
											self.assertListAlmostEqual(values[0].value()     ,_UpperCAmelCase     ,tol=1E-2		)
											self.assertListAlmostEqual(values[1].value()     ,_UpperCAmelCase     ,tol=1E-2		)

									accumulate([1.0, 2.0]     ,[-1.0, 1.0]		)
									accumulate([3.0, -1.0]     ,[-1.0, -1.0]		)
									accumulate([-2.0, 2.0]     ,[3.0, -2.0]		)
									self.assertEqual(accumulator.step     ,3		)
									_check_local_values([2.0, 3.0]     ,[1.0, -2.0]		)
									apply_grad()
									self.assertListAlmostEqual(variable.value()     ,[4.0, 3.0]     ,tol=1E-2		)
									accumulator.reset()
									self.assertEqual(accumulator.step     ,0		)
									_check_local_values([0.0, 0.0]     ,[0.0, 0.0]		)




