code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
'''simple docstring''' def SCREAMING_SNAKE_CASE( ) -> list[list[int]]: return [list(range(1_0_0_0 - i , -1_0_0_0 - i , -1 ) ) for i in range(1_0_0_0 )] UpperCamelCase = generate_large_matrix() UpperCamelCase = ( [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]], [[3, 2], [1, 0]], [[7, 7, 6]], [[7, 7, 6], [-1, -2, -3]], grid, ) def SCREAMING_SNAKE_CASE( __lowercase ) -> None: assert all(row == sorted(__lowercase , reverse=__lowercase ) for row in grid ) assert all(list(__lowercase ) == sorted(__lowercase , reverse=__lowercase ) for col in zip(*__lowercase ) ) def SCREAMING_SNAKE_CASE( __lowercase ) -> int: A: Tuple = 0 A: Any = len(__lowercase ) - 1 # Edge cases such as no values or all numbers are negative. if not array or array[0] < 0: return 0 while right + 1 > left: A: Optional[int] = (left + right) // 2 A: Tuple = array[mid] # Num must be negative and the index must be greater than or equal to 0. if num < 0 and array[mid - 1] >= 0: return mid if num >= 0: A: int = mid + 1 else: A: Union[str, Any] = mid - 1 # No negative numbers so return the last index of the array + 1 which is the length. return len(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> int: A: Optional[int] = 0 A: str = len(grid[0] ) for i in range(len(__lowercase ) ): A: Any = find_negative_index(grid[i][:bound] ) total += bound return (len(__lowercase ) * len(grid[0] )) - total def SCREAMING_SNAKE_CASE( __lowercase ) -> int: return len([number for row in grid for number in row if number < 0] ) def SCREAMING_SNAKE_CASE( __lowercase ) -> int: A: Optional[int] = 0 for row in grid: for i, number in enumerate(__lowercase ): if number < 0: total += len(__lowercase ) - i break return total def SCREAMING_SNAKE_CASE( ) -> None: from timeit import timeit print('''Running benchmarks''' ) A: List[Any] = ( '''from __main__ import count_negatives_binary_search, ''' '''count_negatives_brute_force, count_negatives_brute_force_with_break, grid''' ) for func in ( "count_negatives_binary_search", # took 0.7727 seconds "count_negatives_brute_force_with_break", # took 4.6505 seconds "count_negatives_brute_force", # took 12.8160 seconds ): A: Optional[int] = timeit(F"""{func}(grid=grid)""" , setup=__lowercase , number=5_0_0 ) print(F"""{func}() took {time:0.4f} seconds""" ) if __name__ == "__main__": import doctest doctest.testmod() benchmark()
319
'''simple docstring''' import warnings from ...utils import logging from .image_processing_beit import BeitImageProcessor UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> None: '''simple docstring''' warnings.warn( '''The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use BeitImageProcessor instead.''' , SCREAMING_SNAKE_CASE_ , ) super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ )
319
1
'''simple docstring''' import argparse import torch from ...utils import logging from . import AlbertConfig, AlbertForPreTraining, load_tf_weights_in_albert logging.set_verbosity_info() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> str: # Initialise PyTorch model A: Tuple = AlbertConfig.from_json_file(__lowercase ) print(F"""Building PyTorch model from configuration: {config}""" ) A: List[Any] = AlbertForPreTraining(__lowercase ) # Load weights from tf checkpoint load_tf_weights_in_albert(__lowercase , __lowercase , __lowercase ) # Save pytorch-model print(F"""Save PyTorch model to {pytorch_dump_path}""" ) torch.save(model.state_dict() , __lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--albert_config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained ALBERT model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) UpperCamelCase = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.albert_config_file, args.pytorch_dump_path)
319
'''simple docstring''' import os import pytest from transformers.dynamic_module_utils import get_imports UpperCamelCase = ''' import os ''' UpperCamelCase = ''' def foo(): import os return False ''' UpperCamelCase = ''' def foo(): def bar(): if True: import os return False return bar() ''' UpperCamelCase = ''' import os try: import bar except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os def foo(): try: import bar except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except (ImportError, AttributeError): raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except ImportError as e: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar import baz except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar import baz except ImportError: x = 1 raise ValueError() ''' UpperCamelCase = [ TOP_LEVEL_IMPORT, IMPORT_IN_FUNCTION, DEEPLY_NESTED_IMPORT, TOP_LEVEL_TRY_IMPORT, GENERIC_EXCEPT_IMPORT, MULTILINE_TRY_IMPORT, MULTILINE_BOTH_IMPORT, MULTIPLE_EXCEPTS_IMPORT, EXCEPT_AS_IMPORT, TRY_IMPORT_IN_FUNCTION, ] @pytest.mark.parametrize('''case''' , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Dict: A: Tuple = os.path.join(__lowercase , '''test_file.py''' ) with open(__lowercase , '''w''' ) as _tmp_file: _tmp_file.write(__lowercase ) A: List[Any] = get_imports(__lowercase ) assert parsed_imports == ["os"]
319
1
'''simple docstring''' import argparse import requests import torch from PIL import Image from transformers import SwinConfig, SwinForMaskedImageModeling, ViTImageProcessor def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: A: Any = SwinConfig(image_size=1_9_2 ) if "base" in model_name: A: Dict = 6 A: Optional[Any] = 1_2_8 A: Union[str, Any] = (2, 2, 1_8, 2) A: Any = (4, 8, 1_6, 3_2) elif "large" in model_name: A: Tuple = 1_2 A: Optional[int] = 1_9_2 A: Optional[Any] = (2, 2, 1_8, 2) A: Any = (6, 1_2, 2_4, 4_8) else: raise ValueError('''Model not supported, only supports base and large variants''' ) A: Optional[int] = window_size A: List[Any] = embed_dim A: Tuple = depths A: Union[str, Any] = num_heads return config def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: if "encoder.mask_token" in name: A: Tuple = name.replace('''encoder.mask_token''' , '''embeddings.mask_token''' ) if "encoder.patch_embed.proj" in name: A: Optional[int] = name.replace('''encoder.patch_embed.proj''' , '''embeddings.patch_embeddings.projection''' ) if "encoder.patch_embed.norm" in name: A: List[str] = name.replace('''encoder.patch_embed.norm''' , '''embeddings.norm''' ) if "attn.proj" in name: A: List[str] = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name: A: Tuple = name.replace('''attn''' , '''attention.self''' ) if "norm1" in name: A: Union[str, Any] = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: A: str = name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: A: Optional[int] = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: A: Tuple = name.replace('''mlp.fc2''' , '''output.dense''' ) if name == "encoder.norm.weight": A: List[Any] = '''layernorm.weight''' if name == "encoder.norm.bias": A: List[Any] = '''layernorm.bias''' if "decoder" in name: pass else: A: Any = '''swin.''' + name return name def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: for key in orig_state_dict.copy().keys(): A: Optional[int] = orig_state_dict.pop(__lowercase ) if "attn_mask" in key: pass elif "qkv" in key: A: List[str] = key.split('''.''' ) A: Dict = int(key_split[2] ) A: List[str] = int(key_split[4] ) A: str = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: A: Optional[Any] = val[:dim, :] A: Union[str, Any] = val[ dim : dim * 2, : ] A: int = val[-dim:, :] else: A: Optional[int] = val[ :dim ] A: List[str] = val[ dim : dim * 2 ] A: Optional[int] = val[ -dim: ] else: A: Optional[Any] = val return orig_state_dict def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase ) -> Optional[int]: A: List[Any] = torch.load(__lowercase , map_location='''cpu''' )['''model'''] A: Tuple = get_swin_config(__lowercase ) A: List[str] = SwinForMaskedImageModeling(__lowercase ) model.eval() A: List[str] = convert_state_dict(__lowercase , __lowercase ) model.load_state_dict(__lowercase ) A: Dict = '''http://images.cocodataset.org/val2017/000000039769.jpg''' A: Dict = ViTImageProcessor(size={'''height''': 1_9_2, '''width''': 1_9_2} ) A: List[Any] = Image.open(requests.get(__lowercase , stream=__lowercase ).raw ) A: List[Any] = image_processor(images=__lowercase , return_tensors='''pt''' ) with torch.no_grad(): A: List[str] = model(**__lowercase ).logits print(outputs.keys() ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(__lowercase ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(__lowercase ) if push_to_hub: print(F"""Pushing model and image processor for {model_name} to hub""" ) model.push_to_hub(F"""microsoft/{model_name}""" ) image_processor.push_to_hub(F"""microsoft/{model_name}""" ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''swin-base-simmim-window6-192''', type=str, choices=['''swin-base-simmim-window6-192''', '''swin-large-simmim-window12-192'''], help='''Name of the Swin SimMIM model you\'d like to convert.''', ) parser.add_argument( '''--checkpoint_path''', default='''/Users/nielsrogge/Documents/SwinSimMIM/simmim_pretrain__swin_base__img192_window6__100ep.pth''', type=str, help='''Path to the original PyTorch checkpoint (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) UpperCamelCase = parser.parse_args() convert_swin_checkpoint(args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
319
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( BertTokenizer, ViltConfig, ViltForImageAndTextRetrieval, ViltForImagesAndTextClassification, ViltForMaskedLM, ViltForQuestionAnswering, ViltImageProcessor, ViltProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=False , __lowercase=False , __lowercase=False ) -> Optional[Any]: A: str = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""transformer.blocks.{i}.norm1.weight""", F"""vilt.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.norm1.bias""", F"""vilt.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append( (F"""transformer.blocks.{i}.attn.proj.weight""", F"""vilt.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append( (F"""transformer.blocks.{i}.attn.proj.bias""", F"""vilt.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""transformer.blocks.{i}.norm2.weight""", F"""vilt.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.norm2.bias""", F"""vilt.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append( (F"""transformer.blocks.{i}.mlp.fc1.weight""", F"""vilt.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc1.bias""", F"""vilt.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc2.weight""", F"""vilt.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc2.bias""", F"""vilt.encoder.layer.{i}.output.dense.bias""") ) # embeddings rename_keys.extend( [ # text embeddings ('''text_embeddings.word_embeddings.weight''', '''vilt.embeddings.text_embeddings.word_embeddings.weight'''), ( '''text_embeddings.position_embeddings.weight''', '''vilt.embeddings.text_embeddings.position_embeddings.weight''', ), ('''text_embeddings.position_ids''', '''vilt.embeddings.text_embeddings.position_ids'''), ( '''text_embeddings.token_type_embeddings.weight''', '''vilt.embeddings.text_embeddings.token_type_embeddings.weight''', ), ('''text_embeddings.LayerNorm.weight''', '''vilt.embeddings.text_embeddings.LayerNorm.weight'''), ('''text_embeddings.LayerNorm.bias''', '''vilt.embeddings.text_embeddings.LayerNorm.bias'''), # patch embeddings ('''transformer.cls_token''', '''vilt.embeddings.cls_token'''), ('''transformer.patch_embed.proj.weight''', '''vilt.embeddings.patch_embeddings.projection.weight'''), ('''transformer.patch_embed.proj.bias''', '''vilt.embeddings.patch_embeddings.projection.bias'''), ('''transformer.pos_embed''', '''vilt.embeddings.position_embeddings'''), # token type embeddings ('''token_type_embeddings.weight''', '''vilt.embeddings.token_type_embeddings.weight'''), ] ) # final layernorm + pooler rename_keys.extend( [ ('''transformer.norm.weight''', '''vilt.layernorm.weight'''), ('''transformer.norm.bias''', '''vilt.layernorm.bias'''), ('''pooler.dense.weight''', '''vilt.pooler.dense.weight'''), ('''pooler.dense.bias''', '''vilt.pooler.dense.bias'''), ] ) # classifier head(s) if vqa_model: # classification head rename_keys.extend( [ ('''vqa_classifier.0.weight''', '''classifier.0.weight'''), ('''vqa_classifier.0.bias''', '''classifier.0.bias'''), ('''vqa_classifier.1.weight''', '''classifier.1.weight'''), ('''vqa_classifier.1.bias''', '''classifier.1.bias'''), ('''vqa_classifier.3.weight''', '''classifier.3.weight'''), ('''vqa_classifier.3.bias''', '''classifier.3.bias'''), ] ) elif nlvr_model: # classification head rename_keys.extend( [ ('''nlvr2_classifier.0.weight''', '''classifier.0.weight'''), ('''nlvr2_classifier.0.bias''', '''classifier.0.bias'''), ('''nlvr2_classifier.1.weight''', '''classifier.1.weight'''), ('''nlvr2_classifier.1.bias''', '''classifier.1.bias'''), ('''nlvr2_classifier.3.weight''', '''classifier.3.weight'''), ('''nlvr2_classifier.3.bias''', '''classifier.3.bias'''), ] ) else: pass return rename_keys def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Any: for i in range(config.num_hidden_layers ): A: Tuple = '''vilt.''' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A: List[str] = state_dict.pop(F"""transformer.blocks.{i}.attn.qkv.weight""" ) A: Optional[Any] = state_dict.pop(F"""transformer.blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict A: Dict = in_proj_weight[ : config.hidden_size, : ] A: int = in_proj_bias[: config.hidden_size] A: Any = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A: int = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A: Optional[int] = in_proj_weight[ -config.hidden_size :, : ] A: Optional[Any] = in_proj_bias[-config.hidden_size :] def SCREAMING_SNAKE_CASE( __lowercase ) -> int: A: Optional[int] = ['''head.weight''', '''head.bias'''] for k in ignore_keys: state_dict.pop(__lowercase , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: A: List[Any] = dct.pop(__lowercase ) A: int = val @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str: A: Optional[Any] = ViltConfig(image_size=3_8_4 , patch_size=3_2 , tie_word_embeddings=__lowercase ) A: Tuple = False A: str = False A: List[Any] = False A: Optional[int] = False if "vqa" in checkpoint_url: A: Union[str, Any] = True A: Union[str, Any] = 3_1_2_9 A: List[Any] = '''huggingface/label-files''' A: Any = '''vqa2-id2label.json''' A: Optional[Any] = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: Union[str, Any] = {int(__lowercase ): v for k, v in idalabel.items()} A: Any = idalabel A: Optional[Any] = {v: k for k, v in idalabel.items()} A: List[str] = ViltForQuestionAnswering(__lowercase ) elif "nlvr" in checkpoint_url: A: Dict = True A: str = 2 A: Union[str, Any] = {0: '''False''', 1: '''True'''} A: Any = {v: k for k, v in config.idalabel.items()} A: Optional[Any] = 3 A: Any = ViltForImagesAndTextClassification(__lowercase ) elif "irtr" in checkpoint_url: A: Tuple = True A: Optional[Any] = ViltForImageAndTextRetrieval(__lowercase ) elif "mlm_itm" in checkpoint_url: A: Tuple = True A: Optional[int] = ViltForMaskedLM(__lowercase ) else: raise ValueError('''Unknown model type''' ) # load state_dict of original model, remove and rename some keys A: int = torch.hub.load_state_dict_from_url(__lowercase , map_location='''cpu''' )['''state_dict'''] A: List[str] = create_rename_keys(__lowercase , __lowercase , __lowercase , __lowercase ) for src, dest in rename_keys: rename_key(__lowercase , __lowercase , __lowercase ) read_in_q_k_v(__lowercase , __lowercase ) if mlm_model or irtr_model: A: str = ['''itm_score.fc.weight''', '''itm_score.fc.bias'''] for k in ignore_keys: state_dict.pop(__lowercase , __lowercase ) # load state dict into HuggingFace model model.eval() if mlm_model: A , A: Union[str, Any] = model.load_state_dict(__lowercase , strict=__lowercase ) assert missing_keys == ["mlm_score.decoder.bias"] else: model.load_state_dict(__lowercase ) # Define processor A: Optional[Any] = ViltImageProcessor(size=3_8_4 ) A: Dict = BertTokenizer.from_pretrained('''bert-base-uncased''' ) A: Optional[int] = ViltProcessor(__lowercase , __lowercase ) # Forward pass on example inputs (image + text) if nlvr_model: A: str = Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg''' , stream=__lowercase ).raw ) A: List[str] = Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg''' , stream=__lowercase ).raw ) A: Any = ( '''The left image contains twice the number of dogs as the right image, and at least two dogs in total are''' ''' standing.''' ) A: List[Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: List[Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: List[str] = model( input_ids=encoding_a.input_ids , pixel_values=encoding_a.pixel_values , pixel_values_a=encoding_a.pixel_values , ) else: A: Any = Image.open(requests.get('''http://images.cocodataset.org/val2017/000000039769.jpg''' , stream=__lowercase ).raw ) if mlm_model: A: Optional[int] = '''a bunch of [MASK] laying on a [MASK].''' else: A: Optional[int] = '''How many cats are there?''' A: Union[str, Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: Any = model(**__lowercase ) # Verify outputs if mlm_model: A: Any = torch.Size([1, 1_1, 3_0_5_2_2] ) A: Tuple = torch.tensor([-1_2.5_0_6_1, -1_2.5_1_2_3, -1_2.5_1_7_4] ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , __lowercase , atol=1E-4 ) # verify masked token prediction equals "cats" A: List[str] = outputs.logits[0, 4, :].argmax(-1 ).item() assert tokenizer.decode([predicted_id] ) == "cats" elif vqa_model: A: Any = torch.Size([1, 3_1_2_9] ) A: Optional[int] = torch.tensor([-1_5.9_4_9_5, -1_8.1_4_7_2, -1_0.3_0_4_1] ) assert torch.allclose(outputs.logits[0, :3] , __lowercase , atol=1E-4 ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , __lowercase , atol=1E-4 ) # verify vqa prediction equals "2" A: Dict = outputs.logits.argmax(-1 ).item() assert model.config.idalabel[predicted_idx] == "2" elif nlvr_model: A: Union[str, Any] = torch.Size([1, 2] ) A: Optional[Any] = torch.tensor([-2.8_7_2_1, 2.1_2_9_1] ) assert torch.allclose(outputs.logits[0, :3] , __lowercase , atol=1E-4 ) assert outputs.logits.shape == expected_shape Path(__lowercase ).mkdir(exist_ok=__lowercase ) print(F"""Saving model and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(__lowercase ) processor.save_pretrained(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://github.com/dandelin/ViLT/releases/download/200k/vilt_200k_mlm_itm.ckpt''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) UpperCamelCase = parser.parse_args() convert_vilt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
319
1
'''simple docstring''' import inspect import unittest from datasets import load_dataset from packaging import version from transformers import BeitConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_MAPPING, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, ) from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): import PIL from PIL import Image from transformers import BeitImageProcessor class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Dict=1_00 , SCREAMING_SNAKE_CASE_ : int=13 , SCREAMING_SNAKE_CASE_ : Tuple=30 , SCREAMING_SNAKE_CASE_ : Dict=2 , SCREAMING_SNAKE_CASE_ : Any=3 , SCREAMING_SNAKE_CASE_ : Any=True , SCREAMING_SNAKE_CASE_ : int=True , SCREAMING_SNAKE_CASE_ : List[str]=32 , SCREAMING_SNAKE_CASE_ : str=4 , SCREAMING_SNAKE_CASE_ : Dict=4 , SCREAMING_SNAKE_CASE_ : List[Any]=37 , SCREAMING_SNAKE_CASE_ : str="gelu" , SCREAMING_SNAKE_CASE_ : Any=0.1 , SCREAMING_SNAKE_CASE_ : Dict=0.1 , SCREAMING_SNAKE_CASE_ : Any=10 , SCREAMING_SNAKE_CASE_ : int=0.02 , SCREAMING_SNAKE_CASE_ : List[str]=3 , SCREAMING_SNAKE_CASE_ : List[str]=None , SCREAMING_SNAKE_CASE_ : Dict=[0, 1, 2, 3] , ) -> str: '''simple docstring''' A: Dict = parent A: Optional[Any] = 1_00 A: Dict = batch_size A: Union[str, Any] = image_size A: Tuple = patch_size A: Optional[Any] = num_channels A: List[Any] = is_training A: Optional[Any] = use_labels A: str = hidden_size A: Optional[Any] = num_hidden_layers A: List[Any] = num_attention_heads A: Dict = intermediate_size A: List[str] = hidden_act A: Any = hidden_dropout_prob A: Optional[Any] = attention_probs_dropout_prob A: Optional[int] = type_sequence_label_size A: Dict = initializer_range A: str = scope A: Any = out_indices A: Dict = num_labels # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) A: Dict = (image_size // patch_size) ** 2 A: Tuple = num_patches + 1 def _snake_case ( self : Any ) -> str: '''simple docstring''' A: List[Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A: Tuple = None A: str = None if self.use_labels: A: Optional[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A: List[Any] = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) A: Union[str, Any] = self.get_config() return config, pixel_values, labels, pixel_labels def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' return BeitConfig( vocab_size=self.vocab_size , image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=SCREAMING_SNAKE_CASE_ , initializer_range=self.initializer_range , out_indices=self.out_indices , ) def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Any ) -> Optional[Any]: '''simple docstring''' A: List[str] = BeitModel(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() A: Optional[int] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Dict ) -> List[str]: '''simple docstring''' A: Union[str, Any] = BeitForMaskedImageModeling(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() A: Union[str, Any] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> List[Any]: '''simple docstring''' A: Optional[Any] = self.type_sequence_label_size A: Dict = BeitForImageClassification(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() A: int = model(SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images A: int = 1 A: str = BeitForImageClassification(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() A: List[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A: Tuple = model(SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _snake_case ( self : Dict , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : List[str] ) -> Union[str, Any]: '''simple docstring''' A: Optional[Any] = self.num_labels A: Union[str, Any] = BeitForSemanticSegmentation(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() A: str = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) A: Dict = model(SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) def _snake_case ( self : List[str] ) -> Dict: '''simple docstring''' A: str = self.prepare_config_and_inputs() A , A , A , A: Optional[int] = config_and_inputs A: Tuple = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Tuple = ( (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation) if is_torch_available() else () ) UpperCamelCase_ : Dict = ( { """feature-extraction""": BeitModel, """image-classification""": BeitForImageClassification, """image-segmentation""": BeitForSemanticSegmentation, } if is_torch_available() else {} ) UpperCamelCase_ : str = False UpperCamelCase_ : Tuple = False UpperCamelCase_ : Optional[int] = False def _snake_case ( self : Tuple ) -> Any: '''simple docstring''' A: str = BeitModelTester(self ) A: Any = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE_ , has_text_modality=SCREAMING_SNAKE_CASE_ , hidden_size=37 ) def _snake_case ( self : Union[str, Any] ) -> Dict: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='''BEiT does not use inputs_embeds''' ) def _snake_case ( self : int ) -> Dict: '''simple docstring''' pass @require_torch_multi_gpu @unittest.skip(reason='''BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`''' ) def _snake_case ( self : Tuple ) -> Optional[int]: '''simple docstring''' pass def _snake_case ( self : Tuple ) -> List[Any]: '''simple docstring''' A , A: Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A: Optional[int] = model_class(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A: Any = model.get_output_embeddings() self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE_ , nn.Linear ) ) def _snake_case ( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' A , A: List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A: Optional[int] = model_class(SCREAMING_SNAKE_CASE_ ) A: Tuple = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A: str = [*signature.parameters.keys()] A: Dict = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' A: List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] ) -> List[Any]: '''simple docstring''' A: List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int ) -> int: '''simple docstring''' A: Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[int] ) -> Optional[int]: '''simple docstring''' A: List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' if not self.model_tester.is_training: return A , A: str = self.model_tester.prepare_config_and_inputs_for_common() A: Tuple = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if model_class in [*get_values(SCREAMING_SNAKE_CASE_ ), BeitForMaskedImageModeling]: continue A: Optional[Any] = model_class(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.train() A: str = self._prepare_for_class(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , return_labels=SCREAMING_SNAKE_CASE_ ) A: int = model(**SCREAMING_SNAKE_CASE_ ).loss loss.backward() def _snake_case ( self : Optional[Any] ) -> Any: '''simple docstring''' A , A: Dict = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return A: List[str] = False A: List[str] = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if ( model_class in [*get_values(SCREAMING_SNAKE_CASE_ ), BeitForMaskedImageModeling] or not model_class.supports_gradient_checkpointing ): continue A: List[Any] = model_class(SCREAMING_SNAKE_CASE_ ) model.gradient_checkpointing_enable() model.to(SCREAMING_SNAKE_CASE_ ) model.train() A: Optional[int] = self._prepare_for_class(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , return_labels=SCREAMING_SNAKE_CASE_ ) A: int = model(**SCREAMING_SNAKE_CASE_ ).loss loss.backward() def _snake_case ( self : Optional[int] ) -> Tuple: '''simple docstring''' A , A: Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() A: Any = _config_zero_init(SCREAMING_SNAKE_CASE_ ) for model_class in self.all_model_classes: A: Any = model_class(config=SCREAMING_SNAKE_CASE_ ) for name, param in model.named_parameters(): # we skip lambda parameters as these require special initial values # determined by config.layer_scale_init_value if "lambda" in name: continue if param.requires_grad: self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=f"""Parameter {name} of model {model_class} seems not properly initialized""" , ) @slow def _snake_case ( self : int ) -> Any: '''simple docstring''' for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A: Tuple = BeitModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE( ) -> Optional[Any]: A: Any = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' @cached_property def _snake_case ( self : Union[str, Any] ) -> Any: '''simple docstring''' return BeitImageProcessor.from_pretrained('''microsoft/beit-base-patch16-224''' ) if is_vision_available() else None @slow def _snake_case ( self : str ) -> Optional[int]: '''simple docstring''' A: str = BeitForMaskedImageModeling.from_pretrained('''microsoft/beit-base-patch16-224-pt22k''' ).to(SCREAMING_SNAKE_CASE_ ) A: int = self.default_image_processor A: Any = prepare_img() A: Union[str, Any] = image_processor(images=SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).pixel_values.to(SCREAMING_SNAKE_CASE_ ) # prepare bool_masked_pos A: List[str] = torch.ones((1, 1_96) , dtype=torch.bool ).to(SCREAMING_SNAKE_CASE_ ) # forward pass with torch.no_grad(): A: Union[str, Any] = model(pixel_values=SCREAMING_SNAKE_CASE_ , bool_masked_pos=SCREAMING_SNAKE_CASE_ ) A: Tuple = outputs.logits # verify the logits A: Optional[int] = torch.Size((1, 1_96, 81_92) ) self.assertEqual(logits.shape , SCREAMING_SNAKE_CASE_ ) A: List[str] = torch.tensor( [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]] ).to(SCREAMING_SNAKE_CASE_ ) self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] , SCREAMING_SNAKE_CASE_ , atol=1E-2 ) ) @slow def _snake_case ( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' A: Optional[int] = BeitForImageClassification.from_pretrained('''microsoft/beit-base-patch16-224''' ).to(SCREAMING_SNAKE_CASE_ ) A: List[str] = self.default_image_processor A: Any = prepare_img() A: Any = image_processor(images=SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).to(SCREAMING_SNAKE_CASE_ ) # forward pass with torch.no_grad(): A: int = model(**SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = outputs.logits # verify the logits A: Any = torch.Size((1, 10_00) ) self.assertEqual(logits.shape , SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = torch.tensor([-1.2385, -1.0987, -1.0108] ).to(SCREAMING_SNAKE_CASE_ ) self.assertTrue(torch.allclose(logits[0, :3] , SCREAMING_SNAKE_CASE_ , atol=1E-4 ) ) A: Tuple = 2_81 self.assertEqual(logits.argmax(-1 ).item() , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Any ) -> List[str]: '''simple docstring''' A: Dict = BeitForImageClassification.from_pretrained('''microsoft/beit-large-patch16-224-pt22k-ft22k''' ).to( SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = self.default_image_processor A: Optional[Any] = prepare_img() A: Tuple = image_processor(images=SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).to(SCREAMING_SNAKE_CASE_ ) # forward pass with torch.no_grad(): A: Dict = model(**SCREAMING_SNAKE_CASE_ ) A: Optional[int] = outputs.logits # verify the logits A: Optional[int] = torch.Size((1, 2_18_41) ) self.assertEqual(logits.shape , SCREAMING_SNAKE_CASE_ ) A: List[Any] = torch.tensor([1.6881, -0.2787, 0.5901] ).to(SCREAMING_SNAKE_CASE_ ) self.assertTrue(torch.allclose(logits[0, :3] , SCREAMING_SNAKE_CASE_ , atol=1E-4 ) ) A: Dict = 23_96 self.assertEqual(logits.argmax(-1 ).item() , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Optional[Any] ) -> int: '''simple docstring''' A: Optional[int] = BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640''' ) A: Dict = model.to(SCREAMING_SNAKE_CASE_ ) A: List[str] = BeitImageProcessor(do_resize=SCREAMING_SNAKE_CASE_ , size=6_40 , do_center_crop=SCREAMING_SNAKE_CASE_ ) A: str = load_dataset('''hf-internal-testing/fixtures_ade20k''' , split='''test''' ) A: Optional[int] = Image.open(ds[0]['''file'''] ) A: str = image_processor(images=SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).to(SCREAMING_SNAKE_CASE_ ) # forward pass with torch.no_grad(): A: List[str] = model(**SCREAMING_SNAKE_CASE_ ) A: Tuple = outputs.logits # verify the logits A: int = torch.Size((1, 1_50, 1_60, 1_60) ) self.assertEqual(logits.shape , SCREAMING_SNAKE_CASE_ ) A: List[Any] = version.parse(PIL.__version__ ) < version.parse('''9.0.0''' ) if is_pillow_less_than_a: A: Dict = torch.tensor( [ [[-4.9225, -2.3954, -3.0522], [-2.8822, -1.0046, -1.7561], [-2.9549, -1.3228, -2.1347]], [[-5.8168, -3.4129, -4.0778], [-3.8651, -2.2214, -3.0277], [-3.8356, -2.4643, -3.3535]], [[-0.0078, 3.9952, 4.0754], [2.9856, 4.6944, 5.0035], [3.2413, 4.7813, 4.9969]], ] , device=SCREAMING_SNAKE_CASE_ , ) else: A: Tuple = torch.tensor( [ [[-4.8960, -2.3688, -3.0355], [-2.8478, -0.9836, -1.7418], [-2.9449, -1.3332, -2.1456]], [[-5.8081, -3.4124, -4.1006], [-3.8561, -2.2081, -3.0323], [-3.8365, -2.4601, -3.3669]], [[-0.0309, 3.9868, 4.0540], [2.9640, 4.6877, 4.9976], [3.2081, 4.7690, 4.9942]], ] , device=SCREAMING_SNAKE_CASE_ , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=1E-4 ) ) @slow def _snake_case ( self : Union[str, Any] ) -> Tuple: '''simple docstring''' A: List[str] = BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640''' ) A: Optional[Any] = model.to(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = BeitImageProcessor(do_resize=SCREAMING_SNAKE_CASE_ , size=6_40 , do_center_crop=SCREAMING_SNAKE_CASE_ ) A: Dict = load_dataset('''hf-internal-testing/fixtures_ade20k''' , split='''test''' ) A: Optional[Any] = Image.open(ds[0]['''file'''] ) A: List[str] = image_processor(images=SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).to(SCREAMING_SNAKE_CASE_ ) # forward pass with torch.no_grad(): A: Optional[int] = model(**SCREAMING_SNAKE_CASE_ ) A: Tuple = outputs.logits.detach().cpu() A: Dict = image_processor.post_process_semantic_segmentation(outputs=SCREAMING_SNAKE_CASE_ , target_sizes=[(5_00, 3_00)] ) A: Dict = torch.Size((5_00, 3_00) ) self.assertEqual(segmentation[0].shape , SCREAMING_SNAKE_CASE_ ) A: List[str] = image_processor.post_process_semantic_segmentation(outputs=SCREAMING_SNAKE_CASE_ ) A: List[str] = torch.Size((1_60, 1_60) ) self.assertEqual(segmentation[0].shape , SCREAMING_SNAKE_CASE_ )
319
'''simple docstring''' import argparse import json import os import numpy as np import PIL import requests import tensorflow.keras.applications.efficientnet as efficientnet import torch from huggingface_hub import hf_hub_download from PIL import Image from tensorflow.keras.preprocessing import image from transformers import ( EfficientNetConfig, EfficientNetForImageClassification, EfficientNetImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''b0''': efficientnet.EfficientNetBa, '''b1''': efficientnet.EfficientNetBa, '''b2''': efficientnet.EfficientNetBa, '''b3''': efficientnet.EfficientNetBa, '''b4''': efficientnet.EfficientNetBa, '''b5''': efficientnet.EfficientNetBa, '''b6''': efficientnet.EfficientNetBa, '''b7''': efficientnet.EfficientNetBa, } UpperCamelCase = { '''b0''': { '''hidden_dim''': 1280, '''width_coef''': 1.0, '''depth_coef''': 1.0, '''image_size''': 224, '''dropout_rate''': 0.2, '''dw_padding''': [], }, '''b1''': { '''hidden_dim''': 1280, '''width_coef''': 1.0, '''depth_coef''': 1.1, '''image_size''': 240, '''dropout_rate''': 0.2, '''dw_padding''': [16], }, '''b2''': { '''hidden_dim''': 1408, '''width_coef''': 1.1, '''depth_coef''': 1.2, '''image_size''': 260, '''dropout_rate''': 0.3, '''dw_padding''': [5, 8, 16], }, '''b3''': { '''hidden_dim''': 1536, '''width_coef''': 1.2, '''depth_coef''': 1.4, '''image_size''': 300, '''dropout_rate''': 0.3, '''dw_padding''': [5, 18], }, '''b4''': { '''hidden_dim''': 1792, '''width_coef''': 1.4, '''depth_coef''': 1.8, '''image_size''': 380, '''dropout_rate''': 0.4, '''dw_padding''': [6], }, '''b5''': { '''hidden_dim''': 2048, '''width_coef''': 1.6, '''depth_coef''': 2.2, '''image_size''': 456, '''dropout_rate''': 0.4, '''dw_padding''': [13, 27], }, '''b6''': { '''hidden_dim''': 2304, '''width_coef''': 1.8, '''depth_coef''': 2.6, '''image_size''': 528, '''dropout_rate''': 0.5, '''dw_padding''': [31], }, '''b7''': { '''hidden_dim''': 2560, '''width_coef''': 2.0, '''depth_coef''': 3.1, '''image_size''': 600, '''dropout_rate''': 0.5, '''dw_padding''': [18], }, } def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: A: Tuple = EfficientNetConfig() A: Optional[int] = CONFIG_MAP[model_name]['''hidden_dim'''] A: Optional[int] = CONFIG_MAP[model_name]['''width_coef'''] A: str = CONFIG_MAP[model_name]['''depth_coef'''] A: Dict = CONFIG_MAP[model_name]['''image_size'''] A: str = CONFIG_MAP[model_name]['''dropout_rate'''] A: Optional[Any] = CONFIG_MAP[model_name]['''dw_padding'''] A: Optional[Any] = '''huggingface/label-files''' A: List[str] = '''imagenet-1k-id2label.json''' A: Dict = 1_0_0_0 A: Any = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: Tuple = {int(__lowercase ): v for k, v in idalabel.items()} A: int = idalabel A: Tuple = {v: k for k, v in idalabel.items()} return config def SCREAMING_SNAKE_CASE( ) -> Any: A: Optional[Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' A: Union[str, Any] = Image.open(requests.get(__lowercase , stream=__lowercase ).raw ) return im def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: A: List[str] = CONFIG_MAP[model_name]['''image_size'''] A: List[Any] = EfficientNetImageProcessor( size={'''height''': size, '''width''': size} , image_mean=[0.4_8_5, 0.4_5_6, 0.4_0_6] , image_std=[0.4_7_8_5_3_9_4_4, 0.4_7_3_2_8_6_4, 0.4_7_4_3_4_1_6_3] , do_center_crop=__lowercase , ) return preprocessor def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: A: List[str] = [v.split('''_''' )[0].split('''block''' )[1] for v in original_param_names if v.startswith('''block''' )] A: List[str] = sorted(set(__lowercase ) ) A: Dict = len(__lowercase ) A: List[str] = {b: str(__lowercase ) for b, i in zip(__lowercase , range(__lowercase ) )} A: Optional[int] = [] rename_keys.append(('''stem_conv/kernel:0''', '''embeddings.convolution.weight''') ) rename_keys.append(('''stem_bn/gamma:0''', '''embeddings.batchnorm.weight''') ) rename_keys.append(('''stem_bn/beta:0''', '''embeddings.batchnorm.bias''') ) rename_keys.append(('''stem_bn/moving_mean:0''', '''embeddings.batchnorm.running_mean''') ) rename_keys.append(('''stem_bn/moving_variance:0''', '''embeddings.batchnorm.running_var''') ) for b in block_names: A: int = block_name_mapping[b] rename_keys.append((F"""block{b}_expand_conv/kernel:0""", F"""encoder.blocks.{hf_b}.expansion.expand_conv.weight""") ) rename_keys.append((F"""block{b}_expand_bn/gamma:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.weight""") ) rename_keys.append((F"""block{b}_expand_bn/beta:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.bias""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_mean""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_var""") ) rename_keys.append( (F"""block{b}_dwconv/depthwise_kernel:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight""") ) rename_keys.append((F"""block{b}_bn/gamma:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight""") ) rename_keys.append((F"""block{b}_bn/beta:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias""") ) rename_keys.append( (F"""block{b}_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean""") ) rename_keys.append( (F"""block{b}_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var""") ) rename_keys.append((F"""block{b}_se_reduce/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.weight""") ) rename_keys.append((F"""block{b}_se_reduce/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.bias""") ) rename_keys.append((F"""block{b}_se_expand/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.weight""") ) rename_keys.append((F"""block{b}_se_expand/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.bias""") ) rename_keys.append( (F"""block{b}_project_conv/kernel:0""", F"""encoder.blocks.{hf_b}.projection.project_conv.weight""") ) rename_keys.append((F"""block{b}_project_bn/gamma:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.weight""") ) rename_keys.append((F"""block{b}_project_bn/beta:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.bias""") ) rename_keys.append( (F"""block{b}_project_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_mean""") ) rename_keys.append( (F"""block{b}_project_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_var""") ) rename_keys.append(('''top_conv/kernel:0''', '''encoder.top_conv.weight''') ) rename_keys.append(('''top_bn/gamma:0''', '''encoder.top_bn.weight''') ) rename_keys.append(('''top_bn/beta:0''', '''encoder.top_bn.bias''') ) rename_keys.append(('''top_bn/moving_mean:0''', '''encoder.top_bn.running_mean''') ) rename_keys.append(('''top_bn/moving_variance:0''', '''encoder.top_bn.running_var''') ) A: Union[str, Any] = {} for item in rename_keys: if item[0] in original_param_names: A: str = '''efficientnet.''' + item[1] A: int = '''classifier.weight''' A: Tuple = '''classifier.bias''' return key_mapping def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Tuple: for key, value in tf_params.items(): if "normalization" in key: continue A: Union[str, Any] = key_mapping[key] if "_conv" in key and "kernel" in key: A: List[str] = torch.from_numpy(__lowercase ).permute(3 , 2 , 0 , 1 ) elif "depthwise_kernel" in key: A: List[Any] = torch.from_numpy(__lowercase ).permute(2 , 3 , 0 , 1 ) elif "kernel" in key: A: Optional[Any] = torch.from_numpy(np.transpose(__lowercase ) ) else: A: Any = torch.from_numpy(__lowercase ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(__lowercase ) @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase ) -> Tuple: A: Optional[int] = model_classes[model_name]( include_top=__lowercase , weights='''imagenet''' , input_tensor=__lowercase , input_shape=__lowercase , pooling=__lowercase , classes=1_0_0_0 , classifier_activation='''softmax''' , ) A: List[str] = original_model.trainable_variables A: Optional[Any] = original_model.non_trainable_variables A: Union[str, Any] = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: A: int = param.numpy() A: Tuple = list(tf_params.keys() ) # Load HuggingFace model A: Dict = get_efficientnet_config(__lowercase ) A: Union[str, Any] = EfficientNetForImageClassification(__lowercase ).eval() A: Dict = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print('''Converting parameters...''' ) A: int = rename_keys(__lowercase ) replace_params(__lowercase , __lowercase , __lowercase ) # Initialize preprocessor and preprocess input image A: List[Any] = convert_image_processor(__lowercase ) A: Optional[Any] = preprocessor(images=prepare_img() , return_tensors='''pt''' ) # HF model inference hf_model.eval() with torch.no_grad(): A: str = hf_model(**__lowercase ) A: List[Any] = outputs.logits.detach().numpy() # Original model inference A: Any = False A: List[Any] = CONFIG_MAP[model_name]['''image_size'''] A: List[Any] = prepare_img().resize((image_size, image_size) , resample=PIL.Image.NEAREST ) A: str = image.img_to_array(__lowercase ) A: Dict = np.expand_dims(__lowercase , axis=0 ) A: Any = original_model.predict(__lowercase ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(__lowercase , __lowercase , atol=1E-3 ), "The predicted logits are not the same." print('''Model outputs match!''' ) if save_model: # Create folder to save model if not os.path.isdir(__lowercase ): os.mkdir(__lowercase ) # Save converted model and image processor hf_model.save_pretrained(__lowercase ) preprocessor.save_pretrained(__lowercase ) if push_to_hub: # Push model and image processor to hub print(F"""Pushing converted {model_name} to the hub...""" ) A: int = F"""efficientnet-{model_name}""" preprocessor.push_to_hub(__lowercase ) hf_model.push_to_hub(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''b0''', type=str, help='''Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''hf_model''', type=str, help='''Path to the output PyTorch model directory.''', ) parser.add_argument('''--save_model''', action='''store_true''', help='''Save model to local''') parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Push model and image processor to the hub''') UpperCamelCase = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
319
1
'''simple docstring''' from dataclasses import dataclass from typing import Dict, Optional, Union import torch import torch.nn.functional as F from torch import nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .attention import BasicTransformerBlock from .attention_processor import AttentionProcessor, AttnProcessor from .embeddings import TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin @dataclass class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : torch.FloatTensor class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' @register_to_config def __init__( self : str , SCREAMING_SNAKE_CASE_ : int = 32 , SCREAMING_SNAKE_CASE_ : int = 64 , SCREAMING_SNAKE_CASE_ : int = 20 , SCREAMING_SNAKE_CASE_ : int = 7_68 , SCREAMING_SNAKE_CASE_ : Any=77 , SCREAMING_SNAKE_CASE_ : Optional[int]=4 , SCREAMING_SNAKE_CASE_ : float = 0.0 , SCREAMING_SNAKE_CASE_ : str = "silu" , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : Optional[str] = "linear" , SCREAMING_SNAKE_CASE_ : Optional[str] = "prd" , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[int] = None , ) -> Optional[Any]: '''simple docstring''' super().__init__() A: Any = num_attention_heads A: Optional[int] = attention_head_dim A: Any = num_attention_heads * attention_head_dim A: Optional[int] = additional_embeddings A: List[Any] = time_embed_dim or inner_dim A: Optional[int] = embedding_proj_dim or embedding_dim A: str = clip_embed_dim or embedding_dim A: Tuple = Timesteps(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , 0 ) A: int = TimestepEmbedding(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , out_dim=SCREAMING_SNAKE_CASE_ , act_fn=SCREAMING_SNAKE_CASE_ ) A: Any = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if embedding_proj_norm_type is None: A: str = None elif embedding_proj_norm_type == "layer": A: Dict = nn.LayerNorm(SCREAMING_SNAKE_CASE_ ) else: raise ValueError(f"""unsupported embedding_proj_norm_type: {embedding_proj_norm_type}""" ) A: Tuple = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if encoder_hid_proj_type is None: A: List[str] = None elif encoder_hid_proj_type == "linear": A: str = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else: raise ValueError(f"""unsupported encoder_hid_proj_type: {encoder_hid_proj_type}""" ) A: Union[str, Any] = nn.Parameter(torch.zeros(1 , num_embeddings + additional_embeddings , SCREAMING_SNAKE_CASE_ ) ) if added_emb_type == "prd": A: List[Any] = nn.Parameter(torch.zeros(1 , 1 , SCREAMING_SNAKE_CASE_ ) ) elif added_emb_type is None: A: List[Any] = None else: raise ValueError( f"""`added_emb_type`: {added_emb_type} is not supported. Make sure to choose one of `'prd'` or `None`.""" ) A: Dict = nn.ModuleList( [ BasicTransformerBlock( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , dropout=SCREAMING_SNAKE_CASE_ , activation_fn='''gelu''' , attention_bias=SCREAMING_SNAKE_CASE_ , ) for d in range(SCREAMING_SNAKE_CASE_ ) ] ) if norm_in_type == "layer": A: Tuple = nn.LayerNorm(SCREAMING_SNAKE_CASE_ ) elif norm_in_type is None: A: List[str] = None else: raise ValueError(f"""Unsupported norm_in_type: {norm_in_type}.""" ) A: Any = nn.LayerNorm(SCREAMING_SNAKE_CASE_ ) A: Tuple = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = torch.full( [num_embeddings + additional_embeddings, num_embeddings + additional_embeddings] , -1_0000.0 ) causal_attention_mask.triu_(1 ) A: List[str] = causal_attention_mask[None, ...] self.register_buffer('''causal_attention_mask''' , SCREAMING_SNAKE_CASE_ , persistent=SCREAMING_SNAKE_CASE_ ) A: List[Any] = nn.Parameter(torch.zeros(1 , SCREAMING_SNAKE_CASE_ ) ) A: Tuple = nn.Parameter(torch.zeros(1 , SCREAMING_SNAKE_CASE_ ) ) @property # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors def _snake_case ( self : Optional[Any] ) -> Dict[str, AttentionProcessor]: '''simple docstring''' A: Tuple = {} def fn_recursive_add_processors(SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : torch.nn.Module , SCREAMING_SNAKE_CASE_ : Dict[str, AttentionProcessor] ): if hasattr(SCREAMING_SNAKE_CASE_ , '''set_processor''' ): A: str = module.processor for sub_name, child in module.named_children(): fn_recursive_add_processors(f"""{name}.{sub_name}""" , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return processors for name, module in self.named_children(): fn_recursive_add_processors(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return processors def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Union[AttentionProcessor, Dict[str, AttentionProcessor]] ) -> Union[str, Any]: '''simple docstring''' A: Tuple = len(self.attn_processors.keys() ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) and len(SCREAMING_SNAKE_CASE_ ) != count: raise ValueError( f"""A dict of processors was passed, but the number of processors {len(SCREAMING_SNAKE_CASE_ )} does not match the""" f""" number of attention layers: {count}. Please make sure to pass {count} processor classes.""" ) def fn_recursive_attn_processor(SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : torch.nn.Module , SCREAMING_SNAKE_CASE_ : Tuple ): if hasattr(SCREAMING_SNAKE_CASE_ , '''set_processor''' ): if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): module.set_processor(SCREAMING_SNAKE_CASE_ ) else: module.set_processor(processor.pop(f"""{name}.processor""" ) ) for sub_name, child in module.named_children(): fn_recursive_attn_processor(f"""{name}.{sub_name}""" , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for name, module in self.named_children(): fn_recursive_attn_processor(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Tuple ) -> Dict: '''simple docstring''' self.set_attn_processor(AttnProcessor() ) def _snake_case ( self : Dict , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Union[torch.Tensor, float, int] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : Optional[torch.FloatTensor] = None , SCREAMING_SNAKE_CASE_ : Optional[torch.BoolTensor] = None , SCREAMING_SNAKE_CASE_ : bool = True , ) -> Tuple: '''simple docstring''' A: Optional[Any] = hidden_states.shape[0] A: Optional[Any] = timestep if not torch.is_tensor(SCREAMING_SNAKE_CASE_ ): A: Any = torch.tensor([timesteps] , dtype=torch.long , device=hidden_states.device ) elif torch.is_tensor(SCREAMING_SNAKE_CASE_ ) and len(timesteps.shape ) == 0: A: Dict = timesteps[None].to(hidden_states.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML A: Dict = timesteps * torch.ones(SCREAMING_SNAKE_CASE_ , dtype=timesteps.dtype , device=timesteps.device ) A: Dict = self.time_proj(SCREAMING_SNAKE_CASE_ ) # timesteps does not contain any weights and will always return f32 tensors # but time_embedding might be fp16, so we need to cast here. A: Any = timesteps_projected.to(dtype=self.dtype ) A: Optional[Any] = self.time_embedding(SCREAMING_SNAKE_CASE_ ) if self.embedding_proj_norm is not None: A: List[Any] = self.embedding_proj_norm(SCREAMING_SNAKE_CASE_ ) A: Dict = self.embedding_proj(SCREAMING_SNAKE_CASE_ ) if self.encoder_hidden_states_proj is not None and encoder_hidden_states is not None: A: str = self.encoder_hidden_states_proj(SCREAMING_SNAKE_CASE_ ) elif self.encoder_hidden_states_proj is not None and encoder_hidden_states is None: raise ValueError('''`encoder_hidden_states_proj` requires `encoder_hidden_states` to be set''' ) A: Dict = self.proj_in(SCREAMING_SNAKE_CASE_ ) A: Dict = self.positional_embedding.to(hidden_states.dtype ) A: str = [] A: str = 0 if encoder_hidden_states is not None: additional_embeds.append(SCREAMING_SNAKE_CASE_ ) additional_embeddings_len += encoder_hidden_states.shape[1] if len(proj_embeddings.shape ) == 2: A: List[str] = proj_embeddings[:, None, :] if len(hidden_states.shape ) == 2: A: Tuple = hidden_states[:, None, :] A: Any = additional_embeds + [ proj_embeddings, time_embeddings[:, None, :], hidden_states, ] if self.prd_embedding is not None: A: Dict = self.prd_embedding.to(hidden_states.dtype ).expand(SCREAMING_SNAKE_CASE_ , -1 , -1 ) additional_embeds.append(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = torch.cat( SCREAMING_SNAKE_CASE_ , dim=1 , ) # Allow positional_embedding to not include the `addtional_embeddings` and instead pad it with zeros for these additional tokens A: Any = additional_embeddings_len + proj_embeddings.shape[1] + 1 if positional_embeddings.shape[1] < hidden_states.shape[1]: A: List[str] = F.pad( SCREAMING_SNAKE_CASE_ , ( 0, 0, additional_embeddings_len, self.prd_embedding.shape[1] if self.prd_embedding is not None else 0, ) , value=0.0 , ) A: List[Any] = hidden_states + positional_embeddings if attention_mask is not None: A: int = (1 - attention_mask.to(hidden_states.dtype )) * -1_0000.0 A: List[str] = F.pad(SCREAMING_SNAKE_CASE_ , (0, self.additional_embeddings) , value=0.0 ) A: List[str] = (attention_mask[:, None, :] + self.causal_attention_mask).to(hidden_states.dtype ) A: str = attention_mask.repeat_interleave(self.config.num_attention_heads , dim=0 ) if self.norm_in is not None: A: List[str] = self.norm_in(SCREAMING_SNAKE_CASE_ ) for block in self.transformer_blocks: A: int = block(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ ) A: List[Any] = self.norm_out(SCREAMING_SNAKE_CASE_ ) if self.prd_embedding is not None: A: Any = hidden_states[:, -1] else: A: Optional[Any] = hidden_states[:, additional_embeddings_len:] A: Optional[Any] = self.proj_to_clip_embeddings(SCREAMING_SNAKE_CASE_ ) if not return_dict: return (predicted_image_embedding,) return PriorTransformerOutput(predicted_image_embedding=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : Any ) -> Optional[int]: '''simple docstring''' A: Optional[Any] = (prior_latents * self.clip_std) + self.clip_mean return prior_latents
319
'''simple docstring''' from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase = {'''configuration_focalnet''': ['''FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FocalNetConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FocalNetForImageClassification''', '''FocalNetForMaskedImageModeling''', '''FocalNetBackbone''', '''FocalNetModel''', '''FocalNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_focalnet import FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FocalNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_focalnet import ( FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST, FocalNetBackbone, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetModel, FocalNetPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
1
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow if is_torch_available(): import torch from transformers import XLMRobertaModel @require_sentencepiece @require_tokenizers @require_torch class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' @slow def _snake_case ( self : Union[str, Any] ) -> Tuple: '''simple docstring''' A: int = XLMRobertaModel.from_pretrained('''xlm-roberta-base''' ) A: Union[str, Any] = torch.tensor([[0, 5_81, 1_02_69, 83, 9_99_42, 1_36, 6_07_42, 23, 70, 8_05_83, 1_82_76, 2]] ) # The dog is cute and lives in the garden house A: List[Any] = torch.Size((1, 12, 7_68) ) # batch_size, sequence_length, embedding_vector_dim A: Tuple = torch.tensor( [[-0.0101, 0.1218, -0.0803, 0.0801, 0.1327, 0.0776, -0.1215, 0.2383, 0.3338, 0.3106, 0.0300, 0.0252]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): A: Tuple = model(SCREAMING_SNAKE_CASE_ )['''last_hidden_state'''].detach() self.assertEqual(output.shape , SCREAMING_SNAKE_CASE_ ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , SCREAMING_SNAKE_CASE_ , atol=1E-3 ) ) @slow def _snake_case ( self : Dict ) -> Any: '''simple docstring''' A: Dict = XLMRobertaModel.from_pretrained('''xlm-roberta-large''' ) A: Any = torch.tensor([[0, 5_81, 1_02_69, 83, 9_99_42, 1_36, 6_07_42, 23, 70, 8_05_83, 1_82_76, 2]] ) # The dog is cute and lives in the garden house A: int = torch.Size((1, 12, 10_24) ) # batch_size, sequence_length, embedding_vector_dim A: str = torch.tensor( [[-0.0699, -0.0318, 0.0705, -0.1241, 0.0999, -0.0520, 0.1004, -0.1838, -0.4704, 0.1437, 0.0821, 0.0126]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): A: List[Any] = model(SCREAMING_SNAKE_CASE_ )['''last_hidden_state'''].detach() self.assertEqual(output.shape , SCREAMING_SNAKE_CASE_ ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , SCREAMING_SNAKE_CASE_ , atol=1E-3 ) )
319
'''simple docstring''' from typing import Any, Dict, List, Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from ..image_utils import load_image if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_OBJECT_DETECTION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = Dict[str, Any] UpperCamelCase = List[Prediction] @add_end_docstrings(UpperCAmelCase_ ) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : List[str] ) -> int: '''simple docstring''' super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if self.framework == "tf": raise ValueError(f"""The {self.__class__} is only available in PyTorch.""" ) requires_backends(self , '''vision''' ) self.check_model_type( dict(MODEL_FOR_OBJECT_DETECTION_MAPPING.items() + MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.items() ) ) def _snake_case ( self : int , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[Any]: '''simple docstring''' A: Any = {} if "threshold" in kwargs: A: List[Any] = kwargs['''threshold'''] return {}, {}, postprocess_kwargs def __call__( self : str , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Union[Predictions, List[Prediction]]: '''simple docstring''' return super().__call__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Union[str, Any]: '''simple docstring''' A: int = load_image(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = torch.IntTensor([[image.height, image.width]] ) A: Union[str, Any] = self.image_processor(images=[image] , return_tensors='''pt''' ) if self.tokenizer is not None: A: int = self.tokenizer(text=inputs['''words'''] , boxes=inputs['''boxes'''] , return_tensors='''pt''' ) A: Any = target_size return inputs def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : str ) -> List[Any]: '''simple docstring''' A: Tuple = model_inputs.pop('''target_size''' ) A: Tuple = self.model(**SCREAMING_SNAKE_CASE_ ) A: List[str] = outputs.__class__({'''target_size''': target_size, **outputs} ) if self.tokenizer is not None: A: Dict = model_inputs['''bbox'''] return model_outputs def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : str=0.9 ) -> Union[str, Any]: '''simple docstring''' A: List[Any] = model_outputs['''target_size'''] if self.tokenizer is not None: # This is a LayoutLMForTokenClassification variant. # The OCR got the boxes and the model classified the words. A , A: Union[str, Any] = target_size[0].tolist() def unnormalize(SCREAMING_SNAKE_CASE_ : str ): return self._get_bounding_box( torch.Tensor( [ (width * bbox[0] / 10_00), (height * bbox[1] / 10_00), (width * bbox[2] / 10_00), (height * bbox[3] / 10_00), ] ) ) A , A: Dict = model_outputs['''logits'''].squeeze(0 ).softmax(dim=-1 ).max(dim=-1 ) A: List[str] = [self.model.config.idalabel[prediction] for prediction in classes.tolist()] A: List[str] = [unnormalize(SCREAMING_SNAKE_CASE_ ) for bbox in model_outputs['''bbox'''].squeeze(0 )] A: Dict = ['''score''', '''label''', '''box'''] A: Optional[int] = [dict(zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) for vals in zip(scores.tolist() , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if vals[0] > threshold] else: # This is a regular ForObjectDetectionModel A: Any = self.image_processor.post_process_object_detection(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: List[str] = raw_annotations[0] A: List[Any] = raw_annotation['''scores'''] A: List[Any] = raw_annotation['''labels'''] A: int = raw_annotation['''boxes'''] A: Any = scores.tolist() A: List[Any] = [self.model.config.idalabel[label.item()] for label in labels] A: List[Any] = [self._get_bounding_box(SCREAMING_SNAKE_CASE_ ) for box in boxes] # {"scores": [...], ...} --> [{"score":x, ...}, ...] A: Tuple = ['''score''', '''label''', '''box'''] A: str = [ dict(zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) for vals in zip(raw_annotation['''scores'''] , raw_annotation['''labels'''] , raw_annotation['''boxes'''] ) ] return annotation def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : "torch.Tensor" ) -> Dict[str, int]: '''simple docstring''' if self.framework != "pt": raise ValueError('''The ObjectDetectionPipeline is only available in PyTorch.''' ) A , A , A , A: str = box.int().tolist() A: str = { '''xmin''': xmin, '''ymin''': ymin, '''xmax''': xmax, '''ymax''': ymax, } return bbox
319
1
'''simple docstring''' import os try: from .build_directory_md import good_file_paths except ImportError: from build_directory_md import good_file_paths # type: ignore UpperCamelCase = list(good_file_paths()) assert filepaths, "good_file_paths() failed!" UpperCamelCase = [file for file in filepaths if file != file.lower()] if upper_files: print(f'{len(upper_files)} files contain uppercase characters:') print('''\n'''.join(upper_files) + '''\n''') UpperCamelCase = [file for file in filepaths if ''' ''' in file] if space_files: print(f'{len(space_files)} files contain space characters:') print('''\n'''.join(space_files) + '''\n''') UpperCamelCase = [file for file in filepaths if '''-''' in file] if hyphen_files: print(f'{len(hyphen_files)} files contain hyphen characters:') print('''\n'''.join(hyphen_files) + '''\n''') UpperCamelCase = [file for file in filepaths if os.sep not in file] if nodir_files: print(f'{len(nodir_files)} files are not in a directory:') print('''\n'''.join(nodir_files) + '''\n''') UpperCamelCase = len(upper_files + space_files + hyphen_files + nodir_files) if bad_files: import sys sys.exit(bad_files)
319
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''YituTech/conv-bert-base''': '''https://huggingface.co/YituTech/conv-bert-base/resolve/main/config.json''', '''YituTech/conv-bert-medium-small''': ( '''https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/config.json''' ), '''YituTech/conv-bert-small''': '''https://huggingface.co/YituTech/conv-bert-small/resolve/main/config.json''', # See all ConvBERT models at https://huggingface.co/models?filter=convbert } class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = """convbert""" def __init__( self : Dict , SCREAMING_SNAKE_CASE_ : Dict=3_05_22 , SCREAMING_SNAKE_CASE_ : int=7_68 , SCREAMING_SNAKE_CASE_ : List[str]=12 , SCREAMING_SNAKE_CASE_ : List[str]=12 , SCREAMING_SNAKE_CASE_ : Dict=30_72 , SCREAMING_SNAKE_CASE_ : Optional[int]="gelu" , SCREAMING_SNAKE_CASE_ : List[Any]=0.1 , SCREAMING_SNAKE_CASE_ : int=0.1 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=5_12 , SCREAMING_SNAKE_CASE_ : List[Any]=2 , SCREAMING_SNAKE_CASE_ : List[str]=0.02 , SCREAMING_SNAKE_CASE_ : int=1E-12 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=1 , SCREAMING_SNAKE_CASE_ : int=0 , SCREAMING_SNAKE_CASE_ : str=2 , SCREAMING_SNAKE_CASE_ : List[Any]=7_68 , SCREAMING_SNAKE_CASE_ : Optional[Any]=2 , SCREAMING_SNAKE_CASE_ : Any=9 , SCREAMING_SNAKE_CASE_ : Tuple=1 , SCREAMING_SNAKE_CASE_ : List[Any]=None , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> List[Any]: '''simple docstring''' super().__init__( pad_token_id=SCREAMING_SNAKE_CASE_ , bos_token_id=SCREAMING_SNAKE_CASE_ , eos_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Dict = vocab_size A: Tuple = hidden_size A: Optional[int] = num_hidden_layers A: List[str] = num_attention_heads A: int = intermediate_size A: int = hidden_act A: List[str] = hidden_dropout_prob A: int = attention_probs_dropout_prob A: Tuple = max_position_embeddings A: Any = type_vocab_size A: str = initializer_range A: Union[str, Any] = layer_norm_eps A: str = embedding_size A: Optional[int] = head_ratio A: List[Any] = conv_kernel_size A: List[Any] = num_groups A: Optional[int] = classifier_dropout class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' @property def _snake_case ( self : Optional[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": A: Tuple = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: A: List[str] = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis), ] )
319
1
'''simple docstring''' import inspect import os import re from transformers.configuration_utils import PretrainedConfig from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_config_docstrings.py UpperCamelCase = '''src/transformers''' # This is to make sure the transformers module imported is the one in the repo. UpperCamelCase = direct_transformers_import(PATH_TO_TRANSFORMERS) UpperCamelCase = transformers.models.auto.configuration_auto.CONFIG_MAPPING UpperCamelCase = { # used to compute the property `self.chunk_length` '''EncodecConfig''': ['''overlap'''], # used as `self.bert_model = BertModel(config, ...)` '''DPRConfig''': True, # not used in modeling files, but it's an important information '''FSMTConfig''': ['''langs'''], # used internally in the configuration class file '''GPTNeoConfig''': ['''attention_types'''], # used internally in the configuration class file '''EsmConfig''': ['''is_folding_model'''], # used during training (despite we don't have training script for these models yet) '''Mask2FormerConfig''': ['''ignore_value'''], # `ignore_value` used during training (despite we don't have training script for these models yet) # `norm` used in conversion script (despite not using in the modeling file) '''OneFormerConfig''': ['''ignore_value''', '''norm'''], # used during preprocessing and collation, see `collating_graphormer.py` '''GraphormerConfig''': ['''spatial_pos_max'''], # used internally in the configuration class file '''T5Config''': ['''feed_forward_proj'''], # used internally in the configuration class file # `tokenizer_class` get default value `T5Tokenizer` intentionally '''MT5Config''': ['''feed_forward_proj''', '''tokenizer_class'''], '''UMT5Config''': ['''feed_forward_proj''', '''tokenizer_class'''], # used internally in the configuration class file '''LongT5Config''': ['''feed_forward_proj'''], # used internally in the configuration class file '''SwitchTransformersConfig''': ['''feed_forward_proj'''], # having default values other than `1e-5` - we can't fix them without breaking '''BioGptConfig''': ['''layer_norm_eps'''], # having default values other than `1e-5` - we can't fix them without breaking '''GLPNConfig''': ['''layer_norm_eps'''], # having default values other than `1e-5` - we can't fix them without breaking '''SegformerConfig''': ['''layer_norm_eps'''], # having default values other than `1e-5` - we can't fix them without breaking '''CvtConfig''': ['''layer_norm_eps'''], # having default values other than `1e-5` - we can't fix them without breaking '''PerceiverConfig''': ['''layer_norm_eps'''], # used internally to calculate the feature size '''InformerConfig''': ['''num_static_real_features''', '''num_time_features'''], # used internally to calculate the feature size '''TimeSeriesTransformerConfig''': ['''num_static_real_features''', '''num_time_features'''], # used internally to calculate the feature size '''AutoformerConfig''': ['''num_static_real_features''', '''num_time_features'''], # used internally to calculate `mlp_dim` '''SamVisionConfig''': ['''mlp_ratio'''], # For (head) training, but so far not implemented '''ClapAudioConfig''': ['''num_classes'''], # Not used, but providing useful information to users '''SpeechT5HifiGanConfig''': ['''sampling_rate'''], } # TODO (ydshieh): Check the failing cases, try to fix them or move some cases to the above block once we are sure SPECIAL_CASES_TO_ALLOW.update( { '''CLIPSegConfig''': True, '''DeformableDetrConfig''': True, '''DetaConfig''': True, '''DinatConfig''': True, '''DonutSwinConfig''': True, '''EfficientFormerConfig''': True, '''FSMTConfig''': True, '''JukeboxConfig''': True, '''LayoutLMv2Config''': True, '''MaskFormerSwinConfig''': True, '''MT5Config''': True, '''NatConfig''': True, '''OneFormerConfig''': True, '''PerceiverConfig''': True, '''RagConfig''': True, '''SpeechT5Config''': True, '''SwinConfig''': True, '''Swin2SRConfig''': True, '''Swinv2Config''': True, '''SwitchTransformersConfig''': True, '''TableTransformerConfig''': True, '''TapasConfig''': True, '''TransfoXLConfig''': True, '''UniSpeechConfig''': True, '''UniSpeechSatConfig''': True, '''WavLMConfig''': True, '''WhisperConfig''': True, # TODO: @Arthur (for `alignment_head` and `alignment_layer`) '''JukeboxPriorConfig''': True, # TODO: @Younes (for `is_decoder`) '''Pix2StructTextConfig''': True, } ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase ) -> Any: A: Tuple = False for attribute in attributes: for modeling_source in source_strings: # check if we can find `config.xxx`, `getattr(config, "xxx", ...)` or `getattr(self.config, "xxx", ...)` if ( F"""config.{attribute}""" in modeling_source or F"""getattr(config, \"{attribute}\"""" in modeling_source or F"""getattr(self.config, \"{attribute}\"""" in modeling_source ): A: Optional[Any] = True # Deal with multi-line cases elif ( re.search( rF"""getattr[ \t\v\n\r\f]*\([ \t\v\n\r\f]*(self\.)?config,[ \t\v\n\r\f]*\"{attribute}\"""" , __lowercase , ) is not None ): A: Optional[Any] = True # `SequenceSummary` is called with `SequenceSummary(config)` elif attribute in [ "summary_type", "summary_use_proj", "summary_activation", "summary_last_dropout", "summary_proj_to_labels", "summary_first_dropout", ]: if "SequenceSummary" in modeling_source: A: Optional[Any] = True if attribute_used: break if attribute_used: break # common and important attributes, even if they do not always appear in the modeling files A: str = [ '''bos_index''', '''eos_index''', '''pad_index''', '''unk_index''', '''mask_index''', '''image_size''', '''use_cache''', '''out_features''', '''out_indices''', ] A: Any = ['''encoder_no_repeat_ngram_size'''] # Special cases to be allowed A: Optional[int] = True if not attribute_used: A: Optional[int] = False for attribute in attributes: # Allow if the default value in the configuration class is different from the one in `PretrainedConfig` if attribute in ["is_encoder_decoder"] and default_value is True: A: str = True elif attribute in ["tie_word_embeddings"] and default_value is False: A: Dict = True # Allow cases without checking the default value in the configuration class elif attribute in attributes_to_allow + attributes_used_in_generation: A: Optional[int] = True elif attribute.endswith('''_token_id''' ): A: List[Any] = True # configuration class specific cases if not case_allowed: A: Optional[Any] = SPECIAL_CASES_TO_ALLOW.get(config_class.__name__ , [] ) A: Tuple = allowed_cases is True or attribute in allowed_cases return attribute_used or case_allowed def SCREAMING_SNAKE_CASE( __lowercase ) -> Any: A: List[str] = dict(inspect.signature(config_class.__init__ ).parameters ) A: Any = [x for x in list(signature.keys() ) if x not in ['''self''', '''kwargs''']] A: Dict = [signature[param].default for param in parameter_names] # If `attribute_map` exists, an attribute can have different names to be used in the modeling files, and as long # as one variant is used, the test should pass A: List[Any] = {} if len(config_class.attribute_map ) > 0: A: int = {v: k for k, v in config_class.attribute_map.items()} # Get the path to modeling source files A: Any = inspect.getsourcefile(__lowercase ) A: int = os.path.dirname(__lowercase ) # Let's check against all frameworks: as long as one framework uses an attribute, we are good. A: Optional[Any] = [os.path.join(__lowercase , __lowercase ) for fn in os.listdir(__lowercase ) if fn.startswith('''modeling_''' )] # Get the source code strings A: Any = [] for path in modeling_paths: if os.path.isfile(__lowercase ): with open(__lowercase ) as fp: modeling_sources.append(fp.read() ) A: Optional[Any] = [] for config_param, default_value in zip(__lowercase , __lowercase ): # `attributes` here is all the variant names for `config_param` A: List[Any] = [config_param] # some configuration classes have non-empty `attribute_map`, and both names could be used in the # corresponding modeling files. As long as one of them appears, it is fine. if config_param in reversed_attribute_map: attributes.append(reversed_attribute_map[config_param] ) if not check_attribute_being_used(__lowercase , __lowercase , __lowercase , __lowercase ): unused_attributes.append(attributes[0] ) return sorted(__lowercase ) def SCREAMING_SNAKE_CASE( ) -> Optional[Any]: A: Tuple = {} for _config_class in list(CONFIG_MAPPING.values() ): # Skip deprecated models if "models.deprecated" in _config_class.__module__: continue # Some config classes are not in `CONFIG_MAPPING` (e.g. `CLIPVisionConfig`, `Blip2VisionConfig`, etc.) A: str = [ cls for name, cls in inspect.getmembers( inspect.getmodule(_config_class ) , lambda __lowercase : inspect.isclass(__lowercase ) and issubclass(__lowercase , __lowercase ) and inspect.getmodule(__lowercase ) == inspect.getmodule(_config_class ) , ) ] for config_class in config_classes_in_module: A: Dict = check_config_attributes_being_used(__lowercase ) if len(__lowercase ) > 0: A: Union[str, Any] = unused_attributes if len(__lowercase ) > 0: A: Any = '''The following configuration classes contain unused attributes in the corresponding modeling files:\n''' for name, attributes in configs_with_unused_attributes.items(): error += F"""{name}: {attributes}\n""" raise ValueError(__lowercase ) if __name__ == "__main__": check_config_attributes()
319
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE( __lowercase ) -> bool: if len(__lowercase ) < 2: raise ValueError('''Monogons and Digons are not polygons in the Euclidean space''' ) if any(i <= 0 for i in nums ): raise ValueError('''All values must be greater than 0''' ) A: Any = nums.copy() copy_nums.sort() return copy_nums[-1] < sum(copy_nums[:-1] ) if __name__ == "__main__": import doctest doctest.testmod()
319
1
'''simple docstring''' import os # Precomputes a list of the 100 first triangular numbers UpperCamelCase = [int(0.5 * n * (n + 1)) for n in range(1, 101)] def SCREAMING_SNAKE_CASE( ) -> Union[str, Any]: A: List[str] = os.path.dirname(os.path.realpath(__lowercase ) ) A: Any = os.path.join(__lowercase , '''words.txt''' ) A: str = '''''' with open(__lowercase ) as f: A: Optional[int] = f.readline() A: Any = [word.strip('''"''' ) for word in words.strip('''\r\n''' ).split(''',''' )] A: Dict = [ word for word in [sum(ord(__lowercase ) - 6_4 for x in word ) for word in words] if word in TRIANGULAR_NUMBERS ] return len(__lowercase ) if __name__ == "__main__": print(solution())
319
'''simple docstring''' # tests directory-specific settings - this file is run automatically # by pytest before any tests are run import doctest import sys import warnings from os.path import abspath, dirname, join import _pytest from transformers.testing_utils import HfDoctestModule, HfDocTestParser # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. UpperCamelCase = abspath(join(dirname(__file__), '''src''')) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action='''ignore''', category=FutureWarning) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: config.addinivalue_line( '''markers''' , '''is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested''' ) config.addinivalue_line( '''markers''' , '''is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested''' ) config.addinivalue_line('''markers''' , '''is_pipeline_test: mark test to run only when pipelines are tested''' ) config.addinivalue_line('''markers''' , '''is_staging_test: mark test to run only in the staging environment''' ) config.addinivalue_line('''markers''' , '''accelerate_tests: mark test that require accelerate''' ) config.addinivalue_line('''markers''' , '''tool_tests: mark the tool tests that are run on their specific schedule''' ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: from transformers.testing_utils import pytest_terminal_summary_main A: Optional[int] = terminalreporter.config.getoption('''--make-reports''' ) if make_reports: pytest_terminal_summary_main(__lowercase , id=__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Any: # If no tests are collected, pytest exists with code 5, which makes the CI fail. if exitstatus == 5: A: Tuple = 0 # Doctest custom flag to ignore output. UpperCamelCase = doctest.register_optionflag('''IGNORE_RESULT''') UpperCamelCase = doctest.OutputChecker class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ) -> str: '''simple docstring''' if IGNORE_RESULT & optionflags: return True return OutputChecker.check_output(self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase = CustomOutputChecker UpperCamelCase = HfDoctestModule UpperCamelCase = HfDocTestParser
319
1
'''simple docstring''' from argparse import ArgumentParser from .add_new_model import AddNewModelCommand from .add_new_model_like import AddNewModelLikeCommand from .convert import ConvertCommand from .download import DownloadCommand from .env import EnvironmentCommand from .lfs import LfsCommands from .pt_to_tf import PTtoTFCommand from .run import RunCommand from .serving import ServeCommand from .user import UserCommands def SCREAMING_SNAKE_CASE( ) -> Optional[int]: A: Dict = ArgumentParser('''Transformers CLI tool''' , usage='''transformers-cli <command> [<args>]''' ) A: Optional[int] = parser.add_subparsers(help='''transformers-cli command helpers''' ) # Register commands ConvertCommand.register_subcommand(__lowercase ) DownloadCommand.register_subcommand(__lowercase ) EnvironmentCommand.register_subcommand(__lowercase ) RunCommand.register_subcommand(__lowercase ) ServeCommand.register_subcommand(__lowercase ) UserCommands.register_subcommand(__lowercase ) AddNewModelCommand.register_subcommand(__lowercase ) AddNewModelLikeCommand.register_subcommand(__lowercase ) LfsCommands.register_subcommand(__lowercase ) PTtoTFCommand.register_subcommand(__lowercase ) # Let's go A: List[str] = parser.parse_args() if not hasattr(__lowercase , '''func''' ): parser.print_help() exit(1 ) # Run A: Optional[Any] = args.func(__lowercase ) service.run() if __name__ == "__main__": main()
319
'''simple docstring''' import heapq import sys import numpy as np UpperCamelCase = tuple[int, int] class lowerCAmelCase_ : '''simple docstring''' def __init__( self : List[Any] ) -> str: '''simple docstring''' A: Any = [] A: int = set() def _snake_case ( self : Optional[Any] ) -> int: '''simple docstring''' if not self.empty(): return self.elements[0][0] else: return float('''inf''' ) def _snake_case ( self : List[str] ) -> List[Any]: '''simple docstring''' return len(self.elements ) == 0 def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Any ) -> List[Any]: '''simple docstring''' if item not in self.set: heapq.heappush(self.elements , (priority, item) ) self.set.add(SCREAMING_SNAKE_CASE_ ) else: # update # print("update", item) A: Optional[int] = [] ((A) , (A)): str = heapq.heappop(self.elements ) while x != item: temp.append((pri, x) ) ((A) , (A)): int = heapq.heappop(self.elements ) temp.append((priority, item) ) for pro, xxx in temp: heapq.heappush(self.elements , (pro, xxx) ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : str ) -> Any: '''simple docstring''' if item in self.set: self.set.remove(SCREAMING_SNAKE_CASE_ ) A: str = [] ((A) , (A)): List[str] = heapq.heappop(self.elements ) while x != item: temp.append((pro, x) ) ((A) , (A)): Any = heapq.heappop(self.elements ) for prito, yyy in temp: heapq.heappush(self.elements , (prito, yyy) ) def _snake_case ( self : List[Any] ) -> Optional[int]: '''simple docstring''' return self.elements[0][1] def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' ((A) , (A)): Dict = heapq.heappop(self.elements ) self.set.remove(SCREAMING_SNAKE_CASE_ ) return (priority, item) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: # euclidean distance A: List[str] = np.array(__lowercase ) A: Optional[int] = np.array(__lowercase ) return np.linalg.norm(a - b ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> int: # integer division by time variable return consistent_heuristic(__lowercase , __lowercase ) // t def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[Any]: # manhattan distance return abs(p[0] - goal[0] ) + abs(p[1] - goal[1] ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase ) -> List[Any]: A: int = g_function[start] + Wa * heuristics[i](__lowercase , __lowercase ) return ans def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Optional[int]: A: Union[str, Any] = np.chararray((n, n) ) for i in range(__lowercase ): for j in range(__lowercase ): A: Union[str, Any] = '''*''' for i in range(__lowercase ): for j in range(__lowercase ): if (j, (n - 1) - i) in blocks: A: Optional[Any] = '''#''' A: Tuple = '''-''' A: List[str] = back_pointer[goal] while x != start: ((A) , (A)): Tuple = x # print(x) A: List[str] = '''-''' A: str = back_pointer[x] A: Dict = '''-''' for i in range(__lowercase ): for j in range(__lowercase ): if (i, j) == (0, n - 1): print(grid[i][j] , end=''' ''' ) print('''<-- End position''' , end=''' ''' ) else: print(grid[i][j] , end=''' ''' ) print() print('''^''' ) print('''Start position''' ) print() print('''# is an obstacle''' ) print('''- is the path taken by algorithm''' ) print('''PATH TAKEN BY THE ALGORITHM IS:-''' ) A: List[str] = back_pointer[goal] while x != start: print(__lowercase , end=''' ''' ) A: Optional[int] = back_pointer[x] print(__lowercase ) sys.exit() def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: if p[0] < 0 or p[0] > n - 1: return False if p[1] < 0 or p[1] > n - 1: return False return True def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) -> Union[str, Any]: for itera in range(__lowercase ): open_list[itera].remove_element(__lowercase ) # print("s", s) # print("j", j) ((A) , (A)): Tuple = s A: Optional[Any] = (x - 1, y) A: str = (x + 1, y) A: List[Any] = (x, y + 1) A: int = (x, y - 1) for neighbours in [left, right, up, down]: if neighbours not in blocks: if valid(__lowercase ) and neighbours not in visited: # print("neighbour", neighbours) visited.add(__lowercase ) A: int = -1 A: int = float('''inf''' ) if valid(__lowercase ) and g_function[neighbours] > g_function[s] + 1: A: List[str] = g_function[s] + 1 A: List[str] = s if neighbours not in close_list_anchor: open_list[0].put(__lowercase , key(__lowercase , 0 , __lowercase , __lowercase ) ) if neighbours not in close_list_inad: for var in range(1 , __lowercase ): if key(__lowercase , __lowercase , __lowercase , __lowercase ) <= Wa * key( __lowercase , 0 , __lowercase , __lowercase ): open_list[j].put( __lowercase , key(__lowercase , __lowercase , __lowercase , __lowercase ) ) def SCREAMING_SNAKE_CASE( ) -> Tuple: A: str = [] for x in range(1 , 5 ): for y in range(1 , 6 ): some_list.append((x, y) ) for x in range(1_5 , 2_0 ): some_list.append((x, 1_7) ) for x in range(1_0 , 1_9 ): for y in range(1 , 1_5 ): some_list.append((x, y) ) # L block for x in range(1 , 4 ): for y in range(1_2 , 1_9 ): some_list.append((x, y) ) for x in range(3 , 1_3 ): for y in range(1_6 , 1_9 ): some_list.append((x, y) ) return some_list UpperCamelCase = {0: consistent_heuristic, 1: heuristic_a, 2: heuristic_a} UpperCamelCase = [ (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1), (15, 1), (16, 1), (17, 1), (18, 1), (19, 1), ] UpperCamelCase = make_common_ground() UpperCamelCase = blocks_blk # hyper parameters UpperCamelCase = 1 UpperCamelCase = 1 UpperCamelCase = 20 UpperCamelCase = 3 # one consistent and two other inconsistent # start and end destination UpperCamelCase = (0, 0) UpperCamelCase = (n - 1, n - 1) UpperCamelCase = 1 def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: A: int = {start: 0, goal: float('''inf''' )} A: Union[str, Any] = {start: -1, goal: -1} A: List[Any] = [] A: Union[str, Any] = set() for i in range(__lowercase ): open_list.append(PriorityQueue() ) open_list[i].put(__lowercase , key(__lowercase , __lowercase , __lowercase , __lowercase ) ) A: list[int] = [] A: list[int] = [] while open_list[0].minkey() < float('''inf''' ): for i in range(1 , __lowercase ): # print(open_list[0].minkey(), open_list[i].minkey()) if open_list[i].minkey() <= Wa * open_list[0].minkey(): global t t += 1 if g_function[goal] <= open_list[i].minkey(): if g_function[goal] < float('''inf''' ): do_something(__lowercase , __lowercase , __lowercase ) else: A , A: Union[str, Any] = open_list[i].top_show() visited.add(__lowercase ) expand_state( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) close_list_inad.append(__lowercase ) else: if g_function[goal] <= open_list[0].minkey(): if g_function[goal] < float('''inf''' ): do_something(__lowercase , __lowercase , __lowercase ) else: A: Union[str, Any] = open_list[0].top_show() visited.add(__lowercase ) expand_state( __lowercase , 0 , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) close_list_anchor.append(__lowercase ) print('''No path found to goal''' ) print() for i in range(n - 1 , -1 , -1 ): for j in range(__lowercase ): if (j, i) in blocks: print('''#''' , end=''' ''' ) elif (j, i) in back_pointer: if (j, i) == (n - 1, n - 1): print('''*''' , end=''' ''' ) else: print('''-''' , end=''' ''' ) else: print('''*''' , end=''' ''' ) if (j, i) == (n - 1, n - 1): print('''<-- End position''' , end=''' ''' ) print() print('''^''' ) print('''Start position''' ) print() print('''# is an obstacle''' ) print('''- is the path taken by algorithm''' ) if __name__ == "__main__": multi_a_star(start, goal, n_heuristic)
319
1
'''simple docstring''' import math from datetime import datetime, timedelta def SCREAMING_SNAKE_CASE( __lowercase ) -> datetime: A: Optional[Any] = year % 1_9 A: str = year % 4 A: int = year % 7 A: int = math.floor(year / 1_0_0 ) A: Optional[int] = math.floor((1_3 + 8 * leap_day_inhibits) / 2_5 ) A: Optional[int] = leap_day_inhibits / 4 A: Dict = ( 1_5 - lunar_orbit_correction + leap_day_inhibits - leap_day_reinstall_number ) % 3_0 A: Union[str, Any] = (4 + leap_day_inhibits - leap_day_reinstall_number) % 7 # days to be added to March 21 A: Dict = (1_9 * metonic_cycle + secular_moon_shift) % 3_0 # PHM -> Paschal Full Moon A: Optional[int] = ( 2 * julian_leap_year + 4 * non_leap_year + 6 * days_to_add + century_starting_point ) % 7 if days_to_add == 2_9 and days_from_phm_to_sunday == 6: return datetime(__lowercase , 4 , 1_9 ) elif days_to_add == 2_8 and days_from_phm_to_sunday == 6: return datetime(__lowercase , 4 , 1_8 ) else: return datetime(__lowercase , 3 , 2_2 ) + timedelta( days=int(days_to_add + days_from_phm_to_sunday ) ) if __name__ == "__main__": for year in (1994, 2000, 2010, 2021, 2023): UpperCamelCase = '''will be''' if year > datetime.now().year else '''was''' print(f'Easter in {year} {tense} {gauss_easter(year)}')
319
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase = 1 , __lowercase = 1_0_0_0 ) -> int: A: Any = 1 A: Optional[Any] = 0 for divide_by_number in range(__lowercase , digit + 1 ): A: list[int] = [] A: List[Any] = numerator for _ in range(1 , digit + 1 ): if now_divide in has_been_divided: if longest_list_length < len(__lowercase ): A: Any = len(__lowercase ) A: Dict = divide_by_number else: has_been_divided.append(__lowercase ) A: str = now_divide * 1_0 % divide_by_number return the_digit # Tests if __name__ == "__main__": import doctest doctest.testmod()
319
1
'''simple docstring''' import os import time import numpy as np import onnxruntime as ort UpperCamelCase = '''1''' UpperCamelCase = '''0''' UpperCamelCase = '''1''' UpperCamelCase = ort.SessionOptions() UpperCamelCase = ort.GraphOptimizationLevel.ORT_DISABLE_ALL print('''Create inference session...''') UpperCamelCase = ['''TensorrtExecutionProvider''', '''CUDAExecutionProvider'''] UpperCamelCase = ort.InferenceSession('''model.onnx''', sess_options=sess_opt, providers=execution_provider) UpperCamelCase = ort.RunOptions() UpperCamelCase = 128 UpperCamelCase = 1 UpperCamelCase = np.ones((batch, sequence), dtype=np.intaa) UpperCamelCase = np.ones((batch, sequence), dtype=np.intaa) UpperCamelCase = np.ones((batch, sequence), dtype=np.intaa) print('''Warm up phase...''') sess.run( None, { sess.get_inputs()[0].name: input_ids, sess.get_inputs()[1].name: attention_mask, sess.get_inputs()[2].name: token_type_ids, }, run_options=run_opt, ) print('''Start inference...''') UpperCamelCase = time.time() UpperCamelCase = 2000 UpperCamelCase = {} for iter in range(max_iters): UpperCamelCase = sess.run( None, { sess.get_inputs()[0].name: input_ids, sess.get_inputs()[1].name: attention_mask, sess.get_inputs()[2].name: token_type_ids, }, run_options=run_opt, ) print('''Average Inference Time = {:.3f} ms'''.format((time.time() - start_time) * 1000 / max_iters))
319
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) UpperCamelCase = { '''configuration_vision_encoder_decoder''': ['''VisionEncoderDecoderConfig''', '''VisionEncoderDecoderOnnxConfig'''] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''VisionEncoderDecoderModel'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''TFVisionEncoderDecoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''FlaxVisionEncoderDecoderModel'''] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
1
'''simple docstring''' from queue import PriorityQueue from typing import Any import numpy as np def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) -> float | int: for nxt, d in graph[v]: if nxt in visited_forward: continue A: Optional[Any] = cst_fwd.get(__lowercase , np.inf ) A: Dict = cst_fwd[v] + d if new_cost_f < old_cost_f: queue.put((new_cost_f, nxt) ) A: str = new_cost_f A: List[str] = v if nxt in visited_backward: if cst_fwd[v] + d + cst_bwd[nxt] < shortest_distance: A: int = cst_fwd[v] + d + cst_bwd[nxt] return shortest_distance def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase ) -> int: A: List[str] = -1 A: Optional[Any] = set() A: List[str] = set() A: int = {source: 0} A: Optional[int] = {destination: 0} A: Tuple = {source: None} A: Tuple = {destination: None} A: PriorityQueue[Any] = PriorityQueue() A: PriorityQueue[Any] = PriorityQueue() A: Optional[Any] = np.inf queue_forward.put((0, source) ) queue_backward.put((0, destination) ) if source == destination: return 0 while not queue_forward.empty() and not queue_backward.empty(): A , A: List[Any] = queue_forward.get() visited_forward.add(__lowercase ) A , A: List[Any] = queue_backward.get() visited_backward.add(__lowercase ) A: str = pass_and_relaxation( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) A: Optional[Any] = pass_and_relaxation( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance: break if shortest_distance != np.inf: A: Union[str, Any] = shortest_distance return shortest_path_distance UpperCamelCase = { '''B''': [['''C''', 1]], '''C''': [['''D''', 1]], '''D''': [['''F''', 1]], '''E''': [['''B''', 1], ['''G''', 2]], '''F''': [], '''G''': [['''F''', 1]], } UpperCamelCase = { '''B''': [['''E''', 1]], '''C''': [['''B''', 1]], '''D''': [['''C''', 1]], '''F''': [['''D''', 1], ['''G''', 1]], '''E''': [[None, np.inf]], '''G''': [['''E''', 2]], } if __name__ == "__main__": import doctest doctest.testmod()
319
'''simple docstring''' import fire from utils import calculate_rouge, save_json def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=None , **__lowercase ) -> Any: A: Any = [x.strip() for x in open(__lowercase ).readlines()] A: Dict = [x.strip() for x in open(__lowercase ).readlines()][: len(__lowercase )] A: Union[str, Any] = calculate_rouge(__lowercase , __lowercase , **__lowercase ) if save_path is not None: save_json(__lowercase , __lowercase , indent=__lowercase ) return metrics # these print nicely if __name__ == "__main__": fire.Fire(calculate_rouge_path)
319
1
'''simple docstring''' from ..utils import DummyObject, requires_backends class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[int] = ["""keras_nlp"""] def __init__( self : Optional[int] , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : Dict ) -> Union[str, Any]: '''simple docstring''' requires_backends(self , ['''keras_nlp'''] )
319
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = 0 ) -> list: A: Dict = length or len(__lowercase ) A: Dict = False for i in range(length - 1 ): if list_data[i] > list_data[i + 1]: A , A: Tuple = list_data[i + 1], list_data[i] A: Union[str, Any] = True return list_data if not swapped else bubble_sort(__lowercase , length - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
319
1
'''simple docstring''' from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast from ...utils import logging if TYPE_CHECKING: from ...feature_extraction_utils import FeatureExtractionMixin from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import TensorType UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''openai/whisper-base''': '''https://huggingface.co/openai/whisper-base/resolve/main/config.json''', } # fmt: off UpperCamelCase = [ 1, 2, 7, 8, 9, 10, 14, 25, 26, 27, 28, 29, 31, 58, 59, 60, 61, 62, 63, 90, 91, 92, 93, 357, 366, 438, 532, 685, 705, 796, 930, 1058, 1220, 1267, 1279, 1303, 1343, 1377, 1391, 1635, 1782, 1875, 2162, 2361, 2488, 3467, 4008, 4211, 4600, 4808, 5299, 5855, 6329, 7203, 9609, 9959, 10563, 10786, 11420, 11709, 11907, 13163, 13697, 13700, 14808, 15306, 16410, 16791, 17992, 19203, 19510, 20724, 22305, 22935, 27007, 30109, 30420, 33409, 34949, 40283, 40493, 40549, 47282, 49146, 50257, 50359, 50360, 50361 ] UpperCamelCase = [ 1, 2, 7, 8, 9, 10, 14, 25, 26, 27, 28, 29, 31, 58, 59, 60, 61, 62, 63, 90, 91, 92, 93, 359, 503, 522, 542, 873, 893, 902, 918, 922, 931, 1350, 1853, 1982, 2460, 2627, 3246, 3253, 3268, 3536, 3846, 3961, 4183, 4667, 6585, 6647, 7273, 9061, 9383, 10428, 10929, 11938, 12033, 12331, 12562, 13793, 14157, 14635, 15265, 15618, 16553, 16604, 18362, 18956, 20075, 21675, 22520, 26130, 26161, 26435, 28279, 29464, 31650, 32302, 32470, 36865, 42863, 47425, 49870, 50254, 50258, 50360, 50361, 50362 ] class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : List[str] = """whisper""" UpperCamelCase_ : Dict = ["""past_key_values"""] UpperCamelCase_ : int = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self : Tuple , SCREAMING_SNAKE_CASE_ : Dict=5_18_65 , SCREAMING_SNAKE_CASE_ : str=80 , SCREAMING_SNAKE_CASE_ : Optional[int]=6 , SCREAMING_SNAKE_CASE_ : Dict=4 , SCREAMING_SNAKE_CASE_ : List[str]=6 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=4 , SCREAMING_SNAKE_CASE_ : List[str]=15_36 , SCREAMING_SNAKE_CASE_ : List[Any]=15_36 , SCREAMING_SNAKE_CASE_ : List[Any]=0.0 , SCREAMING_SNAKE_CASE_ : Any=0.0 , SCREAMING_SNAKE_CASE_ : str=5_02_57 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=True , SCREAMING_SNAKE_CASE_ : Optional[int]=True , SCREAMING_SNAKE_CASE_ : List[str]="gelu" , SCREAMING_SNAKE_CASE_ : Optional[Any]=2_56 , SCREAMING_SNAKE_CASE_ : List[str]=0.0 , SCREAMING_SNAKE_CASE_ : Optional[Any]=0.0 , SCREAMING_SNAKE_CASE_ : Dict=0.0 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=0.02 , SCREAMING_SNAKE_CASE_ : List[Any]=False , SCREAMING_SNAKE_CASE_ : Optional[int]=15_00 , SCREAMING_SNAKE_CASE_ : str=4_48 , SCREAMING_SNAKE_CASE_ : Any=5_02_56 , SCREAMING_SNAKE_CASE_ : List[str]=5_02_56 , SCREAMING_SNAKE_CASE_ : Tuple=5_02_56 , SCREAMING_SNAKE_CASE_ : Optional[int]=None , SCREAMING_SNAKE_CASE_ : Optional[int]=[2_20, 5_02_56] , SCREAMING_SNAKE_CASE_ : Any=False , SCREAMING_SNAKE_CASE_ : str=2_56 , SCREAMING_SNAKE_CASE_ : List[str]=False , SCREAMING_SNAKE_CASE_ : Optional[Any]=0.05 , SCREAMING_SNAKE_CASE_ : List[Any]=10 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=2 , SCREAMING_SNAKE_CASE_ : Any=0.0 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=10 , SCREAMING_SNAKE_CASE_ : Tuple=0 , SCREAMING_SNAKE_CASE_ : Tuple=7 , **SCREAMING_SNAKE_CASE_ : Any , ) -> Tuple: '''simple docstring''' A: Tuple = vocab_size A: Dict = num_mel_bins A: Tuple = d_model A: Optional[Any] = encoder_layers A: List[Any] = encoder_attention_heads A: Any = decoder_layers A: List[str] = decoder_attention_heads A: Optional[int] = decoder_ffn_dim A: Optional[Any] = encoder_ffn_dim A: Dict = dropout A: Optional[Any] = attention_dropout A: Dict = activation_dropout A: List[Any] = activation_function A: Optional[Any] = init_std A: Union[str, Any] = encoder_layerdrop A: List[Any] = decoder_layerdrop A: Any = use_cache A: Any = encoder_layers A: List[Any] = scale_embedding # scale factor will be sqrt(d_model) if True A: Tuple = max_source_positions A: str = max_target_positions # Audio Classification-specific parameters. Feel free to ignore for other classes. A: Optional[int] = classifier_proj_size A: Dict = use_weighted_layer_sum # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 A: Optional[Any] = apply_spec_augment A: Any = mask_time_prob A: List[str] = mask_time_length A: Optional[int] = mask_time_min_masks A: Any = mask_feature_prob A: Optional[int] = mask_feature_length A: List[str] = mask_feature_min_masks A: Union[str, Any] = median_filter_width super().__init__( pad_token_id=SCREAMING_SNAKE_CASE_ , bos_token_id=SCREAMING_SNAKE_CASE_ , eos_token_id=SCREAMING_SNAKE_CASE_ , is_encoder_decoder=SCREAMING_SNAKE_CASE_ , decoder_start_token_id=SCREAMING_SNAKE_CASE_ , suppress_tokens=SCREAMING_SNAKE_CASE_ , begin_suppress_tokens=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' @property def _snake_case ( self : Union[str, Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' A: int = OrderedDict( [ ('''input_features''', {0: '''batch''', 1: '''feature_size''', 2: '''encoder_sequence'''}), ] ) if self.use_past: A: Dict = {0: '''batch'''} else: A: int = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(SCREAMING_SNAKE_CASE_ , direction='''inputs''' ) return common_inputs def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"] , SCREAMING_SNAKE_CASE_ : int = -1 , SCREAMING_SNAKE_CASE_ : int = -1 , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : Optional["TensorType"] = None , SCREAMING_SNAKE_CASE_ : int = 2_20_50 , SCREAMING_SNAKE_CASE_ : float = 5.0 , SCREAMING_SNAKE_CASE_ : int = 2_20 , ) -> Mapping[str, Any]: '''simple docstring''' A: Any = OrderedDict() A: List[Any] = OnnxConfig.generate_dummy_inputs( self , preprocessor=preprocessor.feature_extractor , batch_size=SCREAMING_SNAKE_CASE_ , framework=SCREAMING_SNAKE_CASE_ , sampling_rate=SCREAMING_SNAKE_CASE_ , time_duration=SCREAMING_SNAKE_CASE_ , frequency=SCREAMING_SNAKE_CASE_ , ) A: Any = encoder_inputs['''input_features'''].shape[2] A: int = encoder_sequence_length // 2 if self.use_past else seq_length A: List[Any] = super().generate_dummy_inputs( preprocessor.tokenizer , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Tuple = encoder_inputs.pop('''input_features''' ) A: Optional[int] = decoder_inputs.pop('''decoder_input_ids''' ) if "past_key_values" in decoder_inputs: A: Dict = decoder_inputs.pop('''past_key_values''' ) return dummy_inputs @property def _snake_case ( self : Any ) -> float: '''simple docstring''' return 1E-3
319
'''simple docstring''' import argparse from collections import OrderedDict from pathlib import Path import torch from transformers import ( VisualBertConfig, VisualBertForMultipleChoice, VisualBertForPreTraining, VisualBertForQuestionAnswering, VisualBertForVisualReasoning, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = [ ('''bert.bert''', '''visual_bert'''), ('''bert.cls''', '''cls'''), ('''bert.classifier''', '''cls'''), ('''token_type_embeddings_visual''', '''visual_token_type_embeddings'''), ('''position_embeddings_visual''', '''visual_position_embeddings'''), ('''projection''', '''visual_projection'''), ] UpperCamelCase = [ '''nlvr2_coco_pre_trained.th''', '''nlvr2_fine_tuned.th''', '''nlvr2_pre_trained.th''', '''vcr_coco_pre_train.th''', '''vcr_fine_tune.th''', '''vcr_pre_train.th''', '''vqa_coco_pre_trained.th''', '''vqa_fine_tuned.th''', '''vqa_pre_trained.th''', ] def SCREAMING_SNAKE_CASE( __lowercase ) -> List[Any]: A: List[Any] = torch.load(__lowercase , map_location='''cpu''' ) return sd def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=rename_keys_prefix ) -> Optional[Any]: A: Tuple = OrderedDict() A: Dict = torch.arange(config.max_position_embeddings ).expand((1, -1) ) # detector_d = OrderedDict() for key in d: if "detector" in key: # detector_d[key.replace('detector.','')] = d[key] continue A: int = key for name_pair in rename_keys_prefix: A: Optional[int] = new_key.replace(name_pair[0] , name_pair[1] ) A: Union[str, Any] = d[key] if key == "bert.cls.predictions.decoder.weight": # Old bert code didn't have `decoder.bias`, but was added separately A: int = new_d['''cls.predictions.bias'''] return new_d @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Dict: assert ( checkpoint_path.split('''/''' )[-1] in ACCEPTABLE_CHECKPOINTS ), F"""The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}.""" # Get Config if "pre" in checkpoint_path: A: Optional[Any] = '''pretraining''' if "vcr" in checkpoint_path: A: Optional[int] = {'''visual_embedding_dim''': 5_1_2} elif "vqa_advanced" in checkpoint_path: A: Optional[Any] = {'''visual_embedding_dim''': 2_0_4_8} elif "vqa" in checkpoint_path: A: Dict = {'''visual_embedding_dim''': 2_0_4_8} elif "nlvr" in checkpoint_path: A: Tuple = {'''visual_embedding_dim''': 1_0_2_4} else: raise NotImplementedError(F"""No implementation found for `{checkpoint_path}`.""" ) else: if "vcr" in checkpoint_path: A: Dict = {'''visual_embedding_dim''': 5_1_2} A: List[str] = '''multichoice''' elif "vqa_advanced" in checkpoint_path: A: List[str] = {'''visual_embedding_dim''': 2_0_4_8} A: Optional[int] = '''vqa_advanced''' elif "vqa" in checkpoint_path: A: Dict = {'''visual_embedding_dim''': 2_0_4_8, '''num_labels''': 3_1_2_9} A: Union[str, Any] = '''vqa''' elif "nlvr" in checkpoint_path: A: Optional[int] = { '''visual_embedding_dim''': 1_0_2_4, '''num_labels''': 2, } A: str = '''nlvr''' A: Union[str, Any] = VisualBertConfig(**__lowercase ) # Load State Dict A: Union[str, Any] = load_state_dict(__lowercase ) A: str = get_new_dict(__lowercase , __lowercase ) if model_type == "pretraining": A: Optional[Any] = VisualBertForPreTraining(__lowercase ) elif model_type == "vqa": A: Optional[Any] = VisualBertForQuestionAnswering(__lowercase ) elif model_type == "nlvr": A: Union[str, Any] = VisualBertForVisualReasoning(__lowercase ) elif model_type == "multichoice": A: Any = VisualBertForMultipleChoice(__lowercase ) model.load_state_dict(__lowercase ) # Save Checkpoints Path(__lowercase ).mkdir(exist_ok=__lowercase ) model.save_pretrained(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument('''orig_checkpoint_path''', type=str, help='''A path to .th on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', type=str, help='''Path to the output PyTorch model.''') UpperCamelCase = parser.parse_args() convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)
319
1
'''simple docstring''' import os import sys import unittest UpperCamelCase = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, '''utils''')) import get_test_info # noqa: E402 from get_test_info import ( # noqa: E402 get_model_to_test_mapping, get_model_to_tester_mapping, get_test_to_tester_mapping, ) UpperCamelCase = os.path.join('''tests''', '''models''', '''bert''', '''test_modeling_bert.py''') UpperCamelCase = os.path.join('''tests''', '''models''', '''blip''', '''test_modeling_blip.py''') class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : Any ) -> Dict: '''simple docstring''' A: Dict = get_test_to_tester_mapping(SCREAMING_SNAKE_CASE_ ) A: str = get_test_to_tester_mapping(SCREAMING_SNAKE_CASE_ ) A: str = {'''BertModelTest''': '''BertModelTester'''} A: Optional[Any] = { '''BlipModelTest''': '''BlipModelTester''', '''BlipTextImageModelTest''': '''BlipTextImageModelsModelTester''', '''BlipTextModelTest''': '''BlipTextModelTester''', '''BlipTextRetrievalModelTest''': '''BlipTextRetrievalModelTester''', '''BlipVQAModelTest''': '''BlipVQAModelTester''', '''BlipVisionModelTest''': '''BlipVisionModelTester''', } self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Tuple ) -> Any: '''simple docstring''' A: Optional[int] = get_model_to_test_mapping(SCREAMING_SNAKE_CASE_ ) A: Dict = get_model_to_test_mapping(SCREAMING_SNAKE_CASE_ ) A: List[Any] = { '''BertForMaskedLM''': ['''BertModelTest'''], '''BertForMultipleChoice''': ['''BertModelTest'''], '''BertForNextSentencePrediction''': ['''BertModelTest'''], '''BertForPreTraining''': ['''BertModelTest'''], '''BertForQuestionAnswering''': ['''BertModelTest'''], '''BertForSequenceClassification''': ['''BertModelTest'''], '''BertForTokenClassification''': ['''BertModelTest'''], '''BertLMHeadModel''': ['''BertModelTest'''], '''BertModel''': ['''BertModelTest'''], } A: List[str] = { '''BlipForConditionalGeneration''': ['''BlipTextImageModelTest'''], '''BlipForImageTextRetrieval''': ['''BlipTextRetrievalModelTest'''], '''BlipForQuestionAnswering''': ['''BlipVQAModelTest'''], '''BlipModel''': ['''BlipModelTest'''], '''BlipTextModel''': ['''BlipTextModelTest'''], '''BlipVisionModel''': ['''BlipVisionModelTest'''], } self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int ) -> Tuple: '''simple docstring''' A: List[str] = get_model_to_tester_mapping(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = get_model_to_tester_mapping(SCREAMING_SNAKE_CASE_ ) A: str = { '''BertForMaskedLM''': ['''BertModelTester'''], '''BertForMultipleChoice''': ['''BertModelTester'''], '''BertForNextSentencePrediction''': ['''BertModelTester'''], '''BertForPreTraining''': ['''BertModelTester'''], '''BertForQuestionAnswering''': ['''BertModelTester'''], '''BertForSequenceClassification''': ['''BertModelTester'''], '''BertForTokenClassification''': ['''BertModelTester'''], '''BertLMHeadModel''': ['''BertModelTester'''], '''BertModel''': ['''BertModelTester'''], } A: Any = { '''BlipForConditionalGeneration''': ['''BlipTextImageModelsModelTester'''], '''BlipForImageTextRetrieval''': ['''BlipTextRetrievalModelTester'''], '''BlipForQuestionAnswering''': ['''BlipVQAModelTester'''], '''BlipModel''': ['''BlipModelTester'''], '''BlipTextModel''': ['''BlipTextModelTester'''], '''BlipVisionModel''': ['''BlipVisionModelTester'''], } self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ )
319
'''simple docstring''' from itertools import permutations def SCREAMING_SNAKE_CASE( __lowercase ) -> bool: if num[3] % 2 != 0: return False if (num[2] + num[3] + num[4]) % 3 != 0: return False if num[5] % 5 != 0: return False A: int = [7, 1_1, 1_3, 1_7] for i, test in enumerate(__lowercase ): if (num[i + 4] * 1_0_0 + num[i + 5] * 1_0 + num[i + 6]) % test != 0: return False return True def SCREAMING_SNAKE_CASE( __lowercase = 1_0 ) -> int: return sum( int(''''''.join(map(__lowercase , __lowercase ) ) ) for num in permutations(range(__lowercase ) ) if is_substring_divisible(__lowercase ) ) if __name__ == "__main__": print(f'{solution() = }')
319
1
'''simple docstring''' from __future__ import annotations import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFViTForImageClassification, TFViTModel if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Dict , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : int=13 , SCREAMING_SNAKE_CASE_ : List[str]=30 , SCREAMING_SNAKE_CASE_ : Optional[Any]=2 , SCREAMING_SNAKE_CASE_ : str=3 , SCREAMING_SNAKE_CASE_ : str=True , SCREAMING_SNAKE_CASE_ : Union[str, Any]=True , SCREAMING_SNAKE_CASE_ : List[Any]=32 , SCREAMING_SNAKE_CASE_ : str=2 , SCREAMING_SNAKE_CASE_ : Optional[int]=4 , SCREAMING_SNAKE_CASE_ : Dict=37 , SCREAMING_SNAKE_CASE_ : Union[str, Any]="gelu" , SCREAMING_SNAKE_CASE_ : Optional[int]=0.1 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=0.1 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=10 , SCREAMING_SNAKE_CASE_ : Dict=0.02 , SCREAMING_SNAKE_CASE_ : Optional[Any]=3 , SCREAMING_SNAKE_CASE_ : Dict=None , ) -> Tuple: '''simple docstring''' A: Union[str, Any] = parent A: int = batch_size A: Union[str, Any] = image_size A: Dict = patch_size A: List[Any] = num_channels A: List[Any] = is_training A: Any = use_labels A: str = hidden_size A: Optional[int] = num_hidden_layers A: Union[str, Any] = num_attention_heads A: List[str] = intermediate_size A: List[str] = hidden_act A: Tuple = hidden_dropout_prob A: List[Any] = attention_probs_dropout_prob A: List[str] = type_sequence_label_size A: Optional[Any] = initializer_range A: List[str] = scope # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) A: Union[str, Any] = (image_size // patch_size) ** 2 A: List[Any] = num_patches + 1 def _snake_case ( self : List[str] ) -> List[Any]: '''simple docstring''' A: Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A: List[Any] = None if self.use_labels: A: str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A: int = self.get_config() return config, pixel_values, labels def _snake_case ( self : List[str] ) -> Any: '''simple docstring''' return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=SCREAMING_SNAKE_CASE_ , initializer_range=self.initializer_range , ) def _snake_case ( self : Dict , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Any: '''simple docstring''' A: List[Any] = TFViTModel(config=SCREAMING_SNAKE_CASE_ ) A: Any = model(SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # Test with an image with different size than the one specified in config. A: int = self.image_size // 2 A: List[Any] = pixel_values[:, :, :image_size, :image_size] A: str = model(SCREAMING_SNAKE_CASE_ , interpolate_pos_encoding=SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_ ) A: Tuple = (image_size // self.patch_size) ** 2 + 1 self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, seq_length, self.hidden_size) ) def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Dict ) -> Union[str, Any]: '''simple docstring''' A: Union[str, Any] = self.type_sequence_label_size A: Union[str, Any] = TFViTForImageClassification(SCREAMING_SNAKE_CASE_ ) A: Tuple = model(SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # Test with an image with different size than the one specified in config. A: List[str] = self.image_size // 2 A: Optional[Any] = pixel_values[:, :, :image_size, :image_size] A: Optional[int] = model(SCREAMING_SNAKE_CASE_ , interpolate_pos_encoding=SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images A: List[str] = 1 A: Dict = TFViTForImageClassification(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A: Dict = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _snake_case ( self : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' A: str = self.prepare_config_and_inputs() A , A , A: Dict = config_and_inputs A: str = {'''pixel_values''': pixel_values} return config, inputs_dict @require_tf class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = (TFViTModel, TFViTForImageClassification) if is_tf_available() else () UpperCamelCase_ : Optional[Any] = ( {"""feature-extraction""": TFViTModel, """image-classification""": TFViTForImageClassification} if is_tf_available() else {} ) UpperCamelCase_ : Optional[Any] = False UpperCamelCase_ : int = False UpperCamelCase_ : str = False def _snake_case ( self : List[str] ) -> Optional[Any]: '''simple docstring''' A: str = TFViTModelTester(self ) A: Any = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE_ , has_text_modality=SCREAMING_SNAKE_CASE_ , hidden_size=37 ) def _snake_case ( self : Dict ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='''ViT does not use inputs_embeds''' ) def _snake_case ( self : Any ) -> Tuple: '''simple docstring''' pass @unittest.skip(reason='''ViT does not use inputs_embeds''' ) def _snake_case ( self : Optional[Any] ) -> Any: '''simple docstring''' pass def _snake_case ( self : Any ) -> List[Any]: '''simple docstring''' A , A: Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A: str = model_class(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) A: List[str] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE_ , tf.keras.layers.Layer ) ) def _snake_case ( self : int ) -> Optional[int]: '''simple docstring''' A , A: Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A: List[str] = model_class(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A: Any = [*signature.parameters.keys()] A: Tuple = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[Any] ) -> List[Any]: '''simple docstring''' A: Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] ) -> Optional[int]: '''simple docstring''' A: Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Tuple ) -> List[Any]: '''simple docstring''' A: str = TFViTModel.from_pretrained('''google/vit-base-patch16-224''' ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE( ) -> Union[str, Any]: A: str = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf @require_vision class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' @cached_property def _snake_case ( self : Tuple ) -> Dict: '''simple docstring''' return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224''' ) if is_vision_available() else None @slow def _snake_case ( self : Any ) -> str: '''simple docstring''' A: List[Any] = TFViTForImageClassification.from_pretrained('''google/vit-base-patch16-224''' ) A: Optional[Any] = self.default_image_processor A: Optional[int] = prepare_img() A: int = image_processor(images=SCREAMING_SNAKE_CASE_ , return_tensors='''tf''' ) # forward pass A: Union[str, Any] = model(**SCREAMING_SNAKE_CASE_ ) # verify the logits A: Tuple = tf.TensorShape((1, 10_00) ) self.assertEqual(outputs.logits.shape , SCREAMING_SNAKE_CASE_ ) A: Any = tf.constant([-0.2744, 0.8215, -0.0836] ) tf.debugging.assert_near(outputs.logits[0, :3] , SCREAMING_SNAKE_CASE_ , atol=1E-4 )
319
'''simple docstring''' import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt'''} UpperCamelCase = { '''vocab_file''': { '''allenai/longformer-base-4096''': '''https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json''', '''allenai/longformer-large-4096''': ( '''https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json''' ), '''allenai/longformer-large-4096-finetuned-triviaqa''': ( '''https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json''' ), '''allenai/longformer-base-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json''' ), '''allenai/longformer-large-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json''' ), }, '''merges_file''': { '''allenai/longformer-base-4096''': '''https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt''', '''allenai/longformer-large-4096''': ( '''https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt''' ), '''allenai/longformer-large-4096-finetuned-triviaqa''': ( '''https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt''' ), '''allenai/longformer-base-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt''' ), '''allenai/longformer-large-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt''' ), }, } UpperCamelCase = { '''allenai/longformer-base-4096''': 4096, '''allenai/longformer-large-4096''': 4096, '''allenai/longformer-large-4096-finetuned-triviaqa''': 4096, '''allenai/longformer-base-4096-extra.pos.embd.only''': 4096, '''allenai/longformer-large-4096-extra.pos.embd.only''': 4096, } @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def SCREAMING_SNAKE_CASE( ) -> Dict: A: Dict = ( list(range(ord('''!''' ) , ord('''~''' ) + 1 ) ) + list(range(ord('''¡''' ) , ord('''¬''' ) + 1 ) ) + list(range(ord('''®''' ) , ord('''ÿ''' ) + 1 ) ) ) A: Union[str, Any] = bs[:] A: List[str] = 0 for b in range(2**8 ): if b not in bs: bs.append(__lowercase ) cs.append(2**8 + n ) n += 1 A: List[Any] = [chr(__lowercase ) for n in cs] return dict(zip(__lowercase , __lowercase ) ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: A: Optional[Any] = set() A: Tuple = word[0] for char in word[1:]: pairs.add((prev_char, char) ) A: List[Any] = char return pairs class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : int = VOCAB_FILES_NAMES UpperCamelCase_ : int = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : int = ["""input_ids""", """attention_mask"""] def __init__( self : int , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : str="replace" , SCREAMING_SNAKE_CASE_ : str="<s>" , SCREAMING_SNAKE_CASE_ : Any="</s>" , SCREAMING_SNAKE_CASE_ : int="</s>" , SCREAMING_SNAKE_CASE_ : List[Any]="<s>" , SCREAMING_SNAKE_CASE_ : str="<unk>" , SCREAMING_SNAKE_CASE_ : Dict="<pad>" , SCREAMING_SNAKE_CASE_ : Dict="<mask>" , SCREAMING_SNAKE_CASE_ : Union[str, Any]=False , **SCREAMING_SNAKE_CASE_ : Tuple , ) -> List[str]: '''simple docstring''' A: int = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else bos_token A: Dict = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else eos_token A: int = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else sep_token A: Dict = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else cls_token A: Any = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else unk_token A: str = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it A: Dict = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else mask_token super().__init__( errors=SCREAMING_SNAKE_CASE_ , bos_token=SCREAMING_SNAKE_CASE_ , eos_token=SCREAMING_SNAKE_CASE_ , unk_token=SCREAMING_SNAKE_CASE_ , sep_token=SCREAMING_SNAKE_CASE_ , cls_token=SCREAMING_SNAKE_CASE_ , pad_token=SCREAMING_SNAKE_CASE_ , mask_token=SCREAMING_SNAKE_CASE_ , add_prefix_space=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) with open(SCREAMING_SNAKE_CASE_ , encoding='''utf-8''' ) as vocab_handle: A: str = json.load(SCREAMING_SNAKE_CASE_ ) A: str = {v: k for k, v in self.encoder.items()} A: Union[str, Any] = errors # how to handle errors in decoding A: Optional[int] = bytes_to_unicode() A: Union[str, Any] = {v: k for k, v in self.byte_encoder.items()} with open(SCREAMING_SNAKE_CASE_ , encoding='''utf-8''' ) as merges_handle: A: int = merges_handle.read().split('''\n''' )[1:-1] A: str = [tuple(merge.split() ) for merge in bpe_merges] A: Any = dict(zip(SCREAMING_SNAKE_CASE_ , range(len(SCREAMING_SNAKE_CASE_ ) ) ) ) A: Union[str, Any] = {} A: Tuple = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions A: Dict = re.compile(R'''\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+''' ) @property def _snake_case ( self : int ) -> List[Any]: '''simple docstring''' return len(self.encoder ) def _snake_case ( self : Optional[Any] ) -> int: '''simple docstring''' return dict(self.encoder , **self.added_tokens_encoder ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[Any]: '''simple docstring''' if token in self.cache: return self.cache[token] A: str = tuple(SCREAMING_SNAKE_CASE_ ) A: str = get_pairs(SCREAMING_SNAKE_CASE_ ) if not pairs: return token while True: A: Dict = min(SCREAMING_SNAKE_CASE_ , key=lambda SCREAMING_SNAKE_CASE_ : self.bpe_ranks.get(SCREAMING_SNAKE_CASE_ , float('''inf''' ) ) ) if bigram not in self.bpe_ranks: break A , A: Optional[Any] = bigram A: Tuple = [] A: List[Any] = 0 while i < len(SCREAMING_SNAKE_CASE_ ): try: A: Union[str, Any] = word.index(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) A: int = j if word[i] == first and i < len(SCREAMING_SNAKE_CASE_ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 A: Optional[Any] = tuple(SCREAMING_SNAKE_CASE_ ) A: Any = new_word if len(SCREAMING_SNAKE_CASE_ ) == 1: break else: A: Union[str, Any] = get_pairs(SCREAMING_SNAKE_CASE_ ) A: str = ''' '''.join(SCREAMING_SNAKE_CASE_ ) A: str = word return word def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Optional[int]: '''simple docstring''' A: Dict = [] for token in re.findall(self.pat , SCREAMING_SNAKE_CASE_ ): A: Tuple = ''''''.join( self.byte_encoder[b] for b in token.encode('''utf-8''' ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(SCREAMING_SNAKE_CASE_ ).split(''' ''' ) ) return bpe_tokens def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Optional[Any]: '''simple docstring''' return self.encoder.get(SCREAMING_SNAKE_CASE_ , self.encoder.get(self.unk_token ) ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> str: '''simple docstring''' return self.decoder.get(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Tuple: '''simple docstring''' A: Optional[int] = ''''''.join(SCREAMING_SNAKE_CASE_ ) A: Tuple = bytearray([self.byte_decoder[c] for c in text] ).decode('''utf-8''' , errors=self.errors ) return text def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(SCREAMING_SNAKE_CASE_ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return A: Union[str, Any] = os.path.join( SCREAMING_SNAKE_CASE_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) A: int = os.path.join( SCREAMING_SNAKE_CASE_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file'''] ) with open(SCREAMING_SNAKE_CASE_ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=SCREAMING_SNAKE_CASE_ , ensure_ascii=SCREAMING_SNAKE_CASE_ ) + '''\n''' ) A: Any = 0 with open(SCREAMING_SNAKE_CASE_ , '''w''' , encoding='''utf-8''' ) as writer: writer.write('''#version: 0.2\n''' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda SCREAMING_SNAKE_CASE_ : kv[1] ): if index != token_index: logger.warning( f"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.""" ''' Please check that the tokenizer is not corrupted!''' ) A: Union[str, Any] = token_index writer.write(''' '''.join(SCREAMING_SNAKE_CASE_ ) + '''\n''' ) index += 1 return vocab_file, merge_file def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] A: int = [self.cls_token_id] A: str = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None , SCREAMING_SNAKE_CASE_ : bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=SCREAMING_SNAKE_CASE_ , token_ids_a=SCREAMING_SNAKE_CASE_ , already_has_special_tokens=SCREAMING_SNAKE_CASE_ ) if token_ids_a is None: return [1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1] return [1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1, 1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1] def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' A: Dict = [self.sep_token_id] A: Optional[Any] = [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 + sep + token_ids_a + sep ) * [0] def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Dict=False , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> int: '''simple docstring''' A: Tuple = kwargs.pop('''add_prefix_space''' , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(SCREAMING_SNAKE_CASE_ ) > 0 and not text[0].isspace()): A: List[Any] = ''' ''' + text return (text, kwargs)
319
1
'''simple docstring''' import argparse import os import torch from transformers import FlavaConfig, FlavaForPreTraining from transformers.models.flava.convert_dalle_to_flava_codebook import convert_dalle_checkpoint def SCREAMING_SNAKE_CASE( __lowercase ) -> str: # encoder.embeddings are double copied in original FLAVA return sum(param.float().sum() if '''encoder.embeddings''' not in key else 0 for key, param in state_dict.items() ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> int: A: Any = {} for key, value in state_dict.items(): if "text_encoder.embeddings" in key or "image_encoder.embeddings" in key: continue A: str = key.replace('''heads.cmd.mim_head.cls.predictions''' , '''mmm_image_head''' ) A: Any = key.replace('''heads.cmd.mlm_head.cls.predictions''' , '''mmm_text_head''' ) A: Optional[Any] = key.replace('''heads.cmd.itm_head.cls''' , '''itm_head''' ) A: List[Any] = key.replace('''heads.cmd.itm_head.pooler''' , '''itm_head.pooler''' ) A: Tuple = key.replace('''heads.cmd.clip_head.logit_scale''' , '''flava.logit_scale''' ) A: Optional[int] = key.replace('''heads.fairseq_mlm.cls.predictions''' , '''mlm_head''' ) A: List[Any] = key.replace('''heads.imagenet.mim_head.cls.predictions''' , '''mim_head''' ) A: Tuple = key.replace('''mm_text_projection''' , '''flava.text_to_mm_projection''' ) A: int = key.replace('''mm_image_projection''' , '''flava.image_to_mm_projection''' ) A: List[Any] = key.replace('''image_encoder.module''' , '''flava.image_model''' ) A: Optional[int] = key.replace('''text_encoder.module''' , '''flava.text_model''' ) A: Tuple = key.replace('''mm_encoder.module.encoder.cls_token''' , '''flava.multimodal_model.cls_token''' ) A: str = key.replace('''mm_encoder.module''' , '''flava.multimodal_model''' ) A: Optional[Any] = key.replace('''text_projection''' , '''flava.text_projection''' ) A: Any = key.replace('''image_projection''' , '''flava.image_projection''' ) A: str = value.float() for key, value in codebook_state_dict.items(): A: List[Any] = value return upgrade @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase=None ) -> int: if config_path is not None: A: Tuple = FlavaConfig.from_pretrained(__lowercase ) else: A: List[str] = FlavaConfig() A: List[Any] = FlavaForPreTraining(__lowercase ).eval() A: Optional[Any] = convert_dalle_checkpoint(__lowercase , __lowercase , save_checkpoint=__lowercase ) if os.path.exists(__lowercase ): A: List[str] = torch.load(__lowercase , map_location='''cpu''' ) else: A: Optional[Any] = torch.hub.load_state_dict_from_url(__lowercase , map_location='''cpu''' ) A: int = upgrade_state_dict(__lowercase , __lowercase ) hf_model.load_state_dict(__lowercase ) A: Any = hf_model.state_dict() A: int = count_parameters(__lowercase ) A: Union[str, Any] = count_parameters(__lowercase ) + count_parameters(__lowercase ) assert torch.allclose(__lowercase , __lowercase , atol=1E-3 ) hf_model.save_pretrained(__lowercase ) if __name__ == "__main__": UpperCamelCase = 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 flava checkpoint''') parser.add_argument('''--codebook_path''', default=None, type=str, help='''Path to flava codebook checkpoint''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') UpperCamelCase = parser.parse_args() convert_flava_checkpoint(args.checkpoint_path, args.codebook_path, args.pytorch_dump_folder_path, args.config_path)
319
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> int: if not isinstance(__lowercase , __lowercase ): raise TypeError('''only integers accepted as input''' ) else: A: str = str(abs(__lowercase ) ) A: int = [list(__lowercase ) for char in range(len(__lowercase ) )] for index in range(len(__lowercase ) ): num_transpositions[index].pop(__lowercase ) return max( int(''''''.join(list(__lowercase ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__('''doctest''').testmod()
319
1
'''simple docstring''' import json import os import unittest from transformers.models.gptsan_japanese.tokenization_gptsan_japanese import ( VOCAB_FILES_NAMES, GPTSanJapaneseTokenizer, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowerCAmelCase_ ( UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Tuple = GPTSanJapaneseTokenizer UpperCamelCase_ : str = False UpperCamelCase_ : Any = {"""do_clean_text""": False, """add_prefix_space""": False} def _snake_case ( self : Optional[int] ) -> Optional[int]: '''simple docstring''' super().setUp() # fmt: off A: Optional[Any] = ['''こん''', '''こんに''', '''にちは''', '''ばんは''', '''世界,㔺界''', '''、''', '''。''', '''<BR>''', '''<SP>''', '''<TAB>''', '''<URL>''', '''<EMAIL>''', '''<TEL>''', '''<DATE>''', '''<PRICE>''', '''<BLOCK>''', '''<KIGOU>''', '''<U2000U2BFF>''', '''<|emoji1|>''', '''<unk>''', '''<|bagoftoken|>''', '''<|endoftext|>'''] # fmt: on A: Dict = {'''emoji''': {'''\ud83d\ude00''': '''<|emoji1|>'''}, '''emoji_inv''': {'''<|emoji1|>''': '''\ud83d\ude00'''}} # 😀 A: str = {'''unk_token''': '''<unk>'''} A: int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) A: int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''emoji_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) with open(self.emoji_file , '''w''' ) as emoji_writer: emoji_writer.write(json.dumps(SCREAMING_SNAKE_CASE_ ) ) def _snake_case ( self : Any , **SCREAMING_SNAKE_CASE_ : str ) -> Optional[int]: '''simple docstring''' kwargs.update(self.special_tokens_map ) return GPTSanJapaneseTokenizer.from_pretrained(self.tmpdirname , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : List[Any] ) -> Optional[int]: '''simple docstring''' A: List[str] = '''こんにちは、世界。 \nこんばんは、㔺界。😀''' A: Any = '''こんにちは、世界。 \nこんばんは、世界。😀''' return input_text, output_text def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : Any ) -> str: '''simple docstring''' A , A: List[Any] = self.get_input_output_texts(SCREAMING_SNAKE_CASE_ ) A: str = tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: Dict = tokenizer.decode(SCREAMING_SNAKE_CASE_ , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) return text, ids def _snake_case ( self : List[Any] ) -> int: '''simple docstring''' pass # TODO add if relevant def _snake_case ( self : List[Any] ) -> int: '''simple docstring''' pass # TODO add if relevant def _snake_case ( self : int ) -> Optional[int]: '''simple docstring''' pass # TODO add if relevant def _snake_case ( self : Tuple ) -> str: '''simple docstring''' A: Union[str, Any] = self.get_tokenizer() # Testing tokenization A: str = '''こんにちは、世界。 こんばんは、㔺界。''' A: List[str] = ['''こん''', '''にちは''', '''、''', '''世界''', '''。''', '''<SP>''', '''こん''', '''ばんは''', '''、''', '''㔺界''', '''。'''] A: Tuple = tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Testing conversion to ids without special tokens A: Optional[int] = [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6] A: str = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Testing conversion to ids with special tokens A: int = tokens + [tokenizer.unk_token] A: Dict = [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6, 19] A: int = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' A: Tuple = self.get_tokenizer() # Testing tokenization A: Tuple = '''こんにちは、<|bagoftoken|>世界。こんばんは、<|bagoftoken|>㔺界。''' A: int = '''こんにちは、、、、世界。こんばんは、、、、世界。''' A: int = tokenizer.encode(SCREAMING_SNAKE_CASE_ ) A: Dict = tokenizer.decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Dict ) -> str: '''simple docstring''' A: Any = self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''' ) # Testing tokenization A: List[Any] = '''こんにちは、世界。''' A: List[str] = '''こんばんは、㔺界。😀''' A: Union[str, Any] = '''こんにちは、世界。こんばんは、世界。😀''' A: List[str] = tokenizer.encode(prefix_text + input_text ) A: List[str] = tokenizer.encode('''''' , prefix_text=prefix_text + input_text ) A: str = tokenizer.encode(SCREAMING_SNAKE_CASE_ , prefix_text=SCREAMING_SNAKE_CASE_ ) A: List[str] = tokenizer.decode(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = tokenizer.decode(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = tokenizer.decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Optional[int] ) -> List[Any]: '''simple docstring''' A: Optional[int] = self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''' ) # Testing tokenization A: Optional[int] = '''こんにちは、世界。''' A: Optional[Any] = '''こんばんは、㔺界。😀''' A: Dict = len(tokenizer.encode(SCREAMING_SNAKE_CASE_ ) ) - 2 A: Optional[int] = len(tokenizer.encode(SCREAMING_SNAKE_CASE_ ) ) - 2 A: str = [1] + [0] * (len_prefix + len_text + 1) A: Tuple = [1] * (len_prefix + len_text + 1) + [0] A: List[str] = [1] + [1] * (len_prefix) + [0] * (len_text + 1) A: Dict = tokenizer(prefix_text + input_text ).token_type_ids A: int = tokenizer('''''' , prefix_text=prefix_text + input_text ).token_type_ids A: List[Any] = tokenizer(SCREAMING_SNAKE_CASE_ , prefix_text=SCREAMING_SNAKE_CASE_ ).token_type_ids self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : List[Any] ) -> int: '''simple docstring''' A: Dict = self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''' ) A: List[str] = tokenizer.encode('''あンいワ''' ) A: Optional[Any] = tokenizer.encode('''''' , prefix_text='''あンいワ''' ) A: Optional[Any] = tokenizer.encode('''いワ''' , prefix_text='''あン''' ) self.assertEqual(tokenizer.decode(SCREAMING_SNAKE_CASE_ ) , tokenizer.decode(SCREAMING_SNAKE_CASE_ ) ) self.assertEqual(tokenizer.decode(SCREAMING_SNAKE_CASE_ ) , tokenizer.decode(SCREAMING_SNAKE_CASE_ ) ) self.assertNotEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertNotEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertEqual(x_token_a[1] , x_token_a[-1] ) # SEG token self.assertEqual(x_token_a[1] , x_token_a[3] ) # SEG token @slow def _snake_case ( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' A: str = self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''' ) A: Optional[Any] = [['''武田信玄''', '''は、'''], ['''織田信長''', '''の配下の、''']] A: Optional[Any] = tokenizer(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ ) A: str = tokenizer.batch_encode_plus(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ ) # fmt: off A: Optional[int] = [[3_59_93, 86_40, 2_59_48, 3_59_98, 3_06_47, 3_56_75, 3_59_99, 3_59_99], [3_59_93, 1_03_82, 98_68, 3_59_98, 3_06_46, 94_59, 3_06_46, 3_56_75]] A: int = [[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0]] A: List[str] = [[1, 1, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1]] # fmt: on self.assertListEqual(x_token.input_ids , SCREAMING_SNAKE_CASE_ ) self.assertListEqual(x_token.token_type_ids , SCREAMING_SNAKE_CASE_ ) self.assertListEqual(x_token.attention_mask , SCREAMING_SNAKE_CASE_ ) self.assertListEqual(x_token_a.input_ids , SCREAMING_SNAKE_CASE_ ) self.assertListEqual(x_token_a.token_type_ids , SCREAMING_SNAKE_CASE_ ) self.assertListEqual(x_token_a.attention_mask , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Any ) -> str: '''simple docstring''' pass def _snake_case ( self : str ) -> List[str]: '''simple docstring''' pass
319
'''simple docstring''' from __future__ import annotations import math def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list: if len(__lowercase ) != 2 or len(a[0] ) != 2 or len(__lowercase ) != 2 or len(b[0] ) != 2: raise Exception('''Matrices are not 2x2''' ) A: str = [ [a[0][0] * b[0][0] + a[0][1] * b[1][0], a[0][0] * b[0][1] + a[0][1] * b[1][1]], [a[1][0] * b[0][0] + a[1][1] * b[1][0], a[1][0] * b[0][1] + a[1][1] * b[1][1]], ] return new_matrix def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: return [ [matrix_a[row][col] + matrix_b[row][col] for col in range(len(matrix_a[row] ) )] for row in range(len(__lowercase ) ) ] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: return [ [matrix_a[row][col] - matrix_b[row][col] for col in range(len(matrix_a[row] ) )] for row in range(len(__lowercase ) ) ] def SCREAMING_SNAKE_CASE( __lowercase ) -> tuple[list, list, list, list]: if len(__lowercase ) % 2 != 0 or len(a[0] ) % 2 != 0: raise Exception('''Odd matrices are not supported!''' ) A: Union[str, Any] = len(__lowercase ) A: str = matrix_length // 2 A: Optional[int] = [[a[i][j] for j in range(__lowercase , __lowercase )] for i in range(__lowercase )] A: Optional[Any] = [ [a[i][j] for j in range(__lowercase , __lowercase )] for i in range(__lowercase , __lowercase ) ] A: Union[str, Any] = [[a[i][j] for j in range(__lowercase )] for i in range(__lowercase )] A: int = [[a[i][j] for j in range(__lowercase )] for i in range(__lowercase , __lowercase )] return top_left, top_right, bot_left, bot_right def SCREAMING_SNAKE_CASE( __lowercase ) -> tuple[int, int]: return len(__lowercase ), len(matrix[0] ) def SCREAMING_SNAKE_CASE( __lowercase ) -> None: print('''\n'''.join(str(__lowercase ) for line in matrix ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list: if matrix_dimensions(__lowercase ) == (2, 2): return default_matrix_multiplication(__lowercase , __lowercase ) A , A , A , A: Union[str, Any] = split_matrix(__lowercase ) A , A , A , A: List[Any] = split_matrix(__lowercase ) A: Optional[int] = actual_strassen(__lowercase , matrix_subtraction(__lowercase , __lowercase ) ) A: Any = actual_strassen(matrix_addition(__lowercase , __lowercase ) , __lowercase ) A: Tuple = actual_strassen(matrix_addition(__lowercase , __lowercase ) , __lowercase ) A: Optional[int] = actual_strassen(__lowercase , matrix_subtraction(__lowercase , __lowercase ) ) A: Tuple = actual_strassen(matrix_addition(__lowercase , __lowercase ) , matrix_addition(__lowercase , __lowercase ) ) A: Union[str, Any] = actual_strassen(matrix_subtraction(__lowercase , __lowercase ) , matrix_addition(__lowercase , __lowercase ) ) A: List[str] = actual_strassen(matrix_subtraction(__lowercase , __lowercase ) , matrix_addition(__lowercase , __lowercase ) ) A: int = matrix_addition(matrix_subtraction(matrix_addition(__lowercase , __lowercase ) , __lowercase ) , __lowercase ) A: Any = matrix_addition(__lowercase , __lowercase ) A: List[Any] = matrix_addition(__lowercase , __lowercase ) A: List[str] = matrix_subtraction(matrix_subtraction(matrix_addition(__lowercase , __lowercase ) , __lowercase ) , __lowercase ) # construct the new matrix from our 4 quadrants A: Union[str, Any] = [] for i in range(len(__lowercase ) ): new_matrix.append(top_left[i] + top_right[i] ) for i in range(len(__lowercase ) ): new_matrix.append(bot_left[i] + bot_right[i] ) return new_matrix def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list: if matrix_dimensions(__lowercase )[1] != matrix_dimensions(__lowercase )[0]: A: int = ( '''Unable to multiply these matrices, please check the dimensions.\n''' F"""Matrix A: {matrixa}\n""" F"""Matrix B: {matrixa}""" ) raise Exception(__lowercase ) A: str = matrix_dimensions(__lowercase ) A: str = matrix_dimensions(__lowercase ) if dimensiona[0] == dimensiona[1] and dimensiona[0] == dimensiona[1]: return [matrixa, matrixa] A: Union[str, Any] = max(*__lowercase , *__lowercase ) A: Optional[int] = int(math.pow(2 , math.ceil(math.loga(__lowercase ) ) ) ) A: List[Any] = matrixa A: Tuple = matrixa # Adding zeros to the matrices so that the arrays dimensions are the same and also # power of 2 for i in range(0 , __lowercase ): if i < dimensiona[0]: for _ in range(dimensiona[1] , __lowercase ): new_matrixa[i].append(0 ) else: new_matrixa.append([0] * maxim ) if i < dimensiona[0]: for _ in range(dimensiona[1] , __lowercase ): new_matrixa[i].append(0 ) else: new_matrixa.append([0] * maxim ) A: Any = actual_strassen(__lowercase , __lowercase ) # Removing the additional zeros for i in range(0 , __lowercase ): if i < dimensiona[0]: for _ in range(dimensiona[1] , __lowercase ): final_matrix[i].pop() else: final_matrix.pop() return final_matrix if __name__ == "__main__": UpperCamelCase = [ [2, 3, 4, 5], [6, 4, 3, 1], [2, 3, 6, 7], [3, 1, 2, 4], [2, 3, 4, 5], [6, 4, 3, 1], [2, 3, 6, 7], [3, 1, 2, 4], [2, 3, 4, 5], [6, 2, 3, 1], ] UpperCamelCase = [[0, 2, 1, 1], [16, 2, 3, 3], [2, 2, 7, 7], [13, 11, 22, 4]] print(strassen(matrixa, matrixa))
319
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) UpperCamelCase = { '''configuration_swiftformer''': [ '''SWIFTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SwiftFormerConfig''', '''SwiftFormerOnnxConfig''', ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''SwiftFormerForImageClassification''', '''SwiftFormerModel''', '''SwiftFormerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_swiftformer import ( SWIFTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, SwiftFormerConfig, SwiftFormerOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swiftformer import ( SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, SwiftFormerForImageClassification, SwiftFormerModel, SwiftFormerPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
'''simple docstring''' from dataclasses import dataclass from typing import Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin @dataclass class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : torch.FloatTensor UpperCamelCase_ : torch.FloatTensor UpperCamelCase_ : Optional[torch.FloatTensor] = None class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = 2 @register_to_config def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : float = 0.02 , SCREAMING_SNAKE_CASE_ : float = 1_00 , SCREAMING_SNAKE_CASE_ : float = 1.007 , SCREAMING_SNAKE_CASE_ : float = 80 , SCREAMING_SNAKE_CASE_ : float = 0.05 , SCREAMING_SNAKE_CASE_ : float = 50 , ) -> Optional[int]: '''simple docstring''' A: Union[str, Any] = sigma_max # setable values A: int = None A: np.IntTensor = None A: torch.FloatTensor = None # sigma(t_i) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : Optional[int] = None ) -> torch.FloatTensor: '''simple docstring''' return sample def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Union[str, torch.device] = None ) -> Optional[Any]: '''simple docstring''' A: List[Any] = num_inference_steps A: List[str] = np.arange(0 , self.num_inference_steps )[::-1].copy() A: Any = torch.from_numpy(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ) A: str = [ ( self.config.sigma_max**2 * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1)) ) for i in self.timesteps ] A: Tuple = torch.tensor(SCREAMING_SNAKE_CASE_ , dtype=torch.floataa , device=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : Optional[torch.Generator] = None ) -> Tuple[torch.FloatTensor, float]: '''simple docstring''' if self.config.s_min <= sigma <= self.config.s_max: A: str = min(self.config.s_churn / self.num_inference_steps , 2**0.5 - 1 ) else: A: List[str] = 0 # sample eps ~ N(0, S_noise^2 * I) A: Optional[Any] = self.config.s_noise * randn_tensor(sample.shape , generator=SCREAMING_SNAKE_CASE_ ).to(sample.device ) A: Optional[Any] = sigma + gamma * sigma A: List[Any] = sample + ((sigma_hat**2 - sigma**2) ** 0.5 * eps) return sample_hat, sigma_hat def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : bool = True , ) -> Union[KarrasVeOutput, Tuple]: '''simple docstring''' A: Union[str, Any] = sample_hat + sigma_hat * model_output A: str = (sample_hat - pred_original_sample) / sigma_hat A: Optional[int] = sample_hat + (sigma_prev - sigma_hat) * derivative if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=SCREAMING_SNAKE_CASE_ , derivative=SCREAMING_SNAKE_CASE_ , pred_original_sample=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : bool = True , ) -> Union[KarrasVeOutput, Tuple]: '''simple docstring''' A: int = sample_prev + sigma_prev * model_output A: List[Any] = (sample_prev - pred_original_sample) / sigma_prev A: Dict = sample_hat + (sigma_prev - sigma_hat) * (0.5 * derivative + 0.5 * derivative_corr) if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=SCREAMING_SNAKE_CASE_ , derivative=SCREAMING_SNAKE_CASE_ , pred_original_sample=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : str ) -> Dict: '''simple docstring''' raise NotImplementedError()
319
1
'''simple docstring''' import pyarrow.parquet as pq import pytest from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config from datasets.features.image import Image from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[int]: assert isinstance(__lowercase , __lowercase ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: A: Union[str, Any] = tmp_path / '''cache''' A: List[str] = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A: Union[str, Any] = ParquetDatasetReader(__lowercase , cache_dir=__lowercase , keep_in_memory=__lowercase ).read() _check_parquet_dataset(__lowercase , __lowercase ) @pytest.mark.parametrize( '''features''' , [ None, {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}, {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''}, {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''}, {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''}, ] , ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> List[Any]: A: Union[str, Any] = tmp_path / '''cache''' A: Tuple = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} A: List[str] = features.copy() if features else default_expected_features A: Optional[Any] = ( Features({feature: Value(__lowercase ) for feature, dtype in features.items()} ) if features is not None else None ) A: Tuple = ParquetDatasetReader(__lowercase , features=__lowercase , cache_dir=__lowercase ).read() _check_parquet_dataset(__lowercase , __lowercase ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Union[str, Any]: A: Optional[int] = tmp_path / '''cache''' A: Union[str, Any] = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} A: Optional[int] = ParquetDatasetReader(__lowercase , cache_dir=__lowercase , split=__lowercase ).read() _check_parquet_dataset(__lowercase , __lowercase ) assert dataset.split == split if split else "train" @pytest.mark.parametrize('''path_type''' , [str, list] ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> List[Any]: if issubclass(__lowercase , __lowercase ): A: Dict = parquet_path elif issubclass(__lowercase , __lowercase ): A: Optional[Any] = [parquet_path] A: Union[str, Any] = tmp_path / '''cache''' A: List[str] = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} A: Any = ParquetDatasetReader(__lowercase , cache_dir=__lowercase ).read() _check_parquet_dataset(__lowercase , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=("train",) ) -> Tuple: assert isinstance(__lowercase , __lowercase ) for split in splits: A: Any = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Any: A: int = tmp_path / '''cache''' A: Optional[int] = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A: List[str] = ParquetDatasetReader( {'''train''': parquet_path} , cache_dir=__lowercase , keep_in_memory=__lowercase ).read() _check_parquet_datasetdict(__lowercase , __lowercase ) @pytest.mark.parametrize( '''features''' , [ None, {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}, {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''}, {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''}, {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''}, ] , ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Dict: A: List[Any] = tmp_path / '''cache''' A: Any = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} A: str = features.copy() if features else default_expected_features A: Tuple = ( Features({feature: Value(__lowercase ) for feature, dtype in features.items()} ) if features is not None else None ) A: str = ParquetDatasetReader({'''train''': parquet_path} , features=__lowercase , cache_dir=__lowercase ).read() _check_parquet_datasetdict(__lowercase , __lowercase ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Union[str, Any]: if split: A: str = {split: parquet_path} else: A: Any = '''train''' A: Optional[Any] = {'''train''': parquet_path, '''test''': parquet_path} A: Union[str, Any] = tmp_path / '''cache''' A: int = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} A: int = ParquetDatasetReader(__lowercase , cache_dir=__lowercase ).read() _check_parquet_datasetdict(__lowercase , __lowercase , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[str]: A: Union[str, Any] = ParquetDatasetWriter(__lowercase , tmp_path / '''foo.parquet''' ) assert writer.write() > 0 A: Tuple = pq.ParquetFile(tmp_path / '''foo.parquet''' ) A: Optional[Any] = pf.read() assert dataset.data.table == output_table def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[Any]: A: Any = str(shared_datadir / '''test_image_rgb.jpg''' ) A: str = {'''image''': [image_path]} A: Union[str, Any] = Features({'''image''': Image()} ) A: str = Dataset.from_dict(__lowercase , features=__lowercase ) A: Optional[Any] = ParquetDatasetWriter(__lowercase , tmp_path / '''foo.parquet''' ) assert writer.write() > 0 A: int = Dataset.from_parquet(str(tmp_path / '''foo.parquet''' ) ) assert dataset.features == reloaded_dataset.features A: List[str] = ParquetDatasetReader(str(tmp_path / '''foo.parquet''' ) , streaming=__lowercase ).read() assert dataset.features == reloaded_iterable_dataset.features @pytest.mark.parametrize( '''feature, expected''' , [ (Features({'''foo''': Value('''int32''' )} ), None), (Features({'''image''': Image(), '''foo''': Value('''int32''' )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS), (Features({'''nested''': Sequence(Audio() )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS), ] , ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: assert get_writer_batch_size(__lowercase ) == expected
319
'''simple docstring''' import json import logging import math import os import sys from dataclasses import dataclass, field from typing import Optional from datasets import Dataset, load_dataset import transformers from transformers import ( CONFIG_MAPPING, MODEL_FOR_MASKED_LM_MAPPING, AutoConfig, AutoModelForMaskedLM, AutoTokenizer, DataCollatorForWholeWordMask, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process UpperCamelCase = logging.getLogger(__name__) UpperCamelCase = list(MODEL_FOR_MASKED_LM_MAPPING.keys()) UpperCamelCase = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={ """help""": ( """The model checkpoint for weights initialization.Don't set if you want to train a model from scratch.""" ) } , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """If training from scratch, pass a model type from the list: """ + """, """.join(UpperCAmelCase_ )} , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={ """help""": ( """Override some existing default config settings when a model is trained from scratch. Example: """ """n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index""" ) } , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) UpperCamelCase_ : bool = field( default=UpperCAmelCase_ , metadata={"""help""": """Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."""} , ) UpperCamelCase_ : str = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) UpperCamelCase_ : bool = field( default=UpperCAmelCase_ , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) def _snake_case ( self : Tuple ) -> List[Any]: '''simple docstring''' if self.config_overrides is not None and (self.config_name is not None or self.model_name_or_path is not None): raise ValueError( '''--config_overrides can\'t be used in combination with --config_name or --model_name_or_path''' ) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """The name of the dataset to use (via the datasets library)."""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) UpperCamelCase_ : Optional[str] = field(default=UpperCAmelCase_ , metadata={"""help""": """The input training data file (a text file)."""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """An optional input evaluation data file to evaluate the perplexity on (a text file)."""} , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """An optional input train ref data file for whole word masking in Chinese."""} , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """An optional input validation ref data file for whole word masking in Chinese."""} , ) UpperCamelCase_ : bool = field( default=UpperCAmelCase_ , metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) UpperCamelCase_ : Optional[int] = field( default=5 , metadata={ """help""": """The percentage of the train set used as validation set in case there's no validation split""" } , ) UpperCamelCase_ : Optional[int] = field( default=UpperCAmelCase_ , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated. Default to the max input length of the model.""" ) } , ) UpperCamelCase_ : Optional[int] = field( default=UpperCAmelCase_ , metadata={"""help""": """The number of processes to use for the preprocessing."""} , ) UpperCamelCase_ : float = field( default=0.15 , metadata={"""help""": """Ratio of tokens to mask for masked language modeling loss"""} ) UpperCamelCase_ : bool = field( default=UpperCAmelCase_ , metadata={ """help""": ( """Whether to pad all samples to `max_seq_length`. """ """If False, will pad the samples dynamically when batching to the maximum length in the batch.""" ) } , ) def _snake_case ( self : List[Any] ) -> Optional[int]: '''simple docstring''' if self.train_file is not None: A: Tuple = self.train_file.split('''.''' )[-1] assert extension in ["csv", "json", "txt"], "`train_file` should be a csv, a json or a txt file." if self.validation_file is not None: A: str = self.validation_file.split('''.''' )[-1] assert extension in ["csv", "json", "txt"], "`validation_file` should be a csv, a json or a txt file." def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[str]: with open(__lowercase , '''r''' , encoding='''utf-8''' ) as f: A: List[Any] = [json.loads(__lowercase ) for line in f.read().splitlines() if (len(__lowercase ) > 0 and not line.isspace())] assert len(__lowercase ) == len(__lowercase ) A: Optional[int] = {c: dataset[c] for c in dataset.column_names} A: Union[str, Any] = refs return Dataset.from_dict(__lowercase ) def SCREAMING_SNAKE_CASE( ) -> int: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A: int = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('''.json''' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. A , A , A: Optional[int] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: A , A , A: List[Any] = parser.parse_args_into_dataclasses() # Detecting last checkpoint. A: Any = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: A: Any = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. """ '''Use --overwrite_output_dir to overcome.''' ) elif last_checkpoint is not None: logger.info( F"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ '''the `--output_dir` or add `--overwrite_output_dir` to train from scratch.''' ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , handlers=[logging.StreamHandler(sys.stdout )] , ) logger.setLevel(logging.INFO if is_main_process(training_args.local_rank ) else logging.WARN ) # Log on each process the small summary: logger.warning( F"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + F"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('''Training/evaluation parameters %s''' , __lowercase ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below) # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/ # (the dataset will be downloaded automatically from the datasets Hub). # # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called # 'text' is found. You can easily tweak this behavior (see below). # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. A: Dict = load_dataset(data_args.dataset_name , data_args.dataset_config_name ) if "validation" not in datasets.keys(): A: int = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F"""train[:{data_args.validation_split_percentage}%]""" , ) A: Dict = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F"""train[{data_args.validation_split_percentage}%:]""" , ) else: A: Any = {} if data_args.train_file is not None: A: int = data_args.train_file if data_args.validation_file is not None: A: Optional[int] = data_args.validation_file A: List[str] = data_args.train_file.split('''.''' )[-1] if extension == "txt": A: int = '''text''' A: Any = load_dataset(__lowercase , data_files=__lowercase ) # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at # https://huggingface.co/docs/datasets/loading_datasets.html. # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. A: Dict = { '''cache_dir''': model_args.cache_dir, '''revision''': model_args.model_revision, '''use_auth_token''': True if model_args.use_auth_token else None, } if model_args.config_name: A: List[Any] = AutoConfig.from_pretrained(model_args.config_name , **__lowercase ) elif model_args.model_name_or_path: A: int = AutoConfig.from_pretrained(model_args.model_name_or_path , **__lowercase ) else: A: str = CONFIG_MAPPING[model_args.model_type]() logger.warning('''You are instantiating a new config instance from scratch.''' ) if model_args.config_overrides is not None: logger.info(F"""Overriding config: {model_args.config_overrides}""" ) config.update_from_string(model_args.config_overrides ) logger.info(F"""New config: {config}""" ) A: Tuple = { '''cache_dir''': model_args.cache_dir, '''use_fast''': model_args.use_fast_tokenizer, '''revision''': model_args.model_revision, '''use_auth_token''': True if model_args.use_auth_token else None, } if model_args.tokenizer_name: A: Optional[int] = AutoTokenizer.from_pretrained(model_args.tokenizer_name , **__lowercase ) elif model_args.model_name_or_path: A: Union[str, Any] = AutoTokenizer.from_pretrained(model_args.model_name_or_path , **__lowercase ) else: raise ValueError( '''You are instantiating a new tokenizer from scratch. This is not supported by this script.''' '''You can do it from another script, save it, and load it from here, using --tokenizer_name.''' ) if model_args.model_name_or_path: A: List[Any] = AutoModelForMaskedLM.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=__lowercase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info('''Training new model from scratch''' ) A: List[Any] = AutoModelForMaskedLM.from_config(__lowercase ) model.resize_token_embeddings(len(__lowercase ) ) # Preprocessing the datasets. # First we tokenize all the texts. if training_args.do_train: A: int = datasets['''train'''].column_names else: A: str = datasets['''validation'''].column_names A: Tuple = '''text''' if '''text''' in column_names else column_names[0] A: List[str] = '''max_length''' if data_args.pad_to_max_length else False def tokenize_function(__lowercase ): # Remove empty lines A: int = [line for line in examples['''text'''] if len(__lowercase ) > 0 and not line.isspace()] return tokenizer(examples['''text'''] , padding=__lowercase , truncation=__lowercase , max_length=data_args.max_seq_length ) A: str = datasets.map( __lowercase , batched=__lowercase , num_proc=data_args.preprocessing_num_workers , remove_columns=[text_column_name] , load_from_cache_file=not data_args.overwrite_cache , ) # Add the chinese references if provided if data_args.train_ref_file is not None: A: List[str] = add_chinese_references(tokenized_datasets['''train'''] , data_args.train_ref_file ) if data_args.validation_ref_file is not None: A: Dict = add_chinese_references( tokenized_datasets['''validation'''] , data_args.validation_ref_file ) # If we have ref files, need to avoid it removed by trainer A: Optional[Any] = data_args.train_ref_file or data_args.validation_ref_file if has_ref: A: List[Any] = False # Data collator # This one will take care of randomly masking the tokens. A: Optional[Any] = DataCollatorForWholeWordMask(tokenizer=__lowercase , mlm_probability=data_args.mlm_probability ) # Initialize our Trainer A: Optional[int] = Trainer( model=__lowercase , args=__lowercase , train_dataset=tokenized_datasets['''train'''] if training_args.do_train else None , eval_dataset=tokenized_datasets['''validation'''] if training_args.do_eval else None , tokenizer=__lowercase , data_collator=__lowercase , ) # Training if training_args.do_train: if last_checkpoint is not None: A: Optional[int] = last_checkpoint elif model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path ): A: str = model_args.model_name_or_path else: A: List[str] = None A: str = trainer.train(resume_from_checkpoint=__lowercase ) trainer.save_model() # Saves the tokenizer too for easy upload A: Union[str, Any] = os.path.join(training_args.output_dir , '''train_results.txt''' ) if trainer.is_world_process_zero(): with open(__lowercase , '''w''' ) as writer: logger.info('''***** Train results *****''' ) for key, value in sorted(train_result.metrics.items() ): logger.info(F""" {key} = {value}""" ) writer.write(F"""{key} = {value}\n""" ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , '''trainer_state.json''' ) ) # Evaluation A: Optional[int] = {} if training_args.do_eval: logger.info('''*** Evaluate ***''' ) A: Optional[Any] = trainer.evaluate() A: Union[str, Any] = math.exp(eval_output['''eval_loss'''] ) A: Dict = perplexity A: Any = os.path.join(training_args.output_dir , '''eval_results_mlm_wwm.txt''' ) if trainer.is_world_process_zero(): with open(__lowercase , '''w''' ) as writer: logger.info('''***** Eval results *****''' ) for key, value in sorted(results.items() ): logger.info(F""" {key} = {value}""" ) writer.write(F"""{key} = {value}\n""" ) return results def SCREAMING_SNAKE_CASE( __lowercase ) -> List[Any]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
319
1
'''simple docstring''' from __future__ import annotations import matplotlib.pyplot as plt # type: ignore import numpy # initial triangle of Koch snowflake UpperCamelCase = numpy.array([0, 0]) UpperCamelCase = numpy.array([0.5, 0.8_66_02_54]) UpperCamelCase = numpy.array([1, 0]) UpperCamelCase = [VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[numpy.ndarray]: A: List[str] = initial_vectors for _ in range(__lowercase ): A: Dict = iteration_step(__lowercase ) return vectors def SCREAMING_SNAKE_CASE( __lowercase ) -> list[numpy.ndarray]: A: Union[str, Any] = [] for i, start_vector in enumerate(vectors[:-1] ): A: Optional[Any] = vectors[i + 1] new_vectors.append(__lowercase ) A: List[Any] = end_vector - start_vector new_vectors.append(start_vector + difference_vector / 3 ) new_vectors.append( start_vector + difference_vector / 3 + rotate(difference_vector / 3 , 6_0 ) ) new_vectors.append(start_vector + difference_vector * 2 / 3 ) new_vectors.append(vectors[-1] ) return new_vectors def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> numpy.ndarray: A: Any = numpy.radians(__lowercase ) A , A: Optional[Any] = numpy.cos(__lowercase ), numpy.sin(__lowercase ) A: Tuple = numpy.array(((c, -s), (s, c)) ) return numpy.dot(__lowercase , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> None: A: List[Any] = plt.gca() axes.set_aspect('''equal''' ) # matplotlib.pyplot.plot takes a list of all x-coordinates and a list of all # y-coordinates as inputs, which are constructed from the vector-list using # zip() A , A: str = zip(*__lowercase ) plt.plot(__lowercase , __lowercase ) plt.show() if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase = iterate(INITIAL_VECTORS, 5) plot(processed_vectors)
319
'''simple docstring''' import json import os import unittest from typing import Tuple from transformers import WavaVecaPhonemeCTCTokenizer from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.models.wavaveca_phoneme.tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizerOutput from transformers.testing_utils import require_phonemizer from ...test_tokenization_common import TokenizerTesterMixin @require_phonemizer class lowerCAmelCase_ ( UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Any = WavaVecaPhonemeCTCTokenizer UpperCamelCase_ : Tuple = False def _snake_case ( self : str ) -> Union[str, Any]: '''simple docstring''' super().setUp() A: Optional[int] = ( '''<s> <pad> </s> <unk> n s t ə l a i k d m ɛ ɾ e ɪ p o ɐ z ð f j v b ɹ ʁ ʊ iː r w ʌ u ɡ æ aɪ ʃ h ɔ ɑː ''' '''ŋ ɚ eɪ β uː y ɑ̃ oʊ ᵻ eː θ aʊ ts oː ɔ̃ ɣ ɜ ɑ dʒ əl x ɜː ç ʒ tʃ ɔː ɑːɹ ɛ̃ ʎ ɔːɹ ʋ aː ɕ œ ø oːɹ ɲ yː ''' '''ʔ iə i5 s. tɕ ?? nʲ ɛː œ̃ ɭ ɔø ʑ tʲ ɨ ɛɹ ts. rʲ ɪɹ ɭʲ i.5 ɔɪ q sʲ u5 ʊɹ iɜ a5 iɛ5 øː ʕ ja əɜ th ɑ5 ''' '''oɪ dʲ ə5 tɕh ts.h mʲ ɯ dʑ vʲ e̞ tʃʲ ei5 o5 onɡ5 ɑu5 iɑ5 ai5 aɪɚ kh ə1 ʐ i2 ʉ ħ t[ aɪə ʲ ju ə2 u2 oɜ ''' '''pː iɛɜ ou5 y5 uɜ tː uo5 d[ uoɜ tsh ɑɜ ɵ i̪5 uei5 ɟ aɜ ɑɨ i.ɜ eʊ o2 ɐ̃ ä pʲ kʲ n̩ ɒ ph ɑu2 uɨ əɪ ɫ ɬ ''' '''yɜ bʲ ɑ2 s̪ aiɜ χ ɐ̃ʊ̃ 1 ə4 yæɜ a2 ɨː t̪ iouɜ ũ onɡɜ aɨ iɛ2 ɔɨ ɑuɜ o̞ ei2 iou2 c kː y2 ɖ oe dˤ yɛɜ ''' '''əʊ S ɡʲ onɡ2 u" eiɜ ʈ ɯᵝ iou5 dZ r̝̊ i.2 tS s^ ʝ yə5 iɑɜ uə5 pf ɨu iɑ2 ou2 ər2 fʲ ai2 r̝ uəɜ ɳ əɨ ''' '''ua5 uɪ ɽ bː yu5 uo2 yɛ5 l̩ ɻ ərɜ ʂ i̪2 ouɜ uaɜ a. a.ː yæ5 dː r̩ ee ɪu ər5 i̪ ɜ æi u: i.ː t^ o1 ɪ^ ''' '''ai ueiɜ æː ɛɪ eə i. ɴ ie ua2 ɑ1 o4 tʃː o: ɑ: u1 N i̪1 au yæ2 u. qː yəɜ y: kʰ tʃʰ iʊ sx õ uo tʰ ''' '''uai5 bʰ u.ː uə2 ʊə d^ s̪ː yiɜ dʰ r. oe: i1 ɟː yu2 nʲʲ i̪4 uei2 tsʲ ɸ ĩ ɑ4 t̪ː eɑ u4 e: tsː ʈʰ ɡʰ ''' '''ɯɯ dʒʲ ʂʲ X ɵː uaiɜ tɕʲ ã t^ː ẽː yɛ2 cː i.1 ɛʊ dˤdˤ dʒː i4 ɡː yi ɕʲ ɟʰ pʰ dʑʲ yuɜ ua1 ua4 æiː ɐɐ ''' '''ui iou1 ʊː a1 iou4 cʰ iɛ1 yə2 ɖʰ ẽ ʒʲ ää ər4 iːː ɪː iɑ1 ər1 œː øi ɪuː cʰcʰ əː1 iː1 ũ kʰː o̞o̞ xʲ ''' '''ou1 iɛ4 e̞e̞ y1 dzː dʲʲ dʰː ɯᵝɯᵝ lː uo1 i.4 i: yɛ5ʲ a4''' ).split(''' ''' ) A: Union[str, Any] = dict(zip(SCREAMING_SNAKE_CASE_ , range(len(SCREAMING_SNAKE_CASE_ ) ) ) ) A: Dict = {'''pad_token''': '''<pad>''', '''unk_token''': '''<unk>''', '''bos_token''': '''<s>''', '''eos_token''': '''</s>'''} A: Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(SCREAMING_SNAKE_CASE_ ) + '''\n''' ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Tuple=False , SCREAMING_SNAKE_CASE_ : Any=20 , SCREAMING_SNAKE_CASE_ : Optional[int]=5 ) -> Tuple[str, list]: '''simple docstring''' A: int = [(i, tokenizer.decode([i] , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ )) for i in range(len(SCREAMING_SNAKE_CASE_ ) )] A: Optional[Any] = list(filter(lambda SCREAMING_SNAKE_CASE_ : [t[0]] == tokenizer.encode(t[1] , do_phonemize=SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) ) if max_length is not None and len(SCREAMING_SNAKE_CASE_ ) > max_length: A: int = toks[:max_length] if min_length is not None and len(SCREAMING_SNAKE_CASE_ ) < min_length and len(SCREAMING_SNAKE_CASE_ ) > 0: while len(SCREAMING_SNAKE_CASE_ ) < min_length: A: Dict = toks + toks # toks_str = [t[1] for t in toks] A: Union[str, Any] = [t[0] for t in toks] # Ensure consistency A: List[str] = tokenizer.decode(SCREAMING_SNAKE_CASE_ , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) if " " not in output_txt and len(SCREAMING_SNAKE_CASE_ ) > 1: A: int = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) + ''' ''' + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) ) if with_prefix_space: A: Tuple = ''' ''' + output_txt A: List[str] = tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) return output_txt, output_ids def _snake_case ( self : Optional[int] , **SCREAMING_SNAKE_CASE_ : int ) -> Dict: '''simple docstring''' kwargs.update(self.special_tokens_map ) return WavaVecaPhonemeCTCTokenizer.from_pretrained(self.tmpdirname , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int ) -> Optional[Any]: '''simple docstring''' A: List[Any] = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) # check adding a single token tokenizer.add_tokens('''xxx''' ) A: Any = tokenizer('''m xxx ɪ''' , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids self.assertEqual(SCREAMING_SNAKE_CASE_ , [13, 3_92, 17] ) # xxx should be last token tokenizer.add_tokens(['''aaa''', '''bbb''', '''ccc'''] ) A: Optional[int] = tokenizer('''m aaa ɪ ccc''' , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids self.assertEqual(SCREAMING_SNAKE_CASE_ , [13, 3_93, 17, 3_95] ) # aaa and ccc should be after xxx and 2 after aaa A: str = tokenizer('''maɪ c''' , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids self.assertEqual(SCREAMING_SNAKE_CASE_ , [3, 2_00] ) # mai should be <unk> (=3) def _snake_case ( self : int ) -> List[Any]: '''simple docstring''' A: Any = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: Any = '''Hello how are you''' A: Optional[Any] = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''h ə l oʊ h aʊ ɑːɹ j uː''' ) def _snake_case ( self : Tuple ) -> Dict: '''simple docstring''' A: str = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: List[Any] = '''Hello how are you''' A: Any = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) self.assertEqual(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids , tokenizer(SCREAMING_SNAKE_CASE_ , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids ) def _snake_case ( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' A: str = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: List[str] = '''Hello how are you''' A: Union[str, Any] = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) A: Union[str, Any] = tokenizer.decode(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Dict ) -> Optional[Any]: '''simple docstring''' A: Dict = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: Optional[Any] = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98], [24, 22, 5, 24, 22, 5, 77], ] A: List[str] = tokenizer.decode(sample_ids[0] ) A: List[str] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , batch_tokens[0] ) self.assertEqual(SCREAMING_SNAKE_CASE_ , ['''k s ɾ ɾ l ɭʲ''', '''j ð s j ð s oːɹ'''] ) def _snake_case ( self : Any ) -> Optional[int]: '''simple docstring''' A: int = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) A: List[Any] = '''Hello how are you''' A: Optional[Any] = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''h ə l oʊ | h aʊ | ɑːɹ | j uː |''' ) def _snake_case ( self : List[str] ) -> int: '''simple docstring''' A: Optional[Any] = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) A: Optional[Any] = '''Hello how are you''' A: Any = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) self.assertEqual(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids , tokenizer(SCREAMING_SNAKE_CASE_ , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids ) def _snake_case ( self : Dict ) -> Any: '''simple docstring''' A: Optional[int] = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) # fmt: off A: str = [ [11, 5, 15, tokenizer.pad_token_id, tokenizer.word_delimiter_token_id, 15, 8, tokenizer.word_delimiter_token_id, 98], [tokenizer.word_delimiter_token_id, 24, 22, tokenizer.word_delimiter_token_id, 5, 24, 22, 5, 77], ] # fmt: on # decode with word_del_token filter A: Tuple = tokenizer.decode(sample_ids[0] ) A: Optional[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , batch_tokens[0] ) self.assertEqual(SCREAMING_SNAKE_CASE_ , ['''k s ɾ ɾ l ɭʲ''', '''j ð s j ð s oːɹ'''] ) # decode with no word_del_token filter A: str = tokenizer.decode(sample_ids[0] , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , batch_tokens[0] ) self.assertEqual(SCREAMING_SNAKE_CASE_ , ['''k s ɾ | ɾ l | ɭʲ''', '''| j ð | s j ð s oːɹ'''] ) def _snake_case ( self : int ) -> List[str]: '''simple docstring''' A: Dict = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) A: Union[str, Any] = '''Hello how are you''' A: Tuple = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) A: Any = tokenizer.decode(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] ) -> Any: '''simple docstring''' A: Dict = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) A: Any = '''Hello how are you''' A: List[Any] = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) A: List[Any] = tokenizer.decode(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) self.assertEqual(''' '''.join([p.strip() for p in phonemes.split(''' |''' )] ).strip() , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] ) -> Optional[Any]: '''simple docstring''' A: List[str] = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token=SCREAMING_SNAKE_CASE_ ) A: List[Any] = '''Hello how are you''' A: List[str] = tokenizer(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ).input_ids A: Tuple = tokenizer(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''fr-fr''' ).input_ids self.assertNotEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Tuple = tokenizer.decode(SCREAMING_SNAKE_CASE_ ) A: Any = tokenizer.decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''h ə l oʊ h aʊ ɑːɹ j uː''' ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''ɛ l o h aʊ a ʁ j u''' ) def _snake_case ( self : str ) -> str: '''simple docstring''' A: str = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: str = '''Hello how Are you''' A: Union[str, Any] = '''hello how are you''' A: List[str] = tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids A: str = tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int ) -> List[Any]: '''simple docstring''' A: Union[str, Any] = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) tokenizer.add_tokens(['''!''', '''?'''] ) tokenizer.add_special_tokens({'''cls_token''': '''$$$'''} ) # fmt: off A: Tuple = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98, 3_92, 3_92, 3_93, 3_92, 3_92, 3_93, 3_94, 3_94], [24, 22, 5, 24, 22, 5, 77, tokenizer.pad_token_id, 3_94, 3_94], ] # fmt: on A: List[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , ['''k s ɾ ɾ l ɭʲ!?!? $$$''', '''j ð s j ð s oːɹ $$$'''] ) @staticmethod def _snake_case ( SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Tuple: '''simple docstring''' A: Any = [d[key] for d in offsets] return retrieved_list def _snake_case ( self : Any ) -> Tuple: '''simple docstring''' A: str = self.get_tokenizer(word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) # fmt: off # ksssɾɾ|ɾɾ<pad>ɾɾ|<pad>ɾlll|ɭʲ -> k s ɾ ɾ | ɾ l | ɭʲ" A: Union[str, Any] = [11, 5, 5, 5, 15, 15, tokenizer.pad_token_id, 15, 15, tokenizer.word_delimiter_token_id, tokenizer.pad_token_id, 15, 8, 8, 8, tokenizer.word_delimiter_token_id, 98] # fmt: on A: int = tokenizer.decode(SCREAMING_SNAKE_CASE_ , output_char_offsets=SCREAMING_SNAKE_CASE_ , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) # check Wav2Vec2CTCTokenizerOutput keys for char self.assertEqual(len(outputs.keys() ) , 2 ) self.assertTrue('''text''' in outputs ) self.assertTrue('''char_offsets''' in outputs ) self.assertTrue(isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) # check that order of chars is correct and identical for both outputs self.assertEqual(''' '''.join(self.get_from_offsets(outputs['''char_offsets'''] , '''char''' ) ) , outputs.text ) self.assertListEqual( self.get_from_offsets(outputs['''char_offsets'''] , '''char''' ) , ['''k''', '''s''', '''ɾ''', '''ɾ''', '''|''', '''ɾ''', '''l''', '''|''', '''ɭʲ'''] ) # check that offsets are actually correct for char # 0-1 is 11, 1-4 is 5, 4-6 is first 15, 6-7 is <pad> (thus not shown), 7-9 is second 15, 9-10 is word_delimiter_token, # 10-11 is <pad> (thus not shown), 11-12 is third 15, 12-15 is 8, 15-16 is word_delimiter_token, 16-17 is 98 self.assertListEqual( self.get_from_offsets(outputs['''char_offsets'''] , '''start_offset''' ) , [0, 1, 4, 7, 9, 11, 12, 15, 16] ) self.assertListEqual( self.get_from_offsets(outputs['''char_offsets'''] , '''end_offset''' ) , [1, 4, 6, 9, 10, 12, 15, 16, 17] ) def _snake_case ( self : Any ) -> List[Any]: '''simple docstring''' A: Optional[int] = self.get_tokenizer(word_delimiter_token='''|''' ) def check_list_tuples_equal(SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any] ): self.assertTrue(isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) self.assertTrue(isinstance(outputs_list[0] , SCREAMING_SNAKE_CASE_ ) ) # transform list to ModelOutput A: Dict = WavaVecaPhonemeCTCTokenizerOutput( {k: [d[k] for d in outputs_list] for k in outputs_list[0]} ) self.assertListEqual(outputs_batch['''text'''] , outputs_batch_a['''text'''] ) def recursive_check(SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[str] ): if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): [recursive_check(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for la, la in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )] self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if "char_offsets" in outputs_batch: recursive_check(outputs_batch['''char_offsets'''] , outputs_batch_a['''char_offsets'''] ) # fmt: off A: int = [ [11, 5, 15, tokenizer.pad_token_id, 15, 4, 8, 98, 32, 32, 32, 32, 4, 33, tokenizer.word_delimiter_token_id, 32, 32, 33, 34, 34], [24, 22, 5, tokenizer.word_delimiter_token_id, tokenizer.word_delimiter_token_id, 24, 22, 22, 22, 4, 5, 77, tokenizer.pad_token_id, 22, 22, 4, 34, 34, 34, 34], ] # fmt: on # We assume that `decode` works as expected. All we will check now is # the output type is correct and the output is identical to `decode` # char A: List[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ , output_char_offsets=SCREAMING_SNAKE_CASE_ ) A: List[Any] = [tokenizer.decode(SCREAMING_SNAKE_CASE_ , output_char_offsets=SCREAMING_SNAKE_CASE_ ) for ids in sample_ids] check_list_tuples_equal(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @unittest.skip('''Wav2Vec2PhonemeTokenizer always lower cases letters to correctly map to phonemes''' ) def _snake_case ( self : int ) -> int: '''simple docstring''' pass @unittest.skip('''Wav2Vec2PhonemeTokenizer always puts spaces between phonemes''' ) def _snake_case ( self : str ) -> Any: '''simple docstring''' pass @unittest.skip('''encodes to text to ids, but decodes ids to phonemes -> not possible to have internal consistency''' ) def _snake_case ( self : List[str] ) -> List[str]: '''simple docstring''' pass @unittest.skip('''Wav2Vec2PhonemeModel has no max model length => no testing''' ) def _snake_case ( self : Dict ) -> List[Any]: '''simple docstring''' pass def _snake_case ( self : Tuple ) -> Any: '''simple docstring''' A: Any = self.get_tokenizers(do_lower_case=SCREAMING_SNAKE_CASE_ ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): A: str = tokenizer.vocab_size A: str = len(SCREAMING_SNAKE_CASE_ ) self.assertNotEqual(SCREAMING_SNAKE_CASE_ , 0 ) # We usually have added tokens from the start in tests because our vocab fixtures are # smaller than the original vocabs - let's not assert this # self.assertEqual(vocab_size, all_size) A: List[Any] = ['''aaaaa bbbbbb''', '''cccccccccdddddddd'''] A: List[Any] = tokenizer.add_tokens(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = tokenizer.vocab_size A: Union[str, Any] = len(SCREAMING_SNAKE_CASE_ ) self.assertNotEqual(SCREAMING_SNAKE_CASE_ , 0 ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , len(SCREAMING_SNAKE_CASE_ ) ) self.assertEqual(SCREAMING_SNAKE_CASE_ , all_size + len(SCREAMING_SNAKE_CASE_ ) ) A: Any = tokenizer.encode('''aaaaa bbbbbb low cccccccccdddddddd l''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertGreaterEqual(len(SCREAMING_SNAKE_CASE_ ) , 4 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) A: str = {'''eos_token''': '''>>>>|||<||<<|<<''', '''pad_token''': '''<<<<<|||>|>>>>|>'''} A: int = tokenizer.add_special_tokens(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = tokenizer.vocab_size A: Optional[Any] = len(SCREAMING_SNAKE_CASE_ ) self.assertNotEqual(SCREAMING_SNAKE_CASE_ , 0 ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , len(SCREAMING_SNAKE_CASE_ ) ) self.assertEqual(SCREAMING_SNAKE_CASE_ , all_size_a + len(SCREAMING_SNAKE_CASE_ ) ) A: int = tokenizer.encode( '''>>>>|||<||<<|<< aaaaabbbbbb low cccccccccdddddddd <<<<<|||>|>>>>|> l''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertGreaterEqual(len(SCREAMING_SNAKE_CASE_ ) , 6 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[0] , tokens[1] ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokens[-4] ) self.assertEqual(tokens[0] , tokenizer.eos_token_id ) self.assertEqual(tokens[-3] , tokenizer.pad_token_id ) @unittest.skip('''The tokenizer shouldn\'t be used to encode input IDs (except for labels), only to decode.''' ) def _snake_case ( self : List[Any] ) -> Optional[Any]: '''simple docstring''' pass @unittest.skip('''The tokenizer shouldn\'t be used to encode input IDs (except for labels), only to decode.''' ) def _snake_case ( self : Tuple ) -> Optional[Any]: '''simple docstring''' pass def _snake_case ( self : str ) -> Tuple: '''simple docstring''' A: List[Any] = self.get_tokenizers(fast=SCREAMING_SNAKE_CASE_ , do_lower_case=SCREAMING_SNAKE_CASE_ ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): A: Union[str, Any] = ['''ð''', '''ɪ''', '''s''', '''ɪ''', '''z''', '''ɐ''', '''t''', '''ɛ''', '''k''', '''s''', '''t'''] A: Union[str, Any] = tokenizer.convert_tokens_to_string(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(output['''text'''] , SCREAMING_SNAKE_CASE_ )
319
1
'''simple docstring''' import argparse import os import torch from transformers.utils import WEIGHTS_NAME UpperCamelCase = ['''small''', '''medium''', '''large'''] UpperCamelCase = '''lm_head.decoder.weight''' UpperCamelCase = '''lm_head.weight''' def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[int]: A: Tuple = torch.load(__lowercase ) A: str = d.pop(__lowercase ) os.makedirs(__lowercase , exist_ok=__lowercase ) torch.save(__lowercase , os.path.join(__lowercase , __lowercase ) ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument('''--dialogpt_path''', default='''.''', type=str) UpperCamelCase = parser.parse_args() for MODEL in DIALOGPT_MODELS: UpperCamelCase = os.path.join(args.dialogpt_path, f'{MODEL}_ft.pkl') UpperCamelCase = f'./DialoGPT-{MODEL}' convert_dialogpt_checkpoint( checkpoint_path, pytorch_dump_folder_path, )
319
'''simple docstring''' import warnings from ...utils import logging from .image_processing_beit import BeitImageProcessor UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> None: '''simple docstring''' warnings.warn( '''The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use BeitImageProcessor instead.''' , SCREAMING_SNAKE_CASE_ , ) super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ )
319
1
'''simple docstring''' from manim import * class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' A: Union[str, Any] = Rectangle(height=0.5 , width=0.5 ) A: str = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) A: Any = Rectangle(height=0.25 , width=0.25 ) A: Optional[Any] = [mem.copy() for i in range(6 )] A: str = [mem.copy() for i in range(6 )] A: Optional[int] = VGroup(*SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ , buff=0 ) A: Tuple = VGroup(*SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ , buff=0 ) A: List[str] = VGroup(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ , buff=0 ) A: Any = Text('''CPU''' , font_size=24 ) A: List[Any] = Group(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ , buff=0.5 , aligned_edge=SCREAMING_SNAKE_CASE_ ) cpu.move_to([-2.5, -0.5, 0] ) self.add(SCREAMING_SNAKE_CASE_ ) A: Tuple = [mem.copy() for i in range(4 )] A: Union[str, Any] = VGroup(*SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ , buff=0 ) A: List[str] = Text('''GPU''' , font_size=24 ) A: List[Any] = Group(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ , buff=0.5 , aligned_edge=SCREAMING_SNAKE_CASE_ ) gpu.move_to([-1, -1, 0] ) self.add(SCREAMING_SNAKE_CASE_ ) A: Any = [mem.copy() for i in range(6 )] A: Optional[int] = VGroup(*SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ , buff=0 ) A: str = Text('''Model''' , font_size=24 ) A: Optional[Any] = Group(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ , buff=0.5 , aligned_edge=SCREAMING_SNAKE_CASE_ ) model.move_to([3, -1.0, 0] ) self.add(SCREAMING_SNAKE_CASE_ ) A: List[str] = [] A: List[Any] = [] for i, rect in enumerate(SCREAMING_SNAKE_CASE_ ): A: Dict = fill.copy().set_fill(SCREAMING_SNAKE_CASE_ , opacity=0.8 ) target.move_to(SCREAMING_SNAKE_CASE_ ) model_arr.append(SCREAMING_SNAKE_CASE_ ) A: str = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0.0 ).set_fill(SCREAMING_SNAKE_CASE_ , opacity=0.8 ) cpu_target.move_to(cpu_left_col_base[i] ) model_cpu_arr.append(SCREAMING_SNAKE_CASE_ ) self.add(*SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ ) A: Tuple = [meta_mem.copy() for i in range(6 )] A: Optional[Any] = [meta_mem.copy() for i in range(6 )] A: List[str] = VGroup(*SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ , buff=0 ) A: Optional[Any] = VGroup(*SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ , buff=0 ) A: Dict = VGroup(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ , buff=0 ) A: int = Text('''Disk''' , font_size=24 ) A: Union[str, Any] = Group(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ , buff=0.5 , aligned_edge=SCREAMING_SNAKE_CASE_ ) disk.move_to([-4, -1.25, 0] ) self.add(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Any = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) A: str = MarkupText( f"""<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model""" , font_size=18 , ) key_text.move_to([-5, 2.4, 0] ) self.add(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = MarkupText( f"""<span fgcolor='{BLUE}'>●</span> Checkpoint""" , font_size=18 , ) blue_text.next_to(SCREAMING_SNAKE_CASE_ , DOWN * 2.4 , aligned_edge=key_text.get_left() ) self.add(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = MarkupText( f"""Now watch as an input is passed through the model\nand how the memory is utilized and handled.""" , font_size=24 , ) step_a.move_to([2, 2, 0] ) self.play(Write(SCREAMING_SNAKE_CASE_ ) ) A: Any = Square(0.3 ) input.set_fill(SCREAMING_SNAKE_CASE_ , opacity=1.0 ) input.set_stroke(width=0.0 ) input.next_to(model_base[0] , SCREAMING_SNAKE_CASE_ , buff=0.5 ) self.play(Write(SCREAMING_SNAKE_CASE_ ) ) input.generate_target() input.target.next_to(model_arr[0] , direction=SCREAMING_SNAKE_CASE_ , buff=0.02 ) self.play(MoveToTarget(SCREAMING_SNAKE_CASE_ ) ) self.play(FadeOut(SCREAMING_SNAKE_CASE_ ) ) A: List[str] = Arrow(start=SCREAMING_SNAKE_CASE_ , end=SCREAMING_SNAKE_CASE_ , color=SCREAMING_SNAKE_CASE_ , buff=0.5 ) a.next_to(model_arr[0].get_left() , SCREAMING_SNAKE_CASE_ , buff=0.2 ) model_cpu_arr[0].generate_target() model_cpu_arr[0].target.move_to(gpu_rect[0] ) A: List[str] = MarkupText( f"""As the input reaches a layer, the hook triggers\nand weights are moved from the CPU\nto the GPU and back.""" , font_size=24 , ) step_a.move_to([2, 2, 0] ) self.play(Write(SCREAMING_SNAKE_CASE_ , run_time=3 ) ) A: Union[str, Any] = {'''run_time''': 1, '''fade_in''': True, '''fade_out''': True, '''buff''': 0.02} self.play( Write(SCREAMING_SNAKE_CASE_ ) , Circumscribe(model_arr[0] , color=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) , Circumscribe(model_cpu_arr[0] , color=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) , Circumscribe(gpu_rect[0] , color=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) , ) self.play(MoveToTarget(model_cpu_arr[0] ) ) A: List[str] = a.copy() for i in range(6 ): a_c.next_to(model_arr[i].get_right() + 0.02 , SCREAMING_SNAKE_CASE_ , buff=0.2 ) input.generate_target() input.target.move_to(model_arr[i].get_right() + 0.02 ) A: str = AnimationGroup( FadeOut(SCREAMING_SNAKE_CASE_ , run_time=0.5 ) , MoveToTarget(SCREAMING_SNAKE_CASE_ , run_time=0.5 ) , FadeIn(SCREAMING_SNAKE_CASE_ , run_time=0.5 ) , lag_ratio=0.2 ) self.play(SCREAMING_SNAKE_CASE_ ) model_cpu_arr[i].generate_target() model_cpu_arr[i].target.move_to(cpu_left_col_base[i] ) if i < 5: model_cpu_arr[i + 1].generate_target() model_cpu_arr[i + 1].target.move_to(gpu_rect[0] ) if i >= 1: A: List[Any] = 0.7 self.play( Circumscribe(model_arr[i] , **SCREAMING_SNAKE_CASE_ ) , Circumscribe(cpu_left_col_base[i] , **SCREAMING_SNAKE_CASE_ ) , Circumscribe(cpu_left_col_base[i + 1] , color=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) , Circumscribe(gpu_rect[0] , color=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) , Circumscribe(model_arr[i + 1] , color=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) , ) if i < 1: self.play( MoveToTarget(model_cpu_arr[i] ) , MoveToTarget(model_cpu_arr[i + 1] ) , ) else: self.play( MoveToTarget(model_cpu_arr[i] , run_time=0.7 ) , MoveToTarget(model_cpu_arr[i + 1] , run_time=0.7 ) , ) else: model_cpu_arr[i].generate_target() model_cpu_arr[i].target.move_to(cpu_left_col_base[-1] ) input.generate_target() input.target.next_to(model_arr[-1].get_right() , RIGHT + 0.02 , buff=0.2 ) self.play( Circumscribe(model_arr[-1] , color=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) , Circumscribe(cpu_left_col_base[-1] , color=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) , Circumscribe(gpu_rect[0] , color=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) , ) self.play(MoveToTarget(model_cpu_arr[i] ) ) A: Optional[Any] = a_c A: Optional[int] = a_c.copy() input.generate_target() input.target.next_to(model_base[-1] , RIGHT + 0.02 , buff=0.5 ) self.play( FadeOut(SCREAMING_SNAKE_CASE_ ) , FadeOut(SCREAMING_SNAKE_CASE_ , run_time=0.5 ) , ) A: Optional[Any] = MarkupText(f"""Inference on a model too large for GPU memory\nis successfully completed.""" , font_size=24 ) step_a.move_to([2, 2, 0] ) self.play(Write(SCREAMING_SNAKE_CASE_ , run_time=3 ) , MoveToTarget(SCREAMING_SNAKE_CASE_ ) ) self.wait()
319
'''simple docstring''' import os import pytest from transformers.dynamic_module_utils import get_imports UpperCamelCase = ''' import os ''' UpperCamelCase = ''' def foo(): import os return False ''' UpperCamelCase = ''' def foo(): def bar(): if True: import os return False return bar() ''' UpperCamelCase = ''' import os try: import bar except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os def foo(): try: import bar except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except (ImportError, AttributeError): raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except ImportError as e: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar import baz except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar import baz except ImportError: x = 1 raise ValueError() ''' UpperCamelCase = [ TOP_LEVEL_IMPORT, IMPORT_IN_FUNCTION, DEEPLY_NESTED_IMPORT, TOP_LEVEL_TRY_IMPORT, GENERIC_EXCEPT_IMPORT, MULTILINE_TRY_IMPORT, MULTILINE_BOTH_IMPORT, MULTIPLE_EXCEPTS_IMPORT, EXCEPT_AS_IMPORT, TRY_IMPORT_IN_FUNCTION, ] @pytest.mark.parametrize('''case''' , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Dict: A: Tuple = os.path.join(__lowercase , '''test_file.py''' ) with open(__lowercase , '''w''' ) as _tmp_file: _tmp_file.write(__lowercase ) A: List[Any] = get_imports(__lowercase ) assert parsed_imports == ["os"]
319
1
'''simple docstring''' from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean UpperCamelCase = 0 UpperCamelCase = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] UpperCamelCase = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right UpperCamelCase = tuple[int, int] class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Node | None , ) -> None: '''simple docstring''' A: Union[str, Any] = pos_x A: Tuple = pos_y A: Optional[Any] = (pos_y, pos_x) A: List[Any] = goal_x A: Union[str, Any] = goal_y A: Tuple = g_cost A: Optional[int] = parent A: int = self.calculate_heuristic() A: str = self.g_cost + self.h_cost def _snake_case ( self : Tuple ) -> float: '''simple docstring''' A: Optional[Any] = self.pos_x - self.goal_x A: Optional[Any] = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(SCREAMING_SNAKE_CASE_ ) + abs(SCREAMING_SNAKE_CASE_ ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self : Any , SCREAMING_SNAKE_CASE_ : Node ) -> bool: '''simple docstring''' return self.f_cost < other.f_cost class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : TPosition , SCREAMING_SNAKE_CASE_ : TPosition ) -> Optional[int]: '''simple docstring''' A: Optional[int] = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , SCREAMING_SNAKE_CASE_ ) A: List[Any] = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_99_99 , SCREAMING_SNAKE_CASE_ ) A: str = [self.start] A: list[Node] = [] A: str = False def _snake_case ( self : Tuple ) -> list[TPosition]: '''simple docstring''' while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() A: List[Any] = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(SCREAMING_SNAKE_CASE_ ) self.closed_nodes.append(SCREAMING_SNAKE_CASE_ ) A: int = self.get_successors(SCREAMING_SNAKE_CASE_ ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(SCREAMING_SNAKE_CASE_ ) else: # retrieve the best current path A: int = self.open_nodes.pop(self.open_nodes.index(SCREAMING_SNAKE_CASE_ ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(SCREAMING_SNAKE_CASE_ ) else: self.open_nodes.append(SCREAMING_SNAKE_CASE_ ) return [self.start.pos] def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : Node ) -> list[Node]: '''simple docstring''' A: int = [] for action in delta: A: Union[str, Any] = parent.pos_x + action[1] A: Tuple = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(SCREAMING_SNAKE_CASE_ ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , SCREAMING_SNAKE_CASE_ , ) ) return successors def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Node | None ) -> list[TPosition]: '''simple docstring''' A: Any = node A: List[Any] = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) A: int = current_node.parent path.reverse() return path class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : TPosition , SCREAMING_SNAKE_CASE_ : TPosition ) -> None: '''simple docstring''' A: List[str] = AStar(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: int = AStar(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: List[Any] = False def _snake_case ( self : Dict ) -> list[TPosition]: '''simple docstring''' while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() A: Tuple = self.fwd_astar.open_nodes.pop(0 ) A: List[str] = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.fwd_astar.closed_nodes.append(SCREAMING_SNAKE_CASE_ ) self.bwd_astar.closed_nodes.append(SCREAMING_SNAKE_CASE_ ) A: str = current_bwd_node A: Tuple = current_fwd_node A: Union[str, Any] = { self.fwd_astar: self.fwd_astar.get_successors(SCREAMING_SNAKE_CASE_ ), self.bwd_astar: self.bwd_astar.get_successors(SCREAMING_SNAKE_CASE_ ), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(SCREAMING_SNAKE_CASE_ ) else: # retrieve the best current path A: Any = astar.open_nodes.pop( astar.open_nodes.index(SCREAMING_SNAKE_CASE_ ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(SCREAMING_SNAKE_CASE_ ) else: astar.open_nodes.append(SCREAMING_SNAKE_CASE_ ) return [self.fwd_astar.start.pos] def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : Node , SCREAMING_SNAKE_CASE_ : Node ) -> list[TPosition]: '''simple docstring''' A: Union[str, Any] = self.fwd_astar.retrace_path(SCREAMING_SNAKE_CASE_ ) A: Dict = self.bwd_astar.retrace_path(SCREAMING_SNAKE_CASE_ ) bwd_path.pop() bwd_path.reverse() A: Tuple = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] UpperCamelCase = (0, 0) UpperCamelCase = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) UpperCamelCase = time.time() UpperCamelCase = AStar(init, goal) UpperCamelCase = a_star.search() UpperCamelCase = time.time() - start_time print(f'AStar execution time = {end_time:f} seconds') UpperCamelCase = time.time() UpperCamelCase = BidirectionalAStar(init, goal) UpperCamelCase = time.time() - bd_start_time print(f'BidirectionalAStar execution time = {bd_end_time:f} seconds')
319
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( BertTokenizer, ViltConfig, ViltForImageAndTextRetrieval, ViltForImagesAndTextClassification, ViltForMaskedLM, ViltForQuestionAnswering, ViltImageProcessor, ViltProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=False , __lowercase=False , __lowercase=False ) -> Optional[Any]: A: str = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""transformer.blocks.{i}.norm1.weight""", F"""vilt.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.norm1.bias""", F"""vilt.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append( (F"""transformer.blocks.{i}.attn.proj.weight""", F"""vilt.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append( (F"""transformer.blocks.{i}.attn.proj.bias""", F"""vilt.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""transformer.blocks.{i}.norm2.weight""", F"""vilt.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.norm2.bias""", F"""vilt.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append( (F"""transformer.blocks.{i}.mlp.fc1.weight""", F"""vilt.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc1.bias""", F"""vilt.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc2.weight""", F"""vilt.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc2.bias""", F"""vilt.encoder.layer.{i}.output.dense.bias""") ) # embeddings rename_keys.extend( [ # text embeddings ('''text_embeddings.word_embeddings.weight''', '''vilt.embeddings.text_embeddings.word_embeddings.weight'''), ( '''text_embeddings.position_embeddings.weight''', '''vilt.embeddings.text_embeddings.position_embeddings.weight''', ), ('''text_embeddings.position_ids''', '''vilt.embeddings.text_embeddings.position_ids'''), ( '''text_embeddings.token_type_embeddings.weight''', '''vilt.embeddings.text_embeddings.token_type_embeddings.weight''', ), ('''text_embeddings.LayerNorm.weight''', '''vilt.embeddings.text_embeddings.LayerNorm.weight'''), ('''text_embeddings.LayerNorm.bias''', '''vilt.embeddings.text_embeddings.LayerNorm.bias'''), # patch embeddings ('''transformer.cls_token''', '''vilt.embeddings.cls_token'''), ('''transformer.patch_embed.proj.weight''', '''vilt.embeddings.patch_embeddings.projection.weight'''), ('''transformer.patch_embed.proj.bias''', '''vilt.embeddings.patch_embeddings.projection.bias'''), ('''transformer.pos_embed''', '''vilt.embeddings.position_embeddings'''), # token type embeddings ('''token_type_embeddings.weight''', '''vilt.embeddings.token_type_embeddings.weight'''), ] ) # final layernorm + pooler rename_keys.extend( [ ('''transformer.norm.weight''', '''vilt.layernorm.weight'''), ('''transformer.norm.bias''', '''vilt.layernorm.bias'''), ('''pooler.dense.weight''', '''vilt.pooler.dense.weight'''), ('''pooler.dense.bias''', '''vilt.pooler.dense.bias'''), ] ) # classifier head(s) if vqa_model: # classification head rename_keys.extend( [ ('''vqa_classifier.0.weight''', '''classifier.0.weight'''), ('''vqa_classifier.0.bias''', '''classifier.0.bias'''), ('''vqa_classifier.1.weight''', '''classifier.1.weight'''), ('''vqa_classifier.1.bias''', '''classifier.1.bias'''), ('''vqa_classifier.3.weight''', '''classifier.3.weight'''), ('''vqa_classifier.3.bias''', '''classifier.3.bias'''), ] ) elif nlvr_model: # classification head rename_keys.extend( [ ('''nlvr2_classifier.0.weight''', '''classifier.0.weight'''), ('''nlvr2_classifier.0.bias''', '''classifier.0.bias'''), ('''nlvr2_classifier.1.weight''', '''classifier.1.weight'''), ('''nlvr2_classifier.1.bias''', '''classifier.1.bias'''), ('''nlvr2_classifier.3.weight''', '''classifier.3.weight'''), ('''nlvr2_classifier.3.bias''', '''classifier.3.bias'''), ] ) else: pass return rename_keys def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Any: for i in range(config.num_hidden_layers ): A: Tuple = '''vilt.''' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A: List[str] = state_dict.pop(F"""transformer.blocks.{i}.attn.qkv.weight""" ) A: Optional[Any] = state_dict.pop(F"""transformer.blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict A: Dict = in_proj_weight[ : config.hidden_size, : ] A: int = in_proj_bias[: config.hidden_size] A: Any = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A: int = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A: Optional[int] = in_proj_weight[ -config.hidden_size :, : ] A: Optional[Any] = in_proj_bias[-config.hidden_size :] def SCREAMING_SNAKE_CASE( __lowercase ) -> int: A: Optional[int] = ['''head.weight''', '''head.bias'''] for k in ignore_keys: state_dict.pop(__lowercase , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: A: List[Any] = dct.pop(__lowercase ) A: int = val @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str: A: Optional[Any] = ViltConfig(image_size=3_8_4 , patch_size=3_2 , tie_word_embeddings=__lowercase ) A: Tuple = False A: str = False A: List[Any] = False A: Optional[int] = False if "vqa" in checkpoint_url: A: Union[str, Any] = True A: Union[str, Any] = 3_1_2_9 A: List[Any] = '''huggingface/label-files''' A: Any = '''vqa2-id2label.json''' A: Optional[Any] = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: Union[str, Any] = {int(__lowercase ): v for k, v in idalabel.items()} A: Any = idalabel A: Optional[Any] = {v: k for k, v in idalabel.items()} A: List[str] = ViltForQuestionAnswering(__lowercase ) elif "nlvr" in checkpoint_url: A: Dict = True A: str = 2 A: Union[str, Any] = {0: '''False''', 1: '''True'''} A: Any = {v: k for k, v in config.idalabel.items()} A: Optional[Any] = 3 A: Any = ViltForImagesAndTextClassification(__lowercase ) elif "irtr" in checkpoint_url: A: Tuple = True A: Optional[Any] = ViltForImageAndTextRetrieval(__lowercase ) elif "mlm_itm" in checkpoint_url: A: Tuple = True A: Optional[int] = ViltForMaskedLM(__lowercase ) else: raise ValueError('''Unknown model type''' ) # load state_dict of original model, remove and rename some keys A: int = torch.hub.load_state_dict_from_url(__lowercase , map_location='''cpu''' )['''state_dict'''] A: List[str] = create_rename_keys(__lowercase , __lowercase , __lowercase , __lowercase ) for src, dest in rename_keys: rename_key(__lowercase , __lowercase , __lowercase ) read_in_q_k_v(__lowercase , __lowercase ) if mlm_model or irtr_model: A: str = ['''itm_score.fc.weight''', '''itm_score.fc.bias'''] for k in ignore_keys: state_dict.pop(__lowercase , __lowercase ) # load state dict into HuggingFace model model.eval() if mlm_model: A , A: Union[str, Any] = model.load_state_dict(__lowercase , strict=__lowercase ) assert missing_keys == ["mlm_score.decoder.bias"] else: model.load_state_dict(__lowercase ) # Define processor A: Optional[Any] = ViltImageProcessor(size=3_8_4 ) A: Dict = BertTokenizer.from_pretrained('''bert-base-uncased''' ) A: Optional[int] = ViltProcessor(__lowercase , __lowercase ) # Forward pass on example inputs (image + text) if nlvr_model: A: str = Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg''' , stream=__lowercase ).raw ) A: List[str] = Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg''' , stream=__lowercase ).raw ) A: Any = ( '''The left image contains twice the number of dogs as the right image, and at least two dogs in total are''' ''' standing.''' ) A: List[Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: List[Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: List[str] = model( input_ids=encoding_a.input_ids , pixel_values=encoding_a.pixel_values , pixel_values_a=encoding_a.pixel_values , ) else: A: Any = Image.open(requests.get('''http://images.cocodataset.org/val2017/000000039769.jpg''' , stream=__lowercase ).raw ) if mlm_model: A: Optional[int] = '''a bunch of [MASK] laying on a [MASK].''' else: A: Optional[int] = '''How many cats are there?''' A: Union[str, Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: Any = model(**__lowercase ) # Verify outputs if mlm_model: A: Any = torch.Size([1, 1_1, 3_0_5_2_2] ) A: Tuple = torch.tensor([-1_2.5_0_6_1, -1_2.5_1_2_3, -1_2.5_1_7_4] ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , __lowercase , atol=1E-4 ) # verify masked token prediction equals "cats" A: List[str] = outputs.logits[0, 4, :].argmax(-1 ).item() assert tokenizer.decode([predicted_id] ) == "cats" elif vqa_model: A: Any = torch.Size([1, 3_1_2_9] ) A: Optional[int] = torch.tensor([-1_5.9_4_9_5, -1_8.1_4_7_2, -1_0.3_0_4_1] ) assert torch.allclose(outputs.logits[0, :3] , __lowercase , atol=1E-4 ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , __lowercase , atol=1E-4 ) # verify vqa prediction equals "2" A: Dict = outputs.logits.argmax(-1 ).item() assert model.config.idalabel[predicted_idx] == "2" elif nlvr_model: A: Union[str, Any] = torch.Size([1, 2] ) A: Optional[Any] = torch.tensor([-2.8_7_2_1, 2.1_2_9_1] ) assert torch.allclose(outputs.logits[0, :3] , __lowercase , atol=1E-4 ) assert outputs.logits.shape == expected_shape Path(__lowercase ).mkdir(exist_ok=__lowercase ) print(F"""Saving model and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(__lowercase ) processor.save_pretrained(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://github.com/dandelin/ViLT/releases/download/200k/vilt_200k_mlm_itm.ckpt''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) UpperCamelCase = parser.parse_args() convert_vilt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
319
1
'''simple docstring''' from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ( ImageTextPipelineOutput, UniDiffuserPipeline, ) else: from .modeling_text_decoder import UniDiffuserTextDecoder from .modeling_uvit import UniDiffuserModel, UTransformeraDModel from .pipeline_unidiffuser import ImageTextPipelineOutput, UniDiffuserPipeline
319
'''simple docstring''' import argparse import json import os import numpy as np import PIL import requests import tensorflow.keras.applications.efficientnet as efficientnet import torch from huggingface_hub import hf_hub_download from PIL import Image from tensorflow.keras.preprocessing import image from transformers import ( EfficientNetConfig, EfficientNetForImageClassification, EfficientNetImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''b0''': efficientnet.EfficientNetBa, '''b1''': efficientnet.EfficientNetBa, '''b2''': efficientnet.EfficientNetBa, '''b3''': efficientnet.EfficientNetBa, '''b4''': efficientnet.EfficientNetBa, '''b5''': efficientnet.EfficientNetBa, '''b6''': efficientnet.EfficientNetBa, '''b7''': efficientnet.EfficientNetBa, } UpperCamelCase = { '''b0''': { '''hidden_dim''': 1280, '''width_coef''': 1.0, '''depth_coef''': 1.0, '''image_size''': 224, '''dropout_rate''': 0.2, '''dw_padding''': [], }, '''b1''': { '''hidden_dim''': 1280, '''width_coef''': 1.0, '''depth_coef''': 1.1, '''image_size''': 240, '''dropout_rate''': 0.2, '''dw_padding''': [16], }, '''b2''': { '''hidden_dim''': 1408, '''width_coef''': 1.1, '''depth_coef''': 1.2, '''image_size''': 260, '''dropout_rate''': 0.3, '''dw_padding''': [5, 8, 16], }, '''b3''': { '''hidden_dim''': 1536, '''width_coef''': 1.2, '''depth_coef''': 1.4, '''image_size''': 300, '''dropout_rate''': 0.3, '''dw_padding''': [5, 18], }, '''b4''': { '''hidden_dim''': 1792, '''width_coef''': 1.4, '''depth_coef''': 1.8, '''image_size''': 380, '''dropout_rate''': 0.4, '''dw_padding''': [6], }, '''b5''': { '''hidden_dim''': 2048, '''width_coef''': 1.6, '''depth_coef''': 2.2, '''image_size''': 456, '''dropout_rate''': 0.4, '''dw_padding''': [13, 27], }, '''b6''': { '''hidden_dim''': 2304, '''width_coef''': 1.8, '''depth_coef''': 2.6, '''image_size''': 528, '''dropout_rate''': 0.5, '''dw_padding''': [31], }, '''b7''': { '''hidden_dim''': 2560, '''width_coef''': 2.0, '''depth_coef''': 3.1, '''image_size''': 600, '''dropout_rate''': 0.5, '''dw_padding''': [18], }, } def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: A: Tuple = EfficientNetConfig() A: Optional[int] = CONFIG_MAP[model_name]['''hidden_dim'''] A: Optional[int] = CONFIG_MAP[model_name]['''width_coef'''] A: str = CONFIG_MAP[model_name]['''depth_coef'''] A: Dict = CONFIG_MAP[model_name]['''image_size'''] A: str = CONFIG_MAP[model_name]['''dropout_rate'''] A: Optional[Any] = CONFIG_MAP[model_name]['''dw_padding'''] A: Optional[Any] = '''huggingface/label-files''' A: List[str] = '''imagenet-1k-id2label.json''' A: Dict = 1_0_0_0 A: Any = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: Tuple = {int(__lowercase ): v for k, v in idalabel.items()} A: int = idalabel A: Tuple = {v: k for k, v in idalabel.items()} return config def SCREAMING_SNAKE_CASE( ) -> Any: A: Optional[Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' A: Union[str, Any] = Image.open(requests.get(__lowercase , stream=__lowercase ).raw ) return im def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: A: List[str] = CONFIG_MAP[model_name]['''image_size'''] A: List[Any] = EfficientNetImageProcessor( size={'''height''': size, '''width''': size} , image_mean=[0.4_8_5, 0.4_5_6, 0.4_0_6] , image_std=[0.4_7_8_5_3_9_4_4, 0.4_7_3_2_8_6_4, 0.4_7_4_3_4_1_6_3] , do_center_crop=__lowercase , ) return preprocessor def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: A: List[str] = [v.split('''_''' )[0].split('''block''' )[1] for v in original_param_names if v.startswith('''block''' )] A: List[str] = sorted(set(__lowercase ) ) A: Dict = len(__lowercase ) A: List[str] = {b: str(__lowercase ) for b, i in zip(__lowercase , range(__lowercase ) )} A: Optional[int] = [] rename_keys.append(('''stem_conv/kernel:0''', '''embeddings.convolution.weight''') ) rename_keys.append(('''stem_bn/gamma:0''', '''embeddings.batchnorm.weight''') ) rename_keys.append(('''stem_bn/beta:0''', '''embeddings.batchnorm.bias''') ) rename_keys.append(('''stem_bn/moving_mean:0''', '''embeddings.batchnorm.running_mean''') ) rename_keys.append(('''stem_bn/moving_variance:0''', '''embeddings.batchnorm.running_var''') ) for b in block_names: A: int = block_name_mapping[b] rename_keys.append((F"""block{b}_expand_conv/kernel:0""", F"""encoder.blocks.{hf_b}.expansion.expand_conv.weight""") ) rename_keys.append((F"""block{b}_expand_bn/gamma:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.weight""") ) rename_keys.append((F"""block{b}_expand_bn/beta:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.bias""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_mean""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_var""") ) rename_keys.append( (F"""block{b}_dwconv/depthwise_kernel:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight""") ) rename_keys.append((F"""block{b}_bn/gamma:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight""") ) rename_keys.append((F"""block{b}_bn/beta:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias""") ) rename_keys.append( (F"""block{b}_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean""") ) rename_keys.append( (F"""block{b}_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var""") ) rename_keys.append((F"""block{b}_se_reduce/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.weight""") ) rename_keys.append((F"""block{b}_se_reduce/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.bias""") ) rename_keys.append((F"""block{b}_se_expand/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.weight""") ) rename_keys.append((F"""block{b}_se_expand/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.bias""") ) rename_keys.append( (F"""block{b}_project_conv/kernel:0""", F"""encoder.blocks.{hf_b}.projection.project_conv.weight""") ) rename_keys.append((F"""block{b}_project_bn/gamma:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.weight""") ) rename_keys.append((F"""block{b}_project_bn/beta:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.bias""") ) rename_keys.append( (F"""block{b}_project_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_mean""") ) rename_keys.append( (F"""block{b}_project_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_var""") ) rename_keys.append(('''top_conv/kernel:0''', '''encoder.top_conv.weight''') ) rename_keys.append(('''top_bn/gamma:0''', '''encoder.top_bn.weight''') ) rename_keys.append(('''top_bn/beta:0''', '''encoder.top_bn.bias''') ) rename_keys.append(('''top_bn/moving_mean:0''', '''encoder.top_bn.running_mean''') ) rename_keys.append(('''top_bn/moving_variance:0''', '''encoder.top_bn.running_var''') ) A: Union[str, Any] = {} for item in rename_keys: if item[0] in original_param_names: A: str = '''efficientnet.''' + item[1] A: int = '''classifier.weight''' A: Tuple = '''classifier.bias''' return key_mapping def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Tuple: for key, value in tf_params.items(): if "normalization" in key: continue A: Union[str, Any] = key_mapping[key] if "_conv" in key and "kernel" in key: A: List[str] = torch.from_numpy(__lowercase ).permute(3 , 2 , 0 , 1 ) elif "depthwise_kernel" in key: A: List[Any] = torch.from_numpy(__lowercase ).permute(2 , 3 , 0 , 1 ) elif "kernel" in key: A: Optional[Any] = torch.from_numpy(np.transpose(__lowercase ) ) else: A: Any = torch.from_numpy(__lowercase ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(__lowercase ) @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase ) -> Tuple: A: Optional[int] = model_classes[model_name]( include_top=__lowercase , weights='''imagenet''' , input_tensor=__lowercase , input_shape=__lowercase , pooling=__lowercase , classes=1_0_0_0 , classifier_activation='''softmax''' , ) A: List[str] = original_model.trainable_variables A: Optional[Any] = original_model.non_trainable_variables A: Union[str, Any] = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: A: int = param.numpy() A: Tuple = list(tf_params.keys() ) # Load HuggingFace model A: Dict = get_efficientnet_config(__lowercase ) A: Union[str, Any] = EfficientNetForImageClassification(__lowercase ).eval() A: Dict = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print('''Converting parameters...''' ) A: int = rename_keys(__lowercase ) replace_params(__lowercase , __lowercase , __lowercase ) # Initialize preprocessor and preprocess input image A: List[Any] = convert_image_processor(__lowercase ) A: Optional[Any] = preprocessor(images=prepare_img() , return_tensors='''pt''' ) # HF model inference hf_model.eval() with torch.no_grad(): A: str = hf_model(**__lowercase ) A: List[Any] = outputs.logits.detach().numpy() # Original model inference A: Any = False A: List[Any] = CONFIG_MAP[model_name]['''image_size'''] A: List[Any] = prepare_img().resize((image_size, image_size) , resample=PIL.Image.NEAREST ) A: str = image.img_to_array(__lowercase ) A: Dict = np.expand_dims(__lowercase , axis=0 ) A: Any = original_model.predict(__lowercase ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(__lowercase , __lowercase , atol=1E-3 ), "The predicted logits are not the same." print('''Model outputs match!''' ) if save_model: # Create folder to save model if not os.path.isdir(__lowercase ): os.mkdir(__lowercase ) # Save converted model and image processor hf_model.save_pretrained(__lowercase ) preprocessor.save_pretrained(__lowercase ) if push_to_hub: # Push model and image processor to hub print(F"""Pushing converted {model_name} to the hub...""" ) A: int = F"""efficientnet-{model_name}""" preprocessor.push_to_hub(__lowercase ) hf_model.push_to_hub(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''b0''', type=str, help='''Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''hf_model''', type=str, help='''Path to the output PyTorch model directory.''', ) parser.add_argument('''--save_model''', action='''store_true''', help='''Save model to local''') parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Push model and image processor to the hub''') UpperCamelCase = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
319
1
'''simple docstring''' import baseaa import io import json import os from copy import deepcopy from ..optimizer import AcceleratedOptimizer from ..scheduler import AcceleratedScheduler class lowerCAmelCase_ : '''simple docstring''' def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : List[Any] ) -> str: '''simple docstring''' if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): # Don't modify user's data should they want to reuse it (e.g. in tests), because once we # modified it, it will not be accepted here again, since `auto` values would have been overridden A: Dict = deepcopy(SCREAMING_SNAKE_CASE_ ) elif os.path.exists(SCREAMING_SNAKE_CASE_ ): with io.open(SCREAMING_SNAKE_CASE_ , '''r''' , encoding='''utf-8''' ) as f: A: Dict = json.load(SCREAMING_SNAKE_CASE_ ) else: try: A: Optional[int] = baseaa.urlsafe_baadecode(SCREAMING_SNAKE_CASE_ ).decode('''utf-8''' ) A: Any = json.loads(SCREAMING_SNAKE_CASE_ ) except (UnicodeDecodeError, AttributeError, ValueError): raise ValueError( f"""Expected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: {config_file_or_dict}""" ) A: Union[str, Any] = config self.set_stage_and_offload() def _snake_case ( self : str ) -> Optional[Any]: '''simple docstring''' A: List[Any] = self.get_value('''zero_optimization.stage''' , -1 ) # offload A: str = False if self.is_zeroa() or self.is_zeroa(): A: List[str] = set(['''cpu''', '''nvme'''] ) A: Dict = set( [ self.get_value('''zero_optimization.offload_optimizer.device''' ), self.get_value('''zero_optimization.offload_param.device''' ), ] ) if len(offload_devices & offload_devices_valid ) > 0: A: Optional[Any] = True def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : Any ) -> Optional[Any]: '''simple docstring''' A: int = self.config # find the config node of interest if it exists A: Optional[Any] = ds_key_long.split('''.''' ) A: Tuple = nodes.pop() for node in nodes: A: int = config.get(SCREAMING_SNAKE_CASE_ ) if config is None: return None, ds_key return config, ds_key def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Tuple=None ) -> Optional[Any]: '''simple docstring''' A , A: Any = self.find_config_node(SCREAMING_SNAKE_CASE_ ) if config is None: return default return config.get(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Tuple=False ) -> List[str]: '''simple docstring''' A: Union[str, Any] = self.config # find the config node of interest if it exists A: List[str] = ds_key_long.split('''.''' ) for node in nodes: A: Tuple = config A: List[str] = config.get(SCREAMING_SNAKE_CASE_ ) if config is None: if must_exist: raise ValueError(f"""Can't find {ds_key_long} entry in the config: {self.config}""" ) else: return # if found remove it if parent_config is not None: parent_config.pop(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Dict , SCREAMING_SNAKE_CASE_ : Dict ) -> List[Any]: '''simple docstring''' A: Optional[int] = self.get_value(SCREAMING_SNAKE_CASE_ ) return False if value is None else bool(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Dict , SCREAMING_SNAKE_CASE_ : List[str] ) -> int: '''simple docstring''' A: Dict = self.get_value(SCREAMING_SNAKE_CASE_ ) return False if value is None else not bool(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] ) -> List[str]: '''simple docstring''' return self._stage == 2 def _snake_case ( self : str ) -> List[str]: '''simple docstring''' return self._stage == 3 def _snake_case ( self : int ) -> Tuple: '''simple docstring''' return self._offload class lowerCAmelCase_ : '''simple docstring''' def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : str ) -> Optional[Any]: '''simple docstring''' A: List[Any] = engine def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Optional[int]: '''simple docstring''' self.engine.backward(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) # Deepspeed's `engine.step` performs the following operations: # - gradient accumulation check # - gradient clipping # - optimizer step # - zero grad # - checking overflow # - lr_scheduler step (only if engine.lr_scheduler is not None) self.engine.step() # and this plugin overrides the above calls with no-ops when Accelerate runs under # Deepspeed, but allows normal functionality for non-Deepspeed cases thus enabling a simple # training loop that works transparently under many training regimes. class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : str , SCREAMING_SNAKE_CASE_ : int ) -> Optional[Any]: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ , device_placement=SCREAMING_SNAKE_CASE_ , scaler=SCREAMING_SNAKE_CASE_ ) A: Tuple = hasattr(self.optimizer , '''overflow''' ) def _snake_case ( self : Dict , SCREAMING_SNAKE_CASE_ : Optional[Any]=None ) -> List[Any]: '''simple docstring''' pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed def _snake_case ( self : List[str] ) -> Optional[Any]: '''simple docstring''' pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed @property def _snake_case ( self : List[Any] ) -> str: '''simple docstring''' if self.__has_overflow__: return self.optimizer.overflow return False class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> List[Any]: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int ) -> List[str]: '''simple docstring''' pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Tuple , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Dict=0.001 , SCREAMING_SNAKE_CASE_ : int=0 , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> List[str]: '''simple docstring''' A: List[Any] = params A: Tuple = lr A: Union[str, Any] = weight_decay A: Optional[int] = kwargs class lowerCAmelCase_ : '''simple docstring''' def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Tuple=None , SCREAMING_SNAKE_CASE_ : Dict=0 , **SCREAMING_SNAKE_CASE_ : Dict ) -> Optional[int]: '''simple docstring''' A: Any = optimizer A: str = total_num_steps A: Optional[int] = warmup_num_steps A: Optional[int] = kwargs
319
'''simple docstring''' from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase = {'''configuration_focalnet''': ['''FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FocalNetConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FocalNetForImageClassification''', '''FocalNetForMaskedImageModeling''', '''FocalNetBackbone''', '''FocalNetModel''', '''FocalNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_focalnet import FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FocalNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_focalnet import ( FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST, FocalNetBackbone, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetModel, FocalNetPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
1
'''simple docstring''' import argparse from pathlib import Path from transformers import AutoConfig, AutoTokenizer, RagConfig, RagSequenceForGeneration, RagTokenForGeneration def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase = None , __lowercase = None , __lowercase = None , ) -> Optional[int]: if config_name_or_path is None: A: Union[str, Any] = '''facebook/rag-token-base''' if model_type == '''rag_token''' else '''facebook/rag-sequence-base''' if generator_tokenizer_name_or_path is None: A: List[str] = generator_name_or_path if question_encoder_tokenizer_name_or_path is None: A: Optional[int] = question_encoder_name_or_path A: Optional[Any] = RagTokenForGeneration if model_type == '''rag_token''' else RagSequenceForGeneration # Save model. A: List[Any] = RagConfig.from_pretrained(__lowercase ) A: Optional[int] = AutoConfig.from_pretrained(__lowercase ) A: str = AutoConfig.from_pretrained(__lowercase ) A: Union[str, Any] = gen_config A: Optional[int] = question_encoder_config A: Dict = model_class.from_pretrained_question_encoder_generator( __lowercase , __lowercase , config=__lowercase ) rag_model.save_pretrained(__lowercase ) # Sanity check. model_class.from_pretrained(__lowercase ) # Save tokenizers. A: Optional[int] = AutoTokenizer.from_pretrained(__lowercase ) gen_tokenizer.save_pretrained(dest_dir / '''generator_tokenizer/''' ) A: Dict = AutoTokenizer.from_pretrained(__lowercase ) question_encoder_tokenizer.save_pretrained(dest_dir / '''question_encoder_tokenizer/''' ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument( '''--model_type''', choices=['''rag_sequence''', '''rag_token'''], required=True, type=str, help='''RAG model type: rag_sequence, rag_token''', ) parser.add_argument('''--dest''', type=str, required=True, help='''Path to the output checkpoint directory.''') parser.add_argument('''--generator_name_or_path''', type=str, required=True, help='''Generator model identifier''') parser.add_argument( '''--question_encoder_name_or_path''', type=str, required=True, help='''Question encoder model identifier''' ) parser.add_argument( '''--generator_tokenizer_name_or_path''', type=str, help='''Generator tokenizer identifier, if not specified, resolves to ``generator_name_or_path``''', ) parser.add_argument( '''--question_encoder_tokenizer_name_or_path''', type=str, help='''Question encoder tokenizer identifier, if not specified, resolves to ``question_encoder_name_or_path``''', ) parser.add_argument( '''--config_name_or_path''', type=str, help=( '''Identifier of the model config to use, if not provided, resolves to a base config for a given''' ''' ``model_type``''' ), ) UpperCamelCase = parser.parse_args() UpperCamelCase = Path(args.dest) dest_dir.mkdir(exist_ok=True) consolidate( args.model_type, args.generator_name_or_path, args.question_encoder_name_or_path, dest_dir, args.config_name_or_path, args.generator_tokenizer_name_or_path, args.question_encoder_tokenizer_name_or_path, )
319
'''simple docstring''' from typing import Any, Dict, List, Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from ..image_utils import load_image if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_OBJECT_DETECTION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = Dict[str, Any] UpperCamelCase = List[Prediction] @add_end_docstrings(UpperCAmelCase_ ) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : List[str] ) -> int: '''simple docstring''' super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if self.framework == "tf": raise ValueError(f"""The {self.__class__} is only available in PyTorch.""" ) requires_backends(self , '''vision''' ) self.check_model_type( dict(MODEL_FOR_OBJECT_DETECTION_MAPPING.items() + MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.items() ) ) def _snake_case ( self : int , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[Any]: '''simple docstring''' A: Any = {} if "threshold" in kwargs: A: List[Any] = kwargs['''threshold'''] return {}, {}, postprocess_kwargs def __call__( self : str , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Union[Predictions, List[Prediction]]: '''simple docstring''' return super().__call__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Union[str, Any]: '''simple docstring''' A: int = load_image(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = torch.IntTensor([[image.height, image.width]] ) A: Union[str, Any] = self.image_processor(images=[image] , return_tensors='''pt''' ) if self.tokenizer is not None: A: int = self.tokenizer(text=inputs['''words'''] , boxes=inputs['''boxes'''] , return_tensors='''pt''' ) A: Any = target_size return inputs def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : str ) -> List[Any]: '''simple docstring''' A: Tuple = model_inputs.pop('''target_size''' ) A: Tuple = self.model(**SCREAMING_SNAKE_CASE_ ) A: List[str] = outputs.__class__({'''target_size''': target_size, **outputs} ) if self.tokenizer is not None: A: Dict = model_inputs['''bbox'''] return model_outputs def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : str=0.9 ) -> Union[str, Any]: '''simple docstring''' A: List[Any] = model_outputs['''target_size'''] if self.tokenizer is not None: # This is a LayoutLMForTokenClassification variant. # The OCR got the boxes and the model classified the words. A , A: Union[str, Any] = target_size[0].tolist() def unnormalize(SCREAMING_SNAKE_CASE_ : str ): return self._get_bounding_box( torch.Tensor( [ (width * bbox[0] / 10_00), (height * bbox[1] / 10_00), (width * bbox[2] / 10_00), (height * bbox[3] / 10_00), ] ) ) A , A: Dict = model_outputs['''logits'''].squeeze(0 ).softmax(dim=-1 ).max(dim=-1 ) A: List[str] = [self.model.config.idalabel[prediction] for prediction in classes.tolist()] A: List[str] = [unnormalize(SCREAMING_SNAKE_CASE_ ) for bbox in model_outputs['''bbox'''].squeeze(0 )] A: Dict = ['''score''', '''label''', '''box'''] A: Optional[int] = [dict(zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) for vals in zip(scores.tolist() , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if vals[0] > threshold] else: # This is a regular ForObjectDetectionModel A: Any = self.image_processor.post_process_object_detection(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: List[str] = raw_annotations[0] A: List[Any] = raw_annotation['''scores'''] A: List[Any] = raw_annotation['''labels'''] A: int = raw_annotation['''boxes'''] A: Any = scores.tolist() A: List[Any] = [self.model.config.idalabel[label.item()] for label in labels] A: List[Any] = [self._get_bounding_box(SCREAMING_SNAKE_CASE_ ) for box in boxes] # {"scores": [...], ...} --> [{"score":x, ...}, ...] A: Tuple = ['''score''', '''label''', '''box'''] A: str = [ dict(zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) for vals in zip(raw_annotation['''scores'''] , raw_annotation['''labels'''] , raw_annotation['''boxes'''] ) ] return annotation def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : "torch.Tensor" ) -> Dict[str, int]: '''simple docstring''' if self.framework != "pt": raise ValueError('''The ObjectDetectionPipeline is only available in PyTorch.''' ) A , A , A , A: str = box.int().tolist() A: str = { '''xmin''': xmin, '''ymin''': ymin, '''xmax''': xmax, '''ymax''': ymax, } return bbox
319
1
'''simple docstring''' from collections.abc import Iterator, MutableMapping from dataclasses import dataclass from typing import Generic, TypeVar UpperCamelCase = TypeVar('''KEY''') UpperCamelCase = TypeVar('''VAL''') @dataclass(frozen=UpperCAmelCase_ , slots=UpperCAmelCase_ ) class lowerCAmelCase_ ( Generic[KEY, VAL] ): '''simple docstring''' UpperCamelCase_ : KEY UpperCamelCase_ : VAL class lowerCAmelCase_ ( _Item ): '''simple docstring''' def __init__( self : int ) -> None: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def __bool__( self : str ) -> bool: '''simple docstring''' return False UpperCamelCase = _DeletedItem() class lowerCAmelCase_ ( MutableMapping[KEY, VAL] ): '''simple docstring''' def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : int = 8 , SCREAMING_SNAKE_CASE_ : float = 0.75 ) -> None: '''simple docstring''' A: List[str] = initial_block_size A: list[_Item | None] = [None] * initial_block_size assert 0.0 < capacity_factor < 1.0 A: List[Any] = capacity_factor A: Union[str, Any] = 0 def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : KEY ) -> int: '''simple docstring''' return hash(SCREAMING_SNAKE_CASE_ ) % len(self._buckets ) def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : int ) -> int: '''simple docstring''' return (ind + 1) % len(self._buckets ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : KEY , SCREAMING_SNAKE_CASE_ : VAL ) -> bool: '''simple docstring''' A: Tuple = self._buckets[ind] if not stored: A: Optional[int] = _Item(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self._len += 1 return True elif stored.key == key: A: List[Any] = _Item(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return True else: return False def _snake_case ( self : str ) -> bool: '''simple docstring''' A: Union[str, Any] = len(self._buckets ) * self._capacity_factor return len(self ) >= int(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Any ) -> bool: '''simple docstring''' if len(self._buckets ) <= self._initial_block_size: return False A: Any = len(self._buckets ) * self._capacity_factor / 2 return len(self ) < limit def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : int ) -> None: '''simple docstring''' A: int = self._buckets A: Tuple = [None] * new_size A: Dict = 0 for item in old_buckets: if item: self._add_item(item.key , item.val ) def _snake_case ( self : Optional[Any] ) -> None: '''simple docstring''' self._resize(len(self._buckets ) * 2 ) def _snake_case ( self : int ) -> None: '''simple docstring''' self._resize(len(self._buckets ) // 2 ) def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : KEY ) -> Iterator[int]: '''simple docstring''' A: Dict = self._get_bucket_index(SCREAMING_SNAKE_CASE_ ) for _ in range(len(self._buckets ) ): yield ind A: List[Any] = self._get_next_ind(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : KEY , SCREAMING_SNAKE_CASE_ : VAL ) -> None: '''simple docstring''' for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE_ ): if self._try_set(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): break def __setitem__( self : List[str] , SCREAMING_SNAKE_CASE_ : KEY , SCREAMING_SNAKE_CASE_ : VAL ) -> None: '''simple docstring''' if self._is_full(): self._size_up() self._add_item(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def __delitem__( self : List[str] , SCREAMING_SNAKE_CASE_ : KEY ) -> None: '''simple docstring''' for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE_ ): A: int = self._buckets[ind] if item is None: raise KeyError(SCREAMING_SNAKE_CASE_ ) if item is _deleted: continue if item.key == key: A: Optional[int] = _deleted self._len -= 1 break if self._is_sparse(): self._size_down() def __getitem__( self : Tuple , SCREAMING_SNAKE_CASE_ : KEY ) -> VAL: '''simple docstring''' for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE_ ): A: Optional[int] = self._buckets[ind] if item is None: break if item is _deleted: continue if item.key == key: return item.val raise KeyError(SCREAMING_SNAKE_CASE_ ) def __len__( self : Optional[Any] ) -> int: '''simple docstring''' return self._len def __iter__( self : Any ) -> Iterator[KEY]: '''simple docstring''' yield from (item.key for item in self._buckets if item) def __repr__( self : Dict ) -> str: '''simple docstring''' A: Union[str, Any] = ''' ,'''.join( f"""{item.key}: {item.val}""" for item in self._buckets if item ) return f"""HashMap({val_string})"""
319
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''YituTech/conv-bert-base''': '''https://huggingface.co/YituTech/conv-bert-base/resolve/main/config.json''', '''YituTech/conv-bert-medium-small''': ( '''https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/config.json''' ), '''YituTech/conv-bert-small''': '''https://huggingface.co/YituTech/conv-bert-small/resolve/main/config.json''', # See all ConvBERT models at https://huggingface.co/models?filter=convbert } class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = """convbert""" def __init__( self : Dict , SCREAMING_SNAKE_CASE_ : Dict=3_05_22 , SCREAMING_SNAKE_CASE_ : int=7_68 , SCREAMING_SNAKE_CASE_ : List[str]=12 , SCREAMING_SNAKE_CASE_ : List[str]=12 , SCREAMING_SNAKE_CASE_ : Dict=30_72 , SCREAMING_SNAKE_CASE_ : Optional[int]="gelu" , SCREAMING_SNAKE_CASE_ : List[Any]=0.1 , SCREAMING_SNAKE_CASE_ : int=0.1 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=5_12 , SCREAMING_SNAKE_CASE_ : List[Any]=2 , SCREAMING_SNAKE_CASE_ : List[str]=0.02 , SCREAMING_SNAKE_CASE_ : int=1E-12 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=1 , SCREAMING_SNAKE_CASE_ : int=0 , SCREAMING_SNAKE_CASE_ : str=2 , SCREAMING_SNAKE_CASE_ : List[Any]=7_68 , SCREAMING_SNAKE_CASE_ : Optional[Any]=2 , SCREAMING_SNAKE_CASE_ : Any=9 , SCREAMING_SNAKE_CASE_ : Tuple=1 , SCREAMING_SNAKE_CASE_ : List[Any]=None , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> List[Any]: '''simple docstring''' super().__init__( pad_token_id=SCREAMING_SNAKE_CASE_ , bos_token_id=SCREAMING_SNAKE_CASE_ , eos_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Dict = vocab_size A: Tuple = hidden_size A: Optional[int] = num_hidden_layers A: List[str] = num_attention_heads A: int = intermediate_size A: int = hidden_act A: List[str] = hidden_dropout_prob A: int = attention_probs_dropout_prob A: Tuple = max_position_embeddings A: Any = type_vocab_size A: str = initializer_range A: Union[str, Any] = layer_norm_eps A: str = embedding_size A: Optional[int] = head_ratio A: List[Any] = conv_kernel_size A: List[Any] = num_groups A: Optional[int] = classifier_dropout class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' @property def _snake_case ( self : Optional[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": A: Tuple = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: A: List[str] = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis), ] )
319
1
'''simple docstring''' import numpy as np from cva import COLOR_BGR2GRAY, cvtColor, imread from numpy import array, uinta from PIL import Image from digital_image_processing import change_contrast as cc from digital_image_processing import convert_to_negative as cn from digital_image_processing import sepia as sp from digital_image_processing.dithering import burkes as bs from digital_image_processing.edge_detection import canny from digital_image_processing.filters import convolve as conv from digital_image_processing.filters import gaussian_filter as gg from digital_image_processing.filters import local_binary_pattern as lbp from digital_image_processing.filters import median_filter as med from digital_image_processing.filters import sobel_filter as sob from digital_image_processing.resize import resize as rs UpperCamelCase = imread(R'''digital_image_processing/image_data/lena_small.jpg''') UpperCamelCase = cvtColor(img, COLOR_BGR2GRAY) def SCREAMING_SNAKE_CASE( ) -> Tuple: A: Union[str, Any] = cn.convert_to_negative(__lowercase ) # assert negative_img array for at least one True assert negative_img.any() def SCREAMING_SNAKE_CASE( ) -> int: with Image.open('''digital_image_processing/image_data/lena_small.jpg''' ) as img: # Work around assertion for response assert str(cc.change_contrast(__lowercase , 1_1_0 ) ).startswith( '''<PIL.Image.Image image mode=RGB size=100x100 at''' ) def SCREAMING_SNAKE_CASE( ) -> List[str]: A: List[Any] = canny.gen_gaussian_kernel(9 , sigma=1.4 ) # Assert ambiguous array assert resp.all() def SCREAMING_SNAKE_CASE( ) -> List[str]: A: List[str] = imread('''digital_image_processing/image_data/lena_small.jpg''' , 0 ) # assert ambiguous array for all == True assert canny_img.all() A: Any = canny.canny(__lowercase ) # assert canny array for at least one True assert canny_array.any() def SCREAMING_SNAKE_CASE( ) -> Optional[int]: assert gg.gaussian_filter(__lowercase , 5 , sigma=0.9 ).all() def SCREAMING_SNAKE_CASE( ) -> str: # laplace diagonals A: Optional[Any] = array([[0.2_5, 0.5, 0.2_5], [0.5, -3, 0.5], [0.2_5, 0.5, 0.2_5]] ) A: Optional[int] = conv.img_convolve(__lowercase , __lowercase ).astype(__lowercase ) assert res.any() def SCREAMING_SNAKE_CASE( ) -> Union[str, Any]: assert med.median_filter(__lowercase , 3 ).any() def SCREAMING_SNAKE_CASE( ) -> Dict: A , A: Optional[Any] = sob.sobel_filter(__lowercase ) assert grad.any() and theta.any() def SCREAMING_SNAKE_CASE( ) -> Optional[Any]: A: str = sp.make_sepia(__lowercase , 2_0 ) assert sepia.all() def SCREAMING_SNAKE_CASE( __lowercase = "digital_image_processing/image_data/lena_small.jpg" ) -> int: A: List[Any] = bs.Burkes(imread(__lowercase , 1 ) , 1_2_0 ) burkes.process() assert burkes.output_img.any() def SCREAMING_SNAKE_CASE( __lowercase = "digital_image_processing/image_data/lena_small.jpg" , ) -> str: A: str = rs.NearestNeighbour(imread(__lowercase , 1 ) , 4_0_0 , 2_0_0 ) nn.process() assert nn.output.any() def SCREAMING_SNAKE_CASE( ) -> List[str]: A: List[Any] = '''digital_image_processing/image_data/lena.jpg''' # Reading the image and converting it to grayscale. A: Union[str, Any] = imread(__lowercase , 0 ) # Test for get_neighbors_pixel function() return not None A: Optional[Any] = 0 A: List[str] = 0 A: Tuple = image[x_coordinate][y_coordinate] A: Tuple = lbp.get_neighbors_pixel( __lowercase , __lowercase , __lowercase , __lowercase ) assert neighbors_pixels is not None # Test for local_binary_pattern function() # Create a numpy array as the same height and width of read image A: Any = np.zeros((image.shape[0], image.shape[1]) ) # Iterating through the image and calculating the local binary pattern value # for each pixel. for i in range(0 , image.shape[0] ): for j in range(0 , image.shape[1] ): A: Union[str, Any] = lbp.local_binary_value(__lowercase , __lowercase , __lowercase ) assert lbp_image.any()
319
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE( __lowercase ) -> bool: if len(__lowercase ) < 2: raise ValueError('''Monogons and Digons are not polygons in the Euclidean space''' ) if any(i <= 0 for i in nums ): raise ValueError('''All values must be greater than 0''' ) A: Any = nums.copy() copy_nums.sort() return copy_nums[-1] < sum(copy_nums[:-1] ) if __name__ == "__main__": import doctest doctest.testmod()
319
1
'''simple docstring''' import math def SCREAMING_SNAKE_CASE( __lowercase ) -> list: A: int = [True] * n A: List[str] = False A: List[str] = False A: List[Any] = True for i in range(3 , int(n**0.5 + 1 ) , 2 ): A: List[str] = i * 2 while index < n: A: Any = False A: Dict = index + i A: str = [2] for i in range(3 , __lowercase , 2 ): if is_prime[i]: primes.append(__lowercase ) return primes def SCREAMING_SNAKE_CASE( __lowercase = 9_9_9_9_6_6_6_6_3_3_3_3 ) -> int: A: Union[str, Any] = math.floor(math.sqrt(__lowercase ) ) + 1_0_0 A: Tuple = prime_sieve(__lowercase ) A: List[Any] = 0 A: Optional[int] = 0 A: Dict = primes[prime_index] while (last_prime**2) <= limit: A: str = primes[prime_index + 1] A: List[str] = last_prime**2 A: Union[str, Any] = next_prime**2 # Get numbers divisible by lps(current) A: Dict = lower_bound + last_prime while upper_bound > current <= limit: matches_sum += current current += last_prime # Reset the upper_bound while (upper_bound - next_prime) > limit: upper_bound -= next_prime # Add the numbers divisible by ups(current) A: Dict = upper_bound - next_prime while current > lower_bound: matches_sum += current current -= next_prime # Remove the numbers divisible by both ups and lps A: List[Any] = 0 while upper_bound > current <= limit: if current <= lower_bound: # Increment the current number current += last_prime * next_prime continue if current > limit: break # Remove twice since it was added by both ups and lps matches_sum -= current * 2 # Increment the current number current += last_prime * next_prime # Setup for next pair A: Any = next_prime prime_index += 1 return matches_sum if __name__ == "__main__": print(solution())
319
'''simple docstring''' # tests directory-specific settings - this file is run automatically # by pytest before any tests are run import doctest import sys import warnings from os.path import abspath, dirname, join import _pytest from transformers.testing_utils import HfDoctestModule, HfDocTestParser # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. UpperCamelCase = abspath(join(dirname(__file__), '''src''')) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action='''ignore''', category=FutureWarning) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: config.addinivalue_line( '''markers''' , '''is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested''' ) config.addinivalue_line( '''markers''' , '''is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested''' ) config.addinivalue_line('''markers''' , '''is_pipeline_test: mark test to run only when pipelines are tested''' ) config.addinivalue_line('''markers''' , '''is_staging_test: mark test to run only in the staging environment''' ) config.addinivalue_line('''markers''' , '''accelerate_tests: mark test that require accelerate''' ) config.addinivalue_line('''markers''' , '''tool_tests: mark the tool tests that are run on their specific schedule''' ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: from transformers.testing_utils import pytest_terminal_summary_main A: Optional[int] = terminalreporter.config.getoption('''--make-reports''' ) if make_reports: pytest_terminal_summary_main(__lowercase , id=__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Any: # If no tests are collected, pytest exists with code 5, which makes the CI fail. if exitstatus == 5: A: Tuple = 0 # Doctest custom flag to ignore output. UpperCamelCase = doctest.register_optionflag('''IGNORE_RESULT''') UpperCamelCase = doctest.OutputChecker class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ) -> str: '''simple docstring''' if IGNORE_RESULT & optionflags: return True return OutputChecker.check_output(self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase = CustomOutputChecker UpperCamelCase = HfDoctestModule UpperCamelCase = HfDocTestParser
319
1
'''simple docstring''' # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from pathlib import Path import torch from ...utils import is_npu_available, is_xpu_available from .config_args import ClusterConfig, default_json_config_file from .config_utils import SubcommandHelpFormatter UpperCamelCase = '''Create a default config file for Accelerate with only a few flags set.''' def SCREAMING_SNAKE_CASE( __lowercase="no" , __lowercase = default_json_config_file , __lowercase = False ) -> List[Any]: A: int = Path(__lowercase ) path.parent.mkdir(parents=__lowercase , exist_ok=__lowercase ) if path.exists(): print( F"""Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.""" ) return False A: List[Any] = mixed_precision.lower() if mixed_precision not in ["no", "fp16", "bf16", "fp8"]: raise ValueError( F"""`mixed_precision` should be one of 'no', 'fp16', 'bf16', or 'fp8'. Received {mixed_precision}""" ) A: Any = { '''compute_environment''': '''LOCAL_MACHINE''', '''mixed_precision''': mixed_precision, } if torch.cuda.is_available(): A: Any = torch.cuda.device_count() A: Optional[int] = num_gpus A: Tuple = False if num_gpus > 1: A: Tuple = '''MULTI_GPU''' else: A: str = '''NO''' elif is_xpu_available() and use_xpu: A: Tuple = torch.xpu.device_count() A: int = num_xpus A: str = False if num_xpus > 1: A: List[Any] = '''MULTI_XPU''' else: A: List[Any] = '''NO''' elif is_npu_available(): A: Dict = torch.npu.device_count() A: Tuple = num_npus A: str = False if num_npus > 1: A: Any = '''MULTI_NPU''' else: A: Union[str, Any] = '''NO''' else: A: List[Any] = 0 A: Any = True A: Tuple = 1 A: Tuple = '''NO''' A: Optional[Any] = ClusterConfig(**__lowercase ) config.to_json_file(__lowercase ) return path def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[Any]: A: Union[str, Any] = parser.add_parser('''default''' , parents=__lowercase , help=__lowercase , formatter_class=__lowercase ) parser.add_argument( '''--config_file''' , default=__lowercase , help=( '''The path to use to store the config file. Will default to a file named default_config.yaml in the cache ''' '''location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have ''' '''such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed ''' '''with \'huggingface\'.''' ) , dest='''save_location''' , ) parser.add_argument( '''--mixed_precision''' , choices=['''no''', '''fp16''', '''bf16'''] , type=__lowercase , help='''Whether or not to use mixed precision training. ''' '''Choose between FP16 and BF16 (bfloat16) training. ''' '''BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later.''' , default='''no''' , ) parser.set_defaults(func=__lowercase ) return parser def SCREAMING_SNAKE_CASE( __lowercase ) -> str: A: Tuple = write_basic_config(args.mixed_precision , args.save_location ) if config_file: print(F"""accelerate configuration saved at {config_file}""" )
319
'''simple docstring''' import heapq import sys import numpy as np UpperCamelCase = tuple[int, int] class lowerCAmelCase_ : '''simple docstring''' def __init__( self : List[Any] ) -> str: '''simple docstring''' A: Any = [] A: int = set() def _snake_case ( self : Optional[Any] ) -> int: '''simple docstring''' if not self.empty(): return self.elements[0][0] else: return float('''inf''' ) def _snake_case ( self : List[str] ) -> List[Any]: '''simple docstring''' return len(self.elements ) == 0 def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Any ) -> List[Any]: '''simple docstring''' if item not in self.set: heapq.heappush(self.elements , (priority, item) ) self.set.add(SCREAMING_SNAKE_CASE_ ) else: # update # print("update", item) A: Optional[int] = [] ((A) , (A)): str = heapq.heappop(self.elements ) while x != item: temp.append((pri, x) ) ((A) , (A)): int = heapq.heappop(self.elements ) temp.append((priority, item) ) for pro, xxx in temp: heapq.heappush(self.elements , (pro, xxx) ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : str ) -> Any: '''simple docstring''' if item in self.set: self.set.remove(SCREAMING_SNAKE_CASE_ ) A: str = [] ((A) , (A)): List[str] = heapq.heappop(self.elements ) while x != item: temp.append((pro, x) ) ((A) , (A)): Any = heapq.heappop(self.elements ) for prito, yyy in temp: heapq.heappush(self.elements , (prito, yyy) ) def _snake_case ( self : List[Any] ) -> Optional[int]: '''simple docstring''' return self.elements[0][1] def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' ((A) , (A)): Dict = heapq.heappop(self.elements ) self.set.remove(SCREAMING_SNAKE_CASE_ ) return (priority, item) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: # euclidean distance A: List[str] = np.array(__lowercase ) A: Optional[int] = np.array(__lowercase ) return np.linalg.norm(a - b ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> int: # integer division by time variable return consistent_heuristic(__lowercase , __lowercase ) // t def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[Any]: # manhattan distance return abs(p[0] - goal[0] ) + abs(p[1] - goal[1] ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase ) -> List[Any]: A: int = g_function[start] + Wa * heuristics[i](__lowercase , __lowercase ) return ans def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Optional[int]: A: Union[str, Any] = np.chararray((n, n) ) for i in range(__lowercase ): for j in range(__lowercase ): A: Union[str, Any] = '''*''' for i in range(__lowercase ): for j in range(__lowercase ): if (j, (n - 1) - i) in blocks: A: Optional[Any] = '''#''' A: Tuple = '''-''' A: List[str] = back_pointer[goal] while x != start: ((A) , (A)): Tuple = x # print(x) A: List[str] = '''-''' A: str = back_pointer[x] A: Dict = '''-''' for i in range(__lowercase ): for j in range(__lowercase ): if (i, j) == (0, n - 1): print(grid[i][j] , end=''' ''' ) print('''<-- End position''' , end=''' ''' ) else: print(grid[i][j] , end=''' ''' ) print() print('''^''' ) print('''Start position''' ) print() print('''# is an obstacle''' ) print('''- is the path taken by algorithm''' ) print('''PATH TAKEN BY THE ALGORITHM IS:-''' ) A: List[str] = back_pointer[goal] while x != start: print(__lowercase , end=''' ''' ) A: Optional[int] = back_pointer[x] print(__lowercase ) sys.exit() def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: if p[0] < 0 or p[0] > n - 1: return False if p[1] < 0 or p[1] > n - 1: return False return True def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) -> Union[str, Any]: for itera in range(__lowercase ): open_list[itera].remove_element(__lowercase ) # print("s", s) # print("j", j) ((A) , (A)): Tuple = s A: Optional[Any] = (x - 1, y) A: str = (x + 1, y) A: List[Any] = (x, y + 1) A: int = (x, y - 1) for neighbours in [left, right, up, down]: if neighbours not in blocks: if valid(__lowercase ) and neighbours not in visited: # print("neighbour", neighbours) visited.add(__lowercase ) A: int = -1 A: int = float('''inf''' ) if valid(__lowercase ) and g_function[neighbours] > g_function[s] + 1: A: List[str] = g_function[s] + 1 A: List[str] = s if neighbours not in close_list_anchor: open_list[0].put(__lowercase , key(__lowercase , 0 , __lowercase , __lowercase ) ) if neighbours not in close_list_inad: for var in range(1 , __lowercase ): if key(__lowercase , __lowercase , __lowercase , __lowercase ) <= Wa * key( __lowercase , 0 , __lowercase , __lowercase ): open_list[j].put( __lowercase , key(__lowercase , __lowercase , __lowercase , __lowercase ) ) def SCREAMING_SNAKE_CASE( ) -> Tuple: A: str = [] for x in range(1 , 5 ): for y in range(1 , 6 ): some_list.append((x, y) ) for x in range(1_5 , 2_0 ): some_list.append((x, 1_7) ) for x in range(1_0 , 1_9 ): for y in range(1 , 1_5 ): some_list.append((x, y) ) # L block for x in range(1 , 4 ): for y in range(1_2 , 1_9 ): some_list.append((x, y) ) for x in range(3 , 1_3 ): for y in range(1_6 , 1_9 ): some_list.append((x, y) ) return some_list UpperCamelCase = {0: consistent_heuristic, 1: heuristic_a, 2: heuristic_a} UpperCamelCase = [ (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1), (15, 1), (16, 1), (17, 1), (18, 1), (19, 1), ] UpperCamelCase = make_common_ground() UpperCamelCase = blocks_blk # hyper parameters UpperCamelCase = 1 UpperCamelCase = 1 UpperCamelCase = 20 UpperCamelCase = 3 # one consistent and two other inconsistent # start and end destination UpperCamelCase = (0, 0) UpperCamelCase = (n - 1, n - 1) UpperCamelCase = 1 def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: A: int = {start: 0, goal: float('''inf''' )} A: Union[str, Any] = {start: -1, goal: -1} A: List[Any] = [] A: Union[str, Any] = set() for i in range(__lowercase ): open_list.append(PriorityQueue() ) open_list[i].put(__lowercase , key(__lowercase , __lowercase , __lowercase , __lowercase ) ) A: list[int] = [] A: list[int] = [] while open_list[0].minkey() < float('''inf''' ): for i in range(1 , __lowercase ): # print(open_list[0].minkey(), open_list[i].minkey()) if open_list[i].minkey() <= Wa * open_list[0].minkey(): global t t += 1 if g_function[goal] <= open_list[i].minkey(): if g_function[goal] < float('''inf''' ): do_something(__lowercase , __lowercase , __lowercase ) else: A , A: Union[str, Any] = open_list[i].top_show() visited.add(__lowercase ) expand_state( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) close_list_inad.append(__lowercase ) else: if g_function[goal] <= open_list[0].minkey(): if g_function[goal] < float('''inf''' ): do_something(__lowercase , __lowercase , __lowercase ) else: A: Union[str, Any] = open_list[0].top_show() visited.add(__lowercase ) expand_state( __lowercase , 0 , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) close_list_anchor.append(__lowercase ) print('''No path found to goal''' ) print() for i in range(n - 1 , -1 , -1 ): for j in range(__lowercase ): if (j, i) in blocks: print('''#''' , end=''' ''' ) elif (j, i) in back_pointer: if (j, i) == (n - 1, n - 1): print('''*''' , end=''' ''' ) else: print('''-''' , end=''' ''' ) else: print('''*''' , end=''' ''' ) if (j, i) == (n - 1, n - 1): print('''<-- End position''' , end=''' ''' ) print() print('''^''' ) print('''Start position''' ) print() print('''# is an obstacle''' ) print('''- is the path taken by algorithm''' ) if __name__ == "__main__": multi_a_star(start, goal, n_heuristic)
319
1
'''simple docstring''' import itertools import json import linecache import os import pickle import re import socket import string from collections import Counter from logging import getLogger from pathlib import Path from typing import Callable, Dict, Iterable, List import git import torch from torch.utils.data import Dataset from transformers import BartTokenizer, RagTokenizer, TaTokenizer def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase=True , __lowercase="pt" ) -> Dict: A: str = {'''add_prefix_space''': True} if isinstance(__lowercase , __lowercase ) and not line.startswith(''' ''' ) else {} A: Dict = padding_side return tokenizer( [line] , max_length=__lowercase , padding='''max_length''' if pad_to_max_length else None , truncation=__lowercase , return_tensors=__lowercase , add_special_tokens=__lowercase , **__lowercase , ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=None , ) -> Optional[int]: A: Optional[int] = input_ids.ne(__lowercase ).any(dim=0 ) if attention_mask is None: return input_ids[:, keep_column_mask] else: return (input_ids[:, keep_column_mask], attention_mask[:, keep_column_mask]) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Dict="train" , SCREAMING_SNAKE_CASE_ : List[str]=None , SCREAMING_SNAKE_CASE_ : List[str]=None , SCREAMING_SNAKE_CASE_ : Optional[int]=None , SCREAMING_SNAKE_CASE_ : Dict="" , ) -> Tuple: '''simple docstring''' super().__init__() A: Union[str, Any] = Path(SCREAMING_SNAKE_CASE_ ).joinpath(type_path + '''.source''' ) A: Any = Path(SCREAMING_SNAKE_CASE_ ).joinpath(type_path + '''.target''' ) A: Optional[Any] = self.get_char_lens(self.src_file ) A: str = max_source_length A: Union[str, Any] = max_target_length assert min(self.src_lens ) > 0, f"""found empty line in {self.src_file}""" A: Dict = tokenizer A: int = prefix if n_obs is not None: A: int = self.src_lens[:n_obs] A: Tuple = src_lang A: Union[str, Any] = tgt_lang def __len__( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' return len(self.src_lens ) def __getitem__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Dict ) -> Dict[str, torch.Tensor]: '''simple docstring''' A: Optional[Any] = index + 1 # linecache starts at 1 A: Optional[int] = self.prefix + linecache.getline(str(self.src_file ) , SCREAMING_SNAKE_CASE_ ).rstrip('''\n''' ) A: int = linecache.getline(str(self.tgt_file ) , SCREAMING_SNAKE_CASE_ ).rstrip('''\n''' ) assert source_line, f"""empty source line for index {index}""" assert tgt_line, f"""empty tgt line for index {index}""" # Need to add eos token manually for T5 if isinstance(self.tokenizer , SCREAMING_SNAKE_CASE_ ): source_line += self.tokenizer.eos_token tgt_line += self.tokenizer.eos_token # Pad source and target to the right A: Any = ( self.tokenizer.question_encoder if isinstance(self.tokenizer , SCREAMING_SNAKE_CASE_ ) else self.tokenizer ) A: Dict = self.tokenizer.generator if isinstance(self.tokenizer , SCREAMING_SNAKE_CASE_ ) else self.tokenizer A: str = encode_line(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , self.max_source_length , '''right''' ) A: Dict = encode_line(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , self.max_target_length , '''right''' ) A: Union[str, Any] = source_inputs['''input_ids'''].squeeze() A: List[Any] = target_inputs['''input_ids'''].squeeze() A: int = source_inputs['''attention_mask'''].squeeze() return { "input_ids": source_ids, "attention_mask": src_mask, "decoder_input_ids": target_ids, } @staticmethod def _snake_case ( SCREAMING_SNAKE_CASE_ : List[Any] ) -> Tuple: '''simple docstring''' return [len(SCREAMING_SNAKE_CASE_ ) for x in Path(SCREAMING_SNAKE_CASE_ ).open().readlines()] def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : int ) -> Dict[str, torch.Tensor]: '''simple docstring''' A: Any = torch.stack([x['''input_ids'''] for x in batch] ) A: Optional[Any] = torch.stack([x['''attention_mask'''] for x in batch] ) A: List[str] = torch.stack([x['''decoder_input_ids'''] for x in batch] ) A: Union[str, Any] = ( self.tokenizer.generator.pad_token_id if isinstance(self.tokenizer , SCREAMING_SNAKE_CASE_ ) else self.tokenizer.pad_token_id ) A: str = ( self.tokenizer.question_encoder.pad_token_id if isinstance(self.tokenizer , SCREAMING_SNAKE_CASE_ ) else self.tokenizer.pad_token_id ) A: Dict = trim_batch(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A , A: Tuple = trim_batch(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ ) A: str = { '''input_ids''': source_ids, '''attention_mask''': source_mask, '''decoder_input_ids''': y, } return batch UpperCamelCase = getLogger(__name__) def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: return list(itertools.chain.from_iterable(__lowercase ) ) def SCREAMING_SNAKE_CASE( __lowercase ) -> None: A: Optional[int] = get_git_info() save_json(__lowercase , os.path.join(__lowercase , '''git_log.json''' ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=4 , **__lowercase ) -> Optional[Any]: with open(__lowercase , '''w''' ) as f: json.dump(__lowercase , __lowercase , indent=__lowercase , **__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> str: with open(__lowercase ) as f: return json.load(__lowercase ) def SCREAMING_SNAKE_CASE( ) -> int: A: Optional[int] = git.Repo(search_parent_directories=__lowercase ) A: Tuple = { '''repo_id''': str(__lowercase ), '''repo_sha''': str(repo.head.object.hexsha ), '''repo_branch''': str(repo.active_branch ), '''hostname''': str(socket.gethostname() ), } return repo_infos def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List: return list(map(__lowercase , __lowercase ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[str]: with open(__lowercase , '''wb''' ) as f: return pickle.dump(__lowercase , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: def remove_articles(__lowercase ): return re.sub(r'''\b(a|an|the)\b''' , ''' ''' , __lowercase ) def white_space_fix(__lowercase ): return " ".join(text.split() ) def remove_punc(__lowercase ): A: List[Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__lowercase ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__lowercase ) ) ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Tuple: A: int = normalize_answer(__lowercase ).split() A: Union[str, Any] = normalize_answer(__lowercase ).split() A: List[str] = Counter(__lowercase ) & Counter(__lowercase ) A: List[str] = sum(common.values() ) if num_same == 0: return 0 A: Union[str, Any] = 1.0 * num_same / len(__lowercase ) A: str = 1.0 * num_same / len(__lowercase ) A: str = (2 * precision * recall) / (precision + recall) return fa def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[str]: return normalize_answer(__lowercase ) == normalize_answer(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Dict: assert len(__lowercase ) == len(__lowercase ) A: Union[str, Any] = 0 for hypo, pred in zip(__lowercase , __lowercase ): em += exact_match_score(__lowercase , __lowercase ) if len(__lowercase ) > 0: em /= len(__lowercase ) return {"em": em} def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: return model_prefix.startswith('''rag''' ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Optional[int]: A: Optional[Any] = {p: p for p in extra_params} # T5 models don't have `dropout` param, they have `dropout_rate` instead A: Optional[Any] = '''dropout_rate''' for p in extra_params: if getattr(__lowercase , __lowercase , __lowercase ): if not hasattr(__lowercase , __lowercase ) and not hasattr(__lowercase , equivalent_param[p] ): logger.info('''config doesn\'t have a `{}` attribute'''.format(__lowercase ) ) delattr(__lowercase , __lowercase ) continue A: Dict = p if hasattr(__lowercase , __lowercase ) else equivalent_param[p] setattr(__lowercase , __lowercase , getattr(__lowercase , __lowercase ) ) delattr(__lowercase , __lowercase ) return hparams, config
319
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase = 1 , __lowercase = 1_0_0_0 ) -> int: A: Any = 1 A: Optional[Any] = 0 for divide_by_number in range(__lowercase , digit + 1 ): A: list[int] = [] A: List[Any] = numerator for _ in range(1 , digit + 1 ): if now_divide in has_been_divided: if longest_list_length < len(__lowercase ): A: Any = len(__lowercase ) A: Dict = divide_by_number else: has_been_divided.append(__lowercase ) A: str = now_divide * 1_0 % divide_by_number return the_digit # Tests if __name__ == "__main__": import doctest doctest.testmod()
319
1
'''simple docstring''' import inspect import os import sys import unittest import accelerate from accelerate.test_utils import execute_subprocess_async, require_tpu class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : List[str] ) -> Dict: '''simple docstring''' A: str = inspect.getfile(accelerate.test_utils ) A: Optional[Any] = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_script.py'''] ) A: Union[str, Any] = os.path.sep.join(inspect.getfile(self.__class__ ).split(os.path.sep )[:-1] ) @require_tpu def _snake_case ( self : int ) -> List[str]: '''simple docstring''' A: List[Any] = f""" {self.test_dir}/xla_spawn.py --num_cores 8 {self.test_file_path} """.split() A: Union[str, Any] = [sys.executable] + distributed_args execute_subprocess_async(SCREAMING_SNAKE_CASE_ , env=os.environ.copy() )
319
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) UpperCamelCase = { '''configuration_vision_encoder_decoder''': ['''VisionEncoderDecoderConfig''', '''VisionEncoderDecoderOnnxConfig'''] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''VisionEncoderDecoderModel'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''TFVisionEncoderDecoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''FlaxVisionEncoderDecoderModel'''] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
1
'''simple docstring''' # limitations under the License. # NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from .pipelines import DiffusionPipeline, ImagePipelineOutput # noqa: F401 from .utils import deprecate deprecate( '''pipelines_utils''', '''0.22.0''', '''Importing `DiffusionPipeline` or `ImagePipelineOutput` from diffusers.pipeline_utils is deprecated. Please import from diffusers.pipelines.pipeline_utils instead.''', standard_warn=False, stacklevel=3, )
319
'''simple docstring''' import fire from utils import calculate_rouge, save_json def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=None , **__lowercase ) -> Any: A: Any = [x.strip() for x in open(__lowercase ).readlines()] A: Dict = [x.strip() for x in open(__lowercase ).readlines()][: len(__lowercase )] A: Union[str, Any] = calculate_rouge(__lowercase , __lowercase , **__lowercase ) if save_path is not None: save_json(__lowercase , __lowercase , indent=__lowercase ) return metrics # these print nicely if __name__ == "__main__": fire.Fire(calculate_rouge_path)
319
1
'''simple docstring''' from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import nn from ..utils import maybe_allow_in_graph from .activations import get_activation from .attention_processor import Attention from .embeddings import CombinedTimestepLabelEmbeddings @maybe_allow_in_graph class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : str=0.0 , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : str = "geglu" , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : bool = True , SCREAMING_SNAKE_CASE_ : str = "layer_norm" , SCREAMING_SNAKE_CASE_ : bool = False , ) -> Optional[int]: '''simple docstring''' super().__init__() A: str = only_cross_attention A: int = (num_embeds_ada_norm is not None) and norm_type == '''ada_norm_zero''' A: int = (num_embeds_ada_norm is not None) and norm_type == '''ada_norm''' if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None: raise ValueError( f"""`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to""" f""" define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.""" ) # Define 3 blocks. Each block has its own normalization layer. # 1. Self-Attn if self.use_ada_layer_norm: A: Tuple = AdaLayerNorm(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) elif self.use_ada_layer_norm_zero: A: str = AdaLayerNormZero(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else: A: Tuple = nn.LayerNorm(SCREAMING_SNAKE_CASE_ , elementwise_affine=SCREAMING_SNAKE_CASE_ ) A: List[Any] = Attention( query_dim=SCREAMING_SNAKE_CASE_ , heads=SCREAMING_SNAKE_CASE_ , dim_head=SCREAMING_SNAKE_CASE_ , dropout=SCREAMING_SNAKE_CASE_ , bias=SCREAMING_SNAKE_CASE_ , cross_attention_dim=cross_attention_dim if only_cross_attention else None , upcast_attention=SCREAMING_SNAKE_CASE_ , ) # 2. Cross-Attn if cross_attention_dim is not None or double_self_attention: # We currently only use AdaLayerNormZero for self attention where there will only be one attention block. # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during # the second cross attention block. A: Any = ( AdaLayerNorm(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if self.use_ada_layer_norm else nn.LayerNorm(SCREAMING_SNAKE_CASE_ , elementwise_affine=SCREAMING_SNAKE_CASE_ ) ) A: Optional[int] = Attention( query_dim=SCREAMING_SNAKE_CASE_ , cross_attention_dim=cross_attention_dim if not double_self_attention else None , heads=SCREAMING_SNAKE_CASE_ , dim_head=SCREAMING_SNAKE_CASE_ , dropout=SCREAMING_SNAKE_CASE_ , bias=SCREAMING_SNAKE_CASE_ , upcast_attention=SCREAMING_SNAKE_CASE_ , ) # is self-attn if encoder_hidden_states is none else: A: Any = None A: Optional[Any] = None # 3. Feed-forward A: str = nn.LayerNorm(SCREAMING_SNAKE_CASE_ , elementwise_affine=SCREAMING_SNAKE_CASE_ ) A: Dict = FeedForward(SCREAMING_SNAKE_CASE_ , dropout=SCREAMING_SNAKE_CASE_ , activation_fn=SCREAMING_SNAKE_CASE_ , final_dropout=SCREAMING_SNAKE_CASE_ ) # let chunk size default to None A: Tuple = None A: Tuple = 0 def _snake_case ( self : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : int ) -> Optional[Any]: '''simple docstring''' A: Tuple = chunk_size A: Tuple = dim def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : Optional[torch.FloatTensor] = None , SCREAMING_SNAKE_CASE_ : Optional[torch.FloatTensor] = None , SCREAMING_SNAKE_CASE_ : Optional[torch.FloatTensor] = None , SCREAMING_SNAKE_CASE_ : Optional[torch.LongTensor] = None , SCREAMING_SNAKE_CASE_ : Dict[str, Any] = None , SCREAMING_SNAKE_CASE_ : Optional[torch.LongTensor] = None , ) -> Union[str, Any]: '''simple docstring''' if self.use_ada_layer_norm: A: Optional[int] = self.norma(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) elif self.use_ada_layer_norm_zero: A , A , A , A , A: Union[str, Any] = self.norma( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , hidden_dtype=hidden_states.dtype ) else: A: Tuple = self.norma(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = cross_attention_kwargs if cross_attention_kwargs is not None else {} A: List[str] = self.attna( SCREAMING_SNAKE_CASE_ , encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None , attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) if self.use_ada_layer_norm_zero: A: Optional[int] = gate_msa.unsqueeze(1 ) * attn_output A: Tuple = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: A: Optional[Any] = ( self.norma(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if self.use_ada_layer_norm else self.norma(SCREAMING_SNAKE_CASE_ ) ) A: int = self.attna( SCREAMING_SNAKE_CASE_ , encoder_hidden_states=SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: str = attn_output + hidden_states # 3. Feed-forward A: Dict = self.norma(SCREAMING_SNAKE_CASE_ ) if self.use_ada_layer_norm_zero: A: Optional[Any] = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] if self._chunk_size is not None: # "feed_forward_chunk_size" can be used to save memory if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0: raise ValueError( f"""`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.""" ) A: Tuple = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size A: Dict = torch.cat( [self.ff(SCREAMING_SNAKE_CASE_ ) for hid_slice in norm_hidden_states.chunk(SCREAMING_SNAKE_CASE_ , dim=self._chunk_dim )] , dim=self._chunk_dim , ) else: A: Tuple = self.ff(SCREAMING_SNAKE_CASE_ ) if self.use_ada_layer_norm_zero: A: Dict = gate_mlp.unsqueeze(1 ) * ff_output A: Tuple = ff_output + hidden_states return hidden_states class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : Any , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : int = 4 , SCREAMING_SNAKE_CASE_ : float = 0.0 , SCREAMING_SNAKE_CASE_ : str = "geglu" , SCREAMING_SNAKE_CASE_ : bool = False , ) -> Tuple: '''simple docstring''' super().__init__() A: Optional[int] = int(dim * mult ) A: List[Any] = dim_out if dim_out is not None else dim if activation_fn == "gelu": A: List[Any] = GELU(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if activation_fn == "gelu-approximate": A: Optional[Any] = GELU(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , approximate='''tanh''' ) elif activation_fn == "geglu": A: Dict = GEGLU(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) elif activation_fn == "geglu-approximate": A: List[str] = ApproximateGELU(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: List[str] = nn.ModuleList([] ) # project in self.net.append(SCREAMING_SNAKE_CASE_ ) # project dropout self.net.append(nn.Dropout(SCREAMING_SNAKE_CASE_ ) ) # project out self.net.append(nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(SCREAMING_SNAKE_CASE_ ) ) def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> List[str]: '''simple docstring''' for module in self.net: A: Tuple = module(SCREAMING_SNAKE_CASE_ ) return hidden_states class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : str = "none" ) -> List[Any]: '''simple docstring''' super().__init__() A: int = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Dict = approximate def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : List[Any] ) -> Tuple: '''simple docstring''' if gate.device.type != "mps": return F.gelu(SCREAMING_SNAKE_CASE_ , approximate=self.approximate ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) , approximate=self.approximate ).to(dtype=gate.dtype ) def _snake_case ( self : Dict , SCREAMING_SNAKE_CASE_ : str ) -> List[str]: '''simple docstring''' A: Dict = self.proj(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = self.gelu(SCREAMING_SNAKE_CASE_ ) return hidden_states class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : str , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ) -> Dict: '''simple docstring''' super().__init__() A: List[Any] = nn.Linear(SCREAMING_SNAKE_CASE_ , dim_out * 2 ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[Any]: '''simple docstring''' if gate.device.type != "mps": return F.gelu(SCREAMING_SNAKE_CASE_ ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : List[str] ) -> Optional[Any]: '''simple docstring''' A , A: Any = self.proj(SCREAMING_SNAKE_CASE_ ).chunk(2 , dim=-1 ) return hidden_states * self.gelu(SCREAMING_SNAKE_CASE_ ) class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : Any , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ) -> Union[str, Any]: '''simple docstring''' super().__init__() A: Dict = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Tuple ) -> int: '''simple docstring''' A: str = self.proj(SCREAMING_SNAKE_CASE_ ) return x * torch.sigmoid(1.702 * x ) class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> List[str]: '''simple docstring''' super().__init__() A: List[str] = nn.Embedding(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = nn.SiLU() A: Tuple = nn.Linear(SCREAMING_SNAKE_CASE_ , embedding_dim * 2 ) A: Dict = nn.LayerNorm(SCREAMING_SNAKE_CASE_ , elementwise_affine=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Dict ) -> List[Any]: '''simple docstring''' A: str = self.linear(self.silu(self.emb(SCREAMING_SNAKE_CASE_ ) ) ) A , A: Dict = torch.chunk(SCREAMING_SNAKE_CASE_ , 2 ) A: List[str] = self.norm(SCREAMING_SNAKE_CASE_ ) * (1 + scale) + shift return x class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : Any , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' super().__init__() A: Dict = CombinedTimestepLabelEmbeddings(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Optional[int] = nn.SiLU() A: Any = nn.Linear(SCREAMING_SNAKE_CASE_ , 6 * embedding_dim , bias=SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = nn.LayerNorm(SCREAMING_SNAKE_CASE_ , elementwise_affine=SCREAMING_SNAKE_CASE_ , eps=1E-6 ) def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[str]=None ) -> Any: '''simple docstring''' A: int = self.linear(self.silu(self.emb(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , hidden_dtype=SCREAMING_SNAKE_CASE_ ) ) ) A , A , A , A , A , A: Dict = emb.chunk(6 , dim=1 ) A: int = self.norm(SCREAMING_SNAKE_CASE_ ) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : float = 1E-5 ) -> int: '''simple docstring''' super().__init__() A: Optional[int] = num_groups A: List[Any] = eps if act_fn is None: A: Dict = None else: A: str = get_activation(SCREAMING_SNAKE_CASE_ ) A: int = nn.Linear(SCREAMING_SNAKE_CASE_ , out_dim * 2 ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Any ) -> List[str]: '''simple docstring''' if self.act: A: Tuple = self.act(SCREAMING_SNAKE_CASE_ ) A: Dict = self.linear(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = emb[:, :, None, None] A , A: Optional[Any] = emb.chunk(2 , dim=1 ) A: Any = F.group_norm(SCREAMING_SNAKE_CASE_ , self.num_groups , eps=self.eps ) A: Union[str, Any] = x * (1 + scale) + shift return x
319
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = 0 ) -> list: A: Dict = length or len(__lowercase ) A: Dict = False for i in range(length - 1 ): if list_data[i] > list_data[i + 1]: A , A: Tuple = list_data[i + 1], list_data[i] A: Union[str, Any] = True return list_data if not swapped else bubble_sort(__lowercase , length - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
319
1
'''simple docstring''' import argparse import os import shutil import torch from emmental.modules import MagnitudeBinarizer, ThresholdBinarizer, TopKBinarizer def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: A: List[Any] = args.pruning_method A: Union[str, Any] = args.threshold A: List[str] = args.model_name_or_path.rstrip('''/''' ) A: List[Any] = args.target_model_path print(F"""Load fine-pruned model from {model_name_or_path}""" ) A: List[Any] = torch.load(os.path.join(__lowercase , '''pytorch_model.bin''' ) ) A: List[Any] = {} for name, tensor in model.items(): if "embeddings" in name or "LayerNorm" in name or "pooler" in name: A: Dict = tensor print(F"""Copied layer {name}""" ) elif "classifier" in name or "qa_output" in name: A: Tuple = tensor print(F"""Copied layer {name}""" ) elif "bias" in name: A: Optional[int] = tensor print(F"""Copied layer {name}""" ) else: if pruning_method == "magnitude": A: List[Any] = MagnitudeBinarizer.apply(inputs=__lowercase , threshold=__lowercase ) A: List[str] = tensor * mask print(F"""Pruned layer {name}""" ) elif pruning_method == "topK": if "mask_scores" in name: continue A: List[str] = name[:-6] A: List[str] = model[F"""{prefix_}mask_scores"""] A: Union[str, Any] = TopKBinarizer.apply(__lowercase , __lowercase ) A: Dict = tensor * mask print(F"""Pruned layer {name}""" ) elif pruning_method == "sigmoied_threshold": if "mask_scores" in name: continue A: Dict = name[:-6] A: List[Any] = model[F"""{prefix_}mask_scores"""] A: Dict = ThresholdBinarizer.apply(__lowercase , __lowercase , __lowercase ) A: Any = tensor * mask print(F"""Pruned layer {name}""" ) elif pruning_method == "l0": if "mask_scores" in name: continue A: int = name[:-6] A: Any = model[F"""{prefix_}mask_scores"""] A , A: List[Any] = -0.1, 1.1 A: Optional[int] = torch.sigmoid(__lowercase ) A: str = s * (r - l) + l A: Tuple = s_bar.clamp(min=0.0 , max=1.0 ) A: Tuple = tensor * mask print(F"""Pruned layer {name}""" ) else: raise ValueError('''Unknown pruning method''' ) if target_model_path is None: A: List[str] = os.path.join( os.path.dirname(__lowercase ) , F"""bertarized_{os.path.basename(__lowercase )}""" ) if not os.path.isdir(__lowercase ): shutil.copytree(__lowercase , __lowercase ) print(F"""\nCreated folder {target_model_path}""" ) torch.save(__lowercase , os.path.join(__lowercase , '''pytorch_model.bin''' ) ) print('''\nPruned model saved! See you later!''' ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument( '''--pruning_method''', choices=['''l0''', '''magnitude''', '''topK''', '''sigmoied_threshold'''], type=str, required=True, help=( '''Pruning Method (l0 = L0 regularization, magnitude = Magnitude pruning, topK = Movement pruning,''' ''' sigmoied_threshold = Soft movement pruning)''' ), ) parser.add_argument( '''--threshold''', type=float, required=False, help=( '''For `magnitude` and `topK`, it is the level of remaining weights (in %) in the fine-pruned model.''' '''For `sigmoied_threshold`, it is the threshold \tau against which the (sigmoied) scores are compared.''' '''Not needed for `l0`''' ), ) parser.add_argument( '''--model_name_or_path''', type=str, required=True, help='''Folder containing the model that was previously fine-pruned''', ) parser.add_argument( '''--target_model_path''', default=None, type=str, required=False, help='''Folder containing the model that was previously fine-pruned''', ) UpperCamelCase = parser.parse_args() main(args)
319
'''simple docstring''' import argparse from collections import OrderedDict from pathlib import Path import torch from transformers import ( VisualBertConfig, VisualBertForMultipleChoice, VisualBertForPreTraining, VisualBertForQuestionAnswering, VisualBertForVisualReasoning, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = [ ('''bert.bert''', '''visual_bert'''), ('''bert.cls''', '''cls'''), ('''bert.classifier''', '''cls'''), ('''token_type_embeddings_visual''', '''visual_token_type_embeddings'''), ('''position_embeddings_visual''', '''visual_position_embeddings'''), ('''projection''', '''visual_projection'''), ] UpperCamelCase = [ '''nlvr2_coco_pre_trained.th''', '''nlvr2_fine_tuned.th''', '''nlvr2_pre_trained.th''', '''vcr_coco_pre_train.th''', '''vcr_fine_tune.th''', '''vcr_pre_train.th''', '''vqa_coco_pre_trained.th''', '''vqa_fine_tuned.th''', '''vqa_pre_trained.th''', ] def SCREAMING_SNAKE_CASE( __lowercase ) -> List[Any]: A: List[Any] = torch.load(__lowercase , map_location='''cpu''' ) return sd def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=rename_keys_prefix ) -> Optional[Any]: A: Tuple = OrderedDict() A: Dict = torch.arange(config.max_position_embeddings ).expand((1, -1) ) # detector_d = OrderedDict() for key in d: if "detector" in key: # detector_d[key.replace('detector.','')] = d[key] continue A: int = key for name_pair in rename_keys_prefix: A: Optional[int] = new_key.replace(name_pair[0] , name_pair[1] ) A: Union[str, Any] = d[key] if key == "bert.cls.predictions.decoder.weight": # Old bert code didn't have `decoder.bias`, but was added separately A: int = new_d['''cls.predictions.bias'''] return new_d @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Dict: assert ( checkpoint_path.split('''/''' )[-1] in ACCEPTABLE_CHECKPOINTS ), F"""The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}.""" # Get Config if "pre" in checkpoint_path: A: Optional[Any] = '''pretraining''' if "vcr" in checkpoint_path: A: Optional[int] = {'''visual_embedding_dim''': 5_1_2} elif "vqa_advanced" in checkpoint_path: A: Optional[Any] = {'''visual_embedding_dim''': 2_0_4_8} elif "vqa" in checkpoint_path: A: Dict = {'''visual_embedding_dim''': 2_0_4_8} elif "nlvr" in checkpoint_path: A: Tuple = {'''visual_embedding_dim''': 1_0_2_4} else: raise NotImplementedError(F"""No implementation found for `{checkpoint_path}`.""" ) else: if "vcr" in checkpoint_path: A: Dict = {'''visual_embedding_dim''': 5_1_2} A: List[str] = '''multichoice''' elif "vqa_advanced" in checkpoint_path: A: List[str] = {'''visual_embedding_dim''': 2_0_4_8} A: Optional[int] = '''vqa_advanced''' elif "vqa" in checkpoint_path: A: Dict = {'''visual_embedding_dim''': 2_0_4_8, '''num_labels''': 3_1_2_9} A: Union[str, Any] = '''vqa''' elif "nlvr" in checkpoint_path: A: Optional[int] = { '''visual_embedding_dim''': 1_0_2_4, '''num_labels''': 2, } A: str = '''nlvr''' A: Union[str, Any] = VisualBertConfig(**__lowercase ) # Load State Dict A: Union[str, Any] = load_state_dict(__lowercase ) A: str = get_new_dict(__lowercase , __lowercase ) if model_type == "pretraining": A: Optional[Any] = VisualBertForPreTraining(__lowercase ) elif model_type == "vqa": A: Optional[Any] = VisualBertForQuestionAnswering(__lowercase ) elif model_type == "nlvr": A: Union[str, Any] = VisualBertForVisualReasoning(__lowercase ) elif model_type == "multichoice": A: Any = VisualBertForMultipleChoice(__lowercase ) model.load_state_dict(__lowercase ) # Save Checkpoints Path(__lowercase ).mkdir(exist_ok=__lowercase ) model.save_pretrained(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument('''orig_checkpoint_path''', type=str, help='''A path to .th on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', type=str, help='''Path to the output PyTorch model.''') UpperCamelCase = parser.parse_args() convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)
319
1
'''simple docstring''' import warnings from ...utils import logging from .image_processing_beit import BeitImageProcessor UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> None: '''simple docstring''' warnings.warn( '''The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use BeitImageProcessor instead.''' , SCREAMING_SNAKE_CASE_ , ) super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ )
319
'''simple docstring''' from itertools import permutations def SCREAMING_SNAKE_CASE( __lowercase ) -> bool: if num[3] % 2 != 0: return False if (num[2] + num[3] + num[4]) % 3 != 0: return False if num[5] % 5 != 0: return False A: int = [7, 1_1, 1_3, 1_7] for i, test in enumerate(__lowercase ): if (num[i + 4] * 1_0_0 + num[i + 5] * 1_0 + num[i + 6]) % test != 0: return False return True def SCREAMING_SNAKE_CASE( __lowercase = 1_0 ) -> int: return sum( int(''''''.join(map(__lowercase , __lowercase ) ) ) for num in permutations(range(__lowercase ) ) if is_substring_divisible(__lowercase ) ) if __name__ == "__main__": print(f'{solution() = }')
319
1
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''YituTech/conv-bert-base''': '''https://huggingface.co/YituTech/conv-bert-base/resolve/main/config.json''', '''YituTech/conv-bert-medium-small''': ( '''https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/config.json''' ), '''YituTech/conv-bert-small''': '''https://huggingface.co/YituTech/conv-bert-small/resolve/main/config.json''', # See all ConvBERT models at https://huggingface.co/models?filter=convbert } class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = """convbert""" def __init__( self : Dict , SCREAMING_SNAKE_CASE_ : Dict=3_05_22 , SCREAMING_SNAKE_CASE_ : int=7_68 , SCREAMING_SNAKE_CASE_ : List[str]=12 , SCREAMING_SNAKE_CASE_ : List[str]=12 , SCREAMING_SNAKE_CASE_ : Dict=30_72 , SCREAMING_SNAKE_CASE_ : Optional[int]="gelu" , SCREAMING_SNAKE_CASE_ : List[Any]=0.1 , SCREAMING_SNAKE_CASE_ : int=0.1 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=5_12 , SCREAMING_SNAKE_CASE_ : List[Any]=2 , SCREAMING_SNAKE_CASE_ : List[str]=0.02 , SCREAMING_SNAKE_CASE_ : int=1E-12 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=1 , SCREAMING_SNAKE_CASE_ : int=0 , SCREAMING_SNAKE_CASE_ : str=2 , SCREAMING_SNAKE_CASE_ : List[Any]=7_68 , SCREAMING_SNAKE_CASE_ : Optional[Any]=2 , SCREAMING_SNAKE_CASE_ : Any=9 , SCREAMING_SNAKE_CASE_ : Tuple=1 , SCREAMING_SNAKE_CASE_ : List[Any]=None , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> List[Any]: '''simple docstring''' super().__init__( pad_token_id=SCREAMING_SNAKE_CASE_ , bos_token_id=SCREAMING_SNAKE_CASE_ , eos_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Dict = vocab_size A: Tuple = hidden_size A: Optional[int] = num_hidden_layers A: List[str] = num_attention_heads A: int = intermediate_size A: int = hidden_act A: List[str] = hidden_dropout_prob A: int = attention_probs_dropout_prob A: Tuple = max_position_embeddings A: Any = type_vocab_size A: str = initializer_range A: Union[str, Any] = layer_norm_eps A: str = embedding_size A: Optional[int] = head_ratio A: List[Any] = conv_kernel_size A: List[Any] = num_groups A: Optional[int] = classifier_dropout class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' @property def _snake_case ( self : Optional[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": A: Tuple = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: A: List[str] = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis), ] )
319
'''simple docstring''' import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt'''} UpperCamelCase = { '''vocab_file''': { '''allenai/longformer-base-4096''': '''https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json''', '''allenai/longformer-large-4096''': ( '''https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json''' ), '''allenai/longformer-large-4096-finetuned-triviaqa''': ( '''https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json''' ), '''allenai/longformer-base-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json''' ), '''allenai/longformer-large-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json''' ), }, '''merges_file''': { '''allenai/longformer-base-4096''': '''https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt''', '''allenai/longformer-large-4096''': ( '''https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt''' ), '''allenai/longformer-large-4096-finetuned-triviaqa''': ( '''https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt''' ), '''allenai/longformer-base-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt''' ), '''allenai/longformer-large-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt''' ), }, } UpperCamelCase = { '''allenai/longformer-base-4096''': 4096, '''allenai/longformer-large-4096''': 4096, '''allenai/longformer-large-4096-finetuned-triviaqa''': 4096, '''allenai/longformer-base-4096-extra.pos.embd.only''': 4096, '''allenai/longformer-large-4096-extra.pos.embd.only''': 4096, } @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def SCREAMING_SNAKE_CASE( ) -> Dict: A: Dict = ( list(range(ord('''!''' ) , ord('''~''' ) + 1 ) ) + list(range(ord('''¡''' ) , ord('''¬''' ) + 1 ) ) + list(range(ord('''®''' ) , ord('''ÿ''' ) + 1 ) ) ) A: Union[str, Any] = bs[:] A: List[str] = 0 for b in range(2**8 ): if b not in bs: bs.append(__lowercase ) cs.append(2**8 + n ) n += 1 A: List[Any] = [chr(__lowercase ) for n in cs] return dict(zip(__lowercase , __lowercase ) ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: A: Optional[Any] = set() A: Tuple = word[0] for char in word[1:]: pairs.add((prev_char, char) ) A: List[Any] = char return pairs class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : int = VOCAB_FILES_NAMES UpperCamelCase_ : int = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : int = ["""input_ids""", """attention_mask"""] def __init__( self : int , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : str="replace" , SCREAMING_SNAKE_CASE_ : str="<s>" , SCREAMING_SNAKE_CASE_ : Any="</s>" , SCREAMING_SNAKE_CASE_ : int="</s>" , SCREAMING_SNAKE_CASE_ : List[Any]="<s>" , SCREAMING_SNAKE_CASE_ : str="<unk>" , SCREAMING_SNAKE_CASE_ : Dict="<pad>" , SCREAMING_SNAKE_CASE_ : Dict="<mask>" , SCREAMING_SNAKE_CASE_ : Union[str, Any]=False , **SCREAMING_SNAKE_CASE_ : Tuple , ) -> List[str]: '''simple docstring''' A: int = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else bos_token A: Dict = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else eos_token A: int = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else sep_token A: Dict = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else cls_token A: Any = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else unk_token A: str = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it A: Dict = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else mask_token super().__init__( errors=SCREAMING_SNAKE_CASE_ , bos_token=SCREAMING_SNAKE_CASE_ , eos_token=SCREAMING_SNAKE_CASE_ , unk_token=SCREAMING_SNAKE_CASE_ , sep_token=SCREAMING_SNAKE_CASE_ , cls_token=SCREAMING_SNAKE_CASE_ , pad_token=SCREAMING_SNAKE_CASE_ , mask_token=SCREAMING_SNAKE_CASE_ , add_prefix_space=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) with open(SCREAMING_SNAKE_CASE_ , encoding='''utf-8''' ) as vocab_handle: A: str = json.load(SCREAMING_SNAKE_CASE_ ) A: str = {v: k for k, v in self.encoder.items()} A: Union[str, Any] = errors # how to handle errors in decoding A: Optional[int] = bytes_to_unicode() A: Union[str, Any] = {v: k for k, v in self.byte_encoder.items()} with open(SCREAMING_SNAKE_CASE_ , encoding='''utf-8''' ) as merges_handle: A: int = merges_handle.read().split('''\n''' )[1:-1] A: str = [tuple(merge.split() ) for merge in bpe_merges] A: Any = dict(zip(SCREAMING_SNAKE_CASE_ , range(len(SCREAMING_SNAKE_CASE_ ) ) ) ) A: Union[str, Any] = {} A: Tuple = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions A: Dict = re.compile(R'''\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+''' ) @property def _snake_case ( self : int ) -> List[Any]: '''simple docstring''' return len(self.encoder ) def _snake_case ( self : Optional[Any] ) -> int: '''simple docstring''' return dict(self.encoder , **self.added_tokens_encoder ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[Any]: '''simple docstring''' if token in self.cache: return self.cache[token] A: str = tuple(SCREAMING_SNAKE_CASE_ ) A: str = get_pairs(SCREAMING_SNAKE_CASE_ ) if not pairs: return token while True: A: Dict = min(SCREAMING_SNAKE_CASE_ , key=lambda SCREAMING_SNAKE_CASE_ : self.bpe_ranks.get(SCREAMING_SNAKE_CASE_ , float('''inf''' ) ) ) if bigram not in self.bpe_ranks: break A , A: Optional[Any] = bigram A: Tuple = [] A: List[Any] = 0 while i < len(SCREAMING_SNAKE_CASE_ ): try: A: Union[str, Any] = word.index(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) A: int = j if word[i] == first and i < len(SCREAMING_SNAKE_CASE_ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 A: Optional[Any] = tuple(SCREAMING_SNAKE_CASE_ ) A: Any = new_word if len(SCREAMING_SNAKE_CASE_ ) == 1: break else: A: Union[str, Any] = get_pairs(SCREAMING_SNAKE_CASE_ ) A: str = ''' '''.join(SCREAMING_SNAKE_CASE_ ) A: str = word return word def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Optional[int]: '''simple docstring''' A: Dict = [] for token in re.findall(self.pat , SCREAMING_SNAKE_CASE_ ): A: Tuple = ''''''.join( self.byte_encoder[b] for b in token.encode('''utf-8''' ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(SCREAMING_SNAKE_CASE_ ).split(''' ''' ) ) return bpe_tokens def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Optional[Any]: '''simple docstring''' return self.encoder.get(SCREAMING_SNAKE_CASE_ , self.encoder.get(self.unk_token ) ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> str: '''simple docstring''' return self.decoder.get(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Tuple: '''simple docstring''' A: Optional[int] = ''''''.join(SCREAMING_SNAKE_CASE_ ) A: Tuple = bytearray([self.byte_decoder[c] for c in text] ).decode('''utf-8''' , errors=self.errors ) return text def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(SCREAMING_SNAKE_CASE_ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return A: Union[str, Any] = os.path.join( SCREAMING_SNAKE_CASE_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) A: int = os.path.join( SCREAMING_SNAKE_CASE_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file'''] ) with open(SCREAMING_SNAKE_CASE_ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=SCREAMING_SNAKE_CASE_ , ensure_ascii=SCREAMING_SNAKE_CASE_ ) + '''\n''' ) A: Any = 0 with open(SCREAMING_SNAKE_CASE_ , '''w''' , encoding='''utf-8''' ) as writer: writer.write('''#version: 0.2\n''' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda SCREAMING_SNAKE_CASE_ : kv[1] ): if index != token_index: logger.warning( f"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.""" ''' Please check that the tokenizer is not corrupted!''' ) A: Union[str, Any] = token_index writer.write(''' '''.join(SCREAMING_SNAKE_CASE_ ) + '''\n''' ) index += 1 return vocab_file, merge_file def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] A: int = [self.cls_token_id] A: str = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None , SCREAMING_SNAKE_CASE_ : bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=SCREAMING_SNAKE_CASE_ , token_ids_a=SCREAMING_SNAKE_CASE_ , already_has_special_tokens=SCREAMING_SNAKE_CASE_ ) if token_ids_a is None: return [1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1] return [1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1, 1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1] def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' A: Dict = [self.sep_token_id] A: Optional[Any] = [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 + sep + token_ids_a + sep ) * [0] def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Dict=False , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> int: '''simple docstring''' A: Tuple = kwargs.pop('''add_prefix_space''' , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(SCREAMING_SNAKE_CASE_ ) > 0 and not text[0].isspace()): A: List[Any] = ''' ''' + text return (text, kwargs)
319
1
'''simple docstring''' import argparse import collections import os import re from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_table.py UpperCamelCase = '''src/transformers''' UpperCamelCase = '''docs/source/en''' UpperCamelCase = '''.''' def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Tuple: with open(__lowercase , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: A: Optional[int] = f.readlines() # Find the start prompt. A: Optional[int] = 0 while not lines[start_index].startswith(__lowercase ): start_index += 1 start_index += 1 A: Optional[int] = start_index while not lines[end_index].startswith(__lowercase ): end_index += 1 end_index -= 1 while len(lines[start_index] ) <= 1: start_index += 1 while len(lines[end_index] ) <= 1: end_index -= 1 end_index += 1 return "".join(lines[start_index:end_index] ), start_index, end_index, lines # Add here suffixes that are used to identify models, separated by | UpperCamelCase = '''Model|Encoder|Decoder|ForConditionalGeneration''' # Regexes that match TF/Flax/PT model names. UpperCamelCase = re.compile(R'''TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)''') UpperCamelCase = re.compile(R'''Flax(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)''') # Will match any TF or Flax model too so need to be in an else branch afterthe two previous regexes. UpperCamelCase = re.compile(R'''(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)''') # This is to make sure the transformers module imported is the one in the repo. UpperCamelCase = direct_transformers_import(TRANSFORMERS_PATH) def SCREAMING_SNAKE_CASE( __lowercase ) -> List[Any]: A: int = re.finditer('''.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)''' , __lowercase ) return [m.group(0 ) for m in matches] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[str]: A: Optional[int] = 2 if text == '''✅''' or text == '''❌''' else len(__lowercase ) A: Union[str, Any] = (width - text_length) // 2 A: Optional[int] = width - text_length - left_indent return " " * left_indent + text + " " * right_indent def SCREAMING_SNAKE_CASE( ) -> Dict: A: Dict = transformers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES A: Tuple = { name: config_maping_names[code] for code, name in transformers_module.MODEL_NAMES_MAPPING.items() if code in config_maping_names } A: Dict = {name: config.replace('''Config''' , '''''' ) for name, config in model_name_to_config.items()} # Dictionaries flagging if each model prefix has a slow/fast tokenizer, backend in PT/TF/Flax. A: List[str] = collections.defaultdict(__lowercase ) A: str = collections.defaultdict(__lowercase ) A: str = collections.defaultdict(__lowercase ) A: Optional[int] = collections.defaultdict(__lowercase ) A: Dict = collections.defaultdict(__lowercase ) # Let's lookup through all transformers object (once). for attr_name in dir(__lowercase ): A: List[Any] = None if attr_name.endswith('''Tokenizer''' ): A: Tuple = slow_tokenizers A: int = attr_name[:-9] elif attr_name.endswith('''TokenizerFast''' ): A: Any = fast_tokenizers A: Optional[int] = attr_name[:-1_3] elif _re_tf_models.match(__lowercase ) is not None: A: Dict = tf_models A: Any = _re_tf_models.match(__lowercase ).groups()[0] elif _re_flax_models.match(__lowercase ) is not None: A: int = flax_models A: int = _re_flax_models.match(__lowercase ).groups()[0] elif _re_pt_models.match(__lowercase ) is not None: A: Union[str, Any] = pt_models A: Optional[Any] = _re_pt_models.match(__lowercase ).groups()[0] if lookup_dict is not None: while len(__lowercase ) > 0: if attr_name in model_name_to_prefix.values(): A: Union[str, Any] = True break # Try again after removing the last word in the name A: int = ''''''.join(camel_case_split(__lowercase )[:-1] ) # Let's build that table! A: Optional[int] = list(model_name_to_config.keys() ) model_names.sort(key=str.lower ) A: Dict = ['''Model''', '''Tokenizer slow''', '''Tokenizer fast''', '''PyTorch support''', '''TensorFlow support''', '''Flax Support'''] # We'll need widths to properly display everything in the center (+2 is to leave one extra space on each side). A: List[Any] = [len(__lowercase ) + 2 for c in columns] A: Optional[int] = max([len(__lowercase ) for name in model_names] ) + 2 # Build the table per se A: Tuple = '''|''' + '''|'''.join([_center_text(__lowercase , __lowercase ) for c, w in zip(__lowercase , __lowercase )] ) + '''|\n''' # Use ":-----:" format to center-aligned table cell texts table += "|" + "|".join([''':''' + '''-''' * (w - 2) + ''':''' for w in widths] ) + "|\n" A: Tuple = {True: '''✅''', False: '''❌'''} for name in model_names: A: str = model_name_to_prefix[name] A: List[str] = [ name, check[slow_tokenizers[prefix]], check[fast_tokenizers[prefix]], check[pt_models[prefix]], check[tf_models[prefix]], check[flax_models[prefix]], ] table += "|" + "|".join([_center_text(__lowercase , __lowercase ) for l, w in zip(__lowercase , __lowercase )] ) + "|\n" return table def SCREAMING_SNAKE_CASE( __lowercase=False ) -> Union[str, Any]: A , A , A , A: str = _find_text_in_file( filename=os.path.join(__lowercase , '''index.md''' ) , start_prompt='''<!--This table is updated automatically from the auto modules''' , end_prompt='''<!-- End table-->''' , ) A: Optional[int] = get_model_table_from_auto_modules() if current_table != new_table: if overwrite: with open(os.path.join(__lowercase , '''index.md''' ) , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.writelines(lines[:start_index] + [new_table] + lines[end_index:] ) else: raise ValueError( '''The model table in the `index.md` has not been updated. Run `make fix-copies` to fix this.''' ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') UpperCamelCase = parser.parse_args() check_model_table(args.fix_and_overwrite)
319
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> int: if not isinstance(__lowercase , __lowercase ): raise TypeError('''only integers accepted as input''' ) else: A: str = str(abs(__lowercase ) ) A: int = [list(__lowercase ) for char in range(len(__lowercase ) )] for index in range(len(__lowercase ) ): num_transpositions[index].pop(__lowercase ) return max( int(''''''.join(list(__lowercase ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__('''doctest''').testmod()
319
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, is_vision_available, ) UpperCamelCase = { '''configuration_clip''': [ '''CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''CLIPConfig''', '''CLIPOnnxConfig''', '''CLIPTextConfig''', '''CLIPVisionConfig''', ], '''processing_clip''': ['''CLIPProcessor'''], '''tokenization_clip''': ['''CLIPTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''CLIPTokenizerFast'''] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''CLIPFeatureExtractor'''] UpperCamelCase = ['''CLIPImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''CLIPModel''', '''CLIPPreTrainedModel''', '''CLIPTextModel''', '''CLIPTextModelWithProjection''', '''CLIPVisionModel''', '''CLIPVisionModelWithProjection''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFCLIPModel''', '''TFCLIPPreTrainedModel''', '''TFCLIPTextModel''', '''TFCLIPVisionModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''FlaxCLIPModel''', '''FlaxCLIPPreTrainedModel''', '''FlaxCLIPTextModel''', '''FlaxCLIPTextPreTrainedModel''', '''FlaxCLIPVisionModel''', '''FlaxCLIPVisionPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_clip import ( CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPConfig, CLIPOnnxConfig, CLIPTextConfig, CLIPVisionConfig, ) from .processing_clip import CLIPProcessor from .tokenization_clip import CLIPTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_clip_fast import CLIPTokenizerFast try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_clip import CLIPFeatureExtractor from .image_processing_clip import CLIPImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clip import ( CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPModel, CLIPPreTrainedModel, CLIPTextModel, CLIPTextModelWithProjection, CLIPVisionModel, CLIPVisionModelWithProjection, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_clip import ( TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFCLIPModel, TFCLIPPreTrainedModel, TFCLIPTextModel, TFCLIPVisionModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_clip import ( FlaxCLIPModel, FlaxCLIPPreTrainedModel, FlaxCLIPTextModel, FlaxCLIPTextPreTrainedModel, FlaxCLIPVisionModel, FlaxCLIPVisionPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
'''simple docstring''' from __future__ import annotations import math def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list: if len(__lowercase ) != 2 or len(a[0] ) != 2 or len(__lowercase ) != 2 or len(b[0] ) != 2: raise Exception('''Matrices are not 2x2''' ) A: str = [ [a[0][0] * b[0][0] + a[0][1] * b[1][0], a[0][0] * b[0][1] + a[0][1] * b[1][1]], [a[1][0] * b[0][0] + a[1][1] * b[1][0], a[1][0] * b[0][1] + a[1][1] * b[1][1]], ] return new_matrix def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: return [ [matrix_a[row][col] + matrix_b[row][col] for col in range(len(matrix_a[row] ) )] for row in range(len(__lowercase ) ) ] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: return [ [matrix_a[row][col] - matrix_b[row][col] for col in range(len(matrix_a[row] ) )] for row in range(len(__lowercase ) ) ] def SCREAMING_SNAKE_CASE( __lowercase ) -> tuple[list, list, list, list]: if len(__lowercase ) % 2 != 0 or len(a[0] ) % 2 != 0: raise Exception('''Odd matrices are not supported!''' ) A: Union[str, Any] = len(__lowercase ) A: str = matrix_length // 2 A: Optional[int] = [[a[i][j] for j in range(__lowercase , __lowercase )] for i in range(__lowercase )] A: Optional[Any] = [ [a[i][j] for j in range(__lowercase , __lowercase )] for i in range(__lowercase , __lowercase ) ] A: Union[str, Any] = [[a[i][j] for j in range(__lowercase )] for i in range(__lowercase )] A: int = [[a[i][j] for j in range(__lowercase )] for i in range(__lowercase , __lowercase )] return top_left, top_right, bot_left, bot_right def SCREAMING_SNAKE_CASE( __lowercase ) -> tuple[int, int]: return len(__lowercase ), len(matrix[0] ) def SCREAMING_SNAKE_CASE( __lowercase ) -> None: print('''\n'''.join(str(__lowercase ) for line in matrix ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list: if matrix_dimensions(__lowercase ) == (2, 2): return default_matrix_multiplication(__lowercase , __lowercase ) A , A , A , A: Union[str, Any] = split_matrix(__lowercase ) A , A , A , A: List[Any] = split_matrix(__lowercase ) A: Optional[int] = actual_strassen(__lowercase , matrix_subtraction(__lowercase , __lowercase ) ) A: Any = actual_strassen(matrix_addition(__lowercase , __lowercase ) , __lowercase ) A: Tuple = actual_strassen(matrix_addition(__lowercase , __lowercase ) , __lowercase ) A: Optional[int] = actual_strassen(__lowercase , matrix_subtraction(__lowercase , __lowercase ) ) A: Tuple = actual_strassen(matrix_addition(__lowercase , __lowercase ) , matrix_addition(__lowercase , __lowercase ) ) A: Union[str, Any] = actual_strassen(matrix_subtraction(__lowercase , __lowercase ) , matrix_addition(__lowercase , __lowercase ) ) A: List[str] = actual_strassen(matrix_subtraction(__lowercase , __lowercase ) , matrix_addition(__lowercase , __lowercase ) ) A: int = matrix_addition(matrix_subtraction(matrix_addition(__lowercase , __lowercase ) , __lowercase ) , __lowercase ) A: Any = matrix_addition(__lowercase , __lowercase ) A: List[Any] = matrix_addition(__lowercase , __lowercase ) A: List[str] = matrix_subtraction(matrix_subtraction(matrix_addition(__lowercase , __lowercase ) , __lowercase ) , __lowercase ) # construct the new matrix from our 4 quadrants A: Union[str, Any] = [] for i in range(len(__lowercase ) ): new_matrix.append(top_left[i] + top_right[i] ) for i in range(len(__lowercase ) ): new_matrix.append(bot_left[i] + bot_right[i] ) return new_matrix def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list: if matrix_dimensions(__lowercase )[1] != matrix_dimensions(__lowercase )[0]: A: int = ( '''Unable to multiply these matrices, please check the dimensions.\n''' F"""Matrix A: {matrixa}\n""" F"""Matrix B: {matrixa}""" ) raise Exception(__lowercase ) A: str = matrix_dimensions(__lowercase ) A: str = matrix_dimensions(__lowercase ) if dimensiona[0] == dimensiona[1] and dimensiona[0] == dimensiona[1]: return [matrixa, matrixa] A: Union[str, Any] = max(*__lowercase , *__lowercase ) A: Optional[int] = int(math.pow(2 , math.ceil(math.loga(__lowercase ) ) ) ) A: List[Any] = matrixa A: Tuple = matrixa # Adding zeros to the matrices so that the arrays dimensions are the same and also # power of 2 for i in range(0 , __lowercase ): if i < dimensiona[0]: for _ in range(dimensiona[1] , __lowercase ): new_matrixa[i].append(0 ) else: new_matrixa.append([0] * maxim ) if i < dimensiona[0]: for _ in range(dimensiona[1] , __lowercase ): new_matrixa[i].append(0 ) else: new_matrixa.append([0] * maxim ) A: Any = actual_strassen(__lowercase , __lowercase ) # Removing the additional zeros for i in range(0 , __lowercase ): if i < dimensiona[0]: for _ in range(dimensiona[1] , __lowercase ): final_matrix[i].pop() else: final_matrix.pop() return final_matrix if __name__ == "__main__": UpperCamelCase = [ [2, 3, 4, 5], [6, 4, 3, 1], [2, 3, 6, 7], [3, 1, 2, 4], [2, 3, 4, 5], [6, 4, 3, 1], [2, 3, 6, 7], [3, 1, 2, 4], [2, 3, 4, 5], [6, 2, 3, 1], ] UpperCamelCase = [[0, 2, 1, 1], [16, 2, 3, 3], [2, 2, 7, 7], [13, 11, 22, 4]] print(strassen(matrixa, matrixa))
319
1
'''simple docstring''' import json import os import re import shutil import tempfile import unittest from typing import Tuple from transformers import AddedToken, BatchEncoding, ByTaTokenizer from transformers.utils import cached_property, is_tf_available, is_torch_available from ...test_tokenization_common import TokenizerTesterMixin if is_torch_available(): UpperCamelCase = '''pt''' elif is_tf_available(): UpperCamelCase = '''tf''' else: UpperCamelCase = '''jax''' class lowerCAmelCase_ ( UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = ByTaTokenizer UpperCamelCase_ : List[str] = False def _snake_case ( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' super().setUp() A: int = ByTaTokenizer() tokenizer.save_pretrained(self.tmpdirname ) @cached_property def _snake_case ( self : int ) -> str: '''simple docstring''' return ByTaTokenizer.from_pretrained('''google/byt5-small''' ) def _snake_case ( self : Any , **SCREAMING_SNAKE_CASE_ : int ) -> ByTaTokenizer: '''simple docstring''' return self.tokenizer_class.from_pretrained(self.tmpdirname , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Dict=False , SCREAMING_SNAKE_CASE_ : Tuple=20 , SCREAMING_SNAKE_CASE_ : Optional[int]=5 ) -> Tuple[str, list]: '''simple docstring''' A: Dict = [] for i in range(len(SCREAMING_SNAKE_CASE_ ) ): try: A: str = tokenizer.decode([i] , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) except UnicodeDecodeError: pass toks.append((i, tok) ) A: Tuple = list(filter(lambda SCREAMING_SNAKE_CASE_ : re.match(R'''^[ a-zA-Z]+$''' , t[1] ) , SCREAMING_SNAKE_CASE_ ) ) A: Optional[int] = list(filter(lambda SCREAMING_SNAKE_CASE_ : [t[0]] == tokenizer.encode(t[1] , add_special_tokens=SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) ) if max_length is not None and len(SCREAMING_SNAKE_CASE_ ) > max_length: A: Optional[Any] = toks[:max_length] if min_length is not None and len(SCREAMING_SNAKE_CASE_ ) < min_length and len(SCREAMING_SNAKE_CASE_ ) > 0: while len(SCREAMING_SNAKE_CASE_ ) < min_length: A: str = toks + toks # toks_str = [t[1] for t in toks] A: Tuple = [t[0] for t in toks] # Ensure consistency A: Tuple = tokenizer.decode(SCREAMING_SNAKE_CASE_ , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) if " " not in output_txt and len(SCREAMING_SNAKE_CASE_ ) > 1: A: Tuple = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) + ''' ''' + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) ) if with_prefix_space: A: str = ''' ''' + output_txt A: List[str] = tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) return output_txt, output_ids def _snake_case ( self : Any ) -> Union[str, Any]: '''simple docstring''' A: Tuple = self.ta_base_tokenizer A: List[str] = tokenizer(['''hi</s>''', '''I went to the gym</s>''', '''</s>'''] ) A: str = tokenizer(['''hi''', '''I went to the gym''', ''''''] ) self.assertListEqual(batch_with_eos_added['''input_ids'''] , batch_without_eos_added['''input_ids'''] ) def _snake_case ( self : Optional[Any] ) -> int: '''simple docstring''' A: Optional[int] = self.ta_base_tokenizer A: List[Any] = '''Unicode €.''' A: Any = tokenizer(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = [88, 1_13, 1_08, 1_02, 1_14, 1_03, 1_04, 35, 2_29, 1_33, 1_75, 49, 1] self.assertEqual(encoded['''input_ids'''] , SCREAMING_SNAKE_CASE_ ) # decoding A: List[str] = tokenizer.decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''Unicode €.</s>''' ) A: Optional[int] = tokenizer('''e è é ê ë''' ) A: List[Any] = [1_04, 35, 1_98, 1_71, 35, 1_98, 1_72, 35, 1_98, 1_73, 35, 1_98, 1_74, 1] self.assertEqual(encoded['''input_ids'''] , SCREAMING_SNAKE_CASE_ ) # decoding A: Optional[int] = tokenizer.decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''e è é ê ë</s>''' ) # encode/decode, but with `encode` instead of `__call__` self.assertEqual(tokenizer.decode(tokenizer.encode('''e è é ê ë''' ) ) , '''e è é ê ë</s>''' ) def _snake_case ( self : List[Any] ) -> List[str]: '''simple docstring''' A: Union[str, Any] = self.ta_base_tokenizer A: int = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] # fmt: off A: int = [68, 35, 1_11, 1_14, 1_13, 1_06, 35, 1_15, 1_00, 1_17, 1_00, 1_06, 1_17, 1_00, 1_15, 1_07, 35, 1_05, 1_14, 1_17, 35, 1_18, 1_20, 1_12, 1_12, 1_00, 1_17, 1_08, 1_25, 1_00, 1_19, 1_08, 1_14, 1_13, 49, 1, 0] # fmt: on A: int = tokenizer(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if FRAMEWORK != "jax": A: Optional[int] = list(batch.input_ids.numpy()[0] ) else: A: Optional[int] = list(batch.input_ids.tolist()[0] ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertEqual((2, 37) , batch.input_ids.shape ) self.assertEqual((2, 37) , batch.attention_mask.shape ) def _snake_case ( self : Dict ) -> List[Any]: '''simple docstring''' A: Optional[Any] = self.ta_base_tokenizer A: int = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] A: str = tokenizer(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ ) # check if input_ids are returned and no decoder_input_ids self.assertIn('''input_ids''' , SCREAMING_SNAKE_CASE_ ) self.assertIn('''attention_mask''' , SCREAMING_SNAKE_CASE_ ) self.assertNotIn('''decoder_input_ids''' , SCREAMING_SNAKE_CASE_ ) self.assertNotIn('''decoder_attention_mask''' , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Dict ) -> Dict: '''simple docstring''' A: Dict = self.ta_base_tokenizer A: Any = [ '''Summary of the text.''', '''Another summary.''', ] A: Any = tokenizer( text_target=SCREAMING_SNAKE_CASE_ , max_length=32 , padding='''max_length''' , truncation=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ ) self.assertEqual(32 , targets['''input_ids'''].shape[1] ) def _snake_case ( self : str ) -> List[str]: '''simple docstring''' A: Optional[Any] = self.ta_base_tokenizer A: Dict = ['''A long paragraph for summarization. </s>'''] A: Tuple = ['''Summary of the text. </s>'''] # fmt: off A: Tuple = [68, 35, 1_11, 1_14, 1_13, 1_06, 35, 1_15, 1_00, 1_17, 1_00, 1_06, 1_17, 1_00, 1_15, 1_07, 35, 1_05, 1_14, 1_17, 35, 1_18, 1_20, 1_12, 1_12, 1_00, 1_17, 1_08, 1_25, 1_00, 1_19, 1_08, 1_14, 1_13, 49, 35, 1] A: Optional[Any] = [86, 1_20, 1_12, 1_12, 1_00, 1_17, 1_24, 35, 1_14, 1_05, 35, 1_19, 1_07, 1_04, 35, 1_19, 1_04, 1_23, 1_19, 49, 35, 1] # fmt: on A: Union[str, Any] = tokenizer(SCREAMING_SNAKE_CASE_ , text_target=SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , batch['''input_ids'''][0] ) self.assertEqual(SCREAMING_SNAKE_CASE_ , batch['''labels'''][0] ) def _snake_case ( self : Optional[Any] ) -> List[Any]: '''simple docstring''' A: Any = 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: Optional[Any] = 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[Any] = tempfile.mkdtemp() A: List[Any] = ''' He is very happy, UNwant\u00E9d,running''' A: Any = tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) tokenizer.save_pretrained(SCREAMING_SNAKE_CASE_ ) A: List[str] = tokenizer.__class__.from_pretrained(SCREAMING_SNAKE_CASE_ ) A: List[Any] = after_tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) shutil.rmtree(SCREAMING_SNAKE_CASE_ ) A: int = 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: Tuple = tempfile.mkdtemp() A: Optional[int] = ''' He is very happy, UNwant\u00E9d,running''' tokenizer.add_tokens(['''bim''', '''bambam'''] ) A: Any = tokenizer.additional_special_tokens additional_special_tokens.append('''new_additional_special_token''' ) tokenizer.add_special_tokens({'''additional_special_tokens''': additional_special_tokens} ) A: str = tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) tokenizer.save_pretrained(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = tokenizer.__class__.from_pretrained(SCREAMING_SNAKE_CASE_ ) A: Tuple = after_tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertIn('''new_additional_special_token''' , after_tokenizer.additional_special_tokens ) self.assertEqual(after_tokenizer.model_max_length , 42 ) A: Optional[int] = tokenizer.__class__.from_pretrained(SCREAMING_SNAKE_CASE_ , model_max_length=43 ) self.assertEqual(tokenizer.model_max_length , 43 ) shutil.rmtree(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int ) -> List[str]: '''simple docstring''' A: List[str] = [] 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(SCREAMING_SNAKE_CASE_ ) with open(os.path.join(SCREAMING_SNAKE_CASE_ , '''special_tokens_map.json''' ) , encoding='''utf-8''' ) as json_file: A: Any = json.load(SCREAMING_SNAKE_CASE_ ) with open(os.path.join(SCREAMING_SNAKE_CASE_ , '''tokenizer_config.json''' ) , encoding='''utf-8''' ) as json_file: A: Union[str, Any] = json.load(SCREAMING_SNAKE_CASE_ ) A: Dict = [f"""<extra_id_{i}>""" for i in range(1_25 )] A: Optional[int] = added_tokens_extra_ids + [ '''an_additional_special_token''' ] A: List[Any] = added_tokens_extra_ids + [ '''an_additional_special_token''' ] with open(os.path.join(SCREAMING_SNAKE_CASE_ , '''special_tokens_map.json''' ) , '''w''' , encoding='''utf-8''' ) as outfile: json.dump(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) with open(os.path.join(SCREAMING_SNAKE_CASE_ , '''tokenizer_config.json''' ) , '''w''' , encoding='''utf-8''' ) as outfile: json.dump(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # 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( SCREAMING_SNAKE_CASE_ , ) self.assertIn( '''an_additional_special_token''' , 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( ['''an_additional_special_token'''] , tokenizer_without_change_in_init.convert_ids_to_tokens( tokenizer_without_change_in_init.convert_tokens_to_ids(['''an_additional_special_token'''] ) ) , ) # Now we test that we can change the value of additional_special_tokens in the from_pretrained A: Tuple = added_tokens_extra_ids + [AddedToken('''a_new_additional_special_token''' , lstrip=SCREAMING_SNAKE_CASE_ )] A: str = tokenizer_class.from_pretrained( SCREAMING_SNAKE_CASE_ , additional_special_tokens=SCREAMING_SNAKE_CASE_ , ) self.assertIn('''a_new_additional_special_token''' , tokenizer.additional_special_tokens ) self.assertEqual( ['''a_new_additional_special_token'''] , tokenizer.convert_ids_to_tokens( tokenizer.convert_tokens_to_ids(['''a_new_additional_special_token'''] ) ) , ) def _snake_case ( self : Optional[int] ) -> List[Any]: '''simple docstring''' A: Tuple = [] 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(SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokenizer_class.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertTrue(tokenizer.decode([2_55] ) == '''''' ) def _snake_case ( self : int ) -> Optional[int]: '''simple docstring''' pass def _snake_case ( self : Optional[int] ) -> List[str]: '''simple docstring''' pass def _snake_case ( self : List[str] ) -> Any: '''simple docstring''' pass def _snake_case ( self : List[Any] ) -> List[Any]: '''simple docstring''' pass def _snake_case ( self : Union[str, Any] ) -> Tuple: '''simple docstring''' A: List[str] = self.get_tokenizers(fast=SCREAMING_SNAKE_CASE_ , do_lower_case=SCREAMING_SNAKE_CASE_ ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): A: List[str] = ['''t''', '''h''', '''i''', '''s''', ''' ''', '''i''', '''s''', ''' ''', '''a''', ''' ''', '''t''', '''e''', '''x''', '''t''', '''</s>'''] A: int = tokenizer.convert_tokens_to_string(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] ) -> Tuple: '''simple docstring''' A: Tuple = 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 = 0 A: List[Any] = tokenizer.convert_ids_to_tokens( SCREAMING_SNAKE_CASE_ , skip_special_tokens=SCREAMING_SNAKE_CASE_ ) for attr in attributes_list: setattr(SCREAMING_SNAKE_CASE_ , attr + '''_id''' , SCREAMING_SNAKE_CASE_ ) self.assertEqual(getattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) self.assertEqual(getattr(SCREAMING_SNAKE_CASE_ , attr + '''_id''' ) , SCREAMING_SNAKE_CASE_ ) setattr(SCREAMING_SNAKE_CASE_ , attr + '''_id''' , SCREAMING_SNAKE_CASE_ ) self.assertEqual(getattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) self.assertEqual(getattr(SCREAMING_SNAKE_CASE_ , attr + '''_id''' ) , SCREAMING_SNAKE_CASE_ ) setattr(SCREAMING_SNAKE_CASE_ , '''additional_special_tokens_ids''' , [] ) self.assertListEqual(getattr(SCREAMING_SNAKE_CASE_ , '''additional_special_tokens''' ) , [] ) self.assertListEqual(getattr(SCREAMING_SNAKE_CASE_ , '''additional_special_tokens_ids''' ) , [] ) setattr(SCREAMING_SNAKE_CASE_ , '''additional_special_tokens_ids''' , [token_id_to_test_setters] ) self.assertListEqual(getattr(SCREAMING_SNAKE_CASE_ , '''additional_special_tokens''' ) , [token_to_test_setters] ) self.assertListEqual(getattr(SCREAMING_SNAKE_CASE_ , '''additional_special_tokens_ids''' ) , [token_id_to_test_setters] )
319
'''simple docstring''' from dataclasses import dataclass from typing import Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin @dataclass class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : torch.FloatTensor UpperCamelCase_ : torch.FloatTensor UpperCamelCase_ : Optional[torch.FloatTensor] = None class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = 2 @register_to_config def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : float = 0.02 , SCREAMING_SNAKE_CASE_ : float = 1_00 , SCREAMING_SNAKE_CASE_ : float = 1.007 , SCREAMING_SNAKE_CASE_ : float = 80 , SCREAMING_SNAKE_CASE_ : float = 0.05 , SCREAMING_SNAKE_CASE_ : float = 50 , ) -> Optional[int]: '''simple docstring''' A: Union[str, Any] = sigma_max # setable values A: int = None A: np.IntTensor = None A: torch.FloatTensor = None # sigma(t_i) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : Optional[int] = None ) -> torch.FloatTensor: '''simple docstring''' return sample def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Union[str, torch.device] = None ) -> Optional[Any]: '''simple docstring''' A: List[Any] = num_inference_steps A: List[str] = np.arange(0 , self.num_inference_steps )[::-1].copy() A: Any = torch.from_numpy(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ) A: str = [ ( self.config.sigma_max**2 * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1)) ) for i in self.timesteps ] A: Tuple = torch.tensor(SCREAMING_SNAKE_CASE_ , dtype=torch.floataa , device=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : Optional[torch.Generator] = None ) -> Tuple[torch.FloatTensor, float]: '''simple docstring''' if self.config.s_min <= sigma <= self.config.s_max: A: str = min(self.config.s_churn / self.num_inference_steps , 2**0.5 - 1 ) else: A: List[str] = 0 # sample eps ~ N(0, S_noise^2 * I) A: Optional[Any] = self.config.s_noise * randn_tensor(sample.shape , generator=SCREAMING_SNAKE_CASE_ ).to(sample.device ) A: Optional[Any] = sigma + gamma * sigma A: List[Any] = sample + ((sigma_hat**2 - sigma**2) ** 0.5 * eps) return sample_hat, sigma_hat def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : bool = True , ) -> Union[KarrasVeOutput, Tuple]: '''simple docstring''' A: Union[str, Any] = sample_hat + sigma_hat * model_output A: str = (sample_hat - pred_original_sample) / sigma_hat A: Optional[int] = sample_hat + (sigma_prev - sigma_hat) * derivative if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=SCREAMING_SNAKE_CASE_ , derivative=SCREAMING_SNAKE_CASE_ , pred_original_sample=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : bool = True , ) -> Union[KarrasVeOutput, Tuple]: '''simple docstring''' A: int = sample_prev + sigma_prev * model_output A: List[Any] = (sample_prev - pred_original_sample) / sigma_prev A: Dict = sample_hat + (sigma_prev - sigma_hat) * (0.5 * derivative + 0.5 * derivative_corr) if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=SCREAMING_SNAKE_CASE_ , derivative=SCREAMING_SNAKE_CASE_ , pred_original_sample=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : str ) -> Dict: '''simple docstring''' raise NotImplementedError()
319
1
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( BertTokenizer, ViltConfig, ViltForImageAndTextRetrieval, ViltForImagesAndTextClassification, ViltForMaskedLM, ViltForQuestionAnswering, ViltImageProcessor, ViltProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=False , __lowercase=False , __lowercase=False ) -> Optional[Any]: A: str = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""transformer.blocks.{i}.norm1.weight""", F"""vilt.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.norm1.bias""", F"""vilt.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append( (F"""transformer.blocks.{i}.attn.proj.weight""", F"""vilt.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append( (F"""transformer.blocks.{i}.attn.proj.bias""", F"""vilt.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""transformer.blocks.{i}.norm2.weight""", F"""vilt.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.norm2.bias""", F"""vilt.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append( (F"""transformer.blocks.{i}.mlp.fc1.weight""", F"""vilt.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc1.bias""", F"""vilt.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc2.weight""", F"""vilt.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc2.bias""", F"""vilt.encoder.layer.{i}.output.dense.bias""") ) # embeddings rename_keys.extend( [ # text embeddings ('''text_embeddings.word_embeddings.weight''', '''vilt.embeddings.text_embeddings.word_embeddings.weight'''), ( '''text_embeddings.position_embeddings.weight''', '''vilt.embeddings.text_embeddings.position_embeddings.weight''', ), ('''text_embeddings.position_ids''', '''vilt.embeddings.text_embeddings.position_ids'''), ( '''text_embeddings.token_type_embeddings.weight''', '''vilt.embeddings.text_embeddings.token_type_embeddings.weight''', ), ('''text_embeddings.LayerNorm.weight''', '''vilt.embeddings.text_embeddings.LayerNorm.weight'''), ('''text_embeddings.LayerNorm.bias''', '''vilt.embeddings.text_embeddings.LayerNorm.bias'''), # patch embeddings ('''transformer.cls_token''', '''vilt.embeddings.cls_token'''), ('''transformer.patch_embed.proj.weight''', '''vilt.embeddings.patch_embeddings.projection.weight'''), ('''transformer.patch_embed.proj.bias''', '''vilt.embeddings.patch_embeddings.projection.bias'''), ('''transformer.pos_embed''', '''vilt.embeddings.position_embeddings'''), # token type embeddings ('''token_type_embeddings.weight''', '''vilt.embeddings.token_type_embeddings.weight'''), ] ) # final layernorm + pooler rename_keys.extend( [ ('''transformer.norm.weight''', '''vilt.layernorm.weight'''), ('''transformer.norm.bias''', '''vilt.layernorm.bias'''), ('''pooler.dense.weight''', '''vilt.pooler.dense.weight'''), ('''pooler.dense.bias''', '''vilt.pooler.dense.bias'''), ] ) # classifier head(s) if vqa_model: # classification head rename_keys.extend( [ ('''vqa_classifier.0.weight''', '''classifier.0.weight'''), ('''vqa_classifier.0.bias''', '''classifier.0.bias'''), ('''vqa_classifier.1.weight''', '''classifier.1.weight'''), ('''vqa_classifier.1.bias''', '''classifier.1.bias'''), ('''vqa_classifier.3.weight''', '''classifier.3.weight'''), ('''vqa_classifier.3.bias''', '''classifier.3.bias'''), ] ) elif nlvr_model: # classification head rename_keys.extend( [ ('''nlvr2_classifier.0.weight''', '''classifier.0.weight'''), ('''nlvr2_classifier.0.bias''', '''classifier.0.bias'''), ('''nlvr2_classifier.1.weight''', '''classifier.1.weight'''), ('''nlvr2_classifier.1.bias''', '''classifier.1.bias'''), ('''nlvr2_classifier.3.weight''', '''classifier.3.weight'''), ('''nlvr2_classifier.3.bias''', '''classifier.3.bias'''), ] ) else: pass return rename_keys def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Any: for i in range(config.num_hidden_layers ): A: Tuple = '''vilt.''' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A: List[str] = state_dict.pop(F"""transformer.blocks.{i}.attn.qkv.weight""" ) A: Optional[Any] = state_dict.pop(F"""transformer.blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict A: Dict = in_proj_weight[ : config.hidden_size, : ] A: int = in_proj_bias[: config.hidden_size] A: Any = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A: int = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A: Optional[int] = in_proj_weight[ -config.hidden_size :, : ] A: Optional[Any] = in_proj_bias[-config.hidden_size :] def SCREAMING_SNAKE_CASE( __lowercase ) -> int: A: Optional[int] = ['''head.weight''', '''head.bias'''] for k in ignore_keys: state_dict.pop(__lowercase , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: A: List[Any] = dct.pop(__lowercase ) A: int = val @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str: A: Optional[Any] = ViltConfig(image_size=3_8_4 , patch_size=3_2 , tie_word_embeddings=__lowercase ) A: Tuple = False A: str = False A: List[Any] = False A: Optional[int] = False if "vqa" in checkpoint_url: A: Union[str, Any] = True A: Union[str, Any] = 3_1_2_9 A: List[Any] = '''huggingface/label-files''' A: Any = '''vqa2-id2label.json''' A: Optional[Any] = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: Union[str, Any] = {int(__lowercase ): v for k, v in idalabel.items()} A: Any = idalabel A: Optional[Any] = {v: k for k, v in idalabel.items()} A: List[str] = ViltForQuestionAnswering(__lowercase ) elif "nlvr" in checkpoint_url: A: Dict = True A: str = 2 A: Union[str, Any] = {0: '''False''', 1: '''True'''} A: Any = {v: k for k, v in config.idalabel.items()} A: Optional[Any] = 3 A: Any = ViltForImagesAndTextClassification(__lowercase ) elif "irtr" in checkpoint_url: A: Tuple = True A: Optional[Any] = ViltForImageAndTextRetrieval(__lowercase ) elif "mlm_itm" in checkpoint_url: A: Tuple = True A: Optional[int] = ViltForMaskedLM(__lowercase ) else: raise ValueError('''Unknown model type''' ) # load state_dict of original model, remove and rename some keys A: int = torch.hub.load_state_dict_from_url(__lowercase , map_location='''cpu''' )['''state_dict'''] A: List[str] = create_rename_keys(__lowercase , __lowercase , __lowercase , __lowercase ) for src, dest in rename_keys: rename_key(__lowercase , __lowercase , __lowercase ) read_in_q_k_v(__lowercase , __lowercase ) if mlm_model or irtr_model: A: str = ['''itm_score.fc.weight''', '''itm_score.fc.bias'''] for k in ignore_keys: state_dict.pop(__lowercase , __lowercase ) # load state dict into HuggingFace model model.eval() if mlm_model: A , A: Union[str, Any] = model.load_state_dict(__lowercase , strict=__lowercase ) assert missing_keys == ["mlm_score.decoder.bias"] else: model.load_state_dict(__lowercase ) # Define processor A: Optional[Any] = ViltImageProcessor(size=3_8_4 ) A: Dict = BertTokenizer.from_pretrained('''bert-base-uncased''' ) A: Optional[int] = ViltProcessor(__lowercase , __lowercase ) # Forward pass on example inputs (image + text) if nlvr_model: A: str = Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg''' , stream=__lowercase ).raw ) A: List[str] = Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg''' , stream=__lowercase ).raw ) A: Any = ( '''The left image contains twice the number of dogs as the right image, and at least two dogs in total are''' ''' standing.''' ) A: List[Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: List[Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: List[str] = model( input_ids=encoding_a.input_ids , pixel_values=encoding_a.pixel_values , pixel_values_a=encoding_a.pixel_values , ) else: A: Any = Image.open(requests.get('''http://images.cocodataset.org/val2017/000000039769.jpg''' , stream=__lowercase ).raw ) if mlm_model: A: Optional[int] = '''a bunch of [MASK] laying on a [MASK].''' else: A: Optional[int] = '''How many cats are there?''' A: Union[str, Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: Any = model(**__lowercase ) # Verify outputs if mlm_model: A: Any = torch.Size([1, 1_1, 3_0_5_2_2] ) A: Tuple = torch.tensor([-1_2.5_0_6_1, -1_2.5_1_2_3, -1_2.5_1_7_4] ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , __lowercase , atol=1E-4 ) # verify masked token prediction equals "cats" A: List[str] = outputs.logits[0, 4, :].argmax(-1 ).item() assert tokenizer.decode([predicted_id] ) == "cats" elif vqa_model: A: Any = torch.Size([1, 3_1_2_9] ) A: Optional[int] = torch.tensor([-1_5.9_4_9_5, -1_8.1_4_7_2, -1_0.3_0_4_1] ) assert torch.allclose(outputs.logits[0, :3] , __lowercase , atol=1E-4 ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , __lowercase , atol=1E-4 ) # verify vqa prediction equals "2" A: Dict = outputs.logits.argmax(-1 ).item() assert model.config.idalabel[predicted_idx] == "2" elif nlvr_model: A: Union[str, Any] = torch.Size([1, 2] ) A: Optional[Any] = torch.tensor([-2.8_7_2_1, 2.1_2_9_1] ) assert torch.allclose(outputs.logits[0, :3] , __lowercase , atol=1E-4 ) assert outputs.logits.shape == expected_shape Path(__lowercase ).mkdir(exist_ok=__lowercase ) print(F"""Saving model and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(__lowercase ) processor.save_pretrained(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://github.com/dandelin/ViLT/releases/download/200k/vilt_200k_mlm_itm.ckpt''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) UpperCamelCase = parser.parse_args() convert_vilt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
319
'''simple docstring''' import json import logging import math import os import sys from dataclasses import dataclass, field from typing import Optional from datasets import Dataset, load_dataset import transformers from transformers import ( CONFIG_MAPPING, MODEL_FOR_MASKED_LM_MAPPING, AutoConfig, AutoModelForMaskedLM, AutoTokenizer, DataCollatorForWholeWordMask, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process UpperCamelCase = logging.getLogger(__name__) UpperCamelCase = list(MODEL_FOR_MASKED_LM_MAPPING.keys()) UpperCamelCase = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={ """help""": ( """The model checkpoint for weights initialization.Don't set if you want to train a model from scratch.""" ) } , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """If training from scratch, pass a model type from the list: """ + """, """.join(UpperCAmelCase_ )} , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={ """help""": ( """Override some existing default config settings when a model is trained from scratch. Example: """ """n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index""" ) } , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) UpperCamelCase_ : bool = field( default=UpperCAmelCase_ , metadata={"""help""": """Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."""} , ) UpperCamelCase_ : str = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) UpperCamelCase_ : bool = field( default=UpperCAmelCase_ , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) def _snake_case ( self : Tuple ) -> List[Any]: '''simple docstring''' if self.config_overrides is not None and (self.config_name is not None or self.model_name_or_path is not None): raise ValueError( '''--config_overrides can\'t be used in combination with --config_name or --model_name_or_path''' ) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """The name of the dataset to use (via the datasets library)."""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) UpperCamelCase_ : Optional[str] = field(default=UpperCAmelCase_ , metadata={"""help""": """The input training data file (a text file)."""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """An optional input evaluation data file to evaluate the perplexity on (a text file)."""} , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """An optional input train ref data file for whole word masking in Chinese."""} , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """An optional input validation ref data file for whole word masking in Chinese."""} , ) UpperCamelCase_ : bool = field( default=UpperCAmelCase_ , metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) UpperCamelCase_ : Optional[int] = field( default=5 , metadata={ """help""": """The percentage of the train set used as validation set in case there's no validation split""" } , ) UpperCamelCase_ : Optional[int] = field( default=UpperCAmelCase_ , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated. Default to the max input length of the model.""" ) } , ) UpperCamelCase_ : Optional[int] = field( default=UpperCAmelCase_ , metadata={"""help""": """The number of processes to use for the preprocessing."""} , ) UpperCamelCase_ : float = field( default=0.15 , metadata={"""help""": """Ratio of tokens to mask for masked language modeling loss"""} ) UpperCamelCase_ : bool = field( default=UpperCAmelCase_ , metadata={ """help""": ( """Whether to pad all samples to `max_seq_length`. """ """If False, will pad the samples dynamically when batching to the maximum length in the batch.""" ) } , ) def _snake_case ( self : List[Any] ) -> Optional[int]: '''simple docstring''' if self.train_file is not None: A: Tuple = self.train_file.split('''.''' )[-1] assert extension in ["csv", "json", "txt"], "`train_file` should be a csv, a json or a txt file." if self.validation_file is not None: A: str = self.validation_file.split('''.''' )[-1] assert extension in ["csv", "json", "txt"], "`validation_file` should be a csv, a json or a txt file." def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[str]: with open(__lowercase , '''r''' , encoding='''utf-8''' ) as f: A: List[Any] = [json.loads(__lowercase ) for line in f.read().splitlines() if (len(__lowercase ) > 0 and not line.isspace())] assert len(__lowercase ) == len(__lowercase ) A: Optional[int] = {c: dataset[c] for c in dataset.column_names} A: Union[str, Any] = refs return Dataset.from_dict(__lowercase ) def SCREAMING_SNAKE_CASE( ) -> int: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A: int = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('''.json''' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. A , A , A: Optional[int] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: A , A , A: List[Any] = parser.parse_args_into_dataclasses() # Detecting last checkpoint. A: Any = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: A: Any = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. """ '''Use --overwrite_output_dir to overcome.''' ) elif last_checkpoint is not None: logger.info( F"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ '''the `--output_dir` or add `--overwrite_output_dir` to train from scratch.''' ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , handlers=[logging.StreamHandler(sys.stdout )] , ) logger.setLevel(logging.INFO if is_main_process(training_args.local_rank ) else logging.WARN ) # Log on each process the small summary: logger.warning( F"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + F"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('''Training/evaluation parameters %s''' , __lowercase ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below) # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/ # (the dataset will be downloaded automatically from the datasets Hub). # # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called # 'text' is found. You can easily tweak this behavior (see below). # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. A: Dict = load_dataset(data_args.dataset_name , data_args.dataset_config_name ) if "validation" not in datasets.keys(): A: int = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F"""train[:{data_args.validation_split_percentage}%]""" , ) A: Dict = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F"""train[{data_args.validation_split_percentage}%:]""" , ) else: A: Any = {} if data_args.train_file is not None: A: int = data_args.train_file if data_args.validation_file is not None: A: Optional[int] = data_args.validation_file A: List[str] = data_args.train_file.split('''.''' )[-1] if extension == "txt": A: int = '''text''' A: Any = load_dataset(__lowercase , data_files=__lowercase ) # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at # https://huggingface.co/docs/datasets/loading_datasets.html. # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. A: Dict = { '''cache_dir''': model_args.cache_dir, '''revision''': model_args.model_revision, '''use_auth_token''': True if model_args.use_auth_token else None, } if model_args.config_name: A: List[Any] = AutoConfig.from_pretrained(model_args.config_name , **__lowercase ) elif model_args.model_name_or_path: A: int = AutoConfig.from_pretrained(model_args.model_name_or_path , **__lowercase ) else: A: str = CONFIG_MAPPING[model_args.model_type]() logger.warning('''You are instantiating a new config instance from scratch.''' ) if model_args.config_overrides is not None: logger.info(F"""Overriding config: {model_args.config_overrides}""" ) config.update_from_string(model_args.config_overrides ) logger.info(F"""New config: {config}""" ) A: Tuple = { '''cache_dir''': model_args.cache_dir, '''use_fast''': model_args.use_fast_tokenizer, '''revision''': model_args.model_revision, '''use_auth_token''': True if model_args.use_auth_token else None, } if model_args.tokenizer_name: A: Optional[int] = AutoTokenizer.from_pretrained(model_args.tokenizer_name , **__lowercase ) elif model_args.model_name_or_path: A: Union[str, Any] = AutoTokenizer.from_pretrained(model_args.model_name_or_path , **__lowercase ) else: raise ValueError( '''You are instantiating a new tokenizer from scratch. This is not supported by this script.''' '''You can do it from another script, save it, and load it from here, using --tokenizer_name.''' ) if model_args.model_name_or_path: A: List[Any] = AutoModelForMaskedLM.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=__lowercase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info('''Training new model from scratch''' ) A: List[Any] = AutoModelForMaskedLM.from_config(__lowercase ) model.resize_token_embeddings(len(__lowercase ) ) # Preprocessing the datasets. # First we tokenize all the texts. if training_args.do_train: A: int = datasets['''train'''].column_names else: A: str = datasets['''validation'''].column_names A: Tuple = '''text''' if '''text''' in column_names else column_names[0] A: List[str] = '''max_length''' if data_args.pad_to_max_length else False def tokenize_function(__lowercase ): # Remove empty lines A: int = [line for line in examples['''text'''] if len(__lowercase ) > 0 and not line.isspace()] return tokenizer(examples['''text'''] , padding=__lowercase , truncation=__lowercase , max_length=data_args.max_seq_length ) A: str = datasets.map( __lowercase , batched=__lowercase , num_proc=data_args.preprocessing_num_workers , remove_columns=[text_column_name] , load_from_cache_file=not data_args.overwrite_cache , ) # Add the chinese references if provided if data_args.train_ref_file is not None: A: List[str] = add_chinese_references(tokenized_datasets['''train'''] , data_args.train_ref_file ) if data_args.validation_ref_file is not None: A: Dict = add_chinese_references( tokenized_datasets['''validation'''] , data_args.validation_ref_file ) # If we have ref files, need to avoid it removed by trainer A: Optional[Any] = data_args.train_ref_file or data_args.validation_ref_file if has_ref: A: List[Any] = False # Data collator # This one will take care of randomly masking the tokens. A: Optional[Any] = DataCollatorForWholeWordMask(tokenizer=__lowercase , mlm_probability=data_args.mlm_probability ) # Initialize our Trainer A: Optional[int] = Trainer( model=__lowercase , args=__lowercase , train_dataset=tokenized_datasets['''train'''] if training_args.do_train else None , eval_dataset=tokenized_datasets['''validation'''] if training_args.do_eval else None , tokenizer=__lowercase , data_collator=__lowercase , ) # Training if training_args.do_train: if last_checkpoint is not None: A: Optional[int] = last_checkpoint elif model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path ): A: str = model_args.model_name_or_path else: A: List[str] = None A: str = trainer.train(resume_from_checkpoint=__lowercase ) trainer.save_model() # Saves the tokenizer too for easy upload A: Union[str, Any] = os.path.join(training_args.output_dir , '''train_results.txt''' ) if trainer.is_world_process_zero(): with open(__lowercase , '''w''' ) as writer: logger.info('''***** Train results *****''' ) for key, value in sorted(train_result.metrics.items() ): logger.info(F""" {key} = {value}""" ) writer.write(F"""{key} = {value}\n""" ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , '''trainer_state.json''' ) ) # Evaluation A: Optional[int] = {} if training_args.do_eval: logger.info('''*** Evaluate ***''' ) A: Optional[Any] = trainer.evaluate() A: Union[str, Any] = math.exp(eval_output['''eval_loss'''] ) A: Dict = perplexity A: Any = os.path.join(training_args.output_dir , '''eval_results_mlm_wwm.txt''' ) if trainer.is_world_process_zero(): with open(__lowercase , '''w''' ) as writer: logger.info('''***** Eval results *****''' ) for key, value in sorted(results.items() ): logger.info(F""" {key} = {value}""" ) writer.write(F"""{key} = {value}\n""" ) return results def SCREAMING_SNAKE_CASE( __lowercase ) -> List[Any]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
319
1
'''simple docstring''' import argparse import shutil from pathlib import Path from tqdm import tqdm from transformers import AutoTokenizer def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase=1_0_2_4 ) -> Optional[int]: A , A: Optional[Any] = [], [] A: List[str] = list(zip(__lowercase , __lowercase ) ) A , A: str = sorted_examples[0] def is_too_big(__lowercase ): return tok(__lowercase , return_tensors='''pt''' ).input_ids.shape[1] > max_tokens for src, tgt in tqdm(sorted_examples[1:] ): A: Optional[int] = new_src + ''' ''' + src A: Optional[Any] = new_tgt + ''' ''' + tgt if is_too_big(__lowercase ) or is_too_big(__lowercase ): # cant fit, finalize example finished_src.append(__lowercase ) finished_tgt.append(__lowercase ) A , A: Optional[int] = src, tgt else: # can fit, keep adding A , A: Tuple = cand_src, cand_tgt # cleanup if new_src: assert new_tgt finished_src.append(__lowercase ) finished_tgt.append(__lowercase ) return finished_src, finished_tgt def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase ) -> int: A: Optional[int] = Path(__lowercase ) save_path.mkdir(exist_ok=__lowercase ) for split in ["train"]: A , A: Union[str, Any] = data_dir / F"""{split}.source""", data_dir / F"""{split}.target""" A: Optional[int] = [x.rstrip() for x in Path(__lowercase ).open().readlines()] A: str = [x.rstrip() for x in Path(__lowercase ).open().readlines()] A , A: str = pack_examples(__lowercase , __lowercase , __lowercase , __lowercase ) print(F"""packed {split} split from {len(__lowercase )} examples -> {len(__lowercase )}.""" ) Path(save_path / F"""{split}.source""" ).open('''w''' ).write('''\n'''.join(__lowercase ) ) Path(save_path / F"""{split}.target""" ).open('''w''' ).write('''\n'''.join(__lowercase ) ) for split in ["val", "test"]: A , A: Optional[Any] = data_dir / F"""{split}.source""", data_dir / F"""{split}.target""" shutil.copyfile(__lowercase , save_path / F"""{split}.source""" ) shutil.copyfile(__lowercase , save_path / F"""{split}.target""" ) def SCREAMING_SNAKE_CASE( ) -> Dict: A: Any = argparse.ArgumentParser() parser.add_argument('''--tok_name''' , type=__lowercase , help='''like facebook/bart-large-cnn,t5-base, etc.''' ) parser.add_argument('''--max_seq_len''' , type=__lowercase , default=1_2_8 ) parser.add_argument('''--data_dir''' , type=__lowercase ) parser.add_argument('''--save_path''' , type=__lowercase ) A: str = parser.parse_args() A: Optional[Any] = AutoTokenizer.from_pretrained(args.tok_name ) return pack_data_dir(__lowercase , Path(args.data_dir ) , args.max_seq_len , args.save_path ) if __name__ == "__main__": packer_cli()
319
'''simple docstring''' import json import os import unittest from typing import Tuple from transformers import WavaVecaPhonemeCTCTokenizer from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.models.wavaveca_phoneme.tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizerOutput from transformers.testing_utils import require_phonemizer from ...test_tokenization_common import TokenizerTesterMixin @require_phonemizer class lowerCAmelCase_ ( UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Any = WavaVecaPhonemeCTCTokenizer UpperCamelCase_ : Tuple = False def _snake_case ( self : str ) -> Union[str, Any]: '''simple docstring''' super().setUp() A: Optional[int] = ( '''<s> <pad> </s> <unk> n s t ə l a i k d m ɛ ɾ e ɪ p o ɐ z ð f j v b ɹ ʁ ʊ iː r w ʌ u ɡ æ aɪ ʃ h ɔ ɑː ''' '''ŋ ɚ eɪ β uː y ɑ̃ oʊ ᵻ eː θ aʊ ts oː ɔ̃ ɣ ɜ ɑ dʒ əl x ɜː ç ʒ tʃ ɔː ɑːɹ ɛ̃ ʎ ɔːɹ ʋ aː ɕ œ ø oːɹ ɲ yː ''' '''ʔ iə i5 s. tɕ ?? nʲ ɛː œ̃ ɭ ɔø ʑ tʲ ɨ ɛɹ ts. rʲ ɪɹ ɭʲ i.5 ɔɪ q sʲ u5 ʊɹ iɜ a5 iɛ5 øː ʕ ja əɜ th ɑ5 ''' '''oɪ dʲ ə5 tɕh ts.h mʲ ɯ dʑ vʲ e̞ tʃʲ ei5 o5 onɡ5 ɑu5 iɑ5 ai5 aɪɚ kh ə1 ʐ i2 ʉ ħ t[ aɪə ʲ ju ə2 u2 oɜ ''' '''pː iɛɜ ou5 y5 uɜ tː uo5 d[ uoɜ tsh ɑɜ ɵ i̪5 uei5 ɟ aɜ ɑɨ i.ɜ eʊ o2 ɐ̃ ä pʲ kʲ n̩ ɒ ph ɑu2 uɨ əɪ ɫ ɬ ''' '''yɜ bʲ ɑ2 s̪ aiɜ χ ɐ̃ʊ̃ 1 ə4 yæɜ a2 ɨː t̪ iouɜ ũ onɡɜ aɨ iɛ2 ɔɨ ɑuɜ o̞ ei2 iou2 c kː y2 ɖ oe dˤ yɛɜ ''' '''əʊ S ɡʲ onɡ2 u" eiɜ ʈ ɯᵝ iou5 dZ r̝̊ i.2 tS s^ ʝ yə5 iɑɜ uə5 pf ɨu iɑ2 ou2 ər2 fʲ ai2 r̝ uəɜ ɳ əɨ ''' '''ua5 uɪ ɽ bː yu5 uo2 yɛ5 l̩ ɻ ərɜ ʂ i̪2 ouɜ uaɜ a. a.ː yæ5 dː r̩ ee ɪu ər5 i̪ ɜ æi u: i.ː t^ o1 ɪ^ ''' '''ai ueiɜ æː ɛɪ eə i. ɴ ie ua2 ɑ1 o4 tʃː o: ɑ: u1 N i̪1 au yæ2 u. qː yəɜ y: kʰ tʃʰ iʊ sx õ uo tʰ ''' '''uai5 bʰ u.ː uə2 ʊə d^ s̪ː yiɜ dʰ r. oe: i1 ɟː yu2 nʲʲ i̪4 uei2 tsʲ ɸ ĩ ɑ4 t̪ː eɑ u4 e: tsː ʈʰ ɡʰ ''' '''ɯɯ dʒʲ ʂʲ X ɵː uaiɜ tɕʲ ã t^ː ẽː yɛ2 cː i.1 ɛʊ dˤdˤ dʒː i4 ɡː yi ɕʲ ɟʰ pʰ dʑʲ yuɜ ua1 ua4 æiː ɐɐ ''' '''ui iou1 ʊː a1 iou4 cʰ iɛ1 yə2 ɖʰ ẽ ʒʲ ää ər4 iːː ɪː iɑ1 ər1 œː øi ɪuː cʰcʰ əː1 iː1 ũ kʰː o̞o̞ xʲ ''' '''ou1 iɛ4 e̞e̞ y1 dzː dʲʲ dʰː ɯᵝɯᵝ lː uo1 i.4 i: yɛ5ʲ a4''' ).split(''' ''' ) A: Union[str, Any] = dict(zip(SCREAMING_SNAKE_CASE_ , range(len(SCREAMING_SNAKE_CASE_ ) ) ) ) A: Dict = {'''pad_token''': '''<pad>''', '''unk_token''': '''<unk>''', '''bos_token''': '''<s>''', '''eos_token''': '''</s>'''} A: Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(SCREAMING_SNAKE_CASE_ ) + '''\n''' ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Tuple=False , SCREAMING_SNAKE_CASE_ : Any=20 , SCREAMING_SNAKE_CASE_ : Optional[int]=5 ) -> Tuple[str, list]: '''simple docstring''' A: int = [(i, tokenizer.decode([i] , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ )) for i in range(len(SCREAMING_SNAKE_CASE_ ) )] A: Optional[Any] = list(filter(lambda SCREAMING_SNAKE_CASE_ : [t[0]] == tokenizer.encode(t[1] , do_phonemize=SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) ) if max_length is not None and len(SCREAMING_SNAKE_CASE_ ) > max_length: A: int = toks[:max_length] if min_length is not None and len(SCREAMING_SNAKE_CASE_ ) < min_length and len(SCREAMING_SNAKE_CASE_ ) > 0: while len(SCREAMING_SNAKE_CASE_ ) < min_length: A: Dict = toks + toks # toks_str = [t[1] for t in toks] A: Union[str, Any] = [t[0] for t in toks] # Ensure consistency A: List[str] = tokenizer.decode(SCREAMING_SNAKE_CASE_ , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) if " " not in output_txt and len(SCREAMING_SNAKE_CASE_ ) > 1: A: int = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) + ''' ''' + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) ) if with_prefix_space: A: Tuple = ''' ''' + output_txt A: List[str] = tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) return output_txt, output_ids def _snake_case ( self : Optional[int] , **SCREAMING_SNAKE_CASE_ : int ) -> Dict: '''simple docstring''' kwargs.update(self.special_tokens_map ) return WavaVecaPhonemeCTCTokenizer.from_pretrained(self.tmpdirname , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int ) -> Optional[Any]: '''simple docstring''' A: List[Any] = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) # check adding a single token tokenizer.add_tokens('''xxx''' ) A: Any = tokenizer('''m xxx ɪ''' , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids self.assertEqual(SCREAMING_SNAKE_CASE_ , [13, 3_92, 17] ) # xxx should be last token tokenizer.add_tokens(['''aaa''', '''bbb''', '''ccc'''] ) A: Optional[int] = tokenizer('''m aaa ɪ ccc''' , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids self.assertEqual(SCREAMING_SNAKE_CASE_ , [13, 3_93, 17, 3_95] ) # aaa and ccc should be after xxx and 2 after aaa A: str = tokenizer('''maɪ c''' , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids self.assertEqual(SCREAMING_SNAKE_CASE_ , [3, 2_00] ) # mai should be <unk> (=3) def _snake_case ( self : int ) -> List[Any]: '''simple docstring''' A: Any = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: Any = '''Hello how are you''' A: Optional[Any] = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''h ə l oʊ h aʊ ɑːɹ j uː''' ) def _snake_case ( self : Tuple ) -> Dict: '''simple docstring''' A: str = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: List[Any] = '''Hello how are you''' A: Any = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) self.assertEqual(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids , tokenizer(SCREAMING_SNAKE_CASE_ , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids ) def _snake_case ( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' A: str = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: List[str] = '''Hello how are you''' A: Union[str, Any] = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) A: Union[str, Any] = tokenizer.decode(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Dict ) -> Optional[Any]: '''simple docstring''' A: Dict = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: Optional[Any] = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98], [24, 22, 5, 24, 22, 5, 77], ] A: List[str] = tokenizer.decode(sample_ids[0] ) A: List[str] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , batch_tokens[0] ) self.assertEqual(SCREAMING_SNAKE_CASE_ , ['''k s ɾ ɾ l ɭʲ''', '''j ð s j ð s oːɹ'''] ) def _snake_case ( self : Any ) -> Optional[int]: '''simple docstring''' A: int = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) A: List[Any] = '''Hello how are you''' A: Optional[Any] = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''h ə l oʊ | h aʊ | ɑːɹ | j uː |''' ) def _snake_case ( self : List[str] ) -> int: '''simple docstring''' A: Optional[Any] = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) A: Optional[Any] = '''Hello how are you''' A: Any = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) self.assertEqual(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids , tokenizer(SCREAMING_SNAKE_CASE_ , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids ) def _snake_case ( self : Dict ) -> Any: '''simple docstring''' A: Optional[int] = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) # fmt: off A: str = [ [11, 5, 15, tokenizer.pad_token_id, tokenizer.word_delimiter_token_id, 15, 8, tokenizer.word_delimiter_token_id, 98], [tokenizer.word_delimiter_token_id, 24, 22, tokenizer.word_delimiter_token_id, 5, 24, 22, 5, 77], ] # fmt: on # decode with word_del_token filter A: Tuple = tokenizer.decode(sample_ids[0] ) A: Optional[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , batch_tokens[0] ) self.assertEqual(SCREAMING_SNAKE_CASE_ , ['''k s ɾ ɾ l ɭʲ''', '''j ð s j ð s oːɹ'''] ) # decode with no word_del_token filter A: str = tokenizer.decode(sample_ids[0] , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , batch_tokens[0] ) self.assertEqual(SCREAMING_SNAKE_CASE_ , ['''k s ɾ | ɾ l | ɭʲ''', '''| j ð | s j ð s oːɹ'''] ) def _snake_case ( self : int ) -> List[str]: '''simple docstring''' A: Dict = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) A: Union[str, Any] = '''Hello how are you''' A: Tuple = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) A: Any = tokenizer.decode(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] ) -> Any: '''simple docstring''' A: Dict = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) A: Any = '''Hello how are you''' A: List[Any] = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) A: List[Any] = tokenizer.decode(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) self.assertEqual(''' '''.join([p.strip() for p in phonemes.split(''' |''' )] ).strip() , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] ) -> Optional[Any]: '''simple docstring''' A: List[str] = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token=SCREAMING_SNAKE_CASE_ ) A: List[Any] = '''Hello how are you''' A: List[str] = tokenizer(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ).input_ids A: Tuple = tokenizer(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''fr-fr''' ).input_ids self.assertNotEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Tuple = tokenizer.decode(SCREAMING_SNAKE_CASE_ ) A: Any = tokenizer.decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''h ə l oʊ h aʊ ɑːɹ j uː''' ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''ɛ l o h aʊ a ʁ j u''' ) def _snake_case ( self : str ) -> str: '''simple docstring''' A: str = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: str = '''Hello how Are you''' A: Union[str, Any] = '''hello how are you''' A: List[str] = tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids A: str = tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int ) -> List[Any]: '''simple docstring''' A: Union[str, Any] = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) tokenizer.add_tokens(['''!''', '''?'''] ) tokenizer.add_special_tokens({'''cls_token''': '''$$$'''} ) # fmt: off A: Tuple = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98, 3_92, 3_92, 3_93, 3_92, 3_92, 3_93, 3_94, 3_94], [24, 22, 5, 24, 22, 5, 77, tokenizer.pad_token_id, 3_94, 3_94], ] # fmt: on A: List[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , ['''k s ɾ ɾ l ɭʲ!?!? $$$''', '''j ð s j ð s oːɹ $$$'''] ) @staticmethod def _snake_case ( SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Tuple: '''simple docstring''' A: Any = [d[key] for d in offsets] return retrieved_list def _snake_case ( self : Any ) -> Tuple: '''simple docstring''' A: str = self.get_tokenizer(word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) # fmt: off # ksssɾɾ|ɾɾ<pad>ɾɾ|<pad>ɾlll|ɭʲ -> k s ɾ ɾ | ɾ l | ɭʲ" A: Union[str, Any] = [11, 5, 5, 5, 15, 15, tokenizer.pad_token_id, 15, 15, tokenizer.word_delimiter_token_id, tokenizer.pad_token_id, 15, 8, 8, 8, tokenizer.word_delimiter_token_id, 98] # fmt: on A: int = tokenizer.decode(SCREAMING_SNAKE_CASE_ , output_char_offsets=SCREAMING_SNAKE_CASE_ , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) # check Wav2Vec2CTCTokenizerOutput keys for char self.assertEqual(len(outputs.keys() ) , 2 ) self.assertTrue('''text''' in outputs ) self.assertTrue('''char_offsets''' in outputs ) self.assertTrue(isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) # check that order of chars is correct and identical for both outputs self.assertEqual(''' '''.join(self.get_from_offsets(outputs['''char_offsets'''] , '''char''' ) ) , outputs.text ) self.assertListEqual( self.get_from_offsets(outputs['''char_offsets'''] , '''char''' ) , ['''k''', '''s''', '''ɾ''', '''ɾ''', '''|''', '''ɾ''', '''l''', '''|''', '''ɭʲ'''] ) # check that offsets are actually correct for char # 0-1 is 11, 1-4 is 5, 4-6 is first 15, 6-7 is <pad> (thus not shown), 7-9 is second 15, 9-10 is word_delimiter_token, # 10-11 is <pad> (thus not shown), 11-12 is third 15, 12-15 is 8, 15-16 is word_delimiter_token, 16-17 is 98 self.assertListEqual( self.get_from_offsets(outputs['''char_offsets'''] , '''start_offset''' ) , [0, 1, 4, 7, 9, 11, 12, 15, 16] ) self.assertListEqual( self.get_from_offsets(outputs['''char_offsets'''] , '''end_offset''' ) , [1, 4, 6, 9, 10, 12, 15, 16, 17] ) def _snake_case ( self : Any ) -> List[Any]: '''simple docstring''' A: Optional[int] = self.get_tokenizer(word_delimiter_token='''|''' ) def check_list_tuples_equal(SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any] ): self.assertTrue(isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) self.assertTrue(isinstance(outputs_list[0] , SCREAMING_SNAKE_CASE_ ) ) # transform list to ModelOutput A: Dict = WavaVecaPhonemeCTCTokenizerOutput( {k: [d[k] for d in outputs_list] for k in outputs_list[0]} ) self.assertListEqual(outputs_batch['''text'''] , outputs_batch_a['''text'''] ) def recursive_check(SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[str] ): if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): [recursive_check(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for la, la in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )] self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if "char_offsets" in outputs_batch: recursive_check(outputs_batch['''char_offsets'''] , outputs_batch_a['''char_offsets'''] ) # fmt: off A: int = [ [11, 5, 15, tokenizer.pad_token_id, 15, 4, 8, 98, 32, 32, 32, 32, 4, 33, tokenizer.word_delimiter_token_id, 32, 32, 33, 34, 34], [24, 22, 5, tokenizer.word_delimiter_token_id, tokenizer.word_delimiter_token_id, 24, 22, 22, 22, 4, 5, 77, tokenizer.pad_token_id, 22, 22, 4, 34, 34, 34, 34], ] # fmt: on # We assume that `decode` works as expected. All we will check now is # the output type is correct and the output is identical to `decode` # char A: List[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ , output_char_offsets=SCREAMING_SNAKE_CASE_ ) A: List[Any] = [tokenizer.decode(SCREAMING_SNAKE_CASE_ , output_char_offsets=SCREAMING_SNAKE_CASE_ ) for ids in sample_ids] check_list_tuples_equal(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @unittest.skip('''Wav2Vec2PhonemeTokenizer always lower cases letters to correctly map to phonemes''' ) def _snake_case ( self : int ) -> int: '''simple docstring''' pass @unittest.skip('''Wav2Vec2PhonemeTokenizer always puts spaces between phonemes''' ) def _snake_case ( self : str ) -> Any: '''simple docstring''' pass @unittest.skip('''encodes to text to ids, but decodes ids to phonemes -> not possible to have internal consistency''' ) def _snake_case ( self : List[str] ) -> List[str]: '''simple docstring''' pass @unittest.skip('''Wav2Vec2PhonemeModel has no max model length => no testing''' ) def _snake_case ( self : Dict ) -> List[Any]: '''simple docstring''' pass def _snake_case ( self : Tuple ) -> Any: '''simple docstring''' A: Any = self.get_tokenizers(do_lower_case=SCREAMING_SNAKE_CASE_ ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): A: str = tokenizer.vocab_size A: str = len(SCREAMING_SNAKE_CASE_ ) self.assertNotEqual(SCREAMING_SNAKE_CASE_ , 0 ) # We usually have added tokens from the start in tests because our vocab fixtures are # smaller than the original vocabs - let's not assert this # self.assertEqual(vocab_size, all_size) A: List[Any] = ['''aaaaa bbbbbb''', '''cccccccccdddddddd'''] A: List[Any] = tokenizer.add_tokens(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = tokenizer.vocab_size A: Union[str, Any] = len(SCREAMING_SNAKE_CASE_ ) self.assertNotEqual(SCREAMING_SNAKE_CASE_ , 0 ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , len(SCREAMING_SNAKE_CASE_ ) ) self.assertEqual(SCREAMING_SNAKE_CASE_ , all_size + len(SCREAMING_SNAKE_CASE_ ) ) A: Any = tokenizer.encode('''aaaaa bbbbbb low cccccccccdddddddd l''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertGreaterEqual(len(SCREAMING_SNAKE_CASE_ ) , 4 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) A: str = {'''eos_token''': '''>>>>|||<||<<|<<''', '''pad_token''': '''<<<<<|||>|>>>>|>'''} A: int = tokenizer.add_special_tokens(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = tokenizer.vocab_size A: Optional[Any] = len(SCREAMING_SNAKE_CASE_ ) self.assertNotEqual(SCREAMING_SNAKE_CASE_ , 0 ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , len(SCREAMING_SNAKE_CASE_ ) ) self.assertEqual(SCREAMING_SNAKE_CASE_ , all_size_a + len(SCREAMING_SNAKE_CASE_ ) ) A: int = tokenizer.encode( '''>>>>|||<||<<|<< aaaaabbbbbb low cccccccccdddddddd <<<<<|||>|>>>>|> l''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertGreaterEqual(len(SCREAMING_SNAKE_CASE_ ) , 6 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[0] , tokens[1] ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokens[-4] ) self.assertEqual(tokens[0] , tokenizer.eos_token_id ) self.assertEqual(tokens[-3] , tokenizer.pad_token_id ) @unittest.skip('''The tokenizer shouldn\'t be used to encode input IDs (except for labels), only to decode.''' ) def _snake_case ( self : List[Any] ) -> Optional[Any]: '''simple docstring''' pass @unittest.skip('''The tokenizer shouldn\'t be used to encode input IDs (except for labels), only to decode.''' ) def _snake_case ( self : Tuple ) -> Optional[Any]: '''simple docstring''' pass def _snake_case ( self : str ) -> Tuple: '''simple docstring''' A: List[Any] = self.get_tokenizers(fast=SCREAMING_SNAKE_CASE_ , do_lower_case=SCREAMING_SNAKE_CASE_ ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): A: Union[str, Any] = ['''ð''', '''ɪ''', '''s''', '''ɪ''', '''z''', '''ɐ''', '''t''', '''ɛ''', '''k''', '''s''', '''t'''] A: Union[str, Any] = tokenizer.convert_tokens_to_string(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(output['''text'''] , SCREAMING_SNAKE_CASE_ )
319
1
'''simple docstring''' import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartTokenizer, MBartTokenizerFast, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin UpperCamelCase = get_tests_dir('''fixtures/test_sentencepiece.model''') if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right UpperCamelCase = 250004 UpperCamelCase = 250020 @require_sentencepiece @require_tokenizers class lowerCAmelCase_ ( UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Optional[int] = MBartTokenizer UpperCamelCase_ : int = MBartTokenizerFast UpperCamelCase_ : int = True UpperCamelCase_ : Dict = True def _snake_case ( self : Union[str, Any] ) -> List[str]: '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing A: Union[str, Any] = MBartTokenizer(SCREAMING_SNAKE_CASE_ , keep_accents=SCREAMING_SNAKE_CASE_ ) tokenizer.save_pretrained(self.tmpdirname ) def _snake_case ( self : str ) -> Optional[Any]: '''simple docstring''' A: List[Any] = MBartTokenizer(SCREAMING_SNAKE_CASE_ , keep_accents=SCREAMING_SNAKE_CASE_ ) A: Tuple = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) , [value + tokenizer.fairseq_offset for value in [2_85, 46, 10, 1_70, 3_82]] , ) A: Dict = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( SCREAMING_SNAKE_CASE_ , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) A: Optional[int] = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) self.assertListEqual( SCREAMING_SNAKE_CASE_ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 2, 4] # ^ unk: 2 + 1 = 3 unk: 2 + 1 = 3 ^ ] , ) A: List[str] = tokenizer.convert_ids_to_tokens(SCREAMING_SNAKE_CASE_ ) self.assertListEqual( SCREAMING_SNAKE_CASE_ , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) def _snake_case ( self : List[str] ) -> Dict: '''simple docstring''' if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return A: Any = (self.rust_tokenizer_class, '''hf-internal-testing/tiny-random-mbart''', {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): A: int = self.rust_tokenizer_class.from_pretrained(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) A: List[str] = self.tokenizer_class.from_pretrained(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = tempfile.mkdtemp() A: List[str] = tokenizer_r.save_pretrained(SCREAMING_SNAKE_CASE_ ) A: int = tokenizer_p.save_pretrained(SCREAMING_SNAKE_CASE_ ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files ) ) A: Union[str, Any] = tuple(f for f in tokenizer_r_files if '''tokenizer.json''' not in f ) self.assertSequenceEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Checks everything loads correctly in the same way A: Any = tokenizer_r.from_pretrained(SCREAMING_SNAKE_CASE_ ) A: int = tokenizer_p.from_pretrained(SCREAMING_SNAKE_CASE_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(SCREAMING_SNAKE_CASE_ ) # Save tokenizer rust, legacy_format=True A: Optional[Any] = tempfile.mkdtemp() A: str = tokenizer_r.save_pretrained(SCREAMING_SNAKE_CASE_ , legacy_format=SCREAMING_SNAKE_CASE_ ) A: List[str] = tokenizer_p.save_pretrained(SCREAMING_SNAKE_CASE_ ) # Checks it save with the same files self.assertSequenceEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Checks everything loads correctly in the same way A: List[Any] = tokenizer_r.from_pretrained(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = tokenizer_p.from_pretrained(SCREAMING_SNAKE_CASE_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) shutil.rmtree(SCREAMING_SNAKE_CASE_ ) # Save tokenizer rust, legacy_format=False A: List[str] = tempfile.mkdtemp() A: Any = tokenizer_r.save_pretrained(SCREAMING_SNAKE_CASE_ , legacy_format=SCREAMING_SNAKE_CASE_ ) A: int = tokenizer_p.save_pretrained(SCREAMING_SNAKE_CASE_ ) # Checks it saved the tokenizer.json file self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way A: List[str] = tokenizer_r.from_pretrained(SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokenizer_p.from_pretrained(SCREAMING_SNAKE_CASE_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) shutil.rmtree(SCREAMING_SNAKE_CASE_ ) @require_torch @require_sentencepiece @require_tokenizers class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : str = """facebook/mbart-large-en-ro""" UpperCamelCase_ : Tuple = [ """ UN Chief Says There Is No Military Solution in Syria""", """ Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.""", ] UpperCamelCase_ : Union[str, Any] = [ """Şeful ONU declară că nu există o soluţie militară în Siria""", """Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei""" """ pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor""" """ face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.""", ] UpperCamelCase_ : List[Any] = [8274, 12_7873, 2_5916, 7, 8622, 2071, 438, 6_7485, 53, 18_7895, 23, 5_1712, 2, EN_CODE] @classmethod def _snake_case ( cls : Any ) -> int: '''simple docstring''' A: MBartTokenizer = MBartTokenizer.from_pretrained( cls.checkpoint_name , src_lang='''en_XX''' , tgt_lang='''ro_RO''' ) A: List[str] = 1 return cls def _snake_case ( self : Dict ) -> Union[str, Any]: '''simple docstring''' self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''ar_AR'''] , 25_00_01 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''en_EN'''] , 25_00_04 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''ro_RO'''] , 25_00_20 ) def _snake_case ( self : Tuple ) -> List[Any]: '''simple docstring''' A: Optional[int] = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] ) -> str: '''simple docstring''' self.assertIn(SCREAMING_SNAKE_CASE_ , self.tokenizer.all_special_ids ) A: Any = [RO_CODE, 8_84, 90_19, 96, 9, 9_16, 8_67_92, 36, 1_87_43, 1_55_96, 5, 2] A: Tuple = self.tokenizer.decode(SCREAMING_SNAKE_CASE_ , skip_special_tokens=SCREAMING_SNAKE_CASE_ ) A: List[str] = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertNotIn(self.tokenizer.eos_token , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : str ) -> List[str]: '''simple docstring''' A: Optional[int] = ['''this is gunna be a long sentence ''' * 20] assert isinstance(src_text[0] , SCREAMING_SNAKE_CASE_ ) A: Any = 10 A: Tuple = self.tokenizer(SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ ).input_ids[0] self.assertEqual(ids[-2] , 2 ) self.assertEqual(ids[-1] , SCREAMING_SNAKE_CASE_ ) self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[int] ) -> Any: '''simple docstring''' self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['''<mask>''', '''ar_AR'''] ) , [25_00_26, 25_00_01] ) def _snake_case ( self : Any ) -> Optional[Any]: '''simple docstring''' A: Dict = tempfile.mkdtemp() A: Optional[Any] = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(SCREAMING_SNAKE_CASE_ ) A: Any = MBartTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , SCREAMING_SNAKE_CASE_ ) @require_torch def _snake_case ( self : int ) -> Dict: '''simple docstring''' A: Optional[int] = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ) A: Optional[Any] = shift_tokens_right(batch['''labels'''] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 assert batch.input_ids[1][-2:].tolist() == [2, EN_CODE] assert batch.decoder_input_ids[1][0].tolist() == RO_CODE assert batch.decoder_input_ids[1][-1] == 2 assert batch.labels[1][-2:].tolist() == [2, RO_CODE] @require_torch def _snake_case ( self : str ) -> List[Any]: '''simple docstring''' A: Any = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=len(self.expected_src_tokens ) , return_tensors='''pt''' , ) A: List[str] = shift_tokens_right(batch['''labels'''] , self.tokenizer.pad_token_id ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertEqual((2, 14) , batch.input_ids.shape ) self.assertEqual((2, 14) , batch.attention_mask.shape ) A: str = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , SCREAMING_SNAKE_CASE_ ) self.assertEqual(2 , batch.decoder_input_ids[0, -1] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id, EN_CODE] ) def _snake_case ( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' A: Tuple = self.tokenizer(self.src_text , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=3 , return_tensors='''pt''' ) A: Union[str, Any] = self.tokenizer( text_target=self.tgt_text , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=10 , return_tensors='''pt''' ) A: int = targets['''input_ids'''] A: int = shift_tokens_right(SCREAMING_SNAKE_CASE_ , self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def _snake_case ( self : str ) -> str: '''simple docstring''' A: str = self.tokenizer._build_translation_inputs( '''A test''' , return_tensors='''pt''' , src_lang='''en_XX''' , tgt_lang='''ar_AR''' ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ) , { # A, test, EOS, en_XX '''input_ids''': [[62, 30_34, 2, 25_00_04]], '''attention_mask''': [[1, 1, 1, 1]], # ar_AR '''forced_bos_token_id''': 25_00_01, } , )
319
'''simple docstring''' import warnings from ...utils import logging from .image_processing_beit import BeitImageProcessor UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> None: '''simple docstring''' warnings.warn( '''The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use BeitImageProcessor instead.''' , SCREAMING_SNAKE_CASE_ , ) super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ )
319
1
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, convert_to_rgb, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging UpperCamelCase = logging.get_logger(__name__) if is_vision_available(): import PIL class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : List[str] = ["""pixel_values"""] def __init__( self : int , SCREAMING_SNAKE_CASE_ : bool = True , SCREAMING_SNAKE_CASE_ : Dict[str, int] = None , SCREAMING_SNAKE_CASE_ : PILImageResampling = PILImageResampling.BICUBIC , SCREAMING_SNAKE_CASE_ : bool = True , SCREAMING_SNAKE_CASE_ : Dict[str, int] = None , SCREAMING_SNAKE_CASE_ : bool = True , SCREAMING_SNAKE_CASE_ : Union[int, float] = 1 / 2_55 , SCREAMING_SNAKE_CASE_ : bool = True , SCREAMING_SNAKE_CASE_ : Optional[Union[float, List[float]]] = None , SCREAMING_SNAKE_CASE_ : Optional[Union[float, List[float]]] = None , SCREAMING_SNAKE_CASE_ : bool = True , **SCREAMING_SNAKE_CASE_ : List[Any] , ) -> None: '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) A: Optional[int] = size if size is not None else {'''shortest_edge''': 2_24} A: List[str] = get_size_dict(SCREAMING_SNAKE_CASE_ , default_to_square=SCREAMING_SNAKE_CASE_ ) A: Any = crop_size if crop_size is not None else {'''height''': 2_24, '''width''': 2_24} A: Union[str, Any] = get_size_dict(SCREAMING_SNAKE_CASE_ , default_to_square=SCREAMING_SNAKE_CASE_ , param_name='''crop_size''' ) A: Any = do_resize A: Union[str, Any] = size A: Dict = resample A: Any = do_center_crop A: Tuple = crop_size A: int = do_rescale A: Optional[int] = rescale_factor A: int = do_normalize A: int = image_mean if image_mean is not None else OPENAI_CLIP_MEAN A: int = image_std if image_std is not None else OPENAI_CLIP_STD A: Optional[int] = do_convert_rgb def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : Dict[str, int] , SCREAMING_SNAKE_CASE_ : PILImageResampling = PILImageResampling.BICUBIC , SCREAMING_SNAKE_CASE_ : Optional[Union[str, ChannelDimension]] = None , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> np.ndarray: '''simple docstring''' A: List[str] = get_size_dict(SCREAMING_SNAKE_CASE_ , default_to_square=SCREAMING_SNAKE_CASE_ ) if "shortest_edge" not in size: raise ValueError(f"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}""" ) A: Union[str, Any] = get_resize_output_image_size(SCREAMING_SNAKE_CASE_ , size=size['''shortest_edge'''] , default_to_square=SCREAMING_SNAKE_CASE_ ) return resize(SCREAMING_SNAKE_CASE_ , size=SCREAMING_SNAKE_CASE_ , resample=SCREAMING_SNAKE_CASE_ , data_format=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : Dict[str, int] , SCREAMING_SNAKE_CASE_ : Optional[Union[str, ChannelDimension]] = None , **SCREAMING_SNAKE_CASE_ : int , ) -> np.ndarray: '''simple docstring''' A: Tuple = get_size_dict(SCREAMING_SNAKE_CASE_ ) if "height" not in size or "width" not in size: raise ValueError(f"""The `size` parameter must contain the keys (height, width). Got {size.keys()}""" ) return center_crop(SCREAMING_SNAKE_CASE_ , size=(size['''height'''], size['''width''']) , data_format=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : Union[int, float] , SCREAMING_SNAKE_CASE_ : Optional[Union[str, ChannelDimension]] = None , **SCREAMING_SNAKE_CASE_ : List[Any] , ) -> List[str]: '''simple docstring''' return rescale(SCREAMING_SNAKE_CASE_ , scale=SCREAMING_SNAKE_CASE_ , data_format=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : Union[float, List[float]] , SCREAMING_SNAKE_CASE_ : Union[float, List[float]] , SCREAMING_SNAKE_CASE_ : Optional[Union[str, ChannelDimension]] = None , **SCREAMING_SNAKE_CASE_ : Dict , ) -> np.ndarray: '''simple docstring''' return normalize(SCREAMING_SNAKE_CASE_ , mean=SCREAMING_SNAKE_CASE_ , std=SCREAMING_SNAKE_CASE_ , data_format=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : ImageInput , SCREAMING_SNAKE_CASE_ : bool = None , SCREAMING_SNAKE_CASE_ : Dict[str, int] = None , SCREAMING_SNAKE_CASE_ : PILImageResampling = None , SCREAMING_SNAKE_CASE_ : bool = None , SCREAMING_SNAKE_CASE_ : int = None , SCREAMING_SNAKE_CASE_ : bool = None , SCREAMING_SNAKE_CASE_ : float = None , SCREAMING_SNAKE_CASE_ : bool = None , SCREAMING_SNAKE_CASE_ : Optional[Union[float, List[float]]] = None , SCREAMING_SNAKE_CASE_ : Optional[Union[float, List[float]]] = None , SCREAMING_SNAKE_CASE_ : bool = None , SCREAMING_SNAKE_CASE_ : Optional[Union[str, TensorType]] = None , SCREAMING_SNAKE_CASE_ : Optional[ChannelDimension] = ChannelDimension.FIRST , **SCREAMING_SNAKE_CASE_ : int , ) -> PIL.Image.Image: '''simple docstring''' A: int = do_resize if do_resize is not None else self.do_resize A: Dict = size if size is not None else self.size A: Any = get_size_dict(SCREAMING_SNAKE_CASE_ , param_name='''size''' , default_to_square=SCREAMING_SNAKE_CASE_ ) A: Tuple = resample if resample is not None else self.resample A: List[Any] = do_center_crop if do_center_crop is not None else self.do_center_crop A: Union[str, Any] = crop_size if crop_size is not None else self.crop_size A: List[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ , param_name='''crop_size''' , default_to_square=SCREAMING_SNAKE_CASE_ ) A: Optional[int] = do_rescale if do_rescale is not None else self.do_rescale A: Optional[int] = rescale_factor if rescale_factor is not None else self.rescale_factor A: List[Any] = do_normalize if do_normalize is not None else self.do_normalize A: Any = image_mean if image_mean is not None else self.image_mean A: Union[str, Any] = image_std if image_std is not None else self.image_std A: Optional[Any] = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb A: List[str] = make_list_of_images(SCREAMING_SNAKE_CASE_ ) if not valid_images(SCREAMING_SNAKE_CASE_ ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None: raise ValueError('''Size must be specified if do_resize is True.''' ) if do_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # PIL RGBA images are converted to RGB if do_convert_rgb: A: Any = [convert_to_rgb(SCREAMING_SNAKE_CASE_ ) for image in images] # All transformations expect numpy arrays. A: str = [to_numpy_array(SCREAMING_SNAKE_CASE_ ) for image in images] if do_resize: A: Dict = [self.resize(image=SCREAMING_SNAKE_CASE_ , size=SCREAMING_SNAKE_CASE_ , resample=SCREAMING_SNAKE_CASE_ ) for image in images] if do_center_crop: A: Tuple = [self.center_crop(image=SCREAMING_SNAKE_CASE_ , size=SCREAMING_SNAKE_CASE_ ) for image in images] if do_rescale: A: Optional[int] = [self.rescale(image=SCREAMING_SNAKE_CASE_ , scale=SCREAMING_SNAKE_CASE_ ) for image in images] if do_normalize: A: List[Any] = [self.normalize(image=SCREAMING_SNAKE_CASE_ , mean=SCREAMING_SNAKE_CASE_ , std=SCREAMING_SNAKE_CASE_ ) for image in images] A: List[str] = [to_channel_dimension_format(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for image in images] A: Dict = {'''pixel_values''': images} return BatchFeature(data=SCREAMING_SNAKE_CASE_ , tensor_type=SCREAMING_SNAKE_CASE_ )
319
'''simple docstring''' import os import pytest from transformers.dynamic_module_utils import get_imports UpperCamelCase = ''' import os ''' UpperCamelCase = ''' def foo(): import os return False ''' UpperCamelCase = ''' def foo(): def bar(): if True: import os return False return bar() ''' UpperCamelCase = ''' import os try: import bar except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os def foo(): try: import bar except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except (ImportError, AttributeError): raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except ImportError as e: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar import baz except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar import baz except ImportError: x = 1 raise ValueError() ''' UpperCamelCase = [ TOP_LEVEL_IMPORT, IMPORT_IN_FUNCTION, DEEPLY_NESTED_IMPORT, TOP_LEVEL_TRY_IMPORT, GENERIC_EXCEPT_IMPORT, MULTILINE_TRY_IMPORT, MULTILINE_BOTH_IMPORT, MULTIPLE_EXCEPTS_IMPORT, EXCEPT_AS_IMPORT, TRY_IMPORT_IN_FUNCTION, ] @pytest.mark.parametrize('''case''' , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Dict: A: Tuple = os.path.join(__lowercase , '''test_file.py''' ) with open(__lowercase , '''w''' ) as _tmp_file: _tmp_file.write(__lowercase ) A: List[Any] = get_imports(__lowercase ) assert parsed_imports == ["os"]
319
1
'''simple docstring''' import copy from typing import Dict, List, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING UpperCamelCase = { '''facebook/mask2former-swin-small-coco-instance''': ( '''https://huggingface.co/facebook/mask2former-swin-small-coco-instance/blob/main/config.json''' ) # See all Mask2Former models at https://huggingface.co/models?filter=mask2former } UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : str = """mask2former""" UpperCamelCase_ : int = ["""swin"""] UpperCamelCase_ : List[Any] = {"""hidden_size""": """hidden_dim"""} def __init__( self : Tuple , SCREAMING_SNAKE_CASE_ : Optional[Dict] = None , SCREAMING_SNAKE_CASE_ : int = 2_56 , SCREAMING_SNAKE_CASE_ : int = 2_56 , SCREAMING_SNAKE_CASE_ : int = 2_56 , SCREAMING_SNAKE_CASE_ : int = 10_24 , SCREAMING_SNAKE_CASE_ : str = "relu" , SCREAMING_SNAKE_CASE_ : int = 6 , SCREAMING_SNAKE_CASE_ : int = 10 , SCREAMING_SNAKE_CASE_ : int = 8 , SCREAMING_SNAKE_CASE_ : float = 0.0 , SCREAMING_SNAKE_CASE_ : int = 20_48 , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : int = 4 , SCREAMING_SNAKE_CASE_ : int = 2_55 , SCREAMING_SNAKE_CASE_ : int = 1_00 , SCREAMING_SNAKE_CASE_ : float = 0.1 , SCREAMING_SNAKE_CASE_ : float = 2.0 , SCREAMING_SNAKE_CASE_ : float = 5.0 , SCREAMING_SNAKE_CASE_ : float = 5.0 , SCREAMING_SNAKE_CASE_ : int = 1_25_44 , SCREAMING_SNAKE_CASE_ : float = 3.0 , SCREAMING_SNAKE_CASE_ : float = 0.75 , SCREAMING_SNAKE_CASE_ : float = 0.02 , SCREAMING_SNAKE_CASE_ : float = 1.0 , SCREAMING_SNAKE_CASE_ : bool = True , SCREAMING_SNAKE_CASE_ : List[int] = [4, 8, 16, 32] , SCREAMING_SNAKE_CASE_ : bool = None , **SCREAMING_SNAKE_CASE_ : str , ) -> str: '''simple docstring''' if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `Swin` backbone.''' ) A: List[Any] = CONFIG_MAPPING['''swin''']( image_size=2_24 , in_channels=3 , patch_size=4 , embed_dim=96 , depths=[2, 2, 18, 2] , num_heads=[3, 6, 12, 24] , window_size=7 , drop_path_rate=0.3 , use_absolute_embeddings=SCREAMING_SNAKE_CASE_ , out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] , ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): A: int = backbone_config.pop('''model_type''' ) A: Optional[int] = CONFIG_MAPPING[backbone_model_type] A: str = config_class.from_dict(SCREAMING_SNAKE_CASE_ ) # verify that the backbone is supported if backbone_config.model_type not in self.backbones_supported: logger.warning_once( f"""Backbone {backbone_config.model_type} is not a supported model and may not be compatible with Mask2Former. """ f"""Supported model types: {','.join(self.backbones_supported )}""" ) A: Any = backbone_config A: Optional[Any] = feature_size A: Optional[Any] = mask_feature_size A: Any = hidden_dim A: Optional[Any] = encoder_feedforward_dim A: Optional[int] = activation_function A: Tuple = encoder_layers A: Any = decoder_layers A: Tuple = num_attention_heads A: int = dropout A: List[str] = dim_feedforward A: Dict = pre_norm A: Tuple = enforce_input_projection A: Tuple = common_stride A: str = ignore_value A: Optional[int] = num_queries A: Any = no_object_weight A: Tuple = class_weight A: Any = mask_weight A: List[str] = dice_weight A: Optional[Any] = train_num_points A: Tuple = oversample_ratio A: int = importance_sample_ratio A: List[str] = init_std A: Tuple = init_xavier_std A: List[Any] = use_auxiliary_loss A: int = feature_strides A: List[Any] = output_auxiliary_logits A: Union[str, Any] = decoder_layers super().__init__(**SCREAMING_SNAKE_CASE_ ) @classmethod def _snake_case ( cls : Any , SCREAMING_SNAKE_CASE_ : PretrainedConfig , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[int]: '''simple docstring''' return cls( backbone_config=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) def _snake_case ( self : Any ) -> Dict[str, any]: '''simple docstring''' A: List[Any] = copy.deepcopy(self.__dict__ ) A: List[Any] = self.backbone_config.to_dict() A: Any = self.__class__.model_type return output
319
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( BertTokenizer, ViltConfig, ViltForImageAndTextRetrieval, ViltForImagesAndTextClassification, ViltForMaskedLM, ViltForQuestionAnswering, ViltImageProcessor, ViltProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=False , __lowercase=False , __lowercase=False ) -> Optional[Any]: A: str = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""transformer.blocks.{i}.norm1.weight""", F"""vilt.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.norm1.bias""", F"""vilt.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append( (F"""transformer.blocks.{i}.attn.proj.weight""", F"""vilt.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append( (F"""transformer.blocks.{i}.attn.proj.bias""", F"""vilt.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""transformer.blocks.{i}.norm2.weight""", F"""vilt.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.norm2.bias""", F"""vilt.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append( (F"""transformer.blocks.{i}.mlp.fc1.weight""", F"""vilt.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc1.bias""", F"""vilt.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc2.weight""", F"""vilt.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc2.bias""", F"""vilt.encoder.layer.{i}.output.dense.bias""") ) # embeddings rename_keys.extend( [ # text embeddings ('''text_embeddings.word_embeddings.weight''', '''vilt.embeddings.text_embeddings.word_embeddings.weight'''), ( '''text_embeddings.position_embeddings.weight''', '''vilt.embeddings.text_embeddings.position_embeddings.weight''', ), ('''text_embeddings.position_ids''', '''vilt.embeddings.text_embeddings.position_ids'''), ( '''text_embeddings.token_type_embeddings.weight''', '''vilt.embeddings.text_embeddings.token_type_embeddings.weight''', ), ('''text_embeddings.LayerNorm.weight''', '''vilt.embeddings.text_embeddings.LayerNorm.weight'''), ('''text_embeddings.LayerNorm.bias''', '''vilt.embeddings.text_embeddings.LayerNorm.bias'''), # patch embeddings ('''transformer.cls_token''', '''vilt.embeddings.cls_token'''), ('''transformer.patch_embed.proj.weight''', '''vilt.embeddings.patch_embeddings.projection.weight'''), ('''transformer.patch_embed.proj.bias''', '''vilt.embeddings.patch_embeddings.projection.bias'''), ('''transformer.pos_embed''', '''vilt.embeddings.position_embeddings'''), # token type embeddings ('''token_type_embeddings.weight''', '''vilt.embeddings.token_type_embeddings.weight'''), ] ) # final layernorm + pooler rename_keys.extend( [ ('''transformer.norm.weight''', '''vilt.layernorm.weight'''), ('''transformer.norm.bias''', '''vilt.layernorm.bias'''), ('''pooler.dense.weight''', '''vilt.pooler.dense.weight'''), ('''pooler.dense.bias''', '''vilt.pooler.dense.bias'''), ] ) # classifier head(s) if vqa_model: # classification head rename_keys.extend( [ ('''vqa_classifier.0.weight''', '''classifier.0.weight'''), ('''vqa_classifier.0.bias''', '''classifier.0.bias'''), ('''vqa_classifier.1.weight''', '''classifier.1.weight'''), ('''vqa_classifier.1.bias''', '''classifier.1.bias'''), ('''vqa_classifier.3.weight''', '''classifier.3.weight'''), ('''vqa_classifier.3.bias''', '''classifier.3.bias'''), ] ) elif nlvr_model: # classification head rename_keys.extend( [ ('''nlvr2_classifier.0.weight''', '''classifier.0.weight'''), ('''nlvr2_classifier.0.bias''', '''classifier.0.bias'''), ('''nlvr2_classifier.1.weight''', '''classifier.1.weight'''), ('''nlvr2_classifier.1.bias''', '''classifier.1.bias'''), ('''nlvr2_classifier.3.weight''', '''classifier.3.weight'''), ('''nlvr2_classifier.3.bias''', '''classifier.3.bias'''), ] ) else: pass return rename_keys def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Any: for i in range(config.num_hidden_layers ): A: Tuple = '''vilt.''' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A: List[str] = state_dict.pop(F"""transformer.blocks.{i}.attn.qkv.weight""" ) A: Optional[Any] = state_dict.pop(F"""transformer.blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict A: Dict = in_proj_weight[ : config.hidden_size, : ] A: int = in_proj_bias[: config.hidden_size] A: Any = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A: int = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A: Optional[int] = in_proj_weight[ -config.hidden_size :, : ] A: Optional[Any] = in_proj_bias[-config.hidden_size :] def SCREAMING_SNAKE_CASE( __lowercase ) -> int: A: Optional[int] = ['''head.weight''', '''head.bias'''] for k in ignore_keys: state_dict.pop(__lowercase , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: A: List[Any] = dct.pop(__lowercase ) A: int = val @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str: A: Optional[Any] = ViltConfig(image_size=3_8_4 , patch_size=3_2 , tie_word_embeddings=__lowercase ) A: Tuple = False A: str = False A: List[Any] = False A: Optional[int] = False if "vqa" in checkpoint_url: A: Union[str, Any] = True A: Union[str, Any] = 3_1_2_9 A: List[Any] = '''huggingface/label-files''' A: Any = '''vqa2-id2label.json''' A: Optional[Any] = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: Union[str, Any] = {int(__lowercase ): v for k, v in idalabel.items()} A: Any = idalabel A: Optional[Any] = {v: k for k, v in idalabel.items()} A: List[str] = ViltForQuestionAnswering(__lowercase ) elif "nlvr" in checkpoint_url: A: Dict = True A: str = 2 A: Union[str, Any] = {0: '''False''', 1: '''True'''} A: Any = {v: k for k, v in config.idalabel.items()} A: Optional[Any] = 3 A: Any = ViltForImagesAndTextClassification(__lowercase ) elif "irtr" in checkpoint_url: A: Tuple = True A: Optional[Any] = ViltForImageAndTextRetrieval(__lowercase ) elif "mlm_itm" in checkpoint_url: A: Tuple = True A: Optional[int] = ViltForMaskedLM(__lowercase ) else: raise ValueError('''Unknown model type''' ) # load state_dict of original model, remove and rename some keys A: int = torch.hub.load_state_dict_from_url(__lowercase , map_location='''cpu''' )['''state_dict'''] A: List[str] = create_rename_keys(__lowercase , __lowercase , __lowercase , __lowercase ) for src, dest in rename_keys: rename_key(__lowercase , __lowercase , __lowercase ) read_in_q_k_v(__lowercase , __lowercase ) if mlm_model or irtr_model: A: str = ['''itm_score.fc.weight''', '''itm_score.fc.bias'''] for k in ignore_keys: state_dict.pop(__lowercase , __lowercase ) # load state dict into HuggingFace model model.eval() if mlm_model: A , A: Union[str, Any] = model.load_state_dict(__lowercase , strict=__lowercase ) assert missing_keys == ["mlm_score.decoder.bias"] else: model.load_state_dict(__lowercase ) # Define processor A: Optional[Any] = ViltImageProcessor(size=3_8_4 ) A: Dict = BertTokenizer.from_pretrained('''bert-base-uncased''' ) A: Optional[int] = ViltProcessor(__lowercase , __lowercase ) # Forward pass on example inputs (image + text) if nlvr_model: A: str = Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg''' , stream=__lowercase ).raw ) A: List[str] = Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg''' , stream=__lowercase ).raw ) A: Any = ( '''The left image contains twice the number of dogs as the right image, and at least two dogs in total are''' ''' standing.''' ) A: List[Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: List[Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: List[str] = model( input_ids=encoding_a.input_ids , pixel_values=encoding_a.pixel_values , pixel_values_a=encoding_a.pixel_values , ) else: A: Any = Image.open(requests.get('''http://images.cocodataset.org/val2017/000000039769.jpg''' , stream=__lowercase ).raw ) if mlm_model: A: Optional[int] = '''a bunch of [MASK] laying on a [MASK].''' else: A: Optional[int] = '''How many cats are there?''' A: Union[str, Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: Any = model(**__lowercase ) # Verify outputs if mlm_model: A: Any = torch.Size([1, 1_1, 3_0_5_2_2] ) A: Tuple = torch.tensor([-1_2.5_0_6_1, -1_2.5_1_2_3, -1_2.5_1_7_4] ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , __lowercase , atol=1E-4 ) # verify masked token prediction equals "cats" A: List[str] = outputs.logits[0, 4, :].argmax(-1 ).item() assert tokenizer.decode([predicted_id] ) == "cats" elif vqa_model: A: Any = torch.Size([1, 3_1_2_9] ) A: Optional[int] = torch.tensor([-1_5.9_4_9_5, -1_8.1_4_7_2, -1_0.3_0_4_1] ) assert torch.allclose(outputs.logits[0, :3] , __lowercase , atol=1E-4 ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , __lowercase , atol=1E-4 ) # verify vqa prediction equals "2" A: Dict = outputs.logits.argmax(-1 ).item() assert model.config.idalabel[predicted_idx] == "2" elif nlvr_model: A: Union[str, Any] = torch.Size([1, 2] ) A: Optional[Any] = torch.tensor([-2.8_7_2_1, 2.1_2_9_1] ) assert torch.allclose(outputs.logits[0, :3] , __lowercase , atol=1E-4 ) assert outputs.logits.shape == expected_shape Path(__lowercase ).mkdir(exist_ok=__lowercase ) print(F"""Saving model and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(__lowercase ) processor.save_pretrained(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://github.com/dandelin/ViLT/releases/download/200k/vilt_200k_mlm_itm.ckpt''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) UpperCamelCase = parser.parse_args() convert_vilt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
319
1
'''simple docstring''' from .glue import GlueDataset, GlueDataTrainingArguments from .language_modeling import ( LineByLineTextDataset, LineByLineWithRefDataset, LineByLineWithSOPTextDataset, TextDataset, TextDatasetForNextSentencePrediction, ) from .squad import SquadDataset, SquadDataTrainingArguments
319
'''simple docstring''' import argparse import json import os import numpy as np import PIL import requests import tensorflow.keras.applications.efficientnet as efficientnet import torch from huggingface_hub import hf_hub_download from PIL import Image from tensorflow.keras.preprocessing import image from transformers import ( EfficientNetConfig, EfficientNetForImageClassification, EfficientNetImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''b0''': efficientnet.EfficientNetBa, '''b1''': efficientnet.EfficientNetBa, '''b2''': efficientnet.EfficientNetBa, '''b3''': efficientnet.EfficientNetBa, '''b4''': efficientnet.EfficientNetBa, '''b5''': efficientnet.EfficientNetBa, '''b6''': efficientnet.EfficientNetBa, '''b7''': efficientnet.EfficientNetBa, } UpperCamelCase = { '''b0''': { '''hidden_dim''': 1280, '''width_coef''': 1.0, '''depth_coef''': 1.0, '''image_size''': 224, '''dropout_rate''': 0.2, '''dw_padding''': [], }, '''b1''': { '''hidden_dim''': 1280, '''width_coef''': 1.0, '''depth_coef''': 1.1, '''image_size''': 240, '''dropout_rate''': 0.2, '''dw_padding''': [16], }, '''b2''': { '''hidden_dim''': 1408, '''width_coef''': 1.1, '''depth_coef''': 1.2, '''image_size''': 260, '''dropout_rate''': 0.3, '''dw_padding''': [5, 8, 16], }, '''b3''': { '''hidden_dim''': 1536, '''width_coef''': 1.2, '''depth_coef''': 1.4, '''image_size''': 300, '''dropout_rate''': 0.3, '''dw_padding''': [5, 18], }, '''b4''': { '''hidden_dim''': 1792, '''width_coef''': 1.4, '''depth_coef''': 1.8, '''image_size''': 380, '''dropout_rate''': 0.4, '''dw_padding''': [6], }, '''b5''': { '''hidden_dim''': 2048, '''width_coef''': 1.6, '''depth_coef''': 2.2, '''image_size''': 456, '''dropout_rate''': 0.4, '''dw_padding''': [13, 27], }, '''b6''': { '''hidden_dim''': 2304, '''width_coef''': 1.8, '''depth_coef''': 2.6, '''image_size''': 528, '''dropout_rate''': 0.5, '''dw_padding''': [31], }, '''b7''': { '''hidden_dim''': 2560, '''width_coef''': 2.0, '''depth_coef''': 3.1, '''image_size''': 600, '''dropout_rate''': 0.5, '''dw_padding''': [18], }, } def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: A: Tuple = EfficientNetConfig() A: Optional[int] = CONFIG_MAP[model_name]['''hidden_dim'''] A: Optional[int] = CONFIG_MAP[model_name]['''width_coef'''] A: str = CONFIG_MAP[model_name]['''depth_coef'''] A: Dict = CONFIG_MAP[model_name]['''image_size'''] A: str = CONFIG_MAP[model_name]['''dropout_rate'''] A: Optional[Any] = CONFIG_MAP[model_name]['''dw_padding'''] A: Optional[Any] = '''huggingface/label-files''' A: List[str] = '''imagenet-1k-id2label.json''' A: Dict = 1_0_0_0 A: Any = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: Tuple = {int(__lowercase ): v for k, v in idalabel.items()} A: int = idalabel A: Tuple = {v: k for k, v in idalabel.items()} return config def SCREAMING_SNAKE_CASE( ) -> Any: A: Optional[Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' A: Union[str, Any] = Image.open(requests.get(__lowercase , stream=__lowercase ).raw ) return im def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: A: List[str] = CONFIG_MAP[model_name]['''image_size'''] A: List[Any] = EfficientNetImageProcessor( size={'''height''': size, '''width''': size} , image_mean=[0.4_8_5, 0.4_5_6, 0.4_0_6] , image_std=[0.4_7_8_5_3_9_4_4, 0.4_7_3_2_8_6_4, 0.4_7_4_3_4_1_6_3] , do_center_crop=__lowercase , ) return preprocessor def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: A: List[str] = [v.split('''_''' )[0].split('''block''' )[1] for v in original_param_names if v.startswith('''block''' )] A: List[str] = sorted(set(__lowercase ) ) A: Dict = len(__lowercase ) A: List[str] = {b: str(__lowercase ) for b, i in zip(__lowercase , range(__lowercase ) )} A: Optional[int] = [] rename_keys.append(('''stem_conv/kernel:0''', '''embeddings.convolution.weight''') ) rename_keys.append(('''stem_bn/gamma:0''', '''embeddings.batchnorm.weight''') ) rename_keys.append(('''stem_bn/beta:0''', '''embeddings.batchnorm.bias''') ) rename_keys.append(('''stem_bn/moving_mean:0''', '''embeddings.batchnorm.running_mean''') ) rename_keys.append(('''stem_bn/moving_variance:0''', '''embeddings.batchnorm.running_var''') ) for b in block_names: A: int = block_name_mapping[b] rename_keys.append((F"""block{b}_expand_conv/kernel:0""", F"""encoder.blocks.{hf_b}.expansion.expand_conv.weight""") ) rename_keys.append((F"""block{b}_expand_bn/gamma:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.weight""") ) rename_keys.append((F"""block{b}_expand_bn/beta:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.bias""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_mean""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_var""") ) rename_keys.append( (F"""block{b}_dwconv/depthwise_kernel:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight""") ) rename_keys.append((F"""block{b}_bn/gamma:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight""") ) rename_keys.append((F"""block{b}_bn/beta:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias""") ) rename_keys.append( (F"""block{b}_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean""") ) rename_keys.append( (F"""block{b}_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var""") ) rename_keys.append((F"""block{b}_se_reduce/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.weight""") ) rename_keys.append((F"""block{b}_se_reduce/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.bias""") ) rename_keys.append((F"""block{b}_se_expand/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.weight""") ) rename_keys.append((F"""block{b}_se_expand/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.bias""") ) rename_keys.append( (F"""block{b}_project_conv/kernel:0""", F"""encoder.blocks.{hf_b}.projection.project_conv.weight""") ) rename_keys.append((F"""block{b}_project_bn/gamma:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.weight""") ) rename_keys.append((F"""block{b}_project_bn/beta:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.bias""") ) rename_keys.append( (F"""block{b}_project_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_mean""") ) rename_keys.append( (F"""block{b}_project_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_var""") ) rename_keys.append(('''top_conv/kernel:0''', '''encoder.top_conv.weight''') ) rename_keys.append(('''top_bn/gamma:0''', '''encoder.top_bn.weight''') ) rename_keys.append(('''top_bn/beta:0''', '''encoder.top_bn.bias''') ) rename_keys.append(('''top_bn/moving_mean:0''', '''encoder.top_bn.running_mean''') ) rename_keys.append(('''top_bn/moving_variance:0''', '''encoder.top_bn.running_var''') ) A: Union[str, Any] = {} for item in rename_keys: if item[0] in original_param_names: A: str = '''efficientnet.''' + item[1] A: int = '''classifier.weight''' A: Tuple = '''classifier.bias''' return key_mapping def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Tuple: for key, value in tf_params.items(): if "normalization" in key: continue A: Union[str, Any] = key_mapping[key] if "_conv" in key and "kernel" in key: A: List[str] = torch.from_numpy(__lowercase ).permute(3 , 2 , 0 , 1 ) elif "depthwise_kernel" in key: A: List[Any] = torch.from_numpy(__lowercase ).permute(2 , 3 , 0 , 1 ) elif "kernel" in key: A: Optional[Any] = torch.from_numpy(np.transpose(__lowercase ) ) else: A: Any = torch.from_numpy(__lowercase ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(__lowercase ) @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase ) -> Tuple: A: Optional[int] = model_classes[model_name]( include_top=__lowercase , weights='''imagenet''' , input_tensor=__lowercase , input_shape=__lowercase , pooling=__lowercase , classes=1_0_0_0 , classifier_activation='''softmax''' , ) A: List[str] = original_model.trainable_variables A: Optional[Any] = original_model.non_trainable_variables A: Union[str, Any] = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: A: int = param.numpy() A: Tuple = list(tf_params.keys() ) # Load HuggingFace model A: Dict = get_efficientnet_config(__lowercase ) A: Union[str, Any] = EfficientNetForImageClassification(__lowercase ).eval() A: Dict = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print('''Converting parameters...''' ) A: int = rename_keys(__lowercase ) replace_params(__lowercase , __lowercase , __lowercase ) # Initialize preprocessor and preprocess input image A: List[Any] = convert_image_processor(__lowercase ) A: Optional[Any] = preprocessor(images=prepare_img() , return_tensors='''pt''' ) # HF model inference hf_model.eval() with torch.no_grad(): A: str = hf_model(**__lowercase ) A: List[Any] = outputs.logits.detach().numpy() # Original model inference A: Any = False A: List[Any] = CONFIG_MAP[model_name]['''image_size'''] A: List[Any] = prepare_img().resize((image_size, image_size) , resample=PIL.Image.NEAREST ) A: str = image.img_to_array(__lowercase ) A: Dict = np.expand_dims(__lowercase , axis=0 ) A: Any = original_model.predict(__lowercase ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(__lowercase , __lowercase , atol=1E-3 ), "The predicted logits are not the same." print('''Model outputs match!''' ) if save_model: # Create folder to save model if not os.path.isdir(__lowercase ): os.mkdir(__lowercase ) # Save converted model and image processor hf_model.save_pretrained(__lowercase ) preprocessor.save_pretrained(__lowercase ) if push_to_hub: # Push model and image processor to hub print(F"""Pushing converted {model_name} to the hub...""" ) A: int = F"""efficientnet-{model_name}""" preprocessor.push_to_hub(__lowercase ) hf_model.push_to_hub(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''b0''', type=str, help='''Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''hf_model''', type=str, help='''Path to the output PyTorch model directory.''', ) parser.add_argument('''--save_model''', action='''store_true''', help='''Save model to local''') parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Push model and image processor to the hub''') UpperCamelCase = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
319
1
'''simple docstring''' import fire from utils import calculate_rouge, save_json def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=None , **__lowercase ) -> Any: A: Any = [x.strip() for x in open(__lowercase ).readlines()] A: Dict = [x.strip() for x in open(__lowercase ).readlines()][: len(__lowercase )] A: Union[str, Any] = calculate_rouge(__lowercase , __lowercase , **__lowercase ) if save_path is not None: save_json(__lowercase , __lowercase , indent=__lowercase ) return metrics # these print nicely if __name__ == "__main__": fire.Fire(calculate_rouge_path)
319
'''simple docstring''' from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase = {'''configuration_focalnet''': ['''FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FocalNetConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FocalNetForImageClassification''', '''FocalNetForMaskedImageModeling''', '''FocalNetBackbone''', '''FocalNetModel''', '''FocalNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_focalnet import FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FocalNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_focalnet import ( FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST, FocalNetBackbone, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetModel, FocalNetPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
1
'''simple docstring''' from typing import List, Optional, Union import numpy as np import tensorflow as tf from .utils import logging UpperCamelCase = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE( __lowercase ) -> List[int]: if isinstance(__lowercase , np.ndarray ): return list(tensor.shape ) A: Tuple = tf.shape(__lowercase ) if tensor.shape == tf.TensorShape(__lowercase ): return dynamic A: Tuple = tensor.shape.as_list() return [dynamic[i] if s is None else s for i, s in enumerate(__lowercase )] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = None , __lowercase = None ) -> tf.Tensor: return tf.nn.softmax(logits=logits + 1E-9 , axis=__lowercase , name=__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase=1E-5 , __lowercase=-1 ) -> List[Any]: # This is a very simplified functional layernorm, designed to duplicate # the functionality of PyTorch nn.functional.layer_norm when this is needed to port # models in Transformers. if weight.shape.rank != 1 or bias.shape.rank != 1 or not isinstance(__lowercase , __lowercase ): raise NotImplementedError('''Only 1D weight and bias tensors are supported for now, with only a single axis.''' ) # Get mean and variance on the axis to be normalized A , A: Dict = tf.nn.moments(__lowercase , axes=[axis] , keepdims=__lowercase ) if axis != -1: # Reshape scale and weight to have the same rank as inputs, but with 1 dimensions # on every dimension except axis A: str = [1] * inputs.shape.rank A: Dict = shape_list(__lowercase )[axis] A: Optional[int] = tf.reshape(__lowercase , __lowercase ) A: int = tf.reshape(__lowercase , __lowercase ) # Compute layer normalization using the batch_normalization # function. A: Optional[int] = tf.nn.batch_normalization( __lowercase , __lowercase , __lowercase , offset=__lowercase , scale=__lowercase , variance_epsilon=__lowercase , ) return outputs def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=0 , __lowercase=-1 ) -> Any: # Replicates the behavior of torch.flatten in TF # If end_dim or start_dim is negative, count them from the end if end_dim < 0: end_dim += input.shape.rank if start_dim < 0: start_dim += input.shape.rank if start_dim == end_dim: return input A: str = tf.shape(__lowercase ) A: Optional[Any] = tf.math.reduce_prod(in_shape[start_dim : end_dim + 1] ) A: List[Any] = tf.concat([in_shape[:start_dim], [flattened_dim], in_shape[end_dim + 1 :]] , axis=0 ) return tf.reshape(__lowercase , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> tf.Tensor: if not isinstance(__lowercase , tf.Tensor ): A: Any = tf.convert_to_tensor(__lowercase ) # Catches stray NumPy inputs if encoder_attention_mask.shape.rank == 3: A: int = encoder_attention_mask[:, None, :, :] if encoder_attention_mask.shape.rank == 2: A: Union[str, Any] = encoder_attention_mask[:, None, None, :] # T5 has a mask that can compare sequence ids, we can simulate this here with this transposition # Cf. https://github.com/tensorflow/mesh/blob/8d2465e9bc93129b913b5ccc6a59aa97abd96ec6/mesh_tensorflow # /transformer/transformer_layers.py#L270 # encoder_extended_attention_mask = (encoder_extended_attention_mask == # encoder_extended_attention_mask.transpose(-1, -2)) A: List[Any] = ( tf.cast(1 , encoder_attention_mask.dtype ) - encoder_extended_attention_mask ) * encoder_extended_attention_mask.dtype.min return encoder_extended_attention_mask def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase = "input_ids" ) -> None: tf.debugging.assert_less( __lowercase , tf.cast(__lowercase , dtype=tensor.dtype ) , message=( F"""The maximum value of {tensor_name} ({tf.math.reduce_max(__lowercase )}) must be smaller than the embedding """ F"""layer's input dimension ({embed_dim}). The likely cause is some problem at tokenization time.""" ) , ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> List[Any]: A: str = 6_4_5_1_2 # Check that no item in `data` is larger than `HDF5_OBJECT_HEADER_LIMIT` # because in that case even chunking the array would not make the saving # possible. A: Tuple = [x for x in data if len(__lowercase ) > HDF5_OBJECT_HEADER_LIMIT] # Expecting this to never be true. if bad_attributes: raise RuntimeError( '''The following attributes cannot be saved to HDF5 file because ''' F"""they are larger than {HDF5_OBJECT_HEADER_LIMIT} """ F"""bytes: {bad_attributes}""" ) A: Any = np.asarray(__lowercase ) A: List[Any] = 1 A: Optional[int] = np.array_split(__lowercase , __lowercase ) # This will never loop forever thanks to the test above. while any(x.nbytes > HDF5_OBJECT_HEADER_LIMIT for x in chunked_data ): num_chunks += 1 A: Dict = np.array_split(__lowercase , __lowercase ) if num_chunks > 1: for chunk_id, chunk_data in enumerate(__lowercase ): A: Any = chunk_data else: A: Any = data def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: if name in group.attrs: A: Dict = [n.decode('''utf8''' ) if hasattr(__lowercase , '''decode''' ) else n for n in group.attrs[name]] else: A: Tuple = [] A: Tuple = 0 while "%s%d" % (name, chunk_id) in group.attrs: data.extend( [n.decode('''utf8''' ) if hasattr(__lowercase , '''decode''' ) else n for n in group.attrs['''%s%d''' % (name, chunk_id)]] ) chunk_id += 1 return data def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: def _expand_single_ad_tensor(__lowercase ): if isinstance(__lowercase , tf.Tensor ) and t.shape.rank == 1: return tf.expand_dims(__lowercase , axis=-1 ) return t return tf.nest.map_structure(_expand_single_ad_tensor , __lowercase )
319
'''simple docstring''' from typing import Any, Dict, List, Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from ..image_utils import load_image if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_OBJECT_DETECTION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = Dict[str, Any] UpperCamelCase = List[Prediction] @add_end_docstrings(UpperCAmelCase_ ) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : List[str] ) -> int: '''simple docstring''' super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if self.framework == "tf": raise ValueError(f"""The {self.__class__} is only available in PyTorch.""" ) requires_backends(self , '''vision''' ) self.check_model_type( dict(MODEL_FOR_OBJECT_DETECTION_MAPPING.items() + MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.items() ) ) def _snake_case ( self : int , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[Any]: '''simple docstring''' A: Any = {} if "threshold" in kwargs: A: List[Any] = kwargs['''threshold'''] return {}, {}, postprocess_kwargs def __call__( self : str , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Union[Predictions, List[Prediction]]: '''simple docstring''' return super().__call__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Union[str, Any]: '''simple docstring''' A: int = load_image(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = torch.IntTensor([[image.height, image.width]] ) A: Union[str, Any] = self.image_processor(images=[image] , return_tensors='''pt''' ) if self.tokenizer is not None: A: int = self.tokenizer(text=inputs['''words'''] , boxes=inputs['''boxes'''] , return_tensors='''pt''' ) A: Any = target_size return inputs def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : str ) -> List[Any]: '''simple docstring''' A: Tuple = model_inputs.pop('''target_size''' ) A: Tuple = self.model(**SCREAMING_SNAKE_CASE_ ) A: List[str] = outputs.__class__({'''target_size''': target_size, **outputs} ) if self.tokenizer is not None: A: Dict = model_inputs['''bbox'''] return model_outputs def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : str=0.9 ) -> Union[str, Any]: '''simple docstring''' A: List[Any] = model_outputs['''target_size'''] if self.tokenizer is not None: # This is a LayoutLMForTokenClassification variant. # The OCR got the boxes and the model classified the words. A , A: Union[str, Any] = target_size[0].tolist() def unnormalize(SCREAMING_SNAKE_CASE_ : str ): return self._get_bounding_box( torch.Tensor( [ (width * bbox[0] / 10_00), (height * bbox[1] / 10_00), (width * bbox[2] / 10_00), (height * bbox[3] / 10_00), ] ) ) A , A: Dict = model_outputs['''logits'''].squeeze(0 ).softmax(dim=-1 ).max(dim=-1 ) A: List[str] = [self.model.config.idalabel[prediction] for prediction in classes.tolist()] A: List[str] = [unnormalize(SCREAMING_SNAKE_CASE_ ) for bbox in model_outputs['''bbox'''].squeeze(0 )] A: Dict = ['''score''', '''label''', '''box'''] A: Optional[int] = [dict(zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) for vals in zip(scores.tolist() , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if vals[0] > threshold] else: # This is a regular ForObjectDetectionModel A: Any = self.image_processor.post_process_object_detection(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: List[str] = raw_annotations[0] A: List[Any] = raw_annotation['''scores'''] A: List[Any] = raw_annotation['''labels'''] A: int = raw_annotation['''boxes'''] A: Any = scores.tolist() A: List[Any] = [self.model.config.idalabel[label.item()] for label in labels] A: List[Any] = [self._get_bounding_box(SCREAMING_SNAKE_CASE_ ) for box in boxes] # {"scores": [...], ...} --> [{"score":x, ...}, ...] A: Tuple = ['''score''', '''label''', '''box'''] A: str = [ dict(zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) for vals in zip(raw_annotation['''scores'''] , raw_annotation['''labels'''] , raw_annotation['''boxes'''] ) ] return annotation def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : "torch.Tensor" ) -> Dict[str, int]: '''simple docstring''' if self.framework != "pt": raise ValueError('''The ObjectDetectionPipeline is only available in PyTorch.''' ) A , A , A , A: str = box.int().tolist() A: str = { '''xmin''': xmin, '''ymin''': ymin, '''xmax''': xmax, '''ymax''': ymax, } return bbox
319
1
'''simple docstring''' import secrets from random import shuffle from string import ascii_letters, ascii_lowercase, ascii_uppercase, digits, punctuation def SCREAMING_SNAKE_CASE( __lowercase = 8 ) -> str: A: Optional[int] = ascii_letters + digits + punctuation return "".join(secrets.choice(__lowercase ) for _ in range(__lowercase ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str: # Password Generator = full boot with random_number, random_letters, and # random_character FUNCTIONS # Put your code here... i -= len(__lowercase ) A: Optional[int] = i // 3 A: Any = i % 3 # chars = chars_incl + random_letters(ascii_letters, i / 3 + remainder) + # random_number(digits, i / 3) + random_characters(punctuation, i / 3) A: Union[str, Any] = ( chars_incl + random(__lowercase , quotient + remainder ) + random(__lowercase , __lowercase ) + random(__lowercase , __lowercase ) ) A: str = list(__lowercase ) shuffle(__lowercase ) return "".join(__lowercase ) # random is a generalised function for letters, characters and numbers def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str: return "".join(secrets.choice(__lowercase ) for _ in range(__lowercase ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str: pass # Put your code here... def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Tuple: pass # Put your code here... def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Any: pass # Put your code here... def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = 8 ) -> bool: if len(__lowercase ) < min_length: # Your Password must be at least 8 characters long return False A: Any = any(char in ascii_uppercase for char in password ) A: int = any(char in ascii_lowercase for char in password ) A: Optional[Any] = any(char in digits for char in password ) A: int = any(char in punctuation for char in password ) return upper and lower and num and spec_char # Passwords should contain UPPERCASE, lowerase # numbers, and special characters def SCREAMING_SNAKE_CASE( ) -> Dict: A: List[Any] = int(input('''Please indicate the max length of your password: ''' ).strip() ) A: Optional[int] = input( '''Please indicate the characters that must be in your password: ''' ).strip() print('''Password generated:''' , password_generator(__lowercase ) ) print( '''Alternative Password generated:''' , alternative_password_generator(__lowercase , __lowercase ) , ) print('''[If you are thinking of using this passsword, You better save it.]''' ) if __name__ == "__main__": main()
319
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''YituTech/conv-bert-base''': '''https://huggingface.co/YituTech/conv-bert-base/resolve/main/config.json''', '''YituTech/conv-bert-medium-small''': ( '''https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/config.json''' ), '''YituTech/conv-bert-small''': '''https://huggingface.co/YituTech/conv-bert-small/resolve/main/config.json''', # See all ConvBERT models at https://huggingface.co/models?filter=convbert } class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = """convbert""" def __init__( self : Dict , SCREAMING_SNAKE_CASE_ : Dict=3_05_22 , SCREAMING_SNAKE_CASE_ : int=7_68 , SCREAMING_SNAKE_CASE_ : List[str]=12 , SCREAMING_SNAKE_CASE_ : List[str]=12 , SCREAMING_SNAKE_CASE_ : Dict=30_72 , SCREAMING_SNAKE_CASE_ : Optional[int]="gelu" , SCREAMING_SNAKE_CASE_ : List[Any]=0.1 , SCREAMING_SNAKE_CASE_ : int=0.1 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=5_12 , SCREAMING_SNAKE_CASE_ : List[Any]=2 , SCREAMING_SNAKE_CASE_ : List[str]=0.02 , SCREAMING_SNAKE_CASE_ : int=1E-12 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=1 , SCREAMING_SNAKE_CASE_ : int=0 , SCREAMING_SNAKE_CASE_ : str=2 , SCREAMING_SNAKE_CASE_ : List[Any]=7_68 , SCREAMING_SNAKE_CASE_ : Optional[Any]=2 , SCREAMING_SNAKE_CASE_ : Any=9 , SCREAMING_SNAKE_CASE_ : Tuple=1 , SCREAMING_SNAKE_CASE_ : List[Any]=None , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> List[Any]: '''simple docstring''' super().__init__( pad_token_id=SCREAMING_SNAKE_CASE_ , bos_token_id=SCREAMING_SNAKE_CASE_ , eos_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Dict = vocab_size A: Tuple = hidden_size A: Optional[int] = num_hidden_layers A: List[str] = num_attention_heads A: int = intermediate_size A: int = hidden_act A: List[str] = hidden_dropout_prob A: int = attention_probs_dropout_prob A: Tuple = max_position_embeddings A: Any = type_vocab_size A: str = initializer_range A: Union[str, Any] = layer_norm_eps A: str = embedding_size A: Optional[int] = head_ratio A: List[Any] = conv_kernel_size A: List[Any] = num_groups A: Optional[int] = classifier_dropout class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' @property def _snake_case ( self : Optional[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": A: Tuple = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: A: List[str] = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis), ] )
319
1
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str: if not isinstance(__lowercase , __lowercase ): raise ValueError('''iterations must be defined as integers''' ) if not isinstance(__lowercase , __lowercase ) or not number >= 1: raise ValueError( '''starting number must be and integer and be more than 0''' ) if not iterations >= 1: raise ValueError('''Iterations must be done more than 0 times to play FizzBuzz''' ) A: Union[str, Any] = '''''' while number <= iterations: if number % 3 == 0: out += "Fizz" if number % 5 == 0: out += "Buzz" if 0 not in (number % 3, number % 5): out += str(__lowercase ) # print(out) number += 1 out += " " return out if __name__ == "__main__": import doctest doctest.testmod()
319
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE( __lowercase ) -> bool: if len(__lowercase ) < 2: raise ValueError('''Monogons and Digons are not polygons in the Euclidean space''' ) if any(i <= 0 for i in nums ): raise ValueError('''All values must be greater than 0''' ) A: Any = nums.copy() copy_nums.sort() return copy_nums[-1] < sum(copy_nums[:-1] ) if __name__ == "__main__": import doctest doctest.testmod()
319
1
'''simple docstring''' import os import pytest from transformers.dynamic_module_utils import get_imports UpperCamelCase = ''' import os ''' UpperCamelCase = ''' def foo(): import os return False ''' UpperCamelCase = ''' def foo(): def bar(): if True: import os return False return bar() ''' UpperCamelCase = ''' import os try: import bar except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os def foo(): try: import bar except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except (ImportError, AttributeError): raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except ImportError as e: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar import baz except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar import baz except ImportError: x = 1 raise ValueError() ''' UpperCamelCase = [ TOP_LEVEL_IMPORT, IMPORT_IN_FUNCTION, DEEPLY_NESTED_IMPORT, TOP_LEVEL_TRY_IMPORT, GENERIC_EXCEPT_IMPORT, MULTILINE_TRY_IMPORT, MULTILINE_BOTH_IMPORT, MULTIPLE_EXCEPTS_IMPORT, EXCEPT_AS_IMPORT, TRY_IMPORT_IN_FUNCTION, ] @pytest.mark.parametrize('''case''' , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Dict: A: Tuple = os.path.join(__lowercase , '''test_file.py''' ) with open(__lowercase , '''w''' ) as _tmp_file: _tmp_file.write(__lowercase ) A: List[Any] = get_imports(__lowercase ) assert parsed_imports == ["os"]
319
'''simple docstring''' # tests directory-specific settings - this file is run automatically # by pytest before any tests are run import doctest import sys import warnings from os.path import abspath, dirname, join import _pytest from transformers.testing_utils import HfDoctestModule, HfDocTestParser # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. UpperCamelCase = abspath(join(dirname(__file__), '''src''')) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action='''ignore''', category=FutureWarning) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: config.addinivalue_line( '''markers''' , '''is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested''' ) config.addinivalue_line( '''markers''' , '''is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested''' ) config.addinivalue_line('''markers''' , '''is_pipeline_test: mark test to run only when pipelines are tested''' ) config.addinivalue_line('''markers''' , '''is_staging_test: mark test to run only in the staging environment''' ) config.addinivalue_line('''markers''' , '''accelerate_tests: mark test that require accelerate''' ) config.addinivalue_line('''markers''' , '''tool_tests: mark the tool tests that are run on their specific schedule''' ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: from transformers.testing_utils import pytest_terminal_summary_main A: Optional[int] = terminalreporter.config.getoption('''--make-reports''' ) if make_reports: pytest_terminal_summary_main(__lowercase , id=__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Any: # If no tests are collected, pytest exists with code 5, which makes the CI fail. if exitstatus == 5: A: Tuple = 0 # Doctest custom flag to ignore output. UpperCamelCase = doctest.register_optionflag('''IGNORE_RESULT''') UpperCamelCase = doctest.OutputChecker class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ) -> str: '''simple docstring''' if IGNORE_RESULT & optionflags: return True return OutputChecker.check_output(self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase = CustomOutputChecker UpperCamelCase = HfDoctestModule UpperCamelCase = HfDocTestParser
319
1
'''simple docstring''' import argparse from pathlib import Path import fairseq import torch from fairseq.models.xmod import XMODModel as FairseqXmodModel from packaging import version from transformers import XmodConfig, XmodForMaskedLM, XmodForSequenceClassification from transformers.utils import logging if version.parse(fairseq.__version__) < version.parse('''0.12.2'''): raise Exception('''requires fairseq >= 0.12.2''') if version.parse(fairseq.__version__) > version.parse('''2'''): raise Exception('''requires fairseq < v2''') logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = '''Hello, World!''' UpperCamelCase = '''en_XX''' def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Any: A: List[str] = Path('''data_bin''' ) A: str = FairseqXmodModel.from_pretrained( model_name_or_path=str(Path(__lowercase ).parent ) , checkpoint_file=Path(__lowercase ).name , _name='''xmod_base''' , arch='''xmod_base''' , task='''multilingual_masked_lm''' , data_name_or_path=str(__lowercase ) , bpe='''sentencepiece''' , sentencepiece_model=str(Path(__lowercase ).parent / '''sentencepiece.bpe.model''' ) , src_dict=str(data_dir / '''dict.txt''' ) , ) xmod.eval() # disable dropout print(__lowercase ) A: Any = xmod.model.encoder.sentence_encoder A: List[Any] = XmodConfig( vocab_size=xmod_sent_encoder.embed_tokens.num_embeddings , hidden_size=xmod.cfg.model.encoder_embed_dim , num_hidden_layers=xmod.cfg.model.encoder_layers , num_attention_heads=xmod.cfg.model.encoder_attention_heads , intermediate_size=xmod.cfg.model.encoder_ffn_embed_dim , max_position_embeddings=5_1_4 , type_vocab_size=1 , layer_norm_eps=1E-5 , pre_norm=xmod.cfg.model.encoder_normalize_before , adapter_reduction_factor=getattr(xmod.cfg.model , '''bottleneck''' , 2 ) , adapter_layer_norm=xmod.cfg.model.adapter_layer_norm , adapter_reuse_layer_norm=xmod.cfg.model.adapter_reuse_layer_norm , ln_before_adapter=xmod.cfg.model.ln_before_adapter , languages=xmod.cfg.model.languages , ) if classification_head: A: str = xmod.model.classification_heads['''mnli'''].out_proj.weight.shape[0] print('''Our X-MOD config:''' , __lowercase ) A: Any = XmodForSequenceClassification(__lowercase ) if classification_head else XmodForMaskedLM(__lowercase ) model.eval() # Now let's copy all the weights. # Embeddings A: List[str] = xmod_sent_encoder.embed_tokens.weight A: List[str] = xmod_sent_encoder.embed_positions.weight A: Dict = torch.zeros_like( model.roberta.embeddings.token_type_embeddings.weight ) # just zero them out b/c xmod doesn't use them. A: str = xmod_sent_encoder.layernorm_embedding.weight A: List[Any] = xmod_sent_encoder.layernorm_embedding.bias for i in range(config.num_hidden_layers ): # Encoder: start of layer A: Any = model.roberta.encoder.layer[i] A: Union[str, Any] = xmod_sent_encoder.layers[i] # self attention A: str = layer.attention.self if not ( xmod_layer.self_attn.k_proj.weight.data.shape == xmod_layer.self_attn.q_proj.weight.data.shape == xmod_layer.self_attn.v_proj.weight.data.shape == torch.Size((config.hidden_size, config.hidden_size) ) ): raise AssertionError('''Dimensions of self-attention weights do not match.''' ) A: int = xmod_layer.self_attn.q_proj.weight A: Union[str, Any] = xmod_layer.self_attn.q_proj.bias A: Dict = xmod_layer.self_attn.k_proj.weight A: Optional[int] = xmod_layer.self_attn.k_proj.bias A: Union[str, Any] = xmod_layer.self_attn.v_proj.weight A: Union[str, Any] = xmod_layer.self_attn.v_proj.bias # self-attention output A: Tuple = layer.attention.output if self_output.dense.weight.shape != xmod_layer.self_attn.out_proj.weight.shape: raise AssertionError('''Dimensions of self-attention output weights do not match.''' ) A: Tuple = xmod_layer.self_attn.out_proj.weight A: Dict = xmod_layer.self_attn.out_proj.bias A: Union[str, Any] = xmod_layer.self_attn_layer_norm.weight A: Optional[int] = xmod_layer.self_attn_layer_norm.bias # intermediate A: Optional[Any] = layer.intermediate if intermediate.dense.weight.shape != xmod_layer.fca.weight.shape: raise AssertionError('''Dimensions of intermediate weights do not match.''' ) A: Union[str, Any] = xmod_layer.fca.weight A: Union[str, Any] = xmod_layer.fca.bias # output A: Optional[int] = layer.output if bert_output.dense.weight.shape != xmod_layer.fca.weight.shape: raise AssertionError('''Dimensions of feed-forward weights do not match.''' ) A: Union[str, Any] = xmod_layer.fca.weight A: Optional[Any] = xmod_layer.fca.bias A: Optional[int] = xmod_layer.final_layer_norm.weight A: Optional[Any] = xmod_layer.final_layer_norm.bias if bert_output.adapter_layer_norm is not None: A: Any = xmod_layer.adapter_layer_norm.weight A: List[Any] = xmod_layer.adapter_layer_norm.bias if sorted(bert_output.adapter_modules.keys() ) != sorted(xmod_layer.adapter_modules.keys() ): raise AssertionError('''Lists of language adapters do not match.''' ) for lang_code, adapter in xmod_layer.adapter_modules.items(): A: Union[str, Any] = bert_output.adapter_modules[lang_code] A: Tuple = xmod_layer.adapter_modules[lang_code] A: str = from_adapter.fca.weight A: int = from_adapter.fca.bias A: Tuple = from_adapter.fca.weight A: List[str] = from_adapter.fca.bias # end of layer if xmod_sent_encoder.layer_norm is not None: A: str = xmod_sent_encoder.layer_norm.weight A: Dict = xmod_sent_encoder.layer_norm.bias if classification_head: A: Tuple = xmod.model.classification_heads['''mnli'''].dense.weight A: Optional[int] = xmod.model.classification_heads['''mnli'''].dense.bias A: Optional[int] = xmod.model.classification_heads['''mnli'''].out_proj.weight A: int = xmod.model.classification_heads['''mnli'''].out_proj.bias else: # LM Head A: Optional[int] = xmod.model.encoder.lm_head.dense.weight A: Union[str, Any] = xmod.model.encoder.lm_head.dense.bias A: Dict = xmod.model.encoder.lm_head.layer_norm.weight A: Optional[int] = xmod.model.encoder.lm_head.layer_norm.bias A: List[str] = xmod.model.encoder.lm_head.weight A: int = xmod.model.encoder.lm_head.bias # Let's check that we get the same results. A: Dict = xmod.encode(__lowercase ).unsqueeze(0 ) # batch of size 1 model.roberta.set_default_language(__lowercase ) A: Union[str, Any] = model(__lowercase )[0] if classification_head: A: int = xmod.model.classification_heads['''mnli'''](xmod.extract_features(__lowercase ) ) else: A: str = xmod.model(__lowercase , lang_id=[SAMPLE_LANGUAGE] )[0] print(our_output.shape , their_output.shape ) A: List[Any] = torch.max(torch.abs(our_output - their_output ) ).item() print(F"""max_absolute_diff = {max_absolute_diff}""" ) # ~ 1e-7 A: List[str] = torch.allclose(__lowercase , __lowercase , atol=1E-3 ) print('''Do both models output the same tensors?''' , '''🔥''' if success else '''💩''' ) if not success: raise Exception('''Something went wRoNg''' ) Path(__lowercase ).mkdir(parents=__lowercase , exist_ok=__lowercase ) print(F"""Saving model to {pytorch_dump_folder_path}""" ) model.save_pretrained(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--xmod_checkpoint_path''', default=None, type=str, required=True, help='''Path the official PyTorch dump.''' ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--classification_head''', action='''store_true''', help='''Whether to convert a final classification head.''' ) UpperCamelCase = parser.parse_args() convert_xmod_checkpoint_to_pytorch( args.xmod_checkpoint_path, args.pytorch_dump_folder_path, args.classification_head )
319
'''simple docstring''' import heapq import sys import numpy as np UpperCamelCase = tuple[int, int] class lowerCAmelCase_ : '''simple docstring''' def __init__( self : List[Any] ) -> str: '''simple docstring''' A: Any = [] A: int = set() def _snake_case ( self : Optional[Any] ) -> int: '''simple docstring''' if not self.empty(): return self.elements[0][0] else: return float('''inf''' ) def _snake_case ( self : List[str] ) -> List[Any]: '''simple docstring''' return len(self.elements ) == 0 def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Any ) -> List[Any]: '''simple docstring''' if item not in self.set: heapq.heappush(self.elements , (priority, item) ) self.set.add(SCREAMING_SNAKE_CASE_ ) else: # update # print("update", item) A: Optional[int] = [] ((A) , (A)): str = heapq.heappop(self.elements ) while x != item: temp.append((pri, x) ) ((A) , (A)): int = heapq.heappop(self.elements ) temp.append((priority, item) ) for pro, xxx in temp: heapq.heappush(self.elements , (pro, xxx) ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : str ) -> Any: '''simple docstring''' if item in self.set: self.set.remove(SCREAMING_SNAKE_CASE_ ) A: str = [] ((A) , (A)): List[str] = heapq.heappop(self.elements ) while x != item: temp.append((pro, x) ) ((A) , (A)): Any = heapq.heappop(self.elements ) for prito, yyy in temp: heapq.heappush(self.elements , (prito, yyy) ) def _snake_case ( self : List[Any] ) -> Optional[int]: '''simple docstring''' return self.elements[0][1] def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' ((A) , (A)): Dict = heapq.heappop(self.elements ) self.set.remove(SCREAMING_SNAKE_CASE_ ) return (priority, item) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: # euclidean distance A: List[str] = np.array(__lowercase ) A: Optional[int] = np.array(__lowercase ) return np.linalg.norm(a - b ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> int: # integer division by time variable return consistent_heuristic(__lowercase , __lowercase ) // t def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[Any]: # manhattan distance return abs(p[0] - goal[0] ) + abs(p[1] - goal[1] ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase ) -> List[Any]: A: int = g_function[start] + Wa * heuristics[i](__lowercase , __lowercase ) return ans def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Optional[int]: A: Union[str, Any] = np.chararray((n, n) ) for i in range(__lowercase ): for j in range(__lowercase ): A: Union[str, Any] = '''*''' for i in range(__lowercase ): for j in range(__lowercase ): if (j, (n - 1) - i) in blocks: A: Optional[Any] = '''#''' A: Tuple = '''-''' A: List[str] = back_pointer[goal] while x != start: ((A) , (A)): Tuple = x # print(x) A: List[str] = '''-''' A: str = back_pointer[x] A: Dict = '''-''' for i in range(__lowercase ): for j in range(__lowercase ): if (i, j) == (0, n - 1): print(grid[i][j] , end=''' ''' ) print('''<-- End position''' , end=''' ''' ) else: print(grid[i][j] , end=''' ''' ) print() print('''^''' ) print('''Start position''' ) print() print('''# is an obstacle''' ) print('''- is the path taken by algorithm''' ) print('''PATH TAKEN BY THE ALGORITHM IS:-''' ) A: List[str] = back_pointer[goal] while x != start: print(__lowercase , end=''' ''' ) A: Optional[int] = back_pointer[x] print(__lowercase ) sys.exit() def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: if p[0] < 0 or p[0] > n - 1: return False if p[1] < 0 or p[1] > n - 1: return False return True def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) -> Union[str, Any]: for itera in range(__lowercase ): open_list[itera].remove_element(__lowercase ) # print("s", s) # print("j", j) ((A) , (A)): Tuple = s A: Optional[Any] = (x - 1, y) A: str = (x + 1, y) A: List[Any] = (x, y + 1) A: int = (x, y - 1) for neighbours in [left, right, up, down]: if neighbours not in blocks: if valid(__lowercase ) and neighbours not in visited: # print("neighbour", neighbours) visited.add(__lowercase ) A: int = -1 A: int = float('''inf''' ) if valid(__lowercase ) and g_function[neighbours] > g_function[s] + 1: A: List[str] = g_function[s] + 1 A: List[str] = s if neighbours not in close_list_anchor: open_list[0].put(__lowercase , key(__lowercase , 0 , __lowercase , __lowercase ) ) if neighbours not in close_list_inad: for var in range(1 , __lowercase ): if key(__lowercase , __lowercase , __lowercase , __lowercase ) <= Wa * key( __lowercase , 0 , __lowercase , __lowercase ): open_list[j].put( __lowercase , key(__lowercase , __lowercase , __lowercase , __lowercase ) ) def SCREAMING_SNAKE_CASE( ) -> Tuple: A: str = [] for x in range(1 , 5 ): for y in range(1 , 6 ): some_list.append((x, y) ) for x in range(1_5 , 2_0 ): some_list.append((x, 1_7) ) for x in range(1_0 , 1_9 ): for y in range(1 , 1_5 ): some_list.append((x, y) ) # L block for x in range(1 , 4 ): for y in range(1_2 , 1_9 ): some_list.append((x, y) ) for x in range(3 , 1_3 ): for y in range(1_6 , 1_9 ): some_list.append((x, y) ) return some_list UpperCamelCase = {0: consistent_heuristic, 1: heuristic_a, 2: heuristic_a} UpperCamelCase = [ (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1), (15, 1), (16, 1), (17, 1), (18, 1), (19, 1), ] UpperCamelCase = make_common_ground() UpperCamelCase = blocks_blk # hyper parameters UpperCamelCase = 1 UpperCamelCase = 1 UpperCamelCase = 20 UpperCamelCase = 3 # one consistent and two other inconsistent # start and end destination UpperCamelCase = (0, 0) UpperCamelCase = (n - 1, n - 1) UpperCamelCase = 1 def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: A: int = {start: 0, goal: float('''inf''' )} A: Union[str, Any] = {start: -1, goal: -1} A: List[Any] = [] A: Union[str, Any] = set() for i in range(__lowercase ): open_list.append(PriorityQueue() ) open_list[i].put(__lowercase , key(__lowercase , __lowercase , __lowercase , __lowercase ) ) A: list[int] = [] A: list[int] = [] while open_list[0].minkey() < float('''inf''' ): for i in range(1 , __lowercase ): # print(open_list[0].minkey(), open_list[i].minkey()) if open_list[i].minkey() <= Wa * open_list[0].minkey(): global t t += 1 if g_function[goal] <= open_list[i].minkey(): if g_function[goal] < float('''inf''' ): do_something(__lowercase , __lowercase , __lowercase ) else: A , A: Union[str, Any] = open_list[i].top_show() visited.add(__lowercase ) expand_state( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) close_list_inad.append(__lowercase ) else: if g_function[goal] <= open_list[0].minkey(): if g_function[goal] < float('''inf''' ): do_something(__lowercase , __lowercase , __lowercase ) else: A: Union[str, Any] = open_list[0].top_show() visited.add(__lowercase ) expand_state( __lowercase , 0 , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) close_list_anchor.append(__lowercase ) print('''No path found to goal''' ) print() for i in range(n - 1 , -1 , -1 ): for j in range(__lowercase ): if (j, i) in blocks: print('''#''' , end=''' ''' ) elif (j, i) in back_pointer: if (j, i) == (n - 1, n - 1): print('''*''' , end=''' ''' ) else: print('''-''' , end=''' ''' ) else: print('''*''' , end=''' ''' ) if (j, i) == (n - 1, n - 1): print('''<-- End position''' , end=''' ''' ) print() print('''^''' ) print('''Start position''' ) print() print('''# is an obstacle''' ) print('''- is the path taken by algorithm''' ) if __name__ == "__main__": multi_a_star(start, goal, n_heuristic)
319
1
'''simple docstring''' from collections import OrderedDict from typing import Any, List, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''EleutherAI/gpt-j-6B''': '''https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json''', # See all GPT-J models at https://huggingface.co/models?filter=gpt_j } class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : List[str] = """gptj""" UpperCamelCase_ : Tuple = { """max_position_embeddings""": """n_positions""", """hidden_size""": """n_embd""", """num_attention_heads""": """n_head""", """num_hidden_layers""": """n_layer""", } def __init__( self : Any , SCREAMING_SNAKE_CASE_ : int=5_04_00 , SCREAMING_SNAKE_CASE_ : Any=20_48 , SCREAMING_SNAKE_CASE_ : Optional[int]=40_96 , SCREAMING_SNAKE_CASE_ : List[str]=28 , SCREAMING_SNAKE_CASE_ : List[str]=16 , SCREAMING_SNAKE_CASE_ : int=64 , SCREAMING_SNAKE_CASE_ : Tuple=None , SCREAMING_SNAKE_CASE_ : Union[str, Any]="gelu_new" , SCREAMING_SNAKE_CASE_ : str=0.0 , SCREAMING_SNAKE_CASE_ : Tuple=0.0 , SCREAMING_SNAKE_CASE_ : Dict=0.0 , SCREAMING_SNAKE_CASE_ : Any=1E-5 , SCREAMING_SNAKE_CASE_ : Optional[Any]=0.02 , SCREAMING_SNAKE_CASE_ : Dict=True , SCREAMING_SNAKE_CASE_ : Tuple=5_02_56 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=5_02_56 , SCREAMING_SNAKE_CASE_ : str=False , **SCREAMING_SNAKE_CASE_ : Tuple , ) -> Optional[Any]: '''simple docstring''' A: Optional[Any] = vocab_size A: List[str] = n_positions A: Dict = n_embd A: str = n_layer A: List[str] = n_head A: Optional[Any] = n_inner A: List[Any] = rotary_dim A: Any = activation_function A: Dict = resid_pdrop A: List[str] = embd_pdrop A: int = attn_pdrop A: int = layer_norm_epsilon A: Any = initializer_range A: Optional[int] = use_cache A: Tuple = bos_token_id A: int = eos_token_id super().__init__( bos_token_id=SCREAMING_SNAKE_CASE_ , eos_token_id=SCREAMING_SNAKE_CASE_ , tie_word_embeddings=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : PretrainedConfig , SCREAMING_SNAKE_CASE_ : str = "default" , SCREAMING_SNAKE_CASE_ : List[PatchingSpec] = None , SCREAMING_SNAKE_CASE_ : bool = False , ) -> List[Any]: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ , task=SCREAMING_SNAKE_CASE_ , patching_specs=SCREAMING_SNAKE_CASE_ , use_past=SCREAMING_SNAKE_CASE_ ) if not getattr(self._config , '''pad_token_id''' , SCREAMING_SNAKE_CASE_ ): # TODO: how to do that better? A: List[str] = 0 @property def _snake_case ( self : Union[str, Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' A: str = OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}} ) if self.use_past: self.fill_with_past_key_values_(SCREAMING_SNAKE_CASE_ , direction='''inputs''' ) A: Optional[Any] = {0: '''batch''', 1: '''past_sequence + sequence'''} else: A: Dict = {0: '''batch''', 1: '''sequence'''} return common_inputs @property def _snake_case ( self : int ) -> int: '''simple docstring''' return self._config.n_layer @property def _snake_case ( self : int ) -> int: '''simple docstring''' return self._config.n_head def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : PreTrainedTokenizer , SCREAMING_SNAKE_CASE_ : int = -1 , SCREAMING_SNAKE_CASE_ : int = -1 , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' A: List[Any] = super(SCREAMING_SNAKE_CASE_ , self ).generate_dummy_inputs( SCREAMING_SNAKE_CASE_ , batch_size=SCREAMING_SNAKE_CASE_ , seq_length=SCREAMING_SNAKE_CASE_ , is_pair=SCREAMING_SNAKE_CASE_ , framework=SCREAMING_SNAKE_CASE_ ) # We need to order the input in the way they appears in the forward() A: Optional[Any] = OrderedDict({'''input_ids''': common_inputs['''input_ids''']} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch A , A: Tuple = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values A: Tuple = seqlen + 2 A: Tuple = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) A: Union[str, Any] = [ (torch.zeros(SCREAMING_SNAKE_CASE_ ), torch.zeros(SCREAMING_SNAKE_CASE_ )) for _ in range(self.num_layers ) ] A: int = common_inputs['''attention_mask'''] if self.use_past: A: List[str] = ordered_inputs['''attention_mask'''].dtype A: Dict = torch.cat( [ordered_inputs['''attention_mask'''], torch.ones(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , dtype=SCREAMING_SNAKE_CASE_ )] , dim=1 ) return ordered_inputs @property def _snake_case ( self : List[str] ) -> int: '''simple docstring''' return 13
319
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase = 1 , __lowercase = 1_0_0_0 ) -> int: A: Any = 1 A: Optional[Any] = 0 for divide_by_number in range(__lowercase , digit + 1 ): A: list[int] = [] A: List[Any] = numerator for _ in range(1 , digit + 1 ): if now_divide in has_been_divided: if longest_list_length < len(__lowercase ): A: Any = len(__lowercase ) A: Dict = divide_by_number else: has_been_divided.append(__lowercase ) A: str = now_divide * 1_0 % divide_by_number return the_digit # Tests if __name__ == "__main__": import doctest doctest.testmod()
319
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase = { '''configuration_clipseg''': [ '''CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''CLIPSegConfig''', '''CLIPSegTextConfig''', '''CLIPSegVisionConfig''', ], '''processing_clipseg''': ['''CLIPSegProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST''', '''CLIPSegModel''', '''CLIPSegPreTrainedModel''', '''CLIPSegTextModel''', '''CLIPSegVisionModel''', '''CLIPSegForImageSegmentation''', ] if TYPE_CHECKING: from .configuration_clipseg import ( CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPSegConfig, CLIPSegTextConfig, CLIPSegVisionConfig, ) from .processing_clipseg import CLIPSegProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clipseg import ( CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPSegForImageSegmentation, CLIPSegModel, CLIPSegPreTrainedModel, CLIPSegTextModel, CLIPSegVisionModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) UpperCamelCase = { '''configuration_vision_encoder_decoder''': ['''VisionEncoderDecoderConfig''', '''VisionEncoderDecoderOnnxConfig'''] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''VisionEncoderDecoderModel'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''TFVisionEncoderDecoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''FlaxVisionEncoderDecoderModel'''] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
1
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) -> None: A: Any = len(__lowercase ) # If row is equal to the size of the board it means there are a queen in each row in # the current board (possible_board) if row == n: # We convert the variable possible_board that looks like this: [1, 3, 0, 2] to # this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . '] boards.append(['''. ''' * i + '''Q ''' + '''. ''' * (n - 1 - i) for i in possible_board] ) return # We iterate each column in the row to find all possible results in each row for col in range(__lowercase ): # We apply that we learned previously. First we check that in the current board # (possible_board) there are not other same value because if there is it means # that there are a collision in vertical. Then we apply the two formulas we # learned before: # # 45º: y - x = b or 45: row - col = b # 135º: y + x = b or row + col = b. # # And we verify if the results of this two formulas not exist in their variables # respectively. (diagonal_right_collisions, diagonal_left_collisions) # # If any or these are True it means there is a collision so we continue to the # next value in the for loop. if ( col in possible_board or row - col in diagonal_right_collisions or row + col in diagonal_left_collisions ): continue # If it is False we call dfs function again and we update the inputs depth_first_search( [*possible_board, col] , [*diagonal_right_collisions, row - col] , [*diagonal_left_collisions, row + col] , __lowercase , __lowercase , ) def SCREAMING_SNAKE_CASE( __lowercase ) -> None: A: list[list[str]] = [] depth_first_search([] , [] , [] , __lowercase , __lowercase ) # Print all the boards for board in boards: for column in board: print(__lowercase ) print('''''' ) print(len(__lowercase ) , '''solutions were found.''' ) if __name__ == "__main__": import doctest doctest.testmod() n_queens_solution(4)
319
'''simple docstring''' import fire from utils import calculate_rouge, save_json def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=None , **__lowercase ) -> Any: A: Any = [x.strip() for x in open(__lowercase ).readlines()] A: Dict = [x.strip() for x in open(__lowercase ).readlines()][: len(__lowercase )] A: Union[str, Any] = calculate_rouge(__lowercase , __lowercase , **__lowercase ) if save_path is not None: save_json(__lowercase , __lowercase , indent=__lowercase ) return metrics # these print nicely if __name__ == "__main__": fire.Fire(calculate_rouge_path)
319
1
'''simple docstring''' from __future__ import annotations import copy import tempfile import unittest from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available from transformers.testing_utils import ( DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tensorflow_probability, require_tf, slow, ) from ..bert.test_modeling_bert import BertModelTester if is_tf_available(): from transformers import ( TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelForTableQuestionAnswering, TFAutoModelForTokenClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFFunnelBaseModel, TFFunnelModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, TFTapasForQuestionAnswering, ) from transformers.models.auto.modeling_tf_auto import ( TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_MAPPING, ) from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = """new-model""" if is_tf_available(): class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : str = NewModelConfig @require_tf class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' @slow def _snake_case ( self : Optional[int] ) -> Optional[int]: '''simple docstring''' A: int = '''bert-base-cased''' A: Optional[int] = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Tuple = TFAutoModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : str ) -> str: '''simple docstring''' A: Tuple = '''bert-base-cased''' A: str = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Optional[int] = TFAutoModelForPreTraining.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Tuple ) -> Dict: '''simple docstring''' for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A: Optional[Any] = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = TFAutoModelForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE_ ) A , A: Dict = TFAutoModelForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE_ , output_loading_info=SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : str ) -> List[Any]: '''simple docstring''' for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A: List[str] = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: List[Any] = TFAutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A: List[str] = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: str = TFAutoModelForMaskedLM.from_pretrained(SCREAMING_SNAKE_CASE_ ) A , A: int = TFAutoModelForMaskedLM.from_pretrained(SCREAMING_SNAKE_CASE_ , output_loading_info=SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Any ) -> Tuple: '''simple docstring''' for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A: Union[str, Any] = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: str = TFAutoModelForSeqaSeqLM.from_pretrained(SCREAMING_SNAKE_CASE_ ) A , A: Any = TFAutoModelForSeqaSeqLM.from_pretrained(SCREAMING_SNAKE_CASE_ , output_loading_info=SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Tuple ) -> Union[str, Any]: '''simple docstring''' for model_name in ["bert-base-uncased"]: A: Any = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Tuple = TFAutoModelForSequenceClassification.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Optional[int] ) -> Any: '''simple docstring''' for model_name in ["bert-base-uncased"]: A: Any = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Optional[int] = TFAutoModelForQuestionAnswering.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @slow @require_tensorflow_probability def _snake_case ( self : List[str] ) -> Any: '''simple docstring''' for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]: A: int = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Tuple = TFAutoModelForTableQuestionAnswering.from_pretrained(SCREAMING_SNAKE_CASE_ ) A , A: Optional[Any] = TFAutoModelForTableQuestionAnswering.from_pretrained( SCREAMING_SNAKE_CASE_ , output_loading_info=SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[Any] ) -> Any: '''simple docstring''' A: Optional[Any] = TFAutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertEqual(model.num_parameters() , 1_44_10 ) self.assertEqual(model.num_parameters(only_trainable=SCREAMING_SNAKE_CASE_ ) , 1_44_10 ) def _snake_case ( self : Tuple ) -> List[Any]: '''simple docstring''' A: List[str] = TFAutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertEqual(model.num_parameters() , 1_44_10 ) self.assertEqual(model.num_parameters(only_trainable=SCREAMING_SNAKE_CASE_ ) , 1_44_10 ) def _snake_case ( self : List[Any] ) -> Optional[int]: '''simple docstring''' A: str = TFAutoModel.from_pretrained('''sgugger/funnel-random-tiny''' ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: int = copy.deepcopy(model.config ) A: Optional[Any] = ['''FunnelBaseModel'''] A: Union[str, Any] = TFAutoModel.from_config(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(SCREAMING_SNAKE_CASE_ ) A: Any = TFAutoModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] ) -> Union[str, Any]: '''simple docstring''' try: AutoConfig.register('''new-model''' , SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = [ TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSequenceClassification, TFAutoModelForTokenClassification, ] for auto_class in auto_classes: with self.subTest(auto_class.__name__ ): # Wrong config class will raise an error with self.assertRaises(SCREAMING_SNAKE_CASE_ ): auto_class.register(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) auto_class.register(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(SCREAMING_SNAKE_CASE_ ): auto_class.register(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Now that the config is registered, it can be used as any other config with the auto-API A: Optional[int] = BertModelTester(self ).get_config() A: List[str] = NewModelConfig(**tiny_config.to_dict() ) A: Any = auto_class.from_config(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(SCREAMING_SNAKE_CASE_ ) A: Dict = auto_class.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) finally: if "new-model" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["new-model"] for mapping in ( TF_MODEL_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, ): if NewModelConfig in mapping._extra_content: del mapping._extra_content[NewModelConfig] def _snake_case ( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' with self.assertRaisesRegex( SCREAMING_SNAKE_CASE_ , '''bert-base is not a local folder and is not a valid model identifier''' ): A: List[Any] = TFAutoModel.from_pretrained('''bert-base''' ) def _snake_case ( self : Union[str, Any] ) -> List[Any]: '''simple docstring''' with self.assertRaisesRegex( SCREAMING_SNAKE_CASE_ , R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): A: int = TFAutoModel.from_pretrained(SCREAMING_SNAKE_CASE_ , revision='''aaaaaa''' ) def _snake_case ( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' with self.assertRaisesRegex( SCREAMING_SNAKE_CASE_ , '''hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin''' , ): A: Dict = TFAutoModel.from_pretrained('''hf-internal-testing/config-no-model''' ) def _snake_case ( self : Union[str, Any] ) -> int: '''simple docstring''' with self.assertRaisesRegex(SCREAMING_SNAKE_CASE_ , '''Use `from_pt=True` to load this model''' ): A: Optional[int] = TFAutoModel.from_pretrained('''hf-internal-testing/tiny-bert-pt-only''' ) def _snake_case ( self : Dict ) -> int: '''simple docstring''' A: Optional[int] = TFAutoModel.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) with RequestCounter() as counter: A: Union[str, Any] = TFAutoModel.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 ) # With a sharded checkpoint A: List[str] = TFAutoModel.from_pretrained('''ArthurZ/tiny-random-bert-sharded''' ) with RequestCounter() as counter: A: str = TFAutoModel.from_pretrained('''ArthurZ/tiny-random-bert-sharded''' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 )
319
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = 0 ) -> list: A: Dict = length or len(__lowercase ) A: Dict = False for i in range(length - 1 ): if list_data[i] > list_data[i + 1]: A , A: Tuple = list_data[i + 1], list_data[i] A: Union[str, Any] = True return list_data if not swapped else bubble_sort(__lowercase , length - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
319
1
'''simple docstring''' import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin from .feature_extraction_wavaveca import WavaVecaFeatureExtractor from .tokenization_wavaveca import WavaVecaCTCTokenizer class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : List[str] = """Wav2Vec2FeatureExtractor""" UpperCamelCase_ : List[Any] = """AutoTokenizer""" def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Any: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Any = self.feature_extractor A: Any = False @classmethod def _snake_case ( cls : List[str] , SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Any ) -> str: '''simple docstring''' try: return super().from_pretrained(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) except OSError: warnings.warn( f"""Loading a tokenizer inside {cls.__name__} from a config that does not""" ''' include a `tokenizer_class` attribute is deprecated and will be ''' '''removed in v5. Please add `\'tokenizer_class\': \'Wav2Vec2CTCTokenizer\'`''' ''' attribute to either your `config.json` or `tokenizer_config.json` ''' '''file to suppress this warning: ''' , SCREAMING_SNAKE_CASE_ , ) A: List[Any] = WavaVecaFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) A: Tuple = WavaVecaCTCTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) return cls(feature_extractor=SCREAMING_SNAKE_CASE_ , tokenizer=SCREAMING_SNAKE_CASE_ ) def __call__( self : int , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> str: '''simple docstring''' if self._in_target_context_manager: return self.current_processor(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if "raw_speech" in kwargs: warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.''' ) A: Optional[int] = kwargs.pop('''raw_speech''' ) else: A: int = kwargs.pop('''audio''' , SCREAMING_SNAKE_CASE_ ) A: Tuple = kwargs.pop('''sampling_rate''' , SCREAMING_SNAKE_CASE_ ) A: int = kwargs.pop('''text''' , SCREAMING_SNAKE_CASE_ ) if len(SCREAMING_SNAKE_CASE_ ) > 0: A: Dict = args[0] A: int = args[1:] if audio is None and text is None: raise ValueError('''You need to specify either an `audio` or `text` input to process.''' ) if audio is not None: A: Optional[Any] = self.feature_extractor(SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ , sampling_rate=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if text is not None: A: List[Any] = self.tokenizer(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if text is None: return inputs elif audio is None: return encodings else: A: Optional[int] = encodings['''input_ids'''] return inputs def _snake_case ( self : Optional[int] , *SCREAMING_SNAKE_CASE_ : Tuple , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' if self._in_target_context_manager: return self.current_processor.pad(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) A: Optional[int] = kwargs.pop('''input_features''' , SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = kwargs.pop('''labels''' , SCREAMING_SNAKE_CASE_ ) if len(SCREAMING_SNAKE_CASE_ ) > 0: A: Any = args[0] A: int = args[1:] if input_features is not None: A: Dict = self.feature_extractor.pad(SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if labels is not None: A: Dict = self.tokenizer.pad(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if labels is None: return input_features elif input_features is None: return labels else: A: List[str] = labels['''input_ids'''] return input_features def _snake_case ( self : Any , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Optional[Any]: '''simple docstring''' return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : str ) -> Dict: '''simple docstring''' return self.tokenizer.decode(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) @contextmanager def _snake_case ( self : Optional[int] ) -> Dict: '''simple docstring''' warnings.warn( '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ''' '''labels by using the argument `text` of the regular `__call__` method (either in the same call as ''' '''your audio inputs, or in a separate call.''' ) A: Optional[int] = True A: Union[str, Any] = self.tokenizer yield A: int = self.feature_extractor A: Dict = False
319
'''simple docstring''' import argparse from collections import OrderedDict from pathlib import Path import torch from transformers import ( VisualBertConfig, VisualBertForMultipleChoice, VisualBertForPreTraining, VisualBertForQuestionAnswering, VisualBertForVisualReasoning, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = [ ('''bert.bert''', '''visual_bert'''), ('''bert.cls''', '''cls'''), ('''bert.classifier''', '''cls'''), ('''token_type_embeddings_visual''', '''visual_token_type_embeddings'''), ('''position_embeddings_visual''', '''visual_position_embeddings'''), ('''projection''', '''visual_projection'''), ] UpperCamelCase = [ '''nlvr2_coco_pre_trained.th''', '''nlvr2_fine_tuned.th''', '''nlvr2_pre_trained.th''', '''vcr_coco_pre_train.th''', '''vcr_fine_tune.th''', '''vcr_pre_train.th''', '''vqa_coco_pre_trained.th''', '''vqa_fine_tuned.th''', '''vqa_pre_trained.th''', ] def SCREAMING_SNAKE_CASE( __lowercase ) -> List[Any]: A: List[Any] = torch.load(__lowercase , map_location='''cpu''' ) return sd def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=rename_keys_prefix ) -> Optional[Any]: A: Tuple = OrderedDict() A: Dict = torch.arange(config.max_position_embeddings ).expand((1, -1) ) # detector_d = OrderedDict() for key in d: if "detector" in key: # detector_d[key.replace('detector.','')] = d[key] continue A: int = key for name_pair in rename_keys_prefix: A: Optional[int] = new_key.replace(name_pair[0] , name_pair[1] ) A: Union[str, Any] = d[key] if key == "bert.cls.predictions.decoder.weight": # Old bert code didn't have `decoder.bias`, but was added separately A: int = new_d['''cls.predictions.bias'''] return new_d @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Dict: assert ( checkpoint_path.split('''/''' )[-1] in ACCEPTABLE_CHECKPOINTS ), F"""The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}.""" # Get Config if "pre" in checkpoint_path: A: Optional[Any] = '''pretraining''' if "vcr" in checkpoint_path: A: Optional[int] = {'''visual_embedding_dim''': 5_1_2} elif "vqa_advanced" in checkpoint_path: A: Optional[Any] = {'''visual_embedding_dim''': 2_0_4_8} elif "vqa" in checkpoint_path: A: Dict = {'''visual_embedding_dim''': 2_0_4_8} elif "nlvr" in checkpoint_path: A: Tuple = {'''visual_embedding_dim''': 1_0_2_4} else: raise NotImplementedError(F"""No implementation found for `{checkpoint_path}`.""" ) else: if "vcr" in checkpoint_path: A: Dict = {'''visual_embedding_dim''': 5_1_2} A: List[str] = '''multichoice''' elif "vqa_advanced" in checkpoint_path: A: List[str] = {'''visual_embedding_dim''': 2_0_4_8} A: Optional[int] = '''vqa_advanced''' elif "vqa" in checkpoint_path: A: Dict = {'''visual_embedding_dim''': 2_0_4_8, '''num_labels''': 3_1_2_9} A: Union[str, Any] = '''vqa''' elif "nlvr" in checkpoint_path: A: Optional[int] = { '''visual_embedding_dim''': 1_0_2_4, '''num_labels''': 2, } A: str = '''nlvr''' A: Union[str, Any] = VisualBertConfig(**__lowercase ) # Load State Dict A: Union[str, Any] = load_state_dict(__lowercase ) A: str = get_new_dict(__lowercase , __lowercase ) if model_type == "pretraining": A: Optional[Any] = VisualBertForPreTraining(__lowercase ) elif model_type == "vqa": A: Optional[Any] = VisualBertForQuestionAnswering(__lowercase ) elif model_type == "nlvr": A: Union[str, Any] = VisualBertForVisualReasoning(__lowercase ) elif model_type == "multichoice": A: Any = VisualBertForMultipleChoice(__lowercase ) model.load_state_dict(__lowercase ) # Save Checkpoints Path(__lowercase ).mkdir(exist_ok=__lowercase ) model.save_pretrained(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument('''orig_checkpoint_path''', type=str, help='''A path to .th on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', type=str, help='''Path to the output PyTorch model.''') UpperCamelCase = parser.parse_args() convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)
319
1
'''simple docstring''' import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : Any ) -> int: '''simple docstring''' A: Any = inspect.getfile(accelerate.test_utils ) A: List[Any] = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_script.py'''] ) A: Optional[Any] = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_distributed_data_loop.py'''] ) A: List[Any] = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_ops.py'''] ) @require_multi_gpu def _snake_case ( self : List[str] ) -> Dict: '''simple docstring''' print(f"""Found {torch.cuda.device_count()} devices.""" ) A: Tuple = ['''torchrun''', f"""--nproc_per_node={torch.cuda.device_count()}""", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(SCREAMING_SNAKE_CASE_ , env=os.environ.copy() ) @require_multi_gpu def _snake_case ( self : List[str] ) -> Optional[int]: '''simple docstring''' print(f"""Found {torch.cuda.device_count()} devices.""" ) A: Dict = ['''torchrun''', f"""--nproc_per_node={torch.cuda.device_count()}""", self.operation_file_path] print(f"""Command: {cmd}""" ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(SCREAMING_SNAKE_CASE_ , env=os.environ.copy() ) @require_multi_gpu def _snake_case ( self : Union[str, Any] ) -> List[str]: '''simple docstring''' A: List[str] = ['''torchrun''', f"""--nproc_per_node={torch.cuda.device_count()}""", inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(SCREAMING_SNAKE_CASE_ , env=os.environ.copy() ) @require_multi_gpu def _snake_case ( self : str ) -> Any: '''simple docstring''' print(f"""Found {torch.cuda.device_count()} devices, using 2 devices only""" ) A: Dict = ['''torchrun''', f"""--nproc_per_node={torch.cuda.device_count()}""", self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices='''0,1''' ): execute_subprocess_async(SCREAMING_SNAKE_CASE_ , env=os.environ.copy() ) if __name__ == "__main__": UpperCamelCase = Accelerator() UpperCamelCase = (accelerator.state.process_index + 2, 10) UpperCamelCase = torch.randint(0, 10, shape).to(accelerator.device) UpperCamelCase = '''''' UpperCamelCase = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." UpperCamelCase = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." UpperCamelCase = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # 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)
319
'''simple docstring''' from itertools import permutations def SCREAMING_SNAKE_CASE( __lowercase ) -> bool: if num[3] % 2 != 0: return False if (num[2] + num[3] + num[4]) % 3 != 0: return False if num[5] % 5 != 0: return False A: int = [7, 1_1, 1_3, 1_7] for i, test in enumerate(__lowercase ): if (num[i + 4] * 1_0_0 + num[i + 5] * 1_0 + num[i + 6]) % test != 0: return False return True def SCREAMING_SNAKE_CASE( __lowercase = 1_0 ) -> int: return sum( int(''''''.join(map(__lowercase , __lowercase ) ) ) for num in permutations(range(__lowercase ) ) if is_substring_divisible(__lowercase ) ) if __name__ == "__main__": print(f'{solution() = }')
319
1
'''simple docstring''' # coding=utf-8 # Copyright 2023 The HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # this script dumps information about the environment import os import platform import sys UpperCamelCase = '''3''' print('''Python version:''', sys.version) print('''OS platform:''', platform.platform()) print('''OS architecture:''', platform.machine()) try: import torch print('''Torch version:''', torch.__version__) print('''Cuda available:''', torch.cuda.is_available()) print('''Cuda version:''', torch.version.cuda) print('''CuDNN version:''', torch.backends.cudnn.version()) print('''Number of GPUs available:''', torch.cuda.device_count()) except ImportError: print('''Torch version:''', None) try: import transformers print('''transformers version:''', transformers.__version__) except ImportError: print('''transformers version:''', None)
319
'''simple docstring''' import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt'''} UpperCamelCase = { '''vocab_file''': { '''allenai/longformer-base-4096''': '''https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json''', '''allenai/longformer-large-4096''': ( '''https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json''' ), '''allenai/longformer-large-4096-finetuned-triviaqa''': ( '''https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json''' ), '''allenai/longformer-base-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json''' ), '''allenai/longformer-large-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json''' ), }, '''merges_file''': { '''allenai/longformer-base-4096''': '''https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt''', '''allenai/longformer-large-4096''': ( '''https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt''' ), '''allenai/longformer-large-4096-finetuned-triviaqa''': ( '''https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt''' ), '''allenai/longformer-base-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt''' ), '''allenai/longformer-large-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt''' ), }, } UpperCamelCase = { '''allenai/longformer-base-4096''': 4096, '''allenai/longformer-large-4096''': 4096, '''allenai/longformer-large-4096-finetuned-triviaqa''': 4096, '''allenai/longformer-base-4096-extra.pos.embd.only''': 4096, '''allenai/longformer-large-4096-extra.pos.embd.only''': 4096, } @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def SCREAMING_SNAKE_CASE( ) -> Dict: A: Dict = ( list(range(ord('''!''' ) , ord('''~''' ) + 1 ) ) + list(range(ord('''¡''' ) , ord('''¬''' ) + 1 ) ) + list(range(ord('''®''' ) , ord('''ÿ''' ) + 1 ) ) ) A: Union[str, Any] = bs[:] A: List[str] = 0 for b in range(2**8 ): if b not in bs: bs.append(__lowercase ) cs.append(2**8 + n ) n += 1 A: List[Any] = [chr(__lowercase ) for n in cs] return dict(zip(__lowercase , __lowercase ) ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: A: Optional[Any] = set() A: Tuple = word[0] for char in word[1:]: pairs.add((prev_char, char) ) A: List[Any] = char return pairs class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : int = VOCAB_FILES_NAMES UpperCamelCase_ : int = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : int = ["""input_ids""", """attention_mask"""] def __init__( self : int , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : str="replace" , SCREAMING_SNAKE_CASE_ : str="<s>" , SCREAMING_SNAKE_CASE_ : Any="</s>" , SCREAMING_SNAKE_CASE_ : int="</s>" , SCREAMING_SNAKE_CASE_ : List[Any]="<s>" , SCREAMING_SNAKE_CASE_ : str="<unk>" , SCREAMING_SNAKE_CASE_ : Dict="<pad>" , SCREAMING_SNAKE_CASE_ : Dict="<mask>" , SCREAMING_SNAKE_CASE_ : Union[str, Any]=False , **SCREAMING_SNAKE_CASE_ : Tuple , ) -> List[str]: '''simple docstring''' A: int = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else bos_token A: Dict = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else eos_token A: int = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else sep_token A: Dict = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else cls_token A: Any = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else unk_token A: str = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it A: Dict = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else mask_token super().__init__( errors=SCREAMING_SNAKE_CASE_ , bos_token=SCREAMING_SNAKE_CASE_ , eos_token=SCREAMING_SNAKE_CASE_ , unk_token=SCREAMING_SNAKE_CASE_ , sep_token=SCREAMING_SNAKE_CASE_ , cls_token=SCREAMING_SNAKE_CASE_ , pad_token=SCREAMING_SNAKE_CASE_ , mask_token=SCREAMING_SNAKE_CASE_ , add_prefix_space=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) with open(SCREAMING_SNAKE_CASE_ , encoding='''utf-8''' ) as vocab_handle: A: str = json.load(SCREAMING_SNAKE_CASE_ ) A: str = {v: k for k, v in self.encoder.items()} A: Union[str, Any] = errors # how to handle errors in decoding A: Optional[int] = bytes_to_unicode() A: Union[str, Any] = {v: k for k, v in self.byte_encoder.items()} with open(SCREAMING_SNAKE_CASE_ , encoding='''utf-8''' ) as merges_handle: A: int = merges_handle.read().split('''\n''' )[1:-1] A: str = [tuple(merge.split() ) for merge in bpe_merges] A: Any = dict(zip(SCREAMING_SNAKE_CASE_ , range(len(SCREAMING_SNAKE_CASE_ ) ) ) ) A: Union[str, Any] = {} A: Tuple = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions A: Dict = re.compile(R'''\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+''' ) @property def _snake_case ( self : int ) -> List[Any]: '''simple docstring''' return len(self.encoder ) def _snake_case ( self : Optional[Any] ) -> int: '''simple docstring''' return dict(self.encoder , **self.added_tokens_encoder ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[Any]: '''simple docstring''' if token in self.cache: return self.cache[token] A: str = tuple(SCREAMING_SNAKE_CASE_ ) A: str = get_pairs(SCREAMING_SNAKE_CASE_ ) if not pairs: return token while True: A: Dict = min(SCREAMING_SNAKE_CASE_ , key=lambda SCREAMING_SNAKE_CASE_ : self.bpe_ranks.get(SCREAMING_SNAKE_CASE_ , float('''inf''' ) ) ) if bigram not in self.bpe_ranks: break A , A: Optional[Any] = bigram A: Tuple = [] A: List[Any] = 0 while i < len(SCREAMING_SNAKE_CASE_ ): try: A: Union[str, Any] = word.index(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) A: int = j if word[i] == first and i < len(SCREAMING_SNAKE_CASE_ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 A: Optional[Any] = tuple(SCREAMING_SNAKE_CASE_ ) A: Any = new_word if len(SCREAMING_SNAKE_CASE_ ) == 1: break else: A: Union[str, Any] = get_pairs(SCREAMING_SNAKE_CASE_ ) A: str = ''' '''.join(SCREAMING_SNAKE_CASE_ ) A: str = word return word def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Optional[int]: '''simple docstring''' A: Dict = [] for token in re.findall(self.pat , SCREAMING_SNAKE_CASE_ ): A: Tuple = ''''''.join( self.byte_encoder[b] for b in token.encode('''utf-8''' ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(SCREAMING_SNAKE_CASE_ ).split(''' ''' ) ) return bpe_tokens def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Optional[Any]: '''simple docstring''' return self.encoder.get(SCREAMING_SNAKE_CASE_ , self.encoder.get(self.unk_token ) ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> str: '''simple docstring''' return self.decoder.get(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Tuple: '''simple docstring''' A: Optional[int] = ''''''.join(SCREAMING_SNAKE_CASE_ ) A: Tuple = bytearray([self.byte_decoder[c] for c in text] ).decode('''utf-8''' , errors=self.errors ) return text def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(SCREAMING_SNAKE_CASE_ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return A: Union[str, Any] = os.path.join( SCREAMING_SNAKE_CASE_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) A: int = os.path.join( SCREAMING_SNAKE_CASE_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file'''] ) with open(SCREAMING_SNAKE_CASE_ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=SCREAMING_SNAKE_CASE_ , ensure_ascii=SCREAMING_SNAKE_CASE_ ) + '''\n''' ) A: Any = 0 with open(SCREAMING_SNAKE_CASE_ , '''w''' , encoding='''utf-8''' ) as writer: writer.write('''#version: 0.2\n''' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda SCREAMING_SNAKE_CASE_ : kv[1] ): if index != token_index: logger.warning( f"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.""" ''' Please check that the tokenizer is not corrupted!''' ) A: Union[str, Any] = token_index writer.write(''' '''.join(SCREAMING_SNAKE_CASE_ ) + '''\n''' ) index += 1 return vocab_file, merge_file def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] A: int = [self.cls_token_id] A: str = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None , SCREAMING_SNAKE_CASE_ : bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=SCREAMING_SNAKE_CASE_ , token_ids_a=SCREAMING_SNAKE_CASE_ , already_has_special_tokens=SCREAMING_SNAKE_CASE_ ) if token_ids_a is None: return [1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1] return [1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1, 1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1] def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' A: Dict = [self.sep_token_id] A: Optional[Any] = [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 + sep + token_ids_a + sep ) * [0] def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Dict=False , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> int: '''simple docstring''' A: Tuple = kwargs.pop('''add_prefix_space''' , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(SCREAMING_SNAKE_CASE_ ) > 0 and not text[0].isspace()): A: List[Any] = ''' ''' + text return (text, kwargs)
319
1
'''simple docstring''' import pickle import numpy as np from matplotlib import pyplot as plt class lowerCAmelCase_ : '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : int=0.2 , SCREAMING_SNAKE_CASE_ : Optional[Any]=0.2 ) -> Tuple: '''simple docstring''' A: List[Any] = bp_numa A: Optional[int] = bp_numa A: Tuple = bp_numa A: List[str] = conva_get[:2] A: List[Any] = conva_get[2] A: int = size_pa A: Any = rate_w A: Optional[int] = rate_t A: Optional[int] = [ np.mat(-1 * np.random.rand(self.conva[0] , self.conva[0] ) + 0.5 ) for i in range(self.conva[1] ) ] A: Any = np.mat(-1 * np.random.rand(self.num_bpa , self.num_bpa ) + 0.5 ) A: List[Any] = np.mat(-1 * np.random.rand(self.num_bpa , self.num_bpa ) + 0.5 ) A: List[str] = -2 * np.random.rand(self.conva[1] ) + 1 A: Any = -2 * np.random.rand(self.num_bpa ) + 1 A: Optional[int] = -2 * np.random.rand(self.num_bpa ) + 1 def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> int: '''simple docstring''' A: str = { '''num_bp1''': self.num_bpa, '''num_bp2''': self.num_bpa, '''num_bp3''': self.num_bpa, '''conv1''': self.conva, '''step_conv1''': self.step_conva, '''size_pooling1''': self.size_poolinga, '''rate_weight''': self.rate_weight, '''rate_thre''': self.rate_thre, '''w_conv1''': self.w_conva, '''wkj''': self.wkj, '''vji''': self.vji, '''thre_conv1''': self.thre_conva, '''thre_bp2''': self.thre_bpa, '''thre_bp3''': self.thre_bpa, } with open(SCREAMING_SNAKE_CASE_ , '''wb''' ) as f: pickle.dump(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) print(f"""Model saved: {save_path}""" ) @classmethod def _snake_case ( cls : Optional[int] , SCREAMING_SNAKE_CASE_ : Any ) -> Dict: '''simple docstring''' with open(SCREAMING_SNAKE_CASE_ , '''rb''' ) as f: A: Dict = pickle.load(SCREAMING_SNAKE_CASE_ ) # noqa: S301 A: Optional[int] = model_dic.get('''conv1''' ) conv_get.append(model_dic.get('''step_conv1''' ) ) A: List[Any] = model_dic.get('''size_pooling1''' ) A: int = model_dic.get('''num_bp1''' ) A: List[Any] = model_dic.get('''num_bp2''' ) A: int = model_dic.get('''num_bp3''' ) A: Optional[Any] = model_dic.get('''rate_weight''' ) A: Any = model_dic.get('''rate_thre''' ) # create model instance A: List[Any] = CNN(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # modify model parameter A: str = model_dic.get('''w_conv1''' ) A: Tuple = model_dic.get('''wkj''' ) A: Optional[Any] = model_dic.get('''vji''' ) A: Dict = model_dic.get('''thre_conv1''' ) A: int = model_dic.get('''thre_bp2''' ) A: List[Any] = model_dic.get('''thre_bp3''' ) return conv_ins def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : int ) -> Any: '''simple docstring''' return 1 / (1 + np.exp(-1 * x )) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : Tuple ) -> Any: '''simple docstring''' return round(SCREAMING_SNAKE_CASE_ , 3 ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Any: '''simple docstring''' A: Optional[Any] = convs[0] A: List[Any] = convs[1] A: List[str] = np.shape(SCREAMING_SNAKE_CASE_ )[0] # get the data slice of original image data, data_focus A: Tuple = [] for i_focus in range(0 , size_data - size_conv + 1 , SCREAMING_SNAKE_CASE_ ): for j_focus in range(0 , size_data - size_conv + 1 , SCREAMING_SNAKE_CASE_ ): A: List[Any] = data[ i_focus : i_focus + size_conv, j_focus : j_focus + size_conv ] data_focus.append(SCREAMING_SNAKE_CASE_ ) # calculate the feature map of every single kernel, and saved as list of matrix A: Optional[Any] = [] A: Dict = int((size_data - size_conv) / conv_step + 1 ) for i_map in range(SCREAMING_SNAKE_CASE_ ): A: Optional[int] = [] for i_focus in range(len(SCREAMING_SNAKE_CASE_ ) ): A: Union[str, Any] = ( np.sum(np.multiply(data_focus[i_focus] , w_convs[i_map] ) ) - thre_convs[i_map] ) featuremap.append(self.sig(SCREAMING_SNAKE_CASE_ ) ) A: int = np.asmatrix(SCREAMING_SNAKE_CASE_ ).reshape( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) data_featuremap.append(SCREAMING_SNAKE_CASE_ ) # expanding the data slice to One dimenssion A: int = [] for each_focus in data_focus: focusa_list.extend(self.Expand_Mat(SCREAMING_SNAKE_CASE_ ) ) A: Optional[Any] = np.asarray(SCREAMING_SNAKE_CASE_ ) return focus_list, data_featuremap def _snake_case ( self : Dict , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Union[str, Any]="average_pool" ) -> Optional[Any]: '''simple docstring''' A: Union[str, Any] = len(featuremaps[0] ) A: Dict = int(size_map / size_pooling ) A: Tuple = [] for i_map in range(len(SCREAMING_SNAKE_CASE_ ) ): A: List[str] = featuremaps[i_map] A: int = [] for i_focus in range(0 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): for j_focus in range(0 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): A: Dict = feature_map[ i_focus : i_focus + size_pooling, j_focus : j_focus + size_pooling, ] if pooling_type == "average_pool": # average pooling map_pooled.append(np.average(SCREAMING_SNAKE_CASE_ ) ) elif pooling_type == "max_pooling": # max pooling map_pooled.append(np.max(SCREAMING_SNAKE_CASE_ ) ) A: Union[str, Any] = np.asmatrix(SCREAMING_SNAKE_CASE_ ).reshape(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) featuremap_pooled.append(SCREAMING_SNAKE_CASE_ ) return featuremap_pooled def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' A: List[Any] = [] for i in range(len(SCREAMING_SNAKE_CASE_ ) ): A: List[str] = np.shape(data[i] ) A: List[Any] = data[i].reshape(1 , shapes[0] * shapes[1] ) A: str = data_listed.getA().tolist()[0] data_expanded.extend(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = np.asarray(SCREAMING_SNAKE_CASE_ ) return data_expanded def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : List[str] ) -> Any: '''simple docstring''' A: List[str] = np.asarray(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = np.shape(SCREAMING_SNAKE_CASE_ ) A: List[str] = data_mat.reshape(1 , shapes[0] * shapes[1] ) return data_expanded def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Any ) -> Any: '''simple docstring''' A: List[Any] = [] A: Tuple = 0 for i_map in range(SCREAMING_SNAKE_CASE_ ): A: Dict = np.ones((size_map, size_map) ) for i in range(0 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): for j in range(0 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): A: Any = pd_pool[ i_pool ] A: Any = i_pool + 1 A: Dict = np.multiply( SCREAMING_SNAKE_CASE_ , np.multiply(out_map[i_map] , (1 - out_map[i_map]) ) ) pd_all.append(SCREAMING_SNAKE_CASE_ ) return pd_all def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Dict=bool ) -> Dict: '''simple docstring''' print('''----------------------Start Training-------------------------''' ) print((''' - - Shape: Train_Data ''', np.shape(SCREAMING_SNAKE_CASE_ )) ) print((''' - - Shape: Teach_Data ''', np.shape(SCREAMING_SNAKE_CASE_ )) ) A: Optional[Any] = 0 A: List[Any] = [] A: Optional[int] = 1_00_00 while rp < n_repeat and mse >= error_accuracy: A: Tuple = 0 print(f"""-------------Learning Time {rp}--------------""" ) for p in range(len(SCREAMING_SNAKE_CASE_ ) ): # print('------------Learning Image: %d--------------'%p) A: Any = np.asmatrix(datas_train[p] ) A: Union[str, Any] = np.asarray(datas_teach[p] ) A , A: Tuple = self.convolute( SCREAMING_SNAKE_CASE_ , self.conva , self.w_conva , self.thre_conva , conv_step=self.step_conva , ) A: Any = self.pooling(SCREAMING_SNAKE_CASE_ , self.size_poolinga ) A: Any = np.shape(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = self._expand(SCREAMING_SNAKE_CASE_ ) A: int = data_bp_input A: Optional[int] = np.dot(SCREAMING_SNAKE_CASE_ , self.vji.T ) - self.thre_bpa A: List[Any] = self.sig(SCREAMING_SNAKE_CASE_ ) A: List[Any] = np.dot(SCREAMING_SNAKE_CASE_ , self.wkj.T ) - self.thre_bpa A: List[str] = self.sig(SCREAMING_SNAKE_CASE_ ) # --------------Model Leaning ------------------------ # calculate error and gradient--------------- A: Optional[Any] = np.multiply( (data_teach - bp_outa) , np.multiply(SCREAMING_SNAKE_CASE_ , (1 - bp_outa) ) ) A: Tuple = np.multiply( np.dot(SCREAMING_SNAKE_CASE_ , self.wkj ) , np.multiply(SCREAMING_SNAKE_CASE_ , (1 - bp_outa) ) ) A: Union[str, Any] = np.dot(SCREAMING_SNAKE_CASE_ , self.vji ) A: Union[str, Any] = pd_i_all / (self.size_poolinga * self.size_poolinga) A: Dict = pd_conva_pooled.T.getA().tolist() A: List[Any] = self._calculate_gradient_from_pool( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , shape_featuremapa[0] , shape_featuremapa[1] , self.size_poolinga , ) # weight and threshold learning process--------- # convolution layer for k_conv in range(self.conva[1] ): A: List[str] = self._expand_mat(pd_conva_all[k_conv] ) A: Optional[int] = self.rate_weight * np.dot(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = self.w_conva[k_conv] + delta_w.reshape( (self.conva[0], self.conva[0]) ) A: Tuple = ( self.thre_conva[k_conv] - np.sum(pd_conva_all[k_conv] ) * self.rate_thre ) # all connected layer A: int = self.wkj + pd_k_all.T * bp_outa * self.rate_weight A: List[str] = self.vji + pd_j_all.T * bp_outa * self.rate_weight A: int = self.thre_bpa - pd_k_all * self.rate_thre A: Tuple = self.thre_bpa - pd_j_all * self.rate_thre # calculate the sum error of all single image A: Optional[Any] = np.sum(abs(data_teach - bp_outa ) ) error_count += errors # print(' ----Teach ',data_teach) # print(' ----BP_output ',bp_out3) A: Union[str, Any] = rp + 1 A: Tuple = error_count / patterns all_mse.append(SCREAMING_SNAKE_CASE_ ) def draw_error(): A: Union[str, Any] = [error_accuracy for i in range(int(n_repeat * 1.2 ) )] plt.plot(SCREAMING_SNAKE_CASE_ , '''+-''' ) plt.plot(SCREAMING_SNAKE_CASE_ , '''r--''' ) plt.xlabel('''Learning Times''' ) plt.ylabel('''All_mse''' ) plt.grid(SCREAMING_SNAKE_CASE_ , alpha=0.5 ) plt.show() print('''------------------Training Complished---------------------''' ) print((''' - - Training epoch: ''', rp, f""" - - Mse: {mse:.6f}""") ) if draw_e: draw_error() return mse def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : Any ) -> int: '''simple docstring''' A: List[str] = [] print('''-------------------Start Testing-------------------------''' ) print((''' - - Shape: Test_Data ''', np.shape(SCREAMING_SNAKE_CASE_ )) ) for p in range(len(SCREAMING_SNAKE_CASE_ ) ): A: str = np.asmatrix(datas_test[p] ) A , A: int = self.convolute( SCREAMING_SNAKE_CASE_ , self.conva , self.w_conva , self.thre_conva , conv_step=self.step_conva , ) A: Dict = self.pooling(SCREAMING_SNAKE_CASE_ , self.size_poolinga ) A: Any = self._expand(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = data_bp_input A: int = bp_outa * self.vji.T - self.thre_bpa A: Any = self.sig(SCREAMING_SNAKE_CASE_ ) A: int = bp_outa * self.wkj.T - self.thre_bpa A: int = self.sig(SCREAMING_SNAKE_CASE_ ) produce_out.extend(bp_outa.getA().tolist() ) A: Dict = [list(map(self.do_round , SCREAMING_SNAKE_CASE_ ) ) for each in produce_out] return np.asarray(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Any: '''simple docstring''' A: str = np.asmatrix(SCREAMING_SNAKE_CASE_ ) A , A: List[str] = self.convolute( SCREAMING_SNAKE_CASE_ , self.conva , self.w_conva , self.thre_conva , conv_step=self.step_conva , ) A: str = self.pooling(SCREAMING_SNAKE_CASE_ , self.size_poolinga ) return data_conveda, data_pooleda if __name__ == "__main__": pass
319
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> int: if not isinstance(__lowercase , __lowercase ): raise TypeError('''only integers accepted as input''' ) else: A: str = str(abs(__lowercase ) ) A: int = [list(__lowercase ) for char in range(len(__lowercase ) )] for index in range(len(__lowercase ) ): num_transpositions[index].pop(__lowercase ) return max( int(''''''.join(list(__lowercase ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__('''doctest''').testmod()
319
1
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> int: assert ( isinstance(__lowercase , __lowercase ) and number_of_steps > 0 ), F"""number_of_steps needs to be positive integer, your input {number_of_steps}""" if number_of_steps == 1: return 1 A , A: int = 1, 1 for _ in range(number_of_steps - 1 ): A , A: Any = current + previous, current return current if __name__ == "__main__": import doctest doctest.testmod()
319
'''simple docstring''' from __future__ import annotations import math def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list: if len(__lowercase ) != 2 or len(a[0] ) != 2 or len(__lowercase ) != 2 or len(b[0] ) != 2: raise Exception('''Matrices are not 2x2''' ) A: str = [ [a[0][0] * b[0][0] + a[0][1] * b[1][0], a[0][0] * b[0][1] + a[0][1] * b[1][1]], [a[1][0] * b[0][0] + a[1][1] * b[1][0], a[1][0] * b[0][1] + a[1][1] * b[1][1]], ] return new_matrix def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: return [ [matrix_a[row][col] + matrix_b[row][col] for col in range(len(matrix_a[row] ) )] for row in range(len(__lowercase ) ) ] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: return [ [matrix_a[row][col] - matrix_b[row][col] for col in range(len(matrix_a[row] ) )] for row in range(len(__lowercase ) ) ] def SCREAMING_SNAKE_CASE( __lowercase ) -> tuple[list, list, list, list]: if len(__lowercase ) % 2 != 0 or len(a[0] ) % 2 != 0: raise Exception('''Odd matrices are not supported!''' ) A: Union[str, Any] = len(__lowercase ) A: str = matrix_length // 2 A: Optional[int] = [[a[i][j] for j in range(__lowercase , __lowercase )] for i in range(__lowercase )] A: Optional[Any] = [ [a[i][j] for j in range(__lowercase , __lowercase )] for i in range(__lowercase , __lowercase ) ] A: Union[str, Any] = [[a[i][j] for j in range(__lowercase )] for i in range(__lowercase )] A: int = [[a[i][j] for j in range(__lowercase )] for i in range(__lowercase , __lowercase )] return top_left, top_right, bot_left, bot_right def SCREAMING_SNAKE_CASE( __lowercase ) -> tuple[int, int]: return len(__lowercase ), len(matrix[0] ) def SCREAMING_SNAKE_CASE( __lowercase ) -> None: print('''\n'''.join(str(__lowercase ) for line in matrix ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list: if matrix_dimensions(__lowercase ) == (2, 2): return default_matrix_multiplication(__lowercase , __lowercase ) A , A , A , A: Union[str, Any] = split_matrix(__lowercase ) A , A , A , A: List[Any] = split_matrix(__lowercase ) A: Optional[int] = actual_strassen(__lowercase , matrix_subtraction(__lowercase , __lowercase ) ) A: Any = actual_strassen(matrix_addition(__lowercase , __lowercase ) , __lowercase ) A: Tuple = actual_strassen(matrix_addition(__lowercase , __lowercase ) , __lowercase ) A: Optional[int] = actual_strassen(__lowercase , matrix_subtraction(__lowercase , __lowercase ) ) A: Tuple = actual_strassen(matrix_addition(__lowercase , __lowercase ) , matrix_addition(__lowercase , __lowercase ) ) A: Union[str, Any] = actual_strassen(matrix_subtraction(__lowercase , __lowercase ) , matrix_addition(__lowercase , __lowercase ) ) A: List[str] = actual_strassen(matrix_subtraction(__lowercase , __lowercase ) , matrix_addition(__lowercase , __lowercase ) ) A: int = matrix_addition(matrix_subtraction(matrix_addition(__lowercase , __lowercase ) , __lowercase ) , __lowercase ) A: Any = matrix_addition(__lowercase , __lowercase ) A: List[Any] = matrix_addition(__lowercase , __lowercase ) A: List[str] = matrix_subtraction(matrix_subtraction(matrix_addition(__lowercase , __lowercase ) , __lowercase ) , __lowercase ) # construct the new matrix from our 4 quadrants A: Union[str, Any] = [] for i in range(len(__lowercase ) ): new_matrix.append(top_left[i] + top_right[i] ) for i in range(len(__lowercase ) ): new_matrix.append(bot_left[i] + bot_right[i] ) return new_matrix def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list: if matrix_dimensions(__lowercase )[1] != matrix_dimensions(__lowercase )[0]: A: int = ( '''Unable to multiply these matrices, please check the dimensions.\n''' F"""Matrix A: {matrixa}\n""" F"""Matrix B: {matrixa}""" ) raise Exception(__lowercase ) A: str = matrix_dimensions(__lowercase ) A: str = matrix_dimensions(__lowercase ) if dimensiona[0] == dimensiona[1] and dimensiona[0] == dimensiona[1]: return [matrixa, matrixa] A: Union[str, Any] = max(*__lowercase , *__lowercase ) A: Optional[int] = int(math.pow(2 , math.ceil(math.loga(__lowercase ) ) ) ) A: List[Any] = matrixa A: Tuple = matrixa # Adding zeros to the matrices so that the arrays dimensions are the same and also # power of 2 for i in range(0 , __lowercase ): if i < dimensiona[0]: for _ in range(dimensiona[1] , __lowercase ): new_matrixa[i].append(0 ) else: new_matrixa.append([0] * maxim ) if i < dimensiona[0]: for _ in range(dimensiona[1] , __lowercase ): new_matrixa[i].append(0 ) else: new_matrixa.append([0] * maxim ) A: Any = actual_strassen(__lowercase , __lowercase ) # Removing the additional zeros for i in range(0 , __lowercase ): if i < dimensiona[0]: for _ in range(dimensiona[1] , __lowercase ): final_matrix[i].pop() else: final_matrix.pop() return final_matrix if __name__ == "__main__": UpperCamelCase = [ [2, 3, 4, 5], [6, 4, 3, 1], [2, 3, 6, 7], [3, 1, 2, 4], [2, 3, 4, 5], [6, 4, 3, 1], [2, 3, 6, 7], [3, 1, 2, 4], [2, 3, 4, 5], [6, 2, 3, 1], ] UpperCamelCase = [[0, 2, 1, 1], [16, 2, 3, 3], [2, 2, 7, 7], [13, 11, 22, 4]] print(strassen(matrixa, matrixa))
319
1
'''simple docstring''' class lowerCAmelCase_ : '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : list ) -> None: '''simple docstring''' A: Any = set_counts A: List[Any] = max(SCREAMING_SNAKE_CASE_ ) A: Any = len(SCREAMING_SNAKE_CASE_ ) A: Tuple = [1] * num_sets A: str = list(range(SCREAMING_SNAKE_CASE_ ) ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ) -> bool: '''simple docstring''' A: int = self.get_parent(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = self.get_parent(SCREAMING_SNAKE_CASE_ ) if src_parent == dst_parent: return False if self.ranks[dst_parent] >= self.ranks[src_parent]: self.set_counts[dst_parent] += self.set_counts[src_parent] A: Tuple = 0 A: str = dst_parent if self.ranks[dst_parent] == self.ranks[src_parent]: self.ranks[dst_parent] += 1 A: Union[str, Any] = self.set_counts[dst_parent] else: self.set_counts[src_parent] += self.set_counts[dst_parent] A: Optional[int] = 0 A: Any = src_parent A: List[str] = self.set_counts[src_parent] A: Optional[Any] = max(self.max_set , SCREAMING_SNAKE_CASE_ ) return True def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : int ) -> int: '''simple docstring''' if self.parents[disj_set] == disj_set: return disj_set A: Tuple = self.get_parent(self.parents[disj_set] ) return self.parents[disj_set]
319
'''simple docstring''' from dataclasses import dataclass from typing import Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin @dataclass class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : torch.FloatTensor UpperCamelCase_ : torch.FloatTensor UpperCamelCase_ : Optional[torch.FloatTensor] = None class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = 2 @register_to_config def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : float = 0.02 , SCREAMING_SNAKE_CASE_ : float = 1_00 , SCREAMING_SNAKE_CASE_ : float = 1.007 , SCREAMING_SNAKE_CASE_ : float = 80 , SCREAMING_SNAKE_CASE_ : float = 0.05 , SCREAMING_SNAKE_CASE_ : float = 50 , ) -> Optional[int]: '''simple docstring''' A: Union[str, Any] = sigma_max # setable values A: int = None A: np.IntTensor = None A: torch.FloatTensor = None # sigma(t_i) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : Optional[int] = None ) -> torch.FloatTensor: '''simple docstring''' return sample def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Union[str, torch.device] = None ) -> Optional[Any]: '''simple docstring''' A: List[Any] = num_inference_steps A: List[str] = np.arange(0 , self.num_inference_steps )[::-1].copy() A: Any = torch.from_numpy(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ) A: str = [ ( self.config.sigma_max**2 * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1)) ) for i in self.timesteps ] A: Tuple = torch.tensor(SCREAMING_SNAKE_CASE_ , dtype=torch.floataa , device=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : Optional[torch.Generator] = None ) -> Tuple[torch.FloatTensor, float]: '''simple docstring''' if self.config.s_min <= sigma <= self.config.s_max: A: str = min(self.config.s_churn / self.num_inference_steps , 2**0.5 - 1 ) else: A: List[str] = 0 # sample eps ~ N(0, S_noise^2 * I) A: Optional[Any] = self.config.s_noise * randn_tensor(sample.shape , generator=SCREAMING_SNAKE_CASE_ ).to(sample.device ) A: Optional[Any] = sigma + gamma * sigma A: List[Any] = sample + ((sigma_hat**2 - sigma**2) ** 0.5 * eps) return sample_hat, sigma_hat def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : bool = True , ) -> Union[KarrasVeOutput, Tuple]: '''simple docstring''' A: Union[str, Any] = sample_hat + sigma_hat * model_output A: str = (sample_hat - pred_original_sample) / sigma_hat A: Optional[int] = sample_hat + (sigma_prev - sigma_hat) * derivative if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=SCREAMING_SNAKE_CASE_ , derivative=SCREAMING_SNAKE_CASE_ , pred_original_sample=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : bool = True , ) -> Union[KarrasVeOutput, Tuple]: '''simple docstring''' A: int = sample_prev + sigma_prev * model_output A: List[Any] = (sample_prev - pred_original_sample) / sigma_prev A: Dict = sample_hat + (sigma_prev - sigma_hat) * (0.5 * derivative + 0.5 * derivative_corr) if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=SCREAMING_SNAKE_CASE_ , derivative=SCREAMING_SNAKE_CASE_ , pred_original_sample=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : str ) -> Dict: '''simple docstring''' raise NotImplementedError()
319
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase = { '''configuration_upernet''': ['''UperNetConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''UperNetForSemanticSegmentation''', '''UperNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_upernet import UperNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_upernet import UperNetForSemanticSegmentation, UperNetPreTrainedModel else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
'''simple docstring''' import json import logging import math import os import sys from dataclasses import dataclass, field from typing import Optional from datasets import Dataset, load_dataset import transformers from transformers import ( CONFIG_MAPPING, MODEL_FOR_MASKED_LM_MAPPING, AutoConfig, AutoModelForMaskedLM, AutoTokenizer, DataCollatorForWholeWordMask, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process UpperCamelCase = logging.getLogger(__name__) UpperCamelCase = list(MODEL_FOR_MASKED_LM_MAPPING.keys()) UpperCamelCase = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={ """help""": ( """The model checkpoint for weights initialization.Don't set if you want to train a model from scratch.""" ) } , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """If training from scratch, pass a model type from the list: """ + """, """.join(UpperCAmelCase_ )} , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={ """help""": ( """Override some existing default config settings when a model is trained from scratch. Example: """ """n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index""" ) } , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) UpperCamelCase_ : bool = field( default=UpperCAmelCase_ , metadata={"""help""": """Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."""} , ) UpperCamelCase_ : str = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) UpperCamelCase_ : bool = field( default=UpperCAmelCase_ , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) def _snake_case ( self : Tuple ) -> List[Any]: '''simple docstring''' if self.config_overrides is not None and (self.config_name is not None or self.model_name_or_path is not None): raise ValueError( '''--config_overrides can\'t be used in combination with --config_name or --model_name_or_path''' ) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """The name of the dataset to use (via the datasets library)."""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) UpperCamelCase_ : Optional[str] = field(default=UpperCAmelCase_ , metadata={"""help""": """The input training data file (a text file)."""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """An optional input evaluation data file to evaluate the perplexity on (a text file)."""} , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """An optional input train ref data file for whole word masking in Chinese."""} , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """An optional input validation ref data file for whole word masking in Chinese."""} , ) UpperCamelCase_ : bool = field( default=UpperCAmelCase_ , metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) UpperCamelCase_ : Optional[int] = field( default=5 , metadata={ """help""": """The percentage of the train set used as validation set in case there's no validation split""" } , ) UpperCamelCase_ : Optional[int] = field( default=UpperCAmelCase_ , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated. Default to the max input length of the model.""" ) } , ) UpperCamelCase_ : Optional[int] = field( default=UpperCAmelCase_ , metadata={"""help""": """The number of processes to use for the preprocessing."""} , ) UpperCamelCase_ : float = field( default=0.15 , metadata={"""help""": """Ratio of tokens to mask for masked language modeling loss"""} ) UpperCamelCase_ : bool = field( default=UpperCAmelCase_ , metadata={ """help""": ( """Whether to pad all samples to `max_seq_length`. """ """If False, will pad the samples dynamically when batching to the maximum length in the batch.""" ) } , ) def _snake_case ( self : List[Any] ) -> Optional[int]: '''simple docstring''' if self.train_file is not None: A: Tuple = self.train_file.split('''.''' )[-1] assert extension in ["csv", "json", "txt"], "`train_file` should be a csv, a json or a txt file." if self.validation_file is not None: A: str = self.validation_file.split('''.''' )[-1] assert extension in ["csv", "json", "txt"], "`validation_file` should be a csv, a json or a txt file." def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[str]: with open(__lowercase , '''r''' , encoding='''utf-8''' ) as f: A: List[Any] = [json.loads(__lowercase ) for line in f.read().splitlines() if (len(__lowercase ) > 0 and not line.isspace())] assert len(__lowercase ) == len(__lowercase ) A: Optional[int] = {c: dataset[c] for c in dataset.column_names} A: Union[str, Any] = refs return Dataset.from_dict(__lowercase ) def SCREAMING_SNAKE_CASE( ) -> int: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A: int = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('''.json''' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. A , A , A: Optional[int] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: A , A , A: List[Any] = parser.parse_args_into_dataclasses() # Detecting last checkpoint. A: Any = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: A: Any = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. """ '''Use --overwrite_output_dir to overcome.''' ) elif last_checkpoint is not None: logger.info( F"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ '''the `--output_dir` or add `--overwrite_output_dir` to train from scratch.''' ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , handlers=[logging.StreamHandler(sys.stdout )] , ) logger.setLevel(logging.INFO if is_main_process(training_args.local_rank ) else logging.WARN ) # Log on each process the small summary: logger.warning( F"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + F"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('''Training/evaluation parameters %s''' , __lowercase ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below) # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/ # (the dataset will be downloaded automatically from the datasets Hub). # # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called # 'text' is found. You can easily tweak this behavior (see below). # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. A: Dict = load_dataset(data_args.dataset_name , data_args.dataset_config_name ) if "validation" not in datasets.keys(): A: int = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F"""train[:{data_args.validation_split_percentage}%]""" , ) A: Dict = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F"""train[{data_args.validation_split_percentage}%:]""" , ) else: A: Any = {} if data_args.train_file is not None: A: int = data_args.train_file if data_args.validation_file is not None: A: Optional[int] = data_args.validation_file A: List[str] = data_args.train_file.split('''.''' )[-1] if extension == "txt": A: int = '''text''' A: Any = load_dataset(__lowercase , data_files=__lowercase ) # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at # https://huggingface.co/docs/datasets/loading_datasets.html. # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. A: Dict = { '''cache_dir''': model_args.cache_dir, '''revision''': model_args.model_revision, '''use_auth_token''': True if model_args.use_auth_token else None, } if model_args.config_name: A: List[Any] = AutoConfig.from_pretrained(model_args.config_name , **__lowercase ) elif model_args.model_name_or_path: A: int = AutoConfig.from_pretrained(model_args.model_name_or_path , **__lowercase ) else: A: str = CONFIG_MAPPING[model_args.model_type]() logger.warning('''You are instantiating a new config instance from scratch.''' ) if model_args.config_overrides is not None: logger.info(F"""Overriding config: {model_args.config_overrides}""" ) config.update_from_string(model_args.config_overrides ) logger.info(F"""New config: {config}""" ) A: Tuple = { '''cache_dir''': model_args.cache_dir, '''use_fast''': model_args.use_fast_tokenizer, '''revision''': model_args.model_revision, '''use_auth_token''': True if model_args.use_auth_token else None, } if model_args.tokenizer_name: A: Optional[int] = AutoTokenizer.from_pretrained(model_args.tokenizer_name , **__lowercase ) elif model_args.model_name_or_path: A: Union[str, Any] = AutoTokenizer.from_pretrained(model_args.model_name_or_path , **__lowercase ) else: raise ValueError( '''You are instantiating a new tokenizer from scratch. This is not supported by this script.''' '''You can do it from another script, save it, and load it from here, using --tokenizer_name.''' ) if model_args.model_name_or_path: A: List[Any] = AutoModelForMaskedLM.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=__lowercase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info('''Training new model from scratch''' ) A: List[Any] = AutoModelForMaskedLM.from_config(__lowercase ) model.resize_token_embeddings(len(__lowercase ) ) # Preprocessing the datasets. # First we tokenize all the texts. if training_args.do_train: A: int = datasets['''train'''].column_names else: A: str = datasets['''validation'''].column_names A: Tuple = '''text''' if '''text''' in column_names else column_names[0] A: List[str] = '''max_length''' if data_args.pad_to_max_length else False def tokenize_function(__lowercase ): # Remove empty lines A: int = [line for line in examples['''text'''] if len(__lowercase ) > 0 and not line.isspace()] return tokenizer(examples['''text'''] , padding=__lowercase , truncation=__lowercase , max_length=data_args.max_seq_length ) A: str = datasets.map( __lowercase , batched=__lowercase , num_proc=data_args.preprocessing_num_workers , remove_columns=[text_column_name] , load_from_cache_file=not data_args.overwrite_cache , ) # Add the chinese references if provided if data_args.train_ref_file is not None: A: List[str] = add_chinese_references(tokenized_datasets['''train'''] , data_args.train_ref_file ) if data_args.validation_ref_file is not None: A: Dict = add_chinese_references( tokenized_datasets['''validation'''] , data_args.validation_ref_file ) # If we have ref files, need to avoid it removed by trainer A: Optional[Any] = data_args.train_ref_file or data_args.validation_ref_file if has_ref: A: List[Any] = False # Data collator # This one will take care of randomly masking the tokens. A: Optional[Any] = DataCollatorForWholeWordMask(tokenizer=__lowercase , mlm_probability=data_args.mlm_probability ) # Initialize our Trainer A: Optional[int] = Trainer( model=__lowercase , args=__lowercase , train_dataset=tokenized_datasets['''train'''] if training_args.do_train else None , eval_dataset=tokenized_datasets['''validation'''] if training_args.do_eval else None , tokenizer=__lowercase , data_collator=__lowercase , ) # Training if training_args.do_train: if last_checkpoint is not None: A: Optional[int] = last_checkpoint elif model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path ): A: str = model_args.model_name_or_path else: A: List[str] = None A: str = trainer.train(resume_from_checkpoint=__lowercase ) trainer.save_model() # Saves the tokenizer too for easy upload A: Union[str, Any] = os.path.join(training_args.output_dir , '''train_results.txt''' ) if trainer.is_world_process_zero(): with open(__lowercase , '''w''' ) as writer: logger.info('''***** Train results *****''' ) for key, value in sorted(train_result.metrics.items() ): logger.info(F""" {key} = {value}""" ) writer.write(F"""{key} = {value}\n""" ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , '''trainer_state.json''' ) ) # Evaluation A: Optional[int] = {} if training_args.do_eval: logger.info('''*** Evaluate ***''' ) A: Optional[Any] = trainer.evaluate() A: Union[str, Any] = math.exp(eval_output['''eval_loss'''] ) A: Dict = perplexity A: Any = os.path.join(training_args.output_dir , '''eval_results_mlm_wwm.txt''' ) if trainer.is_world_process_zero(): with open(__lowercase , '''w''' ) as writer: logger.info('''***** Eval results *****''' ) for key, value in sorted(results.items() ): logger.info(F""" {key} = {value}""" ) writer.write(F"""{key} = {value}\n""" ) return results def SCREAMING_SNAKE_CASE( __lowercase ) -> List[Any]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
319
1
'''simple docstring''' from ..utils import DummyObject, requires_backends class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = ["""torch"""] def __init__( self : str , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> List[Any]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : List[str] , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : int ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : List[Any] , **SCREAMING_SNAKE_CASE_ : List[str] ) -> List[str]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = ["""torch"""] def __init__( self : str , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Dict ) -> str: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Optional[int] , *SCREAMING_SNAKE_CASE_ : Tuple , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Tuple , *SCREAMING_SNAKE_CASE_ : int , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Any = ["""torch"""] def __init__( self : Optional[Any] , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> Any: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Optional[Any] , *SCREAMING_SNAKE_CASE_ : int , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> str: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : str , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : str ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Union[str, Any] = ["""torch"""] def __init__( self : Optional[int] , *SCREAMING_SNAKE_CASE_ : Optional[int] , **SCREAMING_SNAKE_CASE_ : Any ) -> List[Any]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Tuple , *SCREAMING_SNAKE_CASE_ : Any , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Tuple: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> str: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[int] = ["""torch"""] def __init__( self : int , *SCREAMING_SNAKE_CASE_ : Tuple , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> int: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Tuple , *SCREAMING_SNAKE_CASE_ : int , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> List[str]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Any , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> str: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[int] = ["""torch"""] def __init__( self : Tuple , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Tuple ) -> List[str]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Any , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Dict , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Tuple ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Any = ["""torch"""] def __init__( self : Optional[Any] , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Dict ) -> Any: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : int ) -> Tuple: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Any , *SCREAMING_SNAKE_CASE_ : List[Any] , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Any = ["""torch"""] def __init__( self : int , *SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : str ) -> str: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Optional[Any] , *SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : str ) -> List[str]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : int , *SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : int = ["""torch"""] def __init__( self : Optional[int] , *SCREAMING_SNAKE_CASE_ : Any , **SCREAMING_SNAKE_CASE_ : Any ) -> Dict: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : str , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : Dict ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : List[Any] , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : Dict ) -> Tuple: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : List[Any] = ["""torch"""] def __init__( self : Tuple , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Tuple: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Optional[int] , *SCREAMING_SNAKE_CASE_ : Tuple , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Dict: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Any , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Tuple ) -> str: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : str = ["""torch"""] def __init__( self : str , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Tuple ) -> int: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Tuple , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Tuple ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : str , *SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) def SCREAMING_SNAKE_CASE( *__lowercase , **__lowercase ) -> Any: requires_backends(__lowercase , ['''torch'''] ) def SCREAMING_SNAKE_CASE( *__lowercase , **__lowercase ) -> str: requires_backends(__lowercase , ['''torch'''] ) def SCREAMING_SNAKE_CASE( *__lowercase , **__lowercase ) -> str: requires_backends(__lowercase , ['''torch'''] ) def SCREAMING_SNAKE_CASE( *__lowercase , **__lowercase ) -> Tuple: requires_backends(__lowercase , ['''torch'''] ) def SCREAMING_SNAKE_CASE( *__lowercase , **__lowercase ) -> Union[str, Any]: requires_backends(__lowercase , ['''torch'''] ) def SCREAMING_SNAKE_CASE( *__lowercase , **__lowercase ) -> Dict: requires_backends(__lowercase , ['''torch'''] ) def SCREAMING_SNAKE_CASE( *__lowercase , **__lowercase ) -> Any: requires_backends(__lowercase , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[int] = ["""torch"""] def __init__( self : int , *SCREAMING_SNAKE_CASE_ : Optional[int] , **SCREAMING_SNAKE_CASE_ : Any ) -> Union[str, Any]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Dict , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Tuple ) -> List[str]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : int , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : str ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = ["""torch"""] def __init__( self : str , *SCREAMING_SNAKE_CASE_ : Tuple , **SCREAMING_SNAKE_CASE_ : Dict ) -> Dict: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Optional[int] , *SCREAMING_SNAKE_CASE_ : Optional[int] , **SCREAMING_SNAKE_CASE_ : List[str] ) -> str: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Dict , *SCREAMING_SNAKE_CASE_ : Any , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> Tuple: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Any = ["""torch"""] def __init__( self : Dict , *SCREAMING_SNAKE_CASE_ : List[Any] , **SCREAMING_SNAKE_CASE_ : int ) -> Union[str, Any]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : List[Any] , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Dict: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : List[str] , *SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : Any ) -> int: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = ["""torch"""] def __init__( self : Optional[int] , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> List[Any]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Any , *SCREAMING_SNAKE_CASE_ : Any , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> Dict: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : int , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Tuple ) -> List[str]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : str = ["""torch"""] def __init__( self : int , *SCREAMING_SNAKE_CASE_ : int , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> Union[str, Any]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Dict , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> str: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Optional[int] , *SCREAMING_SNAKE_CASE_ : Any , **SCREAMING_SNAKE_CASE_ : str ) -> Tuple: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : List[str] = ["""torch"""] def __init__( self : List[str] , *SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : Any ) -> int: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Any , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> str: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : int , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Tuple ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Any = ["""torch"""] def __init__( self : Tuple , *SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[Any]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Any , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Tuple: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Tuple , *SCREAMING_SNAKE_CASE_ : Optional[int] , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Tuple: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = ["""torch"""] def __init__( self : Optional[Any] , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> Dict: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : int ) -> int: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : List[Any] , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Any ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : List[Any] = ["""torch"""] def __init__( self : Optional[int] , *SCREAMING_SNAKE_CASE_ : List[Any] , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Tuple: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Dict , *SCREAMING_SNAKE_CASE_ : int , **SCREAMING_SNAKE_CASE_ : str ) -> str: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Optional[Any] , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Tuple ) -> List[str]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : List[Any] = ["""torch"""] def __init__( self : Optional[int] , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : Dict ) -> Union[str, Any]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Dict , *SCREAMING_SNAKE_CASE_ : List[Any] , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Dict: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : Tuple ) -> str: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = ["""torch"""] def __init__( self : Any , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[int]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : str , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> str: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : Tuple ) -> Dict: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = ["""torch"""] def __init__( self : str , *SCREAMING_SNAKE_CASE_ : List[Any] , **SCREAMING_SNAKE_CASE_ : int ) -> Union[str, Any]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Any , *SCREAMING_SNAKE_CASE_ : Tuple , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> str: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : str , *SCREAMING_SNAKE_CASE_ : int , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> str: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : int = ["""torch"""] def __init__( self : Dict , *SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Any: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Tuple , *SCREAMING_SNAKE_CASE_ : Optional[int] , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Optional[int] , *SCREAMING_SNAKE_CASE_ : Tuple , **SCREAMING_SNAKE_CASE_ : Dict ) -> List[str]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = ["""torch"""] def __init__( self : Optional[Any] , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : int ) -> str: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : int , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Dict: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Any , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Tuple ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : List[str] = ["""torch"""] def __init__( self : Optional[int] , *SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Optional[int]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Dict ) -> Tuple: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Any , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> List[str]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = ["""torch"""] def __init__( self : Any , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Dict ) -> int: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : Any , **SCREAMING_SNAKE_CASE_ : Any ) -> Dict: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : str , *SCREAMING_SNAKE_CASE_ : Optional[int] , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = ["""torch"""] def __init__( self : Tuple , *SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : Tuple ) -> str: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Any , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : List[str] , *SCREAMING_SNAKE_CASE_ : Tuple , **SCREAMING_SNAKE_CASE_ : Any ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = ["""torch"""] def __init__( self : Any , *SCREAMING_SNAKE_CASE_ : Optional[int] , **SCREAMING_SNAKE_CASE_ : str ) -> List[str]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : List[Any] , *SCREAMING_SNAKE_CASE_ : List[Any] , **SCREAMING_SNAKE_CASE_ : str ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : int ) -> str: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : str = ["""torch"""] def __init__( self : int , *SCREAMING_SNAKE_CASE_ : Any , **SCREAMING_SNAKE_CASE_ : str ) -> List[Any]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : str , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Tuple ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : List[str] , *SCREAMING_SNAKE_CASE_ : Tuple , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> List[str]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = ["""torch"""] def __init__( self : str , *SCREAMING_SNAKE_CASE_ : Any , **SCREAMING_SNAKE_CASE_ : List[str] ) -> List[Any]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Any , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Dict ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Optional[Any] , *SCREAMING_SNAKE_CASE_ : Optional[int] , **SCREAMING_SNAKE_CASE_ : Any ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = ["""torch"""] def __init__( self : Dict , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Any ) -> Tuple: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Dict , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Dict ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : List[Any] , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : Any ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Any = ["""torch"""] def __init__( self : Optional[int] , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : Tuple ) -> Optional[Any]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : int , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> Dict: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Dict , *SCREAMING_SNAKE_CASE_ : Tuple , **SCREAMING_SNAKE_CASE_ : List[str] ) -> int: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = ["""torch"""] def __init__( self : List[Any] , *SCREAMING_SNAKE_CASE_ : List[Any] , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Tuple: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : str , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : List[str] ) -> List[str]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Dict , *SCREAMING_SNAKE_CASE_ : Optional[int] , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> Any: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = ["""torch"""] def __init__( self : Dict , *SCREAMING_SNAKE_CASE_ : List[Any] , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Optional[int]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : Any , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Any: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Tuple , *SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : Dict ) -> Any: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : List[Any] = ["""torch"""] def __init__( self : str , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : str ) -> str: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Dict , *SCREAMING_SNAKE_CASE_ : List[Any] , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : int = ["""torch"""] def __init__( self : str , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : Tuple ) -> int: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : int , *SCREAMING_SNAKE_CASE_ : Any , **SCREAMING_SNAKE_CASE_ : str ) -> str: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : str , *SCREAMING_SNAKE_CASE_ : Optional[int] , **SCREAMING_SNAKE_CASE_ : str ) -> Dict: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : List[str] = ["""torch"""] def __init__( self : Optional[int] , *SCREAMING_SNAKE_CASE_ : List[Any] , **SCREAMING_SNAKE_CASE_ : int ) -> int: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Optional[Any] , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Dict: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Optional[int] , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Any ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Any = ["""torch"""] def __init__( self : Dict , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> Tuple: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Optional[int] , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : List[str] ) -> List[str]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : List[str] , *SCREAMING_SNAKE_CASE_ : Tuple , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Union[str, Any] = ["""torch"""] def __init__( self : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : Optional[int] , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[int]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Optional[int] , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : Any ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = ["""torch"""] def __init__( self : Dict , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Tuple ) -> int: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : str , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Tuple ) -> int: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : List[str] , *SCREAMING_SNAKE_CASE_ : Tuple , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = ["""torch"""] def __init__( self : Optional[Any] , *SCREAMING_SNAKE_CASE_ : List[Any] , **SCREAMING_SNAKE_CASE_ : Dict ) -> List[str]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : List[Any] , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Dict: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : int , *SCREAMING_SNAKE_CASE_ : Any , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Any: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = ["""torch"""] def __init__( self : Optional[int] , *SCREAMING_SNAKE_CASE_ : Optional[int] , **SCREAMING_SNAKE_CASE_ : Any ) -> List[Any]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : str , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Tuple: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Tuple , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Dict ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = ["""torch"""] def __init__( self : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : Optional[int] , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Tuple: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : Optional[int] , *SCREAMING_SNAKE_CASE_ : Tuple , **SCREAMING_SNAKE_CASE_ : Tuple ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Any , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : int ) -> str: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : int = ["""torch"""] def __init__( self : List[Any] , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : int ) -> Union[str, Any]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : List[Any] , *SCREAMING_SNAKE_CASE_ : int , **SCREAMING_SNAKE_CASE_ : str ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Any , *SCREAMING_SNAKE_CASE_ : Tuple , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = ["""torch"""] def __init__( self : List[str] , *SCREAMING_SNAKE_CASE_ : Tuple , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Optional[Any]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : List[str] , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> List[str]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Optional[Any] , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : int ) -> Dict: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : List[str] = ["""torch"""] def __init__( self : List[Any] , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : str ) -> List[Any]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : str , *SCREAMING_SNAKE_CASE_ : Any , **SCREAMING_SNAKE_CASE_ : Tuple ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Any , *SCREAMING_SNAKE_CASE_ : int , **SCREAMING_SNAKE_CASE_ : Dict ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = ["""torch"""] def __init__( self : Tuple , *SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : Any ) -> List[str]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : List[str] , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Tuple ) -> List[str]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> List[str]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[int] = ["""torch"""] def __init__( self : Tuple , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> List[Any]: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : List[Any] , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : Optional[int] , *SCREAMING_SNAKE_CASE_ : List[Any] , **SCREAMING_SNAKE_CASE_ : Tuple ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''torch'''] ) class lowerCAmelCase_ ( metaclass=UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Union[str, Any] = ["""torch"""] def __init__( self : Dict , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : List[str] ) -> str: '''simple docstring''' requires_backends(self , ['''torch'''] ) @classmethod def _snake_case ( cls : List[Any] , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : Any ) -> Dict: '''simple docstring''' requires_backends(cls , ['''torch'''] ) @classmethod def _snake_case ( cls : int , *SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : Any ) -> List[str]: '''simple docstring''' requires_backends(cls , ['''torch'''] )
319
'''simple docstring''' import json import os import unittest from typing import Tuple from transformers import WavaVecaPhonemeCTCTokenizer from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.models.wavaveca_phoneme.tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizerOutput from transformers.testing_utils import require_phonemizer from ...test_tokenization_common import TokenizerTesterMixin @require_phonemizer class lowerCAmelCase_ ( UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Any = WavaVecaPhonemeCTCTokenizer UpperCamelCase_ : Tuple = False def _snake_case ( self : str ) -> Union[str, Any]: '''simple docstring''' super().setUp() A: Optional[int] = ( '''<s> <pad> </s> <unk> n s t ə l a i k d m ɛ ɾ e ɪ p o ɐ z ð f j v b ɹ ʁ ʊ iː r w ʌ u ɡ æ aɪ ʃ h ɔ ɑː ''' '''ŋ ɚ eɪ β uː y ɑ̃ oʊ ᵻ eː θ aʊ ts oː ɔ̃ ɣ ɜ ɑ dʒ əl x ɜː ç ʒ tʃ ɔː ɑːɹ ɛ̃ ʎ ɔːɹ ʋ aː ɕ œ ø oːɹ ɲ yː ''' '''ʔ iə i5 s. tɕ ?? nʲ ɛː œ̃ ɭ ɔø ʑ tʲ ɨ ɛɹ ts. rʲ ɪɹ ɭʲ i.5 ɔɪ q sʲ u5 ʊɹ iɜ a5 iɛ5 øː ʕ ja əɜ th ɑ5 ''' '''oɪ dʲ ə5 tɕh ts.h mʲ ɯ dʑ vʲ e̞ tʃʲ ei5 o5 onɡ5 ɑu5 iɑ5 ai5 aɪɚ kh ə1 ʐ i2 ʉ ħ t[ aɪə ʲ ju ə2 u2 oɜ ''' '''pː iɛɜ ou5 y5 uɜ tː uo5 d[ uoɜ tsh ɑɜ ɵ i̪5 uei5 ɟ aɜ ɑɨ i.ɜ eʊ o2 ɐ̃ ä pʲ kʲ n̩ ɒ ph ɑu2 uɨ əɪ ɫ ɬ ''' '''yɜ bʲ ɑ2 s̪ aiɜ χ ɐ̃ʊ̃ 1 ə4 yæɜ a2 ɨː t̪ iouɜ ũ onɡɜ aɨ iɛ2 ɔɨ ɑuɜ o̞ ei2 iou2 c kː y2 ɖ oe dˤ yɛɜ ''' '''əʊ S ɡʲ onɡ2 u" eiɜ ʈ ɯᵝ iou5 dZ r̝̊ i.2 tS s^ ʝ yə5 iɑɜ uə5 pf ɨu iɑ2 ou2 ər2 fʲ ai2 r̝ uəɜ ɳ əɨ ''' '''ua5 uɪ ɽ bː yu5 uo2 yɛ5 l̩ ɻ ərɜ ʂ i̪2 ouɜ uaɜ a. a.ː yæ5 dː r̩ ee ɪu ər5 i̪ ɜ æi u: i.ː t^ o1 ɪ^ ''' '''ai ueiɜ æː ɛɪ eə i. ɴ ie ua2 ɑ1 o4 tʃː o: ɑ: u1 N i̪1 au yæ2 u. qː yəɜ y: kʰ tʃʰ iʊ sx õ uo tʰ ''' '''uai5 bʰ u.ː uə2 ʊə d^ s̪ː yiɜ dʰ r. oe: i1 ɟː yu2 nʲʲ i̪4 uei2 tsʲ ɸ ĩ ɑ4 t̪ː eɑ u4 e: tsː ʈʰ ɡʰ ''' '''ɯɯ dʒʲ ʂʲ X ɵː uaiɜ tɕʲ ã t^ː ẽː yɛ2 cː i.1 ɛʊ dˤdˤ dʒː i4 ɡː yi ɕʲ ɟʰ pʰ dʑʲ yuɜ ua1 ua4 æiː ɐɐ ''' '''ui iou1 ʊː a1 iou4 cʰ iɛ1 yə2 ɖʰ ẽ ʒʲ ää ər4 iːː ɪː iɑ1 ər1 œː øi ɪuː cʰcʰ əː1 iː1 ũ kʰː o̞o̞ xʲ ''' '''ou1 iɛ4 e̞e̞ y1 dzː dʲʲ dʰː ɯᵝɯᵝ lː uo1 i.4 i: yɛ5ʲ a4''' ).split(''' ''' ) A: Union[str, Any] = dict(zip(SCREAMING_SNAKE_CASE_ , range(len(SCREAMING_SNAKE_CASE_ ) ) ) ) A: Dict = {'''pad_token''': '''<pad>''', '''unk_token''': '''<unk>''', '''bos_token''': '''<s>''', '''eos_token''': '''</s>'''} A: Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(SCREAMING_SNAKE_CASE_ ) + '''\n''' ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Tuple=False , SCREAMING_SNAKE_CASE_ : Any=20 , SCREAMING_SNAKE_CASE_ : Optional[int]=5 ) -> Tuple[str, list]: '''simple docstring''' A: int = [(i, tokenizer.decode([i] , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ )) for i in range(len(SCREAMING_SNAKE_CASE_ ) )] A: Optional[Any] = list(filter(lambda SCREAMING_SNAKE_CASE_ : [t[0]] == tokenizer.encode(t[1] , do_phonemize=SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) ) if max_length is not None and len(SCREAMING_SNAKE_CASE_ ) > max_length: A: int = toks[:max_length] if min_length is not None and len(SCREAMING_SNAKE_CASE_ ) < min_length and len(SCREAMING_SNAKE_CASE_ ) > 0: while len(SCREAMING_SNAKE_CASE_ ) < min_length: A: Dict = toks + toks # toks_str = [t[1] for t in toks] A: Union[str, Any] = [t[0] for t in toks] # Ensure consistency A: List[str] = tokenizer.decode(SCREAMING_SNAKE_CASE_ , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) if " " not in output_txt and len(SCREAMING_SNAKE_CASE_ ) > 1: A: int = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) + ''' ''' + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) ) if with_prefix_space: A: Tuple = ''' ''' + output_txt A: List[str] = tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) return output_txt, output_ids def _snake_case ( self : Optional[int] , **SCREAMING_SNAKE_CASE_ : int ) -> Dict: '''simple docstring''' kwargs.update(self.special_tokens_map ) return WavaVecaPhonemeCTCTokenizer.from_pretrained(self.tmpdirname , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int ) -> Optional[Any]: '''simple docstring''' A: List[Any] = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) # check adding a single token tokenizer.add_tokens('''xxx''' ) A: Any = tokenizer('''m xxx ɪ''' , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids self.assertEqual(SCREAMING_SNAKE_CASE_ , [13, 3_92, 17] ) # xxx should be last token tokenizer.add_tokens(['''aaa''', '''bbb''', '''ccc'''] ) A: Optional[int] = tokenizer('''m aaa ɪ ccc''' , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids self.assertEqual(SCREAMING_SNAKE_CASE_ , [13, 3_93, 17, 3_95] ) # aaa and ccc should be after xxx and 2 after aaa A: str = tokenizer('''maɪ c''' , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids self.assertEqual(SCREAMING_SNAKE_CASE_ , [3, 2_00] ) # mai should be <unk> (=3) def _snake_case ( self : int ) -> List[Any]: '''simple docstring''' A: Any = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: Any = '''Hello how are you''' A: Optional[Any] = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''h ə l oʊ h aʊ ɑːɹ j uː''' ) def _snake_case ( self : Tuple ) -> Dict: '''simple docstring''' A: str = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: List[Any] = '''Hello how are you''' A: Any = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) self.assertEqual(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids , tokenizer(SCREAMING_SNAKE_CASE_ , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids ) def _snake_case ( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' A: str = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: List[str] = '''Hello how are you''' A: Union[str, Any] = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) A: Union[str, Any] = tokenizer.decode(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Dict ) -> Optional[Any]: '''simple docstring''' A: Dict = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: Optional[Any] = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98], [24, 22, 5, 24, 22, 5, 77], ] A: List[str] = tokenizer.decode(sample_ids[0] ) A: List[str] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , batch_tokens[0] ) self.assertEqual(SCREAMING_SNAKE_CASE_ , ['''k s ɾ ɾ l ɭʲ''', '''j ð s j ð s oːɹ'''] ) def _snake_case ( self : Any ) -> Optional[int]: '''simple docstring''' A: int = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) A: List[Any] = '''Hello how are you''' A: Optional[Any] = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''h ə l oʊ | h aʊ | ɑːɹ | j uː |''' ) def _snake_case ( self : List[str] ) -> int: '''simple docstring''' A: Optional[Any] = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) A: Optional[Any] = '''Hello how are you''' A: Any = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) self.assertEqual(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids , tokenizer(SCREAMING_SNAKE_CASE_ , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids ) def _snake_case ( self : Dict ) -> Any: '''simple docstring''' A: Optional[int] = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) # fmt: off A: str = [ [11, 5, 15, tokenizer.pad_token_id, tokenizer.word_delimiter_token_id, 15, 8, tokenizer.word_delimiter_token_id, 98], [tokenizer.word_delimiter_token_id, 24, 22, tokenizer.word_delimiter_token_id, 5, 24, 22, 5, 77], ] # fmt: on # decode with word_del_token filter A: Tuple = tokenizer.decode(sample_ids[0] ) A: Optional[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , batch_tokens[0] ) self.assertEqual(SCREAMING_SNAKE_CASE_ , ['''k s ɾ ɾ l ɭʲ''', '''j ð s j ð s oːɹ'''] ) # decode with no word_del_token filter A: str = tokenizer.decode(sample_ids[0] , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , batch_tokens[0] ) self.assertEqual(SCREAMING_SNAKE_CASE_ , ['''k s ɾ | ɾ l | ɭʲ''', '''| j ð | s j ð s oːɹ'''] ) def _snake_case ( self : int ) -> List[str]: '''simple docstring''' A: Dict = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) A: Union[str, Any] = '''Hello how are you''' A: Tuple = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) A: Any = tokenizer.decode(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] ) -> Any: '''simple docstring''' A: Dict = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) A: Any = '''Hello how are you''' A: List[Any] = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) A: List[Any] = tokenizer.decode(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) self.assertEqual(''' '''.join([p.strip() for p in phonemes.split(''' |''' )] ).strip() , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] ) -> Optional[Any]: '''simple docstring''' A: List[str] = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token=SCREAMING_SNAKE_CASE_ ) A: List[Any] = '''Hello how are you''' A: List[str] = tokenizer(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ).input_ids A: Tuple = tokenizer(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''fr-fr''' ).input_ids self.assertNotEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Tuple = tokenizer.decode(SCREAMING_SNAKE_CASE_ ) A: Any = tokenizer.decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''h ə l oʊ h aʊ ɑːɹ j uː''' ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''ɛ l o h aʊ a ʁ j u''' ) def _snake_case ( self : str ) -> str: '''simple docstring''' A: str = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: str = '''Hello how Are you''' A: Union[str, Any] = '''hello how are you''' A: List[str] = tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids A: str = tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int ) -> List[Any]: '''simple docstring''' A: Union[str, Any] = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) tokenizer.add_tokens(['''!''', '''?'''] ) tokenizer.add_special_tokens({'''cls_token''': '''$$$'''} ) # fmt: off A: Tuple = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98, 3_92, 3_92, 3_93, 3_92, 3_92, 3_93, 3_94, 3_94], [24, 22, 5, 24, 22, 5, 77, tokenizer.pad_token_id, 3_94, 3_94], ] # fmt: on A: List[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , ['''k s ɾ ɾ l ɭʲ!?!? $$$''', '''j ð s j ð s oːɹ $$$'''] ) @staticmethod def _snake_case ( SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Tuple: '''simple docstring''' A: Any = [d[key] for d in offsets] return retrieved_list def _snake_case ( self : Any ) -> Tuple: '''simple docstring''' A: str = self.get_tokenizer(word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) # fmt: off # ksssɾɾ|ɾɾ<pad>ɾɾ|<pad>ɾlll|ɭʲ -> k s ɾ ɾ | ɾ l | ɭʲ" A: Union[str, Any] = [11, 5, 5, 5, 15, 15, tokenizer.pad_token_id, 15, 15, tokenizer.word_delimiter_token_id, tokenizer.pad_token_id, 15, 8, 8, 8, tokenizer.word_delimiter_token_id, 98] # fmt: on A: int = tokenizer.decode(SCREAMING_SNAKE_CASE_ , output_char_offsets=SCREAMING_SNAKE_CASE_ , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) # check Wav2Vec2CTCTokenizerOutput keys for char self.assertEqual(len(outputs.keys() ) , 2 ) self.assertTrue('''text''' in outputs ) self.assertTrue('''char_offsets''' in outputs ) self.assertTrue(isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) # check that order of chars is correct and identical for both outputs self.assertEqual(''' '''.join(self.get_from_offsets(outputs['''char_offsets'''] , '''char''' ) ) , outputs.text ) self.assertListEqual( self.get_from_offsets(outputs['''char_offsets'''] , '''char''' ) , ['''k''', '''s''', '''ɾ''', '''ɾ''', '''|''', '''ɾ''', '''l''', '''|''', '''ɭʲ'''] ) # check that offsets are actually correct for char # 0-1 is 11, 1-4 is 5, 4-6 is first 15, 6-7 is <pad> (thus not shown), 7-9 is second 15, 9-10 is word_delimiter_token, # 10-11 is <pad> (thus not shown), 11-12 is third 15, 12-15 is 8, 15-16 is word_delimiter_token, 16-17 is 98 self.assertListEqual( self.get_from_offsets(outputs['''char_offsets'''] , '''start_offset''' ) , [0, 1, 4, 7, 9, 11, 12, 15, 16] ) self.assertListEqual( self.get_from_offsets(outputs['''char_offsets'''] , '''end_offset''' ) , [1, 4, 6, 9, 10, 12, 15, 16, 17] ) def _snake_case ( self : Any ) -> List[Any]: '''simple docstring''' A: Optional[int] = self.get_tokenizer(word_delimiter_token='''|''' ) def check_list_tuples_equal(SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any] ): self.assertTrue(isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) self.assertTrue(isinstance(outputs_list[0] , SCREAMING_SNAKE_CASE_ ) ) # transform list to ModelOutput A: Dict = WavaVecaPhonemeCTCTokenizerOutput( {k: [d[k] for d in outputs_list] for k in outputs_list[0]} ) self.assertListEqual(outputs_batch['''text'''] , outputs_batch_a['''text'''] ) def recursive_check(SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[str] ): if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): [recursive_check(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for la, la in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )] self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if "char_offsets" in outputs_batch: recursive_check(outputs_batch['''char_offsets'''] , outputs_batch_a['''char_offsets'''] ) # fmt: off A: int = [ [11, 5, 15, tokenizer.pad_token_id, 15, 4, 8, 98, 32, 32, 32, 32, 4, 33, tokenizer.word_delimiter_token_id, 32, 32, 33, 34, 34], [24, 22, 5, tokenizer.word_delimiter_token_id, tokenizer.word_delimiter_token_id, 24, 22, 22, 22, 4, 5, 77, tokenizer.pad_token_id, 22, 22, 4, 34, 34, 34, 34], ] # fmt: on # We assume that `decode` works as expected. All we will check now is # the output type is correct and the output is identical to `decode` # char A: List[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ , output_char_offsets=SCREAMING_SNAKE_CASE_ ) A: List[Any] = [tokenizer.decode(SCREAMING_SNAKE_CASE_ , output_char_offsets=SCREAMING_SNAKE_CASE_ ) for ids in sample_ids] check_list_tuples_equal(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @unittest.skip('''Wav2Vec2PhonemeTokenizer always lower cases letters to correctly map to phonemes''' ) def _snake_case ( self : int ) -> int: '''simple docstring''' pass @unittest.skip('''Wav2Vec2PhonemeTokenizer always puts spaces between phonemes''' ) def _snake_case ( self : str ) -> Any: '''simple docstring''' pass @unittest.skip('''encodes to text to ids, but decodes ids to phonemes -> not possible to have internal consistency''' ) def _snake_case ( self : List[str] ) -> List[str]: '''simple docstring''' pass @unittest.skip('''Wav2Vec2PhonemeModel has no max model length => no testing''' ) def _snake_case ( self : Dict ) -> List[Any]: '''simple docstring''' pass def _snake_case ( self : Tuple ) -> Any: '''simple docstring''' A: Any = self.get_tokenizers(do_lower_case=SCREAMING_SNAKE_CASE_ ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): A: str = tokenizer.vocab_size A: str = len(SCREAMING_SNAKE_CASE_ ) self.assertNotEqual(SCREAMING_SNAKE_CASE_ , 0 ) # We usually have added tokens from the start in tests because our vocab fixtures are # smaller than the original vocabs - let's not assert this # self.assertEqual(vocab_size, all_size) A: List[Any] = ['''aaaaa bbbbbb''', '''cccccccccdddddddd'''] A: List[Any] = tokenizer.add_tokens(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = tokenizer.vocab_size A: Union[str, Any] = len(SCREAMING_SNAKE_CASE_ ) self.assertNotEqual(SCREAMING_SNAKE_CASE_ , 0 ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , len(SCREAMING_SNAKE_CASE_ ) ) self.assertEqual(SCREAMING_SNAKE_CASE_ , all_size + len(SCREAMING_SNAKE_CASE_ ) ) A: Any = tokenizer.encode('''aaaaa bbbbbb low cccccccccdddddddd l''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertGreaterEqual(len(SCREAMING_SNAKE_CASE_ ) , 4 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) A: str = {'''eos_token''': '''>>>>|||<||<<|<<''', '''pad_token''': '''<<<<<|||>|>>>>|>'''} A: int = tokenizer.add_special_tokens(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = tokenizer.vocab_size A: Optional[Any] = len(SCREAMING_SNAKE_CASE_ ) self.assertNotEqual(SCREAMING_SNAKE_CASE_ , 0 ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , len(SCREAMING_SNAKE_CASE_ ) ) self.assertEqual(SCREAMING_SNAKE_CASE_ , all_size_a + len(SCREAMING_SNAKE_CASE_ ) ) A: int = tokenizer.encode( '''>>>>|||<||<<|<< aaaaabbbbbb low cccccccccdddddddd <<<<<|||>|>>>>|> l''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertGreaterEqual(len(SCREAMING_SNAKE_CASE_ ) , 6 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[0] , tokens[1] ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokens[-4] ) self.assertEqual(tokens[0] , tokenizer.eos_token_id ) self.assertEqual(tokens[-3] , tokenizer.pad_token_id ) @unittest.skip('''The tokenizer shouldn\'t be used to encode input IDs (except for labels), only to decode.''' ) def _snake_case ( self : List[Any] ) -> Optional[Any]: '''simple docstring''' pass @unittest.skip('''The tokenizer shouldn\'t be used to encode input IDs (except for labels), only to decode.''' ) def _snake_case ( self : Tuple ) -> Optional[Any]: '''simple docstring''' pass def _snake_case ( self : str ) -> Tuple: '''simple docstring''' A: List[Any] = self.get_tokenizers(fast=SCREAMING_SNAKE_CASE_ , do_lower_case=SCREAMING_SNAKE_CASE_ ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): A: Union[str, Any] = ['''ð''', '''ɪ''', '''s''', '''ɪ''', '''z''', '''ɐ''', '''t''', '''ɛ''', '''k''', '''s''', '''t'''] A: Union[str, Any] = tokenizer.convert_tokens_to_string(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(output['''text'''] , SCREAMING_SNAKE_CASE_ )
319
1
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: print('''\nThe shortest path matrix using Floyd Warshall algorithm\n''' ) for i in range(__lowercase ): for j in range(__lowercase ): if dist[i][j] != float('''inf''' ): print(int(dist[i][j] ) , end='''\t''' ) else: print('''INF''' , end='''\t''' ) print() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str: A: Any = [[float('''inf''' ) for _ in range(__lowercase )] for _ in range(__lowercase )] for i in range(__lowercase ): for j in range(__lowercase ): A: str = graph[i][j] # check vertex k against all other vertices (i, j) for k in range(__lowercase ): # looping through rows of graph array for i in range(__lowercase ): # looping through columns of graph array for j in range(__lowercase ): if ( dist[i][k] != float('''inf''' ) and dist[k][j] != float('''inf''' ) and dist[i][k] + dist[k][j] < dist[i][j] ): A: Tuple = dist[i][k] + dist[k][j] _print_dist(__lowercase , __lowercase ) return dist, v if __name__ == "__main__": UpperCamelCase = int(input('''Enter number of vertices: ''')) UpperCamelCase = int(input('''Enter number of edges: ''')) UpperCamelCase = [[float('''inf''') for i in range(v)] for j in range(v)] for i in range(v): UpperCamelCase = 0.0 # src and dst are indices that must be within the array size graph[e][v] # failure to follow this will result in an error for i in range(e): print('''\nEdge ''', i + 1) UpperCamelCase = int(input('''Enter source:''')) UpperCamelCase = int(input('''Enter destination:''')) UpperCamelCase = float(input('''Enter weight:''')) UpperCamelCase = weight floyd_warshall(graph, v) # Example Input # Enter number of vertices: 3 # Enter number of edges: 2 # # generated graph from vertex and edge inputs # [[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]] # [[0.0, inf, inf], [inf, 0.0, inf], [inf, inf, 0.0]] # specify source, destination and weight for edge #1 # Edge 1 # Enter source:1 # Enter destination:2 # Enter weight:2 # specify source, destination and weight for edge #2 # Edge 2 # Enter source:2 # Enter destination:1 # Enter weight:1 # # Expected Output from the vertice, edge and src, dst, weight inputs!! # 0 INF INF # INF 0 2 # INF 1 0
319
'''simple docstring''' import warnings from ...utils import logging from .image_processing_beit import BeitImageProcessor UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> None: '''simple docstring''' warnings.warn( '''The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use BeitImageProcessor instead.''' , SCREAMING_SNAKE_CASE_ , ) super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ )
319
1
'''simple docstring''' import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device UpperCamelCase = False class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' pass @nightly @require_torch_gpu class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : Any ) -> Optional[int]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def _snake_case ( self : Dict ) -> int: '''simple docstring''' A: Optional[Any] = VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion''' , torch_dtype=torch.floataa ) pipe.to(SCREAMING_SNAKE_CASE_ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) A: Tuple = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) A: Optional[int] = torch.manual_seed(0 ) A: List[Any] = pipe.dual_guided( prompt='''first prompt''' , image=SCREAMING_SNAKE_CASE_ , text_to_image_strength=0.75 , generator=SCREAMING_SNAKE_CASE_ , guidance_scale=7.5 , num_inference_steps=2 , output_type='''numpy''' , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(SCREAMING_SNAKE_CASE_ ) A: Any = VersatileDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE_ , torch_dtype=torch.floataa ) pipe.to(SCREAMING_SNAKE_CASE_ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) A: List[str] = generator.manual_seed(0 ) A: int = pipe.dual_guided( prompt='''first prompt''' , image=SCREAMING_SNAKE_CASE_ , text_to_image_strength=0.75 , generator=SCREAMING_SNAKE_CASE_ , guidance_scale=7.5 , num_inference_steps=2 , output_type='''numpy''' , ).images assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass" def _snake_case ( self : Any ) -> str: '''simple docstring''' A: Optional[int] = VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion''' , torch_dtype=torch.floataa ) pipe.to(SCREAMING_SNAKE_CASE_ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) A: Optional[int] = '''cyberpunk 2077''' A: Dict = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) A: Optional[Any] = torch.manual_seed(0 ) A: List[Any] = pipe.dual_guided( prompt=SCREAMING_SNAKE_CASE_ , image=SCREAMING_SNAKE_CASE_ , text_to_image_strength=0.75 , generator=SCREAMING_SNAKE_CASE_ , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' , ).images A: List[str] = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) A: Optional[int] = np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 A: Union[str, Any] = '''A painting of a squirrel eating a burger ''' A: Any = torch.manual_seed(0 ) A: Tuple = pipe.text_to_image( prompt=SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' ).images A: Union[str, Any] = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) A: int = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 A: List[Any] = pipe.image_variation(SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ , output_type='''numpy''' ).images A: Tuple = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) A: Union[str, Any] = np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1
319
'''simple docstring''' import os import pytest from transformers.dynamic_module_utils import get_imports UpperCamelCase = ''' import os ''' UpperCamelCase = ''' def foo(): import os return False ''' UpperCamelCase = ''' def foo(): def bar(): if True: import os return False return bar() ''' UpperCamelCase = ''' import os try: import bar except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os def foo(): try: import bar except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except (ImportError, AttributeError): raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except ImportError as e: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar import baz except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar import baz except ImportError: x = 1 raise ValueError() ''' UpperCamelCase = [ TOP_LEVEL_IMPORT, IMPORT_IN_FUNCTION, DEEPLY_NESTED_IMPORT, TOP_LEVEL_TRY_IMPORT, GENERIC_EXCEPT_IMPORT, MULTILINE_TRY_IMPORT, MULTILINE_BOTH_IMPORT, MULTIPLE_EXCEPTS_IMPORT, EXCEPT_AS_IMPORT, TRY_IMPORT_IN_FUNCTION, ] @pytest.mark.parametrize('''case''' , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Dict: A: Tuple = os.path.join(__lowercase , '''test_file.py''' ) with open(__lowercase , '''w''' ) as _tmp_file: _tmp_file.write(__lowercase ) A: List[Any] = get_imports(__lowercase ) assert parsed_imports == ["os"]
319
1
'''simple docstring''' import datasets UpperCamelCase = '''\ @InProceedings{conneau2018xnli, author = "Conneau, Alexis and Rinott, Ruty and Lample, Guillaume and Williams, Adina and Bowman, Samuel R. and Schwenk, Holger and Stoyanov, Veselin", title = "XNLI: Evaluating Cross-lingual Sentence Representations", booktitle = "Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing", year = "2018", publisher = "Association for Computational Linguistics", location = "Brussels, Belgium", } ''' UpperCamelCase = '''\ XNLI is a subset of a few thousand examples from MNLI which has been translated into a 14 different languages (some low-ish resource). As with MNLI, the goal is to predict textual entailment (does sentence A imply/contradict/neither sentence B) and is a classification task (given two sentences, predict one of three labels). ''' UpperCamelCase = ''' Computes XNLI score which is just simple accuracy. Args: predictions: Predicted labels. references: Ground truth labels. Returns: \'accuracy\': accuracy Examples: >>> predictions = [0, 1] >>> references = [0, 1] >>> xnli_metric = datasets.load_metric("xnli") >>> results = xnli_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0} ''' def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> int: return (preds == labels).mean() @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCAmelCase_ ( datasets.Metric ): '''simple docstring''' def _snake_case ( self : List[str] ) -> List[str]: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''int64''' if self.config_name != '''sts-b''' else '''float32''' ), '''references''': datasets.Value('''int64''' if self.config_name != '''sts-b''' else '''float32''' ), } ) , codebase_urls=[] , reference_urls=[] , format='''numpy''' , ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> List[Any]: '''simple docstring''' return {"accuracy": simple_accuracy(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )}
319
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( BertTokenizer, ViltConfig, ViltForImageAndTextRetrieval, ViltForImagesAndTextClassification, ViltForMaskedLM, ViltForQuestionAnswering, ViltImageProcessor, ViltProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=False , __lowercase=False , __lowercase=False ) -> Optional[Any]: A: str = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""transformer.blocks.{i}.norm1.weight""", F"""vilt.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.norm1.bias""", F"""vilt.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append( (F"""transformer.blocks.{i}.attn.proj.weight""", F"""vilt.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append( (F"""transformer.blocks.{i}.attn.proj.bias""", F"""vilt.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""transformer.blocks.{i}.norm2.weight""", F"""vilt.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.norm2.bias""", F"""vilt.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append( (F"""transformer.blocks.{i}.mlp.fc1.weight""", F"""vilt.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc1.bias""", F"""vilt.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc2.weight""", F"""vilt.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc2.bias""", F"""vilt.encoder.layer.{i}.output.dense.bias""") ) # embeddings rename_keys.extend( [ # text embeddings ('''text_embeddings.word_embeddings.weight''', '''vilt.embeddings.text_embeddings.word_embeddings.weight'''), ( '''text_embeddings.position_embeddings.weight''', '''vilt.embeddings.text_embeddings.position_embeddings.weight''', ), ('''text_embeddings.position_ids''', '''vilt.embeddings.text_embeddings.position_ids'''), ( '''text_embeddings.token_type_embeddings.weight''', '''vilt.embeddings.text_embeddings.token_type_embeddings.weight''', ), ('''text_embeddings.LayerNorm.weight''', '''vilt.embeddings.text_embeddings.LayerNorm.weight'''), ('''text_embeddings.LayerNorm.bias''', '''vilt.embeddings.text_embeddings.LayerNorm.bias'''), # patch embeddings ('''transformer.cls_token''', '''vilt.embeddings.cls_token'''), ('''transformer.patch_embed.proj.weight''', '''vilt.embeddings.patch_embeddings.projection.weight'''), ('''transformer.patch_embed.proj.bias''', '''vilt.embeddings.patch_embeddings.projection.bias'''), ('''transformer.pos_embed''', '''vilt.embeddings.position_embeddings'''), # token type embeddings ('''token_type_embeddings.weight''', '''vilt.embeddings.token_type_embeddings.weight'''), ] ) # final layernorm + pooler rename_keys.extend( [ ('''transformer.norm.weight''', '''vilt.layernorm.weight'''), ('''transformer.norm.bias''', '''vilt.layernorm.bias'''), ('''pooler.dense.weight''', '''vilt.pooler.dense.weight'''), ('''pooler.dense.bias''', '''vilt.pooler.dense.bias'''), ] ) # classifier head(s) if vqa_model: # classification head rename_keys.extend( [ ('''vqa_classifier.0.weight''', '''classifier.0.weight'''), ('''vqa_classifier.0.bias''', '''classifier.0.bias'''), ('''vqa_classifier.1.weight''', '''classifier.1.weight'''), ('''vqa_classifier.1.bias''', '''classifier.1.bias'''), ('''vqa_classifier.3.weight''', '''classifier.3.weight'''), ('''vqa_classifier.3.bias''', '''classifier.3.bias'''), ] ) elif nlvr_model: # classification head rename_keys.extend( [ ('''nlvr2_classifier.0.weight''', '''classifier.0.weight'''), ('''nlvr2_classifier.0.bias''', '''classifier.0.bias'''), ('''nlvr2_classifier.1.weight''', '''classifier.1.weight'''), ('''nlvr2_classifier.1.bias''', '''classifier.1.bias'''), ('''nlvr2_classifier.3.weight''', '''classifier.3.weight'''), ('''nlvr2_classifier.3.bias''', '''classifier.3.bias'''), ] ) else: pass return rename_keys def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Any: for i in range(config.num_hidden_layers ): A: Tuple = '''vilt.''' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A: List[str] = state_dict.pop(F"""transformer.blocks.{i}.attn.qkv.weight""" ) A: Optional[Any] = state_dict.pop(F"""transformer.blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict A: Dict = in_proj_weight[ : config.hidden_size, : ] A: int = in_proj_bias[: config.hidden_size] A: Any = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A: int = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A: Optional[int] = in_proj_weight[ -config.hidden_size :, : ] A: Optional[Any] = in_proj_bias[-config.hidden_size :] def SCREAMING_SNAKE_CASE( __lowercase ) -> int: A: Optional[int] = ['''head.weight''', '''head.bias'''] for k in ignore_keys: state_dict.pop(__lowercase , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: A: List[Any] = dct.pop(__lowercase ) A: int = val @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str: A: Optional[Any] = ViltConfig(image_size=3_8_4 , patch_size=3_2 , tie_word_embeddings=__lowercase ) A: Tuple = False A: str = False A: List[Any] = False A: Optional[int] = False if "vqa" in checkpoint_url: A: Union[str, Any] = True A: Union[str, Any] = 3_1_2_9 A: List[Any] = '''huggingface/label-files''' A: Any = '''vqa2-id2label.json''' A: Optional[Any] = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: Union[str, Any] = {int(__lowercase ): v for k, v in idalabel.items()} A: Any = idalabel A: Optional[Any] = {v: k for k, v in idalabel.items()} A: List[str] = ViltForQuestionAnswering(__lowercase ) elif "nlvr" in checkpoint_url: A: Dict = True A: str = 2 A: Union[str, Any] = {0: '''False''', 1: '''True'''} A: Any = {v: k for k, v in config.idalabel.items()} A: Optional[Any] = 3 A: Any = ViltForImagesAndTextClassification(__lowercase ) elif "irtr" in checkpoint_url: A: Tuple = True A: Optional[Any] = ViltForImageAndTextRetrieval(__lowercase ) elif "mlm_itm" in checkpoint_url: A: Tuple = True A: Optional[int] = ViltForMaskedLM(__lowercase ) else: raise ValueError('''Unknown model type''' ) # load state_dict of original model, remove and rename some keys A: int = torch.hub.load_state_dict_from_url(__lowercase , map_location='''cpu''' )['''state_dict'''] A: List[str] = create_rename_keys(__lowercase , __lowercase , __lowercase , __lowercase ) for src, dest in rename_keys: rename_key(__lowercase , __lowercase , __lowercase ) read_in_q_k_v(__lowercase , __lowercase ) if mlm_model or irtr_model: A: str = ['''itm_score.fc.weight''', '''itm_score.fc.bias'''] for k in ignore_keys: state_dict.pop(__lowercase , __lowercase ) # load state dict into HuggingFace model model.eval() if mlm_model: A , A: Union[str, Any] = model.load_state_dict(__lowercase , strict=__lowercase ) assert missing_keys == ["mlm_score.decoder.bias"] else: model.load_state_dict(__lowercase ) # Define processor A: Optional[Any] = ViltImageProcessor(size=3_8_4 ) A: Dict = BertTokenizer.from_pretrained('''bert-base-uncased''' ) A: Optional[int] = ViltProcessor(__lowercase , __lowercase ) # Forward pass on example inputs (image + text) if nlvr_model: A: str = Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg''' , stream=__lowercase ).raw ) A: List[str] = Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg''' , stream=__lowercase ).raw ) A: Any = ( '''The left image contains twice the number of dogs as the right image, and at least two dogs in total are''' ''' standing.''' ) A: List[Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: List[Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: List[str] = model( input_ids=encoding_a.input_ids , pixel_values=encoding_a.pixel_values , pixel_values_a=encoding_a.pixel_values , ) else: A: Any = Image.open(requests.get('''http://images.cocodataset.org/val2017/000000039769.jpg''' , stream=__lowercase ).raw ) if mlm_model: A: Optional[int] = '''a bunch of [MASK] laying on a [MASK].''' else: A: Optional[int] = '''How many cats are there?''' A: Union[str, Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: Any = model(**__lowercase ) # Verify outputs if mlm_model: A: Any = torch.Size([1, 1_1, 3_0_5_2_2] ) A: Tuple = torch.tensor([-1_2.5_0_6_1, -1_2.5_1_2_3, -1_2.5_1_7_4] ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , __lowercase , atol=1E-4 ) # verify masked token prediction equals "cats" A: List[str] = outputs.logits[0, 4, :].argmax(-1 ).item() assert tokenizer.decode([predicted_id] ) == "cats" elif vqa_model: A: Any = torch.Size([1, 3_1_2_9] ) A: Optional[int] = torch.tensor([-1_5.9_4_9_5, -1_8.1_4_7_2, -1_0.3_0_4_1] ) assert torch.allclose(outputs.logits[0, :3] , __lowercase , atol=1E-4 ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , __lowercase , atol=1E-4 ) # verify vqa prediction equals "2" A: Dict = outputs.logits.argmax(-1 ).item() assert model.config.idalabel[predicted_idx] == "2" elif nlvr_model: A: Union[str, Any] = torch.Size([1, 2] ) A: Optional[Any] = torch.tensor([-2.8_7_2_1, 2.1_2_9_1] ) assert torch.allclose(outputs.logits[0, :3] , __lowercase , atol=1E-4 ) assert outputs.logits.shape == expected_shape Path(__lowercase ).mkdir(exist_ok=__lowercase ) print(F"""Saving model and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(__lowercase ) processor.save_pretrained(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://github.com/dandelin/ViLT/releases/download/200k/vilt_200k_mlm_itm.ckpt''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) UpperCamelCase = parser.parse_args() convert_vilt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
319
1
'''simple docstring''' import unittest from datasets import load_dataset from transformers.pipelines import pipeline from transformers.testing_utils import is_pipeline_test, nested_simplify, require_torch, slow @is_pipeline_test @require_torch class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' @require_torch def _snake_case ( self : Dict ) -> List[Any]: '''simple docstring''' A: int = pipeline( task='''zero-shot-audio-classification''' , model='''hf-internal-testing/tiny-clap-htsat-unfused''' ) A: List[Any] = load_dataset('''ashraq/esc50''' ) A: int = dataset['''train''']['''audio'''][-1]['''array'''] A: Dict = audio_classifier(SCREAMING_SNAKE_CASE_ , candidate_labels=['''Sound of a dog''', '''Sound of vaccum cleaner'''] ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ) , [{'''score''': 0.501, '''label''': '''Sound of a dog'''}, {'''score''': 0.499, '''label''': '''Sound of vaccum cleaner'''}] , ) @unittest.skip('''No models are available in TF''' ) def _snake_case ( self : List[str] ) -> Optional[int]: '''simple docstring''' pass @slow @require_torch def _snake_case ( self : List[Any] ) -> Dict: '''simple docstring''' A: Any = pipeline( task='''zero-shot-audio-classification''' , model='''laion/clap-htsat-unfused''' , ) # This is an audio of a dog A: Tuple = load_dataset('''ashraq/esc50''' ) A: Optional[Any] = dataset['''train''']['''audio'''][-1]['''array'''] A: Union[str, Any] = audio_classifier(SCREAMING_SNAKE_CASE_ , candidate_labels=['''Sound of a dog''', '''Sound of vaccum cleaner'''] ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ) , [ {'''score''': 0.999, '''label''': '''Sound of a dog'''}, {'''score''': 0.001, '''label''': '''Sound of vaccum cleaner'''}, ] , ) A: Any = audio_classifier([audio] * 5 , candidate_labels=['''Sound of a dog''', '''Sound of vaccum cleaner'''] ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ) , [ [ {'''score''': 0.999, '''label''': '''Sound of a dog'''}, {'''score''': 0.001, '''label''': '''Sound of vaccum cleaner'''}, ], ] * 5 , ) A: Dict = audio_classifier( [audio] * 5 , candidate_labels=['''Sound of a dog''', '''Sound of vaccum cleaner'''] , batch_size=5 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ) , [ [ {'''score''': 0.999, '''label''': '''Sound of a dog'''}, {'''score''': 0.001, '''label''': '''Sound of vaccum cleaner'''}, ], ] * 5 , ) @unittest.skip('''No models are available in TF''' ) def _snake_case ( self : Dict ) -> Dict: '''simple docstring''' pass
319
'''simple docstring''' import argparse import json import os import numpy as np import PIL import requests import tensorflow.keras.applications.efficientnet as efficientnet import torch from huggingface_hub import hf_hub_download from PIL import Image from tensorflow.keras.preprocessing import image from transformers import ( EfficientNetConfig, EfficientNetForImageClassification, EfficientNetImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''b0''': efficientnet.EfficientNetBa, '''b1''': efficientnet.EfficientNetBa, '''b2''': efficientnet.EfficientNetBa, '''b3''': efficientnet.EfficientNetBa, '''b4''': efficientnet.EfficientNetBa, '''b5''': efficientnet.EfficientNetBa, '''b6''': efficientnet.EfficientNetBa, '''b7''': efficientnet.EfficientNetBa, } UpperCamelCase = { '''b0''': { '''hidden_dim''': 1280, '''width_coef''': 1.0, '''depth_coef''': 1.0, '''image_size''': 224, '''dropout_rate''': 0.2, '''dw_padding''': [], }, '''b1''': { '''hidden_dim''': 1280, '''width_coef''': 1.0, '''depth_coef''': 1.1, '''image_size''': 240, '''dropout_rate''': 0.2, '''dw_padding''': [16], }, '''b2''': { '''hidden_dim''': 1408, '''width_coef''': 1.1, '''depth_coef''': 1.2, '''image_size''': 260, '''dropout_rate''': 0.3, '''dw_padding''': [5, 8, 16], }, '''b3''': { '''hidden_dim''': 1536, '''width_coef''': 1.2, '''depth_coef''': 1.4, '''image_size''': 300, '''dropout_rate''': 0.3, '''dw_padding''': [5, 18], }, '''b4''': { '''hidden_dim''': 1792, '''width_coef''': 1.4, '''depth_coef''': 1.8, '''image_size''': 380, '''dropout_rate''': 0.4, '''dw_padding''': [6], }, '''b5''': { '''hidden_dim''': 2048, '''width_coef''': 1.6, '''depth_coef''': 2.2, '''image_size''': 456, '''dropout_rate''': 0.4, '''dw_padding''': [13, 27], }, '''b6''': { '''hidden_dim''': 2304, '''width_coef''': 1.8, '''depth_coef''': 2.6, '''image_size''': 528, '''dropout_rate''': 0.5, '''dw_padding''': [31], }, '''b7''': { '''hidden_dim''': 2560, '''width_coef''': 2.0, '''depth_coef''': 3.1, '''image_size''': 600, '''dropout_rate''': 0.5, '''dw_padding''': [18], }, } def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: A: Tuple = EfficientNetConfig() A: Optional[int] = CONFIG_MAP[model_name]['''hidden_dim'''] A: Optional[int] = CONFIG_MAP[model_name]['''width_coef'''] A: str = CONFIG_MAP[model_name]['''depth_coef'''] A: Dict = CONFIG_MAP[model_name]['''image_size'''] A: str = CONFIG_MAP[model_name]['''dropout_rate'''] A: Optional[Any] = CONFIG_MAP[model_name]['''dw_padding'''] A: Optional[Any] = '''huggingface/label-files''' A: List[str] = '''imagenet-1k-id2label.json''' A: Dict = 1_0_0_0 A: Any = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: Tuple = {int(__lowercase ): v for k, v in idalabel.items()} A: int = idalabel A: Tuple = {v: k for k, v in idalabel.items()} return config def SCREAMING_SNAKE_CASE( ) -> Any: A: Optional[Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' A: Union[str, Any] = Image.open(requests.get(__lowercase , stream=__lowercase ).raw ) return im def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: A: List[str] = CONFIG_MAP[model_name]['''image_size'''] A: List[Any] = EfficientNetImageProcessor( size={'''height''': size, '''width''': size} , image_mean=[0.4_8_5, 0.4_5_6, 0.4_0_6] , image_std=[0.4_7_8_5_3_9_4_4, 0.4_7_3_2_8_6_4, 0.4_7_4_3_4_1_6_3] , do_center_crop=__lowercase , ) return preprocessor def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: A: List[str] = [v.split('''_''' )[0].split('''block''' )[1] for v in original_param_names if v.startswith('''block''' )] A: List[str] = sorted(set(__lowercase ) ) A: Dict = len(__lowercase ) A: List[str] = {b: str(__lowercase ) for b, i in zip(__lowercase , range(__lowercase ) )} A: Optional[int] = [] rename_keys.append(('''stem_conv/kernel:0''', '''embeddings.convolution.weight''') ) rename_keys.append(('''stem_bn/gamma:0''', '''embeddings.batchnorm.weight''') ) rename_keys.append(('''stem_bn/beta:0''', '''embeddings.batchnorm.bias''') ) rename_keys.append(('''stem_bn/moving_mean:0''', '''embeddings.batchnorm.running_mean''') ) rename_keys.append(('''stem_bn/moving_variance:0''', '''embeddings.batchnorm.running_var''') ) for b in block_names: A: int = block_name_mapping[b] rename_keys.append((F"""block{b}_expand_conv/kernel:0""", F"""encoder.blocks.{hf_b}.expansion.expand_conv.weight""") ) rename_keys.append((F"""block{b}_expand_bn/gamma:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.weight""") ) rename_keys.append((F"""block{b}_expand_bn/beta:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.bias""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_mean""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_var""") ) rename_keys.append( (F"""block{b}_dwconv/depthwise_kernel:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight""") ) rename_keys.append((F"""block{b}_bn/gamma:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight""") ) rename_keys.append((F"""block{b}_bn/beta:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias""") ) rename_keys.append( (F"""block{b}_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean""") ) rename_keys.append( (F"""block{b}_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var""") ) rename_keys.append((F"""block{b}_se_reduce/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.weight""") ) rename_keys.append((F"""block{b}_se_reduce/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.bias""") ) rename_keys.append((F"""block{b}_se_expand/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.weight""") ) rename_keys.append((F"""block{b}_se_expand/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.bias""") ) rename_keys.append( (F"""block{b}_project_conv/kernel:0""", F"""encoder.blocks.{hf_b}.projection.project_conv.weight""") ) rename_keys.append((F"""block{b}_project_bn/gamma:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.weight""") ) rename_keys.append((F"""block{b}_project_bn/beta:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.bias""") ) rename_keys.append( (F"""block{b}_project_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_mean""") ) rename_keys.append( (F"""block{b}_project_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_var""") ) rename_keys.append(('''top_conv/kernel:0''', '''encoder.top_conv.weight''') ) rename_keys.append(('''top_bn/gamma:0''', '''encoder.top_bn.weight''') ) rename_keys.append(('''top_bn/beta:0''', '''encoder.top_bn.bias''') ) rename_keys.append(('''top_bn/moving_mean:0''', '''encoder.top_bn.running_mean''') ) rename_keys.append(('''top_bn/moving_variance:0''', '''encoder.top_bn.running_var''') ) A: Union[str, Any] = {} for item in rename_keys: if item[0] in original_param_names: A: str = '''efficientnet.''' + item[1] A: int = '''classifier.weight''' A: Tuple = '''classifier.bias''' return key_mapping def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Tuple: for key, value in tf_params.items(): if "normalization" in key: continue A: Union[str, Any] = key_mapping[key] if "_conv" in key and "kernel" in key: A: List[str] = torch.from_numpy(__lowercase ).permute(3 , 2 , 0 , 1 ) elif "depthwise_kernel" in key: A: List[Any] = torch.from_numpy(__lowercase ).permute(2 , 3 , 0 , 1 ) elif "kernel" in key: A: Optional[Any] = torch.from_numpy(np.transpose(__lowercase ) ) else: A: Any = torch.from_numpy(__lowercase ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(__lowercase ) @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase ) -> Tuple: A: Optional[int] = model_classes[model_name]( include_top=__lowercase , weights='''imagenet''' , input_tensor=__lowercase , input_shape=__lowercase , pooling=__lowercase , classes=1_0_0_0 , classifier_activation='''softmax''' , ) A: List[str] = original_model.trainable_variables A: Optional[Any] = original_model.non_trainable_variables A: Union[str, Any] = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: A: int = param.numpy() A: Tuple = list(tf_params.keys() ) # Load HuggingFace model A: Dict = get_efficientnet_config(__lowercase ) A: Union[str, Any] = EfficientNetForImageClassification(__lowercase ).eval() A: Dict = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print('''Converting parameters...''' ) A: int = rename_keys(__lowercase ) replace_params(__lowercase , __lowercase , __lowercase ) # Initialize preprocessor and preprocess input image A: List[Any] = convert_image_processor(__lowercase ) A: Optional[Any] = preprocessor(images=prepare_img() , return_tensors='''pt''' ) # HF model inference hf_model.eval() with torch.no_grad(): A: str = hf_model(**__lowercase ) A: List[Any] = outputs.logits.detach().numpy() # Original model inference A: Any = False A: List[Any] = CONFIG_MAP[model_name]['''image_size'''] A: List[Any] = prepare_img().resize((image_size, image_size) , resample=PIL.Image.NEAREST ) A: str = image.img_to_array(__lowercase ) A: Dict = np.expand_dims(__lowercase , axis=0 ) A: Any = original_model.predict(__lowercase ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(__lowercase , __lowercase , atol=1E-3 ), "The predicted logits are not the same." print('''Model outputs match!''' ) if save_model: # Create folder to save model if not os.path.isdir(__lowercase ): os.mkdir(__lowercase ) # Save converted model and image processor hf_model.save_pretrained(__lowercase ) preprocessor.save_pretrained(__lowercase ) if push_to_hub: # Push model and image processor to hub print(F"""Pushing converted {model_name} to the hub...""" ) A: int = F"""efficientnet-{model_name}""" preprocessor.push_to_hub(__lowercase ) hf_model.push_to_hub(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''b0''', type=str, help='''Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''hf_model''', type=str, help='''Path to the output PyTorch model directory.''', ) parser.add_argument('''--save_model''', action='''store_true''', help='''Save model to local''') parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Push model and image processor to the hub''') UpperCamelCase = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
319
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) UpperCamelCase = { '''configuration_vision_encoder_decoder''': ['''VisionEncoderDecoderConfig''', '''VisionEncoderDecoderOnnxConfig'''] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''VisionEncoderDecoderModel'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''TFVisionEncoderDecoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''FlaxVisionEncoderDecoderModel'''] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
'''simple docstring''' from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase = {'''configuration_focalnet''': ['''FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FocalNetConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FocalNetForImageClassification''', '''FocalNetForMaskedImageModeling''', '''FocalNetBackbone''', '''FocalNetModel''', '''FocalNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_focalnet import FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FocalNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_focalnet import ( FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST, FocalNetBackbone, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetModel, FocalNetPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
1
'''simple docstring''' import os import re import shutil from argparse import ArgumentParser, Namespace from datasets.commands import BaseDatasetsCLICommand from datasets.utils.logging import get_logger UpperCamelCase = '''<<<<<<< This should probably be modified because it mentions: ''' UpperCamelCase = '''======= >>>>>>> ''' UpperCamelCase = [ '''TextEncoderConfig''', '''ByteTextEncoder''', '''SubwordTextEncoder''', '''encoder_config''', '''maybe_build_from_corpus''', '''manual_dir''', ] UpperCamelCase = [ # (pattern, replacement) # Order is important here for some replacements (R'''tfds\.core''', R'''datasets'''), (R'''tf\.io\.gfile\.GFile''', R'''open'''), (R'''tf\.([\w\d]+)''', R'''datasets.Value(\'\1\')'''), (R'''tfds\.features\.Text\(\)''', R'''datasets.Value(\'string\')'''), (R'''tfds\.features\.Text\(''', R'''datasets.Value(\'string\'),'''), (R'''features\s*=\s*tfds.features.FeaturesDict\(''', R'''features=datasets.Features('''), (R'''tfds\.features\.FeaturesDict\(''', R'''dict('''), (R'''The TensorFlow Datasets Authors''', R'''The TensorFlow Datasets Authors and the HuggingFace Datasets Authors'''), (R'''tfds\.''', R'''datasets.'''), (R'''dl_manager\.manual_dir''', R'''self.config.data_dir'''), (R'''self\.builder_config''', R'''self.config'''), ] def SCREAMING_SNAKE_CASE( __lowercase ) -> Union[str, Any]: return ConvertCommand(args.tfds_path , args.datasets_directory ) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' @staticmethod def _snake_case ( SCREAMING_SNAKE_CASE_ : ArgumentParser ) -> Optional[Any]: '''simple docstring''' A: Optional[Any] = parser.add_parser( '''convert''' , help='''Convert a TensorFlow Datasets dataset to a HuggingFace Datasets dataset.''' , ) train_parser.add_argument( '''--tfds_path''' , type=SCREAMING_SNAKE_CASE_ , required=SCREAMING_SNAKE_CASE_ , help='''Path to a TensorFlow Datasets folder to convert or a single tfds file to convert.''' , ) train_parser.add_argument( '''--datasets_directory''' , type=SCREAMING_SNAKE_CASE_ , required=SCREAMING_SNAKE_CASE_ , help='''Path to the HuggingFace Datasets folder.''' ) train_parser.set_defaults(func=SCREAMING_SNAKE_CASE_ ) def __init__( self : Any , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : str , *SCREAMING_SNAKE_CASE_ : Any ) -> Any: '''simple docstring''' A: List[str] = get_logger('''datasets-cli/converting''' ) A: Tuple = tfds_path A: Tuple = datasets_directory def _snake_case ( self : str ) -> str: '''simple docstring''' if os.path.isdir(self._tfds_path ): A: List[Any] = os.path.abspath(self._tfds_path ) elif os.path.isfile(self._tfds_path ): A: Tuple = os.path.dirname(self._tfds_path ) else: raise ValueError('''--tfds_path is neither a directory nor a file. Please check path.''' ) A: int = os.path.abspath(self._datasets_directory ) self._logger.info(f"""Converting datasets from {abs_tfds_path} to {abs_datasets_path}""" ) A: Tuple = [] A: List[Any] = [] A: Dict = {} if os.path.isdir(self._tfds_path ): A: Optional[Any] = os.listdir(SCREAMING_SNAKE_CASE_ ) else: A: List[Any] = [os.path.basename(self._tfds_path )] for f_name in file_names: self._logger.info(f"""Looking at file {f_name}""" ) A: Optional[int] = os.path.join(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Any = os.path.join(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if not os.path.isfile(SCREAMING_SNAKE_CASE_ ) or "__init__" in f_name or "_test" in f_name or ".py" not in f_name: self._logger.info('''Skipping file''' ) continue with open(SCREAMING_SNAKE_CASE_ , encoding='''utf-8''' ) as f: A: int = f.readlines() A: Optional[Any] = [] A: List[Any] = False A: str = False A: str = [] for line in lines: A: int = line # Convert imports if "import tensorflow.compat.v2 as tf" in out_line: continue elif "@tfds.core" in out_line: continue elif "builder=self" in out_line: continue elif "import tensorflow_datasets.public_api as tfds" in out_line: A: Optional[int] = '''import datasets\n''' elif "import tensorflow" in out_line: # order is important here A: Any = '''''' continue elif "from absl import logging" in out_line: A: List[str] = '''from datasets import logging\n''' elif "getLogger" in out_line: A: Optional[Any] = out_line.replace('''getLogger''' , '''get_logger''' ) elif any(expression in out_line for expression in TO_HIGHLIGHT ): A: str = True A: List[str] = list(filter(lambda SCREAMING_SNAKE_CASE_ : e in out_line , SCREAMING_SNAKE_CASE_ ) ) out_lines.append(HIGHLIGHT_MESSAGE_PRE + str(SCREAMING_SNAKE_CASE_ ) + '''\n''' ) out_lines.append(SCREAMING_SNAKE_CASE_ ) out_lines.append(SCREAMING_SNAKE_CASE_ ) continue else: for pattern, replacement in TO_CONVERT: A: Optional[Any] = re.sub(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Take care of saving utilities (to later move them together with main script) if "tensorflow_datasets" in out_line: A: int = re.match(R'''from\stensorflow_datasets.*import\s([^\.\r\n]+)''' , SCREAMING_SNAKE_CASE_ ) tfds_imports.extend(imp.strip() for imp in match.group(1 ).split(''',''' ) ) A: List[str] = '''from . import ''' + match.group(1 ) # Check we have not forget anything if "tf." in out_line or "tfds." in out_line or "tensorflow_datasets" in out_line: raise ValueError(f"""Error converting {out_line.strip()}""" ) if "GeneratorBasedBuilder" in out_line or "BeamBasedBuilder" in out_line: A: Tuple = True out_lines.append(SCREAMING_SNAKE_CASE_ ) if is_builder or "wmt" in f_name: # We create a new directory for each dataset A: str = f_name.replace('''.py''' , '''''' ) A: Any = os.path.join(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: int = os.path.join(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) os.makedirs(SCREAMING_SNAKE_CASE_ , exist_ok=SCREAMING_SNAKE_CASE_ ) self._logger.info(f"""Adding directory {output_dir}""" ) imports_to_builder_map.update({imp: output_dir for imp in tfds_imports} ) else: # Utilities will be moved at the end utils_files.append(SCREAMING_SNAKE_CASE_ ) if needs_manual_update: with_manual_update.append(SCREAMING_SNAKE_CASE_ ) with open(SCREAMING_SNAKE_CASE_ , '''w''' , encoding='''utf-8''' ) as f: f.writelines(SCREAMING_SNAKE_CASE_ ) self._logger.info(f"""Converted in {output_file}""" ) for utils_file in utils_files: try: A: List[Any] = os.path.basename(SCREAMING_SNAKE_CASE_ ) A: List[str] = imports_to_builder_map[f_name.replace('''.py''' , '''''' )] self._logger.info(f"""Moving {dest_folder} to {utils_file}""" ) shutil.copy(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) except KeyError: self._logger.error(f"""Cannot find destination folder for {utils_file}. Please copy manually.""" ) if with_manual_update: for file_path in with_manual_update: self._logger.warning( f"""You need to manually update file {file_path} to remove configurations using 'TextEncoderConfig'.""" )
319
'''simple docstring''' from typing import Any, Dict, List, Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from ..image_utils import load_image if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_OBJECT_DETECTION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = Dict[str, Any] UpperCamelCase = List[Prediction] @add_end_docstrings(UpperCAmelCase_ ) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : List[str] ) -> int: '''simple docstring''' super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if self.framework == "tf": raise ValueError(f"""The {self.__class__} is only available in PyTorch.""" ) requires_backends(self , '''vision''' ) self.check_model_type( dict(MODEL_FOR_OBJECT_DETECTION_MAPPING.items() + MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.items() ) ) def _snake_case ( self : int , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[Any]: '''simple docstring''' A: Any = {} if "threshold" in kwargs: A: List[Any] = kwargs['''threshold'''] return {}, {}, postprocess_kwargs def __call__( self : str , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Union[Predictions, List[Prediction]]: '''simple docstring''' return super().__call__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Union[str, Any]: '''simple docstring''' A: int = load_image(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = torch.IntTensor([[image.height, image.width]] ) A: Union[str, Any] = self.image_processor(images=[image] , return_tensors='''pt''' ) if self.tokenizer is not None: A: int = self.tokenizer(text=inputs['''words'''] , boxes=inputs['''boxes'''] , return_tensors='''pt''' ) A: Any = target_size return inputs def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : str ) -> List[Any]: '''simple docstring''' A: Tuple = model_inputs.pop('''target_size''' ) A: Tuple = self.model(**SCREAMING_SNAKE_CASE_ ) A: List[str] = outputs.__class__({'''target_size''': target_size, **outputs} ) if self.tokenizer is not None: A: Dict = model_inputs['''bbox'''] return model_outputs def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : str=0.9 ) -> Union[str, Any]: '''simple docstring''' A: List[Any] = model_outputs['''target_size'''] if self.tokenizer is not None: # This is a LayoutLMForTokenClassification variant. # The OCR got the boxes and the model classified the words. A , A: Union[str, Any] = target_size[0].tolist() def unnormalize(SCREAMING_SNAKE_CASE_ : str ): return self._get_bounding_box( torch.Tensor( [ (width * bbox[0] / 10_00), (height * bbox[1] / 10_00), (width * bbox[2] / 10_00), (height * bbox[3] / 10_00), ] ) ) A , A: Dict = model_outputs['''logits'''].squeeze(0 ).softmax(dim=-1 ).max(dim=-1 ) A: List[str] = [self.model.config.idalabel[prediction] for prediction in classes.tolist()] A: List[str] = [unnormalize(SCREAMING_SNAKE_CASE_ ) for bbox in model_outputs['''bbox'''].squeeze(0 )] A: Dict = ['''score''', '''label''', '''box'''] A: Optional[int] = [dict(zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) for vals in zip(scores.tolist() , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if vals[0] > threshold] else: # This is a regular ForObjectDetectionModel A: Any = self.image_processor.post_process_object_detection(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: List[str] = raw_annotations[0] A: List[Any] = raw_annotation['''scores'''] A: List[Any] = raw_annotation['''labels'''] A: int = raw_annotation['''boxes'''] A: Any = scores.tolist() A: List[Any] = [self.model.config.idalabel[label.item()] for label in labels] A: List[Any] = [self._get_bounding_box(SCREAMING_SNAKE_CASE_ ) for box in boxes] # {"scores": [...], ...} --> [{"score":x, ...}, ...] A: Tuple = ['''score''', '''label''', '''box'''] A: str = [ dict(zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) for vals in zip(raw_annotation['''scores'''] , raw_annotation['''labels'''] , raw_annotation['''boxes'''] ) ] return annotation def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : "torch.Tensor" ) -> Dict[str, int]: '''simple docstring''' if self.framework != "pt": raise ValueError('''The ObjectDetectionPipeline is only available in PyTorch.''' ) A , A , A , A: str = box.int().tolist() A: str = { '''xmin''': xmin, '''ymin''': ymin, '''xmax''': xmax, '''ymax''': ymax, } return bbox
319
1
'''simple docstring''' import argparse import logging import os from pathlib import Path from typing import Any, Dict import pytorch_lightning as pl from pytorch_lightning.utilities import rank_zero_info from transformers import ( AdamW, AutoConfig, AutoModel, AutoModelForPreTraining, AutoModelForQuestionAnswering, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoModelForTokenClassification, AutoModelWithLMHead, AutoTokenizer, PretrainedConfig, PreTrainedTokenizer, ) from transformers.optimization import ( Adafactor, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.utils.versions import require_version UpperCamelCase = logging.getLogger(__name__) require_version('''pytorch_lightning>=1.0.4''') UpperCamelCase = { '''base''': AutoModel, '''sequence-classification''': AutoModelForSequenceClassification, '''question-answering''': AutoModelForQuestionAnswering, '''pretraining''': AutoModelForPreTraining, '''token-classification''': AutoModelForTokenClassification, '''language-modeling''': AutoModelWithLMHead, '''summarization''': AutoModelForSeqaSeqLM, '''translation''': AutoModelForSeqaSeqLM, } # update this and the import above to support new schedulers from transformers.optimization UpperCamelCase = { '''linear''': get_linear_schedule_with_warmup, '''cosine''': get_cosine_schedule_with_warmup, '''cosine_w_restarts''': get_cosine_with_hard_restarts_schedule_with_warmup, '''polynomial''': get_polynomial_decay_schedule_with_warmup, # '': get_constant_schedule, # not supported for now # '': get_constant_schedule_with_warmup, # not supported for now } UpperCamelCase = sorted(arg_to_scheduler.keys()) UpperCamelCase = '''{''' + ''', '''.join(arg_to_scheduler_choices) + '''}''' class lowerCAmelCase_ ( pl.LightningModule ): '''simple docstring''' def __init__( self : Tuple , SCREAMING_SNAKE_CASE_ : argparse.Namespace , SCREAMING_SNAKE_CASE_ : int=None , SCREAMING_SNAKE_CASE_ : Union[str, Any]="base" , SCREAMING_SNAKE_CASE_ : int=None , SCREAMING_SNAKE_CASE_ : List[Any]=None , SCREAMING_SNAKE_CASE_ : List[str]=None , **SCREAMING_SNAKE_CASE_ : Any , ) -> str: '''simple docstring''' super().__init__() # TODO: move to self.save_hyperparameters() # self.save_hyperparameters() # can also expand arguments into trainer signature for easier reading self.save_hyperparameters(SCREAMING_SNAKE_CASE_ ) A: Any = 0 A: List[str] = Path(self.hparams.output_dir ) A: Dict = self.hparams.cache_dir if self.hparams.cache_dir else None if config is None: A: str = AutoConfig.from_pretrained( self.hparams.config_name if self.hparams.config_name else self.hparams.model_name_or_path , **({'''num_labels''': num_labels} if num_labels is not None else {}) , cache_dir=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) else: A: PretrainedConfig = config A: str = ('''encoder_layerdrop''', '''decoder_layerdrop''', '''dropout''', '''attention_dropout''') for p in extra_model_params: if getattr(self.hparams , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): assert hasattr(self.config , SCREAMING_SNAKE_CASE_ ), f"""model config doesn't have a `{p}` attribute""" setattr(self.config , SCREAMING_SNAKE_CASE_ , getattr(self.hparams , SCREAMING_SNAKE_CASE_ ) ) if tokenizer is None: A: Any = AutoTokenizer.from_pretrained( self.hparams.tokenizer_name if self.hparams.tokenizer_name else self.hparams.model_name_or_path , cache_dir=SCREAMING_SNAKE_CASE_ , ) else: A: PreTrainedTokenizer = tokenizer A: Dict = MODEL_MODES[mode] if model is None: A: Any = self.model_type.from_pretrained( self.hparams.model_name_or_path , from_tf=bool('''.ckpt''' in self.hparams.model_name_or_path ) , config=self.config , cache_dir=SCREAMING_SNAKE_CASE_ , ) else: A: List[Any] = model def _snake_case ( self : str , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Tuple ) -> List[str]: '''simple docstring''' A: Union[str, Any] = self.model_type.from_pretrained(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Tuple ) -> Optional[int]: '''simple docstring''' A: Optional[Any] = arg_to_scheduler[self.hparams.lr_scheduler] A: Tuple = get_schedule_func( self.opt , num_warmup_steps=self.hparams.warmup_steps , num_training_steps=self.total_steps() ) A: Any = {'''scheduler''': scheduler, '''interval''': '''step''', '''frequency''': 1} return scheduler def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' A: List[str] = self.model A: str = ['''bias''', '''LayerNorm.weight'''] A: Tuple = [ { '''params''': [ p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay ) ], # check this named paramters '''weight_decay''': self.hparams.weight_decay, }, { '''params''': [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay )], '''weight_decay''': 0.0, }, ] if self.hparams.adafactor: A: Union[str, Any] = Adafactor( SCREAMING_SNAKE_CASE_ , lr=self.hparams.learning_rate , scale_parameter=SCREAMING_SNAKE_CASE_ , relative_step=SCREAMING_SNAKE_CASE_ ) else: A: List[str] = AdamW( SCREAMING_SNAKE_CASE_ , lr=self.hparams.learning_rate , eps=self.hparams.adam_epsilon ) A: List[Any] = optimizer A: Dict = self.get_lr_scheduler() return [optimizer], [scheduler] def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Any ) -> Union[str, Any]: '''simple docstring''' return self.validation_step(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Tuple ) -> List[str]: '''simple docstring''' return self.validation_end(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int ) -> int: '''simple docstring''' A: str = max(1 , self.hparams.gpus ) # TODO: consider num_tpu_cores A: Union[str, Any] = self.hparams.train_batch_size * self.hparams.accumulate_grad_batches * num_devices return (self.dataset_size / effective_batch_size) * self.hparams.max_epochs def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Any ) -> Optional[Any]: '''simple docstring''' if stage == "test": A: List[Any] = len(self.test_dataloader().dataset ) else: A: Union[str, Any] = self.get_dataloader('''train''' , self.hparams.train_batch_size , shuffle=SCREAMING_SNAKE_CASE_ ) A: Dict = len(self.train_dataloader().dataset ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : bool = False ) -> Optional[Any]: '''simple docstring''' raise NotImplementedError('''You must implement this for your task''' ) def _snake_case ( self : Tuple ) -> Optional[int]: '''simple docstring''' return self.train_loader def _snake_case ( self : int ) -> Any: '''simple docstring''' return self.get_dataloader('''dev''' , self.hparams.eval_batch_size , shuffle=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] ) -> List[str]: '''simple docstring''' return self.get_dataloader('''test''' , self.hparams.eval_batch_size , shuffle=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Union[str, Any]: '''simple docstring''' return os.path.join( self.hparams.data_dir , '''cached_{}_{}_{}'''.format( SCREAMING_SNAKE_CASE_ , list(filter(SCREAMING_SNAKE_CASE_ , self.hparams.model_name_or_path.split('''/''' ) ) ).pop() , str(self.hparams.max_seq_length ) , ) , ) @pl.utilities.rank_zero_only def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Dict[str, Any] ) -> None: '''simple docstring''' A: int = self.output_dir.joinpath('''best_tfmr''' ) A: Optional[int] = self.step_count self.model.save_pretrained(SCREAMING_SNAKE_CASE_ ) self.tokenizer.save_pretrained(SCREAMING_SNAKE_CASE_ ) @staticmethod def _snake_case ( SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Tuple ) -> str: '''simple docstring''' parser.add_argument( '''--model_name_or_path''' , default=SCREAMING_SNAKE_CASE_ , type=SCREAMING_SNAKE_CASE_ , required=SCREAMING_SNAKE_CASE_ , help='''Path to pretrained model or model identifier from huggingface.co/models''' , ) parser.add_argument( '''--config_name''' , default='''''' , type=SCREAMING_SNAKE_CASE_ , help='''Pretrained config name or path if not the same as model_name''' ) parser.add_argument( '''--tokenizer_name''' , default=SCREAMING_SNAKE_CASE_ , type=SCREAMING_SNAKE_CASE_ , help='''Pretrained tokenizer name or path if not the same as model_name''' , ) parser.add_argument( '''--cache_dir''' , default=str(Path(SCREAMING_SNAKE_CASE_ ).parent / '''test_run''' / '''cache''' ) , type=SCREAMING_SNAKE_CASE_ , help='''Where do you want to store the pre-trained models downloaded from huggingface.co''' , ) parser.add_argument( '''--encoder_layerdrop''' , type=SCREAMING_SNAKE_CASE_ , help='''Encoder layer dropout probability (Optional). Goes into model.config''' , ) parser.add_argument( '''--decoder_layerdrop''' , type=SCREAMING_SNAKE_CASE_ , help='''Decoder layer dropout probability (Optional). Goes into model.config''' , ) parser.add_argument( '''--dropout''' , type=SCREAMING_SNAKE_CASE_ , help='''Dropout probability (Optional). Goes into model.config''' , ) parser.add_argument( '''--attention_dropout''' , type=SCREAMING_SNAKE_CASE_ , help='''Attention dropout probability (Optional). Goes into model.config''' , ) parser.add_argument('''--learning_rate''' , default=5E-5 , type=SCREAMING_SNAKE_CASE_ , help='''The initial learning rate for Adam.''' ) parser.add_argument( '''--lr_scheduler''' , default='''linear''' , choices=SCREAMING_SNAKE_CASE_ , metavar=SCREAMING_SNAKE_CASE_ , type=SCREAMING_SNAKE_CASE_ , help='''Learning rate scheduler''' , ) parser.add_argument('''--weight_decay''' , default=0.0 , type=SCREAMING_SNAKE_CASE_ , help='''Weight decay if we apply some.''' ) parser.add_argument('''--adam_epsilon''' , default=1E-8 , type=SCREAMING_SNAKE_CASE_ , help='''Epsilon for Adam optimizer.''' ) parser.add_argument('''--warmup_steps''' , default=0 , type=SCREAMING_SNAKE_CASE_ , help='''Linear warmup over warmup_steps.''' ) parser.add_argument('''--num_workers''' , default=4 , type=SCREAMING_SNAKE_CASE_ , help='''kwarg passed to DataLoader''' ) parser.add_argument('''--num_train_epochs''' , dest='''max_epochs''' , default=3 , type=SCREAMING_SNAKE_CASE_ ) parser.add_argument('''--train_batch_size''' , default=32 , type=SCREAMING_SNAKE_CASE_ ) parser.add_argument('''--eval_batch_size''' , default=32 , type=SCREAMING_SNAKE_CASE_ ) parser.add_argument('''--adafactor''' , action='''store_true''' ) class lowerCAmelCase_ ( pl.Callback ): '''simple docstring''' def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' if ( trainer.is_global_zero and trainer.global_rank == 0 ): # we initialize the retriever only on master worker with RAY. In new pytorch-lightning accelorators are removed. pl_module.model.rag.retriever.init_retrieval() # better to use hook functions. class lowerCAmelCase_ ( pl.Callback ): '''simple docstring''' def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Any ) -> Union[str, Any]: '''simple docstring''' for name, param in pl_module.model.rag.named_parameters(): if param.grad is None: print(SCREAMING_SNAKE_CASE_ ) class lowerCAmelCase_ ( pl.Callback ): '''simple docstring''' def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> str: '''simple docstring''' A: List[Any] = trainer.lr_schedulers[0]['''scheduler'''] A: Optional[Any] = {f"""lr_group_{i}""": lr for i, lr in enumerate(lr_scheduler.get_lr() )} pl_module.logger.log_metrics(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : pl.Trainer , SCREAMING_SNAKE_CASE_ : pl.LightningModule ) -> List[str]: '''simple docstring''' rank_zero_info('''***** Validation results *****''' ) A: Any = trainer.callback_metrics # Log results for key in sorted(SCREAMING_SNAKE_CASE_ ): if key not in ["log", "progress_bar"]: rank_zero_info('''{} = {}\n'''.format(SCREAMING_SNAKE_CASE_ , str(metrics[key] ) ) ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : pl.Trainer , SCREAMING_SNAKE_CASE_ : pl.LightningModule ) -> Union[str, Any]: '''simple docstring''' rank_zero_info('''***** Test results *****''' ) A: Union[str, Any] = trainer.callback_metrics # Log and save results to file A: int = os.path.join(pl_module.hparams.output_dir , '''test_results.txt''' ) with open(SCREAMING_SNAKE_CASE_ , '''w''' ) as writer: for key in sorted(SCREAMING_SNAKE_CASE_ ): if key not in ["log", "progress_bar"]: rank_zero_info('''{} = {}\n'''.format(SCREAMING_SNAKE_CASE_ , str(metrics[key] ) ) ) writer.write('''{} = {}\n'''.format(SCREAMING_SNAKE_CASE_ , str(metrics[key] ) ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> None: # To allow all pl args uncomment the following line # parser = pl.Trainer.add_argparse_args(parser) parser.add_argument( '''--output_dir''' , default=str(Path(__lowercase ).parent / '''test_run''' / '''model_checkpoints''' ) , type=__lowercase , help='''The output directory where the model predictions and checkpoints will be written.''' , ) parser.add_argument( '''--fp16''' , action='''store_true''' , help='''Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit''' , ) parser.add_argument( '''--fp16_opt_level''' , type=__lowercase , default='''O2''' , help=( '''For fp16: Apex AMP optimization level selected in [\'O0\', \'O1\', \'O2\', and \'O3\'].''' '''See details at https://nvidia.github.io/apex/amp.html''' ) , ) parser.add_argument('''--n_tpu_cores''' , dest='''tpu_cores''' , type=__lowercase ) parser.add_argument('''--max_grad_norm''' , dest='''gradient_clip_val''' , default=1.0 , type=__lowercase , help='''Max gradient norm''' ) parser.add_argument('''--do_train''' , action='''store_true''' , help='''Whether to run training.''' ) parser.add_argument('''--do_predict''' , action='''store_true''' , help='''Whether to run predictions on the test set.''' ) parser.add_argument( '''--gradient_accumulation_steps''' , dest='''accumulate_grad_batches''' , type=__lowercase , default=1 , help='''Number of updates steps to accumulate before performing a backward/update pass.''' , ) parser.add_argument('''--seed''' , type=__lowercase , default=4_2 , help='''random seed for initialization''' ) parser.add_argument( '''--data_dir''' , default=str(Path(__lowercase ).parent / '''test_run''' / '''dummy-train-data''' ) , type=__lowercase , help='''The input data dir. Should contain the training files for the CoNLL-2003 NER task.''' , ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=None , __lowercase=True , __lowercase=[] , __lowercase=None , __lowercase=None , **__lowercase , ) -> Optional[Any]: pl.seed_everything(args.seed ) # init model A: int = Path(model.hparams.output_dir ) odir.mkdir(exist_ok=__lowercase ) # add custom checkpoints if checkpoint_callback is None: A: Optional[int] = pl.callbacks.ModelCheckpoint( filepath=args.output_dir , prefix='''checkpoint''' , monitor='''val_loss''' , mode='''min''' , save_top_k=1 ) if early_stopping_callback: extra_callbacks.append(__lowercase ) if logging_callback is None: A: int = LoggingCallback() A: Optional[int] = {} if args.fpaa: A: int = 1_6 if args.gpus > 1: A: Union[str, Any] = '''auto''' A: Tuple = '''ddp''' A: Optional[int] = args.accumulate_grad_batches A: str = None A: Any = '''auto''' A: int = pl.Trainer.from_argparse_args( __lowercase , weights_summary=__lowercase , callbacks=[logging_callback] + extra_callbacks + [InitCallback()] + [checkpoint_callback] , logger=__lowercase , val_check_interval=1 , num_sanity_val_steps=2 , **__lowercase , ) if args.do_train: trainer.fit(__lowercase ) else: print('''RAG modeling tests with new set functions successfuly executed!''' ) return trainer
319
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''YituTech/conv-bert-base''': '''https://huggingface.co/YituTech/conv-bert-base/resolve/main/config.json''', '''YituTech/conv-bert-medium-small''': ( '''https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/config.json''' ), '''YituTech/conv-bert-small''': '''https://huggingface.co/YituTech/conv-bert-small/resolve/main/config.json''', # See all ConvBERT models at https://huggingface.co/models?filter=convbert } class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = """convbert""" def __init__( self : Dict , SCREAMING_SNAKE_CASE_ : Dict=3_05_22 , SCREAMING_SNAKE_CASE_ : int=7_68 , SCREAMING_SNAKE_CASE_ : List[str]=12 , SCREAMING_SNAKE_CASE_ : List[str]=12 , SCREAMING_SNAKE_CASE_ : Dict=30_72 , SCREAMING_SNAKE_CASE_ : Optional[int]="gelu" , SCREAMING_SNAKE_CASE_ : List[Any]=0.1 , SCREAMING_SNAKE_CASE_ : int=0.1 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=5_12 , SCREAMING_SNAKE_CASE_ : List[Any]=2 , SCREAMING_SNAKE_CASE_ : List[str]=0.02 , SCREAMING_SNAKE_CASE_ : int=1E-12 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=1 , SCREAMING_SNAKE_CASE_ : int=0 , SCREAMING_SNAKE_CASE_ : str=2 , SCREAMING_SNAKE_CASE_ : List[Any]=7_68 , SCREAMING_SNAKE_CASE_ : Optional[Any]=2 , SCREAMING_SNAKE_CASE_ : Any=9 , SCREAMING_SNAKE_CASE_ : Tuple=1 , SCREAMING_SNAKE_CASE_ : List[Any]=None , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> List[Any]: '''simple docstring''' super().__init__( pad_token_id=SCREAMING_SNAKE_CASE_ , bos_token_id=SCREAMING_SNAKE_CASE_ , eos_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Dict = vocab_size A: Tuple = hidden_size A: Optional[int] = num_hidden_layers A: List[str] = num_attention_heads A: int = intermediate_size A: int = hidden_act A: List[str] = hidden_dropout_prob A: int = attention_probs_dropout_prob A: Tuple = max_position_embeddings A: Any = type_vocab_size A: str = initializer_range A: Union[str, Any] = layer_norm_eps A: str = embedding_size A: Optional[int] = head_ratio A: List[Any] = conv_kernel_size A: List[Any] = num_groups A: Optional[int] = classifier_dropout class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' @property def _snake_case ( self : Optional[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": A: Tuple = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: A: List[str] = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis), ] )
319
1
'''simple docstring''' import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' @register_to_config def __init__( self : Dict , *, SCREAMING_SNAKE_CASE_ : int = 4 , SCREAMING_SNAKE_CASE_ : int = 7_68 , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[int] , ) -> Tuple: '''simple docstring''' super().__init__() A: Optional[int] = nn.Parameter(torch.zeros(SCREAMING_SNAKE_CASE_ ) ) # parameters for additional clip time embeddings A: List[Any] = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: List[str] = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # parameters for encoder hidden states A: Any = clip_extra_context_tokens A: Optional[int] = nn.Linear( SCREAMING_SNAKE_CASE_ , self.clip_extra_context_tokens * cross_attention_dim ) A: str = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Tuple = nn.LayerNorm(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[Any] , *, SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[Any] ) -> List[Any]: '''simple docstring''' if do_classifier_free_guidance: # Add the classifier free guidance embeddings to the image embeddings A: List[Any] = image_embeddings.shape[0] A: Optional[Any] = self.learned_classifier_free_guidance_embeddings.unsqueeze(0 ) A: Optional[Any] = classifier_free_guidance_embeddings.expand( SCREAMING_SNAKE_CASE_ , -1 ) A: Any = torch.cat([classifier_free_guidance_embeddings, image_embeddings] , dim=0 ) # The image embeddings batch size and the text embeddings batch size are equal assert image_embeddings.shape[0] == prompt_embeds.shape[0] A: Dict = prompt_embeds.shape[0] # "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and # adding CLIP embeddings to the existing timestep embedding, ... A: Any = self.embedding_proj(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = self.clip_image_embeddings_project_to_time_embeddings(SCREAMING_SNAKE_CASE_ ) A: int = time_projected_image_embeddings + time_projected_prompt_embeds # ... and by projecting CLIP embeddings into four # extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder" A: List[str] = self.clip_extra_context_tokens_proj(SCREAMING_SNAKE_CASE_ ) A: Any = clip_extra_context_tokens.reshape(SCREAMING_SNAKE_CASE_ , -1 , self.clip_extra_context_tokens ) A: Union[str, Any] = clip_extra_context_tokens.permute(0 , 2 , 1 ) A: Tuple = self.encoder_hidden_states_proj(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = self.text_encoder_hidden_states_norm(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = torch.cat([clip_extra_context_tokens, text_encoder_hidden_states] , dim=1 ) return text_encoder_hidden_states, additive_clip_time_embeddings
319
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE( __lowercase ) -> bool: if len(__lowercase ) < 2: raise ValueError('''Monogons and Digons are not polygons in the Euclidean space''' ) if any(i <= 0 for i in nums ): raise ValueError('''All values must be greater than 0''' ) A: Any = nums.copy() copy_nums.sort() return copy_nums[-1] < sum(copy_nums[:-1] ) if __name__ == "__main__": import doctest doctest.testmod()
319
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) UpperCamelCase = { '''configuration_encodec''': [ '''ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EncodecConfig''', ], '''feature_extraction_encodec''': ['''EncodecFeatureExtractor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EncodecModel''', '''EncodecPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_encodec import ( ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP, EncodecConfig, ) from .feature_extraction_encodec import EncodecFeatureExtractor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_encodec import ( ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST, EncodecModel, EncodecPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
'''simple docstring''' # tests directory-specific settings - this file is run automatically # by pytest before any tests are run import doctest import sys import warnings from os.path import abspath, dirname, join import _pytest from transformers.testing_utils import HfDoctestModule, HfDocTestParser # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. UpperCamelCase = abspath(join(dirname(__file__), '''src''')) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action='''ignore''', category=FutureWarning) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: config.addinivalue_line( '''markers''' , '''is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested''' ) config.addinivalue_line( '''markers''' , '''is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested''' ) config.addinivalue_line('''markers''' , '''is_pipeline_test: mark test to run only when pipelines are tested''' ) config.addinivalue_line('''markers''' , '''is_staging_test: mark test to run only in the staging environment''' ) config.addinivalue_line('''markers''' , '''accelerate_tests: mark test that require accelerate''' ) config.addinivalue_line('''markers''' , '''tool_tests: mark the tool tests that are run on their specific schedule''' ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: from transformers.testing_utils import pytest_terminal_summary_main A: Optional[int] = terminalreporter.config.getoption('''--make-reports''' ) if make_reports: pytest_terminal_summary_main(__lowercase , id=__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Any: # If no tests are collected, pytest exists with code 5, which makes the CI fail. if exitstatus == 5: A: Tuple = 0 # Doctest custom flag to ignore output. UpperCamelCase = doctest.register_optionflag('''IGNORE_RESULT''') UpperCamelCase = doctest.OutputChecker class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ) -> str: '''simple docstring''' if IGNORE_RESULT & optionflags: return True return OutputChecker.check_output(self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase = CustomOutputChecker UpperCamelCase = HfDoctestModule UpperCamelCase = HfDocTestParser
319
1
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=False ) -> Dict: A: Union[str, Any] = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""blocks.{i}.norm1.weight""", F"""vit.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""blocks.{i}.norm1.bias""", F"""vit.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append((F"""blocks.{i}.attn.proj.weight""", F"""vit.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.attn.proj.bias""", F"""vit.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""blocks.{i}.norm2.weight""", F"""vit.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""blocks.{i}.norm2.bias""", F"""vit.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.weight""", F"""vit.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.bias""", F"""vit.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.weight""", F"""vit.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.bias""", F"""vit.encoder.layer.{i}.output.dense.bias""") ) # projection layer + position embeddings rename_keys.extend( [ ('''cls_token''', '''vit.embeddings.cls_token'''), ('''patch_embed.proj.weight''', '''vit.embeddings.patch_embeddings.projection.weight'''), ('''patch_embed.proj.bias''', '''vit.embeddings.patch_embeddings.projection.bias'''), ('''pos_embed''', '''vit.embeddings.position_embeddings'''), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ('''norm.weight''', '''layernorm.weight'''), ('''norm.bias''', '''layernorm.bias'''), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" A: Union[str, Any] = [(pair[0], pair[1][4:]) if pair[1].startswith('''vit''' ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ('''norm.weight''', '''vit.layernorm.weight'''), ('''norm.bias''', '''vit.layernorm.bias'''), ('''head.weight''', '''classifier.weight'''), ('''head.bias''', '''classifier.bias'''), ] ) return rename_keys def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=False ) -> int: for i in range(config.num_hidden_layers ): if base_model: A: Dict = '''''' else: A: Union[str, Any] = '''vit.''' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A: Tuple = state_dict.pop(F"""blocks.{i}.attn.qkv.weight""" ) A: Any = state_dict.pop(F"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict A: Dict = in_proj_weight[ : config.hidden_size, : ] A: Dict = in_proj_bias[: config.hidden_size] A: Optional[int] = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A: List[str] = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A: str = in_proj_weight[ -config.hidden_size :, : ] A: List[Any] = in_proj_bias[-config.hidden_size :] def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: A: Dict = ['''head.weight''', '''head.bias'''] for k in ignore_keys: state_dict.pop(__lowercase , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Optional[int]: A: str = dct.pop(__lowercase ) A: List[str] = val def SCREAMING_SNAKE_CASE( ) -> Tuple: A: Tuple = '''http://images.cocodataset.org/val2017/000000039769.jpg''' A: str = Image.open(requests.get(__lowercase , stream=__lowercase ).raw ) return im @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=True ) -> Tuple: A: Tuple = ViTConfig() # patch_size if model_name[-1] == "8": A: List[str] = 8 # set labels if required if not base_model: A: Dict = 1_0_0_0 A: Tuple = '''huggingface/label-files''' A: Optional[int] = '''imagenet-1k-id2label.json''' A: Any = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: str = {int(__lowercase ): v for k, v in idalabel.items()} A: int = idalabel A: Tuple = {v: k for k, v in idalabel.items()} # size of the architecture if model_name in ["dino_vits8", "dino_vits16"]: A: Tuple = 3_8_4 A: Union[str, Any] = 1_5_3_6 A: Tuple = 1_2 A: List[str] = 6 # load original model from torch hub A: List[str] = torch.hub.load('''facebookresearch/dino:main''' , __lowercase ) original_model.eval() # load state_dict of original model, remove and rename some keys A: Union[str, Any] = original_model.state_dict() if base_model: remove_classification_head_(__lowercase ) A: Optional[Any] = create_rename_keys(__lowercase , base_model=__lowercase ) for src, dest in rename_keys: rename_key(__lowercase , __lowercase , __lowercase ) read_in_q_k_v(__lowercase , __lowercase , __lowercase ) # load HuggingFace model if base_model: A: Dict = ViTModel(__lowercase , add_pooling_layer=__lowercase ).eval() else: A: Union[str, Any] = ViTForImageClassification(__lowercase ).eval() model.load_state_dict(__lowercase ) # Check outputs on an image, prepared by ViTImageProcessor A: Tuple = ViTImageProcessor() A: Any = image_processor(images=prepare_img() , return_tensors='''pt''' ) A: str = encoding['''pixel_values'''] A: str = model(__lowercase ) if base_model: A: Optional[Any] = original_model(__lowercase ) assert torch.allclose(__lowercase , outputs.last_hidden_state[:, 0, :] , atol=1E-1 ) else: A: Tuple = original_model(__lowercase ) assert logits.shape == outputs.logits.shape assert torch.allclose(__lowercase , outputs.logits , atol=1E-3 ) Path(__lowercase ).mkdir(exist_ok=__lowercase ) print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(__lowercase ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''dino_vitb16''', type=str, help='''Name of the model trained with DINO you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--base_model''', action='''store_true''', help='''Whether to only convert the base model (no projection head weights).''', ) parser.set_defaults(base_model=True) UpperCamelCase = parser.parse_args() convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)
319
'''simple docstring''' import heapq import sys import numpy as np UpperCamelCase = tuple[int, int] class lowerCAmelCase_ : '''simple docstring''' def __init__( self : List[Any] ) -> str: '''simple docstring''' A: Any = [] A: int = set() def _snake_case ( self : Optional[Any] ) -> int: '''simple docstring''' if not self.empty(): return self.elements[0][0] else: return float('''inf''' ) def _snake_case ( self : List[str] ) -> List[Any]: '''simple docstring''' return len(self.elements ) == 0 def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Any ) -> List[Any]: '''simple docstring''' if item not in self.set: heapq.heappush(self.elements , (priority, item) ) self.set.add(SCREAMING_SNAKE_CASE_ ) else: # update # print("update", item) A: Optional[int] = [] ((A) , (A)): str = heapq.heappop(self.elements ) while x != item: temp.append((pri, x) ) ((A) , (A)): int = heapq.heappop(self.elements ) temp.append((priority, item) ) for pro, xxx in temp: heapq.heappush(self.elements , (pro, xxx) ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : str ) -> Any: '''simple docstring''' if item in self.set: self.set.remove(SCREAMING_SNAKE_CASE_ ) A: str = [] ((A) , (A)): List[str] = heapq.heappop(self.elements ) while x != item: temp.append((pro, x) ) ((A) , (A)): Any = heapq.heappop(self.elements ) for prito, yyy in temp: heapq.heappush(self.elements , (prito, yyy) ) def _snake_case ( self : List[Any] ) -> Optional[int]: '''simple docstring''' return self.elements[0][1] def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' ((A) , (A)): Dict = heapq.heappop(self.elements ) self.set.remove(SCREAMING_SNAKE_CASE_ ) return (priority, item) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: # euclidean distance A: List[str] = np.array(__lowercase ) A: Optional[int] = np.array(__lowercase ) return np.linalg.norm(a - b ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> int: # integer division by time variable return consistent_heuristic(__lowercase , __lowercase ) // t def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[Any]: # manhattan distance return abs(p[0] - goal[0] ) + abs(p[1] - goal[1] ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase ) -> List[Any]: A: int = g_function[start] + Wa * heuristics[i](__lowercase , __lowercase ) return ans def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Optional[int]: A: Union[str, Any] = np.chararray((n, n) ) for i in range(__lowercase ): for j in range(__lowercase ): A: Union[str, Any] = '''*''' for i in range(__lowercase ): for j in range(__lowercase ): if (j, (n - 1) - i) in blocks: A: Optional[Any] = '''#''' A: Tuple = '''-''' A: List[str] = back_pointer[goal] while x != start: ((A) , (A)): Tuple = x # print(x) A: List[str] = '''-''' A: str = back_pointer[x] A: Dict = '''-''' for i in range(__lowercase ): for j in range(__lowercase ): if (i, j) == (0, n - 1): print(grid[i][j] , end=''' ''' ) print('''<-- End position''' , end=''' ''' ) else: print(grid[i][j] , end=''' ''' ) print() print('''^''' ) print('''Start position''' ) print() print('''# is an obstacle''' ) print('''- is the path taken by algorithm''' ) print('''PATH TAKEN BY THE ALGORITHM IS:-''' ) A: List[str] = back_pointer[goal] while x != start: print(__lowercase , end=''' ''' ) A: Optional[int] = back_pointer[x] print(__lowercase ) sys.exit() def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: if p[0] < 0 or p[0] > n - 1: return False if p[1] < 0 or p[1] > n - 1: return False return True def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) -> Union[str, Any]: for itera in range(__lowercase ): open_list[itera].remove_element(__lowercase ) # print("s", s) # print("j", j) ((A) , (A)): Tuple = s A: Optional[Any] = (x - 1, y) A: str = (x + 1, y) A: List[Any] = (x, y + 1) A: int = (x, y - 1) for neighbours in [left, right, up, down]: if neighbours not in blocks: if valid(__lowercase ) and neighbours not in visited: # print("neighbour", neighbours) visited.add(__lowercase ) A: int = -1 A: int = float('''inf''' ) if valid(__lowercase ) and g_function[neighbours] > g_function[s] + 1: A: List[str] = g_function[s] + 1 A: List[str] = s if neighbours not in close_list_anchor: open_list[0].put(__lowercase , key(__lowercase , 0 , __lowercase , __lowercase ) ) if neighbours not in close_list_inad: for var in range(1 , __lowercase ): if key(__lowercase , __lowercase , __lowercase , __lowercase ) <= Wa * key( __lowercase , 0 , __lowercase , __lowercase ): open_list[j].put( __lowercase , key(__lowercase , __lowercase , __lowercase , __lowercase ) ) def SCREAMING_SNAKE_CASE( ) -> Tuple: A: str = [] for x in range(1 , 5 ): for y in range(1 , 6 ): some_list.append((x, y) ) for x in range(1_5 , 2_0 ): some_list.append((x, 1_7) ) for x in range(1_0 , 1_9 ): for y in range(1 , 1_5 ): some_list.append((x, y) ) # L block for x in range(1 , 4 ): for y in range(1_2 , 1_9 ): some_list.append((x, y) ) for x in range(3 , 1_3 ): for y in range(1_6 , 1_9 ): some_list.append((x, y) ) return some_list UpperCamelCase = {0: consistent_heuristic, 1: heuristic_a, 2: heuristic_a} UpperCamelCase = [ (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1), (15, 1), (16, 1), (17, 1), (18, 1), (19, 1), ] UpperCamelCase = make_common_ground() UpperCamelCase = blocks_blk # hyper parameters UpperCamelCase = 1 UpperCamelCase = 1 UpperCamelCase = 20 UpperCamelCase = 3 # one consistent and two other inconsistent # start and end destination UpperCamelCase = (0, 0) UpperCamelCase = (n - 1, n - 1) UpperCamelCase = 1 def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: A: int = {start: 0, goal: float('''inf''' )} A: Union[str, Any] = {start: -1, goal: -1} A: List[Any] = [] A: Union[str, Any] = set() for i in range(__lowercase ): open_list.append(PriorityQueue() ) open_list[i].put(__lowercase , key(__lowercase , __lowercase , __lowercase , __lowercase ) ) A: list[int] = [] A: list[int] = [] while open_list[0].minkey() < float('''inf''' ): for i in range(1 , __lowercase ): # print(open_list[0].minkey(), open_list[i].minkey()) if open_list[i].minkey() <= Wa * open_list[0].minkey(): global t t += 1 if g_function[goal] <= open_list[i].minkey(): if g_function[goal] < float('''inf''' ): do_something(__lowercase , __lowercase , __lowercase ) else: A , A: Union[str, Any] = open_list[i].top_show() visited.add(__lowercase ) expand_state( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) close_list_inad.append(__lowercase ) else: if g_function[goal] <= open_list[0].minkey(): if g_function[goal] < float('''inf''' ): do_something(__lowercase , __lowercase , __lowercase ) else: A: Union[str, Any] = open_list[0].top_show() visited.add(__lowercase ) expand_state( __lowercase , 0 , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) close_list_anchor.append(__lowercase ) print('''No path found to goal''' ) print() for i in range(n - 1 , -1 , -1 ): for j in range(__lowercase ): if (j, i) in blocks: print('''#''' , end=''' ''' ) elif (j, i) in back_pointer: if (j, i) == (n - 1, n - 1): print('''*''' , end=''' ''' ) else: print('''-''' , end=''' ''' ) else: print('''*''' , end=''' ''' ) if (j, i) == (n - 1, n - 1): print('''<-- End position''' , end=''' ''' ) print() print('''^''' ) print('''Start position''' ) print() print('''# is an obstacle''' ) print('''- is the path taken by algorithm''' ) if __name__ == "__main__": multi_a_star(start, goal, n_heuristic)
319
1
'''simple docstring''' import os import random import sys from . import cryptomath_module as cryptomath from . import rabin_miller UpperCamelCase = 3 def SCREAMING_SNAKE_CASE( __lowercase ) -> int: print('''Generating primitive root of p''' ) while True: A: Optional[Any] = random.randrange(3 , __lowercase ) if pow(__lowercase , 2 , __lowercase ) == 1: continue if pow(__lowercase , __lowercase , __lowercase ) == 1: continue return g def SCREAMING_SNAKE_CASE( __lowercase ) -> tuple[tuple[int, int, int, int], tuple[int, int]]: print('''Generating prime p...''' ) A: Any = rabin_miller.generate_large_prime(__lowercase ) # select large prime number. A: Optional[int] = primitive_root(__lowercase ) # one primitive root on modulo p. A: Optional[int] = random.randrange(3 , __lowercase ) # private_key -> have to be greater than 2 for safety. A: List[str] = cryptomath.find_mod_inverse(pow(__lowercase , __lowercase , __lowercase ) , __lowercase ) A: Any = (key_size, e_a, e_a, p) A: int = (key_size, d) return public_key, private_key def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> None: if os.path.exists(F"""{name}_pubkey.txt""" ) or os.path.exists(F"""{name}_privkey.txt""" ): print('''\nWARNING:''' ) print( F"""\"{name}_pubkey.txt\" or \"{name}_privkey.txt\" already exists. \n""" '''Use a different name or delete these files and re-run this program.''' ) sys.exit() A , A: Union[str, Any] = generate_key(__lowercase ) print(F"""\nWriting public key to file {name}_pubkey.txt...""" ) with open(F"""{name}_pubkey.txt""" , '''w''' ) as fo: fo.write(F"""{public_key[0]},{public_key[1]},{public_key[2]},{public_key[3]}""" ) print(F"""Writing private key to file {name}_privkey.txt...""" ) with open(F"""{name}_privkey.txt""" , '''w''' ) as fo: fo.write(F"""{private_key[0]},{private_key[1]}""" ) def SCREAMING_SNAKE_CASE( ) -> None: print('''Making key files...''' ) make_key_files('''elgamal''' , 2_0_4_8 ) print('''Key files generation successful''' ) if __name__ == "__main__": main()
319
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase = 1 , __lowercase = 1_0_0_0 ) -> int: A: Any = 1 A: Optional[Any] = 0 for divide_by_number in range(__lowercase , digit + 1 ): A: list[int] = [] A: List[Any] = numerator for _ in range(1 , digit + 1 ): if now_divide in has_been_divided: if longest_list_length < len(__lowercase ): A: Any = len(__lowercase ) A: Dict = divide_by_number else: has_been_divided.append(__lowercase ) A: str = now_divide * 1_0 % divide_by_number return the_digit # Tests if __name__ == "__main__": import doctest doctest.testmod()
319
1
'''simple docstring''' import inspect import tempfile from collections import OrderedDict, UserDict from collections.abc import MutableMapping from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from typing import Any, ContextManager, List, Tuple import numpy as np from .import_utils import is_flax_available, is_tf_available, is_torch_available, is_torch_fx_proxy if is_flax_available(): import jax.numpy as jnp class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __get__( self : List[Any] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : str=None ) -> List[str]: '''simple docstring''' if obj is None: return self if self.fget is None: raise AttributeError('''unreadable attribute''' ) A: int = '''__cached_''' + self.fget.__name__ A: Union[str, Any] = getattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if cached is None: A: Union[str, Any] = self.fget(SCREAMING_SNAKE_CASE_ ) setattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return cached def SCREAMING_SNAKE_CASE( __lowercase ) -> str: A: int = val.lower() if val in {"y", "yes", "t", "true", "on", "1"}: return 1 if val in {"n", "no", "f", "false", "off", "0"}: return 0 raise ValueError(F"""invalid truth value {val!r}""" ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Union[str, Any]: if is_torch_fx_proxy(__lowercase ): return True if is_torch_available(): import torch if isinstance(__lowercase , torch.Tensor ): return True if is_tf_available(): import tensorflow as tf if isinstance(__lowercase , tf.Tensor ): return True if is_flax_available(): import jax.numpy as jnp from jax.core import Tracer if isinstance(__lowercase , (jnp.ndarray, Tracer) ): return True return isinstance(__lowercase , np.ndarray ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: return isinstance(__lowercase , np.ndarray ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: return _is_numpy(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> List[Any]: import torch return isinstance(__lowercase , torch.Tensor ) def SCREAMING_SNAKE_CASE( __lowercase ) -> str: return False if not is_torch_available() else _is_torch(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> int: import torch return isinstance(__lowercase , torch.device ) def SCREAMING_SNAKE_CASE( __lowercase ) -> List[Any]: return False if not is_torch_available() else _is_torch_device(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Any: import torch if isinstance(__lowercase , __lowercase ): if hasattr(__lowercase , __lowercase ): A: Union[str, Any] = getattr(__lowercase , __lowercase ) else: return False return isinstance(__lowercase , torch.dtype ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Union[str, Any]: return False if not is_torch_available() else _is_torch_dtype(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> int: import tensorflow as tf return isinstance(__lowercase , tf.Tensor ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: return False if not is_tf_available() else _is_tensorflow(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: import tensorflow as tf # the `is_symbolic_tensor` predicate is only available starting with TF 2.14 if hasattr(__lowercase , '''is_symbolic_tensor''' ): return tf.is_symbolic_tensor(__lowercase ) return type(__lowercase ) == tf.Tensor def SCREAMING_SNAKE_CASE( __lowercase ) -> Union[str, Any]: return False if not is_tf_available() else _is_tf_symbolic_tensor(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: import jax.numpy as jnp # noqa: F811 return isinstance(__lowercase , jnp.ndarray ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: return False if not is_flax_available() else _is_jax(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: if isinstance(__lowercase , (dict, UserDict) ): return {k: to_py_obj(__lowercase ) for k, v in obj.items()} elif isinstance(__lowercase , (list, tuple) ): return [to_py_obj(__lowercase ) for o in obj] elif is_tf_tensor(__lowercase ): return obj.numpy().tolist() elif is_torch_tensor(__lowercase ): return obj.detach().cpu().tolist() elif is_jax_tensor(__lowercase ): return np.asarray(__lowercase ).tolist() elif isinstance(__lowercase , (np.ndarray, np.number) ): # tolist also works on 0d np arrays return obj.tolist() else: return obj def SCREAMING_SNAKE_CASE( __lowercase ) -> List[str]: if isinstance(__lowercase , (dict, UserDict) ): return {k: to_numpy(__lowercase ) for k, v in obj.items()} elif isinstance(__lowercase , (list, tuple) ): return np.array(__lowercase ) elif is_tf_tensor(__lowercase ): return obj.numpy() elif is_torch_tensor(__lowercase ): return obj.detach().cpu().numpy() elif is_jax_tensor(__lowercase ): return np.asarray(__lowercase ) else: return obj class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def _snake_case ( self : Union[str, Any] ) -> str: '''simple docstring''' A: List[Any] = fields(self ) # Safety and consistency checks if not len(SCREAMING_SNAKE_CASE_ ): raise ValueError(f"""{self.__class__.__name__} has no fields.""" ) if not all(field.default is None for field in class_fields[1:] ): raise ValueError(f"""{self.__class__.__name__} should not have more than one required field.""" ) A: Any = getattr(self , class_fields[0].name ) A: Union[str, Any] = all(getattr(self , field.name ) is None for field in class_fields[1:] ) if other_fields_are_none and not is_tensor(SCREAMING_SNAKE_CASE_ ): if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): A: Optional[int] = first_field.items() A: Tuple = True else: try: A: Any = iter(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = True except TypeError: A: Tuple = False # if we provided an iterator as first field and the iterator is a (key, value) iterator # set the associated fields if first_field_iterator: for idx, element in enumerate(SCREAMING_SNAKE_CASE_ ): if ( not isinstance(SCREAMING_SNAKE_CASE_ , (list, tuple) ) or not len(SCREAMING_SNAKE_CASE_ ) == 2 or not isinstance(element[0] , SCREAMING_SNAKE_CASE_ ) ): if idx == 0: # If we do not have an iterator of key/values, set it as attribute A: Tuple = first_field else: # If we have a mixed iterator, raise an error raise ValueError( f"""Cannot set key/value for {element}. It needs to be a tuple (key, value).""" ) break setattr(self , element[0] , element[1] ) if element[1] is not None: A: int = element[1] elif first_field is not None: A: str = first_field else: for field in class_fields: A: Optional[Any] = getattr(self , field.name ) if v is not None: A: List[str] = v def __delitem__( self : Any , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Union[str, Any]: '''simple docstring''' raise Exception(f"""You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.""" ) def _snake_case ( self : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : Any , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Any: '''simple docstring''' raise Exception(f"""You cannot use ``setdefault`` on a {self.__class__.__name__} instance.""" ) def _snake_case ( self : int , *SCREAMING_SNAKE_CASE_ : Tuple , **SCREAMING_SNAKE_CASE_ : str ) -> int: '''simple docstring''' raise Exception(f"""You cannot use ``pop`` on a {self.__class__.__name__} instance.""" ) def _snake_case ( self : Tuple , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : Dict ) -> Any: '''simple docstring''' raise Exception(f"""You cannot use ``update`` on a {self.__class__.__name__} instance.""" ) def __getitem__( self : List[str] , SCREAMING_SNAKE_CASE_ : Dict ) -> Optional[Any]: '''simple docstring''' if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): A: Tuple = dict(self.items() ) return inner_dict[k] else: return self.to_tuple()[k] def __setattr__( self : Dict , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Tuple ) -> Optional[Any]: '''simple docstring''' if name in self.keys() and value is not None: # Don't call self.__setitem__ to avoid recursion errors super().__setitem__(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) super().__setattr__(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def __setitem__( self : Tuple , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : List[Any] ) -> Union[str, Any]: '''simple docstring''' super().__setitem__(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Don't call self.__setattr__ to avoid recursion errors super().__setattr__(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[Any] ) -> Tuple[Any]: '''simple docstring''' return tuple(self[k] for k in self.keys() ) class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' @classmethod def _snake_case ( cls : Any , SCREAMING_SNAKE_CASE_ : List[Any] ) -> int: '''simple docstring''' raise ValueError( f"""{value} is not a valid {cls.__name__}, please select one of {list(cls._valueamember_map_.keys() )}""" ) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = """longest""" UpperCamelCase_ : int = """max_length""" UpperCamelCase_ : List[str] = """do_not_pad""" class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : List[Any] = """pt""" UpperCamelCase_ : Optional[Any] = """tf""" UpperCamelCase_ : List[Any] = """np""" UpperCamelCase_ : List[Any] = """jax""" class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Any , SCREAMING_SNAKE_CASE_ : List[ContextManager] ) -> Optional[Any]: '''simple docstring''' A: List[str] = context_managers A: int = ExitStack() def __enter__( self : Optional[Any] ) -> Any: '''simple docstring''' for context_manager in self.context_managers: self.stack.enter_context(SCREAMING_SNAKE_CASE_ ) def __exit__( self : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : int ) -> int: '''simple docstring''' self.stack.__exit__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: A: str = infer_framework(__lowercase ) if framework == "tf": A: Optional[int] = inspect.signature(model_class.call ) # TensorFlow models elif framework == "pt": A: Optional[int] = inspect.signature(model_class.forward ) # PyTorch models else: A: int = inspect.signature(model_class.__call__ ) # Flax models for p in signature.parameters: if p == "return_loss" and signature.parameters[p].default is True: return True return False def SCREAMING_SNAKE_CASE( __lowercase ) -> str: A: Dict = model_class.__name__ A: Optional[int] = infer_framework(__lowercase ) if framework == "tf": A: Any = inspect.signature(model_class.call ) # TensorFlow models elif framework == "pt": A: str = inspect.signature(model_class.forward ) # PyTorch models else: A: List[Any] = inspect.signature(model_class.__call__ ) # Flax models if "QuestionAnswering" in model_name: return [p for p in signature.parameters if "label" in p or p in ("start_positions", "end_positions")] else: return [p for p in signature.parameters if "label" in p] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = "" , __lowercase = "." ) -> List[Any]: def _flatten_dict(__lowercase , __lowercase="" , __lowercase="." ): for k, v in d.items(): A: Tuple = str(__lowercase ) + delimiter + str(__lowercase ) if parent_key else k if v and isinstance(__lowercase , __lowercase ): yield from flatten_dict(__lowercase , __lowercase , delimiter=__lowercase ).items() else: yield key, v return dict(_flatten_dict(__lowercase , __lowercase , __lowercase ) ) @contextmanager def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = False ) -> str: if use_temp_dir: with tempfile.TemporaryDirectory() as tmp_dir: yield tmp_dir else: yield working_dir def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=None ) -> str: if is_numpy_array(__lowercase ): return np.transpose(__lowercase , axes=__lowercase ) elif is_torch_tensor(__lowercase ): return array.T if axes is None else array.permute(*__lowercase ) elif is_tf_tensor(__lowercase ): import tensorflow as tf return tf.transpose(__lowercase , perm=__lowercase ) elif is_jax_tensor(__lowercase ): return jnp.transpose(__lowercase , axes=__lowercase ) else: raise ValueError(F"""Type not supported for transpose: {type(__lowercase )}.""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: if is_numpy_array(__lowercase ): return np.reshape(__lowercase , __lowercase ) elif is_torch_tensor(__lowercase ): return array.reshape(*__lowercase ) elif is_tf_tensor(__lowercase ): import tensorflow as tf return tf.reshape(__lowercase , __lowercase ) elif is_jax_tensor(__lowercase ): return jnp.reshape(__lowercase , __lowercase ) else: raise ValueError(F"""Type not supported for reshape: {type(__lowercase )}.""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=None ) -> Optional[int]: if is_numpy_array(__lowercase ): return np.squeeze(__lowercase , axis=__lowercase ) elif is_torch_tensor(__lowercase ): return array.squeeze() if axis is None else array.squeeze(dim=__lowercase ) elif is_tf_tensor(__lowercase ): import tensorflow as tf return tf.squeeze(__lowercase , axis=__lowercase ) elif is_jax_tensor(__lowercase ): return jnp.squeeze(__lowercase , axis=__lowercase ) else: raise ValueError(F"""Type not supported for squeeze: {type(__lowercase )}.""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[Any]: if is_numpy_array(__lowercase ): return np.expand_dims(__lowercase , __lowercase ) elif is_torch_tensor(__lowercase ): return array.unsqueeze(dim=__lowercase ) elif is_tf_tensor(__lowercase ): import tensorflow as tf return tf.expand_dims(__lowercase , axis=__lowercase ) elif is_jax_tensor(__lowercase ): return jnp.expand_dims(__lowercase , axis=__lowercase ) else: raise ValueError(F"""Type not supported for expand_dims: {type(__lowercase )}.""" ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: if is_numpy_array(__lowercase ): return np.size(__lowercase ) elif is_torch_tensor(__lowercase ): return array.numel() elif is_tf_tensor(__lowercase ): import tensorflow as tf return tf.size(__lowercase ) elif is_jax_tensor(__lowercase ): return array.size else: raise ValueError(F"""Type not supported for expand_dims: {type(__lowercase )}.""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[Any]: for key, value in auto_map.items(): if isinstance(__lowercase , (tuple, list) ): A: int = [F"""{repo_id}--{v}""" if (v is not None and '''--''' not in v) else v for v in value] elif value is not None and "--" not in value: A: int = F"""{repo_id}--{value}""" return auto_map def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: for base_class in inspect.getmro(__lowercase ): A: List[str] = base_class.__module__ A: List[str] = base_class.__name__ if module.startswith('''tensorflow''' ) or module.startswith('''keras''' ) or name == "TFPreTrainedModel": return "tf" elif module.startswith('''torch''' ) or name == "PreTrainedModel": return "pt" elif module.startswith('''flax''' ) or module.startswith('''jax''' ) or name == "FlaxPreTrainedModel": return "flax" else: raise TypeError(F"""Could not infer framework from class {model_class}.""" )
319
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) UpperCamelCase = { '''configuration_vision_encoder_decoder''': ['''VisionEncoderDecoderConfig''', '''VisionEncoderDecoderOnnxConfig'''] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''VisionEncoderDecoderModel'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''TFVisionEncoderDecoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''FlaxVisionEncoderDecoderModel'''] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
1
'''simple docstring''' import os from collections.abc import Iterator def SCREAMING_SNAKE_CASE( __lowercase = "." ) -> Iterator[str]: for dir_path, dir_names, filenames in os.walk(__lowercase ): A: Tuple = [d for d in dir_names if d != '''scripts''' and d[0] not in '''._'''] for filename in filenames: if filename == "__init__.py": continue if os.path.splitext(__lowercase )[1] in (".py", ".ipynb"): yield os.path.join(__lowercase , __lowercase ).lstrip('''./''' ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: return F"""{i * ' '}*""" if i else "\n##" def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str: A: str = old_path.split(os.sep ) for i, new_part in enumerate(new_path.split(os.sep ) ): if (i + 1 > len(__lowercase ) or old_parts[i] != new_part) and new_part: print(F"""{md_prefix(__lowercase )} {new_part.replace('_' , ' ' ).title()}""" ) return new_path def SCREAMING_SNAKE_CASE( __lowercase = "." ) -> None: A: str = '''''' for filepath in sorted(good_file_paths(__lowercase ) ): A , A: Union[str, Any] = os.path.split(__lowercase ) if filepath != old_path: A: Any = print_path(__lowercase , __lowercase ) A: str = (filepath.count(os.sep ) + 1) if filepath else 0 A: Optional[int] = F"""{filepath}/{filename}""".replace(''' ''' , '''%20''' ) A: Union[str, Any] = os.path.splitext(filename.replace('''_''' , ''' ''' ).title() )[0] print(F"""{md_prefix(__lowercase )} [{filename}]({url})""" ) if __name__ == "__main__": print_directory_md('''.''')
319
'''simple docstring''' import fire from utils import calculate_rouge, save_json def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=None , **__lowercase ) -> Any: A: Any = [x.strip() for x in open(__lowercase ).readlines()] A: Dict = [x.strip() for x in open(__lowercase ).readlines()][: len(__lowercase )] A: Union[str, Any] = calculate_rouge(__lowercase , __lowercase , **__lowercase ) if save_path is not None: save_json(__lowercase , __lowercase , indent=__lowercase ) return metrics # these print nicely if __name__ == "__main__": fire.Fire(calculate_rouge_path)
319
1
'''simple docstring''' import time import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch, torch_device from ..test_modeling_common import ids_tensor if is_torch_available(): import torch from transformers.generation import ( MaxLengthCriteria, MaxNewTokensCriteria, MaxTimeCriteria, StoppingCriteriaList, validate_stopping_criteria, ) @require_torch class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' A: Any = 3 A: Any = 2_50 A: Dict = ids_tensor((batch_size, length) , SCREAMING_SNAKE_CASE_ ) A: str = torch.ones((batch_size, length) , device=SCREAMING_SNAKE_CASE_ , dtype=torch.float ) / length return input_ids, scores def _snake_case ( self : Tuple ) -> Any: '''simple docstring''' A , A: Union[str, Any] = self._get_tensors(5 ) A: List[Any] = StoppingCriteriaList( [ MaxLengthCriteria(max_length=10 ), MaxTimeCriteria(max_time=0.1 ), ] ) self.assertFalse(criteria(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) A , A: Tuple = self._get_tensors(9 ) self.assertFalse(criteria(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) A , A: Union[str, Any] = self._get_tensors(10 ) self.assertTrue(criteria(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) def _snake_case ( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' A: List[str] = MaxLengthCriteria(max_length=10 ) A , A: Dict = self._get_tensors(5 ) self.assertFalse(criteria(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) A , A: int = self._get_tensors(9 ) self.assertFalse(criteria(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) A , A: Any = self._get_tensors(10 ) self.assertTrue(criteria(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) def _snake_case ( self : List[str] ) -> Optional[Any]: '''simple docstring''' A: Any = MaxNewTokensCriteria(start_length=5 , max_new_tokens=5 ) A , A: Union[str, Any] = self._get_tensors(5 ) self.assertFalse(criteria(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) A , A: List[str] = self._get_tensors(9 ) self.assertFalse(criteria(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) A , A: int = self._get_tensors(10 ) self.assertTrue(criteria(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) A: List[str] = StoppingCriteriaList([criteria] ) self.assertEqual(criteria_list.max_length , 10 ) def _snake_case ( self : int ) -> Optional[int]: '''simple docstring''' A , A: Union[str, Any] = self._get_tensors(5 ) A: Optional[int] = MaxTimeCriteria(max_time=0.1 ) self.assertFalse(criteria(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) A: Union[str, Any] = MaxTimeCriteria(max_time=0.1 , initial_timestamp=time.time() - 0.2 ) self.assertTrue(criteria(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) def _snake_case ( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10 )] ) , 10 ) with self.assertWarns(SCREAMING_SNAKE_CASE_ ): validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10 )] ) , 11 ) A: Optional[Any] = validate_stopping_criteria(StoppingCriteriaList() , 11 ) self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) , 1 )
319
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = 0 ) -> list: A: Dict = length or len(__lowercase ) A: Dict = False for i in range(length - 1 ): if list_data[i] > list_data[i + 1]: A , A: Tuple = list_data[i + 1], list_data[i] A: Union[str, Any] = True return list_data if not swapped else bubble_sort(__lowercase , length - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
319
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase = {'''configuration_vit_msn''': ['''VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ViTMSNConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ViTMSNModel''', '''ViTMSNForImageClassification''', '''ViTMSNPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
'''simple docstring''' import argparse from collections import OrderedDict from pathlib import Path import torch from transformers import ( VisualBertConfig, VisualBertForMultipleChoice, VisualBertForPreTraining, VisualBertForQuestionAnswering, VisualBertForVisualReasoning, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = [ ('''bert.bert''', '''visual_bert'''), ('''bert.cls''', '''cls'''), ('''bert.classifier''', '''cls'''), ('''token_type_embeddings_visual''', '''visual_token_type_embeddings'''), ('''position_embeddings_visual''', '''visual_position_embeddings'''), ('''projection''', '''visual_projection'''), ] UpperCamelCase = [ '''nlvr2_coco_pre_trained.th''', '''nlvr2_fine_tuned.th''', '''nlvr2_pre_trained.th''', '''vcr_coco_pre_train.th''', '''vcr_fine_tune.th''', '''vcr_pre_train.th''', '''vqa_coco_pre_trained.th''', '''vqa_fine_tuned.th''', '''vqa_pre_trained.th''', ] def SCREAMING_SNAKE_CASE( __lowercase ) -> List[Any]: A: List[Any] = torch.load(__lowercase , map_location='''cpu''' ) return sd def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=rename_keys_prefix ) -> Optional[Any]: A: Tuple = OrderedDict() A: Dict = torch.arange(config.max_position_embeddings ).expand((1, -1) ) # detector_d = OrderedDict() for key in d: if "detector" in key: # detector_d[key.replace('detector.','')] = d[key] continue A: int = key for name_pair in rename_keys_prefix: A: Optional[int] = new_key.replace(name_pair[0] , name_pair[1] ) A: Union[str, Any] = d[key] if key == "bert.cls.predictions.decoder.weight": # Old bert code didn't have `decoder.bias`, but was added separately A: int = new_d['''cls.predictions.bias'''] return new_d @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Dict: assert ( checkpoint_path.split('''/''' )[-1] in ACCEPTABLE_CHECKPOINTS ), F"""The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}.""" # Get Config if "pre" in checkpoint_path: A: Optional[Any] = '''pretraining''' if "vcr" in checkpoint_path: A: Optional[int] = {'''visual_embedding_dim''': 5_1_2} elif "vqa_advanced" in checkpoint_path: A: Optional[Any] = {'''visual_embedding_dim''': 2_0_4_8} elif "vqa" in checkpoint_path: A: Dict = {'''visual_embedding_dim''': 2_0_4_8} elif "nlvr" in checkpoint_path: A: Tuple = {'''visual_embedding_dim''': 1_0_2_4} else: raise NotImplementedError(F"""No implementation found for `{checkpoint_path}`.""" ) else: if "vcr" in checkpoint_path: A: Dict = {'''visual_embedding_dim''': 5_1_2} A: List[str] = '''multichoice''' elif "vqa_advanced" in checkpoint_path: A: List[str] = {'''visual_embedding_dim''': 2_0_4_8} A: Optional[int] = '''vqa_advanced''' elif "vqa" in checkpoint_path: A: Dict = {'''visual_embedding_dim''': 2_0_4_8, '''num_labels''': 3_1_2_9} A: Union[str, Any] = '''vqa''' elif "nlvr" in checkpoint_path: A: Optional[int] = { '''visual_embedding_dim''': 1_0_2_4, '''num_labels''': 2, } A: str = '''nlvr''' A: Union[str, Any] = VisualBertConfig(**__lowercase ) # Load State Dict A: Union[str, Any] = load_state_dict(__lowercase ) A: str = get_new_dict(__lowercase , __lowercase ) if model_type == "pretraining": A: Optional[Any] = VisualBertForPreTraining(__lowercase ) elif model_type == "vqa": A: Optional[Any] = VisualBertForQuestionAnswering(__lowercase ) elif model_type == "nlvr": A: Union[str, Any] = VisualBertForVisualReasoning(__lowercase ) elif model_type == "multichoice": A: Any = VisualBertForMultipleChoice(__lowercase ) model.load_state_dict(__lowercase ) # Save Checkpoints Path(__lowercase ).mkdir(exist_ok=__lowercase ) model.save_pretrained(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument('''orig_checkpoint_path''', type=str, help='''A path to .th on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', type=str, help='''Path to the output PyTorch model.''') UpperCamelCase = parser.parse_args() convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)
319
1
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> int: return int((input_a, input_a).count(1 ) != 0 ) def SCREAMING_SNAKE_CASE( ) -> None: assert or_gate(0 , 0 ) == 0 assert or_gate(0 , 1 ) == 1 assert or_gate(1 , 0 ) == 1 assert or_gate(1 , 1 ) == 1 if __name__ == "__main__": print(or_gate(0, 1)) print(or_gate(1, 0)) print(or_gate(0, 0)) print(or_gate(1, 1))
319
'''simple docstring''' from itertools import permutations def SCREAMING_SNAKE_CASE( __lowercase ) -> bool: if num[3] % 2 != 0: return False if (num[2] + num[3] + num[4]) % 3 != 0: return False if num[5] % 5 != 0: return False A: int = [7, 1_1, 1_3, 1_7] for i, test in enumerate(__lowercase ): if (num[i + 4] * 1_0_0 + num[i + 5] * 1_0 + num[i + 6]) % test != 0: return False return True def SCREAMING_SNAKE_CASE( __lowercase = 1_0 ) -> int: return sum( int(''''''.join(map(__lowercase , __lowercase ) ) ) for num in permutations(range(__lowercase ) ) if is_substring_divisible(__lowercase ) ) if __name__ == "__main__": print(f'{solution() = }')
319
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCamelCase = { '''configuration_blenderbot''': [ '''BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BlenderbotConfig''', '''BlenderbotOnnxConfig''', ], '''tokenization_blenderbot''': ['''BlenderbotTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''BlenderbotTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BlenderbotForCausalLM''', '''BlenderbotForConditionalGeneration''', '''BlenderbotModel''', '''BlenderbotPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''TFBlenderbotForConditionalGeneration''', '''TFBlenderbotModel''', '''TFBlenderbotPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''FlaxBlenderbotForConditionalGeneration''', '''FlaxBlenderbotModel''', '''FlaxBlenderbotPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
'''simple docstring''' import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt'''} UpperCamelCase = { '''vocab_file''': { '''allenai/longformer-base-4096''': '''https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json''', '''allenai/longformer-large-4096''': ( '''https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json''' ), '''allenai/longformer-large-4096-finetuned-triviaqa''': ( '''https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json''' ), '''allenai/longformer-base-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json''' ), '''allenai/longformer-large-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json''' ), }, '''merges_file''': { '''allenai/longformer-base-4096''': '''https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt''', '''allenai/longformer-large-4096''': ( '''https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt''' ), '''allenai/longformer-large-4096-finetuned-triviaqa''': ( '''https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt''' ), '''allenai/longformer-base-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt''' ), '''allenai/longformer-large-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt''' ), }, } UpperCamelCase = { '''allenai/longformer-base-4096''': 4096, '''allenai/longformer-large-4096''': 4096, '''allenai/longformer-large-4096-finetuned-triviaqa''': 4096, '''allenai/longformer-base-4096-extra.pos.embd.only''': 4096, '''allenai/longformer-large-4096-extra.pos.embd.only''': 4096, } @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def SCREAMING_SNAKE_CASE( ) -> Dict: A: Dict = ( list(range(ord('''!''' ) , ord('''~''' ) + 1 ) ) + list(range(ord('''¡''' ) , ord('''¬''' ) + 1 ) ) + list(range(ord('''®''' ) , ord('''ÿ''' ) + 1 ) ) ) A: Union[str, Any] = bs[:] A: List[str] = 0 for b in range(2**8 ): if b not in bs: bs.append(__lowercase ) cs.append(2**8 + n ) n += 1 A: List[Any] = [chr(__lowercase ) for n in cs] return dict(zip(__lowercase , __lowercase ) ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: A: Optional[Any] = set() A: Tuple = word[0] for char in word[1:]: pairs.add((prev_char, char) ) A: List[Any] = char return pairs class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : int = VOCAB_FILES_NAMES UpperCamelCase_ : int = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : int = ["""input_ids""", """attention_mask"""] def __init__( self : int , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : str="replace" , SCREAMING_SNAKE_CASE_ : str="<s>" , SCREAMING_SNAKE_CASE_ : Any="</s>" , SCREAMING_SNAKE_CASE_ : int="</s>" , SCREAMING_SNAKE_CASE_ : List[Any]="<s>" , SCREAMING_SNAKE_CASE_ : str="<unk>" , SCREAMING_SNAKE_CASE_ : Dict="<pad>" , SCREAMING_SNAKE_CASE_ : Dict="<mask>" , SCREAMING_SNAKE_CASE_ : Union[str, Any]=False , **SCREAMING_SNAKE_CASE_ : Tuple , ) -> List[str]: '''simple docstring''' A: int = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else bos_token A: Dict = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else eos_token A: int = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else sep_token A: Dict = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else cls_token A: Any = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else unk_token A: str = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it A: Dict = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else mask_token super().__init__( errors=SCREAMING_SNAKE_CASE_ , bos_token=SCREAMING_SNAKE_CASE_ , eos_token=SCREAMING_SNAKE_CASE_ , unk_token=SCREAMING_SNAKE_CASE_ , sep_token=SCREAMING_SNAKE_CASE_ , cls_token=SCREAMING_SNAKE_CASE_ , pad_token=SCREAMING_SNAKE_CASE_ , mask_token=SCREAMING_SNAKE_CASE_ , add_prefix_space=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) with open(SCREAMING_SNAKE_CASE_ , encoding='''utf-8''' ) as vocab_handle: A: str = json.load(SCREAMING_SNAKE_CASE_ ) A: str = {v: k for k, v in self.encoder.items()} A: Union[str, Any] = errors # how to handle errors in decoding A: Optional[int] = bytes_to_unicode() A: Union[str, Any] = {v: k for k, v in self.byte_encoder.items()} with open(SCREAMING_SNAKE_CASE_ , encoding='''utf-8''' ) as merges_handle: A: int = merges_handle.read().split('''\n''' )[1:-1] A: str = [tuple(merge.split() ) for merge in bpe_merges] A: Any = dict(zip(SCREAMING_SNAKE_CASE_ , range(len(SCREAMING_SNAKE_CASE_ ) ) ) ) A: Union[str, Any] = {} A: Tuple = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions A: Dict = re.compile(R'''\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+''' ) @property def _snake_case ( self : int ) -> List[Any]: '''simple docstring''' return len(self.encoder ) def _snake_case ( self : Optional[Any] ) -> int: '''simple docstring''' return dict(self.encoder , **self.added_tokens_encoder ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[Any]: '''simple docstring''' if token in self.cache: return self.cache[token] A: str = tuple(SCREAMING_SNAKE_CASE_ ) A: str = get_pairs(SCREAMING_SNAKE_CASE_ ) if not pairs: return token while True: A: Dict = min(SCREAMING_SNAKE_CASE_ , key=lambda SCREAMING_SNAKE_CASE_ : self.bpe_ranks.get(SCREAMING_SNAKE_CASE_ , float('''inf''' ) ) ) if bigram not in self.bpe_ranks: break A , A: Optional[Any] = bigram A: Tuple = [] A: List[Any] = 0 while i < len(SCREAMING_SNAKE_CASE_ ): try: A: Union[str, Any] = word.index(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) A: int = j if word[i] == first and i < len(SCREAMING_SNAKE_CASE_ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 A: Optional[Any] = tuple(SCREAMING_SNAKE_CASE_ ) A: Any = new_word if len(SCREAMING_SNAKE_CASE_ ) == 1: break else: A: Union[str, Any] = get_pairs(SCREAMING_SNAKE_CASE_ ) A: str = ''' '''.join(SCREAMING_SNAKE_CASE_ ) A: str = word return word def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Optional[int]: '''simple docstring''' A: Dict = [] for token in re.findall(self.pat , SCREAMING_SNAKE_CASE_ ): A: Tuple = ''''''.join( self.byte_encoder[b] for b in token.encode('''utf-8''' ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(SCREAMING_SNAKE_CASE_ ).split(''' ''' ) ) return bpe_tokens def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Optional[Any]: '''simple docstring''' return self.encoder.get(SCREAMING_SNAKE_CASE_ , self.encoder.get(self.unk_token ) ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> str: '''simple docstring''' return self.decoder.get(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Tuple: '''simple docstring''' A: Optional[int] = ''''''.join(SCREAMING_SNAKE_CASE_ ) A: Tuple = bytearray([self.byte_decoder[c] for c in text] ).decode('''utf-8''' , errors=self.errors ) return text def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(SCREAMING_SNAKE_CASE_ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return A: Union[str, Any] = os.path.join( SCREAMING_SNAKE_CASE_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) A: int = os.path.join( SCREAMING_SNAKE_CASE_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file'''] ) with open(SCREAMING_SNAKE_CASE_ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=SCREAMING_SNAKE_CASE_ , ensure_ascii=SCREAMING_SNAKE_CASE_ ) + '''\n''' ) A: Any = 0 with open(SCREAMING_SNAKE_CASE_ , '''w''' , encoding='''utf-8''' ) as writer: writer.write('''#version: 0.2\n''' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda SCREAMING_SNAKE_CASE_ : kv[1] ): if index != token_index: logger.warning( f"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.""" ''' Please check that the tokenizer is not corrupted!''' ) A: Union[str, Any] = token_index writer.write(''' '''.join(SCREAMING_SNAKE_CASE_ ) + '''\n''' ) index += 1 return vocab_file, merge_file def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] A: int = [self.cls_token_id] A: str = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None , SCREAMING_SNAKE_CASE_ : bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=SCREAMING_SNAKE_CASE_ , token_ids_a=SCREAMING_SNAKE_CASE_ , already_has_special_tokens=SCREAMING_SNAKE_CASE_ ) if token_ids_a is None: return [1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1] return [1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1, 1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1] def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' A: Dict = [self.sep_token_id] A: Optional[Any] = [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 + sep + token_ids_a + sep ) * [0] def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Dict=False , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> int: '''simple docstring''' A: Tuple = kwargs.pop('''add_prefix_space''' , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(SCREAMING_SNAKE_CASE_ ) > 0 and not text[0].isspace()): A: List[Any] = ''' ''' + text return (text, kwargs)
319
1
'''simple docstring''' from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Union[str, Any] = """ClapFeatureExtractor""" UpperCamelCase_ : Any = ("""RobertaTokenizer""", """RobertaTokenizerFast""") def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : str ) -> Tuple: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def __call__( self : str , SCREAMING_SNAKE_CASE_ : List[Any]=None , SCREAMING_SNAKE_CASE_ : Tuple=None , SCREAMING_SNAKE_CASE_ : Union[str, Any]=None , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> List[str]: '''simple docstring''' A: Union[str, Any] = kwargs.pop('''sampling_rate''' , SCREAMING_SNAKE_CASE_ ) if text is None and audios is None: raise ValueError('''You have to specify either text or audios. Both cannot be none.''' ) if text is not None: A: List[str] = self.tokenizer(SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if audios is not None: A: List[str] = self.feature_extractor( SCREAMING_SNAKE_CASE_ , sampling_rate=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if text is not None and audios is not None: A: Tuple = audio_features.input_features return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**SCREAMING_SNAKE_CASE_ ) , tensor_type=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[Any] , *SCREAMING_SNAKE_CASE_ : Tuple , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Tuple: '''simple docstring''' return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[int] , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Optional[int]: '''simple docstring''' return self.tokenizer.decode(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) @property def _snake_case ( self : Any ) -> Dict: '''simple docstring''' A: Optional[Any] = self.tokenizer.model_input_names A: List[str] = self.feature_extractor.model_input_names return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names ) )
319
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> int: if not isinstance(__lowercase , __lowercase ): raise TypeError('''only integers accepted as input''' ) else: A: str = str(abs(__lowercase ) ) A: int = [list(__lowercase ) for char in range(len(__lowercase ) )] for index in range(len(__lowercase ) ): num_transpositions[index].pop(__lowercase ) return max( int(''''''.join(list(__lowercase ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__('''doctest''').testmod()
319
1
'''simple docstring''' import json import os import shutil import tempfile from unittest import TestCase from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available if is_torch_available() and is_datasets_available() and is_faiss_available(): from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.tokenization_rag import RagTokenizer @require_faiss @require_torch class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def _snake_case ( self : Any ) -> List[str]: '''simple docstring''' A: str = tempfile.mkdtemp() A: int = 8 # DPR tok A: Tuple = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] A: Any = os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) os.makedirs(SCREAMING_SNAKE_CASE_ , exist_ok=SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = os.path.join(SCREAMING_SNAKE_CASE_ , DPR_VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) # BART tok A: List[str] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] A: Any = dict(zip(SCREAMING_SNAKE_CASE_ , range(len(SCREAMING_SNAKE_CASE_ ) ) ) ) A: Any = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] A: Optional[int] = {'''unk_token''': '''<unk>'''} A: Dict = os.path.join(self.tmpdirname , '''bart_tokenizer''' ) os.makedirs(SCREAMING_SNAKE_CASE_ , exist_ok=SCREAMING_SNAKE_CASE_ ) A: int = os.path.join(SCREAMING_SNAKE_CASE_ , BART_VOCAB_FILES_NAMES['''vocab_file'''] ) A: Any = os.path.join(SCREAMING_SNAKE_CASE_ , BART_VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(SCREAMING_SNAKE_CASE_ ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(SCREAMING_SNAKE_CASE_ ) ) def _snake_case ( self : str ) -> DPRQuestionEncoderTokenizer: '''simple docstring''' return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def _snake_case ( self : Union[str, Any] ) -> BartTokenizer: '''simple docstring''' return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''bart_tokenizer''' ) ) def _snake_case ( self : int ) -> List[Any]: '''simple docstring''' shutil.rmtree(self.tmpdirname ) @require_tokenizers def _snake_case ( self : Any ) -> List[Any]: '''simple docstring''' A: Any = os.path.join(self.tmpdirname , '''rag_tokenizer''' ) A: Any = RagConfig(question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() ) A: Tuple = RagTokenizer(question_encoder=self.get_dpr_tokenizer() , generator=self.get_bart_tokenizer() ) rag_config.save_pretrained(SCREAMING_SNAKE_CASE_ ) rag_tokenizer.save_pretrained(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = RagTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ , config=SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(new_rag_tokenizer.question_encoder , SCREAMING_SNAKE_CASE_ ) self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab() , rag_tokenizer.question_encoder.get_vocab() ) self.assertIsInstance(new_rag_tokenizer.generator , SCREAMING_SNAKE_CASE_ ) self.assertEqual(new_rag_tokenizer.generator.get_vocab() , rag_tokenizer.generator.get_vocab() ) @slow def _snake_case ( self : Optional[int] ) -> int: '''simple docstring''' A: Dict = RagTokenizer.from_pretrained('''facebook/rag-token-nq''' ) A: Optional[Any] = [ '''who got the first nobel prize in physics''', '''when is the next deadpool movie being released''', '''which mode is used for short wave broadcast service''', '''who is the owner of reading football club''', '''when is the next scandal episode coming out''', '''when is the last time the philadelphia won the superbowl''', '''what is the most current adobe flash player version''', '''how many episodes are there in dragon ball z''', '''what is the first step in the evolution of the eye''', '''where is gall bladder situated in human body''', '''what is the main mineral in lithium batteries''', '''who is the president of usa right now''', '''where do the greasers live in the outsiders''', '''panda is a national animal of which country''', '''what is the name of manchester united stadium''', ] A: Union[str, Any] = tokenizer(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Dict ) -> int: '''simple docstring''' A: Union[str, Any] = RagTokenizer.from_pretrained('''facebook/rag-sequence-nq''' ) A: str = [ '''who got the first nobel prize in physics''', '''when is the next deadpool movie being released''', '''which mode is used for short wave broadcast service''', '''who is the owner of reading football club''', '''when is the next scandal episode coming out''', '''when is the last time the philadelphia won the superbowl''', '''what is the most current adobe flash player version''', '''how many episodes are there in dragon ball z''', '''what is the first step in the evolution of the eye''', '''where is gall bladder situated in human body''', '''what is the main mineral in lithium batteries''', '''who is the president of usa right now''', '''where do the greasers live in the outsiders''', '''panda is a national animal of which country''', '''what is the name of manchester united stadium''', ] A: Union[str, Any] = tokenizer(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ )
319
'''simple docstring''' from __future__ import annotations import math def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list: if len(__lowercase ) != 2 or len(a[0] ) != 2 or len(__lowercase ) != 2 or len(b[0] ) != 2: raise Exception('''Matrices are not 2x2''' ) A: str = [ [a[0][0] * b[0][0] + a[0][1] * b[1][0], a[0][0] * b[0][1] + a[0][1] * b[1][1]], [a[1][0] * b[0][0] + a[1][1] * b[1][0], a[1][0] * b[0][1] + a[1][1] * b[1][1]], ] return new_matrix def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: return [ [matrix_a[row][col] + matrix_b[row][col] for col in range(len(matrix_a[row] ) )] for row in range(len(__lowercase ) ) ] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: return [ [matrix_a[row][col] - matrix_b[row][col] for col in range(len(matrix_a[row] ) )] for row in range(len(__lowercase ) ) ] def SCREAMING_SNAKE_CASE( __lowercase ) -> tuple[list, list, list, list]: if len(__lowercase ) % 2 != 0 or len(a[0] ) % 2 != 0: raise Exception('''Odd matrices are not supported!''' ) A: Union[str, Any] = len(__lowercase ) A: str = matrix_length // 2 A: Optional[int] = [[a[i][j] for j in range(__lowercase , __lowercase )] for i in range(__lowercase )] A: Optional[Any] = [ [a[i][j] for j in range(__lowercase , __lowercase )] for i in range(__lowercase , __lowercase ) ] A: Union[str, Any] = [[a[i][j] for j in range(__lowercase )] for i in range(__lowercase )] A: int = [[a[i][j] for j in range(__lowercase )] for i in range(__lowercase , __lowercase )] return top_left, top_right, bot_left, bot_right def SCREAMING_SNAKE_CASE( __lowercase ) -> tuple[int, int]: return len(__lowercase ), len(matrix[0] ) def SCREAMING_SNAKE_CASE( __lowercase ) -> None: print('''\n'''.join(str(__lowercase ) for line in matrix ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list: if matrix_dimensions(__lowercase ) == (2, 2): return default_matrix_multiplication(__lowercase , __lowercase ) A , A , A , A: Union[str, Any] = split_matrix(__lowercase ) A , A , A , A: List[Any] = split_matrix(__lowercase ) A: Optional[int] = actual_strassen(__lowercase , matrix_subtraction(__lowercase , __lowercase ) ) A: Any = actual_strassen(matrix_addition(__lowercase , __lowercase ) , __lowercase ) A: Tuple = actual_strassen(matrix_addition(__lowercase , __lowercase ) , __lowercase ) A: Optional[int] = actual_strassen(__lowercase , matrix_subtraction(__lowercase , __lowercase ) ) A: Tuple = actual_strassen(matrix_addition(__lowercase , __lowercase ) , matrix_addition(__lowercase , __lowercase ) ) A: Union[str, Any] = actual_strassen(matrix_subtraction(__lowercase , __lowercase ) , matrix_addition(__lowercase , __lowercase ) ) A: List[str] = actual_strassen(matrix_subtraction(__lowercase , __lowercase ) , matrix_addition(__lowercase , __lowercase ) ) A: int = matrix_addition(matrix_subtraction(matrix_addition(__lowercase , __lowercase ) , __lowercase ) , __lowercase ) A: Any = matrix_addition(__lowercase , __lowercase ) A: List[Any] = matrix_addition(__lowercase , __lowercase ) A: List[str] = matrix_subtraction(matrix_subtraction(matrix_addition(__lowercase , __lowercase ) , __lowercase ) , __lowercase ) # construct the new matrix from our 4 quadrants A: Union[str, Any] = [] for i in range(len(__lowercase ) ): new_matrix.append(top_left[i] + top_right[i] ) for i in range(len(__lowercase ) ): new_matrix.append(bot_left[i] + bot_right[i] ) return new_matrix def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list: if matrix_dimensions(__lowercase )[1] != matrix_dimensions(__lowercase )[0]: A: int = ( '''Unable to multiply these matrices, please check the dimensions.\n''' F"""Matrix A: {matrixa}\n""" F"""Matrix B: {matrixa}""" ) raise Exception(__lowercase ) A: str = matrix_dimensions(__lowercase ) A: str = matrix_dimensions(__lowercase ) if dimensiona[0] == dimensiona[1] and dimensiona[0] == dimensiona[1]: return [matrixa, matrixa] A: Union[str, Any] = max(*__lowercase , *__lowercase ) A: Optional[int] = int(math.pow(2 , math.ceil(math.loga(__lowercase ) ) ) ) A: List[Any] = matrixa A: Tuple = matrixa # Adding zeros to the matrices so that the arrays dimensions are the same and also # power of 2 for i in range(0 , __lowercase ): if i < dimensiona[0]: for _ in range(dimensiona[1] , __lowercase ): new_matrixa[i].append(0 ) else: new_matrixa.append([0] * maxim ) if i < dimensiona[0]: for _ in range(dimensiona[1] , __lowercase ): new_matrixa[i].append(0 ) else: new_matrixa.append([0] * maxim ) A: Any = actual_strassen(__lowercase , __lowercase ) # Removing the additional zeros for i in range(0 , __lowercase ): if i < dimensiona[0]: for _ in range(dimensiona[1] , __lowercase ): final_matrix[i].pop() else: final_matrix.pop() return final_matrix if __name__ == "__main__": UpperCamelCase = [ [2, 3, 4, 5], [6, 4, 3, 1], [2, 3, 6, 7], [3, 1, 2, 4], [2, 3, 4, 5], [6, 4, 3, 1], [2, 3, 6, 7], [3, 1, 2, 4], [2, 3, 4, 5], [6, 2, 3, 1], ] UpperCamelCase = [[0, 2, 1, 1], [16, 2, 3, 3], [2, 2, 7, 7], [13, 11, 22, 4]] print(strassen(matrixa, matrixa))
319
1
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Value from .base import TaskTemplate @dataclass(frozen=UpperCAmelCase_ ) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : str = field(default="""text-classification""" , metadata={"""include_in_asdict_even_if_is_default""": True} ) UpperCamelCase_ : ClassVar[Features] = Features({"""text""": Value("""string""" )} ) UpperCamelCase_ : ClassVar[Features] = Features({"""labels""": ClassLabel} ) UpperCamelCase_ : str = "text" UpperCamelCase_ : str = "labels" def _snake_case ( self : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> List[str]: '''simple docstring''' if self.label_column not in features: raise ValueError(f"""Column {self.label_column} is not present in features.""" ) if not isinstance(features[self.label_column] , SCREAMING_SNAKE_CASE_ ): raise ValueError(f"""Column {self.label_column} is not a ClassLabel.""" ) A: Any = copy.deepcopy(self ) A: List[Any] = self.label_schema.copy() A: Tuple = features[self.label_column] A: Union[str, Any] = label_schema return task_template @property def _snake_case ( self : Union[str, Any] ) -> Dict[str, str]: '''simple docstring''' return { self.text_column: "text", self.label_column: "labels", }
319
'''simple docstring''' from dataclasses import dataclass from typing import Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin @dataclass class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : torch.FloatTensor UpperCamelCase_ : torch.FloatTensor UpperCamelCase_ : Optional[torch.FloatTensor] = None class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = 2 @register_to_config def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : float = 0.02 , SCREAMING_SNAKE_CASE_ : float = 1_00 , SCREAMING_SNAKE_CASE_ : float = 1.007 , SCREAMING_SNAKE_CASE_ : float = 80 , SCREAMING_SNAKE_CASE_ : float = 0.05 , SCREAMING_SNAKE_CASE_ : float = 50 , ) -> Optional[int]: '''simple docstring''' A: Union[str, Any] = sigma_max # setable values A: int = None A: np.IntTensor = None A: torch.FloatTensor = None # sigma(t_i) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : Optional[int] = None ) -> torch.FloatTensor: '''simple docstring''' return sample def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Union[str, torch.device] = None ) -> Optional[Any]: '''simple docstring''' A: List[Any] = num_inference_steps A: List[str] = np.arange(0 , self.num_inference_steps )[::-1].copy() A: Any = torch.from_numpy(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ) A: str = [ ( self.config.sigma_max**2 * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1)) ) for i in self.timesteps ] A: Tuple = torch.tensor(SCREAMING_SNAKE_CASE_ , dtype=torch.floataa , device=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : Optional[torch.Generator] = None ) -> Tuple[torch.FloatTensor, float]: '''simple docstring''' if self.config.s_min <= sigma <= self.config.s_max: A: str = min(self.config.s_churn / self.num_inference_steps , 2**0.5 - 1 ) else: A: List[str] = 0 # sample eps ~ N(0, S_noise^2 * I) A: Optional[Any] = self.config.s_noise * randn_tensor(sample.shape , generator=SCREAMING_SNAKE_CASE_ ).to(sample.device ) A: Optional[Any] = sigma + gamma * sigma A: List[Any] = sample + ((sigma_hat**2 - sigma**2) ** 0.5 * eps) return sample_hat, sigma_hat def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : bool = True , ) -> Union[KarrasVeOutput, Tuple]: '''simple docstring''' A: Union[str, Any] = sample_hat + sigma_hat * model_output A: str = (sample_hat - pred_original_sample) / sigma_hat A: Optional[int] = sample_hat + (sigma_prev - sigma_hat) * derivative if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=SCREAMING_SNAKE_CASE_ , derivative=SCREAMING_SNAKE_CASE_ , pred_original_sample=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : bool = True , ) -> Union[KarrasVeOutput, Tuple]: '''simple docstring''' A: int = sample_prev + sigma_prev * model_output A: List[Any] = (sample_prev - pred_original_sample) / sigma_prev A: Dict = sample_hat + (sigma_prev - sigma_hat) * (0.5 * derivative + 0.5 * derivative_corr) if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=SCREAMING_SNAKE_CASE_ , derivative=SCREAMING_SNAKE_CASE_ , pred_original_sample=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : str ) -> Dict: '''simple docstring''' raise NotImplementedError()
319
1
'''simple docstring''' # We ignore warnings about stepping the scheduler since we step it ourselves during gradient accumulation import warnings from .state import AcceleratorState, GradientState warnings.filterwarnings('''ignore''', category=UserWarning, module='''torch.optim.lr_scheduler''') class lowerCAmelCase_ : '''simple docstring''' def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : bool = True , SCREAMING_SNAKE_CASE_ : bool = False ) -> List[str]: '''simple docstring''' A: Union[str, Any] = scheduler A: Dict = optimizers if isinstance(SCREAMING_SNAKE_CASE_ , (list, tuple) ) else [optimizers] A: Any = split_batches A: int = step_with_optimizer A: Optional[int] = GradientState() def _snake_case ( self : Optional[Any] , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : int ) -> List[str]: '''simple docstring''' if not self.step_with_optimizer: # No link between scheduler and optimizer -> just step self.scheduler.step(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) return # Otherwise, first make sure the optimizer was stepped. if not self.gradient_state.sync_gradients: if self.gradient_state.adjust_scheduler: self.scheduler._step_count += 1 return for opt in self.optimizers: if opt.step_was_skipped: return if self.split_batches: # Split batches -> the training dataloader batch size is not changed so one step per training step self.scheduler.step(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) else: # Otherwise the training dataloader batch size was multiplied by `num_processes`, so we need to do # num_processes steps per training step A: Tuple = AcceleratorState().num_processes for _ in range(SCREAMING_SNAKE_CASE_ ): # Special case when using OneCycle and `drop_last` was not used if hasattr(self.scheduler , '''total_steps''' ): if self.scheduler._step_count <= self.scheduler.total_steps: self.scheduler.step(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) else: self.scheduler.step(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[Any] ) -> Any: '''simple docstring''' return self.scheduler.get_last_lr() def _snake_case ( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' return self.scheduler.state_dict() def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Optional[Any]: '''simple docstring''' self.scheduler.load_state_dict(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[Any] ) -> Any: '''simple docstring''' return self.scheduler.get_lr() def _snake_case ( self : Optional[Any] , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Any ) -> Tuple: '''simple docstring''' return self.scheduler.print_lr(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ )
319
'''simple docstring''' import json import logging import math import os import sys from dataclasses import dataclass, field from typing import Optional from datasets import Dataset, load_dataset import transformers from transformers import ( CONFIG_MAPPING, MODEL_FOR_MASKED_LM_MAPPING, AutoConfig, AutoModelForMaskedLM, AutoTokenizer, DataCollatorForWholeWordMask, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process UpperCamelCase = logging.getLogger(__name__) UpperCamelCase = list(MODEL_FOR_MASKED_LM_MAPPING.keys()) UpperCamelCase = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={ """help""": ( """The model checkpoint for weights initialization.Don't set if you want to train a model from scratch.""" ) } , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """If training from scratch, pass a model type from the list: """ + """, """.join(UpperCAmelCase_ )} , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={ """help""": ( """Override some existing default config settings when a model is trained from scratch. Example: """ """n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index""" ) } , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) UpperCamelCase_ : bool = field( default=UpperCAmelCase_ , metadata={"""help""": """Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."""} , ) UpperCamelCase_ : str = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) UpperCamelCase_ : bool = field( default=UpperCAmelCase_ , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) def _snake_case ( self : Tuple ) -> List[Any]: '''simple docstring''' if self.config_overrides is not None and (self.config_name is not None or self.model_name_or_path is not None): raise ValueError( '''--config_overrides can\'t be used in combination with --config_name or --model_name_or_path''' ) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """The name of the dataset to use (via the datasets library)."""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) UpperCamelCase_ : Optional[str] = field(default=UpperCAmelCase_ , metadata={"""help""": """The input training data file (a text file)."""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """An optional input evaluation data file to evaluate the perplexity on (a text file)."""} , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """An optional input train ref data file for whole word masking in Chinese."""} , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """An optional input validation ref data file for whole word masking in Chinese."""} , ) UpperCamelCase_ : bool = field( default=UpperCAmelCase_ , metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) UpperCamelCase_ : Optional[int] = field( default=5 , metadata={ """help""": """The percentage of the train set used as validation set in case there's no validation split""" } , ) UpperCamelCase_ : Optional[int] = field( default=UpperCAmelCase_ , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated. Default to the max input length of the model.""" ) } , ) UpperCamelCase_ : Optional[int] = field( default=UpperCAmelCase_ , metadata={"""help""": """The number of processes to use for the preprocessing."""} , ) UpperCamelCase_ : float = field( default=0.15 , metadata={"""help""": """Ratio of tokens to mask for masked language modeling loss"""} ) UpperCamelCase_ : bool = field( default=UpperCAmelCase_ , metadata={ """help""": ( """Whether to pad all samples to `max_seq_length`. """ """If False, will pad the samples dynamically when batching to the maximum length in the batch.""" ) } , ) def _snake_case ( self : List[Any] ) -> Optional[int]: '''simple docstring''' if self.train_file is not None: A: Tuple = self.train_file.split('''.''' )[-1] assert extension in ["csv", "json", "txt"], "`train_file` should be a csv, a json or a txt file." if self.validation_file is not None: A: str = self.validation_file.split('''.''' )[-1] assert extension in ["csv", "json", "txt"], "`validation_file` should be a csv, a json or a txt file." def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[str]: with open(__lowercase , '''r''' , encoding='''utf-8''' ) as f: A: List[Any] = [json.loads(__lowercase ) for line in f.read().splitlines() if (len(__lowercase ) > 0 and not line.isspace())] assert len(__lowercase ) == len(__lowercase ) A: Optional[int] = {c: dataset[c] for c in dataset.column_names} A: Union[str, Any] = refs return Dataset.from_dict(__lowercase ) def SCREAMING_SNAKE_CASE( ) -> int: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A: int = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('''.json''' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. A , A , A: Optional[int] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: A , A , A: List[Any] = parser.parse_args_into_dataclasses() # Detecting last checkpoint. A: Any = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: A: Any = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. """ '''Use --overwrite_output_dir to overcome.''' ) elif last_checkpoint is not None: logger.info( F"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ '''the `--output_dir` or add `--overwrite_output_dir` to train from scratch.''' ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , handlers=[logging.StreamHandler(sys.stdout )] , ) logger.setLevel(logging.INFO if is_main_process(training_args.local_rank ) else logging.WARN ) # Log on each process the small summary: logger.warning( F"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + F"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('''Training/evaluation parameters %s''' , __lowercase ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below) # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/ # (the dataset will be downloaded automatically from the datasets Hub). # # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called # 'text' is found. You can easily tweak this behavior (see below). # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. A: Dict = load_dataset(data_args.dataset_name , data_args.dataset_config_name ) if "validation" not in datasets.keys(): A: int = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F"""train[:{data_args.validation_split_percentage}%]""" , ) A: Dict = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F"""train[{data_args.validation_split_percentage}%:]""" , ) else: A: Any = {} if data_args.train_file is not None: A: int = data_args.train_file if data_args.validation_file is not None: A: Optional[int] = data_args.validation_file A: List[str] = data_args.train_file.split('''.''' )[-1] if extension == "txt": A: int = '''text''' A: Any = load_dataset(__lowercase , data_files=__lowercase ) # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at # https://huggingface.co/docs/datasets/loading_datasets.html. # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. A: Dict = { '''cache_dir''': model_args.cache_dir, '''revision''': model_args.model_revision, '''use_auth_token''': True if model_args.use_auth_token else None, } if model_args.config_name: A: List[Any] = AutoConfig.from_pretrained(model_args.config_name , **__lowercase ) elif model_args.model_name_or_path: A: int = AutoConfig.from_pretrained(model_args.model_name_or_path , **__lowercase ) else: A: str = CONFIG_MAPPING[model_args.model_type]() logger.warning('''You are instantiating a new config instance from scratch.''' ) if model_args.config_overrides is not None: logger.info(F"""Overriding config: {model_args.config_overrides}""" ) config.update_from_string(model_args.config_overrides ) logger.info(F"""New config: {config}""" ) A: Tuple = { '''cache_dir''': model_args.cache_dir, '''use_fast''': model_args.use_fast_tokenizer, '''revision''': model_args.model_revision, '''use_auth_token''': True if model_args.use_auth_token else None, } if model_args.tokenizer_name: A: Optional[int] = AutoTokenizer.from_pretrained(model_args.tokenizer_name , **__lowercase ) elif model_args.model_name_or_path: A: Union[str, Any] = AutoTokenizer.from_pretrained(model_args.model_name_or_path , **__lowercase ) else: raise ValueError( '''You are instantiating a new tokenizer from scratch. This is not supported by this script.''' '''You can do it from another script, save it, and load it from here, using --tokenizer_name.''' ) if model_args.model_name_or_path: A: List[Any] = AutoModelForMaskedLM.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=__lowercase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info('''Training new model from scratch''' ) A: List[Any] = AutoModelForMaskedLM.from_config(__lowercase ) model.resize_token_embeddings(len(__lowercase ) ) # Preprocessing the datasets. # First we tokenize all the texts. if training_args.do_train: A: int = datasets['''train'''].column_names else: A: str = datasets['''validation'''].column_names A: Tuple = '''text''' if '''text''' in column_names else column_names[0] A: List[str] = '''max_length''' if data_args.pad_to_max_length else False def tokenize_function(__lowercase ): # Remove empty lines A: int = [line for line in examples['''text'''] if len(__lowercase ) > 0 and not line.isspace()] return tokenizer(examples['''text'''] , padding=__lowercase , truncation=__lowercase , max_length=data_args.max_seq_length ) A: str = datasets.map( __lowercase , batched=__lowercase , num_proc=data_args.preprocessing_num_workers , remove_columns=[text_column_name] , load_from_cache_file=not data_args.overwrite_cache , ) # Add the chinese references if provided if data_args.train_ref_file is not None: A: List[str] = add_chinese_references(tokenized_datasets['''train'''] , data_args.train_ref_file ) if data_args.validation_ref_file is not None: A: Dict = add_chinese_references( tokenized_datasets['''validation'''] , data_args.validation_ref_file ) # If we have ref files, need to avoid it removed by trainer A: Optional[Any] = data_args.train_ref_file or data_args.validation_ref_file if has_ref: A: List[Any] = False # Data collator # This one will take care of randomly masking the tokens. A: Optional[Any] = DataCollatorForWholeWordMask(tokenizer=__lowercase , mlm_probability=data_args.mlm_probability ) # Initialize our Trainer A: Optional[int] = Trainer( model=__lowercase , args=__lowercase , train_dataset=tokenized_datasets['''train'''] if training_args.do_train else None , eval_dataset=tokenized_datasets['''validation'''] if training_args.do_eval else None , tokenizer=__lowercase , data_collator=__lowercase , ) # Training if training_args.do_train: if last_checkpoint is not None: A: Optional[int] = last_checkpoint elif model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path ): A: str = model_args.model_name_or_path else: A: List[str] = None A: str = trainer.train(resume_from_checkpoint=__lowercase ) trainer.save_model() # Saves the tokenizer too for easy upload A: Union[str, Any] = os.path.join(training_args.output_dir , '''train_results.txt''' ) if trainer.is_world_process_zero(): with open(__lowercase , '''w''' ) as writer: logger.info('''***** Train results *****''' ) for key, value in sorted(train_result.metrics.items() ): logger.info(F""" {key} = {value}""" ) writer.write(F"""{key} = {value}\n""" ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , '''trainer_state.json''' ) ) # Evaluation A: Optional[int] = {} if training_args.do_eval: logger.info('''*** Evaluate ***''' ) A: Optional[Any] = trainer.evaluate() A: Union[str, Any] = math.exp(eval_output['''eval_loss'''] ) A: Dict = perplexity A: Any = os.path.join(training_args.output_dir , '''eval_results_mlm_wwm.txt''' ) if trainer.is_world_process_zero(): with open(__lowercase , '''w''' ) as writer: logger.info('''***** Eval results *****''' ) for key, value in sorted(results.items() ): logger.info(F""" {key} = {value}""" ) writer.write(F"""{key} = {value}\n""" ) return results def SCREAMING_SNAKE_CASE( __lowercase ) -> List[Any]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
319
1
'''simple docstring''' import argparse import pathlib import fairseq import torch from fairseq.models.roberta import RobertaModel as FairseqRobertaModel from fairseq.modules import TransformerSentenceEncoderLayer from packaging import version from transformers import XLMRobertaConfig, XLMRobertaXLForMaskedLM, XLMRobertaXLForSequenceClassification from transformers.models.bert.modeling_bert import ( BertIntermediate, BertLayer, BertOutput, BertSelfAttention, BertSelfOutput, ) from transformers.models.roberta.modeling_roberta import RobertaAttention from transformers.utils import logging if version.parse(fairseq.__version__) < version.parse('''1.0.0a'''): raise Exception('''requires fairseq >= 1.0.0a''') logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = '''Hello world! cécé herlolip''' def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Any: A: List[str] = FairseqRobertaModel.from_pretrained(__lowercase ) roberta.eval() # disable dropout A: Tuple = roberta.model.encoder.sentence_encoder A: Tuple = XLMRobertaConfig( vocab_size=roberta_sent_encoder.embed_tokens.num_embeddings , hidden_size=roberta.cfg.model.encoder_embed_dim , num_hidden_layers=roberta.cfg.model.encoder_layers , num_attention_heads=roberta.cfg.model.encoder_attention_heads , intermediate_size=roberta.cfg.model.encoder_ffn_embed_dim , max_position_embeddings=5_1_4 , type_vocab_size=1 , layer_norm_eps=1E-5 , ) if classification_head: A: Union[str, Any] = roberta.model.classification_heads['''mnli'''].out_proj.weight.shape[0] print('''Our RoBERTa config:''' , __lowercase ) A: Any = XLMRobertaXLForSequenceClassification(__lowercase ) if classification_head else XLMRobertaXLForMaskedLM(__lowercase ) model.eval() # Now let's copy all the weights. # Embeddings A: Union[str, Any] = roberta_sent_encoder.embed_tokens.weight A: int = roberta_sent_encoder.embed_positions.weight A: Dict = torch.zeros_like( model.roberta.embeddings.token_type_embeddings.weight ) # just zero them out b/c RoBERTa doesn't use them. A: Dict = roberta_sent_encoder.layer_norm.weight A: Union[str, Any] = roberta_sent_encoder.layer_norm.bias for i in range(config.num_hidden_layers ): # Encoder: start of layer A: BertLayer = model.roberta.encoder.layer[i] A: TransformerSentenceEncoderLayer = roberta_sent_encoder.layers[i] A: RobertaAttention = layer.attention A: Optional[Any] = roberta_layer.self_attn_layer_norm.weight A: Dict = roberta_layer.self_attn_layer_norm.bias # self attention A: BertSelfAttention = layer.attention.self assert ( roberta_layer.self_attn.k_proj.weight.data.shape == roberta_layer.self_attn.q_proj.weight.data.shape == roberta_layer.self_attn.v_proj.weight.data.shape == torch.Size((config.hidden_size, config.hidden_size) ) ) A: Dict = roberta_layer.self_attn.q_proj.weight A: List[Any] = roberta_layer.self_attn.q_proj.bias A: int = roberta_layer.self_attn.k_proj.weight A: int = roberta_layer.self_attn.k_proj.bias A: Tuple = roberta_layer.self_attn.v_proj.weight A: Optional[Any] = roberta_layer.self_attn.v_proj.bias # self-attention output A: BertSelfOutput = layer.attention.output assert self_output.dense.weight.shape == roberta_layer.self_attn.out_proj.weight.shape A: str = roberta_layer.self_attn.out_proj.weight A: List[Any] = roberta_layer.self_attn.out_proj.bias # this one is final layer norm A: List[Any] = roberta_layer.final_layer_norm.weight A: Dict = roberta_layer.final_layer_norm.bias # intermediate A: BertIntermediate = layer.intermediate assert intermediate.dense.weight.shape == roberta_layer.fca.weight.shape A: List[Any] = roberta_layer.fca.weight A: Dict = roberta_layer.fca.bias # output A: BertOutput = layer.output assert bert_output.dense.weight.shape == roberta_layer.fca.weight.shape A: Any = roberta_layer.fca.weight A: List[Any] = roberta_layer.fca.bias # end of layer if classification_head: A: Optional[Any] = roberta.model.classification_heads['''mnli'''].dense.weight A: Any = roberta.model.classification_heads['''mnli'''].dense.bias A: str = roberta.model.classification_heads['''mnli'''].out_proj.weight A: int = roberta.model.classification_heads['''mnli'''].out_proj.bias else: # LM Head A: str = roberta.model.encoder.lm_head.dense.weight A: int = roberta.model.encoder.lm_head.dense.bias A: List[Any] = roberta.model.encoder.lm_head.layer_norm.weight A: str = roberta.model.encoder.lm_head.layer_norm.bias A: List[str] = roberta.model.encoder.lm_head.weight A: str = roberta.model.encoder.lm_head.bias # Let's check that we get the same results. A: torch.Tensor = roberta.encode(__lowercase ).unsqueeze(0 ) # batch of size 1 A: Optional[int] = model(__lowercase )[0] if classification_head: A: int = roberta.model.classification_heads['''mnli'''](roberta.extract_features(__lowercase ) ) else: A: Dict = roberta.model(__lowercase )[0] print(our_output.shape , their_output.shape ) A: Any = torch.max(torch.abs(our_output - their_output ) ).item() print(F"""max_absolute_diff = {max_absolute_diff}""" ) # ~ 1e-7 A: str = torch.allclose(__lowercase , __lowercase , atol=1E-3 ) print('''Do both models output the same tensors?''' , '''🔥''' if success else '''💩''' ) if not success: raise Exception('''Something went wRoNg''' ) pathlib.Path(__lowercase ).mkdir(parents=__lowercase , exist_ok=__lowercase ) print(F"""Saving model to {pytorch_dump_folder_path}""" ) model.save_pretrained(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--roberta_checkpoint_path''', default=None, type=str, required=True, help='''Path the official PyTorch dump.''' ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--classification_head''', action='''store_true''', help='''Whether to convert a final classification head.''' ) UpperCamelCase = parser.parse_args() convert_xlm_roberta_xl_checkpoint_to_pytorch( args.roberta_checkpoint_path, args.pytorch_dump_folder_path, args.classification_head )
319
'''simple docstring''' import json import os import unittest from typing import Tuple from transformers import WavaVecaPhonemeCTCTokenizer from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.models.wavaveca_phoneme.tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizerOutput from transformers.testing_utils import require_phonemizer from ...test_tokenization_common import TokenizerTesterMixin @require_phonemizer class lowerCAmelCase_ ( UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Any = WavaVecaPhonemeCTCTokenizer UpperCamelCase_ : Tuple = False def _snake_case ( self : str ) -> Union[str, Any]: '''simple docstring''' super().setUp() A: Optional[int] = ( '''<s> <pad> </s> <unk> n s t ə l a i k d m ɛ ɾ e ɪ p o ɐ z ð f j v b ɹ ʁ ʊ iː r w ʌ u ɡ æ aɪ ʃ h ɔ ɑː ''' '''ŋ ɚ eɪ β uː y ɑ̃ oʊ ᵻ eː θ aʊ ts oː ɔ̃ ɣ ɜ ɑ dʒ əl x ɜː ç ʒ tʃ ɔː ɑːɹ ɛ̃ ʎ ɔːɹ ʋ aː ɕ œ ø oːɹ ɲ yː ''' '''ʔ iə i5 s. tɕ ?? nʲ ɛː œ̃ ɭ ɔø ʑ tʲ ɨ ɛɹ ts. rʲ ɪɹ ɭʲ i.5 ɔɪ q sʲ u5 ʊɹ iɜ a5 iɛ5 øː ʕ ja əɜ th ɑ5 ''' '''oɪ dʲ ə5 tɕh ts.h mʲ ɯ dʑ vʲ e̞ tʃʲ ei5 o5 onɡ5 ɑu5 iɑ5 ai5 aɪɚ kh ə1 ʐ i2 ʉ ħ t[ aɪə ʲ ju ə2 u2 oɜ ''' '''pː iɛɜ ou5 y5 uɜ tː uo5 d[ uoɜ tsh ɑɜ ɵ i̪5 uei5 ɟ aɜ ɑɨ i.ɜ eʊ o2 ɐ̃ ä pʲ kʲ n̩ ɒ ph ɑu2 uɨ əɪ ɫ ɬ ''' '''yɜ bʲ ɑ2 s̪ aiɜ χ ɐ̃ʊ̃ 1 ə4 yæɜ a2 ɨː t̪ iouɜ ũ onɡɜ aɨ iɛ2 ɔɨ ɑuɜ o̞ ei2 iou2 c kː y2 ɖ oe dˤ yɛɜ ''' '''əʊ S ɡʲ onɡ2 u" eiɜ ʈ ɯᵝ iou5 dZ r̝̊ i.2 tS s^ ʝ yə5 iɑɜ uə5 pf ɨu iɑ2 ou2 ər2 fʲ ai2 r̝ uəɜ ɳ əɨ ''' '''ua5 uɪ ɽ bː yu5 uo2 yɛ5 l̩ ɻ ərɜ ʂ i̪2 ouɜ uaɜ a. a.ː yæ5 dː r̩ ee ɪu ər5 i̪ ɜ æi u: i.ː t^ o1 ɪ^ ''' '''ai ueiɜ æː ɛɪ eə i. ɴ ie ua2 ɑ1 o4 tʃː o: ɑ: u1 N i̪1 au yæ2 u. qː yəɜ y: kʰ tʃʰ iʊ sx õ uo tʰ ''' '''uai5 bʰ u.ː uə2 ʊə d^ s̪ː yiɜ dʰ r. oe: i1 ɟː yu2 nʲʲ i̪4 uei2 tsʲ ɸ ĩ ɑ4 t̪ː eɑ u4 e: tsː ʈʰ ɡʰ ''' '''ɯɯ dʒʲ ʂʲ X ɵː uaiɜ tɕʲ ã t^ː ẽː yɛ2 cː i.1 ɛʊ dˤdˤ dʒː i4 ɡː yi ɕʲ ɟʰ pʰ dʑʲ yuɜ ua1 ua4 æiː ɐɐ ''' '''ui iou1 ʊː a1 iou4 cʰ iɛ1 yə2 ɖʰ ẽ ʒʲ ää ər4 iːː ɪː iɑ1 ər1 œː øi ɪuː cʰcʰ əː1 iː1 ũ kʰː o̞o̞ xʲ ''' '''ou1 iɛ4 e̞e̞ y1 dzː dʲʲ dʰː ɯᵝɯᵝ lː uo1 i.4 i: yɛ5ʲ a4''' ).split(''' ''' ) A: Union[str, Any] = dict(zip(SCREAMING_SNAKE_CASE_ , range(len(SCREAMING_SNAKE_CASE_ ) ) ) ) A: Dict = {'''pad_token''': '''<pad>''', '''unk_token''': '''<unk>''', '''bos_token''': '''<s>''', '''eos_token''': '''</s>'''} A: Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(SCREAMING_SNAKE_CASE_ ) + '''\n''' ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Tuple=False , SCREAMING_SNAKE_CASE_ : Any=20 , SCREAMING_SNAKE_CASE_ : Optional[int]=5 ) -> Tuple[str, list]: '''simple docstring''' A: int = [(i, tokenizer.decode([i] , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ )) for i in range(len(SCREAMING_SNAKE_CASE_ ) )] A: Optional[Any] = list(filter(lambda SCREAMING_SNAKE_CASE_ : [t[0]] == tokenizer.encode(t[1] , do_phonemize=SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) ) if max_length is not None and len(SCREAMING_SNAKE_CASE_ ) > max_length: A: int = toks[:max_length] if min_length is not None and len(SCREAMING_SNAKE_CASE_ ) < min_length and len(SCREAMING_SNAKE_CASE_ ) > 0: while len(SCREAMING_SNAKE_CASE_ ) < min_length: A: Dict = toks + toks # toks_str = [t[1] for t in toks] A: Union[str, Any] = [t[0] for t in toks] # Ensure consistency A: List[str] = tokenizer.decode(SCREAMING_SNAKE_CASE_ , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) if " " not in output_txt and len(SCREAMING_SNAKE_CASE_ ) > 1: A: int = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) + ''' ''' + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) ) if with_prefix_space: A: Tuple = ''' ''' + output_txt A: List[str] = tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) return output_txt, output_ids def _snake_case ( self : Optional[int] , **SCREAMING_SNAKE_CASE_ : int ) -> Dict: '''simple docstring''' kwargs.update(self.special_tokens_map ) return WavaVecaPhonemeCTCTokenizer.from_pretrained(self.tmpdirname , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int ) -> Optional[Any]: '''simple docstring''' A: List[Any] = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) # check adding a single token tokenizer.add_tokens('''xxx''' ) A: Any = tokenizer('''m xxx ɪ''' , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids self.assertEqual(SCREAMING_SNAKE_CASE_ , [13, 3_92, 17] ) # xxx should be last token tokenizer.add_tokens(['''aaa''', '''bbb''', '''ccc'''] ) A: Optional[int] = tokenizer('''m aaa ɪ ccc''' , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids self.assertEqual(SCREAMING_SNAKE_CASE_ , [13, 3_93, 17, 3_95] ) # aaa and ccc should be after xxx and 2 after aaa A: str = tokenizer('''maɪ c''' , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids self.assertEqual(SCREAMING_SNAKE_CASE_ , [3, 2_00] ) # mai should be <unk> (=3) def _snake_case ( self : int ) -> List[Any]: '''simple docstring''' A: Any = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: Any = '''Hello how are you''' A: Optional[Any] = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''h ə l oʊ h aʊ ɑːɹ j uː''' ) def _snake_case ( self : Tuple ) -> Dict: '''simple docstring''' A: str = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: List[Any] = '''Hello how are you''' A: Any = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) self.assertEqual(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids , tokenizer(SCREAMING_SNAKE_CASE_ , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids ) def _snake_case ( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' A: str = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: List[str] = '''Hello how are you''' A: Union[str, Any] = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) A: Union[str, Any] = tokenizer.decode(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Dict ) -> Optional[Any]: '''simple docstring''' A: Dict = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: Optional[Any] = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98], [24, 22, 5, 24, 22, 5, 77], ] A: List[str] = tokenizer.decode(sample_ids[0] ) A: List[str] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , batch_tokens[0] ) self.assertEqual(SCREAMING_SNAKE_CASE_ , ['''k s ɾ ɾ l ɭʲ''', '''j ð s j ð s oːɹ'''] ) def _snake_case ( self : Any ) -> Optional[int]: '''simple docstring''' A: int = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) A: List[Any] = '''Hello how are you''' A: Optional[Any] = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''h ə l oʊ | h aʊ | ɑːɹ | j uː |''' ) def _snake_case ( self : List[str] ) -> int: '''simple docstring''' A: Optional[Any] = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) A: Optional[Any] = '''Hello how are you''' A: Any = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) self.assertEqual(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids , tokenizer(SCREAMING_SNAKE_CASE_ , do_phonemize=SCREAMING_SNAKE_CASE_ ).input_ids ) def _snake_case ( self : Dict ) -> Any: '''simple docstring''' A: Optional[int] = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) # fmt: off A: str = [ [11, 5, 15, tokenizer.pad_token_id, tokenizer.word_delimiter_token_id, 15, 8, tokenizer.word_delimiter_token_id, 98], [tokenizer.word_delimiter_token_id, 24, 22, tokenizer.word_delimiter_token_id, 5, 24, 22, 5, 77], ] # fmt: on # decode with word_del_token filter A: Tuple = tokenizer.decode(sample_ids[0] ) A: Optional[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , batch_tokens[0] ) self.assertEqual(SCREAMING_SNAKE_CASE_ , ['''k s ɾ ɾ l ɭʲ''', '''j ð s j ð s oːɹ'''] ) # decode with no word_del_token filter A: str = tokenizer.decode(sample_ids[0] , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , batch_tokens[0] ) self.assertEqual(SCREAMING_SNAKE_CASE_ , ['''k s ɾ | ɾ l | ɭʲ''', '''| j ð | s j ð s oːɹ'''] ) def _snake_case ( self : int ) -> List[str]: '''simple docstring''' A: Dict = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) A: Union[str, Any] = '''Hello how are you''' A: Tuple = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) A: Any = tokenizer.decode(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] ) -> Any: '''simple docstring''' A: Dict = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) A: Any = '''Hello how are you''' A: List[Any] = tokenizer.phonemize(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ) A: List[Any] = tokenizer.decode(tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) self.assertEqual(''' '''.join([p.strip() for p in phonemes.split(''' |''' )] ).strip() , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] ) -> Optional[Any]: '''simple docstring''' A: List[str] = self.tokenizer_class.from_pretrained( '''facebook/wav2vec2-lv-60-espeak-cv-ft''' , word_delimiter_token=SCREAMING_SNAKE_CASE_ ) A: List[Any] = '''Hello how are you''' A: List[str] = tokenizer(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''en-us''' ).input_ids A: Tuple = tokenizer(SCREAMING_SNAKE_CASE_ , phonemizer_lang='''fr-fr''' ).input_ids self.assertNotEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Tuple = tokenizer.decode(SCREAMING_SNAKE_CASE_ ) A: Any = tokenizer.decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''h ə l oʊ h aʊ ɑːɹ j uː''' ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''ɛ l o h aʊ a ʁ j u''' ) def _snake_case ( self : str ) -> str: '''simple docstring''' A: str = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) A: str = '''Hello how Are you''' A: Union[str, Any] = '''hello how are you''' A: List[str] = tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids A: str = tokenizer(SCREAMING_SNAKE_CASE_ ).input_ids self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int ) -> List[Any]: '''simple docstring''' A: Union[str, Any] = self.tokenizer_class.from_pretrained('''facebook/wav2vec2-lv-60-espeak-cv-ft''' ) tokenizer.add_tokens(['''!''', '''?'''] ) tokenizer.add_special_tokens({'''cls_token''': '''$$$'''} ) # fmt: off A: Tuple = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98, 3_92, 3_92, 3_93, 3_92, 3_92, 3_93, 3_94, 3_94], [24, 22, 5, 24, 22, 5, 77, tokenizer.pad_token_id, 3_94, 3_94], ] # fmt: on A: List[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , ['''k s ɾ ɾ l ɭʲ!?!? $$$''', '''j ð s j ð s oːɹ $$$'''] ) @staticmethod def _snake_case ( SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Tuple: '''simple docstring''' A: Any = [d[key] for d in offsets] return retrieved_list def _snake_case ( self : Any ) -> Tuple: '''simple docstring''' A: str = self.get_tokenizer(word_delimiter_token='''|''' ) tokenizer.add_tokens('''|''' ) # fmt: off # ksssɾɾ|ɾɾ<pad>ɾɾ|<pad>ɾlll|ɭʲ -> k s ɾ ɾ | ɾ l | ɭʲ" A: Union[str, Any] = [11, 5, 5, 5, 15, 15, tokenizer.pad_token_id, 15, 15, tokenizer.word_delimiter_token_id, tokenizer.pad_token_id, 15, 8, 8, 8, tokenizer.word_delimiter_token_id, 98] # fmt: on A: int = tokenizer.decode(SCREAMING_SNAKE_CASE_ , output_char_offsets=SCREAMING_SNAKE_CASE_ , filter_word_delimiter_token=SCREAMING_SNAKE_CASE_ ) # check Wav2Vec2CTCTokenizerOutput keys for char self.assertEqual(len(outputs.keys() ) , 2 ) self.assertTrue('''text''' in outputs ) self.assertTrue('''char_offsets''' in outputs ) self.assertTrue(isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) # check that order of chars is correct and identical for both outputs self.assertEqual(''' '''.join(self.get_from_offsets(outputs['''char_offsets'''] , '''char''' ) ) , outputs.text ) self.assertListEqual( self.get_from_offsets(outputs['''char_offsets'''] , '''char''' ) , ['''k''', '''s''', '''ɾ''', '''ɾ''', '''|''', '''ɾ''', '''l''', '''|''', '''ɭʲ'''] ) # check that offsets are actually correct for char # 0-1 is 11, 1-4 is 5, 4-6 is first 15, 6-7 is <pad> (thus not shown), 7-9 is second 15, 9-10 is word_delimiter_token, # 10-11 is <pad> (thus not shown), 11-12 is third 15, 12-15 is 8, 15-16 is word_delimiter_token, 16-17 is 98 self.assertListEqual( self.get_from_offsets(outputs['''char_offsets'''] , '''start_offset''' ) , [0, 1, 4, 7, 9, 11, 12, 15, 16] ) self.assertListEqual( self.get_from_offsets(outputs['''char_offsets'''] , '''end_offset''' ) , [1, 4, 6, 9, 10, 12, 15, 16, 17] ) def _snake_case ( self : Any ) -> List[Any]: '''simple docstring''' A: Optional[int] = self.get_tokenizer(word_delimiter_token='''|''' ) def check_list_tuples_equal(SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any] ): self.assertTrue(isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) self.assertTrue(isinstance(outputs_list[0] , SCREAMING_SNAKE_CASE_ ) ) # transform list to ModelOutput A: Dict = WavaVecaPhonemeCTCTokenizerOutput( {k: [d[k] for d in outputs_list] for k in outputs_list[0]} ) self.assertListEqual(outputs_batch['''text'''] , outputs_batch_a['''text'''] ) def recursive_check(SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[str] ): if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): [recursive_check(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for la, la in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )] self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if "char_offsets" in outputs_batch: recursive_check(outputs_batch['''char_offsets'''] , outputs_batch_a['''char_offsets'''] ) # fmt: off A: int = [ [11, 5, 15, tokenizer.pad_token_id, 15, 4, 8, 98, 32, 32, 32, 32, 4, 33, tokenizer.word_delimiter_token_id, 32, 32, 33, 34, 34], [24, 22, 5, tokenizer.word_delimiter_token_id, tokenizer.word_delimiter_token_id, 24, 22, 22, 22, 4, 5, 77, tokenizer.pad_token_id, 22, 22, 4, 34, 34, 34, 34], ] # fmt: on # We assume that `decode` works as expected. All we will check now is # the output type is correct and the output is identical to `decode` # char A: List[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ , output_char_offsets=SCREAMING_SNAKE_CASE_ ) A: List[Any] = [tokenizer.decode(SCREAMING_SNAKE_CASE_ , output_char_offsets=SCREAMING_SNAKE_CASE_ ) for ids in sample_ids] check_list_tuples_equal(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @unittest.skip('''Wav2Vec2PhonemeTokenizer always lower cases letters to correctly map to phonemes''' ) def _snake_case ( self : int ) -> int: '''simple docstring''' pass @unittest.skip('''Wav2Vec2PhonemeTokenizer always puts spaces between phonemes''' ) def _snake_case ( self : str ) -> Any: '''simple docstring''' pass @unittest.skip('''encodes to text to ids, but decodes ids to phonemes -> not possible to have internal consistency''' ) def _snake_case ( self : List[str] ) -> List[str]: '''simple docstring''' pass @unittest.skip('''Wav2Vec2PhonemeModel has no max model length => no testing''' ) def _snake_case ( self : Dict ) -> List[Any]: '''simple docstring''' pass def _snake_case ( self : Tuple ) -> Any: '''simple docstring''' A: Any = self.get_tokenizers(do_lower_case=SCREAMING_SNAKE_CASE_ ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): A: str = tokenizer.vocab_size A: str = len(SCREAMING_SNAKE_CASE_ ) self.assertNotEqual(SCREAMING_SNAKE_CASE_ , 0 ) # We usually have added tokens from the start in tests because our vocab fixtures are # smaller than the original vocabs - let's not assert this # self.assertEqual(vocab_size, all_size) A: List[Any] = ['''aaaaa bbbbbb''', '''cccccccccdddddddd'''] A: List[Any] = tokenizer.add_tokens(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = tokenizer.vocab_size A: Union[str, Any] = len(SCREAMING_SNAKE_CASE_ ) self.assertNotEqual(SCREAMING_SNAKE_CASE_ , 0 ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , len(SCREAMING_SNAKE_CASE_ ) ) self.assertEqual(SCREAMING_SNAKE_CASE_ , all_size + len(SCREAMING_SNAKE_CASE_ ) ) A: Any = tokenizer.encode('''aaaaa bbbbbb low cccccccccdddddddd l''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertGreaterEqual(len(SCREAMING_SNAKE_CASE_ ) , 4 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) A: str = {'''eos_token''': '''>>>>|||<||<<|<<''', '''pad_token''': '''<<<<<|||>|>>>>|>'''} A: int = tokenizer.add_special_tokens(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = tokenizer.vocab_size A: Optional[Any] = len(SCREAMING_SNAKE_CASE_ ) self.assertNotEqual(SCREAMING_SNAKE_CASE_ , 0 ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , len(SCREAMING_SNAKE_CASE_ ) ) self.assertEqual(SCREAMING_SNAKE_CASE_ , all_size_a + len(SCREAMING_SNAKE_CASE_ ) ) A: int = tokenizer.encode( '''>>>>|||<||<<|<< aaaaabbbbbb low cccccccccdddddddd <<<<<|||>|>>>>|> l''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertGreaterEqual(len(SCREAMING_SNAKE_CASE_ ) , 6 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[0] , tokens[1] ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokens[-4] ) self.assertEqual(tokens[0] , tokenizer.eos_token_id ) self.assertEqual(tokens[-3] , tokenizer.pad_token_id ) @unittest.skip('''The tokenizer shouldn\'t be used to encode input IDs (except for labels), only to decode.''' ) def _snake_case ( self : List[Any] ) -> Optional[Any]: '''simple docstring''' pass @unittest.skip('''The tokenizer shouldn\'t be used to encode input IDs (except for labels), only to decode.''' ) def _snake_case ( self : Tuple ) -> Optional[Any]: '''simple docstring''' pass def _snake_case ( self : str ) -> Tuple: '''simple docstring''' A: List[Any] = self.get_tokenizers(fast=SCREAMING_SNAKE_CASE_ , do_lower_case=SCREAMING_SNAKE_CASE_ ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): A: Union[str, Any] = ['''ð''', '''ɪ''', '''s''', '''ɪ''', '''z''', '''ɐ''', '''t''', '''ɛ''', '''k''', '''s''', '''t'''] A: Union[str, Any] = tokenizer.convert_tokens_to_string(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(output['''text'''] , SCREAMING_SNAKE_CASE_ )
319
1
'''simple docstring''' from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , ) -> list[float]: A , A: int = coefficient_matrix.shape A , A: Any = constant_matrix.shape if rowsa != colsa: A: Dict = F"""Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}""" raise ValueError(__lowercase ) if colsa != 1: A: Any = F"""Constant matrix must be nx1 but received {rowsa}x{colsa}""" raise ValueError(__lowercase ) if rowsa != rowsa: A: Optional[Any] = ( '''Coefficient and constant matrices dimensions must be nxn and nx1 but ''' F"""received {rowsa}x{colsa} and {rowsa}x{colsa}""" ) raise ValueError(__lowercase ) if len(__lowercase ) != rowsa: A: List[Any] = ( '''Number of initial values must be equal to number of rows in coefficient ''' F"""matrix but received {len(__lowercase )} and {rowsa}""" ) raise ValueError(__lowercase ) if iterations <= 0: raise ValueError('''Iterations must be at least 1''' ) A: NDArray[floataa] = np.concatenate( (coefficient_matrix, constant_matrix) , axis=1 ) A , A: Optional[Any] = table.shape strictly_diagonally_dominant(__lowercase ) # Iterates the whole matrix for given number of times for _ in range(__lowercase ): A: str = [] for row in range(__lowercase ): A: Optional[int] = 0 for col in range(__lowercase ): if col == row: A: Dict = table[row][col] elif col == cols - 1: A: str = table[row][col] else: temp += (-1) * table[row][col] * init_val[col] A: str = (temp + val) / denom new_val.append(__lowercase ) A: Optional[Any] = new_val return [float(__lowercase ) for i in new_val] def SCREAMING_SNAKE_CASE( __lowercase ) -> bool: A , A: List[Any] = table.shape A: Optional[int] = True for i in range(0 , __lowercase ): A: Union[str, Any] = 0 for j in range(0 , cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError('''Coefficient matrix is not strictly diagonally dominant''' ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
319
'''simple docstring''' import warnings from ...utils import logging from .image_processing_beit import BeitImageProcessor UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> None: '''simple docstring''' warnings.warn( '''The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use BeitImageProcessor instead.''' , SCREAMING_SNAKE_CASE_ , ) super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ )
319
1
'''simple docstring''' import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: A: Union[str, Any] = tmp_path / '''file.csv''' A: Any = textwrap.dedent( '''\ header1,header2 1,2 10,20 ''' ) with open(__lowercase , '''w''' ) as f: f.write(__lowercase ) return str(__lowercase ) @pytest.fixture def SCREAMING_SNAKE_CASE( __lowercase ) -> List[str]: A: Tuple = tmp_path / '''malformed_file.csv''' A: Dict = textwrap.dedent( '''\ header1,header2 1,2 10,20, ''' ) with open(__lowercase , '''w''' ) as f: f.write(__lowercase ) return str(__lowercase ) @pytest.fixture def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[Any]: A: List[str] = tmp_path / '''csv_with_image.csv''' A: Optional[int] = textwrap.dedent( F"""\ image {image_file} """ ) with open(__lowercase , '''w''' ) as f: f.write(__lowercase ) return str(__lowercase ) @pytest.fixture def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: A: Union[str, Any] = tmp_path / '''csv_with_label.csv''' A: Any = textwrap.dedent( '''\ label good bad good ''' ) with open(__lowercase , '''w''' ) as f: f.write(__lowercase ) return str(__lowercase ) @pytest.fixture def SCREAMING_SNAKE_CASE( __lowercase ) -> List[str]: A: List[str] = tmp_path / '''csv_with_int_list.csv''' A: int = textwrap.dedent( '''\ int_list 1 2 3 4 5 6 7 8 9 ''' ) with open(__lowercase , '''w''' ) as f: f.write(__lowercase ) return str(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: A: Union[str, Any] = Csv() A: List[str] = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(__lowercase , match='''Error tokenizing data''' ): for _ in generator: pass assert any( record.levelname == '''ERROR''' and '''Failed to read file''' in record.message and os.path.basename(__lowercase ) in record.message for record in caplog.records ) @require_pil def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: with open(__lowercase , encoding='''utf-8''' ) as f: A: Tuple = f.read().splitlines()[1] A: List[str] = Csv(encoding='''utf-8''' , features=Features({'''image''': Image()} ) ) A: Union[str, Any] = csv._generate_tables([[csv_file_with_image]] ) A: Any = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''image''' ).type == Image()() A: str = pa_table.to_pydict()['''image'''] assert generated_content == [{"path": image_file, "bytes": None}] def SCREAMING_SNAKE_CASE( __lowercase ) -> str: with open(__lowercase , encoding='''utf-8''' ) as f: A: Dict = f.read().splitlines()[1:] A: Any = Csv(encoding='''utf-8''' , features=Features({'''label''': ClassLabel(names=['''good''', '''bad'''] )} ) ) A: List[str] = csv._generate_tables([[csv_file_with_label]] ) A: List[str] = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''label''' ).type == ClassLabel(names=['''good''', '''bad'''] )() A: List[str] = pa_table.to_pydict()['''label'''] assert generated_content == [ClassLabel(names=['''good''', '''bad'''] ).straint(__lowercase ) for label in labels] def SCREAMING_SNAKE_CASE( __lowercase ) -> Any: A: Optional[int] = Csv(encoding='''utf-8''' , sep=''',''' , converters={'''int_list''': lambda __lowercase : [int(__lowercase ) for i in x.split()]} ) A: Dict = csv._generate_tables([[csv_file_with_int_list]] ) A: List[str] = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field('''int_list''' ).type ) A: Dict = pa_table.to_pydict()['''int_list'''] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
319
'''simple docstring''' import os import pytest from transformers.dynamic_module_utils import get_imports UpperCamelCase = ''' import os ''' UpperCamelCase = ''' def foo(): import os return False ''' UpperCamelCase = ''' def foo(): def bar(): if True: import os return False return bar() ''' UpperCamelCase = ''' import os try: import bar except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os def foo(): try: import bar except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except (ImportError, AttributeError): raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except ImportError as e: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar except: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar import baz except ImportError: raise ValueError() ''' UpperCamelCase = ''' import os try: import bar import baz except ImportError: x = 1 raise ValueError() ''' UpperCamelCase = [ TOP_LEVEL_IMPORT, IMPORT_IN_FUNCTION, DEEPLY_NESTED_IMPORT, TOP_LEVEL_TRY_IMPORT, GENERIC_EXCEPT_IMPORT, MULTILINE_TRY_IMPORT, MULTILINE_BOTH_IMPORT, MULTIPLE_EXCEPTS_IMPORT, EXCEPT_AS_IMPORT, TRY_IMPORT_IN_FUNCTION, ] @pytest.mark.parametrize('''case''' , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Dict: A: Tuple = os.path.join(__lowercase , '''test_file.py''' ) with open(__lowercase , '''w''' ) as _tmp_file: _tmp_file.write(__lowercase ) A: List[Any] = get_imports(__lowercase ) assert parsed_imports == ["os"]
319
1
'''simple docstring''' import math import unittest def SCREAMING_SNAKE_CASE( __lowercase ) -> bool: assert isinstance(__lowercase , __lowercase ) and ( number >= 0 ), "'number' must been an int and positive" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(__lowercase ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : List[Any] ) -> Any: '''simple docstring''' self.assertTrue(is_prime(2 ) ) self.assertTrue(is_prime(3 ) ) self.assertTrue(is_prime(5 ) ) self.assertTrue(is_prime(7 ) ) self.assertTrue(is_prime(11 ) ) self.assertTrue(is_prime(13 ) ) self.assertTrue(is_prime(17 ) ) self.assertTrue(is_prime(19 ) ) self.assertTrue(is_prime(23 ) ) self.assertTrue(is_prime(29 ) ) def _snake_case ( self : Dict ) -> Optional[Any]: '''simple docstring''' with self.assertRaises(SCREAMING_SNAKE_CASE_ ): is_prime(-19 ) self.assertFalse( is_prime(0 ) , '''Zero doesn\'t have any positive factors, primes must have exactly two.''' , ) self.assertFalse( is_prime(1 ) , '''One only has 1 positive factor, primes must have exactly two.''' , ) self.assertFalse(is_prime(2 * 2 ) ) self.assertFalse(is_prime(2 * 3 ) ) self.assertFalse(is_prime(3 * 3 ) ) self.assertFalse(is_prime(3 * 5 ) ) self.assertFalse(is_prime(3 * 5 * 7 ) ) if __name__ == "__main__": unittest.main()
319
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( BertTokenizer, ViltConfig, ViltForImageAndTextRetrieval, ViltForImagesAndTextClassification, ViltForMaskedLM, ViltForQuestionAnswering, ViltImageProcessor, ViltProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=False , __lowercase=False , __lowercase=False ) -> Optional[Any]: A: str = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""transformer.blocks.{i}.norm1.weight""", F"""vilt.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.norm1.bias""", F"""vilt.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append( (F"""transformer.blocks.{i}.attn.proj.weight""", F"""vilt.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append( (F"""transformer.blocks.{i}.attn.proj.bias""", F"""vilt.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""transformer.blocks.{i}.norm2.weight""", F"""vilt.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.norm2.bias""", F"""vilt.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append( (F"""transformer.blocks.{i}.mlp.fc1.weight""", F"""vilt.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc1.bias""", F"""vilt.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc2.weight""", F"""vilt.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""transformer.blocks.{i}.mlp.fc2.bias""", F"""vilt.encoder.layer.{i}.output.dense.bias""") ) # embeddings rename_keys.extend( [ # text embeddings ('''text_embeddings.word_embeddings.weight''', '''vilt.embeddings.text_embeddings.word_embeddings.weight'''), ( '''text_embeddings.position_embeddings.weight''', '''vilt.embeddings.text_embeddings.position_embeddings.weight''', ), ('''text_embeddings.position_ids''', '''vilt.embeddings.text_embeddings.position_ids'''), ( '''text_embeddings.token_type_embeddings.weight''', '''vilt.embeddings.text_embeddings.token_type_embeddings.weight''', ), ('''text_embeddings.LayerNorm.weight''', '''vilt.embeddings.text_embeddings.LayerNorm.weight'''), ('''text_embeddings.LayerNorm.bias''', '''vilt.embeddings.text_embeddings.LayerNorm.bias'''), # patch embeddings ('''transformer.cls_token''', '''vilt.embeddings.cls_token'''), ('''transformer.patch_embed.proj.weight''', '''vilt.embeddings.patch_embeddings.projection.weight'''), ('''transformer.patch_embed.proj.bias''', '''vilt.embeddings.patch_embeddings.projection.bias'''), ('''transformer.pos_embed''', '''vilt.embeddings.position_embeddings'''), # token type embeddings ('''token_type_embeddings.weight''', '''vilt.embeddings.token_type_embeddings.weight'''), ] ) # final layernorm + pooler rename_keys.extend( [ ('''transformer.norm.weight''', '''vilt.layernorm.weight'''), ('''transformer.norm.bias''', '''vilt.layernorm.bias'''), ('''pooler.dense.weight''', '''vilt.pooler.dense.weight'''), ('''pooler.dense.bias''', '''vilt.pooler.dense.bias'''), ] ) # classifier head(s) if vqa_model: # classification head rename_keys.extend( [ ('''vqa_classifier.0.weight''', '''classifier.0.weight'''), ('''vqa_classifier.0.bias''', '''classifier.0.bias'''), ('''vqa_classifier.1.weight''', '''classifier.1.weight'''), ('''vqa_classifier.1.bias''', '''classifier.1.bias'''), ('''vqa_classifier.3.weight''', '''classifier.3.weight'''), ('''vqa_classifier.3.bias''', '''classifier.3.bias'''), ] ) elif nlvr_model: # classification head rename_keys.extend( [ ('''nlvr2_classifier.0.weight''', '''classifier.0.weight'''), ('''nlvr2_classifier.0.bias''', '''classifier.0.bias'''), ('''nlvr2_classifier.1.weight''', '''classifier.1.weight'''), ('''nlvr2_classifier.1.bias''', '''classifier.1.bias'''), ('''nlvr2_classifier.3.weight''', '''classifier.3.weight'''), ('''nlvr2_classifier.3.bias''', '''classifier.3.bias'''), ] ) else: pass return rename_keys def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Any: for i in range(config.num_hidden_layers ): A: Tuple = '''vilt.''' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A: List[str] = state_dict.pop(F"""transformer.blocks.{i}.attn.qkv.weight""" ) A: Optional[Any] = state_dict.pop(F"""transformer.blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict A: Dict = in_proj_weight[ : config.hidden_size, : ] A: int = in_proj_bias[: config.hidden_size] A: Any = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A: int = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A: Optional[int] = in_proj_weight[ -config.hidden_size :, : ] A: Optional[Any] = in_proj_bias[-config.hidden_size :] def SCREAMING_SNAKE_CASE( __lowercase ) -> int: A: Optional[int] = ['''head.weight''', '''head.bias'''] for k in ignore_keys: state_dict.pop(__lowercase , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: A: List[Any] = dct.pop(__lowercase ) A: int = val @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str: A: Optional[Any] = ViltConfig(image_size=3_8_4 , patch_size=3_2 , tie_word_embeddings=__lowercase ) A: Tuple = False A: str = False A: List[Any] = False A: Optional[int] = False if "vqa" in checkpoint_url: A: Union[str, Any] = True A: Union[str, Any] = 3_1_2_9 A: List[Any] = '''huggingface/label-files''' A: Any = '''vqa2-id2label.json''' A: Optional[Any] = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: Union[str, Any] = {int(__lowercase ): v for k, v in idalabel.items()} A: Any = idalabel A: Optional[Any] = {v: k for k, v in idalabel.items()} A: List[str] = ViltForQuestionAnswering(__lowercase ) elif "nlvr" in checkpoint_url: A: Dict = True A: str = 2 A: Union[str, Any] = {0: '''False''', 1: '''True'''} A: Any = {v: k for k, v in config.idalabel.items()} A: Optional[Any] = 3 A: Any = ViltForImagesAndTextClassification(__lowercase ) elif "irtr" in checkpoint_url: A: Tuple = True A: Optional[Any] = ViltForImageAndTextRetrieval(__lowercase ) elif "mlm_itm" in checkpoint_url: A: Tuple = True A: Optional[int] = ViltForMaskedLM(__lowercase ) else: raise ValueError('''Unknown model type''' ) # load state_dict of original model, remove and rename some keys A: int = torch.hub.load_state_dict_from_url(__lowercase , map_location='''cpu''' )['''state_dict'''] A: List[str] = create_rename_keys(__lowercase , __lowercase , __lowercase , __lowercase ) for src, dest in rename_keys: rename_key(__lowercase , __lowercase , __lowercase ) read_in_q_k_v(__lowercase , __lowercase ) if mlm_model or irtr_model: A: str = ['''itm_score.fc.weight''', '''itm_score.fc.bias'''] for k in ignore_keys: state_dict.pop(__lowercase , __lowercase ) # load state dict into HuggingFace model model.eval() if mlm_model: A , A: Union[str, Any] = model.load_state_dict(__lowercase , strict=__lowercase ) assert missing_keys == ["mlm_score.decoder.bias"] else: model.load_state_dict(__lowercase ) # Define processor A: Optional[Any] = ViltImageProcessor(size=3_8_4 ) A: Dict = BertTokenizer.from_pretrained('''bert-base-uncased''' ) A: Optional[int] = ViltProcessor(__lowercase , __lowercase ) # Forward pass on example inputs (image + text) if nlvr_model: A: str = Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg''' , stream=__lowercase ).raw ) A: List[str] = Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg''' , stream=__lowercase ).raw ) A: Any = ( '''The left image contains twice the number of dogs as the right image, and at least two dogs in total are''' ''' standing.''' ) A: List[Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: List[Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: List[str] = model( input_ids=encoding_a.input_ids , pixel_values=encoding_a.pixel_values , pixel_values_a=encoding_a.pixel_values , ) else: A: Any = Image.open(requests.get('''http://images.cocodataset.org/val2017/000000039769.jpg''' , stream=__lowercase ).raw ) if mlm_model: A: Optional[int] = '''a bunch of [MASK] laying on a [MASK].''' else: A: Optional[int] = '''How many cats are there?''' A: Union[str, Any] = processor(__lowercase , __lowercase , return_tensors='''pt''' ) A: Any = model(**__lowercase ) # Verify outputs if mlm_model: A: Any = torch.Size([1, 1_1, 3_0_5_2_2] ) A: Tuple = torch.tensor([-1_2.5_0_6_1, -1_2.5_1_2_3, -1_2.5_1_7_4] ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , __lowercase , atol=1E-4 ) # verify masked token prediction equals "cats" A: List[str] = outputs.logits[0, 4, :].argmax(-1 ).item() assert tokenizer.decode([predicted_id] ) == "cats" elif vqa_model: A: Any = torch.Size([1, 3_1_2_9] ) A: Optional[int] = torch.tensor([-1_5.9_4_9_5, -1_8.1_4_7_2, -1_0.3_0_4_1] ) assert torch.allclose(outputs.logits[0, :3] , __lowercase , atol=1E-4 ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , __lowercase , atol=1E-4 ) # verify vqa prediction equals "2" A: Dict = outputs.logits.argmax(-1 ).item() assert model.config.idalabel[predicted_idx] == "2" elif nlvr_model: A: Union[str, Any] = torch.Size([1, 2] ) A: Optional[Any] = torch.tensor([-2.8_7_2_1, 2.1_2_9_1] ) assert torch.allclose(outputs.logits[0, :3] , __lowercase , atol=1E-4 ) assert outputs.logits.shape == expected_shape Path(__lowercase ).mkdir(exist_ok=__lowercase ) print(F"""Saving model and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(__lowercase ) processor.save_pretrained(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://github.com/dandelin/ViLT/releases/download/200k/vilt_200k_mlm_itm.ckpt''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) UpperCamelCase = parser.parse_args() convert_vilt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
319
1
'''simple docstring''' from typing import List, Optional, Union import torch from transformers import ( XLMRobertaTokenizer, ) from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) from .text_encoder import MultilingualCLIP UpperCamelCase = logging.get_logger(__name__) # pylint: disable=invalid-name UpperCamelCase = ''' Examples: ```py >>> from diffusers import KandinskyPipeline, KandinskyPriorPipeline >>> import torch >>> pipe_prior = KandinskyPriorPipeline.from_pretrained("kandinsky-community/Kandinsky-2-1-prior") >>> pipe_prior.to("cuda") >>> prompt = "red cat, 4k photo" >>> out = pipe_prior(prompt) >>> image_emb = out.image_embeds >>> negative_image_emb = out.negative_image_embeds >>> pipe = KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1") >>> pipe.to("cuda") >>> image = pipe( ... prompt, ... image_embeds=image_emb, ... negative_image_embeds=negative_image_emb, ... height=768, ... width=768, ... num_inference_steps=100, ... ).images >>> image[0].save("cat.png") ``` ''' def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=8 ) -> Optional[int]: A: str = h // scale_factor**2 if h % scale_factor**2 != 0: new_h += 1 A: List[str] = w // scale_factor**2 if w % scale_factor**2 != 0: new_w += 1 return new_h * scale_factor, new_w * scale_factor class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : MultilingualCLIP , SCREAMING_SNAKE_CASE_ : XLMRobertaTokenizer , SCREAMING_SNAKE_CASE_ : UNetaDConditionModel , SCREAMING_SNAKE_CASE_ : Union[DDIMScheduler, DDPMScheduler] , SCREAMING_SNAKE_CASE_ : VQModel , ) -> Dict: '''simple docstring''' super().__init__() self.register_modules( text_encoder=SCREAMING_SNAKE_CASE_ , tokenizer=SCREAMING_SNAKE_CASE_ , unet=SCREAMING_SNAKE_CASE_ , scheduler=SCREAMING_SNAKE_CASE_ , movq=SCREAMING_SNAKE_CASE_ , ) A: Union[str, Any] = 2 ** (len(self.movq.config.block_out_channels ) - 1) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Tuple ) -> int: '''simple docstring''' if latents is None: A: Optional[int] = randn_tensor(SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ , device=SCREAMING_SNAKE_CASE_ , dtype=SCREAMING_SNAKE_CASE_ ) else: if latents.shape != shape: raise ValueError(f"""Unexpected latents shape, got {latents.shape}, expected {shape}""" ) A: Union[str, Any] = latents.to(SCREAMING_SNAKE_CASE_ ) A: List[str] = latents * scheduler.init_noise_sigma return latents def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : List[str]=None , ) -> List[Any]: '''simple docstring''' A: Union[str, Any] = len(SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else 1 # get prompt text embeddings A: str = self.tokenizer( SCREAMING_SNAKE_CASE_ , padding='''max_length''' , truncation=SCREAMING_SNAKE_CASE_ , max_length=77 , return_attention_mask=SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' , ) A: Optional[Any] = text_inputs.input_ids A: List[Any] = self.tokenizer(SCREAMING_SNAKE_CASE_ , padding='''longest''' , return_tensors='''pt''' ).input_ids if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): A: int = self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1] ) logger.warning( '''The following part of your input was truncated because CLIP can only handle sequences up to''' f""" {self.tokenizer.model_max_length} tokens: {removed_text}""" ) A: List[str] = text_input_ids.to(SCREAMING_SNAKE_CASE_ ) A: List[str] = text_inputs.attention_mask.to(SCREAMING_SNAKE_CASE_ ) A , A: Dict = self.text_encoder( input_ids=SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ ) A: Optional[int] = prompt_embeds.repeat_interleave(SCREAMING_SNAKE_CASE_ , dim=0 ) A: List[str] = text_encoder_hidden_states.repeat_interleave(SCREAMING_SNAKE_CASE_ , dim=0 ) A: Optional[int] = text_mask.repeat_interleave(SCREAMING_SNAKE_CASE_ , dim=0 ) if do_classifier_free_guidance: A: List[str] if negative_prompt is None: A: Union[str, Any] = [''''''] * batch_size elif type(SCREAMING_SNAKE_CASE_ ) is not type(SCREAMING_SNAKE_CASE_ ): raise TypeError( f"""`negative_prompt` should be the same type to `prompt`, but got {type(SCREAMING_SNAKE_CASE_ )} !=""" f""" {type(SCREAMING_SNAKE_CASE_ )}.""" ) elif isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): A: int = [negative_prompt] elif batch_size != len(SCREAMING_SNAKE_CASE_ ): raise ValueError( f"""`negative_prompt`: {negative_prompt} has batch size {len(SCREAMING_SNAKE_CASE_ )}, but `prompt`:""" f""" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches""" ''' the batch size of `prompt`.''' ) else: A: Tuple = negative_prompt A: List[Any] = self.tokenizer( SCREAMING_SNAKE_CASE_ , padding='''max_length''' , max_length=77 , truncation=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' , ) A: List[str] = uncond_input.input_ids.to(SCREAMING_SNAKE_CASE_ ) A: List[str] = uncond_input.attention_mask.to(SCREAMING_SNAKE_CASE_ ) A , A: Optional[Any] = self.text_encoder( input_ids=SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method A: str = negative_prompt_embeds.shape[1] A: Any = negative_prompt_embeds.repeat(1 , SCREAMING_SNAKE_CASE_ ) A: Optional[int] = negative_prompt_embeds.view(batch_size * num_images_per_prompt , SCREAMING_SNAKE_CASE_ ) A: int = uncond_text_encoder_hidden_states.shape[1] A: Any = uncond_text_encoder_hidden_states.repeat(1 , SCREAMING_SNAKE_CASE_ , 1 ) A: Optional[Any] = uncond_text_encoder_hidden_states.view( batch_size * num_images_per_prompt , SCREAMING_SNAKE_CASE_ , -1 ) A: List[str] = uncond_text_mask.repeat_interleave(SCREAMING_SNAKE_CASE_ , dim=0 ) # done duplicates # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes A: str = torch.cat([negative_prompt_embeds, prompt_embeds] ) A: Any = torch.cat([uncond_text_encoder_hidden_states, text_encoder_hidden_states] ) A: Dict = torch.cat([uncond_text_mask, text_mask] ) return prompt_embeds, text_encoder_hidden_states, text_mask def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : Any=0 ) -> str: '''simple docstring''' if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('''Please install accelerate via `pip install accelerate`''' ) A: Dict = torch.device(f"""cuda:{gpu_id}""" ) A: str = [ self.unet, self.text_encoder, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Union[str, Any]=0 ) -> str: '''simple docstring''' if is_accelerate_available() and is_accelerate_version('''>=''' , '''0.17.0.dev0''' ): from accelerate import cpu_offload_with_hook else: raise ImportError('''`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.''' ) A: List[str] = torch.device(f"""cuda:{gpu_id}""" ) if self.device.type != "cpu": self.to('''cpu''' , silence_dtype_warnings=SCREAMING_SNAKE_CASE_ ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) A: Optional[int] = None for cpu_offloaded_model in [self.text_encoder, self.unet, self.movq]: A , A: Dict = cpu_offload_with_hook(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , prev_module_hook=SCREAMING_SNAKE_CASE_ ) if self.safety_checker is not None: A , A: List[str] = cpu_offload_with_hook(self.safety_checker , SCREAMING_SNAKE_CASE_ , prev_module_hook=SCREAMING_SNAKE_CASE_ ) # We'll offload the last model manually. A: str = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def _snake_case ( self : List[str] ) -> Tuple: '''simple docstring''' if not hasattr(self.unet , '''_hf_hook''' ): return self.device for module in self.unet.modules(): if ( hasattr(SCREAMING_SNAKE_CASE_ , '''_hf_hook''' ) and hasattr(module._hf_hook , '''execution_device''' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(SCREAMING_SNAKE_CASE_ ) def __call__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : Union[str, List[str]] , SCREAMING_SNAKE_CASE_ : Union[torch.FloatTensor, List[torch.FloatTensor]] , SCREAMING_SNAKE_CASE_ : Union[torch.FloatTensor, List[torch.FloatTensor]] , SCREAMING_SNAKE_CASE_ : Optional[Union[str, List[str]]] = None , SCREAMING_SNAKE_CASE_ : int = 5_12 , SCREAMING_SNAKE_CASE_ : int = 5_12 , SCREAMING_SNAKE_CASE_ : int = 1_00 , SCREAMING_SNAKE_CASE_ : float = 4.0 , SCREAMING_SNAKE_CASE_ : int = 1 , SCREAMING_SNAKE_CASE_ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , SCREAMING_SNAKE_CASE_ : Optional[torch.FloatTensor] = None , SCREAMING_SNAKE_CASE_ : Optional[str] = "pil" , SCREAMING_SNAKE_CASE_ : bool = True , ) -> List[Any]: '''simple docstring''' if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): A: Tuple = 1 elif isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): A: int = len(SCREAMING_SNAKE_CASE_ ) else: raise ValueError(f"""`prompt` has to be of type `str` or `list` but is {type(SCREAMING_SNAKE_CASE_ )}""" ) A: Optional[int] = self._execution_device A: Union[str, Any] = batch_size * num_images_per_prompt A: str = guidance_scale > 1.0 A , A , A: Any = self._encode_prompt( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): A: Union[str, Any] = torch.cat(SCREAMING_SNAKE_CASE_ , dim=0 ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): A: Dict = torch.cat(SCREAMING_SNAKE_CASE_ , dim=0 ) if do_classifier_free_guidance: A: int = image_embeds.repeat_interleave(SCREAMING_SNAKE_CASE_ , dim=0 ) A: int = negative_image_embeds.repeat_interleave(SCREAMING_SNAKE_CASE_ , dim=0 ) A: int = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to( dtype=prompt_embeds.dtype , device=SCREAMING_SNAKE_CASE_ ) self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE_ , device=SCREAMING_SNAKE_CASE_ ) A: Dict = self.scheduler.timesteps A: Tuple = self.unet.config.in_channels A , A: Union[str, Any] = get_new_h_w(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , self.movq_scale_factor ) # create initial latent A: Dict = self.prepare_latents( (batch_size, num_channels_latents, height, width) , text_encoder_hidden_states.dtype , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , self.scheduler , ) for i, t in enumerate(self.progress_bar(SCREAMING_SNAKE_CASE_ ) ): # expand the latents if we are doing classifier free guidance A: str = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents A: Dict = {'''text_embeds''': prompt_embeds, '''image_embeds''': image_embeds} A: int = self.unet( sample=SCREAMING_SNAKE_CASE_ , timestep=SCREAMING_SNAKE_CASE_ , encoder_hidden_states=SCREAMING_SNAKE_CASE_ , added_cond_kwargs=SCREAMING_SNAKE_CASE_ , return_dict=SCREAMING_SNAKE_CASE_ , )[0] if do_classifier_free_guidance: A , A: Any = noise_pred.split(latents.shape[1] , dim=1 ) A , A: List[Any] = noise_pred.chunk(2 ) A , A: Union[str, Any] = variance_pred.chunk(2 ) A: Tuple = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) A: Any = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , '''variance_type''' ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): A , A: str = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 A: List[Any] = self.scheduler.step( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ , ).prev_sample # post-processing A: Any = self.movq.decode(SCREAMING_SNAKE_CASE_ , force_not_quantize=SCREAMING_SNAKE_CASE_ )['''sample'''] if output_type not in ["pt", "np", "pil"]: raise ValueError(f"""Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}""" ) if output_type in ["np", "pil"]: A: List[Any] = image * 0.5 + 0.5 A: Optional[Any] = image.clamp(0 , 1 ) A: List[Any] = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": A: List[Any] = self.numpy_to_pil(SCREAMING_SNAKE_CASE_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=SCREAMING_SNAKE_CASE_ )
319
'''simple docstring''' import argparse import json import os import numpy as np import PIL import requests import tensorflow.keras.applications.efficientnet as efficientnet import torch from huggingface_hub import hf_hub_download from PIL import Image from tensorflow.keras.preprocessing import image from transformers import ( EfficientNetConfig, EfficientNetForImageClassification, EfficientNetImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''b0''': efficientnet.EfficientNetBa, '''b1''': efficientnet.EfficientNetBa, '''b2''': efficientnet.EfficientNetBa, '''b3''': efficientnet.EfficientNetBa, '''b4''': efficientnet.EfficientNetBa, '''b5''': efficientnet.EfficientNetBa, '''b6''': efficientnet.EfficientNetBa, '''b7''': efficientnet.EfficientNetBa, } UpperCamelCase = { '''b0''': { '''hidden_dim''': 1280, '''width_coef''': 1.0, '''depth_coef''': 1.0, '''image_size''': 224, '''dropout_rate''': 0.2, '''dw_padding''': [], }, '''b1''': { '''hidden_dim''': 1280, '''width_coef''': 1.0, '''depth_coef''': 1.1, '''image_size''': 240, '''dropout_rate''': 0.2, '''dw_padding''': [16], }, '''b2''': { '''hidden_dim''': 1408, '''width_coef''': 1.1, '''depth_coef''': 1.2, '''image_size''': 260, '''dropout_rate''': 0.3, '''dw_padding''': [5, 8, 16], }, '''b3''': { '''hidden_dim''': 1536, '''width_coef''': 1.2, '''depth_coef''': 1.4, '''image_size''': 300, '''dropout_rate''': 0.3, '''dw_padding''': [5, 18], }, '''b4''': { '''hidden_dim''': 1792, '''width_coef''': 1.4, '''depth_coef''': 1.8, '''image_size''': 380, '''dropout_rate''': 0.4, '''dw_padding''': [6], }, '''b5''': { '''hidden_dim''': 2048, '''width_coef''': 1.6, '''depth_coef''': 2.2, '''image_size''': 456, '''dropout_rate''': 0.4, '''dw_padding''': [13, 27], }, '''b6''': { '''hidden_dim''': 2304, '''width_coef''': 1.8, '''depth_coef''': 2.6, '''image_size''': 528, '''dropout_rate''': 0.5, '''dw_padding''': [31], }, '''b7''': { '''hidden_dim''': 2560, '''width_coef''': 2.0, '''depth_coef''': 3.1, '''image_size''': 600, '''dropout_rate''': 0.5, '''dw_padding''': [18], }, } def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: A: Tuple = EfficientNetConfig() A: Optional[int] = CONFIG_MAP[model_name]['''hidden_dim'''] A: Optional[int] = CONFIG_MAP[model_name]['''width_coef'''] A: str = CONFIG_MAP[model_name]['''depth_coef'''] A: Dict = CONFIG_MAP[model_name]['''image_size'''] A: str = CONFIG_MAP[model_name]['''dropout_rate'''] A: Optional[Any] = CONFIG_MAP[model_name]['''dw_padding'''] A: Optional[Any] = '''huggingface/label-files''' A: List[str] = '''imagenet-1k-id2label.json''' A: Dict = 1_0_0_0 A: Any = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: Tuple = {int(__lowercase ): v for k, v in idalabel.items()} A: int = idalabel A: Tuple = {v: k for k, v in idalabel.items()} return config def SCREAMING_SNAKE_CASE( ) -> Any: A: Optional[Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' A: Union[str, Any] = Image.open(requests.get(__lowercase , stream=__lowercase ).raw ) return im def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: A: List[str] = CONFIG_MAP[model_name]['''image_size'''] A: List[Any] = EfficientNetImageProcessor( size={'''height''': size, '''width''': size} , image_mean=[0.4_8_5, 0.4_5_6, 0.4_0_6] , image_std=[0.4_7_8_5_3_9_4_4, 0.4_7_3_2_8_6_4, 0.4_7_4_3_4_1_6_3] , do_center_crop=__lowercase , ) return preprocessor def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: A: List[str] = [v.split('''_''' )[0].split('''block''' )[1] for v in original_param_names if v.startswith('''block''' )] A: List[str] = sorted(set(__lowercase ) ) A: Dict = len(__lowercase ) A: List[str] = {b: str(__lowercase ) for b, i in zip(__lowercase , range(__lowercase ) )} A: Optional[int] = [] rename_keys.append(('''stem_conv/kernel:0''', '''embeddings.convolution.weight''') ) rename_keys.append(('''stem_bn/gamma:0''', '''embeddings.batchnorm.weight''') ) rename_keys.append(('''stem_bn/beta:0''', '''embeddings.batchnorm.bias''') ) rename_keys.append(('''stem_bn/moving_mean:0''', '''embeddings.batchnorm.running_mean''') ) rename_keys.append(('''stem_bn/moving_variance:0''', '''embeddings.batchnorm.running_var''') ) for b in block_names: A: int = block_name_mapping[b] rename_keys.append((F"""block{b}_expand_conv/kernel:0""", F"""encoder.blocks.{hf_b}.expansion.expand_conv.weight""") ) rename_keys.append((F"""block{b}_expand_bn/gamma:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.weight""") ) rename_keys.append((F"""block{b}_expand_bn/beta:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.bias""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_mean""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_var""") ) rename_keys.append( (F"""block{b}_dwconv/depthwise_kernel:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight""") ) rename_keys.append((F"""block{b}_bn/gamma:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight""") ) rename_keys.append((F"""block{b}_bn/beta:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias""") ) rename_keys.append( (F"""block{b}_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean""") ) rename_keys.append( (F"""block{b}_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var""") ) rename_keys.append((F"""block{b}_se_reduce/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.weight""") ) rename_keys.append((F"""block{b}_se_reduce/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.bias""") ) rename_keys.append((F"""block{b}_se_expand/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.weight""") ) rename_keys.append((F"""block{b}_se_expand/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.bias""") ) rename_keys.append( (F"""block{b}_project_conv/kernel:0""", F"""encoder.blocks.{hf_b}.projection.project_conv.weight""") ) rename_keys.append((F"""block{b}_project_bn/gamma:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.weight""") ) rename_keys.append((F"""block{b}_project_bn/beta:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.bias""") ) rename_keys.append( (F"""block{b}_project_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_mean""") ) rename_keys.append( (F"""block{b}_project_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_var""") ) rename_keys.append(('''top_conv/kernel:0''', '''encoder.top_conv.weight''') ) rename_keys.append(('''top_bn/gamma:0''', '''encoder.top_bn.weight''') ) rename_keys.append(('''top_bn/beta:0''', '''encoder.top_bn.bias''') ) rename_keys.append(('''top_bn/moving_mean:0''', '''encoder.top_bn.running_mean''') ) rename_keys.append(('''top_bn/moving_variance:0''', '''encoder.top_bn.running_var''') ) A: Union[str, Any] = {} for item in rename_keys: if item[0] in original_param_names: A: str = '''efficientnet.''' + item[1] A: int = '''classifier.weight''' A: Tuple = '''classifier.bias''' return key_mapping def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Tuple: for key, value in tf_params.items(): if "normalization" in key: continue A: Union[str, Any] = key_mapping[key] if "_conv" in key and "kernel" in key: A: List[str] = torch.from_numpy(__lowercase ).permute(3 , 2 , 0 , 1 ) elif "depthwise_kernel" in key: A: List[Any] = torch.from_numpy(__lowercase ).permute(2 , 3 , 0 , 1 ) elif "kernel" in key: A: Optional[Any] = torch.from_numpy(np.transpose(__lowercase ) ) else: A: Any = torch.from_numpy(__lowercase ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(__lowercase ) @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase ) -> Tuple: A: Optional[int] = model_classes[model_name]( include_top=__lowercase , weights='''imagenet''' , input_tensor=__lowercase , input_shape=__lowercase , pooling=__lowercase , classes=1_0_0_0 , classifier_activation='''softmax''' , ) A: List[str] = original_model.trainable_variables A: Optional[Any] = original_model.non_trainable_variables A: Union[str, Any] = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: A: int = param.numpy() A: Tuple = list(tf_params.keys() ) # Load HuggingFace model A: Dict = get_efficientnet_config(__lowercase ) A: Union[str, Any] = EfficientNetForImageClassification(__lowercase ).eval() A: Dict = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print('''Converting parameters...''' ) A: int = rename_keys(__lowercase ) replace_params(__lowercase , __lowercase , __lowercase ) # Initialize preprocessor and preprocess input image A: List[Any] = convert_image_processor(__lowercase ) A: Optional[Any] = preprocessor(images=prepare_img() , return_tensors='''pt''' ) # HF model inference hf_model.eval() with torch.no_grad(): A: str = hf_model(**__lowercase ) A: List[Any] = outputs.logits.detach().numpy() # Original model inference A: Any = False A: List[Any] = CONFIG_MAP[model_name]['''image_size'''] A: List[Any] = prepare_img().resize((image_size, image_size) , resample=PIL.Image.NEAREST ) A: str = image.img_to_array(__lowercase ) A: Dict = np.expand_dims(__lowercase , axis=0 ) A: Any = original_model.predict(__lowercase ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(__lowercase , __lowercase , atol=1E-3 ), "The predicted logits are not the same." print('''Model outputs match!''' ) if save_model: # Create folder to save model if not os.path.isdir(__lowercase ): os.mkdir(__lowercase ) # Save converted model and image processor hf_model.save_pretrained(__lowercase ) preprocessor.save_pretrained(__lowercase ) if push_to_hub: # Push model and image processor to hub print(F"""Pushing converted {model_name} to the hub...""" ) A: int = F"""efficientnet-{model_name}""" preprocessor.push_to_hub(__lowercase ) hf_model.push_to_hub(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''b0''', type=str, help='''Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''hf_model''', type=str, help='''Path to the output PyTorch model directory.''', ) parser.add_argument('''--save_model''', action='''store_true''', help='''Save model to local''') parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Push model and image processor to the hub''') UpperCamelCase = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
319
1
'''simple docstring''' import unittest from transformers import load_tool from .test_tools_common import ToolTesterMixin UpperCamelCase = ''' Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning. In March 2021, Hugging Face raised $40 million in a Series B funding round.[3] On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5] ''' class lowerCAmelCase_ ( unittest.TestCase , UpperCAmelCase_ ): '''simple docstring''' def _snake_case ( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' A: str = load_tool('''text-question-answering''' ) self.tool.setup() A: Union[str, Any] = load_tool('''text-question-answering''' , remote=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Dict ) -> Optional[int]: '''simple docstring''' A: int = self.tool(SCREAMING_SNAKE_CASE_ , '''What did Hugging Face do in April 2021?''' ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''launched the BigScience Research Workshop''' ) def _snake_case ( self : Dict ) -> int: '''simple docstring''' A: int = self.remote_tool(SCREAMING_SNAKE_CASE_ , '''What did Hugging Face do in April 2021?''' ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''launched the BigScience Research Workshop''' ) def _snake_case ( self : str ) -> int: '''simple docstring''' A: Union[str, Any] = self.tool(text=SCREAMING_SNAKE_CASE_ , question='''What did Hugging Face do in April 2021?''' ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''launched the BigScience Research Workshop''' ) def _snake_case ( self : List[Any] ) -> Optional[int]: '''simple docstring''' A: Optional[int] = self.remote_tool(text=SCREAMING_SNAKE_CASE_ , question='''What did Hugging Face do in April 2021?''' ) self.assertEqual(SCREAMING_SNAKE_CASE_ , '''launched the BigScience Research Workshop''' )
319
'''simple docstring''' from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase = {'''configuration_focalnet''': ['''FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FocalNetConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FocalNetForImageClassification''', '''FocalNetForMaskedImageModeling''', '''FocalNetBackbone''', '''FocalNetModel''', '''FocalNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_focalnet import FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FocalNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_focalnet import ( FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST, FocalNetBackbone, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetModel, FocalNetPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
1
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> str: A: Optional[Any] = 0 # if input_string is "aba" than new_input_string become "a|b|a" A: Tuple = '''''' A: Optional[Any] = '''''' # append each character + "|" in new_string for range(0, length-1) for i in input_string[: len(__lowercase ) - 1]: new_input_string += i + "|" # append last character new_input_string += input_string[-1] # we will store the starting and ending of previous furthest ending palindromic # substring A , A: List[Any] = 0, 0 # length[i] shows the length of palindromic substring with center i A: Any = [1 for i in range(len(__lowercase ) )] # for each character in new_string find corresponding palindromic string A: Optional[int] = 0 for j in range(len(__lowercase ) ): A: Tuple = 1 if j > r else min(length[l + r - j] // 2 , r - j + 1 ) while ( j - k >= 0 and j + k < len(__lowercase ) and new_input_string[k + j] == new_input_string[j - k] ): k += 1 A: int = 2 * k - 1 # does this string is ending after the previously explored end (that is r) ? # if yes the update the new r to the last index of this if j + k - 1 > r: A: Optional[Any] = j - k + 1 # noqa: E741 A: Tuple = j + k - 1 # update max_length and start position if max_length < length[j]: A: Union[str, Any] = length[j] A: List[str] = j # create that string A: Optional[int] = new_input_string[start - max_length // 2 : start + max_length // 2 + 1] for i in s: if i != "|": output_string += i return output_string if __name__ == "__main__": import doctest doctest.testmod()
319
'''simple docstring''' from typing import Any, Dict, List, Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from ..image_utils import load_image if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_OBJECT_DETECTION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = Dict[str, Any] UpperCamelCase = List[Prediction] @add_end_docstrings(UpperCAmelCase_ ) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : List[str] ) -> int: '''simple docstring''' super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if self.framework == "tf": raise ValueError(f"""The {self.__class__} is only available in PyTorch.""" ) requires_backends(self , '''vision''' ) self.check_model_type( dict(MODEL_FOR_OBJECT_DETECTION_MAPPING.items() + MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.items() ) ) def _snake_case ( self : int , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[Any]: '''simple docstring''' A: Any = {} if "threshold" in kwargs: A: List[Any] = kwargs['''threshold'''] return {}, {}, postprocess_kwargs def __call__( self : str , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Union[Predictions, List[Prediction]]: '''simple docstring''' return super().__call__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Union[str, Any]: '''simple docstring''' A: int = load_image(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = torch.IntTensor([[image.height, image.width]] ) A: Union[str, Any] = self.image_processor(images=[image] , return_tensors='''pt''' ) if self.tokenizer is not None: A: int = self.tokenizer(text=inputs['''words'''] , boxes=inputs['''boxes'''] , return_tensors='''pt''' ) A: Any = target_size return inputs def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : str ) -> List[Any]: '''simple docstring''' A: Tuple = model_inputs.pop('''target_size''' ) A: Tuple = self.model(**SCREAMING_SNAKE_CASE_ ) A: List[str] = outputs.__class__({'''target_size''': target_size, **outputs} ) if self.tokenizer is not None: A: Dict = model_inputs['''bbox'''] return model_outputs def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : str=0.9 ) -> Union[str, Any]: '''simple docstring''' A: List[Any] = model_outputs['''target_size'''] if self.tokenizer is not None: # This is a LayoutLMForTokenClassification variant. # The OCR got the boxes and the model classified the words. A , A: Union[str, Any] = target_size[0].tolist() def unnormalize(SCREAMING_SNAKE_CASE_ : str ): return self._get_bounding_box( torch.Tensor( [ (width * bbox[0] / 10_00), (height * bbox[1] / 10_00), (width * bbox[2] / 10_00), (height * bbox[3] / 10_00), ] ) ) A , A: Dict = model_outputs['''logits'''].squeeze(0 ).softmax(dim=-1 ).max(dim=-1 ) A: List[str] = [self.model.config.idalabel[prediction] for prediction in classes.tolist()] A: List[str] = [unnormalize(SCREAMING_SNAKE_CASE_ ) for bbox in model_outputs['''bbox'''].squeeze(0 )] A: Dict = ['''score''', '''label''', '''box'''] A: Optional[int] = [dict(zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) for vals in zip(scores.tolist() , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if vals[0] > threshold] else: # This is a regular ForObjectDetectionModel A: Any = self.image_processor.post_process_object_detection(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: List[str] = raw_annotations[0] A: List[Any] = raw_annotation['''scores'''] A: List[Any] = raw_annotation['''labels'''] A: int = raw_annotation['''boxes'''] A: Any = scores.tolist() A: List[Any] = [self.model.config.idalabel[label.item()] for label in labels] A: List[Any] = [self._get_bounding_box(SCREAMING_SNAKE_CASE_ ) for box in boxes] # {"scores": [...], ...} --> [{"score":x, ...}, ...] A: Tuple = ['''score''', '''label''', '''box'''] A: str = [ dict(zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) for vals in zip(raw_annotation['''scores'''] , raw_annotation['''labels'''] , raw_annotation['''boxes'''] ) ] return annotation def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : "torch.Tensor" ) -> Dict[str, int]: '''simple docstring''' if self.framework != "pt": raise ValueError('''The ObjectDetectionPipeline is only available in PyTorch.''' ) A , A , A , A: str = box.int().tolist() A: str = { '''xmin''': xmin, '''ymin''': ymin, '''xmax''': xmax, '''ymax''': ymax, } return bbox
319
1
'''simple docstring''' from itertools import permutations def SCREAMING_SNAKE_CASE( __lowercase ) -> bool: if num[3] % 2 != 0: return False if (num[2] + num[3] + num[4]) % 3 != 0: return False if num[5] % 5 != 0: return False A: int = [7, 1_1, 1_3, 1_7] for i, test in enumerate(__lowercase ): if (num[i + 4] * 1_0_0 + num[i + 5] * 1_0 + num[i + 6]) % test != 0: return False return True def SCREAMING_SNAKE_CASE( __lowercase = 1_0 ) -> int: return sum( int(''''''.join(map(__lowercase , __lowercase ) ) ) for num in permutations(range(__lowercase ) ) if is_substring_divisible(__lowercase ) ) if __name__ == "__main__": print(f'{solution() = }')
319
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''YituTech/conv-bert-base''': '''https://huggingface.co/YituTech/conv-bert-base/resolve/main/config.json''', '''YituTech/conv-bert-medium-small''': ( '''https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/config.json''' ), '''YituTech/conv-bert-small''': '''https://huggingface.co/YituTech/conv-bert-small/resolve/main/config.json''', # See all ConvBERT models at https://huggingface.co/models?filter=convbert } class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = """convbert""" def __init__( self : Dict , SCREAMING_SNAKE_CASE_ : Dict=3_05_22 , SCREAMING_SNAKE_CASE_ : int=7_68 , SCREAMING_SNAKE_CASE_ : List[str]=12 , SCREAMING_SNAKE_CASE_ : List[str]=12 , SCREAMING_SNAKE_CASE_ : Dict=30_72 , SCREAMING_SNAKE_CASE_ : Optional[int]="gelu" , SCREAMING_SNAKE_CASE_ : List[Any]=0.1 , SCREAMING_SNAKE_CASE_ : int=0.1 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=5_12 , SCREAMING_SNAKE_CASE_ : List[Any]=2 , SCREAMING_SNAKE_CASE_ : List[str]=0.02 , SCREAMING_SNAKE_CASE_ : int=1E-12 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=1 , SCREAMING_SNAKE_CASE_ : int=0 , SCREAMING_SNAKE_CASE_ : str=2 , SCREAMING_SNAKE_CASE_ : List[Any]=7_68 , SCREAMING_SNAKE_CASE_ : Optional[Any]=2 , SCREAMING_SNAKE_CASE_ : Any=9 , SCREAMING_SNAKE_CASE_ : Tuple=1 , SCREAMING_SNAKE_CASE_ : List[Any]=None , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> List[Any]: '''simple docstring''' super().__init__( pad_token_id=SCREAMING_SNAKE_CASE_ , bos_token_id=SCREAMING_SNAKE_CASE_ , eos_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Dict = vocab_size A: Tuple = hidden_size A: Optional[int] = num_hidden_layers A: List[str] = num_attention_heads A: int = intermediate_size A: int = hidden_act A: List[str] = hidden_dropout_prob A: int = attention_probs_dropout_prob A: Tuple = max_position_embeddings A: Any = type_vocab_size A: str = initializer_range A: Union[str, Any] = layer_norm_eps A: str = embedding_size A: Optional[int] = head_ratio A: List[Any] = conv_kernel_size A: List[Any] = num_groups A: Optional[int] = classifier_dropout class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' @property def _snake_case ( self : Optional[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": A: Tuple = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: A: List[str] = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis), ] )
319
1
'''simple docstring''' import argparse import os import sys from unittest.mock import patch import pytorch_lightning as pl import timeout_decorator import torch from distillation import SummarizationDistiller, distill_main from finetune import SummarizationModule, main from transformers import MarianMTModel from transformers.file_utils import cached_path from transformers.testing_utils import TestCasePlus, require_torch_gpu, slow from utils import load_json UpperCamelCase = '''sshleifer/mar_enro_6_3_student''' class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def _snake_case ( self : Union[str, Any] ) -> Tuple: '''simple docstring''' super().setUp() A: Any = cached_path( '''https://cdn-datasets.huggingface.co/translation/wmt_en_ro-tr40k-va0.5k-te0.5k.tar.gz''' , extract_compressed_file=SCREAMING_SNAKE_CASE_ , ) A: int = f"""{data_cached}/wmt_en_ro-tr40k-va0.5k-te0.5k""" @slow @require_torch_gpu def _snake_case ( self : int ) -> int: '''simple docstring''' MarianMTModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) @slow @require_torch_gpu def _snake_case ( self : Dict ) -> Optional[int]: '''simple docstring''' A: Union[str, Any] = { '''$MAX_LEN''': 64, '''$BS''': 64, '''$GAS''': 1, '''$ENRO_DIR''': self.data_dir, '''facebook/mbart-large-cc25''': MARIAN_MODEL, # "val_check_interval=0.25": "val_check_interval=1.0", '''--learning_rate=3e-5''': '''--learning_rate 3e-4''', '''--num_train_epochs 6''': '''--num_train_epochs 1''', } # Clean up bash script A: Dict = (self.test_file_dir / '''train_mbart_cc25_enro.sh''').open().read().split('''finetune.py''' )[1].strip() A: List[Any] = bash_script.replace('''\\\n''' , '''''' ).strip().replace('''"$@"''' , '''''' ) for k, v in env_vars_to_replace.items(): A: str = bash_script.replace(SCREAMING_SNAKE_CASE_ , str(SCREAMING_SNAKE_CASE_ ) ) A: str = self.get_auto_remove_tmp_dir() # bash_script = bash_script.replace("--fp16 ", "") A: Optional[int] = f""" --output_dir {output_dir} --tokenizer_name Helsinki-NLP/opus-mt-en-ro --sortish_sampler --do_predict --gpus 1 --freeze_encoder --n_train 40000 --n_val 500 --n_test 500 --fp16_opt_level O1 --num_sanity_val_steps 0 --eval_beams 2 """.split() # XXX: args.gpus > 1 : handle multi_gpu in the future A: Optional[int] = ['''finetune.py'''] + bash_script.split() + args with patch.object(SCREAMING_SNAKE_CASE_ , '''argv''' , SCREAMING_SNAKE_CASE_ ): A: Union[str, Any] = argparse.ArgumentParser() A: List[Any] = pl.Trainer.add_argparse_args(SCREAMING_SNAKE_CASE_ ) A: List[str] = SummarizationModule.add_model_specific_args(SCREAMING_SNAKE_CASE_ , os.getcwd() ) A: Optional[Any] = parser.parse_args() A: Optional[Any] = main(SCREAMING_SNAKE_CASE_ ) # Check metrics A: List[Any] = load_json(model.metrics_save_path ) A: List[str] = metrics['''val'''][0] A: Dict = metrics['''val'''][-1] self.assertEqual(len(metrics['''val'''] ) , (args.max_epochs / args.val_check_interval) ) assert isinstance(last_step_stats[f"""val_avg_{model.val_metric}"""] , SCREAMING_SNAKE_CASE_ ) self.assertGreater(last_step_stats['''val_avg_gen_time'''] , 0.01 ) # model hanging on generate. Maybe bad config was saved. (XXX: old comment/assert?) self.assertLessEqual(last_step_stats['''val_avg_gen_time'''] , 1.0 ) # test learning requirements: # 1. BLEU improves over the course of training by more than 2 pts self.assertGreater(last_step_stats['''val_avg_bleu'''] - first_step_stats['''val_avg_bleu'''] , 2 ) # 2. BLEU finishes above 17 self.assertGreater(last_step_stats['''val_avg_bleu'''] , 17 ) # 3. test BLEU and val BLEU within ~1.1 pt. self.assertLess(abs(metrics['''val'''][-1]['''val_avg_bleu'''] - metrics['''test'''][-1]['''test_avg_bleu'''] ) , 1.1 ) # check lightning ckpt can be loaded and has a reasonable statedict A: int = os.listdir(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = [x for x in contents if x.endswith('''.ckpt''' )][0] A: List[Any] = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE_ ) A: Optional[int] = torch.load(SCREAMING_SNAKE_CASE_ , map_location='''cpu''' ) A: Tuple = '''model.model.decoder.layers.0.encoder_attn_layer_norm.weight''' assert expected_key in ckpt["state_dict"] assert ckpt["state_dict"]["model.model.decoder.layers.0.encoder_attn_layer_norm.weight"].dtype == torch.floataa # TODO: turn on args.do_predict when PL bug fixed. if args.do_predict: A: Optional[int] = {os.path.basename(SCREAMING_SNAKE_CASE_ ) for p in contents} assert "test_generations.txt" in contents assert "test_results.txt" in contents # assert len(metrics["val"]) == desired_n_evals assert len(metrics['''test'''] ) == 1 class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' @timeout_decorator.timeout(6_00 ) @slow @require_torch_gpu def _snake_case ( self : List[str] ) -> str: '''simple docstring''' A: int = f"""{self.test_file_dir_str}/test_data/wmt_en_ro""" A: Tuple = { '''--fp16_opt_level=O1''': '''''', '''$MAX_LEN''': 1_28, '''$BS''': 16, '''$GAS''': 1, '''$ENRO_DIR''': data_dir, '''$m''': '''sshleifer/student_marian_en_ro_6_1''', '''val_check_interval=0.25''': '''val_check_interval=1.0''', } # Clean up bash script A: int = ( (self.test_file_dir / '''distil_marian_no_teacher.sh''').open().read().split('''distillation.py''' )[1].strip() ) A: Optional[Any] = bash_script.replace('''\\\n''' , '''''' ).strip().replace('''"$@"''' , '''''' ) A: List[str] = bash_script.replace('''--fp16 ''' , ''' ''' ) for k, v in env_vars_to_replace.items(): A: Tuple = bash_script.replace(SCREAMING_SNAKE_CASE_ , str(SCREAMING_SNAKE_CASE_ ) ) A: int = self.get_auto_remove_tmp_dir() A: List[str] = bash_script.replace('''--fp16''' , '''''' ) A: Any = 6 A: int = ( ['''distillation.py'''] + bash_script.split() + [ f"""--output_dir={output_dir}""", '''--gpus=1''', '''--learning_rate=1e-3''', f"""--num_train_epochs={epochs}""", '''--warmup_steps=10''', '''--val_check_interval=1.0''', '''--do_predict''', ] ) with patch.object(SCREAMING_SNAKE_CASE_ , '''argv''' , SCREAMING_SNAKE_CASE_ ): A: Any = argparse.ArgumentParser() A: Optional[Any] = pl.Trainer.add_argparse_args(SCREAMING_SNAKE_CASE_ ) A: List[Any] = SummarizationDistiller.add_model_specific_args(SCREAMING_SNAKE_CASE_ , os.getcwd() ) A: Optional[int] = parser.parse_args() # assert args.gpus == gpus THIS BREAKS for multi_gpu A: Any = distill_main(SCREAMING_SNAKE_CASE_ ) # Check metrics A: Union[str, Any] = load_json(model.metrics_save_path ) A: List[Any] = metrics['''val'''][0] A: int = metrics['''val'''][-1] assert len(metrics['''val'''] ) >= (args.max_epochs / args.val_check_interval) # +1 accounts for val_sanity_check assert last_step_stats["val_avg_gen_time"] >= 0.01 assert first_step_stats["val_avg_bleu"] < last_step_stats["val_avg_bleu"] # model learned nothing assert 1.0 >= last_step_stats["val_avg_gen_time"] # model hanging on generate. Maybe bad config was saved. assert isinstance(last_step_stats[f"""val_avg_{model.val_metric}"""] , SCREAMING_SNAKE_CASE_ ) # check lightning ckpt can be loaded and has a reasonable statedict A: Optional[Any] = os.listdir(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = [x for x in contents if x.endswith('''.ckpt''' )][0] A: Dict = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE_ ) A: Optional[int] = torch.load(SCREAMING_SNAKE_CASE_ , map_location='''cpu''' ) A: Union[str, Any] = '''model.model.decoder.layers.0.encoder_attn_layer_norm.weight''' assert expected_key in ckpt["state_dict"] assert ckpt["state_dict"]["model.model.decoder.layers.0.encoder_attn_layer_norm.weight"].dtype == torch.floataa # TODO: turn on args.do_predict when PL bug fixed. if args.do_predict: A: Any = {os.path.basename(SCREAMING_SNAKE_CASE_ ) for p in contents} assert "test_generations.txt" in contents assert "test_results.txt" in contents # assert len(metrics["val"]) == desired_n_evals assert len(metrics['''test'''] ) == 1
319
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE( __lowercase ) -> bool: if len(__lowercase ) < 2: raise ValueError('''Monogons and Digons are not polygons in the Euclidean space''' ) if any(i <= 0 for i in nums ): raise ValueError('''All values must be greater than 0''' ) A: Any = nums.copy() copy_nums.sort() return copy_nums[-1] < sum(copy_nums[:-1] ) if __name__ == "__main__": import doctest doctest.testmod()
319
1
'''simple docstring''' import json import os import unittest from transformers.models.roc_bert.tokenization_roc_bert import ( VOCAB_FILES_NAMES, RoCBertBasicTokenizer, RoCBertTokenizer, RoCBertWordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class lowerCAmelCase_ ( UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Union[str, Any] = RoCBertTokenizer UpperCamelCase_ : int = None UpperCamelCase_ : Union[str, Any] = False UpperCamelCase_ : Tuple = True UpperCamelCase_ : Dict = filter_non_english def _snake_case ( self : List[str] ) -> Any: '''simple docstring''' super().setUp() A: Optional[int] = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''你''', '''好''', '''是''', '''谁''', '''a''', '''b''', '''c''', '''d'''] A: Any = {} A: Union[str, Any] = {} for i, value in enumerate(SCREAMING_SNAKE_CASE_ ): A: str = i A: Any = i A: Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) A: int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''word_shape_file'''] ) A: int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''word_pronunciation_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) with open(self.word_shape_file , '''w''' , encoding='''utf-8''' ) as word_shape_writer: json.dump(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , ensure_ascii=SCREAMING_SNAKE_CASE_ ) with open(self.word_pronunciation_file , '''w''' , encoding='''utf-8''' ) as word_pronunciation_writer: json.dump(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , ensure_ascii=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] ) -> int: '''simple docstring''' A: Union[str, Any] = self.tokenizer_class(self.vocab_file , self.word_shape_file , self.word_pronunciation_file ) A: List[Any] = tokenizer.tokenize('''你好[SEP]你是谁''' ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , ['''你''', '''好''', '''[SEP]''', '''你''', '''是''', '''谁'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) , [5, 6, 2, 5, 7, 8] ) self.assertListEqual(tokenizer.convert_tokens_to_shape_ids(SCREAMING_SNAKE_CASE_ ) , [5, 6, 2, 5, 7, 8] ) self.assertListEqual(tokenizer.convert_tokens_to_pronunciation_ids(SCREAMING_SNAKE_CASE_ ) , [5, 6, 2, 5, 7, 8] ) def _snake_case ( self : int ) -> Optional[int]: '''simple docstring''' A: str = RoCBertBasicTokenizer() self.assertListEqual(tokenizer.tokenize('''ah\u535A\u63A8zz''' ) , ['''ah''', '''\u535A''', '''\u63A8''', '''zz'''] ) def _snake_case ( self : str ) -> Dict: '''simple docstring''' A: Optional[int] = RoCBertBasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE_ ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? ''' ) , ['''hello''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def _snake_case ( self : str ) -> Tuple: '''simple docstring''' A: Optional[Any] = RoCBertBasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE_ , strip_accents=SCREAMING_SNAKE_CASE_ ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hällo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''h\u00E9llo'''] ) def _snake_case ( self : Tuple ) -> str: '''simple docstring''' A: Any = RoCBertBasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE_ , strip_accents=SCREAMING_SNAKE_CASE_ ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def _snake_case ( self : List[str] ) -> Tuple: '''simple docstring''' A: List[Any] = RoCBertBasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE_ ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def _snake_case ( self : Optional[Any] ) -> Optional[int]: '''simple docstring''' A: Union[str, Any] = RoCBertBasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE_ ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? ''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def _snake_case ( self : Optional[Any] ) -> Tuple: '''simple docstring''' A: str = RoCBertBasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE_ , strip_accents=SCREAMING_SNAKE_CASE_ ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''HäLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def _snake_case ( self : str ) -> int: '''simple docstring''' A: Any = RoCBertBasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE_ , strip_accents=SCREAMING_SNAKE_CASE_ ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''HaLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def _snake_case ( self : Union[str, Any] ) -> Any: '''simple docstring''' A: Optional[Any] = RoCBertBasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE_ , never_split=['''[UNK]'''] ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? [UNK]''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''', '''[UNK]'''] ) def _snake_case ( self : Dict ) -> Tuple: '''simple docstring''' A: str = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing'''] A: Optional[Any] = {} for i, token in enumerate(SCREAMING_SNAKE_CASE_ ): A: Union[str, Any] = i A: List[str] = RoCBertWordpieceTokenizer(vocab=SCREAMING_SNAKE_CASE_ , unk_token='''[UNK]''' ) self.assertListEqual(tokenizer.tokenize('''''' ) , [] ) self.assertListEqual(tokenizer.tokenize('''unwanted running''' ) , ['''un''', '''##want''', '''##ed''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.tokenize('''unwantedX running''' ) , ['''[UNK]''', '''runn''', '''##ing'''] ) def _snake_case ( self : int ) -> Optional[Any]: '''simple docstring''' self.assertTrue(_is_whitespace(''' ''' ) ) self.assertTrue(_is_whitespace('''\t''' ) ) self.assertTrue(_is_whitespace('''\r''' ) ) self.assertTrue(_is_whitespace('''\n''' ) ) self.assertTrue(_is_whitespace('''\u00A0''' ) ) self.assertFalse(_is_whitespace('''A''' ) ) self.assertFalse(_is_whitespace('''-''' ) ) def _snake_case ( self : str ) -> str: '''simple docstring''' self.assertTrue(_is_control('''\u0005''' ) ) self.assertFalse(_is_control('''A''' ) ) self.assertFalse(_is_control(''' ''' ) ) self.assertFalse(_is_control('''\t''' ) ) self.assertFalse(_is_control('''\r''' ) ) def _snake_case ( self : Optional[int] ) -> Dict: '''simple docstring''' self.assertTrue(_is_punctuation('''-''' ) ) self.assertTrue(_is_punctuation('''$''' ) ) self.assertTrue(_is_punctuation('''`''' ) ) self.assertTrue(_is_punctuation('''.''' ) ) self.assertFalse(_is_punctuation('''A''' ) ) self.assertFalse(_is_punctuation(''' ''' ) ) def _snake_case ( self : Any ) -> int: '''simple docstring''' A: str = self.get_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) for t in ['''Test''', '''\xad''', '''test''']] , [['''[UNK]'''], [], ['''[UNK]''']] ) if self.test_rust_tokenizer: A: Tuple = self.get_rust_tokenizer() self.assertListEqual( [rust_tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) for t in ['''Test''', '''\xad''', '''test''']] , [['''[UNK]'''], [], ['''[UNK]''']] ) def _snake_case ( self : List[Any] ) -> Any: '''simple docstring''' for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): A: Union[str, Any] = self.rust_tokenizer_class.from_pretrained(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) A: Tuple = f"""A, naïve {tokenizer_r.mask_token} AllenNLP sentence.""" A: Any = tokenizer_r.encode_plus( SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , return_token_type_ids=SCREAMING_SNAKE_CASE_ , return_offsets_mapping=SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ , ) A: List[str] = tokenizer_r.do_lower_case if hasattr(SCREAMING_SNAKE_CASE_ , '''do_lower_case''' ) else False A: Optional[Any] = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), '''A'''), ((1, 2), ''','''), ((3, 5), '''na'''), ((5, 6), '''##ï'''), ((6, 8), '''##ve'''), ((9, 15), tokenizer_r.mask_token), ((16, 21), '''Allen'''), ((21, 23), '''##NL'''), ((23, 24), '''##P'''), ((25, 33), '''sentence'''), ((33, 34), '''.'''), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), '''a'''), ((1, 2), ''','''), ((3, 8), '''naive'''), ((9, 15), tokenizer_r.mask_token), ((16, 21), '''allen'''), ((21, 23), '''##nl'''), ((23, 24), '''##p'''), ((25, 33), '''sentence'''), ((33, 34), '''.'''), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results] , tokenizer_r.convert_ids_to_tokens(tokens['''input_ids'''] ) ) self.assertEqual([e[0] for e in expected_results] , tokens['''offset_mapping'''] ) def _snake_case ( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' A: str = ['''的''', '''人''', '''有'''] A: str = ''''''.join(SCREAMING_SNAKE_CASE_ ) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): A: Optional[int] = True A: Union[str, Any] = self.tokenizer_class.from_pretrained(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) A: str = self.rust_tokenizer_class.from_pretrained(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) A: Dict = tokenizer_p.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: int = tokenizer_r.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokenizer_r.convert_ids_to_tokens(SCREAMING_SNAKE_CASE_ ) A: Tuple = tokenizer_p.convert_ids_to_tokens(SCREAMING_SNAKE_CASE_ ) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: List[Any] = False A: List[Any] = self.rust_tokenizer_class.from_pretrained(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) A: List[str] = self.tokenizer_class.from_pretrained(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) A: Optional[int] = tokenizer_r.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokenizer_p.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: Optional[int] = tokenizer_r.convert_ids_to_tokens(SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokenizer_p.convert_ids_to_tokens(SCREAMING_SNAKE_CASE_ ) # it is expected that only the first Chinese character is not preceded by "##". A: str = [ f"""##{token}""" if idx != 0 else token for idx, token in enumerate(SCREAMING_SNAKE_CASE_ ) ] self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : int ) -> Tuple: '''simple docstring''' A: Union[str, Any] = self.tokenizer_class(self.vocab_file , self.word_shape_file , self.word_pronunciation_file ) A: Union[str, Any] = tokenizer.encode('''你好''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = tokenizer.encode('''你是谁''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: List[str] = tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_ ) A: Tuple = tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert encoded_sentence == [1] + text + [2] assert encoded_pair == [1] + text + [2] + text_a + [2] def _snake_case ( self : List[str] ) -> str: '''simple docstring''' A: List[str] = self.get_tokenizers(do_lower_case=SCREAMING_SNAKE_CASE_ ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): A: Any = '''你好,你是谁''' A: List[Any] = tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = tokenizer.convert_tokens_to_shape_ids(SCREAMING_SNAKE_CASE_ ) A: Dict = tokenizer.convert_tokens_to_pronunciation_ids(SCREAMING_SNAKE_CASE_ ) A: List[str] = tokenizer.prepare_for_model( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: str = tokenizer.encode_plus(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )
319
'''simple docstring''' # tests directory-specific settings - this file is run automatically # by pytest before any tests are run import doctest import sys import warnings from os.path import abspath, dirname, join import _pytest from transformers.testing_utils import HfDoctestModule, HfDocTestParser # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. UpperCamelCase = abspath(join(dirname(__file__), '''src''')) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action='''ignore''', category=FutureWarning) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: config.addinivalue_line( '''markers''' , '''is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested''' ) config.addinivalue_line( '''markers''' , '''is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested''' ) config.addinivalue_line('''markers''' , '''is_pipeline_test: mark test to run only when pipelines are tested''' ) config.addinivalue_line('''markers''' , '''is_staging_test: mark test to run only in the staging environment''' ) config.addinivalue_line('''markers''' , '''accelerate_tests: mark test that require accelerate''' ) config.addinivalue_line('''markers''' , '''tool_tests: mark the tool tests that are run on their specific schedule''' ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: from transformers.testing_utils import pytest_terminal_summary_main A: Optional[int] = terminalreporter.config.getoption('''--make-reports''' ) if make_reports: pytest_terminal_summary_main(__lowercase , id=__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Any: # If no tests are collected, pytest exists with code 5, which makes the CI fail. if exitstatus == 5: A: Tuple = 0 # Doctest custom flag to ignore output. UpperCamelCase = doctest.register_optionflag('''IGNORE_RESULT''') UpperCamelCase = doctest.OutputChecker class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ) -> str: '''simple docstring''' if IGNORE_RESULT & optionflags: return True return OutputChecker.check_output(self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase = CustomOutputChecker UpperCamelCase = HfDoctestModule UpperCamelCase = HfDocTestParser
319
1
'''simple docstring''' import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import DeiTConfig, DeiTForImageClassificationWithTeacher, DeiTImageProcessor from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=False ) -> Optional[int]: A: Any = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""blocks.{i}.norm1.weight""", F"""deit.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""blocks.{i}.norm1.bias""", F"""deit.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append((F"""blocks.{i}.attn.proj.weight""", F"""deit.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.attn.proj.bias""", F"""deit.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""blocks.{i}.norm2.weight""", F"""deit.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""blocks.{i}.norm2.bias""", F"""deit.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.weight""", F"""deit.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.bias""", F"""deit.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.weight""", F"""deit.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.bias""", F"""deit.encoder.layer.{i}.output.dense.bias""") ) # projection layer + position embeddings rename_keys.extend( [ ('''cls_token''', '''deit.embeddings.cls_token'''), ('''dist_token''', '''deit.embeddings.distillation_token'''), ('''patch_embed.proj.weight''', '''deit.embeddings.patch_embeddings.projection.weight'''), ('''patch_embed.proj.bias''', '''deit.embeddings.patch_embeddings.projection.bias'''), ('''pos_embed''', '''deit.embeddings.position_embeddings'''), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ('''norm.weight''', '''layernorm.weight'''), ('''norm.bias''', '''layernorm.bias'''), ('''pre_logits.fc.weight''', '''pooler.dense.weight'''), ('''pre_logits.fc.bias''', '''pooler.dense.bias'''), ] ) # if just the base model, we should remove "deit" from all keys that start with "deit" A: Dict = [(pair[0], pair[1][4:]) if pair[1].startswith('''deit''' ) else pair for pair in rename_keys] else: # layernorm + classification heads rename_keys.extend( [ ('''norm.weight''', '''deit.layernorm.weight'''), ('''norm.bias''', '''deit.layernorm.bias'''), ('''head.weight''', '''cls_classifier.weight'''), ('''head.bias''', '''cls_classifier.bias'''), ('''head_dist.weight''', '''distillation_classifier.weight'''), ('''head_dist.bias''', '''distillation_classifier.bias'''), ] ) return rename_keys def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=False ) -> Optional[Any]: for i in range(config.num_hidden_layers ): if base_model: A: Tuple = '''''' else: A: Dict = '''deit.''' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A: Tuple = state_dict.pop(F"""blocks.{i}.attn.qkv.weight""" ) A: Tuple = state_dict.pop(F"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict A: Optional[int] = in_proj_weight[ : config.hidden_size, : ] A: Dict = in_proj_bias[: config.hidden_size] A: int = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A: List[Any] = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A: int = in_proj_weight[ -config.hidden_size :, : ] A: Dict = in_proj_bias[-config.hidden_size :] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Optional[int]: A: Any = dct.pop(__lowercase ) A: Any = val def SCREAMING_SNAKE_CASE( ) -> Optional[int]: A: List[Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' A: Optional[int] = Image.open(requests.get(__lowercase , stream=__lowercase ).raw ) return im @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Any: A: Optional[Any] = DeiTConfig() # all deit models have fine-tuned heads A: int = False # dataset (fine-tuned on ImageNet 2012), patch_size and image_size A: Union[str, Any] = 1_0_0_0 A: Union[str, Any] = '''huggingface/label-files''' A: List[str] = '''imagenet-1k-id2label.json''' A: Optional[int] = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: List[Any] = {int(__lowercase ): v for k, v in idalabel.items()} A: Dict = idalabel A: Union[str, Any] = {v: k for k, v in idalabel.items()} A: Dict = int(deit_name[-6:-4] ) A: Dict = int(deit_name[-3:] ) # size of the architecture if deit_name[9:].startswith('''tiny''' ): A: Optional[int] = 1_9_2 A: Optional[Any] = 7_6_8 A: Optional[int] = 1_2 A: Union[str, Any] = 3 elif deit_name[9:].startswith('''small''' ): A: str = 3_8_4 A: List[str] = 1_5_3_6 A: int = 1_2 A: List[str] = 6 if deit_name[9:].startswith('''base''' ): pass elif deit_name[4:].startswith('''large''' ): A: Optional[Any] = 1_0_2_4 A: List[Any] = 4_0_9_6 A: Optional[int] = 2_4 A: Dict = 1_6 # load original model from timm A: int = timm.create_model(__lowercase , pretrained=__lowercase ) timm_model.eval() # load state_dict of original model, remove and rename some keys A: int = timm_model.state_dict() A: List[str] = create_rename_keys(__lowercase , __lowercase ) for src, dest in rename_keys: rename_key(__lowercase , __lowercase , __lowercase ) read_in_q_k_v(__lowercase , __lowercase , __lowercase ) # load HuggingFace model A: Any = DeiTForImageClassificationWithTeacher(__lowercase ).eval() model.load_state_dict(__lowercase ) # Check outputs on an image, prepared by DeiTImageProcessor A: Optional[int] = int( (2_5_6 / 2_2_4) * config.image_size ) # to maintain same ratio w.r.t. 224 images, see https://github.com/facebookresearch/deit/blob/ab5715372db8c6cad5740714b2216d55aeae052e/datasets.py#L103 A: List[str] = DeiTImageProcessor(size=__lowercase , crop_size=config.image_size ) A: Optional[int] = image_processor(images=prepare_img() , return_tensors='''pt''' ) A: Any = encoding['''pixel_values'''] A: int = model(__lowercase ) A: str = timm_model(__lowercase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__lowercase , outputs.logits , atol=1E-3 ) Path(__lowercase ).mkdir(exist_ok=__lowercase ) print(F"""Saving model {deit_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(__lowercase ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--deit_name''', default='''vit_deit_base_distilled_patch16_224''', type=str, help='''Name of the DeiT timm model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) UpperCamelCase = parser.parse_args() convert_deit_checkpoint(args.deit_name, args.pytorch_dump_folder_path)
319
'''simple docstring''' import heapq import sys import numpy as np UpperCamelCase = tuple[int, int] class lowerCAmelCase_ : '''simple docstring''' def __init__( self : List[Any] ) -> str: '''simple docstring''' A: Any = [] A: int = set() def _snake_case ( self : Optional[Any] ) -> int: '''simple docstring''' if not self.empty(): return self.elements[0][0] else: return float('''inf''' ) def _snake_case ( self : List[str] ) -> List[Any]: '''simple docstring''' return len(self.elements ) == 0 def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Any ) -> List[Any]: '''simple docstring''' if item not in self.set: heapq.heappush(self.elements , (priority, item) ) self.set.add(SCREAMING_SNAKE_CASE_ ) else: # update # print("update", item) A: Optional[int] = [] ((A) , (A)): str = heapq.heappop(self.elements ) while x != item: temp.append((pri, x) ) ((A) , (A)): int = heapq.heappop(self.elements ) temp.append((priority, item) ) for pro, xxx in temp: heapq.heappush(self.elements , (pro, xxx) ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : str ) -> Any: '''simple docstring''' if item in self.set: self.set.remove(SCREAMING_SNAKE_CASE_ ) A: str = [] ((A) , (A)): List[str] = heapq.heappop(self.elements ) while x != item: temp.append((pro, x) ) ((A) , (A)): Any = heapq.heappop(self.elements ) for prito, yyy in temp: heapq.heappush(self.elements , (prito, yyy) ) def _snake_case ( self : List[Any] ) -> Optional[int]: '''simple docstring''' return self.elements[0][1] def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' ((A) , (A)): Dict = heapq.heappop(self.elements ) self.set.remove(SCREAMING_SNAKE_CASE_ ) return (priority, item) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: # euclidean distance A: List[str] = np.array(__lowercase ) A: Optional[int] = np.array(__lowercase ) return np.linalg.norm(a - b ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> int: # integer division by time variable return consistent_heuristic(__lowercase , __lowercase ) // t def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[Any]: # manhattan distance return abs(p[0] - goal[0] ) + abs(p[1] - goal[1] ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase ) -> List[Any]: A: int = g_function[start] + Wa * heuristics[i](__lowercase , __lowercase ) return ans def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Optional[int]: A: Union[str, Any] = np.chararray((n, n) ) for i in range(__lowercase ): for j in range(__lowercase ): A: Union[str, Any] = '''*''' for i in range(__lowercase ): for j in range(__lowercase ): if (j, (n - 1) - i) in blocks: A: Optional[Any] = '''#''' A: Tuple = '''-''' A: List[str] = back_pointer[goal] while x != start: ((A) , (A)): Tuple = x # print(x) A: List[str] = '''-''' A: str = back_pointer[x] A: Dict = '''-''' for i in range(__lowercase ): for j in range(__lowercase ): if (i, j) == (0, n - 1): print(grid[i][j] , end=''' ''' ) print('''<-- End position''' , end=''' ''' ) else: print(grid[i][j] , end=''' ''' ) print() print('''^''' ) print('''Start position''' ) print() print('''# is an obstacle''' ) print('''- is the path taken by algorithm''' ) print('''PATH TAKEN BY THE ALGORITHM IS:-''' ) A: List[str] = back_pointer[goal] while x != start: print(__lowercase , end=''' ''' ) A: Optional[int] = back_pointer[x] print(__lowercase ) sys.exit() def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: if p[0] < 0 or p[0] > n - 1: return False if p[1] < 0 or p[1] > n - 1: return False return True def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) -> Union[str, Any]: for itera in range(__lowercase ): open_list[itera].remove_element(__lowercase ) # print("s", s) # print("j", j) ((A) , (A)): Tuple = s A: Optional[Any] = (x - 1, y) A: str = (x + 1, y) A: List[Any] = (x, y + 1) A: int = (x, y - 1) for neighbours in [left, right, up, down]: if neighbours not in blocks: if valid(__lowercase ) and neighbours not in visited: # print("neighbour", neighbours) visited.add(__lowercase ) A: int = -1 A: int = float('''inf''' ) if valid(__lowercase ) and g_function[neighbours] > g_function[s] + 1: A: List[str] = g_function[s] + 1 A: List[str] = s if neighbours not in close_list_anchor: open_list[0].put(__lowercase , key(__lowercase , 0 , __lowercase , __lowercase ) ) if neighbours not in close_list_inad: for var in range(1 , __lowercase ): if key(__lowercase , __lowercase , __lowercase , __lowercase ) <= Wa * key( __lowercase , 0 , __lowercase , __lowercase ): open_list[j].put( __lowercase , key(__lowercase , __lowercase , __lowercase , __lowercase ) ) def SCREAMING_SNAKE_CASE( ) -> Tuple: A: str = [] for x in range(1 , 5 ): for y in range(1 , 6 ): some_list.append((x, y) ) for x in range(1_5 , 2_0 ): some_list.append((x, 1_7) ) for x in range(1_0 , 1_9 ): for y in range(1 , 1_5 ): some_list.append((x, y) ) # L block for x in range(1 , 4 ): for y in range(1_2 , 1_9 ): some_list.append((x, y) ) for x in range(3 , 1_3 ): for y in range(1_6 , 1_9 ): some_list.append((x, y) ) return some_list UpperCamelCase = {0: consistent_heuristic, 1: heuristic_a, 2: heuristic_a} UpperCamelCase = [ (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1), (15, 1), (16, 1), (17, 1), (18, 1), (19, 1), ] UpperCamelCase = make_common_ground() UpperCamelCase = blocks_blk # hyper parameters UpperCamelCase = 1 UpperCamelCase = 1 UpperCamelCase = 20 UpperCamelCase = 3 # one consistent and two other inconsistent # start and end destination UpperCamelCase = (0, 0) UpperCamelCase = (n - 1, n - 1) UpperCamelCase = 1 def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: A: int = {start: 0, goal: float('''inf''' )} A: Union[str, Any] = {start: -1, goal: -1} A: List[Any] = [] A: Union[str, Any] = set() for i in range(__lowercase ): open_list.append(PriorityQueue() ) open_list[i].put(__lowercase , key(__lowercase , __lowercase , __lowercase , __lowercase ) ) A: list[int] = [] A: list[int] = [] while open_list[0].minkey() < float('''inf''' ): for i in range(1 , __lowercase ): # print(open_list[0].minkey(), open_list[i].minkey()) if open_list[i].minkey() <= Wa * open_list[0].minkey(): global t t += 1 if g_function[goal] <= open_list[i].minkey(): if g_function[goal] < float('''inf''' ): do_something(__lowercase , __lowercase , __lowercase ) else: A , A: Union[str, Any] = open_list[i].top_show() visited.add(__lowercase ) expand_state( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) close_list_inad.append(__lowercase ) else: if g_function[goal] <= open_list[0].minkey(): if g_function[goal] < float('''inf''' ): do_something(__lowercase , __lowercase , __lowercase ) else: A: Union[str, Any] = open_list[0].top_show() visited.add(__lowercase ) expand_state( __lowercase , 0 , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) close_list_anchor.append(__lowercase ) print('''No path found to goal''' ) print() for i in range(n - 1 , -1 , -1 ): for j in range(__lowercase ): if (j, i) in blocks: print('''#''' , end=''' ''' ) elif (j, i) in back_pointer: if (j, i) == (n - 1, n - 1): print('''*''' , end=''' ''' ) else: print('''-''' , end=''' ''' ) else: print('''*''' , end=''' ''' ) if (j, i) == (n - 1, n - 1): print('''<-- End position''' , end=''' ''' ) print() print('''^''' ) print('''Start position''' ) print() print('''# is an obstacle''' ) print('''- is the path taken by algorithm''' ) if __name__ == "__main__": multi_a_star(start, goal, n_heuristic)
319
1
'''simple docstring''' import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) UpperCamelCase = logging.getLogger(__name__) @dataclass(frozen=UpperCAmelCase_ ) class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : str UpperCamelCase_ : str UpperCamelCase_ : Optional[str] = None UpperCamelCase_ : Optional[str] = None UpperCamelCase_ : Optional[str] = None @dataclass(frozen=UpperCAmelCase_ ) class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : List[int] UpperCamelCase_ : Optional[List[int]] = None UpperCamelCase_ : Optional[List[int]] = None UpperCamelCase_ : Optional[Union[int, float]] = None UpperCamelCase_ : Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : List[InputFeatures] def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : PreTrainedTokenizer , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Any=False , SCREAMING_SNAKE_CASE_ : bool = False , ) -> Optional[int]: '''simple docstring''' A: Union[str, Any] = hans_processors[task]() A: int = os.path.join( SCREAMING_SNAKE_CASE_ , '''cached_{}_{}_{}_{}'''.format( '''dev''' if evaluate else '''train''' , tokenizer.__class__.__name__ , str(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ , ) , ) A: List[Any] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) A , A: Any = label_list[2], label_list[1] A: Optional[int] = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. A: List[Any] = cached_features_file + '''.lock''' with FileLock(SCREAMING_SNAKE_CASE_ ): if os.path.exists(SCREAMING_SNAKE_CASE_ ) and not overwrite_cache: logger.info(f"""Loading features from cached file {cached_features_file}""" ) A: Any = torch.load(SCREAMING_SNAKE_CASE_ ) else: logger.info(f"""Creating features from dataset file at {data_dir}""" ) A: Tuple = ( processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) ) logger.info('''Training examples: %s''' , len(SCREAMING_SNAKE_CASE_ ) ) A: List[Any] = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) logger.info('''Saving features into cached file %s''' , SCREAMING_SNAKE_CASE_ ) torch.save(self.features , SCREAMING_SNAKE_CASE_ ) def __len__( self : Any ) -> Optional[int]: '''simple docstring''' return len(self.features ) def __getitem__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> InputFeatures: '''simple docstring''' return self.features[i] def _snake_case ( self : List[Any] ) -> List[str]: '''simple docstring''' return self.label_list if is_tf_available(): import tensorflow as tf class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : List[InputFeatures] def __init__( self : int , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : PreTrainedTokenizer , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[int] = 1_28 , SCREAMING_SNAKE_CASE_ : Optional[Any]=False , SCREAMING_SNAKE_CASE_ : bool = False , ) -> List[str]: '''simple docstring''' A: Dict = hans_processors[task]() A: Optional[int] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) A , A: Union[str, Any] = label_list[2], label_list[1] A: Tuple = label_list A: Optional[int] = processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) A: List[str] = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) , desc='''convert examples to features''' ): if ex_index % 1_00_00 == 0: logger.info('''Writing example %d of %d''' % (ex_index, len(SCREAMING_SNAKE_CASE_ )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) A: Union[str, Any] = tf.data.Dataset.from_generator( SCREAMING_SNAKE_CASE_ , ( { '''example_id''': tf.intaa, '''input_ids''': tf.intaa, '''attention_mask''': tf.intaa, '''token_type_ids''': tf.intaa, }, tf.intaa, ) , ( { '''example_id''': tf.TensorShape([] ), '''input_ids''': tf.TensorShape([None, None] ), '''attention_mask''': tf.TensorShape([None, None] ), '''token_type_ids''': tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) , ) def _snake_case ( self : Dict ) -> Optional[int]: '''simple docstring''' return self.dataset def __len__( self : Optional[Any] ) -> Optional[int]: '''simple docstring''' return len(self.features ) def __getitem__( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Tuple ) -> InputFeatures: '''simple docstring''' return self.features[i] def _snake_case ( self : Union[str, Any] ) -> List[Any]: '''simple docstring''' return self.label_list class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : Any ) -> Optional[Any]: '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ , '''heuristics_train_set.txt''' ) ) , '''train''' ) def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : Tuple ) -> int: '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ , '''heuristics_evaluation_set.txt''' ) ) , '''dev''' ) def _snake_case ( self : List[Any] ) -> Optional[Any]: '''simple docstring''' return ["contradiction", "entailment", "neutral"] def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Tuple: '''simple docstring''' A: Optional[Any] = [] for i, line in enumerate(SCREAMING_SNAKE_CASE_ ): if i == 0: continue A: Optional[Any] = '''%s-%s''' % (set_type, line[0]) A: List[str] = line[5] A: List[str] = line[6] A: List[str] = line[7][2:] if line[7].startswith('''ex''' ) else line[7] A: Union[str, Any] = line[0] examples.append(InputExample(guid=SCREAMING_SNAKE_CASE_ , text_a=SCREAMING_SNAKE_CASE_ , text_b=SCREAMING_SNAKE_CASE_ , label=SCREAMING_SNAKE_CASE_ , pairID=SCREAMING_SNAKE_CASE_ ) ) return examples def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , ) -> Optional[int]: A: Any = {label: i for i, label in enumerate(__lowercase )} A: List[str] = [] for ex_index, example in tqdm.tqdm(enumerate(__lowercase ) , desc='''convert examples to features''' ): if ex_index % 1_0_0_0_0 == 0: logger.info('''Writing example %d''' % (ex_index) ) A: Optional[Any] = tokenizer( example.text_a , example.text_b , add_special_tokens=__lowercase , max_length=__lowercase , padding='''max_length''' , truncation=__lowercase , return_overflowing_tokens=__lowercase , ) A: Any = label_map[example.label] if example.label in label_map else 0 A: Any = int(example.pairID ) features.append(InputFeatures(**__lowercase , label=__lowercase , pairID=__lowercase ) ) for i, example in enumerate(examples[:5] ): logger.info('''*** Example ***''' ) logger.info(F"""guid: {example}""" ) logger.info(F"""features: {features[i]}""" ) return features UpperCamelCase = { '''hans''': 3, } UpperCamelCase = { '''hans''': HansProcessor, }
319
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase = 1 , __lowercase = 1_0_0_0 ) -> int: A: Any = 1 A: Optional[Any] = 0 for divide_by_number in range(__lowercase , digit + 1 ): A: list[int] = [] A: List[Any] = numerator for _ in range(1 , digit + 1 ): if now_divide in has_been_divided: if longest_list_length < len(__lowercase ): A: Any = len(__lowercase ) A: Dict = divide_by_number else: has_been_divided.append(__lowercase ) A: str = now_divide * 1_0 % divide_by_number return the_digit # Tests if __name__ == "__main__": import doctest doctest.testmod()
319
1
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: def count_of_possible_combinations(__lowercase ) -> int: if target < 0: return 0 if target == 0: return 1 return sum(count_of_possible_combinations(target - item ) for item in array ) return count_of_possible_combinations(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: def count_of_possible_combinations_with_dp_array( __lowercase , __lowercase ) -> int: if target < 0: return 0 if target == 0: return 1 if dp_array[target] != -1: return dp_array[target] A: Optional[int] = sum( count_of_possible_combinations_with_dp_array(target - item , __lowercase ) for item in array ) A: Optional[int] = answer return answer A: Any = [-1] * (target + 1) return count_of_possible_combinations_with_dp_array(__lowercase , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: A: int = [0] * (target + 1) A: Optional[Any] = 1 for i in range(1 , target + 1 ): for j in range(__lowercase ): if i - array[j] >= 0: dp_array[i] += dp_array[i - array[j]] return dp_array[target] if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase = 3 UpperCamelCase = 5 UpperCamelCase = [1, 2, 5] print(combination_sum_iv(n, array, target))
319
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) UpperCamelCase = { '''configuration_vision_encoder_decoder''': ['''VisionEncoderDecoderConfig''', '''VisionEncoderDecoderOnnxConfig'''] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''VisionEncoderDecoderModel'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''TFVisionEncoderDecoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''FlaxVisionEncoderDecoderModel'''] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
319
1