


import argparse
import json
import os

import fairseq
import torch
from fairseq.data import Dictionary

from transformers import (
    WavaVecaConformerConfig,
    WavaVecaConformerForCTC,
    WavaVecaConformerForPreTraining,
    WavaVecaCTCTokenizer,
    WavaVecaFeatureExtractor,
    WavaVecaProcessor,
    logging,
)


logging.set_verbosity_info()
snake_case							:       Any										=	logging.get_logger(__name__)

snake_case							:       Optional[Any]										=	{
    '''post_extract_proj''': '''feature_projection.projection''',
    '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''',
    '''self_attn.linear_k''': '''encoder.layers.*.self_attn.linear_k''',
    '''self_attn.linear_v''': '''encoder.layers.*.self_attn.linear_v''',
    '''self_attn.linear_q''': '''encoder.layers.*.self_attn.linear_q''',
    '''self_attn.pos_bias_u''': '''encoder.layers.*.self_attn.pos_bias_u''',
    '''self_attn.pos_bias_v''': '''encoder.layers.*.self_attn.pos_bias_v''',
    '''self_attn.linear_out''': '''encoder.layers.*.self_attn.linear_out''',
    '''self_attn.linear_pos''': '''encoder.layers.*.self_attn.linear_pos''',
    '''self_attn.rotary_emb''': '''encoder.embed_positions''',
    '''self_attn_layer_norm''': '''encoder.layers.*.self_attn_layer_norm''',
    '''conv_module.pointwise_conv1''': '''encoder.layers.*.conv_module.pointwise_conv1''',
    '''conv_module.pointwise_conv2''': '''encoder.layers.*.conv_module.pointwise_conv2''',
    '''conv_module.depthwise_conv''': '''encoder.layers.*.conv_module.depthwise_conv''',
    '''conv_module.batch_norm''': '''encoder.layers.*.conv_module.batch_norm''',
    '''conv_module.layer_norm''': '''encoder.layers.*.conv_module.layer_norm''',
    '''ffn1.w_1''': '''encoder.layers.*.ffn1.intermediate_dense''',
    '''ffn1.w_2''': '''encoder.layers.*.ffn1.output_dense''',
    '''ffn1.layer_norm''': '''encoder.layers.*.ffn1_layer_norm''',
    '''ffn2.w_1''': '''encoder.layers.*.ffn2.intermediate_dense''',
    '''ffn2.w_2''': '''encoder.layers.*.ffn2.output_dense''',
    '''ffn2.layer_norm''': '''encoder.layers.*.ffn2_layer_norm''',
    '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''',
    '''encoder.layer_norm''': '''encoder.layer_norm''',
    '''w2v_model.layer_norm''': '''feature_projection.layer_norm''',
    '''quantizer.weight_proj''': '''quantizer.weight_proj''',
    '''quantizer.vars''': '''quantizer.codevectors''',
    '''project_q''': '''project_q''',
    '''final_proj''': '''project_hid''',
    '''w2v_encoder.proj''': '''lm_head''',
    '''mask_emb''': '''masked_spec_embed''',
}
snake_case							:       Tuple										=	[
    '''lm_head''',
    '''quantizer.weight_proj''',
    '''quantizer.codevectors''',
    '''project_q''',
    '''project_hid''',
]
def 							__lowerCamelCase		(					UpperCAmelCase_		:						Optional[int]					,    UpperCAmelCase_		:						Optional[int]					,    UpperCAmelCase_		:						str					,    UpperCAmelCase_		:						Any					,    UpperCAmelCase_		:						List[Any]		):




      """simple docstring"""





      for attribute in key.split('''.'''		):
            a		:Union[str, Any]									=			getattr(UpperCAmelCase_					,    UpperCAmelCase_		)

      if weight_type is not None:
            a		:Dict									=			getattr(UpperCAmelCase_					,    UpperCAmelCase_		).shape
      else:
            a		:List[Any]									=			hf_pointer.shape

      if hf_shape != value.shape:
            raise ValueError(
                F'''Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be'''
                F''' {value.shape} for {full_name}'''		)

      if weight_type == "weight":
            a		:Union[str, Any]									=			value
      elif weight_type == "weight_g":
            a		:Optional[Any]									=			value
      elif weight_type == "weight_v":
            a		:List[Any]									=			value
      elif weight_type == "bias":
            a		:Tuple									=			value
      elif weight_type == "running_mean":
            a		:Any									=			value
      elif weight_type == "running_var":
            a		:int									=			value
      elif weight_type == "num_batches_tracked":
            a		:Any									=			value
      elif weight_type == "inv_freq":
            a		:Tuple									=			value
      else:
            a		:Optional[int]									=			value

      logger.info(F'''{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}.'''		)
def 							__lowerCamelCase		(					UpperCAmelCase_		:						List[Any]					,    UpperCAmelCase_		:						int					,    UpperCAmelCase_		:						List[str]		):




      """simple docstring"""





      a		:Tuple									=			[]
      a		:Any									=			fairseq_model.state_dict()

      a		:List[Any]									=			hf_model.wavaveca_conformer.feature_extractor

      for name, value in fairseq_dict.items():
            a		:Dict									=			False
            if "conv_layers" in name:
                  load_conv_layer(
                      UpperCAmelCase_					,    UpperCAmelCase_					,    UpperCAmelCase_					,    UpperCAmelCase_					,    hf_model.config.feat_extract_norm == '''group'''					,    )
                  a		:int									=			True
            else:
                  for key, mapped_key in MAPPING.items():
                        a		:Tuple									=			'''wav2vec2_conformer.''' + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key
                        if key in name or key.split('''w2v_model.'''		)[-1] == name.split('''.'''		)[0]:
                              a		:Union[str, Any]									=			True
                              if "*" in mapped_key:
                                    a		:Any									=			name.split(UpperCAmelCase_		)[0].split('''.'''		)[-2]
                                    a		:Union[str, Any]									=			mapped_key.replace('''*'''					,    UpperCAmelCase_		)
                              if "pos_bias_u" in name:
                                    a		:List[Any]									=			None
                              elif "pos_bias_v" in name:
                                    a		:Optional[Any]									=			None
                              elif "weight_g" in name:
                                    a		:Any									=			'''weight_g'''
                              elif "weight_v" in name:
                                    a		:int									=			'''weight_v'''
                              elif "bias" in name:
                                    a		:Tuple									=			'''bias'''
                              elif "weight" in name:
                                    # TODO: don't match quantizer.weight_proj
                                    a		:Union[str, Any]									=			'''weight'''
                              elif "running_mean" in name:
                                    a		:Dict									=			'''running_mean'''
                              elif "inv_freq" in name:
                                    a		:Any									=			'''inv_freq'''
                              elif "running_var" in name:
                                    a		:Dict									=			'''running_var'''
                              elif "num_batches_tracked" in name:
                                    a		:List[str]									=			'''num_batches_tracked'''
                              else:
                                    a		:Optional[int]									=			None
                              set_recursively(UpperCAmelCase_					,    UpperCAmelCase_					,    UpperCAmelCase_					,    UpperCAmelCase_					,    UpperCAmelCase_		)
                        continue
            if not is_used:
                  unused_weights.append(UpperCAmelCase_		)

      logger.warning(F'''Unused weights: {unused_weights}'''		)
def 							__lowerCamelCase		(					UpperCAmelCase_		:						List[Any]					,    UpperCAmelCase_		:						Dict					,    UpperCAmelCase_		:						int					,    UpperCAmelCase_		:						Tuple					,    UpperCAmelCase_		:						Dict		):




      """simple docstring"""





      a		:Dict									=			full_name.split('''conv_layers.'''		)[-1]
      a		:Optional[int]									=			name.split('''.'''		)
      a		:Dict									=			int(items[0]		)
      a		:Dict									=			int(items[1]		)

      if type_id == 0:
            if "bias" in name:
                  if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape:
                        raise ValueError(
                            F'''{full_name} has size {value.shape}, but'''
                            F''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.'''		)
                  a		:Optional[Any]									=			value
                  logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.'''		)
            elif "weight" in name:
                  if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape:
                        raise ValueError(
                            F'''{full_name} has size {value.shape}, but'''
                            F''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.'''		)
                  a		:Union[str, Any]									=			value
                  logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.'''		)
      elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm):
            if "bias" in name:
                  if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape:
                        raise ValueError(
                            F'''{full_name} has size {value.shape}, but'''
                            F''' {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found.'''		)
                  a		:Optional[Any]									=			value
                  logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.'''		)
            elif "weight" in name:
                  if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape:
                        raise ValueError(
                            F'''{full_name} has size {value.shape}, but'''
                            F''' {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found.'''		)
                  a		:Optional[int]									=			value
                  logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.'''		)
      else:
            unused_weights.append(UpperCAmelCase_		)
@torch.no_grad()
def 							__lowerCamelCase		(					UpperCAmelCase_		:						Any					,    UpperCAmelCase_		:						Tuple					,    UpperCAmelCase_		:						Union[str, Any]=None					,    UpperCAmelCase_		:						List[str]=None					,    UpperCAmelCase_		:						Tuple=True		):




      """simple docstring"""





      if config_path is not None:
            a		:Union[str, Any]									=			WavaVecaConformerConfig.from_pretrained(UpperCAmelCase_					,    hidden_act='''swish'''		)
      else:
            a		:List[Any]									=			WavaVecaConformerConfig()

      if "rope" in checkpoint_path:
            a		:Tuple									=			'''rotary'''

      if is_finetuned:
            if dict_path:
                  a		:Any									=			Dictionary.load(UpperCAmelCase_		)

                  # important change bos & pad token id since CTC symbol is <pad> and
                  # not <s> as in fairseq
                  a		:str									=			target_dict.pad_index
                  a		:List[str]									=			target_dict.bos_index
                  a		:str									=			target_dict.eos_index
                  a		:Optional[Any]									=			len(target_dict.symbols		)
                  a		:Dict									=			os.path.join(UpperCAmelCase_					,    '''vocab.json'''		)
                  if not os.path.isdir(UpperCAmelCase_		):
                        logger.error('''--pytorch_dump_folder_path ({}) should be a directory'''.format(UpperCAmelCase_		)		)
                        return
                  os.makedirs(UpperCAmelCase_					,    exist_ok=UpperCAmelCase_		)
                  a		:List[str]									=			target_dict.indices

                  # fairseq has the <pad> and <s> switched
                  a		:int									=			0
                  a		:Any									=			1
                  with open(UpperCAmelCase_					,    '''w'''					,    encoding='''utf-8'''		) as vocab_handle:
                        json.dump(UpperCAmelCase_					,    UpperCAmelCase_		)
                  a		:Optional[int]									=			WavaVecaCTCTokenizer(
                      UpperCAmelCase_					,    unk_token=target_dict.unk_word					,    pad_token=target_dict.pad_word					,    bos_token=target_dict.bos_word					,    eos_token=target_dict.eos_word					,    word_delimiter_token='''|'''					,    do_lower_case=UpperCAmelCase_					,    )
                  a		:List[str]									=			True if config.feat_extract_norm == '''layer''' else False
                  a		:str									=			WavaVecaFeatureExtractor(
                      feature_size=1					,    sampling_rate=1_6000					,    padding_value=0					,    do_normalize=UpperCAmelCase_					,    return_attention_mask=UpperCAmelCase_					,    )
                  a		:List[str]									=			WavaVecaProcessor(feature_extractor=UpperCAmelCase_					,    tokenizer=UpperCAmelCase_		)
                  processor.save_pretrained(UpperCAmelCase_		)

            a		:Optional[int]									=			WavaVecaConformerForCTC(UpperCAmelCase_		)
      else:
            a		:List[str]									=			WavaVecaConformerForPreTraining(UpperCAmelCase_		)

      if is_finetuned:
            a				,							a				,							a		:Dict									=			fairseq.checkpoint_utils.load_model_ensemble_and_task(
                [checkpoint_path]					,    arg_overrides={'''data''': '''/'''.join(dict_path.split('''/'''		)[:-1]		)}		)
      else:
            a		:Optional[int]									=			argparse.Namespace(task='''audio_pretraining'''		)
            a		:Optional[int]									=			fairseq.tasks.setup_task(UpperCAmelCase_		)

            a				,							a				,							a		:List[Any]									=			fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path]					,    task=UpperCAmelCase_		)

      a		:Tuple									=			model[0].eval()

      recursively_load_weights(UpperCAmelCase_					,    UpperCAmelCase_					,    not is_finetuned		)

      hf_wavavec.save_pretrained(UpperCAmelCase_		)


if __name__ == "__main__":
     snake_case							:       Union[str, Any]										=	argparse.ArgumentParser()
     parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''')
     parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''')
     parser.add_argument('''--dict_path''', default=None, type=str, help='''Path to dict of fine-tuned model''')
     parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''')
     parser.add_argument(
         '''--not_finetuned''', action='''store_true''', help='''Whether the model to convert is a fine-tuned model or not'''
     )
     snake_case							:       int										=	parser.parse_args()
     convert_wavaveca_conformer_checkpoint(
         args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned
     )









import json
import os
import shutil
import tempfile
import unittest

from transformers import BatchEncoding, CanineTokenizer
from transformers.testing_utils import require_tokenizers, require_torch
from transformers.tokenization_utils import AddedToken
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin







class 						_snake_case    ( _snake_case		,       unittest.TestCase				):
 SCREAMING_SNAKE_CASE__ =			CanineTokenizer
 SCREAMING_SNAKE_CASE__ =			False




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       super().setUp()
       a		:List[str]									=			CanineTokenizer()
       tokenizer.save_pretrained(self.tmpdirname       )




 @cached_property
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       return CanineTokenizer.from_pretrained('''google/canine-s'''       )




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    **_lowerCamelCase       ):
       a		:Dict									=			self.tokenizer_class.from_pretrained(self.tmpdirname   ,    **_lowerCamelCase       )
       a		:List[Any]									=			1024
       return tokenizer




 @require_torch
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Any									=			self.canine_tokenizer
       a		:Tuple									=			['''Life is like a box of chocolates.''', '''You never know what you\'re gonna get.''']
       # fmt: off
       a		:Any									=			[5_7344, 76, 105, 102, 101, 32, 105, 115, 32, 108, 105, 107, 101, 32, 97, 32, 98, 111, 120, 32, 111, 102, 32, 99, 104, 111, 99, 111, 108, 97, 116, 101, 115, 46, 5_7345, 0, 0, 0, 0]
       # fmt: on
       a		:Dict									=			tokenizer(_lowerCamelCase   ,    padding=_lowerCamelCase   ,    return_tensors='''pt'''       )
       self.assertIsInstance(_lowerCamelCase   ,    _lowerCamelCase       )

       a		:str									=			list(batch.input_ids.numpy()[0]       )

       self.assertListEqual(_lowerCamelCase   ,    _lowerCamelCase       )

       self.assertEqual((2, 39)   ,    batch.input_ids.shape       )
       self.assertEqual((2, 39)   ,    batch.attention_mask.shape       )




 @require_torch
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[Any]									=			self.canine_tokenizer
       a		:Optional[int]									=			['''Once there was a man.''', '''He wrote a test in HuggingFace Tranformers.''']
       a		:Dict									=			tokenizer(_lowerCamelCase   ,    padding=_lowerCamelCase   ,    return_tensors='''pt'''       )
       # check if input_ids, attention_mask and token_type_ids are returned
       self.assertIn('''input_ids'''   ,    _lowerCamelCase       )
       self.assertIn('''attention_mask'''   ,    _lowerCamelCase       )
       self.assertIn('''token_type_ids'''   ,    _lowerCamelCase       )




 @require_torch
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Any									=			self.canine_tokenizer
       a		:str									=			[
           '''What\'s the weater?''',
           '''It\'s about 25 degrees.''',
       ]
       a		:List[str]									=			tokenizer(
           text_target=_lowerCamelCase   ,    max_length=32   ,    padding='''max_length'''   ,    truncation=_lowerCamelCase   ,    return_tensors='''pt'''       )
       self.assertEqual(32   ,    targets['''input_ids'''].shape[1]       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       # safety check on max_len default value so we are sure the test works
       a		:Tuple									=			self.get_tokenizers()
       for tokenizer in tokenizers:
             with self.subTest(F'''{tokenizer.__class__.__name__}'''       ):
                   self.assertNotEqual(tokenizer.model_max_length   ,    42       )

        # Now let's start the test
       a		:Tuple									=			self.get_tokenizers()
       for tokenizer in tokenizers:
             with self.subTest(F'''{tokenizer.__class__.__name__}'''       ):
                   # Isolate this from the other tests because we save additional tokens/etc
                   a		:List[str]									=			tempfile.mkdtemp()

                   a		:Any									=			''' He is very happy, UNwant\u00E9d,running'''
                   a		:Dict									=			tokenizer.encode(_lowerCamelCase   ,    add_special_tokens=_lowerCamelCase       )
                   tokenizer.save_pretrained(_lowerCamelCase       )

                   a		:Optional[Any]									=			tokenizer.__class__.from_pretrained(_lowerCamelCase       )
                   a		:str									=			after_tokenizer.encode(_lowerCamelCase   ,    add_special_tokens=_lowerCamelCase       )
                   self.assertListEqual(_lowerCamelCase   ,    _lowerCamelCase       )

                   shutil.rmtree(_lowerCamelCase       )

       a		:str									=			self.get_tokenizers(model_max_length=42       )
       for tokenizer in tokenizers:
             with self.subTest(F'''{tokenizer.__class__.__name__}'''       ):
                   # Isolate this from the other tests because we save additional tokens/etc
                   a		:Any									=			tempfile.mkdtemp()

                   a		:Optional[Any]									=			''' He is very happy, UNwant\u00E9d,running'''

                   a		:Optional[int]									=			tokenizer.additional_special_tokens

                   # We can add a new special token for Canine as follows:
                   a		:str									=			chr(0Xe_0_0_7       )
                   additional_special_tokens.append(_lowerCamelCase       )
                   tokenizer.add_special_tokens({'''additional_special_tokens''': additional_special_tokens}       )
                   a		:Tuple									=			tokenizer.encode(_lowerCamelCase   ,    add_special_tokens=_lowerCamelCase       )
                   tokenizer.save_pretrained(_lowerCamelCase       )

                   a		:int									=			tokenizer.__class__.from_pretrained(_lowerCamelCase       )
                   a		:Dict									=			after_tokenizer.encode(_lowerCamelCase   ,    add_special_tokens=_lowerCamelCase       )
                   self.assertListEqual(_lowerCamelCase   ,    _lowerCamelCase       )
                   self.assertIn(_lowerCamelCase   ,    after_tokenizer.additional_special_tokens       )
                   self.assertEqual(after_tokenizer.model_max_length   ,    42       )

                   a		:str									=			tokenizer.__class__.from_pretrained(_lowerCamelCase   ,    model_max_length=43       )
                   self.assertEqual(tokenizer.model_max_length   ,    43       )

                   shutil.rmtree(_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Tuple									=			self.get_tokenizers(do_lower_case=_lowerCamelCase       )
       for tokenizer in tokenizers:
             with self.subTest(F'''{tokenizer.__class__.__name__}'''       ):
                   a				,							a		:Tuple									=			self.get_clean_sequence(_lowerCamelCase       )

                   # a special token for Canine can be defined as follows:
                   a		:Tuple									=			0Xe_0_0_5
                   a		:Optional[int]									=			chr(_lowerCamelCase       )

                   tokenizer.add_special_tokens({'''cls_token''': special_token}       )
                   a		:Optional[int]									=			tokenizer.encode(_lowerCamelCase   ,    add_special_tokens=_lowerCamelCase       )
                   self.assertEqual(len(_lowerCamelCase       )   ,    1       )

                   a		:List[Any]									=			tokenizer.decode(ids + encoded_special_token   ,    clean_up_tokenization_spaces=_lowerCamelCase       )
                   a		:List[str]									=			tokenizer.encode(_lowerCamelCase   ,    add_special_tokens=_lowerCamelCase       )

                   a		:Tuple									=			tokenizer.encode(_lowerCamelCase   ,    add_special_tokens=_lowerCamelCase       )
                   a		:Optional[Any]									=			tokenizer.encode(_lowerCamelCase   ,    add_special_tokens=_lowerCamelCase       )
                   self.assertEqual(_lowerCamelCase   ,    input_encoded + special_token_id       )

                   a		:Any									=			tokenizer.decode(_lowerCamelCase   ,    skip_special_tokens=_lowerCamelCase       )
                   self.assertTrue(special_token not in decoded       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:int									=			self.get_tokenizers(do_lower_case=_lowerCamelCase       )
       for tokenizer in tokenizers:
             with self.subTest(F'''{tokenizer.__class__.__name__}'''       ):
                   a		:int									=			chr(0Xe_0_0_5       )
                   a		:str									=			chr(0Xe_0_0_6       )

                   # `add_tokens` method stores special tokens only in `tokenizer.unique_no_split_tokens`. (in tokenization_utils.py)
                   tokenizer.add_tokens([SPECIAL_TOKEN_1]   ,    special_tokens=_lowerCamelCase       )
                   # `add_special_tokens` method stores special tokens in `tokenizer.additional_special_tokens`,
                   # which also occur in `tokenizer.all_special_tokens`. (in tokenization_utils_base.py)
                   tokenizer.add_special_tokens({'''additional_special_tokens''': [SPECIAL_TOKEN_2]}       )

                   a		:Optional[int]									=			tokenizer.tokenize(_lowerCamelCase       )
                   a		:Optional[Any]									=			tokenizer.tokenize(_lowerCamelCase       )

                   self.assertEqual(len(_lowerCamelCase       )   ,    1       )
                   self.assertEqual(len(_lowerCamelCase       )   ,    1       )
                   self.assertEqual(token_a[0]   ,    _lowerCamelCase       )
                   self.assertEqual(token_a[0]   ,    _lowerCamelCase       )




 @require_tokenizers
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Optional[Any]									=			self.get_tokenizers(do_lower_case=_lowerCamelCase       )
       for tokenizer in tokenizers:
             with self.subTest(F'''{tokenizer.__class__.__name__}'''       ):
                   # a special token for Canine can be defined as follows:
                   a		:Optional[int]									=			0Xe_0_0_6
                   a		:List[str]									=			chr(_lowerCamelCase       )

                   a		:Optional[int]									=			AddedToken(_lowerCamelCase   ,    lstrip=_lowerCamelCase       )
                   tokenizer.add_special_tokens({'''additional_special_tokens''': [new_token]}       )

                   with tempfile.TemporaryDirectory() as tmp_dir_name:
                         tokenizer.save_pretrained(_lowerCamelCase       )
                         tokenizer.from_pretrained(_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:int									=			[]
       if self.test_slow_tokenizer:
             tokenizer_list.append((self.tokenizer_class, self.get_tokenizer())       )

       if self.test_rust_tokenizer:
             tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer())       )

       for tokenizer_class, tokenizer_utils in tokenizer_list:
             with tempfile.TemporaryDirectory() as tmp_dir:
                   tokenizer_utils.save_pretrained(_lowerCamelCase       )

                   with open(os.path.join(_lowerCamelCase   ,    '''special_tokens_map.json'''       )   ,    encoding='''utf-8'''       ) as json_file:
                         a		:Optional[Any]									=			json.load(_lowerCamelCase       )

                   with open(os.path.join(_lowerCamelCase   ,    '''tokenizer_config.json'''       )   ,    encoding='''utf-8'''       ) as json_file:
                         a		:Tuple									=			json.load(_lowerCamelCase       )

                   # a special token for Canine can be defined as follows:
                   a		:int									=			0Xe_0_0_6
                   a		:Optional[Any]									=			chr(_lowerCamelCase       )

                   a		:Union[str, Any]									=			[new_token_a]
                   a		:Optional[int]									=			[new_token_a]

                   with open(os.path.join(_lowerCamelCase   ,    '''special_tokens_map.json'''       )   ,    '''w'''   ,    encoding='''utf-8'''       ) as outfile:
                         json.dump(_lowerCamelCase   ,    _lowerCamelCase       )
                   with open(os.path.join(_lowerCamelCase   ,    '''tokenizer_config.json'''       )   ,    '''w'''   ,    encoding='''utf-8'''       ) as outfile:
                         json.dump(_lowerCamelCase   ,    _lowerCamelCase       )

                   # the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes
                   # into account the new value of additional_special_tokens given in the "tokenizer_config.json" and
                   # "special_tokens_map.json" files
                   a		:str									=			tokenizer_class.from_pretrained(_lowerCamelCase   ,    extra_ids=0       )
                   self.assertIn(_lowerCamelCase   ,    tokenizer_without_change_in_init.additional_special_tokens       )
                   # self.assertIn("an_additional_special_token",tokenizer_without_change_in_init.get_vocab()) # ByT5Tokenization no vocab
                   self.assertEqual(
                       [new_token_a]   ,    tokenizer_without_change_in_init.convert_ids_to_tokens(
                           tokenizer_without_change_in_init.convert_tokens_to_ids([new_token_a]       )       )   ,    )

                   a		:Optional[int]									=			0Xe_0_0_7
                   a		:Any									=			chr(_lowerCamelCase       )
                   # Now we test that we can change the value of additional_special_tokens in the from_pretrained
                   a		:Tuple									=			[AddedToken(_lowerCamelCase   ,    lstrip=_lowerCamelCase       )]
                   a		:Optional[Any]									=			tokenizer_class.from_pretrained(
                       _lowerCamelCase   ,    additional_special_tokens=_lowerCamelCase   ,    extra_ids=0       )

                   self.assertIn(_lowerCamelCase   ,    tokenizer.additional_special_tokens       )
                   # self.assertIn(new_token_2,tokenizer.get_vocab()) # ByT5Tokenization no vocab
                   self.assertEqual(
                       [new_token_a]   ,    tokenizer.convert_ids_to_tokens(tokenizer.convert_tokens_to_ids([new_token_a]       )       )       )




 @require_tokenizers
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[Any]									=			self.get_tokenizers(do_lower_case=_lowerCamelCase       )
       for tokenizer in tokenizers:
             with self.subTest(F'''{tokenizer.__class__.__name__}'''       ):
                   a		:List[str]									=			'''hello world'''
                   if self.space_between_special_tokens:
                         a		:Optional[Any]									=			'''[CLS] hello world [SEP]'''
                   else:
                         a		:Tuple									=			input
                   a		:Any									=			tokenizer.encode(_lowerCamelCase   ,    add_special_tokens=_lowerCamelCase       )
                   a		:List[Any]									=			tokenizer.decode(_lowerCamelCase   ,    spaces_between_special_tokens=self.space_between_special_tokens       )
                   self.assertIn(_lowerCamelCase   ,    [output, output.lower()]       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:int									=			self.get_tokenizers()
       for tokenizer in tokenizers:
             with self.subTest(F'''{tokenizer.__class__.__name__}'''       ):
                   a		:Any									=			[
                       '''bos_token''',
                       '''eos_token''',
                       '''unk_token''',
                       '''sep_token''',
                       '''pad_token''',
                       '''cls_token''',
                       '''mask_token''',
                   ]

       a		:str									=			'''a'''
       a		:List[Any]									=			ord(_lowerCamelCase       )

       for attr in attributes_list:
             setattr(_lowerCamelCase   ,    attr + '''_id'''   ,    _lowerCamelCase       )
             self.assertEqual(getattr(_lowerCamelCase   ,    _lowerCamelCase       )   ,    _lowerCamelCase       )
             self.assertEqual(getattr(_lowerCamelCase   ,    attr + '''_id'''       )   ,    _lowerCamelCase       )

             setattr(_lowerCamelCase   ,    attr + '''_id'''   ,    _lowerCamelCase       )
             self.assertEqual(getattr(_lowerCamelCase   ,    _lowerCamelCase       )   ,    _lowerCamelCase       )
             self.assertEqual(getattr(_lowerCamelCase   ,    attr + '''_id'''       )   ,    _lowerCamelCase       )

       setattr(_lowerCamelCase   ,    '''additional_special_tokens_ids'''   ,    []       )
       self.assertListEqual(getattr(_lowerCamelCase   ,    '''additional_special_tokens'''       )   ,    []       )
       self.assertListEqual(getattr(_lowerCamelCase   ,    '''additional_special_tokens_ids'''       )   ,    []       )

       a		:List[Any]									=			0Xe_0_0_6
       a		:str									=			chr(_lowerCamelCase       )
       setattr(_lowerCamelCase   ,    '''additional_special_tokens_ids'''   ,    [additional_special_token_id]       )
       self.assertListEqual(getattr(_lowerCamelCase   ,    '''additional_special_tokens'''       )   ,    [additional_special_token]       )
       self.assertListEqual(getattr(_lowerCamelCase   ,    '''additional_special_tokens_ids'''       )   ,    [additional_special_token_id]       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass





