





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


							def      _snake_case  (							self    :Dict							,				__A    :List[Any]							,				__A    :Union[str, Any]							,				__A    :int		)   ->   str:



									"""simple docstring"""



									self.assertEqual(len(lowerCamelCase__		)							,				len(lowerCamelCase__		)		)
									for a, b in zip(lowerCamelCase__							,				lowerCamelCase__		):
											self.assertAlmostEqual(lowerCamelCase__							,				lowerCamelCase__							,				delta=lowerCamelCase__		)


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      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      _snake_case  (							self    :str		)   ->   Optional[int]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      None
									ops.enable_eager_execution_internal()
									SCREAMING_SNAKE_CASE__			=      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__			=      tf.config.list_logical_devices(device_type="""CPU"""		)
									SCREAMING_SNAKE_CASE__			=      tf.distribute.MirroredStrategy(devices=devices[:2]		)

									with strategy.scope():
											SCREAMING_SNAKE_CASE__			=      GradientAccumulator()
											SCREAMING_SNAKE_CASE__			=      tf.Variable([4.0, 3.0]		)
											SCREAMING_SNAKE_CASE__			=      create_optimizer(5E-5							,				10							,				5		)
											SCREAMING_SNAKE_CASE__			=      tf.Variable([0.0, 0.0]							,				trainable=lowerCamelCase__		)

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

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

									@tf.function
									def accumulate(__A    :Any							,				__A    :Union[str, Any]		):
											with strategy.scope():
													SCREAMING_SNAKE_CASE__			=      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(__A    :Optional[Any]							,				__A    :Union[str, Any]		):
											SCREAMING_SNAKE_CASE__			=      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]		)






import json
import os
import unittest

from transformers import OpenAIGPTTokenizer, OpenAIGPTTokenizerFast
from transformers.models.openai.tokenization_openai import VOCAB_FILES_NAMES
from transformers.testing_utils import require_ftfy, require_spacy, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin




@require_tokenizers
class   UpperCamelCase_       (     UpperCamelCase__				,  unittest.TestCase    ):
							lowerCamelCase_           =					OpenAIGPTTokenizer
							lowerCamelCase_           =					OpenAIGPTTokenizerFast
							lowerCamelCase_           =					True
							lowerCamelCase_           =					False


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



									"""simple docstring"""



									super().setUp()

									# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
									SCREAMING_SNAKE_CASE__			=      [
									    """l""",
									    """o""",
									    """w""",
									    """e""",
									    """r""",
									    """s""",
									    """t""",
									    """i""",
									    """d""",
									    """n""",
									    """w</w>""",
									    """r</w>""",
									    """t</w>""",
									    """lo""",
									    """low""",
									    """er</w>""",
									    """low</w>""",
									    """lowest</w>""",
									    """newer</w>""",
									    """wider</w>""",
									    """<unk>""",
									]
									SCREAMING_SNAKE_CASE__			=      dict(zip(__A							,				range(len(__A		)		)		)		)
									SCREAMING_SNAKE_CASE__			=      ["""#version: 0.2""", """l o""", """lo w""", """e r</w>""", """"""]

									SCREAMING_SNAKE_CASE__			=      os.path.join(self.tmpdirname							,				VOCAB_FILES_NAMES["""vocab_file"""]		)
									SCREAMING_SNAKE_CASE__			=      os.path.join(self.tmpdirname							,				VOCAB_FILES_NAMES["""merges_file"""]		)
									with open(self.vocab_file							,				"""w"""		) as fp:
											fp.write(json.dumps(__A		)		)
									with open(self.merges_file							,				"""w"""		) as fp:
											fp.write("""\n""".join(__A		)		)


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



									"""simple docstring"""



									return "lower newer", "lower newer"


							def      _snake_case  (							self    :Optional[Any]		)   ->   Tuple:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      OpenAIGPTTokenizer(self.vocab_file							,				self.merges_file		)

									SCREAMING_SNAKE_CASE__			=      """lower"""
									SCREAMING_SNAKE_CASE__			=      ["""low""", """er</w>"""]
									SCREAMING_SNAKE_CASE__			=      tokenizer.tokenize(__A		)
									self.assertListEqual(__A							,				__A		)

									SCREAMING_SNAKE_CASE__			=      tokens + ["""<unk>"""]
									SCREAMING_SNAKE_CASE__			=      [14, 15, 20]
									self.assertListEqual(tokenizer.convert_tokens_to_ids(__A		)							,				__A		)


							def      _snake_case  (							self    :Optional[Any]							,				__A    :Optional[Any]=15		)   ->   Any:



									"""simple docstring"""



									for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
											with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})'''		):
													SCREAMING_SNAKE_CASE__			=      self.rust_tokenizer_class.from_pretrained(__A							,				**__A		)

													# Simple input
													SCREAMING_SNAKE_CASE__			=      """This is a simple input"""
													SCREAMING_SNAKE_CASE__			=      ["""This is a simple input 1""", """This is a simple input 2"""]
													SCREAMING_SNAKE_CASE__			=      ("""This is a simple input""", """This is a pair""")
													SCREAMING_SNAKE_CASE__			=      [
													    ("""This is a simple input 1""", """This is a simple input 2"""),
													    ("""This is a simple pair 1""", """This is a simple pair 2"""),
													]

													# Simple input tests
													self.assertRaises(__A							,				tokenizer_r.encode							,				__A							,				max_length=__A							,				padding="""max_length"""		)

													# Simple input
													self.assertRaises(__A							,				tokenizer_r.encode_plus							,				__A							,				max_length=__A							,				padding="""max_length"""		)

													# Simple input
													self.assertRaises(
													    __A							,				tokenizer_r.batch_encode_plus							,				__A							,				max_length=__A							,				padding="""max_length"""							,				)

													# Pair input
													self.assertRaises(__A							,				tokenizer_r.encode							,				__A							,				max_length=__A							,				padding="""max_length"""		)

													# Pair input
													self.assertRaises(__A							,				tokenizer_r.encode_plus							,				__A							,				max_length=__A							,				padding="""max_length"""		)

													# Pair input
													self.assertRaises(
													    __A							,				tokenizer_r.batch_encode_plus							,				__A							,				max_length=__A							,				padding="""max_length"""							,				)


							def      _snake_case  (							self    :Dict		)   ->   List[Any]:



									"""simple docstring"""



									pass




@require_ftfy
@require_spacy
@require_tokenizers
class   UpperCamelCase_       (     UpperCamelCase__    ):
							pass