


'''simple docstring'''




import inspect
import os
import unittest
from dataclasses import dataclass

import torch

from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs
from accelerate.state import AcceleratorState
from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu
from accelerate.utils import KwargsHandler







@dataclass
class 					a			(				snake_case__  ):
 '''simple docstring'''




 __lowerCAmelCase						: int							  =		0
 __lowerCAmelCase						: bool							  =		False
 __lowerCAmelCase						: float							  =		3.0







class 					a			(				unittest.TestCase  ):
 '''simple docstring'''




 def   __UpperCamelCase   (    self      )      ->    Dict:
    # If no defaults are changed, `to_kwargs` returns an empty dict.
    self.assertDictEqual(MockClass().to_kwargs()		,      {}      )
    self.assertDictEqual(MockClass(a=2      ).to_kwargs()		,      {'a': 2}      )
    self.assertDictEqual(MockClass(a=2		,      b=lowerCamelCase_      ).to_kwargs()		,      {'a': 2, 'b': True}      )
    self.assertDictEqual(MockClass(a=2		,      c=2.25      ).to_kwargs()		,      {'a': 2, 'c': 2.25}      )




 @require_cuda
 def   __UpperCamelCase   (    self      )      ->    int:
    # If no defaults are changed, `to_kwargs` returns an empty dict.
    _a					:  List[str]						=  GradScalerKwargs(init_scale=1_0_2_4		,      growth_factor=2      )
    AcceleratorState._reset_state()
    _a					:  Optional[int]						=  Accelerator(mixed_precision='fp16'		,      kwargs_handlers=[scaler_handler]      )
    print(accelerator.use_fpaa      )
    _a					:  Tuple						=  accelerator.scaler

    # Check the kwargs have been applied
    self.assertEqual(scaler._init_scale		,      1024.0      )
    self.assertEqual(scaler._growth_factor		,      2.0      )

    # Check the other values are at the default
    self.assertEqual(scaler._backoff_factor		,      0.5      )
    self.assertEqual(scaler._growth_interval		,      2_0_0_0      )
    self.assertEqual(scaler._enabled		,      lowerCamelCase_      )




 @require_multi_gpu
 def   __UpperCamelCase   (    self      )      ->    Optional[Any]:
    _a					:  List[str]						=  ['torchrun', F'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__      )]
    execute_subprocess_async(lowerCamelCase_		,      env=os.environ.copy()      )


if __name__ == "__main__":
 UpperCAmelCase_      :		Tuple          =		DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True)
 UpperCAmelCase_      :		str          =		Accelerator(kwargs_handlers=[ddp_scaler])
 UpperCAmelCase_      :		List[Any]          =		torch.nn.Linear(100, 200)
 UpperCAmelCase_      :		Tuple          =		accelerator.prepare(model)

 # Check the values changed in kwargs
 UpperCAmelCase_      :		List[str]          =		""
 UpperCAmelCase_      :		Any          =		model.bucket_bytes_cap // (1024 * 1024)
 if observed_bucket_cap_map != 15:
  error_msg += f"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n"
 if model.find_unused_parameters is not True:
  error_msg += f"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n"

 # Check the values of the defaults
 if model.dim != 0:
  error_msg += f"Default value not respected, should have `0` but found {model.dim}.\n"
 if model.broadcast_buffers is not True:
  error_msg += f"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n"
 if model.gradient_as_bucket_view is not False:
  error_msg += f"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n"

 # Raise error at the end to make sure we don't stop at the first failure.
 if len(error_msg) > 0:
  raise ValueError(error_msg)






'''simple docstring'''




import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

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


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

UpperCAmelCase_      :		Union[str, Any]          =		{
    "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"
        ),
    },
}

UpperCAmelCase_      :		str          =		{
    "unc-nlp/lxmert-base-uncased": 512,
}

UpperCAmelCase_      :		Optional[int]          =		{
    "unc-nlp/lxmert-base-uncased": {"do_lower_case": True},
}







class 					a			(				snake_case__  ):
 '''simple docstring'''




 __lowerCAmelCase						: Tuple							  =		VOCAB_FILES_NAMES
 __lowerCAmelCase						: Dict							  =		PRETRAINED_VOCAB_FILES_MAP
 __lowerCAmelCase						: Optional[Any]							  =		PRETRAINED_INIT_CONFIGURATION
 __lowerCAmelCase						: str							  =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 __lowerCAmelCase						: List[Any]							  =		LxmertTokenizer




 def __init__(    self		,      lowerCamelCase_=None		,      lowerCamelCase_=None		,      lowerCamelCase_=True		,      lowerCamelCase_="[UNK]"		,      lowerCamelCase_="[SEP]"		,      lowerCamelCase_="[PAD]"		,      lowerCamelCase_="[CLS]"		,      lowerCamelCase_="[MASK]"		,      lowerCamelCase_=True		,      lowerCamelCase_=None		,      **lowerCamelCase_		,      )      ->    Optional[int]:
    super().__init__(
        lowerCamelCase_		,      tokenizer_file=lowerCamelCase_		,      do_lower_case=lowerCamelCase_		,      unk_token=lowerCamelCase_		,      sep_token=lowerCamelCase_		,      pad_token=lowerCamelCase_		,      cls_token=lowerCamelCase_		,      mask_token=lowerCamelCase_		,      tokenize_chinese_chars=lowerCamelCase_		,      strip_accents=lowerCamelCase_		,      **lowerCamelCase_		,      )

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

    _a					:  Optional[int]						=  do_lower_case




 def   __UpperCamelCase   (    self		,      lowerCamelCase_		,      lowerCamelCase_=None      )      ->    Optional[Any]:
    _a					:  List[str]						=  [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   __UpperCamelCase   (    self		,      lowerCamelCase_		,      lowerCamelCase_ = None      )      ->    List[int]:
    _a					:  Union[str, Any]						=  [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   __UpperCamelCase   (    self		,      lowerCamelCase_		,      lowerCamelCase_ = None      )      ->    Tuple[str]:
    _a					:  Optional[Any]						=  self._tokenizer.model.save(lowerCamelCase_		,      name=lowerCamelCase_      )
    return tuple(lowerCamelCase_      )


