
"""simple docstring"""




from unittest import TestCase

from datasets import Sequence, Value
from datasets.arrow_dataset import Dataset


class       __magic_name__							(							snake_case_			):
       def 				lowercase_				(  self							)  ->						str:





         """simple docstring"""




         return [
             {"col_1": 3, "col_2": "a"},
             {"col_1": 2, "col_2": "b"},
             {"col_1": 1, "col_2": "c"},
             {"col_1": 0, "col_2": "d"},
         ]
       def 				lowercase_				(  self							)  ->						int:





         """simple docstring"""




         _lowercase:      List[str]   			=  {'''col_1''': [3, 2, 1, 0], '''col_2''': ['''a''', '''b''', '''c''', '''d''']}
         return Dataset.from_dict(lowerCAmelCase__							)
       def 				lowercase_				(  self							)  ->						str:





         """simple docstring"""




         _lowercase:      Optional[int]   			=  self._create_example_records()
         _lowercase:      Optional[int]   			=  Dataset.from_list(lowerCAmelCase__							)
         self.assertListEqual(dset.column_names			,					['''col_1''', '''col_2''']							)
         for i, r in enumerate(lowerCAmelCase__							):
           self.assertDictEqual(lowerCAmelCase__			,					example_records[i]							)
       def 				lowercase_				(  self							)  ->						int:





         """simple docstring"""




         _lowercase:      Optional[Any]   			=  self._create_example_records()
         _lowercase:      Optional[int]   			=  Dataset.from_list(lowerCAmelCase__							)
         _lowercase:      int   			=  Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]}							)
         self.assertEqual(dset.info			,					dset_from_dict.info							)
       def 				lowercase_				(  self							)  ->						Union[str, Any]:  # checks what happens with missing columns





         """simple docstring"""




         _lowercase:      Optional[Any]   			=  [{'''col_1''': 1}, {'''col_2''': '''x'''}]
         _lowercase:      str   			=  Dataset.from_list(lowerCAmelCase__							)
         self.assertDictEqual(dset[0]			,					{'''col_1''': 1}							)
         self.assertDictEqual(dset[1]			,					{'''col_1''': None}							)  # NB: first record is used for columns
       def 				lowercase_				(  self							)  ->						int:  # checks if the type can be inferred from the second record





         """simple docstring"""




         _lowercase:      Dict   			=  [{'''col_1''': []}, {'''col_1''': [1, 2]}]
         _lowercase:      List[str]   			=  Dataset.from_list(lowerCAmelCase__							)
         self.assertEqual(dset.info.features['''col_1''']			,					Sequence(Value('''int64'''							)							)							)



       def 				lowercase_				(  self							)  ->						Tuple:





         """simple docstring"""




         _lowercase:      str   			=  Dataset.from_list([]							)
         self.assertEqual(len(lowerCAmelCase__							)			,					0							)
         self.assertListEqual(dset.column_names			,					[]							)





'''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 						lowerCamelCase_				(       unittest.TestCase      ):







						def        __lowercase     (	self :   List[Any]					,     lowerCAmelCase__ :   str					,     lowerCAmelCase__ :   Dict					,     lowerCAmelCase__ :   int					):

													"""simple docstring"""
													self.assertEqual(len(lowerCAmelCase__					)					,     len(lowerCAmelCase__					)					)
													for a, b in zip(lowerCAmelCase__					,     lowerCAmelCase__					):
																				self.assertAlmostEqual(lowerCAmelCase__					,     lowerCAmelCase__					,     delta=lowerCAmelCase__					)







						def        __lowercase     (	self :   Dict					):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE       :   List[Any]         =      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(lowerCAmelCase__					):
																				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 :   List[str]					):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE       :   Optional[int]         =      None
													ops.enable_eager_execution_internal()
													SCREAMING_SNAKE_CASE       :   List[Any]         =      tf.config.list_physical_devices('''CPU'''					)
													if len(lowerCAmelCase__					) == 1:
																				tf.config.set_logical_device_configuration(
																				    physical_devices[0]					,     [tf.config.LogicalDeviceConfiguration(), tf.config.LogicalDeviceConfiguration()]					)
													SCREAMING_SNAKE_CASE       :   Tuple         =      tf.config.list_logical_devices(device_type='''CPU'''					)
													SCREAMING_SNAKE_CASE       :   List[Any]         =      tf.distribute.MirroredStrategy(devices=devices[:2]					)

													with strategy.scope():
																				SCREAMING_SNAKE_CASE       :   Optional[Any]         =      GradientAccumulator()
																				SCREAMING_SNAKE_CASE       :   Dict         =      tf.Variable([4.0, 3.0]					)
																				SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE       :   Optional[int]         =      create_optimizer(5e-5					,     10					,     5					)
																				SCREAMING_SNAKE_CASE       :   List[Any]         =      tf.Variable([0.0, 0.0]					,     trainable=lowerCAmelCase__					)

													def accumulate_on_replica(lowerCAmelCase__ :   List[str]					):
																				accumulator([gradient]					)

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

													@tf.function
													def accumulate(lowerCAmelCase__ :   str					,     lowerCAmelCase__ :   List[Any]					):
																				with strategy.scope():
																											SCREAMING_SNAKE_CASE       :   Optional[Any]         =      strategy.experimental_local_results(lowerCAmelCase__					)
																											local_variables[0].assign(lowerCAmelCase__					)
																											local_variables[1].assign(lowerCAmelCase__					)
																											strategy.run(lowerCAmelCase__					,     args=(gradient_placeholder,)					)

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

													def _check_local_values(lowerCAmelCase__ :   Dict					,     lowerCAmelCase__ :   int					):
																				SCREAMING_SNAKE_CASE       :   Tuple         =      strategy.experimental_local_results(accumulator._gradients[0]					)
																				self.assertListAlmostEqual(values[0].value()					,     lowerCAmelCase__					,     tol=1e-2					)
																				self.assertListAlmostEqual(values[1].value()					,     lowerCAmelCase__					,     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]					)




