code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
def lowerCAmelCase ( lowerCAmelCase_ )-> bool: if num < 0: return False lowerCAmelCase_ : int = num lowerCAmelCase_ : int = 0 while num > 0: lowerCAmelCase_ : Any = rev_num * 10 + (num % 10) num //= 10 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
262
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 : Optional[int] ="""src/transformers""" _UpperCAmelCase : str ="""docs/source/en""" _UpperCAmelCase : Optional[int] =""".""" def lowerCAmelCase ( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ )-> List[str]: with open(lowerCAmelCase_ , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: lowerCAmelCase_ : int = f.readlines() # Find the start prompt. lowerCAmelCase_ : List[Any] = 0 while not lines[start_index].startswith(lowerCAmelCase_ ): start_index += 1 start_index += 1 lowerCAmelCase_ : List[str] = start_index while not lines[end_index].startswith(lowerCAmelCase_ ): 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 : Optional[Any] ="""Model|Encoder|Decoder|ForConditionalGeneration""" # Regexes that match TF/Flax/PT model names. _UpperCAmelCase : Optional[int] =re.compile(R"""TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)""") _UpperCAmelCase : Dict =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 : Optional[Any] =re.compile(R"""(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)""") # This is to make sure the transformers module imported is the one in the repo. _UpperCAmelCase : Optional[int] =direct_transformers_import(TRANSFORMERS_PATH) def lowerCAmelCase ( lowerCAmelCase_ )-> Optional[int]: lowerCAmelCase_ : str = re.finditer('''.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)''' , lowerCAmelCase_ ) return [m.group(0 ) for m in matches] def lowerCAmelCase ( lowerCAmelCase_ , lowerCAmelCase_ )-> Optional[int]: lowerCAmelCase_ : Tuple = 2 if text == '''✅''' or text == '''❌''' else len(lowerCAmelCase_ ) lowerCAmelCase_ : int = (width - text_length) // 2 lowerCAmelCase_ : Union[str, Any] = width - text_length - left_indent return " " * left_indent + text + " " * right_indent def lowerCAmelCase ( )-> str: lowerCAmelCase_ : Any = transformers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES lowerCAmelCase_ : Dict = { name: config_maping_names[code] for code, name in transformers_module.MODEL_NAMES_MAPPING.items() if code in config_maping_names } lowerCAmelCase_ : List[Any] = {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. lowerCAmelCase_ : Tuple = collections.defaultdict(lowerCAmelCase_ ) lowerCAmelCase_ : List[str] = collections.defaultdict(lowerCAmelCase_ ) lowerCAmelCase_ : Optional[Any] = collections.defaultdict(lowerCAmelCase_ ) lowerCAmelCase_ : Optional[int] = collections.defaultdict(lowerCAmelCase_ ) lowerCAmelCase_ : List[str] = collections.defaultdict(lowerCAmelCase_ ) # Let's lookup through all transformers object (once). for attr_name in dir(lowerCAmelCase_ ): lowerCAmelCase_ : Optional[int] = None if attr_name.endswith('''Tokenizer''' ): lowerCAmelCase_ : Union[str, Any] = slow_tokenizers lowerCAmelCase_ : List[str] = attr_name[:-9] elif attr_name.endswith('''TokenizerFast''' ): lowerCAmelCase_ : int = fast_tokenizers lowerCAmelCase_ : Union[str, Any] = attr_name[:-13] elif _re_tf_models.match(lowerCAmelCase_ ) is not None: lowerCAmelCase_ : Tuple = tf_models lowerCAmelCase_ : str = _re_tf_models.match(lowerCAmelCase_ ).groups()[0] elif _re_flax_models.match(lowerCAmelCase_ ) is not None: lowerCAmelCase_ : Tuple = flax_models lowerCAmelCase_ : Union[str, Any] = _re_flax_models.match(lowerCAmelCase_ ).groups()[0] elif _re_pt_models.match(lowerCAmelCase_ ) is not None: lowerCAmelCase_ : Any = pt_models lowerCAmelCase_ : List[Any] = _re_pt_models.match(lowerCAmelCase_ ).groups()[0] if lookup_dict is not None: while len(lowerCAmelCase_ ) > 0: if attr_name in model_name_to_prefix.values(): lowerCAmelCase_ : Union[str, Any] = True break # Try again after removing the last word in the name lowerCAmelCase_ : Any = ''''''.join(camel_case_split(lowerCAmelCase_ )[:-1] ) # Let's build that table! lowerCAmelCase_ : int = list(model_name_to_config.keys() ) model_names.sort(key=str.lower ) lowerCAmelCase_ : Tuple = ['''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). lowerCAmelCase_ : Union[str, Any] = [len(lowerCAmelCase_ ) + 2 for c in columns] lowerCAmelCase_ : Optional[Any] = max([len(lowerCAmelCase_ ) for name in model_names] ) + 2 # Build the table per se lowerCAmelCase_ : Dict = '''|''' + '''|'''.join([_center_text(lowerCAmelCase_ , lowerCAmelCase_ ) for c, w in zip(lowerCAmelCase_ , lowerCAmelCase_ )] ) + '''|\n''' # Use ":-----:" format to center-aligned table cell texts table += "|" + "|".join([''':''' + '''-''' * (w - 2) + ''':''' for w in widths] ) + "|\n" lowerCAmelCase_ : List[str] = {True: '''✅''', False: '''❌'''} for name in model_names: lowerCAmelCase_ : List[Any] = model_name_to_prefix[name] lowerCAmelCase_ : Union[str, Any] = [ 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(lowerCAmelCase_ , lowerCAmelCase_ ) for l, w in zip(lowerCAmelCase_ , lowerCAmelCase_ )] ) + "|\n" return table def lowerCAmelCase ( lowerCAmelCase_=False )-> Tuple: lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ : str = _find_text_in_file( filename=os.path.join(lowerCAmelCase_ , '''index.md''' ) , start_prompt='''<!--This table is updated automatically from the auto modules''' , end_prompt='''<!-- End table-->''' , ) lowerCAmelCase_ : Tuple = get_model_table_from_auto_modules() if current_table != new_table: if overwrite: with open(os.path.join(lowerCAmelCase_ , '''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 : List[Any] =argparse.ArgumentParser() parser.add_argument("""--fix_and_overwrite""", action="""store_true""", help="""Whether to fix inconsistencies.""") _UpperCAmelCase : Tuple =parser.parse_args() check_model_table(args.fix_and_overwrite)
262
1
'''simple docstring''' import argparse from pathlib import Path from typing import Dict, OrderedDict, Tuple import torch from audiocraft.models import MusicGen from transformers import ( AutoFeatureExtractor, AutoTokenizer, EncodecModel, MusicgenDecoderConfig, MusicgenForConditionalGeneration, MusicgenProcessor, TaEncoderModel, ) from transformers.models.musicgen.modeling_musicgen import MusicgenForCausalLM from transformers.utils import logging logging.set_verbosity_info() lowercase : Tuple = logging.get_logger(__name__) lowercase : Union[str, Any] = ['model.decoder.embed_positions.weights'] def lowerCAmelCase_ ( snake_case__ ): '''simple docstring''' if "emb" in name: A : Optional[int] = name.replace('''emb''' , '''model.decoder.embed_tokens''' ) if "transformer" in name: A : List[Any] = name.replace('''transformer''' , '''model.decoder''' ) if "cross_attention" in name: A : List[Any] = name.replace('''cross_attention''' , '''encoder_attn''' ) if "linear1" in name: A : Optional[int] = name.replace('''linear1''' , '''fc1''' ) if "linear2" in name: A : List[Any] = name.replace('''linear2''' , '''fc2''' ) if "norm1" in name: A : List[Any] = name.replace('''norm1''' , '''self_attn_layer_norm''' ) if "norm_cross" in name: A : Any = name.replace('''norm_cross''' , '''encoder_attn_layer_norm''' ) if "norm2" in name: A : Dict = name.replace('''norm2''' , '''final_layer_norm''' ) if "out_norm" in name: A : Union[str, Any] = name.replace('''out_norm''' , '''model.decoder.layer_norm''' ) if "linears" in name: A : Dict = name.replace('''linears''' , '''lm_heads''' ) if "condition_provider.conditioners.description.output_proj" in name: A : List[Any] = name.replace('''condition_provider.conditioners.description.output_proj''' , '''enc_to_dec_proj''' ) return name def lowerCAmelCase_ ( snake_case__ , snake_case__ ): '''simple docstring''' A : List[Any] = list(state_dict.keys() ) A : Optional[int] = {} for key in keys: A : List[str] = state_dict.pop(snake_case__ ) A : str = rename_keys(snake_case__ ) if "in_proj_weight" in key: # split fused qkv proj A : List[Any] = val[:hidden_size, :] A : Tuple = val[hidden_size : 2 * hidden_size, :] A : Dict = val[-hidden_size:, :] elif "enc_to_dec_proj" in key: A : int = val else: A : Union[str, Any] = val return state_dict, enc_dec_proj_state_dict def lowerCAmelCase_ ( snake_case__ ): '''simple docstring''' if checkpoint == "small": # default config values A : int = 1024 A : int = 24 A : Dict = 16 elif checkpoint == "medium": A : Optional[Any] = 1536 A : str = 48 A : List[str] = 24 elif checkpoint == "large": A : int = 2048 A : int = 48 A : Dict = 32 else: raise ValueError(F'Checkpoint should be one of `[\'small\', \'medium\', \'large\']`, got {checkpoint}.' ) A : Optional[int] = MusicgenDecoderConfig( hidden_size=snake_case__ , ffn_dim=hidden_size * 4 , num_hidden_layers=snake_case__ , num_attention_heads=snake_case__ , ) return config @torch.no_grad() def lowerCAmelCase_ ( snake_case__ , snake_case__=None , snake_case__=None , snake_case__="cpu" ): '''simple docstring''' A : Optional[Any] = MusicGen.get_pretrained(snake_case__ , device=snake_case__ ) A : int = decoder_config_from_checkpoint(snake_case__ ) A : int = fairseq_model.lm.state_dict() A, A : Union[str, Any] = rename_state_dict( snake_case__ , hidden_size=decoder_config.hidden_size ) A : str = TaEncoderModel.from_pretrained('''t5-base''' ) A : str = EncodecModel.from_pretrained('''facebook/encodec_32khz''' ) A : List[str] = MusicgenForCausalLM(snake_case__ ).eval() # load all decoder weights - expect that we'll be missing embeddings and enc-dec projection A, A : str = decoder.load_state_dict(snake_case__ , strict=snake_case__ ) for key in missing_keys.copy(): if key.startswith(('''text_encoder''', '''audio_encoder''') ) or key in EXPECTED_MISSING_KEYS: missing_keys.remove(snake_case__ ) if len(snake_case__ ) > 0: raise ValueError(F'Missing key(s) in state_dict: {missing_keys}' ) if len(snake_case__ ) > 0: raise ValueError(F'Unexpected key(s) in state_dict: {unexpected_keys}' ) # init the composite model A : Union[str, Any] = MusicgenForConditionalGeneration(text_encoder=snake_case__ , audio_encoder=snake_case__ , decoder=snake_case__ ) # load the pre-trained enc-dec projection (from the decoder state dict) model.enc_to_dec_proj.load_state_dict(snake_case__ ) # check we can do a forward pass A : List[Any] = torch.arange(0 , 8 , dtype=torch.long ).reshape(2 , -1 ) A : Any = input_ids.reshape(2 * 4 , -1 ) with torch.no_grad(): A : List[Any] = model(input_ids=snake_case__ , decoder_input_ids=snake_case__ ).logits if logits.shape != (8, 1, 2048): raise ValueError('''Incorrect shape for logits''' ) # now construct the processor A : Optional[Any] = AutoTokenizer.from_pretrained('''t5-base''' ) A : List[str] = AutoFeatureExtractor.from_pretrained('''facebook/encodec_32khz''' , padding_side='''left''' ) A : List[Any] = MusicgenProcessor(feature_extractor=snake_case__ , tokenizer=snake_case__ ) # set the appropriate bos/pad token ids A : List[str] = 2048 A : List[str] = 2048 # set other default generation config params A : Any = int(30 * audio_encoder.config.frame_rate ) A : Optional[Any] = True A : str = 3.0 if pytorch_dump_folder is not None: Path(snake_case__ ).mkdir(exist_ok=snake_case__ ) logger.info(F'Saving model {checkpoint} to {pytorch_dump_folder}' ) model.save_pretrained(snake_case__ ) processor.save_pretrained(snake_case__ ) if repo_id: logger.info(F'Pushing model {checkpoint} to {repo_id}' ) model.push_to_hub(snake_case__ ) processor.push_to_hub(snake_case__ ) if __name__ == "__main__": lowercase : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--checkpoint', default='small', type=str, help='Checkpoint size of the MusicGen model you\'d like to convert. Can be one of: `[\'small\', \'medium\', \'large\']`.', ) parser.add_argument( '--pytorch_dump_folder', required=True, default=None, type=str, help='Path to the output PyTorch model directory.', ) parser.add_argument( '--push_to_hub', default=None, type=str, help='Where to upload the converted model on the 🤗 hub.' ) parser.add_argument( '--device', default='cpu', type=str, help='Torch device to run the conversion, either cpu or cuda.' ) lowercase : Tuple = parser.parse_args() convert_musicgen_checkpoint(args.checkpoint, args.pytorch_dump_folder, args.push_to_hub)
311
'''simple docstring''' from __future__ import annotations from random import random class A : def __init__( self , SCREAMING_SNAKE_CASE = None ) -> Tuple: """simple docstring""" A : Optional[Any] = value A : Any = random() A : Node | None = None A : Node | None = None def __repr__( self ) -> str: """simple docstring""" from pprint import pformat if self.left is None and self.right is None: return F'\'{self.value}: {self.prior:.5}\'' else: return pformat( {F'{self.value}: {self.prior:.5}': (self.left, self.right)} , indent=1 ) def __str__( self ) -> str: """simple docstring""" A : Optional[Any] = str(self.value ) + ''' ''' A : Union[str, Any] = str(self.left or '''''' ) A : Any = str(self.right or '''''' ) return value + left + right def lowerCAmelCase_ ( snake_case__ , snake_case__ ): '''simple docstring''' if root is None: # None tree is split into 2 Nones return None, None elif root.value is None: return None, None else: if value < root.value: A, A : Any = split(root.left , snake_case__ ) return left, root else: A, A : Optional[int] = split(root.right , snake_case__ ) return root, right def lowerCAmelCase_ ( snake_case__ , snake_case__ ): '''simple docstring''' if (not left) or (not right): # If one node is None, return the other return left or right elif left.prior < right.prior: A : List[str] = merge(left.right , snake_case__ ) return left else: A : Tuple = merge(snake_case__ , right.left ) return right def lowerCAmelCase_ ( snake_case__ , snake_case__ ): '''simple docstring''' A : List[Any] = Node(snake_case__ ) A, A : Tuple = split(snake_case__ , snake_case__ ) return merge(merge(snake_case__ , snake_case__ ) , snake_case__ ) def lowerCAmelCase_ ( snake_case__ , snake_case__ ): '''simple docstring''' A, A : Dict = split(snake_case__ , value - 1 ) A, A : Any = split(snake_case__ , snake_case__ ) return merge(snake_case__ , snake_case__ ) def lowerCAmelCase_ ( snake_case__ ): '''simple docstring''' if not root: # None return else: inorder(root.left ) print(root.value , end=''',''' ) inorder(root.right ) def lowerCAmelCase_ ( snake_case__ , snake_case__ ): '''simple docstring''' for arg in args.split(): if arg[0] == "+": A : int = insert(snake_case__ , int(arg[1:] ) ) elif arg[0] == "-": A : int = erase(snake_case__ , int(arg[1:] ) ) else: print('''Unknown command''' ) return root def lowerCAmelCase_ ( ): '''simple docstring''' A : Union[str, Any] = None print( '''enter numbers to create a tree, + value to add value into treap, ''' '''- value to erase all nodes with value. \'q\' to quit. ''' ) A : Optional[int] = input() while args != "q": A : str = interact_treap(snake_case__ , snake_case__ ) print(snake_case__ ) A : Union[str, Any] = input() print('''good by!''' ) if __name__ == "__main__": import doctest doctest.testmod() main()
311
1
'''simple docstring''' from datasets.utils.patching import _PatchedModuleObj, patch_submodule from . import _test_patching def _UpperCamelCase ( ) -> List[Any]: '''simple docstring''' import os as original_os from os import path as original_path from os import rename as original_rename from os.path import dirname as original_dirname from os.path import join as original_join assert _test_patching.os is original_os assert _test_patching.path is original_path assert _test_patching.join is original_join assert _test_patching.renamed_os is original_os assert _test_patching.renamed_path is original_path assert _test_patching.renamed_join is original_join UpperCamelCase__ = "__test_patch_submodule_mock__" with patch_submodule(_test_patching , "os.path.join" , __A ): # Every way to access os.path.join must be patched, and the rest must stay untouched # check os.path.join assert isinstance(_test_patching.os , _PatchedModuleObj ) assert isinstance(_test_patching.os.path , _PatchedModuleObj ) assert _test_patching.os.path.join is mock # check path.join assert isinstance(_test_patching.path , _PatchedModuleObj ) assert _test_patching.path.join is mock # check join assert _test_patching.join is mock # check that the other attributes are untouched assert _test_patching.os.rename is original_rename assert _test_patching.path.dirname is original_dirname assert _test_patching.os.path.dirname is original_dirname # Even renamed modules or objects must be patched # check renamed_os.path.join assert isinstance(_test_patching.renamed_os , _PatchedModuleObj ) assert isinstance(_test_patching.renamed_os.path , _PatchedModuleObj ) assert _test_patching.renamed_os.path.join is mock # check renamed_path.join assert isinstance(_test_patching.renamed_path , _PatchedModuleObj ) assert _test_patching.renamed_path.join is mock # check renamed_join assert _test_patching.renamed_join is mock # check that the other attributes are untouched assert _test_patching.renamed_os.rename is original_rename assert _test_patching.renamed_path.dirname is original_dirname assert _test_patching.renamed_os.path.dirname is original_dirname # check that everthing is back to normal when the patch is over assert _test_patching.os is original_os assert _test_patching.path is original_path assert _test_patching.join is original_join assert _test_patching.renamed_os is original_os assert _test_patching.renamed_path is original_path assert _test_patching.renamed_join is original_join def _UpperCamelCase ( ) -> Optional[Any]: '''simple docstring''' assert _test_patching.open is open UpperCamelCase__ = "__test_patch_submodule_builtin_mock__" # _test_patching has "open" in its globals assert _test_patching.open is open with patch_submodule(_test_patching , "open" , __A ): assert _test_patching.open is mock # check that everthing is back to normal when the patch is over assert _test_patching.open is open def _UpperCamelCase ( ) -> Tuple: '''simple docstring''' UpperCamelCase__ = "__test_patch_submodule_missing_mock__" with patch_submodule(_test_patching , "pandas.read_csv" , __A ): pass def _UpperCamelCase ( ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ = "__test_patch_submodule_missing_builtin_mock__" # _test_patching doesn't have "len" in its globals assert getattr(_test_patching , "len" , __A ) is None with patch_submodule(_test_patching , "len" , __A ): assert _test_patching.len is mock assert _test_patching.len is len def _UpperCamelCase ( ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ = "__test_patch_submodule_start_and_stop_mock__" UpperCamelCase__ = patch_submodule(_test_patching , "open" , __A ) assert _test_patching.open is open patch.start() assert _test_patching.open is mock patch.stop() assert _test_patching.open is open def _UpperCamelCase ( ) -> List[str]: '''simple docstring''' from os import rename as original_rename from os.path import dirname as original_dirname from os.path import join as original_join UpperCamelCase__ = "__test_patch_submodule_successive_join__" UpperCamelCase__ = "__test_patch_submodule_successive_dirname__" UpperCamelCase__ = "__test_patch_submodule_successive_rename__" assert _test_patching.os.path.join is original_join assert _test_patching.os.path.dirname is original_dirname assert _test_patching.os.rename is original_rename with patch_submodule(_test_patching , "os.path.join" , __A ): with patch_submodule(_test_patching , "os.rename" , __A ): with patch_submodule(_test_patching , "os.path.dirname" , __A ): assert _test_patching.os.path.join is mock_join assert _test_patching.os.path.dirname is mock_dirname assert _test_patching.os.rename is mock_rename # try another order with patch_submodule(_test_patching , "os.rename" , __A ): with patch_submodule(_test_patching , "os.path.join" , __A ): with patch_submodule(_test_patching , "os.path.dirname" , __A ): assert _test_patching.os.path.join is mock_join assert _test_patching.os.path.dirname is mock_dirname assert _test_patching.os.rename is mock_rename assert _test_patching.os.path.join is original_join assert _test_patching.os.path.dirname is original_dirname assert _test_patching.os.rename is original_rename def _UpperCamelCase ( ) -> Dict: '''simple docstring''' UpperCamelCase__ = "__test_patch_submodule_doesnt_exist_mock__" with patch_submodule(_test_patching , "__module_that_doesn_exist__.__attribute_that_doesn_exist__" , __A ): pass with patch_submodule(_test_patching , "os.__attribute_that_doesn_exist__" , __A ): pass
80
import json import os import unittest from transformers import AutoTokenizer, GPTaTokenizer, GPTaTokenizerFast from transformers.models.gpta.tokenization_gpta import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __lowerCAmelCase ( __magic_name__ , unittest.TestCase ): UpperCamelCase__ = GPTaTokenizer UpperCamelCase__ = GPTaTokenizerFast UpperCamelCase__ = True UpperCamelCase__ = {'''add_prefix_space''': True} UpperCamelCase__ = False def lowerCamelCase__ ( self :Optional[Any] ): '''simple docstring''' super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", """<|endoftext|>""", ] a = dict(zip(__magic_name__ , range(len(__magic_name__ ) ) ) ) a = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] a = {"""unk_token""": """<unk>"""} a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(__magic_name__ ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(__magic_name__ ) ) def lowerCamelCase__ ( self :Dict , **__magic_name__ :List[str] ): '''simple docstring''' kwargs.update(self.special_tokens_map ) return GPTaTokenizer.from_pretrained(self.tmpdirname , **__magic_name__ ) def lowerCamelCase__ ( self :List[str] , **__magic_name__ :Optional[Any] ): '''simple docstring''' kwargs.update(self.special_tokens_map ) return GPTaTokenizerFast.from_pretrained(self.tmpdirname , **__magic_name__ ) def lowerCamelCase__ ( self :Dict , __magic_name__ :List[str] ): '''simple docstring''' a = """lower newer""" a = """lower newer""" return input_text, output_text def lowerCamelCase__ ( self :int ): '''simple docstring''' a = GPTaTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) a = """lower newer""" a = ["""\u0120low""", """er""", """\u0120""", """n""", """e""", """w""", """er"""] a = tokenizer.tokenize(__magic_name__ , add_prefix_space=__magic_name__ ) self.assertListEqual(__magic_name__ , __magic_name__ ) a = tokens + [tokenizer.unk_token] a = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__magic_name__ ) , __magic_name__ ) def lowerCamelCase__ ( self :int ): '''simple docstring''' if not self.test_rust_tokenizer: return a = self.get_tokenizer() a = self.get_rust_tokenizer(add_prefix_space=__magic_name__ ) a = """lower newer""" # Testing tokenization a = tokenizer.tokenize(__magic_name__ , add_prefix_space=__magic_name__ ) a = rust_tokenizer.tokenize(__magic_name__ ) self.assertListEqual(__magic_name__ , __magic_name__ ) # Testing conversion to ids without special tokens a = tokenizer.encode(__magic_name__ , add_special_tokens=__magic_name__ , add_prefix_space=__magic_name__ ) a = rust_tokenizer.encode(__magic_name__ , add_special_tokens=__magic_name__ ) self.assertListEqual(__magic_name__ , __magic_name__ ) # Testing conversion to ids with special tokens a = self.get_rust_tokenizer(add_prefix_space=__magic_name__ ) a = tokenizer.encode(__magic_name__ , add_prefix_space=__magic_name__ ) a = rust_tokenizer.encode(__magic_name__ ) self.assertListEqual(__magic_name__ , __magic_name__ ) # Testing the unknown token a = tokens + [rust_tokenizer.unk_token] a = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(rust_tokenizer.convert_tokens_to_ids(__magic_name__ ) , __magic_name__ ) def lowerCamelCase__ ( self :Optional[int] , *__magic_name__ :Tuple , **__magic_name__ :str ): '''simple docstring''' pass def lowerCamelCase__ ( self :Optional[Any] , __magic_name__ :Union[str, Any]=15 ): '''simple docstring''' for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})' ): a = self.rust_tokenizer_class.from_pretrained(__magic_name__ , **__magic_name__ ) # Simple input a = """This is a simple input""" a = ["""This is a simple input 1""", """This is a simple input 2"""] a = ("""This is a simple input""", """This is a pair""") a = [ ("""This is a simple input 1""", """This is a simple input 2"""), ("""This is a simple pair 1""", """This is a simple pair 2"""), ] # Simple input tests self.assertRaises(__magic_name__ , tokenizer_r.encode , __magic_name__ , max_length=__magic_name__ , padding="""max_length""" ) # Simple input self.assertRaises(__magic_name__ , tokenizer_r.encode_plus , __magic_name__ , max_length=__magic_name__ , padding="""max_length""" ) # Simple input self.assertRaises( __magic_name__ , tokenizer_r.batch_encode_plus , __magic_name__ , max_length=__magic_name__ , padding="""max_length""" , ) # Pair input self.assertRaises(__magic_name__ , tokenizer_r.encode , __magic_name__ , max_length=__magic_name__ , padding="""max_length""" ) # Pair input self.assertRaises(__magic_name__ , tokenizer_r.encode_plus , __magic_name__ , max_length=__magic_name__ , padding="""max_length""" ) # Pair input self.assertRaises( __magic_name__ , tokenizer_r.batch_encode_plus , __magic_name__ , max_length=__magic_name__ , padding="""max_length""" , ) def lowerCamelCase__ ( self :str ): '''simple docstring''' a = GPTaTokenizer.from_pretrained(self.tmpdirname , pad_token="""<pad>""" ) # Simple input a = """This is a simple input""" a = ["""This is a simple input looooooooong""", """This is a simple input"""] a = ("""This is a simple input""", """This is a pair""") a = [ ("""This is a simple input loooooong""", """This is a simple input"""), ("""This is a simple pair loooooong""", """This is a simple pair"""), ] a = tokenizer.pad_token_id a = tokenizer(__magic_name__ , padding="""max_length""" , max_length=30 , return_tensors="""np""" ) a = tokenizer(__magic_name__ , padding=__magic_name__ , truncate=__magic_name__ , return_tensors="""np""" ) a = tokenizer(*__magic_name__ , padding="""max_length""" , max_length=60 , return_tensors="""np""" ) a = tokenizer(__magic_name__ , padding=__magic_name__ , truncate=__magic_name__ , return_tensors="""np""" ) # s # test single string max_length padding self.assertEqual(out_s["""input_ids"""].shape[-1] , 30 ) self.assertTrue(pad_token_id in out_s["""input_ids"""] ) self.assertTrue(0 in out_s["""attention_mask"""] ) # s2 # test automatic padding self.assertEqual(out_sa["""input_ids"""].shape[-1] , 33 ) # long slice doesn't have padding self.assertFalse(pad_token_id in out_sa["""input_ids"""][0] ) self.assertFalse(0 in out_sa["""attention_mask"""][0] ) # short slice does have padding self.assertTrue(pad_token_id in out_sa["""input_ids"""][1] ) self.assertTrue(0 in out_sa["""attention_mask"""][1] ) # p # test single pair max_length padding self.assertEqual(out_p["""input_ids"""].shape[-1] , 60 ) self.assertTrue(pad_token_id in out_p["""input_ids"""] ) self.assertTrue(0 in out_p["""attention_mask"""] ) # p2 # test automatic padding pair self.assertEqual(out_pa["""input_ids"""].shape[-1] , 52 ) # long slice pair doesn't have padding self.assertFalse(pad_token_id in out_pa["""input_ids"""][0] ) self.assertFalse(0 in out_pa["""attention_mask"""][0] ) # short slice pair does have padding self.assertTrue(pad_token_id in out_pa["""input_ids"""][1] ) self.assertTrue(0 in out_pa["""attention_mask"""][1] ) def lowerCamelCase__ ( self :Union[str, Any] ): '''simple docstring''' a = """$$$""" a = GPTaTokenizer.from_pretrained(self.tmpdirname , bos_token=__magic_name__ , add_bos_token=__magic_name__ ) a = """This is a simple input""" a = ["""This is a simple input 1""", """This is a simple input 2"""] a = tokenizer.bos_token_id a = tokenizer(__magic_name__ ) a = tokenizer(__magic_name__ ) self.assertEqual(out_s.input_ids[0] , __magic_name__ ) self.assertTrue(all(o[0] == bos_token_id for o in out_sa.input_ids ) ) a = tokenizer.decode(out_s.input_ids ) a = tokenizer.batch_decode(out_sa.input_ids ) self.assertEqual(decode_s.split()[0] , __magic_name__ ) self.assertTrue(all(d.split()[0] == bos_token for d in decode_sa ) ) def lowerCamelCase__ ( self :Optional[int] ): '''simple docstring''' pass def lowerCamelCase__ ( self :Any ): '''simple docstring''' a = [self.get_tokenizer(do_lower_case=__magic_name__ , add_bos_token=__magic_name__ )] for tokenizer in tokenizers: with self.subTest(F'{tokenizer.__class__.__name__}' ): a = """Encode this.""" a = """This one too please.""" a = tokenizer.encode(__magic_name__ , add_special_tokens=__magic_name__ ) encoded_sequence += tokenizer.encode(__magic_name__ , add_special_tokens=__magic_name__ ) a = tokenizer.encode_plus( __magic_name__ , __magic_name__ , add_special_tokens=__magic_name__ , return_special_tokens_mask=__magic_name__ , ) a = encoded_sequence_dict["""input_ids"""] a = encoded_sequence_dict["""special_tokens_mask"""] self.assertEqual(len(__magic_name__ ) , len(__magic_name__ ) ) a = [ (x if not special_tokens_mask[i] else None) for i, x in enumerate(__magic_name__ ) ] a = [x for x in filtered_sequence if x is not None] self.assertEqual(__magic_name__ , __magic_name__ ) @require_tokenizers class __lowerCAmelCase ( unittest.TestCase ): def lowerCamelCase__ ( self :Optional[int] ): '''simple docstring''' a = AutoTokenizer.from_pretrained("""facebook/opt-350m""" , from_slow=__magic_name__ ) a = """A photo of a cat""" a = tokenizer.encode( __magic_name__ , ) self.assertEqual(__magic_name__ , [2, 250, 1345, 9, 10, 4758] ) tokenizer.save_pretrained("""test_opt""" ) a = AutoTokenizer.from_pretrained("""./test_opt""" ) a = tokenizer.encode( __magic_name__ , ) self.assertEqual(__magic_name__ , [2, 250, 1345, 9, 10, 4758] ) def lowerCamelCase__ ( self :Union[str, Any] ): '''simple docstring''' a = AutoTokenizer.from_pretrained("""facebook/opt-350m""" , use_slow=__magic_name__ ) a = """A photo of a cat""" a = tokenizer.encode( __magic_name__ , ) # Same as above self.assertEqual(__magic_name__ , [2, 250, 1345, 9, 10, 4758] ) @unittest.skip("""This test is failing because of a bug in the fast tokenizer""" ) def lowerCamelCase__ ( self :str ): '''simple docstring''' a = AutoTokenizer.from_pretrained("""facebook/opt-350m""" , from_slow=__magic_name__ ) a = """bos""" a = tokenizer.get_vocab()["""bos"""] a = """A photo of a cat""" a = tokenizer.encode( __magic_name__ , ) # We changed the bos token self.assertEqual(__magic_name__ , [3_1957, 250, 1345, 9, 10, 4758] ) tokenizer.save_pretrained("""./tok""" ) a = AutoTokenizer.from_pretrained("""./tok""" ) self.assertTrue(tokenizer.is_fast ) a = tokenizer.encode( __magic_name__ , ) self.assertEqual(__magic_name__ , [3_1957, 250, 1345, 9, 10, 4758] )
228
0
__A = { '''Pillow''': '''Pillow<10.0.0''', '''accelerate''': '''accelerate>=0.20.3''', '''av''': '''av==9.2.0''', '''beautifulsoup4''': '''beautifulsoup4''', '''black''': '''black~=23.1''', '''codecarbon''': '''codecarbon==1.2.0''', '''cookiecutter''': '''cookiecutter==1.7.3''', '''dataclasses''': '''dataclasses''', '''datasets''': '''datasets!=2.5.0''', '''decord''': '''decord==0.6.0''', '''deepspeed''': '''deepspeed>=0.9.3''', '''diffusers''': '''diffusers''', '''dill''': '''dill<0.3.5''', '''evaluate''': '''evaluate>=0.2.0''', '''fairscale''': '''fairscale>0.3''', '''faiss-cpu''': '''faiss-cpu''', '''fastapi''': '''fastapi''', '''filelock''': '''filelock''', '''flax''': '''flax>=0.4.1,<=0.7.0''', '''ftfy''': '''ftfy''', '''fugashi''': '''fugashi>=1.0''', '''GitPython''': '''GitPython<3.1.19''', '''hf-doc-builder''': '''hf-doc-builder>=0.3.0''', '''huggingface-hub''': '''huggingface-hub>=0.14.1,<1.0''', '''importlib_metadata''': '''importlib_metadata''', '''ipadic''': '''ipadic>=1.0.0,<2.0''', '''isort''': '''isort>=5.5.4''', '''jax''': '''jax>=0.2.8,!=0.3.2,<=0.4.13''', '''jaxlib''': '''jaxlib>=0.1.65,<=0.4.13''', '''jieba''': '''jieba''', '''kenlm''': '''kenlm''', '''keras-nlp''': '''keras-nlp>=0.3.1''', '''librosa''': '''librosa''', '''nltk''': '''nltk''', '''natten''': '''natten>=0.14.6''', '''numpy''': '''numpy>=1.17''', '''onnxconverter-common''': '''onnxconverter-common''', '''onnxruntime-tools''': '''onnxruntime-tools>=1.4.2''', '''onnxruntime''': '''onnxruntime>=1.4.0''', '''opencv-python''': '''opencv-python''', '''optuna''': '''optuna''', '''optax''': '''optax>=0.0.8,<=0.1.4''', '''packaging''': '''packaging>=20.0''', '''parameterized''': '''parameterized''', '''phonemizer''': '''phonemizer''', '''protobuf''': '''protobuf''', '''psutil''': '''psutil''', '''pyyaml''': '''pyyaml>=5.1''', '''pydantic''': '''pydantic<2''', '''pytest''': '''pytest>=7.2.0''', '''pytest-timeout''': '''pytest-timeout''', '''pytest-xdist''': '''pytest-xdist''', '''python''': '''python>=3.8.0''', '''ray[tune]''': '''ray[tune]''', '''regex''': '''regex!=2019.12.17''', '''requests''': '''requests''', '''rhoknp''': '''rhoknp>=1.1.0,<1.3.1''', '''rjieba''': '''rjieba''', '''rouge-score''': '''rouge-score!=0.0.7,!=0.0.8,!=0.1,!=0.1.1''', '''ruff''': '''ruff>=0.0.241,<=0.0.259''', '''sacrebleu''': '''sacrebleu>=1.4.12,<2.0.0''', '''sacremoses''': '''sacremoses''', '''safetensors''': '''safetensors>=0.3.1''', '''sagemaker''': '''sagemaker>=2.31.0''', '''scikit-learn''': '''scikit-learn''', '''sentencepiece''': '''sentencepiece>=0.1.91,!=0.1.92''', '''sigopt''': '''sigopt''', '''starlette''': '''starlette''', '''sudachipy''': '''sudachipy>=0.6.6''', '''sudachidict_core''': '''sudachidict_core>=20220729''', '''tensorflow-cpu''': '''tensorflow-cpu>=2.6,<2.14''', '''tensorflow''': '''tensorflow>=2.6,<2.14''', '''tensorflow-text''': '''tensorflow-text<2.14''', '''tf2onnx''': '''tf2onnx''', '''timeout-decorator''': '''timeout-decorator''', '''timm''': '''timm''', '''tokenizers''': '''tokenizers>=0.11.1,!=0.11.3,<0.14''', '''torch''': '''torch>=1.9,!=1.12.0''', '''torchaudio''': '''torchaudio''', '''torchvision''': '''torchvision''', '''pyctcdecode''': '''pyctcdecode>=0.4.0''', '''tqdm''': '''tqdm>=4.27''', '''unidic''': '''unidic>=1.0.2''', '''unidic_lite''': '''unidic_lite>=1.0.7''', '''urllib3''': '''urllib3<2.0.0''', '''uvicorn''': '''uvicorn''', }
278
from __future__ import annotations def snake_case_(_UpperCamelCase , _UpperCamelCase ) -> bool: """simple docstring""" _snake_case = get_failure_array(_UpperCamelCase ) # 2) Step through text searching for pattern _snake_case, _snake_case = 0, 0 # index into text, pattern while i < len(_UpperCamelCase ): if pattern[j] == text[i]: if j == (len(_UpperCamelCase ) - 1): return True j += 1 # if this is a prefix in our pattern # just go back far enough to continue elif j > 0: _snake_case = failure[j - 1] continue i += 1 return False def snake_case_(_UpperCamelCase ) -> list[int]: """simple docstring""" _snake_case = [0] _snake_case = 0 _snake_case = 1 while j < len(_UpperCamelCase ): if pattern[i] == pattern[j]: i += 1 elif i > 0: _snake_case = failure[i - 1] continue j += 1 failure.append(_UpperCamelCase ) return failure if __name__ == "__main__": # Test 1) __A = '''abc1abc12''' __A = '''alskfjaldsabc1abc1abc12k23adsfabcabc''' __A = '''alskfjaldsk23adsfabcabc''' assert kmp(pattern, texta) and not kmp(pattern, texta) # Test 2) __A = '''ABABX''' __A = '''ABABZABABYABABX''' assert kmp(pattern, text) # Test 3) __A = '''AAAB''' __A = '''ABAAAAAB''' assert kmp(pattern, text) # Test 4) __A = '''abcdabcy''' __A = '''abcxabcdabxabcdabcdabcy''' assert kmp(pattern, text) # Test 5) __A = '''aabaabaaa''' assert get_failure_array(pattern) == [0, 1, 0, 1, 2, 3, 4, 5, 2]
278
1
"""simple docstring""" from collections.abc import Iterator, MutableMapping from dataclasses import dataclass from typing import Generic, TypeVar _a = TypeVar('KEY') _a = TypeVar('VAL') @dataclass(frozen=lowercase__ ,slots=lowercase__ ) class A_ (Generic[KEY, VAL] ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : KEY SCREAMING_SNAKE_CASE__ : VAL class A_ (_Item ): '''simple docstring''' def __init__( self ): """simple docstring""" super().__init__(lowercase_ , lowercase_ ) def __bool__( self ): """simple docstring""" return False _a = _DeletedItem() class A_ (MutableMapping[KEY, VAL] ): '''simple docstring''' def __init__( self , lowercase_ = 8 , lowercase_ = 0.75 ): """simple docstring""" UpperCAmelCase_ : Tuple = initial_block_size UpperCAmelCase_ : list[_Item | None] = [None] * initial_block_size assert 0.0 < capacity_factor < 1.0 UpperCAmelCase_ : Optional[Any] = capacity_factor UpperCAmelCase_ : int = 0 def UpperCamelCase__ ( self , lowercase_ ): """simple docstring""" return hash(lowercase_ ) % len(self._buckets ) def UpperCamelCase__ ( self , lowercase_ ): """simple docstring""" return (ind + 1) % len(self._buckets ) def UpperCamelCase__ ( self , lowercase_ , lowercase_ , lowercase_ ): """simple docstring""" UpperCAmelCase_ : str = self._buckets[ind] if not stored: UpperCAmelCase_ : List[Any] = _Item(lowercase_ , lowercase_ ) self._len += 1 return True elif stored.key == key: UpperCAmelCase_ : Tuple = _Item(lowercase_ , lowercase_ ) return True else: return False def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Tuple = len(self._buckets ) * self._capacity_factor return len(self ) >= int(lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" if len(self._buckets ) <= self._initial_block_size: return False UpperCAmelCase_ : List[str] = len(self._buckets ) * self._capacity_factor / 2 return len(self ) < limit def UpperCamelCase__ ( self , lowercase_ ): """simple docstring""" UpperCAmelCase_ : List[Any] = self._buckets UpperCAmelCase_ : Any = [None] * new_size UpperCAmelCase_ : Any = 0 for item in old_buckets: if item: self._add_item(item.key , item.val ) def UpperCamelCase__ ( self ): """simple docstring""" self._resize(len(self._buckets ) * 2 ) def UpperCamelCase__ ( self ): """simple docstring""" self._resize(len(self._buckets ) // 2 ) def UpperCamelCase__ ( self , lowercase_ ): """simple docstring""" UpperCAmelCase_ : str = self._get_bucket_index(lowercase_ ) for _ in range(len(self._buckets ) ): yield ind UpperCAmelCase_ : Tuple = self._get_next_ind(lowercase_ ) def UpperCamelCase__ ( self , lowercase_ , lowercase_ ): """simple docstring""" for ind in self._iterate_buckets(lowercase_ ): if self._try_set(lowercase_ , lowercase_ , lowercase_ ): break def __setitem__( self , lowercase_ , lowercase_ ): """simple docstring""" if self._is_full(): self._size_up() self._add_item(lowercase_ , lowercase_ ) def __delitem__( self , lowercase_ ): """simple docstring""" for ind in self._iterate_buckets(lowercase_ ): UpperCAmelCase_ : Optional[Any] = self._buckets[ind] if item is None: raise KeyError(lowercase_ ) if item is _deleted: continue if item.key == key: UpperCAmelCase_ : Optional[Any] = _deleted self._len -= 1 break if self._is_sparse(): self._size_down() def __getitem__( self , lowercase_ ): """simple docstring""" for ind in self._iterate_buckets(lowercase_ ): UpperCAmelCase_ : Dict = self._buckets[ind] if item is None: break if item is _deleted: continue if item.key == key: return item.val raise KeyError(lowercase_ ) def __len__( self ): """simple docstring""" return self._len def __iter__( self ): """simple docstring""" yield from (item.key for item in self._buckets if item) def __repr__( self ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = " ,".join( F"""{item.key}: {item.val}""" for item in self._buckets if item ) return F"""HashMap({val_string})"""
61
"""simple docstring""" import argparse import os import re import numpy as np import PIL import torch from timm import create_model from torch.optim.lr_scheduler import OneCycleLR from torch.utils.data import DataLoader, Dataset from torchvision.transforms import Compose, RandomResizedCrop, Resize, ToTensor from accelerate import Accelerator def lowerCAmelCase_ ( snake_case_ : Dict ) ->Tuple: lowerCamelCase__ : List[str] =fname.split(os.path.sep )[-1] return re.search(R'^(.*)_\d+\.jpg$' , snake_case_ ).groups()[0] class A_ ( A__ ): """simple docstring""" def __init__( self :int , lowerCamelCase_ :int , lowerCamelCase_ :Any=None , lowerCamelCase_ :Any=None ): """simple docstring""" lowerCamelCase__ : Tuple =file_names lowerCamelCase__ : str =image_transform lowerCamelCase__ : str =label_to_id def __len__( self :Optional[int] ): """simple docstring""" return len(self.file_names ) def __getitem__( self :Optional[Any] , lowerCamelCase_ :Optional[int] ): """simple docstring""" lowerCamelCase__ : Tuple =self.file_names[idx] lowerCamelCase__ : Dict =PIL.Image.open(lowerCamelCase_ ) lowerCamelCase__ : Dict =raw_image.convert('RGB' ) if self.image_transform is not None: lowerCamelCase__ : int =self.image_transform(lowerCamelCase_ ) lowerCamelCase__ : List[str] =extract_label(lowerCamelCase_ ) if self.label_to_id is not None: lowerCamelCase__ : Optional[int] =self.label_to_id[label] return {"image": image, "label": label} def lowerCAmelCase_ ( snake_case_ : Dict , snake_case_ : Any ) ->Dict: # Initialize accelerator if args.with_tracking: lowerCamelCase__ : List[str] =Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , log_with='all' , project_dir=args.project_dir ) else: lowerCamelCase__ : Tuple =Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs lowerCamelCase__ : Optional[Any] =config['lr'] lowerCamelCase__ : List[str] =int(config['num_epochs'] ) lowerCamelCase__ : List[str] =int(config['seed'] ) lowerCamelCase__ : Dict =int(config['batch_size'] ) lowerCamelCase__ : Optional[int] =config['image_size'] if not isinstance(snake_case_ , (list, tuple) ): lowerCamelCase__ : Optional[int] =(image_size, image_size) # Parse out whether we are saving every epoch or after a certain number of batches if hasattr(args.checkpointing_steps , 'isdigit' ): if args.checkpointing_steps == "epoch": lowerCamelCase__ : Optional[Any] =args.checkpointing_steps elif args.checkpointing_steps.isdigit(): lowerCamelCase__ : Union[str, Any] =int(args.checkpointing_steps ) else: raise ValueError( f"""Argument `checkpointing_steps` must be either a number or `epoch`. `{args.checkpointing_steps}` passed.""" ) else: lowerCamelCase__ : int =None # We need to initialize the trackers we use, and also store our configuration if args.with_tracking: lowerCamelCase__ : Tuple =os.path.split(snake_case_ )[-1].split('.' )[0] accelerator.init_trackers(snake_case_ , snake_case_ ) # Grab all the image filenames lowerCamelCase__ : List[str] =[os.path.join(args.data_dir , snake_case_ ) for fname in os.listdir(args.data_dir ) if fname.endswith('.jpg' )] # Build the label correspondences lowerCamelCase__ : str =[extract_label(snake_case_ ) for fname in file_names] lowerCamelCase__ : Any =list(set(snake_case_ ) ) id_to_label.sort() lowerCamelCase__ : List[Any] ={lbl: i for i, lbl in enumerate(snake_case_ )} # Set the seed before splitting the data. np.random.seed(snake_case_ ) torch.manual_seed(snake_case_ ) torch.cuda.manual_seed_all(snake_case_ ) # Split our filenames between train and validation lowerCamelCase__ : int =np.random.permutation(len(snake_case_ ) ) lowerCamelCase__ : Tuple =int(0.8 * len(snake_case_ ) ) lowerCamelCase__ : str =random_perm[:cut] lowerCamelCase__ : Dict =random_perm[cut:] # For training we use a simple RandomResizedCrop lowerCamelCase__ : str =Compose([RandomResizedCrop(snake_case_ , scale=(0.5, 1.0) ), ToTensor()] ) lowerCamelCase__ : Any =PetsDataset( [file_names[i] for i in train_split] , image_transform=snake_case_ , label_to_id=snake_case_ ) # For evaluation, we use a deterministic Resize lowerCamelCase__ : Optional[int] =Compose([Resize(snake_case_ ), ToTensor()] ) lowerCamelCase__ : Dict =PetsDataset([file_names[i] for i in eval_split] , image_transform=snake_case_ , label_to_id=snake_case_ ) # Instantiate dataloaders. lowerCamelCase__ : Optional[Any] =DataLoader(snake_case_ , shuffle=snake_case_ , batch_size=snake_case_ , num_workers=4 ) lowerCamelCase__ : int =DataLoader(snake_case_ , shuffle=snake_case_ , batch_size=snake_case_ , num_workers=4 ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) lowerCamelCase__ : Dict =create_model('resnet50d' , pretrained=snake_case_ , num_classes=len(snake_case_ ) ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). lowerCamelCase__ : str =model.to(accelerator.device ) # Freezing the base model for param in model.parameters(): lowerCamelCase__ : Dict =False for param in model.get_classifier().parameters(): lowerCamelCase__ : List[str] =True # We normalize the batches of images to be a bit faster. lowerCamelCase__ : Any =torch.tensor(model.default_cfg['mean'] )[None, :, None, None].to(accelerator.device ) lowerCamelCase__ : Dict =torch.tensor(model.default_cfg['std'] )[None, :, None, None].to(accelerator.device ) # Instantiate optimizer lowerCamelCase__ : int =torch.optim.Adam(params=model.parameters() , lr=lr / 2_5 ) # Instantiate learning rate scheduler lowerCamelCase__ : Dict =OneCycleLR(optimizer=snake_case_ , max_lr=snake_case_ , epochs=snake_case_ , steps_per_epoch=len(snake_case_ ) ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Any =accelerator.prepare( snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ) # We need to keep track of how many total steps we have iterated over lowerCamelCase__ : int =0 # We also need to keep track of the starting epoch so files are named properly lowerCamelCase__ : Optional[int] =0 # Potentially load in the weights and states from a previous save if args.resume_from_checkpoint: if args.resume_from_checkpoint is not None or args.resume_from_checkpoint != "": accelerator.print(f"""Resumed from checkpoint: {args.resume_from_checkpoint}""" ) accelerator.load_state(args.resume_from_checkpoint ) lowerCamelCase__ : int =os.path.basename(args.resume_from_checkpoint ) else: # Get the most recent checkpoint lowerCamelCase__ : int =[f.name for f in os.scandir(os.getcwd() ) if f.is_dir()] dirs.sort(key=os.path.getctime ) lowerCamelCase__ : Optional[int] =dirs[-1] # Sorts folders by date modified, most recent checkpoint is the last # Extract `epoch_{i}` or `step_{i}` lowerCamelCase__ : Tuple =os.path.splitext(snake_case_ )[0] if "epoch" in training_difference: lowerCamelCase__ : Union[str, Any] =int(training_difference.replace('epoch_' , '' ) ) + 1 lowerCamelCase__ : Optional[int] =None else: lowerCamelCase__ : List[Any] =int(training_difference.replace('step_' , '' ) ) lowerCamelCase__ : int =resume_step // len(snake_case_ ) resume_step -= starting_epoch * len(snake_case_ ) # Now we train the model for epoch in range(snake_case_ , snake_case_ ): model.train() if args.with_tracking: lowerCamelCase__ : str =0 if args.resume_from_checkpoint and epoch == starting_epoch and resume_step is not None: # We need to skip steps until we reach the resumed step lowerCamelCase__ : Tuple =accelerator.skip_first_batches(snake_case_ , snake_case_ ) overall_step += resume_step else: # After the first iteration though, we need to go back to the original dataloader lowerCamelCase__ : str =train_dataloader for batch in active_dataloader: # We could avoid this line since we set the accelerator with `device_placement=True`. lowerCamelCase__ : int ={k: v.to(accelerator.device ) for k, v in batch.items()} lowerCamelCase__ : List[str] =(batch['image'] - mean) / std lowerCamelCase__ : Any =model(snake_case_ ) lowerCamelCase__ : List[Any] =torch.nn.functional.cross_entropy(snake_case_ , batch['label'] ) # We keep track of the loss at each epoch if args.with_tracking: total_loss += loss.detach().float() accelerator.backward(snake_case_ ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 if isinstance(snake_case_ , snake_case_ ): lowerCamelCase__ : int =f"""step_{overall_step}""" if overall_step % checkpointing_steps == 0: if args.output_dir is not None: lowerCamelCase__ : List[Any] =os.path.join(args.output_dir , snake_case_ ) accelerator.save_state(snake_case_ ) model.eval() lowerCamelCase__ : int =0 lowerCamelCase__ : Optional[Any] =0 for step, batch in enumerate(snake_case_ ): # We could avoid this line since we set the accelerator with `device_placement=True`. lowerCamelCase__ : Union[str, Any] ={k: v.to(accelerator.device ) for k, v in batch.items()} lowerCamelCase__ : int =(batch['image'] - mean) / std with torch.no_grad(): lowerCamelCase__ : List[str] =model(snake_case_ ) lowerCamelCase__ : Optional[int] =outputs.argmax(dim=-1 ) lowerCamelCase__ , lowerCamelCase__ : Union[str, Any] =accelerator.gather_for_metrics((predictions, batch['label']) ) lowerCamelCase__ : Union[str, Any] =predictions == references num_elems += accurate_preds.shape[0] accurate += accurate_preds.long().sum() lowerCamelCase__ : List[str] =accurate.item() / num_elems # Use accelerator.print to print only on the main process. accelerator.print(f"""epoch {epoch}: {1_0_0 * eval_metric:.2f}""" ) if args.with_tracking: accelerator.log( { 'accuracy': 1_0_0 * eval_metric, 'train_loss': total_loss.item() / len(snake_case_ ), 'epoch': epoch, } , step=snake_case_ , ) if checkpointing_steps == "epoch": lowerCamelCase__ : Tuple =f"""epoch_{epoch}""" if args.output_dir is not None: lowerCamelCase__ : Tuple =os.path.join(args.output_dir , snake_case_ ) accelerator.save_state(snake_case_ ) if args.with_tracking: accelerator.end_training() def lowerCAmelCase_ ( ) ->int: lowerCamelCase__ : Any =argparse.ArgumentParser(description='Simple example of training script.' ) parser.add_argument('--data_dir' , required=snake_case_ , help='The data folder on disk.' ) parser.add_argument('--fp16' , action='store_true' , help='If passed, will use FP16 training.' ) parser.add_argument( '--mixed_precision' , type=snake_case_ , default=snake_case_ , choices=['no', 'fp16', 'bf16', 'fp8'] , help='Whether to use mixed precision. Choose' 'between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.' 'and an Nvidia Ampere GPU.' , ) parser.add_argument('--cpu' , action='store_true' , help='If passed, will train on the CPU.' ) parser.add_argument( '--checkpointing_steps' , type=snake_case_ , default=snake_case_ , help='Whether the various states should be saved at the end of every n steps, or \'epoch\' for each epoch.' , ) parser.add_argument( '--output_dir' , type=snake_case_ , default='.' , help='Optional save directory where all checkpoint folders will be stored. Default is the current working directory.' , ) parser.add_argument( '--resume_from_checkpoint' , type=snake_case_ , default=snake_case_ , help='If the training should continue from a checkpoint folder.' , ) parser.add_argument( '--with_tracking' , action='store_true' , help='Whether to load in all available experiment trackers from the environment and use them for logging.' , ) parser.add_argument( '--project_dir' , type=snake_case_ , default='logs' , help='Location on where to store experiment tracking logs` and relevent project information' , ) lowerCamelCase__ : Dict =parser.parse_args() lowerCamelCase__ : List[str] ={'lr': 3E-2, 'num_epochs': 3, 'seed': 4_2, 'batch_size': 6_4, 'image_size': 2_2_4} training_function(snake_case_ , snake_case_ ) if __name__ == "__main__": main()
126
0
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
282
from jiwer import compute_measures import datasets lowercase_ = "\\n@inproceedings{inproceedings,\n author = {Morris, Andrew and Maier, Viktoria and Green, Phil},\n year = {2004},\n month = {01},\n pages = {},\n title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.}\n}\n" lowercase_ = "\\nWord error rate (WER) is a common metric of the performance of an automatic speech recognition system.\n\nThe general difficulty of measuring performance lies in the fact that the recognized word sequence can have a different length from the reference word sequence (supposedly the correct one). The WER is derived from the Levenshtein distance, working at the word level instead of the phoneme level. The WER is a valuable tool for comparing different systems as well as for evaluating improvements within one system. This kind of measurement, however, provides no details on the nature of translation errors and further work is therefore required to identify the main source(s) of error and to focus any research effort.\n\nThis problem is solved by first aligning the recognized word sequence with the reference (spoken) word sequence using dynamic string alignment. Examination of this issue is seen through a theory called the power law that states the correlation between perplexity and word error rate.\n\nWord error rate can then be computed as:\n\nWER = (S + D + I) / N = (S + D + I) / (S + D + C)\n\nwhere\n\nS is the number of substitutions,\nD is the number of deletions,\nI is the number of insertions,\nC is the number of correct words,\nN is the number of words in the reference (N=S+D+C).\n\nThis value indicates the average number of errors per reference word. The lower the value, the better the\nperformance of the ASR system with a WER of 0 being a perfect score.\n" lowercase_ = "\nCompute WER score of transcribed segments against references.\n\nArgs:\n references: List of references for each speech input.\n predictions: List of transcriptions to score.\n concatenate_texts (bool, default=False): Whether to concatenate all input texts or compute WER iteratively.\n\nReturns:\n (float): the word error rate\n\nExamples:\n\n >>> predictions = [\"this is the prediction\", \"there is an other sample\"]\n >>> references = [\"this is the reference\", \"there is another one\"]\n >>> wer = datasets.load_metric(\"wer\")\n >>> wer_score = wer.compute(predictions=predictions, references=references)\n >>> print(wer_score)\n 0.5\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A ( datasets.Metric ): """simple docstring""" def snake_case__ ( self : Any )-> str: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION,citation=_CITATION,inputs_description=_KWARGS_DESCRIPTION,features=datasets.Features( { 'predictions': datasets.Value('string',id='sequence' ), 'references': datasets.Value('string',id='sequence' ), } ),codebase_urls=['https://github.com/jitsi/jiwer/'],reference_urls=[ 'https://en.wikipedia.org/wiki/Word_error_rate', ],) def snake_case__ ( self : int,lowercase_ : Any=None,lowercase_ : List[str]=None,lowercase_ : Dict=False )-> Optional[int]: '''simple docstring''' if concatenate_texts: return compute_measures(lowercase_,lowercase_ )["wer"] else: A__ = 0 A__ = 0 for prediction, reference in zip(lowercase_,lowercase_ ): A__ = compute_measures(lowercase_,lowercase_ ) incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"] total += measures["substitutions"] + measures["deletions"] + measures["hits"] return incorrect / total
282
1
"""simple docstring""" import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class snake_case ( unittest.TestCase ): """simple docstring""" @property def __lowerCAmelCase ( self : List[str] ): torch.manual_seed(0 ) UpperCAmelCase__ = UNetaDModel( block_out_channels=(32, 64) ,layers_per_block=2 ,sample_size=32 ,in_channels=3 ,out_channels=3 ,down_block_types=('DownBlock2D', 'AttnDownBlock2D') ,up_block_types=('AttnUpBlock2D', 'UpBlock2D') ,) return model def __lowerCAmelCase ( self : Dict ): UpperCAmelCase__ = self.dummy_uncond_unet UpperCAmelCase__ = KarrasVeScheduler() UpperCAmelCase__ = KarrasVePipeline(unet=lowerCamelCase__ ,scheduler=lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCAmelCase__ = torch.manual_seed(0 ) UpperCAmelCase__ = pipe(num_inference_steps=2 ,generator=lowerCamelCase__ ,output_type='numpy' ).images UpperCAmelCase__ = torch.manual_seed(0 ) UpperCAmelCase__ = pipe(num_inference_steps=2 ,generator=lowerCamelCase__ ,output_type='numpy' ,return_dict=lowerCamelCase__ )[0] UpperCAmelCase__ = image[0, -3:, -3:, -1] UpperCAmelCase__ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCAmelCase__ = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class snake_case ( unittest.TestCase ): """simple docstring""" def __lowerCAmelCase ( self : List[Any] ): UpperCAmelCase__ = 'google/ncsnpp-celebahq-256' UpperCAmelCase__ = UNetaDModel.from_pretrained(lowerCamelCase__ ) UpperCAmelCase__ = KarrasVeScheduler() UpperCAmelCase__ = KarrasVePipeline(unet=lowerCamelCase__ ,scheduler=lowerCamelCase__ ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCAmelCase__ = torch.manual_seed(0 ) UpperCAmelCase__ = pipe(num_inference_steps=20 ,generator=lowerCamelCase__ ,output_type='numpy' ).images UpperCAmelCase__ = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) UpperCAmelCase__ = np.array([0.5_7_8, 0.5_8_1_1, 0.5_9_2_4, 0.5_8_0_9, 0.5_8_7, 0.5_8_8_6, 0.5_8_6_1, 0.5_8_0_2, 0.5_8_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
98
'''simple docstring''' UpperCAmelCase = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' UpperCAmelCase = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] UpperCAmelCase = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
141
0
'''simple docstring''' from typing import List, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase_ = logging.get_logger(__name__) lowerCamelCase_ = { '''huggingface/informer-tourism-monthly''': ( '''https://huggingface.co/huggingface/informer-tourism-monthly/resolve/main/config.json''' ), # See all Informer models at https://huggingface.co/models?filter=informer } class _UpperCAmelCase ( snake_case_ ): """simple docstring""" snake_case = '''informer''' snake_case = { '''hidden_size''': '''d_model''', '''num_attention_heads''': '''encoder_attention_heads''', '''num_hidden_layers''': '''encoder_layers''', } def __init__( self : Union[str, Any] , __UpperCAmelCase : Optional[int] = None , __UpperCAmelCase : Optional[int] = None , __UpperCAmelCase : str = "student_t" , __UpperCAmelCase : str = "nll" , __UpperCAmelCase : int = 1 , __UpperCAmelCase : List[int] = None , __UpperCAmelCase : Optional[Union[str, bool]] = "mean" , __UpperCAmelCase : int = 0 , __UpperCAmelCase : int = 0 , __UpperCAmelCase : int = 0 , __UpperCAmelCase : int = 0 , __UpperCAmelCase : Optional[List[int]] = None , __UpperCAmelCase : Optional[List[int]] = None , __UpperCAmelCase : int = 64 , __UpperCAmelCase : int = 32 , __UpperCAmelCase : int = 32 , __UpperCAmelCase : int = 2 , __UpperCAmelCase : int = 2 , __UpperCAmelCase : int = 2 , __UpperCAmelCase : int = 2 , __UpperCAmelCase : bool = True , __UpperCAmelCase : str = "gelu" , __UpperCAmelCase : float = 0.05 , __UpperCAmelCase : float = 0.1 , __UpperCAmelCase : float = 0.1 , __UpperCAmelCase : float = 0.1 , __UpperCAmelCase : float = 0.1 , __UpperCAmelCase : int = 100 , __UpperCAmelCase : float = 0.02 , __UpperCAmelCase : List[Any]=True , __UpperCAmelCase : str = "prob" , __UpperCAmelCase : int = 5 , __UpperCAmelCase : bool = True , **__UpperCAmelCase : Tuple , ): '''simple docstring''' _A = prediction_length _A = context_length or prediction_length _A = distribution_output _A = loss _A = input_size _A = num_time_features _A = lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7] _A = scaling _A = num_dynamic_real_features _A = num_static_real_features _A = num_static_categorical_features # set cardinality if cardinality and num_static_categorical_features > 0: if len(__UpperCAmelCase ) != num_static_categorical_features: raise ValueError( "The cardinality should be a list of the same length as `num_static_categorical_features`" ) _A = cardinality else: _A = [0] # set embedding_dimension if embedding_dimension and num_static_categorical_features > 0: if len(__UpperCAmelCase ) != num_static_categorical_features: raise ValueError( "The embedding dimension should be a list of the same length as `num_static_categorical_features`" ) _A = embedding_dimension else: _A = [min(50 , (cat + 1) // 2 ) for cat in self.cardinality] _A = num_parallel_samples # Transformer architecture configuration _A = input_size * len(self.lags_sequence ) + self._number_of_features _A = d_model _A = encoder_attention_heads _A = decoder_attention_heads _A = encoder_ffn_dim _A = decoder_ffn_dim _A = encoder_layers _A = decoder_layers _A = dropout _A = attention_dropout _A = activation_dropout _A = encoder_layerdrop _A = decoder_layerdrop _A = activation_function _A = init_std _A = use_cache # Informer _A = attention_type _A = sampling_factor _A = distil super().__init__(is_encoder_decoder=__UpperCAmelCase , **__UpperCAmelCase ) @property def lowerCAmelCase ( self : Union[str, Any] ): '''simple docstring''' return ( sum(self.embedding_dimension ) + self.num_dynamic_real_features + self.num_time_features + self.num_static_real_features + self.input_size * 2 # the log1p(abs(loc)) and log(scale) features )
361
'''simple docstring''' import os lowerCamelCase_ = {'''I''': 1, '''V''': 5, '''X''': 10, '''L''': 50, '''C''': 1_00, '''D''': 5_00, '''M''': 10_00} def __lowercase ( __lowercase ) -> int: '''simple docstring''' _A = 0 _A = 0 while index < len(__lowercase ) - 1: _A = SYMBOLS[numerals[index]] _A = SYMBOLS[numerals[index + 1]] if current_value < next_value: total_value -= current_value else: total_value += current_value index += 1 total_value += SYMBOLS[numerals[index]] return total_value def __lowercase ( __lowercase ) -> str: '''simple docstring''' _A = "" _A = num // 1000 numerals += m_count * "M" num %= 1000 _A = num // 100 if c_count == 9: numerals += "CM" c_count -= 9 elif c_count == 4: numerals += "CD" c_count -= 4 if c_count >= 5: numerals += "D" c_count -= 5 numerals += c_count * "C" num %= 100 _A = num // 10 if x_count == 9: numerals += "XC" x_count -= 9 elif x_count == 4: numerals += "XL" x_count -= 4 if x_count >= 5: numerals += "L" x_count -= 5 numerals += x_count * "X" num %= 10 if num == 9: numerals += "IX" num -= 9 elif num == 4: numerals += "IV" num -= 4 if num >= 5: numerals += "V" num -= 5 numerals += num * "I" return numerals def __lowercase ( __lowercase = "/p089_roman.txt" ) -> int: '''simple docstring''' _A = 0 with open(os.path.dirname(__lowercase ) + roman_numerals_filename ) as filea: _A = filea.readlines() for line in lines: _A = line.strip() _A = parse_roman_numerals(__lowercase ) _A = generate_roman_numerals(__lowercase ) savings += len(__lowercase ) - len(__lowercase ) return savings if __name__ == "__main__": print(F"""{solution() = }""")
174
0
'''simple docstring''' from math import ceil def lowercase ( __magic_name__ , __magic_name__ ): '''simple docstring''' UpperCAmelCase : Optional[Any] = list(range(0 , __magic_name__ ) ) UpperCAmelCase : Optional[int] = [item for sublist in list(device_map.values() ) for item in sublist] # Duplicate check UpperCAmelCase : List[str] = [] for i in device_map_blocks: if device_map_blocks.count(__magic_name__ ) > 1 and i not in duplicate_blocks: duplicate_blocks.append(__magic_name__ ) # Missing blocks UpperCAmelCase : int = [i for i in blocks if i not in device_map_blocks] UpperCAmelCase : Optional[Any] = [i for i in device_map_blocks if i not in blocks] if len(__magic_name__ ) != 0: raise ValueError( "Duplicate attention blocks specified in device_map. Attention blocks must be specified to one device." " These attention blocks were specified more than once: " + str(__magic_name__ ) ) if len(__magic_name__ ) != 0: raise ValueError( "There are attention blocks for this model that are not specified in the device_map. Add these attention " "blocks to a device on the device_map: " + str(__magic_name__ ) ) if len(__magic_name__ ) != 0: raise ValueError( "The device_map contains more attention blocks than this model has. Remove these from the device_map:" + str(__magic_name__ ) ) def lowercase ( __magic_name__ , __magic_name__ ): '''simple docstring''' UpperCAmelCase : str = list(range(__magic_name__ ) ) UpperCAmelCase : Union[str, Any] = int(ceil(n_layers / len(__magic_name__ ) ) ) UpperCAmelCase : Tuple = [layers[i : i + n_blocks] for i in range(0 , __magic_name__ , __magic_name__ )] return dict(zip(__magic_name__ , __magic_name__ ) )
311
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) a : Union[str, Any] = { "configuration_encodec": [ "ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP", "EncodecConfig", ], "feature_extraction_encodec": ["EncodecFeatureExtractor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a : Optional[int] = [ "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 a : Any = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
311
1
'''simple docstring''' from dataclasses import dataclass, field from typing import Tuple from ..utils import cached_property, is_tf_available, logging, requires_backends from .benchmark_args_utils import BenchmarkArguments if is_tf_available(): import tensorflow as tf _lowerCAmelCase = logging.get_logger(__name__) @dataclass class _SCREAMING_SNAKE_CASE ( __a ): __SCREAMING_SNAKE_CASE :Dict = [ """no_inference""", """no_cuda""", """no_tpu""", """no_speed""", """no_memory""", """no_env_print""", """no_multi_process""", ] def __init__( self : int , **a__ : List[Any] ): for deprecated_arg in self.deprecated_args: if deprecated_arg in kwargs: __magic_name__ = deprecated_arg[3:] __magic_name__ = not kwargs.pop(a__ ) logger.warning( F'''{deprecated_arg} is depreciated. Please use --no-{positive_arg} or''' F''' {positive_arg}={kwargs[positive_arg]}''' ) __magic_name__ = kwargs.pop('''tpu_name''' , self.tpu_name ) __magic_name__ = kwargs.pop('''device_idx''' , self.device_idx ) __magic_name__ = kwargs.pop('''eager_mode''' , self.eager_mode ) __magic_name__ = kwargs.pop('''use_xla''' , self.use_xla ) super().__init__(**a__ ) __SCREAMING_SNAKE_CASE :str = field( default=__a ,metadata={"""help""": """Name of TPU"""} ,) __SCREAMING_SNAKE_CASE :int = field( default=0 ,metadata={"""help""": """CPU / GPU device index. Defaults to 0."""} ,) __SCREAMING_SNAKE_CASE :bool = field(default=__a ,metadata={"""help""": """Benchmark models in eager model."""} ) __SCREAMING_SNAKE_CASE :bool = field( default=__a ,metadata={ """help""": """Benchmark models using XLA JIT compilation. Note that `eager_model` has to be set to `False`.""" } ,) @cached_property def snake_case__ ( self : int ): requires_backends(self , ['''tf'''] ) __magic_name__ = None if self.tpu: try: if self.tpu_name: __magic_name__ = tf.distribute.cluster_resolver.TPUClusterResolver(self.tpu_name ) else: __magic_name__ = tf.distribute.cluster_resolver.TPUClusterResolver() except ValueError: __magic_name__ = None return tpu @cached_property def snake_case__ ( self : Optional[Any] ): requires_backends(self , ['''tf'''] ) if self.is_tpu: tf.config.experimental_connect_to_cluster(self._setup_tpu ) tf.tpu.experimental.initialize_tpu_system(self._setup_tpu ) __magic_name__ = tf.distribute.TPUStrategy(self._setup_tpu ) else: # currently no multi gpu is allowed if self.is_gpu: # TODO: Currently only single GPU is supported tf.config.set_visible_devices(self.gpu_list[self.device_idx] , '''GPU''' ) __magic_name__ = tf.distribute.OneDeviceStrategy(device=F'''/gpu:{self.device_idx}''' ) else: tf.config.set_visible_devices([] , '''GPU''' ) # disable GPU __magic_name__ = tf.distribute.OneDeviceStrategy(device=F'''/cpu:{self.device_idx}''' ) return strategy @property def snake_case__ ( self : List[str] ): requires_backends(self , ['''tf'''] ) return self._setup_tpu is not None @property def snake_case__ ( self : int ): requires_backends(self , ['''tf'''] ) return self._setup_strategy @property def snake_case__ ( self : str ): requires_backends(self , ['''tf'''] ) return tf.config.list_physical_devices('''GPU''' ) @property def snake_case__ ( self : Dict ): requires_backends(self , ['''tf'''] ) if self.cuda: return len(self.gpu_list ) return 0 @property def snake_case__ ( self : Optional[Any] ): return self.n_gpu > 0
98
'''simple docstring''' import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): __SCREAMING_SNAKE_CASE :List[Any] = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING __SCREAMING_SNAKE_CASE :Any = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def snake_case__ ( self : Tuple , a__ : Tuple , a__ : int , a__ : int ): __magic_name__ = TextaTextGenerationPipeline(model=a__ , tokenizer=a__ ) return generator, ["Something to write", "Something else"] def snake_case__ ( self : List[str] , a__ : List[Any] , a__ : List[str] ): __magic_name__ = generator('''Something there''' ) self.assertEqual(a__ , [{'''generated_text''': ANY(a__ )}] ) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]['''generated_text'''].startswith('''Something there''' ) ) __magic_name__ = generator(['''This is great !''', '''Something else'''] , num_return_sequences=2 , do_sample=a__ ) self.assertEqual( a__ , [ [{'''generated_text''': ANY(a__ )}, {'''generated_text''': ANY(a__ )}], [{'''generated_text''': ANY(a__ )}, {'''generated_text''': ANY(a__ )}], ] , ) __magic_name__ = generator( ['''This is great !''', '''Something else'''] , num_return_sequences=2 , batch_size=2 , do_sample=a__ ) self.assertEqual( a__ , [ [{'''generated_text''': ANY(a__ )}, {'''generated_text''': ANY(a__ )}], [{'''generated_text''': ANY(a__ )}, {'''generated_text''': ANY(a__ )}], ] , ) with self.assertRaises(a__ ): generator(4 ) @require_torch def snake_case__ ( self : Any ): __magic_name__ = pipeline('''text2text-generation''' , model='''patrickvonplaten/t5-tiny-random''' , framework='''pt''' ) # do_sample=False necessary for reproducibility __magic_name__ = generator('''Something there''' , do_sample=a__ ) self.assertEqual(a__ , [{'''generated_text''': ''''''}] ) __magic_name__ = 3 __magic_name__ = generator( '''Something there''' , num_return_sequences=a__ , num_beams=a__ , ) __magic_name__ = [ {'''generated_text''': '''Beide Beide Beide Beide Beide Beide Beide Beide Beide'''}, {'''generated_text''': '''Beide Beide Beide Beide Beide Beide Beide Beide'''}, {'''generated_text''': ''''''}, ] self.assertEqual(a__ , a__ ) __magic_name__ = generator('''This is a test''' , do_sample=a__ , num_return_sequences=2 , return_tensors=a__ ) self.assertEqual( a__ , [ {'''generated_token_ids''': ANY(torch.Tensor )}, {'''generated_token_ids''': ANY(torch.Tensor )}, ] , ) __magic_name__ = generator.model.config.eos_token_id __magic_name__ = '''<pad>''' __magic_name__ = generator( ['''This is a test''', '''This is a second test'''] , do_sample=a__ , num_return_sequences=2 , batch_size=2 , return_tensors=a__ , ) self.assertEqual( a__ , [ [ {'''generated_token_ids''': ANY(torch.Tensor )}, {'''generated_token_ids''': ANY(torch.Tensor )}, ], [ {'''generated_token_ids''': ANY(torch.Tensor )}, {'''generated_token_ids''': ANY(torch.Tensor )}, ], ] , ) @require_tf def snake_case__ ( self : int ): __magic_name__ = pipeline('''text2text-generation''' , model='''patrickvonplaten/t5-tiny-random''' , framework='''tf''' ) # do_sample=False necessary for reproducibility __magic_name__ = generator('''Something there''' , do_sample=a__ ) self.assertEqual(a__ , [{'''generated_text''': ''''''}] )
98
1
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging _A = logging.get_logger(__name__) _A = '''▁''' _A = { '''vocab_file''': '''vocab.json''', '''spm_file''': '''sentencepiece.bpe.model''', '''tokenizer_config_file''': '''tokenizer_config.json''', } _A = { '''vocab_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json''', }, '''spm_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_config_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json''', }, } _A = { '''facebook/m2m100_418M''': 1_024, } # fmt: off _A = { '''m2m100''': ['''af''', '''am''', '''ar''', '''ast''', '''az''', '''ba''', '''be''', '''bg''', '''bn''', '''br''', '''bs''', '''ca''', '''ceb''', '''cs''', '''cy''', '''da''', '''de''', '''el''', '''en''', '''es''', '''et''', '''fa''', '''ff''', '''fi''', '''fr''', '''fy''', '''ga''', '''gd''', '''gl''', '''gu''', '''ha''', '''he''', '''hi''', '''hr''', '''ht''', '''hu''', '''hy''', '''id''', '''ig''', '''ilo''', '''is''', '''it''', '''ja''', '''jv''', '''ka''', '''kk''', '''km''', '''kn''', '''ko''', '''lb''', '''lg''', '''ln''', '''lo''', '''lt''', '''lv''', '''mg''', '''mk''', '''ml''', '''mn''', '''mr''', '''ms''', '''my''', '''ne''', '''nl''', '''no''', '''ns''', '''oc''', '''or''', '''pa''', '''pl''', '''ps''', '''pt''', '''ro''', '''ru''', '''sd''', '''si''', '''sk''', '''sl''', '''so''', '''sq''', '''sr''', '''ss''', '''su''', '''sv''', '''sw''', '''ta''', '''th''', '''tl''', '''tn''', '''tr''', '''uk''', '''ur''', '''uz''', '''vi''', '''wo''', '''xh''', '''yi''', '''yo''', '''zh''', '''zu'''], '''wmt21''': ['''en''', '''ha''', '''is''', '''ja''', '''cs''', '''ru''', '''zh''', '''de'''] } class A ( __UpperCAmelCase ): __snake_case = VOCAB_FILES_NAMES __snake_case = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __snake_case = PRETRAINED_VOCAB_FILES_MAP __snake_case = ['input_ids', 'attention_mask'] __snake_case = [] __snake_case = [] def __init__( self, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__=None, UpperCamelCase__=None, UpperCamelCase__="<s>", UpperCamelCase__="</s>", UpperCamelCase__="</s>", UpperCamelCase__="<pad>", UpperCamelCase__="<unk>", UpperCamelCase__="m2m100", UpperCamelCase__ = None, UpperCamelCase__=8, **UpperCamelCase__, ): """simple docstring""" lowerCAmelCase_ = {} if sp_model_kwargs is None else sp_model_kwargs lowerCAmelCase_ = language_codes lowerCAmelCase_ = FAIRSEQ_LANGUAGE_CODES[language_codes] lowerCAmelCase_ = {lang_code: f"__{lang_code}__" for lang_code in fairseq_language_code} lowerCAmelCase_ = kwargs.get('''additional_special_tokens''', [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(UpperCamelCase__ ) for lang_code in fairseq_language_code if self.get_lang_token(UpperCamelCase__ ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=UpperCamelCase__, tgt_lang=UpperCamelCase__, bos_token=UpperCamelCase__, eos_token=UpperCamelCase__, sep_token=UpperCamelCase__, unk_token=UpperCamelCase__, pad_token=UpperCamelCase__, language_codes=UpperCamelCase__, sp_model_kwargs=self.sp_model_kwargs, num_madeup_words=UpperCamelCase__, **UpperCamelCase__, ) lowerCAmelCase_ = vocab_file lowerCAmelCase_ = load_json(UpperCamelCase__ ) lowerCAmelCase_ = {v: k for k, v in self.encoder.items()} lowerCAmelCase_ = spm_file lowerCAmelCase_ = load_spm(UpperCamelCase__, self.sp_model_kwargs ) lowerCAmelCase_ = len(self.encoder ) lowerCAmelCase_ = { self.get_lang_token(UpperCamelCase__ ): self.encoder_size + i for i, lang_code in enumerate(UpperCamelCase__ ) } lowerCAmelCase_ = {lang_code: self.encoder_size + i for i, lang_code in enumerate(UpperCamelCase__ )} lowerCAmelCase_ = {v: k for k, v in self.lang_token_to_id.items()} lowerCAmelCase_ = src_lang if src_lang is not None else '''en''' lowerCAmelCase_ = tgt_lang lowerCAmelCase_ = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) lowerCAmelCase_ = num_madeup_words @property def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" return len(self.encoder ) + len(self.lang_token_to_id ) @property def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" return self._src_lang @src_lang.setter def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__ ): """simple docstring""" lowerCAmelCase_ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__ ): """simple docstring""" return self.sp_model.encode(UpperCamelCase__, out_type=UpperCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__ ): """simple docstring""" if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(UpperCamelCase__, self.encoder[self.unk_token] ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__ ): """simple docstring""" if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(UpperCamelCase__, self.unk_token ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__ ): """simple docstring""" lowerCAmelCase_ = [] lowerCAmelCase_ = '''''' for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(UpperCamelCase__ ) + token lowerCAmelCase_ = [] else: current_sub_tokens.append(UpperCamelCase__ ) out_string += self.sp_model.decode(UpperCamelCase__ ) return out_string.strip() def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__ = None, UpperCamelCase__ = False ): """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=UpperCamelCase__, token_ids_a=UpperCamelCase__, already_has_special_tokens=UpperCamelCase__ ) lowerCAmelCase_ = [1] * len(self.prefix_tokens ) lowerCAmelCase_ = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(UpperCamelCase__ )) + suffix_ones return prefix_ones + ([0] * len(UpperCamelCase__ )) + ([0] * len(UpperCamelCase__ )) + suffix_ones def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__ = None ): """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = {self.convert_ids_to_tokens(UpperCamelCase__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ): """simple docstring""" lowerCAmelCase_ = self.__dict__.copy() lowerCAmelCase_ = None return state def __setstate__( self, UpperCamelCase__ ): """simple docstring""" lowerCAmelCase_ = d # for backward compatibility if not hasattr(self, '''sp_model_kwargs''' ): lowerCAmelCase_ = {} lowerCAmelCase_ = load_spm(self.spm_file, self.sp_model_kwargs ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__ = None ): """simple docstring""" lowerCAmelCase_ = Path(UpperCamelCase__ ) if not save_dir.is_dir(): raise OSError(f"{save_directory} should be a directory" ) lowerCAmelCase_ = save_dir / ( (filename_prefix + '''-''' if filename_prefix else '''''') + self.vocab_files_names['''vocab_file'''] ) lowerCAmelCase_ = save_dir / ( (filename_prefix + '''-''' if filename_prefix else '''''') + self.vocab_files_names['''spm_file'''] ) save_json(self.encoder, UpperCamelCase__ ) if os.path.abspath(self.spm_file ) != os.path.abspath(UpperCamelCase__ ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file, UpperCamelCase__ ) elif not os.path.isfile(self.spm_file ): with open(UpperCamelCase__, '''wb''' ) as fi: lowerCAmelCase_ = self.sp_model.serialized_model_proto() fi.write(UpperCamelCase__ ) return (str(UpperCamelCase__ ), str(UpperCamelCase__ )) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__ = "en", UpperCamelCase__ = None, UpperCamelCase__ = "ro", **UpperCamelCase__, ): """simple docstring""" lowerCAmelCase_ = src_lang lowerCAmelCase_ = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(UpperCamelCase__, UpperCamelCase__, **UpperCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, **UpperCamelCase__ ): """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError('''Translation requires a `src_lang` and a `tgt_lang` for this model''' ) lowerCAmelCase_ = src_lang lowerCAmelCase_ = self(UpperCamelCase__, add_special_tokens=UpperCamelCase__, **UpperCamelCase__ ) lowerCAmelCase_ = self.get_lang_id(UpperCamelCase__ ) lowerCAmelCase_ = tgt_lang_id return inputs def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" self.set_src_lang_special_tokens(self.src_lang ) def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" self.set_tgt_lang_special_tokens(self.tgt_lang ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__ ): """simple docstring""" lowerCAmelCase_ = self.get_lang_token(UpperCamelCase__ ) lowerCAmelCase_ = self.lang_token_to_id[lang_token] lowerCAmelCase_ = [self.cur_lang_id] lowerCAmelCase_ = [self.eos_token_id] def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__ ): """simple docstring""" lowerCAmelCase_ = self.get_lang_token(UpperCamelCase__ ) lowerCAmelCase_ = self.lang_token_to_id[lang_token] lowerCAmelCase_ = [self.cur_lang_id] lowerCAmelCase_ = [self.eos_token_id] def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__ ): """simple docstring""" return self.lang_code_to_token[lang] def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__ ): """simple docstring""" lowerCAmelCase_ = self.get_lang_token(UpperCamelCase__ ) return self.lang_token_to_id[lang_token] def __UpperCamelCase ( _A , _A ): lowerCAmelCase_ = sentencepiece.SentencePieceProcessor(**_A ) spm.Load(str(_A ) ) return spm def __UpperCamelCase ( _A ): with open(_A , '''r''' ) as f: return json.load(_A ) def __UpperCamelCase ( _A , _A ): with open(_A , '''w''' ) as f: json.dump(_A , _A , indent=2 )
278
import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def __UpperCamelCase ( _A ): lowerCAmelCase_ = [ '''decoder.version''', '''decoder.output_projection.weight''', '''_float_tensor''', '''decoder.embed_positions._float_tensor''', ] for k in ignore_keys: state_dict.pop(_A , _A ) def __UpperCamelCase ( _A ): lowerCAmelCase_ , lowerCAmelCase_ = emb.weight.shape lowerCAmelCase_ = nn.Linear(_A , _A , bias=_A ) lowerCAmelCase_ = emb.weight.data return lin_layer def __UpperCamelCase ( _A ): lowerCAmelCase_ = torch.load(_A , map_location='''cpu''' ) lowerCAmelCase_ = Namespace(**checkpoint['''cfg''']['''model'''] ) lowerCAmelCase_ = checkpoint['''model'''] remove_ignore_keys_(_A ) lowerCAmelCase_ = state_dict['''decoder.embed_tokens.weight'''].shape[0] lowerCAmelCase_ = {key.replace('''decoder''' , '''model''' ): val for key, val in state_dict.items()} lowerCAmelCase_ = XGLMConfig( vocab_size=_A , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function='''gelu''' , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) lowerCAmelCase_ = XGLMForCausalLM(_A ) lowerCAmelCase_ = model.load_state_dict(_A , strict=_A ) print(_A ) lowerCAmelCase_ = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": _A = argparse.ArgumentParser() # Required parameters parser.add_argument('''fairseq_path''', type=str, help='''path to a model.pt on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') _A = parser.parse_args() _A = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
278
1
from timeit import timeit def lowerCamelCase__ (__lowerCamelCase ): if number < 0: raise ValueError("the value of input must not be negative" ) _SCREAMING_SNAKE_CASE : str = 0 while number: number &= number - 1 result += 1 return result def lowerCamelCase__ (__lowerCamelCase ): if number < 0: raise ValueError("the value of input must not be negative" ) _SCREAMING_SNAKE_CASE : str = 0 while number: if number % 2 == 1: result += 1 number >>= 1 return result def lowerCamelCase__ (): def do_benchmark(__lowerCamelCase ) -> None: _SCREAMING_SNAKE_CASE : Tuple = "import __main__ as z" print(f"""Benchmark when {number = }:""" ) print(f"""{get_set_bits_count_using_modulo_operator(__lowerCamelCase ) = }""" ) _SCREAMING_SNAKE_CASE : str = timeit("z.get_set_bits_count_using_modulo_operator(25)", setup=__lowerCamelCase ) print(f"""timeit() runs in {timing} seconds""" ) print(f"""{get_set_bits_count_using_brian_kernighans_algorithm(__lowerCamelCase ) = }""" ) _SCREAMING_SNAKE_CASE : int = timeit( "z.get_set_bits_count_using_brian_kernighans_algorithm(25)", setup=__lowerCamelCase, ) print(f"""timeit() runs in {timing} seconds""" ) for number in (25, 37, 58, 0): do_benchmark(__lowerCamelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
363
from __future__ import annotations import typing from collections import Counter def lowerCamelCase__ (__lowerCamelCase ): _SCREAMING_SNAKE_CASE : typing.Counter[int] = Counter() for base in range(1, max_perimeter + 1 ): for perpendicular in range(__lowerCamelCase, max_perimeter + 1 ): _SCREAMING_SNAKE_CASE : List[Any] = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(__lowerCamelCase ): _SCREAMING_SNAKE_CASE : Union[str, Any] = int(base + perpendicular + hypotenuse ) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def lowerCamelCase__ (__lowerCamelCase = 1000 ): _SCREAMING_SNAKE_CASE : Union[str, Any] = pythagorean_triple(__lowerCamelCase ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(f"Perimeter {solution()} has maximum solutions")
325
0
from __future__ import annotations def a_ ( __lowercase : list , __lowercase : int ) -> Any: # Checks if the entire collection has been sorted if len(__lowercase ) <= 1 or n <= 1: return insert_next(__lowercase , n - 1 ) rec_insertion_sort(__lowercase , n - 1 ) def a_ ( __lowercase : list , __lowercase : int ) -> int: # Checks order between adjacent elements if index >= len(__lowercase ) or collection[index - 1] <= collection[index]: return # Swaps adjacent elements since they are not in ascending order _snake_case , _snake_case = ( collection[index], collection[index - 1], ) insert_next(__lowercase , index + 1 ) if __name__ == "__main__": _lowerCamelCase : Tuple = input('''Enter integers separated by spaces: ''') _lowerCamelCase : list[int] = [int(num) for num in numbers.split()] rec_insertion_sort(number_list, len(number_list)) print(number_list)
282
_lowerCamelCase : int = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] _lowerCamelCase : str = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] _lowerCamelCase : List[str] = { 0: '''Sunday''', 1: '''Monday''', 2: '''Tuesday''', 3: '''Wednesday''', 4: '''Thursday''', 5: '''Friday''', 6: '''Saturday''', } def a_ ( __lowercase : int , __lowercase : int , __lowercase : int ) -> str: assert len(str(__lowercase ) ) > 2, "year should be in YYYY format" assert 1 <= month <= 12, "month should be between 1 to 12" assert 1 <= day <= 31, "day should be between 1 to 31" # Doomsday algorithm: _snake_case = year // 100 _snake_case = (5 * (century % 4) + 2) % 7 _snake_case = year % 100 _snake_case = centurian % 12 _snake_case = ( (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor ) % 7 _snake_case = ( DOOMSDAY_NOT_LEAP[month - 1] if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0) else DOOMSDAY_LEAP[month - 1] ) _snake_case = (dooms_day + day - day_anchor) % 7 return WEEK_DAY_NAMES[week_day] if __name__ == "__main__": import doctest doctest.testmod()
282
1
__UpperCamelCase : List[str] = "\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n" __UpperCamelCase : Tuple = [{"type": "code", "content": INSTALL_CONTENT}] __UpperCamelCase : Dict = { "{processor_class}": "FakeProcessorClass", "{model_class}": "FakeModelClass", "{object_class}": "FakeObjectClass", }
354
from __future__ import annotations from typing import Generic, TypeVar __UpperCamelCase : Union[str, Any] = TypeVar("T") class __lowerCAmelCase ( Generic[T] ): def __init__( self :Tuple , __magic_name__ :T ): '''simple docstring''' a = data a = self a = 0 class __lowerCAmelCase ( Generic[T] ): def __init__( self :Tuple ): '''simple docstring''' a = {} def lowerCamelCase__ ( self :Optional[int] , __magic_name__ :T ): '''simple docstring''' a = DisjointSetTreeNode(__magic_name__ ) def lowerCamelCase__ ( self :List[Any] , __magic_name__ :T ): '''simple docstring''' a = self.map[data] if elem_ref != elem_ref.parent: a = self.find_set(elem_ref.parent.data ) return elem_ref.parent def lowerCamelCase__ ( self :List[Any] , __magic_name__ :DisjointSetTreeNode[T] , __magic_name__ :DisjointSetTreeNode[T] ): '''simple docstring''' if nodea.rank > nodea.rank: a = nodea else: a = nodea if nodea.rank == nodea.rank: nodea.rank += 1 def lowerCamelCase__ ( self :Optional[int] , __magic_name__ :T , __magic_name__ :T ): '''simple docstring''' self.link(self.find_set(__magic_name__ ) , self.find_set(__magic_name__ ) ) class __lowerCAmelCase ( Generic[T] ): def __init__( self :Union[str, Any] ): '''simple docstring''' a = {} def lowerCamelCase__ ( self :Union[str, Any] , __magic_name__ :T ): '''simple docstring''' if node not in self.connections: a = {} def lowerCamelCase__ ( self :Any , __magic_name__ :T , __magic_name__ :T , __magic_name__ :int ): '''simple docstring''' self.add_node(__magic_name__ ) self.add_node(__magic_name__ ) a = weight a = weight def lowerCamelCase__ ( self :int ): '''simple docstring''' a = [] a = set() for start in self.connections: for end in self.connections[start]: if (start, end) not in seen: seen.add((end, start) ) edges.append((start, end, self.connections[start][end]) ) edges.sort(key=lambda __magic_name__ : x[2] ) # creating the disjoint set a = DisjointSetTree[T]() for node in self.connections: disjoint_set.make_set(__magic_name__ ) # MST generation a = 0 a = 0 a = GraphUndirectedWeighted[T]() while num_edges < len(self.connections ) - 1: a , a , a = edges[index] index += 1 a = disjoint_set.find_set(__magic_name__ ) a = disjoint_set.find_set(__magic_name__ ) if parent_u != parent_v: num_edges += 1 graph.add_edge(__magic_name__ , __magic_name__ , __magic_name__ ) disjoint_set.union(__magic_name__ , __magic_name__ ) return graph
347
0
import logging import os from dataclasses import dataclass from enum import Enum from typing import List, Optional, Union from filelock import FileLock from transformers import PreTrainedTokenizer, is_tf_available, is_torch_available UpperCAmelCase_ : Optional[int] = logging.getLogger(__name__) @dataclass class _SCREAMING_SNAKE_CASE : snake_case__ : str snake_case__ : List[str] snake_case__ : Optional[List[str]] @dataclass class _SCREAMING_SNAKE_CASE : snake_case__ : List[int] snake_case__ : List[int] snake_case__ : Optional[List[int]] = None snake_case__ : Optional[List[int]] = None class _SCREAMING_SNAKE_CASE ( __A ): snake_case__ : Union[str, Any] = 'train' snake_case__ : Optional[Any] = 'dev' snake_case__ : Dict = 'test' class _SCREAMING_SNAKE_CASE : @staticmethod def _A ( __lowerCamelCase : Dict , __lowerCamelCase : Any ): raise NotImplementedError @staticmethod def _A ( __lowerCamelCase : Optional[Any] ): raise NotImplementedError @staticmethod def _A ( __lowerCamelCase : int , __lowerCamelCase : List[str] , __lowerCamelCase : List[str] , __lowerCamelCase : Tuple , __lowerCamelCase : Any=False , __lowerCamelCase : Dict="[CLS]" , __lowerCamelCase : Any=1 , __lowerCamelCase : Optional[int]="[SEP]" , __lowerCamelCase : Tuple=False , __lowerCamelCase : Any=False , __lowerCamelCase : str=0 , __lowerCamelCase : Tuple=0 , __lowerCamelCase : Any=-100 , __lowerCamelCase : List[str]=0 , __lowerCamelCase : str=True , ): UpperCamelCase :Optional[Any] = {label: i for i, label in enumerate(__lowercase )} UpperCamelCase :str = [] for ex_index, example in enumerate(__lowercase ): if ex_index % 10_000 == 0: logger.info("""Writing example %d of %d""" , __lowercase , len(__lowercase ) ) UpperCamelCase :Any = [] UpperCamelCase :str = [] for word, label in zip(example.words , example.labels ): UpperCamelCase :Optional[int] = tokenizer.tokenize(__lowercase ) # bert-base-multilingual-cased sometimes output "nothing ([]) when calling tokenize with just a space. if len(__lowercase ) > 0: tokens.extend(__lowercase ) # Use the real label id for the first token of the word, and padding ids for the remaining tokens label_ids.extend([label_map[label]] + [pad_token_label_id] * (len(__lowercase ) - 1) ) # Account for [CLS] and [SEP] with "- 2" and with "- 3" for RoBERTa. UpperCamelCase :Union[str, Any] = tokenizer.num_special_tokens_to_add() if len(__lowercase ) > max_seq_length - special_tokens_count: UpperCamelCase :List[str] = tokens[: (max_seq_length - special_tokens_count)] UpperCamelCase :Optional[int] = label_ids[: (max_seq_length - special_tokens_count)] # The convention in BERT is: # (a) For sequence pairs: # tokens: [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP] # type_ids: 0 0 0 0 0 0 0 0 1 1 1 1 1 1 # (b) For single sequences: # tokens: [CLS] the dog is hairy . [SEP] # type_ids: 0 0 0 0 0 0 0 # # Where "type_ids" are used to indicate whether this is the first # sequence or the second sequence. The embedding vectors for `type=0` and # `type=1` were learned during pre-training and are added to the wordpiece # embedding vector (and position vector). This is not *strictly* necessary # since the [SEP] token unambiguously separates the sequences, but it makes # it easier for the model to learn the concept of sequences. # # For classification tasks, the first vector (corresponding to [CLS]) is # used as the "sentence vector". Note that this only makes sense because # the entire model is fine-tuned. tokens += [sep_token] label_ids += [pad_token_label_id] if sep_token_extra: # roberta uses an extra separator b/w pairs of sentences tokens += [sep_token] label_ids += [pad_token_label_id] UpperCamelCase :Tuple = [sequence_a_segment_id] * len(__lowercase ) if cls_token_at_end: tokens += [cls_token] label_ids += [pad_token_label_id] segment_ids += [cls_token_segment_id] else: UpperCamelCase :List[Any] = [cls_token] + tokens UpperCamelCase :Union[str, Any] = [pad_token_label_id] + label_ids UpperCamelCase :Optional[Any] = [cls_token_segment_id] + segment_ids UpperCamelCase :int = tokenizer.convert_tokens_to_ids(__lowercase ) # The mask has 1 for real tokens and 0 for padding tokens. Only real # tokens are attended to. UpperCamelCase :Tuple = [1 if mask_padding_with_zero else 0] * len(__lowercase ) # Zero-pad up to the sequence length. UpperCamelCase :List[str] = max_seq_length - len(__lowercase ) if pad_on_left: UpperCamelCase :Optional[Any] = ([pad_token] * padding_length) + input_ids UpperCamelCase :Optional[Any] = ([0 if mask_padding_with_zero else 1] * padding_length) + input_mask UpperCamelCase :Dict = ([pad_token_segment_id] * padding_length) + segment_ids UpperCamelCase :List[str] = ([pad_token_label_id] * padding_length) + label_ids else: input_ids += [pad_token] * padding_length input_mask += [0 if mask_padding_with_zero else 1] * padding_length segment_ids += [pad_token_segment_id] * padding_length label_ids += [pad_token_label_id] * padding_length assert len(__lowercase ) == max_seq_length assert len(__lowercase ) == max_seq_length assert len(__lowercase ) == max_seq_length assert len(__lowercase ) == max_seq_length if ex_index < 5: logger.info("""*** Example ***""" ) logger.info("""guid: %s""" , example.guid ) logger.info("""tokens: %s""" , """ """.join([str(__lowercase ) for x in tokens] ) ) logger.info("""input_ids: %s""" , """ """.join([str(__lowercase ) for x in input_ids] ) ) logger.info("""input_mask: %s""" , """ """.join([str(__lowercase ) for x in input_mask] ) ) logger.info("""segment_ids: %s""" , """ """.join([str(__lowercase ) for x in segment_ids] ) ) logger.info("""label_ids: %s""" , """ """.join([str(__lowercase ) for x in label_ids] ) ) if "token_type_ids" not in tokenizer.model_input_names: UpperCamelCase :Dict = None features.append( InputFeatures( input_ids=__lowercase , attention_mask=__lowercase , token_type_ids=__lowercase , label_ids=__lowercase ) ) return features if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset class _SCREAMING_SNAKE_CASE ( __A ): snake_case__ : List[InputFeatures] snake_case__ : int = nn.CrossEntropyLoss().ignore_index def __init__( self : List[str] , __lowerCamelCase : Dict , __lowerCamelCase : Dict , __lowerCamelCase : str , __lowerCamelCase : Optional[int] , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Any = None , __lowerCamelCase : List[str]=False , __lowerCamelCase : Tuple = Split.train , ): # Load data features from cache or dataset file UpperCamelCase :str = os.path.join( __lowercase , """cached_{}_{}_{}""".format(mode.value , tokenizer.__class__.__name__ , str(__lowercase ) ) , ) # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. UpperCamelCase :List[str] = cached_features_file + """.lock""" with FileLock(__lowercase ): if os.path.exists(__lowercase ) and not overwrite_cache: logger.info(F"""Loading features from cached file {cached_features_file}""" ) UpperCamelCase :Union[str, Any] = torch.load(__lowercase ) else: logger.info(F"""Creating features from dataset file at {data_dir}""" ) UpperCamelCase :Tuple = token_classification_task.read_examples_from_file(__lowercase , __lowercase ) # TODO clean up all this to leverage built-in features of tokenizers UpperCamelCase :List[Any] = token_classification_task.convert_examples_to_features( __lowercase , __lowercase , __lowercase , __lowercase , cls_token_at_end=bool(model_type in ["""xlnet"""] ) , cls_token=tokenizer.cls_token , cls_token_segment_id=2 if model_type in ["""xlnet"""] else 0 , sep_token=tokenizer.sep_token , sep_token_extra=__lowercase , pad_on_left=bool(tokenizer.padding_side == """left""" ) , pad_token=tokenizer.pad_token_id , pad_token_segment_id=tokenizer.pad_token_type_id , pad_token_label_id=self.pad_token_label_id , ) logger.info(F"""Saving features into cached file {cached_features_file}""" ) torch.save(self.features , __lowercase ) def __len__( self : Optional[Any] ): return len(self.features ) def __getitem__( self : str , __lowerCamelCase : int ): return self.features[i] if is_tf_available(): import tensorflow as tf class _SCREAMING_SNAKE_CASE : snake_case__ : List[InputFeatures] snake_case__ : int = -1_0_0 def __init__( self : List[Any] , __lowerCamelCase : List[Any] , __lowerCamelCase : int , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : int , __lowerCamelCase : Optional[int] , __lowerCamelCase : int = None , __lowerCamelCase : Dict=False , __lowerCamelCase : str = Split.train , ): UpperCamelCase :str = token_classification_task.read_examples_from_file(__lowercase , __lowercase ) # TODO clean up all this to leverage built-in features of tokenizers UpperCamelCase :str = token_classification_task.convert_examples_to_features( __lowercase , __lowercase , __lowercase , __lowercase , cls_token_at_end=bool(model_type in ["""xlnet"""] ) , cls_token=tokenizer.cls_token , cls_token_segment_id=2 if model_type in ["""xlnet"""] else 0 , sep_token=tokenizer.sep_token , sep_token_extra=__lowercase , pad_on_left=bool(tokenizer.padding_side == """left""" ) , pad_token=tokenizer.pad_token_id , pad_token_segment_id=tokenizer.pad_token_type_id , pad_token_label_id=self.pad_token_label_id , ) def gen(): for ex in self.features: if ex.token_type_ids is None: yield ( {"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label_ids, ) else: yield ( { "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label_ids, ) if "token_type_ids" not in tokenizer.model_input_names: UpperCamelCase :Optional[int] = tf.data.Dataset.from_generator( __lowercase , ({"""input_ids""": tf.intaa, """attention_mask""": tf.intaa}, tf.intaa) , ( {"""input_ids""": tf.TensorShape([None] ), """attention_mask""": tf.TensorShape([None] )}, tf.TensorShape([None] ), ) , ) else: UpperCamelCase :Optional[int] = tf.data.Dataset.from_generator( __lowercase , ({"""input_ids""": tf.intaa, """attention_mask""": tf.intaa, """token_type_ids""": tf.intaa}, tf.intaa) , ( { """input_ids""": tf.TensorShape([None] ), """attention_mask""": tf.TensorShape([None] ), """token_type_ids""": tf.TensorShape([None] ), }, tf.TensorShape([None] ), ) , ) def _A ( self : List[str] ): UpperCamelCase :Dict = self.dataset.apply(tf.data.experimental.assert_cardinality(len(self.features ) ) ) return self.dataset def __len__( self : Optional[Any] ): return len(self.features ) def __getitem__( self : Any , __lowerCamelCase : List[str] ): return self.features[i]
38
'''simple docstring''' # Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def __magic_name__( lowerCamelCase, lowerCamelCase, lowerCamelCase): __lowerCAmelCase = { '''en''': '''Machine learning is great, isn\'t it?''', '''ru''': '''Машинное обучение - это здорово, не так ли?''', '''de''': '''Maschinelles Lernen ist großartig, oder?''', } # BLUE scores as follows: # "pair": [fairseq, transformers] __lowerCAmelCase = { '''ru-en''': ['''[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)''', '''39.20'''], '''en-ru''': ['''[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)''', '''33.47'''], '''en-de''': ['''[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)''', '''42.83'''], '''de-en''': ['''[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)''', '''41.35'''], } __lowerCAmelCase = F"""{src_lang}-{tgt_lang}""" __lowerCAmelCase = F""" --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt19 - facebook license: apache-2.0 datasets: - wmt19 metrics: - bleu --- # FSMT ## Model description This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}. For more details, please see, [Facebook FAIR's WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616). The abbreviation FSMT stands for FairSeqMachineTranslation All four models are available: * [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru) * [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en) * [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de) * [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = \"facebook/wmt19-{src_lang}-{tgt_lang}\" tokenizer = FSMTTokenizer.from_pretrained(mname) model = FSMTForConditionalGeneration.from_pretrained(mname) input = \"{texts[src_lang]}\" input_ids = tokenizer.encode(input, return_tensors=\"pt\") outputs = model.generate(input_ids) decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) print(decoded) # {texts[tgt_lang]} ``` #### Limitations and bias - The original (and this ported model) doesn't seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981) ## Training data Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616). ## Eval results pair | fairseq | transformers -------|---------|---------- {pair} | {scores[pair][0]} | {scores[pair][1]} The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn't support: - model ensemble, therefore the best performing checkpoint was ported (``model4.pt``). - re-ranking The score was calculated using this code: ```bash git clone https://github.com/huggingface/transformers cd transformers export PAIR={pair} export DATA_DIR=data/$PAIR export SAVE_DIR=data/$PAIR export BS=8 export NUM_BEAMS=15 mkdir -p $DATA_DIR sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS ``` note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`. ## Data Sources - [training, etc.](http://www.statmt.org/wmt19/) - [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561) ### BibTeX entry and citation info ```bibtex @inproceedings{{..., year={{2020}}, title={{Facebook FAIR's WMT19 News Translation Task Submission}}, author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}}, booktitle={{Proc. of WMT}}, }} ``` ## TODO - port model ensemble (fairseq uses 4 model checkpoints) """ os.makedirs(lowerCamelCase, exist_ok=lowerCamelCase) __lowerCAmelCase = os.path.join(lowerCamelCase, '''README.md''') print(F"""Generating {path}""") with open(lowerCamelCase, '''w''', encoding='''utf-8''') as f: f.write(lowerCamelCase) # make sure we are under the root of the project _UpperCAmelCase : Dict = Path(__file__).resolve().parent.parent.parent _UpperCAmelCase : Optional[int] = repo_dir / """model_cards""" for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: _UpperCAmelCase ,_UpperCAmelCase ,_UpperCAmelCase : Dict = model_name.split("""-""") _UpperCAmelCase : Union[str, Any] = model_cards_dir / """facebook""" / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
174
0
'''simple docstring''' import numpy as np import pandas as pd from sklearn.preprocessing import Normalizer from sklearn.svm import SVR from statsmodels.tsa.statespace.sarimax import SARIMAX def a ( __a , __a , __a , __a , __a ) -> float: '''simple docstring''' UpperCamelCase__ :Optional[Any] = np.array([[1, item, train_mtch[i]] for i, item in enumerate(__a )] ) UpperCamelCase__ :List[Any] = np.array(__a ) UpperCamelCase__ :str = np.dot(np.dot(np.linalg.inv(np.dot(x.transpose() , __a ) ) , x.transpose() ) , __a ) return abs(beta[0] + test_dt[0] * beta[1] + test_mtch[0] + beta[2] ) def a ( __a , __a , __a ) -> float: '''simple docstring''' UpperCamelCase__ :Any = (1, 2, 1) UpperCamelCase__ :str = (1, 1, 0, 7) UpperCamelCase__ :Optional[int] = SARIMAX( __a , exog=__a , order=__a , seasonal_order=__a ) UpperCamelCase__ :Optional[Any] = model.fit(disp=__a , maxiter=600 , method='''nm''' ) UpperCamelCase__ :Union[str, Any] = model_fit.predict(1 , len(__a ) , exog=[test_match] ) return result[0] def a ( __a , __a , __a ) -> float: '''simple docstring''' UpperCamelCase__ :Optional[int] = SVR(kernel='''rbf''' , C=1 , gamma=0.1 , epsilon=0.1 ) regressor.fit(__a , __a ) UpperCamelCase__ :Optional[int] = regressor.predict(__a ) return y_pred[0] def a ( __a ) -> float: '''simple docstring''' train_user.sort() UpperCamelCase__ :Any = np.percentile(__a , 25 ) UpperCamelCase__ :int = np.percentile(__a , 75 ) UpperCamelCase__ :Tuple = qa - qa UpperCamelCase__ :Tuple = qa - (iqr * 0.1) return low_lim def a ( __a , __a ) -> bool: '''simple docstring''' UpperCamelCase__ :Union[str, Any] = 0 UpperCamelCase__ :Optional[Any] = 0 for i in list_vote: if i > actual_result: UpperCamelCase__ :Optional[Any] = not_safe + 1 else: if abs(abs(__a ) - abs(__a ) ) <= 0.1: safe += 1 else: not_safe += 1 return safe > not_safe if __name__ == "__main__": # data_input_df = pd.read_csv("ex_data.csv", header=None) __snake_case = [[18231, 0.0, 1], [22621, 1.0, 2], [15675, 0.0, 3], [23583, 1.0, 4]] __snake_case = pd.DataFrame( data_input, columns=['''total_user''', '''total_even''', '''days'''] ) __snake_case = Normalizer().fit_transform(data_input_df.values) # split data __snake_case = normalize_df[:, 2].tolist() __snake_case = normalize_df[:, 0].tolist() __snake_case = normalize_df[:, 1].tolist() # for svr (input variable = total date and total match) __snake_case = normalize_df[:, [1, 2]].tolist() __snake_case = x[: len(x) - 1] __snake_case = x[len(x) - 1 :] # for linear regression & sarimax __snake_case = total_date[: len(total_date) - 1] __snake_case = total_user[: len(total_user) - 1] __snake_case = total_match[: len(total_match) - 1] __snake_case = total_date[len(total_date) - 1 :] __snake_case = total_user[len(total_user) - 1 :] __snake_case = total_match[len(total_match) - 1 :] # voting system with forecasting __snake_case = [ linear_regression_prediction( trn_date, trn_user, trn_match, tst_date, tst_match ), sarimax_predictor(trn_user, trn_match, tst_match), support_vector_regressor(x_train, x_test, trn_user), ] # check the safety of today's data __snake_case = '''''' if data_safety_checker(res_vote, tst_user) else '''not ''' print('''Today\'s data is {not_str}safe.''')
350
'''simple docstring''' from math import ceil def a ( __a , __a ) -> Any: '''simple docstring''' UpperCamelCase__ :str = list(range(0 , __a ) ) UpperCamelCase__ :Optional[int] = [item for sublist in list(device_map.values() ) for item in sublist] # Duplicate check UpperCamelCase__ :Optional[int] = [] for i in device_map_blocks: if device_map_blocks.count(__a ) > 1 and i not in duplicate_blocks: duplicate_blocks.append(__a ) # Missing blocks UpperCamelCase__ :List[str] = [i for i in blocks if i not in device_map_blocks] UpperCamelCase__ :Optional[Any] = [i for i in device_map_blocks if i not in blocks] if len(__a ) != 0: raise ValueError( '''Duplicate attention blocks specified in device_map. Attention blocks must be specified to one device.''' ''' These attention blocks were specified more than once: ''' + str(__a ) ) if len(__a ) != 0: raise ValueError( '''There are attention blocks for this model that are not specified in the device_map. Add these attention ''' '''blocks to a device on the device_map: ''' + str(__a ) ) if len(__a ) != 0: raise ValueError( '''The device_map contains more attention blocks than this model has. Remove these from the device_map:''' + str(__a ) ) def a ( __a , __a ) -> Tuple: '''simple docstring''' UpperCamelCase__ :Optional[Any] = list(range(__a ) ) UpperCamelCase__ :Any = int(ceil(n_layers / len(__a ) ) ) UpperCamelCase__ :List[Any] = [layers[i : i + n_blocks] for i in range(0 , __a , __a )] return dict(zip(__a , __a ) )
219
0
"""simple docstring""" import argparse from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import BigBirdPegasusConfig, BigBirdPegasusForConditionalGeneration lowerCAmelCase__ : Any = [ # tf -> hf ('/', '.'), ('layer_', 'layers.'), ('kernel', 'weight'), ('beta', 'bias'), ('gamma', 'weight'), ('pegasus', 'model'), ] lowerCAmelCase__ : int = [ ('.output.dense', '.fc2'), ('intermediate.LayerNorm', 'final_layer_norm'), ('intermediate.dense', 'fc1'), ] lowerCAmelCase__ : int = ( INIT_COMMON + [ ('attention.self.LayerNorm', 'self_attn_layer_norm'), ('attention.output.dense', 'self_attn.out_proj'), ('attention.self', 'self_attn'), ('attention.encdec.LayerNorm', 'encoder_attn_layer_norm'), ('attention.encdec_output.dense', 'encoder_attn.out_proj'), ('attention.encdec', 'encoder_attn'), ('key', 'k_proj'), ('value', 'v_proj'), ('query', 'q_proj'), ('decoder.LayerNorm', 'decoder.layernorm_embedding'), ] + END_COMMON ) lowerCAmelCase__ : Dict = ( INIT_COMMON + [ ('embeddings.word_embeddings', 'shared.weight'), ('embeddings.position_embeddings', 'embed_positions.weight'), ('attention.self.LayerNorm', 'self_attn_layer_norm'), ('attention.output.dense', 'self_attn.output'), ('attention.self', 'self_attn.self'), ('encoder.LayerNorm', 'encoder.layernorm_embedding'), ] + END_COMMON ) lowerCAmelCase__ : Union[str, Any] = [ 'encdec/key/bias', 'encdec/query/bias', 'encdec/value/bias', 'self/key/bias', 'self/query/bias', 'self/value/bias', 'encdec_output/dense/bias', 'attention/output/dense/bias', ] def a_ ( lowerCamelCase , lowerCamelCase ): for tf_name, hf_name in patterns: UpperCAmelCase__ = k.replace(lowerCamelCase , lowerCamelCase ) return k def a_ ( lowerCamelCase , lowerCamelCase ): UpperCAmelCase__ = BigBirdPegasusConfig(**lowerCamelCase ) UpperCAmelCase__ = BigBirdPegasusForConditionalGeneration(lowerCamelCase ) UpperCAmelCase__ = torch_model.state_dict() UpperCAmelCase__ = {} # separating decoder weights UpperCAmelCase__ = {k: tf_weights[k] for k in tf_weights if k.startswith('pegasus/decoder' )} UpperCAmelCase__ = {k: tf_weights[k] for k in tf_weights if not k.startswith('pegasus/decoder' )} for k, v in tqdm(decoder_weights.items() , 'tf -> hf conversion' ): UpperCAmelCase__ = [k.endswith(lowerCamelCase ) for ending in KEYS_TO_IGNORE] if any(lowerCamelCase ): continue UpperCAmelCase__ = DECODER_PATTERNS UpperCAmelCase__ = rename_state_dict_key(lowerCamelCase , lowerCamelCase ) if new_k not in state_dict: raise ValueError(f'''could not find new key {new_k} in state dict. (converted from {k})''' ) if any(True if i in k else False for i in ['dense', 'query', 'key', 'value'] ): UpperCAmelCase__ = v.T UpperCAmelCase__ = torch.from_numpy(lowerCamelCase ) assert v.shape == state_dict[new_k].shape, f'''{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}''' for k, v in tqdm(remaining_weights.items() , 'tf -> hf conversion' ): UpperCAmelCase__ = [k.endswith(lowerCamelCase ) for ending in KEYS_TO_IGNORE] if any(lowerCamelCase ): continue UpperCAmelCase__ = REMAINING_PATTERNS UpperCAmelCase__ = rename_state_dict_key(lowerCamelCase , lowerCamelCase ) if new_k not in state_dict and k != "pegasus/embeddings/position_embeddings": raise ValueError(f'''could not find new key {new_k} in state dict. (converted from {k})''' ) if any(True if i in k else False for i in ['dense', 'query', 'key', 'value'] ): UpperCAmelCase__ = v.T UpperCAmelCase__ = torch.from_numpy(lowerCamelCase ) if k != "pegasus/embeddings/position_embeddings": assert v.shape == state_dict[new_k].shape, f'''{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}''' UpperCAmelCase__ = mapping['model.embed_positions.weight'] UpperCAmelCase__ = mapping.pop('model.embed_positions.weight' ) UpperCAmelCase__ , UpperCAmelCase__ = torch_model.load_state_dict(lowerCamelCase , strict=lowerCamelCase ) UpperCAmelCase__ = [ k for k in missing if k not in [ 'final_logits_bias', 'model.encoder.embed_tokens.weight', 'model.decoder.embed_tokens.weight', 'lm_head.weight', ] ] assert unexpected_missing == [], f'''no matches found for the following torch keys {unexpected_missing}''' assert extra == [], f'''no matches found for the following tf keys {extra}''' return torch_model def a_ ( lowerCamelCase ): UpperCAmelCase__ = tf.train.list_variables(lowerCamelCase ) UpperCAmelCase__ = {} UpperCAmelCase__ = ['global_step'] for name, shape in tqdm(lowerCamelCase , desc='converting tf checkpoint to dict' ): UpperCAmelCase__ = any(pat in name for pat in ignore_name ) if skip_key: continue UpperCAmelCase__ = tf.train.load_variable(lowerCamelCase , lowerCamelCase ) UpperCAmelCase__ = array return tf_weights def a_ ( lowerCamelCase , lowerCamelCase , lowerCamelCase ): UpperCAmelCase__ = get_tf_weights_as_numpy(lowerCamelCase ) UpperCAmelCase__ = convert_bigbird_pegasus(lowerCamelCase , lowerCamelCase ) torch_model.save_pretrained(lowerCamelCase ) if __name__ == "__main__": lowerCAmelCase__ : int = argparse.ArgumentParser() parser.add_argument('--tf_ckpt_path', type=str, help='passed to tf.train.list_variables') parser.add_argument('--save_dir', default=None, type=str, help='Path to the output PyTorch model.') lowerCAmelCase__ : str = parser.parse_args() lowerCAmelCase__ : int = {} convert_bigbird_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir, config_update=config_update)
98
"""simple docstring""" import argparse import logging import sys from unittest.mock import patch import run_glue_deebert from transformers.testing_utils import TestCasePlus, get_gpu_count, require_torch_non_multi_gpu, slow logging.basicConfig(level=logging.DEBUG) lowerCAmelCase__ : Optional[Any] = logging.getLogger() def a_ ( ): UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument('-f' ) UpperCAmelCase__ = parser.parse_args() return args.f class snake_case ( __UpperCAmelCase ): """simple docstring""" def __lowerCAmelCase ( self : int ): UpperCAmelCase__ = logging.StreamHandler(sys.stdout ) logger.addHandler(lowerCamelCase__ ) def __lowerCAmelCase ( self : Optional[Any] ,lowerCamelCase__ : Union[str, Any] ): UpperCAmelCase__ = get_gpu_count() if n_gpu > 1: pass # XXX: doesn't quite work with n_gpu > 1 https://github.com/huggingface/transformers/issues/10560 # script = f"{self.examples_dir_str}/research_projects/deebert/run_glue_deebert.py" # distributed_args = f"-m torch.distributed.launch --nproc_per_node={n_gpu} {script}".split() # cmd = [sys.executable] + distributed_args + args # execute_subprocess_async(cmd, env=self.get_env()) # XXX: test the results - need to save them first into .json file else: args.insert(0 ,'run_glue_deebert.py' ) with patch.object(lowerCamelCase__ ,'argv' ,lowerCamelCase__ ): UpperCAmelCase__ = run_glue_deebert.main() for value in result.values(): self.assertGreaterEqual(lowerCamelCase__ ,0.6_6_6 ) @slow @require_torch_non_multi_gpu def __lowerCAmelCase ( self : List[str] ): UpperCAmelCase__ = '\n --model_type roberta\n --model_name_or_path roberta-base\n --task_name MRPC\n --do_train\n --do_eval\n --do_lower_case\n --data_dir ./tests/fixtures/tests_samples/MRPC/\n --max_seq_length 128\n --per_gpu_eval_batch_size=1\n --per_gpu_train_batch_size=8\n --learning_rate 2e-4\n --num_train_epochs 3\n --overwrite_output_dir\n --seed 42\n --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --plot_data_dir ./examples/deebert/results/\n --save_steps 0\n --overwrite_cache\n --eval_after_first_stage\n '.split() self.run_and_check(lowerCamelCase__ ) UpperCAmelCase__ = '\n --model_type roberta\n --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --task_name MRPC\n --do_eval\n --do_lower_case\n --data_dir ./tests/fixtures/tests_samples/MRPC/\n --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --plot_data_dir ./examples/deebert/results/\n --max_seq_length 128\n --eval_each_highway\n --eval_highway\n --overwrite_cache\n --per_gpu_eval_batch_size=1\n '.split() self.run_and_check(lowerCamelCase__ ) UpperCAmelCase__ = '\n --model_type roberta\n --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --task_name MRPC\n --do_eval\n --do_lower_case\n --data_dir ./tests/fixtures/tests_samples/MRPC/\n --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --plot_data_dir ./examples/deebert/results/\n --max_seq_length 128\n --early_exit_entropy 0.1\n --eval_highway\n --overwrite_cache\n --per_gpu_eval_batch_size=1\n '.split() self.run_and_check(lowerCamelCase__ )
98
1
import unittest from transformers import is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class UpperCAmelCase_ : @staticmethod def snake_case__ ( *__a, **__a): '''simple docstring''' pass @is_pipeline_test @require_vision class UpperCAmelCase_ ( unittest.TestCase): @require_torch def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : str = pipeline( model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification", ) _lowerCAmelCase : Any = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png") _lowerCAmelCase : Optional[Any] = image_classifier(__a, candidate_labels=["a", "b", "c"]) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(__a), [ [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}], [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "c"}, {"score": 0.333, "label": "b"}], ], ) _lowerCAmelCase : Optional[Any] = image_classifier([image] * 5, candidate_labels=["A", "B", "C"], batch_size=2) self.assertEqual( nested_simplify(__a), [ [ {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, ], [ {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, ], [ {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, ], [ {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, ], [ {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, ], ], ) @require_tf def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : Optional[int] = pipeline( model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification", framework="tf") _lowerCAmelCase : List[Any] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png") _lowerCAmelCase : Union[str, Any] = image_classifier(__a, candidate_labels=["a", "b", "c"]) self.assertEqual( nested_simplify(__a), [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}], ) _lowerCAmelCase : Dict = image_classifier([image] * 5, candidate_labels=["A", "B", "C"], batch_size=2) self.assertEqual( nested_simplify(__a), [ [ {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, ], [ {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, ], [ {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, ], [ {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, ], [ {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, {"score": 0.333, "label": ANY(__a)}, ], ], ) @slow @require_torch def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : Any = pipeline( task="zero-shot-image-classification", model="openai/clip-vit-base-patch32", ) # This is an image of 2 cats with remotes and no planes _lowerCAmelCase : str = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png") _lowerCAmelCase : int = image_classifier(__a, candidate_labels=["cat", "plane", "remote"]) self.assertEqual( nested_simplify(__a), [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ) _lowerCAmelCase : Optional[int] = image_classifier([image] * 5, candidate_labels=["cat", "plane", "remote"], batch_size=2) self.assertEqual( nested_simplify(__a), [ [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ] * 5, ) @slow @require_tf def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : Union[str, Any] = pipeline( task="zero-shot-image-classification", model="openai/clip-vit-base-patch32", framework="tf") # This is an image of 2 cats with remotes and no planes _lowerCAmelCase : Tuple = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png") _lowerCAmelCase : List[str] = image_classifier(__a, candidate_labels=["cat", "plane", "remote"]) self.assertEqual( nested_simplify(__a), [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ) _lowerCAmelCase : Optional[int] = image_classifier([image] * 5, candidate_labels=["cat", "plane", "remote"], batch_size=2) self.assertEqual( nested_simplify(__a), [ [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ] * 5, )
369
import copy from ...configuration_utils import PretrainedConfig from ...utils import add_start_docstrings _snake_case = R"\n [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and\n can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information.\n\n Args:\n title_sep (`str`, *optional*, defaults to `\" / \"`):\n Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`].\n doc_sep (`str`, *optional*, defaults to `\" // \"`):\n Separator inserted between the text of the retrieved document and the original input when calling\n [`RagRetriever`].\n n_docs (`int`, *optional*, defaults to 5):\n Number of documents to retrieve.\n max_combined_length (`int`, *optional*, defaults to 300):\n Max length of contextualized input returned by [`~RagRetriever.__call__`].\n retrieval_vector_size (`int`, *optional*, defaults to 768):\n Dimensionality of the document embeddings indexed by [`RagRetriever`].\n retrieval_batch_size (`int`, *optional*, defaults to 8):\n Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated\n [`RagRetriever`].\n dataset (`str`, *optional*, defaults to `\"wiki_dpr\"`):\n A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids\n using `datasets.list_datasets()`).\n dataset_split (`str`, *optional*, defaults to `\"train\"`)\n Which split of the `dataset` to load.\n index_name (`str`, *optional*, defaults to `\"compressed\"`)\n The index name of the index associated with the `dataset`. One can choose between `\"legacy\"`, `\"exact\"` and\n `\"compressed\"`.\n index_path (`str`, *optional*)\n The path to the serialized faiss index on disk.\n passages_path (`str`, *optional*):\n A path to text passages compatible with the faiss index. Required if using\n [`~models.rag.retrieval_rag.LegacyIndex`]\n use_dummy_dataset (`bool`, *optional*, defaults to `False`)\n Whether to load a \"dummy\" variant of the dataset specified by `dataset`.\n label_smoothing (`float`, *optional*, defaults to 0.0):\n Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing\n in the loss calculation. If set to 0, no label smoothing is performed.\n do_marginalize (`bool`, *optional*, defaults to `False`):\n If `True`, the logits are marginalized over all documents by making use of\n `torch.nn.functional.log_softmax`.\n reduce_loss (`bool`, *optional*, defaults to `False`):\n Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation.\n do_deduplication (`bool`, *optional*, defaults to `True`):\n Whether or not to deduplicate the generations from different context documents for a given input. Has to be\n set to `False` if used while training with distributed backend.\n exclude_bos_score (`bool`, *optional*, defaults to `False`):\n Whether or not to disregard the BOS token when computing the loss.\n output_retrieved(`bool`, *optional*, defaults to `False`):\n If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and\n `context_attention_mask` are returned. See returned tensors for more detail.\n use_cache (`bool`, *optional*, defaults to `True`):\n Whether or not the model should return the last key/values attentions (not used by all models).\n forced_eos_token_id (`int`, *optional*):\n The id of the token to force as the last generated token when `max_length` is reached. Usually set to\n `eos_token_id`.\n" @add_start_docstrings(a) class UpperCAmelCase_ ( a): lowerCamelCase__ = 'rag' lowerCamelCase__ = True def __init__( self, __a=None, __a=True, __a=None, __a=None, __a=None, __a=None, __a=None, __a=" / ", __a=" // ", __a=5, __a=300, __a=768, __a=8, __a="wiki_dpr", __a="train", __a="compressed", __a=None, __a=None, __a=False, __a=False, __a=0.0, __a=True, __a=False, __a=False, __a=False, __a=True, __a=None, **__a, ): '''simple docstring''' super().__init__( bos_token_id=__a, pad_token_id=__a, eos_token_id=__a, decoder_start_token_id=__a, forced_eos_token_id=__a, is_encoder_decoder=__a, prefix=__a, vocab_size=__a, **__a, ) assert ( "question_encoder" in kwargs and "generator" in kwargs ), "Config has to be initialized with question_encoder and generator config" _lowerCAmelCase : List[str] = kwargs.pop("question_encoder") _lowerCAmelCase : Union[str, Any] = question_encoder_config.pop("model_type") _lowerCAmelCase : int = kwargs.pop("generator") _lowerCAmelCase : Optional[Any] = decoder_config.pop("model_type") from ..auto.configuration_auto import AutoConfig _lowerCAmelCase : int = AutoConfig.for_model(__a, **__a) _lowerCAmelCase : Tuple = AutoConfig.for_model(__a, **__a) _lowerCAmelCase : List[Any] = reduce_loss _lowerCAmelCase : Any = label_smoothing _lowerCAmelCase : Optional[int] = exclude_bos_score _lowerCAmelCase : Optional[Any] = do_marginalize _lowerCAmelCase : Any = title_sep _lowerCAmelCase : Any = doc_sep _lowerCAmelCase : Optional[int] = n_docs _lowerCAmelCase : Optional[Any] = max_combined_length _lowerCAmelCase : List[str] = dataset _lowerCAmelCase : List[str] = dataset_split _lowerCAmelCase : Optional[Any] = index_name _lowerCAmelCase : Dict = retrieval_vector_size _lowerCAmelCase : Union[str, Any] = retrieval_batch_size _lowerCAmelCase : Optional[int] = passages_path _lowerCAmelCase : Dict = index_path _lowerCAmelCase : Tuple = use_dummy_dataset _lowerCAmelCase : Union[str, Any] = output_retrieved _lowerCAmelCase : str = do_deduplication _lowerCAmelCase : Union[str, Any] = use_cache if self.forced_eos_token_id is None: _lowerCAmelCase : Tuple = getattr(self.generator, "forced_eos_token_id", __a) @classmethod def snake_case__ ( cls, __a, __a, **__a): '''simple docstring''' return cls(question_encoder=question_encoder_config.to_dict(), generator=generator_config.to_dict(), **__a) def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : Dict = copy.deepcopy(self.__dict__) _lowerCAmelCase : Union[str, Any] = self.question_encoder.to_dict() _lowerCAmelCase : Any = self.generator.to_dict() _lowerCAmelCase : Optional[Any] = self.__class__.model_type return output
300
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging _SCREAMING_SNAKE_CASE = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE = { "transfo-xl-wt103": "https://huggingface.co/transfo-xl-wt103/resolve/main/config.json", } class lowerCAmelCase_ ( lowerCAmelCase__ ): __lowerCamelCase : Union[str, Any] = "transfo-xl" __lowerCamelCase : int = ["mems"] __lowerCamelCase : Dict = { "n_token": "vocab_size", "hidden_size": "d_model", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self , _lowerCAmelCase=267735 , _lowerCAmelCase=[20000, 40000, 200000] , _lowerCAmelCase=1024 , _lowerCAmelCase=1024 , _lowerCAmelCase=16 , _lowerCAmelCase=64 , _lowerCAmelCase=4096 , _lowerCAmelCase=4 , _lowerCAmelCase=False , _lowerCAmelCase=18 , _lowerCAmelCase=1600 , _lowerCAmelCase=1000 , _lowerCAmelCase=True , _lowerCAmelCase=True , _lowerCAmelCase=0 , _lowerCAmelCase=-1 , _lowerCAmelCase=True , _lowerCAmelCase=0.1 , _lowerCAmelCase=0.0 , _lowerCAmelCase=True , _lowerCAmelCase="normal" , _lowerCAmelCase=0.01 , _lowerCAmelCase=0.01 , _lowerCAmelCase=0.02 , _lowerCAmelCase=1E-5 , _lowerCAmelCase=0 , **_lowerCAmelCase , ) -> Tuple: _lowerCAmelCase = vocab_size _lowerCAmelCase = [] self.cutoffs.extend(_UpperCAmelCase ) if proj_share_all_but_first: _lowerCAmelCase = [False] + [True] * len(self.cutoffs ) else: _lowerCAmelCase = [False] + [False] * len(self.cutoffs ) _lowerCAmelCase = d_model _lowerCAmelCase = d_embed _lowerCAmelCase = d_head _lowerCAmelCase = d_inner _lowerCAmelCase = div_val _lowerCAmelCase = pre_lnorm _lowerCAmelCase = n_layer _lowerCAmelCase = n_head _lowerCAmelCase = mem_len _lowerCAmelCase = same_length _lowerCAmelCase = attn_type _lowerCAmelCase = clamp_len _lowerCAmelCase = sample_softmax _lowerCAmelCase = adaptive _lowerCAmelCase = dropout _lowerCAmelCase = dropatt _lowerCAmelCase = untie_r _lowerCAmelCase = init _lowerCAmelCase = init_range _lowerCAmelCase = proj_init_std _lowerCAmelCase = init_std _lowerCAmelCase = layer_norm_epsilon super().__init__(eos_token_id=_UpperCAmelCase , **_UpperCAmelCase ) @property def _snake_case ( self ) -> Any: logger.info(f'''The model {self.model_type} is one of the few models that has no sequence length limit.''' ) return -1 @max_position_embeddings.setter def _snake_case ( self , _lowerCAmelCase ) -> Optional[Any]: raise NotImplementedError( f'''The model {self.model_type} is one of the few models that has no sequence length limit.''' )
158
from __future__ import annotations def __SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE : list[list[int]] ) -> int: # preprocessing the first row for i in range(1 , len(matrix[0] ) ): matrix[0][i] += matrix[0][i - 1] # preprocessing the first column for i in range(1 , len(SCREAMING_SNAKE_CASE ) ): matrix[i][0] += matrix[i - 1][0] # updating the path cost for current position for i in range(1 , len(SCREAMING_SNAKE_CASE ) ): for j in range(1 , len(matrix[0] ) ): matrix[i][j] += min(matrix[i - 1][j] , matrix[i][j - 1] ) return matrix[-1][-1] if __name__ == "__main__": import doctest doctest.testmod()
325
0
from ...configuration_utils import PretrainedConfig from ...utils import logging __A : List[str] = logging.get_logger(__name__) __A : Any = {'''ctrl''': '''https://huggingface.co/ctrl/resolve/main/config.json'''} class __A ( lowerCAmelCase ): lowerCAmelCase_ : List[Any] = "ctrl" lowerCAmelCase_ : Optional[int] = ["past_key_values"] lowerCAmelCase_ : Any = { "max_position_embeddings": "n_positions", "hidden_size": "n_embd", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self : Tuple , UpperCAmelCase_ : Dict=246534 , UpperCAmelCase_ : List[str]=256 , UpperCAmelCase_ : List[Any]=1280 , UpperCAmelCase_ : Any=8192 , UpperCAmelCase_ : Tuple=48 , UpperCAmelCase_ : str=16 , UpperCAmelCase_ : List[Any]=0.1 , UpperCAmelCase_ : Tuple=0.1 , UpperCAmelCase_ : Tuple=1E-6 , UpperCAmelCase_ : Dict=0.02 , UpperCAmelCase_ : List[Any]=True , **UpperCAmelCase_ : List[Any] , ): lowerCAmelCase : List[Any] = vocab_size lowerCAmelCase : str = n_positions lowerCAmelCase : Dict = n_embd lowerCAmelCase : Any = n_layer lowerCAmelCase : List[Any] = n_head lowerCAmelCase : str = dff lowerCAmelCase : Optional[int] = resid_pdrop lowerCAmelCase : Any = embd_pdrop lowerCAmelCase : List[Any] = layer_norm_epsilon lowerCAmelCase : str = initializer_range lowerCAmelCase : List[str] = use_cache super().__init__(**UpperCAmelCase_ )
353
from __future__ import annotations from typing import Any class __A : def __init__( self : Optional[Any] , UpperCAmelCase_ : int ): lowerCAmelCase : Tuple = num_of_nodes lowerCAmelCase : list[list[int]] = [] lowerCAmelCase : dict[int, int] = {} def lowercase__ ( self : List[str] , UpperCAmelCase_ : int , UpperCAmelCase_ : int , UpperCAmelCase_ : int ): self.m_edges.append([u_node, v_node, weight] ) def lowercase__ ( self : Dict , UpperCAmelCase_ : int ): if self.m_component[u_node] == u_node: return u_node return self.find_component(self.m_component[u_node] ) def lowercase__ ( self : Optional[int] , UpperCAmelCase_ : int ): if self.m_component[u_node] != u_node: for k in self.m_component: lowerCAmelCase : Dict = self.find_component(UpperCAmelCase_ ) def lowercase__ ( self : List[str] , UpperCAmelCase_ : list[int] , UpperCAmelCase_ : int , UpperCAmelCase_ : int ): if component_size[u_node] <= component_size[v_node]: lowerCAmelCase : Optional[int] = v_node component_size[v_node] += component_size[u_node] self.set_component(UpperCAmelCase_ ) elif component_size[u_node] >= component_size[v_node]: lowerCAmelCase : Union[str, Any] = self.find_component(UpperCAmelCase_ ) component_size[u_node] += component_size[v_node] self.set_component(UpperCAmelCase_ ) def lowercase__ ( self : str ): lowerCAmelCase : str = [] lowerCAmelCase : Tuple = 0 lowerCAmelCase : list[Any] = [-1] * self.m_num_of_nodes # A list of components (initialized to all of the nodes) for node in range(self.m_num_of_nodes ): self.m_component.update({node: node} ) component_size.append(1 ) lowerCAmelCase : int = self.m_num_of_nodes while num_of_components > 1: for edge in self.m_edges: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Union[str, Any] = edge lowerCAmelCase : Optional[int] = self.m_component[u] lowerCAmelCase : str = self.m_component[v] if u_component != v_component: for component in (u_component, v_component): if ( minimum_weight_edge[component] == -1 or minimum_weight_edge[component][2] > w ): lowerCAmelCase : str = [u, v, w] for edge in minimum_weight_edge: if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Any = edge lowerCAmelCase : Optional[Any] = self.m_component[u] lowerCAmelCase : Optional[Any] = self.m_component[v] if u_component != v_component: mst_weight += w self.union(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) print(f"Added edge [{u} - {v}]\nAdded weight: {w}\n" ) num_of_components -= 1 lowerCAmelCase : Optional[Any] = [-1] * self.m_num_of_nodes print(f"The total weight of the minimal spanning tree is: {mst_weight}" ) def SCREAMING_SNAKE_CASE__ ( ) -> None: '''simple docstring''' if __name__ == "__main__": import doctest doctest.testmod()
323
0
# Lint as: python3 # pylint: enable=line-too-long # pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position _snake_case = "2.13.1" import platform import pyarrow from packaging import version if version.parse(platform.python_version()) < version.parse("3.7"): raise ImportWarning( "To use `datasets`, Python>=3.7 is required, and the current version of Python doesn't match this condition." ) if version.parse(pyarrow.__version__).major < 8: raise ImportWarning( "To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn't match this condition.\n" "If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`." ) del platform del pyarrow del version from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled from .info import DatasetInfo, MetricInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, list_datasets, list_metrics, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric from .metric import Metric from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .tasks import * from .utils import * from .utils import logging # deprecated modules from datasets import arrow_dataset as _arrow_dataset # isort:skip from datasets import utils as _utils # isort:skip from datasets.utils import download_manager as _deprecated_download_manager # isort:skip _snake_case = concatenate_datasets _snake_case = DownloadConfig _snake_case = DownloadManager _snake_case = DownloadMode _snake_case = DownloadConfig _snake_case = DownloadMode _snake_case = DownloadManager del _arrow_dataset, _utils, _deprecated_download_manager
26
"""simple docstring""" import unittest from transformers import SPIECE_UNDERLINE from transformers.models.speechta import SpeechTaTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.tokenization_utils import AddedToken from ...test_tokenization_common import TokenizerTesterMixin __SCREAMING_SNAKE_CASE : Tuple = get_tests_dir('fixtures/test_sentencepiece_bpe_char.model') @require_sentencepiece @require_tokenizers class __A (snake_case__ , unittest.TestCase): '''simple docstring''' __lowercase: Tuple = SpeechTaTokenizer __lowercase: int = False __lowercase: List[str] = True def lowerCAmelCase ( self : Any ) ->str: """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing snake_case_ = SpeechTaTokenizer(UpperCAmelCase_ ) snake_case_ = AddedToken("""<mask>""" , lstrip=UpperCAmelCase_ , rstrip=UpperCAmelCase_ ) snake_case_ = mask_token tokenizer.add_special_tokens({"""mask_token""": mask_token} ) tokenizer.add_tokens(["""<ctc_blank>"""] ) tokenizer.save_pretrained(self.tmpdirname ) def lowerCAmelCase ( self : Optional[Any] , UpperCAmelCase_ : Optional[Any] ) ->Dict: """simple docstring""" snake_case_ = """this is a test""" snake_case_ = """this is a test""" return input_text, output_text def lowerCAmelCase ( self : str , UpperCAmelCase_ : int , UpperCAmelCase_ : Any=False , UpperCAmelCase_ : Tuple=20 , UpperCAmelCase_ : Dict=5 ) ->List[Any]: """simple docstring""" snake_case_ , snake_case_ = self.get_input_output_texts(UpperCAmelCase_ ) snake_case_ = tokenizer.encode(UpperCAmelCase_ , add_special_tokens=UpperCAmelCase_ ) snake_case_ = tokenizer.decode(UpperCAmelCase_ , clean_up_tokenization_spaces=UpperCAmelCase_ ) return text, ids def lowerCAmelCase ( self : Union[str, Any] ) ->Optional[Any]: """simple docstring""" snake_case_ = """<pad>""" snake_case_ = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCAmelCase_ ) , UpperCAmelCase_ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCAmelCase_ ) , UpperCAmelCase_ ) def lowerCAmelCase ( self : int ) ->str: """simple docstring""" snake_case_ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , """<s>""" ) self.assertEqual(vocab_keys[1] , """<pad>""" ) self.assertEqual(vocab_keys[-4] , """œ""" ) self.assertEqual(vocab_keys[-2] , """<mask>""" ) self.assertEqual(vocab_keys[-1] , """<ctc_blank>""" ) self.assertEqual(len(UpperCAmelCase_ ) , 81 ) def lowerCAmelCase ( self : Optional[int] ) ->int: """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 79 ) def lowerCAmelCase ( self : Optional[int] ) ->List[Any]: """simple docstring""" snake_case_ = self.get_tokenizers(do_lower_case=UpperCAmelCase_ ) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): snake_case_ = tokenizer.vocab_size snake_case_ = len(UpperCAmelCase_ ) self.assertNotEqual(UpperCAmelCase_ , 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) snake_case_ = ["""aaaaa bbbbbb""", """cccccccccdddddddd"""] snake_case_ = tokenizer.add_tokens(UpperCAmelCase_ ) snake_case_ = tokenizer.vocab_size snake_case_ = len(UpperCAmelCase_ ) self.assertNotEqual(UpperCAmelCase_ , 0 ) self.assertEqual(UpperCAmelCase_ , UpperCAmelCase_ ) self.assertEqual(UpperCAmelCase_ , len(UpperCAmelCase_ ) ) self.assertEqual(UpperCAmelCase_ , all_size + len(UpperCAmelCase_ ) ) snake_case_ = tokenizer.encode("""aaaaa bbbbbb low cccccccccdddddddd l""" , add_special_tokens=UpperCAmelCase_ ) self.assertGreaterEqual(len(UpperCAmelCase_ ) , 4 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) snake_case_ = {"""eos_token""": """>>>>|||<||<<|<<""", """pad_token""": """<<<<<|||>|>>>>|>"""} snake_case_ = tokenizer.add_special_tokens(UpperCAmelCase_ ) snake_case_ = tokenizer.vocab_size snake_case_ = len(UpperCAmelCase_ ) self.assertNotEqual(UpperCAmelCase_ , 0 ) self.assertEqual(UpperCAmelCase_ , UpperCAmelCase_ ) self.assertEqual(UpperCAmelCase_ , len(UpperCAmelCase_ ) ) self.assertEqual(UpperCAmelCase_ , all_size_a + len(UpperCAmelCase_ ) ) snake_case_ = tokenizer.encode( """>>>>|||<||<<|<< aaaaabbbbbb low cccccccccdddddddd <<<<<|||>|>>>>|> l""" , add_special_tokens=UpperCAmelCase_ ) self.assertGreaterEqual(len(UpperCAmelCase_ ) , 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 ) def lowerCAmelCase ( self : Optional[Any] ) ->Tuple: """simple docstring""" pass def lowerCAmelCase ( self : List[str] ) ->Optional[Any]: """simple docstring""" pass def lowerCAmelCase ( self : List[str] ) ->List[str]: """simple docstring""" snake_case_ = self.get_tokenizer() snake_case_ = tokenizer.tokenize("""This is a test""" ) # fmt: off self.assertListEqual(UpperCAmelCase_ , [SPIECE_UNDERLINE, """T""", """h""", """i""", """s""", SPIECE_UNDERLINE, """i""", """s""", SPIECE_UNDERLINE, """a""", SPIECE_UNDERLINE, """t""", """e""", """s""", """t"""] ) # fmt: on self.assertListEqual( tokenizer.convert_tokens_to_ids(UpperCAmelCase_ ) , [4, 32, 11, 10, 12, 4, 10, 12, 4, 7, 4, 6, 5, 12, 6] , ) snake_case_ = tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( UpperCAmelCase_ , [SPIECE_UNDERLINE, """I""", SPIECE_UNDERLINE, """w""", """a""", """s""", SPIECE_UNDERLINE, """b""", """o""", """r""", """n""", SPIECE_UNDERLINE, """i""", """n""", SPIECE_UNDERLINE, """92000""", """,""", SPIECE_UNDERLINE, """a""", """n""", """d""", SPIECE_UNDERLINE, """t""", """h""", """i""", """s""", SPIECE_UNDERLINE, """i""", """s""", SPIECE_UNDERLINE, """f""", """a""", """l""", """s""", """é""", """."""] ) snake_case_ = tokenizer.convert_tokens_to_ids(UpperCAmelCase_ ) # fmt: off self.assertListEqual(UpperCAmelCase_ , [4, 30, 4, 20, 7, 12, 4, 25, 8, 13, 9, 4, 10, 9, 4, 3, 23, 4, 7, 9, 14, 4, 6, 11, 10, 12, 4, 10, 12, 4, 19, 7, 15, 12, 73, 26] ) # fmt: on snake_case_ = tokenizer.convert_ids_to_tokens(UpperCAmelCase_ ) self.assertListEqual( UpperCAmelCase_ , [SPIECE_UNDERLINE, """I""", SPIECE_UNDERLINE, """w""", """a""", """s""", SPIECE_UNDERLINE, """b""", """o""", """r""", """n""", SPIECE_UNDERLINE, """i""", """n""", SPIECE_UNDERLINE, """<unk>""", """,""", SPIECE_UNDERLINE, """a""", """n""", """d""", SPIECE_UNDERLINE, """t""", """h""", """i""", """s""", SPIECE_UNDERLINE, """i""", """s""", SPIECE_UNDERLINE, """f""", """a""", """l""", """s""", """é""", """."""] ) @slow def lowerCAmelCase ( self : str ) ->Dict: """simple docstring""" snake_case_ = [ """Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides """ """general-purpose architectures (BERT, GPT, RoBERTa, XLM, DistilBert, XLNet...) for Natural """ """Language Understanding (NLU) and Natural Language Generation (NLG) with over thirty-two pretrained """ """models in one hundred plus languages and deep interoperability between Jax, PyTorch and TensorFlow.""", """BERT is designed to pre-train deep bidirectional representations from unlabeled text by jointly """ """conditioning on both left and right context in all layers.""", """The quick brown fox jumps over the lazy dog.""", ] # fmt: off snake_case_ = { """input_ids""": [ [4, 32, 13, 7, 9, 12, 19, 8, 13, 18, 5, 13, 12, 4, 64, 19, 8, 13, 18, 5, 13, 15, 22, 4, 28, 9, 8, 20, 9, 4, 7, 12, 4, 24, 22, 6, 8, 13, 17, 11, 39, 6, 13, 7, 9, 12, 19, 8, 13, 18, 5, 13, 12, 4, 7, 9, 14, 4, 24, 22, 6, 8, 13, 17, 11, 39, 24, 13, 5, 6, 13, 7, 10, 9, 5, 14, 39, 25, 5, 13, 6, 63, 4, 24, 13, 8, 27, 10, 14, 5, 12, 4, 21, 5, 9, 5, 13, 7, 15, 39, 24, 16, 13, 24, 8, 12, 5, 4, 7, 13, 17, 11, 10, 6, 5, 17, 6, 16, 13, 5, 12, 4, 64, 40, 47, 54, 32, 23, 4, 53, 49, 32, 23, 4, 54, 8, 40, 47, 54, 32, 7, 23, 4, 69, 52, 43, 23, 4, 51, 10, 12, 6, 10, 15, 40, 5, 13, 6, 23, 4, 69, 52, 48, 5, 6, 26, 26, 26, 63, 4, 19, 8, 13, 4, 48, 7, 6, 16, 13, 7, 15, 4, 52, 7, 9, 21, 16, 7, 21, 5, 4, 61, 9, 14, 5, 13, 12, 6, 7, 9, 14, 10, 9, 21, 4, 64, 48, 52, 61, 63, 4, 7, 9, 14, 4, 48, 7, 6, 16, 13, 7, 15, 4, 52, 7, 9, 21, 16, 7, 21, 5, 4, 53, 5, 9, 5, 13, 7, 6, 10, 8, 9, 4, 64, 48, 52, 53, 63, 4, 20, 10, 6, 11, 4, 8, 27, 5, 13, 4, 6, 11, 10, 13, 6, 22, 39, 6, 20, 8, 4, 24, 13, 5, 6, 13, 7, 10, 9, 5, 14, 4, 18, 8, 14, 5, 15, 12, 4, 10, 9, 4, 8, 9, 5, 4, 11, 16, 9, 14, 13, 5, 14, 4, 24, 15, 16, 12, 4, 15, 7, 9, 21, 16, 7, 21, 5, 12, 4, 7, 9, 14, 4, 14, 5, 5, 24, 4, 10, 9, 6, 5, 13, 8, 24, 5, 13, 7, 25, 10, 15, 10, 6, 22, 4, 25, 5, 6, 20, 5, 5, 9, 4, 58, 7, 37, 23, 4, 49, 22, 32, 8, 13, 17, 11, 4, 7, 9, 14, 4, 32, 5, 9, 12, 8, 13, 55, 15, 8, 20, 26, 2], [4, 40, 47, 54, 32, 4, 10, 12, 4, 14, 5, 12, 10, 21, 9, 5, 14, 4, 6, 8, 4, 24, 13, 5, 39, 6, 13, 7, 10, 9, 4, 14, 5, 5, 24, 4, 25, 10, 14, 10, 13, 5, 17, 6, 10, 8, 9, 7, 15, 4, 13, 5, 24, 13, 5, 12, 5, 9, 6, 7, 6, 10, 8, 9, 12, 4, 19, 13, 8, 18, 4, 16, 9, 15, 7, 25, 5, 15, 5, 14, 4, 6, 5, 37, 6, 4, 25, 22, 4, 46, 8, 10, 9, 6, 15, 22, 4, 17, 8, 9, 14, 10, 6, 10, 8, 9, 10, 9, 21, 4, 8, 9, 4, 25, 8, 6, 11, 4, 15, 5, 19, 6, 4, 7, 9, 14, 4, 13, 10, 21, 11, 6, 4, 17, 8, 9, 6, 5, 37, 6, 4, 10, 9, 4, 7, 15, 15, 4, 15, 7, 22, 5, 13, 12, 26, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [4, 32, 11, 5, 4, 45, 16, 10, 17, 28, 4, 25, 13, 8, 20, 9, 4, 19, 8, 37, 4, 46, 16, 18, 24, 12, 4, 8, 27, 5, 13, 4, 6, 11, 5, 4, 15, 7, 57, 22, 4, 14, 8, 21, 26, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ], """attention_mask""": [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] } # fmt: on self.tokenizer_integration_test_util( expected_encoding=UpperCAmelCase_ , model_name="""microsoft/speecht5_asr""" , revision="""c5ef64c71905caeccde0e4462ef3f9077224c524""" , sequences=UpperCAmelCase_ , )
347
0
'''simple docstring''' from dataclasses import dataclass from typing import List, Optional, Union import numpy as np import torch from ...utils import BaseOutput, OptionalDependencyNotAvailable, is_torch_available, is_transformers_available @dataclass class UpperCamelCase__( lowerCAmelCase ): __magic_name__ : Union[List[np.ndarray], torch.FloatTensor] try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipeline_text_to_video_synth import TextToVideoSDPipeline from .pipeline_text_to_video_synth_imgaimg import VideoToVideoSDPipeline # noqa: F401 from .pipeline_text_to_video_zero import TextToVideoZeroPipeline
91
'''simple docstring''' from __future__ import annotations from collections import namedtuple from dataclasses import dataclass @dataclass class UpperCamelCase__: __magic_name__ : int __magic_name__ : TreeNode | None = None __magic_name__ : TreeNode | None = None _lowercase : Tuple = namedtuple("""CoinsDistribResult""", """moves excess""") def lowerCamelCase__ ( A : TreeNode | None ): '''simple docstring''' if root is None: return 0 # Validation def count_nodes(A : TreeNode | None ) -> int: if node is None: return 0 return count_nodes(node.left ) + count_nodes(node.right ) + 1 def count_coins(A : TreeNode | None ) -> int: if node is None: return 0 return count_coins(node.left ) + count_coins(node.right ) + node.data if count_nodes(A ) != count_coins(A ): raise ValueError('''The nodes number should be same as the number of coins''' ) # Main calculation def get_distrib(A : TreeNode | None ) -> CoinsDistribResult: if node is None: return CoinsDistribResult(0 , 1 ) UpperCAmelCase , UpperCAmelCase = get_distrib(node.left ) UpperCAmelCase , UpperCAmelCase = get_distrib(node.right ) UpperCAmelCase = 1 - left_distrib_excess UpperCAmelCase = 1 - right_distrib_excess UpperCAmelCase = ( left_distrib_moves + right_distrib_moves + abs(A ) + abs(A ) ) UpperCAmelCase = node.data - coins_to_left - coins_to_right return CoinsDistribResult(A , A ) return get_distrib(A )[0] if __name__ == "__main__": import doctest doctest.testmod()
91
1
"""simple docstring""" from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": UpperCAmelCase : str = input('Enter image url: ').strip() print(F"Downloading image from {url} ...") UpperCAmelCase : Any = BeautifulSoup(requests.get(url).content, 'html.parser') # The image URL is in the content field of the first meta tag with property og:image UpperCAmelCase : List[Any] = soup.find('meta', {'property': 'og:image'})['''content'''] UpperCAmelCase : Tuple = requests.get(image_url).content UpperCAmelCase : Union[str, Any] = F"{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg" with open(file_name, 'wb') as fp: fp.write(image_data) print(F"Done. Image saved to disk as {file_name}.")
115
import unittest from dataclasses import dataclass import pytest from accelerate.commands.config.config_args import SageMakerConfig from accelerate.utils import ComputeEnvironment from accelerate.utils.launch import _convert_nargs_to_dict @dataclass class __snake_case ( lowerCamelCase_ ): lowerCAmelCase_ = ComputeEnvironment.AMAZON_SAGEMAKER lowerCAmelCase_ = True lowerCAmelCase_ = "ml.p3.2xlarge" lowerCAmelCase_ = "accelerate_sagemaker_execution_role" lowerCAmelCase_ = "hf-sm" lowerCAmelCase_ = "us-east-1" lowerCAmelCase_ = 1 lowerCAmelCase_ = "accelerate-sagemaker-1" lowerCAmelCase_ = "1.6" lowerCAmelCase_ = "4.4" lowerCAmelCase_ = "train.py" lowerCAmelCase_ = [ "--model_name_or_path", "bert", "--do_train", "False", "--epochs", "3", "--learning_rate", "5e-5", "--max_steps", "50.5", ] lowerCAmelCase_ = [ "--model_name_or_path", "bert", "--do_train", "--do_test", "False", "--do_predict", "--epochs", "3", "--learning_rate", "5e-5", "--max_steps", "50.5", ] class __snake_case ( unittest.TestCase ): def __a ( self : List[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = _convert_nargs_to_dict(MockLaunchConfig.success_training_script_args ) assert isinstance(converted_args["""model_name_or_path"""] , _lowercase ) assert isinstance(converted_args["""do_train"""] , _lowercase ) assert isinstance(converted_args["""epochs"""] , _lowercase ) assert isinstance(converted_args["""learning_rate"""] , _lowercase ) assert isinstance(converted_args["""max_steps"""] , _lowercase ) with pytest.raises(_lowercase ): _convert_nargs_to_dict(MockLaunchConfig.fail_training_script_args )
219
0
import gc import unittest import numpy as np import torch from diffusers import ( AudioDiffusionPipeline, AutoencoderKL, DDIMScheduler, DDPMScheduler, DiffusionPipeline, Mel, UNetaDConditionModel, UNetaDModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class _lowercase ( unittest.TestCase ): """simple docstring""" def UpperCamelCase_ (self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @property def UpperCamelCase_ (self ): """simple docstring""" torch.manual_seed(0 ) a = UNetaDModel( sample_size=(32, 64) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=("AttnDownBlock2D", "DownBlock2D") , up_block_types=("UpBlock2D", "AttnUpBlock2D") , ) return model @property def UpperCamelCase_ (self ): """simple docstring""" torch.manual_seed(0 ) a = UNetaDConditionModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=("CrossAttnDownBlock2D", "DownBlock2D") , up_block_types=("UpBlock2D", "CrossAttnUpBlock2D") , cross_attention_dim=10 , ) return model @property def UpperCamelCase_ (self ): """simple docstring""" torch.manual_seed(0 ) a = AutoencoderKL( sample_size=(128, 64) , in_channels=1 , out_channels=1 , latent_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=("DownEncoderBlock2D", "DownEncoderBlock2D") , up_block_types=("UpDecoderBlock2D", "UpDecoderBlock2D") , ) a = UNetaDModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=("AttnDownBlock2D", "DownBlock2D") , up_block_types=("UpBlock2D", "AttnUpBlock2D") , ) return vqvae, unet @slow def UpperCamelCase_ (self ): """simple docstring""" a = "cpu" # ensure determinism for the device-dependent torch.Generator a = Mel( x_res=self.dummy_unet.config.sample_size[1] , y_res=self.dummy_unet.config.sample_size[0] , ) a = DDPMScheduler() a = AudioDiffusionPipeline(vqvae=lowerCamelCase_ , unet=self.dummy_unet , mel=lowerCamelCase_ , scheduler=lowerCamelCase_ ) a = pipe.to(lowerCamelCase_ ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) a = torch.Generator(device=lowerCamelCase_ ).manual_seed(42 ) a = pipe(generator=lowerCamelCase_ , steps=4 ) a = output.audios[0] a = output.images[0] a = torch.Generator(device=lowerCamelCase_ ).manual_seed(42 ) a = pipe(generator=lowerCamelCase_ , steps=4 , return_dict=lowerCamelCase_ ) a = output[0][0] assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length) assert ( image.height == self.dummy_unet.config.sample_size[0] and image.width == self.dummy_unet.config.sample_size[1] ) a = np.frombuffer(image.tobytes() , dtype="uint8" )[:10] a = np.frombuffer(image_from_tuple.tobytes() , dtype="uint8" )[:10] a = np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() == 0 a = Mel( x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1] , y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0] , ) a = DDIMScheduler() a = self.dummy_vqvae_and_unet a = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=dummy_vqvae_and_unet[1] , mel=lowerCamelCase_ , scheduler=lowerCamelCase_ ) a = pipe.to(lowerCamelCase_ ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) np.random.seed(0 ) a = np.random.uniform(-1 , 1 , ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,) ) a = torch.Generator(device=lowerCamelCase_ ).manual_seed(42 ) a = pipe(raw_audio=lowerCamelCase_ , generator=lowerCamelCase_ , start_step=5 , steps=10 ) a = output.images[0] assert ( image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0] and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1] ) a = np.frombuffer(image.tobytes() , dtype="uint8" )[:10] a = np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 a = self.dummy_unet_condition a = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=lowerCamelCase_ , mel=lowerCamelCase_ , scheduler=lowerCamelCase_ ) a = pipe.to(lowerCamelCase_ ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) np.random.seed(0 ) a = torch.rand((1, 1, 10) ) a = pipe(generator=lowerCamelCase_ , encoding=lowerCamelCase_ ) a = output.images[0] a = np.frombuffer(image.tobytes() , dtype="uint8" )[:10] a = np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 @slow @require_torch_gpu class _lowercase ( unittest.TestCase ): """simple docstring""" def UpperCamelCase_ (self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase_ (self ): """simple docstring""" a = torch_device a = DiffusionPipeline.from_pretrained("teticio/audio-diffusion-ddim-256" ) a = pipe.to(lowerCamelCase_ ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) a = torch.Generator(device=lowerCamelCase_ ).manual_seed(42 ) a = pipe(generator=lowerCamelCase_ ) a = output.audios[0] a = output.images[0] assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length) assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1] a = np.frombuffer(image.tobytes() , dtype="uint8" )[:10] a = np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0
71
def a( A : int , A : float , A : float ) -> float: """simple docstring""" return round(float(moles / volume ) * nfactor ) def a( A : float , A : float , A : float ) -> float: """simple docstring""" return round(float((moles * 0.0_821 * temperature) / (volume) ) ) def a( A : float , A : float , A : float ) -> float: """simple docstring""" return round(float((moles * 0.0_821 * temperature) / (pressure) ) ) def a( A : float , A : float , A : float ) -> float: """simple docstring""" return round(float((pressure * volume) / (0.0_821 * moles) ) ) if __name__ == "__main__": import doctest doctest.testmod()
71
1
def UpperCamelCase (lowercase_: int ) -> list: A__ : Union[str, Any] = int(_lowerCAmelCase ) if n_element < 1: A__ : Dict = ValueError("""a should be a positive number""" ) raise my_error A__ : Union[str, Any] = [1] A__ : str = (0, 0, 0) A__ : Dict = 1 while index < n_element: while hamming_list[i] * 2 <= hamming_list[-1]: i += 1 while hamming_list[j] * 3 <= hamming_list[-1]: j += 1 while hamming_list[k] * 5 <= hamming_list[-1]: k += 1 hamming_list.append( min(hamming_list[i] * 2 , hamming_list[j] * 3 , hamming_list[k] * 5 ) ) index += 1 return hamming_list if __name__ == "__main__": A_ : List[str] = input('Enter the last number (nth term) of the Hamming Number Series: ') print('Formula of Hamming Number Series => 2^i * 3^j * 5^k') A_ : List[Any] = hamming(int(n)) print('-----------------------------------------------------') print(f'''The list with nth numbers is: {hamming_numbers}''') print('-----------------------------------------------------')
192
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 __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : Optional[int] ) -> str: assert isinstance(_lowerCAmelCase , _lowerCAmelCase ) 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 __snake_case ( _lowerCAmelCase : Dict , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : List[Any] ) -> Optional[int]: A_ : Tuple = 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_ : Optional[Any] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase , keep_in_memory=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) @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 __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : List[Any] ) -> str: A_ : List[Any] = tmp_path / "cache" A_ : List[str] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : int = features.copy() if features else default_expected_features A_ : str = ( Features({feature: Value(_lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A_ : Union[str, Any] = ParquetDatasetReader(_lowerCAmelCase , features=_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def __snake_case ( _lowerCAmelCase : Any , _lowerCAmelCase : Any , _lowerCAmelCase : Any ) -> Optional[Any]: A_ : Dict = tmp_path / "cache" A_ : int = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Optional[int] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase , split=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("path_type" , [str, list] ) def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any] ) -> List[str]: if issubclass(_lowerCAmelCase , _lowerCAmelCase ): A_ : int = parquet_path elif issubclass(_lowerCAmelCase , _lowerCAmelCase ): A_ : Optional[int] = [parquet_path] A_ : Optional[int] = tmp_path / "cache" A_ : Union[str, Any] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Optional[int] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Union[str, Any]=("train",) ) -> Tuple: assert isinstance(_lowerCAmelCase , _lowerCAmelCase ) for split in splits: A_ : List[str] = 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 __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Dict ) -> Optional[int]: A_ : Optional[Any] = tmp_path / "cache" A_ : Union[str, Any] = {"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( {"train": parquet_path} , cache_dir=_lowerCAmelCase , keep_in_memory=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase ) @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 __snake_case ( _lowerCAmelCase : Tuple , _lowerCAmelCase : Any , _lowerCAmelCase : str ) -> Tuple: A_ : Optional[Any] = tmp_path / "cache" A_ : Any = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : List[str] = features.copy() if features else default_expected_features A_ : Tuple = ( Features({feature: Value(_lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A_ : Optional[int] = ParquetDatasetReader({"train": parquet_path} , features=_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : str , _lowerCAmelCase : Any ) -> Union[str, Any]: if split: A_ : Any = {split: parquet_path} else: A_ : Optional[Any] = "train" A_ : str = {"train": parquet_path, "test": parquet_path} A_ : Any = tmp_path / "cache" A_ : str = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Dict = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def __snake_case ( _lowerCAmelCase : Dict , _lowerCAmelCase : Optional[Any] ) -> Dict: A_ : List[str] = ParquetDatasetWriter(_lowerCAmelCase , tmp_path / "foo.parquet" ) assert writer.write() > 0 A_ : Tuple = pq.ParquetFile(tmp_path / "foo.parquet" ) A_ : Dict = pf.read() assert dataset.data.table == output_table def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : int ) -> List[Any]: A_ : Tuple = str(shared_datadir / "test_image_rgb.jpg" ) A_ : int = {"image": [image_path]} A_ : Optional[Any] = Features({"image": Image()} ) A_ : Union[str, Any] = Dataset.from_dict(_lowerCAmelCase , features=_lowerCAmelCase ) A_ : Tuple = ParquetDatasetWriter(_lowerCAmelCase , tmp_path / "foo.parquet" ) assert writer.write() > 0 A_ : str = Dataset.from_parquet(str(tmp_path / "foo.parquet" ) ) assert dataset.features == reloaded_dataset.features A_ : int = ParquetDatasetReader(str(tmp_path / "foo.parquet" ) , streaming=_lowerCAmelCase ).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 __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : List[Any] ) -> Any: assert get_writer_batch_size(_lowerCAmelCase ) == expected
300
0
def lowerCAmelCase( SCREAMING_SNAKE_CASE_ )-> float: """simple docstring""" if edge <= 0 or not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): raise ValueError("Length must be a positive." ) return 3 * ((2_5 + 1_0 * (5 ** (1 / 2))) ** (1 / 2)) * (edge**2) def lowerCAmelCase( SCREAMING_SNAKE_CASE_ )-> float: """simple docstring""" if edge <= 0 or not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): raise ValueError("Length must be a positive." ) return ((1_5 + (7 * (5 ** (1 / 2)))) / 4) * (edge**3) if __name__ == "__main__": import doctest doctest.testmod()
60
import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class __magic_name__ ( unittest.TestCase ): def __init__( self , _lowercase , _lowercase=13 , _lowercase=7 , _lowercase=True , _lowercase=True , _lowercase=True , _lowercase=True , _lowercase=99 , _lowercase=32 , _lowercase=5 , _lowercase=4 , _lowercase=37 , _lowercase="gelu" , _lowercase=0.1 , _lowercase=0.1 , _lowercase=512 , _lowercase=16 , _lowercase=2 , _lowercase=0.02 , _lowercase=4 , )-> Union[str, Any]: UpperCamelCase_ = parent UpperCamelCase_ = batch_size UpperCamelCase_ = seq_length UpperCamelCase_ = is_training UpperCamelCase_ = use_attention_mask UpperCamelCase_ = use_token_type_ids UpperCamelCase_ = use_labels UpperCamelCase_ = vocab_size UpperCamelCase_ = hidden_size UpperCamelCase_ = num_hidden_layers UpperCamelCase_ = num_attention_heads UpperCamelCase_ = intermediate_size UpperCamelCase_ = hidden_act UpperCamelCase_ = hidden_dropout_prob UpperCamelCase_ = attention_probs_dropout_prob UpperCamelCase_ = max_position_embeddings UpperCamelCase_ = type_vocab_size UpperCamelCase_ = type_sequence_label_size UpperCamelCase_ = initializer_range UpperCamelCase_ = num_choices def UpperCAmelCase_ ( self )-> Optional[Any]: UpperCamelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase_ = None if self.use_attention_mask: UpperCamelCase_ = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase_ = None if self.use_token_type_ids: UpperCamelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCamelCase_ = AlbertConfig( vocab_size=self.vocab_size , 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 , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_lowercase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def UpperCAmelCase_ ( self )-> str: UpperCamelCase_ = self.prepare_config_and_inputs() UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ = config_and_inputs UpperCamelCase_ = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask} return config, inputs_dict @require_flax class __magic_name__ ( snake_case , unittest.TestCase ): UpperCamelCase_ :Optional[Any] = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def UpperCAmelCase_ ( self )-> Optional[int]: UpperCamelCase_ = FlaxAlbertModelTester(self ) @slow def UpperCAmelCase_ ( self )-> str: for model_class_name in self.all_model_classes: UpperCamelCase_ = model_class_name.from_pretrained("albert-base-v2" ) UpperCamelCase_ = model(np.ones((1, 1) ) ) self.assertIsNotNone(_lowercase ) @require_flax class __magic_name__ ( unittest.TestCase ): @slow def UpperCAmelCase_ ( self )-> List[str]: UpperCamelCase_ = FlaxAlbertModel.from_pretrained("albert-base-v2" ) UpperCamelCase_ = np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] ) UpperCamelCase_ = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) UpperCamelCase_ = model(_lowercase , attention_mask=_lowercase )[0] UpperCamelCase_ = (1, 11, 768) self.assertEqual(output.shape , _lowercase ) UpperCamelCase_ = np.array( [[[-0.6_513, 1.5_035, -0.2_766], [-0.6_515, 1.5_046, -0.2_780], [-0.6_512, 1.5_049, -0.2_784]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , _lowercase , atol=1e-4 ) )
60
1
"""simple docstring""" def __A ( a_ :Dict) -> str: if p < 2: raise ValueError('''p should not be less than 2!''') elif p == 2: return True __a : Optional[Any] = 4 __a : List[str] = (1 << p) - 1 for _ in range(p - 2): __a : List[Any] = ((s * s) - 2) % m return s == 0 if __name__ == "__main__": print(lucas_lehmer_test(7)) print(lucas_lehmer_test(11))
160
'''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 UpperCamelCase__ : """simple docstring""" def __init__( self : Optional[Any] , lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : Node | None , ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = pos_x SCREAMING_SNAKE_CASE : Any = pos_y SCREAMING_SNAKE_CASE : Optional[int] = (pos_y, pos_x) SCREAMING_SNAKE_CASE : Tuple = goal_x SCREAMING_SNAKE_CASE : List[str] = goal_y SCREAMING_SNAKE_CASE : Optional[Any] = g_cost SCREAMING_SNAKE_CASE : Tuple = parent SCREAMING_SNAKE_CASE : int = self.calculate_heuristic() SCREAMING_SNAKE_CASE : Tuple = self.g_cost + self.h_cost def lowerCamelCase_ ( self : Optional[Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.pos_x - self.goal_x SCREAMING_SNAKE_CASE : List[str] = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(lowerCamelCase_ ) + abs(lowerCamelCase_ ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self : Optional[Any] , lowerCamelCase_ : Node ): '''simple docstring''' return self.f_cost < other.f_cost class UpperCamelCase__ : """simple docstring""" def __init__( self : int , lowerCamelCase_ : TPosition , lowerCamelCase_ : TPosition ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_99_99 , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = [self.start] SCREAMING_SNAKE_CASE : list[Node] = [] SCREAMING_SNAKE_CASE : str = False def lowerCamelCase_ ( self : Any ): '''simple docstring''' while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() SCREAMING_SNAKE_CASE : Optional[Any] = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(lowerCamelCase_ ) self.closed_nodes.append(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = self.get_successors(lowerCamelCase_ ) 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(lowerCamelCase_ ) else: # retrieve the best current path SCREAMING_SNAKE_CASE : int = self.open_nodes.pop(self.open_nodes.index(lowerCamelCase_ ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(lowerCamelCase_ ) else: self.open_nodes.append(lowerCamelCase_ ) return [self.start.pos] def lowerCamelCase_ ( self : int , lowerCamelCase_ : Node ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = [] for action in delta: SCREAMING_SNAKE_CASE : Dict = parent.pos_x + action[1] SCREAMING_SNAKE_CASE : List[str] = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(lowerCamelCase_ ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( lowerCamelCase_ , lowerCamelCase_ , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , lowerCamelCase_ , ) ) return successors def lowerCamelCase_ ( self : Tuple , lowerCamelCase_ : Node | None ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = node SCREAMING_SNAKE_CASE : List[str] = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) SCREAMING_SNAKE_CASE : Optional[Any] = current_node.parent path.reverse() return path class UpperCamelCase__ : """simple docstring""" def __init__( self : int , lowerCamelCase_ : TPosition , lowerCamelCase_ : TPosition ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = AStar(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = AStar(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = False def lowerCamelCase_ ( self : Tuple ): '''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() SCREAMING_SNAKE_CASE : List[str] = self.fwd_astar.open_nodes.pop(0 ) SCREAMING_SNAKE_CASE : Optional[Any] = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( lowerCamelCase_ , lowerCamelCase_ ) self.fwd_astar.closed_nodes.append(lowerCamelCase_ ) self.bwd_astar.closed_nodes.append(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = current_bwd_node SCREAMING_SNAKE_CASE : Any = current_fwd_node SCREAMING_SNAKE_CASE : Dict = { self.fwd_astar: self.fwd_astar.get_successors(lowerCamelCase_ ), self.bwd_astar: self.bwd_astar.get_successors(lowerCamelCase_ ), } 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(lowerCamelCase_ ) else: # retrieve the best current path SCREAMING_SNAKE_CASE : int = astar.open_nodes.pop( astar.open_nodes.index(lowerCamelCase_ ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(lowerCamelCase_ ) else: astar.open_nodes.append(lowerCamelCase_ ) return [self.fwd_astar.start.pos] def lowerCamelCase_ ( self : str , lowerCamelCase_ : Node , lowerCamelCase_ : Node ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.fwd_astar.retrace_path(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = self.bwd_astar.retrace_path(lowerCamelCase_ ) bwd_path.pop() bwd_path.reverse() SCREAMING_SNAKE_CASE : str = 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''')
323
0
"""simple docstring""" import shutil import tempfile import unittest import numpy as np from transformers.testing_utils import ( is_pt_tf_cross_test, require_tf, require_torch, require_torchvision, require_vision, ) from transformers.utils import is_tf_available, is_torch_available, is_vision_available if is_vision_available(): from PIL import Image from transformers import AutoProcessor, SamImageProcessor, SamProcessor if is_torch_available(): import torch if is_tf_available(): import tensorflow as tf @require_vision @require_torchvision class snake_case ( unittest.TestCase): def a_ ( self : Optional[Any] ) -> Any: '''simple docstring''' _A = tempfile.mkdtemp() _A = SamImageProcessor() _A = SamProcessor(__snake_case ) processor.save_pretrained(self.tmpdirname ) def a_ ( self : List[str] , **a__ : int ) -> List[str]: '''simple docstring''' return AutoProcessor.from_pretrained(self.tmpdirname , **__snake_case ).image_processor def a_ ( self : Any ) -> Dict: '''simple docstring''' shutil.rmtree(self.tmpdirname ) def a_ ( self : List[Any] ) -> Optional[Any]: '''simple docstring''' _A = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] _A = [Image.fromarray(np.moveaxis(__snake_case , 0 , -1 ) ) for x in image_inputs] return image_inputs def a_ ( self : List[Any] ) -> Any: '''simple docstring''' _A = SamProcessor(image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) _A = self.get_image_processor(do_normalize=__snake_case , padding_value=1.0 ) _A = SamProcessor.from_pretrained(self.tmpdirname , do_normalize=__snake_case , padding_value=1.0 ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __snake_case ) def a_ ( self : int ) -> Union[str, Any]: '''simple docstring''' _A = self.get_image_processor() _A = SamProcessor(image_processor=__snake_case ) _A = self.prepare_image_inputs() _A = image_processor(__snake_case , return_tensors="np" ) _A = processor(images=__snake_case , return_tensors="np" ) input_feat_extract.pop("original_sizes" ) # pop original_sizes as it is popped in the processor input_feat_extract.pop("reshaped_input_sizes" ) # pop original_sizes as it is popped in the processor for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1E-2 ) @require_torch def a_ ( self : List[str] ) -> Any: '''simple docstring''' _A = self.get_image_processor() _A = SamProcessor(image_processor=__snake_case ) _A = [torch.ones((1, 3, 5, 5) )] _A = [[17_64, 26_46]] _A = [[6_83, 10_24]] _A = processor.post_process_masks(__snake_case , __snake_case , __snake_case ) self.assertEqual(masks[0].shape , (1, 3, 17_64, 26_46) ) _A = processor.post_process_masks( __snake_case , torch.tensor(__snake_case ) , torch.tensor(__snake_case ) ) self.assertEqual(masks[0].shape , (1, 3, 17_64, 26_46) ) # should also work with np _A = [np.ones((1, 3, 5, 5) )] _A = processor.post_process_masks(__snake_case , np.array(__snake_case ) , np.array(__snake_case ) ) self.assertEqual(masks[0].shape , (1, 3, 17_64, 26_46) ) _A = [[1, 0], [0, 1]] with self.assertRaises(__snake_case ): _A = processor.post_process_masks(__snake_case , np.array(__snake_case ) , np.array(__snake_case ) ) @require_vision @require_tf class snake_case ( unittest.TestCase): def a_ ( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' _A = tempfile.mkdtemp() _A = SamImageProcessor() _A = SamProcessor(__snake_case ) processor.save_pretrained(self.tmpdirname ) def a_ ( self : Optional[Any] , **a__ : Dict ) -> Any: '''simple docstring''' return AutoProcessor.from_pretrained(self.tmpdirname , **__snake_case ).image_processor def a_ ( self : Tuple ) -> int: '''simple docstring''' shutil.rmtree(self.tmpdirname ) def a_ ( self : Any ) -> Union[str, Any]: '''simple docstring''' _A = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] _A = [Image.fromarray(np.moveaxis(__snake_case , 0 , -1 ) ) for x in image_inputs] return image_inputs def a_ ( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' _A = SamProcessor(image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) _A = self.get_image_processor(do_normalize=__snake_case , padding_value=1.0 ) _A = SamProcessor.from_pretrained(self.tmpdirname , do_normalize=__snake_case , padding_value=1.0 ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __snake_case ) def a_ ( self : Any ) -> int: '''simple docstring''' _A = self.get_image_processor() _A = SamProcessor(image_processor=__snake_case ) _A = self.prepare_image_inputs() _A = image_processor(__snake_case , return_tensors="np" ) _A = processor(images=__snake_case , return_tensors="np" ) input_feat_extract.pop("original_sizes" ) # pop original_sizes as it is popped in the processor input_feat_extract.pop("reshaped_input_sizes" ) # pop reshaped_input_sizes as it is popped in the processor for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1E-2 ) @require_tf def a_ ( self : Optional[int] ) -> str: '''simple docstring''' _A = self.get_image_processor() _A = SamProcessor(image_processor=__snake_case ) _A = [tf.ones((1, 3, 5, 5) )] _A = [[17_64, 26_46]] _A = [[6_83, 10_24]] _A = processor.post_process_masks(__snake_case , __snake_case , __snake_case , return_tensors="tf" ) self.assertEqual(masks[0].shape , (1, 3, 17_64, 26_46) ) _A = processor.post_process_masks( __snake_case , tf.convert_to_tensor(__snake_case ) , tf.convert_to_tensor(__snake_case ) , return_tensors="tf" , ) self.assertEqual(masks[0].shape , (1, 3, 17_64, 26_46) ) # should also work with np _A = [np.ones((1, 3, 5, 5) )] _A = processor.post_process_masks( __snake_case , np.array(__snake_case ) , np.array(__snake_case ) , return_tensors="tf" ) self.assertEqual(masks[0].shape , (1, 3, 17_64, 26_46) ) _A = [[1, 0], [0, 1]] with self.assertRaises(tf.errors.InvalidArgumentError ): _A = processor.post_process_masks( __snake_case , np.array(__snake_case ) , np.array(__snake_case ) , return_tensors="tf" ) @require_vision @require_torchvision class snake_case ( unittest.TestCase): def a_ ( self : str ) -> List[Any]: '''simple docstring''' _A = tempfile.mkdtemp() _A = SamImageProcessor() _A = SamProcessor(__snake_case ) processor.save_pretrained(self.tmpdirname ) def a_ ( self : Dict , **a__ : Tuple ) -> Optional[int]: '''simple docstring''' return AutoProcessor.from_pretrained(self.tmpdirname , **__snake_case ).image_processor def a_ ( self : str ) -> Any: '''simple docstring''' shutil.rmtree(self.tmpdirname ) def a_ ( self : Dict ) -> Dict: '''simple docstring''' _A = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] _A = [Image.fromarray(np.moveaxis(__snake_case , 0 , -1 ) ) for x in image_inputs] return image_inputs @is_pt_tf_cross_test def a_ ( self : int ) -> Union[str, Any]: '''simple docstring''' _A = self.get_image_processor() _A = SamProcessor(image_processor=__snake_case ) _A = np.random.randint(0 , 2 , size=(1, 3, 5, 5) ).astype(np.floataa ) _A = [tf.convert_to_tensor(__snake_case )] _A = [torch.tensor(__snake_case )] _A = [[17_64, 26_46]] _A = [[6_83, 10_24]] _A = processor.post_process_masks( __snake_case , __snake_case , __snake_case , return_tensors="tf" ) _A = processor.post_process_masks( __snake_case , __snake_case , __snake_case , return_tensors="pt" ) self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy() ) ) @is_pt_tf_cross_test def a_ ( self : Union[str, Any] ) -> str: '''simple docstring''' _A = self.get_image_processor() _A = SamProcessor(image_processor=__snake_case ) _A = self.prepare_image_inputs() _A = image_processor(__snake_case , return_tensors="pt" )["pixel_values"].numpy() _A = processor(images=__snake_case , return_tensors="pt" )["pixel_values"].numpy() _A = image_processor(__snake_case , return_tensors="tf" )["pixel_values"].numpy() _A = processor(images=__snake_case , return_tensors="tf" )["pixel_values"].numpy() self.assertTrue(np.allclose(__snake_case , __snake_case ) ) self.assertTrue(np.allclose(__snake_case , __snake_case ) ) self.assertTrue(np.allclose(__snake_case , __snake_case ) )
353
"""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 snake_case : def __init__( self : Optional[int] , a__ : Tuple , a__ : str=1_00 , a__ : Dict=13 , a__ : Tuple=30 , a__ : str=2 , a__ : List[Any]=3 , a__ : Dict=True , a__ : Optional[Any]=True , a__ : List[Any]=32 , a__ : Tuple=4 , a__ : Tuple=4 , a__ : Optional[int]=37 , a__ : Tuple="gelu" , a__ : Optional[int]=0.1 , a__ : int=0.1 , a__ : Optional[Any]=10 , a__ : Optional[int]=0.0_2 , a__ : Dict=3 , a__ : str=None , a__ : Any=[0, 1, 2, 3] , ) -> Tuple: '''simple docstring''' _A = parent _A = 1_00 _A = batch_size _A = image_size _A = patch_size _A = num_channels _A = is_training _A = use_labels _A = hidden_size _A = num_hidden_layers _A = num_attention_heads _A = intermediate_size _A = hidden_act _A = hidden_dropout_prob _A = attention_probs_dropout_prob _A = type_sequence_label_size _A = initializer_range _A = scope _A = out_indices _A = num_labels # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) _A = (image_size // patch_size) ** 2 _A = num_patches + 1 def a_ ( self : List[str] ) -> str: '''simple docstring''' _A = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _A = None _A = None if self.use_labels: _A = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _A = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) _A = self.get_config() return config, pixel_values, labels, pixel_labels def a_ ( self : Tuple ) -> 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=a__ , initializer_range=self.initializer_range , out_indices=self.out_indices , ) def a_ ( self : Any , a__ : List[str] , a__ : Tuple , a__ : List[str] , a__ : str ) -> Any: '''simple docstring''' _A = BeitModel(config=a__ ) model.to(a__ ) model.eval() _A = model(a__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a_ ( self : List[str] , a__ : Optional[Any] , a__ : Tuple , a__ : Any , a__ : Optional[Any] ) -> Tuple: '''simple docstring''' _A = BeitForMaskedImageModeling(config=a__ ) model.to(a__ ) model.eval() _A = model(a__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def a_ ( self : Optional[Any] , a__ : Optional[int] , a__ : Optional[Any] , a__ : List[str] , a__ : Dict ) -> Dict: '''simple docstring''' _A = self.type_sequence_label_size _A = BeitForImageClassification(a__ ) model.to(a__ ) model.eval() _A = model(a__ , labels=a__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images _A = 1 _A = BeitForImageClassification(a__ ) model.to(a__ ) model.eval() _A = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) _A = model(a__ , labels=a__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def a_ ( self : Optional[Any] , a__ : Optional[Any] , a__ : Union[str, Any] , a__ : Union[str, Any] , a__ : Dict ) -> str: '''simple docstring''' _A = self.num_labels _A = BeitForSemanticSegmentation(a__ ) model.to(a__ ) model.eval() _A = model(a__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) _A = model(a__ , labels=a__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) def a_ ( self : List[Any] ) -> Any: '''simple docstring''' _A = self.prepare_config_and_inputs() _A , _A , _A , _A = config_and_inputs _A = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class snake_case ( _UpperCamelCase , _UpperCamelCase , unittest.TestCase): __UpperCamelCase = ( (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation) if is_torch_available() else () ) __UpperCamelCase = ( { 'feature-extraction': BeitModel, 'image-classification': BeitForImageClassification, 'image-segmentation': BeitForSemanticSegmentation, } if is_torch_available() else {} ) __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False def a_ ( self : List[Any] ) -> List[Any]: '''simple docstring''' _A = BeitModelTester(self ) _A = ConfigTester(self , config_class=a__ , has_text_modality=a__ , hidden_size=37 ) def a_ ( self : Any ) -> Union[str, Any]: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="BEiT does not use inputs_embeds" ) def a_ ( self : Optional[Any] ) -> Tuple: '''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 a_ ( self : Any ) -> int: '''simple docstring''' pass def a_ ( self : Optional[int] ) -> Dict: '''simple docstring''' _A , _A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _A = model_class(a__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) _A = model.get_output_embeddings() self.assertTrue(x is None or isinstance(a__ , nn.Linear ) ) def a_ ( self : Tuple ) -> List[Any]: '''simple docstring''' _A , _A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _A = model_class(a__ ) _A = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _A = [*signature.parameters.keys()] _A = ["pixel_values"] self.assertListEqual(arg_names[:1] , a__ ) def a_ ( self : Dict ) -> Any: '''simple docstring''' _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*a__ ) def a_ ( self : Dict ) -> Optional[int]: '''simple docstring''' _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*a__ ) def a_ ( self : int ) -> List[Any]: '''simple docstring''' _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*a__ ) def a_ ( self : Optional[int] ) -> Tuple: '''simple docstring''' _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*a__ ) def a_ ( self : Dict ) -> Optional[Any]: '''simple docstring''' if not self.model_tester.is_training: return _A , _A = self.model_tester.prepare_config_and_inputs_for_common() _A = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if model_class in [*get_values(a__ ), BeitForMaskedImageModeling]: continue _A = model_class(a__ ) model.to(a__ ) model.train() _A = self._prepare_for_class(a__ , a__ , return_labels=a__ ) _A = model(**a__ ).loss loss.backward() def a_ ( self : List[str] ) -> Dict: '''simple docstring''' _A , _A = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return _A = False _A = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if ( model_class in [*get_values(a__ ), BeitForMaskedImageModeling] or not model_class.supports_gradient_checkpointing ): continue _A = model_class(a__ ) model.gradient_checkpointing_enable() model.to(a__ ) model.train() _A = self._prepare_for_class(a__ , a__ , return_labels=a__ ) _A = model(**a__ ).loss loss.backward() def a_ ( self : Dict ) -> Optional[int]: '''simple docstring''' _A , _A = self.model_tester.prepare_config_and_inputs_for_common() _A = _config_zero_init(a__ ) for model_class in self.all_model_classes: _A = model_class(config=a__ ) 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 a_ ( self : List[str] ) -> int: '''simple docstring''' for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _A = BeitModel.from_pretrained(a__ ) self.assertIsNotNone(a__ ) def a__ ( ) -> Tuple: _A = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class snake_case ( unittest.TestCase): @cached_property def a_ ( self : Optional[int] ) -> List[str]: '''simple docstring''' return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224" ) if is_vision_available() else None @slow def a_ ( self : Union[str, Any] ) -> Tuple: '''simple docstring''' _A = BeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k" ).to(a__ ) _A = self.default_image_processor _A = prepare_img() _A = image_processor(images=a__ , return_tensors="pt" ).pixel_values.to(a__ ) # prepare bool_masked_pos _A = torch.ones((1, 1_96) , dtype=torch.bool ).to(a__ ) # forward pass with torch.no_grad(): _A = model(pixel_values=a__ , bool_masked_pos=a__ ) _A = outputs.logits # verify the logits _A = torch.Size((1, 1_96, 81_92) ) self.assertEqual(logits.shape , a__ ) _A = torch.tensor( [[-3.2_4_3_7, 0.5_0_7_2, -1_3.9_1_7_4], [-3.2_4_5_6, 0.4_9_4_8, -1_3.9_4_0_1], [-3.2_0_3_3, 0.5_1_2_1, -1_3.8_5_5_0]] ).to(a__ ) self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] , a__ , atol=1E-2 ) ) @slow def a_ ( self : List[Any] ) -> Optional[int]: '''simple docstring''' _A = BeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224" ).to(a__ ) _A = self.default_image_processor _A = prepare_img() _A = image_processor(images=a__ , return_tensors="pt" ).to(a__ ) # forward pass with torch.no_grad(): _A = model(**a__ ) _A = outputs.logits # verify the logits _A = torch.Size((1, 10_00) ) self.assertEqual(logits.shape , a__ ) _A = torch.tensor([-1.2_3_8_5, -1.0_9_8_7, -1.0_1_0_8] ).to(a__ ) self.assertTrue(torch.allclose(logits[0, :3] , a__ , atol=1E-4 ) ) _A = 2_81 self.assertEqual(logits.argmax(-1 ).item() , a__ ) @slow def a_ ( self : List[Any] ) -> int: '''simple docstring''' _A = BeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k" ).to( a__ ) _A = self.default_image_processor _A = prepare_img() _A = image_processor(images=a__ , return_tensors="pt" ).to(a__ ) # forward pass with torch.no_grad(): _A = model(**a__ ) _A = outputs.logits # verify the logits _A = torch.Size((1, 2_18_41) ) self.assertEqual(logits.shape , a__ ) _A = torch.tensor([1.6_8_8_1, -0.2_7_8_7, 0.5_9_0_1] ).to(a__ ) self.assertTrue(torch.allclose(logits[0, :3] , a__ , atol=1E-4 ) ) _A = 23_96 self.assertEqual(logits.argmax(-1 ).item() , a__ ) @slow def a_ ( self : Tuple ) -> Optional[Any]: '''simple docstring''' _A = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) _A = model.to(a__ ) _A = BeitImageProcessor(do_resize=a__ , size=6_40 , do_center_crop=a__ ) _A = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) _A = Image.open(ds[0]["file"] ) _A = image_processor(images=a__ , return_tensors="pt" ).to(a__ ) # forward pass with torch.no_grad(): _A = model(**a__ ) _A = outputs.logits # verify the logits _A = torch.Size((1, 1_50, 1_60, 1_60) ) self.assertEqual(logits.shape , a__ ) _A = version.parse(PIL.__version__ ) < version.parse("9.0.0" ) if is_pillow_less_than_a: _A = torch.tensor( [ [[-4.9_2_2_5, -2.3_9_5_4, -3.0_5_2_2], [-2.8_8_2_2, -1.0_0_4_6, -1.7_5_6_1], [-2.9_5_4_9, -1.3_2_2_8, -2.1_3_4_7]], [[-5.8_1_6_8, -3.4_1_2_9, -4.0_7_7_8], [-3.8_6_5_1, -2.2_2_1_4, -3.0_2_7_7], [-3.8_3_5_6, -2.4_6_4_3, -3.3_5_3_5]], [[-0.0_0_7_8, 3.9_9_5_2, 4.0_7_5_4], [2.9_8_5_6, 4.6_9_4_4, 5.0_0_3_5], [3.2_4_1_3, 4.7_8_1_3, 4.9_9_6_9]], ] , device=a__ , ) else: _A = torch.tensor( [ [[-4.8_9_6_0, -2.3_6_8_8, -3.0_3_5_5], [-2.8_4_7_8, -0.9_8_3_6, -1.7_4_1_8], [-2.9_4_4_9, -1.3_3_3_2, -2.1_4_5_6]], [[-5.8_0_8_1, -3.4_1_2_4, -4.1_0_0_6], [-3.8_5_6_1, -2.2_0_8_1, -3.0_3_2_3], [-3.8_3_6_5, -2.4_6_0_1, -3.3_6_6_9]], [[-0.0_3_0_9, 3.9_8_6_8, 4.0_5_4_0], [2.9_6_4_0, 4.6_8_7_7, 4.9_9_7_6], [3.2_0_8_1, 4.7_6_9_0, 4.9_9_4_2]], ] , device=a__ , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , a__ , atol=1E-4 ) ) @slow def a_ ( self : Union[str, Any] ) -> List[Any]: '''simple docstring''' _A = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) _A = model.to(a__ ) _A = BeitImageProcessor(do_resize=a__ , size=6_40 , do_center_crop=a__ ) _A = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) _A = Image.open(ds[0]["file"] ) _A = image_processor(images=a__ , return_tensors="pt" ).to(a__ ) # forward pass with torch.no_grad(): _A = model(**a__ ) _A = outputs.logits.detach().cpu() _A = image_processor.post_process_semantic_segmentation(outputs=a__ , target_sizes=[(5_00, 3_00)] ) _A = torch.Size((5_00, 3_00) ) self.assertEqual(segmentation[0].shape , a__ ) _A = image_processor.post_process_semantic_segmentation(outputs=a__ ) _A = torch.Size((1_60, 1_60) ) self.assertEqual(segmentation[0].shape , a__ )
163
0
"""simple docstring""" import unittest from transformers import LiltConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( LiltForQuestionAnswering, LiltForSequenceClassification, LiltForTokenClassification, LiltModel, ) from transformers.models.lilt.modeling_lilt import LILT_PRETRAINED_MODEL_ARCHIVE_LIST class lowerCAmelCase__ : '''simple docstring''' def __init__( self : Any , lowercase_ : List[Any] , lowercase_ : List[str]=13 , lowercase_ : int=7 , lowercase_ : Any=True , lowercase_ : str=True , lowercase_ : List[Any]=True , lowercase_ : List[Any]=True , lowercase_ : Dict=99 , lowercase_ : Union[str, Any]=24 , lowercase_ : int=2 , lowercase_ : List[str]=6 , lowercase_ : Any=37 , lowercase_ : Dict="gelu" , lowercase_ : List[str]=0.1 , lowercase_ : Dict=0.1 , lowercase_ : Union[str, Any]=512 , lowercase_ : List[str]=16 , lowercase_ : Any=2 , lowercase_ : Any=0.02 , lowercase_ : List[Any]=3 , lowercase_ : Optional[int]=None , lowercase_ : str=1000 , ): '''simple docstring''' SCREAMING_SNAKE_CASE_ : List[str] = parent SCREAMING_SNAKE_CASE_ : Optional[Any] = batch_size SCREAMING_SNAKE_CASE_ : Optional[Any] = seq_length SCREAMING_SNAKE_CASE_ : List[Any] = is_training SCREAMING_SNAKE_CASE_ : Optional[int] = use_input_mask SCREAMING_SNAKE_CASE_ : Optional[Any] = use_token_type_ids SCREAMING_SNAKE_CASE_ : int = use_labels SCREAMING_SNAKE_CASE_ : List[Any] = vocab_size SCREAMING_SNAKE_CASE_ : List[str] = hidden_size SCREAMING_SNAKE_CASE_ : List[Any] = num_hidden_layers SCREAMING_SNAKE_CASE_ : List[str] = num_attention_heads SCREAMING_SNAKE_CASE_ : Tuple = intermediate_size SCREAMING_SNAKE_CASE_ : Tuple = hidden_act SCREAMING_SNAKE_CASE_ : int = hidden_dropout_prob SCREAMING_SNAKE_CASE_ : str = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : List[Any] = max_position_embeddings SCREAMING_SNAKE_CASE_ : Union[str, Any] = type_vocab_size SCREAMING_SNAKE_CASE_ : List[str] = type_sequence_label_size SCREAMING_SNAKE_CASE_ : Any = initializer_range SCREAMING_SNAKE_CASE_ : Optional[Any] = num_labels SCREAMING_SNAKE_CASE_ : Tuple = scope SCREAMING_SNAKE_CASE_ : Optional[int] = range_bbox def _SCREAMING_SNAKE_CASE ( self : List[str]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) SCREAMING_SNAKE_CASE_ : Dict = ids_tensor([self.batch_size, self.seq_length, 4] , self.range_bbox) # Ensure that bbox is legal for i in range(bbox.shape[0]): for j in range(bbox.shape[1]): if bbox[i, j, 3] < bbox[i, j, 1]: SCREAMING_SNAKE_CASE_ : Optional[int] = bbox[i, j, 3] SCREAMING_SNAKE_CASE_ : Optional[int] = bbox[i, j, 1] SCREAMING_SNAKE_CASE_ : str = t if bbox[i, j, 2] < bbox[i, j, 0]: SCREAMING_SNAKE_CASE_ : List[str] = bbox[i, j, 2] SCREAMING_SNAKE_CASE_ : Optional[int] = bbox[i, j, 0] SCREAMING_SNAKE_CASE_ : List[str] = t SCREAMING_SNAKE_CASE_ : Tuple = None if self.use_input_mask: SCREAMING_SNAKE_CASE_ : List[Any] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2) SCREAMING_SNAKE_CASE_ : Union[str, Any] = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE_ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size) SCREAMING_SNAKE_CASE_ : List[str] = None SCREAMING_SNAKE_CASE_ : List[str] = None if self.use_labels: SCREAMING_SNAKE_CASE_ : int = ids_tensor([self.batch_size] , self.type_sequence_label_size) SCREAMING_SNAKE_CASE_ : Dict = ids_tensor([self.batch_size, self.seq_length] , self.num_labels) SCREAMING_SNAKE_CASE_ : Any = self.get_config() return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels def _SCREAMING_SNAKE_CASE ( self : Optional[int]): '''simple docstring''' return LiltConfig( vocab_size=self.vocab_size , 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 , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowercase_ : Optional[Any] , lowercase_ : Optional[Any] , lowercase_ : str , lowercase_ : Optional[Any] , lowercase_ : int , lowercase_ : Optional[Any] , lowercase_ : int , ): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Tuple = LiltModel(config=lowercase_) model.to(lowercase_) model.eval() SCREAMING_SNAKE_CASE_ : List[Any] = model(lowercase_ , bbox=lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_) SCREAMING_SNAKE_CASE_ : List[Any] = model(lowercase_ , bbox=lowercase_ , token_type_ids=lowercase_) SCREAMING_SNAKE_CASE_ : int = model(lowercase_ , bbox=lowercase_) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size)) def _SCREAMING_SNAKE_CASE ( self : int , lowercase_ : Union[str, Any] , lowercase_ : Union[str, Any] , lowercase_ : Union[str, Any] , lowercase_ : Optional[Any] , lowercase_ : Union[str, Any] , lowercase_ : List[Any] , lowercase_ : int , ): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Optional[Any] = self.num_labels SCREAMING_SNAKE_CASE_ : Optional[Any] = LiltForTokenClassification(config=lowercase_) model.to(lowercase_) model.eval() SCREAMING_SNAKE_CASE_ : Tuple = model( lowercase_ , bbox=lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ , labels=lowercase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels)) def _SCREAMING_SNAKE_CASE ( self : Tuple , lowercase_ : str , lowercase_ : Union[str, Any] , lowercase_ : Any , lowercase_ : Dict , lowercase_ : List[str] , lowercase_ : List[Any] , lowercase_ : str , ): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Union[str, Any] = LiltForQuestionAnswering(config=lowercase_) model.to(lowercase_) model.eval() SCREAMING_SNAKE_CASE_ : Optional[int] = model( lowercase_ , bbox=lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ , start_positions=lowercase_ , end_positions=lowercase_ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length)) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length)) def _SCREAMING_SNAKE_CASE ( self : Tuple): '''simple docstring''' SCREAMING_SNAKE_CASE_ : str = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ) : List[str] = config_and_inputs SCREAMING_SNAKE_CASE_ : str = { '''input_ids''': input_ids, '''bbox''': bbox, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask, } return config, inputs_dict @require_torch class lowerCAmelCase__ ( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = ( ( LiltModel, LiltForSequenceClassification, LiltForTokenClassification, LiltForQuestionAnswering, ) if is_torch_available() else () ) __UpperCamelCase = ( { "feature-extraction": LiltModel, "question-answering": LiltForQuestionAnswering, "text-classification": LiltForSequenceClassification, "token-classification": LiltForTokenClassification, "zero-shot": LiltForSequenceClassification, } if is_torch_available() else {} ) __UpperCamelCase = False __UpperCamelCase = False def _SCREAMING_SNAKE_CASE ( self : Any , lowercase_ : List[str] , lowercase_ : Optional[int] , lowercase_ : Optional[int] , lowercase_ : str , lowercase_ : str): '''simple docstring''' return True def _SCREAMING_SNAKE_CASE ( self : Tuple): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Dict = LiltModelTester(self) SCREAMING_SNAKE_CASE_ : Optional[int] = ConfigTester(self , config_class=lowercase_ , hidden_size=37) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): '''simple docstring''' self.config_tester.run_common_tests() def _SCREAMING_SNAKE_CASE ( self : List[str]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowercase_) def _SCREAMING_SNAKE_CASE ( self : Tuple): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Tuple = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE_ : Dict = type self.model_tester.create_and_check_model(*lowercase_) def _SCREAMING_SNAKE_CASE ( self : str): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*lowercase_) def _SCREAMING_SNAKE_CASE ( self : Tuple): '''simple docstring''' SCREAMING_SNAKE_CASE_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*lowercase_) @slow def _SCREAMING_SNAKE_CASE ( self : Optional[int]): '''simple docstring''' for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ : Optional[int] = LiltModel.from_pretrained(lowercase_) self.assertIsNotNone(lowercase_) @require_torch @slow class lowerCAmelCase__ ( unittest.TestCase ): '''simple docstring''' def _SCREAMING_SNAKE_CASE ( self : Optional[int]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : List[str] = LiltModel.from_pretrained('''SCUT-DLVCLab/lilt-roberta-en-base''').to(lowercase_) SCREAMING_SNAKE_CASE_ : str = torch.tensor([[1, 2]] , device=lowercase_) SCREAMING_SNAKE_CASE_ : Optional[int] = torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]] , device=lowercase_) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ : Dict = model(input_ids=lowercase_ , bbox=lowercase_) SCREAMING_SNAKE_CASE_ : str = torch.Size([1, 2, 768]) SCREAMING_SNAKE_CASE_ : Dict = torch.tensor( [[-0.06_53, 0.09_50, -0.00_61], [-0.05_45, 0.09_26, -0.03_24]] , device=lowercase_ , ) self.assertTrue(outputs.last_hidden_state.shape , lowercase_) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :, :3] , lowercase_ , atol=1e-3))
91
"""simple docstring""" import tempfile import torch from diffusers import PNDMScheduler from .test_schedulers import SchedulerCommonTest class lowerCAmelCase__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = (PNDMScheduler,) __UpperCamelCase = (("num_inference_steps", 5_0),) def _SCREAMING_SNAKE_CASE ( self : Any , **lowercase_ : Optional[Any]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Any = { '''num_train_timesteps''': 1000, '''beta_start''': 0.00_01, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', } config.update(**lowercase_) return config def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowercase_ : List[str]=0 , **lowercase_ : Union[str, Any]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : List[Any] = dict(self.forward_default_kwargs) SCREAMING_SNAKE_CASE_ : List[str] = kwargs.pop('''num_inference_steps''' , lowercase_) SCREAMING_SNAKE_CASE_ : Tuple = self.dummy_sample SCREAMING_SNAKE_CASE_ : List[Any] = 0.1 * sample SCREAMING_SNAKE_CASE_ : Dict = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: SCREAMING_SNAKE_CASE_ : Tuple = self.get_scheduler_config(**lowercase_) SCREAMING_SNAKE_CASE_ : Optional[Any] = scheduler_class(**lowercase_) scheduler.set_timesteps(lowercase_) # copy over dummy past residuals SCREAMING_SNAKE_CASE_ : Dict = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowercase_) SCREAMING_SNAKE_CASE_ : Union[str, Any] = scheduler_class.from_pretrained(lowercase_) new_scheduler.set_timesteps(lowercase_) # copy over dummy past residuals SCREAMING_SNAKE_CASE_ : Optional[Any] = dummy_past_residuals[:] SCREAMING_SNAKE_CASE_ : Optional[Any] = scheduler.step_prk(lowercase_ , lowercase_ , lowercase_ , **lowercase_).prev_sample SCREAMING_SNAKE_CASE_ : Optional[Any] = new_scheduler.step_prk(lowercase_ , lowercase_ , lowercase_ , **lowercase_).prev_sample assert torch.sum(torch.abs(output - new_output)) < 1e-5, "Scheduler outputs are not identical" SCREAMING_SNAKE_CASE_ : List[str] = scheduler.step_plms(lowercase_ , lowercase_ , lowercase_ , **lowercase_).prev_sample SCREAMING_SNAKE_CASE_ : Dict = new_scheduler.step_plms(lowercase_ , lowercase_ , lowercase_ , **lowercase_).prev_sample assert torch.sum(torch.abs(output - new_output)) < 1e-5, "Scheduler outputs are not identical" def _SCREAMING_SNAKE_CASE ( self : Any): '''simple docstring''' pass def _SCREAMING_SNAKE_CASE ( self : List[Any] , lowercase_ : List[str]=0 , **lowercase_ : List[str]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Optional[int] = dict(self.forward_default_kwargs) SCREAMING_SNAKE_CASE_ : Optional[Any] = kwargs.pop('''num_inference_steps''' , lowercase_) SCREAMING_SNAKE_CASE_ : List[str] = self.dummy_sample SCREAMING_SNAKE_CASE_ : Optional[Any] = 0.1 * sample SCREAMING_SNAKE_CASE_ : int = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: SCREAMING_SNAKE_CASE_ : Dict = self.get_scheduler_config() SCREAMING_SNAKE_CASE_ : Union[str, Any] = scheduler_class(**lowercase_) scheduler.set_timesteps(lowercase_) # copy over dummy past residuals (must be after setting timesteps) SCREAMING_SNAKE_CASE_ : Dict = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowercase_) SCREAMING_SNAKE_CASE_ : str = scheduler_class.from_pretrained(lowercase_) # copy over dummy past residuals new_scheduler.set_timesteps(lowercase_) # copy over dummy past residual (must be after setting timesteps) SCREAMING_SNAKE_CASE_ : Any = dummy_past_residuals[:] SCREAMING_SNAKE_CASE_ : Optional[int] = scheduler.step_prk(lowercase_ , lowercase_ , lowercase_ , **lowercase_).prev_sample SCREAMING_SNAKE_CASE_ : Optional[Any] = new_scheduler.step_prk(lowercase_ , lowercase_ , lowercase_ , **lowercase_).prev_sample assert torch.sum(torch.abs(output - new_output)) < 1e-5, "Scheduler outputs are not identical" SCREAMING_SNAKE_CASE_ : List[str] = scheduler.step_plms(lowercase_ , lowercase_ , lowercase_ , **lowercase_).prev_sample SCREAMING_SNAKE_CASE_ : Tuple = new_scheduler.step_plms(lowercase_ , lowercase_ , lowercase_ , **lowercase_).prev_sample assert torch.sum(torch.abs(output - new_output)) < 1e-5, "Scheduler outputs are not identical" def _SCREAMING_SNAKE_CASE ( self : str , **lowercase_ : str): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Tuple = self.scheduler_classes[0] SCREAMING_SNAKE_CASE_ : Optional[int] = self.get_scheduler_config(**lowercase_) SCREAMING_SNAKE_CASE_ : List[Any] = scheduler_class(**lowercase_) SCREAMING_SNAKE_CASE_ : Dict = 10 SCREAMING_SNAKE_CASE_ : List[Any] = self.dummy_model() SCREAMING_SNAKE_CASE_ : str = self.dummy_sample_deter scheduler.set_timesteps(lowercase_) for i, t in enumerate(scheduler.prk_timesteps): SCREAMING_SNAKE_CASE_ : Optional[Any] = model(lowercase_ , lowercase_) SCREAMING_SNAKE_CASE_ : str = scheduler.step_prk(lowercase_ , lowercase_ , lowercase_).prev_sample for i, t in enumerate(scheduler.plms_timesteps): SCREAMING_SNAKE_CASE_ : List[Any] = model(lowercase_ , lowercase_) SCREAMING_SNAKE_CASE_ : List[str] = scheduler.step_plms(lowercase_ , lowercase_ , lowercase_).prev_sample return sample def _SCREAMING_SNAKE_CASE ( self : Dict): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Any = dict(self.forward_default_kwargs) SCREAMING_SNAKE_CASE_ : Dict = kwargs.pop('''num_inference_steps''' , lowercase_) for scheduler_class in self.scheduler_classes: SCREAMING_SNAKE_CASE_ : Tuple = self.get_scheduler_config() SCREAMING_SNAKE_CASE_ : Optional[Any] = scheduler_class(**lowercase_) SCREAMING_SNAKE_CASE_ : Optional[int] = self.dummy_sample SCREAMING_SNAKE_CASE_ : Any = 0.1 * sample if num_inference_steps is not None and hasattr(lowercase_ , '''set_timesteps'''): scheduler.set_timesteps(lowercase_) elif num_inference_steps is not None and not hasattr(lowercase_ , '''set_timesteps'''): SCREAMING_SNAKE_CASE_ : Optional[Any] = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) SCREAMING_SNAKE_CASE_ : str = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] SCREAMING_SNAKE_CASE_ : Optional[int] = dummy_past_residuals[:] SCREAMING_SNAKE_CASE_ : Dict = scheduler.step_prk(lowercase_ , 0 , lowercase_ , **lowercase_).prev_sample SCREAMING_SNAKE_CASE_ : List[Any] = scheduler.step_prk(lowercase_ , 1 , lowercase_ , **lowercase_).prev_sample self.assertEqual(output_a.shape , sample.shape) self.assertEqual(output_a.shape , output_a.shape) SCREAMING_SNAKE_CASE_ : Optional[int] = scheduler.step_plms(lowercase_ , 0 , lowercase_ , **lowercase_).prev_sample SCREAMING_SNAKE_CASE_ : Any = scheduler.step_plms(lowercase_ , 1 , lowercase_ , **lowercase_).prev_sample self.assertEqual(output_a.shape , sample.shape) self.assertEqual(output_a.shape , output_a.shape) def _SCREAMING_SNAKE_CASE ( self : str): '''simple docstring''' for timesteps in [100, 1000]: self.check_over_configs(num_train_timesteps=lowercase_) def _SCREAMING_SNAKE_CASE ( self : Dict): '''simple docstring''' for steps_offset in [0, 1]: self.check_over_configs(steps_offset=lowercase_) SCREAMING_SNAKE_CASE_ : Dict = self.scheduler_classes[0] SCREAMING_SNAKE_CASE_ : List[str] = self.get_scheduler_config(steps_offset=1) SCREAMING_SNAKE_CASE_ : Tuple = scheduler_class(**lowercase_) scheduler.set_timesteps(10) assert torch.equal( scheduler.timesteps , torch.LongTensor( [901, 851, 851, 801, 801, 751, 751, 701, 701, 651, 651, 601, 601, 501, 401, 301, 201, 101, 1]) , ) def _SCREAMING_SNAKE_CASE ( self : Any): '''simple docstring''' for beta_start, beta_end in zip([0.00_01, 0.0_01] , [0.0_02, 0.02]): self.check_over_configs(beta_start=lowercase_ , beta_end=lowercase_) def _SCREAMING_SNAKE_CASE ( self : List[Any]): '''simple docstring''' for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=lowercase_) def _SCREAMING_SNAKE_CASE ( self : Any): '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=lowercase_) def _SCREAMING_SNAKE_CASE ( self : int): '''simple docstring''' for t in [1, 5, 10]: self.check_over_forward(time_step=lowercase_) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): '''simple docstring''' for t, num_inference_steps in zip([1, 5, 10] , [10, 50, 100]): self.check_over_forward(num_inference_steps=lowercase_) def _SCREAMING_SNAKE_CASE ( self : Any): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Union[str, Any] = 27 for scheduler_class in self.scheduler_classes: SCREAMING_SNAKE_CASE_ : List[Any] = self.dummy_sample SCREAMING_SNAKE_CASE_ : str = 0.1 * sample SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.get_scheduler_config() SCREAMING_SNAKE_CASE_ : Optional[int] = scheduler_class(**lowercase_) scheduler.set_timesteps(lowercase_) # before power of 3 fix, would error on first step, so we only need to do two for i, t in enumerate(scheduler.prk_timesteps[:2]): SCREAMING_SNAKE_CASE_ : int = scheduler.step_prk(lowercase_ , lowercase_ , lowercase_).prev_sample def _SCREAMING_SNAKE_CASE ( self : List[Any]): '''simple docstring''' with self.assertRaises(lowercase_): SCREAMING_SNAKE_CASE_ : int = self.scheduler_classes[0] SCREAMING_SNAKE_CASE_ : List[str] = self.get_scheduler_config() SCREAMING_SNAKE_CASE_ : Dict = scheduler_class(**lowercase_) scheduler.step_plms(self.dummy_sample , 1 , self.dummy_sample).prev_sample def _SCREAMING_SNAKE_CASE ( self : Optional[int]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Any = self.full_loop() SCREAMING_SNAKE_CASE_ : List[Any] = torch.sum(torch.abs(lowercase_)) SCREAMING_SNAKE_CASE_ : Union[str, Any] = torch.mean(torch.abs(lowercase_)) assert abs(result_sum.item() - 1_98.13_18) < 1e-2 assert abs(result_mean.item() - 0.25_80) < 1e-3 def _SCREAMING_SNAKE_CASE ( self : Optional[int]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : str = self.full_loop(prediction_type='''v_prediction''') SCREAMING_SNAKE_CASE_ : str = torch.sum(torch.abs(lowercase_)) SCREAMING_SNAKE_CASE_ : Any = torch.mean(torch.abs(lowercase_)) assert abs(result_sum.item() - 67.39_86) < 1e-2 assert abs(result_mean.item() - 0.08_78) < 1e-3 def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Tuple = self.full_loop(set_alpha_to_one=lowercase_ , beta_start=0.01) SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.sum(torch.abs(lowercase_)) SCREAMING_SNAKE_CASE_ : Any = torch.mean(torch.abs(lowercase_)) assert abs(result_sum.item() - 2_30.03_99) < 1e-2 assert abs(result_mean.item() - 0.29_95) < 1e-3 def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.full_loop(set_alpha_to_one=lowercase_ , beta_start=0.01) SCREAMING_SNAKE_CASE_ : int = torch.sum(torch.abs(lowercase_)) SCREAMING_SNAKE_CASE_ : List[str] = torch.mean(torch.abs(lowercase_)) assert abs(result_sum.item() - 1_86.94_82) < 1e-2 assert abs(result_mean.item() - 0.24_34) < 1e-3
91
1
import numpy # List of input, output pairs __snake_case = ( ((5, 2, 3), 15), ((6, 5, 9), 25), ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41), ) __snake_case = (((5_15, 22, 13), 5_55), ((61, 35, 49), 1_50)) __snake_case = [2, 4, 1, 5] __snake_case = len(train_data) __snake_case = 0.009 def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase="train" )-> List[Any]: '''simple docstring''' return calculate_hypothesis_value(__lowerCAmelCase , __lowerCAmelCase ) - output( __lowerCAmelCase , __lowerCAmelCase ) def lowerCAmelCase_ ( __lowerCAmelCase )-> Optional[Any]: '''simple docstring''' UpperCAmelCase : Optional[int] =0 for i in range(len(__lowerCAmelCase ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> Optional[Any]: '''simple docstring''' if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase )-> Optional[Any]: '''simple docstring''' if data_set == "train": return _hypothesis_value(train_data[example_no][0] ) elif data_set == "test": return _hypothesis_value(test_data[example_no][0] ) return None def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase=m )-> Optional[int]: '''simple docstring''' UpperCAmelCase : Dict =0 for i in range(__lowerCAmelCase ): if index == -1: summation_value += _error(__lowerCAmelCase ) else: summation_value += _error(__lowerCAmelCase ) * train_data[i][0][index] return summation_value def lowerCAmelCase_ ( __lowerCAmelCase )-> List[str]: '''simple docstring''' UpperCAmelCase : Optional[Any] =summation_of_cost_derivative(__lowerCAmelCase , __lowerCAmelCase ) / m return cost_derivative_value def lowerCAmelCase_ ( )-> List[str]: '''simple docstring''' global parameter_vector # Tune these values to set a tolerance value for predicted output UpperCAmelCase : List[str] =0.000002 UpperCAmelCase : int =0 UpperCAmelCase : Dict =0 while True: j += 1 UpperCAmelCase : Optional[int] =[0, 0, 0, 0] for i in range(0 , len(__lowerCAmelCase ) ): UpperCAmelCase : Optional[Any] =get_cost_derivative(i - 1 ) UpperCAmelCase : Dict =( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( __lowerCAmelCase , __lowerCAmelCase , atol=__lowerCAmelCase , rtol=__lowerCAmelCase , ): break UpperCAmelCase : int =temp_parameter_vector print(('''Number of iterations:''', j) ) def lowerCAmelCase_ ( )-> Optional[int]: '''simple docstring''' for i in range(len(__lowerCAmelCase ) ): print(('''Actual output value:''', output(__lowerCAmelCase , '''test''' )) ) print(('''Hypothesis output:''', calculate_hypothesis_value(__lowerCAmelCase , '''test''' )) ) if __name__ == "__main__": run_gradient_descent() print('''\nTesting gradient descent for a linear hypothesis function.\n''') test_gradient_descent()
368
from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) __snake_case = { '''RWKV/rwkv-4-169m-pile''': '''https://huggingface.co/RWKV/rwkv-4-169m-pile/resolve/main/config.json''', '''RWKV/rwkv-4-430m-pile''': '''https://huggingface.co/RWKV/rwkv-4-430m-pile/resolve/main/config.json''', '''RWKV/rwkv-4-1b5-pile''': '''https://huggingface.co/RWKV/rwkv-4-1b5-pile/resolve/main/config.json''', '''RWKV/rwkv-4-3b-pile''': '''https://huggingface.co/RWKV/rwkv-4-3b-pile/resolve/main/config.json''', '''RWKV/rwkv-4-7b-pile''': '''https://huggingface.co/RWKV/rwkv-4-7b-pile/resolve/main/config.json''', '''RWKV/rwkv-4-14b-pile''': '''https://huggingface.co/RWKV/rwkv-4-14b-pile/resolve/main/config.json''', '''RWKV/rwkv-raven-1b5''': '''https://huggingface.co/RWKV/rwkv-raven-1b5/resolve/main/config.json''', '''RWKV/rwkv-raven-3b''': '''https://huggingface.co/RWKV/rwkv-raven-3b/resolve/main/config.json''', '''RWKV/rwkv-raven-7b''': '''https://huggingface.co/RWKV/rwkv-raven-7b/resolve/main/config.json''', '''RWKV/rwkv-raven-14b''': '''https://huggingface.co/RWKV/rwkv-raven-14b/resolve/main/config.json''', } class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : str = """rwkv""" __lowerCamelCase : str = {"""max_position_embeddings""": """context_length"""} def __init__( self , snake_case__=5_0277 , snake_case__=1024 , snake_case__=4096 , snake_case__=32 , snake_case__=None , snake_case__=None , snake_case__=1e-5 , snake_case__=0 , snake_case__=0 , snake_case__=6 , snake_case__=False , snake_case__=True , **snake_case__ , ) -> List[str]: '''simple docstring''' UpperCAmelCase : int =vocab_size UpperCAmelCase : List[str] =context_length UpperCAmelCase : Any =hidden_size UpperCAmelCase : Tuple =num_hidden_layers UpperCAmelCase : str =attention_hidden_size if attention_hidden_size is not None else hidden_size UpperCAmelCase : List[Any] =intermediate_size if intermediate_size is not None else 4 * hidden_size UpperCAmelCase : Optional[int] =layer_norm_epsilon UpperCAmelCase : int =rescale_every UpperCAmelCase : Any =use_cache UpperCAmelCase : List[str] =bos_token_id UpperCAmelCase : Any =eos_token_id super().__init__( tie_word_embeddings=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ , **snake_case__ )
78
0
def A ( a_ ,a_ ) -> list: __UpperCamelCase : Tuple =len(a_ ) __UpperCamelCase : Tuple =[] for i in range(len(a_ ) - pat_len + 1 ): __UpperCamelCase : List[Any] =True for j in range(a_ ): if s[i + j] != pattern[j]: __UpperCamelCase : Union[str, Any] =False break if match_found: position.append(a_ ) return position if __name__ == "__main__": assert naive_pattern_search('''ABCDEFG''', '''DE''') == [3] print(naive_pattern_search('''ABAAABCDBBABCDDEBCABC''', '''ABC'''))
71
from torch import nn class __A ( nn.Module ): """simple docstring""" def __init__( self , lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" super().__init__() __UpperCamelCase : Dict =class_size __UpperCamelCase : Any =embed_size # self.mlp1 = nn.Linear(embed_size, embed_size) # self.mlp2 = (nn.Linear(embed_size, class_size)) __UpperCamelCase : Any =nn.Linear(lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : List[Any] =self.mlp(lowerCamelCase__ ) return logits
71
1
import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger lowerCAmelCase_ = get_logger(__name__) lowerCAmelCase_ = Path(__file__).parent / 'model_card_template.md' lowerCAmelCase_ = uuida().hex lowerCAmelCase_ = os.getenv('HF_HUB_OFFLINE', '').upper() in ENV_VARS_TRUE_VALUES lowerCAmelCase_ = os.getenv('DISABLE_TELEMETRY', '').upper() in ENV_VARS_TRUE_VALUES lowerCAmelCase_ = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '/api/telemetry/' def snake_case( __magic_name__ = None ) -> str: '''simple docstring''' lowercase : List[Any] = F"""diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}""" if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += F"""; torch/{_torch_version}""" if is_flax_available(): ua += F"""; jax/{_jax_version}""" ua += F"""; flax/{_flax_version}""" if is_onnx_available(): ua += F"""; onnxruntime/{_onnxruntime_version}""" # CI will set this value to True if os.environ.get('''DIFFUSERS_IS_CI''' , '''''' ).upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(__magic_name__ , __magic_name__ ): ua += "; " + "; ".join(F"""{k}/{v}""" for k, v in user_agent.items() ) elif isinstance(__magic_name__ , __magic_name__ ): ua += "; " + user_agent return ua def snake_case( __magic_name__ , __magic_name__ = None , __magic_name__ = None ) -> Optional[Any]: '''simple docstring''' if token is None: lowercase : int = HfFolder.get_token() if organization is None: lowercase : List[str] = whoami(__magic_name__ )['''name'''] return F"""{username}/{model_id}""" else: return F"""{organization}/{model_id}""" def snake_case( __magic_name__ , __magic_name__ ) -> Union[str, Any]: '''simple docstring''' if not is_jinja_available(): raise ValueError( '''Modelcard rendering is based on Jinja templates.''' ''' Please make sure to have `jinja` installed before using `create_model_card`.''' ''' To install it, please run `pip install Jinja2`.''' ) if hasattr(__magic_name__ , '''local_rank''' ) and args.local_rank not in [-1, 0]: return lowercase : Optional[Any] = args.hub_token if hasattr(__magic_name__ , '''hub_token''' ) else None lowercase : int = get_full_repo_name(__magic_name__ , token=__magic_name__ ) lowercase : Dict = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language='''en''' , license='''apache-2.0''' , library_name='''diffusers''' , tags=[] , datasets=args.dataset_name , metrics=[] , ) , template_path=__magic_name__ , model_name=__magic_name__ , repo_name=__magic_name__ , dataset_name=args.dataset_name if hasattr(__magic_name__ , '''dataset_name''' ) else None , learning_rate=args.learning_rate , train_batch_size=args.train_batch_size , eval_batch_size=args.eval_batch_size , gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(__magic_name__ , '''gradient_accumulation_steps''' ) else None ) , adam_betaa=args.adam_betaa if hasattr(__magic_name__ , '''adam_beta1''' ) else None , adam_betaa=args.adam_betaa if hasattr(__magic_name__ , '''adam_beta2''' ) else None , adam_weight_decay=args.adam_weight_decay if hasattr(__magic_name__ , '''adam_weight_decay''' ) else None , adam_epsilon=args.adam_epsilon if hasattr(__magic_name__ , '''adam_epsilon''' ) else None , lr_scheduler=args.lr_scheduler if hasattr(__magic_name__ , '''lr_scheduler''' ) else None , lr_warmup_steps=args.lr_warmup_steps if hasattr(__magic_name__ , '''lr_warmup_steps''' ) else None , ema_inv_gamma=args.ema_inv_gamma if hasattr(__magic_name__ , '''ema_inv_gamma''' ) else None , ema_power=args.ema_power if hasattr(__magic_name__ , '''ema_power''' ) else None , ema_max_decay=args.ema_max_decay if hasattr(__magic_name__ , '''ema_max_decay''' ) else None , mixed_precision=args.mixed_precision , ) lowercase : Any = os.path.join(args.output_dir , '''README.md''' ) model_card.save(__magic_name__ ) def snake_case( __magic_name__ , __magic_name__ = None ) -> int: '''simple docstring''' if resolved_file is None or commit_hash is not None: return commit_hash lowercase : Dict = str(Path(__magic_name__ ).as_posix() ) lowercase : Any = re.search(r'''snapshots/([^/]+)/''' , __magic_name__ ) if search is None: return None lowercase : List[Any] = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(__magic_name__ ) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. lowerCAmelCase_ = os.path.expanduser( os.getenv('HF_HOME', os.path.join(os.getenv('XDG_CACHE_HOME', '~/.cache'), 'huggingface')) ) lowerCAmelCase_ = os.path.join(hf_cache_home, 'diffusers') def snake_case( __magic_name__ = None , __magic_name__ = None ) -> None: '''simple docstring''' if new_cache_dir is None: lowercase : str = DIFFUSERS_CACHE if old_cache_dir is None: lowercase : List[str] = old_diffusers_cache lowercase : str = Path(__magic_name__ ).expanduser() lowercase : Dict = Path(__magic_name__ ).expanduser() for old_blob_path in old_cache_dir.glob('''**/blobs/*''' ): if old_blob_path.is_file() and not old_blob_path.is_symlink(): lowercase : List[Any] = new_cache_dir / old_blob_path.relative_to(__magic_name__ ) new_blob_path.parent.mkdir(parents=__magic_name__ , exist_ok=__magic_name__ ) os.replace(__magic_name__ , __magic_name__ ) try: os.symlink(__magic_name__ , __magic_name__ ) except OSError: logger.warning( '''Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.''' ) # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). lowerCAmelCase_ = os.path.join(DIFFUSERS_CACHE, 'version_diffusers_cache.txt') if not os.path.isfile(cache_version_file): lowerCAmelCase_ = 0 else: with open(cache_version_file) as f: try: lowerCAmelCase_ = int(f.read()) except ValueError: lowerCAmelCase_ = 0 if cache_version < 1: lowerCAmelCase_ = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( 'The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ' 'existing cached models. This is a one-time operation, you can interrupt it or run it ' 'later by calling `diffusers.utils.hub_utils.move_cache()`.' ) try: move_cache() except Exception as e: lowerCAmelCase_ = '\n'.join(traceback.format_tb(e.__traceback__)) logger.error( f'''There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease ''' 'file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ' 'message and we will do our best to help.' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, 'w') as f: f.write('1') except Exception: logger.warning( f'''There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure ''' 'the directory exists and can be written to.' ) def snake_case( __magic_name__ , __magic_name__ = None ) -> str: '''simple docstring''' if variant is not None: lowercase : List[str] = weights_name.split('''.''' ) lowercase : Any = splits[:-1] + [variant] + splits[-1:] lowercase : Tuple = '''.'''.join(__magic_name__ ) return weights_name def snake_case( __magic_name__ , *, __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__=None , ) -> Dict: '''simple docstring''' lowercase : Union[str, Any] = str(__magic_name__ ) if os.path.isfile(__magic_name__ ): return pretrained_model_name_or_path elif os.path.isdir(__magic_name__ ): if os.path.isfile(os.path.join(__magic_name__ , __magic_name__ ) ): # Load from a PyTorch checkpoint lowercase : Dict = os.path.join(__magic_name__ , __magic_name__ ) return model_file elif subfolder is not None and os.path.isfile( os.path.join(__magic_name__ , __magic_name__ , __magic_name__ ) ): lowercase : str = os.path.join(__magic_name__ , __magic_name__ , __magic_name__ ) return model_file else: raise EnvironmentError( F"""Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.""" ) else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(__magic_name__ ).base_version ) >= version.parse('''0.20.0''' ) ): try: lowercase : int = hf_hub_download( __magic_name__ , filename=_add_variant(__magic_name__ , __magic_name__ ) , cache_dir=__magic_name__ , force_download=__magic_name__ , proxies=__magic_name__ , resume_download=__magic_name__ , local_files_only=__magic_name__ , use_auth_token=__magic_name__ , user_agent=__magic_name__ , subfolder=__magic_name__ , revision=revision or commit_hash , ) warnings.warn( F"""Loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'` is deprecated. Loading instead from `revision='main'` with `variant={revision}`. Loading model variants via `revision='{revision}'` will be removed in diffusers v1. Please use `variant='{revision}'` instead.""" , __magic_name__ , ) return model_file except: # noqa: E722 warnings.warn( F"""You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant='{revision}'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(__magic_name__ , __magic_name__ )} file in the 'main' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title '{pretrained_model_name_or_path} is missing {_add_variant(__magic_name__ , __magic_name__ )}' so that the correct variant file can be added.""" , __magic_name__ , ) try: # 2. Load model file as usual lowercase : Dict = hf_hub_download( __magic_name__ , filename=__magic_name__ , cache_dir=__magic_name__ , force_download=__magic_name__ , proxies=__magic_name__ , resume_download=__magic_name__ , local_files_only=__magic_name__ , use_auth_token=__magic_name__ , user_agent=__magic_name__ , subfolder=__magic_name__ , revision=revision or commit_hash , ) return model_file except RepositoryNotFoundError: raise EnvironmentError( F"""{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier """ '''listed on \'https://huggingface.co/models\'\nIf this is a private repository, make sure to pass a ''' '''token having permission to this repo with `use_auth_token` or log in with `huggingface-cli ''' '''login`.''' ) except RevisionNotFoundError: raise EnvironmentError( F"""{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for """ '''this model name. Check the model page at ''' F"""'https://huggingface.co/{pretrained_model_name_or_path}' for available revisions.""" ) except EntryNotFoundError: raise EnvironmentError( F"""{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.""" ) except HTTPError as err: raise EnvironmentError( F"""There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}""" ) except ValueError: raise EnvironmentError( F"""We couldn't connect to '{HUGGINGFACE_CO_RESOLVE_ENDPOINT}' to load this model, couldn't find it""" F""" in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a""" F""" directory containing a file named {weights_name} or""" ''' \nCheckout your internet connection or see how to run the library in''' ''' offline mode at \'https://huggingface.co/docs/diffusers/installation#offline-mode\'.''' ) except EnvironmentError: raise EnvironmentError( F"""Can't load the model for '{pretrained_model_name_or_path}'. If you were trying to load it from """ '''\'https://huggingface.co/models\', make sure you don\'t have a local directory with the same name. ''' F"""Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a directory """ F"""containing a file named {weights_name}""" )
116
import os from collections.abc import Iterator def snake_case( __magic_name__ = "." ) -> Iterator[str]: '''simple docstring''' for dir_path, dir_names, filenames in os.walk(__magic_name__ ): lowercase : 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(__magic_name__ )[1] in (".py", ".ipynb"): yield os.path.join(__magic_name__ , __magic_name__ ).lstrip('''./''' ) def snake_case( __magic_name__ ) -> Dict: '''simple docstring''' return F"""{i * ' '}*""" if i else "\n##" def snake_case( __magic_name__ , __magic_name__ ) -> str: '''simple docstring''' lowercase : Dict = old_path.split(os.sep ) for i, new_part in enumerate(new_path.split(os.sep ) ): if (i + 1 > len(__magic_name__ ) or old_parts[i] != new_part) and new_part: print(F"""{md_prefix(__magic_name__ )} {new_part.replace('_' , ' ' ).title()}""" ) return new_path def snake_case( __magic_name__ = "." ) -> None: '''simple docstring''' lowercase : str = '''''' for filepath in sorted(good_file_paths(__magic_name__ ) ): lowercase , lowercase : Optional[int] = os.path.split(__magic_name__ ) if filepath != old_path: lowercase : str = print_path(__magic_name__ , __magic_name__ ) lowercase : Optional[int] = (filepath.count(os.sep ) + 1) if filepath else 0 lowercase : Optional[Any] = F"""{filepath}/{filename}""".replace(''' ''' , '''%20''' ) lowercase : List[str] = os.path.splitext(filename.replace('''_''' , ''' ''' ).title() )[0] print(F"""{md_prefix(__magic_name__ )} [{filename}]({url})""" ) if __name__ == "__main__": print_directory_md('.')
116
1
"""simple docstring""" from typing import List, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging snake_case__ : int = logging.get_logger(__name__) snake_case__ : int = { '''huggingface/time-series-transformer-tourism-monthly''': ( '''https://huggingface.co/huggingface/time-series-transformer-tourism-monthly/resolve/main/config.json''' ), # See all TimeSeriesTransformer models at https://huggingface.co/models?filter=time_series_transformer } class snake_case_( a__ ): __UpperCamelCase = '''time_series_transformer''' __UpperCamelCase = { '''hidden_size''': '''d_model''', '''num_attention_heads''': '''encoder_attention_heads''', '''num_hidden_layers''': '''encoder_layers''', } def __init__( self : Any , UpperCamelCase_ : Optional[int] = None , UpperCamelCase_ : Optional[int] = None , UpperCamelCase_ : str = "student_t" , UpperCamelCase_ : str = "nll" , UpperCamelCase_ : int = 1 , UpperCamelCase_ : List[int] = [1, 2, 3, 4, 5, 6, 7] , UpperCamelCase_ : Optional[Union[str, bool]] = "mean" , UpperCamelCase_ : int = 0 , UpperCamelCase_ : int = 0 , UpperCamelCase_ : int = 0 , UpperCamelCase_ : int = 0 , UpperCamelCase_ : Optional[List[int]] = None , UpperCamelCase_ : Optional[List[int]] = None , UpperCamelCase_ : int = 3_2 , UpperCamelCase_ : int = 3_2 , UpperCamelCase_ : int = 2 , UpperCamelCase_ : int = 2 , UpperCamelCase_ : int = 2 , UpperCamelCase_ : int = 2 , UpperCamelCase_ : bool = True , UpperCamelCase_ : str = "gelu" , UpperCamelCase_ : int = 6_4 , UpperCamelCase_ : float = 0.1 , UpperCamelCase_ : float = 0.1 , UpperCamelCase_ : float = 0.1 , UpperCamelCase_ : float = 0.1 , UpperCamelCase_ : float = 0.1 , UpperCamelCase_ : int = 1_0_0 , UpperCamelCase_ : float = 0.02 , UpperCamelCase_ : int=True , **UpperCamelCase_ : Any , ): # time series specific configuration lowerCAmelCase : Tuple = prediction_length lowerCAmelCase : List[Any] = context_length or prediction_length lowerCAmelCase : str = distribution_output lowerCAmelCase : Union[str, Any] = loss lowerCAmelCase : Optional[Any] = input_size lowerCAmelCase : Tuple = num_time_features lowerCAmelCase : int = lags_sequence lowerCAmelCase : Optional[int] = scaling lowerCAmelCase : Union[str, Any] = num_dynamic_real_features lowerCAmelCase : Dict = num_static_real_features lowerCAmelCase : str = num_static_categorical_features if cardinality and num_static_categorical_features > 0: if len(UpperCamelCase_ ) != num_static_categorical_features: raise ValueError( '''The cardinality should be a list of the same length as `num_static_categorical_features`''' ) lowerCAmelCase : int = cardinality else: lowerCAmelCase : List[Any] = [0] if embedding_dimension and num_static_categorical_features > 0: if len(UpperCamelCase_ ) != num_static_categorical_features: raise ValueError( '''The embedding dimension should be a list of the same length as `num_static_categorical_features`''' ) lowerCAmelCase : Union[str, Any] = embedding_dimension else: lowerCAmelCase : Dict = [min(5_0 , (cat + 1) // 2 ) for cat in self.cardinality] lowerCAmelCase : Union[str, Any] = num_parallel_samples # Transformer architecture configuration lowerCAmelCase : str = input_size * len(UpperCamelCase_ ) + self._number_of_features lowerCAmelCase : Optional[int] = d_model lowerCAmelCase : Dict = encoder_attention_heads lowerCAmelCase : Any = decoder_attention_heads lowerCAmelCase : str = encoder_ffn_dim lowerCAmelCase : List[str] = decoder_ffn_dim lowerCAmelCase : str = encoder_layers lowerCAmelCase : Any = decoder_layers lowerCAmelCase : Optional[int] = dropout lowerCAmelCase : Optional[int] = attention_dropout lowerCAmelCase : int = activation_dropout lowerCAmelCase : str = encoder_layerdrop lowerCAmelCase : Optional[Any] = decoder_layerdrop lowerCAmelCase : List[Any] = activation_function lowerCAmelCase : Optional[int] = init_std lowerCAmelCase : Union[str, Any] = use_cache super().__init__(is_encoder_decoder=UpperCamelCase_ , **UpperCamelCase_ ) @property def lowerCamelCase__ ( self : Optional[int] ): return ( sum(self.embedding_dimension ) + self.num_dynamic_real_features + self.num_time_features + self.num_static_real_features + self.input_size * 2 # the log1p(abs(loc)) and log(scale) features )
60
"""simple docstring""" import unittest import numpy as np from transformers import RobertaPreLayerNormConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roberta_prelayernorm.modeling_flax_roberta_prelayernorm import ( FlaxRobertaPreLayerNormForCausalLM, FlaxRobertaPreLayerNormForMaskedLM, FlaxRobertaPreLayerNormForMultipleChoice, FlaxRobertaPreLayerNormForQuestionAnswering, FlaxRobertaPreLayerNormForSequenceClassification, FlaxRobertaPreLayerNormForTokenClassification, FlaxRobertaPreLayerNormModel, ) class snake_case_( unittest.TestCase ): def __init__( self : List[Any] , UpperCamelCase_ : Union[str, Any] , UpperCamelCase_ : List[Any]=1_3 , UpperCamelCase_ : Tuple=7 , UpperCamelCase_ : List[Any]=True , UpperCamelCase_ : int=True , UpperCamelCase_ : Union[str, Any]=True , UpperCamelCase_ : Optional[Any]=True , UpperCamelCase_ : List[str]=9_9 , UpperCamelCase_ : str=3_2 , UpperCamelCase_ : Union[str, Any]=5 , UpperCamelCase_ : int=4 , UpperCamelCase_ : Optional[Any]=3_7 , UpperCamelCase_ : Optional[int]="gelu" , UpperCamelCase_ : Any=0.1 , UpperCamelCase_ : List[str]=0.1 , UpperCamelCase_ : str=5_1_2 , UpperCamelCase_ : Optional[Any]=1_6 , UpperCamelCase_ : Union[str, Any]=2 , UpperCamelCase_ : Any=0.02 , UpperCamelCase_ : Union[str, Any]=4 , ): lowerCAmelCase : str = parent lowerCAmelCase : List[str] = batch_size lowerCAmelCase : int = seq_length lowerCAmelCase : str = is_training lowerCAmelCase : Tuple = use_attention_mask lowerCAmelCase : Dict = use_token_type_ids lowerCAmelCase : Optional[int] = use_labels lowerCAmelCase : Optional[Any] = vocab_size lowerCAmelCase : Optional[int] = hidden_size lowerCAmelCase : Optional[Any] = num_hidden_layers lowerCAmelCase : str = num_attention_heads lowerCAmelCase : Optional[Any] = intermediate_size lowerCAmelCase : int = hidden_act lowerCAmelCase : int = hidden_dropout_prob lowerCAmelCase : Tuple = attention_probs_dropout_prob lowerCAmelCase : str = max_position_embeddings lowerCAmelCase : str = type_vocab_size lowerCAmelCase : str = type_sequence_label_size lowerCAmelCase : Any = initializer_range lowerCAmelCase : int = num_choices def lowerCamelCase__ ( self : Optional[int] ): lowerCAmelCase : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCAmelCase : Optional[int] = None if self.use_attention_mask: lowerCAmelCase : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) lowerCAmelCase : Union[str, Any] = None if self.use_token_type_ids: lowerCAmelCase : Dict = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCAmelCase : Union[str, Any] = RobertaPreLayerNormConfig( vocab_size=self.vocab_size , 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 , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=UpperCamelCase_ , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def lowerCamelCase__ ( self : int ): lowerCAmelCase : List[str] = self.prepare_config_and_inputs() lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase : Optional[Any] = config_and_inputs lowerCAmelCase : Optional[Any] = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict def lowerCamelCase__ ( self : List[str] ): lowerCAmelCase : int = self.prepare_config_and_inputs() lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase : Tuple = config_and_inputs lowerCAmelCase : str = True lowerCAmelCase : Optional[Any] = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) lowerCAmelCase : str = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax # Copied from tests.models.roberta.test_modelling_flax_roberta.FlaxRobertaPreLayerNormModelTest with ROBERTA->ROBERTA_PRELAYERNORM,Roberta->RobertaPreLayerNorm,roberta-base->andreasmadsen/efficient_mlm_m0.40 class snake_case_( a__ , unittest.TestCase ): __UpperCamelCase = True __UpperCamelCase = ( ( FlaxRobertaPreLayerNormModel, FlaxRobertaPreLayerNormForCausalLM, FlaxRobertaPreLayerNormForMaskedLM, FlaxRobertaPreLayerNormForSequenceClassification, FlaxRobertaPreLayerNormForTokenClassification, FlaxRobertaPreLayerNormForMultipleChoice, FlaxRobertaPreLayerNormForQuestionAnswering, ) if is_flax_available() else () ) def lowerCamelCase__ ( self : List[Any] ): lowerCAmelCase : Any = FlaxRobertaPreLayerNormModelTester(self ) @slow def lowerCamelCase__ ( self : List[str] ): for model_class_name in self.all_model_classes: lowerCAmelCase : Optional[int] = model_class_name.from_pretrained('''andreasmadsen/efficient_mlm_m0.40''' , from_pt=UpperCamelCase_ ) lowerCAmelCase : int = model(np.ones((1, 1) ) ) self.assertIsNotNone(UpperCamelCase_ ) @require_flax class snake_case_( unittest.TestCase ): @slow def lowerCamelCase__ ( self : List[str] ): lowerCAmelCase : str = FlaxRobertaPreLayerNormForMaskedLM.from_pretrained('''andreasmadsen/efficient_mlm_m0.40''' , from_pt=UpperCamelCase_ ) lowerCAmelCase : Any = np.array([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]] , dtype=jnp.intaa ) lowerCAmelCase : Union[str, Any] = model(UpperCamelCase_ )[0] lowerCAmelCase : str = [1, 1_1, 5_0_2_6_5] self.assertEqual(list(output.shape ) , UpperCamelCase_ ) # compare the actual values for a slice. lowerCAmelCase : Optional[Any] = np.array( [[[40.4_880, 18.0_199, -5.2_367], [-1.8_877, -4.0_885, 10.7_085], [-2.2_613, -5.6_110, 7.2_665]]] , dtype=np.floataa ) self.assertTrue(np.allclose(output[:, :3, :3] , UpperCamelCase_ , atol=1E-4 ) ) @slow def lowerCamelCase__ ( self : List[str] ): lowerCAmelCase : Dict = FlaxRobertaPreLayerNormModel.from_pretrained('''andreasmadsen/efficient_mlm_m0.40''' , from_pt=UpperCamelCase_ ) lowerCAmelCase : str = np.array([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]] , dtype=jnp.intaa ) lowerCAmelCase : str = model(UpperCamelCase_ )[0] # compare the actual values for a slice. lowerCAmelCase : str = np.array( [[[0.0_208, -0.0_356, 0.0_237], [-0.1_569, -0.0_411, -0.2_626], [0.1_879, 0.0_125, -0.0_089]]] , dtype=np.floataa ) self.assertTrue(np.allclose(output[:, :3, :3] , UpperCamelCase_ , atol=1E-4 ) )
60
1
from math import isqrt def __UpperCamelCase ( _lowerCAmelCase ) -> list[int]: """simple docstring""" A : Any = [True] * max_number for i in range(2 , isqrt(max_number - 1 ) + 1 ): if is_prime[i]: for j in range(i**2 , _lowerCAmelCase , _lowerCAmelCase ): A : Dict = False return [i for i in range(2 , _lowerCAmelCase ) if is_prime[i]] def __UpperCamelCase ( _lowerCAmelCase = 10**8 ) -> int: """simple docstring""" A : Optional[Any] = calculate_prime_numbers(max_number // 2 ) A : List[str] = 0 A : Optional[Any] = 0 A : Optional[Any] = len(_lowerCAmelCase ) - 1 while left <= right: while prime_numbers[left] * prime_numbers[right] >= max_number: right -= 1 semiprimes_count += right - left + 1 left += 1 return semiprimes_count if __name__ == "__main__": print(F"""{solution() = }""")
360
import tempfile import unittest import numpy as np import transformers from transformers import GPTaTokenizer, GPTJConfig, is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax, tooslow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax import jax.numpy as jnp from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) from transformers.models.gptj.modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel if is_torch_available(): import torch class SCREAMING_SNAKE_CASE__ : '''simple docstring''' def __init__( self, lowerCamelCase__, lowerCamelCase__=14, lowerCamelCase__=7, lowerCamelCase__=True, lowerCamelCase__=True, lowerCamelCase__=False, lowerCamelCase__=True, lowerCamelCase__=99, lowerCamelCase__=32, lowerCamelCase__=4, lowerCamelCase__=4, lowerCamelCase__=4, lowerCamelCase__=37, lowerCamelCase__="gelu", lowerCamelCase__=0.1, lowerCamelCase__=0.1, lowerCamelCase__=512, lowerCamelCase__=0.02, ): A : List[str] = parent A : Any = batch_size A : Dict = seq_length A : Tuple = is_training A : Any = use_input_mask A : Any = use_token_type_ids A : Any = use_labels A : Optional[int] = vocab_size A : Dict = hidden_size A : Dict = rotary_dim A : Dict = num_hidden_layers A : Tuple = num_attention_heads A : Tuple = intermediate_size A : Union[str, Any] = hidden_act A : Dict = hidden_dropout_prob A : List[str] = attention_probs_dropout_prob A : Optional[int] = max_position_embeddings A : str = initializer_range A : Any = None A : Any = vocab_size - 1 A : int = vocab_size - 1 A : int = vocab_size - 1 def _lowerCAmelCase ( self ): A : str = ids_tensor([self.batch_size, self.seq_length], self.vocab_size ) A : Optional[int] = None if self.use_input_mask: A : Any = random_attention_mask([self.batch_size, self.seq_length] ) A : int = GPTJConfig( vocab_size=self.vocab_size, n_embd=self.hidden_size, n_layer=self.num_hidden_layers, n_head=self.num_attention_heads, n_positions=self.max_position_embeddings, use_cache=lowerCamelCase__, bos_token_id=self.bos_token_id, eos_token_id=self.eos_token_id, pad_token_id=self.pad_token_id, rotary_dim=self.rotary_dim, ) return (config, input_ids, input_mask) def _lowerCAmelCase ( self ): A : List[str] = self.prepare_config_and_inputs() A , A , A : List[str] = config_and_inputs A : Optional[int] = {"""input_ids""": input_ids, """attention_mask""": attention_mask} return config, inputs_dict def _lowerCAmelCase ( self, lowerCamelCase__, lowerCamelCase__, lowerCamelCase__, lowerCamelCase__ ): A : Optional[int] = 20 A : Tuple = model_class_name(lowerCamelCase__ ) A : Dict = model.init_cache(input_ids.shape[0], lowerCamelCase__ ) A : int = jnp.ones((input_ids.shape[0], max_decoder_length), dtype="""i4""" ) A : Optional[int] = jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :], (input_ids.shape[0], input_ids.shape[-1] - 1) ) A : List[Any] = model( input_ids[:, :-1], attention_mask=lowerCamelCase__, past_key_values=lowerCamelCase__, position_ids=lowerCamelCase__, ) A : List[Any] = jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]], dtype="""i4""" ) A : Any = model( input_ids[:, -1:], attention_mask=lowerCamelCase__, past_key_values=outputs_cache.past_key_values, position_ids=lowerCamelCase__, ) A : Any = model(lowerCamelCase__ ) A : List[Any] = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3, msg=f'''Max diff is {diff}''' ) def _lowerCAmelCase ( self, lowerCamelCase__, lowerCamelCase__, lowerCamelCase__, lowerCamelCase__ ): A : Any = 20 A : Any = model_class_name(lowerCamelCase__ ) A : Dict = jnp.concatenate( [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]) )], axis=-1, ) A : str = model.init_cache(input_ids.shape[0], lowerCamelCase__ ) A : Any = jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :], (input_ids.shape[0], input_ids.shape[-1] - 1) ) A : Optional[int] = model( input_ids[:, :-1], attention_mask=lowerCamelCase__, past_key_values=lowerCamelCase__, position_ids=lowerCamelCase__, ) A : str = jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]], dtype="""i4""" ) A : List[Any] = model( input_ids[:, -1:], past_key_values=outputs_cache.past_key_values, attention_mask=lowerCamelCase__, position_ids=lowerCamelCase__, ) A : Union[str, Any] = model(lowerCamelCase__, attention_mask=lowerCamelCase__ ) A : str = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3, msg=f'''Max diff is {diff}''' ) @require_flax class SCREAMING_SNAKE_CASE__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Any = (FlaxGPTJModel, FlaxGPTJForCausalLM) if is_flax_available() else () __lowerCamelCase : Optional[int] = (FlaxGPTJForCausalLM,) if is_flax_available() else () def _lowerCAmelCase ( self ): A : List[Any] = FlaxGPTJModelTester(self ) def _lowerCAmelCase ( self ): for model_class_name in self.all_model_classes: A , A , A : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward(lowerCamelCase__, lowerCamelCase__, lowerCamelCase__, lowerCamelCase__ ) def _lowerCAmelCase ( self ): for model_class_name in self.all_model_classes: A , A , A : str = self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward_with_attn_mask( lowerCamelCase__, lowerCamelCase__, lowerCamelCase__, lowerCamelCase__ ) @tooslow def _lowerCAmelCase ( self ): A : int = GPTaTokenizer.from_pretrained("""gpt2""", pad_token="""<|endoftext|>""", padding_side="""left""" ) A : Optional[int] = tokenizer(["""Hello this is a long string""", """Hey"""], return_tensors="""np""", padding=lowerCamelCase__, truncation=lowerCamelCase__ ) A : Dict = FlaxGPTJForCausalLM.from_pretrained("""EleutherAI/gpt-j-6B""" ) A : str = False A : Optional[Any] = model.config.eos_token_id A : Union[str, Any] = jax.jit(model.generate ) A : str = jit_generate( inputs["""input_ids"""], attention_mask=inputs["""attention_mask"""], pad_token_id=tokenizer.pad_token_id ).sequences A : Optional[Any] = tokenizer.batch_decode(lowerCamelCase__, skip_special_tokens=lowerCamelCase__ ) A : Tuple = [ """Hello this is a long string of text.\n\nI'm trying to get the text of the""", """Hey, I'm a little late to the party. I'm going to""", ] self.assertListEqual(lowerCamelCase__, lowerCamelCase__ ) @is_pt_flax_cross_test def _lowerCAmelCase ( self ): A , A : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs A : Any = self._prepare_for_class(lowerCamelCase__, lowerCamelCase__ ) A : Dict = {k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class A : List[Any] = model_class.__name__[4:] # Skip the "Flax" at the beginning A : str = getattr(lowerCamelCase__, lowerCamelCase__ ) A , A : Optional[int] = pt_inputs["""input_ids"""].shape A : List[str] = np.random.randint(0, seq_length - 1, size=(batch_size,) ) for batch_idx, start_index in enumerate(lowerCamelCase__ ): A : List[Any] = 0 A : Tuple = 1 A : Optional[int] = 0 A : str = 1 A : Dict = pt_model_class(lowerCamelCase__ ).eval() A : int = model_class(lowerCamelCase__, dtype=jnp.floataa ) A : int = convert_pytorch_state_dict_to_flax(pt_model.state_dict(), lowerCamelCase__ ) A : Dict = fx_state with torch.no_grad(): A : Optional[int] = pt_model(**lowerCamelCase__ ).to_tuple() A : str = fx_model(**lowerCamelCase__ ).to_tuple() self.assertEqual(len(lowerCamelCase__ ), len(lowerCamelCase__ ), """Output lengths differ between Flax and PyTorch""" ) for fx_output, pt_output in zip(lowerCamelCase__, lowerCamelCase__ ): self.assert_almost_equals(fx_output[:, -1], pt_output[:, -1].numpy(), 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(lowerCamelCase__ ) A : Union[str, Any] = model_class.from_pretrained(lowerCamelCase__, from_pt=lowerCamelCase__ ) A : Any = fx_model_loaded(**lowerCamelCase__ ).to_tuple() self.assertEqual( len(lowerCamelCase__ ), len(lowerCamelCase__ ), """Output lengths differ between Flax and PyTorch""" ) for fx_output_loaded, pt_output in zip(lowerCamelCase__, lowerCamelCase__ ): self.assert_almost_equals(fx_output_loaded[:, -1], pt_output[:, -1].numpy(), 4e-2 ) @is_pt_flax_cross_test def _lowerCAmelCase ( self ): A , A : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs A : int = self._prepare_for_class(lowerCamelCase__, lowerCamelCase__ ) A : List[str] = {k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class A : Dict = model_class.__name__[4:] # Skip the "Flax" at the beginning A : Dict = getattr(lowerCamelCase__, lowerCamelCase__ ) A : int = pt_model_class(lowerCamelCase__ ).eval() A : int = model_class(lowerCamelCase__, dtype=jnp.floataa ) A : List[str] = load_flax_weights_in_pytorch_model(lowerCamelCase__, fx_model.params ) A , A : Optional[int] = pt_inputs["""input_ids"""].shape A : Optional[int] = np.random.randint(0, seq_length - 1, size=(batch_size,) ) for batch_idx, start_index in enumerate(lowerCamelCase__ ): A : Tuple = 0 A : Tuple = 1 A : str = 0 A : int = 1 # make sure weights are tied in PyTorch pt_model.tie_weights() with torch.no_grad(): A : List[str] = pt_model(**lowerCamelCase__ ).to_tuple() A : Optional[int] = fx_model(**lowerCamelCase__ ).to_tuple() self.assertEqual(len(lowerCamelCase__ ), len(lowerCamelCase__ ), """Output lengths differ between Flax and PyTorch""" ) for fx_output, pt_output in zip(lowerCamelCase__, lowerCamelCase__ ): self.assert_almost_equals(fx_output[:, -1], pt_output[:, -1].numpy(), 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(lowerCamelCase__ ) A : str = pt_model_class.from_pretrained(lowerCamelCase__, from_flax=lowerCamelCase__ ) with torch.no_grad(): A : str = pt_model_loaded(**lowerCamelCase__ ).to_tuple() self.assertEqual( len(lowerCamelCase__ ), len(lowerCamelCase__ ), """Output lengths differ between Flax and PyTorch""" ) for fx_output, pt_output in zip(lowerCamelCase__, lowerCamelCase__ ): self.assert_almost_equals(fx_output[:, -1], pt_output[:, -1].numpy(), 4e-2 ) @tooslow def _lowerCAmelCase ( self ): for model_class_name in self.all_model_classes: A : Union[str, Any] = model_class_name.from_pretrained("""EleutherAI/gpt-j-6B""" ) A : List[str] = model(np.ones((1, 1) ) ) self.assertIsNotNone(lowerCamelCase__ )
115
0
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : Dict , __UpperCAmelCase : Any , __UpperCAmelCase : List[str]=13 , __UpperCAmelCase : List[Any]=3 , __UpperCAmelCase : Tuple=224 , __UpperCAmelCase : Dict=30 , __UpperCAmelCase : Any=400 , __UpperCAmelCase : Any=True , __UpperCAmelCase : Any=None , __UpperCAmelCase : str=True , __UpperCAmelCase : Optional[int]=[0.5, 0.5, 0.5] , __UpperCAmelCase : Optional[Any]=[0.5, 0.5, 0.5] , ) ->Optional[int]: """simple docstring""" a = size if size is not None else {'''height''': 18, '''width''': 18} a = parent a = batch_size a = num_channels a = image_size a = min_resolution a = max_resolution a = do_resize a = size a = do_normalize a = image_mean a = image_std def __lowerCAmelCase ( self : Optional[int] ) ->List[Any]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class lowercase_ ( lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = ViTImageProcessor if is_vision_available() else None def __lowerCAmelCase ( self : List[str] ) ->Tuple: """simple docstring""" a = EfficientFormerImageProcessorTester(self ) @property def __lowerCAmelCase ( self : int ) ->Optional[int]: """simple docstring""" return self.image_proc_tester.prepare_image_processor_dict() def __lowerCAmelCase ( self : Dict ) ->Tuple: """simple docstring""" a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__UpperCAmelCase , '''image_mean''' ) ) self.assertTrue(hasattr(__UpperCAmelCase , '''image_std''' ) ) self.assertTrue(hasattr(__UpperCAmelCase , '''do_normalize''' ) ) self.assertTrue(hasattr(__UpperCAmelCase , '''do_resize''' ) ) self.assertTrue(hasattr(__UpperCAmelCase , '''size''' ) ) def __lowerCAmelCase ( self : Tuple ) ->int: """simple docstring""" pass def __lowerCAmelCase ( self : List[str] ) ->Optional[int]: """simple docstring""" a = self.image_processing_class(**self.image_processor_dict ) # create random PIL images a = prepare_image_inputs(self.image_proc_tester , equal_resolution=__UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(__UpperCAmelCase , Image.Image ) # Test not batched input a = image_processor(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ) , ) # Test batched a = image_processor(__UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ) , ) def __lowerCAmelCase ( self : str ) ->str: """simple docstring""" a = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors a = prepare_image_inputs(self.image_proc_tester , equal_resolution=__UpperCAmelCase , numpify=__UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(__UpperCAmelCase , np.ndarray ) # Test not batched input a = image_processor(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ) , ) # Test batched a = image_processor(__UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ) , ) def __lowerCAmelCase ( self : Dict ) ->Any: """simple docstring""" a = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors a = prepare_image_inputs(self.image_proc_tester , equal_resolution=__UpperCAmelCase , torchify=__UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(__UpperCAmelCase , torch.Tensor ) # Test not batched input a = image_processor(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ) , ) # Test batched a = image_processor(__UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ) , )
0
'''simple docstring''' from collections import namedtuple import requests from lxml import html # type: ignore __A =namedtuple('covid_data', 'cases deaths recovered') def _UpperCamelCase ( UpperCamelCase__ = "https://www.worldometers.info/coronavirus/" ): UpperCAmelCase__ : Union[str, Any] = """//div[@class = \"maincounter-number\"]/span/text()""" return covid_data(*html.fromstring(requests.get(UpperCamelCase__ ).content ).xpath(UpperCamelCase__ ) ) __A ='Total COVID-19 cases in the world: {}\nTotal deaths due to COVID-19 in the world: {}\nTotal COVID-19 patients recovered in the world: {}' print(fmt.format(*covid_stats()))
163
0
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Any = logging.get_logger(__name__) lowerCamelCase__ : Union[str, Any] = { 'google/vivit-b-16x2-kinetics400': ( 'https://huggingface.co/google/vivit-b-16x2-kinetics400/resolve/main/config.json' ), # See all Vivit models at https://huggingface.co/models?filter=vivit } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "vivit" def __init__( self : int , _lowerCAmelCase : Union[str, Any]=224 , _lowerCAmelCase : Optional[Any]=32 , _lowerCAmelCase : int=[2, 16, 16] , _lowerCAmelCase : List[str]=3 , _lowerCAmelCase : Optional[int]=768 , _lowerCAmelCase : Any=12 , _lowerCAmelCase : Dict=12 , _lowerCAmelCase : str=3_072 , _lowerCAmelCase : List[Any]="gelu_fast" , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : Any=0.0 , _lowerCAmelCase : Any=0.02 , _lowerCAmelCase : Optional[Any]=1E-06 , _lowerCAmelCase : List[Any]=True , **_lowerCAmelCase : Optional[Any] , ): SCREAMING_SNAKE_CASE_ = hidden_size SCREAMING_SNAKE_CASE_ = num_hidden_layers SCREAMING_SNAKE_CASE_ = num_attention_heads SCREAMING_SNAKE_CASE_ = intermediate_size SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = num_frames SCREAMING_SNAKE_CASE_ = tubelet_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = qkv_bias super().__init__(**_lowerCAmelCase )
369
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Dict = logging.get_logger(__name__) lowerCamelCase__ : Any = { 'facebook/s2t-small-librispeech-asr': ( 'https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/config.json' ), # See all Speech2Text models at https://huggingface.co/models?filter=speech_to_text } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "speech_to_text" lowercase_ = ["past_key_values"] lowercase_ = {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"} def __init__( self : Union[str, Any] , _lowerCAmelCase : Optional[int]=10_000 , _lowerCAmelCase : str=12 , _lowerCAmelCase : Tuple=2_048 , _lowerCAmelCase : str=4 , _lowerCAmelCase : Tuple=6 , _lowerCAmelCase : Optional[int]=2_048 , _lowerCAmelCase : Dict=4 , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : int=0.0 , _lowerCAmelCase : Optional[int]=True , _lowerCAmelCase : Union[str, Any]=True , _lowerCAmelCase : Any="relu" , _lowerCAmelCase : Any=256 , _lowerCAmelCase : List[str]=0.1 , _lowerCAmelCase : Optional[Any]=0.0 , _lowerCAmelCase : Optional[int]=0.0 , _lowerCAmelCase : Tuple=0.02 , _lowerCAmelCase : List[str]=2 , _lowerCAmelCase : Tuple=True , _lowerCAmelCase : List[str]=1 , _lowerCAmelCase : Dict=0 , _lowerCAmelCase : Tuple=2 , _lowerCAmelCase : Optional[int]=6_000 , _lowerCAmelCase : Tuple=1_024 , _lowerCAmelCase : Any=2 , _lowerCAmelCase : str=(5, 5) , _lowerCAmelCase : Optional[int]=1_024 , _lowerCAmelCase : List[Any]=80 , _lowerCAmelCase : List[Any]=1 , **_lowerCAmelCase : List[Any] , ): SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = encoder_ffn_dim SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = encoder_attention_heads SCREAMING_SNAKE_CASE_ = decoder_ffn_dim SCREAMING_SNAKE_CASE_ = decoder_layers SCREAMING_SNAKE_CASE_ = decoder_attention_heads SCREAMING_SNAKE_CASE_ = dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = activation_function SCREAMING_SNAKE_CASE_ = init_std SCREAMING_SNAKE_CASE_ = encoder_layerdrop SCREAMING_SNAKE_CASE_ = decoder_layerdrop SCREAMING_SNAKE_CASE_ = use_cache SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = scale_embedding # scale factor will be sqrt(d_model) if True SCREAMING_SNAKE_CASE_ = max_source_positions SCREAMING_SNAKE_CASE_ = max_target_positions SCREAMING_SNAKE_CASE_ = num_conv_layers SCREAMING_SNAKE_CASE_ = list(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = conv_channels SCREAMING_SNAKE_CASE_ = input_feat_per_channel SCREAMING_SNAKE_CASE_ = input_channels if len(self.conv_kernel_sizes ) != self.num_conv_layers: raise ValueError( 'Configuration for convolutional module is incorrect. ' 'It is required that `len(config.conv_kernel_sizes)` == `config.num_conv_layers` ' F"but is `len(config.conv_kernel_sizes) = {len(self.conv_kernel_sizes )}`, " F"`config.num_conv_layers = {self.num_conv_layers}`." ) super().__init__( pad_token_id=_lowerCAmelCase , bos_token_id=_lowerCAmelCase , eos_token_id=_lowerCAmelCase , is_encoder_decoder=_lowerCAmelCase , decoder_start_token_id=_lowerCAmelCase , **_lowerCAmelCase , )
210
0
"""simple docstring""" def _snake_case ( UpperCamelCase : List[str] = 50000000 ): UpperCAmelCase : List[Any] = set() UpperCAmelCase : str = int((limit - 24) ** (1 / 2) ) UpperCAmelCase : int = set(range(3 , prime_square_limit + 1 , 2 ) ) primes.add(2 ) for p in range(3 , prime_square_limit + 1 , 2 ): if p not in primes: continue primes.difference_update(set(range(p * p , prime_square_limit + 1 , lowercase_ ) ) ) for primea in primes: UpperCAmelCase : Tuple = primea * primea for primea in primes: UpperCAmelCase : int = primea * primea * primea if square + cube >= limit - 16: break for primea in primes: UpperCAmelCase : int = primea * primea * primea * primea UpperCAmelCase : Union[str, Any] = square + cube + tetr if total >= limit: break ret.add(lowercase_ ) return len(lowercase_ ) if __name__ == "__main__": print(f"""{solution() = }""")
109
"""simple docstring""" import requests snake_case_ = """""" # <-- Put your OpenWeatherMap appid here! snake_case_ = """https://api.openweathermap.org/data/2.5/""" def _lowerCAmelCase ( lowercase_ = "Chicago" , lowercase_ = APPID ): return requests.get(URL_BASE + 'weather' , params=locals() ).json() def _lowerCAmelCase ( lowercase_ = "Kolkata, India" , lowercase_ = APPID ): return requests.get(URL_BASE + 'forecast' , params=locals() ).json() def _lowerCAmelCase ( lowercase_ = 5_5.6_8 , lowercase_ = 1_2.5_7 , lowercase_ = APPID ): return requests.get(URL_BASE + 'onecall' , params=locals() ).json() if __name__ == "__main__": from pprint import pprint while True: snake_case_ = input("""Enter a location:""").strip() if location: pprint(current_weather(location)) else: break
78
0
import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger __lowerCAmelCase = get_logger(__name__) __lowerCAmelCase = Path(__file__).parent / '''model_card_template.md''' __lowerCAmelCase = uuida().hex __lowerCAmelCase = os.getenv('''HF_HUB_OFFLINE''', '''''').upper() in ENV_VARS_TRUE_VALUES __lowerCAmelCase = os.getenv('''DISABLE_TELEMETRY''', '''''').upper() in ENV_VARS_TRUE_VALUES __lowerCAmelCase = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '''/api/telemetry/''' def snake_case_ ( snake_case = None ) -> str: lowercase__: Dict = f'diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}' if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += f'; torch/{_torch_version}' if is_flax_available(): ua += f'; jax/{_jax_version}' ua += f'; flax/{_flax_version}' if is_onnx_available(): ua += f'; onnxruntime/{_onnxruntime_version}' # CI will set this value to True if os.environ.get('DIFFUSERS_IS_CI' , '' ).upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(snake_case , snake_case ): ua += "; " + "; ".join(f'{k}/{v}' for k, v in user_agent.items() ) elif isinstance(snake_case , snake_case ): ua += "; " + user_agent return ua def snake_case_ ( snake_case , snake_case = None , snake_case = None ) -> Tuple: if token is None: lowercase__: Optional[Any] = HfFolder.get_token() if organization is None: lowercase__: Any = whoami(snake_case )['name'] return f'{username}/{model_id}' else: return f'{organization}/{model_id}' def snake_case_ ( snake_case , snake_case ) -> Tuple: if not is_jinja_available(): raise ValueError( 'Modelcard rendering is based on Jinja templates.' ' Please make sure to have `jinja` installed before using `create_model_card`.' ' To install it, please run `pip install Jinja2`.' ) if hasattr(snake_case , 'local_rank' ) and args.local_rank not in [-1, 0]: return lowercase__: List[str] = args.hub_token if hasattr(snake_case , 'hub_token' ) else None lowercase__: Tuple = get_full_repo_name(snake_case , token=snake_case ) lowercase__: str = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language='en' , license='apache-2.0' , library_name='diffusers' , tags=[] , datasets=args.dataset_name , metrics=[] , ) , template_path=snake_case , model_name=snake_case , repo_name=snake_case , dataset_name=args.dataset_name if hasattr(snake_case , 'dataset_name' ) else None , learning_rate=args.learning_rate , train_batch_size=args.train_batch_size , eval_batch_size=args.eval_batch_size , gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(snake_case , 'gradient_accumulation_steps' ) else None ) , adam_betaa=args.adam_betaa if hasattr(snake_case , 'adam_beta1' ) else None , adam_betaa=args.adam_betaa if hasattr(snake_case , 'adam_beta2' ) else None , adam_weight_decay=args.adam_weight_decay if hasattr(snake_case , 'adam_weight_decay' ) else None , adam_epsilon=args.adam_epsilon if hasattr(snake_case , 'adam_epsilon' ) else None , lr_scheduler=args.lr_scheduler if hasattr(snake_case , 'lr_scheduler' ) else None , lr_warmup_steps=args.lr_warmup_steps if hasattr(snake_case , 'lr_warmup_steps' ) else None , ema_inv_gamma=args.ema_inv_gamma if hasattr(snake_case , 'ema_inv_gamma' ) else None , ema_power=args.ema_power if hasattr(snake_case , 'ema_power' ) else None , ema_max_decay=args.ema_max_decay if hasattr(snake_case , 'ema_max_decay' ) else None , mixed_precision=args.mixed_precision , ) lowercase__: List[str] = os.path.join(args.output_dir , 'README.md' ) model_card.save(snake_case ) def snake_case_ ( snake_case , snake_case = None ) -> Union[str, Any]: if resolved_file is None or commit_hash is not None: return commit_hash lowercase__: int = str(Path(snake_case ).as_posix() ) lowercase__: List[str] = re.search(R'snapshots/([^/]+)/' , snake_case ) if search is None: return None lowercase__: Dict = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(snake_case ) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. __lowerCAmelCase = os.path.expanduser( os.getenv('''HF_HOME''', os.path.join(os.getenv('''XDG_CACHE_HOME''', '''~/.cache'''), '''huggingface''')) ) __lowerCAmelCase = os.path.join(hf_cache_home, '''diffusers''') def snake_case_ ( snake_case = None , snake_case = None ) -> None: if new_cache_dir is None: lowercase__: Any = DIFFUSERS_CACHE if old_cache_dir is None: lowercase__: int = old_diffusers_cache lowercase__: Dict = Path(snake_case ).expanduser() lowercase__: int = Path(snake_case ).expanduser() for old_blob_path in old_cache_dir.glob('**/blobs/*' ): if old_blob_path.is_file() and not old_blob_path.is_symlink(): lowercase__: Union[str, Any] = new_cache_dir / old_blob_path.relative_to(snake_case ) new_blob_path.parent.mkdir(parents=snake_case , exist_ok=snake_case ) os.replace(snake_case , snake_case ) try: os.symlink(snake_case , snake_case ) except OSError: logger.warning( 'Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.' ) # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). __lowerCAmelCase = os.path.join(DIFFUSERS_CACHE, '''version_diffusers_cache.txt''') if not os.path.isfile(cache_version_file): __lowerCAmelCase = 0 else: with open(cache_version_file) as f: try: __lowerCAmelCase = int(f.read()) except ValueError: __lowerCAmelCase = 0 if cache_version < 1: __lowerCAmelCase = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( '''The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ''' '''existing cached models. This is a one-time operation, you can interrupt it or run it ''' '''later by calling `diffusers.utils.hub_utils.move_cache()`.''' ) try: move_cache() except Exception as e: __lowerCAmelCase = '''\n'''.join(traceback.format_tb(e.__traceback__)) logger.error( F'''There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease ''' '''file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ''' '''message and we will do our best to help.''' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, '''w''') as f: f.write('''1''') except Exception: logger.warning( F'''There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure ''' '''the directory exists and can be written to.''' ) def snake_case_ ( snake_case , snake_case = None ) -> str: if variant is not None: lowercase__: List[Any] = weights_name.split('.' ) lowercase__: List[Any] = splits[:-1] + [variant] + splits[-1:] lowercase__: Optional[Any] = '.'.join(snake_case ) return weights_name def snake_case_ ( snake_case , *, snake_case , snake_case , snake_case , snake_case , snake_case , snake_case , snake_case , snake_case , snake_case , snake_case , snake_case=None , ) -> Dict: lowercase__: int = str(snake_case ) if os.path.isfile(snake_case ): return pretrained_model_name_or_path elif os.path.isdir(snake_case ): if os.path.isfile(os.path.join(snake_case , snake_case ) ): # Load from a PyTorch checkpoint lowercase__: Union[str, Any] = os.path.join(snake_case , snake_case ) return model_file elif subfolder is not None and os.path.isfile( os.path.join(snake_case , snake_case , snake_case ) ): lowercase__: str = os.path.join(snake_case , snake_case , snake_case ) return model_file else: raise EnvironmentError( f'Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.' ) else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(snake_case ).base_version ) >= version.parse('0.20.0' ) ): try: lowercase__: List[str] = hf_hub_download( snake_case , filename=_add_variant(snake_case , snake_case ) , cache_dir=snake_case , force_download=snake_case , proxies=snake_case , resume_download=snake_case , local_files_only=snake_case , use_auth_token=snake_case , user_agent=snake_case , subfolder=snake_case , revision=revision or commit_hash , ) warnings.warn( f'Loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'` is deprecated. Loading instead from `revision=\'main\'` with `variant={revision}`. Loading model variants via `revision=\'{revision}\'` will be removed in diffusers v1. Please use `variant=\'{revision}\'` instead.' , snake_case , ) return model_file except: # noqa: E722 warnings.warn( f'You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant=\'{revision}\'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(snake_case , snake_case )} file in the \'main\' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title \'{pretrained_model_name_or_path} is missing {_add_variant(snake_case , snake_case )}\' so that the correct variant file can be added.' , snake_case , ) try: # 2. Load model file as usual lowercase__: Dict = hf_hub_download( snake_case , filename=snake_case , cache_dir=snake_case , force_download=snake_case , proxies=snake_case , resume_download=snake_case , local_files_only=snake_case , use_auth_token=snake_case , user_agent=snake_case , subfolder=snake_case , revision=revision or commit_hash , ) return model_file except RepositoryNotFoundError: raise EnvironmentError( f'{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier ' 'listed on \'https://huggingface.co/models\'\nIf this is a private repository, make sure to pass a ' 'token having permission to this repo with `use_auth_token` or log in with `huggingface-cli ' 'login`.' ) except RevisionNotFoundError: raise EnvironmentError( f'{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for ' 'this model name. Check the model page at ' f'\'https://huggingface.co/{pretrained_model_name_or_path}\' for available revisions.' ) except EntryNotFoundError: raise EnvironmentError( f'{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.' ) except HTTPError as err: raise EnvironmentError( f'There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}' ) except ValueError: raise EnvironmentError( f'We couldn\'t connect to \'{HUGGINGFACE_CO_RESOLVE_ENDPOINT}\' to load this model, couldn\'t find it' f' in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a' f' directory containing a file named {weights_name} or' ' \nCheckout your internet connection or see how to run the library in' ' offline mode at \'https://huggingface.co/docs/diffusers/installation#offline-mode\'.' ) except EnvironmentError: raise EnvironmentError( f'Can\'t load the model for \'{pretrained_model_name_or_path}\'. If you were trying to load it from ' '\'https://huggingface.co/models\', make sure you don\'t have a local directory with the same name. ' f'Otherwise, make sure \'{pretrained_model_name_or_path}\' is the correct path to a directory ' f'containing a file named {weights_name}' )
288
import sacrebleu as scb from packaging import version from sacrebleu import CHRF import datasets __lowerCAmelCase = '''\ @inproceedings{popovic-2015-chrf, title = "chr{F}: character n-gram {F}-score for automatic {MT} evaluation", author = "Popovi{\'c}, Maja", booktitle = "Proceedings of the Tenth Workshop on Statistical Machine Translation", month = sep, year = "2015", address = "Lisbon, Portugal", publisher = "Association for Computational Linguistics", url = "https://aclanthology.org/W15-3049", doi = "10.18653/v1/W15-3049", pages = "392--395", } @inproceedings{popovic-2017-chrf, title = "chr{F}++: words helping character n-grams", author = "Popovi{\'c}, Maja", booktitle = "Proceedings of the Second Conference on Machine Translation", month = sep, year = "2017", address = "Copenhagen, Denmark", publisher = "Association for Computational Linguistics", url = "https://aclanthology.org/W17-4770", doi = "10.18653/v1/W17-4770", pages = "612--618", } @inproceedings{post-2018-call, title = "A Call for Clarity in Reporting {BLEU} Scores", author = "Post, Matt", booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers", month = oct, year = "2018", address = "Belgium, Brussels", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/W18-6319", pages = "186--191", } ''' __lowerCAmelCase = '''\ ChrF and ChrF++ are two MT evaluation metrics. They both use the F-score statistic for character n-gram matches, and ChrF++ adds word n-grams as well which correlates more strongly with direct assessment. We use the implementation that is already present in sacrebleu. The implementation here is slightly different from sacrebleu in terms of the required input format. The length of the references and hypotheses lists need to be the same, so you may need to transpose your references compared to sacrebleu\'s required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534 See the README.md file at https://github.com/mjpost/sacreBLEU#chrf--chrf for more information. ''' __lowerCAmelCase = ''' Produces ChrF(++) scores for hypotheses given reference translations. Args: predictions (list of str): The predicted sentences. references (list of list of str): The references. There should be one reference sub-list for each prediction sentence. char_order (int): Character n-gram order. Defaults to `6`. word_order (int): Word n-gram order. If equals to `2`, the metric is referred to as chrF++. Defaults to `0`. beta (int): Determine the importance of recall w.r.t precision. Defaults to `2`. lowercase (bool): if `True`, enables case-insensitivity. Defaults to `False`. whitespace (bool): If `True`, include whitespaces when extracting character n-grams. eps_smoothing (bool): If `True`, applies epsilon smoothing similar to reference chrF++.py, NLTK and Moses implementations. If `False`, it takes into account effective match order similar to sacreBLEU < 2.0.0. Defaults to `False`. Returns: \'score\' (float): The chrF (chrF++) score, \'char_order\' (int): The character n-gram order, \'word_order\' (int): The word n-gram order. If equals to 2, the metric is referred to as chrF++, \'beta\' (int): Determine the importance of recall w.r.t precision Examples: Example 1--a simple example of calculating chrF: >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."] >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]] >>> chrf = datasets.load_metric("chrf") >>> results = chrf.compute(predictions=prediction, references=reference) >>> print(results) {\'score\': 84.64214891738334, \'char_order\': 6, \'word_order\': 0, \'beta\': 2} Example 2--the same example, but with the argument word_order=2, to calculate chrF++ instead of chrF: >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."] >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]] >>> chrf = datasets.load_metric("chrf") >>> results = chrf.compute(predictions=prediction, ... references=reference, ... word_order=2) >>> print(results) {\'score\': 82.87263732906315, \'char_order\': 6, \'word_order\': 2, \'beta\': 2} Example 3--the same chrF++ example as above, but with `lowercase=True` to normalize all case: >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."] >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]] >>> chrf = datasets.load_metric("chrf") >>> results = chrf.compute(predictions=prediction, ... references=reference, ... word_order=2, ... lowercase=True) >>> print(results) {\'score\': 92.12853119829202, \'char_order\': 6, \'word_order\': 2, \'beta\': 2} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __a ( datasets.Metric ): def SCREAMING_SNAKE_CASE__ ( self ) -> str: '''simple docstring''' if version.parse(scb.__version__ ) < version.parse('1.4.12' ): raise ImportWarning( 'To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn\'t match this condition.\n' 'You can install it with `pip install "sacrebleu>=1.4.12"`.' ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='https://github.com/mjpost/sacreBLEU#chrf--chrf' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('string' , id='sequence' ), 'references': datasets.Sequence(datasets.Value('string' , id='sequence' ) , id='references' ), } ) , codebase_urls=['https://github.com/mjpost/sacreBLEU#chrf--chrf'] , reference_urls=[ 'https://github.com/m-popovic/chrF', ] , ) def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = CHRF.CHAR_ORDER , lowerCAmelCase__ = CHRF.WORD_ORDER , lowerCAmelCase__ = CHRF.BETA , lowerCAmelCase__ = False , lowerCAmelCase__ = False , lowerCAmelCase__ = False , ) -> List[Any]: '''simple docstring''' lowercase__: str = len(references[0] ) if any(len(lowerCAmelCase__ ) != references_per_prediction for refs in references ): raise ValueError('Sacrebleu requires the same number of references for each prediction' ) lowercase__: List[str] = [[refs[i] for refs in references] for i in range(lowerCAmelCase__ )] lowercase__: Union[str, Any] = CHRF(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) lowercase__: str = sb_chrf.corpus_score(lowerCAmelCase__ , lowerCAmelCase__ ) return { "score": output.score, "char_order": output.char_order, "word_order": output.word_order, "beta": output.beta, }
288
1
def __UpperCamelCase ( _lowerCAmelCase , _lowerCAmelCase ) -> int: """simple docstring""" return int((input_a, input_a).count(0 ) == 0 ) def __UpperCamelCase ( ) -> None: """simple docstring""" assert and_gate(0 , 0 ) == 0 assert and_gate(0 , 1 ) == 0 assert and_gate(1 , 0 ) == 0 assert and_gate(1 , 1 ) == 1 if __name__ == "__main__": test_and_gate() print(and_gate(1, 0)) print(and_gate(0, 0)) print(and_gate(0, 1)) print(and_gate(1, 1))
116
from manim import * class SCREAMING_SNAKE_CASE__ ( SCREAMING_SNAKE_CASE__ ): '''simple docstring''' def _lowerCAmelCase ( self ): A : Union[str, Any] = Rectangle(height=0.5, width=0.5 ) A : Optional[int] = Rectangle(height=0.25, width=0.25 ) A : Optional[Any] = Rectangle(height=0.46, width=0.46 ).set_stroke(width=0 ) A : List[str] = [mem.copy() for i in range(6 )] A : Any = [mem.copy() for i in range(6 )] A : int = VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__, buff=0 ) A : Tuple = VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__, buff=0 ) A : str = VGroup(lowerCamelCase__, lowerCamelCase__ ).arrange(lowerCamelCase__, buff=0 ) A : List[Any] = Text("""CPU""", font_size=24 ) A : Optional[int] = Group(lowerCamelCase__, lowerCamelCase__ ).arrange(lowerCamelCase__, buff=0.5, aligned_edge=lowerCamelCase__ ) cpu.move_to([-2.5, -0.5, 0] ) self.add(lowerCamelCase__ ) A : List[Any] = [mem.copy() for i in range(4 )] A : Optional[Any] = VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__, buff=0 ) A : Dict = Text("""GPU""", font_size=24 ) A : Any = Group(lowerCamelCase__, lowerCamelCase__ ).arrange(lowerCamelCase__, buff=0.5, aligned_edge=lowerCamelCase__ ) gpu.move_to([-1, -1, 0] ) self.add(lowerCamelCase__ ) A : Optional[int] = [mem.copy() for i in range(6 )] A : List[str] = VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__, buff=0 ) A : Optional[int] = Text("""Model""", font_size=24 ) A : List[Any] = Group(lowerCamelCase__, lowerCamelCase__ ).arrange(lowerCamelCase__, buff=0.5, aligned_edge=lowerCamelCase__ ) model.move_to([3, -1.0, 0] ) self.add(lowerCamelCase__ ) A : Tuple = [] A : Tuple = [] A : Any = [] for i, rect in enumerate(lowerCamelCase__ ): rect.set_stroke(lowerCamelCase__ ) A : Any = Rectangle(height=0.46 / 4, width=0.46 / 3 ).set_stroke(width=0.0 ).set_fill(lowerCamelCase__, opacity=0.7 ) if i == 0: cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ), buff=0.02, direction=lowerCamelCase__ ) cpu_target.set_x(cpu_target.get_x() + 0.1 ) elif i == 3: cpu_target.next_to(model_cpu_arr[0], direction=lowerCamelCase__, buff=0.0 ) else: cpu_target.next_to(model_cpu_arr[i - 1], direction=lowerCamelCase__, buff=0.0 ) self.add(lowerCamelCase__ ) model_cpu_arr.append(lowerCamelCase__ ) self.add(*lowerCamelCase__, *lowerCamelCase__, *lowerCamelCase__ ) A : int = [mem.copy() for i in range(6 )] A : List[Any] = VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__, buff=0 ) A : str = Text("""Loaded Checkpoint""", font_size=24 ) A : List[str] = Group(lowerCamelCase__, lowerCamelCase__ ).arrange(lowerCamelCase__, buff=0.5, aligned_edge=lowerCamelCase__ ) checkpoint.move_to([3, 0.5, 0] ) self.add(lowerCamelCase__ ) A : Optional[int] = [] A : List[Any] = [] for i, rect in enumerate(lowerCamelCase__ ): A : int = fill.copy().set_fill(lowerCamelCase__, opacity=0.7 ) target.move_to(lowerCamelCase__ ) ckpt_arr.append(lowerCamelCase__ ) A : List[Any] = target.copy() if i < 5: cpu_target.move_to(cpu_left_col_base[i + 1] ) else: cpu_target.move_to(cpu_right_col_base[i - 5] ) ckpt_cpu_arr.append(lowerCamelCase__ ) self.add(*lowerCamelCase__, *lowerCamelCase__ ) A : str = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) A : List[Any] = 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(lowerCamelCase__, lowerCamelCase__ ) A : Union[str, Any] = MarkupText( f'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint''', font_size=18, ) blue_text.next_to(lowerCamelCase__, DOWN * 2.4, aligned_edge=key_text.get_left() ) self.add(lowerCamelCase__ ) A : List[str] = MarkupText( f'''Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device.''', font_size=24, ) step_a.move_to([2, 2, 0] ) A : List[str] = [meta_mem.copy() for i in range(6 )] A : List[Any] = [meta_mem.copy() for i in range(6 )] A : List[Any] = VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__, buff=0 ) A : List[str] = VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__, buff=0 ) A : Dict = VGroup(lowerCamelCase__, lowerCamelCase__ ).arrange(lowerCamelCase__, buff=0 ) A : Optional[Any] = Text("""Disk""", font_size=24 ) A : List[str] = Group(lowerCamelCase__, lowerCamelCase__ ).arrange(lowerCamelCase__, buff=0.5, aligned_edge=lowerCamelCase__ ) disk.move_to([-4.0, -1.25, 0] ) self.play(Write(lowerCamelCase__, run_time=3 ), Write(lowerCamelCase__, run_time=1 ), Create(lowerCamelCase__, run_time=1 ) ) A : str = [] for i, rect in enumerate(lowerCamelCase__ ): A : Optional[Any] = rect.copy() target.generate_target() target.target.move_to(disk_left_col_base[i] ).scale(0.5 ) animations.append(MoveToTarget(lowerCamelCase__, run_time=1.5 ) ) self.play(*lowerCamelCase__ ) self.play(FadeOut(lowerCamelCase__ ) ) A : List[str] = MarkupText(f'''Then, the checkpoint is removed from memory\nthrough garbage collection.''', font_size=24 ) step_a.move_to([2, 2, 0] ) self.play(Write(lowerCamelCase__, run_time=3 ) ) self.play( FadeOut(lowerCamelCase__, lowerCamelCase__, *lowerCamelCase__, *lowerCamelCase__ ), ) self.wait()
116
1
import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging lowercase_ = logging.get_logger(__name__) class A ( _UpperCAmelCase ): """simple docstring""" def __init__( self : List[str],lowercase_ : Union[List[ControlNetModel], Tuple[ControlNetModel]] )-> Any: '''simple docstring''' super().__init__() A__ = nn.ModuleList(lowercase_ ) def snake_case__ ( self : List[str],lowercase_ : torch.FloatTensor,lowercase_ : Union[torch.Tensor, float, int],lowercase_ : torch.Tensor,lowercase_ : List[torch.tensor],lowercase_ : List[float],lowercase_ : Optional[torch.Tensor] = None,lowercase_ : Optional[torch.Tensor] = None,lowercase_ : Optional[torch.Tensor] = None,lowercase_ : Optional[Dict[str, Any]] = None,lowercase_ : bool = False,lowercase_ : bool = True,)-> Union[ControlNetOutput, Tuple]: '''simple docstring''' for i, (image, scale, controlnet) in enumerate(zip(lowercase_,lowercase_,self.nets ) ): A__ , A__ = controlnet( lowercase_,lowercase_,lowercase_,lowercase_,lowercase_,lowercase_,lowercase_,lowercase_,lowercase_,lowercase_,lowercase_,) # merge samples if i == 0: A__ , A__ = down_samples, mid_sample else: A__ = [ samples_prev + samples_curr for samples_prev, samples_curr in zip(lowercase_,lowercase_ ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def snake_case__ ( self : Optional[Any],lowercase_ : Union[str, os.PathLike],lowercase_ : bool = True,lowercase_ : Callable = None,lowercase_ : bool = False,lowercase_ : Optional[str] = None,)-> List[Any]: '''simple docstring''' A__ = 0 A__ = save_directory for controlnet in self.nets: controlnet.save_pretrained( lowercase_,is_main_process=lowercase_,save_function=lowercase_,safe_serialization=lowercase_,variant=lowercase_,) idx += 1 A__ = model_path_to_save + F'_{idx}' @classmethod def snake_case__ ( cls : List[str],lowercase_ : Optional[Union[str, os.PathLike]],**lowercase_ : int )-> Dict: '''simple docstring''' A__ = 0 A__ = [] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... A__ = pretrained_model_path while os.path.isdir(lowercase_ ): A__ = ControlNetModel.from_pretrained(lowercase_,**lowercase_ ) controlnets.append(lowercase_ ) idx += 1 A__ = pretrained_model_path + F'_{idx}' logger.info(F'{len(lowercase_ )} controlnets loaded from {pretrained_model_path}.' ) if len(lowercase_ ) == 0: raise ValueError( F'No ControlNets found under {os.path.dirname(lowercase_ )}. Expected at least {pretrained_model_path + "_0"}.' ) return cls(lowercase_ )
282
import comet # From: unbabel-comet import torch import datasets lowercase_ = datasets.logging.get_logger(__name__) lowercase_ = "\\n@inproceedings{rei-EtAl:2020:WMT,\n author = {Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon},\n title = {Unbabel's Participation in the WMT20 Metrics Shared Task},\n booktitle = {Proceedings of the Fifth Conference on Machine Translation},\n month = {November},\n year = {2020},\n address = {Online},\n publisher = {Association for Computational Linguistics},\n pages = {909--918},\n}\n@inproceedings{rei-etal-2020-comet,\n title = \"{COMET}: A Neural Framework for {MT} Evaluation\",\n author = \"Rei, Ricardo and\n Stewart, Craig and\n Farinha, Ana C and\n Lavie, Alon\",\n booktitle = \"Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)\",\n month = nov,\n year = \"2020\",\n address = \"Online\",\n publisher = \"Association for Computational Linguistics\",\n url = \"https://www.aclweb.org/anthology/2020.emnlp-main.213\",\n pages = \"2685--2702\",\n}\n" lowercase_ = "\\nCrosslingual Optimized Metric for Evaluation of Translation (COMET) is an open-source framework used to train Machine Translation metrics that achieve high levels of correlation with different types of human judgments (HTER, DA's or MQM).\nWith the release of the framework the authors also released fully trained models that were used to compete in the WMT20 Metrics Shared Task achieving SOTA in that years competition.\n\nSee the [README.md] file at https://unbabel.github.io/COMET/html/models.html for more information.\n" lowercase_ = "\nCOMET score.\n\nArgs:\n\n`sources` (list of str): Source sentences\n`predictions` (list of str): candidate translations\n`references` (list of str): reference translations\n`cuda` (bool): If set to True, runs COMET using GPU\n`show_progress` (bool): Shows progress\n`model`: COMET model to be used. Will default to `wmt-large-da-estimator-1719` if None.\n\nReturns:\n `samples`: List of dictionaries with `src`, `mt`, `ref` and `score`.\n `scores`: List of scores.\n\nExamples:\n\n >>> comet_metric = datasets.load_metric('comet')\n >>> # comet_metric = load_metric('comet', 'wmt20-comet-da') # you can also choose which model to use\n >>> source = [\"Dem Feuer konnte Einhalt geboten werden\", \"Schulen und Kindergärten wurden eröffnet.\"]\n >>> hypothesis = [\"The fire could be stopped\", \"Schools and kindergartens were open\"]\n >>> reference = [\"They were able to control the fire.\", \"Schools and kindergartens opened\"]\n >>> results = comet_metric.compute(predictions=hypothesis, references=reference, sources=source)\n >>> print([round(v, 2) for v in results[\"scores\"]])\n [0.19, 0.92]\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A ( datasets.Metric ): """simple docstring""" def snake_case__ ( self : Optional[Any] )-> str: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION,citation=_CITATION,homepage='https://unbabel.github.io/COMET/html/index.html',inputs_description=_KWARGS_DESCRIPTION,features=datasets.Features( { 'sources': datasets.Value('string',id='sequence' ), 'predictions': datasets.Value('string',id='sequence' ), 'references': datasets.Value('string',id='sequence' ), } ),codebase_urls=['https://github.com/Unbabel/COMET'],reference_urls=[ 'https://github.com/Unbabel/COMET', 'https://www.aclweb.org/anthology/2020.emnlp-main.213/', 'http://www.statmt.org/wmt20/pdf/2020.wmt-1.101.pdf6', ],) def snake_case__ ( self : Optional[Any],lowercase_ : Dict )-> str: '''simple docstring''' if self.config_name == "default": A__ = comet.load_from_checkpoint(comet.download_model('wmt20-comet-da' ) ) else: A__ = comet.load_from_checkpoint(comet.download_model(self.config_name ) ) def snake_case__ ( self : List[Any],lowercase_ : Dict,lowercase_ : int,lowercase_ : List[Any],lowercase_ : Tuple=None,lowercase_ : List[str]=False )-> int: '''simple docstring''' if gpus is None: A__ = 1 if torch.cuda.is_available() else 0 A__ = {'src': sources, 'mt': predictions, 'ref': references} A__ = [dict(zip(lowercase_,lowercase_ ) ) for t in zip(*data.values() )] A__ , A__ = self.scorer.predict(lowercase_,gpus=lowercase_,progress_bar=lowercase_ ) return {"mean_score": mean_score, "scores": scores}
282
1
import unittest import numpy as np from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class snake_case__ (_UpperCamelCase , unittest.TestCase ): """simple docstring""" pass @nightly @require_onnxruntime @require_torch_gpu class snake_case__ (unittest.TestCase ): """simple docstring""" @property def __UpperCAmelCase ( self : List[str] ) -> Dict: return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def __UpperCAmelCase ( self : Dict ) -> int: a = ort.SessionOptions() a = False return options def __UpperCAmelCase ( self : List[str] ) -> List[str]: a = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/in_paint/overture-creations-5sI6fQgYIuo.png" ) a = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/in_paint/overture-creations-5sI6fQgYIuo_mask.png" ) a = OnnxStableDiffusionInpaintPipeline.from_pretrained( "runwayml/stable-diffusion-inpainting" , revision="onnx" , safety_checker=__lowerCamelCase , feature_extractor=__lowerCamelCase , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=__lowerCamelCase ) a = """A red cat sitting on a park bench""" a = np.random.RandomState(0 ) a = pipe( prompt=__lowerCamelCase , image=__lowerCamelCase , mask_image=__lowerCamelCase , guidance_scale=7.5 , num_inference_steps=10 , generator=__lowerCamelCase , output_type="np" , ) a = output.images a = images[0, 2_55:2_58, 2_55:2_58, -1] assert images.shape == (1, 5_12, 5_12, 3) a = np.array([0.2_514, 0.3_007, 0.3_517, 0.1_790, 0.2_382, 0.3_167, 0.1_944, 0.2_273, 0.2_464] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def __UpperCAmelCase ( self : Any ) -> Optional[int]: a = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/in_paint/overture-creations-5sI6fQgYIuo.png" ) a = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/in_paint/overture-creations-5sI6fQgYIuo_mask.png" ) a = LMSDiscreteScheduler.from_pretrained( "runwayml/stable-diffusion-inpainting" , subfolder="scheduler" , revision="onnx" ) a = OnnxStableDiffusionInpaintPipeline.from_pretrained( "runwayml/stable-diffusion-inpainting" , revision="onnx" , scheduler=__lowerCamelCase , safety_checker=__lowerCamelCase , feature_extractor=__lowerCamelCase , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=__lowerCamelCase ) a = """A red cat sitting on a park bench""" a = np.random.RandomState(0 ) a = pipe( prompt=__lowerCamelCase , image=__lowerCamelCase , mask_image=__lowerCamelCase , guidance_scale=7.5 , num_inference_steps=20 , generator=__lowerCamelCase , output_type="np" , ) a = output.images a = images[0, 2_55:2_58, 2_55:2_58, -1] assert images.shape == (1, 5_12, 5_12, 3) a = np.array([0.0_086, 0.0_077, 0.0_083, 0.0_093, 0.0_107, 0.0_139, 0.0_094, 0.0_097, 0.0_125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3
107
"""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 : Any = { '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 : Optional[int] = ['BlenderbotTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : Optional[Any] = [ 'BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BlenderbotForCausalLM', 'BlenderbotForConditionalGeneration', 'BlenderbotModel', 'BlenderbotPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : Optional[int] = [ 'TFBlenderbotForConditionalGeneration', 'TFBlenderbotModel', 'TFBlenderbotPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : Optional[int] = [ '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 : Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
115
0
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase_ : int = logging.get_logger(__name__) lowerCAmelCase_ : Dict = { '''google/vit-base-patch16-224''': '''https://huggingface.co/vit-base-patch16-224/resolve/main/config.json''', # See all ViT models at https://huggingface.co/models?filter=vit } class UpperCamelCase_ ( a_ ): _A : Tuple = 'vit' def __init__( self , snake_case__=7_68 , snake_case__=12 , snake_case__=12 , snake_case__=30_72 , snake_case__="gelu" , snake_case__=0.0 , snake_case__=0.0 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__=2_24 , snake_case__=16 , snake_case__=3 , snake_case__=True , snake_case__=16 , **snake_case__ , ) -> Tuple: """simple docstring""" super().__init__(**snake_case__ ) UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_act UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = initializer_range UpperCAmelCase = layer_norm_eps UpperCAmelCase = image_size UpperCAmelCase = patch_size UpperCAmelCase = num_channels UpperCAmelCase = qkv_bias UpperCAmelCase = encoder_stride class UpperCamelCase_ ( a_ ): _A : Optional[Any] = version.parse('1.11' ) @property def UpperCamelCase_ ( self ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def UpperCamelCase_ ( self ) -> float: """simple docstring""" return 1e-4
248
"""simple docstring""" import os def _lowerCAmelCase ( lowerCAmelCase ): '''simple docstring''' UpperCAmelCase = len(grid[0] ) UpperCAmelCase = len(lowerCAmelCase ) UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 # Check vertically, horizontally, diagonally at the same time (only works # for nxn grid) for i in range(lowerCAmelCase ): for j in range(n_rows - 3 ): UpperCAmelCase = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i] UpperCAmelCase = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3] # Left-to-right diagonal (\) product if i < n_columns - 3: UpperCAmelCase = ( grid[i][j] * grid[i + 1][j + 1] * grid[i + 2][j + 2] * grid[i + 3][j + 3] ) # Right-to-left diagonal(/) product if i > 2: UpperCAmelCase = ( grid[i][j] * grid[i - 1][j + 1] * grid[i - 2][j + 2] * grid[i - 3][j + 3] ) UpperCAmelCase = max( lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) if max_product > largest: UpperCAmelCase = max_product return largest def _lowerCAmelCase ( ): '''simple docstring''' UpperCAmelCase = [] with open(os.path.dirname(lowerCAmelCase ) + """/grid.txt""" ) as file: for line in file: grid.append(line.strip("""\n""" ).split(""" """ ) ) UpperCAmelCase = [[int(lowerCAmelCase ) for i in grid[j]] for j in range(len(lowerCAmelCase ) )] return largest_product(lowerCAmelCase ) if __name__ == "__main__": print(solution())
248
1
# Copyright 2022 The HuggingFace Team and The OpenBMB 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 typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available A : Any = { """configuration_cpmant""": ["""CPMANT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """CpmAntConfig"""], """tokenization_cpmant""": ["""CpmAntTokenizer"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[Any] = [ """CPMANT_PRETRAINED_MODEL_ARCHIVE_LIST""", """CpmAntForCausalLM""", """CpmAntModel""", """CpmAntPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_cpmant import CPMANT_PRETRAINED_CONFIG_ARCHIVE_MAP, CpmAntConfig from .tokenization_cpmant import CpmAntTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_cpmant import ( CPMANT_PRETRAINED_MODEL_ARCHIVE_LIST, CpmAntForCausalLM, CpmAntModel, CpmAntPreTrainedModel, ) else: import sys A : Any = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
184
from ..utils import DummyObject, requires_backends class _UpperCamelCase ( metaclass=_UpperCAmelCase ): """simple docstring""" __a : Dict = ['''keras_nlp'''] def __init__( self , *lowerCAmelCase__ , **lowerCAmelCase__ ) -> Tuple: '''simple docstring''' requires_backends(self , ['''keras_nlp'''] )
210
0
from __future__ import annotations import random import unittest from transformers import TransfoXLConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST, TFTransfoXLForSequenceClassification, TFTransfoXLLMHeadModel, TFTransfoXLModel, ) class lowerCAmelCase__ : def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE__ : Dict , ) -> Optional[Any]: __lowerCamelCase = parent __lowerCamelCase = 13 __lowerCamelCase = 7 __lowerCamelCase = 30 __lowerCamelCase = self.seq_length + self.mem_len __lowerCamelCase = 15 __lowerCamelCase = True __lowerCamelCase = True __lowerCamelCase = 99 __lowerCamelCase = [10, 50, 80] __lowerCamelCase = 32 __lowerCamelCase = 32 __lowerCamelCase = 4 __lowerCamelCase = 8 __lowerCamelCase = 1_28 __lowerCamelCase = 2 __lowerCamelCase = 2 __lowerCamelCase = None __lowerCamelCase = 1 __lowerCamelCase = 0 __lowerCamelCase = 3 __lowerCamelCase = self.vocab_size - 1 __lowerCamelCase = 0.01 def __A ( self : str ) -> Tuple: __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCamelCase = None if self.use_labels: __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCamelCase = TransfoXLConfig( vocab_size=self.vocab_size , mem_len=self.mem_len , clamp_len=self.clamp_len , cutoffs=self.cutoffs , d_model=self.hidden_size , d_embed=self.d_embed , n_head=self.num_attention_heads , d_head=self.d_head , d_inner=self.d_inner , div_val=self.div_val , n_layer=self.num_hidden_layers , eos_token_id=self.eos_token_id , pad_token_id=self.vocab_size - 1 , init_range=self.init_range , num_labels=self.num_labels , ) return (config, input_ids_a, input_ids_a, lm_labels) def __A ( self : Optional[int] ) -> Tuple: random.seed(self.seed ) tf.random.set_seed(self.seed ) def __A ( self : List[str] , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Union[str, Any] ) -> str: __lowerCamelCase = TFTransfoXLModel(SCREAMING_SNAKE_CASE__ ) __lowerCamelCase , __lowerCamelCase = model(SCREAMING_SNAKE_CASE__ ).to_tuple() __lowerCamelCase = {'''input_ids''': input_ids_a, '''mems''': mems_a} __lowerCamelCase , __lowerCamelCase = model(SCREAMING_SNAKE_CASE__ ).to_tuple() self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def __A ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Optional[Any] ) -> Optional[int]: __lowerCamelCase = TFTransfoXLLMHeadModel(SCREAMING_SNAKE_CASE__ ) __lowerCamelCase , __lowerCamelCase = model(SCREAMING_SNAKE_CASE__ ).to_tuple() __lowerCamelCase = {'''input_ids''': input_ids_a, '''labels''': lm_labels} __lowerCamelCase , __lowerCamelCase = model(SCREAMING_SNAKE_CASE__ ).to_tuple() __lowerCamelCase , __lowerCamelCase = model([input_ids_a, mems_a] ).to_tuple() __lowerCamelCase = {'''input_ids''': input_ids_a, '''mems''': mems_a, '''labels''': lm_labels} __lowerCamelCase , __lowerCamelCase = model(SCREAMING_SNAKE_CASE__ ).to_tuple() self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def __A ( self : List[str] , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : List[Any] ) -> List[Any]: __lowerCamelCase = TFTransfoXLForSequenceClassification(SCREAMING_SNAKE_CASE__ ) __lowerCamelCase = model(SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __A ( self : Optional[int] ) -> Tuple: __lowerCamelCase = self.prepare_config_and_inputs() ((__lowerCamelCase) , (__lowerCamelCase) , (__lowerCamelCase) , (__lowerCamelCase)) = config_and_inputs __lowerCamelCase = {'''input_ids''': input_ids_a} return config, inputs_dict @require_tf class lowerCAmelCase__ ( __lowercase , __lowercase , unittest.TestCase ): a__ : List[Any] = ( (TFTransfoXLModel, TFTransfoXLLMHeadModel, TFTransfoXLForSequenceClassification) if is_tf_available() else () ) a__ : List[Any] = () if is_tf_available() else () a__ : Union[str, Any] = ( { """feature-extraction""": TFTransfoXLModel, """text-classification""": TFTransfoXLForSequenceClassification, """text-generation""": TFTransfoXLLMHeadModel, """zero-shot""": TFTransfoXLForSequenceClassification, } if is_tf_available() else {} ) # TODO: add this test when TFTransfoXLLMHead has a linear output layer implemented a__ : Tuple = False a__ : int = False a__ : List[str] = False a__ : List[str] = False def __A ( self : Any , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : int ) -> Optional[int]: if pipeline_test_casse_name == "TextGenerationPipelineTests": # Get `ValueError: AttributeError: 'NoneType' object has no attribute 'new_ones'` or `AssertionError`. # `TransfoXLConfig` was never used in pipeline tests: cannot create a simple # tokenizer. return True return False def __A ( self : Optional[Any] ) -> Dict: __lowerCamelCase = TFTransfoXLModelTester(self ) __lowerCamelCase = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE__ , d_embed=37 ) def __A ( self : str ) -> Dict: self.config_tester.run_common_tests() def __A ( self : Union[str, Any] ) -> str: self.model_tester.set_seed() __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_model(*SCREAMING_SNAKE_CASE__ ) def __A ( self : Dict ) -> Tuple: self.model_tester.set_seed() __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_lm_head(*SCREAMING_SNAKE_CASE__ ) def __A ( self : int ) -> Dict: __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_for_sequence_classification(*SCREAMING_SNAKE_CASE__ ) def __A ( self : List[str] ) -> Tuple: __lowerCamelCase , __lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() __lowerCamelCase = [TFTransfoXLForSequenceClassification] for model_class in self.all_model_classes: __lowerCamelCase = model_class(SCREAMING_SNAKE_CASE__ ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class in list_other_models_with_output_ebd: __lowerCamelCase = model.get_output_embeddings() assert isinstance(SCREAMING_SNAKE_CASE__ , tf.keras.layers.Layer ) __lowerCamelCase = model.get_bias() assert name is None else: __lowerCamelCase = model.get_output_embeddings() assert x is None __lowerCamelCase = model.get_bias() assert name is None def __A ( self : Optional[int] ) -> List[Any]: # TODO JP: Make TransfoXL XLA compliant pass @slow def __A ( self : List[str] ) -> int: for model_name in TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __lowerCamelCase = TFTransfoXLModel.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) @unittest.skip(reason='''This model doesn\'t play well with fit() due to not returning a single loss.''' ) def __A ( self : Optional[int] ) -> Optional[Any]: pass @require_tf class lowerCAmelCase__ ( unittest.TestCase ): @unittest.skip('''Skip test until #12651 is resolved.''' ) @slow def __A ( self : Optional[Any] ) -> Tuple: __lowerCamelCase = TFTransfoXLLMHeadModel.from_pretrained('''transfo-xl-wt103''' ) # fmt: off __lowerCamelCase = tf.convert_to_tensor([[33,12_97,2,1,10_09,4,11_09,1_17_39,47_62,3_58,5,25,2_45,22,17_06,17,2_00_98,5,32_15,21,37,11_10,3,13,10_41,4,24,6_03,4_90,2,7_14_77,2_00_98,10_44_47,2,2_09_61,1,26_04,4,1,3_29,3,62_24,8_31,1_60_02,2,8,6_03,7_89_67,2_95_46,23,8_03,20,25,4_16,5,8,2_32,4,2_77,6,18_55,46_01,3,2_95_46,54,8,36_09,5,5_72_11,49,4,1,2_77,18,8,17_55,1_56_91,3,3_41,25,4_16,6_93,4_25_73,71,17,4_01,94,31,1_79_19,2,2_95_46,78_73,18,1,4_35,23,1_10_11,7_55,5,51_67,3,79_83,98,84,2,2_95_46,32_67,8,36_09,4,1,48_65,10_75,2,60_87,71,6,3_46,8,58_54,3,2_95_46,8_24,14_00,18_68,2,19,1_60,2,3_11,8,54_96,2,2_09_20,17,25,1_50_97,3,24,24,0]] , dtype=tf.intaa ) # noqa: E231 # fmt: on # In 1991 , the remains of Russian Tsar Nicholas II and his family # ( except for Alexei and Maria ) are discovered . # The voice of Nicholas's young son , Tsarevich Alexei Nikolaevich , narrates the # remainder of the story . 1883 Western Siberia , # a young Grigori Rasputin is asked by his father and a group of men to perform magic . # Rasputin has a vision and denounces one of the men as a horse thief . Although his # father initially slaps him for making such an accusation , Rasputin watches as the # man is chased outside and beaten . Twenty years later , Rasputin sees a vision of # the Virgin Mary , prompting him to become a priest . Rasputin quickly becomes famous , # with people , even a bishop , begging for his blessing . <eod> </s> <eos> # fmt: off __lowerCamelCase = [33,12_97,2,1,10_09,4,11_09,1_17_39,47_62,3_58,5,25,2_45,22,17_06,17,2_00_98,5,32_15,21,37,11_10,3,13,10_41,4,24,6_03,4_90,2,7_14_77,2_00_98,10_44_47,2,2_09_61,1,26_04,4,1,3_29,3,62_24,8_31,1_60_02,2,8,6_03,7_89_67,2_95_46,23,8_03,20,25,4_16,5,8,2_32,4,2_77,6,18_55,46_01,3,2_95_46,54,8,36_09,5,5_72_11,49,4,1,2_77,18,8,17_55,1_56_91,3,3_41,25,4_16,6_93,4_25_73,71,17,4_01,94,31,1_79_19,2,2_95_46,78_73,18,1,4_35,23,1_10_11,7_55,5,51_67,3,79_83,98,84,2,2_95_46,32_67,8,36_09,4,1,48_65,10_75,2,60_87,71,6,3_46,8,58_54,3,2_95_46,8_24,14_00,18_68,2,19,1_60,2,3_11,8,54_96,2,2_09_20,17,25,1_50_97,3,24,24,0,33,1,18_57,2,1,10_09,4,11_09,1_17_39,47_62,3_58,5,25,2_45,28,11_10,3,13,10_41,4,24,6_03,4_90,2,7_14_77,2_00_98,10_44_47,2,2_09_61,1,26_04,4,1,3_29,3,0] # noqa: E231 # fmt: on # In 1991, the remains of Russian Tsar Nicholas II and his family ( # except for Alexei and Maria ) are discovered. The voice of young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story. # 1883 Western Siberia, a young Grigori Rasputin is asked by his father # and a group of men to perform magic. Rasputin has a vision and # denounces one of the men as a horse thief. Although his father initially # slaps him for making such an accusation, Rasputin watches as the man # is chased outside and beaten. Twenty years later, Rasputin sees a vision # of the Virgin Mary, prompting him to become a priest. # Rasputin quickly becomes famous, with people, even a bishop, begging for # his blessing. <unk> <unk> <eos> In the 1990s, the remains of Russian Tsar # Nicholas II and his family were discovered. The voice of <unk> young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story.<eos> __lowerCamelCase = model.generate(SCREAMING_SNAKE_CASE__ , max_length=2_00 , do_sample=SCREAMING_SNAKE_CASE__ ) self.assertListEqual(output_ids[0].numpy().tolist() , SCREAMING_SNAKE_CASE__ )
339
import datasets import faiss import numpy as np import streamlit as st import torch from elasticsearch import Elasticsearch from elia_utils import ( embed_questions_for_retrieval, make_qa_sas_model, qa_sas_generate, query_es_index, query_qa_dense_index, ) import transformers from transformers import AutoModel, AutoModelForSeqaSeqLM, AutoTokenizer SCREAMING_SNAKE_CASE__ : Optional[int] = "bart" SCREAMING_SNAKE_CASE__ : Dict = True @st.cache(allow_output_mutation=__lowerCAmelCase ) def __magic_name__ ( ) -> str: if LOAD_DENSE_INDEX: __lowerCamelCase = AutoTokenizer.from_pretrained('''yjernite/retribert-base-uncased''' ) __lowerCamelCase = AutoModel.from_pretrained('''yjernite/retribert-base-uncased''' ).to('''cuda:0''' ) __lowerCamelCase = qar_model.eval() else: __lowerCamelCase , __lowerCamelCase = (None, None) if MODEL_TYPE == "bart": __lowerCamelCase = AutoTokenizer.from_pretrained('''yjernite/bart_eli5''' ) __lowerCamelCase = AutoModelForSeqaSeqLM.from_pretrained('''yjernite/bart_eli5''' ).to('''cuda:0''' ) __lowerCamelCase = torch.load('''seq2seq_models/eli5_bart_model_blm_2.pth''' ) sas_model.load_state_dict(save_dict['''model'''] ) __lowerCamelCase = sas_model.eval() else: __lowerCamelCase , __lowerCamelCase = make_qa_sas_model( model_name='''t5-small''' , from_file='''seq2seq_models/eli5_t5_model_1024_4.pth''' , device='''cuda:0''' ) return (qar_tokenizer, qar_model, sas_tokenizer, sas_model) @st.cache(allow_output_mutation=__lowerCAmelCase ) def __magic_name__ ( ) -> Optional[int]: if LOAD_DENSE_INDEX: __lowerCamelCase = faiss.StandardGpuResources() __lowerCamelCase = datasets.load_dataset(path='''wiki_snippets''' , name='''wiki40b_en_100_0''' )['''train'''] __lowerCamelCase = np.memmap( '''wiki40b_passages_reps_32_l-8_h-768_b-512-512.dat''' , dtype='''float32''' , mode='''r''' , shape=(wikiaab_passages.num_rows, 128) , ) __lowerCamelCase = faiss.IndexFlatIP(128 ) __lowerCamelCase = faiss.index_cpu_to_gpu(__lowerCAmelCase , 1 , __lowerCAmelCase ) wikiaab_gpu_index_flat.add(__lowerCAmelCase ) # TODO fix for larger GPU else: __lowerCamelCase , __lowerCamelCase = (None, None) __lowerCamelCase = Elasticsearch([{'''host''': '''localhost''', '''port''': '''9200'''}] ) return (wikiaab_passages, wikiaab_gpu_index_flat, es_client) @st.cache(allow_output_mutation=__lowerCAmelCase ) def __magic_name__ ( ) -> List[str]: __lowerCamelCase = datasets.load_dataset('''eli5''' , name='''LFQA_reddit''' ) __lowerCamelCase = elia['''train_eli5'''] __lowerCamelCase = np.memmap( '''eli5_questions_reps.dat''' , dtype='''float32''' , mode='''r''' , shape=(elia_train.num_rows, 128) ) __lowerCamelCase = faiss.IndexFlatIP(128 ) eli5_train_q_index.add(__lowerCAmelCase ) return (elia_train, eli5_train_q_index) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : int = load_indexes() SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : List[Any] = load_models() SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : int = load_train_data() def __magic_name__ ( __lowerCAmelCase : Dict , __lowerCAmelCase : List[str]=10 ) -> List[str]: __lowerCamelCase = embed_questions_for_retrieval([question] , __lowerCAmelCase , __lowerCAmelCase ) __lowerCamelCase , __lowerCamelCase = eli5_train_q_index.search(__lowerCAmelCase , __lowerCAmelCase ) __lowerCamelCase = [elia_train[int(__lowerCAmelCase )] for i in I[0]] return nn_examples def __magic_name__ ( __lowerCAmelCase : Tuple , __lowerCAmelCase : Dict="wiki40b" , __lowerCAmelCase : Any="dense" , __lowerCAmelCase : Dict=10 ) -> Union[str, Any]: if source == "none": __lowerCamelCase , __lowerCamelCase = (''' <P> '''.join(['''''' for _ in range(11 )] ).strip(), []) else: if method == "dense": __lowerCamelCase , __lowerCamelCase = query_qa_dense_index( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) else: __lowerCamelCase , __lowerCamelCase = query_es_index( __lowerCAmelCase , __lowerCAmelCase , index_name='''english_wiki40b_snippets_100w''' , n_results=__lowerCAmelCase , ) __lowerCamelCase = [ (res['''article_title'''], res['''section_title'''].strip(), res['''score'''], res['''passage_text''']) for res in hit_lst ] __lowerCamelCase = '''question: {} context: {}'''.format(__lowerCAmelCase , __lowerCAmelCase ) return question_doc, support_list @st.cache( hash_funcs={ torch.Tensor: (lambda __lowerCAmelCase : None), transformers.models.bart.tokenization_bart.BartTokenizer: (lambda __lowerCAmelCase : None), } ) def __magic_name__ ( __lowerCAmelCase : int , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : str=64 , __lowerCAmelCase : Dict=256 , __lowerCAmelCase : Union[str, Any]=False , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : Optional[Any]=0.95 , __lowerCAmelCase : List[Any]=0.8 ) -> Any: with torch.no_grad(): __lowerCamelCase = qa_sas_generate( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , num_answers=1 , num_beams=__lowerCAmelCase , min_len=__lowerCAmelCase , max_len=__lowerCAmelCase , do_sample=__lowerCAmelCase , temp=__lowerCAmelCase , top_p=__lowerCAmelCase , top_k=__lowerCAmelCase , max_input_length=1024 , device='''cuda:0''' , )[0] return (answer, support_list) st.title("Long Form Question Answering with ELI5") # Start sidebar SCREAMING_SNAKE_CASE__ : List[str] = "<img src='https://huggingface.co/front/assets/huggingface_logo.svg'>" SCREAMING_SNAKE_CASE__ : Dict = "\n<html>\n <head>\n <style>\n .img-container {\n padding-left: 90px;\n padding-right: 90px;\n padding-top: 50px;\n padding-bottom: 50px;\n background-color: #f0f3f9;\n }\n </style>\n </head>\n <body>\n <span class=\"img-container\"> <!-- Inline parent element -->\n %s\n </span>\n </body>\n</html>\n" % ( header_html, ) st.sidebar.markdown( header_full, unsafe_allow_html=True, ) # Long Form QA with ELI5 and Wikipedia SCREAMING_SNAKE_CASE__ : int = "\nThis demo presents a model trained to [provide long-form answers to open-domain questions](https://yjernite.github.io/lfqa.html).\nFirst, a document retriever fetches a set of relevant Wikipedia passages given the question from the [Wiki40b](https://research.google/pubs/pub49029/) dataset,\na pre-processed fixed snapshot of Wikipedia.\n" st.sidebar.markdown(description, unsafe_allow_html=True) SCREAMING_SNAKE_CASE__ : str = [ "Answer the question", "View the retrieved document only", "View the most similar ELI5 question and answer", "Show me everything, please!", ] SCREAMING_SNAKE_CASE__ : Optional[int] = st.sidebar.checkbox("Demo options") if demo_options: SCREAMING_SNAKE_CASE__ : Optional[int] = st.sidebar.selectbox( "", action_list, index=3, ) SCREAMING_SNAKE_CASE__ : Optional[Any] = action_list.index(action_st) SCREAMING_SNAKE_CASE__ : int = st.sidebar.selectbox( "", ["Show full text of passages", "Show passage section titles"], index=0, ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = show_type == "Show full text of passages" else: SCREAMING_SNAKE_CASE__ : Any = 3 SCREAMING_SNAKE_CASE__ : Any = True SCREAMING_SNAKE_CASE__ : Union[str, Any] = st.sidebar.checkbox("Retrieval options") if retrieval_options: SCREAMING_SNAKE_CASE__ : Tuple = "\n ### Information retriever options\n\n The **sparse** retriever uses ElasticSearch, while the **dense** retriever uses max-inner-product search between a question and passage embedding\n trained using the [ELI5](https://arxiv.org/abs/1907.09190) questions-answer pairs.\n The answer is then generated by sequence to sequence model which takes the question and retrieved document as input.\n " st.sidebar.markdown(retriever_info) SCREAMING_SNAKE_CASE__ : Union[str, Any] = st.sidebar.selectbox("Which Wikipedia format should the model use?", ["wiki40b", "none"]) SCREAMING_SNAKE_CASE__ : int = st.sidebar.selectbox("Which Wikipedia indexer should the model use?", ["dense", "sparse", "mixed"]) else: SCREAMING_SNAKE_CASE__ : List[str] = "wiki40b" SCREAMING_SNAKE_CASE__ : Optional[Any] = "dense" SCREAMING_SNAKE_CASE__ : str = "beam" SCREAMING_SNAKE_CASE__ : List[Any] = 2 SCREAMING_SNAKE_CASE__ : Optional[Any] = 64 SCREAMING_SNAKE_CASE__ : List[Any] = 256 SCREAMING_SNAKE_CASE__ : Union[str, Any] = None SCREAMING_SNAKE_CASE__ : Union[str, Any] = None SCREAMING_SNAKE_CASE__ : List[str] = st.sidebar.checkbox("Generation options") if generate_options: SCREAMING_SNAKE_CASE__ : Dict = "\n ### Answer generation options\n\n The sequence-to-sequence model was initialized with [BART](https://huggingface.co/facebook/bart-large)\n weights and fine-tuned on the ELI5 QA pairs and retrieved documents. You can use the model for greedy decoding with\n **beam** search, or **sample** from the decoder's output probabilities.\n " st.sidebar.markdown(generate_info) SCREAMING_SNAKE_CASE__ : List[str] = st.sidebar.selectbox("Would you like to use beam search or sample an answer?", ["beam", "sampled"]) SCREAMING_SNAKE_CASE__ : Any = st.sidebar.slider( "Minimum generation length", min_value=8, max_value=256, value=64, step=8, format=None, key=None ) SCREAMING_SNAKE_CASE__ : str = st.sidebar.slider( "Maximum generation length", min_value=64, max_value=512, value=256, step=16, format=None, key=None ) if sampled == "beam": SCREAMING_SNAKE_CASE__ : Union[str, Any] = st.sidebar.slider("Beam size", min_value=1, max_value=8, value=2, step=None, format=None, key=None) else: SCREAMING_SNAKE_CASE__ : Any = st.sidebar.slider( "Nucleus sampling p", min_value=0.1, max_value=1.0, value=0.9_5, step=0.0_1, format=None, key=None ) SCREAMING_SNAKE_CASE__ : Dict = st.sidebar.slider( "Temperature", min_value=0.1, max_value=1.0, value=0.7, step=0.0_1, format=None, key=None ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = None # start main text SCREAMING_SNAKE_CASE__ : Any = [ "<MY QUESTION>", "How do people make chocolate?", "Why do we get a fever when we are sick?", "How can different animals perceive different colors?", "What is natural language processing?", "What's the best way to treat a sunburn?", "What exactly are vitamins ?", "How does nuclear energy provide electricity?", "What's the difference between viruses and bacteria?", "Why are flutes classified as woodwinds when most of them are made out of metal ?", "Why do people like drinking coffee even though it tastes so bad?", "What happens when wine ages? How does it make the wine taste better?", "If an animal is an herbivore, where does it get the protein that it needs to survive if it only eats grass?", "How can we set a date to the beginning or end of an artistic period? Doesn't the change happen gradually?", "How does New Zealand have so many large bird predators?", ] SCREAMING_SNAKE_CASE__ : List[str] = st.selectbox( "What would you like to ask? ---- select <MY QUESTION> to enter a new query", questions_list, index=1, ) if question_s == "<MY QUESTION>": SCREAMING_SNAKE_CASE__ : Union[str, Any] = st.text_input("Enter your question here:", "") else: SCREAMING_SNAKE_CASE__ : str = question_s if st.button("Show me!"): if action in [0, 1, 3]: if index_type == "mixed": SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Union[str, Any] = make_support(question, source=wiki_source, method="dense", n_results=10) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : str = make_support(question, source=wiki_source, method="sparse", n_results=10) SCREAMING_SNAKE_CASE__ : int = [] for res_d, res_s in zip(support_list_dense, support_list_sparse): if tuple(res_d) not in support_list: support_list += [tuple(res_d)] if tuple(res_s) not in support_list: support_list += [tuple(res_s)] SCREAMING_SNAKE_CASE__ : Optional[Any] = support_list[:10] SCREAMING_SNAKE_CASE__ : Tuple = "<P> " + " <P> ".join([res[-1] for res in support_list]) else: SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Optional[int] = make_support(question, source=wiki_source, method=index_type, n_results=10) if action in [0, 3]: SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Optional[Any] = answer_question( question_doc, sas_model, sas_tokenizer, min_len=min_len, max_len=int(max_len), sampling=(sampled == "sampled"), n_beams=n_beams, top_p=top_p, temp=temp, ) st.markdown("### The model generated answer is:") st.write(answer) if action in [0, 1, 3] and wiki_source != "none": st.markdown("--- \n ### The model is drawing information from the following Wikipedia passages:") for i, res in enumerate(support_list): SCREAMING_SNAKE_CASE__ : Optional[int] = "https://en.wikipedia.org/wiki/{}".format(res[0].replace(" ", "_")) SCREAMING_SNAKE_CASE__ : Tuple = res[1].strip() if sec_titles == "": SCREAMING_SNAKE_CASE__ : Union[str, Any] = "[{}]({})".format(res[0], wiki_url) else: SCREAMING_SNAKE_CASE__ : Dict = sec_titles.split(" & ") SCREAMING_SNAKE_CASE__ : int = " & ".join( ["[{}]({}#{})".format(sec.strip(), wiki_url, sec.strip().replace(" ", "_")) for sec in sec_list] ) st.markdown( "{0:02d} - **Article**: {1:<18} <br> _Section_: {2}".format(i + 1, res[0], sections), unsafe_allow_html=True, ) if show_passages: st.write( "> <span style=\"font-family:arial; font-size:10pt;\">" + res[-1] + "</span>", unsafe_allow_html=True ) if action in [2, 3]: SCREAMING_SNAKE_CASE__ : Any = find_nearest_training(question) SCREAMING_SNAKE_CASE__ : List[Any] = nn_train_list[0] st.markdown( "--- \n ### The most similar question in the ELI5 training set was: \n\n {}".format(train_exple["title"]) ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = [ "{}. {}".format(i + 1, " \n".join([line.strip() for line in ans.split("\n") if line.strip() != ""])) for i, (ans, sc) in enumerate(zip(train_exple["answers"]["text"], train_exple["answers"]["score"])) if i == 0 or sc > 2 ] st.markdown("##### Its answers were: \n\n {}".format("\n".join(answers_st))) SCREAMING_SNAKE_CASE__ : List[Any] = "\n---\n\n**Disclaimer**\n\n*The intent of this app is to provide some (hopefully entertaining) insights into the behavior of a current LFQA system.\nEvaluating biases of such a model and ensuring factual generations are still very much open research problems.\nTherefore, until some significant progress is achieved, we caution against using the generated answers for practical purposes.*\n" st.sidebar.markdown(disclaimer, unsafe_allow_html=True)
339
1
"""simple docstring""" import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {'vocab_file': 'spiece.model'} UpperCAmelCase__ = { 'vocab_file': { 'albert-base-v1': 'https://huggingface.co/albert-base-v1/resolve/main/spiece.model', 'albert-large-v1': 'https://huggingface.co/albert-large-v1/resolve/main/spiece.model', 'albert-xlarge-v1': 'https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model', 'albert-xxlarge-v1': 'https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model', 'albert-base-v2': 'https://huggingface.co/albert-base-v2/resolve/main/spiece.model', 'albert-large-v2': 'https://huggingface.co/albert-large-v2/resolve/main/spiece.model', 'albert-xlarge-v2': 'https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model', 'albert-xxlarge-v2': 'https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model', } } UpperCAmelCase__ = { 'albert-base-v1': 512, 'albert-large-v1': 512, 'albert-xlarge-v1': 512, 'albert-xxlarge-v1': 512, 'albert-base-v2': 512, 'albert-large-v2': 512, 'albert-xlarge-v2': 512, 'albert-xxlarge-v2': 512, } UpperCAmelCase__ = '▁' class lowerCAmelCase__ ( A_ ): __a = VOCAB_FILES_NAMES __a = PRETRAINED_VOCAB_FILES_MAP __a = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self : Union[str, Any] , _lowerCamelCase : Any , _lowerCamelCase : List[str]=True , _lowerCamelCase : int=True , _lowerCamelCase : Tuple=False , _lowerCamelCase : str="[CLS]" , _lowerCamelCase : List[str]="[SEP]" , _lowerCamelCase : List[Any]="<unk>" , _lowerCamelCase : int="[SEP]" , _lowerCamelCase : Any="<pad>" , _lowerCamelCase : Any="[CLS]" , _lowerCamelCase : Tuple="[MASK]" , _lowerCamelCase : Optional[Dict[str, Any]] = None , **_lowerCamelCase : Any , ): # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. _snake_case = ( AddedToken(_lowerCamelCase , lstrip=_lowerCamelCase , rstrip=_lowerCamelCase , normalized=_lowerCamelCase ) if isinstance(_lowerCamelCase , _lowerCamelCase ) else mask_token ) _snake_case = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=_lowerCamelCase , remove_space=_lowerCamelCase , keep_accents=_lowerCamelCase , bos_token=_lowerCamelCase , eos_token=_lowerCamelCase , unk_token=_lowerCamelCase , sep_token=_lowerCamelCase , pad_token=_lowerCamelCase , cls_token=_lowerCamelCase , mask_token=_lowerCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_lowerCamelCase , ) _snake_case = do_lower_case _snake_case = remove_space _snake_case = keep_accents _snake_case = vocab_file _snake_case = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(_lowerCamelCase ) @property def lowercase ( self : Dict ): return len(self.sp_model ) def lowercase ( self : str ): _snake_case = {self.convert_ids_to_tokens(_lowerCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Tuple ): _snake_case = self.__dict__.copy() _snake_case = None return state def __setstate__( self : List[Any] , _lowerCamelCase : Optional[Any] ): _snake_case = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): _snake_case = {} _snake_case = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def lowercase ( self : Tuple , _lowerCamelCase : List[Any] ): if self.remove_space: _snake_case = ''' '''.join(inputs.strip().split() ) else: _snake_case = inputs _snake_case = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: _snake_case = unicodedata.normalize('''NFKD''' , _lowerCamelCase ) _snake_case = ''''''.join([c for c in outputs if not unicodedata.combining(_lowerCamelCase )] ) if self.do_lower_case: _snake_case = outputs.lower() return outputs def lowercase ( self : Union[str, Any] , _lowerCamelCase : str ): _snake_case = self.preprocess_text(_lowerCamelCase ) _snake_case = self.sp_model.encode(_lowerCamelCase , out_type=_lowerCamelCase ) _snake_case = [] for piece in pieces: if len(_lowerCamelCase ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): _snake_case = self.sp_model.EncodeAsPieces(piece[:-1].replace(_lowerCamelCase , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: _snake_case = cur_pieces[1:] else: _snake_case = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(_lowerCamelCase ) else: new_pieces.append(_lowerCamelCase ) return new_pieces def lowercase ( self : int , _lowerCamelCase : Any ): return self.sp_model.PieceToId(_lowerCamelCase ) def lowercase ( self : Optional[int] , _lowerCamelCase : str ): return self.sp_model.IdToPiece(_lowerCamelCase ) def lowercase ( self : str , _lowerCamelCase : List[str] ): _snake_case = [] _snake_case = '''''' _snake_case = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(_lowerCamelCase ) + token _snake_case = True _snake_case = [] else: current_sub_tokens.append(_lowerCamelCase ) _snake_case = False out_string += self.sp_model.decode(_lowerCamelCase ) return out_string.strip() def lowercase ( self : int , _lowerCamelCase : List[int] , _lowerCamelCase : Optional[List[int]] = None ): _snake_case = [self.sep_token_id] _snake_case = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def lowercase ( self : Dict , _lowerCamelCase : List[int] , _lowerCamelCase : Optional[List[int]] = None , _lowerCamelCase : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_lowerCamelCase , token_ids_a=_lowerCamelCase , already_has_special_tokens=_lowerCamelCase ) if token_ids_a is not None: return [1] + ([0] * len(_lowerCamelCase )) + [1] + ([0] * len(_lowerCamelCase )) + [1] return [1] + ([0] * len(_lowerCamelCase )) + [1] def lowercase ( self : int , _lowerCamelCase : List[int] , _lowerCamelCase : Optional[List[int]] = None ): _snake_case = [self.sep_token_id] _snake_case = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowercase ( self : Optional[int] , _lowerCamelCase : str , _lowerCamelCase : Optional[str] = None ): if not os.path.isdir(_lowerCamelCase ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return _snake_case = os.path.join( _lowerCamelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_lowerCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _lowerCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(_lowerCamelCase , '''wb''' ) as fi: _snake_case = self.sp_model.serialized_model_proto() fi.write(_lowerCamelCase ) return (out_vocab_file,)
288
"""simple docstring""" def _UpperCAmelCase ( __lowerCamelCase : int = 1_00_00_00 ) -> int: _snake_case = limit + 1 _snake_case = [0] * limit for first_term in range(1 , __lowerCamelCase ): for n in range(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ): _snake_case = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a _snake_case = sum(1 for x in frequency[1:limit] if x == 10 ) return count if __name__ == "__main__": print(F"{solution() = }")
288
1
from ...configuration_utils import PretrainedConfig from ...utils import logging __a = logging.get_logger(__name__) __a = { '''microsoft/swinv2-tiny-patch4-window8-256''': ( '''https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json''' ), } class __SCREAMING_SNAKE_CASE ( A__ ): A : int = 'swinv2' A : Dict = { 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self , SCREAMING_SNAKE_CASE__=224 , SCREAMING_SNAKE_CASE__=4 , SCREAMING_SNAKE_CASE__=3 , SCREAMING_SNAKE_CASE__=96 , SCREAMING_SNAKE_CASE__=[2, 2, 6, 2] , SCREAMING_SNAKE_CASE__=[3, 6, 12, 24] , SCREAMING_SNAKE_CASE__=7 , SCREAMING_SNAKE_CASE__=4.0 , SCREAMING_SNAKE_CASE__=True , SCREAMING_SNAKE_CASE__=0.0 , SCREAMING_SNAKE_CASE__=0.0 , SCREAMING_SNAKE_CASE__=0.1 , SCREAMING_SNAKE_CASE__="gelu" , SCREAMING_SNAKE_CASE__=False , SCREAMING_SNAKE_CASE__=0.02 , SCREAMING_SNAKE_CASE__=1E-5 , SCREAMING_SNAKE_CASE__=32 , **SCREAMING_SNAKE_CASE__ , ): super().__init__(**SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = image_size lowercase : str = patch_size lowercase : Optional[Any] = num_channels lowercase : List[str] = embed_dim lowercase : Tuple = depths lowercase : Any = len(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = num_heads lowercase : Optional[Any] = window_size lowercase : Optional[Any] = mlp_ratio lowercase : Any = qkv_bias lowercase : Any = hidden_dropout_prob lowercase : int = attention_probs_dropout_prob lowercase : Union[str, Any] = drop_path_rate lowercase : int = hidden_act lowercase : int = use_absolute_embeddings lowercase : Union[str, Any] = layer_norm_eps lowercase : Optional[int] = initializer_range lowercase : Optional[Any] = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model lowercase : Optional[int] = int(embed_dim * 2 ** (len(SCREAMING_SNAKE_CASE__ ) - 1) ) lowercase : Union[str, Any] = (0, 0, 0, 0)
173
import argparse import torch from torch import nn from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration def __lowercase ( _UpperCamelCase ) ->Tuple: """simple docstring""" lowercase : List[str] = [ '''encoder.version''', '''decoder.version''', '''model.encoder.version''', '''model.decoder.version''', '''decoder.output_projection.weight''', '''_float_tensor''', '''encoder.embed_positions._float_tensor''', '''decoder.embed_positions._float_tensor''', ] for k in ignore_keys: state_dict.pop(_UpperCamelCase, _UpperCamelCase ) def __lowercase ( _UpperCamelCase ) ->List[str]: """simple docstring""" lowercase , lowercase : str = emb.weight.shape lowercase : Optional[int] = nn.Linear(_UpperCamelCase, _UpperCamelCase, bias=_UpperCamelCase ) lowercase : Any = emb.weight.data return lin_layer def __lowercase ( _UpperCamelCase ) ->List[str]: """simple docstring""" lowercase : Optional[int] = torch.load(_UpperCamelCase, map_location='''cpu''' ) lowercase : List[str] = mam_aaa['''args'''] or mam_aaa['''cfg''']['''model'''] lowercase : int = mam_aaa['''model'''] remove_ignore_keys_(_UpperCamelCase ) lowercase : Any = state_dict['''encoder.embed_tokens.weight'''].shape[0] lowercase : Dict = MaMaaaConfig( vocab_size=_UpperCamelCase, max_position_embeddings=1024, encoder_layers=args.encoder_layers, decoder_layers=args.decoder_layers, encoder_attention_heads=args.encoder_attention_heads, decoder_attention_heads=args.decoder_attention_heads, encoder_ffn_dim=args.encoder_ffn_embed_dim, decoder_ffn_dim=args.decoder_ffn_embed_dim, d_model=args.encoder_embed_dim, encoder_layerdrop=args.encoder_layerdrop, decoder_layerdrop=args.decoder_layerdrop, dropout=args.dropout, attention_dropout=args.attention_dropout, activation_dropout=args.activation_dropout, activation_function='''relu''', ) lowercase : Union[str, Any] = state_dict['''decoder.embed_tokens.weight'''] lowercase : Dict = MaMaaaForConditionalGeneration(_UpperCamelCase ) model.model.load_state_dict(_UpperCamelCase, strict=_UpperCamelCase ) lowercase : Dict = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": __a = argparse.ArgumentParser() # Required parameters parser.add_argument('''fairseq_path''', type=str, help='''path to a model.pt on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') __a = parser.parse_args() __a = convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß) model.save_pretrained(args.pytorch_dump_folder_path)
173
1
import numpy # List of input, output pairs _lowerCamelCase : Tuple = ( ((5, 2, 3), 15), ((6, 5, 9), 25), ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41), ) _lowerCamelCase : List[Any] = (((515, 22, 13), 555), ((61, 35, 49), 150)) _lowerCamelCase : Union[str, Any] = [2, 4, 1, 5] _lowerCamelCase : Tuple = len(train_data) _lowerCamelCase : Tuple = 0.0_0_9 def a_ ( __lowercase : Optional[Any] , __lowercase : Tuple="train" ) -> Union[str, Any]: return calculate_hypothesis_value(__lowercase , __lowercase ) - output( __lowercase , __lowercase ) def a_ ( __lowercase : int ) -> Union[str, Any]: _snake_case = 0 for i in range(len(__lowercase ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def a_ ( __lowercase : Optional[Any] , __lowercase : Union[str, Any] ) -> int: if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def a_ ( __lowercase : List[Any] , __lowercase : Union[str, Any] ) -> Any: if data_set == "train": return _hypothesis_value(train_data[example_no][0] ) elif data_set == "test": return _hypothesis_value(test_data[example_no][0] ) return None def a_ ( __lowercase : str , __lowercase : List[Any]=m ) -> int: _snake_case = 0 for i in range(__lowercase ): if index == -1: summation_value += _error(__lowercase ) else: summation_value += _error(__lowercase ) * train_data[i][0][index] return summation_value def a_ ( __lowercase : List[str] ) -> Dict: _snake_case = summation_of_cost_derivative(__lowercase , __lowercase ) / m return cost_derivative_value def a_ ( ) -> Union[str, Any]: global parameter_vector # Tune these values to set a tolerance value for predicted output _snake_case = 0.0_0_0_0_0_2 _snake_case = 0 _snake_case = 0 while True: j += 1 _snake_case = [0, 0, 0, 0] for i in range(0 , len(__lowercase ) ): _snake_case = get_cost_derivative(i - 1 ) _snake_case = ( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( __lowercase , __lowercase , atol=__lowercase , rtol=__lowercase , ): break _snake_case = temp_parameter_vector print(('Number of iterations:', j) ) def a_ ( ) -> List[str]: for i in range(len(__lowercase ) ): print(('Actual output value:', output(__lowercase , 'test' )) ) print(('Hypothesis output:', calculate_hypothesis_value(__lowercase , 'test' )) ) if __name__ == "__main__": run_gradient_descent() print('''\nTesting gradient descent for a linear hypothesis function.\n''') test_gradient_descent()
282
import inspect import unittest from transformers import MobileNetVaConfig from transformers.testing_utils import require_torch, 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, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import MobileNetVaImageProcessor class SCREAMING_SNAKE_CASE__ ( UpperCAmelCase ): '''simple docstring''' def A ( self : List[str] ): '''simple docstring''' _snake_case = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(lowercase , 'tf_padding' ) ) self.parent.assertTrue(hasattr(lowercase , 'depth_multiplier' ) ) class SCREAMING_SNAKE_CASE__ : '''simple docstring''' def __init__( self : Dict , lowercase : List[str] , lowercase : Dict=13 , lowercase : Optional[int]=3 , lowercase : Any=32 , lowercase : Any=0.25 , lowercase : Union[str, Any]=8 , lowercase : List[Any]=8 , lowercase : List[Any]=6 , lowercase : Dict=32 , lowercase : Dict=True , lowercase : Optional[Any]=True , lowercase : Tuple=True , lowercase : Tuple="relu6" , lowercase : List[Any]=1_280 , lowercase : Optional[Any]=0.1 , lowercase : int=0.02 , lowercase : Optional[Any]=True , lowercase : List[str]=True , lowercase : List[str]=10 , lowercase : Optional[Any]=None , ): '''simple docstring''' _snake_case = parent _snake_case = batch_size _snake_case = num_channels _snake_case = image_size _snake_case = depth_multiplier _snake_case = depth_divisible_by _snake_case = min_depth _snake_case = expand_ratio _snake_case = tf_padding _snake_case = output_stride _snake_case = first_layer_is_expansion _snake_case = finegrained_output _snake_case = hidden_act _snake_case = last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier ) _snake_case = classifier_dropout_prob _snake_case = use_labels _snake_case = is_training _snake_case = num_labels _snake_case = initializer_range _snake_case = scope def A ( self : Union[str, Any] ): '''simple docstring''' _snake_case = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _snake_case = None _snake_case = None if self.use_labels: _snake_case = ids_tensor([self.batch_size] , self.num_labels ) _snake_case = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) _snake_case = self.get_config() return config, pixel_values, labels, pixel_labels def A ( self : str ): '''simple docstring''' return MobileNetVaConfig( num_channels=self.num_channels , image_size=self.image_size , depth_multiplier=self.depth_multiplier , depth_divisible_by=self.depth_divisible_by , min_depth=self.min_depth , expand_ratio=self.expand_ratio , output_stride=self.output_stride , first_layer_is_expansion=self.first_layer_is_expansion , finegrained_output=self.finegrained_output , hidden_act=self.hidden_act , tf_padding=self.tf_padding , classifier_dropout_prob=self.classifier_dropout_prob , initializer_range=self.initializer_range , ) def A ( self : Optional[Any] , lowercase : str , lowercase : List[str] , lowercase : str , lowercase : Dict ): '''simple docstring''' _snake_case = MobileNetVaModel(config=lowercase ) model.to(lowercase ) model.eval() _snake_case = model(lowercase ) self.parent.assertEqual( result.last_hidden_state.shape , ( self.batch_size, self.last_hidden_size, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) self.parent.assertEqual( result.pooler_output.shape , (self.batch_size, self.last_hidden_size) , ) def A ( self : List[Any] , lowercase : Optional[int] , lowercase : Optional[int] , lowercase : Optional[Any] , lowercase : List[Any] ): '''simple docstring''' _snake_case = self.num_labels _snake_case = MobileNetVaForImageClassification(lowercase ) model.to(lowercase ) model.eval() _snake_case = model(lowercase , labels=lowercase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def A ( self : Any , lowercase : int , lowercase : Dict , lowercase : int , lowercase : List[Any] ): '''simple docstring''' _snake_case = self.num_labels _snake_case = MobileNetVaForSemanticSegmentation(lowercase ) model.to(lowercase ) model.eval() _snake_case = model(lowercase ) self.parent.assertEqual( result.logits.shape , ( self.batch_size, self.num_labels, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) _snake_case = model(lowercase , labels=lowercase ) self.parent.assertEqual( result.logits.shape , ( self.batch_size, self.num_labels, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) def A ( self : str ): '''simple docstring''' _snake_case = self.prepare_config_and_inputs() _snake_case , _snake_case , _snake_case , _snake_case = config_and_inputs _snake_case = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class SCREAMING_SNAKE_CASE__ ( UpperCAmelCase ,UpperCAmelCase ,unittest.TestCase ): '''simple docstring''' _UpperCAmelCase : str = ( (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation) if is_torch_available() else () ) _UpperCAmelCase : str = ( { "feature-extraction": MobileNetVaModel, "image-classification": MobileNetVaForImageClassification, "image-segmentation": MobileNetVaForSemanticSegmentation, } if is_torch_available() else {} ) _UpperCAmelCase : Optional[int] = False _UpperCAmelCase : Dict = False _UpperCAmelCase : Dict = False _UpperCAmelCase : Union[str, Any] = False def A ( self : Any ): '''simple docstring''' _snake_case = MobileNetVaModelTester(self ) _snake_case = MobileNetVaConfigTester(self , config_class=lowercase , has_text_modality=lowercase ) def A ( self : Any ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='MobileNetV2 does not use inputs_embeds' ) def A ( self : List[str] ): '''simple docstring''' pass @unittest.skip(reason='MobileNetV2 does not support input and output embeddings' ) def A ( self : int ): '''simple docstring''' pass @unittest.skip(reason='MobileNetV2 does not output attentions' ) def A ( self : Any ): '''simple docstring''' pass def A ( self : Optional[int] ): '''simple docstring''' _snake_case , _snake_case = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _snake_case = model_class(lowercase ) _snake_case = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _snake_case = [*signature.parameters.keys()] _snake_case = ['pixel_values'] self.assertListEqual(arg_names[:1] , lowercase ) def A ( self : List[str] ): '''simple docstring''' _snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowercase ) def A ( self : List[Any] ): '''simple docstring''' def check_hidden_states_output(lowercase : List[Any] , lowercase : Union[str, Any] , lowercase : str ): _snake_case = model_class(lowercase ) model.to(lowercase ) model.eval() with torch.no_grad(): _snake_case = model(**self._prepare_for_class(lowercase , lowercase ) ) _snake_case = outputs.hidden_states _snake_case = 16 self.assertEqual(len(lowercase ) , lowercase ) _snake_case , _snake_case = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _snake_case = True check_hidden_states_output(lowercase , lowercase , lowercase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _snake_case = True check_hidden_states_output(lowercase , lowercase , lowercase ) def A ( self : Tuple ): '''simple docstring''' _snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowercase ) def A ( self : Dict ): '''simple docstring''' _snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*lowercase ) @slow def A ( self : List[Any] ): '''simple docstring''' for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _snake_case = MobileNetVaModel.from_pretrained(lowercase ) self.assertIsNotNone(lowercase ) def a_ ( ) -> Union[str, Any]: _snake_case = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): '''simple docstring''' @cached_property def A ( self : Optional[Any] ): '''simple docstring''' return ( MobileNetVaImageProcessor.from_pretrained('google/mobilenet_v2_1.0_224' ) if is_vision_available() else None ) @slow def A ( self : List[Any] ): '''simple docstring''' _snake_case = MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v2_1.0_224' ).to(lowercase ) _snake_case = self.default_image_processor _snake_case = prepare_img() _snake_case = image_processor(images=lowercase , return_tensors='pt' ).to(lowercase ) # forward pass with torch.no_grad(): _snake_case = model(**lowercase ) # verify the logits _snake_case = torch.Size((1, 1_001) ) self.assertEqual(outputs.logits.shape , lowercase ) _snake_case = torch.tensor([0.2445, -1.1993, 0.1905] ).to(lowercase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowercase , atol=1E-4 ) ) @slow def A ( self : Dict ): '''simple docstring''' _snake_case = MobileNetVaForSemanticSegmentation.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513' ) _snake_case = model.to(lowercase ) _snake_case = MobileNetVaImageProcessor.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513' ) _snake_case = prepare_img() _snake_case = image_processor(images=lowercase , return_tensors='pt' ).to(lowercase ) # forward pass with torch.no_grad(): _snake_case = model(**lowercase ) _snake_case = outputs.logits # verify the logits _snake_case = torch.Size((1, 21, 65, 65) ) self.assertEqual(logits.shape , lowercase ) _snake_case = torch.tensor( [ [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]], [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]], [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]], ] , device=lowercase , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , lowercase , atol=1E-4 ) )
282
1
import unittest from diffusers import FlaxAutoencoderKL from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import require_flax from .test_modeling_common_flax import FlaxModelTesterMixin if is_flax_available(): import jax @require_flax class __SCREAMING_SNAKE_CASE ( snake_case__ , unittest.TestCase ): snake_case : Any = FlaxAutoencoderKL @property def _lowerCamelCase ( self ): UpperCamelCase__ = 4 UpperCamelCase__ = 3 UpperCamelCase__ = (32, 32) UpperCamelCase__ = jax.random.PRNGKey(0 ) UpperCamelCase__ = jax.random.uniform(_A , ((batch_size, num_channels) + sizes) ) return {"sample": image, "prng_key": prng_key} def _lowerCamelCase ( self ): UpperCamelCase__ = { """block_out_channels""": [32, 64], """in_channels""": 3, """out_channels""": 3, """down_block_types""": ["""DownEncoderBlock2D""", """DownEncoderBlock2D"""], """up_block_types""": ["""UpDecoderBlock2D""", """UpDecoderBlock2D"""], """latent_channels""": 4, } UpperCamelCase__ = self.dummy_input return init_dict, inputs_dict
363
import json import os import torch from diffusers import UNetaDModel os.makedirs("hub/hopper-medium-v2/unet/hor32", exist_ok=True) os.makedirs("hub/hopper-medium-v2/unet/hor128", exist_ok=True) os.makedirs("hub/hopper-medium-v2/value_function", exist_ok=True) def _UpperCamelCase (a__ :int ): """simple docstring""" if hor == 128: UpperCamelCase__ = ("""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""") UpperCamelCase__ = (32, 128, 256) UpperCamelCase__ = ("""UpResnetBlock1D""", """UpResnetBlock1D""") elif hor == 32: UpperCamelCase__ = ("""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""") UpperCamelCase__ = (32, 64, 128, 256) UpperCamelCase__ = ("""UpResnetBlock1D""", """UpResnetBlock1D""", """UpResnetBlock1D""") UpperCamelCase__ = torch.load(f"""/Users/bglickenhaus/Documents/diffuser/temporal_unet-hopper-mediumv2-hor{hor}.torch""" ) UpperCamelCase__ = model.state_dict() UpperCamelCase__ = { """down_block_types""": down_block_types, """block_out_channels""": block_out_channels, """up_block_types""": up_block_types, """layers_per_block""": 1, """use_timestep_embedding""": True, """out_block_type""": """OutConv1DBlock""", """norm_num_groups""": 8, """downsample_each_block""": False, """in_channels""": 14, """out_channels""": 14, """extra_in_channels""": 0, """time_embedding_type""": """positional""", """flip_sin_to_cos""": False, """freq_shift""": 1, """sample_size""": 6_5536, """mid_block_type""": """MidResTemporalBlock1D""", """act_fn""": """mish""", } UpperCamelCase__ = UNetaDModel(**a__ ) print(f"""length of state dict: {len(state_dict.keys() )}""" ) print(f"""length of value function dict: {len(hf_value_function.state_dict().keys() )}""" ) UpperCamelCase__ = dict(zip(model.state_dict().keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): UpperCamelCase__ = state_dict.pop(a__ ) hf_value_function.load_state_dict(a__ ) torch.save(hf_value_function.state_dict() , f"""hub/hopper-medium-v2/unet/hor{hor}/diffusion_pytorch_model.bin""" ) with open(f"""hub/hopper-medium-v2/unet/hor{hor}/config.json""" , """w""" ) as f: json.dump(a__ , a__ ) def _UpperCamelCase (): """simple docstring""" UpperCamelCase__ = { """in_channels""": 14, """down_block_types""": ("""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D"""), """up_block_types""": (), """out_block_type""": """ValueFunction""", """mid_block_type""": """ValueFunctionMidBlock1D""", """block_out_channels""": (32, 64, 128, 256), """layers_per_block""": 1, """downsample_each_block""": True, """sample_size""": 6_5536, """out_channels""": 14, """extra_in_channels""": 0, """time_embedding_type""": """positional""", """use_timestep_embedding""": True, """flip_sin_to_cos""": False, """freq_shift""": 1, """norm_num_groups""": 8, """act_fn""": """mish""", } UpperCamelCase__ = torch.load("""/Users/bglickenhaus/Documents/diffuser/value_function-hopper-mediumv2-hor32.torch""" ) UpperCamelCase__ = model UpperCamelCase__ = UNetaDModel(**a__ ) print(f"""length of state dict: {len(state_dict.keys() )}""" ) print(f"""length of value function dict: {len(hf_value_function.state_dict().keys() )}""" ) UpperCamelCase__ = dict(zip(state_dict.keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): UpperCamelCase__ = state_dict.pop(a__ ) hf_value_function.load_state_dict(a__ ) torch.save(hf_value_function.state_dict() , """hub/hopper-medium-v2/value_function/diffusion_pytorch_model.bin""" ) with open("""hub/hopper-medium-v2/value_function/config.json""" , """w""" ) as f: json.dump(a__ , a__ ) if __name__ == "__main__": unet(32) # unet(128) value_function()
87
0
import argparse import os 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_task_guides.py __snake_case : Optional[int] = """src/transformers""" __snake_case : List[str] = """docs/source/en/tasks""" def _UpperCAmelCase ( a__ , a__ , a__): '''simple docstring''' with open(a__ , """r""" , encoding="""utf-8""" , newline="""\n""") as f: a_ : int = f.readlines() # Find the start prompt. a_ : Any = 0 while not lines[start_index].startswith(a__): start_index += 1 start_index += 1 a_ : Tuple = start_index while not lines[end_index].startswith(a__): 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 # This is to make sure the transformers module imported is the one in the repo. __snake_case : Tuple = direct_transformers_import(TRANSFORMERS_PATH) __snake_case : Optional[int] = { """asr.md""": transformers_module.models.auto.modeling_auto.MODEL_FOR_CTC_MAPPING_NAMES, """audio_classification.md""": transformers_module.models.auto.modeling_auto.MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, """language_modeling.md""": transformers_module.models.auto.modeling_auto.MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, """image_classification.md""": transformers_module.models.auto.modeling_auto.MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, """masked_language_modeling.md""": transformers_module.models.auto.modeling_auto.MODEL_FOR_MASKED_LM_MAPPING_NAMES, """multiple_choice.md""": transformers_module.models.auto.modeling_auto.MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES, """object_detection.md""": transformers_module.models.auto.modeling_auto.MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, """question_answering.md""": transformers_module.models.auto.modeling_auto.MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, """semantic_segmentation.md""": transformers_module.models.auto.modeling_auto.MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES, """sequence_classification.md""": transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, """summarization.md""": transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, """token_classification.md""": transformers_module.models.auto.modeling_auto.MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, """translation.md""": transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, """video_classification.md""": transformers_module.models.auto.modeling_auto.MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMES, """document_question_answering.md""": transformers_module.models.auto.modeling_auto.MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES, """monocular_depth_estimation.md""": transformers_module.models.auto.modeling_auto.MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES, } # This list contains model types used in some task guides that are not in `CONFIG_MAPPING_NAMES` (therefore not in any # `MODEL_MAPPING_NAMES` or any `MODEL_FOR_XXX_MAPPING_NAMES`). __snake_case : Union[str, Any] = { """summarization.md""": ("""nllb""",), """translation.md""": ("""nllb""",), } def _UpperCAmelCase ( a__): '''simple docstring''' a_ : List[Any] = TASK_GUIDE_TO_MODELS[task_guide] a_ : Union[str, Any] = SPECIAL_TASK_GUIDE_TO_MODEL_TYPES.get(a__ , set()) a_ : Optional[int] = { code: name for code, name in transformers_module.MODEL_NAMES_MAPPING.items() if (code in model_maping_names or code in special_model_types) } return ", ".join([f'''[{name}](../model_doc/{code})''' for code, name in model_names.items()]) + "\n" def _UpperCAmelCase ( a__ , a__=False): '''simple docstring''' a_ , a_ , a_ , a_ : Any = _find_text_in_file( filename=os.path.join(a__ , a__) , start_prompt="""<!--This tip is automatically generated by `make fix-copies`, do not fill manually!-->""" , end_prompt="""<!--End of the generated tip-->""" , ) a_ : List[str] = get_model_list_for_task(a__) if current_list != new_list: if overwrite: with open(os.path.join(a__ , a__) , """w""" , encoding="""utf-8""" , newline="""\n""") as f: f.writelines(lines[:start_index] + [new_list] + lines[end_index:]) else: raise ValueError( f'''The list of models that can be used in the {task_guide} guide needs an update. Run `make fix-copies`''' """ to fix this.""") if __name__ == "__main__": __snake_case : Tuple = argparse.ArgumentParser() parser.add_argument("""--fix_and_overwrite""", action="""store_true""", help="""Whether to fix inconsistencies.""") __snake_case : Dict = parser.parse_args() for task_guide in TASK_GUIDE_TO_MODELS.keys(): check_model_list_for_task(task_guide, args.fix_and_overwrite)
248
import itertools import os import random import tempfile import unittest import numpy as np from datasets import load_dataset from transformers import is_speech_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_speech_available(): from transformers import WhisperFeatureExtractor if is_torch_available(): import torch __snake_case : Tuple = random.Random() def _UpperCAmelCase ( a__ , a__=1.0 , a__=None , a__=None): '''simple docstring''' if rng is None: a_ : Optional[int] = global_rng a_ : int = [] for batch_idx in range(shape[0]): values.append([]) for _ in range(shape[1]): values[-1].append(rng.random() * scale) return values @require_torch @require_torchaudio class A__(unittest.TestCase ): """simple docstring""" def __init__( self , _lowercase , _lowercase=7 , _lowercase=400 , _lowercase=2_000 , _lowercase=10 , _lowercase=160 , _lowercase=8 , _lowercase=0.0 , _lowercase=4_000 , _lowercase=False , _lowercase=True , ) -> Dict: a_ : Any = parent a_ : Any = batch_size a_ : Union[str, Any] = min_seq_length a_ : Any = max_seq_length a_ : Dict = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) a_ : Dict = padding_value a_ : Dict = sampling_rate a_ : Optional[Any] = return_attention_mask a_ : Dict = do_normalize a_ : Any = feature_size a_ : List[str] = chunk_length a_ : Tuple = hop_length def UpperCamelCase__ ( self ) -> Dict: return { "feature_size": self.feature_size, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def UpperCamelCase__ ( self , _lowercase=False , _lowercase=False ) -> Tuple: def _flatten(_lowercase ): return list(itertools.chain(*_lowercase ) ) if equal_length: a_ : Union[str, Any] = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size a_ : Any = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: a_ : Optional[Any] = [np.asarray(_lowercase ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class A__(a_, unittest.TestCase ): """simple docstring""" _A : List[str] = WhisperFeatureExtractor if is_speech_available() else None def UpperCamelCase__ ( self ) -> Tuple: a_ : Any = WhisperFeatureExtractionTester(self ) def UpperCamelCase__ ( self ) -> Tuple: a_ : Union[str, Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: a_ : Optional[int] = feat_extract_first.save_pretrained(_lowercase )[0] check_json_file_has_correct_format(_lowercase ) a_ : Optional[Any] = self.feature_extraction_class.from_pretrained(_lowercase ) a_ : Optional[int] = feat_extract_first.to_dict() a_ : Union[str, Any] = feat_extract_second.to_dict() a_ : List[str] = feat_extract_first.mel_filters a_ : Optional[int] = feat_extract_second.mel_filters self.assertTrue(np.allclose(_lowercase , _lowercase ) ) self.assertEqual(_lowercase , _lowercase ) def UpperCamelCase__ ( self ) -> Optional[int]: a_ : List[str] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: a_ : int = os.path.join(_lowercase , """feat_extract.json""" ) feat_extract_first.to_json_file(_lowercase ) a_ : str = self.feature_extraction_class.from_json_file(_lowercase ) a_ : Dict = feat_extract_first.to_dict() a_ : Tuple = feat_extract_second.to_dict() a_ : Dict = feat_extract_first.mel_filters a_ : Union[str, Any] = feat_extract_second.mel_filters self.assertTrue(np.allclose(_lowercase , _lowercase ) ) self.assertEqual(_lowercase , _lowercase ) def UpperCamelCase__ ( self ) -> List[str]: # Tests that all call wrap to encode_plus and batch_encode_plus a_ : Any = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 a_ : int = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] a_ : List[str] = [np.asarray(_lowercase ) for speech_input in speech_inputs] # Test feature size a_ : str = feature_extractor(_lowercase , padding="""max_length""" , return_tensors="""np""" ).input_features self.assertTrue(input_features.ndim == 3 ) self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames ) self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size ) # Test not batched input a_ : Any = feature_extractor(speech_inputs[0] , return_tensors="""np""" ).input_features a_ : Any = feature_extractor(np_speech_inputs[0] , return_tensors="""np""" ).input_features self.assertTrue(np.allclose(_lowercase , _lowercase , atol=1e-3 ) ) # Test batched a_ : str = feature_extractor(_lowercase , return_tensors="""np""" ).input_features a_ : str = feature_extractor(_lowercase , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_lowercase , _lowercase ): self.assertTrue(np.allclose(_lowercase , _lowercase , atol=1e-3 ) ) # Test 2-D numpy arrays are batched. a_ : Tuple = [floats_list((1, x) )[0] for x in (800, 800, 800)] a_ : List[str] = np.asarray(_lowercase ) a_ : Optional[Any] = feature_extractor(_lowercase , return_tensors="""np""" ).input_features a_ : Dict = feature_extractor(_lowercase , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_lowercase , _lowercase ): self.assertTrue(np.allclose(_lowercase , _lowercase , atol=1e-3 ) ) # Test truncation required a_ : Optional[int] = [floats_list((1, x) )[0] for x in range(200 , (feature_extractor.n_samples + 500) , 200 )] a_ : str = [np.asarray(_lowercase ) for speech_input in speech_inputs] a_ : List[Any] = [x[: feature_extractor.n_samples] for x in speech_inputs] a_ : Any = [np.asarray(_lowercase ) for speech_input in speech_inputs_truncated] a_ : Union[str, Any] = feature_extractor(_lowercase , return_tensors="""np""" ).input_features a_ : str = feature_extractor(_lowercase , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_lowercase , _lowercase ): self.assertTrue(np.allclose(_lowercase , _lowercase , atol=1e-3 ) ) def UpperCamelCase__ ( self ) -> Dict: import torch a_ : Optional[int] = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) a_ : List[str] = np.random.rand(100 , 32 ).astype(np.floataa ) a_ : Tuple = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: a_ : int = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""np""" ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) a_ : Dict = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""pt""" ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def UpperCamelCase__ ( self , _lowercase ) -> Dict: a_ : Any = load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) # automatic decoding with librispeech a_ : Union[str, Any] = ds.sort("""id""" ).select(range(_lowercase ) )[:num_samples]["""audio"""] return [x["array"] for x in speech_samples] def UpperCamelCase__ ( self ) -> int: # fmt: off a_ : Union[str, Any] = torch.tensor( [ 0.1_1_9_3, -0.0_9_4_6, -0.1_0_9_8, -0.0_1_9_6, 0.0_2_2_5, -0.0_6_9_0, -0.1_7_3_6, 0.0_9_5_1, 0.0_9_7_1, -0.0_8_1_7, -0.0_7_0_2, 0.0_1_6_2, 0.0_2_6_0, 0.0_0_1_7, -0.0_1_9_2, -0.1_6_7_8, 0.0_7_0_9, -0.1_8_6_7, -0.0_6_5_5, -0.0_2_7_4, -0.0_2_3_4, -0.1_8_8_4, -0.0_5_1_6, -0.0_5_5_4, -0.0_2_7_4, -0.1_4_2_5, -0.1_4_2_3, 0.0_8_3_7, 0.0_3_7_7, -0.0_8_5_4 ] ) # fmt: on a_ : List[str] = self._load_datasamples(1 ) a_ : List[Any] = WhisperFeatureExtractor() a_ : Union[str, Any] = feature_extractor(_lowercase , return_tensors="""pt""" ).input_features self.assertEqual(input_features.shape , (1, 80, 3_000) ) self.assertTrue(torch.allclose(input_features[0, 0, :30] , _lowercase , atol=1e-4 ) ) def UpperCamelCase__ ( self ) -> Union[str, Any]: a_ : Any = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) a_ : Any = self._load_datasamples(1 )[0] a_ : List[str] = ((audio - audio.min()) / (audio.max() - audio.min())) * 65_535 # Rescale to [0, 65535] to show issue a_ : Dict = feat_extract.zero_mean_unit_var_norm([audio] , attention_mask=_lowercase )[0] self.assertTrue(np.all(np.mean(_lowercase ) < 1e-3 ) ) self.assertTrue(np.all(np.abs(np.var(_lowercase ) - 1 ) < 1e-3 ) )
248
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a__: str = logging.get_logger(__name__) a__: Optional[Any] = { 'google/vit-base-patch16-224': 'https://huggingface.co/vit-base-patch16-224/resolve/main/config.json', # See all ViT models at https://huggingface.co/models?filter=vit } class SCREAMING_SNAKE_CASE__ ( __UpperCamelCase ): __SCREAMING_SNAKE_CASE = "vit" def __init__( self,__lowerCamelCase=768,__lowerCamelCase=12,__lowerCamelCase=12,__lowerCamelCase=3072,__lowerCamelCase="gelu",__lowerCamelCase=0.0,__lowerCamelCase=0.0,__lowerCamelCase=0.02,__lowerCamelCase=1E-12,__lowerCamelCase=224,__lowerCamelCase=16,__lowerCamelCase=3,__lowerCamelCase=True,__lowerCamelCase=16,**__lowerCamelCase,): super().__init__(**_lowerCAmelCase ) A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = initializer_range A__ = layer_norm_eps A__ = image_size A__ = patch_size A__ = num_channels A__ = qkv_bias A__ = encoder_stride class SCREAMING_SNAKE_CASE__ ( __UpperCamelCase ): __SCREAMING_SNAKE_CASE = version.parse('''1.11''' ) @property def UpperCamelCase ( self ): return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def UpperCamelCase ( self ): return 1E-4
370
from __future__ import annotations import time import numpy as np a__: Optional[Any] = [8, 5, 9, 7] a__: Dict = [ [2, 0, 1, 1], [0, 1, 2, 1], [4, 0, 0, 3], [0, 2, 1, 0], [1, 0, 3, 0], ] a__: List[Any] = [ [3, 2, 1, 4], [0, 2, 5, 2], [5, 1, 0, 5], [1, 5, 3, 0], [3, 0, 3, 3], ] class SCREAMING_SNAKE_CASE__ : def __init__( self,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,): A__ = claim_vector A__ = allocated_resources_table A__ = maximum_claim_table def UpperCamelCase ( self ): return [ sum(p_item[i] for p_item in self.__allocated_resources_table ) for i in range(len(self.__allocated_resources_table[0] ) ) ] def UpperCamelCase ( self ): return np.array(self.__claim_vector ) - np.array( self.__processes_resource_summation() ) def UpperCamelCase ( self ): return [ list(np.array(self.__maximum_claim_table[i] ) - np.array(__lowerCamelCase ) ) for i, allocated_resource in enumerate(self.__allocated_resources_table ) ] def UpperCamelCase ( self ): return {self.__need().index(__lowerCamelCase ): i for i in self.__need()} def UpperCamelCase ( self,**__lowerCamelCase ): A__ = self.__need() A__ = self.__allocated_resources_table A__ = self.__available_resources() A__ = self.__need_index_manager() for kw, val in kwargs.items(): if kw and val is True: self.__pretty_data() print('''_''' * 50 + '''\n''' ) while need_list: A__ = False for each_need in need_list: A__ = True for index, need in enumerate(__lowerCamelCase ): if need > available_resources[index]: A__ = False break if execution: A__ = True # get the original index of the process from ind_ctrl db for original_need_index, need_clone in need_index_manager.items(): if each_need == need_clone: A__ = original_need_index print(f"Process {process_number + 1} is executing." ) # remove the process run from stack need_list.remove(__lowerCamelCase ) # update available/freed resources stack A__ = np.array(__lowerCamelCase ) + np.array( alloc_resources_table[process_number] ) print( '''Updated available resource stack for processes: ''' + ''' '''.join([str(__lowerCamelCase ) for x in available_resources] ) ) break if safe: print('''The process is in a safe state.\n''' ) else: print('''System in unsafe state. Aborting...\n''' ) break def UpperCamelCase ( self ): print(''' ''' * 9 + '''Allocated Resource Table''' ) for item in self.__allocated_resources_table: print( f"P{self.__allocated_resources_table.index(__lowerCamelCase ) + 1}" + ''' '''.join(f"{it:>8}" for it in item ) + '''\n''' ) print(''' ''' * 9 + '''System Resource Table''' ) for item in self.__maximum_claim_table: print( f"P{self.__maximum_claim_table.index(__lowerCamelCase ) + 1}" + ''' '''.join(f"{it:>8}" for it in item ) + '''\n''' ) print( '''Current Usage by Active Processes: ''' + ''' '''.join(str(__lowerCamelCase ) for x in self.__claim_vector ) ) print( '''Initial Available Resources: ''' + ''' '''.join(str(__lowerCamelCase ) for x in self.__available_resources() ) ) time.sleep(1 ) if __name__ == "__main__": import doctest doctest.testmod()
39
0
import warnings from ..trainer import Trainer from ..utils import logging lowerCamelCase : Any = logging.get_logger(__name__) class lowerCAmelCase ( UpperCamelCase_ ): '''simple docstring''' def __init__( self : Optional[Any] , __a : Dict=None , **__a : List[Any] ) -> Optional[int]: """simple docstring""" warnings.warn( """`SageMakerTrainer` is deprecated and will be removed in v5 of Transformers. You can use `Trainer` """ """instead.""" , __SCREAMING_SNAKE_CASE , ) super().__init__(args=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )
233
def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> str: if isinstance(snake_case__ , snake_case__ ): raise TypeError('''\'float\' object cannot be interpreted as an integer''' ) if isinstance(snake_case__ , snake_case__ ): raise TypeError('''\'str\' object cannot be interpreted as an integer''' ) if num == 0: return "0b0" lowerCAmelCase = False if num < 0: lowerCAmelCase = True lowerCAmelCase = -num lowerCAmelCase = [] while num > 0: binary.insert(0 , num % 2 ) num >>= 1 if negative: return "-0b" + "".join(str(snake_case__ ) for e in binary ) return "0b" + "".join(str(snake_case__ ) for e in binary ) if __name__ == "__main__": import doctest doctest.testmod()
338
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''uclanlp/visualbert-vqa''': '''https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json''', '''uclanlp/visualbert-vqa-pre''': '''https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json''', '''uclanlp/visualbert-vqa-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json''' ), '''uclanlp/visualbert-vcr''': '''https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json''', '''uclanlp/visualbert-vcr-pre''': '''https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json''', '''uclanlp/visualbert-vcr-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json''' ), '''uclanlp/visualbert-nlvr2''': '''https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json''', '''uclanlp/visualbert-nlvr2-pre''': '''https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json''', '''uclanlp/visualbert-nlvr2-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json''' ) # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert } class _lowerCamelCase ( _lowercase ): UpperCAmelCase_ = "visual_bert" def __init__(self , __a=3_05_22 , __a=7_68 , __a=5_12 , __a=12 , __a=12 , __a=30_72 , __a="gelu" , __a=0.1 , __a=0.1 , __a=5_12 , __a=2 , __a=0.02 , __a=1e-1_2 , __a=False , __a=True , __a=1 , __a=0 , __a=2 , **__a , ) -> int: super().__init__(pad_token_id=__a , bos_token_id=__a , eos_token_id=__a , **__a ) UpperCamelCase = vocab_size UpperCamelCase = max_position_embeddings UpperCamelCase = hidden_size UpperCamelCase = visual_embedding_dim UpperCamelCase = num_hidden_layers UpperCamelCase = num_attention_heads UpperCamelCase = intermediate_size UpperCamelCase = hidden_act UpperCamelCase = hidden_dropout_prob UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = initializer_range UpperCamelCase = type_vocab_size UpperCamelCase = layer_norm_eps UpperCamelCase = bypass_transformer UpperCamelCase = special_visual_initialize
244
"""simple docstring""" def a__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): """simple docstring""" if len(_SCREAMING_SNAKE_CASE ) != len(_SCREAMING_SNAKE_CASE ): raise ValueError("The length of profit and weight must be same." ) if max_weight <= 0: raise ValueError("max_weight must greater than zero." ) if any(p < 0 for p in profit ): raise ValueError("Profit can not be negative." ) if any(w < 0 for w in weight ): raise ValueError("Weight can not be negative." ) # List created to store profit gained for the 1kg in case of each weight # respectively. Calculate and append profit/weight for each element. UpperCamelCase = [p / w for p, w in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )] # Creating a copy of the list and sorting profit/weight in ascending order UpperCamelCase = sorted(_SCREAMING_SNAKE_CASE ) # declaring useful variables UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) UpperCamelCase = 0 UpperCamelCase = 0 UpperCamelCase = 0 # loop till the total weight do not reach max limit e.g. 15 kg and till i<length while limit <= max_weight and i < length: # flag value for encountered greatest element in sorted_profit_by_weight UpperCamelCase = sorted_profit_by_weight[length - i - 1] UpperCamelCase = profit_by_weight.index(_SCREAMING_SNAKE_CASE ) UpperCamelCase = -1 # check if the weight encountered is less than the total weight # encountered before. if max_weight - limit >= weight[index]: limit += weight[index] # Adding profit gained for the given weight 1 === # weight[index]/weight[index] gain += 1 * profit[index] else: # Since the weight encountered is greater than limit, therefore take the # required number of remaining kgs and calculate profit for it. # weight remaining / weight[index] gain += (max_weight - limit) / weight[index] * profit[index] break i += 1 return gain if __name__ == "__main__": print( '''Input profits, weights, and then max_weight (all positive ints) separated by ''' '''spaces.''' ) lowerCAmelCase__ = [int(x) for x in input('''Input profits separated by spaces: ''').split()] lowerCAmelCase__ = [int(x) for x in input('''Input weights separated by spaces: ''').split()] lowerCAmelCase__ = int(input('''Max weight allowed: ''')) # Function Call calc_profit(profit, weight, max_weight)
244
1
"""simple docstring""" from argparse import ArgumentParser from datasets.commands.convert import ConvertCommand from datasets.commands.dummy_data import DummyDataCommand from datasets.commands.env import EnvironmentCommand from datasets.commands.run_beam import RunBeamCommand from datasets.commands.test import TestCommand from datasets.utils.logging import set_verbosity_info def __magic_name__ ( lowercase ): return {key.lstrip("""-""" ): value for key, value in zip(unknown_args[::2] , unknown_args[1::2] )} def __magic_name__ ( ): SCREAMING_SNAKE_CASE_: List[str] =ArgumentParser( """HuggingFace Datasets CLI tool""" , usage="""datasets-cli <command> [<args>]""" , allow_abbrev=lowercase ) SCREAMING_SNAKE_CASE_: List[Any] =parser.add_subparsers(help="""datasets-cli command helpers""" ) set_verbosity_info() # Register commands ConvertCommand.register_subcommand(lowercase ) EnvironmentCommand.register_subcommand(lowercase ) TestCommand.register_subcommand(lowercase ) RunBeamCommand.register_subcommand(lowercase ) DummyDataCommand.register_subcommand(lowercase ) # Parse args SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] =parser.parse_known_args() if not hasattr(lowercase , """func""" ): parser.print_help() exit(1 ) SCREAMING_SNAKE_CASE_: Dict =parse_unknown_args(lowercase ) # Run SCREAMING_SNAKE_CASE_: Tuple =args.func(lowercase , **lowercase ) service.run() if __name__ == "__main__": main()
173
"""simple docstring""" # Lint as: python3 import itertools import os import re _UpperCAmelCase = re.compile(r"""([A-Z]+)([A-Z][a-z])""") _UpperCAmelCase = re.compile(r"""([a-z\d])([A-Z])""") _UpperCAmelCase = re.compile(r"""(?<!_)_(?!_)""") _UpperCAmelCase = re.compile(r"""(_{2,})""") _UpperCAmelCase = r"""^\w+(\.\w+)*$""" _UpperCAmelCase = r"""<>:/\|?*""" def __magic_name__ ( lowercase ): SCREAMING_SNAKE_CASE_: Optional[int] =_uppercase_uppercase_re.sub(R"""\1_\2""" , lowercase ) SCREAMING_SNAKE_CASE_: str =_lowercase_uppercase_re.sub(R"""\1_\2""" , lowercase ) return name.lower() def __magic_name__ ( lowercase ): SCREAMING_SNAKE_CASE_: Optional[int] =_single_underscore_re.split(lowercase ) SCREAMING_SNAKE_CASE_: Any =[_multiple_underscores_re.split(lowercase ) for n in name] return "".join(n.capitalize() for n in itertools.chain.from_iterable(lowercase ) if n != """""" ) def __magic_name__ ( lowercase ): if os.path.basename(lowercase ) != name: raise ValueError(f'''Should be a dataset name, not a path: {name}''' ) return camelcase_to_snakecase(lowercase ) def __magic_name__ ( lowercase , lowercase ): if os.path.basename(lowercase ) != name: raise ValueError(f'''Should be a dataset name, not a path: {name}''' ) if not re.match(_split_re , lowercase ): raise ValueError(f'''Split name should match \'{_split_re}\'\' but got \'{split}\'.''' ) return f'''{filename_prefix_for_name(lowercase )}-{split}''' def __magic_name__ ( lowercase , lowercase , lowercase , lowercase=None ): SCREAMING_SNAKE_CASE_: List[Any] =filename_prefix_for_split(lowercase , lowercase ) if filetype_suffix: prefix += f'''.{filetype_suffix}''' SCREAMING_SNAKE_CASE_: Dict =os.path.join(lowercase , lowercase ) return f'''{filepath}*''' def __magic_name__ ( lowercase , lowercase , lowercase , lowercase=None , lowercase=None ): SCREAMING_SNAKE_CASE_: List[Any] =filename_prefix_for_split(lowercase , lowercase ) SCREAMING_SNAKE_CASE_: int =os.path.join(lowercase , lowercase ) if shard_lengths: SCREAMING_SNAKE_CASE_: Any =len(lowercase ) SCREAMING_SNAKE_CASE_: Optional[Any] =[f'''{prefix}-{shard_id:05d}-of-{num_shards:05d}''' for shard_id in range(lowercase )] if filetype_suffix: SCREAMING_SNAKE_CASE_: Optional[int] =[filename + f'''.{filetype_suffix}''' for filename in filenames] return filenames else: SCREAMING_SNAKE_CASE_: List[Any] =prefix if filetype_suffix: filename += f'''.{filetype_suffix}''' return [filename]
173
1
'''simple docstring''' import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class lowercase_ (unittest.TestCase ): """simple docstring""" @property def SCREAMING_SNAKE_CASE ( self : int ): torch.manual_seed(0 ) __lowercase = UNetaDModel( block_out_channels=(3_2, 6_4) ,layers_per_block=2 ,sample_size=3_2 ,in_channels=3 ,out_channels=3 ,down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') ,up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') ,) return model def SCREAMING_SNAKE_CASE ( self : Optional[Any] ): __lowercase = self.dummy_uncond_unet __lowercase = KarrasVeScheduler() __lowercase = KarrasVePipeline(unet=lowercase__ ,scheduler=lowercase__ ) pipe.to(lowercase__ ) pipe.set_progress_bar_config(disable=lowercase__ ) __lowercase = torch.manual_seed(0 ) __lowercase = pipe(num_inference_steps=2 ,generator=lowercase__ ,output_type='''numpy''' ).images __lowercase = torch.manual_seed(0 ) __lowercase = pipe(num_inference_steps=2 ,generator=lowercase__ ,output_type='''numpy''' ,return_dict=lowercase__ )[0] __lowercase = image[0, -3:, -3:, -1] __lowercase = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 3_2, 3_2, 3) __lowercase = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class lowercase_ (unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self : Tuple ): __lowercase = '''google/ncsnpp-celebahq-256''' __lowercase = UNetaDModel.from_pretrained(lowercase__ ) __lowercase = KarrasVeScheduler() __lowercase = KarrasVePipeline(unet=lowercase__ ,scheduler=lowercase__ ) pipe.to(lowercase__ ) pipe.set_progress_bar_config(disable=lowercase__ ) __lowercase = torch.manual_seed(0 ) __lowercase = pipe(num_inference_steps=2_0 ,generator=lowercase__ ,output_type='''numpy''' ).images __lowercase = image[0, -3:, -3:, -1] assert image.shape == (1, 2_5_6, 2_5_6, 3) __lowercase = np.array([0.5_7_8, 0.5_8_1_1, 0.5_9_2_4, 0.5_8_0_9, 0.5_8_7, 0.5_8_8_6, 0.5_8_6_1, 0.5_8_0_2, 0.5_8_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
52
'''simple docstring''' lowerCAmelCase__ = '''ABCDEFGHIJKLMNOPQRSTUVWXYZ''' def _A ( ): """simple docstring""" __lowercase = input('''Enter message: ''' ) __lowercase = input('''Enter key [alphanumeric]: ''' ) __lowercase = input('''Encrypt/Decrypt [e/d]: ''' ) if mode.lower().startswith('''e''' ): __lowercase = '''encrypt''' __lowercase = encrypt_message(A__ , A__ ) elif mode.lower().startswith('''d''' ): __lowercase = '''decrypt''' __lowercase = decrypt_message(A__ , A__ ) print(F"\n{mode.title()}ed message:" ) print(A__ ) def _A ( A__ , A__ ): """simple docstring""" return translate_message(A__ , A__ , '''encrypt''' ) def _A ( A__ , A__ ): """simple docstring""" return translate_message(A__ , A__ , '''decrypt''' ) def _A ( A__ , A__ , A__ ): """simple docstring""" __lowercase = [] __lowercase = 0 __lowercase = key.upper() for symbol in message: __lowercase = LETTERS.find(symbol.upper() ) if num != -1: if mode == "encrypt": num += LETTERS.find(key[key_index] ) elif mode == "decrypt": num -= LETTERS.find(key[key_index] ) num %= len(A__ ) if symbol.isupper(): translated.append(LETTERS[num] ) elif symbol.islower(): translated.append(LETTERS[num].lower() ) key_index += 1 if key_index == len(A__ ): __lowercase = 0 else: translated.append(A__ ) return "".join(A__ ) if __name__ == "__main__": main()
52
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __magic_name__ = { "configuration_chinese_clip": [ "CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "ChineseCLIPConfig", "ChineseCLIPOnnxConfig", "ChineseCLIPTextConfig", "ChineseCLIPVisionConfig", ], "processing_chinese_clip": ["ChineseCLIPProcessor"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __magic_name__ = ["ChineseCLIPFeatureExtractor"] __magic_name__ = ["ChineseCLIPImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __magic_name__ = [ "CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "ChineseCLIPModel", "ChineseCLIPPreTrainedModel", "ChineseCLIPTextModel", "ChineseCLIPVisionModel", ] if TYPE_CHECKING: from .configuration_chinese_clip import ( CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, ChineseCLIPConfig, ChineseCLIPOnnxConfig, ChineseCLIPTextConfig, ChineseCLIPVisionConfig, ) from .processing_chinese_clip import ChineseCLIPProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_chinese_clip import ChineseCLIPFeatureExtractor, ChineseCLIPImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_chinese_clip import ( CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, ChineseCLIPModel, ChineseCLIPPreTrainedModel, ChineseCLIPTextModel, ChineseCLIPVisionModel, ) else: import sys __magic_name__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
100
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 snake_case_ ( __A ): __A : List[str] = "convbert" def __init__( self : Union[str, Any] , lowercase_ : str=3_05_22 , lowercase_ : Any=7_68 , lowercase_ : Tuple=12 , lowercase_ : List[str]=12 , lowercase_ : Optional[int]=30_72 , lowercase_ : Union[str, Any]="gelu" , lowercase_ : str=0.1 , lowercase_ : List[str]=0.1 , lowercase_ : Optional[Any]=5_12 , lowercase_ : Dict=2 , lowercase_ : Union[str, Any]=0.02 , lowercase_ : Optional[Any]=1E-12 , lowercase_ : Optional[int]=1 , lowercase_ : List[Any]=0 , lowercase_ : Optional[int]=2 , lowercase_ : str=7_68 , lowercase_ : Dict=2 , lowercase_ : Optional[Any]=9 , lowercase_ : Union[str, Any]=1 , lowercase_ : Any=None , **lowercase_ : Optional[Any] , ) -> Dict: super().__init__( pad_token_id=lowercase_ , bos_token_id=lowercase_ , eos_token_id=lowercase_ , **lowercase_ , ) lowercase__ : List[str] = vocab_size lowercase__ : Union[str, Any] = hidden_size lowercase__ : Any = num_hidden_layers lowercase__ : List[str] = num_attention_heads lowercase__ : Union[str, Any] = intermediate_size lowercase__ : Optional[Any] = hidden_act lowercase__ : int = hidden_dropout_prob lowercase__ : str = attention_probs_dropout_prob lowercase__ : Union[str, Any] = max_position_embeddings lowercase__ : Optional[int] = type_vocab_size lowercase__ : Tuple = initializer_range lowercase__ : List[str] = layer_norm_eps lowercase__ : List[Any] = embedding_size lowercase__ : Optional[Any] = head_ratio lowercase__ : Dict = conv_kernel_size lowercase__ : Tuple = num_groups lowercase__ : Optional[int] = classifier_dropout class snake_case_ ( __A ): @property def __UpperCamelCase ( self : Optional[int] ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": lowercase__ : Tuple = {0: "batch", 1: "choice", 2: "sequence"} else: lowercase__ : str = {0: "batch", 1: "sequence"} return OrderedDict( [ ("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ("token_type_ids", dynamic_axis), ] )
87
0
"""simple docstring""" # tests directory-specific settings - this file is run automatically # by pytest before any tests are run import sys import warnings from os.path import abspath, dirname, join # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. _lowerCAmelCase : Union[str, Any] = abspath(join(dirname(dirname(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__ ( snake_case : Optional[int] )-> Any: '''simple docstring''' from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(snake_case ) def SCREAMING_SNAKE_CASE__ ( snake_case : int )-> str: '''simple docstring''' from transformers.testing_utils import pytest_terminal_summary_main UpperCAmelCase__ : List[str] = terminalreporter.config.getoption("--make-reports" ) if make_reports: pytest_terminal_summary_main(snake_case , id=snake_case )
298
"""simple docstring""" import os import re from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _lowerCAmelCase : Optional[int] = logging.get_logger(__name__) _lowerCAmelCase : Optional[int] = { """vocab_file""": """vocab.txt""", """merges_file""": """bpe.codes""", } _lowerCAmelCase : List[Any] = { """vocab_file""": { """vinai/phobert-base""": """https://huggingface.co/vinai/phobert-base/resolve/main/vocab.txt""", """vinai/phobert-large""": """https://huggingface.co/vinai/phobert-large/resolve/main/vocab.txt""", }, """merges_file""": { """vinai/phobert-base""": """https://huggingface.co/vinai/phobert-base/resolve/main/bpe.codes""", """vinai/phobert-large""": """https://huggingface.co/vinai/phobert-large/resolve/main/bpe.codes""", }, } _lowerCAmelCase : int = { """vinai/phobert-base""": 256, """vinai/phobert-large""": 256, } def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] )-> str: '''simple docstring''' UpperCAmelCase__ : Optional[Any] = set() UpperCAmelCase__ : Optional[int] = word[0] for char in word[1:]: pairs.add((prev_char, char) ) UpperCAmelCase__ : Dict = char UpperCAmelCase__ : Tuple = set(snake_case ) return pairs class lowerCAmelCase__ ( __magic_name__ ): SCREAMING_SNAKE_CASE_ =VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE_ =PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE_ =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self : List[Any] , snake_case__ : int , snake_case__ : Union[str, Any] , snake_case__ : Tuple="<s>" , snake_case__ : List[Any]="</s>" , snake_case__ : Union[str, Any]="</s>" , snake_case__ : Union[str, Any]="<s>" , snake_case__ : Any="<unk>" , snake_case__ : int="<pad>" , snake_case__ : List[str]="<mask>" , **snake_case__ : Optional[int] , ): '''simple docstring''' super().__init__( bos_token=snake_case__ , eos_token=snake_case__ , unk_token=snake_case__ , sep_token=snake_case__ , cls_token=snake_case__ , pad_token=snake_case__ , mask_token=snake_case__ , **snake_case__ , ) UpperCAmelCase__ : Dict = vocab_file UpperCAmelCase__ : Tuple = merges_file UpperCAmelCase__ : List[Any] = {} UpperCAmelCase__ : Dict = 0 UpperCAmelCase__ : int = 1 UpperCAmelCase__ : Dict = 2 UpperCAmelCase__ : Dict = 3 self.add_from_file(snake_case__ ) UpperCAmelCase__ : Optional[Any] = {v: k for k, v in self.encoder.items()} with open(snake_case__ , encoding="utf-8" ) as merges_handle: UpperCAmelCase__ : Tuple = merges_handle.read().split("\n" )[:-1] UpperCAmelCase__ : Optional[Any] = [tuple(merge.split()[:-1] ) for merge in merges] UpperCAmelCase__ : List[Any] = dict(zip(snake_case__ , range(len(snake_case__ ) ) ) ) UpperCAmelCase__ : Dict = {} def __a ( self : int , snake_case__ : List[int] , snake_case__ : Optional[List[int]] = None ): '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] UpperCAmelCase__ : str = [self.cls_token_id] UpperCAmelCase__ : Any = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def __a ( self : List[str] , snake_case__ : List[int] , snake_case__ : Optional[List[int]] = None , snake_case__ : bool = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=snake_case__ , token_ids_a=snake_case__ , already_has_special_tokens=snake_case__ ) if token_ids_a is None: return [1] + ([0] * len(snake_case__ )) + [1] return [1] + ([0] * len(snake_case__ )) + [1, 1] + ([0] * len(snake_case__ )) + [1] def __a ( self : Union[str, Any] , snake_case__ : List[int] , snake_case__ : Optional[List[int]] = None ): '''simple docstring''' UpperCAmelCase__ : Tuple = [self.sep_token_id] UpperCAmelCase__ : str = [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] @property def __a ( self : List[str] ): '''simple docstring''' return len(self.encoder ) def __a ( self : Any ): '''simple docstring''' return dict(self.encoder , **self.added_tokens_encoder ) def __a ( self : Dict , snake_case__ : Tuple ): '''simple docstring''' if token in self.cache: return self.cache[token] UpperCAmelCase__ : Optional[Any] = tuple(snake_case__ ) UpperCAmelCase__ : Optional[Any] = tuple(list(word[:-1] ) + [word[-1] + "</w>"] ) UpperCAmelCase__ : Any = get_pairs(snake_case__ ) if not pairs: return token while True: UpperCAmelCase__ : List[Any] = min(snake_case__ , key=lambda snake_case__ : self.bpe_ranks.get(snake_case__ , float("inf" ) ) ) if bigram not in self.bpe_ranks: break UpperCAmelCase__ , UpperCAmelCase__ : Tuple = bigram UpperCAmelCase__ : Optional[Any] = [] UpperCAmelCase__ : Tuple = 0 while i < len(snake_case__ ): try: UpperCAmelCase__ : Union[str, Any] = word.index(snake_case__ , snake_case__ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) UpperCAmelCase__ : Dict = j if word[i] == first and i < len(snake_case__ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 UpperCAmelCase__ : Dict = tuple(snake_case__ ) UpperCAmelCase__ : List[Any] = new_word if len(snake_case__ ) == 1: break else: UpperCAmelCase__ : Dict = get_pairs(snake_case__ ) UpperCAmelCase__ : List[Any] = "@@ ".join(snake_case__ ) UpperCAmelCase__ : Optional[int] = word[:-4] UpperCAmelCase__ : Union[str, Any] = word return word def __a ( self : List[Any] , snake_case__ : Tuple ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = [] UpperCAmelCase__ : int = re.findall(R"\S+\n?" , snake_case__ ) for token in words: split_tokens.extend(list(self.bpe(snake_case__ ).split(" " ) ) ) return split_tokens def __a ( self : Dict , snake_case__ : List[str] ): '''simple docstring''' return self.encoder.get(snake_case__ , self.encoder.get(self.unk_token ) ) def __a ( self : List[Any] , snake_case__ : Any ): '''simple docstring''' return self.decoder.get(snake_case__ , self.unk_token ) def __a ( self : str , snake_case__ : Tuple ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = " ".join(snake_case__ ).replace("@@ " , "" ).strip() return out_string def __a ( self : Any , snake_case__ : str , snake_case__ : Optional[str] = None ): '''simple docstring''' if not os.path.isdir(snake_case__ ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return UpperCAmelCase__ : Tuple = os.path.join( snake_case__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) UpperCAmelCase__ : str = os.path.join( snake_case__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case__ ): copyfile(self.vocab_file , snake_case__ ) if os.path.abspath(self.merges_file ) != os.path.abspath(snake_case__ ): copyfile(self.merges_file , snake_case__ ) return out_vocab_file, out_merge_file def __a ( self : List[Any] , snake_case__ : Union[str, Any] ): '''simple docstring''' if isinstance(snake_case__ , snake_case__ ): try: with open(snake_case__ , "r" , encoding="utf-8" ) as fd: self.add_from_file(snake_case__ ) except FileNotFoundError as fnfe: raise fnfe except UnicodeError: raise Exception(f'Incorrect encoding detected in {f}, please rebuild the dataset' ) return UpperCAmelCase__ : Dict = f.readlines() for lineTmp in lines: UpperCAmelCase__ : Optional[int] = lineTmp.strip() UpperCAmelCase__ : Tuple = line.rfind(" " ) if idx == -1: raise ValueError("Incorrect dictionary format, expected '<token> <cnt>'" ) UpperCAmelCase__ : Any = line[:idx] UpperCAmelCase__ : str = len(self.encoder )
298
1
import unittest import numpy as np from transformers import RobertaConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): from transformers.models.roberta.modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, ) class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __init__( self , lowercase , lowercase=13 , lowercase=7 , lowercase=True , lowercase=True , lowercase=True , lowercase=True , lowercase=99 , lowercase=32 , lowercase=5 , lowercase=4 , lowercase=37 , lowercase="gelu" , lowercase=0.1 , lowercase=0.1 , lowercase=512 , lowercase=16 , lowercase=2 , lowercase=0.0_2 , lowercase=4 , ) -> Optional[Any]: lowerCamelCase_ = parent lowerCamelCase_ = batch_size lowerCamelCase_ = seq_length lowerCamelCase_ = is_training lowerCamelCase_ = use_attention_mask lowerCamelCase_ = use_token_type_ids lowerCamelCase_ = use_labels lowerCamelCase_ = vocab_size lowerCamelCase_ = hidden_size lowerCamelCase_ = num_hidden_layers lowerCamelCase_ = num_attention_heads lowerCamelCase_ = intermediate_size lowerCamelCase_ = hidden_act lowerCamelCase_ = hidden_dropout_prob lowerCamelCase_ = attention_probs_dropout_prob lowerCamelCase_ = max_position_embeddings lowerCamelCase_ = type_vocab_size lowerCamelCase_ = type_sequence_label_size lowerCamelCase_ = initializer_range lowerCamelCase_ = num_choices def SCREAMING_SNAKE_CASE_( self ) -> Dict: lowerCamelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase_ = None if self.use_attention_mask: lowerCamelCase_ = random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase_ = None if self.use_token_type_ids: lowerCamelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCamelCase_ = RobertaConfig( vocab_size=self.vocab_size , 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 , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowercase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def SCREAMING_SNAKE_CASE_( self ) -> Union[str, Any]: lowerCamelCase_ = self.prepare_config_and_inputs() lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ = config_and_inputs lowerCamelCase_ = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask} return config, inputs_dict def SCREAMING_SNAKE_CASE_( self ) -> List[Any]: lowerCamelCase_ = self.prepare_config_and_inputs() lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ = config_and_inputs lowerCamelCase_ = True lowerCamelCase_ = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) lowerCamelCase_ = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax class _SCREAMING_SNAKE_CASE ( snake_case_ , unittest.TestCase ): lowerCAmelCase__ = True lowerCAmelCase__ = ( ( FlaxRobertaModel, FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, ) if is_flax_available() else () ) def SCREAMING_SNAKE_CASE_( self ) -> int: lowerCamelCase_ = FlaxRobertaModelTester(self ) @slow def SCREAMING_SNAKE_CASE_( self ) -> Optional[int]: for model_class_name in self.all_model_classes: lowerCamelCase_ = model_class_name.from_pretrained("roberta-base" , from_pt=lowercase ) lowerCamelCase_ = model(np.ones((1, 1) ) ) self.assertIsNotNone(lowercase )
19
from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __lowerCamelCase ( snake_case__): """simple docstring""" UpperCamelCase__ = ["image_processor", "tokenizer"] UpperCamelCase__ = "Pix2StructImageProcessor" UpperCamelCase__ = ("T5Tokenizer", "T5TokenizerFast") def __init__( self , UpperCAmelCase , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = False super().__init__(UpperCAmelCase , UpperCAmelCase ) def __call__( self , UpperCAmelCase=None , UpperCAmelCase = None , UpperCAmelCase = True , UpperCAmelCase = False , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = 2048 , UpperCAmelCase = 0 , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = True , UpperCAmelCase = None , **UpperCAmelCase , ): """simple docstring""" if images is None and text is None: raise ValueError('You have to specify either images or text.' ) # Get only text if images is None and not self.image_processor.is_vqa: _UpperCAmelCase = self.tokenizer _UpperCAmelCase = self.tokenizer( text=UpperCAmelCase , add_special_tokens=UpperCAmelCase , padding=UpperCAmelCase , truncation=UpperCAmelCase , max_length=UpperCAmelCase , stride=UpperCAmelCase , pad_to_multiple_of=UpperCAmelCase , return_attention_mask=UpperCAmelCase , return_overflowing_tokens=UpperCAmelCase , return_special_tokens_mask=UpperCAmelCase , return_offsets_mapping=UpperCAmelCase , return_token_type_ids=UpperCAmelCase , return_length=UpperCAmelCase , verbose=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase , ) return text_encoding if not self.image_processor.is_vqa: # add pixel_values _UpperCAmelCase = self.image_processor( UpperCAmelCase , return_tensors=UpperCAmelCase , max_patches=UpperCAmelCase , **UpperCAmelCase ) else: # add pixel_values and bbox _UpperCAmelCase = self.image_processor( UpperCAmelCase , return_tensors=UpperCAmelCase , max_patches=UpperCAmelCase , header_text=UpperCAmelCase , **UpperCAmelCase ) if text is not None and not self.image_processor.is_vqa: _UpperCAmelCase = self.tokenizer( text=UpperCAmelCase , add_special_tokens=UpperCAmelCase , padding=UpperCAmelCase , truncation=UpperCAmelCase , max_length=UpperCAmelCase , stride=UpperCAmelCase , pad_to_multiple_of=UpperCAmelCase , return_attention_mask=UpperCAmelCase , return_overflowing_tokens=UpperCAmelCase , return_special_tokens_mask=UpperCAmelCase , return_offsets_mapping=UpperCAmelCase , return_token_type_ids=UpperCAmelCase , return_length=UpperCAmelCase , verbose=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase , ) if "attention_mask" in text_encoding: _UpperCAmelCase = text_encoding.pop('attention_mask' ) if "input_ids" in text_encoding: _UpperCAmelCase = text_encoding.pop('input_ids' ) else: _UpperCAmelCase = None if text_encoding is not None: encoding_image_processor.update(UpperCAmelCase ) return encoding_image_processor def UpperCamelCase ( self , *UpperCAmelCase , **UpperCAmelCase ): """simple docstring""" return self.tokenizer.batch_decode(*UpperCAmelCase , **UpperCAmelCase ) def UpperCamelCase ( self , *UpperCAmelCase , **UpperCAmelCase ): """simple docstring""" return self.tokenizer.decode(*UpperCAmelCase , **UpperCAmelCase ) @property def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.tokenizer.model_input_names _UpperCAmelCase = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
39
0
import argparse import json import os import fairseq import torch from torch import nn from transformers import ( SpeechaTextaConfig, SpeechaTextaForCausalLM, SpeechaTextaTokenizer, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() _A = logging.get_logger(__name__) _A = { 'post_extract_proj': 'feature_projection.projection', 'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv', 'self_attn.k_proj': 'encoder.layers.*.attention.k_proj', 'self_attn.v_proj': 'encoder.layers.*.attention.v_proj', 'self_attn.q_proj': 'encoder.layers.*.attention.q_proj', 'self_attn.out_proj': 'encoder.layers.*.attention.out_proj', 'self_attn_layer_norm': 'encoder.layers.*.layer_norm', 'fc1': 'encoder.layers.*.feed_forward.intermediate_dense', 'fc2': 'encoder.layers.*.feed_forward.output_dense', 'final_layer_norm': 'encoder.layers.*.final_layer_norm', 'encoder.layer_norm': 'encoder.layer_norm', 'w2v_model.layer_norm': 'feature_projection.layer_norm', 'quantizer.weight_proj': 'quantizer.weight_proj', 'quantizer.vars': 'quantizer.codevectors', 'project_q': 'project_q', 'final_proj': 'project_hid', 'w2v_encoder.proj': 'lm_head', 'mask_emb': 'masked_spec_embed', } _A = [ 'lm_head', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', ] def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : List[Any] ): for attribute in key.split('.' ): __UpperCamelCase =getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if weight_type is not None: __UpperCamelCase =getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ).shape else: __UpperCamelCase =hf_pointer.shape assert hf_shape == value.shape, ( F'Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be' F' {value.shape} for {full_name}' ) if weight_type == "weight": __UpperCamelCase =value elif weight_type == "weight_g": __UpperCamelCase =value elif weight_type == "weight_v": __UpperCamelCase =value elif weight_type == "bias": __UpperCamelCase =value else: __UpperCamelCase =value logger.info(F'{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.' ) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : Optional[int] ): __UpperCamelCase =[] __UpperCamelCase =fairseq_model.state_dict() __UpperCamelCase =hf_model.feature_extractor # if encoder has different dim to decoder -> use proj_weight __UpperCamelCase =None for name, value in fairseq_dict.items(): __UpperCamelCase =False if "conv_layers" in name: load_conv_layer( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , hf_model.config.feat_extract_norm == 'group' , ) __UpperCamelCase =True elif name.split('.' )[0] == "proj": __UpperCamelCase =fairseq_model.proj __UpperCamelCase =True else: for key, mapped_key in MAPPING.items(): if key in name or key.split('w2v_model.' )[-1] == name.split('.' )[0]: __UpperCamelCase =True if "*" in mapped_key: __UpperCamelCase =name.split(SCREAMING_SNAKE_CASE__ )[0].split('.' )[-2] __UpperCamelCase =mapped_key.replace('*' , SCREAMING_SNAKE_CASE__ ) if "weight_g" in name: __UpperCamelCase ='weight_g' elif "weight_v" in name: __UpperCamelCase ='weight_v' elif "bias" in name: __UpperCamelCase ='bias' elif "weight" in name: __UpperCamelCase ='weight' else: __UpperCamelCase =None set_recursively(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) continue if not is_used: unused_weights.append(SCREAMING_SNAKE_CASE__ ) logger.warning(F'Unused weights: {unused_weights}' ) return proj_weight def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Dict ): __UpperCamelCase =full_name.split('conv_layers.' )[-1] __UpperCamelCase =name.split('.' ) __UpperCamelCase =int(items[0] ) __UpperCamelCase =int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F'{full_name} has size {value.shape}, but' F' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.' ) __UpperCamelCase =value logger.info(F'Feat extract conv layer {layer_id} was initialized from {full_name}.' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F'{full_name} has size {value.shape}, but' F' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.' ) __UpperCamelCase =value logger.info(F'Feat extract conv layer {layer_id} was initialized from {full_name}.' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F'{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was' " found." ) __UpperCamelCase =value logger.info(F'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F'{full_name} has size {value.shape}, but' F' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.' ) __UpperCamelCase =value logger.info(F'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.' ) else: unused_weights.append(SCREAMING_SNAKE_CASE__ ) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Tuple ): __UpperCamelCase , __UpperCamelCase =emb.weight.shape __UpperCamelCase =nn.Linear(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , bias=SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =emb.weight.data return lin_layer def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Any ): with open(SCREAMING_SNAKE_CASE__ , 'r' , encoding='utf-8' ) as f: __UpperCamelCase =f.readlines() __UpperCamelCase =[line.split(' ' )[0] for line in lines] __UpperCamelCase =len(SCREAMING_SNAKE_CASE__ ) __UpperCamelCase ={ '<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3, } vocab_dict.update(dict(zip(SCREAMING_SNAKE_CASE__ , range(4 , num_words + 4 ) ) ) ) return vocab_dict @torch.no_grad() def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : Dict , ): __UpperCamelCase =WavaVecaConfig.from_pretrained(SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =SpeechaTextaConfig.from_pretrained( SCREAMING_SNAKE_CASE__ , vocab_size=SCREAMING_SNAKE_CASE__ , decoder_layers=SCREAMING_SNAKE_CASE__ , do_stable_layer_norm=SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_60_00 , padding_value=0 , do_normalize=SCREAMING_SNAKE_CASE__ , return_attention_mask=SCREAMING_SNAKE_CASE__ , ) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase =fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'data': '/'.join(dict_path.split('/' )[:-1] )} ) __UpperCamelCase =model[0].eval() # set weights for wav2vec2 encoder __UpperCamelCase =WavaVecaModel(SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =recursively_load_weights_wavaveca(model.encoder , SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =SpeechaTextaForCausalLM(SCREAMING_SNAKE_CASE__ ) __UpperCamelCase , __UpperCamelCase =hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=SCREAMING_SNAKE_CASE__ ) # set output linear layer unexpected_keys.remove('embed_out' ) __UpperCamelCase =nn.Parameter(model.decoder.embed_out.detach() ) # layer norm is init to identity matrix so leaving it is fine logger.warning(F'The following keys are missing when loading the decoder weights: {missing_keys}' ) logger.warning(F'The following keys are unexpected when loading the decoder weights: {unexpected_keys}' ) __UpperCamelCase =SpeechEncoderDecoderModel(encoder=SCREAMING_SNAKE_CASE__ , decoder=SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =False # add projection layer __UpperCamelCase =nn.Parameter(projection_layer.weight ) __UpperCamelCase =nn.Parameter(projection_layer.bias ) __UpperCamelCase =create_vocab_dict(SCREAMING_SNAKE_CASE__ ) with open(os.path.join(SCREAMING_SNAKE_CASE__ , 'vocab.json' ) , 'w' ) as fp: json.dump(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =SpeechaTextaTokenizer(os.path.join(SCREAMING_SNAKE_CASE__ , 'vocab.json' ) ) tokenizer.save_pretrained(SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =hf_wavavec.config.to_dict() __UpperCamelCase =tokenizer.pad_token_id __UpperCamelCase =tokenizer.bos_token_id __UpperCamelCase =tokenizer.eos_token_id __UpperCamelCase ='speech_to_text_2' __UpperCamelCase ='wav2vec2' __UpperCamelCase =SpeechEncoderDecoderConfig.from_dict(SCREAMING_SNAKE_CASE__ ) hf_wavavec.save_pretrained(SCREAMING_SNAKE_CASE__ ) feature_extractor.save_pretrained(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": _A = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint') parser.add_argument('--dict_path', default=None, type=str, help='Path to dict of fine-tuned model') parser.add_argument( '--encoder_config_path', default='facebook/wav2vec2-large-lv60', type=str, help='Path to hf encoder wav2vec2 checkpoint config', ) parser.add_argument( '--decoder_config_path', default='facebook/s2t-small-mustc-en-fr-st', type=str, help='Path to hf decoder s2t checkpoint config', ) parser.add_argument('--vocab_size', default=1_0224, type=int, help='Vocab size of decoder') parser.add_argument('--num_decoder_layers', default=7, type=int, help='Number of decoder layers') _A = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, vocab_size=args.vocab_size, num_decoder_layers=args.num_decoder_layers, )
117
from ...configuration_utils import PretrainedConfig from ...utils import logging _A = logging.get_logger(__name__) _A = { 'google/realm-cc-news-pretrained-embedder': ( 'https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/config.json' ), 'google/realm-cc-news-pretrained-encoder': ( 'https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/config.json' ), 'google/realm-cc-news-pretrained-scorer': ( 'https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/config.json' ), 'google/realm-cc-news-pretrained-openqa': ( 'https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/config.json' ), 'google/realm-orqa-nq-openqa': 'https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/config.json', 'google/realm-orqa-nq-reader': 'https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/config.json', 'google/realm-orqa-wq-openqa': 'https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/config.json', 'google/realm-orqa-wq-reader': 'https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/config.json', # See all REALM models at https://huggingface.co/models?filter=realm } class UpperCAmelCase__ ( A_ ): """simple docstring""" UpperCAmelCase__ : str = "realm" def __init__( self , A_=30522 , A_=768 , A_=128 , A_=12 , A_=12 , A_=8 , A_=3072 , A_="gelu_new" , A_=0.1 , A_=0.1 , A_=512 , A_=2 , A_=0.02 , A_=1E-12 , A_=256 , A_=10 , A_=1E-3 , A_=5 , A_=320 , A_=13353718 , A_=5000 , A_=1 , A_=0 , A_=2 , **A_ , ) -> Dict: super().__init__(pad_token_id=A_ , bos_token_id=A_ , eos_token_id=A_ , **A_ ) # Common config __UpperCamelCase =vocab_size __UpperCamelCase =max_position_embeddings __UpperCamelCase =hidden_size __UpperCamelCase =retriever_proj_size __UpperCamelCase =num_hidden_layers __UpperCamelCase =num_attention_heads __UpperCamelCase =num_candidates __UpperCamelCase =intermediate_size __UpperCamelCase =hidden_act __UpperCamelCase =hidden_dropout_prob __UpperCamelCase =attention_probs_dropout_prob __UpperCamelCase =initializer_range __UpperCamelCase =type_vocab_size __UpperCamelCase =layer_norm_eps # Reader config __UpperCamelCase =span_hidden_size __UpperCamelCase =max_span_width __UpperCamelCase =reader_layer_norm_eps __UpperCamelCase =reader_beam_size __UpperCamelCase =reader_seq_len # Retrieval config __UpperCamelCase =num_block_records __UpperCamelCase =searcher_beam_size
117
1
import unittest from transformers import DebertaVaTokenizer, DebertaVaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin lowerCamelCase_ = get_tests_dir('''fixtures/spiece.model''') @require_sentencepiece @require_tokenizers class __A( __lowerCamelCase , unittest.TestCase ): """simple docstring""" SCREAMING_SNAKE_CASE__ = DebertaVaTokenizer SCREAMING_SNAKE_CASE__ = DebertaVaTokenizerFast SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = True def UpperCAmelCase_ (self ): super().setUp() # We have a SentencePiece fixture for testing UpperCamelCase__ = DebertaVaTokenizer(SCREAMING_SNAKE_CASE_ , unk_token="""<unk>""" ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ ): UpperCamelCase__ = """this is a test""" UpperCamelCase__ = """this is a test""" return input_text, output_text def UpperCAmelCase_ (self ): UpperCamelCase__ = """<pad>""" UpperCamelCase__ = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): UpperCamelCase__ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , """<pad>""" ) self.assertEqual(vocab_keys[1] , """<unk>""" ) self.assertEqual(vocab_keys[-1] , """[PAD]""" ) self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) , 3_00_01 ) def UpperCAmelCase_ (self ): self.assertEqual(self.get_tokenizer().vocab_size , 3_00_00 ) def UpperCAmelCase_ (self ): # fmt: off UpperCamelCase__ = """ \tHeLLo!how \n Are yoU? """ UpperCamelCase__ = ["""▁hello""", """!""", """how""", """▁are""", """▁you""", """?"""] # fmt: on UpperCamelCase__ = DebertaVaTokenizer(SCREAMING_SNAKE_CASE_ , do_lower_case=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = tokenizer.convert_ids_to_tokens(tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = DebertaVaTokenizerFast(SCREAMING_SNAKE_CASE_ , do_lower_case=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @unittest.skip("""There is an inconsistency between slow and fast tokenizer due to a bug in the fast one.""" ) def UpperCAmelCase_ (self ): pass @unittest.skip("""There is an inconsistency between slow and fast tokenizer due to a bug in the fast one.""" ) def UpperCAmelCase_ (self ): pass def UpperCAmelCase_ (self ): # fmt: off UpperCamelCase__ = """I was born in 92000, and this is falsé.""" UpperCamelCase__ = ["""▁""", """<unk>""", """▁was""", """▁born""", """▁in""", """▁9""", """2000""", """▁""", """,""", """▁and""", """▁this""", """▁is""", """▁fal""", """s""", """<unk>""", """▁""", """.""", ] # fmt: on UpperCamelCase__ = DebertaVaTokenizer(SCREAMING_SNAKE_CASE_ , split_by_punct=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = tokenizer.convert_ids_to_tokens(tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = DebertaVaTokenizerFast(SCREAMING_SNAKE_CASE_ , split_by_punct=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): # fmt: off UpperCamelCase__ = """I was born in 92000, and this is falsé.""" UpperCamelCase__ = ["""▁i""", """▁was""", """▁born""", """▁in""", """▁9""", """2000""", """▁""", """,""", """▁and""", """▁this""", """▁is""", """▁fal""", """s""", """<unk>""", """▁""", """.""", ] # fmt: on UpperCamelCase__ = DebertaVaTokenizer(SCREAMING_SNAKE_CASE_ , do_lower_case=SCREAMING_SNAKE_CASE_ , split_by_punct=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = tokenizer.convert_ids_to_tokens(tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = DebertaVaTokenizerFast(SCREAMING_SNAKE_CASE_ , do_lower_case=SCREAMING_SNAKE_CASE_ , split_by_punct=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): # fmt: off UpperCamelCase__ = """I was born in 92000, and this is falsé.""" UpperCamelCase__ = ["""▁i""", """▁was""", """▁born""", """▁in""", """▁9""", """2000""", """,""", """▁and""", """▁this""", """▁is""", """▁fal""", """s""", """<unk>""", """.""", ] # fmt: on UpperCamelCase__ = DebertaVaTokenizer(SCREAMING_SNAKE_CASE_ , do_lower_case=SCREAMING_SNAKE_CASE_ , split_by_punct=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = tokenizer.convert_ids_to_tokens(tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = DebertaVaTokenizerFast(SCREAMING_SNAKE_CASE_ , do_lower_case=SCREAMING_SNAKE_CASE_ , split_by_punct=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): # fmt: off UpperCamelCase__ = """I was born in 92000, and this is falsé.""" UpperCamelCase__ = ["""▁""", """<unk>""", """▁was""", """▁born""", """▁in""", """▁9""", """2000""", """▁""", """,""", """▁and""", """▁this""", """▁is""", """▁fal""", """s""", """<unk>""", """▁""", """.""", ] # fmt: on UpperCamelCase__ = DebertaVaTokenizer(SCREAMING_SNAKE_CASE_ , do_lower_case=SCREAMING_SNAKE_CASE_ , split_by_punct=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = tokenizer.convert_ids_to_tokens(tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = DebertaVaTokenizerFast(SCREAMING_SNAKE_CASE_ , do_lower_case=SCREAMING_SNAKE_CASE_ , split_by_punct=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): # fmt: off UpperCamelCase__ = """ \tHeLLo!how \n Are yoU? """ UpperCamelCase__ = ["""▁""", """<unk>""", """e""", """<unk>""", """o""", """!""", """how""", """▁""", """<unk>""", """re""", """▁yo""", """<unk>""", """?"""] # fmt: on UpperCamelCase__ = DebertaVaTokenizer(SCREAMING_SNAKE_CASE_ , do_lower_case=SCREAMING_SNAKE_CASE_ , split_by_punct=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = tokenizer.convert_ids_to_tokens(tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = DebertaVaTokenizerFast(SCREAMING_SNAKE_CASE_ , do_lower_case=SCREAMING_SNAKE_CASE_ , split_by_punct=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): UpperCamelCase__ = self.get_tokenizer() UpperCamelCase__ = self.get_rust_tokenizer() UpperCamelCase__ = """I was born in 92000, and this is falsé.""" UpperCamelCase__ = tokenizer.convert_ids_to_tokens(tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) ) UpperCamelCase__ = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = rust_tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = self.get_rust_tokenizer() UpperCamelCase__ = tokenizer.encode(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = rust_tokenizer.encode(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): UpperCamelCase__ = """This is a test""" UpperCamelCase__ = [13, 1, 43_98, 25, 21, 12_89] UpperCamelCase__ = ["""▁""", """T""", """his""", """▁is""", """▁a""", """▁test"""] UpperCamelCase__ = ["""▁""", """<unk>""", """his""", """▁is""", """▁a""", """▁test"""] UpperCamelCase__ = DebertaVaTokenizer(SCREAMING_SNAKE_CASE_ , keep_accents=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = DebertaVaTokenizerFast(SCREAMING_SNAKE_CASE_ , keep_accents=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = tokenizer.convert_ids_to_tokens(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = rust_tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = rust_tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = rust_tokenizer.convert_ids_to_tokens(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # fmt: off UpperCamelCase__ = """I was born in 92000, and this is falsé.""" UpperCamelCase__ = [13, 1, 23, 3_86, 19, 5_61, 30_50, 15, 17, 48, 25, 82_56, 18, 1, 9] UpperCamelCase__ = ["""▁""", """I""", """▁was""", """▁born""", """▁in""", """▁9""", """2000""", """,""", """▁and""", """▁this""", """▁is""", """▁fal""", """s""", """é""", """.""", ] UpperCamelCase__ = ["""▁""", """<unk>""", """▁was""", """▁born""", """▁in""", """▁9""", """2000""", """,""", """▁and""", """▁this""", """▁is""", """▁fal""", """s""", """<unk>""", """.""", ] # fmt: on UpperCamelCase__ = tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = tokenizer.convert_ids_to_tokens(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = rust_tokenizer.encode(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = rust_tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = rust_tokenizer.convert_ids_to_tokens(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): UpperCamelCase__ = DebertaVaTokenizer(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = tokenizer.encode("""sequence builders""" ) UpperCamelCase__ = tokenizer.encode("""multi-sequence build""" ) UpperCamelCase__ = tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertEqual([tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] , SCREAMING_SNAKE_CASE_ ) self.assertEqual( [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [tokenizer.sep_token_id] , SCREAMING_SNAKE_CASE_ , ) @slow def UpperCAmelCase_ (self ): # fmt: off UpperCamelCase__ = {"""input_ids""": [[1, 3_98_67, 36, 1_93_90, 4_86, 27, 3_50_52, 8_14_36, 18, 6_06_85, 12_25, 7, 3_50_52, 8_14_36, 18, 93_67, 1_68_99, 18, 1_59_37, 53, 5_94, 7_73, 18, 1_62_87, 3_04_65, 36, 1_59_37, 6, 4_11_39, 38, 3_69_79, 6_07_63, 1_91, 6, 3_41_32, 99, 6, 5_05_38, 3_90, 4_32_30, 6, 3_41_32, 27_79, 2_08_50, 14, 6_99, 10_72, 11_94, 36, 3_82, 1_09_01, 53, 7, 6_99, 10_72, 20_84, 36, 2_04_22, 6_30, 53, 19, 1_05, 30_49, 18_96, 10_53, 1_68_99, 15_06, 11, 3_79_78, 42_43, 7, 12_37, 3_18_69, 2_00, 1_65_66, 6_54, 6, 3_50_52, 8_14_36, 7, 5_56_30, 1_35_93, 4, 2], [1, 26, 1_50_11, 13, 6_67, 8, 10_53, 18, 2_36_11, 12_37, 7_23_56, 1_28_20, 34, 10_41_34, 12_09, 35, 1_33_13, 66_27, 21, 2_02, 3_47, 7, 1_64, 23_99, 11, 46, 44_85, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 5, 12_32, 28_64, 1_57_85, 1_49_51, 1_05, 5, 85_81, 12_50, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], """token_type_ids""": [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=SCREAMING_SNAKE_CASE_ , model_name="""microsoft/deberta-v2-xlarge""" , revision="""ad6e42c1532ddf3a15c39246b63f5559d558b670""" , )
244
import copy import tempfile import unittest from transformers import MaMaaaConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from transformers.utils import cached_property from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaTokenizer from transformers.models.mam_aaa.modeling_mam_aaa import MaMaaaDecoder, MaMaaaEncoder def __magic_name__ ( __a : List[str] , __a : List[Any] , __a : int , __a : Optional[int]=None , __a : Union[str, Any]=None , __a : Union[str, Any]=None , __a : Union[str, Any]=None , __a : Tuple=None , ): '''simple docstring''' if attention_mask is None: UpperCamelCase__ = input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: UpperCamelCase__ = decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: UpperCamelCase__ = torch.ones(config.encoder_layers , config.encoder_attention_heads , device=__a ) if decoder_head_mask is None: UpperCamelCase__ = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=__a ) if cross_attn_head_mask is None: UpperCamelCase__ = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=__a ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } class __A: """simple docstring""" def __init__(self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=13 , SCREAMING_SNAKE_CASE_=7 , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=99 , SCREAMING_SNAKE_CASE_=16 , SCREAMING_SNAKE_CASE_=2 , SCREAMING_SNAKE_CASE_=4 , SCREAMING_SNAKE_CASE_=4 , SCREAMING_SNAKE_CASE_="relu" , SCREAMING_SNAKE_CASE_=0.1 , SCREAMING_SNAKE_CASE_=0.1 , SCREAMING_SNAKE_CASE_=0.0 , SCREAMING_SNAKE_CASE_=0.0 , SCREAMING_SNAKE_CASE_=20 , SCREAMING_SNAKE_CASE_=2 , SCREAMING_SNAKE_CASE_=1 , SCREAMING_SNAKE_CASE_=0 , ): UpperCamelCase__ = parent UpperCamelCase__ = batch_size UpperCamelCase__ = seq_length UpperCamelCase__ = is_training UpperCamelCase__ = use_labels UpperCamelCase__ = vocab_size UpperCamelCase__ = hidden_size UpperCamelCase__ = num_hidden_layers UpperCamelCase__ = num_attention_heads UpperCamelCase__ = intermediate_size UpperCamelCase__ = hidden_act UpperCamelCase__ = hidden_dropout_prob UpperCamelCase__ = attention_probs_dropout_prob UpperCamelCase__ = encoder_layerdrop UpperCamelCase__ = decoder_layerdrop UpperCamelCase__ = max_position_embeddings UpperCamelCase__ = eos_token_id UpperCamelCase__ = pad_token_id UpperCamelCase__ = bos_token_id def UpperCAmelCase_ (self ): UpperCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase__ = self.eos_token_id # Eos Token UpperCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for M2M100 the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input UpperCamelCase__ = input_ids.clamp(self.pad_token_id + 1 ) UpperCamelCase__ = decoder_input_ids.clamp(self.pad_token_id + 1 ) UpperCamelCase__ = self.get_config() UpperCamelCase__ = prepare_mam_aaa_inputs_dict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return config, inputs_dict def UpperCAmelCase_ (self ): return MaMaaaConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , encoder_layerdrop=self.encoder_layerdrop , decoder_layerdrop=self.decoder_layerdrop , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , ) def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ = self.prepare_config_and_inputs() return config, inputs_dict def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): UpperCamelCase__ = MaMaaaModel(config=SCREAMING_SNAKE_CASE_ ).get_decoder().to(SCREAMING_SNAKE_CASE_ ).eval() UpperCamelCase__ = inputs_dict["""input_ids"""] UpperCamelCase__ = inputs_dict["""attention_mask"""] UpperCamelCase__ = inputs_dict["""head_mask"""] # first forward pass UpperCamelCase__ = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , head_mask=SCREAMING_SNAKE_CASE_ , use_cache=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ , UpperCamelCase__ = outputs.to_tuple() # create hypothetical multiple next token and extent to next_input_ids UpperCamelCase__ = ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCamelCase__ = ids_tensor((self.batch_size, 3) , 2 ) # append to next input_ids and UpperCamelCase__ = torch.cat([input_ids, next_tokens] , dim=-1 ) UpperCamelCase__ = torch.cat([attention_mask, next_attn_mask] , dim=-1 ) UpperCamelCase__ = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ )["""last_hidden_state"""] UpperCamelCase__ = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , past_key_values=SCREAMING_SNAKE_CASE_ )[ """last_hidden_state""" ] # select random slice UpperCamelCase__ = ids_tensor((1,) , output_from_past.shape[-1] ).item() UpperCamelCase__ = output_from_no_past[:, -3:, random_slice_idx].detach() UpperCamelCase__ = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1E-2 ) ) def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): UpperCamelCase__ = MaMaaaModel(config=SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ).eval() UpperCamelCase__ = model(**SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = outputs.encoder_last_hidden_state UpperCamelCase__ = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: UpperCamelCase__ = model.get_encoder() encoder.save_pretrained(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = MaMaaaEncoder.from_pretrained(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = encoder(inputs_dict["""input_ids"""] , attention_mask=inputs_dict["""attention_mask"""] )[ 0 ] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3 ) with tempfile.TemporaryDirectory() as tmpdirname: UpperCamelCase__ = model.get_decoder() decoder.save_pretrained(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = MaMaaaDecoder.from_pretrained(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = decoder( input_ids=inputs_dict["""decoder_input_ids"""] , attention_mask=inputs_dict["""decoder_attention_mask"""] , encoder_hidden_states=SCREAMING_SNAKE_CASE_ , encoder_attention_mask=inputs_dict["""attention_mask"""] , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3 ) @require_torch class __A( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , unittest.TestCase ): """simple docstring""" SCREAMING_SNAKE_CASE__ = ( ( MaMaaaModel, MaMaaaForConditionalGeneration, ) if is_torch_available() else () ) SCREAMING_SNAKE_CASE__ = (MaMaaaForConditionalGeneration,) if is_torch_available() else () SCREAMING_SNAKE_CASE__ = ( { """conversational""": MaMaaaForConditionalGeneration, """feature-extraction""": MaMaaaModel, """summarization""": MaMaaaForConditionalGeneration, """text2text-generation""": MaMaaaForConditionalGeneration, """translation""": MaMaaaForConditionalGeneration, } if is_torch_available() else {} ) SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): if pipeline_test_casse_name == "TranslationPipelineTests": # Get `ValueError: Translation requires a `src_lang` and a `tgt_lang` for this model`. # `M2M100Config` was never used in pipeline tests: cannot create a simple tokenizer. return True return False def UpperCAmelCase_ (self ): UpperCamelCase__ = MaMaaaModelTester(self ) UpperCamelCase__ = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): self.config_tester.run_common_tests() def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: UpperCamelCase__ = model_class(SCREAMING_SNAKE_CASE_ ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ , UpperCamelCase__ = model_class.from_pretrained(SCREAMING_SNAKE_CASE_ , output_loading_info=SCREAMING_SNAKE_CASE_ ) self.assertEqual(info["""missing_keys"""] , [] ) def UpperCAmelCase_ (self ): UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(*SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): UpperCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in (MaMaaaModel, MaMaaaForConditionalGeneration): UpperCamelCase__ = model_class(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() UpperCamelCase__ = copy.deepcopy(self._prepare_for_class(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) if not self.is_encoder_decoder: UpperCamelCase__ = inputs["""input_ids"""] del inputs["input_ids"] else: UpperCamelCase__ = inputs["""input_ids"""] UpperCamelCase__ = inputs.get("""decoder_input_ids""" , SCREAMING_SNAKE_CASE_ ) del inputs["input_ids"] inputs.pop("""decoder_input_ids""" , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = model.get_input_embeddings() if not self.is_encoder_decoder: UpperCamelCase__ = wte(SCREAMING_SNAKE_CASE_ ) else: UpperCamelCase__ = wte(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = wte(SCREAMING_SNAKE_CASE_ ) with torch.no_grad(): model(**SCREAMING_SNAKE_CASE_ )[0] def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() UpperCamelCase__ = input_dict["""input_ids"""] UpperCamelCase__ = input_ids.ne(1 ).to(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = MaMaaaForConditionalGeneration(SCREAMING_SNAKE_CASE_ ).eval().to(SCREAMING_SNAKE_CASE_ ) if torch_device == "cuda": model.half() model.generate(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ ) model.generate(num_beams=4 , do_sample=SCREAMING_SNAKE_CASE_ , early_stopping=SCREAMING_SNAKE_CASE_ , num_return_sequences=3 ) def __magic_name__ ( __a : List[Any] ): '''simple docstring''' return torch.tensor(__a , dtype=torch.long , device=__a ) lowerCamelCase_ = 1E-4 @require_torch @require_sentencepiece @require_tokenizers @slow class __A( unittest.TestCase ): """simple docstring""" @cached_property def UpperCAmelCase_ (self ): return MaMaaaTokenizer.from_pretrained("""facebook/m2m100_418M""" ) def UpperCAmelCase_ (self ): UpperCamelCase__ = MaMaaaModel.from_pretrained("""facebook/m2m100_418M""" ).to(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = _long_tensor([[12_80_28, 98, 12, 3_05_27, 27_32, 1_59, 77_55, 6_19_04, 3_91_44, 38, 2]] ) UpperCamelCase__ = _long_tensor([[2, 12_80_28, 98, 12, 3_05_27, 27_32, 1_59, 77_55, 6_19_04, 3_91_44, 38]] ) UpperCamelCase__ = prepare_mam_aaa_inputs_dict(model.config , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) with torch.no_grad(): UpperCamelCase__ = model(**SCREAMING_SNAKE_CASE_ )[0] UpperCamelCase__ = torch.Size((1, 11, 10_24) ) self.assertEqual(output.shape , SCREAMING_SNAKE_CASE_ ) # change to expected output here UpperCamelCase__ = torch.tensor( [[-0.7780, -0.1676, 0.1038], [-6.7556, -1.3992, 0.0567], [-7.5383, -0.5920, -0.2779]] , device=SCREAMING_SNAKE_CASE_ ) self.assertTrue(torch.allclose(output[:, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=SCREAMING_SNAKE_CASE_ ) ) def UpperCAmelCase_ (self ): UpperCamelCase__ = MaMaaaForConditionalGeneration.from_pretrained("""facebook/m2m100_418M""" ).to(SCREAMING_SNAKE_CASE_ ) # change to intended input UpperCamelCase__ = _long_tensor([[12_80_28, 98, 12, 3_05_27, 27_32, 1_59, 77_55, 6_19_04, 3_91_44, 38, 2]] ) UpperCamelCase__ = _long_tensor([[2, 12_80_28, 98, 12, 3_05_27, 27_32, 1_59, 77_55, 6_19_04, 3_91_44, 38]] ) UpperCamelCase__ = prepare_mam_aaa_inputs_dict(model.config , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) with torch.no_grad(): UpperCamelCase__ = model(**SCREAMING_SNAKE_CASE_ )[0] UpperCamelCase__ = torch.Size((1, 11, model.config.vocab_size) ) self.assertEqual(output.shape , SCREAMING_SNAKE_CASE_ ) # change to expected output here UpperCamelCase__ = torch.tensor( [[-1.0448, -1.0411, 3.7992], [-3.2191, -3.2386, -1.3451], [-3.6210, -3.5993, 0.4925]] , device=SCREAMING_SNAKE_CASE_ ) self.assertTrue(torch.allclose(output[:, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=SCREAMING_SNAKE_CASE_ ) ) def UpperCAmelCase_ (self ): UpperCamelCase__ = MaMaaaForConditionalGeneration.from_pretrained("""facebook/m2m100_418M""" ).to(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = MaMaaaTokenizer.from_pretrained("""facebook/m2m100_418M""" , src_lang="""fr""" , tgt_lang="""en""" ) UpperCamelCase__ = [ """L'affaire NSA souligne l'absence totale de débat sur le renseignement""", """Selon moi, il y a deux niveaux de réponse de la part du gouvernement français.""", """Lorsque François Hollande téléphone à Barack Obama ou quand le ministre des affaires étrangères Laurent""" """ Fabius convoque l'ambassadeur des Etats-Unis, ils réagissent à une vraie découverte, qui est celle de""" """ l'ampleur de la surveillance américaine sur l'ensemble des communications en France.""", ] # The below article tests that we don't add any hypotheses outside of the top n_beams UpperCamelCase__ = tokenizer(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , return_tensors="""pt""" ) UpperCamelCase__ = model.generate( input_ids=dct["""input_ids"""].to(SCREAMING_SNAKE_CASE_ ) , attention_mask=dct["""attention_mask"""].to(SCREAMING_SNAKE_CASE_ ) , num_beams=5 , forced_bos_token_id=tokenizer.get_lang_id("""en""" ) , ) UpperCamelCase__ = [ """The NSA case highlights the total absence of intelligence debate""", """I think there are two levels of response from the French government.""", """When François Hollande calls Barack Obama or when Foreign Minister Laurent Fabius calls the U.S.""" """ Ambassador, they respond to a real discovery, which is that of the scale of U.S. surveillance on all""" """ communications in France.""", ] UpperCamelCase__ = tokenizer.batch_decode( hypotheses_batch.tolist() , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ , skip_special_tokens=SCREAMING_SNAKE_CASE_ ) assert generated == expected_en
244
1
'''simple docstring''' import hashlib import unittest from typing import Dict import numpy as np from transformers import ( MODEL_FOR_MASK_GENERATION_MAPPING, TF_MODEL_FOR_MASK_GENERATION_MAPPING, is_vision_available, pipeline, ) from transformers.pipelines import MaskGenerationPipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) if is_vision_available(): from PIL import Image else: class _SCREAMING_SNAKE_CASE : @staticmethod def snake_case__ ( *a__ : Union[str, Any] , **a__ : Any ): pass def UpperCamelCase ( a ) -> str: '''simple docstring''' __magic_name__ = hashlib.mda(image.tobytes() ) return m.hexdigest()[:10] def UpperCamelCase ( a ) -> Dict: '''simple docstring''' __magic_name__ = np.array(a ) __magic_name__ = npimg.shape return {"hash": hashimage(a ), "shape": shape} @is_pipeline_test @require_vision @require_torch class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): __SCREAMING_SNAKE_CASE :Tuple = dict( (list(MODEL_FOR_MASK_GENERATION_MAPPING.items() ) if MODEL_FOR_MASK_GENERATION_MAPPING else []) ) __SCREAMING_SNAKE_CASE :Any = dict( (list(TF_MODEL_FOR_MASK_GENERATION_MAPPING.items() ) if TF_MODEL_FOR_MASK_GENERATION_MAPPING else []) ) def snake_case__ ( self : List[str] , a__ : Optional[int] , a__ : str , a__ : List[Any] ): __magic_name__ = MaskGenerationPipeline(model=a__ , image_processor=a__ ) return image_segmenter, [ "./tests/fixtures/tests_samples/COCO/000000039769.png", "./tests/fixtures/tests_samples/COCO/000000039769.png", ] def snake_case__ ( self : str , a__ : List[Any] , a__ : str ): pass @require_tf @unittest.skip('''Image segmentation not implemented in TF''' ) def snake_case__ ( self : Optional[int] ): pass @slow @require_torch def snake_case__ ( self : List[str] ): __magic_name__ = pipeline('''mask-generation''' , model='''facebook/sam-vit-huge''' ) __magic_name__ = image_segmenter('''http://images.cocodataset.org/val2017/000000039769.jpg''' , points_per_batch=256 ) # Shortening by hashing __magic_name__ = [] for i, o in enumerate(outputs['''masks'''] ): new_outupt += [{"mask": mask_to_test_readable(a__ ), "scores": outputs["scores"][i]}] # fmt: off self.assertEqual( nested_simplify(a__ , decimals=4 ) , [ {'''mask''': {'''hash''': '''115ad19f5f''', '''shape''': (480, 640)}, '''scores''': 1.0_444}, {'''mask''': {'''hash''': '''6affa964c6''', '''shape''': (480, 640)}, '''scores''': 1.021}, {'''mask''': {'''hash''': '''dfe28a0388''', '''shape''': (480, 640)}, '''scores''': 1.0_167}, {'''mask''': {'''hash''': '''c0a5f4a318''', '''shape''': (480, 640)}, '''scores''': 1.0_132}, {'''mask''': {'''hash''': '''fe8065c197''', '''shape''': (480, 640)}, '''scores''': 1.0_053}, {'''mask''': {'''hash''': '''e2d0b7a0b7''', '''shape''': (480, 640)}, '''scores''': 0.9_967}, {'''mask''': {'''hash''': '''453c7844bd''', '''shape''': (480, 640)}, '''scores''': 0.993}, {'''mask''': {'''hash''': '''3d44f2926d''', '''shape''': (480, 640)}, '''scores''': 0.9_909}, {'''mask''': {'''hash''': '''64033ddc3f''', '''shape''': (480, 640)}, '''scores''': 0.9_879}, {'''mask''': {'''hash''': '''801064ff79''', '''shape''': (480, 640)}, '''scores''': 0.9_834}, {'''mask''': {'''hash''': '''6172f276ef''', '''shape''': (480, 640)}, '''scores''': 0.9_716}, {'''mask''': {'''hash''': '''b49e60e084''', '''shape''': (480, 640)}, '''scores''': 0.9_612}, {'''mask''': {'''hash''': '''a811e775fd''', '''shape''': (480, 640)}, '''scores''': 0.9_599}, {'''mask''': {'''hash''': '''a6a8ebcf4b''', '''shape''': (480, 640)}, '''scores''': 0.9_552}, {'''mask''': {'''hash''': '''9d8257e080''', '''shape''': (480, 640)}, '''scores''': 0.9_532}, {'''mask''': {'''hash''': '''32de6454a8''', '''shape''': (480, 640)}, '''scores''': 0.9_516}, {'''mask''': {'''hash''': '''af3d4af2c8''', '''shape''': (480, 640)}, '''scores''': 0.9_499}, {'''mask''': {'''hash''': '''3c6db475fb''', '''shape''': (480, 640)}, '''scores''': 0.9_483}, {'''mask''': {'''hash''': '''c290813fb9''', '''shape''': (480, 640)}, '''scores''': 0.9_464}, {'''mask''': {'''hash''': '''b6f0b8f606''', '''shape''': (480, 640)}, '''scores''': 0.943}, {'''mask''': {'''hash''': '''92ce16bfdf''', '''shape''': (480, 640)}, '''scores''': 0.943}, {'''mask''': {'''hash''': '''c749b25868''', '''shape''': (480, 640)}, '''scores''': 0.9_408}, {'''mask''': {'''hash''': '''efb6cab859''', '''shape''': (480, 640)}, '''scores''': 0.9_335}, {'''mask''': {'''hash''': '''1ff2eafb30''', '''shape''': (480, 640)}, '''scores''': 0.9_326}, {'''mask''': {'''hash''': '''788b798e24''', '''shape''': (480, 640)}, '''scores''': 0.9_262}, {'''mask''': {'''hash''': '''abea804f0e''', '''shape''': (480, 640)}, '''scores''': 0.8_999}, {'''mask''': {'''hash''': '''7b9e8ddb73''', '''shape''': (480, 640)}, '''scores''': 0.8_986}, {'''mask''': {'''hash''': '''cd24047c8a''', '''shape''': (480, 640)}, '''scores''': 0.8_984}, {'''mask''': {'''hash''': '''6943e6bcbd''', '''shape''': (480, 640)}, '''scores''': 0.8_873}, {'''mask''': {'''hash''': '''b5f47c9191''', '''shape''': (480, 640)}, '''scores''': 0.8_871} ] , ) # fmt: on @require_torch @slow def snake_case__ ( self : Dict ): __magic_name__ = '''facebook/sam-vit-huge''' __magic_name__ = pipeline('''mask-generation''' , model=a__ ) __magic_name__ = image_segmenter( '''http://images.cocodataset.org/val2017/000000039769.jpg''' , pred_iou_thresh=1 , points_per_batch=256 ) # Shortening by hashing __magic_name__ = [] for i, o in enumerate(outputs['''masks'''] ): new_outupt += [{"mask": mask_to_test_readable(a__ ), "scores": outputs["scores"][i]}] self.assertEqual( nested_simplify(a__ , decimals=4 ) , [ {'''mask''': {'''hash''': '''115ad19f5f''', '''shape''': (480, 640)}, '''scores''': 1.0_444}, {'''mask''': {'''hash''': '''6affa964c6''', '''shape''': (480, 640)}, '''scores''': 1.0_210}, {'''mask''': {'''hash''': '''dfe28a0388''', '''shape''': (480, 640)}, '''scores''': 1.0_167}, {'''mask''': {'''hash''': '''c0a5f4a318''', '''shape''': (480, 640)}, '''scores''': 1.0_132}, {'''mask''': {'''hash''': '''fe8065c197''', '''shape''': (480, 640)}, '''scores''': 1.0_053}, ] , )
350
'''simple docstring''' import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class _SCREAMING_SNAKE_CASE ( __a ,__a ,__a ,unittest.TestCase ): __SCREAMING_SNAKE_CASE :Union[str, Any] = StableDiffusionInpaintPipeline __SCREAMING_SNAKE_CASE :Optional[int] = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS __SCREAMING_SNAKE_CASE :Tuple = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __SCREAMING_SNAKE_CASE :str = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess __SCREAMING_SNAKE_CASE :Optional[Any] = frozenset([] ) def snake_case__ ( self : Union[str, Any] ): torch.manual_seed(0 ) __magic_name__ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=9 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=a__ , ) __magic_name__ = PNDMScheduler(skip_prk_steps=a__ ) torch.manual_seed(0 ) __magic_name__ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) __magic_name__ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act='''gelu''' , projection_dim=512 , ) __magic_name__ = CLIPTextModel(a__ ) __magic_name__ = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __magic_name__ = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def snake_case__ ( self : Any , a__ : Optional[int] , a__ : List[Any]=0 ): # TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched __magic_name__ = floats_tensor((1, 3, 32, 32) , rng=random.Random(a__ ) ).to(a__ ) __magic_name__ = image.cpu().permute(0 , 2 , 3 , 1 )[0] __magic_name__ = Image.fromarray(np.uinta(a__ ) ).convert('''RGB''' ).resize((64, 64) ) __magic_name__ = Image.fromarray(np.uinta(image + 4 ) ).convert('''RGB''' ).resize((64, 64) ) if str(a__ ).startswith('''mps''' ): __magic_name__ = torch.manual_seed(a__ ) else: __magic_name__ = torch.Generator(device=a__ ).manual_seed(a__ ) __magic_name__ = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': init_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def snake_case__ ( self : Union[str, Any] ): __magic_name__ = '''cpu''' # ensure determinism for the device-dependent torch.Generator __magic_name__ = self.get_dummy_components() __magic_name__ = StableDiffusionInpaintPipeline(**a__ ) __magic_name__ = sd_pipe.to(a__ ) sd_pipe.set_progress_bar_config(disable=a__ ) __magic_name__ = self.get_dummy_inputs(a__ ) __magic_name__ = sd_pipe(**a__ ).images __magic_name__ = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __magic_name__ = np.array([0.4_727, 0.5_735, 0.3_941, 0.5_446, 0.5_926, 0.4_394, 0.5_062, 0.4_654, 0.4_476] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def snake_case__ ( self : List[Any] ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): def snake_case__ ( self : int ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def snake_case__ ( self : Tuple ): __magic_name__ = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __magic_name__ = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __magic_name__ = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench.npy''' ) __magic_name__ = '''stabilityai/stable-diffusion-2-inpainting''' __magic_name__ = StableDiffusionInpaintPipeline.from_pretrained(a__ , safety_checker=a__ ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) pipe.enable_attention_slicing() __magic_name__ = '''Face of a yellow cat, high resolution, sitting on a park bench''' __magic_name__ = torch.manual_seed(0 ) __magic_name__ = pipe( prompt=a__ , image=a__ , mask_image=a__ , generator=a__ , output_type='''np''' , ) __magic_name__ = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 9E-3 def snake_case__ ( self : List[str] ): __magic_name__ = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __magic_name__ = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __magic_name__ = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench_fp16.npy''' ) __magic_name__ = '''stabilityai/stable-diffusion-2-inpainting''' __magic_name__ = StableDiffusionInpaintPipeline.from_pretrained( a__ , torch_dtype=torch.floataa , safety_checker=a__ , ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) pipe.enable_attention_slicing() __magic_name__ = '''Face of a yellow cat, high resolution, sitting on a park bench''' __magic_name__ = torch.manual_seed(0 ) __magic_name__ = pipe( prompt=a__ , image=a__ , mask_image=a__ , generator=a__ , output_type='''np''' , ) __magic_name__ = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 5E-1 def snake_case__ ( self : List[Any] ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() __magic_name__ = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) __magic_name__ = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) __magic_name__ = '''stabilityai/stable-diffusion-2-inpainting''' __magic_name__ = PNDMScheduler.from_pretrained(a__ , subfolder='''scheduler''' ) __magic_name__ = StableDiffusionInpaintPipeline.from_pretrained( a__ , safety_checker=a__ , scheduler=a__ , torch_dtype=torch.floataa , ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() __magic_name__ = '''Face of a yellow cat, high resolution, sitting on a park bench''' __magic_name__ = torch.manual_seed(0 ) __magic_name__ = pipe( prompt=a__ , image=a__ , mask_image=a__ , generator=a__ , num_inference_steps=2 , output_type='''np''' , ) __magic_name__ = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.65 * 10**9
98
0
import argparse import json import os import torch from torch import nn from transformers import NllbMoeConfig, NllbMoeModel from transformers.modeling_utils import dtype_byte_size from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME def A_ ( _lowerCAmelCase ) -> List[Any]: UpperCamelCase : str = [ "encoder.version", "decoder.version", "model.encoder.version", "model.decoder.version", "decoder.output_projection.weight", "_float_tensor", "encoder.embed_positions._float_tensor", "decoder.embed_positions._float_tensor", ] for k in ignore_keys: state_dict.pop(_lowerCAmelCase , _lowerCAmelCase ) def A_ ( _lowerCAmelCase ) -> Dict: UpperCamelCase , UpperCamelCase : str = emb.weight.shape UpperCamelCase : int = nn.Linear(_lowerCAmelCase , _lowerCAmelCase , bias=_lowerCAmelCase ) UpperCamelCase : str = emb.weight.data return lin_layer def A_ ( _lowerCAmelCase , _lowerCAmelCase=None ) -> Dict: UpperCamelCase : Optional[int] = {} for old_key in state_dict.keys(): UpperCamelCase : Optional[Any] = old_key if "moe_layer.experts." in key: if expert_idx is not None: UpperCamelCase : Any = key.replace("moe_layer.experts.0" , F"""ffn.experts.expert_{expert_idx}""" ) else: UpperCamelCase : List[Any] = key.replace("moe_layer.experts." , "ffn.experts.expert_" ) if "gate" in key: UpperCamelCase : List[Any] = key.replace(".moe_layer.gate.wg" , ".ffn.router.classifier" ) if "fc2" and "experts" not in key: UpperCamelCase : Optional[Any] = key.replace(".fc2." , ".ffn.fc2." ) if "fc1" and "experts" not in key: UpperCamelCase : Dict = key.replace(".fc1." , ".ffn.fc1." ) if ".encoder_attn." in key: UpperCamelCase : List[str] = key.replace(".encoder_attn." , ".cross_attention." ) if "encoder_attn_layer_norm" in key: UpperCamelCase : Union[str, Any] = key.replace("encoder_attn_layer_norm" , "cross_attention_layer_norm" ) if "final_layer_norm" in key: UpperCamelCase : Dict = key.replace("final_layer_norm" , "ff_layer_norm" ) UpperCamelCase : Optional[int] = state_dict[old_key] return new_dict def A_ ( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase = WEIGHTS_NAME ) -> Optional[int]: UpperCamelCase : Optional[Any] = [] UpperCamelCase : Optional[int] = 0 os.makedirs(_lowerCAmelCase , exist_ok=_lowerCAmelCase ) for expert in range(_lowerCAmelCase ): UpperCamelCase : Tuple = switch_checkpoint_path + F"""-rank-{expert}.pt""" if os.path.isfile(_lowerCAmelCase ): UpperCamelCase : str = torch.load(_lowerCAmelCase )["model"] remove_ignore_keys_(_lowerCAmelCase ) UpperCamelCase : List[Any] = rename_fairseq_keys(_lowerCAmelCase , _lowerCAmelCase ) UpperCamelCase : Optional[int] = os.path.join( _lowerCAmelCase , weights_name.replace(".bin" , F"""-{len(_lowerCAmelCase )+1:05d}-of-???.bin""" ) ) torch.save(_lowerCAmelCase , _lowerCAmelCase ) sharded_state_dicts.append(expert_state.keys() ) total_size += sum([value.numel() for key, value in expert_state.items()] ) * dtype_byte_size( expert_state[list(_lowerCAmelCase )[0]].dtype ) # Add the last block UpperCamelCase : int = os.path.join(_lowerCAmelCase , weights_name.replace(".bin" , F"""-{len(_lowerCAmelCase )+1:05d}-of-???.bin""" ) ) UpperCamelCase : Optional[int] = torch.load(switch_checkpoint_path + "-shared.pt" )["model"] remove_ignore_keys_(_lowerCAmelCase ) UpperCamelCase : List[Any] = rename_fairseq_keys(_lowerCAmelCase , _lowerCAmelCase ) UpperCamelCase : List[str] = shared_weights["decoder.embed_tokens.weight"] sharded_state_dicts.append(shared_weights.keys() ) # If we only have the shared weights (dummy model/experts saved on the same file) if len(_lowerCAmelCase ) == 1: UpperCamelCase : Tuple = os.path.join(_lowerCAmelCase , _lowerCAmelCase ) torch.save(_lowerCAmelCase , _lowerCAmelCase ) return {weights_name: sharded_state_dicts[0]}, None else: torch.save(_lowerCAmelCase , _lowerCAmelCase ) # Otherwise, let's build the index UpperCamelCase : List[str] = {} for idx, shard in enumerate(_lowerCAmelCase ): UpperCamelCase : Optional[int] = weights_name.replace(".bin" , F"""-{idx+1:05d}-of-{len(_lowerCAmelCase ):05d}.bin""" ) UpperCamelCase : Union[str, Any] = os.path.join(_lowerCAmelCase , weights_name.replace(".bin" , F"""-{idx+1:05d}-of-???.bin""" ) ) os.rename(_lowerCAmelCase , os.path.join(_lowerCAmelCase , _lowerCAmelCase ) ) for key in shard: UpperCamelCase : Optional[Any] = shard_file # Add the metadata UpperCamelCase : Tuple = {"total_size": total_size} UpperCamelCase : str = {"metadata": metadata, "weight_map": weight_map} with open(os.path.join(_lowerCAmelCase , _lowerCAmelCase ) , "w" , encoding="utf-8" ) as f: UpperCamelCase : str = json.dumps(_lowerCAmelCase , indent=2 , sort_keys=_lowerCAmelCase ) + "\n" f.write(_lowerCAmelCase ) return metadata, index if __name__ == "__main__": __lowerCamelCase : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--nllb_moe_checkpoint_path""", default="""/home/arthur_huggingface_co/fairseq/weights/checkpoints/model_moe_54b/checkpoint_2_300000""", type=str, required=False, help="""Path to a directory containing a folder per layer. Follows the original Google format.""", ) parser.add_argument("""--dtype""", default="""float32""", type=str, required=False, help="""dtype of the saved model""") parser.add_argument( """--pytorch_dump_folder_path""", default="""/home/arthur_huggingface_co/fairseq/weights/checkpoints/hf-converted-moe-54b""", type=str, required=False, help="""Path to the output pytorch model.""", ) __lowerCamelCase : str = parser.parse_args() __lowerCamelCase , __lowerCamelCase : int = shard_on_the_fly( args.nllb_moe_checkpoint_path, args.pytorch_dump_folder_path, 128, args.dtype, ) __lowerCamelCase : str = NllbMoeConfig.from_pretrained( """facebook/nllb-200-3.3B""", encoder_sparse_step=4, decoder_sparse_step=4, num_experts=128 ) config.save_pretrained(args.pytorch_dump_folder_path) __lowerCamelCase : Dict = NllbMoeModel.from_pretrained(args.pytorch_dump_folder_path) print("""Done""") model.save_pretrained(args.pytorch_dump_folder_path)
52
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyInpaintPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class A__ ( __snake_case , unittest.TestCase ): _UpperCAmelCase :List[str] = KandinskyInpaintPipeline _UpperCAmelCase :List[str] = ['prompt', 'image_embeds', 'negative_image_embeds', 'image', 'mask_image'] _UpperCAmelCase :Dict = [ 'prompt', 'negative_prompt', 'image_embeds', 'negative_image_embeds', 'image', 'mask_image', ] _UpperCAmelCase :Optional[int] = [ 'generator', 'height', 'width', 'latents', 'guidance_scale', 'negative_prompt', 'num_inference_steps', 'return_dict', 'guidance_scale', 'num_images_per_prompt', 'output_type', 'return_dict', ] _UpperCAmelCase :int = False @property def __UpperCamelCase( self ): '''simple docstring''' return 32 @property def __UpperCamelCase( self ): '''simple docstring''' return 32 @property def __UpperCamelCase( self ): '''simple docstring''' return self.time_input_dim @property def __UpperCamelCase( self ): '''simple docstring''' return self.time_input_dim * 4 @property def __UpperCamelCase( self ): '''simple docstring''' return 100 @property def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Any = XLMRobertaTokenizerFast.from_pretrained("YiYiXu/tiny-random-mclip-base" ) return tokenizer @property def __UpperCamelCase( self ): '''simple docstring''' torch.manual_seed(0 ) UpperCamelCase : Optional[int] = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1005 , ) UpperCamelCase : Optional[int] = MultilingualCLIP(A_ ) UpperCamelCase : Union[str, Any] = text_encoder.eval() return text_encoder @property def __UpperCamelCase( self ): '''simple docstring''' torch.manual_seed(0 ) UpperCamelCase : Optional[int] = { "in_channels": 9, # Out channels is double in channels because predicts mean and variance "out_channels": 8, "addition_embed_type": "text_image", "down_block_types": ("ResnetDownsampleBlock2D", "SimpleCrossAttnDownBlock2D"), "up_block_types": ("SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"), "mid_block_type": "UNetMidBlock2DSimpleCrossAttn", "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "layers_per_block": 1, "encoder_hid_dim": self.text_embedder_hidden_size, "encoder_hid_dim_type": "text_image_proj", "cross_attention_dim": self.cross_attention_dim, "attention_head_dim": 4, "resnet_time_scale_shift": "scale_shift", "class_embed_type": None, } UpperCamelCase : List[Any] = UNetaDConditionModel(**A_ ) return model @property def __UpperCamelCase( self ): '''simple docstring''' return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def __UpperCamelCase( self ): '''simple docstring''' torch.manual_seed(0 ) UpperCamelCase : List[str] = VQModel(**self.dummy_movq_kwargs ) return model def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Any = self.dummy_text_encoder UpperCamelCase : str = self.dummy_tokenizer UpperCamelCase : List[Any] = self.dummy_unet UpperCamelCase : Optional[Any] = self.dummy_movq UpperCamelCase : Union[str, Any] = DDIMScheduler( num_train_timesteps=1000 , beta_schedule="linear" , beta_start=0.0_00_85 , beta_end=0.0_12 , clip_sample=A_ , set_alpha_to_one=A_ , steps_offset=1 , prediction_type="epsilon" , thresholding=A_ , ) UpperCamelCase : Optional[Any] = { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "movq": movq, } return components def __UpperCamelCase( self , A_ , A_=0 ): '''simple docstring''' UpperCamelCase : Dict = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(A_ ) ).to(A_ ) UpperCamelCase : Union[str, Any] = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(A_ ) # create init_image UpperCamelCase : Union[str, Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(A_ ) ).to(A_ ) UpperCamelCase : str = image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCamelCase : List[Any] = Image.fromarray(np.uinta(A_ ) ).convert("RGB" ).resize((256, 256) ) # create mask UpperCamelCase : str = np.ones((64, 64) , dtype=np.floataa ) UpperCamelCase : str = 0 if str(A_ ).startswith("mps" ): UpperCamelCase : int = torch.manual_seed(A_ ) else: UpperCamelCase : Tuple = torch.Generator(device=A_ ).manual_seed(A_ ) UpperCamelCase : Union[str, Any] = { "prompt": "horse", "image": init_image, "mask_image": mask, "image_embeds": image_embeds, "negative_image_embeds": negative_image_embeds, "generator": generator, "height": 64, "width": 64, "num_inference_steps": 2, "guidance_scale": 4.0, "output_type": "np", } return inputs def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Optional[int] = "cpu" UpperCamelCase : Tuple = self.get_dummy_components() UpperCamelCase : str = self.pipeline_class(**A_ ) UpperCamelCase : Tuple = pipe.to(A_ ) pipe.set_progress_bar_config(disable=A_ ) UpperCamelCase : Any = pipe(**self.get_dummy_inputs(A_ ) ) UpperCamelCase : List[Any] = output.images UpperCamelCase : List[Any] = pipe( **self.get_dummy_inputs(A_ ) , return_dict=A_ , )[0] UpperCamelCase : List[Any] = image[0, -3:, -3:, -1] UpperCamelCase : Any = image_from_tuple[0, -3:, -3:, -1] print(F"""image.shape {image.shape}""" ) assert image.shape == (1, 64, 64, 3) UpperCamelCase : Union[str, Any] = np.array( [0.8_32_69_19, 0.73_79_04_67, 0.20_91_85_81, 0.9_30_96_12, 0.5_51_17_91, 0.43_71_33_28, 0.5_51_33_21, 0.49_92_29_34, 0.59_49_77_86] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), F""" expected_slice {expected_slice}, but got {image_slice.flatten()}""" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), F""" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}""" def __UpperCamelCase( self ): '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class A__ ( unittest.TestCase ): def __UpperCamelCase( self ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Optional[Any] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy" ) UpperCamelCase : List[str] = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png" ) UpperCamelCase : Dict = np.ones((768, 768) , dtype=np.floataa ) UpperCamelCase : str = 0 UpperCamelCase : List[Any] = "a hat" UpperCamelCase : Tuple = KandinskyPriorPipeline.from_pretrained( "kandinsky-community/kandinsky-2-1-prior" , torch_dtype=torch.floataa ) pipe_prior.to(A_ ) UpperCamelCase : Union[str, Any] = KandinskyInpaintPipeline.from_pretrained( "kandinsky-community/kandinsky-2-1-inpaint" , torch_dtype=torch.floataa ) UpperCamelCase : Optional[Any] = pipeline.to(A_ ) pipeline.set_progress_bar_config(disable=A_ ) UpperCamelCase : List[Any] = torch.Generator(device="cpu" ).manual_seed(0 ) UpperCamelCase , UpperCamelCase : Optional[Any] = pipe_prior( A_ , generator=A_ , num_inference_steps=5 , negative_prompt="" , ).to_tuple() UpperCamelCase : Dict = pipeline( A_ , image=A_ , mask_image=A_ , image_embeds=A_ , negative_image_embeds=A_ , generator=A_ , num_inference_steps=100 , height=768 , width=768 , output_type="np" , ) UpperCamelCase : List[str] = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(A_ , A_ )
52
1
"""simple docstring""" from diffusers.utils.testing_utils import require_onnxruntime @require_onnxruntime class UpperCamelCase : pass
95
"""simple docstring""" import warnings from ...utils import logging from .image_processing_clip import CLIPImageProcessor _a : Dict= logging.get_logger(__name__) class UpperCamelCase ( lowercase ): def __init__(self : List[str] , *_A : Dict , **_A : Optional[Any]) -> None: warnings.warn( 'The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use CLIPImageProcessor instead.' , _A , ) super().__init__(*_A , **_A)
95
1
"""simple docstring""" import argparse import collections import numpy as np import torch from flax import traverse_util from tax import checkpoints from transformers import MTaConfig, UMTaEncoderModel, UMTaForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() def a__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): """simple docstring""" return params[F"{prefix}/{prefix}/relpos_bias/rel_embedding"][:, i, :] def a__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE="attention" ): """simple docstring""" UpperCamelCase = np.ascontiguousarray(params[F"{prefix}/{prefix}/{layer_name}/key/kernel"][:, i, :, :] ) UpperCamelCase = k_tmp.reshape(k_tmp.shape[0] , k_tmp.shape[1] * k_tmp.shape[2] ) UpperCamelCase = np.ascontiguousarray(params[F"{prefix}/{prefix}/{layer_name}/out/kernel"][:, i, :, :] ) UpperCamelCase = o_tmp.reshape(o_tmp.shape[0] * o_tmp.shape[1] , o_tmp.shape[2] ) UpperCamelCase = np.ascontiguousarray(params[F"{prefix}/{prefix}/{layer_name}/query/kernel"][:, i, :, :] ) UpperCamelCase = q_tmp.reshape(q_tmp.shape[0] , q_tmp.shape[1] * q_tmp.shape[2] ) UpperCamelCase = np.ascontiguousarray(params[F"{prefix}/{prefix}/{layer_name}/value/kernel"][:, i, :, :] ) UpperCamelCase = v_tmp.reshape(v_tmp.shape[0] , v_tmp.shape[1] * v_tmp.shape[2] ) return k, o, q, v def a__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=False ): """simple docstring""" if split_mlp_wi: UpperCamelCase = params[F"{prefix}/{prefix}/mlp/wi_0/kernel"][:, i, :] UpperCamelCase = params[F"{prefix}/{prefix}/mlp/wi_1/kernel"][:, i, :] UpperCamelCase = (wi_a, wi_a) else: UpperCamelCase = params[F"{prefix}/{prefix}/mlp/wi/kernel"][:, i, :] UpperCamelCase = params[F"{prefix}/{prefix}/mlp/wo/kernel"][:, i, :] return wi, wo def a__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): """simple docstring""" return params[F"{prefix}/{prefix}/{layer_name}/scale"][:, i] def a__ ( _SCREAMING_SNAKE_CASE , *, _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = False ): """simple docstring""" UpperCamelCase = traverse_util.flatten_dict(variables["target"] ) UpperCamelCase = {'/'.join(_A ): v for k, v in old.items()} # v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi UpperCamelCase = 'encoder/encoder/mlp/wi_0/kernel' in old print("Split MLP:" , _A ) UpperCamelCase = collections.OrderedDict() # Shared embeddings. UpperCamelCase = old['token_embedder/embedding'] # Encoder. for i in range(_A ): # Block i, layer 0 (Self Attention). UpperCamelCase = tax_layer_norm_lookup(_A , _A , "encoder" , "pre_attention_layer_norm" ) UpperCamelCase = tax_attention_lookup(_A , _A , "encoder" , "attention" ) UpperCamelCase = layer_norm UpperCamelCase = k.T UpperCamelCase = o.T UpperCamelCase = q.T UpperCamelCase = v.T # Block i, layer 1 (MLP). UpperCamelCase = tax_layer_norm_lookup(_A , _A , "encoder" , "pre_mlp_layer_norm" ) UpperCamelCase = tax_mlp_lookup(_A , _A , "encoder" , _A ) UpperCamelCase = layer_norm if split_mlp_wi: UpperCamelCase = wi[0].T UpperCamelCase = wi[1].T else: UpperCamelCase = wi.T UpperCamelCase = wo.T if scalable_attention: # convert the rel_embedding of each layer UpperCamelCase = tax_relpos_bias_lookup( _A , _A , "encoder" ).T UpperCamelCase = old['encoder/encoder_norm/scale'] if not scalable_attention: UpperCamelCase = tax_relpos_bias_lookup( _A , 0 , "encoder" ).T UpperCamelCase = tax_relpos_bias_lookup( _A , 0 , "decoder" ).T if not is_encoder_only: # Decoder. for i in range(_A ): # Block i, layer 0 (Self Attention). UpperCamelCase = tax_layer_norm_lookup(_A , _A , "decoder" , "pre_self_attention_layer_norm" ) UpperCamelCase = tax_attention_lookup(_A , _A , "decoder" , "self_attention" ) UpperCamelCase = layer_norm UpperCamelCase = k.T UpperCamelCase = o.T UpperCamelCase = q.T UpperCamelCase = v.T # Block i, layer 1 (Cross Attention). UpperCamelCase = tax_layer_norm_lookup(_A , _A , "decoder" , "pre_cross_attention_layer_norm" ) UpperCamelCase = tax_attention_lookup(_A , _A , "decoder" , "encoder_decoder_attention" ) UpperCamelCase = layer_norm UpperCamelCase = k.T UpperCamelCase = o.T UpperCamelCase = q.T UpperCamelCase = v.T # Block i, layer 2 (MLP). UpperCamelCase = tax_layer_norm_lookup(_A , _A , "decoder" , "pre_mlp_layer_norm" ) UpperCamelCase = tax_mlp_lookup(_A , _A , "decoder" , _A ) UpperCamelCase = layer_norm if split_mlp_wi: UpperCamelCase = wi[0].T UpperCamelCase = wi[1].T else: UpperCamelCase = wi.T UpperCamelCase = wo.T if scalable_attention: # convert the rel_embedding of each layer UpperCamelCase = tax_relpos_bias_lookup(_A , _A , "decoder" ).T UpperCamelCase = old['decoder/decoder_norm/scale'] # LM Head (only in v1.1 checkpoints, in v1.0 embeddings are used instead) if "decoder/logits_dense/kernel" in old: UpperCamelCase = old['decoder/logits_dense/kernel'].T return new def a__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase = collections.OrderedDict([(k, torch.from_numpy(v.copy() )) for (k, v) in converted_params.items()] ) # Add what is missing. if "encoder.embed_tokens.weight" not in state_dict: UpperCamelCase = state_dict['shared.weight'] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: UpperCamelCase = state_dict['shared.weight'] if "lm_head.weight" not in state_dict: # For old 1.0 models. print("Using shared word embeddings as lm_head." ) UpperCamelCase = state_dict['shared.weight'] return state_dict def a__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase = checkpoints.load_tax_checkpoint(_A ) UpperCamelCase = convert_tax_to_pytorch( _A , num_layers=config.num_layers , is_encoder_only=_A , scalable_attention=_A ) UpperCamelCase = make_state_dict(_A , _A ) model.load_state_dict(_A , strict=_A ) def a__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = False , ): """simple docstring""" UpperCamelCase = MTaConfig.from_json_file(_A ) print(F"Building PyTorch model from configuration: {config}" ) # Non-v1.1 checkpoints could also use T5Model, but this works for all. # The v1.0 checkpoints will simply have an LM head that is the word embeddings. if is_encoder_only: UpperCamelCase = UMTaEncoderModel(_A ) else: UpperCamelCase = UMTaForConditionalGeneration(_A ) # Load weights from tf checkpoint load_tax_weights_in_ta(_A , _A , _A , _A , _A ) # Save pytorch-model print(F"Save PyTorch model to {pytorch_dump_path}" ) model.save_pretrained(_A ) # Verify that we can load the checkpoint. model.from_pretrained(_A ) print("Done" ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser(description='''Converts a native T5X checkpoint into a PyTorch checkpoint.''') # Required parameters parser.add_argument( '''--t5x_checkpoint_path''', default=None, type=str, required=True, help='''Path to the T5X checkpoint.''' ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The config json file corresponding to the pre-trained T5 model.\nThis specifies the model architecture.''', ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--is_encoder_only''', action='''store_true''', help='''Check if the model is encoder-decoder model''', default=False ) parser.add_argument( '''--scalable_attention''', action='''store_true''', help='''Whether the model uses scaled attention (umt5 model)''', default=False, ) lowerCAmelCase__ = parser.parse_args() convert_tax_checkpoint_to_pytorch( args.tax_checkpoint_path, args.config_file, args.pytorch_dump_path, args.is_encoder_only, args.scalable_attention, )
153
'''simple docstring''' import inspect from typing import Optional, Union import numpy as np import PIL import torch from torch.nn import functional as F from torchvision import transforms from transformers import CLIPFeatureExtractor, CLIPModel, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, DPMSolverMultistepScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.utils import ( PIL_INTERPOLATION, randn_tensor, ) def lowerCamelCase__ ( _A , _A , _A ): if isinstance(_A , torch.Tensor ): return image elif isinstance(_A , PIL.Image.Image ): a : Any = [image] if isinstance(image[0] , PIL.Image.Image ): a : List[str] = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION['lanczos'] ) )[None, :] for i in image] a : int = np.concatenate(_A , axis=0 ) a : int = np.array(_A ).astype(np.floataa ) / 255.0 a : str = image.transpose(0 , 3 , 1 , 2 ) a : str = 2.0 * image - 1.0 a : Optional[int] = torch.from_numpy(_A ) elif isinstance(image[0] , torch.Tensor ): a : Optional[Any] = torch.cat(_A , dim=0 ) return image def lowerCamelCase__ ( _A , _A , _A , _A=0.9995 ): if not isinstance(_A , np.ndarray ): a : Dict = True a : Optional[Any] = va.device a : Optional[int] = va.cpu().numpy() a : Union[str, Any] = va.cpu().numpy() a : Any = np.sum(va * va / (np.linalg.norm(_A ) * np.linalg.norm(_A )) ) if np.abs(_A ) > DOT_THRESHOLD: a : Any = (1 - t) * va + t * va else: a : Any = np.arccos(_A ) a : Tuple = np.sin(_A ) a : Optional[Any] = theta_a * t a : List[Any] = np.sin(_A ) a : Dict = np.sin(theta_a - theta_t ) / sin_theta_a a : int = sin_theta_t / sin_theta_a a : Any = sa * va + sa * va if inputs_are_torch: a : Dict = torch.from_numpy(_A ).to(_A ) return va def lowerCamelCase__ ( _A , _A ): a : Optional[int] = F.normalize(_A , dim=-1 ) a : str = F.normalize(_A , dim=-1 ) return (x - y).norm(dim=-1 ).div(2 ).arcsin().pow(2 ).mul(2 ) def lowerCamelCase__ ( _A , _A ): for param in model.parameters(): a : int = value class a__( lowerCamelCase__ ): def __init__( self : str , __snake_case : AutoencoderKL , __snake_case : CLIPTextModel , __snake_case : CLIPModel , __snake_case : CLIPTokenizer , __snake_case : UNetaDConditionModel , __snake_case : Union[PNDMScheduler, LMSDiscreteScheduler, DDIMScheduler, DPMSolverMultistepScheduler] , __snake_case : CLIPFeatureExtractor , __snake_case : List[str]=None , __snake_case : List[str]=None , __snake_case : List[Any]=None , ): super().__init__() self.register_modules( vae=__snake_case , text_encoder=__snake_case , clip_model=__snake_case , tokenizer=__snake_case , unet=__snake_case , scheduler=__snake_case , feature_extractor=__snake_case , coca_model=__snake_case , coca_tokenizer=__snake_case , coca_transform=__snake_case , ) a : Optional[Any] = ( feature_extractor.size if isinstance(feature_extractor.size , __snake_case ) else feature_extractor.size['shortest_edge'] ) a : Optional[int] = transforms.Normalize(mean=feature_extractor.image_mean , std=feature_extractor.image_std ) set_requires_grad(self.text_encoder , __snake_case ) set_requires_grad(self.clip_model , __snake_case ) def lowercase_ ( self : int , __snake_case : Optional[Union[str, int]] = "auto" ): if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory a : Union[str, Any] = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(__snake_case ) def lowercase_ ( self : Union[str, Any] ): self.enable_attention_slicing(__snake_case ) def lowercase_ ( self : Optional[Any] ): set_requires_grad(self.vae , __snake_case ) def lowercase_ ( self : Tuple ): set_requires_grad(self.vae , __snake_case ) def lowercase_ ( self : int ): set_requires_grad(self.unet , __snake_case ) def lowercase_ ( self : Union[str, Any] ): set_requires_grad(self.unet , __snake_case ) def lowercase_ ( self : int , __snake_case : Dict , __snake_case : str , __snake_case : Optional[int] ): # get the original timestep using init_timestep a : Optional[Any] = min(int(num_inference_steps * strength ) , __snake_case ) a : Union[str, Any] = max(num_inference_steps - init_timestep , 0 ) a : List[Any] = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def lowercase_ ( self : Dict , __snake_case : List[Any] , __snake_case : Union[str, Any] , __snake_case : List[Any] , __snake_case : Union[str, Any] , __snake_case : Any , __snake_case : Optional[Any]=None ): if not isinstance(__snake_case , torch.Tensor ): raise ValueError(F"""`image` has to be of type `torch.Tensor` but is {type(__snake_case )}""" ) a : Optional[Any] = image.to(device=__snake_case , dtype=__snake_case ) if isinstance(__snake_case , __snake_case ): a : Optional[int] = [ self.vae.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(__snake_case ) ] a : Optional[Any] = torch.cat(__snake_case , dim=0 ) else: a : Union[str, Any] = self.vae.encode(__snake_case ).latent_dist.sample(__snake_case ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor a : List[str] = 0.18215 * init_latents a : str = init_latents.repeat_interleave(__snake_case , dim=0 ) a : Dict = randn_tensor(init_latents.shape , generator=__snake_case , device=__snake_case , dtype=__snake_case ) # get latents a : Dict = self.scheduler.add_noise(__snake_case , __snake_case , __snake_case ) a : int = init_latents return latents def lowercase_ ( self : List[str] , __snake_case : Dict ): a : List[Any] = self.coca_transform(__snake_case ).unsqueeze(0 ) with torch.no_grad(), torch.cuda.amp.autocast(): a : Optional[Any] = self.coca_model.generate(transformed_image.to(device=self.device , dtype=self.coca_model.dtype ) ) a : Union[str, Any] = self.coca_tokenizer.decode(generated[0].cpu().numpy() ) return generated.split('<end_of_text>' )[0].replace('<start_of_text>' , '' ).rstrip(' .,' ) def lowercase_ ( self : Tuple , __snake_case : Any , __snake_case : Optional[Any] ): a : List[Any] = self.feature_extractor.preprocess(__snake_case ) a : Optional[Any] = torch.from_numpy(clip_image_input['pixel_values'][0] ).unsqueeze(0 ).to(self.device ).half() a : int = self.clip_model.get_image_features(__snake_case ) a : str = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=__snake_case ) a : Tuple = image_embeddings_clip.repeat_interleave(__snake_case , dim=0 ) return image_embeddings_clip @torch.enable_grad() def lowercase_ ( self : Tuple , __snake_case : Optional[Any] , __snake_case : List[str] , __snake_case : Dict , __snake_case : Union[str, Any] , __snake_case : Dict , __snake_case : Union[str, Any] , __snake_case : List[Any] , ): a : Optional[Any] = latents.detach().requires_grad_() a : List[Any] = self.scheduler.scale_model_input(__snake_case , __snake_case ) # predict the noise residual a : Any = self.unet(__snake_case , __snake_case , encoder_hidden_states=__snake_case ).sample if isinstance(self.scheduler , (PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler) ): a : int = self.scheduler.alphas_cumprod[timestep] a : Any = 1 - alpha_prod_t # compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf a : List[str] = (latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5 a : Tuple = torch.sqrt(__snake_case ) a : str = pred_original_sample * (fac) + latents * (1 - fac) elif isinstance(self.scheduler , __snake_case ): a : List[Any] = self.scheduler.sigmas[index] a : Optional[int] = latents - sigma * noise_pred else: raise ValueError(F"""scheduler type {type(self.scheduler )} not supported""" ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor a : Union[str, Any] = 1 / 0.18215 * sample a : str = self.vae.decode(__snake_case ).sample a : List[Any] = (image / 2 + 0.5).clamp(0 , 1 ) a : Tuple = transforms.Resize(self.feature_extractor_size )(__snake_case ) a : List[str] = self.normalize(__snake_case ).to(latents.dtype ) a : List[str] = self.clip_model.get_image_features(__snake_case ) a : Tuple = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=__snake_case ) a : int = spherical_dist_loss(__snake_case , __snake_case ).mean() * clip_guidance_scale a : List[str] = -torch.autograd.grad(__snake_case , __snake_case )[0] if isinstance(self.scheduler , __snake_case ): a : List[Any] = latents.detach() + grads * (sigma**2) a : Optional[int] = noise_pred_original else: a : List[Any] = noise_pred_original - torch.sqrt(__snake_case ) * grads return noise_pred, latents @torch.no_grad() def __call__( self : Optional[int] , __snake_case : Union[torch.FloatTensor, PIL.Image.Image] , __snake_case : Union[torch.FloatTensor, PIL.Image.Image] , __snake_case : Optional[str] = None , __snake_case : Optional[str] = None , __snake_case : Optional[int] = 5_12 , __snake_case : Optional[int] = 5_12 , __snake_case : float = 0.6 , __snake_case : Optional[int] = 50 , __snake_case : Optional[float] = 7.5 , __snake_case : Optional[int] = 1 , __snake_case : float = 0.0 , __snake_case : Optional[float] = 1_00 , __snake_case : Optional[torch.Generator] = None , __snake_case : Optional[str] = "pil" , __snake_case : bool = True , __snake_case : float = 0.8 , __snake_case : float = 0.1 , __snake_case : float = 0.1 , ): if isinstance(__snake_case , __snake_case ) and len(__snake_case ) != batch_size: raise ValueError(F"""You have passed {batch_size} batch_size, but only {len(__snake_case )} generators.""" ) if height % 8 != 0 or width % 8 != 0: raise ValueError(F"""`height` and `width` have to be divisible by 8 but are {height} and {width}.""" ) if isinstance(__snake_case , torch.Generator ) and batch_size > 1: a : Dict = [generator] + [None] * (batch_size - 1) a : Any = [ ('model', self.coca_model is None), ('tokenizer', self.coca_tokenizer is None), ('transform', self.coca_transform is None), ] a : List[str] = [x[0] for x in coca_is_none if x[1]] a : List[str] = ', '.join(__snake_case ) # generate prompts with coca model if prompt is None if content_prompt is None: if len(__snake_case ): raise ValueError( F"""Content prompt is None and CoCa [{coca_is_none_str}] is None.""" F"""Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.""" ) a : int = self.get_image_description(__snake_case ) if style_prompt is None: if len(__snake_case ): raise ValueError( F"""Style prompt is None and CoCa [{coca_is_none_str}] is None.""" F""" Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.""" ) a : Union[str, Any] = self.get_image_description(__snake_case ) # get prompt text embeddings for content and style a : Optional[Any] = self.tokenizer( __snake_case , padding='max_length' , max_length=self.tokenizer.model_max_length , truncation=__snake_case , return_tensors='pt' , ) a : Dict = self.text_encoder(content_text_input.input_ids.to(self.device ) )[0] a : Dict = self.tokenizer( __snake_case , padding='max_length' , max_length=self.tokenizer.model_max_length , truncation=__snake_case , return_tensors='pt' , ) a : Dict = self.text_encoder(style_text_input.input_ids.to(self.device ) )[0] a : Any = slerp(__snake_case , __snake_case , __snake_case ) # duplicate text embeddings for each generation per prompt a : Optional[Any] = text_embeddings.repeat_interleave(__snake_case , dim=0 ) # set timesteps a : int = 'offset' in set(inspect.signature(self.scheduler.set_timesteps ).parameters.keys() ) a : Any = {} if accepts_offset: a : Optional[Any] = 1 self.scheduler.set_timesteps(__snake_case , **__snake_case ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand self.scheduler.timesteps.to(self.device ) a , a : Tuple = self.get_timesteps(__snake_case , __snake_case , self.device ) a : Optional[int] = timesteps[:1].repeat(__snake_case ) # Preprocess image a : Optional[Any] = preprocess(__snake_case , __snake_case , __snake_case ) a : List[Any] = self.prepare_latents( __snake_case , __snake_case , __snake_case , text_embeddings.dtype , self.device , __snake_case ) a : str = preprocess(__snake_case , __snake_case , __snake_case ) a : Union[str, Any] = self.prepare_latents( __snake_case , __snake_case , __snake_case , text_embeddings.dtype , self.device , __snake_case ) a : Union[str, Any] = slerp(__snake_case , __snake_case , __snake_case ) if clip_guidance_scale > 0: a : Dict = self.get_clip_image_embeddings(__snake_case , __snake_case ) a : int = self.get_clip_image_embeddings(__snake_case , __snake_case ) a : List[str] = slerp( __snake_case , __snake_case , __snake_case ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. a : int = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: a : Any = content_text_input.input_ids.shape[-1] a : List[Any] = self.tokenizer([''] , padding='max_length' , max_length=__snake_case , return_tensors='pt' ) a : List[str] = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt a : Dict = uncond_embeddings.repeat_interleave(__snake_case , dim=0 ) # 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 : Any = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. a : List[str] = (batch_size, self.unet.config.in_channels, height // 8, width // 8) a : List[str] = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not work reproducibly on mps a : int = torch.randn(__snake_case , generator=__snake_case , device='cpu' , dtype=__snake_case ).to( self.device ) else: a : Optional[int] = torch.randn(__snake_case , generator=__snake_case , device=self.device , dtype=__snake_case ) else: if latents.shape != latents_shape: raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {latents_shape}""" ) a : List[str] = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler a : Any = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] a : Optional[Any] = 'eta' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) a : Union[str, Any] = {} if accepts_eta: a : List[str] = eta # check if the scheduler accepts generator a : List[Any] = 'generator' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) if accepts_generator: a : Any = generator with self.progress_bar(total=__snake_case ): for i, t in enumerate(__snake_case ): # expand the latents if we are doing classifier free guidance a : Tuple = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents a : Dict = self.scheduler.scale_model_input(__snake_case , __snake_case ) # predict the noise residual a : List[Any] = self.unet(__snake_case , __snake_case , encoder_hidden_states=__snake_case ).sample # perform classifier free guidance if do_classifier_free_guidance: a , a : List[str] = noise_pred.chunk(2 ) a : Union[str, Any] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # perform clip guidance if clip_guidance_scale > 0: a : Optional[Any] = ( text_embeddings.chunk(2 )[1] if do_classifier_free_guidance else text_embeddings ) a , a : Union[str, Any] = self.cond_fn( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) # compute the previous noisy sample x_t -> x_t-1 a : Any = self.scheduler.step(__snake_case , __snake_case , __snake_case , **__snake_case ).prev_sample # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor a : Tuple = 1 / 0.18215 * latents a : Optional[int] = self.vae.decode(__snake_case ).sample a : List[str] = (image / 2 + 0.5).clamp(0 , 1 ) a : Any = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": a : str = self.numpy_to_pil(__snake_case ) if not return_dict: return (image, None) return StableDiffusionPipelineOutput(images=__snake_case , nsfw_content_detected=__snake_case )
297
0
'''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 UpperCAmelCase_ (__a : Any , __a : Tuple ): """simple docstring""" assert isinstance(__a , __a ) 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 UpperCAmelCase_ (__a : Optional[Any] , __a : Dict , __a : Any ): """simple docstring""" _a : Dict = 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 : List[Any] = ParquetDatasetReader(__a , cache_dir=__a , keep_in_memory=__a ).read() _check_parquet_dataset(__a , __a ) @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 UpperCAmelCase_ (__a : Tuple , __a : Union[str, Any] , __a : List[Any] ): """simple docstring""" _a : Tuple = tmp_path / 'cache' _a : Optional[int] = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} _a : Dict = features.copy() if features else default_expected_features _a : List[str] = ( Features({feature: Value(__a ) for feature, dtype in features.items()} ) if features is not None else None ) _a : Optional[Any] = ParquetDatasetReader(__a , features=__a , cache_dir=__a ).read() _check_parquet_dataset(__a , __a ) @pytest.mark.parametrize('split' , [None, NamedSplit('train' ), 'train', 'test'] ) def UpperCAmelCase_ (__a : Dict , __a : List[Any] , __a : Any ): """simple docstring""" _a : Any = tmp_path / 'cache' _a : List[str] = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} _a : Dict = ParquetDatasetReader(__a , cache_dir=__a , split=__a ).read() _check_parquet_dataset(__a , __a ) assert dataset.split == split if split else "train" @pytest.mark.parametrize('path_type' , [str, list] ) def UpperCAmelCase_ (__a : Optional[int] , __a : Union[str, Any] , __a : Optional[Any] ): """simple docstring""" if issubclass(__a , __a ): _a : Union[str, Any] = parquet_path elif issubclass(__a , __a ): _a : Dict = [parquet_path] _a : List[str] = tmp_path / 'cache' _a : int = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} _a : Tuple = ParquetDatasetReader(__a , cache_dir=__a ).read() _check_parquet_dataset(__a , __a ) def UpperCAmelCase_ (__a : List[Any] , __a : Union[str, Any] , __a : Optional[int]=("train",) ): """simple docstring""" assert isinstance(__a , __a ) for split in splits: _a : int = 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 UpperCAmelCase_ (__a : str , __a : Union[str, Any] , __a : Tuple ): """simple docstring""" _a : Any = tmp_path / 'cache' _a : Optional[Any] = {'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( {'train': parquet_path} , cache_dir=__a , keep_in_memory=__a ).read() _check_parquet_datasetdict(__a , __a ) @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 UpperCAmelCase_ (__a : List[Any] , __a : Any , __a : Dict ): """simple docstring""" _a : Optional[Any] = tmp_path / 'cache' _a : Optional[Any] = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} _a : List[Any] = features.copy() if features else default_expected_features _a : int = ( Features({feature: Value(__a ) for feature, dtype in features.items()} ) if features is not None else None ) _a : List[Any] = ParquetDatasetReader({'train': parquet_path} , features=__a , cache_dir=__a ).read() _check_parquet_datasetdict(__a , __a ) @pytest.mark.parametrize('split' , [None, NamedSplit('train' ), 'train', 'test'] ) def UpperCAmelCase_ (__a : Tuple , __a : Tuple , __a : Dict ): """simple docstring""" if split: _a : Dict = {split: parquet_path} else: _a : Optional[int] = 'train' _a : Dict = {'train': parquet_path, 'test': parquet_path} _a : str = tmp_path / 'cache' _a : Any = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} _a : Optional[int] = ParquetDatasetReader(__a , cache_dir=__a ).read() _check_parquet_datasetdict(__a , __a , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def UpperCAmelCase_ (__a : Optional[Any] , __a : Tuple ): """simple docstring""" _a : int = ParquetDatasetWriter(__a , tmp_path / 'foo.parquet' ) assert writer.write() > 0 _a : Tuple = pq.ParquetFile(tmp_path / 'foo.parquet' ) _a : List[Any] = pf.read() assert dataset.data.table == output_table def UpperCAmelCase_ (__a : List[str] , __a : int ): """simple docstring""" _a : Dict = str(shared_datadir / 'test_image_rgb.jpg' ) _a : List[Any] = {'image': [image_path]} _a : List[Any] = Features({'image': Image()} ) _a : Optional[int] = Dataset.from_dict(__a , features=__a ) _a : List[Any] = ParquetDatasetWriter(__a , tmp_path / 'foo.parquet' ) assert writer.write() > 0 _a : Optional[Any] = Dataset.from_parquet(str(tmp_path / 'foo.parquet' ) ) assert dataset.features == reloaded_dataset.features _a : str = ParquetDatasetReader(str(tmp_path / 'foo.parquet' ) , streaming=__a ).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 UpperCAmelCase_ (__a : Union[str, Any] , __a : List[Any] ): """simple docstring""" assert get_writer_batch_size(__a ) == expected
5
'''simple docstring''' from functools import lru_cache @lru_cache def UpperCAmelCase_ (__a : int ): """simple docstring""" if num < 0: raise ValueError('Number should not be negative.' ) return 1 if num in (0, 1) else num * factorial(num - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
5
1
import argparse import json import gdown import numpy as np import torch from huggingface_hub import hf_hub_download from transformers import ( VideoMAEConfig, VideoMAEForPreTraining, VideoMAEForVideoClassification, VideoMAEImageProcessor, ) def _a ( lowerCamelCase: List[Any] ) -> List[Any]: '''simple docstring''' __A = VideoMAEConfig() set_architecture_configs(lowerCamelCase , lowerCamelCase ) if "finetuned" not in model_name: __A = False if "finetuned" in model_name: __A = '''huggingface/label-files''' if "kinetics" in model_name: __A = 4_00 __A = '''kinetics400-id2label.json''' elif "ssv2" in model_name: __A = 1_74 __A = '''something-something-v2-id2label.json''' else: raise ValueError('''Model name should either contain \'kinetics\' or \'ssv2\' in case it\'s fine-tuned.''' ) __A = json.load(open(hf_hub_download(lowerCamelCase , lowerCamelCase , repo_type='''dataset''' ) , '''r''' ) ) __A = {int(lowerCamelCase ): v for k, v in idalabel.items()} __A = idalabel __A = {v: k for k, v in idalabel.items()} return config def _a ( lowerCamelCase: Union[str, Any] , lowerCamelCase: List[Any] ) -> Optional[Any]: '''simple docstring''' if "small" in model_name: __A = 3_84 __A = 15_36 __A = 12 __A = 16 __A = 12 __A = 3 __A = 1_92 __A = 7_68 elif "large" in model_name: __A = 10_24 __A = 40_96 __A = 24 __A = 16 __A = 12 __A = 8 __A = 5_12 __A = 20_48 elif "huge" in model_name: __A = 12_80 __A = 51_20 __A = 32 __A = 16 __A = 12 __A = 8 __A = 6_40 __A = 25_60 elif "base" not in model_name: raise ValueError('''Model name should include either "small", "base", "large", or "huge"''' ) def _a ( lowerCamelCase: Dict ) -> Union[str, Any]: '''simple docstring''' if "encoder." in name: __A = name.replace('''encoder.''' , '''''' ) if "cls_token" in name: __A = name.replace('''cls_token''' , '''videomae.embeddings.cls_token''' ) if "decoder_pos_embed" in name: __A = name.replace('''decoder_pos_embed''' , '''decoder.decoder_pos_embed''' ) if "pos_embed" in name and "decoder" not in name: __A = name.replace('''pos_embed''' , '''videomae.embeddings.position_embeddings''' ) if "patch_embed.proj" in name: __A = name.replace('''patch_embed.proj''' , '''videomae.embeddings.patch_embeddings.projection''' ) if "patch_embed.norm" in name: __A = name.replace('''patch_embed.norm''' , '''videomae.embeddings.norm''' ) if "decoder.blocks" in name: __A = name.replace('''decoder.blocks''' , '''decoder.decoder_layers''' ) if "blocks" in name: __A = name.replace('''blocks''' , '''videomae.encoder.layer''' ) if "attn.proj" in name: __A = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name and "bias" not in name: __A = name.replace('''attn''' , '''attention.self''' ) if "attn" in name: __A = name.replace('''attn''' , '''attention.attention''' ) if "norm1" in name: __A = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: __A = name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: __A = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: __A = name.replace('''mlp.fc2''' , '''output.dense''' ) if "decoder_embed" in name: __A = name.replace('''decoder_embed''' , '''decoder.decoder_embed''' ) if "decoder_norm" in name: __A = name.replace('''decoder_norm''' , '''decoder.decoder_norm''' ) if "decoder_pred" in name: __A = name.replace('''decoder_pred''' , '''decoder.decoder_pred''' ) if "norm.weight" in name and "decoder" not in name and "fc" not in name: __A = name.replace('''norm.weight''' , '''videomae.layernorm.weight''' ) if "norm.bias" in name and "decoder" not in name and "fc" not in name: __A = name.replace('''norm.bias''' , '''videomae.layernorm.bias''' ) if "head" in name and "decoder" not in name: __A = name.replace('''head''' , '''classifier''' ) return name def _a ( lowerCamelCase: Dict , lowerCamelCase: str ) -> Union[str, Any]: '''simple docstring''' for key in orig_state_dict.copy().keys(): __A = orig_state_dict.pop(lowerCamelCase ) if key.startswith('''encoder.''' ): __A = key.replace('''encoder.''' , '''''' ) if "qkv" in key: __A = key.split('''.''' ) if key.startswith('''decoder.blocks''' ): __A = config.decoder_hidden_size __A = int(key_split[2] ) __A = '''decoder.decoder_layers.''' if "weight" in key: __A = val[:dim, :] __A = val[dim : dim * 2, :] __A = val[-dim:, :] else: __A = config.hidden_size __A = int(key_split[1] ) __A = '''videomae.encoder.layer.''' if "weight" in key: __A = val[:dim, :] __A = val[dim : dim * 2, :] __A = val[-dim:, :] else: __A = val return orig_state_dict def _a ( ) -> str: '''simple docstring''' __A = hf_hub_download( repo_id='''hf-internal-testing/spaghetti-video''' , filename='''eating_spaghetti.npy''' , repo_type='''dataset''' ) __A = np.load(lowerCamelCase ) return list(lowerCamelCase ) def _a ( lowerCamelCase: Optional[Any] , lowerCamelCase: List[str] , lowerCamelCase: int , lowerCamelCase: Union[str, Any] ) -> List[Any]: '''simple docstring''' __A = get_videomae_config(lowerCamelCase ) if "finetuned" in model_name: __A = VideoMAEForVideoClassification(lowerCamelCase ) else: __A = VideoMAEForPreTraining(lowerCamelCase ) # download original checkpoint, hosted on Google Drive __A = '''pytorch_model.bin''' gdown.cached_download(lowerCamelCase , lowerCamelCase , quiet=lowerCamelCase ) __A = torch.load(lowerCamelCase , map_location='''cpu''' ) if "model" in files: __A = files['''model'''] else: __A = files['''module'''] __A = convert_state_dict(lowerCamelCase , lowerCamelCase ) model.load_state_dict(lowerCamelCase ) model.eval() # verify model on basic input __A = VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] , image_std=[0.5, 0.5, 0.5] ) __A = prepare_video() __A = image_processor(lowerCamelCase , return_tensors='''pt''' ) if "finetuned" not in model_name: __A = hf_hub_download(repo_id='''hf-internal-testing/bool-masked-pos''' , filename='''bool_masked_pos.pt''' ) __A = torch.load(lowerCamelCase ) __A = model(**lowerCamelCase ) __A = outputs.logits __A = [ '''videomae-small-finetuned-kinetics''', '''videomae-small-finetuned-ssv2''', # Kinetics-400 checkpoints (short = pretrained only for 800 epochs instead of 1600) '''videomae-base-short''', '''videomae-base-short-finetuned-kinetics''', '''videomae-base''', '''videomae-base-finetuned-kinetics''', '''videomae-large''', '''videomae-large-finetuned-kinetics''', '''videomae-huge-finetuned-kinetics''', # Something-Something-v2 checkpoints (short = pretrained only for 800 epochs instead of 2400) '''videomae-base-short-ssv2''', '''videomae-base-short-finetuned-ssv2''', '''videomae-base-ssv2''', '''videomae-base-finetuned-ssv2''', ] # NOTE: logits were tested with image_mean and image_std equal to [0.5, 0.5, 0.5] and [0.5, 0.5, 0.5] if model_name == "videomae-small-finetuned-kinetics": __A = torch.Size([1, 4_00] ) __A = torch.tensor([-0.9_291, -0.4_061, -0.9_307] ) elif model_name == "videomae-small-finetuned-ssv2": __A = torch.Size([1, 1_74] ) __A = torch.tensor([0.2_671, -0.4_689, -0.8_235] ) elif model_name == "videomae-base": __A = torch.Size([1, 14_08, 15_36] ) __A = torch.tensor([[0.7_739, 0.7_968, 0.7_089], [0.6_701, 0.7_487, 0.6_209], [0.4_287, 0.5_158, 0.4_773]] ) elif model_name == "videomae-base-short": __A = torch.Size([1, 14_08, 15_36] ) __A = torch.tensor([[0.7_994, 0.9_612, 0.8_508], [0.7_401, 0.8_958, 0.8_302], [0.5_862, 0.7_468, 0.7_325]] ) # we verified the loss both for normalized and unnormalized targets for this one __A = torch.tensor([0.5_142] ) if config.norm_pix_loss else torch.tensor([0.6_469] ) elif model_name == "videomae-large": __A = torch.Size([1, 14_08, 15_36] ) __A = torch.tensor([[0.7_149, 0.7_997, 0.6_966], [0.6_768, 0.7_869, 0.6_948], [0.5_139, 0.6_221, 0.5_605]] ) elif model_name == "videomae-large-finetuned-kinetics": __A = torch.Size([1, 4_00] ) __A = torch.tensor([0.0_771, 0.0_011, -0.3_625] ) elif model_name == "videomae-huge-finetuned-kinetics": __A = torch.Size([1, 4_00] ) __A = torch.tensor([0.2_433, 0.1_632, -0.4_894] ) elif model_name == "videomae-base-short-finetuned-kinetics": __A = torch.Size([1, 4_00] ) __A = torch.tensor([0.6_588, 0.0_990, -0.2_493] ) elif model_name == "videomae-base-finetuned-kinetics": __A = torch.Size([1, 4_00] ) __A = torch.tensor([0.3_669, -0.0_688, -0.2_421] ) elif model_name == "videomae-base-short-ssv2": __A = torch.Size([1, 14_08, 15_36] ) __A = torch.tensor([[0.4_712, 0.5_296, 0.5_786], [0.2_278, 0.2_729, 0.4_026], [0.0_352, 0.0_730, 0.2_506]] ) elif model_name == "videomae-base-short-finetuned-ssv2": __A = torch.Size([1, 1_74] ) __A = torch.tensor([-0.0_537, -0.1_539, -0.3_266] ) elif model_name == "videomae-base-ssv2": __A = torch.Size([1, 14_08, 15_36] ) __A = torch.tensor([[0.8_131, 0.8_727, 0.8_546], [0.7_366, 0.9_377, 0.8_870], [0.5_935, 0.8_874, 0.8_564]] ) elif model_name == "videomae-base-finetuned-ssv2": __A = torch.Size([1, 1_74] ) __A = torch.tensor([0.1_961, -0.8_337, -0.6_389] ) else: raise ValueError(F"""Model name not supported. Should be one of {model_names}""" ) # verify logits assert logits.shape == expected_shape if "finetuned" in model_name: assert torch.allclose(logits[0, :3] , lowerCamelCase , atol=1e-4 ) else: print('''Logits:''' , logits[0, :3, :3] ) assert torch.allclose(logits[0, :3, :3] , lowerCamelCase , atol=1e-4 ) print('''Logits ok!''' ) # verify loss, if applicable if model_name == "videomae-base-short": __A = outputs.loss assert torch.allclose(lowerCamelCase , lowerCamelCase , atol=1e-4 ) print('''Loss ok!''' ) if pytorch_dump_folder_path is not None: print(F"""Saving model and image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(lowerCamelCase ) model.save_pretrained(lowerCamelCase ) if push_to_hub: print('''Pushing to the hub...''' ) model.push_to_hub(lowerCamelCase , organization='''nielsr''' ) if __name__ == "__main__": snake_case__ : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--checkpoint_url', default='https://drive.google.com/u/1/uc?id=1tEhLyskjb755TJ65ptsrafUG2llSwQE1&amp;export=download&amp;confirm=t&amp;uuid=aa3276eb-fb7e-482a-adec-dc7171df14c4', type=str, help=( 'URL of the original PyTorch checkpoint (on Google Drive) you\'d like to convert. Should be a direct' ' download link.' ), ) parser.add_argument( '--pytorch_dump_folder_path', default='/Users/nielsrogge/Documents/VideoMAE/Test', type=str, help='Path to the output PyTorch model directory.', ) parser.add_argument('--model_name', default='videomae-base', type=str, help='Name of the model.') parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) snake_case__ : List[str] = parser.parse_args() convert_videomae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.model_name, args.push_to_hub)
117
# Copyright 2023 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 typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available snake_case__ : List[str] = { 'configuration_vivit': ['VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'VivitConfig'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case__ : Tuple = ['VivitImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case__ : Any = [ 'VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'VivitModel', 'VivitPreTrainedModel', 'VivitForVideoClassification', ] if TYPE_CHECKING: from .configuration_vivit import VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, VivitConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_vivit import VivitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vivit import ( VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST, VivitForVideoClassification, VivitModel, VivitPreTrainedModel, ) else: import sys snake_case__ : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
117
1
import math from typing import Dict, Iterable, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, get_image_size, is_torch_available, is_torch_tensor, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_torch_available(): import torch if is_vision_available(): import PIL lowerCamelCase_ = logging.get_logger(__name__) def lowerCamelCase ( a_ , a_ , a_ , a_ ) -> Tuple[int, int]: def constraint_to_multiple_of(a_ , a_ , a_=0 , a_=None ): lowerCAmelCase_ = round(val / multiple ) * multiple if max_val is not None and x > max_val: lowerCAmelCase_ = math.floor(val / multiple ) * multiple if x < min_val: lowerCAmelCase_ = math.ceil(val / multiple ) * multiple return x lowerCAmelCase_ = (output_size, output_size) if isinstance(a_ , a_ ) else output_size lowerCAmelCase_ , lowerCAmelCase_ = get_image_size(a_ ) lowerCAmelCase_ , lowerCAmelCase_ = output_size # determine new height and width lowerCAmelCase_ = output_height / input_height lowerCAmelCase_ = output_width / input_width if keep_aspect_ratio: # scale as little as possible if abs(1 - scale_width ) < abs(1 - scale_height ): # fit width lowerCAmelCase_ = scale_width else: # fit height lowerCAmelCase_ = scale_height lowerCAmelCase_ = constraint_to_multiple_of(scale_height * input_height , multiple=a_ ) lowerCAmelCase_ = constraint_to_multiple_of(scale_width * input_width , multiple=a_ ) return (new_height, new_width) class a_ ( a_ ): '''simple docstring''' __a: Union[str, Any] = ['''pixel_values'''] def __init__( self , lowercase_ = True , lowercase_ = None , lowercase_ = PILImageResampling.BILINEAR , lowercase_ = False , lowercase_ = 1 , lowercase_ = True , lowercase_ = 1 / 2_5_5 , lowercase_ = True , lowercase_ = None , lowercase_ = None , **lowercase_ , ) -> None: '''simple docstring''' super().__init__(**lowercase_ ) lowerCAmelCase_ = size if size is not None else {'height': 3_8_4, 'width': 3_8_4} lowerCAmelCase_ = get_size_dict(lowercase_ ) lowerCAmelCase_ = do_resize lowerCAmelCase_ = size lowerCAmelCase_ = keep_aspect_ratio lowerCAmelCase_ = ensure_multiple_of lowerCAmelCase_ = resample lowerCAmelCase_ = do_rescale lowerCAmelCase_ = rescale_factor lowerCAmelCase_ = do_normalize lowerCAmelCase_ = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN lowerCAmelCase_ = image_std if image_std is not None else IMAGENET_STANDARD_STD def _lowercase ( self , lowercase_ , lowercase_ , lowercase_ = False , lowercase_ = 1 , lowercase_ = PILImageResampling.BICUBIC , lowercase_ = None , **lowercase_ , ) -> np.ndarray: '''simple docstring''' lowerCAmelCase_ = get_size_dict(lowercase_ ) if "height" not in size or "width" not in size: raise ValueError(f'''The size dictionary must contain the keys \'height\' and \'width\'. Got {size.keys()}''' ) lowerCAmelCase_ = get_resize_output_image_size( lowercase_ , output_size=(size['height'], size['width']) , keep_aspect_ratio=lowercase_ , multiple=lowercase_ , ) return resize(lowercase_ , size=lowercase_ , resample=lowercase_ , data_format=lowercase_ , **lowercase_ ) def _lowercase ( self , lowercase_ , lowercase_ , lowercase_ = None , **lowercase_ , ) -> Dict: '''simple docstring''' return rescale(lowercase_ , scale=lowercase_ , data_format=lowercase_ , **lowercase_ ) def _lowercase ( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ = None , **lowercase_ , ) -> np.ndarray: '''simple docstring''' return normalize(lowercase_ , mean=lowercase_ , std=lowercase_ , data_format=lowercase_ , **lowercase_ ) def _lowercase ( self , lowercase_ , lowercase_ = None , lowercase_ = None , lowercase_ = None , lowercase_ = None , lowercase_ = None , lowercase_ = None , lowercase_ = None , lowercase_ = None , lowercase_ = None , lowercase_ = None , lowercase_ = None , lowercase_ = ChannelDimension.FIRST , **lowercase_ , ) -> PIL.Image.Image: '''simple docstring''' lowerCAmelCase_ = do_resize if do_resize is not None else self.do_resize lowerCAmelCase_ = size if size is not None else self.size lowerCAmelCase_ = get_size_dict(lowercase_ ) lowerCAmelCase_ = keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio lowerCAmelCase_ = ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of lowerCAmelCase_ = resample if resample is not None else self.resample lowerCAmelCase_ = do_rescale if do_rescale is not None else self.do_rescale lowerCAmelCase_ = rescale_factor if rescale_factor is not None else self.rescale_factor lowerCAmelCase_ = do_normalize if do_normalize is not None else self.do_normalize lowerCAmelCase_ = image_mean if image_mean is not None else self.image_mean lowerCAmelCase_ = image_std if image_std is not None else self.image_std lowerCAmelCase_ = make_list_of_images(lowercase_ ) if not valid_images(lowercase_ ): 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 or resample is None: raise ValueError('Size and resample must be specified if do_resize 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.' ) # All transformations expect numpy arrays. lowerCAmelCase_ = [to_numpy_array(lowercase_ ) for image in images] if do_resize: lowerCAmelCase_ = [self.resize(image=lowercase_ , size=lowercase_ , resample=lowercase_ ) for image in images] if do_rescale: lowerCAmelCase_ = [self.rescale(image=lowercase_ , scale=lowercase_ ) for image in images] if do_normalize: lowerCAmelCase_ = [self.normalize(image=lowercase_ , mean=lowercase_ , std=lowercase_ ) for image in images] lowerCAmelCase_ = [to_channel_dimension_format(lowercase_ , lowercase_ ) for image in images] lowerCAmelCase_ = {'pixel_values': images} return BatchFeature(data=lowercase_ , tensor_type=lowercase_ ) def _lowercase ( self , lowercase_ , lowercase_ = None ) -> List[Any]: '''simple docstring''' lowerCAmelCase_ = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(lowercase_ ) != len(lowercase_ ): raise ValueError( 'Make sure that you pass in as many target sizes as the batch dimension of the logits' ) if is_torch_tensor(lowercase_ ): lowerCAmelCase_ = target_sizes.numpy() lowerCAmelCase_ = [] for idx in range(len(lowercase_ ) ): lowerCAmelCase_ = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode='bilinear' , align_corners=lowercase_ ) lowerCAmelCase_ = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(lowercase_ ) else: lowerCAmelCase_ = logits.argmax(dim=1 ) lowerCAmelCase_ = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
366
from __future__ import annotations lowerCamelCase_ = 1_0 def lowerCamelCase ( a_ ) -> list[int]: lowerCAmelCase_ = 1 lowerCAmelCase_ = max(a_ ) while placement <= max_digit: # declare and initialize empty buckets lowerCAmelCase_ = [[] for _ in range(a_ )] # split list_of_ints between the buckets for i in list_of_ints: lowerCAmelCase_ = int((i / placement) % RADIX ) buckets[tmp].append(a_ ) # put each buckets' contents into list_of_ints lowerCAmelCase_ = 0 for b in range(a_ ): for i in buckets[b]: lowerCAmelCase_ = i a += 1 # move to next placement *= RADIX return list_of_ints if __name__ == "__main__": import doctest doctest.testmod()
14
0
import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto.configuration_auto import CONFIG_MAPPING _SCREAMING_SNAKE_CASE = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE_ ( __UpperCAmelCase ): __lowerCAmelCase = """upernet""" def __init__( self : int , lowerCamelCase_ : Any=None , lowerCamelCase_ : List[Any]=512 , lowerCamelCase_ : str=0.0_2 , lowerCamelCase_ : List[Any]=[1, 2, 3, 6] , lowerCamelCase_ : Optional[Any]=True , lowerCamelCase_ : Optional[Any]=0.4 , lowerCamelCase_ : int=384 , lowerCamelCase_ : int=256 , lowerCamelCase_ : int=1 , lowerCamelCase_ : Optional[int]=False , lowerCamelCase_ : Union[str, Any]=255 , **lowerCamelCase_ : str , ): """simple docstring""" super().__init__(**lowerCamelCase__ ) if backbone_config is None: logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.""" ) UpperCamelCase = CONFIG_MAPPING["""resnet"""](out_features=["""stage1""", """stage2""", """stage3""", """stage4"""] ) elif isinstance(lowerCamelCase__ , lowerCamelCase__ ): UpperCamelCase = backbone_config.get("""model_type""" ) UpperCamelCase = CONFIG_MAPPING[backbone_model_type] UpperCamelCase = config_class.from_dict(lowerCamelCase__ ) UpperCamelCase = backbone_config UpperCamelCase = hidden_size UpperCamelCase = initializer_range UpperCamelCase = pool_scales UpperCamelCase = use_auxiliary_head UpperCamelCase = auxiliary_loss_weight UpperCamelCase = auxiliary_in_channels UpperCamelCase = auxiliary_channels UpperCamelCase = auxiliary_num_convs UpperCamelCase = auxiliary_concat_input UpperCamelCase = loss_ignore_index def lowerCamelCase_ ( self : Optional[Any] ): """simple docstring""" UpperCamelCase = copy.deepcopy(self.__dict__ ) UpperCamelCase = self.backbone_config.to_dict() UpperCamelCase = self.__class__.model_type return output
343
"""simple docstring""" from PIL import Image def a_ ( lowerCamelCase , lowerCamelCase ): UpperCAmelCase__ = (2_5_9 * (level + 2_5_5)) / (2_5_5 * (2_5_9 - level)) def contrast(lowerCamelCase ) -> int: return int(1_2_8 + factor * (c - 1_2_8) ) return img.point(lowerCamelCase ) if __name__ == "__main__": # Load image with Image.open('image_data/lena.jpg') as img: # Change contrast to 170 lowerCAmelCase__ : Any = change_contrast(img, 170) cont_img.save('image_data/lena_high_contrast.png', format='png')
98
0
from ..utils import DummyObject, requires_backends class lowercase__ ( metaclass=__lowerCamelCase ): '''simple docstring''' a : List[Any] = ["transformers", "torch", "note_seq"] def __init__( self, *__magic_name__, **__magic_name__ ) -> Optional[int]: """simple docstring""" requires_backends(self, ['''transformers''', '''torch''', '''note_seq'''] ) @classmethod def UpperCamelCase__ ( cls, *__magic_name__, **__magic_name__ ) -> List[Any]: """simple docstring""" requires_backends(cls, ['''transformers''', '''torch''', '''note_seq'''] ) @classmethod def UpperCamelCase__ ( cls, *__magic_name__, **__magic_name__ ) -> Tuple: """simple docstring""" requires_backends(cls, ['''transformers''', '''torch''', '''note_seq'''] )
247
import collections import json import math import os import re import time from fnmatch import fnmatch from typing import Dict import requests from slack_sdk import WebClient UpperCAmelCase_ = WebClient(token=os.environ['CI_SLACK_BOT_TOKEN']) def lowerCAmelCase_ ( __UpperCAmelCase: str ) -> Optional[Any]: UpperCamelCase__ : Any = test_results.split(''' ''' ) UpperCamelCase__ : Dict = 0 UpperCamelCase__ : int = 0 # When the output is short enough, the output is surrounded by = signs: "== OUTPUT ==" # When it is too long, those signs are not present. UpperCamelCase__ : List[Any] = expressions[-2] if '''=''' in expressions[-1] else expressions[-1] for i, expression in enumerate(__UpperCAmelCase ): if "failed" in expression: failed += int(expressions[i - 1] ) if "passed" in expression: success += int(expressions[i - 1] ) return failed, success, time_spent def lowerCAmelCase_ ( __UpperCAmelCase: List[str] ) -> Tuple: UpperCamelCase__ : List[Any] = {} UpperCamelCase__ : Optional[Any] = None UpperCamelCase__ : int = False for line in failures_short_lines.split('''\n''' ): if re.search(r'''_ \[doctest\]''' , __UpperCAmelCase ): UpperCamelCase__ : Any = True UpperCamelCase__ : Optional[Any] = line.split(''' ''' )[2] elif in_error and not line.split(''' ''' )[0].isdigit(): UpperCamelCase__ : List[Any] = line UpperCamelCase__ : List[Any] = False return failures class lowercase__ : '''simple docstring''' def __init__( self, __magic_name__, __magic_name__ ) -> List[str]: """simple docstring""" UpperCamelCase__ : Dict = title UpperCamelCase__ : Tuple = doc_test_results['''time_spent'''].split(''',''' )[0] UpperCamelCase__ : Optional[Any] = doc_test_results['''success'''] UpperCamelCase__ : str = doc_test_results['''failures'''] UpperCamelCase__ : str = self.n_success + self.n_failures # Failures and success of the modeling tests UpperCamelCase__ : List[Any] = doc_test_results @property def UpperCamelCase__ ( self ) -> str: """simple docstring""" UpperCamelCase__ : List[Any] = [self._time_spent] UpperCamelCase__ : str = 0 for time in time_spent: UpperCamelCase__ : List[Any] = time.split(''':''' ) # Time can be formatted as xx:xx:xx, as .xx, or as x.xx if the time spent was less than a minute. if len(__magic_name__ ) == 1: UpperCamelCase__ : List[Any] = [0, 0, time_parts[0]] UpperCamelCase__ ,UpperCamelCase__ ,UpperCamelCase__ : int = int(time_parts[0] ), int(time_parts[1] ), float(time_parts[2] ) total_secs += hours * 3600 + minutes * 60 + seconds UpperCamelCase__ ,UpperCamelCase__ ,UpperCamelCase__ : List[str] = total_secs // 3600, (total_secs % 3600) // 60, total_secs % 60 return f"{int(__magic_name__ )}h{int(__magic_name__ )}m{int(__magic_name__ )}s" @property def UpperCamelCase__ ( self ) -> Dict: """simple docstring""" return {"type": "header", "text": {"type": "plain_text", "text": self.title}} @property def UpperCamelCase__ ( self ) -> Dict: """simple docstring""" return { "type": "section", "text": { "type": "plain_text", "text": f"🌞 There were no failures: all {self.n_tests} tests passed. The suite ran in {self.time}.", "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": f"https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}", }, } @property def UpperCamelCase__ ( self ) -> Dict: """simple docstring""" return { "type": "section", "text": { "type": "plain_text", "text": ( f"There were {self.n_failures} failures, out of {self.n_tests} tests.\nThe suite ran in" f" {self.time}." ), "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": f"https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}", }, } @property def UpperCamelCase__ ( self ) -> Dict: """simple docstring""" UpperCamelCase__ : List[Any] = 40 UpperCamelCase__ : Tuple = {k: v['''failed'''] for k, v in doc_test_results.items() if isinstance(__magic_name__, __magic_name__ )} UpperCamelCase__ : List[str] = '''''' for category, failures in category_failures.items(): if len(__magic_name__ ) == 0: continue if report != "": report += "\n\n" report += f"*{category} failures*:".ljust(line_length // 2 ).rjust(line_length // 2 ) + "\n" report += "`" report += "`\n`".join(__magic_name__ ) report += "`" return { "type": "section", "text": { "type": "mrkdwn", "text": f"The following examples had failures:\n\n\n{report}\n", }, } @property def UpperCamelCase__ ( self ) -> str: """simple docstring""" UpperCamelCase__ : str = [self.header] if self.n_failures > 0: blocks.append(self.failures ) if self.n_failures > 0: blocks.extend([self.category_failures] ) if self.n_failures == 0: blocks.append(self.no_failures ) return json.dumps(__magic_name__ ) @staticmethod def UpperCamelCase__ ( ) -> Union[str, Any]: """simple docstring""" UpperCamelCase__ : Optional[Any] = [ { '''type''': '''section''', '''text''': { '''type''': '''plain_text''', '''text''': '''There was an issue running the tests.''', }, '''accessory''': { '''type''': '''button''', '''text''': {'''type''': '''plain_text''', '''text''': '''Check Action results''', '''emoji''': True}, '''url''': f"https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}", }, } ] print('''Sending the following payload''' ) print(json.dumps({'''blocks''': json.loads(__magic_name__ )} ) ) client.chat_postMessage( channel=os.environ['''CI_SLACK_CHANNEL_ID_DAILY'''], text='''There was an issue running the tests.''', blocks=__magic_name__, ) def UpperCamelCase__ ( self ) -> Union[str, Any]: """simple docstring""" print('''Sending the following payload''' ) print(json.dumps({'''blocks''': json.loads(self.payload )} ) ) UpperCamelCase__ : List[str] = f"{self.n_failures} failures out of {self.n_tests} tests," if self.n_failures else '''All tests passed.''' UpperCamelCase__ : Optional[Any] = client.chat_postMessage( channel=os.environ['''CI_SLACK_CHANNEL_ID_DAILY'''], blocks=self.payload, text=__magic_name__, ) def UpperCamelCase__ ( self, __magic_name__, __magic_name__, __magic_name__, __magic_name__ ) -> Optional[Any]: """simple docstring""" UpperCamelCase__ : Optional[Any] = '''''' for key, value in failures.items(): UpperCamelCase__ : List[Any] = value[:200] + ''' [Truncated]''' if len(__magic_name__ ) > 250 else value failures_text += f"*{key}*\n_{value}_\n\n" UpperCamelCase__ : Union[str, Any] = job_name UpperCamelCase__ : Any = {'''type''': '''section''', '''text''': {'''type''': '''mrkdwn''', '''text''': text}} if job_link is not None: UpperCamelCase__ : Union[str, Any] = { '''type''': '''button''', '''text''': {'''type''': '''plain_text''', '''text''': '''GitHub Action job''', '''emoji''': True}, '''url''': job_link, } return [ {"type": "header", "text": {"type": "plain_text", "text": title.upper(), "emoji": True}}, content, {"type": "section", "text": {"type": "mrkdwn", "text": failures_text}}, ] def UpperCamelCase__ ( self ) -> Dict: """simple docstring""" if self.thread_ts is None: raise ValueError('''Can only post reply if a post has been made.''' ) UpperCamelCase__ : Optional[int] = self.doc_test_results.pop('''job_link''' ) self.doc_test_results.pop('''failures''' ) self.doc_test_results.pop('''success''' ) self.doc_test_results.pop('''time_spent''' ) UpperCamelCase__ : Optional[int] = sorted(self.doc_test_results.items(), key=lambda __magic_name__ : t[0] ) for job, job_result in sorted_dict: if len(job_result['''failures'''] ): UpperCamelCase__ : Any = f"*Num failures* :{len(job_result['failed'] )} \n" UpperCamelCase__ : Optional[Any] = job_result['''failures'''] UpperCamelCase__ : Optional[Any] = self.get_reply_blocks(__magic_name__, __magic_name__, __magic_name__, text=__magic_name__ ) print('''Sending the following reply''' ) print(json.dumps({'''blocks''': blocks} ) ) client.chat_postMessage( channel=os.environ['''CI_SLACK_CHANNEL_ID_DAILY'''], text=f"Results for {job}", blocks=__magic_name__, thread_ts=self.thread_ts['''ts'''], ) time.sleep(1 ) def lowerCAmelCase_ ( ) -> Dict: UpperCamelCase__ : Any = os.environ['''GITHUB_RUN_ID'''] UpperCamelCase__ : Tuple = f"https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100" UpperCamelCase__ : Optional[int] = requests.get(__UpperCAmelCase ).json() UpperCamelCase__ : List[Any] = {} try: jobs.update({job['''name''']: job['''html_url'''] for job in result['''jobs''']} ) UpperCamelCase__ : List[Any] = math.ceil((result['''total_count'''] - 100) / 100 ) for i in range(__UpperCAmelCase ): UpperCamelCase__ : Any = requests.get(url + f"&page={i + 2}" ).json() jobs.update({job['''name''']: job['''html_url'''] for job in result['''jobs''']} ) return jobs except Exception as e: print('''Unknown error, could not fetch links.''' , __UpperCAmelCase ) return {} def lowerCAmelCase_ ( __UpperCAmelCase: str ) -> List[Any]: UpperCamelCase__ : Optional[int] = {} if os.path.exists(__UpperCAmelCase ): UpperCamelCase__ : Dict = os.listdir(__UpperCAmelCase ) for file in files: try: with open(os.path.join(__UpperCAmelCase , __UpperCAmelCase ) , encoding='''utf-8''' ) as f: UpperCamelCase__ : int = f.read() except UnicodeDecodeError as e: raise ValueError(f"Could not open {os.path.join(__UpperCAmelCase , __UpperCAmelCase )}." ) from e return _artifact def lowerCAmelCase_ ( ) -> str: class lowercase__ : '''simple docstring''' def __init__( self, __magic_name__ ) -> Tuple: """simple docstring""" UpperCamelCase__ : Any = name UpperCamelCase__ : int = [] def __str__( self ) -> Tuple: """simple docstring""" return self.name def UpperCamelCase__ ( self, __magic_name__ ) -> Union[str, Any]: """simple docstring""" self.paths.append({'''name''': self.name, '''path''': path} ) UpperCamelCase__ : Dict[str, Artifact] = {} UpperCamelCase__ : Union[str, Any] = filter(os.path.isdir , os.listdir() ) for directory in directories: UpperCamelCase__ : Optional[int] = directory if artifact_name not in _available_artifacts: UpperCamelCase__ : Union[str, Any] = Artifact(__UpperCAmelCase ) _available_artifacts[artifact_name].add_path(__UpperCAmelCase ) return _available_artifacts if __name__ == "__main__": UpperCAmelCase_ = get_job_links() UpperCAmelCase_ = retrieve_available_artifacts() UpperCAmelCase_ = collections.OrderedDict( [ ('*.py', 'API Examples'), ('*.md', 'MD Examples'), ] ) # This dict will contain all the information relative to each doc test category: # - failed: list of failed tests # - failures: dict in the format 'test': 'error_message' UpperCAmelCase_ = { v: { 'failed': [], 'failures': {}, } for v in docs.values() } # Link to the GitHub Action job UpperCAmelCase_ = github_actions_job_links.get('run_doctests') UpperCAmelCase_ = available_artifacts['doc_tests_gpu_test_reports'].paths[0] UpperCAmelCase_ = retrieve_artifact(artifact_path['name']) if "stats" in artifact: UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ = handle_test_results(artifact['stats']) UpperCAmelCase_ = failed UpperCAmelCase_ = success UpperCAmelCase_ = time_spent[1:-1] + ', ' UpperCAmelCase_ = extract_first_line_failure(artifact['failures_short']) for line in artifact["summary_short"].split('\n'): if re.search('FAILED', line): UpperCAmelCase_ = line.replace('FAILED ', '') UpperCAmelCase_ = line.split()[0].replace('\n', '') if "::" in line: UpperCAmelCase_ , UpperCAmelCase_ = line.split('::') else: UpperCAmelCase_ , UpperCAmelCase_ = line, line for file_regex in docs.keys(): if fnmatch(file_path, file_regex): UpperCAmelCase_ = docs[file_regex] doc_test_results[category]["failed"].append(test) UpperCAmelCase_ = all_failures[test] if test in all_failures else 'N/A' UpperCAmelCase_ = failure break UpperCAmelCase_ = Message('🤗 Results of the doc tests.', doc_test_results) message.post() message.post_reply()
247
1
import unittest from transformers import GPTNeoXJapaneseConfig, is_torch_available from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel class __lowerCAmelCase : def __init__( self , lowerCAmelCase__ , lowerCAmelCase__=1_3 , lowerCAmelCase__=7 , lowerCAmelCase__=True , lowerCAmelCase__=True , lowerCAmelCase__=True , lowerCAmelCase__=True , lowerCAmelCase__=9_9 , lowerCAmelCase__=3_2 , lowerCAmelCase__=5 , lowerCAmelCase__=4 , lowerCAmelCase__=4 , lowerCAmelCase__="gelu" , lowerCAmelCase__=0.0 , lowerCAmelCase__=0.1 , lowerCAmelCase__=True , lowerCAmelCase__=5_1_2 , lowerCAmelCase__=1_6 , lowerCAmelCase__=2 , lowerCAmelCase__=0.02 , lowerCAmelCase__=3 , lowerCAmelCase__=4 , lowerCAmelCase__=None , ) -> Any: '''simple docstring''' a__ : str =parent a__ : Dict =batch_size a__ : List[str] =seq_length a__ : Any =is_training a__ : Tuple =use_input_mask a__ : List[str] =use_token_type_ids a__ : Union[str, Any] =use_labels a__ : Optional[int] =vocab_size a__ : int =hidden_size a__ : int =num_hidden_layers a__ : List[Any] =num_attention_heads a__ : str =intermediate_multiple_size a__ : List[str] =hidden_act a__ : Optional[int] =hidden_dropout a__ : List[str] =attention_dropout a__ : int =weight_tying a__ : Optional[Any] =max_position_embeddings a__ : Any =type_vocab_size a__ : Optional[int] =type_sequence_label_size a__ : Optional[Any] =initializer_range a__ : Dict =num_labels a__ : List[str] =num_choices a__ : Union[str, Any] =scope def _lowercase ( self ) -> Dict: '''simple docstring''' a__ : int =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) a__ : Optional[int] =None if self.use_input_mask: a__ : List[str] =random_attention_mask([self.batch_size, self.seq_length] ) a__ : Dict =None if self.use_labels: a__ : str =ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) a__ : Any =self.get_config() return config, input_ids, input_mask, token_labels def _lowercase ( self ) -> Dict: '''simple docstring''' return GPTNeoXJapaneseConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_multiple_size=self.intermediate_multiple_size , hidden_act=self.hidden_act , hidden_dropout=self.hidden_dropout , attention_dropout=self.attention_dropout , weight_tying=self.weight_tying , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowerCAmelCase__ , initializer_range=self.initializer_range , ) def _lowercase ( self ) -> Union[str, Any]: '''simple docstring''' a__ , a__ , a__ , a__ : Tuple =self.prepare_config_and_inputs() a__ : List[str] =True return config, input_ids, input_mask, token_labels def _lowercase ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> Tuple: '''simple docstring''' a__ : Any =GPTNeoXJapaneseModel(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() a__ : List[Any] =model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ ) a__ : Union[str, Any] =model(lowerCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowercase ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> Dict: '''simple docstring''' a__ : Optional[int] =True a__ : Dict =GPTNeoXJapaneseModel(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() a__ : Optional[int] =model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowercase ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> List[Any]: '''simple docstring''' a__ : Optional[int] =GPTNeoXJapaneseForCausalLM(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() a__ : Optional[Any] =model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _lowercase ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> List[Any]: '''simple docstring''' a__ : int =True a__ : str =GPTNeoXJapaneseForCausalLM(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() # first forward pass a__ : Any =model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , use_cache=lowerCAmelCase__ ) a__ : List[str] =outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids a__ : Tuple =ids_tensor((self.batch_size, 3) , config.vocab_size ) a__ : List[Any] =ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and a__ : List[str] =torch.cat([input_ids, next_tokens] , dim=-1 ) a__ : List[str] =torch.cat([input_mask, next_mask] , dim=-1 ) a__ : int =model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ ) a__ : Dict =output_from_no_past["hidden_states"][0] a__ : Any =model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , past_key_values=lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ , )["hidden_states"][0] # select random slice a__ : List[str] =ids_tensor((1,) , output_from_past.shape[-1] ).item() a__ : List[Any] =output_from_no_past[:, -3:, random_slice_idx].detach() a__ : Any =output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-3 ) ) def _lowercase ( self ) -> List[str]: '''simple docstring''' a__ : List[Any] =self.prepare_config_and_inputs() a__ , a__ , a__ , a__ : int =config_and_inputs a__ : Optional[Any] ={"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , unittest.TestCase): _lowercase : Optional[int] = (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else () _lowercase : List[str] = (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else () _lowercase : Optional[int] = ( {"""feature-extraction""": GPTNeoXJapaneseModel, """text-generation""": GPTNeoXJapaneseForCausalLM} if is_torch_available() else {} ) _lowercase : int = False _lowercase : Optional[Any] = False _lowercase : Tuple = False _lowercase : int = False def _lowercase ( self ) -> Tuple: '''simple docstring''' a__ : Optional[Any] =GPTNeoXJapaneseModelTester(self ) a__ : Union[str, Any] =ConfigTester(self , config_class=lowerCAmelCase__ , hidden_size=3_7 ) def _lowercase ( self ) -> Optional[Any]: '''simple docstring''' self.config_tester.run_common_tests() def _lowercase ( self ) -> List[Any]: '''simple docstring''' a__ , a__ , a__ , a__ : Any =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def _lowercase ( self ) -> Any: '''simple docstring''' a__ , a__ , a__ , a__ : List[str] =self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def _lowercase ( self ) -> List[str]: '''simple docstring''' a__ , a__ , a__ , a__ : Optional[int] =self.model_tester.prepare_config_and_inputs_for_decoder() a__ : str =None self.model_tester.create_and_check_model_as_decoder(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def _lowercase ( self ) -> Optional[int]: '''simple docstring''' a__ , a__ , a__ , a__ : Any =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def _lowercase ( self ) -> List[Any]: '''simple docstring''' a__ : List[Any] =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_causal_lm(*lowerCAmelCase__ ) @slow def _lowercase ( self ) -> int: '''simple docstring''' a__ : Tuple ="abeja/gpt-neox-japanese-2.7b" a__ : Any =["データサイエンティストとは、", "100年後に必要とされる会社は、", "フルリモートの環境で働くために必要なことは、", "国境の長いトンネルを抜けると", "美味しい日本食といえば、"] a__ : Dict =[ "データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。", "100年後に必要とされる会社は、「人」が中心の会社です。", "フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。", "国境の長いトンネルを抜けると、そこは雪国だった。", "美味しい日本食といえば、やっぱりお寿司ですよね。", ] a__ : int =GPTNeoXJapaneseTokenizer.from_pretrained(lowerCAmelCase__ ) a__ : Dict =GPTNeoXJapaneseForCausalLM.from_pretrained(lowerCAmelCase__ ) a__ : List[str] =[] for prompt in prompts: a__ : List[str] =tokenizer(lowerCAmelCase__ , return_tensors="pt" ).input_ids a__ : int =model.generate(lowerCAmelCase__ , max_length=5_0 ) a__ : Dict =tokenizer.batch_decode(lowerCAmelCase__ , skip_special_tokens=lowerCAmelCase__ ) predicted_outputs += generated_string self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ )
95
def _A ( SCREAMING_SNAKE_CASE : Optional[Any] ): """simple docstring""" a__ : Optional[Any] =len(SCREAMING_SNAKE_CASE ) while cur > 1: # Find the maximum number in arr a__ : List[Any] =arr.index(max(arr[0:cur] ) ) # Reverse from 0 to mi a__ : int =arr[mi::-1] + arr[mi + 1 : len(SCREAMING_SNAKE_CASE )] # Reverse whole list a__ : List[str] =arr[cur - 1 :: -1] + arr[cur : len(SCREAMING_SNAKE_CASE )] cur -= 1 return arr if __name__ == "__main__": UpperCAmelCase : int = input("""Enter numbers separated by a comma:\n""").strip() UpperCAmelCase : Optional[int] = [int(item) for item in user_input.split(""",""")] print(pancake_sort(unsorted))
95
1
# Note: if you intend to run this script make sure you look under scripts/fsmt/ # to locate the appropriate script to do the work correctly. There is a set of scripts to: # - download and prepare data and run the conversion script # - perform eval to get the best hparam into the config # - generate model_cards - useful if you have multiple models from the same paper import argparse import json import os import re from collections import OrderedDict from os.path import basename, dirname import fairseq import torch from fairseq import hub_utils from fairseq.data.dictionary import Dictionary from transformers import FSMTConfig, FSMTForConditionalGeneration from transformers.models.fsmt.tokenization_fsmt import VOCAB_FILES_NAMES from transformers.tokenization_utils_base import TOKENIZER_CONFIG_FILE from transformers.utils import WEIGHTS_NAME, logging logging.set_verbosity_warning() __UpperCamelCase : Optional[int] = 2 # based on the results of a search on a range of `num_beams`, `length_penalty` and `early_stopping` # values against wmt19 test data to obtain the best BLEU scores, we will use the following defaults: # # * `num_beams`: 5 (higher scores better, but requires more memory/is slower, can be adjusted by users) # * `early_stopping`: `False` consistently scored better # * `length_penalty` varied, so will assign the best one depending on the model __UpperCamelCase : Optional[int] = { # fairseq: 'wmt19-ru-en': {'length_penalty': 1.1}, 'wmt19-en-ru': {'length_penalty': 1.15}, 'wmt19-en-de': {'length_penalty': 1.0}, 'wmt19-de-en': {'length_penalty': 1.1}, # allenai: 'wmt16-en-de-dist-12-1': {'length_penalty': 0.6}, 'wmt16-en-de-dist-6-1': {'length_penalty': 0.6}, 'wmt16-en-de-12-1': {'length_penalty': 0.8}, 'wmt19-de-en-6-6-base': {'length_penalty': 0.6}, 'wmt19-de-en-6-6-big': {'length_penalty': 0.6}, } # this remaps the different models to their organization names __UpperCamelCase : int = {} for m in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: __UpperCamelCase : Union[str, Any] = 'facebook' for m in [ "wmt16-en-de-dist-12-1", "wmt16-en-de-dist-6-1", "wmt16-en-de-12-1", "wmt19-de-en-6-6-base", "wmt19-de-en-6-6-big", ]: __UpperCamelCase : Tuple = 'allenai' def A ( _lowercase ): # (1) remove word breaking symbol, (2) add word ending symbol where the word is not broken up, # e.g.: d = {'le@@': 5, 'tt@@': 6, 'er': 7} => {'le': 5, 'tt': 6, 'er</w>': 7} SCREAMING_SNAKE_CASE : int = dict((re.sub(R'''@@$''' , '''''' , _lowercase ), v) if k.endswith('''@@''' ) else (re.sub(R'''$''' , '''</w>''' , _lowercase ), v) for k, v in d.items() ) SCREAMING_SNAKE_CASE : str = '''<s> <pad> </s> <unk>'''.split() # restore the special tokens for k in keep_keys: del da[f"""{k}</w>"""] SCREAMING_SNAKE_CASE : Optional[int] = d[k] # restore return da def A ( _lowercase , _lowercase ): # prep assert os.path.exists(_lowercase ) os.makedirs(_lowercase , exist_ok=_lowercase ) print(f"""Writing results to {pytorch_dump_folder_path}""" ) # handle various types of models SCREAMING_SNAKE_CASE : Union[str, Any] = basename(_lowercase ) SCREAMING_SNAKE_CASE : Optional[Any] = dirname(_lowercase ) SCREAMING_SNAKE_CASE : str = fairseq.model_parallel.models.transformer.ModelParallelTransformerModel SCREAMING_SNAKE_CASE : Optional[int] = cls.hub_models() SCREAMING_SNAKE_CASE : Tuple = {'''bpe''': '''fastbpe''', '''tokenizer''': '''moses'''} SCREAMING_SNAKE_CASE : Tuple = '''.''' # note: since the model dump is old, fairseq has upgraded its model some # time later, and it does a whole lot of rewrites and splits on the saved # weights, therefore we can't use torch.load() directly on the model file. # see: upgrade_state_dict(state_dict) in fairseq_model.py print(f"""using checkpoint {checkpoint_file}""" ) SCREAMING_SNAKE_CASE : str = hub_utils.from_pretrained( _lowercase , _lowercase , _lowercase , archive_map=_lowercase , **_lowercase ) SCREAMING_SNAKE_CASE : Any = vars(chkpt['''args''']['''model'''] ) SCREAMING_SNAKE_CASE : Union[str, Any] = args['''source_lang'''] SCREAMING_SNAKE_CASE : str = args['''target_lang'''] SCREAMING_SNAKE_CASE : int = dirname(_lowercase ) SCREAMING_SNAKE_CASE : List[str] = basename(_lowercase ) # dicts SCREAMING_SNAKE_CASE : int = os.path.join(_lowercase , f"""dict.{src_lang}.txt""" ) SCREAMING_SNAKE_CASE : Optional[Any] = os.path.join(_lowercase , f"""dict.{tgt_lang}.txt""" ) SCREAMING_SNAKE_CASE : Union[str, Any] = Dictionary.load(_lowercase ) SCREAMING_SNAKE_CASE : List[Any] = rewrite_dict_keys(src_dict.indices ) SCREAMING_SNAKE_CASE : str = len(_lowercase ) SCREAMING_SNAKE_CASE : str = os.path.join(_lowercase , '''vocab-src.json''' ) print(f"""Generating {src_vocab_file} of {src_vocab_size} of {src_lang} records""" ) with open(_lowercase , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(_lowercase , ensure_ascii=_lowercase , indent=_lowercase ) ) # detect whether this is a do_lower_case situation, which can be derived by checking whether we # have at least one uppercase letter in the source vocab SCREAMING_SNAKE_CASE : Dict = True for k in src_vocab.keys(): if not k.islower(): SCREAMING_SNAKE_CASE : Optional[Any] = False break SCREAMING_SNAKE_CASE : List[Any] = Dictionary.load(_lowercase ) SCREAMING_SNAKE_CASE : Any = rewrite_dict_keys(tgt_dict.indices ) SCREAMING_SNAKE_CASE : List[str] = len(_lowercase ) SCREAMING_SNAKE_CASE : Any = os.path.join(_lowercase , '''vocab-tgt.json''' ) print(f"""Generating {tgt_vocab_file} of {tgt_vocab_size} of {tgt_lang} records""" ) with open(_lowercase , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(_lowercase , ensure_ascii=_lowercase , indent=_lowercase ) ) # merges_file (bpecodes) SCREAMING_SNAKE_CASE : Optional[int] = os.path.join(_lowercase , VOCAB_FILES_NAMES['''merges_file'''] ) for fn in ["bpecodes", "code"]: # older fairseq called the merges file "code" SCREAMING_SNAKE_CASE : Union[str, Any] = os.path.join(_lowercase , _lowercase ) if os.path.exists(_lowercase ): break with open(_lowercase , encoding='''utf-8''' ) as fin: SCREAMING_SNAKE_CASE : Optional[Any] = fin.read() SCREAMING_SNAKE_CASE : Optional[Any] = re.sub(R''' \d+$''' , '''''' , _lowercase , 0 , re.M ) # remove frequency number print(f"""Generating {merges_file}""" ) with open(_lowercase , '''w''' , encoding='''utf-8''' ) as fout: fout.write(_lowercase ) # model config SCREAMING_SNAKE_CASE : Union[str, Any] = os.path.join(_lowercase , '''config.json''' ) # validate bpe/tokenizer config, as currently it's hardcoded to moses+fastbpe - # may have to modify the tokenizer if a different type is used by a future model assert args["bpe"] == "fastbpe", f"""need to extend tokenizer to support bpe={args['bpe']}""" assert args["tokenizer"] == "moses", f"""need to extend tokenizer to support bpe={args['tokenizer']}""" SCREAMING_SNAKE_CASE : Tuple = { '''architectures''': ['''FSMTForConditionalGeneration'''], '''model_type''': '''fsmt''', '''activation_dropout''': args['''activation_dropout'''], '''activation_function''': '''relu''', '''attention_dropout''': args['''attention_dropout'''], '''d_model''': args['''decoder_embed_dim'''], '''dropout''': args['''dropout'''], '''init_std''': 0.02, '''max_position_embeddings''': args['''max_source_positions'''], '''num_hidden_layers''': args['''encoder_layers'''], '''src_vocab_size''': src_vocab_size, '''tgt_vocab_size''': tgt_vocab_size, '''langs''': [src_lang, tgt_lang], '''encoder_attention_heads''': args['''encoder_attention_heads'''], '''encoder_ffn_dim''': args['''encoder_ffn_embed_dim'''], '''encoder_layerdrop''': args['''encoder_layerdrop'''], '''encoder_layers''': args['''encoder_layers'''], '''decoder_attention_heads''': args['''decoder_attention_heads'''], '''decoder_ffn_dim''': args['''decoder_ffn_embed_dim'''], '''decoder_layerdrop''': args['''decoder_layerdrop'''], '''decoder_layers''': args['''decoder_layers'''], '''bos_token_id''': 0, '''pad_token_id''': 1, '''eos_token_id''': 2, '''is_encoder_decoder''': True, '''scale_embedding''': not args['''no_scale_embedding'''], '''tie_word_embeddings''': args['''share_all_embeddings'''], } # good hparam defaults to start with SCREAMING_SNAKE_CASE : Union[str, Any] = 5 SCREAMING_SNAKE_CASE : str = False if model_dir in best_score_hparams and "length_penalty" in best_score_hparams[model_dir]: SCREAMING_SNAKE_CASE : Optional[int] = best_score_hparams[model_dir]['''length_penalty'''] else: SCREAMING_SNAKE_CASE : str = 1.0 print(f"""Generating {fsmt_model_config_file}""" ) with open(_lowercase , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(_lowercase , ensure_ascii=_lowercase , indent=_lowercase ) ) # tokenizer config SCREAMING_SNAKE_CASE : Tuple = os.path.join(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE : List[Any] = { '''langs''': [src_lang, tgt_lang], '''model_max_length''': 1_024, '''do_lower_case''': do_lower_case, } print(f"""Generating {fsmt_tokenizer_config_file}""" ) with open(_lowercase , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(_lowercase , ensure_ascii=_lowercase , indent=_lowercase ) ) # model SCREAMING_SNAKE_CASE : Dict = chkpt['''models'''][0] SCREAMING_SNAKE_CASE : str = model.state_dict() # rename keys to start with 'model.' SCREAMING_SNAKE_CASE : Optional[int] = OrderedDict(('''model.''' + k, v) for k, v in model_state_dict.items() ) # remove unneeded keys SCREAMING_SNAKE_CASE : List[str] = [ '''model.model''', '''model.encoder.version''', '''model.decoder.version''', '''model.encoder_embed_tokens.weight''', '''model.decoder_embed_tokens.weight''', '''model.encoder.embed_positions._float_tensor''', '''model.decoder.embed_positions._float_tensor''', ] for k in ignore_keys: model_state_dict.pop(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE : Union[str, Any] = FSMTConfig.from_pretrained(_lowercase ) SCREAMING_SNAKE_CASE : Union[str, Any] = FSMTForConditionalGeneration(_lowercase ) # check that it loads ok model_new.load_state_dict(_lowercase , strict=_lowercase ) # save SCREAMING_SNAKE_CASE : int = os.path.join(_lowercase , _lowercase ) print(f"""Generating {pytorch_weights_dump_path}""" ) torch.save(_lowercase , _lowercase ) print('''Conversion is done!''' ) print('''\nLast step is to upload the files to s3''' ) print(f"""cd {data_root}""" ) print(f"""transformers-cli upload {model_dir}""" ) if __name__ == "__main__": __UpperCamelCase : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( '--fsmt_checkpoint_path', default=None, type=str, required=True, help=( 'Path to the official PyTorch checkpoint file which is expected to reside in the dump dir with dicts,' ' bpecodes, etc.' ), ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __UpperCamelCase : int = parser.parse_args() convert_fsmt_checkpoint_to_pytorch(args.fsmt_checkpoint_path, args.pytorch_dump_folder_path)
361
from dataclasses import dataclass from enum import Enum from typing import List, Optional, Union import numpy as np import PIL from PIL import Image from ...utils import BaseOutput, is_torch_available, is_transformers_available @dataclass class lowercase__ ( UpperCamelCase_): UpperCamelCase_ = 42 UpperCamelCase_ = 42 if is_transformers_available() and is_torch_available(): from .pipeline_semantic_stable_diffusion import SemanticStableDiffusionPipeline
258
0
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 UpperCAmelCase_ ( __snake_case , __snake_case ) -> Optional[Any]: """simple docstring""" assert isinstance(__snake_case , __snake_case ) 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 UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> Union[str, Any]: """simple docstring""" _lowercase =tmp_path / '''cache''' _lowercase ={'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): _lowercase =ParquetDatasetReader(__snake_case , cache_dir=__snake_case , keep_in_memory=__snake_case ).read() _check_parquet_dataset(__snake_case , __snake_case ) @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 UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> Any: """simple docstring""" _lowercase =tmp_path / '''cache''' _lowercase ={'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} _lowercase =features.copy() if features else default_expected_features _lowercase =( Features({feature: Value(__snake_case ) for feature, dtype in features.items()} ) if features is not None else None ) _lowercase =ParquetDatasetReader(__snake_case , features=__snake_case , cache_dir=__snake_case ).read() _check_parquet_dataset(__snake_case , __snake_case ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> List[Any]: """simple docstring""" _lowercase =tmp_path / '''cache''' _lowercase ={'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} _lowercase =ParquetDatasetReader(__snake_case , cache_dir=__snake_case , split=__snake_case ).read() _check_parquet_dataset(__snake_case , __snake_case ) assert dataset.split == split if split else "train" @pytest.mark.parametrize('''path_type''' , [str, list] ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> Union[str, Any]: """simple docstring""" if issubclass(__snake_case , __snake_case ): _lowercase =parquet_path elif issubclass(__snake_case , __snake_case ): _lowercase =[parquet_path] _lowercase =tmp_path / '''cache''' _lowercase ={'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} _lowercase =ParquetDatasetReader(__snake_case , cache_dir=__snake_case ).read() _check_parquet_dataset(__snake_case , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case=("train",) ) -> Tuple: """simple docstring""" assert isinstance(__snake_case , __snake_case ) for split in splits: _lowercase =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 UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> List[Any]: """simple docstring""" _lowercase =tmp_path / '''cache''' _lowercase ={'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): _lowercase =ParquetDatasetReader( {'''train''': parquet_path} , cache_dir=__snake_case , keep_in_memory=__snake_case ).read() _check_parquet_datasetdict(__snake_case , __snake_case ) @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 UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> Tuple: """simple docstring""" _lowercase =tmp_path / '''cache''' _lowercase ={'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} _lowercase =features.copy() if features else default_expected_features _lowercase =( Features({feature: Value(__snake_case ) for feature, dtype in features.items()} ) if features is not None else None ) _lowercase =ParquetDatasetReader({'''train''': parquet_path} , features=__snake_case , cache_dir=__snake_case ).read() _check_parquet_datasetdict(__snake_case , __snake_case ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> Optional[Any]: """simple docstring""" if split: _lowercase ={split: parquet_path} else: _lowercase ='''train''' _lowercase ={'''train''': parquet_path, '''test''': parquet_path} _lowercase =tmp_path / '''cache''' _lowercase ={'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} _lowercase =ParquetDatasetReader(__snake_case , cache_dir=__snake_case ).read() _check_parquet_datasetdict(__snake_case , __snake_case , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> List[str]: """simple docstring""" _lowercase =ParquetDatasetWriter(__snake_case , tmp_path / '''foo.parquet''' ) assert writer.write() > 0 _lowercase =pq.ParquetFile(tmp_path / '''foo.parquet''' ) _lowercase =pf.read() assert dataset.data.table == output_table def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Tuple: """simple docstring""" _lowercase =str(shared_datadir / '''test_image_rgb.jpg''' ) _lowercase ={'''image''': [image_path]} _lowercase =Features({'''image''': Image()} ) _lowercase =Dataset.from_dict(__snake_case , features=__snake_case ) _lowercase =ParquetDatasetWriter(__snake_case , tmp_path / '''foo.parquet''' ) assert writer.write() > 0 _lowercase =Dataset.from_parquet(str(tmp_path / '''foo.parquet''' ) ) assert dataset.features == reloaded_dataset.features _lowercase =ParquetDatasetReader(str(tmp_path / '''foo.parquet''' ) , streaming=__snake_case ).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 UpperCAmelCase_ ( __snake_case , __snake_case ) -> Optional[int]: """simple docstring""" assert get_writer_batch_size(__snake_case ) == expected
5
import json import multiprocessing import os import re from collections import defaultdict import torch from accelerate import Accelerator from accelerate.utils import set_seed from arguments import HumanEvalArguments from datasets import load_dataset, load_metric from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from tqdm import tqdm import transformers from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList UpperCAmelCase__ = ['''\nclass''', '''\ndef''', '''\n#''', '''\n@''', '''\nprint''', '''\nif'''] class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=1 ) -> Dict: _lowercase =tokenizer _lowercase =dataset _lowercase =len(UpperCAmelCase ) if n_tasks is None else n_tasks _lowercase =n_copies def __iter__(self ) -> Optional[Any]: _lowercase =[] for task in range(self.n_tasks ): # without strip, the model generate commented codes ... prompts.append(self.tokenizer.eos_token + self.dataset[task]['''prompt'''].strip() ) _lowercase =self.tokenizer(UpperCAmelCase , padding=UpperCAmelCase , return_tensors='''pt''' ) for task in range(self.n_tasks ): for _ in range(self.n_copies ): yield { "ids": outputs.input_ids[task], "task_id": task, "input_len": outputs.attention_mask[task].sum(), } class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: _lowercase =start_length _lowercase =eof_strings _lowercase =tokenizer def __call__(self , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> Dict: _lowercase =self.tokenizer.batch_decode(input_ids[:, self.start_length :] ) _lowercase =[] for decoded_generation in decoded_generations: done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) ) return all(UpperCAmelCase ) def UpperCAmelCase_ ( __snake_case ) -> Optional[Any]: """simple docstring""" _lowercase =re.split('''(%s)''' % '''|'''.join(__snake_case ) , __snake_case ) # last string should be "" return "".join(string_list[:-2] ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case=20 , **__snake_case ) -> Tuple: """simple docstring""" _lowercase =defaultdict(__snake_case ) # dict of list of generated tokens for step, batch in tqdm(enumerate(__snake_case ) ): with torch.no_grad(): _lowercase =batch['''ids'''].shape[-1] _lowercase =accelerator.unwrap_model(__snake_case ).generate( input_ids=batch['''ids'''][:, : batch['''input_len''']] , num_return_sequences=__snake_case , **__snake_case ) # each task is generated batch_size times _lowercase =batch['''task_id'''].repeat(__snake_case ) _lowercase =accelerator.pad_across_processes( __snake_case , dim=1 , pad_index=tokenizer.pad_token_id ) _lowercase , _lowercase =accelerator.gather((generated_tokens, generated_tasks) ) _lowercase =generated_tokens.cpu().numpy() _lowercase =generated_tasks.cpu().numpy() for task, generated_tokens in zip(__snake_case , __snake_case ): gen_token_dict[task].append(__snake_case ) _lowercase =[[] for _ in range(__snake_case )] for task, generated_tokens in gen_token_dict.items(): for s in generated_tokens: _lowercase =tokenizer.decode(__snake_case , skip_special_tokens=__snake_case , clean_up_tokenization_spaces=__snake_case ) code_gens[task].append(remove_last_block(__snake_case ) ) return code_gens def UpperCAmelCase_ ( ) -> str: """simple docstring""" _lowercase =HfArgumentParser(__snake_case ) _lowercase =parser.parse_args() transformers.logging.set_verbosity_error() # enables code execution in code_eval metric _lowercase =args.HF_ALLOW_CODE_EVAL # make sure tokenizer plays nice with multiprocessing _lowercase ='''false''' if args.num_workers is None: _lowercase =multiprocessing.cpu_count() # Use dataset load to feed to accelerate _lowercase =Accelerator() set_seed(args.seed , device_specific=__snake_case ) # Load model and tokenizer _lowercase =AutoTokenizer.from_pretrained(args.model_ckpt ) _lowercase =tokenizer.eos_token _lowercase =AutoModelForCausalLM.from_pretrained(args.model_ckpt ) # Generation settings _lowercase ={ '''do_sample''': args.do_sample, '''temperature''': args.temperature, '''max_new_tokens''': args.max_new_tokens, '''top_p''': args.top_p, '''top_k''': args.top_k, '''stopping_criteria''': StoppingCriteriaList([EndOfFunctionCriteria(0 , __snake_case , __snake_case )] ), } # Load evaluation dataset and metric _lowercase =load_dataset('''openai_humaneval''' ) _lowercase =load_metric('''code_eval''' ) _lowercase =args.num_tasks if args.num_tasks is not None else len(human_eval['''test'''] ) _lowercase =args.n_samples // args.batch_size _lowercase =TokenizedDataset(__snake_case , human_eval['''test'''] , n_copies=__snake_case , n_tasks=__snake_case ) # do not confuse args.batch_size, which is actually the num_return_sequences _lowercase =DataLoader(__snake_case , batch_size=1 ) # Run a quick test to see if code evaluation is enabled try: _lowercase =code_eval_metric.compute(references=[''''''] , predictions=[['''''']] ) except ValueError as exception: print( '''Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL="1"`''' ''' flag to enable code evaluation.''' ) raise exception _lowercase , _lowercase =accelerator.prepare(__snake_case , __snake_case ) _lowercase =complete_code( __snake_case , __snake_case , __snake_case , __snake_case , n_tasks=__snake_case , batch_size=args.batch_size , **__snake_case , ) if accelerator.is_main_process: _lowercase =[] for task in tqdm(range(__snake_case ) ): _lowercase =human_eval['''test'''][task]['''test'''] _lowercase =F"check({human_eval['test'][task]['entry_point']})" references.append('''\n''' + test_func + '''\n''' + entry_point ) # Evaluate completions with "code_eval" metric _lowercase , _lowercase =code_eval_metric.compute( references=__snake_case , predictions=__snake_case , num_workers=args.num_workers ) print(F"Results: {pass_at_k}" ) # Save results to json file with open(args.output_file , '''w''' ) as fp: json.dump(__snake_case , __snake_case ) # For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing # https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script if __name__ == "__main__": main()
5
1
# Copyright 2023 The HuggingFace Inc. 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 ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor from .base import PipelineTool class lowerCAmelCase__( __lowercase ): '''simple docstring''' __snake_case = 'openai/whisper-base' __snake_case = ( 'This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the ' 'transcribed text.' ) __snake_case = 'transcriber' __snake_case = WhisperProcessor __snake_case = WhisperForConditionalGeneration __snake_case = ['audio'] __snake_case = ['text'] def UpperCamelCase_ ( self , __lowerCamelCase ) -> Tuple: return self.pre_processor(__lowerCamelCase , return_tensors="pt" ).input_features def UpperCamelCase_ ( self , __lowerCamelCase ) -> str: return self.model.generate(inputs=__lowerCamelCase ) def UpperCamelCase_ ( self , __lowerCamelCase ) -> Union[str, Any]: return self.pre_processor.batch_decode(__lowerCamelCase , skip_special_tokens=__lowerCamelCase )[0]
325
from __future__ import annotations import typing from collections import Counter def lowerCamelCase__ (__lowerCamelCase ): _SCREAMING_SNAKE_CASE : typing.Counter[int] = Counter() for base in range(1, max_perimeter + 1 ): for perpendicular in range(__lowerCamelCase, max_perimeter + 1 ): _SCREAMING_SNAKE_CASE : List[Any] = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(__lowerCamelCase ): _SCREAMING_SNAKE_CASE : Union[str, Any] = int(base + perpendicular + hypotenuse ) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def lowerCamelCase__ (__lowerCamelCase = 1000 ): _SCREAMING_SNAKE_CASE : Union[str, Any] = pythagorean_triple(__lowerCamelCase ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(f"Perimeter {solution()} has maximum solutions")
325
1
"""simple docstring""" from __future__ import annotations import unittest import numpy as np from transformers import OPTConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import GPTaTokenizer, TFOPTForCausalLM, TFOPTModel def A ( snake_case__ , snake_case__ , snake_case__=None , snake_case__=None ): '''simple docstring''' if attention_mask is None: SCREAMING_SNAKE_CASE__ = tf.cast(tf.math.not_equal(lowercase_ , config.pad_token_id ) , tf.inta ) return {"input_ids": input_ids, "attention_mask": attention_mask} @require_tf class lowerCamelCase : lowerCamelCase__ : str = OPTConfig lowerCamelCase__ : Optional[Any] = {} lowerCamelCase__ : Tuple = 'gelu' def __init__( self : Tuple , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Dict=1_3 , __UpperCAmelCase : List[str]=7 , __UpperCAmelCase : Dict=True , __UpperCAmelCase : Any=False , __UpperCAmelCase : Optional[int]=9_9 , __UpperCAmelCase : Dict=1_6 , __UpperCAmelCase : Dict=2 , __UpperCAmelCase : Tuple=4 , __UpperCAmelCase : Tuple=4 , __UpperCAmelCase : int="gelu" , __UpperCAmelCase : List[str]=0.1 , __UpperCAmelCase : Optional[Any]=0.1 , __UpperCAmelCase : Dict=2_0 , __UpperCAmelCase : Any=2 , __UpperCAmelCase : Tuple=1 , __UpperCAmelCase : str=0 , __UpperCAmelCase : int=1_6 , __UpperCAmelCase : List[Any]=1_6 , ) -> Optional[int]: SCREAMING_SNAKE_CASE__ = parent SCREAMING_SNAKE_CASE__ = batch_size SCREAMING_SNAKE_CASE__ = seq_length SCREAMING_SNAKE_CASE__ = is_training SCREAMING_SNAKE_CASE__ = use_labels SCREAMING_SNAKE_CASE__ = vocab_size SCREAMING_SNAKE_CASE__ = hidden_size SCREAMING_SNAKE_CASE__ = num_hidden_layers SCREAMING_SNAKE_CASE__ = num_attention_heads SCREAMING_SNAKE_CASE__ = intermediate_size SCREAMING_SNAKE_CASE__ = hidden_act SCREAMING_SNAKE_CASE__ = hidden_dropout_prob SCREAMING_SNAKE_CASE__ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE__ = max_position_embeddings SCREAMING_SNAKE_CASE__ = eos_token_id SCREAMING_SNAKE_CASE__ = pad_token_id SCREAMING_SNAKE_CASE__ = bos_token_id SCREAMING_SNAKE_CASE__ = embed_dim SCREAMING_SNAKE_CASE__ = word_embed_proj_dim SCREAMING_SNAKE_CASE__ = False def SCREAMING_SNAKE_CASE ( self : Tuple ) -> List[Any]: SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) SCREAMING_SNAKE_CASE__ = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) SCREAMING_SNAKE_CASE__ = tf.concat([input_ids, eos_tensor] , axis=1 ) SCREAMING_SNAKE_CASE__ = self.config_cls( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , embed_dim=self.embed_dim , word_embed_proj_dim=self.word_embed_proj_dim , is_encoder_decoder=UpperCAmelCase__ , **self.config_updates , ) SCREAMING_SNAKE_CASE__ = prepare_opt_inputs_dict(UpperCAmelCase__ , UpperCAmelCase__ ) return config, inputs_dict def SCREAMING_SNAKE_CASE ( self : List[str] , __UpperCAmelCase : List[str] , __UpperCAmelCase : int ) -> Optional[Any]: SCREAMING_SNAKE_CASE__ = TFOPTModel(config=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ = inputs_dict["""input_ids"""] SCREAMING_SNAKE_CASE__ = input_ids[:1, :] SCREAMING_SNAKE_CASE__ = inputs_dict["""attention_mask"""][:1, :] SCREAMING_SNAKE_CASE__ = 1 # first forward pass SCREAMING_SNAKE_CASE__ = model(UpperCAmelCase__ , attention_mask=UpperCAmelCase__ , use_cache=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids SCREAMING_SNAKE_CASE__ = ids_tensor((self.batch_size, 3) , config.vocab_size ) SCREAMING_SNAKE_CASE__ = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and SCREAMING_SNAKE_CASE__ = tf.concat([input_ids, next_tokens] , axis=-1 ) SCREAMING_SNAKE_CASE__ = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) SCREAMING_SNAKE_CASE__ = model(UpperCAmelCase__ , attention_mask=UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE__ = model(UpperCAmelCase__ , attention_mask=UpperCAmelCase__ , past_key_values=UpperCAmelCase__ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice SCREAMING_SNAKE_CASE__ = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) SCREAMING_SNAKE_CASE__ = output_from_no_past[:, -3:, random_slice_idx] SCREAMING_SNAKE_CASE__ = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(UpperCAmelCase__ , UpperCAmelCase__ , rtol=1e-3 ) @require_tf class lowerCamelCase (UpperCAmelCase__ ,UpperCAmelCase__ ,unittest.TestCase ): lowerCamelCase__ : Tuple = (TFOPTModel, TFOPTForCausalLM) if is_tf_available() else () lowerCamelCase__ : Tuple = (TFOPTForCausalLM,) if is_tf_available() else () lowerCamelCase__ : Optional[int] = ( {'feature-extraction': TFOPTModel, 'text-generation': TFOPTForCausalLM} if is_tf_available() else {} ) lowerCamelCase__ : str = False lowerCamelCase__ : Union[str, Any] = False lowerCamelCase__ : Any = False lowerCamelCase__ : Dict = 1_0 def SCREAMING_SNAKE_CASE ( self : str ) -> Any: SCREAMING_SNAKE_CASE__ = TFOPTModelTester(self ) SCREAMING_SNAKE_CASE__ = ConfigTester(self , config_class=UpperCAmelCase__ ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> List[Any]: self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Dict: SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*UpperCAmelCase__ ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs_for_common() def _get_word_embedding_weight(__UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[Any] ): if hasattr(UpperCAmelCase__ , """weight""" ): return embedding_layer.weight else: # Here we build the word embeddings weights if not exists. # And then we retry to get the attribute once built. model.build() if hasattr(UpperCAmelCase__ , """weight""" ): return embedding_layer.weight else: return None for model_class in self.all_model_classes: for size in [config.vocab_size - 1_0, config.vocab_size + 1_0]: # build the embeddings SCREAMING_SNAKE_CASE__ = model_class(config=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ = _get_word_embedding_weight(UpperCAmelCase__ , model.get_input_embeddings() ) SCREAMING_SNAKE_CASE__ = _get_word_embedding_weight(UpperCAmelCase__ , model.get_output_embeddings() ) # reshape the embeddings model.resize_token_embeddings(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ = _get_word_embedding_weight(UpperCAmelCase__ , model.get_input_embeddings() ) SCREAMING_SNAKE_CASE__ = _get_word_embedding_weight(UpperCAmelCase__ , model.get_output_embeddings() ) # check that the resized embeddings size matches the desired size. SCREAMING_SNAKE_CASE__ = size if size is not None else config.vocab_size self.assertEqual(new_input_embeddings.shape[0] , UpperCAmelCase__ ) # check that weights remain the same after resizing SCREAMING_SNAKE_CASE__ = True for pa, pa in zip(old_input_embeddings.value() , new_input_embeddings.value() ): if tf.math.reduce_sum(tf.math.abs(pa - pa ) ) > 0: SCREAMING_SNAKE_CASE__ = False self.assertTrue(UpperCAmelCase__ ) if old_output_embeddings is not None and new_output_embeddings is not None: self.assertEqual(new_output_embeddings.shape[0] , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ = True for pa, pa in zip(old_output_embeddings.value() , new_output_embeddings.value() ): if tf.math.reduce_sum(tf.math.abs(pa - pa ) ) > 0: SCREAMING_SNAKE_CASE__ = False self.assertTrue(UpperCAmelCase__ ) def A ( snake_case__ ): '''simple docstring''' return tf.constant(lowercase_ , dtype=tf.intaa ) @require_tf class lowerCamelCase (unittest.TestCase ): lowerCamelCase__ : Tuple = 9_9 def SCREAMING_SNAKE_CASE ( self : Any ) -> Optional[Any]: SCREAMING_SNAKE_CASE__ = tf.ones((4, 1) , dtype=tf.intaa ) * 2 SCREAMING_SNAKE_CASE__ = tf.concat([ids_tensor((4, 6) , self.vocab_size - 3 ) + 3, eos_column_vector] , axis=1 ) SCREAMING_SNAKE_CASE__ = input_ids.shape[0] SCREAMING_SNAKE_CASE__ = OPTConfig( vocab_size=self.vocab_size , hidden_size=2_4 , num_hidden_layers=2 , num_attention_heads=2 , ffn_dim=3_2 , max_position_embeddings=4_8 , eos_token_id=2 , pad_token_id=1 , bos_token_id=0 , ) return config, input_ids, batch_size @require_sentencepiece @require_tf class lowerCamelCase (unittest.TestCase ): @slow def SCREAMING_SNAKE_CASE ( self : Any ) -> Dict: SCREAMING_SNAKE_CASE__ = TFOPTModel.from_pretrained("""facebook/opt-350m""" ) SCREAMING_SNAKE_CASE__ = _long_tensor([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]] ) SCREAMING_SNAKE_CASE__ = tf.not_equal(UpperCAmelCase__ , model.config.pad_token_id ) with tf.GradientTape(): SCREAMING_SNAKE_CASE__ = model(input_ids=UpperCAmelCase__ , attention_mask=UpperCAmelCase__ ).last_hidden_state SCREAMING_SNAKE_CASE__ = (1, 1_1, 5_1_2) self.assertEqual(output.shape , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ = tf.constant( [[-0.2_873, -1.9_218, -0.3_033], [-1.2_710, -0.1_338, -0.1_902], [0.4_095, 0.1_214, -1.3_121]] ) self.assertTrue(np.allclose(output[:, :3, :3] , UpperCAmelCase__ , atol=4e-3 ) ) SCREAMING_SNAKE_CASE__ = tf.function(UpperCAmelCase__ , jit_compile=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ = xla_generate(UpperCAmelCase__ , UpperCAmelCase__ )[0] self.assertTrue(np.allclose(output[:, :3, :3] , UpperCAmelCase__ , atol=4e-2 ) ) @require_tf @slow class lowerCamelCase (unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> str: super().setUp() SCREAMING_SNAKE_CASE__ = """facebook/opt-350m""" def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Optional[int]: SCREAMING_SNAKE_CASE__ = TFOPTForCausalLM.from_pretrained(self.path_model ) SCREAMING_SNAKE_CASE__ = GPTaTokenizer.from_pretrained(self.path_model ) SCREAMING_SNAKE_CASE__ = [ """Today is a beautiful day and I want to""", """In the city of""", """Paris is the capital of France and""", """Computers and mobile phones have taken""", ] # verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False SCREAMING_SNAKE_CASE__ = tokenizer(UpperCAmelCase__ , return_tensors="""tf""" , padding=UpperCAmelCase__ , add_special_tokens=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ = tf.math.reduce_mean(model(inputs.input_ids , attention_mask=inputs.attention_mask )[0] , axis=-1 ) SCREAMING_SNAKE_CASE__ = tf.constant( [ [1.3_851, -13.8_923, -10.5_229, -10.7_533, -0.2_309, -10.2_384, -0.5_365, -9.0_947, -5.1_670], [-4.7_073, -10.6_276, -3.9_415, -21.5_242, -0.2_822, -0.2_822, -0.2_822, -0.2_822, -0.2_822], [0.6_247, -3.4_229, -8.9_179, -1.4_297, -14.1_650, 1.4_146, -9.0_218, -0.2_703, -0.2_703], [6.4_783, -1.9_913, -10.7_926, -2.3_336, 1.5_092, -0.9_974, -6.8_213, 1.3_477, 1.3_477], ] ) self.assertTrue(np.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1e-4 ) ) SCREAMING_SNAKE_CASE__ = tf.function(UpperCAmelCase__ , jit_compile=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ = tf.math.reduce_mean(xla_generate(inputs.input_ids , attention_mask=inputs.attention_mask )[0] , axis=-1 ) self.assertTrue(np.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1e-4 ) ) @require_tf @slow class lowerCamelCase (unittest.TestCase ): @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> int: return [ "Today is a beautiful day and I want", "In the city of", "Paris is the capital of France and", "Computers and mobile phones have taken", ] def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Dict: SCREAMING_SNAKE_CASE__ = """facebook/opt-125m""" SCREAMING_SNAKE_CASE__ = [ """Today is a beautiful day and I want to""", """In the city of New York, the city""", """Paris is the capital of France and the capital""", """Computers and mobile phones have taken over the""", ] SCREAMING_SNAKE_CASE__ = [] SCREAMING_SNAKE_CASE__ = GPTaTokenizer.from_pretrained(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ = TFOPTForCausalLM.from_pretrained(UpperCAmelCase__ ) for prompt in self.prompts: SCREAMING_SNAKE_CASE__ = tokenizer(UpperCAmelCase__ , return_tensors="""tf""" ).input_ids SCREAMING_SNAKE_CASE__ = model.generate(UpperCAmelCase__ , max_length=1_0 ) SCREAMING_SNAKE_CASE__ = tokenizer.batch_decode(UpperCAmelCase__ , skip_special_tokens=UpperCAmelCase__ ) predicted_outputs += generated_string self.assertListEqual(UpperCAmelCase__ , UpperCAmelCase__ ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[Any]: SCREAMING_SNAKE_CASE__ = """facebook/opt-350m""" SCREAMING_SNAKE_CASE__ = GPTaTokenizer.from_pretrained(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ = TFOPTForCausalLM.from_pretrained(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ = """left""" # use different length sentences to test batching SCREAMING_SNAKE_CASE__ = [ """Hello, my dog is a little""", """Today, I""", ] SCREAMING_SNAKE_CASE__ = tokenizer(UpperCAmelCase__ , return_tensors="""tf""" , padding=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ = inputs["""input_ids"""] SCREAMING_SNAKE_CASE__ = model.generate(input_ids=UpperCAmelCase__ , attention_mask=inputs["""attention_mask"""] ) SCREAMING_SNAKE_CASE__ = tokenizer(sentences[0] , return_tensors="""tf""" ).input_ids SCREAMING_SNAKE_CASE__ = model.generate(input_ids=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ = inputs_non_padded.shape[-1] - tf.math.reduce_sum( tf.cast(inputs["""attention_mask"""][-1] , tf.intaa ) ) SCREAMING_SNAKE_CASE__ = tokenizer(sentences[1] , return_tensors="""tf""" ).input_ids SCREAMING_SNAKE_CASE__ = model.generate(input_ids=UpperCAmelCase__ , max_length=model.config.max_length - num_paddings ) SCREAMING_SNAKE_CASE__ = tokenizer.batch_decode(UpperCAmelCase__ , skip_special_tokens=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ = tokenizer.decode(output_non_padded[0] , skip_special_tokens=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ = tokenizer.decode(output_padded[0] , skip_special_tokens=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ = [ """Hello, my dog is a little bit of a dork.\nI\'m a little bit""", """Today, I was in the middle of a conversation with a friend about the""", ] self.assertListEqual(UpperCAmelCase__ , UpperCAmelCase__ ) self.assertListEqual(UpperCAmelCase__ , [non_padded_sentence, padded_sentence] ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Dict: SCREAMING_SNAKE_CASE__ = """facebook/opt-350m""" SCREAMING_SNAKE_CASE__ = [ """Today is a beautiful day and I want to""", """In the city of San Francisco, the city""", """Paris is the capital of France and the capital""", """Computers and mobile phones have taken over the""", ] SCREAMING_SNAKE_CASE__ = [] SCREAMING_SNAKE_CASE__ = GPTaTokenizer.from_pretrained(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE__ = TFOPTForCausalLM.from_pretrained(UpperCAmelCase__ ) for prompt in self.prompts: SCREAMING_SNAKE_CASE__ = tokenizer(UpperCAmelCase__ , return_tensors="""tf""" ).input_ids SCREAMING_SNAKE_CASE__ = model.generate(UpperCAmelCase__ , max_length=1_0 ) SCREAMING_SNAKE_CASE__ = tokenizer.batch_decode(UpperCAmelCase__ , skip_special_tokens=UpperCAmelCase__ ) predicted_outputs += generated_string self.assertListEqual(UpperCAmelCase__ , UpperCAmelCase__ )
165
import os import sys import unittest _lowerCamelCase : Optional[Any] = 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, ) _lowerCamelCase : Any = os.path.join("""tests""", """models""", """bert""", """test_modeling_bert.py""") _lowerCamelCase : str = os.path.join("""tests""", """models""", """blip""", """test_modeling_blip.py""") class UpperCamelCase_ ( unittest.TestCase ): '''simple docstring''' def SCREAMING_SNAKE_CASE ( self : Tuple) ->Tuple: '''simple docstring''' A__ = get_test_to_tester_mapping(UpperCAmelCase__) A__ = get_test_to_tester_mapping(UpperCAmelCase__) A__ = {'''BertModelTest''': '''BertModelTester'''} A__ = { '''BlipModelTest''': '''BlipModelTester''', '''BlipTextImageModelTest''': '''BlipTextImageModelsModelTester''', '''BlipTextModelTest''': '''BlipTextModelTester''', '''BlipTextRetrievalModelTest''': '''BlipTextRetrievalModelTester''', '''BlipVQAModelTest''': '''BlipVQAModelTester''', '''BlipVisionModelTest''': '''BlipVisionModelTester''', } self.assertEqual(get_test_info.to_json(UpperCAmelCase__) , UpperCAmelCase__) self.assertEqual(get_test_info.to_json(UpperCAmelCase__) , UpperCAmelCase__) def SCREAMING_SNAKE_CASE ( self : Tuple) ->List[Any]: '''simple docstring''' A__ = get_model_to_test_mapping(UpperCAmelCase__) A__ = get_model_to_test_mapping(UpperCAmelCase__) A__ = { '''BertForMaskedLM''': ['''BertModelTest'''], '''BertForMultipleChoice''': ['''BertModelTest'''], '''BertForNextSentencePrediction''': ['''BertModelTest'''], '''BertForPreTraining''': ['''BertModelTest'''], '''BertForQuestionAnswering''': ['''BertModelTest'''], '''BertForSequenceClassification''': ['''BertModelTest'''], '''BertForTokenClassification''': ['''BertModelTest'''], '''BertLMHeadModel''': ['''BertModelTest'''], '''BertModel''': ['''BertModelTest'''], } A__ = { '''BlipForConditionalGeneration''': ['''BlipTextImageModelTest'''], '''BlipForImageTextRetrieval''': ['''BlipTextRetrievalModelTest'''], '''BlipForQuestionAnswering''': ['''BlipVQAModelTest'''], '''BlipModel''': ['''BlipModelTest'''], '''BlipTextModel''': ['''BlipTextModelTest'''], '''BlipVisionModel''': ['''BlipVisionModelTest'''], } self.assertEqual(get_test_info.to_json(UpperCAmelCase__) , UpperCAmelCase__) self.assertEqual(get_test_info.to_json(UpperCAmelCase__) , UpperCAmelCase__) def SCREAMING_SNAKE_CASE ( self : Optional[Any]) ->str: '''simple docstring''' A__ = get_model_to_tester_mapping(UpperCAmelCase__) A__ = get_model_to_tester_mapping(UpperCAmelCase__) A__ = { '''BertForMaskedLM''': ['''BertModelTester'''], '''BertForMultipleChoice''': ['''BertModelTester'''], '''BertForNextSentencePrediction''': ['''BertModelTester'''], '''BertForPreTraining''': ['''BertModelTester'''], '''BertForQuestionAnswering''': ['''BertModelTester'''], '''BertForSequenceClassification''': ['''BertModelTester'''], '''BertForTokenClassification''': ['''BertModelTester'''], '''BertLMHeadModel''': ['''BertModelTester'''], '''BertModel''': ['''BertModelTester'''], } A__ = { '''BlipForConditionalGeneration''': ['''BlipTextImageModelsModelTester'''], '''BlipForImageTextRetrieval''': ['''BlipTextRetrievalModelTester'''], '''BlipForQuestionAnswering''': ['''BlipVQAModelTester'''], '''BlipModel''': ['''BlipModelTester'''], '''BlipTextModel''': ['''BlipTextModelTester'''], '''BlipVisionModel''': ['''BlipVisionModelTester'''], } self.assertEqual(get_test_info.to_json(UpperCAmelCase__) , UpperCAmelCase__) self.assertEqual(get_test_info.to_json(UpperCAmelCase__) , UpperCAmelCase__)
14
0
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase : Optional[int] = logging.get_logger(__name__) lowercase : Optional[Any] = { "facebook/xmod-base": "https://huggingface.co/facebook/xmod-base/resolve/main/config.json", "facebook/xmod-large-prenorm": "https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json", "facebook/xmod-base-13-125k": "https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json", "facebook/xmod-base-30-125k": "https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json", "facebook/xmod-base-30-195k": "https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json", "facebook/xmod-base-60-125k": "https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json", "facebook/xmod-base-60-265k": "https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json", "facebook/xmod-base-75-125k": "https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json", "facebook/xmod-base-75-269k": "https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json", } class __UpperCAmelCase ( _lowerCamelCase ): __lowercase = """xmod""" def __init__( self , lowerCAmelCase_=3_05_22 , lowerCAmelCase_=7_68 , lowerCAmelCase_=12 , lowerCAmelCase_=12 , lowerCAmelCase_=30_72 , lowerCAmelCase_="gelu" , lowerCAmelCase_=0.1 , lowerCAmelCase_=0.1 , lowerCAmelCase_=5_12 , lowerCAmelCase_=2 , lowerCAmelCase_=0.02 , lowerCAmelCase_=1E-12 , lowerCAmelCase_=1 , lowerCAmelCase_=0 , lowerCAmelCase_=2 , lowerCAmelCase_="absolute" , lowerCAmelCase_=True , lowerCAmelCase_=None , lowerCAmelCase_=False , lowerCAmelCase_=2 , lowerCAmelCase_=False , lowerCAmelCase_=True , lowerCAmelCase_=True , lowerCAmelCase_=("en_XX",) , lowerCAmelCase_=None , **lowerCAmelCase_ , ): """simple docstring""" super().__init__(pad_token_id=lowerCAmelCase_ , bos_token_id=lowerCAmelCase_ , eos_token_id=lowerCAmelCase_ , **lowerCAmelCase_ ) _snake_case = vocab_size _snake_case = hidden_size _snake_case = num_hidden_layers _snake_case = num_attention_heads _snake_case = hidden_act _snake_case = intermediate_size _snake_case = hidden_dropout_prob _snake_case = attention_probs_dropout_prob _snake_case = max_position_embeddings _snake_case = type_vocab_size _snake_case = initializer_range _snake_case = layer_norm_eps _snake_case = position_embedding_type _snake_case = use_cache _snake_case = classifier_dropout _snake_case = pre_norm _snake_case = adapter_reduction_factor _snake_case = adapter_layer_norm _snake_case = adapter_reuse_layer_norm _snake_case = ln_before_adapter _snake_case = list(lowerCAmelCase_ ) _snake_case = default_language class __UpperCAmelCase ( _lowerCamelCase ): @property def lowerCamelCase ( self ): """simple docstring""" if self.task == "multiple-choice": _snake_case = {0: 'batch', 1: 'choice', 2: 'sequence'} else: _snake_case = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ] )
160
'''simple docstring''' import collections import tempfile import unittest import numpy as np from transformers.testing_utils import ( is_pt_flax_cross_test, require_flax, require_torch, require_vision, slow, torch_device, ) from transformers.utils import is_flax_available, is_torch_available, is_vision_available from ...test_modeling_flax_common import floats_tensor, ids_tensor, random_attention_mask from ..bert.test_modeling_flax_bert import FlaxBertModelTester from ..clip.test_modeling_flax_clip import FlaxCLIPVisionModelTester from ..vit.test_modeling_flax_vit import FlaxViTModelTester if is_flax_available(): from transformers import ( FlaxBertModel, FlaxCLIPVisionModel, FlaxVisionTextDualEncoderModel, FlaxViTModel, VisionTextDualEncoderConfig, VisionTextDualEncoderProcessor, ) from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) if is_torch_available(): import torch from transformers import VisionTextDualEncoderModel if is_vision_available(): from PIL import Image def SCREAMING_SNAKE_CASE__ ( __A ) -> Union[str, Any]: if isinstance(__A , collections.abc.Iterable ): return x return (x, x) @require_flax class __UpperCAmelCase : def lowerCamelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ ): """simple docstring""" pass def lowerCamelCase ( self ): """simple docstring""" pass def lowerCamelCase ( self ): """simple docstring""" pass def lowerCamelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ): """simple docstring""" _snake_case = np.abs((a - b) ).max() self.assertLessEqual(lowerCAmelCase_ , lowerCAmelCase_ , F'Difference between torch and flax is {diff} (>= {tol}).' ) def lowerCamelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_=None , **lowerCAmelCase_ ): """simple docstring""" _snake_case = VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase_ , lowerCAmelCase_ ) _snake_case = FlaxVisionTextDualEncoderModel(lowerCAmelCase_ ) _snake_case = model(input_ids=lowerCAmelCase_ , pixel_values=lowerCAmelCase_ , attention_mask=lowerCAmelCase_ ) self.assertEqual(output['text_embeds'].shape , (input_ids.shape[0], config.projection_dim) ) self.assertEqual(output['image_embeds'].shape , (pixel_values.shape[0], config.projection_dim) ) def lowerCamelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_=None , **lowerCAmelCase_ ): """simple docstring""" _snake_case , _snake_case = self.get_vision_text_model(lowerCAmelCase_ , lowerCAmelCase_ ) _snake_case = {'vision_model': vision_model, 'text_model': text_model} _snake_case = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase_ ) _snake_case = model(input_ids=lowerCAmelCase_ , pixel_values=lowerCAmelCase_ , attention_mask=lowerCAmelCase_ ) self.assertEqual(output['text_embeds'].shape , (input_ids.shape[0], model.config.projection_dim) ) self.assertEqual(output['image_embeds'].shape , (pixel_values.shape[0], model.config.projection_dim) ) def lowerCamelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_=None , **lowerCAmelCase_ ): """simple docstring""" _snake_case , _snake_case = self.get_vision_text_model(lowerCAmelCase_ , lowerCAmelCase_ ) _snake_case = {'vision_model': vision_model, 'text_model': text_model} _snake_case = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase_ ) _snake_case = model(input_ids=lowerCAmelCase_ , pixel_values=lowerCAmelCase_ , attention_mask=lowerCAmelCase_ ) _snake_case = output[0] with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(lowerCAmelCase_ ) _snake_case = FlaxVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase_ ) _snake_case = model(input_ids=lowerCAmelCase_ , pixel_values=lowerCAmelCase_ , attention_mask=lowerCAmelCase_ ) _snake_case = after_output[0] _snake_case = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(lowerCAmelCase_ , 1E-3 ) def lowerCamelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_=None , **lowerCAmelCase_ ): """simple docstring""" _snake_case , _snake_case = self.get_vision_text_model(lowerCAmelCase_ , lowerCAmelCase_ ) _snake_case = {'vision_model': vision_model, 'text_model': text_model} _snake_case = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase_ ) _snake_case = model( input_ids=lowerCAmelCase_ , pixel_values=lowerCAmelCase_ , attention_mask=lowerCAmelCase_ , output_attentions=lowerCAmelCase_ ) _snake_case = output.vision_model_output.attentions self.assertEqual(len(lowerCAmelCase_ ) , vision_config.num_hidden_layers ) # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token) _snake_case = to_atuple(vision_model.config.image_size ) _snake_case = to_atuple(vision_model.config.patch_size ) _snake_case = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) _snake_case = num_patches + 1 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len) ) _snake_case = output.text_model_output.attentions self.assertEqual(len(lowerCAmelCase_ ) , text_config.num_hidden_layers ) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def lowerCamelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ): """simple docstring""" pt_model.to(lowerCAmelCase_ ) pt_model.eval() # prepare inputs _snake_case = inputs_dict _snake_case = {k: torch.tensor(v.tolist() ) for k, v in flax_inputs.items()} with torch.no_grad(): _snake_case = pt_model(**lowerCAmelCase_ ).to_tuple() _snake_case = fx_model(**lowerCAmelCase_ ).to_tuple() self.assertEqual(len(lowerCAmelCase_ ) , len(lowerCAmelCase_ ) , 'Output lengths differ between Flax and PyTorch' ) for fx_output, pt_output in zip(fx_outputs[:4] , pt_outputs[:4] ): self.assert_almost_equals(lowerCAmelCase_ , pt_output.numpy() , 4E-2 ) # PT -> Flax with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(lowerCAmelCase_ ) _snake_case = FlaxVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase_ , from_pt=lowerCAmelCase_ ) _snake_case = fx_model_loaded(**lowerCAmelCase_ ).to_tuple() self.assertEqual(len(lowerCAmelCase_ ) , len(lowerCAmelCase_ ) , 'Output lengths differ between Flax and PyTorch' ) for fx_output_loaded, pt_output in zip(fx_outputs_loaded[:4] , pt_outputs[:4] ): self.assert_almost_equals(lowerCAmelCase_ , pt_output.numpy() , 4E-2 ) # Flax -> PT with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(lowerCAmelCase_ ) _snake_case = VisionTextDualEncoderModel.from_pretrained(lowerCAmelCase_ , from_flax=lowerCAmelCase_ ) pt_model_loaded.to(lowerCAmelCase_ ) pt_model_loaded.eval() with torch.no_grad(): _snake_case = pt_model_loaded(**lowerCAmelCase_ ).to_tuple() self.assertEqual(len(lowerCAmelCase_ ) , len(lowerCAmelCase_ ) , 'Output lengths differ between Flax and PyTorch' ) for fx_output, pt_output_loaded in zip(fx_outputs[:4] , pt_outputs_loaded[:4] ): self.assert_almost_equals(lowerCAmelCase_ , pt_output_loaded.numpy() , 4E-2 ) def lowerCamelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ): """simple docstring""" _snake_case = VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase_ , lowerCAmelCase_ ) _snake_case = VisionTextDualEncoderModel(lowerCAmelCase_ ) _snake_case = FlaxVisionTextDualEncoderModel(lowerCAmelCase_ ) _snake_case = convert_pytorch_state_dict_to_flax(pt_model.state_dict() , lowerCAmelCase_ ) _snake_case = fx_state self.check_pt_flax_equivalence(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) def lowerCamelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ): """simple docstring""" _snake_case = VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase_ , lowerCAmelCase_ ) _snake_case = VisionTextDualEncoderModel(lowerCAmelCase_ ) _snake_case = FlaxVisionTextDualEncoderModel(lowerCAmelCase_ ) _snake_case = load_flax_weights_in_pytorch_model(lowerCAmelCase_ , fx_model.params ) self.check_pt_flax_equivalence(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) def lowerCamelCase ( self ): """simple docstring""" _snake_case = self.prepare_config_and_inputs() self.check_model_from_pretrained_configs(**lowerCAmelCase_ ) def lowerCamelCase ( self ): """simple docstring""" _snake_case = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_from_pretrained(**lowerCAmelCase_ ) def lowerCamelCase ( self ): """simple docstring""" _snake_case = self.prepare_config_and_inputs() self.check_save_load(**lowerCAmelCase_ ) def lowerCamelCase ( self ): """simple docstring""" _snake_case = self.prepare_config_and_inputs() self.check_vision_text_output_attention(**lowerCAmelCase_ ) @is_pt_flax_cross_test def lowerCamelCase ( self ): """simple docstring""" _snake_case = self.prepare_config_and_inputs() _snake_case = config_inputs_dict.pop('vision_config' ) _snake_case = config_inputs_dict.pop('text_config' ) _snake_case = config_inputs_dict self.check_equivalence_pt_to_flax(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) self.check_equivalence_flax_to_pt(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) @slow def lowerCamelCase ( self ): """simple docstring""" _snake_case , _snake_case = self.get_pretrained_model_and_inputs() _snake_case = model_a(**lowerCAmelCase_ ) _snake_case = outputs[0] with tempfile.TemporaryDirectory() as tmp_dirname: model_a.save_pretrained(lowerCAmelCase_ ) _snake_case = FlaxVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase_ ) _snake_case = model_a(**lowerCAmelCase_ ) _snake_case = after_outputs[0] _snake_case = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(lowerCAmelCase_ , 1E-5 ) @require_flax class __UpperCAmelCase ( _lowerCamelCase , unittest.TestCase ): def lowerCamelCase ( self ): """simple docstring""" _snake_case = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained( 'hf-internal-testing/tiny-random-vit' , 'hf-internal-testing/tiny-bert' , vision_from_pt=lowerCAmelCase_ , text_from_pt=lowerCAmelCase_ , ) _snake_case = 13 _snake_case = floats_tensor( [ batch_size, model.config.vision_config.num_channels, model.config.vision_config.image_size, model.config.vision_config.image_size, ] ) _snake_case = ids_tensor([batch_size, 4] , model.config.text_config.vocab_size ) _snake_case = random_attention_mask([batch_size, 4] ) _snake_case = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def lowerCamelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ ): """simple docstring""" _snake_case = FlaxViTModel(lowerCAmelCase_ ) _snake_case = FlaxBertModel(lowerCAmelCase_ ) return vision_model, text_model def lowerCamelCase ( self ): """simple docstring""" _snake_case = FlaxViTModelTester(self ) _snake_case = FlaxBertModelTester(self ) _snake_case = vit_model_tester.prepare_config_and_inputs() _snake_case = bert_model_tester.prepare_config_and_inputs() _snake_case , _snake_case = vision_config_and_inputs _snake_case , _snake_case , _snake_case , _snake_case = text_config_and_inputs # make sure that cross attention layers are added return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": attention_mask, "input_ids": input_ids, "token_type_ids": token_type_ids, } @require_torch class __UpperCAmelCase ( _lowerCamelCase , unittest.TestCase ): def lowerCamelCase ( self ): """simple docstring""" _snake_case = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained( 'hf-internal-testing/tiny-random-clip' , 'hf-internal-testing/tiny-bert' , vision_from_pt=lowerCAmelCase_ , text_from_pt=lowerCAmelCase_ , ) _snake_case = 13 _snake_case = floats_tensor( [ batch_size, model.config.vision_config.num_channels, model.config.vision_config.image_size, model.config.vision_config.image_size, ] ) _snake_case = ids_tensor([batch_size, 4] , model.config.text_config.vocab_size ) _snake_case = random_attention_mask([batch_size, 4] ) _snake_case = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def lowerCamelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ ): """simple docstring""" _snake_case = FlaxCLIPVisionModel(lowerCAmelCase_ ) _snake_case = FlaxBertModel(lowerCAmelCase_ ) return vision_model, text_model def lowerCamelCase ( self ): """simple docstring""" _snake_case = FlaxCLIPVisionModelTester(self ) _snake_case = FlaxBertModelTester(self ) _snake_case = clip_model_tester.prepare_config_and_inputs() _snake_case = bert_model_tester.prepare_config_and_inputs() _snake_case , _snake_case = vision_config_and_inputs _snake_case , _snake_case , _snake_case , _snake_case = text_config_and_inputs # make sure that cross attention layers are added return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": attention_mask, "input_ids": input_ids, "token_type_ids": token_type_ids, } @require_flax @require_vision class __UpperCAmelCase ( unittest.TestCase ): @slow def lowerCamelCase ( self ): """simple docstring""" _snake_case = FlaxVisionTextDualEncoderModel.from_pretrained('clip-italian/clip-italian' , logit_scale_init_value=1.0 ) _snake_case = VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian' ) _snake_case = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) _snake_case = processor( text=['una foto di un gatto', 'una foto di un cane'] , images=lowerCAmelCase_ , padding=lowerCAmelCase_ , return_tensors='np' ) _snake_case = model(**lowerCAmelCase_ ) # verify the logits self.assertEqual(outputs.logits_per_image.shape , (inputs.pixel_values.shape[0], inputs.input_ids.shape[0]) ) self.assertEqual( outputs.logits_per_text.shape , (inputs.input_ids.shape[0], inputs.pixel_values.shape[0]) , ) _snake_case = np.array([[1.2284727, 0.3104122]] ) self.assertTrue(np.allclose(outputs.logits_per_image , lowerCAmelCase_ , atol=1E-3 ) )
160
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging SCREAMING_SNAKE_CASE = logging.get_logger(__name__) SCREAMING_SNAKE_CASE = { "google/fnet-base": "https://huggingface.co/google/fnet-base/resolve/main/config.json", "google/fnet-large": "https://huggingface.co/google/fnet-large/resolve/main/config.json" # See all FNet models at https://huggingface.co/models?filter=fnet } class UpperCAmelCase_ ( A_ ): lowercase__ = '''fnet''' def __init__( self : List[str] , snake_case_ : Optional[Any]=32_000 , snake_case_ : Tuple=768 , snake_case_ : Dict=12 , snake_case_ : int=3_072 , snake_case_ : List[Any]="gelu_new" , snake_case_ : Union[str, Any]=0.1 , snake_case_ : Optional[Any]=512 , snake_case_ : Optional[Any]=4 , snake_case_ : Union[str, Any]=0.02 , snake_case_ : Union[str, Any]=1e-12 , snake_case_ : Tuple=False , snake_case_ : str=512 , snake_case_ : int=3 , snake_case_ : Optional[Any]=1 , snake_case_ : Optional[int]=2 , **snake_case_ : Any , ) -> str: '''simple docstring''' super().__init__(pad_token_id=snake_case_ , bos_token_id=snake_case_ , eos_token_id=snake_case_ , **snake_case_ ) A__ = vocab_size A__ = max_position_embeddings A__ = hidden_size A__ = num_hidden_layers A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = initializer_range A__ = type_vocab_size A__ = layer_norm_eps A__ = use_tpu_fourier_optimizations A__ = tpu_short_seq_length
247
"""simple docstring""" import logging import sys from dataclasses import dataclass, field from typing import Any, Dict, List, Optional, Union import librosa import torch from datasets import DatasetDict, load_dataset from packaging import version from torch import nn from transformers import ( HfArgumentParser, Trainer, TrainingArguments, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaForPreTraining, is_apex_available, trainer_utils, ) from transformers.models.wavaveca.modeling_wavaveca import _compute_mask_indices if is_apex_available(): from apex import amp if version.parse(version.parse(torch.__version__).base_version) >= version.parse("1.6"): SCREAMING_SNAKE_CASE = True from torch.cuda.amp import autocast SCREAMING_SNAKE_CASE = logging.getLogger(__name__) @dataclass class UpperCAmelCase_ : lowercase__ = field( metadata={'''help''': '''Path to pretrained model or model identifier from huggingface.co/models'''} ) lowercase__ = field( default=A_, metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''}, ) lowercase__ = field( default=A_, metadata={'''help''': '''Whether to freeze the feature extractor layers of the model.'''} ) lowercase__ = field( default=A_, metadata={'''help''': '''Whether to log verbose messages or not.'''}, ) lowercase__ = field( default=2.0, metadata={'''help''': '''Maximum temperature for gumbel softmax.'''} ) lowercase__ = field( default=0.5, metadata={'''help''': '''Minimum temperature for gumbel softmax.'''} ) lowercase__ = field( default=0.99_99_95, metadata={'''help''': '''Decay of gumbel temperature during training.'''} ) def _SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_ ) -> int: logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , handlers=[logging.StreamHandler(sys.stdout )] , ) A__ = logging.WARNING if model_args.verbose_logging: A__ = logging.DEBUG elif trainer_utils.is_main_process(training_args.local_rank ): A__ = logging.INFO logger.setLevel(lowercase_ ) @dataclass class UpperCAmelCase_ : lowercase__ = field( default=A_, metadata={'''help''': '''The name of the dataset to use (via the datasets library).'''} ) lowercase__ = field( default=A_, metadata={'''help''': '''The configuration name of the dataset to use (via the datasets library).'''} ) lowercase__ = field( default='''train''', metadata={ '''help''': '''The name of the training data set split to use (via the datasets library). Defaults to \'train\'''' }, ) lowercase__ = field( default='''validation''', metadata={ '''help''': ( '''The name of the validation data set split to use (via the datasets library). Defaults to \'validation\'''' ) }, ) lowercase__ = field( default='''file''', metadata={'''help''': '''Column in the dataset that contains speech file path. Defaults to \'file\''''}, ) lowercase__ = field( default=A_, metadata={'''help''': '''Overwrite the cached preprocessed datasets or not.'''} ) lowercase__ = field( default=1, metadata={ '''help''': '''The percentage of the train set used as validation set in case there\'s no validation split''' }, ) lowercase__ = field( default=A_, metadata={'''help''': '''The number of processes to use for the preprocessing.'''}, ) lowercase__ = field( default=20.0, metadata={'''help''': '''Filter audio files that are longer than `max_duration_in_seconds` seconds'''} ) @dataclass class UpperCAmelCase_ : lowercase__ = 42 lowercase__ = 42 lowercase__ = "longest" lowercase__ = None lowercase__ = None def __call__( self : Tuple , snake_case_ : List[Dict[str, Union[List[int], torch.Tensor]]] ) -> Dict[str, torch.Tensor]: '''simple docstring''' A__ = self.feature_extractor.pad( snake_case_ , max_length=self.max_length , padding=self.padding , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors="pt" , ) A__ = self.model._get_feat_extract_output_lengths(batch["input_values"].shape[-1] ) A__ = batch["input_values"].shape[0] # make sure that no loss is computed on padded inputs if batch["attention_mask"] is not None: # compute real output lengths according to convolution formula A__ = self.model._get_feat_extract_output_lengths(batch["attention_mask"].sum(-1 ) ).to( torch.long ) A__ = torch.zeros( (batch_size, mask_indices_seq_length) , dtype=torch.long , device=batch["input_values"].device ) # these two operations makes sure that all values # before the output lengths indices are attended to A__ = 1 A__ = attention_mask.flip([-1] ).cumsum(-1 ).flip([-1] ).bool() # sample randomly masked indices A__ = _compute_mask_indices( (batch_size, mask_indices_seq_length) , self.model.config.mask_time_prob , self.model.config.mask_time_length , attention_mask=snake_case_ , min_masks=2 , ) return batch class UpperCAmelCase_ ( A_ ): def __init__( self : Any , *snake_case_ : Dict , snake_case_ : Optional[int]=1 , snake_case_ : str=0 , snake_case_ : str=1.0 , **snake_case_ : List[str] ) -> List[str]: '''simple docstring''' super().__init__(*snake_case_ , **snake_case_ ) A__ = 0 A__ = max_gumbel_temp A__ = min_gumbel_temp A__ = gumbel_temp_decay def __magic_name__ ( self : Tuple , snake_case_ : nn.Module , snake_case_ : Dict[str, Union[torch.Tensor, Any]] ) -> torch.Tensor: '''simple docstring''' model.train() A__ = self._prepare_inputs(snake_case_ ) if self.use_amp: with autocast(): A__ = self.compute_loss(snake_case_ , snake_case_ ) else: A__ = self.compute_loss(snake_case_ , snake_case_ ) if self.args.n_gpu > 1 or self.deepspeed: if model.module.config.ctc_loss_reduction == "mean": A__ = loss.mean() elif model.module.config.ctc_loss_reduction == "sum": A__ = loss.sum() / (inputs["mask_time_indices"]).sum() else: raise ValueError(F"""{model.config.ctc_loss_reduction} is not valid. Choose one of ['mean', 'sum']""" ) if self.args.gradient_accumulation_steps > 1: A__ = loss / self.args.gradient_accumulation_steps if self.use_amp: self.scaler.scale(snake_case_ ).backward() elif self.use_apex: with amp.scale_loss(snake_case_ , self.optimizer ) as scaled_loss: scaled_loss.backward() elif self.deepspeed: self.deepspeed.backward(snake_case_ ) else: loss.backward() self.num_update_step += 1 # make sure gumbel softmax temperature is decayed if self.args.n_gpu > 1 or self.deepspeed: model.module.set_gumbel_temperature( max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step , self.min_gumbel_temp ) ) else: model.set_gumbel_temperature( max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step , self.min_gumbel_temp ) ) return loss.detach() def _SCREAMING_SNAKE_CASE ( ) -> List[str]: # 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__ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) A__, A__, A__ = parser.parse_args_into_dataclasses() configure_logger(lowercase_ , lowercase_ ) # Downloading and loading a dataset from the hub. A__ = load_dataset(data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) if "validation" not in datasets.keys(): # make sure only "validation" and "train" keys remain" A__ = DatasetDict() A__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=f"""{data_args.train_split_name}[:{data_args.validation_split_percentage}%]""" , cache_dir=model_args.cache_dir , ) A__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=f"""{data_args.train_split_name}[{data_args.validation_split_percentage}%:]""" , cache_dir=model_args.cache_dir , ) else: # make sure only "validation" and "train" keys remain" A__ = DatasetDict() A__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split="validation" , cache_dir=model_args.cache_dir , ) A__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=f"""{data_args.train_split_name}""" , cache_dir=model_args.cache_dir , ) # only normalized-inputs-training is supported A__ = WavaVecaFeatureExtractor.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , do_normalize=lowercase_ ) def prepare_dataset(lowercase_ ): # check that all files have the correct sampling rate A__, A__ = librosa.load(batch[data_args.speech_file_column] , sr=feature_extractor.sampling_rate ) return batch # load audio files into numpy arrays A__ = datasets.map( lowercase_ , num_proc=data_args.preprocessing_num_workers , remove_columns=datasets["train"].column_names ) # filter audio files that are too long A__ = vectorized_datasets.filter( lambda lowercase_ : len(data["speech"] ) < int(data_args.max_duration_in_seconds * feature_extractor.sampling_rate ) ) def normalize(lowercase_ ): return feature_extractor(batch["speech"] , sampling_rate=feature_extractor.sampling_rate ) # normalize and transform to `BatchFeatures` A__ = vectorized_datasets.map( lowercase_ , batched=lowercase_ , num_proc=data_args.preprocessing_num_workers , load_from_cache_file=not data_args.overwrite_cache , remove_columns=vectorized_datasets["train"].column_names , ) # pretraining is only supported for "newer" stable layer norm architecture # apply_spec_augment has to be True, mask_feature_prob has to be 0.0 A__ = WavaVecaConfig.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , gradient_checkpointing=training_args.gradient_checkpointing , ) if not config.do_stable_layer_norm or config.feat_extract_norm != "layer": raise ValueError( "PreTraining is only supported for ``config.do_stable_layer_norm=True`` and" " ``config.feat_extract_norm='layer'" ) A__ = WavaVecaForPreTraining(lowercase_ ) A__ = DataCollatorForWavaVecaPretraining(model=lowercase_ , feature_extractor=lowercase_ ) A__ = WavaVecaPreTrainer( model=lowercase_ , data_collator=lowercase_ , args=lowercase_ , train_dataset=vectorized_datasets["train"] , eval_dataset=vectorized_datasets["validation"] , tokenizer=lowercase_ , max_gumbel_temp=model_args.max_gumbel_temperature , min_gumbel_temp=model_args.min_gumbel_temperature , gumbel_temp_decay=model_args.gumbel_temperature_decay , ) trainer.train() if __name__ == "__main__": main()
247
1
'''simple docstring''' import collections import gzip import os import urllib import numpy from tensorflow.python.framework import dtypes, random_seed from tensorflow.python.platform import gfile from tensorflow.python.util.deprecation import deprecated _lowerCamelCase = collections.namedtuple("""_Datasets""", ["""train""", """validation""", """test"""]) # CVDF mirror of http://yann.lecun.com/exdb/mnist/ _lowerCamelCase = """https://storage.googleapis.com/cvdf-datasets/mnist/""" def a__ ( _SCREAMING_SNAKE_CASE : int ) -> Dict: """simple docstring""" UpperCAmelCase_ : List[str] = numpy.dtype(numpy.uintaa ).newbyteorder(">" ) return numpy.frombuffer(bytestream.read(4 ) , dtype=_SCREAMING_SNAKE_CASE )[0] @deprecated(_SCREAMING_SNAKE_CASE , "Please use tf.data to implement this functionality." ) def a__ ( _SCREAMING_SNAKE_CASE : Optional[int] ) -> List[str]: """simple docstring""" print("Extracting" , f.name ) with gzip.GzipFile(fileobj=_SCREAMING_SNAKE_CASE ) as bytestream: UpperCAmelCase_ : Tuple = _readaa(_SCREAMING_SNAKE_CASE ) if magic != 20_51: raise ValueError( "Invalid magic number %d in MNIST image file: %s" % (magic, f.name) ) UpperCAmelCase_ : Any = _readaa(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[Any] = _readaa(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = _readaa(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Tuple = bytestream.read(rows * cols * num_images ) UpperCAmelCase_ : Union[str, Any] = numpy.frombuffer(_SCREAMING_SNAKE_CASE , dtype=numpy.uinta ) UpperCAmelCase_ : int = data.reshape(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , 1 ) return data @deprecated(_SCREAMING_SNAKE_CASE , "Please use tf.one_hot on tensors." ) def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : str ) -> List[str]: """simple docstring""" UpperCAmelCase_ : List[str] = labels_dense.shape[0] UpperCAmelCase_ : Dict = numpy.arange(_SCREAMING_SNAKE_CASE ) * num_classes UpperCAmelCase_ : Optional[int] = numpy.zeros((num_labels, num_classes) ) UpperCAmelCase_ : str = 1 return labels_one_hot @deprecated(_SCREAMING_SNAKE_CASE , "Please use tf.data to implement this functionality." ) def a__ ( _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : Optional[int]=False , _SCREAMING_SNAKE_CASE : Dict=10 ) -> Optional[int]: """simple docstring""" print("Extracting" , f.name ) with gzip.GzipFile(fileobj=_SCREAMING_SNAKE_CASE ) as bytestream: UpperCAmelCase_ : Any = _readaa(_SCREAMING_SNAKE_CASE ) if magic != 20_49: raise ValueError( "Invalid magic number %d in MNIST label file: %s" % (magic, f.name) ) UpperCAmelCase_ : Union[str, Any] = _readaa(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[Any] = bytestream.read(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Tuple = numpy.frombuffer(_SCREAMING_SNAKE_CASE , dtype=numpy.uinta ) if one_hot: return _dense_to_one_hot(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return labels class _snake_case : @deprecated( _snake_case ,"Please use alternatives such as official/mnist/_DataSet.py" " from tensorflow/models." ,) def __init__( self ,_snake_case ,_snake_case ,_snake_case=False ,_snake_case=False ,_snake_case=dtypes.floataa ,_snake_case=True ,_snake_case=None ,): UpperCAmelCase_ , UpperCAmelCase_ : Optional[Any] = random_seed.get_seed(_snake_case ) # If op level seed is not set, use whatever graph level seed is returned numpy.random.seed(seeda if seed is None else seeda ) UpperCAmelCase_ : Optional[Any] = dtypes.as_dtype(_snake_case ).base_dtype if dtype not in (dtypes.uinta, dtypes.floataa): raise TypeError("Invalid image dtype %r, expected uint8 or float32" % dtype ) if fake_data: UpperCAmelCase_ : List[str] = 1_00_00 UpperCAmelCase_ : Union[str, Any] = one_hot else: assert ( images.shape[0] == labels.shape[0] ), f'''images.shape: {images.shape} labels.shape: {labels.shape}''' UpperCAmelCase_ : Any = images.shape[0] # Convert shape from [num examples, rows, columns, depth] # to [num examples, rows*columns] (assuming depth == 1) if reshape: assert images.shape[3] == 1 UpperCAmelCase_ : Union[str, Any] = images.reshape( images.shape[0] ,images.shape[1] * images.shape[2] ) if dtype == dtypes.floataa: # Convert from [0, 255] -> [0.0, 1.0]. UpperCAmelCase_ : Union[str, Any] = images.astype(numpy.floataa ) UpperCAmelCase_ : int = numpy.multiply(_snake_case ,1.0 / 255.0 ) UpperCAmelCase_ : Union[str, Any] = images UpperCAmelCase_ : Tuple = labels UpperCAmelCase_ : Tuple = 0 UpperCAmelCase_ : str = 0 @property def UpperCamelCase__ ( self ): return self._images @property def UpperCamelCase__ ( self ): return self._labels @property def UpperCamelCase__ ( self ): return self._num_examples @property def UpperCamelCase__ ( self ): return self._epochs_completed def UpperCamelCase__ ( self ,_snake_case ,_snake_case=False ,_snake_case=True ): if fake_data: UpperCAmelCase_ : Dict = [1] * 7_84 UpperCAmelCase_ : Tuple = [1] + [0] * 9 if self.one_hot else 0 return ( [fake_image for _ in range(_snake_case )], [fake_label for _ in range(_snake_case )], ) UpperCAmelCase_ : List[str] = self._index_in_epoch # Shuffle for the first epoch if self._epochs_completed == 0 and start == 0 and shuffle: UpperCAmelCase_ : Union[str, Any] = numpy.arange(self._num_examples ) numpy.random.shuffle(_snake_case ) UpperCAmelCase_ : Union[str, Any] = self.images[perma] UpperCAmelCase_ : Tuple = self.labels[perma] # Go to the next epoch if start + batch_size > self._num_examples: # Finished epoch self._epochs_completed += 1 # Get the rest examples in this epoch UpperCAmelCase_ : Tuple = self._num_examples - start UpperCAmelCase_ : Optional[Any] = self._images[start : self._num_examples] UpperCAmelCase_ : int = self._labels[start : self._num_examples] # Shuffle the data if shuffle: UpperCAmelCase_ : str = numpy.arange(self._num_examples ) numpy.random.shuffle(_snake_case ) UpperCAmelCase_ : Union[str, Any] = self.images[perm] UpperCAmelCase_ : Any = self.labels[perm] # Start next epoch UpperCAmelCase_ : Any = 0 UpperCAmelCase_ : List[Any] = batch_size - rest_num_examples UpperCAmelCase_ : str = self._index_in_epoch UpperCAmelCase_ : Optional[Any] = self._images[start:end] UpperCAmelCase_ : Any = self._labels[start:end] return ( numpy.concatenate((images_rest_part, images_new_part) ,axis=0 ), numpy.concatenate((labels_rest_part, labels_new_part) ,axis=0 ), ) else: self._index_in_epoch += batch_size UpperCAmelCase_ : Tuple = self._index_in_epoch return self._images[start:end], self._labels[start:end] @deprecated(_SCREAMING_SNAKE_CASE , "Please write your own downloading logic." ) def a__ ( _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : List[Any] ) -> Dict: """simple docstring""" if not gfile.Exists(_SCREAMING_SNAKE_CASE ): gfile.MakeDirs(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Any = os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if not gfile.Exists(_SCREAMING_SNAKE_CASE ): urllib.request.urlretrieve(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # noqa: S310 with gfile.GFile(_SCREAMING_SNAKE_CASE ) as f: UpperCAmelCase_ : List[Any] = f.size() print("Successfully downloaded" , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , "bytes." ) return filepath @deprecated( _SCREAMING_SNAKE_CASE , "Please use alternatives such as:" " tensorflow_datasets.load('mnist')" ) def a__ ( _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : int=False , _SCREAMING_SNAKE_CASE : Optional[Any]=False , _SCREAMING_SNAKE_CASE : Union[str, Any]=dtypes.floataa , _SCREAMING_SNAKE_CASE : Tuple=True , _SCREAMING_SNAKE_CASE : str=50_00 , _SCREAMING_SNAKE_CASE : Optional[int]=None , _SCREAMING_SNAKE_CASE : Optional[Any]=DEFAULT_SOURCE_URL , ) -> Dict: """simple docstring""" if fake_data: def fake(): return _DataSet( [] , [] , fake_data=_SCREAMING_SNAKE_CASE , one_hot=_SCREAMING_SNAKE_CASE , dtype=_SCREAMING_SNAKE_CASE , seed=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Tuple = fake() UpperCAmelCase_ : str = fake() UpperCAmelCase_ : Optional[int] = fake() return _Datasets(train=_SCREAMING_SNAKE_CASE , validation=_SCREAMING_SNAKE_CASE , test=_SCREAMING_SNAKE_CASE ) if not source_url: # empty string check UpperCAmelCase_ : Dict = DEFAULT_SOURCE_URL UpperCAmelCase_ : Any = "train-images-idx3-ubyte.gz" UpperCAmelCase_ : List[str] = "train-labels-idx1-ubyte.gz" UpperCAmelCase_ : Union[str, Any] = "t10k-images-idx3-ubyte.gz" UpperCAmelCase_ : List[str] = "t10k-labels-idx1-ubyte.gz" UpperCAmelCase_ : List[Any] = _maybe_download( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , source_url + train_images_file ) with gfile.Open(_SCREAMING_SNAKE_CASE , "rb" ) as f: UpperCAmelCase_ : int = _extract_images(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[str] = _maybe_download( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , source_url + train_labels_file ) with gfile.Open(_SCREAMING_SNAKE_CASE , "rb" ) as f: UpperCAmelCase_ : int = _extract_labels(_SCREAMING_SNAKE_CASE , one_hot=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Tuple = _maybe_download( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , source_url + test_images_file ) with gfile.Open(_SCREAMING_SNAKE_CASE , "rb" ) as f: UpperCAmelCase_ : List[str] = _extract_images(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = _maybe_download( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , source_url + test_labels_file ) with gfile.Open(_SCREAMING_SNAKE_CASE , "rb" ) as f: UpperCAmelCase_ : str = _extract_labels(_SCREAMING_SNAKE_CASE , one_hot=_SCREAMING_SNAKE_CASE ) if not 0 <= validation_size <= len(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Dict = ( "Validation size should be between 0 and " F'''{len(_SCREAMING_SNAKE_CASE )}. Received: {validation_size}.''' ) raise ValueError(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[str] = train_images[:validation_size] UpperCAmelCase_ : Optional[Any] = train_labels[:validation_size] UpperCAmelCase_ : Any = train_images[validation_size:] UpperCAmelCase_ : List[str] = train_labels[validation_size:] UpperCAmelCase_ : Any = {"dtype": dtype, "reshape": reshape, "seed": seed} UpperCAmelCase_ : List[Any] = _DataSet(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Tuple = _DataSet(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = _DataSet(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) return _Datasets(train=_SCREAMING_SNAKE_CASE , validation=_SCREAMING_SNAKE_CASE , test=_SCREAMING_SNAKE_CASE )
67
'''simple docstring''' from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class _snake_case : def __init__( self ,_snake_case ,_snake_case=12 ,_snake_case=7 ,_snake_case=True ,_snake_case=True ,_snake_case=True ,_snake_case=99 ,_snake_case=32 ,_snake_case=32 ,_snake_case=2 ,_snake_case=4 ,_snake_case=37 ,_snake_case=0.1 ,_snake_case=0.1 ,_snake_case=5_12 ,_snake_case=0.02 ,_snake_case=0 ,_snake_case=None ,): UpperCAmelCase_ : Any = parent UpperCAmelCase_ : int = batch_size UpperCAmelCase_ : Optional[int] = seq_length UpperCAmelCase_ : Union[str, Any] = is_training UpperCAmelCase_ : str = use_input_mask UpperCAmelCase_ : List[Any] = use_labels UpperCAmelCase_ : Union[str, Any] = vocab_size UpperCAmelCase_ : List[Any] = hidden_size UpperCAmelCase_ : Any = projection_dim UpperCAmelCase_ : Optional[Any] = num_hidden_layers UpperCAmelCase_ : Union[str, Any] = num_attention_heads UpperCAmelCase_ : Optional[Any] = intermediate_size UpperCAmelCase_ : Any = dropout UpperCAmelCase_ : Dict = attention_dropout UpperCAmelCase_ : Optional[Any] = max_position_embeddings UpperCAmelCase_ : List[Any] = initializer_range UpperCAmelCase_ : Optional[int] = scope UpperCAmelCase_ : List[str] = bos_token_id def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) UpperCAmelCase_ : List[Any] = None if self.use_input_mask: UpperCAmelCase_ : Optional[Any] = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: UpperCAmelCase_ : Any = input_mask.numpy() UpperCAmelCase_ , UpperCAmelCase_ : str = input_mask.shape UpperCAmelCase_ : str = np.random.randint(1 ,seq_length - 1 ,size=(batch_size,) ) for batch_idx, start_index in enumerate(_snake_case ): UpperCAmelCase_ : Optional[int] = 1 UpperCAmelCase_ : Union[str, Any] = 0 UpperCAmelCase_ : int = self.get_config() return config, input_ids, tf.convert_to_tensor(_snake_case ) def UpperCamelCase__ ( self ): return BlipTextConfig( vocab_size=self.vocab_size ,hidden_size=self.hidden_size ,projection_dim=self.projection_dim ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,dropout=self.dropout ,attention_dropout=self.attention_dropout ,max_position_embeddings=self.max_position_embeddings ,initializer_range=self.initializer_range ,bos_token_id=self.bos_token_id ,) def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ): UpperCAmelCase_ : Union[str, Any] = TFBlipTextModel(config=_snake_case ) UpperCAmelCase_ : Optional[int] = model(_snake_case ,attention_mask=_snake_case ,training=_snake_case ) UpperCAmelCase_ : Dict = model(_snake_case ,training=_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape ,(self.batch_size, self.hidden_size) ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = self.prepare_config_and_inputs() UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : str = config_and_inputs UpperCAmelCase_ : str = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_tf class _snake_case (__SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Tuple =(TFBlipTextModel,) if is_tf_available() else () __A : List[Any] =False __A : List[Any] =False __A : Any =False def UpperCamelCase__ ( self ): UpperCAmelCase_ : Any = BlipTextModelTester(self ) UpperCAmelCase_ : List[Any] = ConfigTester(self ,config_class=_snake_case ,hidden_size=37 ) def UpperCamelCase__ ( self ): self.config_tester.run_common_tests() def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def UpperCamelCase__ ( self ): pass def UpperCamelCase__ ( self ): pass @unittest.skip(reason="Blip does not use inputs_embeds" ) def UpperCamelCase__ ( self ): pass @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING" ) def UpperCamelCase__ ( self ): pass @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING" ) def UpperCamelCase__ ( self ): pass @slow def UpperCamelCase__ ( self ): for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : int = TFBlipTextModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def UpperCamelCase__ ( self ,_snake_case=True ): super().test_pt_tf_model_equivalence(allow_missing_keys=_snake_case )
67
1
import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __init__( self : Dict , lowerCAmelCase_ : Tuple , lowerCAmelCase_ : Optional[Any]=7 , lowerCAmelCase_ : Union[str, Any]=3 , lowerCAmelCase_ : List[str]=1_8 , lowerCAmelCase_ : str=3_0 , lowerCAmelCase_ : Any=4_0_0 , lowerCAmelCase_ : Optional[Any]=True , lowerCAmelCase_ : Tuple=None , lowerCAmelCase_ : Optional[Any]=True , ): """simple docstring""" _A: int = size if size is not None else {'''height''': 1_8, '''width''': 1_8} _A: List[Any] = parent _A: Any = batch_size _A: Any = num_channels _A: Union[str, Any] = image_size _A: Optional[Any] = min_resolution _A: Optional[int] = max_resolution _A: List[str] = do_resize _A: Optional[Any] = size _A: Tuple = apply_ocr def __magic_name__ ( self : str ): """simple docstring""" return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class UpperCAmelCase ( A__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase : List[Any] = LayoutLMvaImageProcessor if is_pytesseract_available() else None def __magic_name__ ( self : Tuple ): """simple docstring""" _A: Optional[Any] = LayoutLMvaImageProcessingTester(self ) @property def __magic_name__ ( self : Union[str, Any] ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def __magic_name__ ( self : Dict ): """simple docstring""" _A: str = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_lowerCAmelCase , '''do_resize''' ) ) self.assertTrue(hasattr(_lowerCAmelCase , '''size''' ) ) self.assertTrue(hasattr(_lowerCAmelCase , '''apply_ocr''' ) ) def __magic_name__ ( self : Tuple ): """simple docstring""" _A: List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 1_8, '''width''': 1_8} ) _A: Dict = self.image_processing_class.from_dict(self.image_processor_dict , size=4_2 ) self.assertEqual(image_processor.size , {'''height''': 4_2, '''width''': 4_2} ) def __magic_name__ ( self : Optional[int] ): """simple docstring""" pass def __magic_name__ ( self : int ): """simple docstring""" # Initialize image_processing _A: Any = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _A: int = prepare_image_inputs(self.image_processor_tester , equal_resolution=_lowerCAmelCase ) for image in image_inputs: self.assertIsInstance(_lowerCAmelCase , Image.Image ) # Test not batched input _A: Any = image_processing(image_inputs[0] , return_tensors='''pt''' ) self.assertEqual( encoding.pixel_values.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) self.assertIsInstance(encoding.words , _lowerCAmelCase ) self.assertIsInstance(encoding.boxes , _lowerCAmelCase ) # Test batched _A: Optional[int] = image_processing(_lowerCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def __magic_name__ ( self : List[str] ): """simple docstring""" # Initialize image_processing _A: List[str] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _A: Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=_lowerCAmelCase , numpify=_lowerCAmelCase ) for image in image_inputs: self.assertIsInstance(_lowerCAmelCase , np.ndarray ) # Test not batched input _A: str = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched _A: Any = image_processing(_lowerCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def __magic_name__ ( self : Dict ): """simple docstring""" # Initialize image_processing _A: List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _A: Dict = prepare_image_inputs(self.image_processor_tester , equal_resolution=_lowerCAmelCase , torchify=_lowerCAmelCase ) for image in image_inputs: self.assertIsInstance(_lowerCAmelCase , torch.Tensor ) # Test not batched input _A: Any = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched _A: List[str] = image_processing(_lowerCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def __magic_name__ ( self : Union[str, Any] ): """simple docstring""" # with apply_OCR = True _A: Dict = LayoutLMvaImageProcessor() from datasets import load_dataset _A: str = load_dataset('''hf-internal-testing/fixtures_docvqa''' , split='''test''' ) _A: Union[str, Any] = Image.open(ds[0]['''file'''] ).convert('''RGB''' ) _A: List[str] = image_processing(_lowerCAmelCase , return_tensors='''pt''' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 2_2_4, 2_2_4) ) self.assertEqual(len(encoding.words ) , len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 _A: List[str] = [['''11:14''', '''to''', '''11:39''', '''a.m''', '''11:39''', '''to''', '''11:44''', '''a.m.''', '''11:44''', '''a.m.''', '''to''', '''12:25''', '''p.m.''', '''12:25''', '''to''', '''12:58''', '''p.m.''', '''12:58''', '''to''', '''4:00''', '''p.m.''', '''2:00''', '''to''', '''5:00''', '''p.m.''', '''Coffee''', '''Break''', '''Coffee''', '''will''', '''be''', '''served''', '''for''', '''men''', '''and''', '''women''', '''in''', '''the''', '''lobby''', '''adjacent''', '''to''', '''exhibit''', '''area.''', '''Please''', '''move''', '''into''', '''exhibit''', '''area.''', '''(Exhibits''', '''Open)''', '''TRRF''', '''GENERAL''', '''SESSION''', '''(PART''', '''|)''', '''Presiding:''', '''Lee''', '''A.''', '''Waller''', '''TRRF''', '''Vice''', '''President''', '''“Introductory''', '''Remarks”''', '''Lee''', '''A.''', '''Waller,''', '''TRRF''', '''Vice''', '''Presi-''', '''dent''', '''Individual''', '''Interviews''', '''with''', '''TRRF''', '''Public''', '''Board''', '''Members''', '''and''', '''Sci-''', '''entific''', '''Advisory''', '''Council''', '''Mem-''', '''bers''', '''Conducted''', '''by''', '''TRRF''', '''Treasurer''', '''Philip''', '''G.''', '''Kuehn''', '''to''', '''get''', '''answers''', '''which''', '''the''', '''public''', '''refrigerated''', '''warehousing''', '''industry''', '''is''', '''looking''', '''for.''', '''Plus''', '''questions''', '''from''', '''the''', '''floor.''', '''Dr.''', '''Emil''', '''M.''', '''Mrak,''', '''University''', '''of''', '''Cal-''', '''ifornia,''', '''Chairman,''', '''TRRF''', '''Board;''', '''Sam''', '''R.''', '''Cecil,''', '''University''', '''of''', '''Georgia''', '''College''', '''of''', '''Agriculture;''', '''Dr.''', '''Stanley''', '''Charm,''', '''Tufts''', '''University''', '''School''', '''of''', '''Medicine;''', '''Dr.''', '''Robert''', '''H.''', '''Cotton,''', '''ITT''', '''Continental''', '''Baking''', '''Company;''', '''Dr.''', '''Owen''', '''Fennema,''', '''University''', '''of''', '''Wis-''', '''consin;''', '''Dr.''', '''Robert''', '''E.''', '''Hardenburg,''', '''USDA.''', '''Questions''', '''and''', '''Answers''', '''Exhibits''', '''Open''', '''Capt.''', '''Jack''', '''Stoney''', '''Room''', '''TRRF''', '''Scientific''', '''Advisory''', '''Council''', '''Meeting''', '''Ballroom''', '''Foyer''']] # noqa: E231 _A: List[str] = [[[1_4_1, 5_7, 2_1_4, 6_9], [2_2_8, 5_8, 2_5_2, 6_9], [1_4_1, 7_5, 2_1_6, 8_8], [2_3_0, 7_9, 2_8_0, 8_8], [1_4_2, 2_6_0, 2_1_8, 2_7_3], [2_3_0, 2_6_1, 2_5_5, 2_7_3], [1_4_3, 2_7_9, 2_1_8, 2_9_0], [2_3_1, 2_8_2, 2_9_0, 2_9_1], [1_4_3, 3_4_2, 2_1_8, 3_5_4], [2_3_1, 3_4_5, 2_8_9, 3_5_5], [2_0_2, 3_6_2, 2_2_7, 3_7_3], [1_4_3, 3_7_9, 2_2_0, 3_9_2], [2_3_1, 3_8_2, 2_9_1, 3_9_4], [1_4_4, 7_1_4, 2_2_0, 7_2_6], [2_3_1, 7_1_5, 2_5_6, 7_2_6], [1_4_4, 7_3_2, 2_2_0, 7_4_5], [2_3_2, 7_3_6, 2_9_1, 7_4_7], [1_4_4, 7_6_9, 2_1_8, 7_8_2], [2_3_1, 7_7_0, 2_5_6, 7_8_2], [1_4_1, 7_8_8, 2_0_2, 8_0_1], [2_1_5, 7_9_1, 2_7_4, 8_0_4], [1_4_3, 8_2_6, 2_0_4, 8_3_8], [2_1_5, 8_2_6, 2_4_0, 8_3_8], [1_4_2, 8_4_4, 2_0_2, 8_5_7], [2_1_5, 8_4_7, 2_7_4, 8_5_9], [3_3_4, 5_7, 4_2_7, 6_9], [4_4_0, 5_7, 5_2_2, 6_9], [3_6_9, 7_5, 4_6_1, 8_8], [4_6_9, 7_5, 5_1_6, 8_8], [5_2_8, 7_6, 5_6_2, 8_8], [5_7_0, 7_6, 6_6_7, 8_8], [6_7_5, 7_5, 7_1_1, 8_7], [7_2_1, 7_9, 7_7_8, 8_8], [7_8_9, 7_5, 8_4_0, 8_8], [3_6_9, 9_7, 4_7_0, 1_0_7], [4_8_4, 9_4, 5_0_7, 1_0_6], [5_1_8, 9_4, 5_6_2, 1_0_7], [5_7_6, 9_4, 6_5_5, 1_1_0], [6_6_8, 9_4, 7_9_2, 1_0_9], [8_0_4, 9_5, 8_2_9, 1_0_7], [3_6_9, 1_1_3, 4_6_5, 1_2_5], [4_7_7, 1_1_6, 5_4_7, 1_2_5], [5_6_2, 1_1_3, 6_5_8, 1_2_5], [6_7_1, 1_1_6, 7_4_8, 1_2_5], [7_6_1, 1_1_3, 8_1_1, 1_2_5], [3_6_9, 1_3_1, 4_6_5, 1_4_3], [4_7_7, 1_3_3, 5_4_8, 1_4_3], [5_6_3, 1_3_0, 6_9_8, 1_4_5], [7_1_0, 1_3_0, 8_0_2, 1_4_6], [3_3_6, 1_7_1, 4_1_2, 1_8_3], [4_2_3, 1_7_1, 5_7_2, 1_8_3], [5_8_2, 1_7_0, 7_1_6, 1_8_4], [7_2_8, 1_7_1, 8_1_7, 1_8_7], [8_2_9, 1_7_1, 8_4_4, 1_8_6], [3_3_8, 1_9_7, 4_8_2, 2_1_2], [5_0_7, 1_9_6, 5_5_7, 2_0_9], [5_6_9, 1_9_6, 5_9_5, 2_0_8], [6_1_0, 1_9_6, 7_0_2, 2_0_9], [5_0_5, 2_1_4, 5_8_3, 2_2_6], [5_9_5, 2_1_4, 6_5_6, 2_2_7], [6_7_0, 2_1_5, 8_0_7, 2_2_7], [3_3_5, 2_5_9, 5_4_3, 2_7_4], [5_5_6, 2_5_9, 7_0_8, 2_7_2], [3_7_2, 2_7_9, 4_2_2, 2_9_1], [4_3_5, 2_7_9, 4_6_0, 2_9_1], [4_7_4, 2_7_9, 5_7_4, 2_9_2], [5_8_7, 2_7_8, 6_6_4, 2_9_1], [6_7_6, 2_7_8, 7_3_8, 2_9_1], [7_5_1, 2_7_9, 8_3_4, 2_9_1], [3_7_2, 2_9_8, 4_3_4, 3_1_0], [3_3_5, 3_4_1, 4_8_3, 3_5_4], [4_9_7, 3_4_1, 6_5_5, 3_5_4], [6_6_7, 3_4_1, 7_2_8, 3_5_4], [7_4_0, 3_4_1, 8_2_5, 3_5_4], [3_3_5, 3_6_0, 4_3_0, 3_7_2], [4_4_2, 3_6_0, 5_3_4, 3_7_2], [5_4_5, 3_5_9, 6_8_7, 3_7_2], [6_9_7, 3_6_0, 7_5_4, 3_7_2], [7_6_5, 3_6_0, 8_2_3, 3_7_3], [3_3_4, 3_7_8, 4_2_8, 3_9_1], [4_4_0, 3_7_8, 5_7_7, 3_9_4], [5_9_0, 3_7_8, 7_0_5, 3_9_1], [7_2_0, 3_7_8, 8_0_1, 3_9_1], [3_3_4, 3_9_7, 4_0_0, 4_0_9], [3_7_0, 4_1_6, 5_2_9, 4_2_9], [5_4_4, 4_1_6, 5_7_6, 4_3_2], [5_8_7, 4_1_6, 6_6_5, 4_2_8], [6_7_7, 4_1_6, 8_1_4, 4_2_9], [3_7_2, 4_3_5, 4_5_2, 4_5_0], [4_6_5, 4_3_4, 4_9_5, 4_4_7], [5_1_1, 4_3_4, 6_0_0, 4_4_7], [6_1_1, 4_3_6, 6_3_7, 4_4_7], [6_4_9, 4_3_6, 6_9_4, 4_5_1], [7_0_5, 4_3_8, 8_2_4, 4_4_7], [3_6_9, 4_5_3, 4_5_2, 4_6_6], [4_6_4, 4_5_4, 5_0_9, 4_6_6], [5_2_2, 4_5_3, 6_1_1, 4_6_9], [6_2_5, 4_5_3, 7_9_2, 4_6_9], [3_7_0, 4_7_2, 5_5_6, 4_8_8], [5_7_0, 4_7_2, 6_8_4, 4_8_7], [6_9_7, 4_7_2, 7_1_8, 4_8_5], [7_3_2, 4_7_2, 8_3_5, 4_8_8], [3_6_9, 4_9_0, 4_1_1, 5_0_3], [4_2_5, 4_9_0, 4_8_4, 5_0_3], [4_9_6, 4_9_0, 6_3_5, 5_0_6], [6_4_5, 4_9_0, 7_0_7, 5_0_3], [7_1_8, 4_9_1, 7_6_1, 5_0_3], [7_7_1, 4_9_0, 8_4_0, 5_0_3], [3_3_6, 5_1_0, 3_7_4, 5_2_1], [3_8_8, 5_1_0, 4_4_7, 5_2_2], [4_6_0, 5_1_0, 4_8_9, 5_2_1], [5_0_3, 5_1_0, 5_8_0, 5_2_2], [5_9_2, 5_0_9, 7_3_6, 5_2_5], [7_4_5, 5_0_9, 7_7_0, 5_2_2], [7_8_1, 5_0_9, 8_4_0, 5_2_2], [3_3_8, 5_2_8, 4_3_4, 5_4_1], [4_4_8, 5_2_8, 5_9_6, 5_4_1], [6_0_9, 5_2_7, 6_8_7, 5_4_0], [7_0_0, 5_2_8, 7_9_2, 5_4_1], [3_3_6, 5_4_6, 3_9_7, 5_5_9], [4_0_7, 5_4_6, 4_3_1, 5_5_9], [4_4_3, 5_4_6, 5_2_5, 5_6_0], [5_3_7, 5_4_6, 6_8_0, 5_6_2], [6_8_8, 5_4_6, 7_1_4, 5_5_9], [7_2_2, 5_4_6, 8_3_7, 5_6_2], [3_3_6, 5_6_5, 4_4_9, 5_8_1], [4_6_1, 5_6_5, 4_8_5, 5_7_7], [4_9_7, 5_6_5, 6_6_5, 5_8_1], [6_8_1, 5_6_5, 7_1_8, 5_7_7], [7_3_2, 5_6_5, 8_3_7, 5_8_0], [3_3_7, 5_8_4, 4_3_8, 5_9_7], [4_5_2, 5_8_3, 5_2_1, 5_9_6], [5_3_5, 5_8_4, 6_7_7, 5_9_9], [6_9_0, 5_8_3, 7_8_7, 5_9_6], [8_0_1, 5_8_3, 8_2_5, 5_9_6], [3_3_8, 6_0_2, 4_7_8, 6_1_5], [4_9_2, 6_0_2, 5_3_0, 6_1_4], [5_4_3, 6_0_2, 6_3_8, 6_1_5], [6_5_0, 6_0_2, 6_7_6, 6_1_4], [6_8_8, 6_0_2, 7_8_8, 6_1_5], [8_0_2, 6_0_2, 8_4_3, 6_1_4], [3_3_7, 6_2_1, 5_0_2, 6_3_3], [5_1_6, 6_2_1, 6_1_5, 6_3_7], [6_2_9, 6_2_1, 7_7_4, 6_3_6], [7_8_9, 6_2_1, 8_2_7, 6_3_3], [3_3_7, 6_3_9, 4_1_8, 6_5_2], [4_3_2, 6_4_0, 5_7_1, 6_5_3], [5_8_7, 6_3_9, 7_3_1, 6_5_5], [7_4_3, 6_3_9, 7_6_9, 6_5_2], [7_8_0, 6_3_9, 8_4_1, 6_5_2], [3_3_8, 6_5_8, 4_4_0, 6_7_3], [4_5_5, 6_5_8, 4_9_1, 6_7_0], [5_0_8, 6_5_8, 6_0_2, 6_7_1], [6_1_6, 6_5_8, 6_3_8, 6_7_0], [6_5_4, 6_5_8, 8_3_5, 6_7_4], [3_3_7, 6_7_7, 4_2_9, 6_8_9], [3_3_7, 7_1_4, 4_8_2, 7_2_6], [4_9_5, 7_1_4, 5_4_8, 7_2_6], [5_6_1, 7_1_4, 6_8_3, 7_2_6], [3_3_8, 7_7_0, 4_6_1, 7_8_2], [4_7_4, 7_6_9, 5_5_4, 7_8_5], [4_8_9, 7_8_8, 5_6_2, 8_0_3], [5_7_6, 7_8_8, 6_4_3, 8_0_1], [6_5_6, 7_8_7, 7_5_1, 8_0_4], [7_6_4, 7_8_8, 8_4_4, 8_0_1], [3_3_4, 8_2_5, 4_2_1, 8_3_8], [4_3_0, 8_2_4, 5_7_4, 8_3_8], [5_8_4, 8_2_4, 7_2_3, 8_4_1], [3_3_5, 8_4_4, 4_5_0, 8_5_7], [4_6_4, 8_4_3, 5_8_3, 8_6_0], [6_2_8, 8_6_2, 7_5_5, 8_7_5], [7_6_9, 8_6_1, 8_4_8, 8_7_8]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words , _lowerCAmelCase ) self.assertListEqual(encoding.boxes , _lowerCAmelCase ) # with apply_OCR = False _A: Union[str, Any] = LayoutLMvaImageProcessor(apply_ocr=_lowerCAmelCase ) _A: List[str] = image_processing(_lowerCAmelCase , return_tensors='''pt''' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 2_2_4, 2_2_4) )
121
'''simple docstring''' def __a ( UpperCAmelCase , UpperCAmelCase ) ->float: """simple docstring""" if density <= 0: raise ValueError("""Impossible fluid density""" ) if bulk_modulus <= 0: raise ValueError("""Impossible bulk modulus""" ) return (bulk_modulus / density) ** 0.5 if __name__ == "__main__": import doctest doctest.testmod()
258
0
from __future__ import annotations from math import pi from typing import Protocol import matplotlib.pyplot as plt import numpy as np class _lowerCAmelCase ( UpperCAmelCase_ ): '''simple docstring''' def UpperCamelCase_ ( self : int , UpperCamelCase : float ): '''simple docstring''' return 0.0 def lowerCamelCase_ ( lowerCAmelCase: np.ndarray , lowerCAmelCase: int )-> tuple[int | float, int | float]: _snake_case : Optional[Any] = min([-20, np.min(fft_results[1 : samplerate // 2 - 1] )] ) _snake_case : Dict = max([20, np.max(fft_results[1 : samplerate // 2 - 1] )] ) return lowest, highest def lowerCamelCase_ ( lowerCAmelCase: FilterType , lowerCAmelCase: int )-> None: _snake_case : Dict = 5_12 _snake_case : Any = [1] + [0] * (size - 1) _snake_case : int = [filter_type.process(lowerCAmelCase ) for item in inputs] _snake_case : List[str] = [0] * (samplerate - size) # zero-padding outputs += filler _snake_case : List[str] = np.abs(np.fft.fft(lowerCAmelCase ) ) _snake_case : Dict = 20 * np.logaa(lowerCAmelCase ) # Frequencies on log scale from 24 to nyquist frequency plt.xlim(24 , samplerate / 2 - 1 ) plt.xlabel('Frequency (Hz)' ) plt.xscale('log' ) # Display within reasonable bounds _snake_case : Union[str, Any] = get_bounds(lowerCAmelCase , lowerCAmelCase ) plt.ylim(max([-80, bounds[0]] ) , min([80, bounds[1]] ) ) plt.ylabel('Gain (dB)' ) plt.plot(lowerCAmelCase ) plt.show() def lowerCamelCase_ ( lowerCAmelCase: FilterType , lowerCAmelCase: int )-> None: _snake_case : List[Any] = 5_12 _snake_case : Any = [1] + [0] * (size - 1) _snake_case : List[Any] = [filter_type.process(lowerCAmelCase ) for item in inputs] _snake_case : List[Any] = [0] * (samplerate - size) # zero-padding outputs += filler _snake_case : List[str] = np.angle(np.fft.fft(lowerCAmelCase ) ) # Frequencies on log scale from 24 to nyquist frequency plt.xlim(24 , samplerate / 2 - 1 ) plt.xlabel('Frequency (Hz)' ) plt.xscale('log' ) plt.ylim(-2 * pi , 2 * pi ) plt.ylabel('Phase shift (Radians)' ) plt.plot(np.unwrap(lowerCAmelCase , -2 * pi ) ) plt.show()
260
import argparse import torch from transformers import RemBertConfig, RemBertModel, load_tf_weights_in_rembert from transformers.utils import logging logging.set_verbosity_info() def lowerCamelCase_ ( lowerCAmelCase: List[str] , lowerCAmelCase: int , lowerCAmelCase: List[Any] )-> Dict: # Initialise PyTorch model _snake_case : Dict = RemBertConfig.from_json_file(lowerCAmelCase ) print('Building PyTorch model from configuration: {}'.format(str(lowerCAmelCase ) ) ) _snake_case : Optional[Any] = RemBertModel(lowerCAmelCase ) # Load weights from tf checkpoint load_tf_weights_in_rembert(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) # Save pytorch-model print('Save PyTorch model to {}'.format(lowerCAmelCase ) ) torch.save(model.state_dict() , lowerCAmelCase ) if __name__ == "__main__": lowerCAmelCase_ = 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( """--rembert_config_file""", default=None, type=str, required=True, help=( """The config json file corresponding to the pre-trained RemBERT 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.""" ) lowerCAmelCase_ = parser.parse_args() convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)
260
1
from itertools import zip_longest import requests from bsa import BeautifulSoup from pandas import DataFrame def __SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE : str = "laptop" ) -> DataFrame: __lowercase = F"""https://www.amazon.in/laptop/s?k={product}""" __lowercase = { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36\n (KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36', 'Accept-Language': 'en-US, en;q=0.5', } __lowercase = BeautifulSoup(requests.get(SCREAMING_SNAKE_CASE , headers=SCREAMING_SNAKE_CASE ).text ) # Initialize a Pandas dataframe with the column titles __lowercase = DataFrame( columns=[ 'Product Title', 'Product Link', 'Current Price of the product', 'Product Rating', 'MRP of the product', 'Discount', ] ) # Loop through each entry and store them in the dataframe for item, _ in zip_longest( soup.find_all( 'div' , attrs={'class': 's-result-item', 'data-component-type': 's-search-result'} , ) , soup.find_all('div' , attrs={'class': 'a-row a-size-base a-color-base'} ) , ): try: __lowercase = item.ha.text __lowercase = 'https://www.amazon.in/' + item.ha.a['href'] __lowercase = item.find('span' , attrs={'class': 'a-offscreen'} ).text try: __lowercase = item.find('span' , attrs={'class': 'a-icon-alt'} ).text except AttributeError: __lowercase = 'Not available' try: __lowercase = ( '₹' + item.find( 'span' , attrs={'class': 'a-price a-text-price'} ).text.split('₹' )[1] ) except AttributeError: __lowercase = '' try: __lowercase = float( ( ( float(product_mrp.strip('₹' ).replace(',' , '' ) ) - float(product_price.strip('₹' ).replace(',' , '' ) ) ) / float(product_mrp.strip('₹' ).replace(',' , '' ) ) ) * 100 ) except ValueError: __lowercase = float('nan' ) except AttributeError: pass __lowercase = [ product_title, product_link, product_price, product_rating, product_mrp, discount, ] __lowercase = ' ' __lowercase = ' ' data_frame.index += 1 return data_frame if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = """headphones""" get_amazon_product_data(product).to_csv(F'''Amazon Product Data for {product}.csv''')
325
from __future__ import annotations def __SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE : list[list[int]] ) -> int: # preprocessing the first row for i in range(1 , len(matrix[0] ) ): matrix[0][i] += matrix[0][i - 1] # preprocessing the first column for i in range(1 , len(SCREAMING_SNAKE_CASE ) ): matrix[i][0] += matrix[i - 1][0] # updating the path cost for current position for i in range(1 , len(SCREAMING_SNAKE_CASE ) ): for j in range(1 , len(matrix[0] ) ): matrix[i][j] += min(matrix[i - 1][j] , matrix[i][j - 1] ) return matrix[-1][-1] if __name__ == "__main__": import doctest doctest.testmod()
325
1
import argparse import torch # Step 1. clone https://github.com/microsoft/unilm # Step 2. git checkout to https://github.com/microsoft/unilm/commit/b94ec76c36f02fb2b0bf0dcb0b8554a2185173cd # Step 3. cd unilm # Step 4. ln -s $(realpath wavlm/modules.py) ./ # create simlink # import classes from unilm.wavlm.WavLM import WavLM as WavLMOrig from unilm.wavlm.WavLM import WavLMConfig as WavLMConfigOrig from transformers import WavLMConfig, WavLMModel, logging logging.set_verbosity_info() _SCREAMING_SNAKE_CASE = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE = { 'post_extract_proj': 'feature_projection.projection', 'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv', 'self_attn.k_proj': 'encoder.layers.*.attention.k_proj', 'self_attn.v_proj': 'encoder.layers.*.attention.v_proj', 'self_attn.q_proj': 'encoder.layers.*.attention.q_proj', 'self_attn.out_proj': 'encoder.layers.*.attention.out_proj', 'self_attn.grep_linear': 'encoder.layers.*.attention.gru_rel_pos_linear', 'self_attn.relative_attention_bias': 'encoder.layers.*.attention.rel_attn_embed', 'self_attn.grep_a': 'encoder.layers.*.attention.gru_rel_pos_const', 'self_attn_layer_norm': 'encoder.layers.*.layer_norm', 'fc1': 'encoder.layers.*.feed_forward.intermediate_dense', 'fc2': 'encoder.layers.*.feed_forward.output_dense', 'final_layer_norm': 'encoder.layers.*.final_layer_norm', 'encoder.layer_norm': 'encoder.layer_norm', 'w2v_model.layer_norm': 'feature_projection.layer_norm', 'quantizer.weight_proj': 'quantizer.weight_proj', 'quantizer.vars': 'quantizer.codevectors', 'project_q': 'project_q', 'final_proj': 'project_hid', 'w2v_encoder.proj': 'ctc_proj', 'mask_emb': 'masked_spec_embed', } _SCREAMING_SNAKE_CASE = [ 'ctc_proj', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', ] def snake_case ( snake_case__ :Union[str, Any] , snake_case__ :Optional[int] , snake_case__ :Dict , snake_case__ :Optional[Any] , snake_case__ :int) -> Union[str, Any]: for attribute in key.split("""."""): _A = getattr(snake_case__ , snake_case__) if weight_type is not None: _A = getattr(snake_case__ , snake_case__).shape else: _A = hf_pointer.shape assert hf_shape == value.shape, ( F'''Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be''' F''' {value.shape} for {full_name}''' ) if weight_type == "weight": _A = value elif weight_type == "weight_g": _A = value elif weight_type == "weight_v": _A = value elif weight_type == "bias": _A = value else: _A = value logger.info(F'''{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}.''') def snake_case ( snake_case__ :List[Any] , snake_case__ :Any) -> List[Any]: _A = [] _A = fairseq_model.state_dict() _A = hf_model.feature_extractor for name, value in fairseq_dict.items(): _A = False if "conv_layers" in name: load_conv_layer( snake_case__ , snake_case__ , snake_case__ , snake_case__ , hf_model.config.feat_extract_norm == """group""" , ) _A = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split("""w2v_model.""")[-1] == name.split(""".""")[0]: _A = True if "*" in mapped_key: _A = name.split(snake_case__)[0].split(""".""")[-2] _A = mapped_key.replace("""*""" , snake_case__) if "weight_g" in name: _A = """weight_g""" elif "weight_v" in name: _A = """weight_v""" elif "bias" in name and "relative_attention_bias" not in name: _A = """bias""" elif "weight" in name: # TODO: don't match quantizer.weight_proj _A = """weight""" else: _A = None set_recursively(snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__) continue if not is_used: unused_weights.append(snake_case__) logger.warning(F'''Unused weights: {unused_weights}''') def snake_case ( snake_case__ :Optional[int] , snake_case__ :Any , snake_case__ :str , snake_case__ :Any , snake_case__ :List[Any]) -> Optional[int]: _A = full_name.split("""conv_layers.""")[-1] _A = name.split(""".""") _A = int(items[0]) _A = int(items[1]) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.''' ) _A = value logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.''') elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.''' ) _A = value logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.''') elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F'''{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was''' " found." ) _A = value logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''') elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.''' ) _A = value logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''') else: unused_weights.append(snake_case__) @torch.no_grad() def snake_case ( snake_case__ :str , snake_case__ :List[Any] , snake_case__ :Union[str, Any]=None) -> Optional[Any]: # load the pre-trained checkpoints _A = torch.load(snake_case__) _A = WavLMConfigOrig(checkpoint["""cfg"""]) _A = WavLMOrig(snake_case__) model.load_state_dict(checkpoint["""model"""]) model.eval() if config_path is not None: _A = WavLMConfig.from_pretrained(snake_case__) else: _A = WavLMConfig() _A = WavLMModel(snake_case__) recursively_load_weights(snake_case__ , snake_case__) hf_wavlm.save_pretrained(snake_case__) if __name__ == "__main__": _SCREAMING_SNAKE_CASE = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') _SCREAMING_SNAKE_CASE = parser.parse_args() convert_wavlm_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
81
import argparse import json import os import re import torch from transformers import BloomConfig, BloomModel from transformers.file_utils import CONFIG_NAME, WEIGHTS_NAME from transformers.utils import logging logging.set_verbosity_info() _SCREAMING_SNAKE_CASE = [ 'word_embeddings_layernorm.weight', 'word_embeddings_layernorm.bias', 'input_layernorm.weight', 'input_layernorm.bias', 'post_attention_layernorm.weight', 'post_attention_layernorm.bias', 'self_attention.dense.bias', 'mlp.dense_4h_to_h.bias', 'ln_f.weight', 'ln_f.bias', ] _SCREAMING_SNAKE_CASE = [ 'mlp.dense_4h_to_h.weight', 'self_attention.dense.weight', ] def snake_case ( snake_case__ :List[str] , snake_case__ :Dict) -> str: _A = { """word_embeddings.weight""": """word_embeddings.weight""", """word_embeddings.norm.weight""": """word_embeddings_layernorm.weight""", """word_embeddings.norm.bias""": """word_embeddings_layernorm.bias""", """weight""": """ln_f.weight""", """bias""": """ln_f.bias""", } if key in layer_rename_map: return layer_rename_map[key] # Handle transformer blocks _A = int(re.match(R""".*layer_(\d*).*""" , snake_case__)[1]) layer_number -= 3 return F'''h.{layer_number}.''' + key def snake_case ( snake_case__ :Tuple) -> int: if dtype == torch.bool: return 1 / 8 _A = re.search(R"""[^\d](\d+)$""" , str(snake_case__)) if bit_search is None: raise ValueError(F'''`dtype` is not a valid dtype: {dtype}.''') _A = int(bit_search.groups()[0]) return bit_size // 8 def snake_case ( snake_case__ :Dict , snake_case__ :Any , snake_case__ :Union[str, Any] , snake_case__ :Tuple , snake_case__ :List[Any]) -> List[str]: # Construct model if bloom_config_file == "": _A = BloomConfig() else: _A = BloomConfig.from_json_file(snake_case__) if shard_model: _A = os.listdir(snake_case__) _A = sorted(filter(lambda snake_case__: s.startswith("""layer""") and "model_00" in s , snake_case__)) _A = {"""weight_map""": {}, """metadata""": {}} _A = 0 _A = None _A = BloomConfig() for j, file in enumerate(snake_case__): print("""Processing file: {}""".format(snake_case__)) _A = None for i in range(snake_case__): # load all TP files _A = file.replace("""model_00""" , F'''model_0{i}''') _A = torch.load(os.path.join(snake_case__ , snake_case__) , map_location="""cpu""") # Rename keys in the transformers names _A = list(temp.keys()) for key in keys: _A = temp.pop(snake_case__) if tensors is None: _A = temp else: for key in tensors.keys(): if any(key.endswith(snake_case__) for end in WEIGHTS_TO_AVERAGE_ENDSWITH): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel _A = 1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN) else 0 # We concatenate these weights accross TP ranks _A = torch.cat([tensors[key], temp[key]] , dim=snake_case__) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(snake_case__) for end in WEIGHTS_TO_AVERAGE_ENDSWITH): _A = tensors[key] / pretraining_tp torch.save( snake_case__ , os.path.join( snake_case__ , """pytorch_model_{}-of-{}.bin""".format(str(j + 1).zfill(5) , str(len(snake_case__)).zfill(5)) , ) , ) for key in tensors.keys(): _A = tensors[key] total_size += value.numel() * get_dtype_size(value.dtype) if key not in index_dict["weight_map"]: _A = """pytorch_model_{}-of-{}.bin""".format( str(j + 1).zfill(5) , str(len(snake_case__)).zfill(5)) _A = BloomConfig() _A = pytorch_dump_folder_path + """/""" + CONFIG_NAME _A = total_size with open(snake_case__ , """w""" , encoding="""utf-8""") as f: f.write(config.to_json_string()) with open(os.path.join(snake_case__ , WEIGHTS_NAME + """.index.json""") , """w""" , encoding="""utf-8""") as f: _A = json.dumps(snake_case__ , indent=2 , sort_keys=snake_case__) + """\n""" f.write(snake_case__) else: _A = BloomModel(snake_case__) _A = os.listdir(snake_case__) _A = sorted(filter(lambda snake_case__: s.startswith("""layer""") and "model_00" in s , snake_case__)) _A = None for i, file in enumerate(snake_case__): _A = None for i in range(snake_case__): # load all TP files _A = file.replace("""model_00""" , F'''model_0{i}''') _A = torch.load(os.path.join(snake_case__ , snake_case__) , map_location="""cpu""") # Rename keys in the transformers names _A = list(temp.keys()) for key in keys: _A = temp.pop(snake_case__) if tensors is None: _A = temp else: for key in tensors.keys(): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) if any(key.endswith(snake_case__) for end in WEIGHTS_TO_AVERAGE_ENDSWITH): tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel _A = 1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN) else 0 # We concatenate these weights accross TP ranks _A = torch.cat([tensors[key], temp[key]] , dim=snake_case__) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(snake_case__) for end in WEIGHTS_TO_AVERAGE_ENDSWITH): _A = tensors[key] / pretraining_tp _A = model.load_state_dict(snake_case__ , strict=snake_case__) assert not other_keys.unexpected_keys, F'''The keys {other_keys.unexpected_keys} are unexpected''' if missing_keys is None: _A = set(other_keys.missing_keys) else: _A = missing_keys.intersection(set(other_keys.missing_keys)) assert not missing_keys, F'''The keys {missing_keys} are missing''' # Save pytorch-model os.makedirs(snake_case__ , exist_ok=snake_case__) _A = pytorch_dump_folder_path + """/""" + WEIGHTS_NAME _A = pytorch_dump_folder_path + """/""" + CONFIG_NAME print(F'''Save PyTorch model to {pytorch_weights_dump_path} with dtype {config.torch_dtype}''') if config.torch_dtype is not None: _A = model.to(config.torch_dtype) torch.save(model.state_dict() , snake_case__) print(F'''Save configuration file to {pytorch_config_dump_path}''') with open(snake_case__ , """w""" , encoding="""utf-8""") as f: f.write(config.to_json_string()) if __name__ == "__main__": _SCREAMING_SNAKE_CASE = argparse.ArgumentParser() # Required parameters parser.add_argument( '--bloom_checkpoint_path', default=None, type=str, required=True, help='Path to the Megatron-LM checkpoint path.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) parser.add_argument( '--bloom_config_file', default='', type=str, help=( 'An optional config json file corresponding to the pre-trained model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--shard_model', action='store_true', help='An optional setting to shard the output model \nThis enables sharding the converted checkpoint', ) parser.add_argument( '--pretraining_tp', default=4, type=int, help='Pretraining TP rank that has been used when training the model in Megatron-LM \n', ) _SCREAMING_SNAKE_CASE = parser.parse_args() convert_bloom_checkpoint_to_pytorch( args.bloom_checkpoint_path, args.bloom_config_file, args.pytorch_dump_folder_path, args.shard_model, args.pretraining_tp, )
81
1
"""simple docstring""" import unittest from transformers import PegasusTokenizer, PegasusTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin A = get_tests_dir('''fixtures/test_sentencepiece_no_bos.model''') @require_sentencepiece @require_tokenizers class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = PegasusTokenizer __lowerCAmelCase = PegasusTokenizerFast __lowerCAmelCase = True __lowerCAmelCase = True def _lowerCamelCase ( self ): super().setUp() # We have a SentencePiece fixture for testing __a : Dict = PegasusTokenizer(_UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def _lowerCamelCase ( self ): return PegasusTokenizer.from_pretrained('''google/pegasus-large''' ) def _lowerCamelCase ( self , **_UpperCAmelCase ): return PegasusTokenizer.from_pretrained(self.tmpdirname , **_UpperCAmelCase ) def _lowerCamelCase ( self , _UpperCAmelCase ): return ("This is a test", "This is a test") def _lowerCamelCase ( self ): __a : Dict = '''</s>''' __a : Dict = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_UpperCAmelCase ) , _UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_UpperCAmelCase ) , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Union[str, Any] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<pad>''' ) self.assertEqual(vocab_keys[1] , '''</s>''' ) self.assertEqual(vocab_keys[-1] , '''v''' ) self.assertEqual(len(_UpperCAmelCase ) , 1103 ) def _lowerCamelCase ( self ): self.assertEqual(self.get_tokenizer().vocab_size , 1103 ) def _lowerCamelCase ( self ): __a : str = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) __a : Any = self.tokenizer_class.from_pretrained(self.tmpdirname ) __a : Any = ( '''Let\'s see which <unk> is the better <unk_token_11> one <mask_1> It seems like this <mask_2> was important''' ''' </s> <pad> <pad> <pad>''' ) __a : Tuple = rust_tokenizer([raw_input_str] , return_tensors=_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ).input_ids[0] __a : Optional[Any] = py_tokenizer([raw_input_str] , return_tensors=_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ).input_ids[0] self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Dict = self._large_tokenizer # <mask_1> masks whole sentence while <mask_2> masks single word __a : Tuple = '''<mask_1> To ensure a <mask_2> flow of bank resolutions.''' __a : Dict = [2, 413, 615, 114, 3, 1971, 113, 1679, 10710, 107, 1] __a : Any = tokenizer([raw_input_str] , return_tensors=_UpperCAmelCase ).input_ids[0] self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Dict = self._large_tokenizer # The tracebacks for the following asserts are **better** without messages or self.assertEqual assert tokenizer.vocab_size == 96103 assert tokenizer.pad_token_id == 0 assert tokenizer.eos_token_id == 1 assert tokenizer.offset == 103 assert tokenizer.unk_token_id == tokenizer.offset + 2 == 105 assert tokenizer.unk_token == "<unk>" assert tokenizer.model_max_length == 1024 __a : Optional[Any] = '''To ensure a smooth flow of bank resolutions.''' __a : List[Any] = [413, 615, 114, 2291, 1971, 113, 1679, 10710, 107, 1] __a : Optional[Any] = tokenizer([raw_input_str] , return_tensors=_UpperCAmelCase ).input_ids[0] self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) assert tokenizer.convert_ids_to_tokens([0, 1, 2, 3] ) == ["<pad>", "</s>", "<mask_1>", "<mask_2>"] @require_torch def _lowerCamelCase ( self ): __a : List[Any] = ['''This is going to be way too long.''' * 150, '''short example'''] __a : Any = ['''not super long but more than 5 tokens''', '''tiny'''] __a : int = self._large_tokenizer(_UpperCAmelCase , padding=_UpperCAmelCase , truncation=_UpperCAmelCase , return_tensors='''pt''' ) __a : Optional[Any] = self._large_tokenizer( text_target=_UpperCAmelCase , max_length=5 , padding=_UpperCAmelCase , truncation=_UpperCAmelCase , return_tensors='''pt''' ) assert batch.input_ids.shape == (2, 1024) assert batch.attention_mask.shape == (2, 1024) assert targets["input_ids"].shape == (2, 5) assert len(_UpperCAmelCase ) == 2 # input_ids, attention_mask. @slow def _lowerCamelCase ( self ): # fmt: off __a : Tuple = {'''input_ids''': [[38979, 143, 18485, 606, 130, 26669, 87686, 121, 54189, 1129, 111, 26669, 87686, 121, 9114, 14787, 121, 13249, 158, 592, 956, 121, 14621, 31576, 143, 62613, 108, 9688, 930, 43430, 11562, 62613, 304, 108, 11443, 897, 108, 9314, 17415, 63399, 108, 11443, 7614, 18316, 118, 4284, 7148, 12430, 143, 1400, 25703, 158, 111, 4284, 7148, 11772, 143, 21297, 1064, 158, 122, 204, 3506, 1754, 1133, 14787, 1581, 115, 33224, 4482, 111, 1355, 110, 29173, 317, 50833, 108, 20147, 94665, 111, 77198, 107, 1], [110, 62613, 117, 638, 112, 1133, 121, 20098, 1355, 79050, 13872, 135, 1596, 53541, 1352, 141, 13039, 5542, 124, 302, 518, 111, 268, 2956, 115, 149, 4427, 107, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [139, 1235, 2799, 18289, 17780, 204, 109, 9474, 1296, 107, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_UpperCAmelCase , model_name='''google/bigbird-pegasus-large-arxiv''' , revision='''ba85d0851d708441f91440d509690f1ab6353415''' , ) @require_sentencepiece @require_tokenizers class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = PegasusTokenizer __lowerCAmelCase = PegasusTokenizerFast __lowerCAmelCase = True __lowerCAmelCase = True def _lowerCamelCase ( self ): super().setUp() # We have a SentencePiece fixture for testing __a : Optional[Any] = PegasusTokenizer(_UpperCAmelCase , offset=0 , mask_token_sent=_UpperCAmelCase , mask_token='''[MASK]''' ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def _lowerCamelCase ( self ): return PegasusTokenizer.from_pretrained('''google/bigbird-pegasus-large-arxiv''' ) def _lowerCamelCase ( self , **_UpperCAmelCase ): return PegasusTokenizer.from_pretrained(self.tmpdirname , **_UpperCAmelCase ) def _lowerCamelCase ( self , _UpperCAmelCase ): return ("This is a test", "This is a test") def _lowerCamelCase ( self ): __a : int = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) __a : List[Any] = self.tokenizer_class.from_pretrained(self.tmpdirname ) __a : List[str] = ( '''Let\'s see which <unk> is the better <unk_token> one [MASK] It seems like this [MASK] was important </s>''' ''' <pad> <pad> <pad>''' ) __a : int = rust_tokenizer([raw_input_str] , return_tensors=_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ).input_ids[0] __a : Union[str, Any] = py_tokenizer([raw_input_str] , return_tensors=_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ).input_ids[0] self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) @require_torch def _lowerCamelCase ( self ): __a : int = ['''This is going to be way too long.''' * 1000, '''short example'''] __a : str = ['''not super long but more than 5 tokens''', '''tiny'''] __a : str = self._large_tokenizer(_UpperCAmelCase , padding=_UpperCAmelCase , truncation=_UpperCAmelCase , return_tensors='''pt''' ) __a : Union[str, Any] = self._large_tokenizer( text_target=_UpperCAmelCase , max_length=5 , padding=_UpperCAmelCase , truncation=_UpperCAmelCase , return_tensors='''pt''' ) assert batch.input_ids.shape == (2, 4096) assert batch.attention_mask.shape == (2, 4096) assert targets["input_ids"].shape == (2, 5) assert len(_UpperCAmelCase ) == 2 # input_ids, attention_mask. def _lowerCamelCase ( self ): __a : Optional[int] = ( '''This is an example string that is used to test the original TF implementation against the HF''' ''' implementation''' ) __a : List[str] = self._large_tokenizer(_UpperCAmelCase ).input_ids self.assertListEqual( _UpperCAmelCase , [182, 117, 142, 587, 4211, 120, 117, 263, 112, 804, 109, 856, 25016, 3137, 464, 109, 26955, 3137, 1] , )
160
"""simple docstring""" import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roberta import RobertaTokenizer A = logging.get_logger(__name__) A = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} A = { '''vocab_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/vocab.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/vocab.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/vocab.json''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json''', '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json''' ), }, '''merges_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/merges.txt''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/merges.txt''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/merges.txt''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt''', '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt''' ), }, '''tokenizer_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/tokenizer.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/tokenizer.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json''', '''roberta-base-openai-detector''': ( '''https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json''' ), '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json''' ), }, } A = { '''roberta-base''': 512, '''roberta-large''': 512, '''roberta-large-mnli''': 512, '''distilroberta-base''': 512, '''roberta-base-openai-detector''': 512, '''roberta-large-openai-detector''': 512, } class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = VOCAB_FILES_NAMES __lowerCAmelCase = PRETRAINED_VOCAB_FILES_MAP __lowerCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCAmelCase = ['''input_ids''', '''attention_mask'''] __lowerCAmelCase = RobertaTokenizer def __init__( self , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase="replace" , _UpperCAmelCase="<s>" , _UpperCAmelCase="</s>" , _UpperCAmelCase="</s>" , _UpperCAmelCase="<s>" , _UpperCAmelCase="<unk>" , _UpperCAmelCase="<pad>" , _UpperCAmelCase="<mask>" , _UpperCAmelCase=False , _UpperCAmelCase=True , **_UpperCAmelCase , ): super().__init__( _UpperCAmelCase , _UpperCAmelCase , tokenizer_file=_UpperCAmelCase , errors=_UpperCAmelCase , bos_token=_UpperCAmelCase , eos_token=_UpperCAmelCase , sep_token=_UpperCAmelCase , cls_token=_UpperCAmelCase , unk_token=_UpperCAmelCase , pad_token=_UpperCAmelCase , mask_token=_UpperCAmelCase , add_prefix_space=_UpperCAmelCase , trim_offsets=_UpperCAmelCase , **_UpperCAmelCase , ) __a : str = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , _UpperCAmelCase ) != add_prefix_space: __a : Tuple = getattr(_UpperCAmelCase , pre_tok_state.pop('''type''' ) ) __a : Dict = add_prefix_space __a : str = pre_tok_class(**_UpperCAmelCase ) __a : Optional[int] = add_prefix_space __a : str = '''post_processor''' __a : int = getattr(self.backend_tokenizer , _UpperCAmelCase , _UpperCAmelCase ) if tokenizer_component_instance: __a : Dict = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: __a : Optional[Any] = tuple(state['''sep'''] ) if "cls" in state: __a : List[Any] = tuple(state['''cls'''] ) __a : Optional[Any] = False if state.get('''add_prefix_space''' , _UpperCAmelCase ) != add_prefix_space: __a : Any = add_prefix_space __a : List[Any] = True if state.get('''trim_offsets''' , _UpperCAmelCase ) != trim_offsets: __a : List[Any] = trim_offsets __a : List[str] = True if changes_to_apply: __a : Any = getattr(_UpperCAmelCase , state.pop('''type''' ) ) __a : Any = component_class(**_UpperCAmelCase ) setattr(self.backend_tokenizer , _UpperCAmelCase , _UpperCAmelCase ) @property def _lowerCamelCase ( self ): if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def _lowerCamelCase ( self , _UpperCAmelCase ): __a : int = AddedToken(_UpperCAmelCase , lstrip=_UpperCAmelCase , rstrip=_UpperCAmelCase ) if isinstance(_UpperCAmelCase , _UpperCAmelCase ) else value __a : Tuple = value def _lowerCamelCase ( self , *_UpperCAmelCase , **_UpperCAmelCase ): __a : Tuple = kwargs.get('''is_split_into_words''' , _UpperCAmelCase ) assert self.add_prefix_space or not is_split_into_words, ( f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*_UpperCAmelCase , **_UpperCAmelCase ) def _lowerCamelCase ( self , *_UpperCAmelCase , **_UpperCAmelCase ): __a : str = kwargs.get('''is_split_into_words''' , _UpperCAmelCase ) assert self.add_prefix_space or not is_split_into_words, ( f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ "to use it with pretokenized inputs." ) return super()._encode_plus(*_UpperCAmelCase , **_UpperCAmelCase ) def _lowerCamelCase ( self , _UpperCAmelCase , _UpperCAmelCase = None ): __a : Optional[int] = self._tokenizer.model.save(_UpperCAmelCase , name=_UpperCAmelCase ) return tuple(_UpperCAmelCase ) def _lowerCamelCase ( self , _UpperCAmelCase , _UpperCAmelCase=None ): __a : int = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def _lowerCamelCase ( self , _UpperCAmelCase , _UpperCAmelCase = None ): __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]
160
1
from transformers import HfArgumentParser, TensorFlowBenchmark, TensorFlowBenchmarkArguments def A__ ( ) -> Any: __snake_case: Union[str, Any] = HfArgumentParser(SCREAMING_SNAKE_CASE__) __snake_case: int = parser.parse_args_into_dataclasses()[0] __snake_case: List[Any] = TensorFlowBenchmark(args=SCREAMING_SNAKE_CASE__) try: __snake_case: int = parser.parse_args_into_dataclasses()[0] except ValueError as e: __snake_case: str = """Arg --no_{0} is no longer used, please use --no-{0} instead.""" __snake_case: int = """ """.join(str(SCREAMING_SNAKE_CASE__).split(""" """)[:-1]) __snake_case: List[Any] = """""" __snake_case: Union[str, Any] = eval(str(SCREAMING_SNAKE_CASE__).split(""" """)[-1]) __snake_case: Any = [] for arg in depreciated_args: # arg[2:] removes '--' if arg[2:] in TensorFlowBenchmark.deprecated_args: # arg[5:] removes '--no_' full_error_msg += arg_error_msg.format(arg[5:]) else: wrong_args.append(SCREAMING_SNAKE_CASE__) if len(SCREAMING_SNAKE_CASE__) > 0: __snake_case: Any = full_error_msg + begin_error_msg + str(SCREAMING_SNAKE_CASE__) raise ValueError(SCREAMING_SNAKE_CASE__) benchmark.run() if __name__ == "__main__": main()
293
import copy import tempfile import unittest from huggingface_hub import HfFolder, delete_repo from parameterized import parameterized from requests.exceptions import HTTPError from transformers import AutoConfig, GenerationConfig from transformers.testing_utils import TOKEN, USER, is_staging_test class __snake_case ( unittest.TestCase ): '''simple docstring''' @parameterized.expand([(None,), ("""foo.json""",)] ) def UpperCAmelCase__ ( self : List[str] , A : Optional[Any] ): __snake_case: Any = GenerationConfig( do_sample=A , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(A , config_name=A ) __snake_case: Optional[int] = GenerationConfig.from_pretrained(A , config_name=A ) # Checks parameters that were specified self.assertEqual(loaded_config.do_sample , A ) self.assertEqual(loaded_config.temperature , 0.7 ) self.assertEqual(loaded_config.length_penalty , 1.0 ) self.assertEqual(loaded_config.bad_words_ids , [[1, 2, 3], [4, 5]] ) # Checks parameters that were not specified (defaults) self.assertEqual(loaded_config.top_k , 50 ) self.assertEqual(loaded_config.max_length , 20 ) self.assertEqual(loaded_config.max_time , A ) def UpperCAmelCase__ ( self : Dict ): __snake_case: str = AutoConfig.from_pretrained("""gpt2""" ) __snake_case: Any = GenerationConfig.from_model_config(A ) __snake_case: str = GenerationConfig() # The generation config has loaded a few non-default parameters from the model config self.assertNotEqual(A , A ) # One of those parameters is eos_token_id -- check if it matches self.assertNotEqual(generation_config_from_model.eos_token_id , default_generation_config.eos_token_id ) self.assertEqual(generation_config_from_model.eos_token_id , model_config.eos_token_id ) def UpperCAmelCase__ ( self : str ): __snake_case: List[str] = GenerationConfig() __snake_case: Tuple = { """max_new_tokens""": 1_024, """foo""": """bar""", } __snake_case: List[str] = copy.deepcopy(A ) __snake_case: Optional[int] = generation_config.update(**A ) # update_kwargs was not modified (no side effects) self.assertEqual(A , A ) # update_kwargs was used to update the config on valid attributes self.assertEqual(generation_config.max_new_tokens , 1_024 ) # `.update()` returns a dictionary of unused kwargs self.assertEqual(A , {"""foo""": """bar"""} ) def UpperCAmelCase__ ( self : Tuple ): __snake_case: List[str] = GenerationConfig() __snake_case: Optional[int] = """bar""" with tempfile.TemporaryDirectory("""test-generation-config""" ) as tmp_dir: generation_config.save_pretrained(A ) __snake_case: Any = GenerationConfig.from_pretrained(A ) # update_kwargs was used to update the config on valid attributes self.assertEqual(new_config.foo , """bar""" ) __snake_case: int = GenerationConfig.from_model_config(A ) assert not hasattr(A , """foo""" ) # no new kwargs should be initialized if from config def UpperCAmelCase__ ( self : Dict ): __snake_case: Dict = GenerationConfig() self.assertEqual(default_config.temperature , 1.0 ) self.assertEqual(default_config.do_sample , A ) self.assertEqual(default_config.num_beams , 1 ) __snake_case: Union[str, Any] = GenerationConfig( do_sample=A , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) self.assertEqual(config.temperature , 0.7 ) self.assertEqual(config.do_sample , A ) self.assertEqual(config.num_beams , 1 ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(A ) __snake_case: Tuple = GenerationConfig.from_pretrained(A , temperature=1.0 ) self.assertEqual(loaded_config.temperature , 1.0 ) self.assertEqual(loaded_config.do_sample , A ) self.assertEqual(loaded_config.num_beams , 1 ) # default value @is_staging_test class __snake_case ( unittest.TestCase ): '''simple docstring''' @classmethod def UpperCAmelCase__ ( cls : List[str] ): __snake_case: Optional[int] = TOKEN HfFolder.save_token(A ) @classmethod def UpperCAmelCase__ ( cls : List[Any] ): try: delete_repo(token=cls._token , repo_id="""test-generation-config""" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="""valid_org/test-generation-config-org""" ) except HTTPError: pass def UpperCAmelCase__ ( self : Tuple ): __snake_case: Optional[int] = GenerationConfig( do_sample=A , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub("""test-generation-config""" , use_auth_token=self._token ) __snake_case: str = GenerationConfig.from_pretrained(f'''{USER}/test-generation-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(A , getattr(A , A ) ) # Reset repo delete_repo(token=self._token , repo_id="""test-generation-config""" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( A , repo_id="""test-generation-config""" , push_to_hub=A , use_auth_token=self._token ) __snake_case: Optional[Any] = GenerationConfig.from_pretrained(f'''{USER}/test-generation-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(A , getattr(A , A ) ) def UpperCAmelCase__ ( self : List[Any] ): __snake_case: Union[str, Any] = GenerationConfig( do_sample=A , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub("""valid_org/test-generation-config-org""" , use_auth_token=self._token ) __snake_case: int = GenerationConfig.from_pretrained("""valid_org/test-generation-config-org""" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(A , getattr(A , A ) ) # Reset repo delete_repo(token=self._token , repo_id="""valid_org/test-generation-config-org""" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( A , repo_id="""valid_org/test-generation-config-org""" , push_to_hub=A , use_auth_token=self._token ) __snake_case: Optional[int] = GenerationConfig.from_pretrained("""valid_org/test-generation-config-org""" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(A , getattr(A , A ) )
293
1
'''simple docstring''' from typing import List, Optional import numpy as np from ...processing_utils import ProcessorMixin from ...utils import to_numpy class a__ ( UpperCAmelCase__ ): lowerCamelCase : Dict ="EncodecFeatureExtractor" lowerCamelCase : List[str] =("T5Tokenizer", "T5TokenizerFast") def __init__( self : Dict , a : Optional[int] , a : Optional[int] ): """simple docstring""" super().__init__(a , a ) __lowerCamelCase = self.feature_extractor __lowerCamelCase = False def SCREAMING_SNAKE_CASE__ ( self : int , a : Optional[Any]=None , a : List[str]=None , a : Tuple=True ): """simple docstring""" return self.tokenizer.get_decoder_prompt_ids(task=a , language=a , no_timestamps=a ) def __call__( self : int , *a : List[str] , **a : List[Any] ): """simple docstring""" if self._in_target_context_manager: return self.current_processor(*a , **a ) __lowerCamelCase = kwargs.pop('''audio''' , a ) __lowerCamelCase = kwargs.pop('''sampling_rate''' , a ) __lowerCamelCase = kwargs.pop('''text''' , a ) if len(a ) > 0: __lowerCamelCase = args[0] __lowerCamelCase = 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 text is not None: __lowerCamelCase = self.tokenizer(a , **a ) if audio is not None: __lowerCamelCase = self.feature_extractor(a , *a , sampling_rate=a , **a ) if audio is None: return inputs elif text is None: return audio_inputs else: __lowerCamelCase = audio_inputs['''input_values'''] if "padding_mask" in audio_inputs: __lowerCamelCase = audio_inputs['''padding_mask'''] return inputs def SCREAMING_SNAKE_CASE__ ( self : Tuple , *a : Any , **a : str ): """simple docstring""" __lowerCamelCase = kwargs.pop('''audio''' , a ) __lowerCamelCase = kwargs.pop('''padding_mask''' , a ) if len(a ) > 0: __lowerCamelCase = args[0] __lowerCamelCase = args[1:] if audio_values is not None: return self._decode_audio(a , padding_mask=a ) else: return self.tokenizer.batch_decode(*a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , *a : Dict , **a : List[Any] ): """simple docstring""" return self.tokenizer.decode(*a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Dict , a : Any , a : Optional = None ): """simple docstring""" __lowerCamelCase = to_numpy(a ) __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = audio_values.shape if padding_mask is None: return list(a ) __lowerCamelCase = to_numpy(a ) # match the sequence length of the padding mask to the generated audio arrays by padding with the **non-padding** # token (so that the generated audio values are **not** treated as padded tokens) __lowerCamelCase = seq_len - padding_mask.shape[-1] __lowerCamelCase = 1 - self.feature_extractor.padding_value __lowerCamelCase = np.pad(a , ((0, 0), (0, difference)) , '''constant''' , constant_values=a ) __lowerCamelCase = audio_values.tolist() for i in range(a ): __lowerCamelCase = np.asarray(audio_values[i] )[ padding_mask[i][None, :] != self.feature_extractor.padding_value ] __lowerCamelCase = sliced_audio.reshape(a , -1 ) return audio_values
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> int: while second != 0: __lowerCamelCase = first & second first ^= second __lowerCamelCase = c << 1 return first if __name__ == "__main__": import doctest doctest.testmod() __UpperCAmelCase =int(input("Enter the first number: ").strip()) __UpperCAmelCase =int(input("Enter the second number: ").strip()) print(f'{add(first, second) = }')
67
1
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowercase__ : Optional[Any] = logging.get_logger(__name__) lowercase__ : List[Any] = {'''vocab_file''': '''sentencepiece.bpe.model'''} lowercase__ : Optional[int] = { '''vocab_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model''', } } lowercase__ : Union[str, Any] = { '''camembert-base''': 5_12, } lowercase__ : Tuple = '''▁''' class SCREAMING_SNAKE_CASE (A_ ): lowerCAmelCase = VOCAB_FILES_NAMES lowerCAmelCase = PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase = ['''input_ids''', '''attention_mask'''] def __init__( self , _UpperCAmelCase , _UpperCAmelCase="<s>" , _UpperCAmelCase="</s>" , _UpperCAmelCase="</s>" , _UpperCAmelCase="<s>" , _UpperCAmelCase="<unk>" , _UpperCAmelCase="<pad>" , _UpperCAmelCase="<mask>" , _UpperCAmelCase=["<s>NOTUSED", "</s>NOTUSED"] , _UpperCAmelCase = None , **_UpperCAmelCase , ): '''simple docstring''' __A : Optional[Any] = AddedToken(_lowerCamelCase , lstrip=_lowerCamelCase , rstrip=_lowerCamelCase) if isinstance(_lowerCamelCase , _lowerCamelCase) else mask_token __A : Optional[Any] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_lowerCamelCase , eos_token=_lowerCamelCase , unk_token=_lowerCamelCase , sep_token=_lowerCamelCase , cls_token=_lowerCamelCase , pad_token=_lowerCamelCase , mask_token=_lowerCamelCase , additional_special_tokens=_lowerCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_lowerCamelCase , ) __A : str = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(str(_lowerCamelCase)) __A : Any = vocab_file # HACK: These tokens were added by fairseq but don't seem to be actually used when duplicated in the actual # sentencepiece vocabulary (this is the case for <s> and </s> __A : List[Any] = {'<s>NOTUSED': 0, '<pad>': 1, '</s>NOTUSED': 2, '<unk>': 3} __A : Optional[Any] = len(self.fairseq_tokens_to_ids) __A : Tuple = len(self.sp_model) + len(self.fairseq_tokens_to_ids) __A : Optional[Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase , _UpperCAmelCase = None): '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __A : Optional[Any] = [self.cls_token_id] __A : Union[str, Any] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase , _UpperCAmelCase = None , _UpperCAmelCase = False): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_lowerCamelCase , token_ids_a=_lowerCamelCase , already_has_special_tokens=_lowerCamelCase) if token_ids_a is None: return [1] + ([0] * len(_lowerCamelCase)) + [1] return [1] + ([0] * len(_lowerCamelCase)) + [1, 1] + ([0] * len(_lowerCamelCase)) + [1] def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase , _UpperCAmelCase = None): '''simple docstring''' __A : List[Any] = [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] @property def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' return len(self.fairseq_tokens_to_ids) + len(self.sp_model) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Dict = {self.convert_ids_to_tokens(_lowerCamelCase): i for i in range(self.vocab_size)} vocab.update(self.added_tokens_encoder) return vocab def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase): '''simple docstring''' return self.sp_model.encode(_lowerCamelCase , out_type=_lowerCamelCase) def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase): '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] elif self.sp_model.PieceToId(_lowerCamelCase) == 0: # Convert sentence piece unk token to fairseq unk token index return self.unk_token_id return self.fairseq_offset + self.sp_model.PieceToId(_lowerCamelCase) def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase): '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset) def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase): '''simple docstring''' __A : List[Any] = [] __A : List[str] = '' __A : List[Any] = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(_lowerCamelCase) + token __A : Dict = True __A : str = [] else: current_sub_tokens.append(_lowerCamelCase) __A : str = False out_string += self.sp_model.decode(_lowerCamelCase) return out_string.strip() def __getstate__( self): '''simple docstring''' __A : Union[str, Any] = self.__dict__.copy() __A : Optional[Any] = None return state def __setstate__( self , _UpperCAmelCase): '''simple docstring''' __A : str = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs'): __A : Any = {} __A : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(self.vocab_file) def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase , _UpperCAmelCase = None): '''simple docstring''' if not os.path.isdir(_lowerCamelCase): logger.error(F'Vocabulary path ({save_directory}) should be a directory') return __A : Dict = os.path.join( _lowerCamelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']) if os.path.abspath(self.vocab_file) != os.path.abspath(_lowerCamelCase) and os.path.isfile(self.vocab_file): copyfile(self.vocab_file , _lowerCamelCase) elif not os.path.isfile(self.vocab_file): with open(_lowerCamelCase , 'wb') as fi: __A : Union[str, Any] = self.sp_model.serialized_model_proto() fi.write(_lowerCamelCase) return (out_vocab_file,)
368
'''simple docstring''' import os import shutil import sys import tempfile import unittest from pathlib import Path import pytest import transformers from transformers import ( BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoTokenizer, BertConfig, BertTokenizer, BertTokenizerFast, CTRLTokenizer, GPTaTokenizer, GPTaTokenizerFast, PreTrainedTokenizerFast, RobertaTokenizer, RobertaTokenizerFast, is_tokenizers_available, ) from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig from transformers.models.auto.tokenization_auto import ( TOKENIZER_MAPPING, get_tokenizer_config, tokenizer_class_from_name, ) from transformers.models.roberta.configuration_roberta import RobertaConfig from transformers.testing_utils import ( DUMMY_DIFF_TOKENIZER_IDENTIFIER, DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tokenizers, slow, ) sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils''')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 if is_tokenizers_available(): from test_module.custom_tokenization_fast import CustomTokenizerFast class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Dict = 0 @slow def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' for model_name in (x for x in BERT_PRETRAINED_CONFIG_ARCHIVE_MAP.keys() if "japanese" not in x): __A : List[str] = AutoTokenizer.from_pretrained(_UpperCAmelCase) self.assertIsNotNone(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , (BertTokenizer, BertTokenizerFast)) self.assertGreater(len(_UpperCAmelCase) , 0) for model_name in GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP.keys(): __A : Any = AutoTokenizer.from_pretrained(_UpperCAmelCase) self.assertIsNotNone(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , (GPTaTokenizer, GPTaTokenizerFast)) self.assertGreater(len(_UpperCAmelCase) , 0) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Optional[int] = AutoTokenizer.from_pretrained(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , (BertTokenizer, BertTokenizerFast)) self.assertEqual(tokenizer.vocab_size , 12) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Optional[Any] = AutoTokenizer.from_pretrained(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , (RobertaTokenizer, RobertaTokenizerFast)) self.assertEqual(tokenizer.vocab_size , 20) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Tuple = AutoConfig.from_pretrained(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) # Check that tokenizer_type ≠ model_type __A : Optional[Any] = AutoTokenizer.from_pretrained(_UpperCAmelCase , config=_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , (BertTokenizer, BertTokenizerFast)) self.assertEqual(tokenizer.vocab_size , 12) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('./tests/fixtures/vocab.txt' , os.path.join(_UpperCAmelCase , 'vocab.txt')) __A : Union[str, Any] = AutoTokenizer.from_pretrained(_UpperCAmelCase , tokenizer_type='bert' , use_fast=_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('./tests/fixtures/vocab.json' , os.path.join(_UpperCAmelCase , 'vocab.json')) shutil.copy('./tests/fixtures/merges.txt' , os.path.join(_UpperCAmelCase , 'merges.txt')) __A : str = AutoTokenizer.from_pretrained(_UpperCAmelCase , tokenizer_type='gpt2' , use_fast=_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) @require_tokenizers def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('./tests/fixtures/vocab.txt' , os.path.join(_UpperCAmelCase , 'vocab.txt')) __A : List[str] = AutoTokenizer.from_pretrained(_UpperCAmelCase , tokenizer_type='bert') self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('./tests/fixtures/vocab.json' , os.path.join(_UpperCAmelCase , 'vocab.json')) shutil.copy('./tests/fixtures/merges.txt' , os.path.join(_UpperCAmelCase , 'merges.txt')) __A : List[str] = AutoTokenizer.from_pretrained(_UpperCAmelCase , tokenizer_type='gpt2') self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' with pytest.raises(_UpperCAmelCase): AutoTokenizer.from_pretrained('./' , tokenizer_type='xxx') @require_tokenizers def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: __A : List[Any] = tokenizer_class.from_pretrained('wietsedv/bert-base-dutch-cased') self.assertIsInstance(_UpperCAmelCase , (BertTokenizer, BertTokenizerFast)) if isinstance(_UpperCAmelCase , _UpperCAmelCase): self.assertEqual(tokenizer.basic_tokenizer.do_lower_case , _UpperCAmelCase) else: self.assertEqual(tokenizer.do_lower_case , _UpperCAmelCase) self.assertEqual(tokenizer.model_max_length , 512) @require_tokenizers def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: with self.assertRaisesRegex( _UpperCAmelCase , 'julien-c/herlolip-not-exists is not a local folder and is not a valid model identifier' , ): __A : str = tokenizer_class.from_pretrained('julien-c/herlolip-not-exists') def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Any = TOKENIZER_MAPPING.values() __A : Union[str, Any] = [] for slow_tok, fast_tok in tokenizers: if slow_tok is not None: tokenizer_names.append(slow_tok.__name__) if fast_tok is not None: tokenizer_names.append(fast_tok.__name__) for tokenizer_name in tokenizer_names: # must find the right class tokenizer_class_from_name(_UpperCAmelCase) @require_tokenizers def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' self.assertIsInstance(AutoTokenizer.from_pretrained('bert-base-cased' , use_fast=_UpperCAmelCase) , _UpperCAmelCase) self.assertIsInstance(AutoTokenizer.from_pretrained('bert-base-cased') , _UpperCAmelCase) @require_tokenizers def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : List[str] = AutoTokenizer.from_pretrained('distilbert-base-uncased' , do_lower_case=_UpperCAmelCase) __A : str = 'Hello, world. How are you?' __A : List[str] = tokenizer.tokenize(_UpperCAmelCase) self.assertEqual('[UNK]' , tokens[0]) __A : Dict = AutoTokenizer.from_pretrained('microsoft/mpnet-base' , do_lower_case=_UpperCAmelCase) __A : List[Any] = tokenizer.tokenize(_UpperCAmelCase) self.assertEqual('[UNK]' , tokens[0]) @require_tokenizers def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Union[str, Any] = AutoTokenizer.from_pretrained('robot-test/dummy-tokenizer-fast-with-model-config') self.assertEqual(type(_UpperCAmelCase) , _UpperCAmelCase) self.assertEqual(tokenizer.model_max_length , 512) self.assertEqual(tokenizer.vocab_size , 3_0000) self.assertEqual(tokenizer.unk_token , '[UNK]') self.assertEqual(tokenizer.padding_side , 'right') self.assertEqual(tokenizer.truncation_side , 'right') def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Optional[Any] = AutoTokenizer.from_pretrained(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , (BertTokenizer, BertTokenizerFast)) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_UpperCAmelCase) __A : Any = AutoTokenizer.from_pretrained(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , tokenizer.__class__) self.assertEqual(tokenizera.vocab_size , 12) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Dict = AutoTokenizer.from_pretrained('ctrl') # There is no fast CTRL so this always gives us a slow tokenizer. self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : List[Any] = get_tokenizer_config('bert-base-cased') __A : Optional[int] = config.pop('_commit_hash' , _UpperCAmelCase) # If we ever update bert-base-cased tokenizer config, this dict here will need to be updated. self.assertEqual(_UpperCAmelCase , {'do_lower_case': False}) # This model does not have a tokenizer_config so we get back an empty dict. __A : Dict = get_tokenizer_config(_UpperCAmelCase) self.assertDictEqual(_UpperCAmelCase , {}) # A tokenizer saved with `save_pretrained` always creates a tokenizer config. __A : Any = AutoTokenizer.from_pretrained(_UpperCAmelCase) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_UpperCAmelCase) __A : Any = get_tokenizer_config(_UpperCAmelCase) # Check the class of the tokenizer was properly saved (note that it always saves the slow class). self.assertEqual(config['tokenizer_class'] , 'BertTokenizer') def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' try: AutoConfig.register('custom' , _UpperCAmelCase) AutoTokenizer.register(_UpperCAmelCase , slow_tokenizer_class=_UpperCAmelCase) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_UpperCAmelCase): AutoTokenizer.register(_UpperCAmelCase , slow_tokenizer_class=_UpperCAmelCase) __A : Optional[Any] = CustomTokenizer.from_pretrained(_UpperCAmelCase) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_UpperCAmelCase) __A : int = AutoTokenizer.from_pretrained(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] @require_tokenizers def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' try: AutoConfig.register('custom' , _UpperCAmelCase) # Can register in two steps AutoTokenizer.register(_UpperCAmelCase , slow_tokenizer_class=_UpperCAmelCase) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, None)) AutoTokenizer.register(_UpperCAmelCase , fast_tokenizer_class=_UpperCAmelCase) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, CustomTokenizerFast)) del TOKENIZER_MAPPING._extra_content[CustomConfig] # Can register in one step AutoTokenizer.register( _UpperCAmelCase , slow_tokenizer_class=_UpperCAmelCase , fast_tokenizer_class=_UpperCAmelCase) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, CustomTokenizerFast)) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_UpperCAmelCase): AutoTokenizer.register(_UpperCAmelCase , fast_tokenizer_class=_UpperCAmelCase) # We pass through a bert tokenizer fast cause there is no converter slow to fast for our new toknizer # and that model does not have a tokenizer.json with tempfile.TemporaryDirectory() as tmp_dir: __A : Optional[int] = BertTokenizerFast.from_pretrained(_UpperCAmelCase) bert_tokenizer.save_pretrained(_UpperCAmelCase) __A : Dict = CustomTokenizerFast.from_pretrained(_UpperCAmelCase) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_UpperCAmelCase) __A : Union[str, Any] = AutoTokenizer.from_pretrained(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) __A : Any = AutoTokenizer.from_pretrained(_UpperCAmelCase , use_fast=_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' with self.assertRaises(_UpperCAmelCase): __A : List[str] = AutoTokenizer.from_pretrained('hf-internal-testing/test_dynamic_tokenizer') # If remote code is disabled, we can't load this config. with self.assertRaises(_UpperCAmelCase): __A : Dict = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=_UpperCAmelCase) __A : str = AutoTokenizer.from_pretrained('hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=_UpperCAmelCase) self.assertTrue(tokenizer.special_attribute_present) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_UpperCAmelCase) __A : Dict = AutoTokenizer.from_pretrained(_UpperCAmelCase , trust_remote_code=_UpperCAmelCase) self.assertTrue(reloaded_tokenizer.special_attribute_present) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizerFast') self.assertEqual(reloaded_tokenizer.__class__.__name__ , 'NewTokenizerFast') # Test we can also load the slow version __A : Union[str, Any] = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=_UpperCAmelCase , use_fast=_UpperCAmelCase) self.assertTrue(tokenizer.special_attribute_present) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer') # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_UpperCAmelCase) __A : Union[str, Any] = AutoTokenizer.from_pretrained(_UpperCAmelCase , trust_remote_code=_UpperCAmelCase , use_fast=_UpperCAmelCase) self.assertEqual(reloaded_tokenizer.__class__.__name__ , 'NewTokenizer') self.assertTrue(reloaded_tokenizer.special_attribute_present) else: self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer') self.assertEqual(reloaded_tokenizer.__class__.__name__ , 'NewTokenizer') @require_tokenizers def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' class SCREAMING_SNAKE_CASE (a__ ): lowerCAmelCase = False class SCREAMING_SNAKE_CASE (a__ ): lowerCAmelCase = NewTokenizer lowerCAmelCase = False try: AutoConfig.register('custom' , _UpperCAmelCase) AutoTokenizer.register(_UpperCAmelCase , slow_tokenizer_class=_UpperCAmelCase) AutoTokenizer.register(_UpperCAmelCase , fast_tokenizer_class=_UpperCAmelCase) # If remote code is not set, the default is to use local __A : List[Any] = AutoTokenizer.from_pretrained('hf-internal-testing/test_dynamic_tokenizer') self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizerFast') self.assertFalse(tokenizer.special_attribute_present) __A : Dict = AutoTokenizer.from_pretrained('hf-internal-testing/test_dynamic_tokenizer' , use_fast=_UpperCAmelCase) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer') self.assertFalse(tokenizer.special_attribute_present) # If remote code is disabled, we load the local one. __A : Optional[Any] = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=_UpperCAmelCase) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizerFast') self.assertFalse(tokenizer.special_attribute_present) __A : Any = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=_UpperCAmelCase , use_fast=_UpperCAmelCase) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer') self.assertFalse(tokenizer.special_attribute_present) # If remote is enabled, we load from the Hub __A : int = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=_UpperCAmelCase) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizerFast') self.assertTrue(tokenizer.special_attribute_present) __A : Optional[Any] = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=_UpperCAmelCase , use_fast=_UpperCAmelCase) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer') self.assertTrue(tokenizer.special_attribute_present) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : int = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer_legacy' , trust_remote_code=_UpperCAmelCase) self.assertTrue(tokenizer.special_attribute_present) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizerFast') # Test we can also load the slow version __A : int = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer_legacy' , trust_remote_code=_UpperCAmelCase , use_fast=_UpperCAmelCase) self.assertTrue(tokenizer.special_attribute_present) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer') else: self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer') def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' with self.assertRaisesRegex( _UpperCAmelCase , 'bert-base is not a local folder and is not a valid model identifier'): __A : Union[str, Any] = AutoTokenizer.from_pretrained('bert-base') def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' with self.assertRaisesRegex( _UpperCAmelCase , R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'): __A : Union[str, Any] = AutoTokenizer.from_pretrained(_UpperCAmelCase , revision='aaaaaa') def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Optional[int] = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-bert') with RequestCounter() as counter: __A : Union[str, Any] = AutoTokenizer.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)
190
0
"""simple docstring""" import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm __A : Union[str, Any] = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex __A : Optional[int] = 10 __A : Dict = 256 def lowercase ( _SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' if len(_SCREAMING_SNAKE_CASE ) < MIN_NUM_TOKENS: return None _UpperCAmelCase = MinHash(num_perm=_SCREAMING_SNAKE_CASE ) for token in set(_SCREAMING_SNAKE_CASE ): min_hash.update(token.encode() ) return min_hash def lowercase ( _SCREAMING_SNAKE_CASE : str ): '''simple docstring''' return {t for t in NON_ALPHA.split(_SCREAMING_SNAKE_CASE ) if len(t.strip() ) > 0} class _a : """simple docstring""" def __init__( self : Tuple , *, __UpperCamelCase : float = 0.8_5 , )->List[str]: _UpperCAmelCase = duplication_jaccard_threshold _UpperCAmelCase = NUM_PERM _UpperCAmelCase = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) _UpperCAmelCase = defaultdict(__UpperCamelCase ) def lowercase__ ( self : Any , __UpperCamelCase : Tuple , __UpperCamelCase : MinHash )->None: _UpperCAmelCase = self._index.query(__UpperCamelCase ) if code_key in self._index.keys: print(F'Duplicate key {code_key}' ) return self._index.insert(__UpperCamelCase , __UpperCamelCase ) if len(__UpperCamelCase ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(__UpperCamelCase ) break else: self._duplicate_clusters[close_duplicates[0]].add(__UpperCamelCase ) def lowercase__ ( self : str )->List[List[Dict]]: _UpperCAmelCase = [] for base, duplicates in self._duplicate_clusters.items(): _UpperCAmelCase = [base] + list(__UpperCamelCase ) # reformat the cluster to be a list of dict _UpperCAmelCase = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(__UpperCamelCase ) return duplicate_clusters def lowercase__ ( self : Tuple , __UpperCamelCase : Union[str, Any] )->None: _UpperCAmelCase = self.get_duplicate_clusters() with open(__UpperCamelCase , '''w''' ) as f: json.dump(__UpperCamelCase , __UpperCamelCase ) def lowercase ( _SCREAMING_SNAKE_CASE : Any ): '''simple docstring''' _UpperCAmelCase , _UpperCAmelCase = element _UpperCAmelCase = get_min_hash([t for t in NON_ALPHA.split(data['''content'''] ) if len(t.strip() ) > 0] ) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def lowercase ( _SCREAMING_SNAKE_CASE : Type[Dataset] ): '''simple docstring''' with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(_SCREAMING_SNAKE_CASE , max_queue_size=1_0000 ) , chunksize=100 , ): if data is not None: yield data def lowercase ( _SCREAMING_SNAKE_CASE : Type[Dataset] , _SCREAMING_SNAKE_CASE : float ): '''simple docstring''' _UpperCAmelCase = DuplicationIndex(duplication_jaccard_threshold=_SCREAMING_SNAKE_CASE ) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(_SCREAMING_SNAKE_CASE ) ) , max_queue_size=100 ) ): di.add(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def lowercase ( _SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : str ): '''simple docstring''' _UpperCAmelCase = get_tokens(_SCREAMING_SNAKE_CASE ) _UpperCAmelCase = get_tokens(_SCREAMING_SNAKE_CASE ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) __A : Tuple = None def lowercase ( _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : Any ): '''simple docstring''' _UpperCAmelCase = [] for elementa in cluster: _UpperCAmelCase = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: _UpperCAmelCase = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) >= jaccard_threshold: elementa["copies"] += 1 break else: _UpperCAmelCase = 1 extremes.append(_SCREAMING_SNAKE_CASE ) return extremes def lowercase ( _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : Optional[Any] ): '''simple docstring''' global _shared_dataset _UpperCAmelCase = dataset _UpperCAmelCase = [] _UpperCAmelCase = partial(_find_cluster_extremes_shared , jaccard_threshold=_SCREAMING_SNAKE_CASE ) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , ) , total=len(_SCREAMING_SNAKE_CASE ) , ): extremes_list.append(_SCREAMING_SNAKE_CASE ) return extremes_list def lowercase ( _SCREAMING_SNAKE_CASE : Type[Dataset] , _SCREAMING_SNAKE_CASE : float = 0.85 ): '''simple docstring''' _UpperCAmelCase = make_duplicate_clusters(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) _UpperCAmelCase = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} _UpperCAmelCase = {} _UpperCAmelCase = find_extremes(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for extremes in extremes_clusters: for element in extremes: _UpperCAmelCase = element _UpperCAmelCase = duplicate_indices - set(extreme_dict.keys() ) _UpperCAmelCase = dataset.filter(lambda _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : idx not in remove_indices , with_indices=_SCREAMING_SNAKE_CASE ) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: _UpperCAmelCase = element['''base_index'''] in extreme_dict if element["is_extreme"]: _UpperCAmelCase = extreme_dict[element['''base_index''']]['''copies'''] print(f'Original dataset size: {len(_SCREAMING_SNAKE_CASE )}' ) print(f'Number of duplicate clusters: {len(_SCREAMING_SNAKE_CASE )}' ) print(f'Files in duplicate cluster: {len(_SCREAMING_SNAKE_CASE )}' ) print(f'Unique files in duplicate cluster: {len(_SCREAMING_SNAKE_CASE )}' ) print(f'Filtered dataset size: {len(_SCREAMING_SNAKE_CASE )}' ) return ds_filter, duplicate_clusters
260
"""simple docstring""" import pytest import datasets # Import fixture modules as plugins __A : int = ["tests.fixtures.files", "tests.fixtures.hub", "tests.fixtures.fsspec"] def lowercase ( _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : Union[str, Any] ): '''simple docstring''' for item in items: if any(marker in item.keywords for marker in ['''integration''', '''unit'''] ): continue item.add_marker(pytest.mark.unit ) def lowercase ( _SCREAMING_SNAKE_CASE : Dict ): '''simple docstring''' config.addinivalue_line('''markers''' , '''torchaudio_latest: mark test to run with torchaudio>=0.12''' ) @pytest.fixture(autouse=_SCREAMING_SNAKE_CASE ) def lowercase ( _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : Any ): '''simple docstring''' _UpperCAmelCase = tmp_path_factory.getbasetemp() / '''cache''' _UpperCAmelCase = test_hf_cache_home / '''datasets''' _UpperCAmelCase = test_hf_cache_home / '''metrics''' _UpperCAmelCase = test_hf_cache_home / '''modules''' monkeypatch.setattr('''datasets.config.HF_DATASETS_CACHE''' , str(_SCREAMING_SNAKE_CASE ) ) monkeypatch.setattr('''datasets.config.HF_METRICS_CACHE''' , str(_SCREAMING_SNAKE_CASE ) ) monkeypatch.setattr('''datasets.config.HF_MODULES_CACHE''' , str(_SCREAMING_SNAKE_CASE ) ) _UpperCAmelCase = test_hf_datasets_cache / '''downloads''' monkeypatch.setattr('''datasets.config.DOWNLOADED_DATASETS_PATH''' , str(_SCREAMING_SNAKE_CASE ) ) _UpperCAmelCase = test_hf_datasets_cache / '''downloads''' / '''extracted''' monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_PATH''' , str(_SCREAMING_SNAKE_CASE ) ) @pytest.fixture(autouse=_SCREAMING_SNAKE_CASE , scope='''session''' ) def lowercase ( ): '''simple docstring''' datasets.disable_progress_bar() @pytest.fixture(autouse=_SCREAMING_SNAKE_CASE ) def lowercase ( _SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' monkeypatch.setattr('''datasets.config.HF_UPDATE_DOWNLOAD_COUNTS''' , _SCREAMING_SNAKE_CASE ) @pytest.fixture def lowercase ( _SCREAMING_SNAKE_CASE : Union[str, Any] ): '''simple docstring''' monkeypatch.setattr('''sqlalchemy.util.deprecations.SILENCE_UBER_WARNING''' , _SCREAMING_SNAKE_CASE )
260
1
'''simple docstring''' import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def lowerCamelCase__ ( A : Dict , A : Union[str, Any] , A : Union[str, Any] , A : Any , A : int ): '''simple docstring''' with open(A ) as metadata_file: UpperCAmelCase = json.load(A ) UpperCAmelCase = LukeConfig(use_entity_aware_attention=A , **metadata['''model_config'''] ) # Load in the weights from the checkpoint_path UpperCAmelCase = torch.load(A , map_location='''cpu''' )['''module'''] # Load the entity vocab file UpperCAmelCase = load_original_entity_vocab(A ) # add an entry for [MASK2] UpperCAmelCase = max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 UpperCAmelCase = XLMRobertaTokenizer.from_pretrained(metadata['''model_config''']['''bert_model_name'''] ) # Add special tokens to the token vocabulary for downstream tasks UpperCAmelCase = AddedToken('''<ent>''' , lstrip=A , rstrip=A ) UpperCAmelCase = AddedToken('''<ent2>''' , lstrip=A , rstrip=A ) tokenizer.add_special_tokens({'''additional_special_tokens''': [entity_token_a, entity_token_a]} ) config.vocab_size += 2 print(f"""Saving tokenizer to {pytorch_dump_folder_path}""" ) tokenizer.save_pretrained(A ) with open(os.path.join(A , '''tokenizer_config.json''' ) , '''r''' ) as f: UpperCAmelCase = json.load(A ) UpperCAmelCase = '''MLukeTokenizer''' with open(os.path.join(A , '''tokenizer_config.json''' ) , '''w''' ) as f: json.dump(A , A ) with open(os.path.join(A , MLukeTokenizer.vocab_files_names['''entity_vocab_file'''] ) , '''w''' ) as f: json.dump(A , A ) UpperCAmelCase = MLukeTokenizer.from_pretrained(A ) # Initialize the embeddings of the special tokens UpperCAmelCase = tokenizer.convert_tokens_to_ids(['''@'''] )[0] UpperCAmelCase = tokenizer.convert_tokens_to_ids(['''#'''] )[0] UpperCAmelCase = state_dict['''embeddings.word_embeddings.weight'''] UpperCAmelCase = word_emb[ent_init_index].unsqueeze(0 ) UpperCAmelCase = word_emb[enta_init_index].unsqueeze(0 ) UpperCAmelCase = torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: UpperCAmelCase = state_dict[bias_name] UpperCAmelCase = decoder_bias[ent_init_index].unsqueeze(0 ) UpperCAmelCase = decoder_bias[enta_init_index].unsqueeze(0 ) UpperCAmelCase = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: UpperCAmelCase = f"""encoder.layer.{layer_index}.attention.self.""" UpperCAmelCase = state_dict[prefix + matrix_name] UpperCAmelCase = state_dict[prefix + matrix_name] UpperCAmelCase = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks UpperCAmelCase = state_dict['''entity_embeddings.entity_embeddings.weight'''] UpperCAmelCase = entity_emb[entity_vocab['''[MASK]''']].unsqueeze(0 ) UpperCAmelCase = torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' UpperCAmelCase = state_dict['''entity_predictions.bias'''] UpperCAmelCase = entity_prediction_bias[entity_vocab['''[MASK]''']].unsqueeze(0 ) UpperCAmelCase = torch.cat([entity_prediction_bias, entity_mask_bias] ) UpperCAmelCase = LukeForMaskedLM(config=A ).eval() state_dict.pop('''entity_predictions.decoder.weight''' ) state_dict.pop('''lm_head.decoder.weight''' ) state_dict.pop('''lm_head.decoder.bias''' ) UpperCAmelCase = OrderedDict() for key, value in state_dict.items(): if not (key.startswith('''lm_head''' ) or key.startswith('''entity_predictions''' )): UpperCAmelCase = state_dict[key] else: UpperCAmelCase = state_dict[key] UpperCAmelCase , UpperCAmelCase = model.load_state_dict(A , strict=A ) if set(A ) != {"luke.embeddings.position_ids"}: raise ValueError(f"""Unexpected unexpected_keys: {unexpected_keys}""" ) if set(A ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(f"""Unexpected missing_keys: {missing_keys}""" ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs UpperCAmelCase = MLukeTokenizer.from_pretrained(A , task='''entity_classification''' ) UpperCAmelCase = '''ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan).''' UpperCAmelCase = (0, 9) UpperCAmelCase = tokenizer(A , entity_spans=[span] , return_tensors='''pt''' ) UpperCAmelCase = model(**A ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base UpperCAmelCase = torch.Size((1, 33, 7_68) ) UpperCAmelCase = torch.tensor([[0.0_892, 0.0_596, -0.2_819], [0.0_134, 0.1_199, 0.0_573], [-0.0_169, 0.0_927, 0.0_644]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( f"""Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}""" ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3] , A , atol=1E-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base UpperCAmelCase = torch.Size((1, 1, 7_68) ) UpperCAmelCase = torch.tensor([[-0.1_482, 0.0_609, 0.0_322]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( f"""Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is""" f""" {expected_shape}""" ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] , A , atol=1E-4 ): raise ValueError # Verify masked word/entity prediction UpperCAmelCase = MLukeTokenizer.from_pretrained(A ) UpperCAmelCase = '''Tokyo is the capital of <mask>.''' UpperCAmelCase = (24, 30) UpperCAmelCase = tokenizer(A , entity_spans=[span] , return_tensors='''pt''' ) UpperCAmelCase = model(**A ) UpperCAmelCase = encoding['''input_ids'''][0].tolist() UpperCAmelCase = input_ids.index(tokenizer.convert_tokens_to_ids('''<mask>''' ) ) UpperCAmelCase = outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(A ) UpperCAmelCase = outputs.entity_logits[0][0].argmax().item() UpperCAmelCase = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith('''en:''' )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print('''Saving PyTorch model to {}'''.format(A ) ) model.save_pretrained(A ) def lowerCamelCase__ ( A : Tuple ): '''simple docstring''' UpperCAmelCase = ['''[MASK]''', '''[PAD]''', '''[UNK]'''] UpperCAmelCase = [json.loads(A ) for line in open(A )] UpperCAmelCase = {} for entry in data: UpperCAmelCase = entry['''id'''] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: UpperCAmelCase = entity_id break UpperCAmelCase = f"""{language}:{entity_name}""" UpperCAmelCase = entity_id return new_mapping if __name__ == "__main__": _lowercase : str = argparse.ArgumentParser() # Required parameters parser.add_argument("""--checkpoint_path""", type=str, help="""Path to a pytorch_model.bin file.""") parser.add_argument( """--metadata_path""", default=None, type=str, help="""Path to a metadata.json file, defining the configuration.""" ) parser.add_argument( """--entity_vocab_path""", default=None, type=str, help="""Path to an entity_vocab.tsv file, containing the entity vocabulary.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to where to dump the output PyTorch model.""" ) parser.add_argument( """--model_size""", default="""base""", type=str, choices=["""base""", """large"""], help="""Size of the model to be converted.""" ) _lowercase : Dict = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
91
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _lowercase : List[Any] = { """configuration_x_clip""": [ """XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP""", """XCLIPConfig""", """XCLIPTextConfig""", """XCLIPVisionConfig""", ], """processing_x_clip""": ["""XCLIPProcessor"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowercase : List[str] = [ """XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST""", """XCLIPModel""", """XCLIPPreTrainedModel""", """XCLIPTextModel""", """XCLIPVisionModel""", ] if TYPE_CHECKING: from .configuration_x_clip import ( XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, XCLIPConfig, XCLIPTextConfig, XCLIPVisionConfig, ) from .processing_x_clip import XCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_x_clip import ( XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, XCLIPModel, XCLIPPreTrainedModel, XCLIPTextModel, XCLIPVisionModel, ) else: import sys _lowercase : List[Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
91
1
"""simple docstring""" def _A ( lowercase , lowercase ): """simple docstring""" return int(input_a == input_a == 0 ) def _A ( ): """simple docstring""" print('''Truth Table of NOR Gate:''' ) print('''| Input 1 | Input 2 | Output |''' ) print(f'''| 0 | 0 | {nor_gate(0 , 0 )} |''' ) print(f'''| 0 | 1 | {nor_gate(0 , 1 )} |''' ) print(f'''| 1 | 0 | {nor_gate(1 , 0 )} |''' ) print(f'''| 1 | 1 | {nor_gate(1 , 1 )} |''' ) if __name__ == "__main__": import doctest doctest.testmod() main()
81
"""simple docstring""" lowerCamelCase_ : int = """ # Installazione di Transformers ! pip install transformers datasets # Per installare dalla fonte invece dell'ultima versione rilasciata, commenta il comando sopra e # rimuovi la modalità commento al comando seguente. # ! pip install git+https://github.com/huggingface/transformers.git """ lowerCamelCase_ : Dict = [{"""type""": """code""", """content""": INSTALL_CONTENT}] lowerCamelCase_ : Union[str, Any] = { """{processor_class}""": """FakeProcessorClass""", """{model_class}""": """FakeModelClass""", """{object_class}""": """FakeObjectClass""", }
81
1
import inspect import os import sys import unittest import accelerate from accelerate.test_utils import execute_subprocess_async, require_tpu class lowercase ( unittest.TestCase): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : int ) -> List[str]: UpperCAmelCase_= inspect.getfile(accelerate.test_utils ) UpperCAmelCase_= os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["""scripts""", """test_script.py"""] ) UpperCAmelCase_= os.path.sep.join(inspect.getfile(self.__class__ ).split(os.path.sep )[:-1] ) @require_tpu def _SCREAMING_SNAKE_CASE ( self : Dict ) -> Tuple: UpperCAmelCase_= F""" {self.test_dir}/xla_spawn.py --num_cores 8 {self.test_file_path} """.split() UpperCAmelCase_= [sys.executable] + distributed_args execute_subprocess_async(__UpperCAmelCase , env=os.environ.copy() )
277
import itertools from dataclasses import dataclass from typing import Any, Callable, Dict, List, Optional, Union import pandas as pd import pyarrow as pa import datasets import datasets.config from datasets.features.features import require_storage_cast from datasets.table import table_cast from datasets.utils.py_utils import Literal __A = datasets.utils.logging.get_logger(__name__) __A = ['''names''', '''prefix'''] __A = ['''warn_bad_lines''', '''error_bad_lines''', '''mangle_dupe_cols'''] __A = ['''encoding_errors''', '''on_bad_lines'''] __A = ['''date_format'''] @dataclass class lowercase ( datasets.BuilderConfig): """simple docstring""" a__ : str = "," a__ : Optional[str] = None a__ : Optional[Union[int, List[int], str]] = "infer" a__ : Optional[List[str]] = None a__ : Optional[List[str]] = None a__ : Optional[Union[int, str, List[int], List[str]]] = None a__ : Optional[Union[List[int], List[str]]] = None a__ : Optional[str] = None a__ : bool = True a__ : Optional[Literal["c", "python", "pyarrow"]] = None a__ : Dict[Union[int, str], Callable[[Any], Any]] = None a__ : Optional[list] = None a__ : Optional[list] = None a__ : bool = False a__ : Optional[Union[int, List[int]]] = None a__ : Optional[int] = None a__ : Optional[Union[str, List[str]]] = None a__ : bool = True a__ : bool = True a__ : bool = False a__ : bool = True a__ : Optional[str] = None a__ : str = "." a__ : Optional[str] = None a__ : str = '"' a__ : int = 0 a__ : Optional[str] = None a__ : Optional[str] = None a__ : Optional[str] = None a__ : Optional[str] = None a__ : bool = True a__ : bool = True a__ : int = 0 a__ : bool = True a__ : bool = False a__ : Optional[str] = None a__ : int = 1_0000 a__ : Optional[datasets.Features] = None a__ : Optional[str] = "strict" a__ : Literal["error", "warn", "skip"] = "error" a__ : Optional[str] = None def _SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> int: if self.delimiter is not None: UpperCAmelCase_= self.delimiter if self.column_names is not None: UpperCAmelCase_= self.column_names @property def _SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: UpperCAmelCase_= { """sep""": self.sep, """header""": self.header, """names""": self.names, """index_col""": self.index_col, """usecols""": self.usecols, """prefix""": self.prefix, """mangle_dupe_cols""": self.mangle_dupe_cols, """engine""": self.engine, """converters""": self.converters, """true_values""": self.true_values, """false_values""": self.false_values, """skipinitialspace""": self.skipinitialspace, """skiprows""": self.skiprows, """nrows""": self.nrows, """na_values""": self.na_values, """keep_default_na""": self.keep_default_na, """na_filter""": self.na_filter, """verbose""": self.verbose, """skip_blank_lines""": self.skip_blank_lines, """thousands""": self.thousands, """decimal""": self.decimal, """lineterminator""": self.lineterminator, """quotechar""": self.quotechar, """quoting""": self.quoting, """escapechar""": self.escapechar, """comment""": self.comment, """encoding""": self.encoding, """dialect""": self.dialect, """error_bad_lines""": self.error_bad_lines, """warn_bad_lines""": self.warn_bad_lines, """skipfooter""": self.skipfooter, """doublequote""": self.doublequote, """memory_map""": self.memory_map, """float_precision""": self.float_precision, """chunksize""": self.chunksize, """encoding_errors""": self.encoding_errors, """on_bad_lines""": self.on_bad_lines, """date_format""": self.date_format, } # some kwargs must not be passed if they don't have a default value # some others are deprecated and we can also not pass them if they are the default value for pd_read_csv_parameter in _PANDAS_READ_CSV_NO_DEFAULT_PARAMETERS + _PANDAS_READ_CSV_DEPRECATED_PARAMETERS: if pd_read_csv_kwargs[pd_read_csv_parameter] == getattr(CsvConfig() , __UpperCAmelCase ): del pd_read_csv_kwargs[pd_read_csv_parameter] # Remove 2.0 new arguments if not (datasets.config.PANDAS_VERSION.major >= 2): for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_2_0_0_PARAMETERS: del pd_read_csv_kwargs[pd_read_csv_parameter] # Remove 1.3 new arguments if not (datasets.config.PANDAS_VERSION.major >= 1 and datasets.config.PANDAS_VERSION.minor >= 3): for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_1_3_0_PARAMETERS: del pd_read_csv_kwargs[pd_read_csv_parameter] return pd_read_csv_kwargs class lowercase ( datasets.ArrowBasedBuilder): """simple docstring""" a__ : int = CsvConfig def _SCREAMING_SNAKE_CASE ( self : List[str] ) -> Any: return datasets.DatasetInfo(features=self.config.features ) def _SCREAMING_SNAKE_CASE ( self : Any , __UpperCAmelCase : Dict ) -> Optional[int]: if not self.config.data_files: raise ValueError(F"""At least one data file must be specified, but got data_files={self.config.data_files}""" ) UpperCAmelCase_= dl_manager.download_and_extract(self.config.data_files ) if isinstance(__UpperCAmelCase , (str, list, tuple) ): UpperCAmelCase_= data_files if isinstance(__UpperCAmelCase , __UpperCAmelCase ): UpperCAmelCase_= [files] UpperCAmelCase_= [dl_manager.iter_files(__UpperCAmelCase ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={"""files""": files} )] UpperCAmelCase_= [] for split_name, files in data_files.items(): if isinstance(__UpperCAmelCase , __UpperCAmelCase ): UpperCAmelCase_= [files] UpperCAmelCase_= [dl_manager.iter_files(__UpperCAmelCase ) for file in files] splits.append(datasets.SplitGenerator(name=__UpperCAmelCase , gen_kwargs={"""files""": files} ) ) return splits def _SCREAMING_SNAKE_CASE ( self : Tuple , __UpperCAmelCase : pa.Table ) -> pa.Table: if self.config.features is not None: UpperCAmelCase_= self.config.features.arrow_schema if all(not require_storage_cast(__UpperCAmelCase ) for feature in self.config.features.values() ): # cheaper cast UpperCAmelCase_= pa.Table.from_arrays([pa_table[field.name] for field in schema] , schema=__UpperCAmelCase ) else: # more expensive cast; allows str <-> int/float or str to Audio for example UpperCAmelCase_= table_cast(__UpperCAmelCase , __UpperCAmelCase ) return pa_table def _SCREAMING_SNAKE_CASE ( self : Any , __UpperCAmelCase : List[Any] ) -> List[str]: UpperCAmelCase_= self.config.features.arrow_schema if self.config.features else None # dtype allows reading an int column as str UpperCAmelCase_= ( { name: dtype.to_pandas_dtype() if not require_storage_cast(__UpperCAmelCase ) else object for name, dtype, feature in zip(schema.names , schema.types , self.config.features.values() ) } if schema is not None else None ) for file_idx, file in enumerate(itertools.chain.from_iterable(__UpperCAmelCase ) ): UpperCAmelCase_= pd.read_csv(__UpperCAmelCase , iterator=__UpperCAmelCase , dtype=__UpperCAmelCase , **self.config.pd_read_csv_kwargs ) try: for batch_idx, df in enumerate(__UpperCAmelCase ): UpperCAmelCase_= pa.Table.from_pandas(__UpperCAmelCase ) # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield (file_idx, batch_idx), self._cast_table(__UpperCAmelCase ) except ValueError as e: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCAmelCase )}: {e}""" ) raise
277
1
"""simple docstring""" import collections import json import os import re from typing import TYPE_CHECKING, List, Optional, Tuple import numpy as np from ...tokenization_utils_fast import PreTrainedTokenizer from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation __A = logging.get_logger(__name__) __A = {"""vocab_file""": """vocab.txt""", """emoji_file""": """emoji.json"""} __A = { """vocab_file""": { """abeja/gpt-neox-japanese-2.7b""": """https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/vocab.txt""", }, """emoji_file""": { """abeja/gpt-neox-japanese-2.7b""": """https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/emoji.json""", }, } __A = { """abeja/gpt-neox-japanese-2.7b""": 2048, } def __A (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ->Dict: """simple docstring""" with open(_SCREAMING_SNAKE_CASE , 'r' , encoding='utf-8' ) as f: lowerCAmelCase__ :Optional[Any] = json.loads(f.read() ) lowerCAmelCase__ :Optional[Any] = collections.OrderedDict() lowerCAmelCase__ :List[Any] = collections.OrderedDict() lowerCAmelCase__ :List[str] = collections.OrderedDict() with open(_SCREAMING_SNAKE_CASE , 'r' , encoding='utf-8' ) as f: lowerCAmelCase__ :str = f.readlines() lowerCAmelCase__ :Optional[int] = [[t.rstrip('\n' )] if (t == ',' or ',' not in t) else t.rstrip('\n' ).split(',' ) for t in token] for idx, b in enumerate(_SCREAMING_SNAKE_CASE ): lowerCAmelCase__ :Tuple = b lowerCAmelCase__ :Optional[int] = idx for wd in b: lowerCAmelCase__ :Any = idx return vocab, raw_vocab, ids_to_tokens, emoji class _lowerCAmelCase ( a ): """simple docstring""" __magic_name__ :Optional[Any] = VOCAB_FILES_NAMES __magic_name__ :Any = PRETRAINED_VOCAB_FILES_MAP __magic_name__ :Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __magic_name__ :List[str] = ["""input_ids""", """attention_mask"""] def __init__( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase="<|endoftext|>" , __UpperCAmelCase="<|endoftext|>" , __UpperCAmelCase="<|startoftext|>" , __UpperCAmelCase="<|endoftext|>" , __UpperCAmelCase=False , **__UpperCAmelCase , ): '''simple docstring''' super().__init__( unk_token=__UpperCAmelCase , pad_token=__UpperCAmelCase , bos_token=__UpperCAmelCase , eos_token=__UpperCAmelCase , do_clean_text=__UpperCAmelCase , **__UpperCAmelCase , ) if not os.path.isfile(__UpperCAmelCase ): raise ValueError( F"Can't find a vocabulary file at path '{vocab_file}'. To load the vocabulary from a Google pretrained" ' model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`' ) if not os.path.isfile(__UpperCAmelCase ): raise ValueError( F"Can't find a emoji file at path '{emoji_file}'. To load the emoji information from a Google" ' pretrained model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`' ) lowerCAmelCase__ :Tuple = do_clean_text lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ :int = load_vocab_and_emoji(__UpperCAmelCase , __UpperCAmelCase ) lowerCAmelCase__ :int = SubWordJapaneseTokenizer( vocab=self.vocab , ids_to_tokens=self.ids_to_tokens , emoji=self.emoji ) @property def snake_case ( self ): '''simple docstring''' return len(self.raw_vocab ) def snake_case ( self ): '''simple docstring''' return dict(self.raw_vocab , **self.added_tokens_encoder ) def snake_case ( self , __UpperCAmelCase ): '''simple docstring''' return self.subword_tokenizer.tokenize(__UpperCAmelCase , clean=self.do_clean_text ) def snake_case ( self , __UpperCAmelCase ): '''simple docstring''' return self.vocab.get(__UpperCAmelCase , self.vocab.get(self.unk_token ) ) def snake_case ( self , __UpperCAmelCase ): '''simple docstring''' return self.subword_tokenizer.convert_id_to_token(__UpperCAmelCase ) def snake_case ( self , __UpperCAmelCase ): '''simple docstring''' lowerCAmelCase__ :Tuple = ''.join(__UpperCAmelCase ).strip() return out_string def snake_case ( self , __UpperCAmelCase ): '''simple docstring''' lowerCAmelCase__ :Dict = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(__UpperCAmelCase , add_special_tokens=__UpperCAmelCase ) + [self.eos_token_id] ) if len(__UpperCAmelCase ) > self.model_max_length: lowerCAmelCase__ :Dict = input_ids[-self.model_max_length :] return input_ids def snake_case ( self , __UpperCAmelCase , __UpperCAmelCase = None ): '''simple docstring''' lowerCAmelCase__ :List[str] = 0 if os.path.isdir(__UpperCAmelCase ): lowerCAmelCase__ :Tuple = os.path.join( __UpperCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) lowerCAmelCase__ :int = os.path.join( __UpperCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['emoji_file'] ) else: lowerCAmelCase__ :List[Any] = ( (filename_prefix + '-' if filename_prefix else '') + save_directory + VOCAB_FILES_NAMES['vocab_file'] ) lowerCAmelCase__ :Optional[Any] = ( (filename_prefix + '-' if filename_prefix else '') + save_directory + VOCAB_FILES_NAMES['emoji_file'] ) with open(__UpperCAmelCase , 'w' , encoding='utf-8' ) as writer: for token_index, token in self.ids_to_tokens.items(): if index != token_index: logger.warning( F"Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive." ' Please check that the vocabulary is not corrupted!' ) lowerCAmelCase__ :int = token_index writer.write(','.join(__UpperCAmelCase ) + '\n' ) index += 1 with open(__UpperCAmelCase , 'w' , encoding='utf-8' ) as writer: json.dump(self.emoji , __UpperCAmelCase ) return vocab_file, emoji_file class _lowerCAmelCase ( a ): """simple docstring""" def __init__( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ): '''simple docstring''' lowerCAmelCase__ :Dict = vocab # same as swe lowerCAmelCase__ :str = ids_to_tokens # same as bpe lowerCAmelCase__ :Tuple = emoji lowerCAmelCase__ :Optional[int] = np.max([len(__UpperCAmelCase ) for w in self.vocab.keys()] ) lowerCAmelCase__ :Union[str, Any] = re.compile(R'(https?|ftp)(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/?:\@&=\+$,%#]+)' ) lowerCAmelCase__ :List[Any] = re.compile(R'[A-Za-z0-9\._+]*@[\-_0-9A-Za-z]+(\.[A-Za-z]+)*' ) lowerCAmelCase__ :Optional[Any] = re.compile(R'[\(]{0,1}[0-9]{2,4}[\)\-\(]{0,1}[0-9]{2,4}[\)\-]{0,1}[0-9]{3,4}' ) lowerCAmelCase__ :int = re.compile( R'([12]\d{3}[/\-年])*(0?[1-9]|1[0-2])[/\-月]((0?[1-9]|[12][0-9]|3[01])日?)*(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*' ) lowerCAmelCase__ :List[Any] = re.compile( R'(明治|大正|昭和|平成|令和|㍾|㍽|㍼|㍻|\u32ff)\d{1,2}年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*' ) lowerCAmelCase__ :Optional[Any] = re.compile( R'((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*億)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*万)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*千)*(0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*(千円|万円|千万円|円|千ドル|万ドル|千万ドル|ドル|千ユーロ|万ユーロ|千万ユーロ|ユーロ)+(\(税込\)|\(税抜\)|\+tax)*' ) lowerCAmelCase__ :Dict = '─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿' lowerCAmelCase__ :Optional[int] = '▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟' lowerCAmelCase__ :Optional[Any] = str.maketrans({k: '<BLOCK>' for k in keisen + blocks} ) def __len__( self ): '''simple docstring''' return len(self.ids_to_tokens ) def snake_case ( self , __UpperCAmelCase ): '''simple docstring''' lowerCAmelCase__ :Optional[int] = self.content_repattera.sub('<URL>' , __UpperCAmelCase ) lowerCAmelCase__ :Dict = self.content_repattera.sub('<EMAIL>' , __UpperCAmelCase ) lowerCAmelCase__ :Dict = self.content_repattera.sub('<TEL>' , __UpperCAmelCase ) lowerCAmelCase__ :Dict = self.content_repattera.sub('<DATE>' , __UpperCAmelCase ) lowerCAmelCase__ :List[Any] = self.content_repattera.sub('<DATE>' , __UpperCAmelCase ) lowerCAmelCase__ :Union[str, Any] = self.content_repattera.sub('<PRICE>' , __UpperCAmelCase ) lowerCAmelCase__ :List[str] = content.translate(self.content_transa ) while "<BLOCK><BLOCK>" in content: lowerCAmelCase__ :Optional[Any] = content.replace('<BLOCK><BLOCK>' , '<BLOCK>' ) return content def snake_case ( self , __UpperCAmelCase , __UpperCAmelCase=False ): '''simple docstring''' lowerCAmelCase__ :Tuple = text.replace(' ' , '<SP>' ) lowerCAmelCase__ :Union[str, Any] = text.replace(' ' , '<SP>' ) lowerCAmelCase__ :Any = text.replace('\r\n' , '<BR>' ) lowerCAmelCase__ :Tuple = text.replace('\n' , '<BR>' ) lowerCAmelCase__ :int = text.replace('\r' , '<BR>' ) lowerCAmelCase__ :int = text.replace('\t' , '<TAB>' ) lowerCAmelCase__ :Optional[Any] = text.replace('—' , 'ー' ) lowerCAmelCase__ :List[Any] = text.replace('−' , 'ー' ) for k, v in self.emoji["emoji"].items(): if k in text: lowerCAmelCase__ :List[str] = text.replace(__UpperCAmelCase , __UpperCAmelCase ) if clean: lowerCAmelCase__ :Optional[Any] = self.clean_text(__UpperCAmelCase ) def check_simbol(__UpperCAmelCase ): lowerCAmelCase__ :Dict = x.encode() if len(__UpperCAmelCase ) == 1 and len(__UpperCAmelCase ) == 2: lowerCAmelCase__ :Optional[int] = (int(e[0] ) << 8) + int(e[1] ) if ( (c >= 0xc2_a1 and c <= 0xc2_bf) or (c >= 0xc7_80 and c <= 0xc7_83) or (c >= 0xca_b9 and c <= 0xcb_bf) or (c >= 0xcc_80 and c <= 0xcd_a2) ): return True return False def checkuae(__UpperCAmelCase ): lowerCAmelCase__ :Union[str, Any] = x.encode() if len(__UpperCAmelCase ) == 1 and len(__UpperCAmelCase ) == 3: lowerCAmelCase__ :Any = (int(e[0] ) << 1_6) + (int(e[1] ) << 8) + int(e[2] ) if c >= 0xe2_80_80 and c <= 0xe2_b0_7f: return True return False lowerCAmelCase__ :Optional[Any] = 0 lowerCAmelCase__ :int = [] while pos < len(__UpperCAmelCase ): lowerCAmelCase__ :Optional[Any] = min(len(__UpperCAmelCase ) , pos + self.maxlen + 1 ) if text[pos] == '<' else pos + 3 lowerCAmelCase__ :Tuple = [] # (token_id, token, pos) for e in range(__UpperCAmelCase , __UpperCAmelCase , -1 ): lowerCAmelCase__ :Optional[Any] = text[pos:e] if wd in self.vocab: if wd[0] == "<" and len(__UpperCAmelCase ) > 2: lowerCAmelCase__ :Tuple = [(self.vocab[wd], wd, e)] break else: candidates.append((self.vocab[wd], wd, e) ) if len(__UpperCAmelCase ) > 0: # the smallest token_id is adopted lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ :Any = sorted(__UpperCAmelCase , key=lambda __UpperCAmelCase : x[0] )[0] result.append(__UpperCAmelCase ) lowerCAmelCase__ :str = e else: lowerCAmelCase__ :List[Any] = pos + 1 lowerCAmelCase__ :Dict = text[pos:end] if check_simbol(__UpperCAmelCase ): result.append('<KIGOU>' ) elif checkuae(__UpperCAmelCase ): result.append('<U2000U2BFF>' ) else: for i in wd.encode('utf-8' ): result.append('<|byte%d|>' % i ) lowerCAmelCase__ :List[str] = end return result def snake_case ( self , __UpperCAmelCase , __UpperCAmelCase="\n" ): '''simple docstring''' lowerCAmelCase__ :Optional[int] = [] lowerCAmelCase__ :Tuple = [] lowerCAmelCase__ :int = self.ids_to_tokens[index][0] if word[:6] == "<|byte" and word[-2:] == "|>": byte_tokens.append(int(word[6:-2] ) ) else: if len(__UpperCAmelCase ) > 0: words.append(bytearray(__UpperCAmelCase ).decode('utf-8' , errors='replace' ) ) lowerCAmelCase__ :str = [] if word[:7] == "<|emoji" and word[-2:] == "|>": words.append(self.emoji['emoji_inv'][word] ) elif word == "<SP>": words.append(' ' ) elif word == "<BR>": words.append(__UpperCAmelCase ) elif word == "<TAB>": words.append('\t' ) elif word == "<BLOCK>": words.append('▀' ) elif word == "<KIGOU>": words.append('ǀ' ) elif word == "<U2000U2BFF>": words.append('‖' ) else: words.append(__UpperCAmelCase ) if len(__UpperCAmelCase ) > 0: words.append(bytearray(__UpperCAmelCase ).decode('utf-8' , errors='replace' ) ) lowerCAmelCase__ :Any = ''.join(__UpperCAmelCase ) return text
293
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class _lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def __init__( self , __UpperCAmelCase , __UpperCAmelCase=7 , __UpperCAmelCase=3 , __UpperCAmelCase=1_8 , __UpperCAmelCase=3_0 , __UpperCAmelCase=4_0_0 , __UpperCAmelCase=True , __UpperCAmelCase=None , __UpperCAmelCase=True , ): '''simple docstring''' lowerCAmelCase__ :Dict = size if size is not None else {'height': 1_8, 'width': 1_8} lowerCAmelCase__ :Tuple = parent lowerCAmelCase__ :List[Any] = batch_size lowerCAmelCase__ :List[Any] = num_channels lowerCAmelCase__ :Any = image_size lowerCAmelCase__ :int = min_resolution lowerCAmelCase__ :int = max_resolution lowerCAmelCase__ :Dict = do_resize lowerCAmelCase__ :str = size lowerCAmelCase__ :Any = apply_ocr def snake_case ( self ): '''simple docstring''' return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class _lowerCAmelCase ( a , unittest.TestCase ): """simple docstring""" __magic_name__ :str = LayoutLMvaImageProcessor if is_pytesseract_available() else None def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :List[Any] = LayoutLMvaImageProcessingTester(self ) @property def snake_case ( self ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Optional[int] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__UpperCAmelCase , 'do_resize' ) ) self.assertTrue(hasattr(__UpperCAmelCase , 'size' ) ) self.assertTrue(hasattr(__UpperCAmelCase , 'apply_ocr' ) ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 1_8, 'width': 1_8} ) lowerCAmelCase__ :List[str] = self.image_processing_class.from_dict(self.image_processor_dict , size=4_2 ) self.assertEqual(image_processor.size , {'height': 4_2, 'width': 4_2} ) def snake_case ( self ): '''simple docstring''' pass def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowerCAmelCase__ :Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=__UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(__UpperCAmelCase , Image.Image ) # Test not batched input lowerCAmelCase__ :Tuple = image_processing(image_inputs[0] , return_tensors='pt' ) self.assertEqual( encoding.pixel_values.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) self.assertIsInstance(encoding.words , __UpperCAmelCase ) self.assertIsInstance(encoding.boxes , __UpperCAmelCase ) # Test batched lowerCAmelCase__ :Any = image_processing(__UpperCAmelCase , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowerCAmelCase__ :Any = prepare_image_inputs(self.image_processor_tester , equal_resolution=__UpperCAmelCase , numpify=__UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(__UpperCAmelCase , np.ndarray ) # Test not batched input lowerCAmelCase__ :Tuple = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowerCAmelCase__ :Optional[Any] = image_processing(__UpperCAmelCase , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Tuple = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowerCAmelCase__ :List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__UpperCAmelCase , torchify=__UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(__UpperCAmelCase , torch.Tensor ) # Test not batched input lowerCAmelCase__ :Tuple = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowerCAmelCase__ :Any = image_processing(__UpperCAmelCase , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :List[str] = LayoutLMvaImageProcessor() from datasets import load_dataset lowerCAmelCase__ :Tuple = load_dataset('hf-internal-testing/fixtures_docvqa' , split='test' ) lowerCAmelCase__ :int = Image.open(ds[0]['file'] ).convert('RGB' ) lowerCAmelCase__ :Optional[int] = image_processing(__UpperCAmelCase , return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 2_2_4, 2_2_4) ) self.assertEqual(len(encoding.words ) , len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 lowerCAmelCase__ :Optional[Any] = [['11:14', 'to', '11:39', 'a.m', '11:39', 'to', '11:44', 'a.m.', '11:44', 'a.m.', 'to', '12:25', 'p.m.', '12:25', 'to', '12:58', 'p.m.', '12:58', 'to', '4:00', 'p.m.', '2:00', 'to', '5:00', 'p.m.', 'Coffee', 'Break', 'Coffee', 'will', 'be', 'served', 'for', 'men', 'and', 'women', 'in', 'the', 'lobby', 'adjacent', 'to', 'exhibit', 'area.', 'Please', 'move', 'into', 'exhibit', 'area.', '(Exhibits', 'Open)', 'TRRF', 'GENERAL', 'SESSION', '(PART', '|)', 'Presiding:', 'Lee', 'A.', 'Waller', 'TRRF', 'Vice', 'President', '“Introductory', 'Remarks”', 'Lee', 'A.', 'Waller,', 'TRRF', 'Vice', 'Presi-', 'dent', 'Individual', 'Interviews', 'with', 'TRRF', 'Public', 'Board', 'Members', 'and', 'Sci-', 'entific', 'Advisory', 'Council', 'Mem-', 'bers', 'Conducted', 'by', 'TRRF', 'Treasurer', 'Philip', 'G.', 'Kuehn', 'to', 'get', 'answers', 'which', 'the', 'public', 'refrigerated', 'warehousing', 'industry', 'is', 'looking', 'for.', 'Plus', 'questions', 'from', 'the', 'floor.', 'Dr.', 'Emil', 'M.', 'Mrak,', 'University', 'of', 'Cal-', 'ifornia,', 'Chairman,', 'TRRF', 'Board;', 'Sam', 'R.', 'Cecil,', 'University', 'of', 'Georgia', 'College', 'of', 'Agriculture;', 'Dr.', 'Stanley', 'Charm,', 'Tufts', 'University', 'School', 'of', 'Medicine;', 'Dr.', 'Robert', 'H.', 'Cotton,', 'ITT', 'Continental', 'Baking', 'Company;', 'Dr.', 'Owen', 'Fennema,', 'University', 'of', 'Wis-', 'consin;', 'Dr.', 'Robert', 'E.', 'Hardenburg,', 'USDA.', 'Questions', 'and', 'Answers', 'Exhibits', 'Open', 'Capt.', 'Jack', 'Stoney', 'Room', 'TRRF', 'Scientific', 'Advisory', 'Council', 'Meeting', 'Ballroom', 'Foyer']] # noqa: E231 lowerCAmelCase__ :List[str] = [[[1_4_1, 5_7, 2_1_4, 6_9], [2_2_8, 5_8, 2_5_2, 6_9], [1_4_1, 7_5, 2_1_6, 8_8], [2_3_0, 7_9, 2_8_0, 8_8], [1_4_2, 2_6_0, 2_1_8, 2_7_3], [2_3_0, 2_6_1, 2_5_5, 2_7_3], [1_4_3, 2_7_9, 2_1_8, 2_9_0], [2_3_1, 2_8_2, 2_9_0, 2_9_1], [1_4_3, 3_4_2, 2_1_8, 3_5_4], [2_3_1, 3_4_5, 2_8_9, 3_5_5], [2_0_2, 3_6_2, 2_2_7, 3_7_3], [1_4_3, 3_7_9, 2_2_0, 3_9_2], [2_3_1, 3_8_2, 2_9_1, 3_9_4], [1_4_4, 7_1_4, 2_2_0, 7_2_6], [2_3_1, 7_1_5, 2_5_6, 7_2_6], [1_4_4, 7_3_2, 2_2_0, 7_4_5], [2_3_2, 7_3_6, 2_9_1, 7_4_7], [1_4_4, 7_6_9, 2_1_8, 7_8_2], [2_3_1, 7_7_0, 2_5_6, 7_8_2], [1_4_1, 7_8_8, 2_0_2, 8_0_1], [2_1_5, 7_9_1, 2_7_4, 8_0_4], [1_4_3, 8_2_6, 2_0_4, 8_3_8], [2_1_5, 8_2_6, 2_4_0, 8_3_8], [1_4_2, 8_4_4, 2_0_2, 8_5_7], [2_1_5, 8_4_7, 2_7_4, 8_5_9], [3_3_4, 5_7, 4_2_7, 6_9], [4_4_0, 5_7, 5_2_2, 6_9], [3_6_9, 7_5, 4_6_1, 8_8], [4_6_9, 7_5, 5_1_6, 8_8], [5_2_8, 7_6, 5_6_2, 8_8], [5_7_0, 7_6, 6_6_7, 8_8], [6_7_5, 7_5, 7_1_1, 8_7], [7_2_1, 7_9, 7_7_8, 8_8], [7_8_9, 7_5, 8_4_0, 8_8], [3_6_9, 9_7, 4_7_0, 1_0_7], [4_8_4, 9_4, 5_0_7, 1_0_6], [5_1_8, 9_4, 5_6_2, 1_0_7], [5_7_6, 9_4, 6_5_5, 1_1_0], [6_6_8, 9_4, 7_9_2, 1_0_9], [8_0_4, 9_5, 8_2_9, 1_0_7], [3_6_9, 1_1_3, 4_6_5, 1_2_5], [4_7_7, 1_1_6, 5_4_7, 1_2_5], [5_6_2, 1_1_3, 6_5_8, 1_2_5], [6_7_1, 1_1_6, 7_4_8, 1_2_5], [7_6_1, 1_1_3, 8_1_1, 1_2_5], [3_6_9, 1_3_1, 4_6_5, 1_4_3], [4_7_7, 1_3_3, 5_4_8, 1_4_3], [5_6_3, 1_3_0, 6_9_8, 1_4_5], [7_1_0, 1_3_0, 8_0_2, 1_4_6], [3_3_6, 1_7_1, 4_1_2, 1_8_3], [4_2_3, 1_7_1, 5_7_2, 1_8_3], [5_8_2, 1_7_0, 7_1_6, 1_8_4], [7_2_8, 1_7_1, 8_1_7, 1_8_7], [8_2_9, 1_7_1, 8_4_4, 1_8_6], [3_3_8, 1_9_7, 4_8_2, 2_1_2], [5_0_7, 1_9_6, 5_5_7, 2_0_9], [5_6_9, 1_9_6, 5_9_5, 2_0_8], [6_1_0, 1_9_6, 7_0_2, 2_0_9], [5_0_5, 2_1_4, 5_8_3, 2_2_6], [5_9_5, 2_1_4, 6_5_6, 2_2_7], [6_7_0, 2_1_5, 8_0_7, 2_2_7], [3_3_5, 2_5_9, 5_4_3, 2_7_4], [5_5_6, 2_5_9, 7_0_8, 2_7_2], [3_7_2, 2_7_9, 4_2_2, 2_9_1], [4_3_5, 2_7_9, 4_6_0, 2_9_1], [4_7_4, 2_7_9, 5_7_4, 2_9_2], [5_8_7, 2_7_8, 6_6_4, 2_9_1], [6_7_6, 2_7_8, 7_3_8, 2_9_1], [7_5_1, 2_7_9, 8_3_4, 2_9_1], [3_7_2, 2_9_8, 4_3_4, 3_1_0], [3_3_5, 3_4_1, 4_8_3, 3_5_4], [4_9_7, 3_4_1, 6_5_5, 3_5_4], [6_6_7, 3_4_1, 7_2_8, 3_5_4], [7_4_0, 3_4_1, 8_2_5, 3_5_4], [3_3_5, 3_6_0, 4_3_0, 3_7_2], [4_4_2, 3_6_0, 5_3_4, 3_7_2], [5_4_5, 3_5_9, 6_8_7, 3_7_2], [6_9_7, 3_6_0, 7_5_4, 3_7_2], [7_6_5, 3_6_0, 8_2_3, 3_7_3], [3_3_4, 3_7_8, 4_2_8, 3_9_1], [4_4_0, 3_7_8, 5_7_7, 3_9_4], [5_9_0, 3_7_8, 7_0_5, 3_9_1], [7_2_0, 3_7_8, 8_0_1, 3_9_1], [3_3_4, 3_9_7, 4_0_0, 4_0_9], [3_7_0, 4_1_6, 5_2_9, 4_2_9], [5_4_4, 4_1_6, 5_7_6, 4_3_2], [5_8_7, 4_1_6, 6_6_5, 4_2_8], [6_7_7, 4_1_6, 8_1_4, 4_2_9], [3_7_2, 4_3_5, 4_5_2, 4_5_0], [4_6_5, 4_3_4, 4_9_5, 4_4_7], [5_1_1, 4_3_4, 6_0_0, 4_4_7], [6_1_1, 4_3_6, 6_3_7, 4_4_7], [6_4_9, 4_3_6, 6_9_4, 4_5_1], [7_0_5, 4_3_8, 8_2_4, 4_4_7], [3_6_9, 4_5_3, 4_5_2, 4_6_6], [4_6_4, 4_5_4, 5_0_9, 4_6_6], [5_2_2, 4_5_3, 6_1_1, 4_6_9], [6_2_5, 4_5_3, 7_9_2, 4_6_9], [3_7_0, 4_7_2, 5_5_6, 4_8_8], [5_7_0, 4_7_2, 6_8_4, 4_8_7], [6_9_7, 4_7_2, 7_1_8, 4_8_5], [7_3_2, 4_7_2, 8_3_5, 4_8_8], [3_6_9, 4_9_0, 4_1_1, 5_0_3], [4_2_5, 4_9_0, 4_8_4, 5_0_3], [4_9_6, 4_9_0, 6_3_5, 5_0_6], [6_4_5, 4_9_0, 7_0_7, 5_0_3], [7_1_8, 4_9_1, 7_6_1, 5_0_3], [7_7_1, 4_9_0, 8_4_0, 5_0_3], [3_3_6, 5_1_0, 3_7_4, 5_2_1], [3_8_8, 5_1_0, 4_4_7, 5_2_2], [4_6_0, 5_1_0, 4_8_9, 5_2_1], [5_0_3, 5_1_0, 5_8_0, 5_2_2], [5_9_2, 5_0_9, 7_3_6, 5_2_5], [7_4_5, 5_0_9, 7_7_0, 5_2_2], [7_8_1, 5_0_9, 8_4_0, 5_2_2], [3_3_8, 5_2_8, 4_3_4, 5_4_1], [4_4_8, 5_2_8, 5_9_6, 5_4_1], [6_0_9, 5_2_7, 6_8_7, 5_4_0], [7_0_0, 5_2_8, 7_9_2, 5_4_1], [3_3_6, 5_4_6, 3_9_7, 5_5_9], [4_0_7, 5_4_6, 4_3_1, 5_5_9], [4_4_3, 5_4_6, 5_2_5, 5_6_0], [5_3_7, 5_4_6, 6_8_0, 5_6_2], [6_8_8, 5_4_6, 7_1_4, 5_5_9], [7_2_2, 5_4_6, 8_3_7, 5_6_2], [3_3_6, 5_6_5, 4_4_9, 5_8_1], [4_6_1, 5_6_5, 4_8_5, 5_7_7], [4_9_7, 5_6_5, 6_6_5, 5_8_1], [6_8_1, 5_6_5, 7_1_8, 5_7_7], [7_3_2, 5_6_5, 8_3_7, 5_8_0], [3_3_7, 5_8_4, 4_3_8, 5_9_7], [4_5_2, 5_8_3, 5_2_1, 5_9_6], [5_3_5, 5_8_4, 6_7_7, 5_9_9], [6_9_0, 5_8_3, 7_8_7, 5_9_6], [8_0_1, 5_8_3, 8_2_5, 5_9_6], [3_3_8, 6_0_2, 4_7_8, 6_1_5], [4_9_2, 6_0_2, 5_3_0, 6_1_4], [5_4_3, 6_0_2, 6_3_8, 6_1_5], [6_5_0, 6_0_2, 6_7_6, 6_1_4], [6_8_8, 6_0_2, 7_8_8, 6_1_5], [8_0_2, 6_0_2, 8_4_3, 6_1_4], [3_3_7, 6_2_1, 5_0_2, 6_3_3], [5_1_6, 6_2_1, 6_1_5, 6_3_7], [6_2_9, 6_2_1, 7_7_4, 6_3_6], [7_8_9, 6_2_1, 8_2_7, 6_3_3], [3_3_7, 6_3_9, 4_1_8, 6_5_2], [4_3_2, 6_4_0, 5_7_1, 6_5_3], [5_8_7, 6_3_9, 7_3_1, 6_5_5], [7_4_3, 6_3_9, 7_6_9, 6_5_2], [7_8_0, 6_3_9, 8_4_1, 6_5_2], [3_3_8, 6_5_8, 4_4_0, 6_7_3], [4_5_5, 6_5_8, 4_9_1, 6_7_0], [5_0_8, 6_5_8, 6_0_2, 6_7_1], [6_1_6, 6_5_8, 6_3_8, 6_7_0], [6_5_4, 6_5_8, 8_3_5, 6_7_4], [3_3_7, 6_7_7, 4_2_9, 6_8_9], [3_3_7, 7_1_4, 4_8_2, 7_2_6], [4_9_5, 7_1_4, 5_4_8, 7_2_6], [5_6_1, 7_1_4, 6_8_3, 7_2_6], [3_3_8, 7_7_0, 4_6_1, 7_8_2], [4_7_4, 7_6_9, 5_5_4, 7_8_5], [4_8_9, 7_8_8, 5_6_2, 8_0_3], [5_7_6, 7_8_8, 6_4_3, 8_0_1], [6_5_6, 7_8_7, 7_5_1, 8_0_4], [7_6_4, 7_8_8, 8_4_4, 8_0_1], [3_3_4, 8_2_5, 4_2_1, 8_3_8], [4_3_0, 8_2_4, 5_7_4, 8_3_8], [5_8_4, 8_2_4, 7_2_3, 8_4_1], [3_3_5, 8_4_4, 4_5_0, 8_5_7], [4_6_4, 8_4_3, 5_8_3, 8_6_0], [6_2_8, 8_6_2, 7_5_5, 8_7_5], [7_6_9, 8_6_1, 8_4_8, 8_7_8]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words , __UpperCAmelCase ) self.assertListEqual(encoding.boxes , __UpperCAmelCase ) # with apply_OCR = False lowerCAmelCase__ :int = LayoutLMvaImageProcessor(apply_ocr=__UpperCAmelCase ) lowerCAmelCase__ :Optional[int] = image_processing(__UpperCAmelCase , return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 2_2_4, 2_2_4) )
293
1
"""simple docstring""" def UpperCAmelCase ( a_ ): '''simple docstring''' return number & 1 == 0 if __name__ == "__main__": import doctest doctest.testmod()
352
"""simple docstring""" import numpy as np def UpperCAmelCase ( a_, a_, a_ = 1E-12, a_ = 100, ): '''simple docstring''' assert np.shape(a_ )[0] == np.shape(a_ )[1] # Ensure proper dimensionality. assert np.shape(a_ )[0] == np.shape(a_ )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(a_ ) == np.iscomplexobj(a_ ) lowerCamelCase : Optional[int] = np.iscomplexobj(a_ ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(a_, input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. lowerCamelCase : Union[str, Any] = False lowerCamelCase : List[str] = 0 lowerCamelCase : Any = 0 lowerCamelCase : Dict = 1E12 while not convergence: # Multiple matrix by the vector. lowerCamelCase : Optional[int] = np.dot(a_, a_ ) # Normalize the resulting output vector. lowerCamelCase : Optional[int] = w / np.linalg.norm(a_ ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) lowerCamelCase : Optional[Any] = vector.conj().T if is_complex else vector.T lowerCamelCase : str = np.dot(a_, np.dot(a_, a_ ) ) # Check convergence. lowerCamelCase : Optional[int] = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: lowerCamelCase : int = True lowerCamelCase : Optional[Any] = lambda_ if is_complex: lowerCamelCase : Any = np.real(lambda_ ) return lambda_, vector def UpperCAmelCase ( ): '''simple docstring''' lowerCamelCase : str = np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]] ) lowerCamelCase : str = np.array([41, 4, 20] ) lowerCamelCase : Optional[Any] = real_input_matrix.astype(np.complexaaa ) lowerCamelCase : Dict = np.triu(1j * complex_input_matrix, 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T lowerCamelCase : List[Any] = np.array([41, 4, 20] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": lowerCamelCase : str = real_input_matrix lowerCamelCase : Any = real_vector elif problem_type == "complex": lowerCamelCase : str = complex_input_matrix lowerCamelCase : Dict = complex_vector # Our implementation. lowerCamelCase , lowerCamelCase : List[str] = power_iteration(a_, a_ ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). lowerCamelCase , lowerCamelCase : Optional[Any] = np.linalg.eigh(a_ ) # Last eigenvalue is the maximum one. lowerCamelCase : Dict = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. lowerCamelCase : List[str] = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1E-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(a_ ) - np.abs(a_ ) ) <= 1E-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
205
0
import logging import os import random import sys from dataclasses import dataclass, field from typing import Optional import datasets import evaluate import numpy as np from datasets import load_dataset import transformers from transformers import ( AutoConfig, AutoModelForSequenceClassification, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.31.0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/text-classification/requirements.txt") _snake_case = logging.getLogger(__name__) @dataclass class UpperCAmelCase_ : lowerCamelCase__ = field( default=128 , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) lowerCamelCase__ = field( default=a , metadata={'help': 'Overwrite the cached preprocessed datasets or not.'}) lowerCamelCase__ = field( default=a , 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.' ) } , ) lowerCamelCase__ = field( default=a , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) lowerCamelCase__ = field( default=a , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) } , ) lowerCamelCase__ = field( default=a , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of prediction examples to this ' 'value if set.' ) } , ) @dataclass class UpperCAmelCase_ : lowerCamelCase__ = field( default=a , metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'}) lowerCamelCase__ = field( default=a , metadata={'help': 'Evaluation language. Also train language if `train_language` is set to None.'}) lowerCamelCase__ = field( default=a , metadata={'help': 'Train language if it is different from the evaluation language.'}) lowerCamelCase__ = field( default=a , metadata={'help': 'Pretrained config name or path if not the same as model_name'}) lowerCamelCase__ = field( default=a , metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'}) lowerCamelCase__ = field( default=a , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} , ) lowerCamelCase__ = field( default=a , metadata={'help': 'arg to indicate if tokenizer should do lower case in AutoTokenizer.from_pretrained()'} , ) lowerCamelCase__ = field( default=a , metadata={'help': 'Whether to use one of the fast tokenizer (backed by the tokenizers library) or not.'} , ) lowerCamelCase__ = field( default='main' , metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'} , ) lowerCamelCase__ = field( default=a , metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) } , ) lowerCamelCase__ = field( default=a , metadata={'help': 'Will enable to load a pretrained model whose head dimensions are different.'} , ) def A ( ): '''simple docstring''' _lowerCAmelCase : int = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : int = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry("run_xnli" , _lowerCamelCase ) # 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 )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() _lowerCAmelCase : str = training_args.get_process_log_level() logger.setLevel(_lowerCamelCase ) datasets.utils.logging.set_verbosity(_lowerCamelCase ) transformers.utils.logging.set_verbosity(_lowerCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # 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}" ) logger.info(F"Training/evaluation parameters {training_args}" ) # Detecting last checkpoint. _lowerCAmelCase : Optional[Any] = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: _lowerCAmelCase : Tuple = 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." ) # Set seed before initializing model. set_seed(training_args.seed ) # In distributed training, the load_dataset function guarantees that only one local process can concurrently # download the dataset. # Downloading and loading xnli dataset from the hub. if training_args.do_train: if model_args.train_language is None: _lowerCAmelCase : List[Any] = load_dataset( "xnli" , model_args.language , split="train" , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) else: _lowerCAmelCase : Union[str, Any] = load_dataset( "xnli" , model_args.train_language , split="train" , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) _lowerCAmelCase : Optional[Any] = train_dataset.features["label"].names if training_args.do_eval: _lowerCAmelCase : int = load_dataset( "xnli" , model_args.language , split="validation" , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) _lowerCAmelCase : Dict = eval_dataset.features["label"].names if training_args.do_predict: _lowerCAmelCase : Optional[Any] = load_dataset( "xnli" , model_args.language , split="test" , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) _lowerCAmelCase : List[str] = predict_dataset.features["label"].names # Labels _lowerCAmelCase : List[Any] = len(_lowerCamelCase ) # Load pretrained model and tokenizer # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. _lowerCAmelCase : str = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=_lowerCamelCase , idalabel={str(_lowerCamelCase ): label for i, label in enumerate(_lowerCamelCase )} , labelaid={label: i for i, label in enumerate(_lowerCamelCase )} , finetuning_task="xnli" , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) _lowerCAmelCase : Optional[int] = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , do_lower_case=model_args.do_lower_case , 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 , ) _lowerCAmelCase : Any = AutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool(".ckpt" in model_args.model_name_or_path ) , config=_lowerCamelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ignore_mismatched_sizes=model_args.ignore_mismatched_sizes , ) # Preprocessing the datasets # Padding strategy if data_args.pad_to_max_length: _lowerCAmelCase : List[Any] = "max_length" else: # We will pad later, dynamically at batch creation, to the max sequence length in each batch _lowerCAmelCase : Optional[Any] = False def preprocess_function(_lowerCamelCase ): # Tokenize the texts return tokenizer( examples["premise"] , examples["hypothesis"] , padding=_lowerCamelCase , max_length=data_args.max_seq_length , truncation=_lowerCamelCase , ) if training_args.do_train: if data_args.max_train_samples is not None: _lowerCAmelCase : Any = min(len(_lowerCamelCase ) , data_args.max_train_samples ) _lowerCAmelCase : Optional[Any] = train_dataset.select(range(_lowerCamelCase ) ) with training_args.main_process_first(desc="train dataset map pre-processing" ): _lowerCAmelCase : Dict = train_dataset.map( _lowerCamelCase , batched=_lowerCamelCase , load_from_cache_file=not data_args.overwrite_cache , desc="Running tokenizer on train dataset" , ) # Log a few random samples from the training set: for index in random.sample(range(len(_lowerCamelCase ) ) , 3 ): logger.info(F"Sample {index} of the training set: {train_dataset[index]}." ) if training_args.do_eval: if data_args.max_eval_samples is not None: _lowerCAmelCase : Union[str, Any] = min(len(_lowerCamelCase ) , data_args.max_eval_samples ) _lowerCAmelCase : Union[str, Any] = eval_dataset.select(range(_lowerCamelCase ) ) with training_args.main_process_first(desc="validation dataset map pre-processing" ): _lowerCAmelCase : Tuple = eval_dataset.map( _lowerCamelCase , batched=_lowerCamelCase , load_from_cache_file=not data_args.overwrite_cache , desc="Running tokenizer on validation dataset" , ) if training_args.do_predict: if data_args.max_predict_samples is not None: _lowerCAmelCase : List[Any] = min(len(_lowerCamelCase ) , data_args.max_predict_samples ) _lowerCAmelCase : List[str] = predict_dataset.select(range(_lowerCamelCase ) ) with training_args.main_process_first(desc="prediction dataset map pre-processing" ): _lowerCAmelCase : Optional[int] = predict_dataset.map( _lowerCamelCase , batched=_lowerCamelCase , load_from_cache_file=not data_args.overwrite_cache , desc="Running tokenizer on prediction dataset" , ) # Get the metric function _lowerCAmelCase : List[str] = evaluate.load("xnli" ) # You can define your custom compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(_lowerCamelCase ): _lowerCAmelCase : Optional[Any] = p.predictions[0] if isinstance(p.predictions , _lowerCamelCase ) else p.predictions _lowerCAmelCase : Dict = np.argmax(_lowerCamelCase , axis=1 ) return metric.compute(predictions=_lowerCamelCase , references=p.label_ids ) # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding. if data_args.pad_to_max_length: _lowerCAmelCase : int = default_data_collator elif training_args.fpaa: _lowerCAmelCase : List[str] = DataCollatorWithPadding(_lowerCamelCase , pad_to_multiple_of=8 ) else: _lowerCAmelCase : Union[str, Any] = None # Initialize our Trainer _lowerCAmelCase : Dict = Trainer( model=_lowerCamelCase , args=_lowerCamelCase , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , compute_metrics=_lowerCamelCase , tokenizer=_lowerCamelCase , data_collator=_lowerCamelCase , ) # Training if training_args.do_train: _lowerCAmelCase : Union[str, Any] = None if training_args.resume_from_checkpoint is not None: _lowerCAmelCase : List[Any] = training_args.resume_from_checkpoint elif last_checkpoint is not None: _lowerCAmelCase : int = last_checkpoint _lowerCAmelCase : Any = trainer.train(resume_from_checkpoint=_lowerCamelCase ) _lowerCAmelCase : List[str] = train_result.metrics _lowerCAmelCase : Dict = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(_lowerCamelCase ) ) _lowerCAmelCase : Tuple = min(_lowerCamelCase , len(_lowerCamelCase ) ) trainer.save_model() # Saves the tokenizer too for easy upload trainer.log_metrics("train" , _lowerCamelCase ) trainer.save_metrics("train" , _lowerCamelCase ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info("*** Evaluate ***" ) _lowerCAmelCase : Optional[int] = trainer.evaluate(eval_dataset=_lowerCamelCase ) _lowerCAmelCase : Dict = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(_lowerCamelCase ) _lowerCAmelCase : Union[str, Any] = min(_lowerCamelCase , len(_lowerCamelCase ) ) trainer.log_metrics("eval" , _lowerCamelCase ) trainer.save_metrics("eval" , _lowerCamelCase ) # Prediction if training_args.do_predict: logger.info("*** Predict ***" ) _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : Union[str, Any] = trainer.predict(_lowerCamelCase , metric_key_prefix="predict" ) _lowerCAmelCase : str = ( data_args.max_predict_samples if data_args.max_predict_samples is not None else len(_lowerCamelCase ) ) _lowerCAmelCase : int = min(_lowerCamelCase , len(_lowerCamelCase ) ) trainer.log_metrics("predict" , _lowerCamelCase ) trainer.save_metrics("predict" , _lowerCamelCase ) _lowerCAmelCase : str = np.argmax(_lowerCamelCase , axis=1 ) _lowerCAmelCase : Any = os.path.join(training_args.output_dir , "predictions.txt" ) if trainer.is_world_process_zero(): with open(_lowerCamelCase , "w" ) as writer: writer.write("index\tprediction\n" ) for index, item in enumerate(_lowerCamelCase ): _lowerCAmelCase : str = label_list[item] writer.write(F"{index}\t{item}\n" ) if __name__ == "__main__": main()
36
'''simple docstring''' lowercase__ : Dict = { '''Pillow''': '''Pillow''', '''accelerate''': '''accelerate>=0.11.0''', '''compel''': '''compel==0.1.8''', '''black''': '''black~=23.1''', '''datasets''': '''datasets''', '''filelock''': '''filelock''', '''flax''': '''flax>=0.4.1''', '''hf-doc-builder''': '''hf-doc-builder>=0.3.0''', '''huggingface-hub''': '''huggingface-hub>=0.13.2''', '''requests-mock''': '''requests-mock==1.10.0''', '''importlib_metadata''': '''importlib_metadata''', '''invisible-watermark''': '''invisible-watermark''', '''isort''': '''isort>=5.5.4''', '''jax''': '''jax>=0.2.8,!=0.3.2''', '''jaxlib''': '''jaxlib>=0.1.65''', '''Jinja2''': '''Jinja2''', '''k-diffusion''': '''k-diffusion>=0.0.12''', '''torchsde''': '''torchsde''', '''note_seq''': '''note_seq''', '''librosa''': '''librosa''', '''numpy''': '''numpy''', '''omegaconf''': '''omegaconf''', '''parameterized''': '''parameterized''', '''protobuf''': '''protobuf>=3.20.3,<4''', '''pytest''': '''pytest''', '''pytest-timeout''': '''pytest-timeout''', '''pytest-xdist''': '''pytest-xdist''', '''ruff''': '''ruff>=0.0.241''', '''safetensors''': '''safetensors''', '''sentencepiece''': '''sentencepiece>=0.1.91,!=0.1.92''', '''scipy''': '''scipy''', '''onnx''': '''onnx''', '''regex''': '''regex!=2019.12.17''', '''requests''': '''requests''', '''tensorboard''': '''tensorboard''', '''torch''': '''torch>=1.4''', '''torchvision''': '''torchvision''', '''transformers''': '''transformers>=4.25.1''', '''urllib3''': '''urllib3<=2.0.0''', }
190
0
'''simple docstring''' import os from typing import Dict, List, Union import tensorflow as tf from keras_nlp.tokenizers import BytePairTokenizer from tensorflow_text import pad_model_inputs from .tokenization_gpta import GPTaTokenizer class __UpperCamelCase ( tf.keras.layers.Layer ): def __init__( self , __a , __a , __a = None , __a = None ): '''simple docstring''' super().__init__() __a : Any = pad_token_id __a : Any = max_length __a : Tuple = vocab __a : int = merges __a : Union[str, Any] = BytePairTokenizer(_a , _a , sequence_length=_a ) @classmethod def __UpperCAmelCase ( cls , __a , *__a , **__a ): '''simple docstring''' __a : List[str] = [' '.join(_a ) for m in tokenizer.bpe_ranks.keys()] __a : Optional[int] = tokenizer.get_vocab() return cls(_a , _a , *_a , **_a ) @classmethod def __UpperCAmelCase ( cls , __a , *__a , **__a ): '''simple docstring''' __a : Any = GPTaTokenizer.from_pretrained(_a , *_a , **_a ) return cls.from_tokenizer(_a , *_a , **_a ) @classmethod def __UpperCAmelCase ( cls , __a ): '''simple docstring''' return cls(**_a ) def __UpperCAmelCase ( self ): '''simple docstring''' return { "vocab": self.vocab, "merges": self.merges, "max_length": self.max_length, "pad_token_id": self.pad_token_id, } def __UpperCAmelCase ( self , __a , __a = None ): '''simple docstring''' __a : List[str] = self.tf_tokenizer(_a ) __a : Union[str, Any] = tf.ones_like(_a ) if self.pad_token_id is not None: # pad the tokens up to max length __a : Union[str, Any] = max_length if max_length is not None else self.max_length if max_length is not None: __a , __a : Any = pad_model_inputs( _a , max_seq_length=_a , pad_value=self.pad_token_id ) return {"attention_mask": attention_mask, "input_ids": input_ids}
350
'''simple docstring''' from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig __lowercase : List[Any] = { 'susnato/ernie-m-base_pytorch': 'https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json', 'susnato/ernie-m-large_pytorch': 'https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json', } class __UpperCamelCase ( lowerCAmelCase_ ): A_ = "ernie_m" A_ = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self , __a = 25_0002 , __a = 768 , __a = 12 , __a = 12 , __a = 3072 , __a = "gelu" , __a = 0.1 , __a = 0.1 , __a = 514 , __a = 0.02 , __a = 1 , __a = 1E-0_5 , __a=None , __a=False , __a=0.0 , **__a , ): '''simple docstring''' super().__init__(pad_token_id=__a , **__a ) __a : int = vocab_size __a : Dict = hidden_size __a : str = num_hidden_layers __a : Dict = num_attention_heads __a : List[str] = intermediate_size __a : Union[str, Any] = hidden_act __a : List[Any] = hidden_dropout_prob __a : str = attention_probs_dropout_prob __a : Any = max_position_embeddings __a : int = initializer_range __a : Dict = layer_norm_eps __a : int = classifier_dropout __a : Dict = is_decoder __a : int = act_dropout
294
0
"""simple docstring""" import re import string from collections import Counter import sacrebleu import sacremoses from packaging import version import datasets UpperCAmelCase_ : Optional[int] = """ @inproceedings{xu-etal-2016-optimizing, title = {Optimizing Statistical Machine Translation for Text Simplification}, authors={Xu, Wei and Napoles, Courtney and Pavlick, Ellie and Chen, Quanze and Callison-Burch, Chris}, journal = {Transactions of the Association for Computational Linguistics}, volume = {4}, year={2016}, url = {https://www.aclweb.org/anthology/Q16-1029}, pages = {401--415 }, @inproceedings{post-2018-call, title = \"A Call for Clarity in Reporting {BLEU} Scores\", author = \"Post, Matt\", booktitle = \"Proceedings of the Third Conference on Machine Translation: Research Papers\", month = oct, year = \"2018\", address = \"Belgium, Brussels\", publisher = \"Association for Computational Linguistics\", url = \"https://www.aclweb.org/anthology/W18-6319\", pages = \"186--191\", } """ UpperCAmelCase_ : Tuple = """\ WIKI_SPLIT is the combination of three metrics SARI, EXACT and SACREBLEU It can be used to evaluate the quality of machine-generated texts. """ UpperCAmelCase_ : Tuple = """ Calculates sari score (between 0 and 100) given a list of source and predicted sentences, and a list of lists of reference sentences. It also computes the BLEU score as well as the exact match score. Args: sources: list of source sentences where each sentence should be a string. predictions: list of predicted sentences where each sentence should be a string. references: list of lists of reference sentences where each sentence should be a string. Returns: sari: sari score sacrebleu: sacrebleu score exact: exact score Examples: >>> sources=[\"About 95 species are currently accepted .\"] >>> predictions=[\"About 95 you now get in .\"] >>> references=[[\"About 95 species are currently known .\"]] >>> wiki_split = datasets.load_metric(\"wiki_split\") >>> results = wiki_split.compute(sources=sources, predictions=predictions, references=references) >>> print(results) {'sari': 21.805555555555557, 'sacrebleu': 14.535768424205482, 'exact': 0.0} """ def _A (__a ) -> List[str]: """simple docstring""" def remove_articles(__a ): SCREAMING_SNAKE_CASE_ : List[Any] = re.compile(R'''\b(a|an|the)\b''' , re.UNICODE ) return re.sub(__a , ''' ''' , __a ) def white_space_fix(__a ): return " ".join(text.split() ) def remove_punc(__a ): SCREAMING_SNAKE_CASE_ : Tuple = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__a ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__a ) ) ) ) def _A (__a , __a ) -> Optional[Any]: """simple docstring""" return int(normalize_answer(__a ) == normalize_answer(__a ) ) def _A (__a , __a ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = [any(compute_exact(__a , __a ) for ref in refs ) for pred, refs in zip(__a , __a )] return (sum(__a ) / len(__a )) * 1_00 def _A (__a , __a , __a , __a ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = [rgram for rgrams in rgramslist for rgram in rgrams] SCREAMING_SNAKE_CASE_ : Tuple = Counter(__a ) SCREAMING_SNAKE_CASE_ : Optional[int] = Counter(__a ) SCREAMING_SNAKE_CASE_ : Tuple = Counter() for sgram, scount in sgramcounter.items(): SCREAMING_SNAKE_CASE_ : Optional[Any] = scount * numref SCREAMING_SNAKE_CASE_ : Any = Counter(__a ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = Counter() for cgram, ccount in cgramcounter.items(): SCREAMING_SNAKE_CASE_ : int = ccount * numref # KEEP SCREAMING_SNAKE_CASE_ : Any = sgramcounter_rep & cgramcounter_rep SCREAMING_SNAKE_CASE_ : int = keepgramcounter_rep & rgramcounter SCREAMING_SNAKE_CASE_ : Dict = sgramcounter_rep & rgramcounter SCREAMING_SNAKE_CASE_ : List[str] = 0 SCREAMING_SNAKE_CASE_ : int = 0 for keepgram in keepgramcountergood_rep: keeptmpscorea += keepgramcountergood_rep[keepgram] / keepgramcounter_rep[keepgram] # Fix an alleged bug [2] in the keep score computation. # keeptmpscore2 += keepgramcountergood_rep[keepgram] / keepgramcounterall_rep[keepgram] keeptmpscorea += keepgramcountergood_rep[keepgram] # Define 0/0=1 instead of 0 to give higher scores for predictions that match # a target exactly. SCREAMING_SNAKE_CASE_ : Tuple = 1 SCREAMING_SNAKE_CASE_ : Optional[int] = 1 if len(__a ) > 0: SCREAMING_SNAKE_CASE_ : Optional[Any] = keeptmpscorea / len(__a ) if len(__a ) > 0: # Fix an alleged bug [2] in the keep score computation. # keepscore_recall = keeptmpscore2 / len(keepgramcounterall_rep) SCREAMING_SNAKE_CASE_ : int = keeptmpscorea / sum(keepgramcounterall_rep.values() ) SCREAMING_SNAKE_CASE_ : Any = 0 if keepscore_precision > 0 or keepscore_recall > 0: SCREAMING_SNAKE_CASE_ : Dict = 2 * keepscore_precision * keepscore_recall / (keepscore_precision + keepscore_recall) # DELETION SCREAMING_SNAKE_CASE_ : Tuple = sgramcounter_rep - cgramcounter_rep SCREAMING_SNAKE_CASE_ : Dict = delgramcounter_rep - rgramcounter SCREAMING_SNAKE_CASE_ : Optional[Any] = sgramcounter_rep - rgramcounter SCREAMING_SNAKE_CASE_ : List[str] = 0 SCREAMING_SNAKE_CASE_ : Dict = 0 for delgram in delgramcountergood_rep: deltmpscorea += delgramcountergood_rep[delgram] / delgramcounter_rep[delgram] deltmpscorea += delgramcountergood_rep[delgram] / delgramcounterall_rep[delgram] # Define 0/0=1 instead of 0 to give higher scores for predictions that match # a target exactly. SCREAMING_SNAKE_CASE_ : List[Any] = 1 if len(__a ) > 0: SCREAMING_SNAKE_CASE_ : List[Any] = deltmpscorea / len(__a ) # ADDITION SCREAMING_SNAKE_CASE_ : List[str] = set(__a ) - set(__a ) SCREAMING_SNAKE_CASE_ : Tuple = set(__a ) & set(__a ) SCREAMING_SNAKE_CASE_ : List[Any] = set(__a ) - set(__a ) SCREAMING_SNAKE_CASE_ : List[Any] = 0 for addgram in addgramcountergood: addtmpscore += 1 # Define 0/0=1 instead of 0 to give higher scores for predictions that match # a target exactly. SCREAMING_SNAKE_CASE_ : Dict = 1 SCREAMING_SNAKE_CASE_ : int = 1 if len(__a ) > 0: SCREAMING_SNAKE_CASE_ : Optional[int] = addtmpscore / len(__a ) if len(__a ) > 0: SCREAMING_SNAKE_CASE_ : Optional[int] = addtmpscore / len(__a ) SCREAMING_SNAKE_CASE_ : List[Any] = 0 if addscore_precision > 0 or addscore_recall > 0: SCREAMING_SNAKE_CASE_ : Optional[int] = 2 * addscore_precision * addscore_recall / (addscore_precision + addscore_recall) return (keepscore, delscore_precision, addscore) def _A (__a , __a , __a ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : int = len(__a ) SCREAMING_SNAKE_CASE_ : Optional[Any] = ssent.split(''' ''' ) SCREAMING_SNAKE_CASE_ : Tuple = csent.split(''' ''' ) SCREAMING_SNAKE_CASE_ : List[str] = [] SCREAMING_SNAKE_CASE_ : List[Any] = [] SCREAMING_SNAKE_CASE_ : List[str] = [] SCREAMING_SNAKE_CASE_ : str = [] SCREAMING_SNAKE_CASE_ : Optional[Any] = [] SCREAMING_SNAKE_CASE_ : int = [] SCREAMING_SNAKE_CASE_ : int = [] SCREAMING_SNAKE_CASE_ : Tuple = [] SCREAMING_SNAKE_CASE_ : Optional[Any] = [] SCREAMING_SNAKE_CASE_ : str = [] for rsent in rsents: SCREAMING_SNAKE_CASE_ : Optional[Any] = rsent.split(''' ''' ) SCREAMING_SNAKE_CASE_ : Dict = [] SCREAMING_SNAKE_CASE_ : int = [] SCREAMING_SNAKE_CASE_ : Any = [] ragramslist.append(__a ) for i in range(0 , len(__a ) - 1 ): if i < len(__a ) - 1: SCREAMING_SNAKE_CASE_ : List[Any] = ragrams[i] + ''' ''' + ragrams[i + 1] ragrams.append(__a ) if i < len(__a ) - 2: SCREAMING_SNAKE_CASE_ : Optional[Any] = ragrams[i] + ''' ''' + ragrams[i + 1] + ''' ''' + ragrams[i + 2] ragrams.append(__a ) if i < len(__a ) - 3: SCREAMING_SNAKE_CASE_ : Optional[Any] = ragrams[i] + ''' ''' + ragrams[i + 1] + ''' ''' + ragrams[i + 2] + ''' ''' + ragrams[i + 3] ragrams.append(__a ) ragramslist.append(__a ) ragramslist.append(__a ) ragramslist.append(__a ) for i in range(0 , len(__a ) - 1 ): if i < len(__a ) - 1: SCREAMING_SNAKE_CASE_ : Dict = sagrams[i] + ''' ''' + sagrams[i + 1] sagrams.append(__a ) if i < len(__a ) - 2: SCREAMING_SNAKE_CASE_ : List[str] = sagrams[i] + ''' ''' + sagrams[i + 1] + ''' ''' + sagrams[i + 2] sagrams.append(__a ) if i < len(__a ) - 3: SCREAMING_SNAKE_CASE_ : Tuple = sagrams[i] + ''' ''' + sagrams[i + 1] + ''' ''' + sagrams[i + 2] + ''' ''' + sagrams[i + 3] sagrams.append(__a ) for i in range(0 , len(__a ) - 1 ): if i < len(__a ) - 1: SCREAMING_SNAKE_CASE_ : Tuple = cagrams[i] + ''' ''' + cagrams[i + 1] cagrams.append(__a ) if i < len(__a ) - 2: SCREAMING_SNAKE_CASE_ : Tuple = cagrams[i] + ''' ''' + cagrams[i + 1] + ''' ''' + cagrams[i + 2] cagrams.append(__a ) if i < len(__a ) - 3: SCREAMING_SNAKE_CASE_ : int = cagrams[i] + ''' ''' + cagrams[i + 1] + ''' ''' + cagrams[i + 2] + ''' ''' + cagrams[i + 3] cagrams.append(__a ) ((SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_)) : Optional[int] = SARIngram(__a , __a , __a , __a ) ((SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_)) : Union[str, Any] = SARIngram(__a , __a , __a , __a ) ((SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_)) : Union[str, Any] = SARIngram(__a , __a , __a , __a ) ((SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_)) : List[str] = SARIngram(__a , __a , __a , __a ) SCREAMING_SNAKE_CASE_ : int = sum([keepascore, keepascore, keepascore, keepascore] ) / 4 SCREAMING_SNAKE_CASE_ : Union[str, Any] = sum([delascore, delascore, delascore, delascore] ) / 4 SCREAMING_SNAKE_CASE_ : List[Any] = sum([addascore, addascore, addascore, addascore] ) / 4 SCREAMING_SNAKE_CASE_ : str = (avgkeepscore + avgdelscore + avgaddscore) / 3 return finalscore def _A (__a , __a = True , __a = "13a" , __a = True ) -> Optional[int]: """simple docstring""" if lowercase: SCREAMING_SNAKE_CASE_ : Union[str, Any] = sentence.lower() if tokenizer in ["13a", "intl"]: if version.parse(sacrebleu.__version__ ).major >= 2: SCREAMING_SNAKE_CASE_ : Dict = sacrebleu.metrics.bleu._get_tokenizer(__a )()(__a ) else: SCREAMING_SNAKE_CASE_ : List[Any] = sacrebleu.TOKENIZERS[tokenizer]()(__a ) elif tokenizer == "moses": SCREAMING_SNAKE_CASE_ : Optional[Any] = sacremoses.MosesTokenizer().tokenize(__a , return_str=__a , escape=__a ) elif tokenizer == "penn": SCREAMING_SNAKE_CASE_ : str = sacremoses.MosesTokenizer().penn_tokenize(__a , return_str=__a ) else: SCREAMING_SNAKE_CASE_ : str = sentence if not return_str: SCREAMING_SNAKE_CASE_ : str = normalized_sent.split() return normalized_sent def _A (__a , __a , __a ) -> int: """simple docstring""" if not (len(__a ) == len(__a ) == len(__a )): raise ValueError('''Sources length must match predictions and references lengths.''' ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = 0 for src, pred, refs in zip(__a , __a , __a ): sari_score += SARIsent(normalize(__a ) , normalize(__a ) , [normalize(__a ) for sent in refs] ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = sari_score / len(__a ) return 1_00 * sari_score def _A (__a , __a , __a="exp" , __a=None , __a=False , __a=False , __a=False , ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : int = len(references[0] ) if any(len(__a ) != references_per_prediction for refs in references ): raise ValueError('''Sacrebleu requires the same number of references for each prediction''' ) SCREAMING_SNAKE_CASE_ : Optional[int] = [[refs[i] for refs in references] for i in range(__a )] SCREAMING_SNAKE_CASE_ : List[str] = sacrebleu.corpus_bleu( __a , __a , smooth_method=__a , smooth_value=__a , force=__a , lowercase=__a , use_effective_order=__a , ) return output.score @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCAmelCase__ ( datasets.Metric ): '''simple docstring''' def _SCREAMING_SNAKE_CASE ( self : Optional[int]): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence'''), '''references''': datasets.Sequence(datasets.Value('''string''' , id='''sequence''') , id='''references'''), }) , codebase_urls=[ '''https://github.com/huggingface/transformers/blob/master/src/transformers/data/metrics/squad_metrics.py''', '''https://github.com/cocoxu/simplification/blob/master/SARI.py''', '''https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/utils/sari_hook.py''', '''https://github.com/mjpost/sacreBLEU''', ] , reference_urls=[ '''https://www.aclweb.org/anthology/Q16-1029.pdf''', '''https://github.com/mjpost/sacreBLEU''', '''https://en.wikipedia.org/wiki/BLEU''', '''https://towardsdatascience.com/evaluating-text-output-in-nlp-bleu-at-your-own-risk-e8609665a213''', ] , ) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowercase_ : Dict , lowercase_ : List[Any] , lowercase_ : List[Any]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : int = {} result.update({'''sari''': compute_sari(sources=lowercase_ , predictions=lowercase_ , references=lowercase_)}) result.update({'''sacrebleu''': compute_sacrebleu(predictions=lowercase_ , references=lowercase_)}) result.update({'''exact''': compute_em(predictions=lowercase_ , references=lowercase_)}) return result
91
"""simple docstring""" from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available from ...utils import OptionalDependencyNotAvailable UpperCAmelCase_ : str = {"""configuration_gpt_neox""": ["""GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTNeoXConfig"""]} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : Dict = ["""GPTNeoXTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : List[str] = [ """GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST""", """GPTNeoXForCausalLM""", """GPTNeoXForQuestionAnswering""", """GPTNeoXForSequenceClassification""", """GPTNeoXForTokenClassification""", """GPTNeoXLayer""", """GPTNeoXModel""", """GPTNeoXPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_gpt_neox import GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_neox_fast import GPTNeoXTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox import ( GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, GPTNeoXLayer, GPTNeoXModel, GPTNeoXPreTrainedModel, ) else: import sys UpperCAmelCase_ : Tuple = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
91
1
"""simple docstring""" import doctest import glob import importlib import inspect import os import re from contextlib import contextmanager from functools import wraps from unittest.mock import patch import numpy as np import pytest from absl.testing import parameterized import datasets from datasets import load_metric from .utils import for_all_test_methods, local, slow # mark all tests as integration __A : List[str] = pytest.mark.integration __A : List[Any] = {'''comet'''} __A : List[str] = importlib.util.find_spec('''fairseq''') is not None __A : Dict = {'''code_eval'''} __A : List[Any] = os.name == '''nt''' __A : Tuple = {'''bertscore''', '''frugalscore''', '''perplexity'''} __A : Union[str, Any] = importlib.util.find_spec('''transformers''') is not None def A_ ( snake_case_ : Optional[int] ): '''simple docstring''' @wraps(snake_case_ ) def wrapper(self : Union[str, Any] ,snake_case_ : List[Any] ): if not _has_fairseq and metric_name in REQUIRE_FAIRSEQ: self.skipTest("""\"test requires Fairseq\"""" ) else: test_case(self ,snake_case_ ) return wrapper def A_ ( snake_case_ : Dict ): '''simple docstring''' @wraps(snake_case_ ) def wrapper(self : Optional[int] ,snake_case_ : Optional[Any] ): if not _has_transformers and metric_name in REQUIRE_TRANSFORMERS: self.skipTest("""\"test requires transformers\"""" ) else: test_case(self ,snake_case_ ) return wrapper def A_ ( snake_case_ : Union[str, Any] ): '''simple docstring''' @wraps(snake_case_ ) def wrapper(self : Optional[Any] ,snake_case_ : Dict ): if _on_windows and metric_name in UNSUPPORTED_ON_WINDOWS: self.skipTest("""\"test not supported on Windows\"""" ) else: test_case(self ,snake_case_ ) return wrapper def A_ ( ): '''simple docstring''' UpperCamelCase : Optional[Any] = [metric_dir.split(os.sep )[-2] for metric_dir in glob.glob("""./metrics/*/""" )] return [{"testcase_name": x, "metric_name": x} for x in metrics if x != "gleu"] # gleu is unfinished @parameterized.named_parameters(get_local_metric_names() ) @for_all_test_methods( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) @local class lowerCamelCase ( parameterized.TestCase ): lowercase : List[str] = {} lowercase : Optional[Any] = None @pytest.mark.filterwarnings("""ignore:metric_module_factory is deprecated:FutureWarning""" ) @pytest.mark.filterwarnings("""ignore:load_metric is deprecated:FutureWarning""" ) def a_ ( self , SCREAMING_SNAKE_CASE_ ): UpperCamelCase : Dict = """[...]""" UpperCamelCase : List[str] = importlib.import_module( datasets.load.metric_module_factory(os.path.join("""metrics""" , SCREAMING_SNAKE_CASE_ ) ).module_path ) UpperCamelCase : int = datasets.load.import_main_class(metric_module.__name__ , dataset=SCREAMING_SNAKE_CASE_ ) # check parameters UpperCamelCase : int = inspect.signature(metric._compute ).parameters self.assertTrue(all(p.kind != p.VAR_KEYWORD for p in parameters.values() ) ) # no **kwargs # run doctest with self.patch_intensive_calls(SCREAMING_SNAKE_CASE_ , metric_module.__name__ ): with self.use_local_metrics(): try: UpperCamelCase : int = doctest.testmod(SCREAMING_SNAKE_CASE_ , verbose=SCREAMING_SNAKE_CASE_ , raise_on_error=SCREAMING_SNAKE_CASE_ ) except doctest.UnexpectedException as e: raise e.exc_info[1] # raise the exception that doctest caught self.assertEqual(results.failed , 0 ) self.assertGreater(results.attempted , 1 ) @slow def a_ ( self , SCREAMING_SNAKE_CASE_ ): UpperCamelCase : int = """[...]""" UpperCamelCase : int = importlib.import_module( datasets.load.metric_module_factory(os.path.join("""metrics""" , SCREAMING_SNAKE_CASE_ ) ).module_path ) # run doctest with self.use_local_metrics(): UpperCamelCase : str = doctest.testmod(SCREAMING_SNAKE_CASE_ , verbose=SCREAMING_SNAKE_CASE_ , raise_on_error=SCREAMING_SNAKE_CASE_ ) self.assertEqual(results.failed , 0 ) self.assertGreater(results.attempted , 1 ) @contextmanager def a_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): if metric_name in self.INTENSIVE_CALLS_PATCHER: with self.INTENSIVE_CALLS_PATCHER[metric_name](SCREAMING_SNAKE_CASE_ ): yield else: yield @contextmanager def a_ ( self ): def load_local_metric(SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ): return load_metric(os.path.join("""metrics""" , SCREAMING_SNAKE_CASE_ ) , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) with patch("""datasets.load_metric""" ) as mock_load_metric: UpperCamelCase : Any = load_local_metric yield @classmethod def a_ ( cls , SCREAMING_SNAKE_CASE_ ): def wrapper(SCREAMING_SNAKE_CASE_ ): UpperCamelCase : List[str] = contextmanager(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Any = patcher return patcher return wrapper @LocalMetricTest.register_intensive_calls_patcher("""bleurt""" ) def A_ ( snake_case_ : Optional[Any] ): '''simple docstring''' import tensorflow.compat.va as tf from bleurt.score import Predictor tf.flags.DEFINE_string("""sv""" ,"""""" ,"""""" ) # handle pytest cli flags class lowerCamelCase ( _UpperCAmelCase ): def a_ ( self , SCREAMING_SNAKE_CASE_ ): assert len(input_dict["""input_ids"""] ) == 2 return np.array([1.03, 1.04] ) # mock predict_fn which is supposed to do a forward pass with a bleurt model with patch("""bleurt.score._create_predictor""" ) as mock_create_predictor: UpperCamelCase : Tuple = MockedPredictor() yield @LocalMetricTest.register_intensive_calls_patcher("""bertscore""" ) def A_ ( snake_case_ : Any ): '''simple docstring''' import torch def bert_cos_score_idf(snake_case_ : List[Any] ,snake_case_ : int ,*snake_case_ : List[Any] ,**snake_case_ : Tuple ): return torch.tensor([[1.0, 1.0, 1.0]] * len(snake_case_ ) ) # mock get_model which is supposed to do download a bert model # mock bert_cos_score_idf which is supposed to do a forward pass with a bert model with patch("""bert_score.scorer.get_model""" ), patch( """bert_score.scorer.bert_cos_score_idf""" ) as mock_bert_cos_score_idf: UpperCamelCase : Optional[int] = bert_cos_score_idf yield @LocalMetricTest.register_intensive_calls_patcher("""comet""" ) def A_ ( snake_case_ : Dict ): '''simple docstring''' def load_from_checkpoint(snake_case_ : List[str] ): class lowerCamelCase : def a_ ( self , SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ): assert len(SCREAMING_SNAKE_CASE_ ) == 2 UpperCamelCase : Dict = [0.19, 0.92] return scores, sum(SCREAMING_SNAKE_CASE_ ) / len(SCREAMING_SNAKE_CASE_ ) return Model() # mock load_from_checkpoint which is supposed to do download a bert model # mock load_from_checkpoint which is supposed to do download a bert model with patch("""comet.download_model""" ) as mock_download_model: UpperCamelCase : Dict = None with patch("""comet.load_from_checkpoint""" ) as mock_load_from_checkpoint: UpperCamelCase : Tuple = load_from_checkpoint yield def A_ ( ): '''simple docstring''' UpperCamelCase : List[str] = load_metric(os.path.join("""metrics""" ,"""seqeval""" ) ) UpperCamelCase : List[Any] = """ERROR""" UpperCamelCase : Union[str, Any] = f'Scheme should be one of [IOB1, IOB2, IOE1, IOE2, IOBES, BILOU], got {wrong_scheme}' with pytest.raises(snake_case_ ,match=re.escape(snake_case_ ) ): metric.compute(predictions=[] ,references=[] ,scheme=snake_case_ )
356
"""simple docstring""" import argparse import re from typing import Dict import torch from datasets import Audio, Dataset, load_dataset, load_metric from transformers import AutoFeatureExtractor, pipeline def A_ ( snake_case_ : Dataset ,snake_case_ : Dict[str, str] ): '''simple docstring''' UpperCamelCase : List[str] = args.log_outputs UpperCamelCase : Tuple = """_""".join(args.dataset.split("""/""" ) + [args.config, args.split] ) # load metric UpperCamelCase : List[Any] = load_metric("""wer""" ) UpperCamelCase : Any = load_metric("""cer""" ) # compute metrics UpperCamelCase : str = wer.compute(references=result["""target"""] ,predictions=result["""prediction"""] ) UpperCamelCase : Dict = cer.compute(references=result["""target"""] ,predictions=result["""prediction"""] ) # print & log results UpperCamelCase : Optional[int] = f'WER: {wer_result}\nCER: {cer_result}' print(snake_case_ ) with open(f'{dataset_id}_eval_results.txt' ,"""w""" ) as f: f.write(snake_case_ ) # log all results in text file. Possibly interesting for analysis if log_outputs is not None: UpperCamelCase : Optional[Any] = f'log_{dataset_id}_predictions.txt' UpperCamelCase : str = f'log_{dataset_id}_targets.txt' with open(snake_case_ ,"""w""" ) as p, open(snake_case_ ,"""w""" ) as t: # mapping function to write output def write_to_file(snake_case_ : Union[str, Any] ,snake_case_ : Tuple ): p.write(f'{i}' + """\n""" ) p.write(batch["""prediction"""] + """\n""" ) t.write(f'{i}' + """\n""" ) t.write(batch["""target"""] + """\n""" ) result.map(snake_case_ ,with_indices=snake_case_ ) def A_ ( snake_case_ : str ): '''simple docstring''' UpperCamelCase : Dict = """[,?.!\-\;\:\"“%‘”�—’…–]""" # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training UpperCamelCase : str = re.sub(snake_case_ ,"""""" ,text.lower() ) # In addition, we can normalize the target text, e.g. removing new lines characters etc... # note that order is important here! UpperCamelCase : List[str] = ["""\n\n""", """\n""", """ """, """ """] for t in token_sequences_to_ignore: UpperCamelCase : Tuple = """ """.join(text.split(snake_case_ ) ) return text def A_ ( snake_case_ : str ): '''simple docstring''' # load dataset UpperCamelCase : Union[str, Any] = load_dataset(args.dataset ,args.config ,split=args.split ,use_auth_token=snake_case_ ) # for testing: only process the first two examples as a test # dataset = dataset.select(range(10)) # load processor UpperCamelCase : List[Any] = AutoFeatureExtractor.from_pretrained(args.model_id ) UpperCamelCase : Dict = feature_extractor.sampling_rate # resample audio UpperCamelCase : Optional[Any] = dataset.cast_column("""audio""" ,Audio(sampling_rate=snake_case_ ) ) # load eval pipeline if args.device is None: UpperCamelCase : int = 0 if torch.cuda.is_available() else -1 UpperCamelCase : Union[str, Any] = pipeline("""automatic-speech-recognition""" ,model=args.model_id ,device=args.device ) # map function to decode audio def map_to_pred(snake_case_ : Union[str, Any] ): UpperCamelCase : List[Any] = asr( batch["""audio"""]["""array"""] ,chunk_length_s=args.chunk_length_s ,stride_length_s=args.stride_length_s ) UpperCamelCase : Union[str, Any] = prediction["""text"""] UpperCamelCase : Optional[Any] = normalize_text(batch["""sentence"""] ) return batch # run inference on all examples UpperCamelCase : Any = dataset.map(snake_case_ ,remove_columns=dataset.column_names ) # compute and log_results # do not change function below log_results(snake_case_ ,snake_case_ ) if __name__ == "__main__": __A : List[str] = argparse.ArgumentParser() parser.add_argument( '''--model_id''', type=str, required=True, help='''Model identifier. Should be loadable with 🤗 Transformers''' ) parser.add_argument( '''--dataset''', type=str, required=True, help='''Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets''', ) parser.add_argument( '''--config''', type=str, required=True, help='''Config of the dataset. *E.g.* `\'en\'` for Common Voice''' ) parser.add_argument('''--split''', type=str, required=True, help='''Split of the dataset. *E.g.* `\'test\'`''') parser.add_argument( '''--chunk_length_s''', type=float, default=None, help='''Chunk length in seconds. Defaults to 5 seconds.''' ) parser.add_argument( '''--stride_length_s''', type=float, default=None, help='''Stride of the audio chunks. Defaults to 1 second.''' ) parser.add_argument( '''--log_outputs''', action='''store_true''', help='''If defined, write outputs to log file for analysis.''' ) parser.add_argument( '''--device''', type=int, default=None, help='''The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.''', ) __A : Optional[Any] = parser.parse_args() main(args)
27
0
import numpy as np import torch from torch.utils.data import Dataset from utils import logger class snake_case__ ( lowerCAmelCase_ ): """simple docstring""" def __init__( self : Tuple, _snake_case : str, _snake_case : Union[str, Any] ) ->Tuple: snake_case__ : Union[str, Any] = params snake_case__ : Optional[Any] = np.array(_snake_case ) snake_case__ : Tuple = np.array([len(_snake_case ) for t in data] ) self.check() self.remove_long_sequences() self.remove_empty_sequences() self.remove_unknown_sequences() self.check() self.print_statistics() def __getitem__( self : Tuple, _snake_case : List[str] ) ->str: return (self.token_ids[index], self.lengths[index]) def __len__( self : Union[str, Any] ) ->str: return len(self.lengths ) def lowercase_ ( self : int ) ->Optional[int]: assert len(self.token_ids ) == len(self.lengths ) assert all(self.lengths[i] == len(self.token_ids[i] ) for i in range(len(self.lengths ) ) ) def lowercase_ ( self : Dict ) ->Optional[int]: snake_case__ : Dict = self.params.max_model_input_size snake_case__ : int = self.lengths > max_len logger.info(F'''Splitting {sum(_snake_case )} too long sequences.''' ) def divide_chunks(_snake_case : str, _snake_case : Any ): return [l[i : i + n] for i in range(0, len(_snake_case ), _snake_case )] snake_case__ : Dict = [] snake_case__ : Any = [] if self.params.mlm: snake_case__ , snake_case__ : str = self.params.special_tok_ids['cls_token'], self.params.special_tok_ids['sep_token'] else: snake_case__ , snake_case__ : Dict = self.params.special_tok_ids['bos_token'], self.params.special_tok_ids['eos_token'] for seq_, len_ in zip(self.token_ids, self.lengths ): assert (seq_[0] == cls_id) and (seq_[-1] == sep_id), seq_ if len_ <= max_len: new_tok_ids.append(seq_ ) new_lengths.append(len_ ) else: snake_case__ : int = [] for sub_s in divide_chunks(seq_, max_len - 2 ): if sub_s[0] != cls_id: snake_case__ : List[Any] = np.insert(_snake_case, 0, _snake_case ) if sub_s[-1] != sep_id: snake_case__ : Optional[Any] = np.insert(_snake_case, len(_snake_case ), _snake_case ) assert len(_snake_case ) <= max_len assert (sub_s[0] == cls_id) and (sub_s[-1] == sep_id), sub_s sub_seqs.append(_snake_case ) new_tok_ids.extend(_snake_case ) new_lengths.extend([len(_snake_case ) for l in sub_seqs] ) snake_case__ : int = np.array(_snake_case ) snake_case__ : Union[str, Any] = np.array(_snake_case ) def lowercase_ ( self : Optional[int] ) ->Optional[int]: snake_case__ : int = len(self ) snake_case__ : Dict = self.lengths > 1_1 snake_case__ : List[Any] = self.token_ids[indices] snake_case__ : Any = self.lengths[indices] snake_case__ : Dict = len(self ) logger.info(F'''Remove {init_size - new_size} too short (<=11 tokens) sequences.''' ) def lowercase_ ( self : List[str] ) ->Tuple: if "unk_token" not in self.params.special_tok_ids: return else: snake_case__ : str = self.params.special_tok_ids['unk_token'] snake_case__ : str = len(self ) snake_case__ : Any = np.array([np.count_nonzero(a == unk_token_id ) for a in self.token_ids] ) snake_case__ : Any = (unk_occs / self.lengths) < 0.5 snake_case__ : Optional[int] = self.token_ids[indices] snake_case__ : List[str] = self.lengths[indices] snake_case__ : List[Any] = len(self ) logger.info(F'''Remove {init_size - new_size} sequences with a high level of unknown tokens (50%).''' ) def lowercase_ ( self : List[str] ) ->Optional[int]: if not self.params.is_master: return logger.info(F'''{len(self )} sequences''' ) # data_len = sum(self.lengths) # nb_unique_tokens = len(Counter(list(chain(*self.token_ids)))) # logger.info(f'{data_len} tokens ({nb_unique_tokens} unique)') # unk_idx = self.params.special_tok_ids['unk_token'] # nb_unknown = sum([(t==unk_idx).sum() for t in self.token_ids]) # logger.info(f'{nb_unknown} unknown tokens (covering {100*nb_unknown/data_len:.2f}% of the data)') def lowercase_ ( self : Optional[int], _snake_case : int ) ->List[Any]: snake_case__ : Any = [t[0] for t in batch] snake_case__ : Optional[Any] = [t[1] for t in batch] assert len(_snake_case ) == len(_snake_case ) # Max for paddings snake_case__ : Tuple = max(_snake_case ) # Pad token ids if self.params.mlm: snake_case__ : Dict = self.params.special_tok_ids['pad_token'] else: snake_case__ : List[Any] = self.params.special_tok_ids['unk_token'] snake_case__ : Union[str, Any] = [list(t.astype(_snake_case ) ) + [pad_idx] * (max_seq_len_ - len(_snake_case )) for t in token_ids] assert len(tk_ ) == len(_snake_case ) assert all(len(_snake_case ) == max_seq_len_ for t in tk_ ) snake_case__ : int = torch.tensor(tk_ ) # (bs, max_seq_len_) snake_case__ : Optional[int] = torch.tensor(_snake_case ) # (bs) return tk_t, lg_t
277
from collections import UserDict from typing import Union import numpy as np import requests from ..utils import ( add_end_docstrings, logging, ) from .audio_classification import ffmpeg_read from .base import PIPELINE_INIT_ARGS, Pipeline a_ :List[Any] = logging.get_logger(__name__) @add_end_docstrings(lowerCAmelCase_ ) class snake_case__ ( lowerCAmelCase_ ): """simple docstring""" def __init__( self : Optional[Any], **_snake_case : str ) ->Dict: super().__init__(**_snake_case ) if self.framework != "pt": raise ValueError(F'''The {self.__class__} is only available in PyTorch.''' ) # No specific FOR_XXX available yet def __call__( self : Union[str, Any], _snake_case : Union[np.ndarray, bytes, str], **_snake_case : Tuple ) ->Dict: return super().__call__(_snake_case, **_snake_case ) def lowercase_ ( self : Tuple, **_snake_case : Any ) ->Union[str, Any]: snake_case__ : str = {} if "candidate_labels" in kwargs: snake_case__ : str = kwargs['candidate_labels'] if "hypothesis_template" in kwargs: snake_case__ : str = kwargs['hypothesis_template'] return preprocess_params, {}, {} def lowercase_ ( self : Dict, _snake_case : str, _snake_case : Optional[int]=None, _snake_case : List[str]="This is a sound of {}." ) ->int: if isinstance(_snake_case, _snake_case ): if audio.startswith('http://' ) or audio.startswith('https://' ): # We need to actually check for a real protocol, otherwise it's impossible to use a local file # like http_huggingface_co.png snake_case__ : List[Any] = requests.get(_snake_case ).content else: with open(_snake_case, 'rb' ) as f: snake_case__ : Union[str, Any] = f.read() if isinstance(_snake_case, _snake_case ): snake_case__ : List[Any] = ffmpeg_read(_snake_case, self.feature_extractor.sampling_rate ) if not isinstance(_snake_case, np.ndarray ): raise ValueError('We expect a numpy ndarray as input' ) if len(audio.shape ) != 1: raise ValueError('We expect a single channel audio input for ZeroShotAudioClassificationPipeline' ) snake_case__ : Tuple = self.feature_extractor( [audio], sampling_rate=self.feature_extractor.sampling_rate, return_tensors='pt' ) snake_case__ : int = candidate_labels snake_case__ : int = [hypothesis_template.format(_snake_case ) for x in candidate_labels] snake_case__ : Optional[int] = self.tokenizer(_snake_case, return_tensors=self.framework, padding=_snake_case ) snake_case__ : List[Any] = [text_inputs] return inputs def lowercase_ ( self : Optional[int], _snake_case : Optional[Any] ) ->int: snake_case__ : Optional[int] = model_inputs.pop('candidate_labels' ) snake_case__ : str = model_inputs.pop('text_inputs' ) if isinstance(text_inputs[0], _snake_case ): snake_case__ : Optional[Any] = text_inputs[0] else: # Batching case. snake_case__ : int = text_inputs[0][0] snake_case__ : Any = self.model(**_snake_case, **_snake_case ) snake_case__ : List[Any] = { 'candidate_labels': candidate_labels, 'logits': outputs.logits_per_audio, } return model_outputs def lowercase_ ( self : Union[str, Any], _snake_case : str ) ->List[str]: snake_case__ : int = model_outputs.pop('candidate_labels' ) snake_case__ : List[Any] = model_outputs['logits'][0] if self.framework == "pt": snake_case__ : Tuple = logits.softmax(dim=0 ) snake_case__ : Union[str, Any] = probs.tolist() else: raise ValueError('`tf` framework not supported.' ) snake_case__ : Union[str, Any] = [ {'score': score, 'label': candidate_label} for score, candidate_label in sorted(zip(_snake_case, _snake_case ), key=lambda _snake_case : -x[0] ) ] return result
277
1
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { '''google/bigbird-roberta-base''': '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/config.json''', '''google/bigbird-roberta-large''': '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/config.json''', '''google/bigbird-base-trivia-itc''': '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/config.json''', # See all BigBird models at https://huggingface.co/models?filter=big_bird } class _UpperCAmelCase ( lowerCAmelCase ): '''simple docstring''' __A = '''big_bird''' def __init__( self : Tuple , lowercase_ : Dict=50358 , lowercase_ : List[Any]=768 , lowercase_ : Union[str, Any]=12 , lowercase_ : List[str]=12 , lowercase_ : Optional[Any]=3072 , lowercase_ : List[str]="gelu_new" , lowercase_ : List[Any]=0.1 , lowercase_ : str=0.1 , lowercase_ : List[str]=4096 , lowercase_ : str=2 , lowercase_ : Optional[int]=0.02 , lowercase_ : Dict=1e-1_2 , lowercase_ : Tuple=True , lowercase_ : Union[str, Any]=0 , lowercase_ : List[str]=1 , lowercase_ : Optional[int]=2 , lowercase_ : List[Any]=66 , lowercase_ : List[Any]="block_sparse" , lowercase_ : Optional[Any]=True , lowercase_ : Any=False , lowercase_ : Dict=64 , lowercase_ : List[Any]=3 , lowercase_ : Tuple=None , **lowercase_ : Optional[int] , ) -> Tuple: """simple docstring""" super().__init__( pad_token_id=lowercase_ , bos_token_id=lowercase_ , eos_token_id=lowercase_ , sep_token_id=lowercase_ , **lowercase_ , ) _UpperCamelCase = vocab_size _UpperCamelCase = max_position_embeddings _UpperCamelCase = hidden_size _UpperCamelCase = num_hidden_layers _UpperCamelCase = num_attention_heads _UpperCamelCase = intermediate_size _UpperCamelCase = hidden_act _UpperCamelCase = hidden_dropout_prob _UpperCamelCase = attention_probs_dropout_prob _UpperCamelCase = initializer_range _UpperCamelCase = type_vocab_size _UpperCamelCase = layer_norm_eps _UpperCamelCase = use_cache _UpperCamelCase = rescale_embeddings _UpperCamelCase = attention_type _UpperCamelCase = use_bias _UpperCamelCase = block_size _UpperCamelCase = num_random_blocks _UpperCamelCase = classifier_dropout class _UpperCAmelCase ( lowerCAmelCase ): '''simple docstring''' @property def __UpperCAmelCase ( self : Union[str, Any]) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": _UpperCamelCase = {0: "batch", 1: "choice", 2: "sequence"} else: _UpperCamelCase = {0: "batch", 1: "sequence"} return OrderedDict( [ ("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ])
63
from copy import deepcopy import torch import torch.nn.functional as F from torch.optim import AdamW from torch.optim.lr_scheduler import LambdaLR from torch.utils.data import DataLoader from accelerate.accelerator import Accelerator from accelerate.state import GradientState from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import DistributedType, is_torch_version, set_seed def lowerCAmelCase__ ( a__ , a__ , a__ , a__ ) ->Optional[int]: '''simple docstring''' for param, grad_param in zip(model_a.parameters() , model_b.parameters() ): if not param.requires_grad: continue if not did_step: # Grads should not be in sync assert ( torch.allclose(param.grad , grad_param.grad ) is False ), f'Gradients in sync when they should not be at iteration {iteration}:\nmodel_a grad ({param.grad}) == model_b grad ({grad_param.grad})' else: # Grads should be in sync assert ( torch.allclose(param.grad , grad_param.grad ) is True ), f'Gradients not in sync when they should be at iteration {iteration}:\nmodel_a grad ({param.grad}) != model_b grad ({grad_param.grad})' def lowerCAmelCase__ ( a__ , a__ , a__ , a__ , a__=True ) ->List[str]: '''simple docstring''' model.train() _UpperCamelCase = model(a__ ) _UpperCamelCase = F.mse_loss(a__ , target.to(output.device ) ) if not do_backward: loss /= accelerator.gradient_accumulation_steps loss.backward() else: accelerator.backward(a__ ) def lowerCAmelCase__ ( a__ , a__=False ) ->Union[str, Any]: '''simple docstring''' set_seed(42 ) _UpperCamelCase = RegressionModel() _UpperCamelCase = deepcopy(a__ ) _UpperCamelCase = RegressionDataset(length=80 ) _UpperCamelCase = DataLoader(a__ , batch_size=16 ) model.to(accelerator.device ) if sched: _UpperCamelCase = AdamW(params=model.parameters() , lr=1e-3 ) _UpperCamelCase = AdamW(params=ddp_model.parameters() , lr=1e-3 ) _UpperCamelCase = LambdaLR(a__ , lr_lambda=lambda a__ : epoch**0.65 ) _UpperCamelCase = LambdaLR(a__ , lr_lambda=lambda a__ : epoch**0.65 ) # Make a copy of `model` if sched: _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = accelerator.prepare(a__ , a__ , a__ , a__ ) else: _UpperCamelCase , _UpperCamelCase = accelerator.prepare(a__ , a__ ) if sched: return (model, opt, sched, dataloader, ddp_model, ddp_opt, ddp_sched) return model, ddp_model, dataloader def lowerCAmelCase__ ( a__ ) ->List[Any]: '''simple docstring''' _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = get_training_setup(a__ ) # Use a single batch _UpperCamelCase , _UpperCamelCase = next(iter(a__ ) ).values() for iteration in range(3 ): # Gather the distributed inputs and targs for the base model _UpperCamelCase , _UpperCamelCase = accelerator.gather((ddp_input, ddp_target) ) _UpperCamelCase , _UpperCamelCase = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(a__ , a__ , a__ , a__ ) # Do "gradient accumulation" (noop) if iteration % 2 == 0: # Accumulate grads locally with accelerator.no_sync(a__ ): step_model(a__ , a__ , a__ , a__ ) else: # Sync grads step_model(a__ , a__ , a__ , a__ ) # Since `no_sync` is a noop, `ddp_model` and `model` grads should always be in sync check_model_parameters(a__ , a__ , a__ , a__ ) for param, ddp_param in zip(model.parameters() , ddp_model.parameters() ): if not param.requires_grad: continue assert torch.allclose( param.grad , ddp_param.grad ), f'Gradients not in sync when they should be:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})' # Shuffle ddp_input on each iteration torch.manual_seed(1_337 + iteration ) _UpperCamelCase = ddp_input[torch.randperm(len(a__ ) )] def lowerCAmelCase__ ( a__ ) ->str: '''simple docstring''' _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = get_training_setup(a__ ) # Use a single batch _UpperCamelCase , _UpperCamelCase = next(iter(a__ ) ).values() for iteration in range(3 ): # Gather the distributed inputs and targs for the base model _UpperCamelCase , _UpperCamelCase = accelerator.gather((ddp_input, ddp_target) ) _UpperCamelCase , _UpperCamelCase = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(a__ , a__ , a__ , a__ ) # Do "gradient accumulation" (noop) if iteration % 2 == 0: # Accumulate grads locally with accelerator.no_sync(a__ ): step_model(a__ , a__ , a__ , a__ ) else: # Sync grads step_model(a__ , a__ , a__ , a__ ) # DDP model and model should only be in sync when not (iteration % 2 == 0) for param, ddp_param in zip(model.parameters() , ddp_model.parameters() ): if not param.requires_grad: continue if iteration % 2 == 0: # Grads should not be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is False ), f'Gradients in sync when they should not be:\nModel grad ({param.grad}) == DDP grad ({ddp_param.grad})' else: # Grads should be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is True ), f'Gradients not in sync when they should be:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})' # Shuffle ddp_input on each iteration torch.manual_seed(1_337 + iteration ) _UpperCamelCase = ddp_input[torch.randperm(len(a__ ) )] def lowerCAmelCase__ ( a__=False , a__=False ) ->List[Any]: '''simple docstring''' _UpperCamelCase = Accelerator( split_batches=a__ , dispatch_batches=a__ , gradient_accumulation_steps=2 ) # Test that context manager behaves properly _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = get_training_setup(a__ ) for iteration, batch in enumerate(a__ ): _UpperCamelCase , _UpperCamelCase = batch.values() # Gather the distributed inputs and targs for the base model _UpperCamelCase , _UpperCamelCase = accelerator.gather((ddp_input, ddp_target) ) _UpperCamelCase , _UpperCamelCase = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(a__ , a__ , a__ , a__ , a__ ) # Do "gradient accumulation" (noop) with accelerator.accumulate(a__ ): step_model(a__ , a__ , a__ , a__ ) # DDP model and model should only be in sync when not (iteration % 2 == 0) for param, ddp_param in zip(model.parameters() , ddp_model.parameters() ): if not param.requires_grad: continue if ((iteration + 1) % 2 == 0) or (iteration == len(a__ ) - 1): # Grads should be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is True ), f'Gradients not in sync when they should be at iteration {iteration}:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})' else: # Grads should not be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is False ), f'Gradients in sync when they should not be at iteration {iteration}:\nModel grad ({param.grad}) == DDP grad ({ddp_param.grad})' # Shuffle ddp_input on each iteration torch.manual_seed(1_337 + iteration ) _UpperCamelCase = ddp_input[torch.randperm(len(a__ ) )] GradientState._reset_state() def lowerCAmelCase__ ( a__=False , a__=False ) ->Dict: '''simple docstring''' _UpperCamelCase = Accelerator( split_batches=a__ , dispatch_batches=a__ , gradient_accumulation_steps=2 ) # Test that context manager behaves properly _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = get_training_setup(a__ , a__ ) for iteration, batch in enumerate(a__ ): _UpperCamelCase , _UpperCamelCase = batch.values() # Gather the distributed inputs and targs for the base model _UpperCamelCase , _UpperCamelCase = accelerator.gather((ddp_input, ddp_target) ) _UpperCamelCase , _UpperCamelCase = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" model.train() ddp_model.train() step_model(a__ , a__ , a__ , a__ , a__ ) opt.step() if ((iteration + 1) % 2 == 0) or ((iteration + 1) == len(a__ )): if split_batches: sched.step() else: for _ in range(accelerator.num_processes ): sched.step() opt.zero_grad() # Perform gradient accumulation under wrapper with accelerator.accumulate(a__ ): step_model(a__ , a__ , a__ , a__ ) ddp_opt.step() ddp_sched.step() ddp_opt.zero_grad() # Learning rates should be the same assert ( opt.param_groups[0]["lr"] == ddp_opt.param_groups[0]["lr"] ), f'Learning rates found in each optimizer did not align\nopt: {opt.param_groups[0]["lr"]}\nDDP opt: {ddp_opt.param_groups[0]["lr"]}\n' _UpperCamelCase = (((iteration + 1) % 2) == 0) or ((iteration + 1) == len(a__ )) if accelerator.num_processes > 1: check_model_parameters(a__ , a__ , a__ , a__ ) # Shuffle ddp_input on each iteration torch.manual_seed(1_337 + iteration ) GradientState._reset_state() def lowerCAmelCase__ ( ) ->Union[str, Any]: '''simple docstring''' _UpperCamelCase = Accelerator() _UpperCamelCase = RegressionDataset(length=80 ) _UpperCamelCase = DataLoader(a__ , batch_size=16 ) _UpperCamelCase = RegressionDataset(length=96 ) _UpperCamelCase = DataLoader(a__ , batch_size=16 ) _UpperCamelCase , _UpperCamelCase = accelerator.prepare(a__ , a__ ) assert accelerator.gradient_state.active_dataloader is None for iteration, _ in enumerate(a__ ): assert id(accelerator.gradient_state.active_dataloader ) == id(a__ ) if iteration < len(a__ ) - 1: assert not accelerator.gradient_state.end_of_dataloader if iteration == 1: for batch_num, _ in enumerate(a__ ): assert id(accelerator.gradient_state.active_dataloader ) == id(a__ ) if batch_num < len(a__ ) - 1: assert not accelerator.gradient_state.end_of_dataloader else: assert accelerator.gradient_state.end_of_dataloader else: assert accelerator.gradient_state.end_of_dataloader assert accelerator.gradient_state.active_dataloader is None def lowerCAmelCase__ ( ) ->int: '''simple docstring''' _UpperCamelCase = Accelerator() _UpperCamelCase = accelerator.state if state.local_process_index == 0: print("**Test `accumulate` gradient accumulation with dataloader break**" ) test_dataloader_break() if state.distributed_type == DistributedType.NO: if state.local_process_index == 0: print("**Test NOOP `no_sync` context manager**" ) test_noop_sync(a__ ) if state.distributed_type in (DistributedType.MULTI_GPU, DistributedType.MULTI_CPU): if state.local_process_index == 0: print("**Test Distributed `no_sync` context manager**" ) test_distributed_sync(a__ ) if state.distributed_type == DistributedType.MULTI_GPU: for split_batch in [True, False]: for dispatch_batches in [True, False]: if state.local_process_index == 0: print( "**Test `accumulate` gradient accumulation, " , f'`split_batches={split_batch}` and `dispatch_batches={dispatch_batches}`**' , ) test_gradient_accumulation(a__ , a__ ) # Currently will break on torch 2.0 +, need to investigate why if is_torch_version("<" , "2.0" ) or state.distributed_type == DistributedType.NO: if state.local_process_index == 0: print( "**Test `accumulate` gradient accumulation with optimizer and scheduler, " , "`split_batches=False`, `dispatch_batches=False`**" , ) test_gradient_accumulation_with_opt_and_scheduler() if state.distributed_type == DistributedType.MULTI_GPU: for split_batch in [True, False]: for dispatch_batches in [True, False]: if not split_batch and not dispatch_batches: continue if state.local_process_index == 0: print( "**Test `accumulate` gradient accumulation with optimizer and scheduler, " , f'`split_batches={split_batch}` and `dispatch_batches={dispatch_batches}`**' , ) test_gradient_accumulation_with_opt_and_scheduler(a__ , a__ ) def lowerCAmelCase__ ( a__ ) ->Tuple: '''simple docstring''' main() if __name__ == "__main__": main()
63
1
"""simple docstring""" import os _snake_case : Dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} def A__ ( UpperCamelCase ): A = 0 A = 0 while index < len(A__ ) - 1: A = SYMBOLS[numerals[index]] A = SYMBOLS[numerals[index + 1]] if current_value < next_value: total_value -= current_value else: total_value += current_value index += 1 total_value += SYMBOLS[numerals[index]] return total_value def A__ ( UpperCamelCase ): A = "" A = num // 1_000 numerals += m_count * "M" num %= 1_000 A = num // 100 if c_count == 9: numerals += "CM" c_count -= 9 elif c_count == 4: numerals += "CD" c_count -= 4 if c_count >= 5: numerals += "D" c_count -= 5 numerals += c_count * "C" num %= 100 A = num // 10 if x_count == 9: numerals += "XC" x_count -= 9 elif x_count == 4: numerals += "XL" x_count -= 4 if x_count >= 5: numerals += "L" x_count -= 5 numerals += x_count * "X" num %= 10 if num == 9: numerals += "IX" num -= 9 elif num == 4: numerals += "IV" num -= 4 if num >= 5: numerals += "V" num -= 5 numerals += num * "I" return numerals def A__ ( UpperCamelCase = "/p089_roman.txt" ): A = 0 with open(os.path.dirname(A__ ) + roman_numerals_filename ) as filea: A = filea.readlines() for line in lines: A = line.strip() A = parse_roman_numerals(A__ ) A = generate_roman_numerals(A__ ) savings += len(A__ ) - len(A__ ) return savings if __name__ == "__main__": print(F"""{solution() = }""")
292
import inspect import tempfile import unittest from huggingface_hub import hf_hub_download from transformers import is_torch_available from transformers.testing_utils import is_flaky, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin lowercase_ = 1e-4 if is_torch_available(): import torch from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder @require_torch class __lowerCAmelCase : def __init__( self , lowerCAmelCase , lowerCAmelCase=16 , lowerCAmelCase=13 , lowerCAmelCase=7 , lowerCAmelCase=14 , lowerCAmelCase=10 , lowerCAmelCase=19 , lowerCAmelCase=5 , lowerCAmelCase=4 , lowerCAmelCase=True , lowerCAmelCase=16 , lowerCAmelCase=2 , lowerCAmelCase=4 , lowerCAmelCase=4 , lowerCAmelCase="gelu" , lowerCAmelCase=0.1 , lowerCAmelCase=0.1 , lowerCAmelCase=[1, 2, 3, 4, 5] , lowerCAmelCase=25 , lowerCAmelCase=5 , ) -> Optional[Any]: '''simple docstring''' _lowercase =d_model _lowercase =parent _lowercase =batch_size _lowercase =prediction_length _lowercase =context_length _lowercase =cardinality _lowercase =num_time_features _lowercase =lags_sequence _lowercase =embedding_dimension _lowercase =is_training _lowercase =hidden_size _lowercase =num_hidden_layers _lowercase =num_attention_heads _lowercase =intermediate_size _lowercase =hidden_act _lowercase =hidden_dropout_prob _lowercase =attention_probs_dropout_prob _lowercase =context_length _lowercase =prediction_length + label_length _lowercase =label_length _lowercase =moving_average _lowercase =autocorrelation_factor def A__ ( self ) -> Optional[Any]: '''simple docstring''' return AutoformerConfig( d_model=self.d_model , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , prediction_length=self.prediction_length , context_length=self.context_length , label_length=self.label_length , lags_sequence=self.lags_sequence , num_time_features=self.num_time_features , num_static_categorical_features=1 , cardinality=[self.cardinality] , embedding_dimension=[self.embedding_dimension] , moving_average=self.moving_average , ) def A__ ( self , lowerCAmelCase ) -> Dict: '''simple docstring''' _lowercase =config.context_length + max(config.lags_sequence ) _lowercase =ids_tensor([self.batch_size, 1] , config.cardinality[0] ) _lowercase =floats_tensor([self.batch_size, _past_length, config.num_time_features] ) _lowercase =floats_tensor([self.batch_size, _past_length] ) _lowercase =floats_tensor([self.batch_size, _past_length] ) > 0.5 # decoder inputs _lowercase =floats_tensor([self.batch_size, config.prediction_length, config.num_time_features] ) _lowercase =floats_tensor([self.batch_size, config.prediction_length] ) _lowercase ={ 'past_values': past_values, 'static_categorical_features': static_categorical_features, 'past_time_features': past_time_features, 'past_observed_mask': past_observed_mask, 'future_time_features': future_time_features, 'future_values': future_values, } return inputs_dict def A__ ( self ) -> List[str]: '''simple docstring''' _lowercase =self.get_config() _lowercase =self.prepare_autoformer_inputs_dict(lowerCAmelCase ) return config, inputs_dict def A__ ( self ) -> List[str]: '''simple docstring''' _lowercase , _lowercase =self.prepare_config_and_inputs() return config, inputs_dict def A__ ( self , lowerCAmelCase , lowerCAmelCase ) -> Tuple: '''simple docstring''' _lowercase =AutoformerModel(config=lowerCAmelCase ).to(lowerCAmelCase ).eval() _lowercase =model(**lowerCAmelCase ) _lowercase =outputs.encoder_last_hidden_state _lowercase =outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: _lowercase =model.get_encoder() encoder.save_pretrained(lowerCAmelCase ) _lowercase =AutoformerEncoder.from_pretrained(lowerCAmelCase ).to(lowerCAmelCase ) _lowercase , _lowercase , _lowercase , _lowercase , _lowercase =model.create_network_inputs(**lowerCAmelCase ) _lowercase , _lowercase =model.decomposition_layer(transformer_inputs[:, : config.context_length, ...] ) _lowercase =torch.cat( (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...]) , dim=-1 , ) _lowercase =encoder(inputs_embeds=lowerCAmelCase )[0] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1e-3 ) _lowercase =( torch.mean(transformer_inputs[:, : config.context_length, ...] , dim=1 ) .unsqueeze(1 ) .repeat(1 , config.prediction_length , 1 ) ) _lowercase =torch.zeros( [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]] , device=enc_input.device , ) _lowercase =torch.cat( ( torch.cat((seasonal_input[:, -config.label_length :, ...], zeros) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) _lowercase =torch.cat( ( torch.cat((trend_input[:, -config.label_length :, ...], mean) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) with tempfile.TemporaryDirectory() as tmpdirname: _lowercase =model.get_decoder() decoder.save_pretrained(lowerCAmelCase ) _lowercase =AutoformerDecoder.from_pretrained(lowerCAmelCase ).to(lowerCAmelCase ) _lowercase =decoder( trend=lowerCAmelCase , inputs_embeds=lowerCAmelCase , encoder_hidden_states=lowerCAmelCase , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1e-3 ) @require_torch class __lowerCAmelCase ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , unittest.TestCase ): _a = (AutoformerModel, AutoformerForPrediction) if is_torch_available() else () _a = (AutoformerForPrediction,) if is_torch_available() else () _a = {"""feature-extraction""": AutoformerModel} if is_torch_available() else {} _a = False _a = False _a = False _a = False _a = False _a = False def A__ ( self ) -> str: '''simple docstring''' _lowercase =AutoformerModelTester(self ) _lowercase =ConfigTester(self , config_class=lowerCAmelCase , has_text_modality=lowerCAmelCase ) def A__ ( self ) -> Optional[Any]: '''simple docstring''' self.config_tester.run_common_tests() def A__ ( self ) -> Optional[Any]: '''simple docstring''' _lowercase , _lowercase =self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: _lowercase =model_class(lowerCAmelCase ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(lowerCAmelCase ) _lowercase , _lowercase =model_class.from_pretrained(lowerCAmelCase , output_loading_info=lowerCAmelCase ) self.assertEqual(info['missing_keys'] , [] ) def A__ ( self ) -> List[str]: '''simple docstring''' _lowercase =self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*lowerCAmelCase ) @unittest.skip(reason='Model has no tokens embeddings' ) def A__ ( self ) -> int: '''simple docstring''' pass def A__ ( self ) -> Dict: '''simple docstring''' _lowercase =inspect.signature(getattr(lowerCAmelCase , 'forward' ) ) # The main input is the name of the argument after `self` _lowercase =list(model_signature.parameters.keys() )[1] self.assertEqual(AutoformerModel.main_input_name , lowerCAmelCase ) def A__ ( self ) -> List[Any]: '''simple docstring''' _lowercase , _lowercase =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowercase =model_class(lowerCAmelCase ) _lowercase =inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _lowercase =[*signature.parameters.keys()] _lowercase =[ 'past_values', 'past_time_features', 'past_observed_mask', 'static_categorical_features', 'static_real_features', 'future_values', 'future_time_features', ] if model.__class__.__name__ in ["AutoformerForPrediction"]: expected_arg_names.append('future_observed_mask' ) expected_arg_names.extend( [ 'decoder_attention_mask', 'head_mask', 'decoder_head_mask', 'cross_attn_head_mask', 'encoder_outputs', 'past_key_values', 'output_hidden_states', 'output_attentions', 'use_cache', 'return_dict', ] ) self.assertListEqual(arg_names[: len(lowerCAmelCase )] , lowerCAmelCase ) def A__ ( self ) -> int: '''simple docstring''' _lowercase , _lowercase =self.model_tester.prepare_config_and_inputs_for_common() _lowercase =True _lowercase =getattr(self.model_tester , 'seq_length' , lowerCAmelCase ) _lowercase =getattr(self.model_tester , 'decoder_seq_length' , lowerCAmelCase ) _lowercase =getattr(self.model_tester , 'encoder_seq_length' , lowerCAmelCase ) _lowercase =getattr(self.model_tester , 'd_model' , lowerCAmelCase ) _lowercase =getattr(self.model_tester , 'num_attention_heads' , lowerCAmelCase ) _lowercase =d_model // num_attention_heads for model_class in self.all_model_classes: _lowercase =True _lowercase =False _lowercase =True _lowercase =model_class(lowerCAmelCase ) model.to(lowerCAmelCase ) model.eval() with torch.no_grad(): _lowercase =model(**self._prepare_for_class(lowerCAmelCase , lowerCAmelCase ) ) _lowercase =outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(lowerCAmelCase ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _lowercase =True _lowercase =model_class(lowerCAmelCase ) model.to(lowerCAmelCase ) model.eval() with torch.no_grad(): _lowercase =model(**self._prepare_for_class(lowerCAmelCase , lowerCAmelCase ) ) _lowercase =outputs.encoder_attentions self.assertEqual(len(lowerCAmelCase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) _lowercase =len(lowerCAmelCase ) _lowercase =7 if "last_hidden_state" in outputs: correct_outlen += 1 if "trend" in outputs: correct_outlen += 1 if "past_key_values" in outputs: correct_outlen += 1 # past_key_values have been returned if "loss" in outputs: correct_outlen += 1 if "params" in outputs: correct_outlen += 1 self.assertEqual(lowerCAmelCase , lowerCAmelCase ) # decoder attentions _lowercase =outputs.decoder_attentions self.assertIsInstance(lowerCAmelCase , (list, tuple) ) self.assertEqual(len(lowerCAmelCase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # cross attentions _lowercase =outputs.cross_attentions self.assertIsInstance(lowerCAmelCase , (list, tuple) ) self.assertEqual(len(lowerCAmelCase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(cross_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # Check attention is always last and order is fine _lowercase =True _lowercase =True _lowercase =model_class(lowerCAmelCase ) model.to(lowerCAmelCase ) model.eval() with torch.no_grad(): _lowercase =model(**self._prepare_for_class(lowerCAmelCase , lowerCAmelCase ) ) self.assertEqual(out_len + 2 , len(lowerCAmelCase ) ) _lowercase =outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(lowerCAmelCase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) @is_flaky() def A__ ( self ) -> Dict: '''simple docstring''' super().test_retain_grad_hidden_states_attentions() def a ( A__ : List[str]="train-batch.pt" ) -> str: """simple docstring""" _lowercase =hf_hub_download(repo_id='hf-internal-testing/tourism-monthly-batch' , filename=A__ , repo_type='dataset' ) _lowercase =torch.load(A__ , map_location=A__ ) return batch @require_torch @slow class __lowerCAmelCase ( unittest.TestCase ): def A__ ( self ) -> int: '''simple docstring''' _lowercase =AutoformerModel.from_pretrained('huggingface/autoformer-tourism-monthly' ).to(lowerCAmelCase ) _lowercase =prepare_batch() with torch.no_grad(): _lowercase =model( past_values=batch['past_values'] , past_time_features=batch['past_time_features'] , past_observed_mask=batch['past_observed_mask'] , static_categorical_features=batch['static_categorical_features'] , future_values=batch['future_values'] , future_time_features=batch['future_time_features'] , )[0] _lowercase =torch.Size( (64, model.config.prediction_length + model.config.label_length, model.config.feature_size) ) self.assertEqual(output.shape , lowerCAmelCase ) _lowercase =torch.tensor( [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]] , device=lowerCAmelCase ) self.assertTrue(torch.allclose(output[0, :3, :3] , lowerCAmelCase , atol=lowerCAmelCase ) ) def A__ ( self ) -> str: '''simple docstring''' _lowercase =AutoformerForPrediction.from_pretrained('huggingface/autoformer-tourism-monthly' ).to(lowerCAmelCase ) _lowercase =prepare_batch('val-batch.pt' ) with torch.no_grad(): _lowercase =model( past_values=batch['past_values'] , past_time_features=batch['past_time_features'] , past_observed_mask=batch['past_observed_mask'] , static_categorical_features=batch['static_categorical_features'] , ).encoder_last_hidden_state _lowercase =torch.Size((64, model.config.context_length, model.config.d_model) ) self.assertEqual(output.shape , lowerCAmelCase ) _lowercase =torch.tensor( [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]] , device=lowerCAmelCase ) self.assertTrue(torch.allclose(output[0, :3, :3] , lowerCAmelCase , atol=lowerCAmelCase ) ) def A__ ( self ) -> Optional[int]: '''simple docstring''' _lowercase =AutoformerForPrediction.from_pretrained('huggingface/autoformer-tourism-monthly' ).to(lowerCAmelCase ) _lowercase =prepare_batch('val-batch.pt' ) with torch.no_grad(): _lowercase =model.generate( static_categorical_features=batch['static_categorical_features'] , past_time_features=batch['past_time_features'] , past_values=batch['past_values'] , future_time_features=batch['future_time_features'] , past_observed_mask=batch['past_observed_mask'] , ) _lowercase =torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length) ) self.assertEqual(outputs.sequences.shape , lowerCAmelCase ) _lowercase =torch.tensor([3130.6763, 4056.5293, 7053.0786] , device=lowerCAmelCase ) _lowercase =outputs.sequences.mean(dim=1 ) self.assertTrue(torch.allclose(mean_prediction[0, -3:] , lowerCAmelCase , rtol=1e-1 ) )
205
0
"""simple docstring""" import sys import webbrowser import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": print("""Googling.....""") A_ : Dict ="""https://www.google.com/search?q=""" + """ """.join(sys.argv[1:]) A_ : Any =requests.get(url, headers={"""UserAgent""": UserAgent().random}) # res.raise_for_status() with open("""project1a.html""", """wb""") as out_file: # only for knowing the class for data in res.iter_content(1_0_0_0_0): out_file.write(data) A_ : Any =BeautifulSoup(res.text, """html.parser""") A_ : List[str] =list(soup.select(""".eZt8xd"""))[:5] print(len(links)) for link in links: if link.text == "Maps": webbrowser.open(link.get("""href""")) else: webbrowser.open(f'https://google.com{link.get("href")}')
357
"""simple docstring""" import argparse import torch from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_from_original_stable_diffusion_ckpt if __name__ == "__main__": A_ : List[Any] =argparse.ArgumentParser() parser.add_argument( """--checkpoint_path""", default=None, type=str, required=True, help="""Path to the checkpoint to convert.""" ) # !wget https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml parser.add_argument( """--original_config_file""", default=None, type=str, help="""The YAML config file corresponding to the original architecture.""", ) parser.add_argument( """--num_in_channels""", default=None, type=int, help="""The number of input channels. If `None` number of input channels will be automatically inferred.""", ) parser.add_argument( """--scheduler_type""", default="""pndm""", type=str, help="""Type of scheduler to use. Should be one of ['pndm', 'lms', 'ddim', 'euler', 'euler-ancestral', 'dpm']""", ) parser.add_argument( """--pipeline_type""", default=None, type=str, help=( """The pipeline type. One of 'FrozenOpenCLIPEmbedder', 'FrozenCLIPEmbedder', 'PaintByExample'""" """. If `None` pipeline will be automatically inferred.""" ), ) parser.add_argument( """--image_size""", default=None, type=int, help=( """The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2""" """ Base. Use 768 for Stable Diffusion v2.""" ), ) parser.add_argument( """--prediction_type""", default=None, type=str, help=( """The prediction type that the model was trained on. Use 'epsilon' for Stable Diffusion v1.X and Stable""" """ Diffusion v2 Base. Use 'v_prediction' for Stable Diffusion v2.""" ), ) parser.add_argument( """--extract_ema""", action="""store_true""", help=( """Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights""" """ or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield""" """ higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning.""" ), ) parser.add_argument( """--upcast_attention""", action="""store_true""", help=( """Whether the attention computation should always be upcasted. This is necessary when running stable""" """ diffusion 2.1.""" ), ) parser.add_argument( """--from_safetensors""", action="""store_true""", help="""If `--checkpoint_path` is in `safetensors` format, load checkpoint with safetensors instead of PyTorch.""", ) parser.add_argument( """--to_safetensors""", action="""store_true""", help="""Whether to store pipeline in safetensors format or not.""", ) parser.add_argument("""--dump_path""", default=None, type=str, required=True, help="""Path to the output model.""") parser.add_argument("""--device""", type=str, help="""Device to use (e.g. cpu, cuda:0, cuda:1, etc.)""") parser.add_argument( """--stable_unclip""", type=str, default=None, required=False, help="""Set if this is a stable unCLIP model. One of 'txt2img' or 'img2img'.""", ) parser.add_argument( """--stable_unclip_prior""", type=str, default=None, required=False, help="""Set if this is a stable unCLIP txt2img model. Selects which prior to use. If `--stable_unclip` is set to `txt2img`, the karlo prior (https://huggingface.co/kakaobrain/karlo-v1-alpha/tree/main/prior) is selected by default.""", ) parser.add_argument( """--clip_stats_path""", type=str, help="""Path to the clip stats file. Only required if the stable unclip model's config specifies `model.params.noise_aug_config.params.clip_stats_path`.""", required=False, ) parser.add_argument( """--controlnet""", action="""store_true""", default=None, help="""Set flag if this is a controlnet checkpoint.""" ) parser.add_argument("""--half""", action="""store_true""", help="""Save weights in half precision.""") parser.add_argument( """--vae_path""", type=str, default=None, required=False, help="""Set to a path, hub id to an already converted vae to not convert it again.""", ) A_ : List[str] =parser.parse_args() A_ : Any =download_from_original_stable_diffusion_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, prediction_type=args.prediction_type, model_type=args.pipeline_type, extract_ema=args.extract_ema, scheduler_type=args.scheduler_type, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, stable_unclip=args.stable_unclip, stable_unclip_prior=args.stable_unclip_prior, clip_stats_path=args.clip_stats_path, controlnet=args.controlnet, vae_path=args.vae_path, ) if args.half: pipe.to(torch_dtype=torch.floataa) if args.controlnet: # only save the controlnet model pipe.controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors) else: pipe.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
80
0
from math import sqrt import numpy as np from sympy import symbols # Coefficient # Speed of light (m/s) SCREAMING_SNAKE_CASE :Optional[Any] = 2_9979_2458 # Symbols SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE :Tuple = symbols('ct x y z') def UpperCAmelCase ( a_ ) -> float: """simple docstring""" if velocity > c: raise ValueError("Speed must not exceed light speed 299,792,458 [m/s]!" ) elif velocity < 1: # Usually the speed should be much higher than 1 (c order of magnitude) raise ValueError("Speed must be greater than or equal to 1!" ) return velocity / c def UpperCAmelCase ( a_ ) -> float: """simple docstring""" return 1 / sqrt(1 - beta(a_ ) ** 2 ) def UpperCAmelCase ( a_ ) -> np.ndarray: """simple docstring""" return np.array( [ [gamma(a_ ), -gamma(a_ ) * beta(a_ ), 0, 0], [-gamma(a_ ) * beta(a_ ), gamma(a_ ), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], ] ) def UpperCAmelCase ( a_ , a_ = None ) -> np.ndarray: """simple docstring""" if event is None: __A = np.array([ct, x, y, z] ) # Symbolic four vector else: event[0] *= c # x0 is ct (speed of light * time) return transformation_matrix(a_ ) @ event if __name__ == "__main__": import doctest doctest.testmod() # Example of symbolic vector: SCREAMING_SNAKE_CASE :Any = transform(2997_9245) print('Example of four vector: ') print(f'''ct\' = {four_vector[0]}''') print(f'''x\' = {four_vector[1]}''') print(f'''y\' = {four_vector[2]}''') print(f'''z\' = {four_vector[3]}''') # Substitute symbols with numerical values SCREAMING_SNAKE_CASE :Union[str, Any] = {ct: c, x: 1, y: 1, z: 1} SCREAMING_SNAKE_CASE :Optional[Any] = [four_vector[i].subs(sub_dict) for i in range(4)] print(f'''\n{numerical_vector}''')
15
"""simple docstring""" import json import os from typing import Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = {'vocab_file': 'vocab.json'} _snake_case = { 'vocab_file': { 'mgp-str': 'https://huggingface.co/alibaba-damo/mgp-str-base/blob/main/vocab.json', } } _snake_case = {'mgp-str': 27} class UpperCamelCase ( snake_case_ ): UpperCamelCase : List[str] = VOCAB_FILES_NAMES UpperCamelCase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self : Optional[int] , UpperCAmelCase__ : List[str] , UpperCAmelCase__ : List[Any]="[GO]" , UpperCAmelCase__ : Tuple="[GO]" , UpperCAmelCase__ : Optional[int]="[s]" , UpperCAmelCase__ : int="[GO]" , **UpperCAmelCase__ : Dict ) -> int: super().__init__( unk_token=UpperCAmelCase__ , bos_token=UpperCAmelCase__ , eos_token=UpperCAmelCase__ , pad_token=UpperCAmelCase__ , **UpperCAmelCase__ , ) with open(UpperCAmelCase__ , encoding="""utf-8""" ) as vocab_handle: _a : int = json.load(UpperCAmelCase__ ) _a : Optional[int] = {v: k for k, v in self.vocab.items()} @property def _lowercase ( self : Dict ) -> Union[str, Any]: return len(self.vocab ) def _lowercase ( self : Union[str, Any] ) -> str: return dict(self.vocab , **self.added_tokens_encoder ) def _lowercase ( self : Dict , UpperCAmelCase__ : str ) -> Union[str, Any]: _a : Tuple = [] for s in text: char_tokens.extend(UpperCAmelCase__ ) return char_tokens def _lowercase ( self : List[Any] , UpperCAmelCase__ : str ) -> Dict: return self.vocab.get(UpperCAmelCase__ , self.vocab.get(self.unk_token ) ) def _lowercase ( self : Optional[int] , UpperCAmelCase__ : Tuple ) -> List[Any]: return self.decoder.get(UpperCAmelCase__ ) def _lowercase ( self : Optional[int] , UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(UpperCAmelCase__ ): logger.error("""Vocabulary path ({}) should be a directory""".format(UpperCAmelCase__ ) ) return _a : Tuple = os.path.join( UpperCAmelCase__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) with open(UpperCAmelCase__ , """w""" , encoding="""utf-8""" ) as f: f.write(json.dumps(self.vocab , indent=2 , sort_keys=UpperCAmelCase__ , ensure_ascii=UpperCAmelCase__ ) + """\n""" ) return (vocab_file,)
294
0
"""simple docstring""" import collections.abc from typing import Optional, Tuple, Union import torch import torch.utils.checkpoint from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_poolformer import PoolFormerConfig SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) # General docstring SCREAMING_SNAKE_CASE__ = "PoolFormerConfig" # Base docstring SCREAMING_SNAKE_CASE__ = "sail/poolformer_s12" SCREAMING_SNAKE_CASE__ = [1, 512, 7, 7] # Image classification docstring SCREAMING_SNAKE_CASE__ = "sail/poolformer_s12" SCREAMING_SNAKE_CASE__ = "tabby, tabby cat" SCREAMING_SNAKE_CASE__ = [ "sail/poolformer_s12", # See all PoolFormer models at https://huggingface.co/models?filter=poolformer ] def lowerCAmelCase__ ( _UpperCamelCase : Tuple , _UpperCamelCase : float = 0.0 , _UpperCamelCase : bool = False ) -> Optional[Any]: """simple docstring""" if drop_prob == 0.0 or not training: return input snake_case = 1 - drop_prob snake_case = (input.shape[0],) + (1,) * (input.ndim - 1) # work with diff dim tensors, not just 2D ConvNets snake_case = keep_prob + torch.rand(_UpperCamelCase , dtype=input.dtype , device=input.device ) random_tensor.floor_() # binarize snake_case = input.div(_UpperCamelCase ) * random_tensor return output class lowerCAmelCase_ ( nn.Module ): """simple docstring""" def __init__( self , lowerCAmelCase = None ): """simple docstring""" super().__init__() snake_case = drop_prob def snake_case ( self , lowerCAmelCase ): """simple docstring""" return drop_path(lowerCAmelCase , self.drop_prob , self.training ) def snake_case ( self ): """simple docstring""" return "p={}".format(self.drop_prob ) class lowerCAmelCase_ ( nn.Module ): """simple docstring""" def __init__( self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase=None ): """simple docstring""" super().__init__() snake_case = patch_size if isinstance(lowerCAmelCase , collections.abc.Iterable ) else (patch_size, patch_size) snake_case = stride if isinstance(lowerCAmelCase , collections.abc.Iterable ) else (stride, stride) snake_case = padding if isinstance(lowerCAmelCase , collections.abc.Iterable ) else (padding, padding) snake_case = nn.Convad(lowerCAmelCase , lowerCAmelCase , kernel_size=lowerCAmelCase , stride=lowerCAmelCase , padding=lowerCAmelCase ) snake_case = norm_layer(lowerCAmelCase ) if norm_layer else nn.Identity() def snake_case ( self , lowerCAmelCase ): """simple docstring""" snake_case = self.projection(lowerCAmelCase ) snake_case = self.norm(lowerCAmelCase ) return embeddings class lowerCAmelCase_ ( nn.GroupNorm ): """simple docstring""" def __init__( self , lowerCAmelCase , **lowerCAmelCase ): """simple docstring""" super().__init__(1 , lowerCAmelCase , **lowerCAmelCase ) class lowerCAmelCase_ ( nn.Module ): """simple docstring""" def __init__( self , lowerCAmelCase ): """simple docstring""" super().__init__() snake_case = nn.AvgPoolad(lowerCAmelCase , stride=1 , padding=pool_size // 2 , count_include_pad=lowerCAmelCase ) def snake_case ( self , lowerCAmelCase ): """simple docstring""" return self.pool(lowerCAmelCase ) - hidden_states class lowerCAmelCase_ ( nn.Module ): """simple docstring""" def __init__( self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ): """simple docstring""" super().__init__() snake_case = nn.Convad(lowerCAmelCase , lowerCAmelCase , 1 ) snake_case = nn.Convad(lowerCAmelCase , lowerCAmelCase , 1 ) snake_case = PoolFormerDropPath(lowerCAmelCase ) if isinstance(config.hidden_act , lowerCAmelCase ): snake_case = ACTaFN[config.hidden_act] else: snake_case = config.hidden_act def snake_case ( self , lowerCAmelCase ): """simple docstring""" snake_case = self.conva(lowerCAmelCase ) snake_case = self.act_fn(lowerCAmelCase ) snake_case = self.drop(lowerCAmelCase ) snake_case = self.conva(lowerCAmelCase ) snake_case = self.drop(lowerCAmelCase ) return hidden_states class lowerCAmelCase_ ( nn.Module ): """simple docstring""" def __init__( self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ): """simple docstring""" super().__init__() snake_case = PoolFormerPooling(lowerCAmelCase ) snake_case = PoolFormerOutput(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) snake_case = PoolFormerGroupNorm(lowerCAmelCase ) snake_case = PoolFormerGroupNorm(lowerCAmelCase ) # Useful for training neural nets snake_case = PoolFormerDropPath(lowerCAmelCase ) if drop_path > 0.0 else nn.Identity() snake_case = config.use_layer_scale if config.use_layer_scale: snake_case = nn.Parameter( config.layer_scale_init_value * torch.ones((lowerCAmelCase) ) , requires_grad=lowerCAmelCase ) snake_case = nn.Parameter( config.layer_scale_init_value * torch.ones((lowerCAmelCase) ) , requires_grad=lowerCAmelCase ) def snake_case ( self , lowerCAmelCase ): """simple docstring""" if self.use_layer_scale: snake_case = self.pooling(self.before_norm(lowerCAmelCase ) ) snake_case = self.layer_scale_a.unsqueeze(-1 ).unsqueeze(-1 ) * pooling_output # First residual connection snake_case = hidden_states + self.drop_path(lowerCAmelCase ) snake_case = () snake_case = self.output(self.after_norm(lowerCAmelCase ) ) snake_case = self.layer_scale_a.unsqueeze(-1 ).unsqueeze(-1 ) * layer_output # Second residual connection snake_case = hidden_states + self.drop_path(lowerCAmelCase ) snake_case = (output,) + outputs return outputs else: snake_case = self.drop_path(self.pooling(self.before_norm(lowerCAmelCase ) ) ) # First residual connection snake_case = pooling_output + hidden_states snake_case = () # Second residual connection inside the PoolFormerOutput block snake_case = self.drop_path(self.output(self.after_norm(lowerCAmelCase ) ) ) snake_case = hidden_states + layer_output snake_case = (output,) + outputs return outputs class lowerCAmelCase_ ( nn.Module ): """simple docstring""" def __init__( self , lowerCAmelCase ): """simple docstring""" super().__init__() snake_case = config # stochastic depth decay rule snake_case = [x.item() for x in torch.linspace(0 , config.drop_path_rate , sum(config.depths ) )] # patch embeddings snake_case = [] for i in range(config.num_encoder_blocks ): embeddings.append( PoolFormerEmbeddings( patch_size=config.patch_sizes[i] , stride=config.strides[i] , padding=config.padding[i] , num_channels=config.num_channels if i == 0 else config.hidden_sizes[i - 1] , hidden_size=config.hidden_sizes[i] , ) ) snake_case = nn.ModuleList(lowerCAmelCase ) # Transformer blocks snake_case = [] snake_case = 0 for i in range(config.num_encoder_blocks ): # each block consists of layers snake_case = [] if i != 0: cur += config.depths[i - 1] for j in range(config.depths[i] ): layers.append( PoolFormerLayer( lowerCAmelCase , num_channels=config.hidden_sizes[i] , pool_size=config.pool_size , hidden_size=config.hidden_sizes[i] , intermediate_size=int(config.hidden_sizes[i] * config.mlp_ratio ) , drop_path=dpr[cur + j] , ) ) blocks.append(nn.ModuleList(lowerCAmelCase ) ) snake_case = nn.ModuleList(lowerCAmelCase ) def snake_case ( self , lowerCAmelCase , lowerCAmelCase=False , lowerCAmelCase=True ): """simple docstring""" snake_case = () if output_hidden_states else None snake_case = pixel_values for idx, layers in enumerate(zip(self.patch_embeddings , self.block ) ): snake_case ,snake_case = layers # Get patch embeddings from hidden_states snake_case = embedding_layer(lowerCAmelCase ) # Send the embeddings through the blocks for _, blk in enumerate(lowerCAmelCase ): snake_case = blk(lowerCAmelCase ) snake_case = layer_outputs[0] if output_hidden_states: snake_case = all_hidden_states + (hidden_states,) if not return_dict: return tuple(v for v in [hidden_states, all_hidden_states] if v is not None ) return BaseModelOutputWithNoAttention(last_hidden_state=lowerCAmelCase , hidden_states=lowerCAmelCase ) class lowerCAmelCase_ ( lowerCAmelCase ): """simple docstring""" _lowerCAmelCase : str = PoolFormerConfig _lowerCAmelCase : List[Any] = """poolformer""" _lowerCAmelCase : Tuple = """pixel_values""" _lowerCAmelCase : Any = True def snake_case ( self , lowerCAmelCase ): """simple docstring""" if isinstance(lowerCAmelCase , (nn.Linear, nn.Convad) ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() elif isinstance(lowerCAmelCase , nn.LayerNorm ): module.bias.data.zero_() module.weight.data.fill_(1.0 ) def snake_case ( self , lowerCAmelCase , lowerCAmelCase=False ): """simple docstring""" if isinstance(lowerCAmelCase , lowerCAmelCase ): snake_case = value SCREAMING_SNAKE_CASE__ = R"\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n\n Parameters:\n config ([`PoolFormerConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n" SCREAMING_SNAKE_CASE__ = R"\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`PoolFormerImageProcessor.__call__`] for details.\n" @add_start_docstrings( """The bare PoolFormer Model transformer outputting raw hidden-states without any specific head on top.""" , lowerCAmelCase , ) class lowerCAmelCase_ ( lowerCAmelCase ): """simple docstring""" def __init__( self , lowerCAmelCase ): """simple docstring""" super().__init__(lowerCAmelCase ) snake_case = config snake_case = PoolFormerEncoder(lowerCAmelCase ) # Initialize weights and apply final processing self.post_init() def snake_case ( self ): """simple docstring""" return self.embeddings.patch_embeddings @add_start_docstrings_to_model_forward(lowerCAmelCase ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=lowerCAmelCase , config_class=_CONFIG_FOR_DOC , modality='vision' , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def snake_case ( self , lowerCAmelCase = None , lowerCAmelCase = None , lowerCAmelCase = None , ): """simple docstring""" snake_case = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) snake_case = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError('You have to specify pixel_values' ) snake_case = self.encoder( lowerCAmelCase , output_hidden_states=lowerCAmelCase , return_dict=lowerCAmelCase , ) snake_case = encoder_outputs[0] if not return_dict: return (sequence_output, None) + encoder_outputs[1:] return BaseModelOutputWithNoAttention( last_hidden_state=lowerCAmelCase , hidden_states=encoder_outputs.hidden_states , ) class lowerCAmelCase_ ( nn.Module ): """simple docstring""" def __init__( self , lowerCAmelCase ): """simple docstring""" super().__init__() snake_case = nn.Linear(config.hidden_size , config.hidden_size ) def snake_case ( self , lowerCAmelCase ): """simple docstring""" snake_case = self.dense(lowerCAmelCase ) return output @add_start_docstrings( """ PoolFormer Model transformer with an image classification head on top """ , lowerCAmelCase , ) class lowerCAmelCase_ ( lowerCAmelCase ): """simple docstring""" def __init__( self , lowerCAmelCase ): """simple docstring""" super().__init__(lowerCAmelCase ) snake_case = config.num_labels snake_case = PoolFormerModel(lowerCAmelCase ) # Final norm snake_case = PoolFormerGroupNorm(config.hidden_sizes[-1] ) # Classifier head snake_case = ( nn.Linear(config.hidden_sizes[-1] , config.num_labels ) if config.num_labels > 0 else nn.Identity() ) # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(lowerCAmelCase ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=lowerCAmelCase , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def snake_case ( self , lowerCAmelCase = None , lowerCAmelCase = None , lowerCAmelCase = None , lowerCAmelCase = None , ): """simple docstring""" snake_case = return_dict if return_dict is not None else self.config.use_return_dict snake_case = self.poolformer( lowerCAmelCase , output_hidden_states=lowerCAmelCase , return_dict=lowerCAmelCase , ) snake_case = outputs[0] snake_case = self.classifier(self.norm(lowerCAmelCase ).mean([-2, -1] ) ) snake_case = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: snake_case = 'regression' elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): snake_case = 'single_label_classification' else: snake_case = 'multi_label_classification' if self.config.problem_type == "regression": snake_case = MSELoss() if self.num_labels == 1: snake_case = loss_fct(logits.squeeze() , labels.squeeze() ) else: snake_case = loss_fct(lowerCAmelCase , lowerCAmelCase ) elif self.config.problem_type == "single_label_classification": snake_case = CrossEntropyLoss() snake_case = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": snake_case = BCEWithLogitsLoss() snake_case = loss_fct(lowerCAmelCase , lowerCAmelCase ) if not return_dict: snake_case = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention(loss=lowerCAmelCase , logits=lowerCAmelCase , hidden_states=outputs.hidden_states )
356
"""simple docstring""" from __future__ import annotations import unittest from transformers import DebertaVaConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, TFDebertaVaModel, ) class lowerCAmelCase_ : """simple docstring""" def __init__( self , lowerCAmelCase , lowerCAmelCase=13 , lowerCAmelCase=7 , lowerCAmelCase=True , lowerCAmelCase=True , lowerCAmelCase=True , lowerCAmelCase=True , lowerCAmelCase=99 , lowerCAmelCase=32 , lowerCAmelCase=2 , lowerCAmelCase=4 , lowerCAmelCase=37 , lowerCAmelCase="gelu" , lowerCAmelCase=0.1 , lowerCAmelCase=0.1 , lowerCAmelCase=5_12 , lowerCAmelCase=16 , lowerCAmelCase=2 , lowerCAmelCase=0.02 , lowerCAmelCase=False , lowerCAmelCase=True , lowerCAmelCase="None" , lowerCAmelCase=3 , lowerCAmelCase=4 , lowerCAmelCase=None , ): """simple docstring""" snake_case = parent snake_case = batch_size snake_case = seq_length snake_case = is_training snake_case = use_input_mask snake_case = use_token_type_ids snake_case = use_labels snake_case = vocab_size snake_case = hidden_size snake_case = num_hidden_layers snake_case = num_attention_heads snake_case = intermediate_size snake_case = hidden_act snake_case = hidden_dropout_prob snake_case = attention_probs_dropout_prob snake_case = max_position_embeddings snake_case = type_vocab_size snake_case = type_sequence_label_size snake_case = initializer_range snake_case = num_labels snake_case = num_choices snake_case = relative_attention snake_case = position_biased_input snake_case = pos_att_type snake_case = scope def snake_case ( self ): """simple docstring""" snake_case = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) snake_case = None if self.use_input_mask: snake_case = random_attention_mask([self.batch_size, self.seq_length] ) snake_case = None if self.use_token_type_ids: snake_case = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) snake_case = None snake_case = None snake_case = None if self.use_labels: snake_case = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) snake_case = DebertaVaConfig( vocab_size=self.vocab_size , 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 , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , relative_attention=self.relative_attention , position_biased_input=self.position_biased_input , initializer_range=self.initializer_range , return_dict=lowerCAmelCase , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def snake_case ( self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ): """simple docstring""" snake_case = TFDebertaVaModel(config=lowerCAmelCase ) snake_case = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} snake_case = [input_ids, input_mask] snake_case = model(lowerCAmelCase ) snake_case = model(lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def snake_case ( self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ): """simple docstring""" snake_case = TFDebertaVaForMaskedLM(config=lowerCAmelCase ) snake_case = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } snake_case = model(lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def snake_case ( self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ): """simple docstring""" snake_case = self.num_labels snake_case = TFDebertaVaForSequenceClassification(config=lowerCAmelCase ) snake_case = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } snake_case = model(lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def snake_case ( self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ): """simple docstring""" snake_case = self.num_labels snake_case = TFDebertaVaForTokenClassification(config=lowerCAmelCase ) snake_case = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } snake_case = model(lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def snake_case ( self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ): """simple docstring""" snake_case = TFDebertaVaForQuestionAnswering(config=lowerCAmelCase ) snake_case = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } snake_case = model(lowerCAmelCase ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def snake_case ( self ): """simple docstring""" snake_case = self.prepare_config_and_inputs() ( ( snake_case ) ,( snake_case ) ,( snake_case ) ,( snake_case ) ,( snake_case ) ,( snake_case ) ,( snake_case ) , ) = config_and_inputs snake_case = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class lowerCAmelCase_ ( lowerCAmelCase , lowerCAmelCase , unittest.TestCase ): """simple docstring""" _lowerCAmelCase : Optional[int] = ( ( TFDebertaVaModel, TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, ) if is_tf_available() else () ) _lowerCAmelCase : Any = ( { """feature-extraction""": TFDebertaVaModel, """fill-mask""": TFDebertaVaForMaskedLM, """question-answering""": TFDebertaVaForQuestionAnswering, """text-classification""": TFDebertaVaForSequenceClassification, """token-classification""": TFDebertaVaForTokenClassification, """zero-shot""": TFDebertaVaForSequenceClassification, } if is_tf_available() else {} ) _lowerCAmelCase : Optional[Any] = False _lowerCAmelCase : List[Any] = False def snake_case ( self ): """simple docstring""" snake_case = TFDebertaVaModelTester(self ) snake_case = ConfigTester(self , config_class=lowerCAmelCase , hidden_size=37 ) def snake_case ( self ): """simple docstring""" self.config_tester.run_common_tests() def snake_case ( self ): """simple docstring""" snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase ) def snake_case ( self ): """simple docstring""" snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase ) def snake_case ( self ): """simple docstring""" snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase ) def snake_case ( self ): """simple docstring""" snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase ) def snake_case ( self ): """simple docstring""" snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase ) @slow def snake_case ( self ): """simple docstring""" snake_case = TFDebertaVaModel.from_pretrained('kamalkraj/deberta-v2-xlarge' ) self.assertIsNotNone(lowerCAmelCase ) @require_tf class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" @unittest.skip(reason='Model not available yet' ) def snake_case ( self ): """simple docstring""" pass @slow def snake_case ( self ): """simple docstring""" snake_case = TFDebertaVaModel.from_pretrained('kamalkraj/deberta-v2-xlarge' ) snake_case = tf.constant([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]] ) snake_case = tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) snake_case = model(lowerCAmelCase , attention_mask=lowerCAmelCase )[0] snake_case = tf.constant( [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]] ) tf.debugging.assert_near(output[:, 1:4, 1:4] , lowerCAmelCase , atol=1E-4 )
149
0