





import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from .tokenization_lxmert import LxmertTokenizer


lowercase				:	Optional[Any]      						=  {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

lowercase				:	Optional[int]      						=  {
    """vocab_file""": {
        """unc-nlp/lxmert-base-uncased""": """https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/vocab.txt""",
    },
    """tokenizer_file""": {
        """unc-nlp/lxmert-base-uncased""": (
            """https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/tokenizer.json"""
        ),
    },
}

lowercase				:	str      						=  {
    """unc-nlp/lxmert-base-uncased""": 5_1_2,
}

lowercase				:	List[str]      						=  {
    """unc-nlp/lxmert-base-uncased""": {"""do_lower_case""": True},
}





class 			A__   (							__UpperCAmelCase     ):




   """simple docstring"""



   __A       :    Dict	  =  VOCAB_FILES_NAMES
   __A       :    str	  =  PRETRAINED_VOCAB_FILES_MAP
   __A       :    List[Any]	  =  PRETRAINED_INIT_CONFIGURATION
   __A       :    Union[str, Any]	  =  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   __A       :    Union[str, Any]	  =  LxmertTokenizer

   def __init__( self ,  lowercase=None ,  lowercase=None ,  lowercase=True ,  lowercase="[UNK]" ,  lowercase="[SEP]" ,  lowercase="[PAD]" ,  lowercase="[CLS]" ,  lowercase="[MASK]" ,  lowercase=True ,  lowercase=None ,  **lowercase ,  )      ->						Optional[int]:







          '''simple docstring'''

          super().__init__(
              lowercase ,  tokenizer_file=lowercase ,  do_lower_case=lowercase ,  unk_token=lowercase ,  sep_token=lowercase ,  pad_token=lowercase ,  cls_token=lowercase ,  mask_token=lowercase ,  tokenize_chinese_chars=lowercase ,  strip_accents=lowercase ,  **lowercase ,  )

          a__						:     List[str]			=    json.loads(self.backend_tokenizer.normalizer.__getstate__())
          if (
              normalizer_state.get('lowercase' ,  lowercase) != do_lower_case
              or normalizer_state.get('strip_accents' ,  lowercase) != strip_accents
              or normalizer_state.get('handle_chinese_chars' ,  lowercase) != tokenize_chinese_chars
          ):
                 a__						:     Optional[Any]			=    getattr(lowercase ,  normalizer_state.pop('type'))
                 a__						:     List[str]			=    do_lower_case
                 a__						:     int			=    strip_accents
                 a__						:     int			=    tokenize_chinese_chars
                 a__						:     Dict			=    normalizer_class(**lowercase)

          a__						:     Any			=    do_lower_case

   def 							__lowercase		( self ,  lowercase ,  lowercase=None)      ->						List[str]:







          '''simple docstring'''

          a__						:     int			=    [self.cls_token_id] + token_ids_a + [self.sep_token_id]

          if token_ids_a:
                 output += token_ids_a + [self.sep_token_id]

          return output

   def 							__lowercase		( self ,  lowercase ,  lowercase = None)      ->						List[int]:







          '''simple docstring'''

          a__						:     List[str]			=    [self.sep_token_id]
          a__						:     Optional[int]			=    [self.cls_token_id]
          if token_ids_a is None:
                 return len(cls + token_ids_a + sep) * [0]
          return len(cls + token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1]

   def 							__lowercase		( self ,  lowercase ,  lowercase = None)      ->						Tuple[str]:







          '''simple docstring'''

          a__						:     Optional[Any]			=    self._tokenizer.model.save(lowercase ,  name=lowercase)
          return tuple(lowercase)









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




   """simple docstring"""



   def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase)      ->						List[str]:







          '''simple docstring'''

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

   def 							__lowercase		( self)      ->						Any:







          '''simple docstring'''

          a__						:     Tuple			=    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(lowercase):
                 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[Any]:







          '''simple docstring'''

          a__						:     Tuple			=    None
          ops.enable_eager_execution_internal()
          a__						:     Optional[int]			=    tf.config.list_physical_devices('CPU')
          if len(lowercase) == 1:
                 tf.config.set_logical_device_configuration(
                     physical_devices[0] ,  [tf.config.LogicalDeviceConfiguration(), tf.config.LogicalDeviceConfiguration()])
          a__						:     int			=    tf.config.list_logical_devices(device_type='CPU')
          a__						:     Union[str, Any]			=    tf.distribute.MirroredStrategy(devices=devices[:2])

          with strategy.scope():
                 a__						:     str			=    GradientAccumulator()
                 a__						:     Tuple			=    tf.Variable([4.0, 3.0])
                 a__			,							a__						:     Tuple			=    create_optimizer(5e-5 ,  10 ,  5)
                 a__						:     Tuple			=    tf.Variable([0.0, 0.0] ,  trainable=lowercase)

          def accumulate_on_replica(lowercase):
                 accumulator([gradient])

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

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

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

          def _check_local_values(lowercase ,  lowercase):
                 a__						:     Union[str, Any]			=    strategy.experimental_local_results(accumulator._gradients[0])
                 self.assertListAlmostEqual(values[0].value() ,  lowercase ,  tol=1e-2)
                 self.assertListAlmostEqual(values[1].value() ,  lowercase ,  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])


