




import os
import tempfile
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
					import torch
					from torch import nn

					from transformers import (
					    Adafactor,
					    AdamW,
					    get_constant_schedule,
					    get_constant_schedule_with_warmup,
					    get_cosine_schedule_with_warmup,
					    get_cosine_with_hard_restarts_schedule_with_warmup,
					    get_inverse_sqrt_schedule,
					    get_linear_schedule_with_warmup,
					    get_polynomial_decay_schedule_with_warmup,
					)







def 			lowerCamelCase_		(       UpperCamelCase__				:      List[Any] ,     UpperCamelCase__				:      Union[str, Any]=10      )     ->       str:





				"""simple docstring"""


				__lowerCamelCase		       =    []
				for _ in range(UpperCamelCase__      ):
								lrs.append(scheduler.get_lr()[0]      )
								scheduler.step()
				return lrs







def 			lowerCamelCase_		(       UpperCamelCase__				:      Optional[int] ,     UpperCamelCase__				:      Any=10      )     ->       Any:





				"""simple docstring"""


				__lowerCamelCase		       =    []
				for step in range(UpperCamelCase__      ):
								lrs.append(scheduler.get_lr()[0]      )
								scheduler.step()
								if step == num_steps // 2:
												with tempfile.TemporaryDirectory() as tmpdirname:
																__lowerCamelCase		       =    os.path.join(UpperCamelCase__ ,     'schedule.bin'      )
																torch.save(scheduler.state_dict() ,     UpperCamelCase__      )

																__lowerCamelCase		       =    torch.load(UpperCamelCase__      )
																scheduler.load_state_dict(UpperCamelCase__      )
				return lrs







@require_torch
class     __lowerCAmelCase (       unittest.TestCase ):

			"""simple docstring"""







			def       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__							)    ->      Any:



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


			def       lowercase_    (      self							)    ->      Any:



							'''simple docstring'''
							__lowerCamelCase		       =    torch.tensor([0.1, -0.2, -0.1]			,						requires_grad=lowerCamelCase__							)
							__lowerCamelCase		       =    torch.tensor([0.4, 0.2, -0.5]							)
							__lowerCamelCase		       =    nn.MSELoss()
							# No warmup, constant schedule, no gradient clipping
							__lowerCamelCase		       =    AdamW(params=[w]			,						lr=2e-1			,						weight_decay=0.0							)
							for _ in range(100							):
											__lowerCamelCase		       =    criterion(lowerCamelCase__			,						lowerCamelCase__							)
											loss.backward()
											optimizer.step()
											w.grad.detach_()  # No zero_grad() function on simple tensors. we do it ourselves.
											w.grad.zero_()
							self.assertListAlmostEqual(w.tolist()			,						[0.4, 0.2, -0.5]			,						tol=1e-2							)






			def       lowercase_    (      self							)    ->      Optional[int]:



							'''simple docstring'''
							__lowerCamelCase		       =    torch.tensor([0.1, -0.2, -0.1]			,						requires_grad=lowerCamelCase__							)
							__lowerCamelCase		       =    torch.tensor([0.4, 0.2, -0.5]							)
							__lowerCamelCase		       =    nn.MSELoss()
							# No warmup, constant schedule, no gradient clipping
							__lowerCamelCase		       =    Adafactor(
							    params=[w]			,						lr=1e-2			,						eps=(1e-30, 1e-3)			,						clip_threshold=1.0			,						decay_rate=-0.8			,						betaa=lowerCamelCase__			,						weight_decay=0.0			,						relative_step=lowerCamelCase__			,						scale_parameter=lowerCamelCase__			,						warmup_init=lowerCamelCase__			,						)
							for _ in range(1_000							):
											__lowerCamelCase		       =    criterion(lowerCamelCase__			,						lowerCamelCase__							)
											loss.backward()
											optimizer.step()
											w.grad.detach_()  # No zero_grad() function on simple tensors. we do it ourselves.
											w.grad.zero_()
							self.assertListAlmostEqual(w.tolist()			,						[0.4, 0.2, -0.5]			,						tol=1e-2							)







@require_torch
class     __lowerCAmelCase (       unittest.TestCase ):

			"""simple docstring"""







			snake_case_   		=       nn.Linear(50			,				50 ) if is_torch_available() else None
			snake_case_   		=       AdamW(m.parameters()			,				lr=1_0.0 ) if is_torch_available() else None
			snake_case_   		=       10


			def       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__=None							)    ->      Any:



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






			def       lowercase_    (      self							)    ->      Optional[Any]:



							'''simple docstring'''
							__lowerCamelCase		       =    {'num_warmup_steps': 2, 'num_training_steps': 10}
							# schedulers doct format
							# function: (sched_args_dict, expected_learning_rates)
							__lowerCamelCase		       =    {
							    get_constant_schedule: ({}, [10.0] * self.num_steps),
							    get_constant_schedule_with_warmup: (
							        {'num_warmup_steps': 4},
							        [0.0, 2.5, 5.0, 7.5, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0],
							    ),
							    get_linear_schedule_with_warmup: (
							        {**common_kwargs},
							        [0.0, 5.0, 10.0, 8.75, 7.5, 6.25, 5.0, 3.75, 2.5, 1.25],
							    ),
							    get_cosine_schedule_with_warmup: (
							        {**common_kwargs},
							        [0.0, 5.0, 10.0, 9.61, 8.53, 6.91, 5.0, 3.08, 1.46, 0.38],
							    ),
							    get_cosine_with_hard_restarts_schedule_with_warmup: (
							        {**common_kwargs, 'num_cycles': 2},
							        [0.0, 5.0, 10.0, 8.53, 5.0, 1.46, 10.0, 8.53, 5.0, 1.46],
							    ),
							    get_polynomial_decay_schedule_with_warmup: (
							        {**common_kwargs, 'power': 2.0, 'lr_end': 1e-7},
							        [0.0, 5.0, 10.0, 7.6_56, 5.6_25, 3.9_06, 2.5, 1.4_06, 0.6_25, 0.1_56],
							    ),
							    get_inverse_sqrt_schedule: (
							        {'num_warmup_steps': 2},
							        [0.0, 5.0, 10.0, 8.1_65, 7.0_71, 6.3_25, 5.7_74, 5.3_45, 5.0, 4.7_14],
							    ),
							}

							for scheduler_func, data in scheds.items():
											__lowerCamelCase       ,						__lowerCamelCase		       =    data

											__lowerCamelCase		       =    scheduler_func(self.optimizer			,						**lowerCamelCase__							)
											self.assertEqual(len([scheduler.get_lr()[0]]							)			,						1							)
											__lowerCamelCase		       =    unwrap_schedule(lowerCamelCase__			,						self.num_steps							)
											self.assertListAlmostEqual(
											    lowerCamelCase__			,						lowerCamelCase__			,						tol=1e-2			,						msg=f"""failed for {scheduler_func} in normal scheduler"""			,						)

											__lowerCamelCase		       =    scheduler_func(self.optimizer			,						**lowerCamelCase__							)
											if scheduler_func.__name__ != "get_constant_schedule":
															LambdaScheduleWrapper.wrap_scheduler(lowerCamelCase__							)  # wrap to test picklability of the schedule
											__lowerCamelCase		       =    unwrap_and_save_reload_schedule(lowerCamelCase__			,						self.num_steps							)
											self.assertListEqual(lowerCamelCase__			,						lowerCamelCase__			,						msg=f"""failed for {scheduler_func} in save and reload"""							)







class     __lowerCAmelCase :

			"""simple docstring"""







			def __init__(      self			,						lowerCamelCase__							)    ->      Tuple:



							'''simple docstring'''
							__lowerCamelCase		       =    fn


			def __call__(      self			,						*lowerCamelCase__			,						**lowerCamelCase__							)    ->      Any:



							'''simple docstring'''
							return self.fn(*lowerCamelCase__			,						**lowerCamelCase__							)






			@classmethod
			def       lowercase_    (      self			,						lowerCamelCase__							)    ->      Any:



							'''simple docstring'''
							__lowerCamelCase		       =    list(map(self			,						scheduler.lr_lambdas							)							)









import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_bert import BertTokenizer


a__:      Optional[int]									=		logging.get_logger(__name__)

a__:      int									=		{'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

a__:      Optional[Any]									=		{
    'vocab_file': {
        'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/vocab.txt',
        'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/vocab.txt',
        'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/vocab.txt',
        'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/vocab.txt',
        'bert-base-multilingual-uncased': (
            'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/vocab.txt'
        ),
        'bert-base-multilingual-cased': 'https://huggingface.co/bert-base-multilingual-cased/resolve/main/vocab.txt',
        'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/vocab.txt',
        'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/vocab.txt',
        'bert-large-uncased-whole-word-masking': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/vocab.txt'
        ),
        'bert-large-cased-whole-word-masking': (
            'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/vocab.txt'
        ),
        'bert-large-uncased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt'
        ),
        'bert-large-cased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt'
        ),
        'bert-base-cased-finetuned-mrpc': (
            'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/vocab.txt'
        ),
        'bert-base-german-dbmdz-cased': 'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/vocab.txt',
        'bert-base-german-dbmdz-uncased': (
            'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/vocab.txt'
        ),
        'TurkuNLP/bert-base-finnish-cased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/vocab.txt'
        ),
        'TurkuNLP/bert-base-finnish-uncased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/vocab.txt'
        ),
        'wietsedv/bert-base-dutch-cased': (
            'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/vocab.txt'
        ),
    },
    'tokenizer_file': {
        'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/tokenizer.json',
        'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/tokenizer.json',
        'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/tokenizer.json',
        'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/tokenizer.json',
        'bert-base-multilingual-uncased': (
            'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/tokenizer.json'
        ),
        'bert-base-multilingual-cased': (
            'https://huggingface.co/bert-base-multilingual-cased/resolve/main/tokenizer.json'
        ),
        'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/tokenizer.json',
        'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/tokenizer.json',
        'bert-large-uncased-whole-word-masking': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/tokenizer.json'
        ),
        'bert-large-cased-whole-word-masking': (
            'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/tokenizer.json'
        ),
        'bert-large-uncased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json'
        ),
        'bert-large-cased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json'
        ),
        'bert-base-cased-finetuned-mrpc': (
            'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/tokenizer.json'
        ),
        'bert-base-german-dbmdz-cased': (
            'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/tokenizer.json'
        ),
        'bert-base-german-dbmdz-uncased': (
            'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/tokenizer.json'
        ),
        'TurkuNLP/bert-base-finnish-cased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/tokenizer.json'
        ),
        'TurkuNLP/bert-base-finnish-uncased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/tokenizer.json'
        ),
        'wietsedv/bert-base-dutch-cased': (
            'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/tokenizer.json'
        ),
    },
}

a__:      List[str]									=		{
    'bert-base-uncased': 512,
    'bert-large-uncased': 512,
    'bert-base-cased': 512,
    'bert-large-cased': 512,
    'bert-base-multilingual-uncased': 512,
    'bert-base-multilingual-cased': 512,
    'bert-base-chinese': 512,
    'bert-base-german-cased': 512,
    'bert-large-uncased-whole-word-masking': 512,
    'bert-large-cased-whole-word-masking': 512,
    'bert-large-uncased-whole-word-masking-finetuned-squad': 512,
    'bert-large-cased-whole-word-masking-finetuned-squad': 512,
    'bert-base-cased-finetuned-mrpc': 512,
    'bert-base-german-dbmdz-cased': 512,
    'bert-base-german-dbmdz-uncased': 512,
    'TurkuNLP/bert-base-finnish-cased-v1': 512,
    'TurkuNLP/bert-base-finnish-uncased-v1': 512,
    'wietsedv/bert-base-dutch-cased': 512,
}

a__:      Optional[Any]									=		{
    'bert-base-uncased': {'do_lower_case': True},
    'bert-large-uncased': {'do_lower_case': True},
    'bert-base-cased': {'do_lower_case': False},
    'bert-large-cased': {'do_lower_case': False},
    'bert-base-multilingual-uncased': {'do_lower_case': True},
    'bert-base-multilingual-cased': {'do_lower_case': False},
    'bert-base-chinese': {'do_lower_case': False},
    'bert-base-german-cased': {'do_lower_case': False},
    'bert-large-uncased-whole-word-masking': {'do_lower_case': True},
    'bert-large-cased-whole-word-masking': {'do_lower_case': False},
    'bert-large-uncased-whole-word-masking-finetuned-squad': {'do_lower_case': True},
    'bert-large-cased-whole-word-masking-finetuned-squad': {'do_lower_case': False},
    'bert-base-cased-finetuned-mrpc': {'do_lower_case': False},
    'bert-base-german-dbmdz-cased': {'do_lower_case': False},
    'bert-base-german-dbmdz-uncased': {'do_lower_case': True},
    'TurkuNLP/bert-base-finnish-cased-v1': {'do_lower_case': False},
    'TurkuNLP/bert-base-finnish-uncased-v1': {'do_lower_case': True},
    'wietsedv/bert-base-dutch-cased': {'do_lower_case': False},
}




class     SCREAMING_SNAKE_CASE__ (						UpperCamelCase__      ):
 __SCREAMING_SNAKE_CASE							       =	VOCAB_FILES_NAMES
 __SCREAMING_SNAKE_CASE							       =	PRETRAINED_VOCAB_FILES_MAP
 __SCREAMING_SNAKE_CASE							       =	PRETRAINED_INIT_CONFIGURATION
 __SCREAMING_SNAKE_CASE							       =	PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 __SCREAMING_SNAKE_CASE							       =	BertTokenizer



 def __init__(							self,__lowerCamelCase=None,__lowerCamelCase=None,__lowerCamelCase=True,__lowerCamelCase="[UNK]",__lowerCamelCase="[SEP]",__lowerCamelCase="[PAD]",__lowerCamelCase="[CLS]",__lowerCamelCase="[MASK]",__lowerCamelCase=True,__lowerCamelCase=None,**__lowerCamelCase,):
        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__											=						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__											=						getattr(__lowerCamelCase,normalizer_state.pop('''type''' ) )
               A__											=						do_lower_case
               A__											=						strip_accents
               A__											=						tokenize_chinese_chars
               A__											=						normalizer_class(**__lowerCamelCase )

        A__											=						do_lower_case



 def 				UpperCamelCase			(							self,__lowerCamelCase,__lowerCamelCase=None ):
        A__											=						[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 ):
        A__											=						[self.sep_token_id]
        A__											=						[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 ):
        A__											=						self._tokenizer.model.save(__lowerCamelCase,name=__lowerCamelCase )
        return tuple(__lowerCamelCase )





